pch_gbe_ethtool.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  1. /*
  2. * Copyright (C) 1999 - 2010 Intel Corporation.
  3. * Copyright (C) 2010 OKI SEMICONDUCTOR Co., LTD.
  4. *
  5. * This code was derived from the Intel e1000e Linux driver.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; version 2 of the License.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "pch_gbe.h"
  20. #include "pch_gbe_api.h"
  21. /**
  22. * pch_gbe_stats - Stats item information
  23. */
  24. struct pch_gbe_stats {
  25. char string[ETH_GSTRING_LEN];
  26. size_t size;
  27. size_t offset;
  28. };
  29. #define PCH_GBE_STAT(m) \
  30. { \
  31. .string = #m, \
  32. .size = FIELD_SIZEOF(struct pch_gbe_hw_stats, m), \
  33. .offset = offsetof(struct pch_gbe_hw_stats, m), \
  34. }
  35. /**
  36. * pch_gbe_gstrings_stats - ethtool information status name list
  37. */
  38. static const struct pch_gbe_stats pch_gbe_gstrings_stats[] = {
  39. PCH_GBE_STAT(rx_packets),
  40. PCH_GBE_STAT(tx_packets),
  41. PCH_GBE_STAT(rx_bytes),
  42. PCH_GBE_STAT(tx_bytes),
  43. PCH_GBE_STAT(rx_errors),
  44. PCH_GBE_STAT(tx_errors),
  45. PCH_GBE_STAT(rx_dropped),
  46. PCH_GBE_STAT(tx_dropped),
  47. PCH_GBE_STAT(multicast),
  48. PCH_GBE_STAT(collisions),
  49. PCH_GBE_STAT(rx_crc_errors),
  50. PCH_GBE_STAT(rx_frame_errors),
  51. PCH_GBE_STAT(rx_alloc_buff_failed),
  52. PCH_GBE_STAT(tx_length_errors),
  53. PCH_GBE_STAT(tx_aborted_errors),
  54. PCH_GBE_STAT(tx_carrier_errors),
  55. PCH_GBE_STAT(tx_timeout_count),
  56. PCH_GBE_STAT(tx_restart_count),
  57. PCH_GBE_STAT(intr_rx_dsc_empty_count),
  58. PCH_GBE_STAT(intr_rx_frame_err_count),
  59. PCH_GBE_STAT(intr_rx_fifo_err_count),
  60. PCH_GBE_STAT(intr_rx_dma_err_count),
  61. PCH_GBE_STAT(intr_tx_fifo_err_count),
  62. PCH_GBE_STAT(intr_tx_dma_err_count),
  63. PCH_GBE_STAT(intr_tcpip_err_count)
  64. };
  65. #define PCH_GBE_QUEUE_STATS_LEN 0
  66. #define PCH_GBE_GLOBAL_STATS_LEN ARRAY_SIZE(pch_gbe_gstrings_stats)
  67. #define PCH_GBE_STATS_LEN (PCH_GBE_GLOBAL_STATS_LEN + PCH_GBE_QUEUE_STATS_LEN)
  68. #define PCH_GBE_MAC_REGS_LEN (sizeof(struct pch_gbe_regs) / 4)
  69. #define PCH_GBE_REGS_LEN (PCH_GBE_MAC_REGS_LEN + PCH_GBE_PHY_REGS_LEN)
  70. /**
  71. * pch_gbe_get_settings - Get device-specific settings
  72. * @netdev: Network interface device structure
  73. * @ecmd: Ethtool command
  74. * Returns:
  75. * 0: Successful.
  76. * Negative value: Failed.
  77. */
  78. static int pch_gbe_get_settings(struct net_device *netdev,
  79. struct ethtool_cmd *ecmd)
  80. {
  81. struct pch_gbe_adapter *adapter = netdev_priv(netdev);
  82. int ret;
  83. ret = mii_ethtool_gset(&adapter->mii, ecmd);
  84. ecmd->supported &= ~(SUPPORTED_TP | SUPPORTED_1000baseT_Half);
  85. ecmd->advertising &= ~(ADVERTISED_TP | ADVERTISED_1000baseT_Half);
  86. if (!netif_carrier_ok(adapter->netdev))
  87. ethtool_cmd_speed_set(ecmd, SPEED_UNKNOWN);
  88. return ret;
  89. }
  90. /**
  91. * pch_gbe_set_settings - Set device-specific settings
  92. * @netdev: Network interface device structure
  93. * @ecmd: Ethtool command
  94. * Returns:
  95. * 0: Successful.
  96. * Negative value: Failed.
  97. */
  98. static int pch_gbe_set_settings(struct net_device *netdev,
  99. struct ethtool_cmd *ecmd)
  100. {
  101. struct pch_gbe_adapter *adapter = netdev_priv(netdev);
  102. struct pch_gbe_hw *hw = &adapter->hw;
  103. u32 speed = ethtool_cmd_speed(ecmd);
  104. int ret;
  105. pch_gbe_hal_write_phy_reg(hw, MII_BMCR, BMCR_RESET);
  106. /* when set_settings() is called with a ethtool_cmd previously
  107. * filled by get_settings() on a down link, speed is -1: */
  108. if (speed == UINT_MAX) {
  109. speed = SPEED_1000;
  110. ethtool_cmd_speed_set(ecmd, speed);
  111. ecmd->duplex = DUPLEX_FULL;
  112. }
  113. ret = mii_ethtool_sset(&adapter->mii, ecmd);
  114. if (ret) {
  115. netdev_err(netdev, "Error: mii_ethtool_sset\n");
  116. return ret;
  117. }
  118. hw->mac.link_speed = speed;
  119. hw->mac.link_duplex = ecmd->duplex;
  120. hw->phy.autoneg_advertised = ecmd->advertising;
  121. hw->mac.autoneg = ecmd->autoneg;
  122. /* reset the link */
  123. if (netif_running(adapter->netdev)) {
  124. pch_gbe_down(adapter);
  125. ret = pch_gbe_up(adapter);
  126. } else {
  127. pch_gbe_reset(adapter);
  128. }
  129. return ret;
  130. }
  131. /**
  132. * pch_gbe_get_regs_len - Report the size of device registers
  133. * @netdev: Network interface device structure
  134. * Returns: the size of device registers.
  135. */
  136. static int pch_gbe_get_regs_len(struct net_device *netdev)
  137. {
  138. return PCH_GBE_REGS_LEN * (int)sizeof(u32);
  139. }
  140. /**
  141. * pch_gbe_get_drvinfo - Report driver information
  142. * @netdev: Network interface device structure
  143. * @drvinfo: Driver information structure
  144. */
  145. static void pch_gbe_get_drvinfo(struct net_device *netdev,
  146. struct ethtool_drvinfo *drvinfo)
  147. {
  148. struct pch_gbe_adapter *adapter = netdev_priv(netdev);
  149. strlcpy(drvinfo->driver, KBUILD_MODNAME, sizeof(drvinfo->driver));
  150. strlcpy(drvinfo->version, pch_driver_version, sizeof(drvinfo->version));
  151. strlcpy(drvinfo->bus_info, pci_name(adapter->pdev),
  152. sizeof(drvinfo->bus_info));
  153. }
  154. /**
  155. * pch_gbe_get_regs - Get device registers
  156. * @netdev: Network interface device structure
  157. * @regs: Ethtool register structure
  158. * @p: Buffer pointer of read device register date
  159. */
  160. static void pch_gbe_get_regs(struct net_device *netdev,
  161. struct ethtool_regs *regs, void *p)
  162. {
  163. struct pch_gbe_adapter *adapter = netdev_priv(netdev);
  164. struct pch_gbe_hw *hw = &adapter->hw;
  165. struct pci_dev *pdev = adapter->pdev;
  166. u32 *regs_buff = p;
  167. u16 i, tmp;
  168. regs->version = 0x1000000 | (__u32)pdev->revision << 16 | pdev->device;
  169. for (i = 0; i < PCH_GBE_MAC_REGS_LEN; i++)
  170. *regs_buff++ = ioread32(&hw->reg->INT_ST + i);
  171. /* PHY register */
  172. for (i = 0; i < PCH_GBE_PHY_REGS_LEN; i++) {
  173. pch_gbe_hal_read_phy_reg(&adapter->hw, i, &tmp);
  174. *regs_buff++ = tmp;
  175. }
  176. }
  177. /**
  178. * pch_gbe_get_wol - Report whether Wake-on-Lan is enabled
  179. * @netdev: Network interface device structure
  180. * @wol: Wake-on-Lan information
  181. */
  182. static void pch_gbe_get_wol(struct net_device *netdev,
  183. struct ethtool_wolinfo *wol)
  184. {
  185. struct pch_gbe_adapter *adapter = netdev_priv(netdev);
  186. wol->supported = WAKE_UCAST | WAKE_MCAST | WAKE_BCAST | WAKE_MAGIC;
  187. wol->wolopts = 0;
  188. if ((adapter->wake_up_evt & PCH_GBE_WLC_IND))
  189. wol->wolopts |= WAKE_UCAST;
  190. if ((adapter->wake_up_evt & PCH_GBE_WLC_MLT))
  191. wol->wolopts |= WAKE_MCAST;
  192. if ((adapter->wake_up_evt & PCH_GBE_WLC_BR))
  193. wol->wolopts |= WAKE_BCAST;
  194. if ((adapter->wake_up_evt & PCH_GBE_WLC_MP))
  195. wol->wolopts |= WAKE_MAGIC;
  196. }
  197. /**
  198. * pch_gbe_set_wol - Turn Wake-on-Lan on or off
  199. * @netdev: Network interface device structure
  200. * @wol: Pointer of wake-on-Lan information straucture
  201. * Returns:
  202. * 0: Successful.
  203. * Negative value: Failed.
  204. */
  205. static int pch_gbe_set_wol(struct net_device *netdev,
  206. struct ethtool_wolinfo *wol)
  207. {
  208. struct pch_gbe_adapter *adapter = netdev_priv(netdev);
  209. if ((wol->wolopts & (WAKE_PHY | WAKE_ARP | WAKE_MAGICSECURE)))
  210. return -EOPNOTSUPP;
  211. /* these settings will always override what we currently have */
  212. adapter->wake_up_evt = 0;
  213. if ((wol->wolopts & WAKE_UCAST))
  214. adapter->wake_up_evt |= PCH_GBE_WLC_IND;
  215. if ((wol->wolopts & WAKE_MCAST))
  216. adapter->wake_up_evt |= PCH_GBE_WLC_MLT;
  217. if ((wol->wolopts & WAKE_BCAST))
  218. adapter->wake_up_evt |= PCH_GBE_WLC_BR;
  219. if ((wol->wolopts & WAKE_MAGIC))
  220. adapter->wake_up_evt |= PCH_GBE_WLC_MP;
  221. return 0;
  222. }
  223. /**
  224. * pch_gbe_nway_reset - Restart autonegotiation
  225. * @netdev: Network interface device structure
  226. * Returns:
  227. * 0: Successful.
  228. * Negative value: Failed.
  229. */
  230. static int pch_gbe_nway_reset(struct net_device *netdev)
  231. {
  232. struct pch_gbe_adapter *adapter = netdev_priv(netdev);
  233. return mii_nway_restart(&adapter->mii);
  234. }
  235. /**
  236. * pch_gbe_get_ringparam - Report ring sizes
  237. * @netdev: Network interface device structure
  238. * @ring: Ring param structure
  239. */
  240. static void pch_gbe_get_ringparam(struct net_device *netdev,
  241. struct ethtool_ringparam *ring)
  242. {
  243. struct pch_gbe_adapter *adapter = netdev_priv(netdev);
  244. struct pch_gbe_tx_ring *txdr = adapter->tx_ring;
  245. struct pch_gbe_rx_ring *rxdr = adapter->rx_ring;
  246. ring->rx_max_pending = PCH_GBE_MAX_RXD;
  247. ring->tx_max_pending = PCH_GBE_MAX_TXD;
  248. ring->rx_pending = rxdr->count;
  249. ring->tx_pending = txdr->count;
  250. }
  251. /**
  252. * pch_gbe_set_ringparam - Set ring sizes
  253. * @netdev: Network interface device structure
  254. * @ring: Ring param structure
  255. * Returns
  256. * 0: Successful.
  257. * Negative value: Failed.
  258. */
  259. static int pch_gbe_set_ringparam(struct net_device *netdev,
  260. struct ethtool_ringparam *ring)
  261. {
  262. struct pch_gbe_adapter *adapter = netdev_priv(netdev);
  263. struct pch_gbe_tx_ring *txdr, *tx_old;
  264. struct pch_gbe_rx_ring *rxdr, *rx_old;
  265. int tx_ring_size, rx_ring_size;
  266. int err = 0;
  267. if ((ring->rx_mini_pending) || (ring->rx_jumbo_pending))
  268. return -EINVAL;
  269. tx_ring_size = (int)sizeof(struct pch_gbe_tx_ring);
  270. rx_ring_size = (int)sizeof(struct pch_gbe_rx_ring);
  271. if ((netif_running(adapter->netdev)))
  272. pch_gbe_down(adapter);
  273. tx_old = adapter->tx_ring;
  274. rx_old = adapter->rx_ring;
  275. txdr = kzalloc(tx_ring_size, GFP_KERNEL);
  276. if (!txdr) {
  277. err = -ENOMEM;
  278. goto err_alloc_tx;
  279. }
  280. rxdr = kzalloc(rx_ring_size, GFP_KERNEL);
  281. if (!rxdr) {
  282. err = -ENOMEM;
  283. goto err_alloc_rx;
  284. }
  285. adapter->tx_ring = txdr;
  286. adapter->rx_ring = rxdr;
  287. rxdr->count =
  288. clamp_val(ring->rx_pending, PCH_GBE_MIN_RXD, PCH_GBE_MAX_RXD);
  289. rxdr->count = roundup(rxdr->count, PCH_GBE_RX_DESC_MULTIPLE);
  290. txdr->count =
  291. clamp_val(ring->tx_pending, PCH_GBE_MIN_RXD, PCH_GBE_MAX_RXD);
  292. txdr->count = roundup(txdr->count, PCH_GBE_TX_DESC_MULTIPLE);
  293. if ((netif_running(adapter->netdev))) {
  294. /* Try to get new resources before deleting old */
  295. err = pch_gbe_setup_rx_resources(adapter, adapter->rx_ring);
  296. if (err)
  297. goto err_setup_rx;
  298. err = pch_gbe_setup_tx_resources(adapter, adapter->tx_ring);
  299. if (err)
  300. goto err_setup_tx;
  301. /* save the new, restore the old in order to free it,
  302. * then restore the new back again */
  303. #ifdef RINGFREE
  304. adapter->rx_ring = rx_old;
  305. adapter->tx_ring = tx_old;
  306. pch_gbe_free_rx_resources(adapter, adapter->rx_ring);
  307. pch_gbe_free_tx_resources(adapter, adapter->tx_ring);
  308. kfree(tx_old);
  309. kfree(rx_old);
  310. adapter->rx_ring = rxdr;
  311. adapter->tx_ring = txdr;
  312. #else
  313. pch_gbe_free_rx_resources(adapter, rx_old);
  314. pch_gbe_free_tx_resources(adapter, tx_old);
  315. kfree(tx_old);
  316. kfree(rx_old);
  317. adapter->rx_ring = rxdr;
  318. adapter->tx_ring = txdr;
  319. #endif
  320. err = pch_gbe_up(adapter);
  321. }
  322. return err;
  323. err_setup_tx:
  324. pch_gbe_free_rx_resources(adapter, adapter->rx_ring);
  325. err_setup_rx:
  326. adapter->rx_ring = rx_old;
  327. adapter->tx_ring = tx_old;
  328. kfree(rxdr);
  329. err_alloc_rx:
  330. kfree(txdr);
  331. err_alloc_tx:
  332. if (netif_running(adapter->netdev))
  333. pch_gbe_up(adapter);
  334. return err;
  335. }
  336. /**
  337. * pch_gbe_get_pauseparam - Report pause parameters
  338. * @netdev: Network interface device structure
  339. * @pause: Pause parameters structure
  340. */
  341. static void pch_gbe_get_pauseparam(struct net_device *netdev,
  342. struct ethtool_pauseparam *pause)
  343. {
  344. struct pch_gbe_adapter *adapter = netdev_priv(netdev);
  345. struct pch_gbe_hw *hw = &adapter->hw;
  346. pause->autoneg =
  347. ((hw->mac.fc_autoneg) ? AUTONEG_ENABLE : AUTONEG_DISABLE);
  348. if (hw->mac.fc == PCH_GBE_FC_RX_PAUSE) {
  349. pause->rx_pause = 1;
  350. } else if (hw->mac.fc == PCH_GBE_FC_TX_PAUSE) {
  351. pause->tx_pause = 1;
  352. } else if (hw->mac.fc == PCH_GBE_FC_FULL) {
  353. pause->rx_pause = 1;
  354. pause->tx_pause = 1;
  355. }
  356. }
  357. /**
  358. * pch_gbe_set_pauseparam - Set pause parameters
  359. * @netdev: Network interface device structure
  360. * @pause: Pause parameters structure
  361. * Returns:
  362. * 0: Successful.
  363. * Negative value: Failed.
  364. */
  365. static int pch_gbe_set_pauseparam(struct net_device *netdev,
  366. struct ethtool_pauseparam *pause)
  367. {
  368. struct pch_gbe_adapter *adapter = netdev_priv(netdev);
  369. struct pch_gbe_hw *hw = &adapter->hw;
  370. int ret = 0;
  371. hw->mac.fc_autoneg = pause->autoneg;
  372. if ((pause->rx_pause) && (pause->tx_pause))
  373. hw->mac.fc = PCH_GBE_FC_FULL;
  374. else if ((pause->rx_pause) && (!pause->tx_pause))
  375. hw->mac.fc = PCH_GBE_FC_RX_PAUSE;
  376. else if ((!pause->rx_pause) && (pause->tx_pause))
  377. hw->mac.fc = PCH_GBE_FC_TX_PAUSE;
  378. else if ((!pause->rx_pause) && (!pause->tx_pause))
  379. hw->mac.fc = PCH_GBE_FC_NONE;
  380. if (hw->mac.fc_autoneg == AUTONEG_ENABLE) {
  381. if ((netif_running(adapter->netdev))) {
  382. pch_gbe_down(adapter);
  383. ret = pch_gbe_up(adapter);
  384. } else {
  385. pch_gbe_reset(adapter);
  386. }
  387. } else {
  388. ret = pch_gbe_mac_force_mac_fc(hw);
  389. }
  390. return ret;
  391. }
  392. /**
  393. * pch_gbe_get_strings - Return a set of strings that describe the requested
  394. * objects
  395. * @netdev: Network interface device structure
  396. * @stringset: Select the stringset. [ETH_SS_TEST] [ETH_SS_STATS]
  397. * @data: Pointer of read string data.
  398. */
  399. static void pch_gbe_get_strings(struct net_device *netdev, u32 stringset,
  400. u8 *data)
  401. {
  402. u8 *p = data;
  403. int i;
  404. switch (stringset) {
  405. case (u32) ETH_SS_STATS:
  406. for (i = 0; i < PCH_GBE_GLOBAL_STATS_LEN; i++) {
  407. memcpy(p, pch_gbe_gstrings_stats[i].string,
  408. ETH_GSTRING_LEN);
  409. p += ETH_GSTRING_LEN;
  410. }
  411. break;
  412. }
  413. }
  414. /**
  415. * pch_gbe_get_ethtool_stats - Return statistics about the device
  416. * @netdev: Network interface device structure
  417. * @stats: Ethtool statue structure
  418. * @data: Pointer of read status area
  419. */
  420. static void pch_gbe_get_ethtool_stats(struct net_device *netdev,
  421. struct ethtool_stats *stats, u64 *data)
  422. {
  423. struct pch_gbe_adapter *adapter = netdev_priv(netdev);
  424. int i;
  425. const struct pch_gbe_stats *gstats = pch_gbe_gstrings_stats;
  426. char *hw_stats = (char *)&adapter->stats;
  427. pch_gbe_update_stats(adapter);
  428. for (i = 0; i < PCH_GBE_GLOBAL_STATS_LEN; i++) {
  429. char *p = hw_stats + gstats->offset;
  430. data[i] = gstats->size == sizeof(u64) ? *(u64 *)p:(*(u32 *)p);
  431. gstats++;
  432. }
  433. }
  434. static int pch_gbe_get_sset_count(struct net_device *netdev, int sset)
  435. {
  436. switch (sset) {
  437. case ETH_SS_STATS:
  438. return PCH_GBE_STATS_LEN;
  439. default:
  440. return -EOPNOTSUPP;
  441. }
  442. }
  443. static const struct ethtool_ops pch_gbe_ethtool_ops = {
  444. .get_settings = pch_gbe_get_settings,
  445. .set_settings = pch_gbe_set_settings,
  446. .get_drvinfo = pch_gbe_get_drvinfo,
  447. .get_regs_len = pch_gbe_get_regs_len,
  448. .get_regs = pch_gbe_get_regs,
  449. .get_wol = pch_gbe_get_wol,
  450. .set_wol = pch_gbe_set_wol,
  451. .nway_reset = pch_gbe_nway_reset,
  452. .get_link = ethtool_op_get_link,
  453. .get_ringparam = pch_gbe_get_ringparam,
  454. .set_ringparam = pch_gbe_set_ringparam,
  455. .get_pauseparam = pch_gbe_get_pauseparam,
  456. .set_pauseparam = pch_gbe_set_pauseparam,
  457. .get_strings = pch_gbe_get_strings,
  458. .get_ethtool_stats = pch_gbe_get_ethtool_stats,
  459. .get_sset_count = pch_gbe_get_sset_count,
  460. };
  461. void pch_gbe_set_ethtool_ops(struct net_device *netdev)
  462. {
  463. netdev->ethtool_ops = &pch_gbe_ethtool_ops;
  464. }