sxgbe_ethtool.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  1. /* 10G controller driver for Samsung SoCs
  2. *
  3. * Copyright (C) 2013 Samsung Electronics Co., Ltd.
  4. * http://www.samsung.com
  5. *
  6. * Author: Siva Reddy Kallam <siva.kallam@samsung.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  13. #include <linux/clk.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/kernel.h>
  16. #include <linux/netdevice.h>
  17. #include <linux/net_tstamp.h>
  18. #include <linux/phy.h>
  19. #include <linux/ptp_clock_kernel.h>
  20. #include "sxgbe_common.h"
  21. #include "sxgbe_reg.h"
  22. #include "sxgbe_dma.h"
  23. struct sxgbe_stats {
  24. char stat_string[ETH_GSTRING_LEN];
  25. int sizeof_stat;
  26. int stat_offset;
  27. };
  28. #define SXGBE_STAT(m) \
  29. { \
  30. #m, \
  31. FIELD_SIZEOF(struct sxgbe_extra_stats, m), \
  32. offsetof(struct sxgbe_priv_data, xstats.m) \
  33. }
  34. static const struct sxgbe_stats sxgbe_gstrings_stats[] = {
  35. /* TX/RX IRQ events */
  36. SXGBE_STAT(tx_process_stopped_irq),
  37. SXGBE_STAT(tx_ctxt_desc_err),
  38. SXGBE_STAT(tx_threshold),
  39. SXGBE_STAT(rx_threshold),
  40. SXGBE_STAT(tx_pkt_n),
  41. SXGBE_STAT(rx_pkt_n),
  42. SXGBE_STAT(normal_irq_n),
  43. SXGBE_STAT(tx_normal_irq_n),
  44. SXGBE_STAT(rx_normal_irq_n),
  45. SXGBE_STAT(napi_poll),
  46. SXGBE_STAT(tx_clean),
  47. SXGBE_STAT(tx_reset_ic_bit),
  48. SXGBE_STAT(rx_process_stopped_irq),
  49. SXGBE_STAT(rx_underflow_irq),
  50. /* Bus access errors */
  51. SXGBE_STAT(fatal_bus_error_irq),
  52. SXGBE_STAT(tx_read_transfer_err),
  53. SXGBE_STAT(tx_write_transfer_err),
  54. SXGBE_STAT(tx_desc_access_err),
  55. SXGBE_STAT(tx_buffer_access_err),
  56. SXGBE_STAT(tx_data_transfer_err),
  57. SXGBE_STAT(rx_read_transfer_err),
  58. SXGBE_STAT(rx_write_transfer_err),
  59. SXGBE_STAT(rx_desc_access_err),
  60. SXGBE_STAT(rx_buffer_access_err),
  61. SXGBE_STAT(rx_data_transfer_err),
  62. /* EEE-LPI stats */
  63. SXGBE_STAT(tx_lpi_entry_n),
  64. SXGBE_STAT(tx_lpi_exit_n),
  65. SXGBE_STAT(rx_lpi_entry_n),
  66. SXGBE_STAT(rx_lpi_exit_n),
  67. SXGBE_STAT(eee_wakeup_error_n),
  68. /* RX specific */
  69. /* L2 error */
  70. SXGBE_STAT(rx_code_gmii_err),
  71. SXGBE_STAT(rx_watchdog_err),
  72. SXGBE_STAT(rx_crc_err),
  73. SXGBE_STAT(rx_gaint_pkt_err),
  74. SXGBE_STAT(ip_hdr_err),
  75. SXGBE_STAT(ip_payload_err),
  76. SXGBE_STAT(overflow_error),
  77. /* L2 Pkt type */
  78. SXGBE_STAT(len_pkt),
  79. SXGBE_STAT(mac_ctl_pkt),
  80. SXGBE_STAT(dcb_ctl_pkt),
  81. SXGBE_STAT(arp_pkt),
  82. SXGBE_STAT(oam_pkt),
  83. SXGBE_STAT(untag_okt),
  84. SXGBE_STAT(other_pkt),
  85. SXGBE_STAT(svlan_tag_pkt),
  86. SXGBE_STAT(cvlan_tag_pkt),
  87. SXGBE_STAT(dvlan_ocvlan_icvlan_pkt),
  88. SXGBE_STAT(dvlan_osvlan_isvlan_pkt),
  89. SXGBE_STAT(dvlan_osvlan_icvlan_pkt),
  90. SXGBE_STAT(dvan_ocvlan_icvlan_pkt),
  91. /* L3/L4 Pkt type */
  92. SXGBE_STAT(not_ip_pkt),
  93. SXGBE_STAT(ip4_tcp_pkt),
  94. SXGBE_STAT(ip4_udp_pkt),
  95. SXGBE_STAT(ip4_icmp_pkt),
  96. SXGBE_STAT(ip4_unknown_pkt),
  97. SXGBE_STAT(ip6_tcp_pkt),
  98. SXGBE_STAT(ip6_udp_pkt),
  99. SXGBE_STAT(ip6_icmp_pkt),
  100. SXGBE_STAT(ip6_unknown_pkt),
  101. /* Filter specific */
  102. SXGBE_STAT(vlan_filter_match),
  103. SXGBE_STAT(sa_filter_fail),
  104. SXGBE_STAT(da_filter_fail),
  105. SXGBE_STAT(hash_filter_pass),
  106. SXGBE_STAT(l3_filter_match),
  107. SXGBE_STAT(l4_filter_match),
  108. /* RX context specific */
  109. SXGBE_STAT(timestamp_dropped),
  110. SXGBE_STAT(rx_msg_type_no_ptp),
  111. SXGBE_STAT(rx_ptp_type_sync),
  112. SXGBE_STAT(rx_ptp_type_follow_up),
  113. SXGBE_STAT(rx_ptp_type_delay_req),
  114. SXGBE_STAT(rx_ptp_type_delay_resp),
  115. SXGBE_STAT(rx_ptp_type_pdelay_req),
  116. SXGBE_STAT(rx_ptp_type_pdelay_resp),
  117. SXGBE_STAT(rx_ptp_type_pdelay_follow_up),
  118. SXGBE_STAT(rx_ptp_announce),
  119. SXGBE_STAT(rx_ptp_mgmt),
  120. SXGBE_STAT(rx_ptp_signal),
  121. SXGBE_STAT(rx_ptp_resv_msg_type),
  122. };
  123. #define SXGBE_STATS_LEN ARRAY_SIZE(sxgbe_gstrings_stats)
  124. static int sxgbe_get_eee(struct net_device *dev,
  125. struct ethtool_eee *edata)
  126. {
  127. struct sxgbe_priv_data *priv = netdev_priv(dev);
  128. if (!priv->hw_cap.eee)
  129. return -EOPNOTSUPP;
  130. edata->eee_enabled = priv->eee_enabled;
  131. edata->eee_active = priv->eee_active;
  132. edata->tx_lpi_timer = priv->tx_lpi_timer;
  133. return phy_ethtool_get_eee(priv->phydev, edata);
  134. }
  135. static int sxgbe_set_eee(struct net_device *dev,
  136. struct ethtool_eee *edata)
  137. {
  138. struct sxgbe_priv_data *priv = netdev_priv(dev);
  139. priv->eee_enabled = edata->eee_enabled;
  140. if (!priv->eee_enabled) {
  141. sxgbe_disable_eee_mode(priv);
  142. } else {
  143. /* We are asking for enabling the EEE but it is safe
  144. * to verify all by invoking the eee_init function.
  145. * In case of failure it will return an error.
  146. */
  147. priv->eee_enabled = sxgbe_eee_init(priv);
  148. if (!priv->eee_enabled)
  149. return -EOPNOTSUPP;
  150. /* Do not change tx_lpi_timer in case of failure */
  151. priv->tx_lpi_timer = edata->tx_lpi_timer;
  152. }
  153. return phy_ethtool_set_eee(priv->phydev, edata);
  154. }
  155. static void sxgbe_getdrvinfo(struct net_device *dev,
  156. struct ethtool_drvinfo *info)
  157. {
  158. strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
  159. strlcpy(info->version, DRV_VERSION, sizeof(info->version));
  160. }
  161. static int sxgbe_getsettings(struct net_device *dev,
  162. struct ethtool_cmd *cmd)
  163. {
  164. struct sxgbe_priv_data *priv = netdev_priv(dev);
  165. if (priv->phydev)
  166. return phy_ethtool_gset(priv->phydev, cmd);
  167. return -EOPNOTSUPP;
  168. }
  169. static int sxgbe_setsettings(struct net_device *dev, struct ethtool_cmd *cmd)
  170. {
  171. struct sxgbe_priv_data *priv = netdev_priv(dev);
  172. if (priv->phydev)
  173. return phy_ethtool_sset(priv->phydev, cmd);
  174. return -EOPNOTSUPP;
  175. }
  176. static u32 sxgbe_getmsglevel(struct net_device *dev)
  177. {
  178. struct sxgbe_priv_data *priv = netdev_priv(dev);
  179. return priv->msg_enable;
  180. }
  181. static void sxgbe_setmsglevel(struct net_device *dev, u32 level)
  182. {
  183. struct sxgbe_priv_data *priv = netdev_priv(dev);
  184. priv->msg_enable = level;
  185. }
  186. static void sxgbe_get_strings(struct net_device *dev, u32 stringset, u8 *data)
  187. {
  188. int i;
  189. u8 *p = data;
  190. switch (stringset) {
  191. case ETH_SS_STATS:
  192. for (i = 0; i < SXGBE_STATS_LEN; i++) {
  193. memcpy(p, sxgbe_gstrings_stats[i].stat_string,
  194. ETH_GSTRING_LEN);
  195. p += ETH_GSTRING_LEN;
  196. }
  197. break;
  198. default:
  199. WARN_ON(1);
  200. break;
  201. }
  202. }
  203. static int sxgbe_get_sset_count(struct net_device *netdev, int sset)
  204. {
  205. int len;
  206. switch (sset) {
  207. case ETH_SS_STATS:
  208. len = SXGBE_STATS_LEN;
  209. return len;
  210. default:
  211. return -EINVAL;
  212. }
  213. }
  214. static void sxgbe_get_ethtool_stats(struct net_device *dev,
  215. struct ethtool_stats *dummy, u64 *data)
  216. {
  217. struct sxgbe_priv_data *priv = netdev_priv(dev);
  218. int i;
  219. char *p;
  220. if (priv->eee_enabled) {
  221. int val = phy_get_eee_err(priv->phydev);
  222. if (val)
  223. priv->xstats.eee_wakeup_error_n = val;
  224. }
  225. for (i = 0; i < SXGBE_STATS_LEN; i++) {
  226. p = (char *)priv + sxgbe_gstrings_stats[i].stat_offset;
  227. data[i] = (sxgbe_gstrings_stats[i].sizeof_stat == sizeof(u64))
  228. ? (*(u64 *)p) : (*(u32 *)p);
  229. }
  230. }
  231. static void sxgbe_get_channels(struct net_device *dev,
  232. struct ethtool_channels *channel)
  233. {
  234. channel->max_rx = SXGBE_MAX_RX_CHANNELS;
  235. channel->max_tx = SXGBE_MAX_TX_CHANNELS;
  236. channel->rx_count = SXGBE_RX_QUEUES;
  237. channel->tx_count = SXGBE_TX_QUEUES;
  238. }
  239. static u32 sxgbe_riwt2usec(u32 riwt, struct sxgbe_priv_data *priv)
  240. {
  241. unsigned long clk = clk_get_rate(priv->sxgbe_clk);
  242. if (!clk)
  243. return 0;
  244. return (riwt * 256) / (clk / 1000000);
  245. }
  246. static u32 sxgbe_usec2riwt(u32 usec, struct sxgbe_priv_data *priv)
  247. {
  248. unsigned long clk = clk_get_rate(priv->sxgbe_clk);
  249. if (!clk)
  250. return 0;
  251. return (usec * (clk / 1000000)) / 256;
  252. }
  253. static int sxgbe_get_coalesce(struct net_device *dev,
  254. struct ethtool_coalesce *ec)
  255. {
  256. struct sxgbe_priv_data *priv = netdev_priv(dev);
  257. if (priv->use_riwt)
  258. ec->rx_coalesce_usecs = sxgbe_riwt2usec(priv->rx_riwt, priv);
  259. return 0;
  260. }
  261. static int sxgbe_set_coalesce(struct net_device *dev,
  262. struct ethtool_coalesce *ec)
  263. {
  264. struct sxgbe_priv_data *priv = netdev_priv(dev);
  265. unsigned int rx_riwt;
  266. if (!ec->rx_coalesce_usecs)
  267. return -EINVAL;
  268. rx_riwt = sxgbe_usec2riwt(ec->rx_coalesce_usecs, priv);
  269. if ((rx_riwt > SXGBE_MAX_DMA_RIWT) || (rx_riwt < SXGBE_MIN_DMA_RIWT))
  270. return -EINVAL;
  271. else if (!priv->use_riwt)
  272. return -EOPNOTSUPP;
  273. priv->rx_riwt = rx_riwt;
  274. priv->hw->dma->rx_watchdog(priv->ioaddr, priv->rx_riwt);
  275. return 0;
  276. }
  277. static int sxgbe_get_rss_hash_opts(struct sxgbe_priv_data *priv,
  278. struct ethtool_rxnfc *cmd)
  279. {
  280. cmd->data = 0;
  281. /* Report default options for RSS on sxgbe */
  282. switch (cmd->flow_type) {
  283. case TCP_V4_FLOW:
  284. case UDP_V4_FLOW:
  285. cmd->data |= RXH_L4_B_0_1 | RXH_L4_B_2_3;
  286. case SCTP_V4_FLOW:
  287. case AH_ESP_V4_FLOW:
  288. case AH_V4_FLOW:
  289. case ESP_V4_FLOW:
  290. case IPV4_FLOW:
  291. cmd->data |= RXH_IP_SRC | RXH_IP_DST;
  292. break;
  293. case TCP_V6_FLOW:
  294. case UDP_V6_FLOW:
  295. cmd->data |= RXH_L4_B_0_1 | RXH_L4_B_2_3;
  296. case SCTP_V6_FLOW:
  297. case AH_ESP_V6_FLOW:
  298. case AH_V6_FLOW:
  299. case ESP_V6_FLOW:
  300. case IPV6_FLOW:
  301. cmd->data |= RXH_IP_SRC | RXH_IP_DST;
  302. break;
  303. default:
  304. return -EINVAL;
  305. }
  306. return 0;
  307. }
  308. static int sxgbe_get_rxnfc(struct net_device *dev, struct ethtool_rxnfc *cmd,
  309. u32 *rule_locs)
  310. {
  311. struct sxgbe_priv_data *priv = netdev_priv(dev);
  312. int ret = -EOPNOTSUPP;
  313. switch (cmd->cmd) {
  314. case ETHTOOL_GRXFH:
  315. ret = sxgbe_get_rss_hash_opts(priv, cmd);
  316. break;
  317. default:
  318. break;
  319. }
  320. return ret;
  321. }
  322. static int sxgbe_set_rss_hash_opt(struct sxgbe_priv_data *priv,
  323. struct ethtool_rxnfc *cmd)
  324. {
  325. u32 reg_val = 0;
  326. /* RSS does not support anything other than hashing
  327. * to queues on src and dst IPs and ports
  328. */
  329. if (cmd->data & ~(RXH_IP_SRC | RXH_IP_DST |
  330. RXH_L4_B_0_1 | RXH_L4_B_2_3))
  331. return -EINVAL;
  332. switch (cmd->flow_type) {
  333. case TCP_V4_FLOW:
  334. case TCP_V6_FLOW:
  335. if (!(cmd->data & RXH_IP_SRC) ||
  336. !(cmd->data & RXH_IP_DST) ||
  337. !(cmd->data & RXH_L4_B_0_1) ||
  338. !(cmd->data & RXH_L4_B_2_3))
  339. return -EINVAL;
  340. reg_val = SXGBE_CORE_RSS_CTL_TCP4TE;
  341. break;
  342. case UDP_V4_FLOW:
  343. case UDP_V6_FLOW:
  344. if (!(cmd->data & RXH_IP_SRC) ||
  345. !(cmd->data & RXH_IP_DST) ||
  346. !(cmd->data & RXH_L4_B_0_1) ||
  347. !(cmd->data & RXH_L4_B_2_3))
  348. return -EINVAL;
  349. reg_val = SXGBE_CORE_RSS_CTL_UDP4TE;
  350. break;
  351. case SCTP_V4_FLOW:
  352. case AH_ESP_V4_FLOW:
  353. case AH_V4_FLOW:
  354. case ESP_V4_FLOW:
  355. case AH_ESP_V6_FLOW:
  356. case AH_V6_FLOW:
  357. case ESP_V6_FLOW:
  358. case SCTP_V6_FLOW:
  359. case IPV4_FLOW:
  360. case IPV6_FLOW:
  361. if (!(cmd->data & RXH_IP_SRC) ||
  362. !(cmd->data & RXH_IP_DST) ||
  363. (cmd->data & RXH_L4_B_0_1) ||
  364. (cmd->data & RXH_L4_B_2_3))
  365. return -EINVAL;
  366. reg_val = SXGBE_CORE_RSS_CTL_IP2TE;
  367. break;
  368. default:
  369. return -EINVAL;
  370. }
  371. /* Read SXGBE RSS control register and update */
  372. reg_val |= readl(priv->ioaddr + SXGBE_CORE_RSS_CTL_REG);
  373. writel(reg_val, priv->ioaddr + SXGBE_CORE_RSS_CTL_REG);
  374. readl(priv->ioaddr + SXGBE_CORE_RSS_CTL_REG);
  375. return 0;
  376. }
  377. static int sxgbe_set_rxnfc(struct net_device *dev, struct ethtool_rxnfc *cmd)
  378. {
  379. struct sxgbe_priv_data *priv = netdev_priv(dev);
  380. int ret = -EOPNOTSUPP;
  381. switch (cmd->cmd) {
  382. case ETHTOOL_SRXFH:
  383. ret = sxgbe_set_rss_hash_opt(priv, cmd);
  384. break;
  385. default:
  386. break;
  387. }
  388. return ret;
  389. }
  390. static void sxgbe_get_regs(struct net_device *dev,
  391. struct ethtool_regs *regs, void *space)
  392. {
  393. struct sxgbe_priv_data *priv = netdev_priv(dev);
  394. u32 *reg_space = (u32 *)space;
  395. int reg_offset;
  396. int reg_ix = 0;
  397. void __iomem *ioaddr = priv->ioaddr;
  398. memset(reg_space, 0x0, REG_SPACE_SIZE);
  399. /* MAC registers */
  400. for (reg_offset = START_MAC_REG_OFFSET;
  401. reg_offset <= MAX_MAC_REG_OFFSET; reg_offset += 4) {
  402. reg_space[reg_ix] = readl(ioaddr + reg_offset);
  403. reg_ix++;
  404. }
  405. /* MTL registers */
  406. for (reg_offset = START_MTL_REG_OFFSET;
  407. reg_offset <= MAX_MTL_REG_OFFSET; reg_offset += 4) {
  408. reg_space[reg_ix] = readl(ioaddr + reg_offset);
  409. reg_ix++;
  410. }
  411. /* DMA registers */
  412. for (reg_offset = START_DMA_REG_OFFSET;
  413. reg_offset <= MAX_DMA_REG_OFFSET; reg_offset += 4) {
  414. reg_space[reg_ix] = readl(ioaddr + reg_offset);
  415. reg_ix++;
  416. }
  417. BUG_ON(reg_ix * 4 > REG_SPACE_SIZE);
  418. }
  419. static int sxgbe_get_regs_len(struct net_device *dev)
  420. {
  421. return REG_SPACE_SIZE;
  422. }
  423. static const struct ethtool_ops sxgbe_ethtool_ops = {
  424. .get_drvinfo = sxgbe_getdrvinfo,
  425. .get_settings = sxgbe_getsettings,
  426. .set_settings = sxgbe_setsettings,
  427. .get_msglevel = sxgbe_getmsglevel,
  428. .set_msglevel = sxgbe_setmsglevel,
  429. .get_link = ethtool_op_get_link,
  430. .get_strings = sxgbe_get_strings,
  431. .get_ethtool_stats = sxgbe_get_ethtool_stats,
  432. .get_sset_count = sxgbe_get_sset_count,
  433. .get_channels = sxgbe_get_channels,
  434. .get_coalesce = sxgbe_get_coalesce,
  435. .set_coalesce = sxgbe_set_coalesce,
  436. .get_rxnfc = sxgbe_get_rxnfc,
  437. .set_rxnfc = sxgbe_set_rxnfc,
  438. .get_regs = sxgbe_get_regs,
  439. .get_regs_len = sxgbe_get_regs_len,
  440. .get_eee = sxgbe_get_eee,
  441. .set_eee = sxgbe_set_eee,
  442. };
  443. void sxgbe_set_ethtool_ops(struct net_device *netdev)
  444. {
  445. netdev->ethtool_ops = &sxgbe_ethtool_ops;
  446. }