pch_gbe_ethtool.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  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. ecmd->duplex = DUPLEX_FULL;
  112. }
  113. ret = mii_ethtool_sset(&adapter->mii, ecmd);
  114. if (ret) {
  115. pr_err("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. drvinfo->regdump_len = pch_gbe_get_regs_len(netdev);
  154. }
  155. /**
  156. * pch_gbe_get_regs - Get device registers
  157. * @netdev: Network interface device structure
  158. * @regs: Ethtool register structure
  159. * @p: Buffer pointer of read device register date
  160. */
  161. static void pch_gbe_get_regs(struct net_device *netdev,
  162. struct ethtool_regs *regs, void *p)
  163. {
  164. struct pch_gbe_adapter *adapter = netdev_priv(netdev);
  165. struct pch_gbe_hw *hw = &adapter->hw;
  166. struct pci_dev *pdev = adapter->pdev;
  167. u32 *regs_buff = p;
  168. u16 i, tmp;
  169. regs->version = 0x1000000 | (__u32)pdev->revision << 16 | pdev->device;
  170. for (i = 0; i < PCH_GBE_MAC_REGS_LEN; i++)
  171. *regs_buff++ = ioread32(&hw->reg->INT_ST + i);
  172. /* PHY register */
  173. for (i = 0; i < PCH_GBE_PHY_REGS_LEN; i++) {
  174. pch_gbe_hal_read_phy_reg(&adapter->hw, i, &tmp);
  175. *regs_buff++ = tmp;
  176. }
  177. }
  178. /**
  179. * pch_gbe_get_wol - Report whether Wake-on-Lan is enabled
  180. * @netdev: Network interface device structure
  181. * @wol: Wake-on-Lan information
  182. */
  183. static void pch_gbe_get_wol(struct net_device *netdev,
  184. struct ethtool_wolinfo *wol)
  185. {
  186. struct pch_gbe_adapter *adapter = netdev_priv(netdev);
  187. wol->supported = WAKE_UCAST | WAKE_MCAST | WAKE_BCAST | WAKE_MAGIC;
  188. wol->wolopts = 0;
  189. if ((adapter->wake_up_evt & PCH_GBE_WLC_IND))
  190. wol->wolopts |= WAKE_UCAST;
  191. if ((adapter->wake_up_evt & PCH_GBE_WLC_MLT))
  192. wol->wolopts |= WAKE_MCAST;
  193. if ((adapter->wake_up_evt & PCH_GBE_WLC_BR))
  194. wol->wolopts |= WAKE_BCAST;
  195. if ((adapter->wake_up_evt & PCH_GBE_WLC_MP))
  196. wol->wolopts |= WAKE_MAGIC;
  197. }
  198. /**
  199. * pch_gbe_set_wol - Turn Wake-on-Lan on or off
  200. * @netdev: Network interface device structure
  201. * @wol: Pointer of wake-on-Lan information straucture
  202. * Returns:
  203. * 0: Successful.
  204. * Negative value: Failed.
  205. */
  206. static int pch_gbe_set_wol(struct net_device *netdev,
  207. struct ethtool_wolinfo *wol)
  208. {
  209. struct pch_gbe_adapter *adapter = netdev_priv(netdev);
  210. if ((wol->wolopts & (WAKE_PHY | WAKE_ARP | WAKE_MAGICSECURE)))
  211. return -EOPNOTSUPP;
  212. /* these settings will always override what we currently have */
  213. adapter->wake_up_evt = 0;
  214. if ((wol->wolopts & WAKE_UCAST))
  215. adapter->wake_up_evt |= PCH_GBE_WLC_IND;
  216. if ((wol->wolopts & WAKE_MCAST))
  217. adapter->wake_up_evt |= PCH_GBE_WLC_MLT;
  218. if ((wol->wolopts & WAKE_BCAST))
  219. adapter->wake_up_evt |= PCH_GBE_WLC_BR;
  220. if ((wol->wolopts & WAKE_MAGIC))
  221. adapter->wake_up_evt |= PCH_GBE_WLC_MP;
  222. return 0;
  223. }
  224. /**
  225. * pch_gbe_nway_reset - Restart autonegotiation
  226. * @netdev: Network interface device structure
  227. * Returns:
  228. * 0: Successful.
  229. * Negative value: Failed.
  230. */
  231. static int pch_gbe_nway_reset(struct net_device *netdev)
  232. {
  233. struct pch_gbe_adapter *adapter = netdev_priv(netdev);
  234. return mii_nway_restart(&adapter->mii);
  235. }
  236. /**
  237. * pch_gbe_get_ringparam - Report ring sizes
  238. * @netdev: Network interface device structure
  239. * @ring: Ring param structure
  240. */
  241. static void pch_gbe_get_ringparam(struct net_device *netdev,
  242. struct ethtool_ringparam *ring)
  243. {
  244. struct pch_gbe_adapter *adapter = netdev_priv(netdev);
  245. struct pch_gbe_tx_ring *txdr = adapter->tx_ring;
  246. struct pch_gbe_rx_ring *rxdr = adapter->rx_ring;
  247. ring->rx_max_pending = PCH_GBE_MAX_RXD;
  248. ring->tx_max_pending = PCH_GBE_MAX_TXD;
  249. ring->rx_pending = rxdr->count;
  250. ring->tx_pending = txdr->count;
  251. }
  252. /**
  253. * pch_gbe_set_ringparam - Set ring sizes
  254. * @netdev: Network interface device structure
  255. * @ring: Ring param structure
  256. * Returns
  257. * 0: Successful.
  258. * Negative value: Failed.
  259. */
  260. static int pch_gbe_set_ringparam(struct net_device *netdev,
  261. struct ethtool_ringparam *ring)
  262. {
  263. struct pch_gbe_adapter *adapter = netdev_priv(netdev);
  264. struct pch_gbe_tx_ring *txdr, *tx_old;
  265. struct pch_gbe_rx_ring *rxdr, *rx_old;
  266. int tx_ring_size, rx_ring_size;
  267. int err = 0;
  268. if ((ring->rx_mini_pending) || (ring->rx_jumbo_pending))
  269. return -EINVAL;
  270. tx_ring_size = (int)sizeof(struct pch_gbe_tx_ring);
  271. rx_ring_size = (int)sizeof(struct pch_gbe_rx_ring);
  272. if ((netif_running(adapter->netdev)))
  273. pch_gbe_down(adapter);
  274. tx_old = adapter->tx_ring;
  275. rx_old = adapter->rx_ring;
  276. txdr = kzalloc(tx_ring_size, GFP_KERNEL);
  277. if (!txdr) {
  278. err = -ENOMEM;
  279. goto err_alloc_tx;
  280. }
  281. rxdr = kzalloc(rx_ring_size, GFP_KERNEL);
  282. if (!rxdr) {
  283. err = -ENOMEM;
  284. goto err_alloc_rx;
  285. }
  286. adapter->tx_ring = txdr;
  287. adapter->rx_ring = rxdr;
  288. rxdr->count =
  289. clamp_val(ring->rx_pending, PCH_GBE_MIN_RXD, PCH_GBE_MAX_RXD);
  290. rxdr->count = roundup(rxdr->count, PCH_GBE_RX_DESC_MULTIPLE);
  291. txdr->count =
  292. clamp_val(ring->tx_pending, PCH_GBE_MIN_RXD, PCH_GBE_MAX_RXD);
  293. txdr->count = roundup(txdr->count, PCH_GBE_TX_DESC_MULTIPLE);
  294. if ((netif_running(adapter->netdev))) {
  295. /* Try to get new resources before deleting old */
  296. err = pch_gbe_setup_rx_resources(adapter, adapter->rx_ring);
  297. if (err)
  298. goto err_setup_rx;
  299. err = pch_gbe_setup_tx_resources(adapter, adapter->tx_ring);
  300. if (err)
  301. goto err_setup_tx;
  302. /* save the new, restore the old in order to free it,
  303. * then restore the new back again */
  304. #ifdef RINGFREE
  305. adapter->rx_ring = rx_old;
  306. adapter->tx_ring = tx_old;
  307. pch_gbe_free_rx_resources(adapter, adapter->rx_ring);
  308. pch_gbe_free_tx_resources(adapter, adapter->tx_ring);
  309. kfree(tx_old);
  310. kfree(rx_old);
  311. adapter->rx_ring = rxdr;
  312. adapter->tx_ring = txdr;
  313. #else
  314. pch_gbe_free_rx_resources(adapter, rx_old);
  315. pch_gbe_free_tx_resources(adapter, tx_old);
  316. kfree(tx_old);
  317. kfree(rx_old);
  318. adapter->rx_ring = rxdr;
  319. adapter->tx_ring = txdr;
  320. #endif
  321. err = pch_gbe_up(adapter);
  322. }
  323. return err;
  324. err_setup_tx:
  325. pch_gbe_free_rx_resources(adapter, adapter->rx_ring);
  326. err_setup_rx:
  327. adapter->rx_ring = rx_old;
  328. adapter->tx_ring = tx_old;
  329. kfree(rxdr);
  330. err_alloc_rx:
  331. kfree(txdr);
  332. err_alloc_tx:
  333. if (netif_running(adapter->netdev))
  334. pch_gbe_up(adapter);
  335. return err;
  336. }
  337. /**
  338. * pch_gbe_get_pauseparam - Report pause parameters
  339. * @netdev: Network interface device structure
  340. * @pause: Pause parameters structure
  341. */
  342. static void pch_gbe_get_pauseparam(struct net_device *netdev,
  343. struct ethtool_pauseparam *pause)
  344. {
  345. struct pch_gbe_adapter *adapter = netdev_priv(netdev);
  346. struct pch_gbe_hw *hw = &adapter->hw;
  347. pause->autoneg =
  348. ((hw->mac.fc_autoneg) ? AUTONEG_ENABLE : AUTONEG_DISABLE);
  349. if (hw->mac.fc == PCH_GBE_FC_RX_PAUSE) {
  350. pause->rx_pause = 1;
  351. } else if (hw->mac.fc == PCH_GBE_FC_TX_PAUSE) {
  352. pause->tx_pause = 1;
  353. } else if (hw->mac.fc == PCH_GBE_FC_FULL) {
  354. pause->rx_pause = 1;
  355. pause->tx_pause = 1;
  356. }
  357. }
  358. /**
  359. * pch_gbe_set_pauseparam - Set pause paramters
  360. * @netdev: Network interface device structure
  361. * @pause: Pause parameters structure
  362. * Returns:
  363. * 0: Successful.
  364. * Negative value: Failed.
  365. */
  366. static int pch_gbe_set_pauseparam(struct net_device *netdev,
  367. struct ethtool_pauseparam *pause)
  368. {
  369. struct pch_gbe_adapter *adapter = netdev_priv(netdev);
  370. struct pch_gbe_hw *hw = &adapter->hw;
  371. int ret = 0;
  372. hw->mac.fc_autoneg = pause->autoneg;
  373. if ((pause->rx_pause) && (pause->tx_pause))
  374. hw->mac.fc = PCH_GBE_FC_FULL;
  375. else if ((pause->rx_pause) && (!pause->tx_pause))
  376. hw->mac.fc = PCH_GBE_FC_RX_PAUSE;
  377. else if ((!pause->rx_pause) && (pause->tx_pause))
  378. hw->mac.fc = PCH_GBE_FC_TX_PAUSE;
  379. else if ((!pause->rx_pause) && (!pause->tx_pause))
  380. hw->mac.fc = PCH_GBE_FC_NONE;
  381. if (hw->mac.fc_autoneg == AUTONEG_ENABLE) {
  382. if ((netif_running(adapter->netdev))) {
  383. pch_gbe_down(adapter);
  384. ret = pch_gbe_up(adapter);
  385. } else {
  386. pch_gbe_reset(adapter);
  387. }
  388. } else {
  389. ret = pch_gbe_mac_force_mac_fc(hw);
  390. }
  391. return ret;
  392. }
  393. /**
  394. * pch_gbe_get_strings - Return a set of strings that describe the requested
  395. * objects
  396. * @netdev: Network interface device structure
  397. * @stringset: Select the stringset. [ETH_SS_TEST] [ETH_SS_STATS]
  398. * @data: Pointer of read string data.
  399. */
  400. static void pch_gbe_get_strings(struct net_device *netdev, u32 stringset,
  401. u8 *data)
  402. {
  403. u8 *p = data;
  404. int i;
  405. switch (stringset) {
  406. case (u32) ETH_SS_STATS:
  407. for (i = 0; i < PCH_GBE_GLOBAL_STATS_LEN; i++) {
  408. memcpy(p, pch_gbe_gstrings_stats[i].string,
  409. ETH_GSTRING_LEN);
  410. p += ETH_GSTRING_LEN;
  411. }
  412. break;
  413. }
  414. }
  415. /**
  416. * pch_gbe_get_ethtool_stats - Return statistics about the device
  417. * @netdev: Network interface device structure
  418. * @stats: Ethtool statue structure
  419. * @data: Pointer of read status area
  420. */
  421. static void pch_gbe_get_ethtool_stats(struct net_device *netdev,
  422. struct ethtool_stats *stats, u64 *data)
  423. {
  424. struct pch_gbe_adapter *adapter = netdev_priv(netdev);
  425. int i;
  426. const struct pch_gbe_stats *gstats = pch_gbe_gstrings_stats;
  427. char *hw_stats = (char *)&adapter->stats;
  428. pch_gbe_update_stats(adapter);
  429. for (i = 0; i < PCH_GBE_GLOBAL_STATS_LEN; i++) {
  430. char *p = hw_stats + gstats->offset;
  431. data[i] = gstats->size == sizeof(u64) ? *(u64 *)p:(*(u32 *)p);
  432. gstats++;
  433. }
  434. }
  435. static int pch_gbe_get_sset_count(struct net_device *netdev, int sset)
  436. {
  437. switch (sset) {
  438. case ETH_SS_STATS:
  439. return PCH_GBE_STATS_LEN;
  440. default:
  441. return -EOPNOTSUPP;
  442. }
  443. }
  444. static const struct ethtool_ops pch_gbe_ethtool_ops = {
  445. .get_settings = pch_gbe_get_settings,
  446. .set_settings = pch_gbe_set_settings,
  447. .get_drvinfo = pch_gbe_get_drvinfo,
  448. .get_regs_len = pch_gbe_get_regs_len,
  449. .get_regs = pch_gbe_get_regs,
  450. .get_wol = pch_gbe_get_wol,
  451. .set_wol = pch_gbe_set_wol,
  452. .nway_reset = pch_gbe_nway_reset,
  453. .get_link = ethtool_op_get_link,
  454. .get_ringparam = pch_gbe_get_ringparam,
  455. .set_ringparam = pch_gbe_set_ringparam,
  456. .get_pauseparam = pch_gbe_get_pauseparam,
  457. .set_pauseparam = pch_gbe_set_pauseparam,
  458. .get_strings = pch_gbe_get_strings,
  459. .get_ethtool_stats = pch_gbe_get_ethtool_stats,
  460. .get_sset_count = pch_gbe_get_sset_count,
  461. };
  462. void pch_gbe_set_ethtool_ops(struct net_device *netdev)
  463. {
  464. SET_ETHTOOL_OPS(netdev, &pch_gbe_ethtool_ops);
  465. }