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