linkmode.c 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. // SPDX-License-Identifier: GPL-2.0+
  2. #include <linux/linkmode.h>
  3. /**
  4. * linkmode_resolve_pause - resolve the allowable pause modes
  5. * @local_adv: local advertisement in ethtool format
  6. * @partner_adv: partner advertisement in ethtool format
  7. * @tx_pause: pointer to bool to indicate whether transmit pause should be
  8. * enabled.
  9. * @rx_pause: pointer to bool to indicate whether receive pause should be
  10. * enabled.
  11. *
  12. * Flow control is resolved according to our and the link partners
  13. * advertisements using the following drawn from the 802.3 specs:
  14. * Local device Link partner
  15. * Pause AsymDir Pause AsymDir Result
  16. * 0 X 0 X Disabled
  17. * 0 1 1 0 Disabled
  18. * 0 1 1 1 TX
  19. * 1 0 0 X Disabled
  20. * 1 X 1 X TX+RX
  21. * 1 1 0 1 RX
  22. */
  23. void linkmode_resolve_pause(const unsigned long *local_adv,
  24. const unsigned long *partner_adv,
  25. bool *tx_pause, bool *rx_pause)
  26. {
  27. __ETHTOOL_DECLARE_LINK_MODE_MASK(m);
  28. linkmode_and(m, local_adv, partner_adv);
  29. if (linkmode_test_bit(ETHTOOL_LINK_MODE_Pause_BIT, m)) {
  30. *tx_pause = true;
  31. *rx_pause = true;
  32. } else if (linkmode_test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, m)) {
  33. *tx_pause = linkmode_test_bit(ETHTOOL_LINK_MODE_Pause_BIT,
  34. partner_adv);
  35. *rx_pause = linkmode_test_bit(ETHTOOL_LINK_MODE_Pause_BIT,
  36. local_adv);
  37. } else {
  38. *tx_pause = false;
  39. *rx_pause = false;
  40. }
  41. }
  42. EXPORT_SYMBOL_GPL(linkmode_resolve_pause);
  43. /**
  44. * linkmode_set_pause - set the pause mode advertisement
  45. * @advertisement: advertisement in ethtool format
  46. * @tx: boolean from ethtool struct ethtool_pauseparam tx_pause member
  47. * @rx: boolean from ethtool struct ethtool_pauseparam rx_pause member
  48. *
  49. * Configure the advertised Pause and Asym_Pause bits according to the
  50. * capabilities of provided in @tx and @rx.
  51. *
  52. * We convert as follows:
  53. * tx rx Pause AsymDir
  54. * 0 0 0 0
  55. * 0 1 1 1
  56. * 1 0 0 1
  57. * 1 1 1 0
  58. *
  59. * Note: this translation from ethtool tx/rx notation to the advertisement
  60. * is actually very problematical. Here are some examples:
  61. *
  62. * For tx=0 rx=1, meaning transmit is unsupported, receive is supported:
  63. *
  64. * Local device Link partner
  65. * Pause AsymDir Pause AsymDir Result
  66. * 1 1 1 0 TX + RX - but we have no TX support.
  67. * 1 1 0 1 Only this gives RX only
  68. *
  69. * For tx=1 rx=1, meaning we have the capability to transmit and receive
  70. * pause frames:
  71. *
  72. * Local device Link partner
  73. * Pause AsymDir Pause AsymDir Result
  74. * 1 0 0 1 Disabled - but since we do support tx and rx,
  75. * this should resolve to RX only.
  76. *
  77. * Hence, asking for:
  78. * rx=1 tx=0 gives Pause+AsymDir advertisement, but we may end up
  79. * resolving to tx+rx pause or only rx pause depending on
  80. * the partners advertisement.
  81. * rx=0 tx=1 gives AsymDir only, which will only give tx pause if
  82. * the partners advertisement allows it.
  83. * rx=1 tx=1 gives Pause only, which will only allow tx+rx pause
  84. * if the other end also advertises Pause.
  85. */
  86. void linkmode_set_pause(unsigned long *advertisement, bool tx, bool rx)
  87. {
  88. linkmode_mod_bit(ETHTOOL_LINK_MODE_Pause_BIT, advertisement, rx);
  89. linkmode_mod_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, advertisement,
  90. rx ^ tx);
  91. }
  92. EXPORT_SYMBOL_GPL(linkmode_set_pause);