pch_gbe_ethtool.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  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. pch_gbe_hal_phy_sw_reset(hw);
  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. strcpy(drvinfo->driver, KBUILD_MODNAME);
  151. strcpy(drvinfo->version, pch_driver_version);
  152. strcpy(drvinfo->fw_version, "N/A");
  153. strcpy(drvinfo->bus_info, pci_name(adapter->pdev));
  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_mini_max_pending = 0;
  251. ring->rx_jumbo_max_pending = 0;
  252. ring->rx_pending = rxdr->count;
  253. ring->tx_pending = txdr->count;
  254. ring->rx_mini_pending = 0;
  255. ring->rx_jumbo_pending = 0;
  256. }
  257. /**
  258. * pch_gbe_set_ringparam - Set ring sizes
  259. * @netdev: Network interface device structure
  260. * @ring: Ring param structure
  261. * Returns
  262. * 0: Successful.
  263. * Negative value: Failed.
  264. */
  265. static int pch_gbe_set_ringparam(struct net_device *netdev,
  266. struct ethtool_ringparam *ring)
  267. {
  268. struct pch_gbe_adapter *adapter = netdev_priv(netdev);
  269. struct pch_gbe_tx_ring *txdr, *tx_old;
  270. struct pch_gbe_rx_ring *rxdr, *rx_old;
  271. int tx_ring_size, rx_ring_size;
  272. int err = 0;
  273. if ((ring->rx_mini_pending) || (ring->rx_jumbo_pending))
  274. return -EINVAL;
  275. tx_ring_size = (int)sizeof(struct pch_gbe_tx_ring);
  276. rx_ring_size = (int)sizeof(struct pch_gbe_rx_ring);
  277. if ((netif_running(adapter->netdev)))
  278. pch_gbe_down(adapter);
  279. tx_old = adapter->tx_ring;
  280. rx_old = adapter->rx_ring;
  281. txdr = kzalloc(tx_ring_size, GFP_KERNEL);
  282. if (!txdr) {
  283. err = -ENOMEM;
  284. goto err_alloc_tx;
  285. }
  286. rxdr = kzalloc(rx_ring_size, GFP_KERNEL);
  287. if (!rxdr) {
  288. err = -ENOMEM;
  289. goto err_alloc_rx;
  290. }
  291. adapter->tx_ring = txdr;
  292. adapter->rx_ring = rxdr;
  293. rxdr->count =
  294. clamp_val(ring->rx_pending, PCH_GBE_MIN_RXD, PCH_GBE_MAX_RXD);
  295. rxdr->count = roundup(rxdr->count, PCH_GBE_RX_DESC_MULTIPLE);
  296. txdr->count =
  297. clamp_val(ring->tx_pending, PCH_GBE_MIN_RXD, PCH_GBE_MAX_RXD);
  298. txdr->count = roundup(txdr->count, PCH_GBE_TX_DESC_MULTIPLE);
  299. if ((netif_running(adapter->netdev))) {
  300. /* Try to get new resources before deleting old */
  301. err = pch_gbe_setup_rx_resources(adapter, adapter->rx_ring);
  302. if (err)
  303. goto err_setup_rx;
  304. err = pch_gbe_setup_tx_resources(adapter, adapter->tx_ring);
  305. if (err)
  306. goto err_setup_tx;
  307. /* save the new, restore the old in order to free it,
  308. * then restore the new back again */
  309. #ifdef RINGFREE
  310. adapter->rx_ring = rx_old;
  311. adapter->tx_ring = tx_old;
  312. pch_gbe_free_rx_resources(adapter, adapter->rx_ring);
  313. pch_gbe_free_tx_resources(adapter, adapter->tx_ring);
  314. kfree(tx_old);
  315. kfree(rx_old);
  316. adapter->rx_ring = rxdr;
  317. adapter->tx_ring = txdr;
  318. #else
  319. pch_gbe_free_rx_resources(adapter, rx_old);
  320. pch_gbe_free_tx_resources(adapter, tx_old);
  321. kfree(tx_old);
  322. kfree(rx_old);
  323. adapter->rx_ring = rxdr;
  324. adapter->tx_ring = txdr;
  325. #endif
  326. err = pch_gbe_up(adapter);
  327. }
  328. return err;
  329. err_setup_tx:
  330. pch_gbe_free_rx_resources(adapter, adapter->rx_ring);
  331. err_setup_rx:
  332. adapter->rx_ring = rx_old;
  333. adapter->tx_ring = tx_old;
  334. kfree(rxdr);
  335. err_alloc_rx:
  336. kfree(txdr);
  337. err_alloc_tx:
  338. if (netif_running(adapter->netdev))
  339. pch_gbe_up(adapter);
  340. return err;
  341. }
  342. /**
  343. * pch_gbe_get_pauseparam - Report pause parameters
  344. * @netdev: Network interface device structure
  345. * @pause: Pause parameters structure
  346. */
  347. static void pch_gbe_get_pauseparam(struct net_device *netdev,
  348. struct ethtool_pauseparam *pause)
  349. {
  350. struct pch_gbe_adapter *adapter = netdev_priv(netdev);
  351. struct pch_gbe_hw *hw = &adapter->hw;
  352. pause->autoneg =
  353. ((hw->mac.fc_autoneg) ? AUTONEG_ENABLE : AUTONEG_DISABLE);
  354. if (hw->mac.fc == PCH_GBE_FC_RX_PAUSE) {
  355. pause->rx_pause = 1;
  356. } else if (hw->mac.fc == PCH_GBE_FC_TX_PAUSE) {
  357. pause->tx_pause = 1;
  358. } else if (hw->mac.fc == PCH_GBE_FC_FULL) {
  359. pause->rx_pause = 1;
  360. pause->tx_pause = 1;
  361. }
  362. }
  363. /**
  364. * pch_gbe_set_pauseparam - Set pause paramters
  365. * @netdev: Network interface device structure
  366. * @pause: Pause parameters structure
  367. * Returns
  368. * 0: Successful.
  369. * Negative value: Failed.
  370. */
  371. static int pch_gbe_set_pauseparam(struct net_device *netdev,
  372. struct ethtool_pauseparam *pause)
  373. {
  374. struct pch_gbe_adapter *adapter = netdev_priv(netdev);
  375. struct pch_gbe_hw *hw = &adapter->hw;
  376. int ret = 0;
  377. hw->mac.fc_autoneg = pause->autoneg;
  378. if ((pause->rx_pause) && (pause->tx_pause))
  379. hw->mac.fc = PCH_GBE_FC_FULL;
  380. else if ((pause->rx_pause) && (!pause->tx_pause))
  381. hw->mac.fc = PCH_GBE_FC_RX_PAUSE;
  382. else if ((!pause->rx_pause) && (pause->tx_pause))
  383. hw->mac.fc = PCH_GBE_FC_TX_PAUSE;
  384. else if ((!pause->rx_pause) && (!pause->tx_pause))
  385. hw->mac.fc = PCH_GBE_FC_NONE;
  386. if (hw->mac.fc_autoneg == AUTONEG_ENABLE) {
  387. if ((netif_running(adapter->netdev))) {
  388. pch_gbe_down(adapter);
  389. ret = pch_gbe_up(adapter);
  390. } else {
  391. pch_gbe_reset(adapter);
  392. }
  393. } else {
  394. ret = pch_gbe_mac_force_mac_fc(hw);
  395. }
  396. return ret;
  397. }
  398. /**
  399. * pch_gbe_get_strings - Return a set of strings that describe the requested
  400. * objects
  401. * @netdev: Network interface device structure
  402. * @stringset: Select the stringset. [ETH_SS_TEST] [ETH_SS_STATS]
  403. * @data: Pointer of read string data.
  404. */
  405. static void pch_gbe_get_strings(struct net_device *netdev, u32 stringset,
  406. u8 *data)
  407. {
  408. u8 *p = data;
  409. int i;
  410. switch (stringset) {
  411. case (u32) ETH_SS_STATS:
  412. for (i = 0; i < PCH_GBE_GLOBAL_STATS_LEN; i++) {
  413. memcpy(p, pch_gbe_gstrings_stats[i].string,
  414. ETH_GSTRING_LEN);
  415. p += ETH_GSTRING_LEN;
  416. }
  417. break;
  418. }
  419. }
  420. /**
  421. * pch_gbe_get_ethtool_stats - Return statistics about the device
  422. * @netdev: Network interface device structure
  423. * @stats: Ethtool statue structure
  424. * @data: Pointer of read status area
  425. */
  426. static void pch_gbe_get_ethtool_stats(struct net_device *netdev,
  427. struct ethtool_stats *stats, u64 *data)
  428. {
  429. struct pch_gbe_adapter *adapter = netdev_priv(netdev);
  430. int i;
  431. const struct pch_gbe_stats *gstats = pch_gbe_gstrings_stats;
  432. char *hw_stats = (char *)&adapter->stats;
  433. pch_gbe_update_stats(adapter);
  434. for (i = 0; i < PCH_GBE_GLOBAL_STATS_LEN; i++) {
  435. char *p = hw_stats + gstats->offset;
  436. data[i] = gstats->size == sizeof(u64) ? *(u64 *)p:(*(u32 *)p);
  437. gstats++;
  438. }
  439. }
  440. static int pch_gbe_get_sset_count(struct net_device *netdev, int sset)
  441. {
  442. switch (sset) {
  443. case ETH_SS_STATS:
  444. return PCH_GBE_STATS_LEN;
  445. default:
  446. return -EOPNOTSUPP;
  447. }
  448. }
  449. static const struct ethtool_ops pch_gbe_ethtool_ops = {
  450. .get_settings = pch_gbe_get_settings,
  451. .set_settings = pch_gbe_set_settings,
  452. .get_drvinfo = pch_gbe_get_drvinfo,
  453. .get_regs_len = pch_gbe_get_regs_len,
  454. .get_regs = pch_gbe_get_regs,
  455. .get_wol = pch_gbe_get_wol,
  456. .set_wol = pch_gbe_set_wol,
  457. .nway_reset = pch_gbe_nway_reset,
  458. .get_link = ethtool_op_get_link,
  459. .get_ringparam = pch_gbe_get_ringparam,
  460. .set_ringparam = pch_gbe_set_ringparam,
  461. .get_pauseparam = pch_gbe_get_pauseparam,
  462. .set_pauseparam = pch_gbe_set_pauseparam,
  463. .get_strings = pch_gbe_get_strings,
  464. .get_ethtool_stats = pch_gbe_get_ethtool_stats,
  465. .get_sset_count = pch_gbe_get_sset_count,
  466. };
  467. void pch_gbe_set_ethtool_ops(struct net_device *netdev)
  468. {
  469. SET_ETHTOOL_OPS(netdev, &pch_gbe_ethtool_ops);
  470. }