pch_gbe_ethtool.c 14 KB

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