GNU Linux-libre 4.9.331-gnu1
[releases.git] / drivers / net / ethernet / amd / xgbe / xgbe-dcb.c
1 /*
2  * AMD 10Gb Ethernet driver
3  *
4  * This file is available to you under your choice of the following two
5  * licenses:
6  *
7  * License 1: GPLv2
8  *
9  * Copyright (c) 2014-2016 Advanced Micro Devices, Inc.
10  *
11  * This file is free software; you may copy, redistribute and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation, either version 2 of the License, or (at
14  * your option) any later version.
15  *
16  * This file is distributed in the hope that it will be useful, but
17  * WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23  *
24  * This file incorporates work covered by the following copyright and
25  * permission notice:
26  *     The Synopsys DWC ETHER XGMAC Software Driver and documentation
27  *     (hereinafter "Software") is an unsupported proprietary work of Synopsys,
28  *     Inc. unless otherwise expressly agreed to in writing between Synopsys
29  *     and you.
30  *
31  *     The Software IS NOT an item of Licensed Software or Licensed Product
32  *     under any End User Software License Agreement or Agreement for Licensed
33  *     Product with Synopsys or any supplement thereto.  Permission is hereby
34  *     granted, free of charge, to any person obtaining a copy of this software
35  *     annotated with this license and the Software, to deal in the Software
36  *     without restriction, including without limitation the rights to use,
37  *     copy, modify, merge, publish, distribute, sublicense, and/or sell copies
38  *     of the Software, and to permit persons to whom the Software is furnished
39  *     to do so, subject to the following conditions:
40  *
41  *     The above copyright notice and this permission notice shall be included
42  *     in all copies or substantial portions of the Software.
43  *
44  *     THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"
45  *     BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
46  *     TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
47  *     PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS
48  *     BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
49  *     CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
50  *     SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
51  *     INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
52  *     CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
53  *     ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
54  *     THE POSSIBILITY OF SUCH DAMAGE.
55  *
56  *
57  * License 2: Modified BSD
58  *
59  * Copyright (c) 2014-2016 Advanced Micro Devices, Inc.
60  * All rights reserved.
61  *
62  * Redistribution and use in source and binary forms, with or without
63  * modification, are permitted provided that the following conditions are met:
64  *     * Redistributions of source code must retain the above copyright
65  *       notice, this list of conditions and the following disclaimer.
66  *     * Redistributions in binary form must reproduce the above copyright
67  *       notice, this list of conditions and the following disclaimer in the
68  *       documentation and/or other materials provided with the distribution.
69  *     * Neither the name of Advanced Micro Devices, Inc. nor the
70  *       names of its contributors may be used to endorse or promote products
71  *       derived from this software without specific prior written permission.
72  *
73  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
74  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
75  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
76  * ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
77  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
78  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
79  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
80  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
81  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
82  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
83  *
84  * This file incorporates work covered by the following copyright and
85  * permission notice:
86  *     The Synopsys DWC ETHER XGMAC Software Driver and documentation
87  *     (hereinafter "Software") is an unsupported proprietary work of Synopsys,
88  *     Inc. unless otherwise expressly agreed to in writing between Synopsys
89  *     and you.
90  *
91  *     The Software IS NOT an item of Licensed Software or Licensed Product
92  *     under any End User Software License Agreement or Agreement for Licensed
93  *     Product with Synopsys or any supplement thereto.  Permission is hereby
94  *     granted, free of charge, to any person obtaining a copy of this software
95  *     annotated with this license and the Software, to deal in the Software
96  *     without restriction, including without limitation the rights to use,
97  *     copy, modify, merge, publish, distribute, sublicense, and/or sell copies
98  *     of the Software, and to permit persons to whom the Software is furnished
99  *     to do so, subject to the following conditions:
100  *
101  *     The above copyright notice and this permission notice shall be included
102  *     in all copies or substantial portions of the Software.
103  *
104  *     THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"
105  *     BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
106  *     TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
107  *     PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS
108  *     BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
109  *     CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
110  *     SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
111  *     INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
112  *     CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
113  *     ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
114  *     THE POSSIBILITY OF SUCH DAMAGE.
115  */
116
117 #include <linux/netdevice.h>
118 #include <net/dcbnl.h>
119
120 #include "xgbe.h"
121 #include "xgbe-common.h"
122
123 static int xgbe_dcb_ieee_getets(struct net_device *netdev,
124                                 struct ieee_ets *ets)
125 {
126         struct xgbe_prv_data *pdata = netdev_priv(netdev);
127
128         /* Set number of supported traffic classes */
129         ets->ets_cap = pdata->hw_feat.tc_cnt;
130
131         if (pdata->ets) {
132                 ets->cbs = pdata->ets->cbs;
133                 memcpy(ets->tc_tx_bw, pdata->ets->tc_tx_bw,
134                        sizeof(ets->tc_tx_bw));
135                 memcpy(ets->tc_tsa, pdata->ets->tc_tsa,
136                        sizeof(ets->tc_tsa));
137                 memcpy(ets->prio_tc, pdata->ets->prio_tc,
138                        sizeof(ets->prio_tc));
139         }
140
141         return 0;
142 }
143
144 static int xgbe_dcb_ieee_setets(struct net_device *netdev,
145                                 struct ieee_ets *ets)
146 {
147         struct xgbe_prv_data *pdata = netdev_priv(netdev);
148         unsigned int i, tc_ets, tc_ets_weight;
149         u8 max_tc = 0;
150
151         tc_ets = 0;
152         tc_ets_weight = 0;
153         for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++) {
154                 netif_dbg(pdata, drv, netdev,
155                           "TC%u: tx_bw=%hhu, rx_bw=%hhu, tsa=%hhu\n", i,
156                           ets->tc_tx_bw[i], ets->tc_rx_bw[i],
157                           ets->tc_tsa[i]);
158                 netif_dbg(pdata, drv, netdev, "PRIO%u: TC=%hhu\n", i,
159                           ets->prio_tc[i]);
160
161                 max_tc = max_t(u8, max_tc, ets->prio_tc[i]);
162                 if ((ets->tc_tx_bw[i] || ets->tc_tsa[i]))
163                         max_tc = max_t(u8, max_tc, i);
164
165                 switch (ets->tc_tsa[i]) {
166                 case IEEE_8021QAZ_TSA_STRICT:
167                         break;
168                 case IEEE_8021QAZ_TSA_ETS:
169                         tc_ets = 1;
170                         tc_ets_weight += ets->tc_tx_bw[i];
171                         break;
172                 default:
173                         netif_err(pdata, drv, netdev,
174                                   "unsupported TSA algorithm (%hhu)\n",
175                                   ets->tc_tsa[i]);
176                         return -EINVAL;
177                 }
178         }
179
180         /* Check maximum traffic class requested */
181         if (max_tc >= pdata->hw_feat.tc_cnt) {
182                 netif_err(pdata, drv, netdev,
183                           "exceeded number of supported traffic classes\n");
184                 return -EINVAL;
185         }
186
187         /* Weights must add up to 100% */
188         if (tc_ets && (tc_ets_weight != 100)) {
189                 netif_err(pdata, drv, netdev,
190                           "sum of ETS algorithm weights is not 100 (%u)\n",
191                           tc_ets_weight);
192                 return -EINVAL;
193         }
194
195         if (!pdata->ets) {
196                 pdata->ets = devm_kzalloc(pdata->dev, sizeof(*pdata->ets),
197                                           GFP_KERNEL);
198                 if (!pdata->ets)
199                         return -ENOMEM;
200         }
201
202         pdata->num_tcs = max_tc + 1;
203         memcpy(pdata->ets, ets, sizeof(*pdata->ets));
204
205         pdata->hw_if.config_dcb_tc(pdata);
206
207         return 0;
208 }
209
210 static int xgbe_dcb_ieee_getpfc(struct net_device *netdev,
211                                 struct ieee_pfc *pfc)
212 {
213         struct xgbe_prv_data *pdata = netdev_priv(netdev);
214
215         /* Set number of supported PFC traffic classes */
216         pfc->pfc_cap = pdata->hw_feat.tc_cnt;
217
218         if (pdata->pfc) {
219                 pfc->pfc_en = pdata->pfc->pfc_en;
220                 pfc->mbc = pdata->pfc->mbc;
221                 pfc->delay = pdata->pfc->delay;
222         }
223
224         return 0;
225 }
226
227 static int xgbe_dcb_ieee_setpfc(struct net_device *netdev,
228                                 struct ieee_pfc *pfc)
229 {
230         struct xgbe_prv_data *pdata = netdev_priv(netdev);
231
232         netif_dbg(pdata, drv, netdev,
233                   "cap=%hhu, en=%#hhx, mbc=%hhu, delay=%hhu\n",
234                   pfc->pfc_cap, pfc->pfc_en, pfc->mbc, pfc->delay);
235
236         /* Check PFC for supported number of traffic classes */
237         if (pfc->pfc_en & ~((1 << pdata->hw_feat.tc_cnt) - 1)) {
238                 netif_err(pdata, drv, netdev,
239                           "PFC requested for unsupported traffic class\n");
240                 return -EINVAL;
241         }
242
243         if (!pdata->pfc) {
244                 pdata->pfc = devm_kzalloc(pdata->dev, sizeof(*pdata->pfc),
245                                           GFP_KERNEL);
246                 if (!pdata->pfc)
247                         return -ENOMEM;
248         }
249
250         memcpy(pdata->pfc, pfc, sizeof(*pdata->pfc));
251
252         pdata->hw_if.config_dcb_pfc(pdata);
253
254         return 0;
255 }
256
257 static u8 xgbe_dcb_getdcbx(struct net_device *netdev)
258 {
259         return DCB_CAP_DCBX_HOST | DCB_CAP_DCBX_VER_IEEE;
260 }
261
262 static u8 xgbe_dcb_setdcbx(struct net_device *netdev, u8 dcbx)
263 {
264         struct xgbe_prv_data *pdata = netdev_priv(netdev);
265         u8 support = xgbe_dcb_getdcbx(netdev);
266
267         netif_dbg(pdata, drv, netdev, "DCBX=%#hhx\n", dcbx);
268
269         if (dcbx & ~support)
270                 return 1;
271
272         if ((dcbx & support) != support)
273                 return 1;
274
275         return 0;
276 }
277
278 static const struct dcbnl_rtnl_ops xgbe_dcbnl_ops = {
279         /* IEEE 802.1Qaz std */
280         .ieee_getets = xgbe_dcb_ieee_getets,
281         .ieee_setets = xgbe_dcb_ieee_setets,
282         .ieee_getpfc = xgbe_dcb_ieee_getpfc,
283         .ieee_setpfc = xgbe_dcb_ieee_setpfc,
284
285         /* DCBX configuration */
286         .getdcbx     = xgbe_dcb_getdcbx,
287         .setdcbx     = xgbe_dcb_setdcbx,
288 };
289
290 const struct dcbnl_rtnl_ops *xgbe_get_dcbnl_ops(void)
291 {
292         return &xgbe_dcbnl_ops;
293 }