lantiq: clarify VG3503J name
[oweals/openwrt.git] / target / linux / mvebu / patches-4.9 / 423-phylink-add-flow-control-support.patch
1 From: Russell King <rmk+kernel@arm.linux.org.uk>
2 Date: Thu, 1 Oct 2015 20:32:07 +0100
3 Subject: [PATCH] phylink: add flow control support
4
5 Add flow control support, including ethtool support, to phylink.  We
6 add support to allow ethtool to get and set the current flow control
7 settings, and the 802.3 specified resolution for the local and remote
8 link partner abilities.
9
10 Signed-off-by: Russell King <rmk+kernel@arm.linux.org.uk>
11 ---
12
13 --- a/drivers/net/phy/phylink.c
14 +++ b/drivers/net/phy/phylink.c
15 @@ -132,6 +132,9 @@ static int phylink_parse_fixedlink(struc
16  
17                 if (of_property_read_bool(fixed_node, "full-duplex"))
18                         pl->link_config.duplex = DUPLEX_FULL;
19 +
20 +               /* We treat the "pause" and "asym-pause" terminology as
21 +                * defining the link partner's ability. */
22                 if (of_property_read_bool(fixed_node, "pause"))
23                         pl->link_config.pause |= MLO_PAUSE_SYM;
24                 if (of_property_read_bool(fixed_node, "asym-pause"))
25 @@ -277,6 +280,56 @@ static void phylink_get_fixed_state(stru
26                 state->link = !!gpiod_get_value(pl->link_gpio);
27  }
28  
29 +/* Flow control is resolved according to our and the link partners
30 + * advertisments using the following drawn from the 802.3 specs:
31 + *  Local device  Link partner
32 + *  Pause AsymDir Pause AsymDir Result
33 + *    1     X       1     X     TX+RX
34 + *    0     1       1     1     RX
35 + *    1     1       0     1     TX
36 + */
37 +static void phylink_resolve_flow(struct phylink *pl,
38 +       struct phylink_link_state *state)
39 +{
40 +       int new_pause = 0;
41 +
42 +       if (pl->link_config.pause & MLO_PAUSE_AN) {
43 +               int pause = 0;
44 +
45 +               if (phylink_test(pl->link_config.advertising, Pause))
46 +                       pause |= MLO_PAUSE_SYM;
47 +               if (phylink_test(pl->link_config.advertising, Asym_Pause))
48 +                       pause |= MLO_PAUSE_ASYM;
49 +
50 +               pause &= state->pause;
51 +
52 +               if (pause & MLO_PAUSE_SYM)
53 +                       new_pause = MLO_PAUSE_TX | MLO_PAUSE_RX;
54 +               else if (pause & MLO_PAUSE_ASYM)
55 +                       new_pause = state->pause & MLO_PAUSE_SYM ?
56 +                                MLO_PAUSE_RX : MLO_PAUSE_TX;
57 +       } else {
58 +               new_pause = pl->link_config.pause & MLO_PAUSE_TXRX_MASK;
59 +       }
60 +
61 +       state->pause &= ~MLO_PAUSE_TXRX_MASK;
62 +       state->pause |= new_pause;
63 +}
64 +
65 +static const char *phylink_pause_to_str(int pause)
66 +{
67 +       switch (pause & MLO_PAUSE_TXRX_MASK) {
68 +       case MLO_PAUSE_TX | MLO_PAUSE_RX:
69 +               return "rx/tx";
70 +       case MLO_PAUSE_TX:
71 +               return "tx";
72 +       case MLO_PAUSE_RX:
73 +               return "rx";
74 +       default:
75 +               return "off";
76 +       }
77 +}
78 +
79  static void phylink_resolve(struct work_struct *w)
80  {
81         struct phylink *pl = container_of(w, struct phylink, resolve);
82 @@ -290,6 +343,7 @@ static void phylink_resolve(struct work_
83                 switch (pl->link_an_mode) {
84                 case MLO_AN_PHY:
85                         link_state = pl->phy_state;
86 +                       phylink_resolve_flow(pl, &link_state);
87                         break;
88  
89                 case MLO_AN_FIXED:
90 @@ -298,9 +352,12 @@ static void phylink_resolve(struct work_
91  
92                 case MLO_AN_SGMII:
93                         phylink_get_mac_state(pl, &link_state);
94 -                       if (pl->phydev)
95 +                       if (pl->phydev) {
96                                 link_state.link = link_state.link &&
97                                                   pl->phy_state.link;
98 +                               link_state.pause |= pl->phy_state.pause;
99 +                               phylink_resolve_flow(pl, &link_state);
100 +                       }
101                         break;
102  
103                 case MLO_AN_8023Z:
104 @@ -330,7 +387,7 @@ static void phylink_resolve(struct work_
105                                     "Link is Up - %s/%s - flow control %s\n",
106                                     phy_speed_to_str(link_state.speed),
107                                     phy_duplex_to_str(link_state.duplex),
108 -                                   link_state.pause ? "rx/tx" : "off");
109 +                                   phylink_pause_to_str(link_state.pause));
110                 }
111         }
112         mutex_unlock(&pl->state_mutex);
113 @@ -358,6 +415,7 @@ struct phylink *phylink_create(struct ne
114         pl->netdev = ndev;
115         pl->link_interface = iface;
116         pl->link_port = PORT_MII;
117 +       pl->link_config.pause = MLO_PAUSE_AN;
118         pl->link_config.speed = SPEED_UNKNOWN;
119         pl->link_config.duplex = DUPLEX_UNKNOWN;
120         pl->ops = ops;
121 @@ -580,6 +638,7 @@ void phylink_start(struct phylink *pl)
122          * a fixed-link to start with the correct parameters, and also
123          * ensures that we set the appropriate advertisment for Serdes links.
124          */
125 +       phylink_resolve_flow(pl, &pl->link_config);
126         phylink_mac_config(pl, &pl->link_config);
127  
128         clear_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state);
129 @@ -797,6 +856,79 @@ int phylink_ethtool_nway_reset(struct ph
130  }
131  EXPORT_SYMBOL_GPL(phylink_ethtool_nway_reset);
132  
133 +void phylink_ethtool_get_pauseparam(struct phylink *pl,
134 +                                   struct ethtool_pauseparam *pause)
135 +{
136 +       mutex_lock(&pl->config_mutex);
137 +
138 +       pause->autoneg = !!(pl->link_config.pause & MLO_PAUSE_AN);
139 +       pause->rx_pause = !!(pl->link_config.pause & MLO_PAUSE_RX);
140 +       pause->tx_pause = !!(pl->link_config.pause & MLO_PAUSE_TX);
141 +
142 +       mutex_unlock(&pl->config_mutex);
143 +}
144 +EXPORT_SYMBOL_GPL(phylink_ethtool_get_pauseparam);
145 +
146 +static int __phylink_ethtool_set_pauseparam(struct phylink *pl,
147 +                                           struct ethtool_pauseparam *pause)
148 +{
149 +       struct phylink_link_state *config = &pl->link_config;
150 +
151 +       if (!phylink_test(pl->supported, Pause) &&
152 +           !phylink_test(pl->supported, Asym_Pause))
153 +               return -EOPNOTSUPP;
154 +
155 +       if (!phylink_test(pl->supported, Asym_Pause) &&
156 +           !pause->autoneg && pause->rx_pause != pause->tx_pause)
157 +               return -EINVAL;
158 +
159 +       config->pause &= ~(MLO_PAUSE_AN | MLO_PAUSE_TXRX_MASK);
160 +
161 +       if (pause->autoneg)
162 +               config->pause |= MLO_PAUSE_AN;
163 +       if (pause->rx_pause)
164 +               config->pause |= MLO_PAUSE_RX;
165 +       if (pause->tx_pause)
166 +               config->pause |= MLO_PAUSE_TX;
167 +
168 +       if (!test_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state)) {
169 +               switch (pl->link_an_mode) {
170 +               case MLO_AN_PHY:
171 +                       /* Silently mark the carrier down, and then trigger a resolve */
172 +                       netif_carrier_off(pl->netdev);
173 +                       phylink_run_resolve(pl);
174 +                       break;
175 +
176 +               case MLO_AN_FIXED:
177 +                       /* Should we allow fixed links to change against the config? */
178 +                       phylink_resolve_flow(pl, config);
179 +                       phylink_mac_config(pl, config);
180 +                       break;
181 +
182 +               case MLO_AN_SGMII:
183 +               case MLO_AN_8023Z:
184 +                       phylink_mac_config(pl, config);
185 +                       phylink_mac_an_restart(pl);
186 +                       break;
187 +               }
188 +       }
189 +
190 +       return 0;
191 +}
192 +
193 +int phylink_ethtool_set_pauseparam(struct phylink *pl,
194 +                                  struct ethtool_pauseparam *pause)
195 +{
196 +       int ret;
197 +
198 +       mutex_lock(&pl->config_mutex);
199 +       ret = __phylink_ethtool_set_pauseparam(pl, pause);
200 +       mutex_unlock(&pl->config_mutex);
201 +
202 +       return ret;
203 +}
204 +EXPORT_SYMBOL_GPL(phylink_ethtool_set_pauseparam);
205 +
206  /* This emulates MII registers for a fixed-mode phy operating as per the
207   * passed in state. "aneg" defines if we report negotiation is possible.
208   *
209 --- a/include/linux/phylink.h
210 +++ b/include/linux/phylink.h
211 @@ -13,6 +13,10 @@ enum {
212         MLO_PAUSE_NONE,
213         MLO_PAUSE_ASYM = BIT(0),
214         MLO_PAUSE_SYM = BIT(1),
215 +       MLO_PAUSE_RX = BIT(2),
216 +       MLO_PAUSE_TX = BIT(3),
217 +       MLO_PAUSE_TXRX_MASK = MLO_PAUSE_TX | MLO_PAUSE_RX,
218 +       MLO_PAUSE_AN = BIT(4),
219  
220         MLO_AN_PHY = 0,
221         MLO_AN_FIXED,
222 @@ -87,6 +91,10 @@ int phylink_ethtool_ksettings_get(struct
223  int phylink_ethtool_ksettings_set(struct phylink *,
224                                   const struct ethtool_link_ksettings *);
225  int phylink_ethtool_nway_reset(struct phylink *);
226 +void phylink_ethtool_get_pauseparam(struct phylink *,
227 +                                   struct ethtool_pauseparam *);
228 +int phylink_ethtool_set_pauseparam(struct phylink *,
229 +                                  struct ethtool_pauseparam *);
230  int phylink_mii_ioctl(struct phylink *, struct ifreq *, int);
231  
232  int phylink_set_link(struct phylink *pl, unsigned int mode, u8 port,