blob: b05393f0ff0aa2386abf0207897a32378f24460d [file] [log] [blame]
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001/*
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 *
Lendacky, Thomasb4eee842016-02-17 11:48:08 -06009 * Copyright (c) 2014-2016 Advanced Micro Devices, Inc.
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -050010 *
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 *
Lendacky, Thomasb4eee842016-02-17 11:48:08 -060059 * Copyright (c) 2014-2016 Advanced Micro Devices, Inc.
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -050060 * 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/phy.h>
Lendacky, Thomasc3152d42015-01-16 12:47:00 -0600118#include <linux/mdio.h>
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500119#include <linux/clk.h>
Lendacky, Thomas801c62d2014-06-24 16:19:24 -0500120#include <linux/bitrev.h>
Lendacky, Thomasb85e4d82014-06-24 16:19:29 -0500121#include <linux/crc32.h>
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500122
123#include "xgbe.h"
124#include "xgbe-common.h"
125
Lendacky, Thomas43e0dcf2016-11-03 13:18:16 -0500126static inline unsigned int xgbe_get_max_frame(struct xgbe_prv_data *pdata)
127{
128 return pdata->netdev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
129}
130
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500131static unsigned int xgbe_usec_to_riwt(struct xgbe_prv_data *pdata,
132 unsigned int usec)
133{
134 unsigned long rate;
135 unsigned int ret;
136
137 DBGPR("-->xgbe_usec_to_riwt\n");
138
Lendacky, Thomas82a19032015-01-16 12:47:16 -0600139 rate = pdata->sysclk_rate;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500140
141 /*
142 * Convert the input usec value to the watchdog timer value. Each
143 * watchdog timer value is equivalent to 256 clock cycles.
144 * Calculate the required value as:
145 * ( usec * ( system_clock_mhz / 10^6 ) / 256
146 */
147 ret = (usec * (rate / 1000000)) / 256;
148
149 DBGPR("<--xgbe_usec_to_riwt\n");
150
151 return ret;
152}
153
154static unsigned int xgbe_riwt_to_usec(struct xgbe_prv_data *pdata,
155 unsigned int riwt)
156{
157 unsigned long rate;
158 unsigned int ret;
159
160 DBGPR("-->xgbe_riwt_to_usec\n");
161
Lendacky, Thomas82a19032015-01-16 12:47:16 -0600162 rate = pdata->sysclk_rate;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500163
164 /*
165 * Convert the input watchdog timer value to the usec value. Each
166 * watchdog timer value is equivalent to 256 clock cycles.
167 * Calculate the required value as:
168 * ( riwt * 256 ) / ( system_clock_mhz / 10^6 )
169 */
170 ret = (riwt * 256) / (rate / 1000000);
171
172 DBGPR("<--xgbe_riwt_to_usec\n");
173
174 return ret;
175}
176
177static int xgbe_config_pblx8(struct xgbe_prv_data *pdata)
178{
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500179 unsigned int i;
180
Lendacky, Thomas18f9f0a2017-06-28 13:42:51 -0500181 for (i = 0; i < pdata->channel_count; i++)
182 XGMAC_DMA_IOWRITE_BITS(pdata->channel[i], DMA_CH_CR, PBLX8,
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500183 pdata->pblx8);
184
185 return 0;
186}
187
188static int xgbe_get_tx_pbl_val(struct xgbe_prv_data *pdata)
189{
Lendacky, Thomas18f9f0a2017-06-28 13:42:51 -0500190 return XGMAC_DMA_IOREAD_BITS(pdata->channel[0], DMA_CH_TCR, PBL);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500191}
192
193static int xgbe_config_tx_pbl_val(struct xgbe_prv_data *pdata)
194{
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500195 unsigned int i;
196
Lendacky, Thomas18f9f0a2017-06-28 13:42:51 -0500197 for (i = 0; i < pdata->channel_count; i++) {
198 if (!pdata->channel[i]->tx_ring)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500199 break;
200
Lendacky, Thomas18f9f0a2017-06-28 13:42:51 -0500201 XGMAC_DMA_IOWRITE_BITS(pdata->channel[i], DMA_CH_TCR, PBL,
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500202 pdata->tx_pbl);
203 }
204
205 return 0;
206}
207
208static int xgbe_get_rx_pbl_val(struct xgbe_prv_data *pdata)
209{
Lendacky, Thomas18f9f0a2017-06-28 13:42:51 -0500210 return XGMAC_DMA_IOREAD_BITS(pdata->channel[0], DMA_CH_RCR, PBL);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500211}
212
213static int xgbe_config_rx_pbl_val(struct xgbe_prv_data *pdata)
214{
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500215 unsigned int i;
216
Lendacky, Thomas18f9f0a2017-06-28 13:42:51 -0500217 for (i = 0; i < pdata->channel_count; i++) {
218 if (!pdata->channel[i]->rx_ring)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500219 break;
220
Lendacky, Thomas18f9f0a2017-06-28 13:42:51 -0500221 XGMAC_DMA_IOWRITE_BITS(pdata->channel[i], DMA_CH_RCR, PBL,
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500222 pdata->rx_pbl);
223 }
224
225 return 0;
226}
227
228static int xgbe_config_osp_mode(struct xgbe_prv_data *pdata)
229{
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500230 unsigned int i;
231
Lendacky, Thomas18f9f0a2017-06-28 13:42:51 -0500232 for (i = 0; i < pdata->channel_count; i++) {
233 if (!pdata->channel[i]->tx_ring)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500234 break;
235
Lendacky, Thomas18f9f0a2017-06-28 13:42:51 -0500236 XGMAC_DMA_IOWRITE_BITS(pdata->channel[i], DMA_CH_TCR, OSP,
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500237 pdata->tx_osp_mode);
238 }
239
240 return 0;
241}
242
243static int xgbe_config_rsf_mode(struct xgbe_prv_data *pdata, unsigned int val)
244{
245 unsigned int i;
246
Lendacky, Thomas853eb162014-07-29 08:57:31 -0500247 for (i = 0; i < pdata->rx_q_count; i++)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500248 XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_Q_RQOMR, RSF, val);
249
250 return 0;
251}
252
253static int xgbe_config_tsf_mode(struct xgbe_prv_data *pdata, unsigned int val)
254{
255 unsigned int i;
256
Lendacky, Thomas853eb162014-07-29 08:57:31 -0500257 for (i = 0; i < pdata->tx_q_count; i++)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500258 XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_Q_TQOMR, TSF, val);
259
260 return 0;
261}
262
263static int xgbe_config_rx_threshold(struct xgbe_prv_data *pdata,
264 unsigned int val)
265{
266 unsigned int i;
267
Lendacky, Thomas853eb162014-07-29 08:57:31 -0500268 for (i = 0; i < pdata->rx_q_count; i++)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500269 XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_Q_RQOMR, RTC, val);
270
271 return 0;
272}
273
274static int xgbe_config_tx_threshold(struct xgbe_prv_data *pdata,
275 unsigned int val)
276{
277 unsigned int i;
278
Lendacky, Thomas853eb162014-07-29 08:57:31 -0500279 for (i = 0; i < pdata->tx_q_count; i++)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500280 XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_Q_TQOMR, TTC, val);
281
282 return 0;
283}
284
285static int xgbe_config_rx_coalesce(struct xgbe_prv_data *pdata)
286{
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500287 unsigned int i;
288
Lendacky, Thomas18f9f0a2017-06-28 13:42:51 -0500289 for (i = 0; i < pdata->channel_count; i++) {
290 if (!pdata->channel[i]->rx_ring)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500291 break;
292
Lendacky, Thomas18f9f0a2017-06-28 13:42:51 -0500293 XGMAC_DMA_IOWRITE_BITS(pdata->channel[i], DMA_CH_RIWT, RWT,
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500294 pdata->rx_riwt);
295 }
296
297 return 0;
298}
299
300static int xgbe_config_tx_coalesce(struct xgbe_prv_data *pdata)
301{
302 return 0;
303}
304
305static void xgbe_config_rx_buffer_size(struct xgbe_prv_data *pdata)
306{
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500307 unsigned int i;
308
Lendacky, Thomas18f9f0a2017-06-28 13:42:51 -0500309 for (i = 0; i < pdata->channel_count; i++) {
310 if (!pdata->channel[i]->rx_ring)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500311 break;
312
Lendacky, Thomas18f9f0a2017-06-28 13:42:51 -0500313 XGMAC_DMA_IOWRITE_BITS(pdata->channel[i], DMA_CH_RCR, RBSZ,
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500314 pdata->rx_buf_size);
315 }
316}
317
318static void xgbe_config_tso_mode(struct xgbe_prv_data *pdata)
319{
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500320 unsigned int i;
321
Lendacky, Thomas18f9f0a2017-06-28 13:42:51 -0500322 for (i = 0; i < pdata->channel_count; i++) {
323 if (!pdata->channel[i]->tx_ring)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500324 break;
325
Lendacky, Thomas18f9f0a2017-06-28 13:42:51 -0500326 XGMAC_DMA_IOWRITE_BITS(pdata->channel[i], DMA_CH_TCR, TSE, 1);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500327 }
328}
329
Lendacky, Thomas174fd252014-11-04 16:06:50 -0600330static void xgbe_config_sph_mode(struct xgbe_prv_data *pdata)
331{
Lendacky, Thomas174fd252014-11-04 16:06:50 -0600332 unsigned int i;
333
Lendacky, Thomas18f9f0a2017-06-28 13:42:51 -0500334 for (i = 0; i < pdata->channel_count; i++) {
335 if (!pdata->channel[i]->rx_ring)
Lendacky, Thomas174fd252014-11-04 16:06:50 -0600336 break;
337
Lendacky, Thomas18f9f0a2017-06-28 13:42:51 -0500338 XGMAC_DMA_IOWRITE_BITS(pdata->channel[i], DMA_CH_CR, SPH, 1);
Lendacky, Thomas174fd252014-11-04 16:06:50 -0600339 }
340
341 XGMAC_IOWRITE_BITS(pdata, MAC_RCR, HDSMS, XGBE_SPH_HDSMS_SIZE);
342}
343
Lendacky, Thomas5b9dfe22014-11-04 16:07:02 -0600344static int xgbe_write_rss_reg(struct xgbe_prv_data *pdata, unsigned int type,
345 unsigned int index, unsigned int val)
346{
347 unsigned int wait;
348 int ret = 0;
349
350 mutex_lock(&pdata->rss_mutex);
351
352 if (XGMAC_IOREAD_BITS(pdata, MAC_RSSAR, OB)) {
353 ret = -EBUSY;
354 goto unlock;
355 }
356
357 XGMAC_IOWRITE(pdata, MAC_RSSDR, val);
358
359 XGMAC_IOWRITE_BITS(pdata, MAC_RSSAR, RSSIA, index);
360 XGMAC_IOWRITE_BITS(pdata, MAC_RSSAR, ADDRT, type);
361 XGMAC_IOWRITE_BITS(pdata, MAC_RSSAR, CT, 0);
362 XGMAC_IOWRITE_BITS(pdata, MAC_RSSAR, OB, 1);
363
364 wait = 1000;
365 while (wait--) {
366 if (!XGMAC_IOREAD_BITS(pdata, MAC_RSSAR, OB))
367 goto unlock;
368
369 usleep_range(1000, 1500);
370 }
371
372 ret = -EBUSY;
373
374unlock:
375 mutex_unlock(&pdata->rss_mutex);
376
377 return ret;
378}
379
380static int xgbe_write_rss_hash_key(struct xgbe_prv_data *pdata)
381{
382 unsigned int key_regs = sizeof(pdata->rss_key) / sizeof(u32);
383 unsigned int *key = (unsigned int *)&pdata->rss_key;
384 int ret;
385
386 while (key_regs--) {
387 ret = xgbe_write_rss_reg(pdata, XGBE_RSS_HASH_KEY_TYPE,
388 key_regs, *key++);
389 if (ret)
390 return ret;
391 }
392
393 return 0;
394}
395
396static int xgbe_write_rss_lookup_table(struct xgbe_prv_data *pdata)
397{
398 unsigned int i;
399 int ret;
400
401 for (i = 0; i < ARRAY_SIZE(pdata->rss_table); i++) {
402 ret = xgbe_write_rss_reg(pdata,
403 XGBE_RSS_LOOKUP_TABLE_TYPE, i,
404 pdata->rss_table[i]);
405 if (ret)
406 return ret;
407 }
408
409 return 0;
410}
411
Lendacky, Thomasf6ac8622014-11-04 16:07:23 -0600412static int xgbe_set_rss_hash_key(struct xgbe_prv_data *pdata, const u8 *key)
413{
414 memcpy(pdata->rss_key, key, sizeof(pdata->rss_key));
415
416 return xgbe_write_rss_hash_key(pdata);
417}
418
419static int xgbe_set_rss_lookup_table(struct xgbe_prv_data *pdata,
420 const u32 *table)
421{
422 unsigned int i;
423
424 for (i = 0; i < ARRAY_SIZE(pdata->rss_table); i++)
425 XGMAC_SET_BITS(pdata->rss_table[i], MAC_RSSDR, DMCH, table[i]);
426
427 return xgbe_write_rss_lookup_table(pdata);
428}
429
Lendacky, Thomas5b9dfe22014-11-04 16:07:02 -0600430static int xgbe_enable_rss(struct xgbe_prv_data *pdata)
431{
432 int ret;
433
434 if (!pdata->hw_feat.rss)
435 return -EOPNOTSUPP;
436
437 /* Program the hash key */
438 ret = xgbe_write_rss_hash_key(pdata);
439 if (ret)
440 return ret;
441
442 /* Program the lookup table */
443 ret = xgbe_write_rss_lookup_table(pdata);
444 if (ret)
445 return ret;
446
447 /* Set the RSS options */
448 XGMAC_IOWRITE(pdata, MAC_RSSCR, pdata->rss_options);
449
450 /* Enable RSS */
451 XGMAC_IOWRITE_BITS(pdata, MAC_RSSCR, RSSE, 1);
452
453 return 0;
454}
455
456static int xgbe_disable_rss(struct xgbe_prv_data *pdata)
457{
458 if (!pdata->hw_feat.rss)
459 return -EOPNOTSUPP;
460
461 XGMAC_IOWRITE_BITS(pdata, MAC_RSSCR, RSSE, 0);
462
463 return 0;
464}
465
466static void xgbe_config_rss(struct xgbe_prv_data *pdata)
467{
468 int ret;
469
470 if (!pdata->hw_feat.rss)
471 return;
472
473 if (pdata->netdev->features & NETIF_F_RXHASH)
474 ret = xgbe_enable_rss(pdata);
475 else
476 ret = xgbe_disable_rss(pdata);
477
478 if (ret)
479 netdev_err(pdata->netdev,
480 "error configuring RSS, RSS disabled\n");
481}
482
Lendacky, Thomas43e0dcf2016-11-03 13:18:16 -0500483static bool xgbe_is_pfc_queue(struct xgbe_prv_data *pdata,
484 unsigned int queue)
485{
486 unsigned int prio, tc;
487
488 for (prio = 0; prio < IEEE_8021QAZ_MAX_TCS; prio++) {
489 /* Does this queue handle the priority? */
490 if (pdata->prio2q_map[prio] != queue)
491 continue;
492
493 /* Get the Traffic Class for this priority */
494 tc = pdata->ets->prio_tc[prio];
495
496 /* Check if PFC is enabled for this traffic class */
497 if (pdata->pfc->pfc_en & (1 << tc))
498 return true;
499 }
500
501 return false;
502}
503
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500504static int xgbe_disable_tx_flow_control(struct xgbe_prv_data *pdata)
505{
506 unsigned int max_q_count, q_count;
507 unsigned int reg, reg_val;
508 unsigned int i;
509
510 /* Clear MTL flow control */
Lendacky, Thomas853eb162014-07-29 08:57:31 -0500511 for (i = 0; i < pdata->rx_q_count; i++)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500512 XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_Q_RQOMR, EHFC, 0);
513
514 /* Clear MAC flow control */
515 max_q_count = XGMAC_MAX_FLOW_CONTROL_QUEUES;
Lendacky, Thomas9fc69af2014-08-29 13:17:08 -0500516 q_count = min_t(unsigned int, pdata->tx_q_count, max_q_count);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500517 reg = MAC_Q0TFCR;
518 for (i = 0; i < q_count; i++) {
519 reg_val = XGMAC_IOREAD(pdata, reg);
520 XGMAC_SET_BITS(reg_val, MAC_Q0TFCR, TFE, 0);
521 XGMAC_IOWRITE(pdata, reg, reg_val);
522
523 reg += MAC_QTFCR_INC;
524 }
525
526 return 0;
527}
528
529static int xgbe_enable_tx_flow_control(struct xgbe_prv_data *pdata)
530{
Lendacky, Thomas8dba2a22016-02-17 11:48:48 -0600531 struct ieee_pfc *pfc = pdata->pfc;
532 struct ieee_ets *ets = pdata->ets;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500533 unsigned int max_q_count, q_count;
534 unsigned int reg, reg_val;
535 unsigned int i;
536
537 /* Set MTL flow control */
Lendacky, Thomas8dba2a22016-02-17 11:48:48 -0600538 for (i = 0; i < pdata->rx_q_count; i++) {
539 unsigned int ehfc = 0;
540
Lendacky, Thomas43e0dcf2016-11-03 13:18:16 -0500541 if (pdata->rx_rfd[i]) {
542 /* Flow control thresholds are established */
543 if (pfc && ets) {
544 if (xgbe_is_pfc_queue(pdata, i))
Lendacky, Thomas8dba2a22016-02-17 11:48:48 -0600545 ehfc = 1;
Lendacky, Thomas43e0dcf2016-11-03 13:18:16 -0500546 } else {
547 ehfc = 1;
Lendacky, Thomas8dba2a22016-02-17 11:48:48 -0600548 }
Lendacky, Thomas8dba2a22016-02-17 11:48:48 -0600549 }
550
551 XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_Q_RQOMR, EHFC, ehfc);
552
553 netif_dbg(pdata, drv, pdata->netdev,
554 "flow control %s for RXq%u\n",
555 ehfc ? "enabled" : "disabled", i);
556 }
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500557
558 /* Set MAC flow control */
559 max_q_count = XGMAC_MAX_FLOW_CONTROL_QUEUES;
Lendacky, Thomas9fc69af2014-08-29 13:17:08 -0500560 q_count = min_t(unsigned int, pdata->tx_q_count, max_q_count);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500561 reg = MAC_Q0TFCR;
562 for (i = 0; i < q_count; i++) {
563 reg_val = XGMAC_IOREAD(pdata, reg);
564
565 /* Enable transmit flow control */
566 XGMAC_SET_BITS(reg_val, MAC_Q0TFCR, TFE, 1);
567 /* Set pause time */
568 XGMAC_SET_BITS(reg_val, MAC_Q0TFCR, PT, 0xffff);
569
570 XGMAC_IOWRITE(pdata, reg, reg_val);
571
572 reg += MAC_QTFCR_INC;
573 }
574
575 return 0;
576}
577
578static int xgbe_disable_rx_flow_control(struct xgbe_prv_data *pdata)
579{
580 XGMAC_IOWRITE_BITS(pdata, MAC_RFCR, RFE, 0);
581
582 return 0;
583}
584
585static int xgbe_enable_rx_flow_control(struct xgbe_prv_data *pdata)
586{
587 XGMAC_IOWRITE_BITS(pdata, MAC_RFCR, RFE, 1);
588
589 return 0;
590}
591
592static int xgbe_config_tx_flow_control(struct xgbe_prv_data *pdata)
593{
Lendacky, Thomasfca2d992014-07-29 08:57:55 -0500594 struct ieee_pfc *pfc = pdata->pfc;
595
596 if (pdata->tx_pause || (pfc && pfc->pfc_en))
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500597 xgbe_enable_tx_flow_control(pdata);
598 else
599 xgbe_disable_tx_flow_control(pdata);
600
601 return 0;
602}
603
604static int xgbe_config_rx_flow_control(struct xgbe_prv_data *pdata)
605{
Lendacky, Thomasfca2d992014-07-29 08:57:55 -0500606 struct ieee_pfc *pfc = pdata->pfc;
607
608 if (pdata->rx_pause || (pfc && pfc->pfc_en))
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500609 xgbe_enable_rx_flow_control(pdata);
610 else
611 xgbe_disable_rx_flow_control(pdata);
612
613 return 0;
614}
615
616static void xgbe_config_flow_control(struct xgbe_prv_data *pdata)
617{
Lendacky, Thomasfca2d992014-07-29 08:57:55 -0500618 struct ieee_pfc *pfc = pdata->pfc;
619
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500620 xgbe_config_tx_flow_control(pdata);
621 xgbe_config_rx_flow_control(pdata);
Lendacky, Thomasfca2d992014-07-29 08:57:55 -0500622
623 XGMAC_IOWRITE_BITS(pdata, MAC_RFCR, PFCE,
624 (pfc && pfc->pfc_en) ? 1 : 0);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500625}
626
627static void xgbe_enable_dma_interrupts(struct xgbe_prv_data *pdata)
628{
629 struct xgbe_channel *channel;
630 unsigned int dma_ch_isr, dma_ch_ier;
631 unsigned int i;
632
Lendacky, Thomas4c70dd82016-11-10 17:10:17 -0600633 /* Set the interrupt mode if supported */
634 if (pdata->channel_irq_mode)
635 XGMAC_IOWRITE_BITS(pdata, DMA_MR, INTM,
636 pdata->channel_irq_mode);
637
Lendacky, Thomas18f9f0a2017-06-28 13:42:51 -0500638 for (i = 0; i < pdata->channel_count; i++) {
639 channel = pdata->channel[i];
640
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500641 /* Clear all the interrupts which are set */
642 dma_ch_isr = XGMAC_DMA_IOREAD(channel, DMA_CH_SR);
643 XGMAC_DMA_IOWRITE(channel, DMA_CH_SR, dma_ch_isr);
644
645 /* Clear all interrupt enable bits */
646 dma_ch_ier = 0;
647
648 /* Enable following interrupts
649 * NIE - Normal Interrupt Summary Enable
650 * AIE - Abnormal Interrupt Summary Enable
651 * FBEE - Fatal Bus Error Enable
652 */
653 XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, NIE, 1);
654 XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, AIE, 1);
655 XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, FBEE, 1);
656
657 if (channel->tx_ring) {
658 /* Enable the following Tx interrupts
Lendacky, Thomas9227dc52014-11-04 16:06:56 -0600659 * TIE - Transmit Interrupt Enable (unless using
Lendacky, Thomas4c70dd82016-11-10 17:10:17 -0600660 * per channel interrupts in edge triggered
661 * mode)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500662 */
Lendacky, Thomas4c70dd82016-11-10 17:10:17 -0600663 if (!pdata->per_channel_irq || pdata->channel_irq_mode)
Lendacky, Thomas9227dc52014-11-04 16:06:56 -0600664 XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, TIE, 1);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500665 }
666 if (channel->rx_ring) {
667 /* Enable following Rx interrupts
668 * RBUE - Receive Buffer Unavailable Enable
Lendacky, Thomas9227dc52014-11-04 16:06:56 -0600669 * RIE - Receive Interrupt Enable (unless using
Lendacky, Thomas4c70dd82016-11-10 17:10:17 -0600670 * per channel interrupts in edge triggered
671 * mode)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500672 */
673 XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, RBUE, 1);
Lendacky, Thomas4c70dd82016-11-10 17:10:17 -0600674 if (!pdata->per_channel_irq || pdata->channel_irq_mode)
Lendacky, Thomas9227dc52014-11-04 16:06:56 -0600675 XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, RIE, 1);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500676 }
677
678 XGMAC_DMA_IOWRITE(channel, DMA_CH_IER, dma_ch_ier);
679 }
680}
681
682static void xgbe_enable_mtl_interrupts(struct xgbe_prv_data *pdata)
683{
684 unsigned int mtl_q_isr;
685 unsigned int q_count, i;
686
687 q_count = max(pdata->hw_feat.tx_q_cnt, pdata->hw_feat.rx_q_cnt);
688 for (i = 0; i < q_count; i++) {
689 /* Clear all the interrupts which are set */
690 mtl_q_isr = XGMAC_MTL_IOREAD(pdata, i, MTL_Q_ISR);
691 XGMAC_MTL_IOWRITE(pdata, i, MTL_Q_ISR, mtl_q_isr);
692
693 /* No MTL interrupts to be enabled */
Lendacky, Thomas91f87342014-07-02 13:04:34 -0500694 XGMAC_MTL_IOWRITE(pdata, i, MTL_Q_IER, 0);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500695 }
696}
697
698static void xgbe_enable_mac_interrupts(struct xgbe_prv_data *pdata)
699{
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -0500700 unsigned int mac_ier = 0;
701
702 /* Enable Timestamp interrupt */
703 XGMAC_SET_BITS(mac_ier, MAC_IER, TSIE, 1);
704
705 XGMAC_IOWRITE(pdata, MAC_IER, mac_ier);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500706
707 /* Enable all counter interrupts */
Lendacky, Thomasa3ba7c92014-09-05 18:02:36 -0500708 XGMAC_IOWRITE_BITS(pdata, MMC_RIER, ALL_INTERRUPTS, 0xffffffff);
709 XGMAC_IOWRITE_BITS(pdata, MMC_TIER, ALL_INTERRUPTS, 0xffffffff);
Lendacky, Thomas732f2ab2016-11-10 17:11:14 -0600710
711 /* Enable MDIO single command completion interrupt */
712 XGMAC_IOWRITE_BITS(pdata, MAC_MDIOIER, SNGLCOMPIE, 1);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500713}
714
Lendacky, Thomase78332b2016-11-10 17:10:26 -0600715static void xgbe_enable_ecc_interrupts(struct xgbe_prv_data *pdata)
716{
717 unsigned int ecc_isr, ecc_ier = 0;
718
719 if (!pdata->vdata->ecc_support)
720 return;
721
722 /* Clear all the interrupts which are set */
723 ecc_isr = XP_IOREAD(pdata, XP_ECC_ISR);
724 XP_IOWRITE(pdata, XP_ECC_ISR, ecc_isr);
725
726 /* Enable ECC interrupts */
727 XP_SET_BITS(ecc_ier, XP_ECC_IER, TX_DED, 1);
728 XP_SET_BITS(ecc_ier, XP_ECC_IER, TX_SEC, 1);
729 XP_SET_BITS(ecc_ier, XP_ECC_IER, RX_DED, 1);
730 XP_SET_BITS(ecc_ier, XP_ECC_IER, RX_SEC, 1);
731 XP_SET_BITS(ecc_ier, XP_ECC_IER, DESC_DED, 1);
732 XP_SET_BITS(ecc_ier, XP_ECC_IER, DESC_SEC, 1);
733
734 XP_IOWRITE(pdata, XP_ECC_IER, ecc_ier);
735}
736
737static void xgbe_disable_ecc_ded(struct xgbe_prv_data *pdata)
738{
739 unsigned int ecc_ier;
740
741 ecc_ier = XP_IOREAD(pdata, XP_ECC_IER);
742
743 /* Disable ECC DED interrupts */
744 XP_SET_BITS(ecc_ier, XP_ECC_IER, TX_DED, 0);
745 XP_SET_BITS(ecc_ier, XP_ECC_IER, RX_DED, 0);
746 XP_SET_BITS(ecc_ier, XP_ECC_IER, DESC_DED, 0);
747
748 XP_IOWRITE(pdata, XP_ECC_IER, ecc_ier);
749}
750
751static void xgbe_disable_ecc_sec(struct xgbe_prv_data *pdata,
752 enum xgbe_ecc_sec sec)
753{
754 unsigned int ecc_ier;
755
756 ecc_ier = XP_IOREAD(pdata, XP_ECC_IER);
757
758 /* Disable ECC SEC interrupt */
759 switch (sec) {
760 case XGBE_ECC_SEC_TX:
761 XP_SET_BITS(ecc_ier, XP_ECC_IER, TX_SEC, 0);
762 break;
763 case XGBE_ECC_SEC_RX:
764 XP_SET_BITS(ecc_ier, XP_ECC_IER, RX_SEC, 0);
765 break;
766 case XGBE_ECC_SEC_DESC:
767 XP_SET_BITS(ecc_ier, XP_ECC_IER, DESC_SEC, 0);
768 break;
769 }
770
771 XP_IOWRITE(pdata, XP_ECC_IER, ecc_ier);
772}
773
Lendacky, Thomase57f7a32016-11-03 13:18:27 -0500774static int xgbe_set_speed(struct xgbe_prv_data *pdata, int speed)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500775{
Lendacky, Thomase57f7a32016-11-03 13:18:27 -0500776 unsigned int ss;
Lendacky, Thomasc3152d42015-01-16 12:47:00 -0600777
Lendacky, Thomase57f7a32016-11-03 13:18:27 -0500778 switch (speed) {
779 case SPEED_1000:
780 ss = 0x03;
781 break;
782 case SPEED_2500:
783 ss = 0x02;
784 break;
785 case SPEED_10000:
786 ss = 0x00;
787 break;
788 default:
789 return -EINVAL;
790 }
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500791
Lendacky, Thomase57f7a32016-11-03 13:18:27 -0500792 if (XGMAC_IOREAD_BITS(pdata, MAC_TCR, SS) != ss)
793 XGMAC_IOWRITE_BITS(pdata, MAC_TCR, SS, ss);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500794
795 return 0;
796}
797
Lendacky, Thomasb4eee842016-02-17 11:48:08 -0600798static int xgbe_enable_rx_vlan_stripping(struct xgbe_prv_data *pdata)
799{
800 /* Put the VLAN tag in the Rx descriptor */
801 XGMAC_IOWRITE_BITS(pdata, MAC_VLANTR, EVLRXS, 1);
802
803 /* Don't check the VLAN type */
804 XGMAC_IOWRITE_BITS(pdata, MAC_VLANTR, DOVLTC, 1);
805
806 /* Check only C-TAG (0x8100) packets */
807 XGMAC_IOWRITE_BITS(pdata, MAC_VLANTR, ERSVLM, 0);
808
809 /* Don't consider an S-TAG (0x88A8) packet as a VLAN packet */
810 XGMAC_IOWRITE_BITS(pdata, MAC_VLANTR, ESVL, 0);
811
812 /* Enable VLAN tag stripping */
813 XGMAC_IOWRITE_BITS(pdata, MAC_VLANTR, EVLS, 0x3);
814
815 return 0;
816}
817
818static int xgbe_disable_rx_vlan_stripping(struct xgbe_prv_data *pdata)
819{
820 XGMAC_IOWRITE_BITS(pdata, MAC_VLANTR, EVLS, 0);
821
822 return 0;
823}
824
825static int xgbe_enable_rx_vlan_filtering(struct xgbe_prv_data *pdata)
826{
827 /* Enable VLAN filtering */
828 XGMAC_IOWRITE_BITS(pdata, MAC_PFR, VTFE, 1);
829
830 /* Enable VLAN Hash Table filtering */
831 XGMAC_IOWRITE_BITS(pdata, MAC_VLANTR, VTHM, 1);
832
833 /* Disable VLAN tag inverse matching */
834 XGMAC_IOWRITE_BITS(pdata, MAC_VLANTR, VTIM, 0);
835
836 /* Only filter on the lower 12-bits of the VLAN tag */
837 XGMAC_IOWRITE_BITS(pdata, MAC_VLANTR, ETV, 1);
838
839 /* In order for the VLAN Hash Table filtering to be effective,
840 * the VLAN tag identifier in the VLAN Tag Register must not
841 * be zero. Set the VLAN tag identifier to "1" to enable the
842 * VLAN Hash Table filtering. This implies that a VLAN tag of
843 * 1 will always pass filtering.
844 */
845 XGMAC_IOWRITE_BITS(pdata, MAC_VLANTR, VL, 1);
846
847 return 0;
848}
849
850static int xgbe_disable_rx_vlan_filtering(struct xgbe_prv_data *pdata)
851{
852 /* Disable VLAN filtering */
853 XGMAC_IOWRITE_BITS(pdata, MAC_PFR, VTFE, 0);
854
855 return 0;
856}
857
858static u32 xgbe_vid_crc32_le(__le16 vid_le)
859{
860 u32 poly = 0xedb88320; /* CRCPOLY_LE */
861 u32 crc = ~0;
862 u32 temp = 0;
863 unsigned char *data = (unsigned char *)&vid_le;
864 unsigned char data_byte = 0;
865 int i, bits;
866
867 bits = get_bitmask_order(VLAN_VID_MASK);
868 for (i = 0; i < bits; i++) {
869 if ((i % 8) == 0)
870 data_byte = data[i / 8];
871
872 temp = ((crc & 1) ^ data_byte) & 1;
873 crc >>= 1;
874 data_byte >>= 1;
875
876 if (temp)
877 crc ^= poly;
878 }
879
880 return crc;
881}
882
883static int xgbe_update_vlan_hash_table(struct xgbe_prv_data *pdata)
884{
885 u32 crc;
886 u16 vid;
887 __le16 vid_le;
888 u16 vlan_hash_table = 0;
889
890 /* Generate the VLAN Hash Table value */
891 for_each_set_bit(vid, pdata->active_vlans, VLAN_N_VID) {
892 /* Get the CRC32 value of the VLAN ID */
893 vid_le = cpu_to_le16(vid);
894 crc = bitrev32(~xgbe_vid_crc32_le(vid_le)) >> 28;
895
896 vlan_hash_table |= (1 << crc);
897 }
898
899 /* Set the VLAN Hash Table filtering register */
900 XGMAC_IOWRITE_BITS(pdata, MAC_VLANHTR, VLHT, vlan_hash_table);
901
902 return 0;
903}
904
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500905static int xgbe_set_promiscuous_mode(struct xgbe_prv_data *pdata,
906 unsigned int enable)
907{
908 unsigned int val = enable ? 1 : 0;
909
910 if (XGMAC_IOREAD_BITS(pdata, MAC_PFR, PR) == val)
911 return 0;
912
Lendacky, Thomas34bf65d2015-05-14 11:44:03 -0500913 netif_dbg(pdata, drv, pdata->netdev, "%s promiscuous mode\n",
914 enable ? "entering" : "leaving");
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500915 XGMAC_IOWRITE_BITS(pdata, MAC_PFR, PR, val);
916
Lendacky, Thomasb4eee842016-02-17 11:48:08 -0600917 /* Hardware will still perform VLAN filtering in promiscuous mode */
918 if (enable) {
919 xgbe_disable_rx_vlan_filtering(pdata);
920 } else {
921 if (pdata->netdev->features & NETIF_F_HW_VLAN_CTAG_FILTER)
922 xgbe_enable_rx_vlan_filtering(pdata);
923 }
924
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500925 return 0;
926}
927
928static int xgbe_set_all_multicast_mode(struct xgbe_prv_data *pdata,
929 unsigned int enable)
930{
931 unsigned int val = enable ? 1 : 0;
932
933 if (XGMAC_IOREAD_BITS(pdata, MAC_PFR, PM) == val)
934 return 0;
935
Lendacky, Thomas34bf65d2015-05-14 11:44:03 -0500936 netif_dbg(pdata, drv, pdata->netdev, "%s allmulti mode\n",
937 enable ? "entering" : "leaving");
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500938 XGMAC_IOWRITE_BITS(pdata, MAC_PFR, PM, val);
939
940 return 0;
941}
942
Lendacky, Thomasb85e4d82014-06-24 16:19:29 -0500943static void xgbe_set_mac_reg(struct xgbe_prv_data *pdata,
944 struct netdev_hw_addr *ha, unsigned int *mac_reg)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500945{
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500946 unsigned int mac_addr_hi, mac_addr_lo;
947 u8 *mac_addr;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500948
Lendacky, Thomasb85e4d82014-06-24 16:19:29 -0500949 mac_addr_lo = 0;
950 mac_addr_hi = 0;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500951
Lendacky, Thomasb85e4d82014-06-24 16:19:29 -0500952 if (ha) {
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500953 mac_addr = (u8 *)&mac_addr_lo;
954 mac_addr[0] = ha->addr[0];
955 mac_addr[1] = ha->addr[1];
956 mac_addr[2] = ha->addr[2];
957 mac_addr[3] = ha->addr[3];
958 mac_addr = (u8 *)&mac_addr_hi;
959 mac_addr[0] = ha->addr[4];
960 mac_addr[1] = ha->addr[5];
961
Lendacky, Thomas34bf65d2015-05-14 11:44:03 -0500962 netif_dbg(pdata, drv, pdata->netdev,
963 "adding mac address %pM at %#x\n",
964 ha->addr, *mac_reg);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500965
966 XGMAC_SET_BITS(mac_addr_hi, MAC_MACA1HR, AE, 1);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500967 }
968
Lendacky, Thomasb85e4d82014-06-24 16:19:29 -0500969 XGMAC_IOWRITE(pdata, *mac_reg, mac_addr_hi);
970 *mac_reg += MAC_MACA_INC;
971 XGMAC_IOWRITE(pdata, *mac_reg, mac_addr_lo);
972 *mac_reg += MAC_MACA_INC;
973}
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500974
Lendacky, Thomasb85e4d82014-06-24 16:19:29 -0500975static void xgbe_set_mac_addn_addrs(struct xgbe_prv_data *pdata)
976{
977 struct net_device *netdev = pdata->netdev;
978 struct netdev_hw_addr *ha;
979 unsigned int mac_reg;
980 unsigned int addn_macs;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500981
Lendacky, Thomasb85e4d82014-06-24 16:19:29 -0500982 mac_reg = MAC_MACA1HR;
983 addn_macs = pdata->hw_feat.addn_mac;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500984
Lendacky, Thomasb85e4d82014-06-24 16:19:29 -0500985 if (netdev_uc_count(netdev) > addn_macs) {
986 xgbe_set_promiscuous_mode(pdata, 1);
987 } else {
988 netdev_for_each_uc_addr(ha, netdev) {
989 xgbe_set_mac_reg(pdata, ha, &mac_reg);
990 addn_macs--;
991 }
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500992
Lendacky, Thomasb85e4d82014-06-24 16:19:29 -0500993 if (netdev_mc_count(netdev) > addn_macs) {
994 xgbe_set_all_multicast_mode(pdata, 1);
995 } else {
996 netdev_for_each_mc_addr(ha, netdev) {
997 xgbe_set_mac_reg(pdata, ha, &mac_reg);
998 addn_macs--;
999 }
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001000 }
1001 }
1002
1003 /* Clear remaining additional MAC address entries */
Lendacky, Thomasb85e4d82014-06-24 16:19:29 -05001004 while (addn_macs--)
1005 xgbe_set_mac_reg(pdata, NULL, &mac_reg);
1006}
1007
1008static void xgbe_set_mac_hash_table(struct xgbe_prv_data *pdata)
1009{
1010 struct net_device *netdev = pdata->netdev;
1011 struct netdev_hw_addr *ha;
1012 unsigned int hash_reg;
1013 unsigned int hash_table_shift, hash_table_count;
1014 u32 hash_table[XGBE_MAC_HASH_TABLE_SIZE];
1015 u32 crc;
1016 unsigned int i;
1017
1018 hash_table_shift = 26 - (pdata->hw_feat.hash_table_size >> 7);
1019 hash_table_count = pdata->hw_feat.hash_table_size / 32;
1020 memset(hash_table, 0, sizeof(hash_table));
1021
1022 /* Build the MAC Hash Table register values */
1023 netdev_for_each_uc_addr(ha, netdev) {
1024 crc = bitrev32(~crc32_le(~0, ha->addr, ETH_ALEN));
1025 crc >>= hash_table_shift;
1026 hash_table[crc >> 5] |= (1 << (crc & 0x1f));
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001027 }
1028
Lendacky, Thomasb85e4d82014-06-24 16:19:29 -05001029 netdev_for_each_mc_addr(ha, netdev) {
1030 crc = bitrev32(~crc32_le(~0, ha->addr, ETH_ALEN));
1031 crc >>= hash_table_shift;
1032 hash_table[crc >> 5] |= (1 << (crc & 0x1f));
1033 }
1034
1035 /* Set the MAC Hash Table registers */
1036 hash_reg = MAC_HTR0;
1037 for (i = 0; i < hash_table_count; i++) {
1038 XGMAC_IOWRITE(pdata, hash_reg, hash_table[i]);
1039 hash_reg += MAC_HTR_INC;
1040 }
1041}
1042
1043static int xgbe_add_mac_addresses(struct xgbe_prv_data *pdata)
1044{
1045 if (pdata->hw_feat.hash_table_size)
1046 xgbe_set_mac_hash_table(pdata);
1047 else
1048 xgbe_set_mac_addn_addrs(pdata);
1049
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001050 return 0;
1051}
1052
1053static int xgbe_set_mac_address(struct xgbe_prv_data *pdata, u8 *addr)
1054{
1055 unsigned int mac_addr_hi, mac_addr_lo;
1056
1057 mac_addr_hi = (addr[5] << 8) | (addr[4] << 0);
1058 mac_addr_lo = (addr[3] << 24) | (addr[2] << 16) |
1059 (addr[1] << 8) | (addr[0] << 0);
1060
1061 XGMAC_IOWRITE(pdata, MAC_MACA0HR, mac_addr_hi);
1062 XGMAC_IOWRITE(pdata, MAC_MACA0LR, mac_addr_lo);
1063
1064 return 0;
1065}
1066
Lendacky, Thomasb8763822015-04-09 12:11:57 -05001067static int xgbe_config_rx_mode(struct xgbe_prv_data *pdata)
1068{
1069 struct net_device *netdev = pdata->netdev;
1070 unsigned int pr_mode, am_mode;
1071
1072 pr_mode = ((netdev->flags & IFF_PROMISC) != 0);
1073 am_mode = ((netdev->flags & IFF_ALLMULTI) != 0);
1074
1075 xgbe_set_promiscuous_mode(pdata, pr_mode);
1076 xgbe_set_all_multicast_mode(pdata, am_mode);
1077
1078 xgbe_add_mac_addresses(pdata);
1079
1080 return 0;
1081}
1082
Lendacky, Thomas732f2ab2016-11-10 17:11:14 -06001083static int xgbe_clr_gpio(struct xgbe_prv_data *pdata, unsigned int gpio)
1084{
1085 unsigned int reg;
1086
Lendacky, Thomas1c1f6192016-11-15 16:11:15 -06001087 if (gpio > 15)
Lendacky, Thomas732f2ab2016-11-10 17:11:14 -06001088 return -EINVAL;
1089
1090 reg = XGMAC_IOREAD(pdata, MAC_GPIOSR);
1091
1092 reg &= ~(1 << (gpio + 16));
1093 XGMAC_IOWRITE(pdata, MAC_GPIOSR, reg);
1094
1095 return 0;
1096}
1097
1098static int xgbe_set_gpio(struct xgbe_prv_data *pdata, unsigned int gpio)
1099{
1100 unsigned int reg;
1101
Lendacky, Thomas1c1f6192016-11-15 16:11:15 -06001102 if (gpio > 15)
Lendacky, Thomas732f2ab2016-11-10 17:11:14 -06001103 return -EINVAL;
1104
1105 reg = XGMAC_IOREAD(pdata, MAC_GPIOSR);
1106
1107 reg |= (1 << (gpio + 16));
1108 XGMAC_IOWRITE(pdata, MAC_GPIOSR, reg);
1109
1110 return 0;
1111}
1112
Lendacky, Thomasb03a4a62016-11-03 13:18:56 -05001113static int xgbe_read_mmd_regs_v2(struct xgbe_prv_data *pdata, int prtad,
1114 int mmd_reg)
1115{
1116 unsigned long flags;
1117 unsigned int mmd_address, index, offset;
1118 int mmd_data;
1119
1120 if (mmd_reg & MII_ADDR_C45)
1121 mmd_address = mmd_reg & ~MII_ADDR_C45;
1122 else
1123 mmd_address = (pdata->mdio_mmd << 16) | (mmd_reg & 0xffff);
1124
1125 /* The PCS registers are accessed using mmio. The underlying
1126 * management interface uses indirect addressing to access the MMD
1127 * register sets. This requires accessing of the PCS register in two
1128 * phases, an address phase and a data phase.
1129 *
1130 * The mmio interface is based on 16-bit offsets and values. All
1131 * register offsets must therefore be adjusted by left shifting the
1132 * offset 1 bit and reading 16 bits of data.
1133 */
1134 mmd_address <<= 1;
1135 index = mmd_address & ~pdata->xpcs_window_mask;
1136 offset = pdata->xpcs_window + (mmd_address & pdata->xpcs_window_mask);
1137
1138 spin_lock_irqsave(&pdata->xpcs_lock, flags);
Lendacky, Thomas4eccbfc2017-01-20 12:14:03 -06001139 XPCS32_IOWRITE(pdata, pdata->xpcs_window_sel_reg, index);
Lendacky, Thomasb03a4a62016-11-03 13:18:56 -05001140 mmd_data = XPCS16_IOREAD(pdata, offset);
1141 spin_unlock_irqrestore(&pdata->xpcs_lock, flags);
1142
1143 return mmd_data;
1144}
1145
1146static void xgbe_write_mmd_regs_v2(struct xgbe_prv_data *pdata, int prtad,
1147 int mmd_reg, int mmd_data)
1148{
1149 unsigned long flags;
1150 unsigned int mmd_address, index, offset;
1151
1152 if (mmd_reg & MII_ADDR_C45)
1153 mmd_address = mmd_reg & ~MII_ADDR_C45;
1154 else
1155 mmd_address = (pdata->mdio_mmd << 16) | (mmd_reg & 0xffff);
1156
1157 /* The PCS registers are accessed using mmio. The underlying
1158 * management interface uses indirect addressing to access the MMD
1159 * register sets. This requires accessing of the PCS register in two
1160 * phases, an address phase and a data phase.
1161 *
1162 * The mmio interface is based on 16-bit offsets and values. All
1163 * register offsets must therefore be adjusted by left shifting the
1164 * offset 1 bit and writing 16 bits of data.
1165 */
1166 mmd_address <<= 1;
1167 index = mmd_address & ~pdata->xpcs_window_mask;
1168 offset = pdata->xpcs_window + (mmd_address & pdata->xpcs_window_mask);
1169
1170 spin_lock_irqsave(&pdata->xpcs_lock, flags);
Lendacky, Thomas4eccbfc2017-01-20 12:14:03 -06001171 XPCS32_IOWRITE(pdata, pdata->xpcs_window_sel_reg, index);
Lendacky, Thomasb03a4a62016-11-03 13:18:56 -05001172 XPCS16_IOWRITE(pdata, offset, mmd_data);
1173 spin_unlock_irqrestore(&pdata->xpcs_lock, flags);
1174}
1175
1176static int xgbe_read_mmd_regs_v1(struct xgbe_prv_data *pdata, int prtad,
1177 int mmd_reg)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001178{
Lendacky, Thomasced3fca2016-02-17 11:49:28 -06001179 unsigned long flags;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001180 unsigned int mmd_address;
1181 int mmd_data;
1182
1183 if (mmd_reg & MII_ADDR_C45)
1184 mmd_address = mmd_reg & ~MII_ADDR_C45;
1185 else
1186 mmd_address = (pdata->mdio_mmd << 16) | (mmd_reg & 0xffff);
1187
1188 /* The PCS registers are accessed using mmio. The underlying APB3
1189 * management interface uses indirect addressing to access the MMD
1190 * register sets. This requires accessing of the PCS register in two
1191 * phases, an address phase and a data phase.
1192 *
1193 * The mmio interface is based on 32-bit offsets and values. All
1194 * register offsets must therefore be adjusted by left shifting the
1195 * offset 2 bits and reading 32 bits of data.
1196 */
Lendacky, Thomasced3fca2016-02-17 11:49:28 -06001197 spin_lock_irqsave(&pdata->xpcs_lock, flags);
Lendacky, Thomasb03a4a62016-11-03 13:18:56 -05001198 XPCS32_IOWRITE(pdata, PCS_V1_WINDOW_SELECT, mmd_address >> 8);
1199 mmd_data = XPCS32_IOREAD(pdata, (mmd_address & 0xff) << 2);
Lendacky, Thomasced3fca2016-02-17 11:49:28 -06001200 spin_unlock_irqrestore(&pdata->xpcs_lock, flags);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001201
1202 return mmd_data;
1203}
1204
Lendacky, Thomasb03a4a62016-11-03 13:18:56 -05001205static void xgbe_write_mmd_regs_v1(struct xgbe_prv_data *pdata, int prtad,
1206 int mmd_reg, int mmd_data)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001207{
1208 unsigned int mmd_address;
Lendacky, Thomasced3fca2016-02-17 11:49:28 -06001209 unsigned long flags;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001210
1211 if (mmd_reg & MII_ADDR_C45)
1212 mmd_address = mmd_reg & ~MII_ADDR_C45;
1213 else
1214 mmd_address = (pdata->mdio_mmd << 16) | (mmd_reg & 0xffff);
1215
1216 /* The PCS registers are accessed using mmio. The underlying APB3
1217 * management interface uses indirect addressing to access the MMD
1218 * register sets. This requires accessing of the PCS register in two
1219 * phases, an address phase and a data phase.
1220 *
1221 * The mmio interface is based on 32-bit offsets and values. All
1222 * register offsets must therefore be adjusted by left shifting the
Lendacky, Thomasb03a4a62016-11-03 13:18:56 -05001223 * offset 2 bits and writing 32 bits of data.
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001224 */
Lendacky, Thomasced3fca2016-02-17 11:49:28 -06001225 spin_lock_irqsave(&pdata->xpcs_lock, flags);
Lendacky, Thomasb03a4a62016-11-03 13:18:56 -05001226 XPCS32_IOWRITE(pdata, PCS_V1_WINDOW_SELECT, mmd_address >> 8);
1227 XPCS32_IOWRITE(pdata, (mmd_address & 0xff) << 2, mmd_data);
Lendacky, Thomasced3fca2016-02-17 11:49:28 -06001228 spin_unlock_irqrestore(&pdata->xpcs_lock, flags);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001229}
1230
Lendacky, Thomasb03a4a62016-11-03 13:18:56 -05001231static int xgbe_read_mmd_regs(struct xgbe_prv_data *pdata, int prtad,
1232 int mmd_reg)
1233{
1234 switch (pdata->vdata->xpcs_access) {
1235 case XGBE_XPCS_ACCESS_V1:
1236 return xgbe_read_mmd_regs_v1(pdata, prtad, mmd_reg);
1237
1238 case XGBE_XPCS_ACCESS_V2:
1239 default:
1240 return xgbe_read_mmd_regs_v2(pdata, prtad, mmd_reg);
1241 }
1242}
1243
1244static void xgbe_write_mmd_regs(struct xgbe_prv_data *pdata, int prtad,
1245 int mmd_reg, int mmd_data)
1246{
1247 switch (pdata->vdata->xpcs_access) {
1248 case XGBE_XPCS_ACCESS_V1:
1249 return xgbe_write_mmd_regs_v1(pdata, prtad, mmd_reg, mmd_data);
1250
1251 case XGBE_XPCS_ACCESS_V2:
1252 default:
1253 return xgbe_write_mmd_regs_v2(pdata, prtad, mmd_reg, mmd_data);
1254 }
1255}
1256
Lendacky, Thomas732f2ab2016-11-10 17:11:14 -06001257static int xgbe_write_ext_mii_regs(struct xgbe_prv_data *pdata, int addr,
1258 int reg, u16 val)
1259{
1260 unsigned int mdio_sca, mdio_sccd;
1261
1262 reinit_completion(&pdata->mdio_complete);
1263
1264 mdio_sca = 0;
1265 XGMAC_SET_BITS(mdio_sca, MAC_MDIOSCAR, REG, reg);
1266 XGMAC_SET_BITS(mdio_sca, MAC_MDIOSCAR, DA, addr);
1267 XGMAC_IOWRITE(pdata, MAC_MDIOSCAR, mdio_sca);
1268
1269 mdio_sccd = 0;
1270 XGMAC_SET_BITS(mdio_sccd, MAC_MDIOSCCDR, DATA, val);
1271 XGMAC_SET_BITS(mdio_sccd, MAC_MDIOSCCDR, CMD, 1);
1272 XGMAC_SET_BITS(mdio_sccd, MAC_MDIOSCCDR, BUSY, 1);
1273 XGMAC_IOWRITE(pdata, MAC_MDIOSCCDR, mdio_sccd);
1274
1275 if (!wait_for_completion_timeout(&pdata->mdio_complete, HZ)) {
1276 netdev_err(pdata->netdev, "mdio write operation timed out\n");
1277 return -ETIMEDOUT;
1278 }
1279
1280 return 0;
1281}
1282
1283static int xgbe_read_ext_mii_regs(struct xgbe_prv_data *pdata, int addr,
1284 int reg)
1285{
1286 unsigned int mdio_sca, mdio_sccd;
1287
1288 reinit_completion(&pdata->mdio_complete);
1289
1290 mdio_sca = 0;
1291 XGMAC_SET_BITS(mdio_sca, MAC_MDIOSCAR, REG, reg);
1292 XGMAC_SET_BITS(mdio_sca, MAC_MDIOSCAR, DA, addr);
1293 XGMAC_IOWRITE(pdata, MAC_MDIOSCAR, mdio_sca);
1294
1295 mdio_sccd = 0;
1296 XGMAC_SET_BITS(mdio_sccd, MAC_MDIOSCCDR, CMD, 3);
1297 XGMAC_SET_BITS(mdio_sccd, MAC_MDIOSCCDR, BUSY, 1);
1298 XGMAC_IOWRITE(pdata, MAC_MDIOSCCDR, mdio_sccd);
1299
1300 if (!wait_for_completion_timeout(&pdata->mdio_complete, HZ)) {
1301 netdev_err(pdata->netdev, "mdio read operation timed out\n");
1302 return -ETIMEDOUT;
1303 }
1304
1305 return XGMAC_IOREAD_BITS(pdata, MAC_MDIOSCCDR, DATA);
1306}
1307
1308static int xgbe_set_ext_mii_mode(struct xgbe_prv_data *pdata, unsigned int port,
1309 enum xgbe_mdio_mode mode)
1310{
Lendacky, Thomasb42c6762017-02-28 15:03:01 -06001311 unsigned int reg_val = XGMAC_IOREAD(pdata, MAC_MDIOCL22R);
Lendacky, Thomas732f2ab2016-11-10 17:11:14 -06001312
1313 switch (mode) {
1314 case XGBE_MDIO_MODE_CL22:
1315 if (port > XGMAC_MAX_C22_PORT)
1316 return -EINVAL;
1317 reg_val |= (1 << port);
1318 break;
1319 case XGBE_MDIO_MODE_CL45:
1320 break;
1321 default:
1322 return -EINVAL;
1323 }
1324
1325 XGMAC_IOWRITE(pdata, MAC_MDIOCL22R, reg_val);
1326
1327 return 0;
1328}
1329
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001330static int xgbe_tx_complete(struct xgbe_ring_desc *rdesc)
1331{
1332 return !XGMAC_GET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, OWN);
1333}
1334
1335static int xgbe_disable_rx_csum(struct xgbe_prv_data *pdata)
1336{
1337 XGMAC_IOWRITE_BITS(pdata, MAC_RCR, IPC, 0);
1338
1339 return 0;
1340}
1341
1342static int xgbe_enable_rx_csum(struct xgbe_prv_data *pdata)
1343{
1344 XGMAC_IOWRITE_BITS(pdata, MAC_RCR, IPC, 1);
1345
1346 return 0;
1347}
1348
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001349static void xgbe_tx_desc_reset(struct xgbe_ring_data *rdata)
1350{
1351 struct xgbe_ring_desc *rdesc = rdata->rdesc;
1352
1353 /* Reset the Tx descriptor
1354 * Set buffer 1 (lo) address to zero
1355 * Set buffer 1 (hi) address to zero
1356 * Reset all other control bits (IC, TTSE, B2L & B1L)
1357 * Reset all other control bits (OWN, CTXT, FD, LD, CPC, CIC, etc)
1358 */
1359 rdesc->desc0 = 0;
1360 rdesc->desc1 = 0;
1361 rdesc->desc2 = 0;
1362 rdesc->desc3 = 0;
Lendacky, Thomas08dcc472014-11-04 16:06:44 -06001363
1364 /* Make sure ownership is written to the descriptor */
Lendacky, Thomasceb8f6b2015-03-20 11:50:16 -05001365 dma_wmb();
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001366}
1367
1368static void xgbe_tx_desc_init(struct xgbe_channel *channel)
1369{
1370 struct xgbe_ring *ring = channel->tx_ring;
1371 struct xgbe_ring_data *rdata;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001372 int i;
1373 int start_index = ring->cur;
1374
1375 DBGPR("-->tx_desc_init\n");
1376
1377 /* Initialze all descriptors */
1378 for (i = 0; i < ring->rdesc_count; i++) {
Lendacky, Thomasd0a8ba62014-06-24 16:19:06 -05001379 rdata = XGBE_GET_DESC_DATA(ring, i);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001380
Lendacky, Thomas08dcc472014-11-04 16:06:44 -06001381 /* Initialize Tx descriptor */
1382 xgbe_tx_desc_reset(rdata);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001383 }
1384
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001385 /* Update the total number of Tx descriptors */
1386 XGMAC_DMA_IOWRITE(channel, DMA_CH_TDRLR, ring->rdesc_count - 1);
1387
1388 /* Update the starting address of descriptor ring */
Lendacky, Thomasd0a8ba62014-06-24 16:19:06 -05001389 rdata = XGBE_GET_DESC_DATA(ring, start_index);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001390 XGMAC_DMA_IOWRITE(channel, DMA_CH_TDLR_HI,
1391 upper_32_bits(rdata->rdesc_dma));
1392 XGMAC_DMA_IOWRITE(channel, DMA_CH_TDLR_LO,
1393 lower_32_bits(rdata->rdesc_dma));
1394
1395 DBGPR("<--tx_desc_init\n");
1396}
1397
Lendacky, Thomas8dee19e2015-04-09 12:11:51 -05001398static void xgbe_rx_desc_reset(struct xgbe_prv_data *pdata,
1399 struct xgbe_ring_data *rdata, unsigned int index)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001400{
1401 struct xgbe_ring_desc *rdesc = rdata->rdesc;
Lendacky, Thomas8dee19e2015-04-09 12:11:51 -05001402 unsigned int rx_usecs = pdata->rx_usecs;
1403 unsigned int rx_frames = pdata->rx_frames;
1404 unsigned int inte;
Lendacky, Thomascfbfd862015-07-06 11:57:37 -05001405 dma_addr_t hdr_dma, buf_dma;
Lendacky, Thomas8dee19e2015-04-09 12:11:51 -05001406
1407 if (!rx_usecs && !rx_frames) {
1408 /* No coalescing, interrupt for every descriptor */
1409 inte = 1;
1410 } else {
1411 /* Set interrupt based on Rx frame coalescing setting */
1412 if (rx_frames && !((index + 1) % rx_frames))
1413 inte = 1;
1414 else
1415 inte = 0;
1416 }
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001417
1418 /* Reset the Rx descriptor
Lendacky, Thomas174fd252014-11-04 16:06:50 -06001419 * Set buffer 1 (lo) address to header dma address (lo)
1420 * Set buffer 1 (hi) address to header dma address (hi)
1421 * Set buffer 2 (lo) address to buffer dma address (lo)
1422 * Set buffer 2 (hi) address to buffer dma address (hi) and
1423 * set control bits OWN and INTE
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001424 */
Lendacky, Thomascfbfd862015-07-06 11:57:37 -05001425 hdr_dma = rdata->rx.hdr.dma_base + rdata->rx.hdr.dma_off;
1426 buf_dma = rdata->rx.buf.dma_base + rdata->rx.buf.dma_off;
1427 rdesc->desc0 = cpu_to_le32(lower_32_bits(hdr_dma));
1428 rdesc->desc1 = cpu_to_le32(upper_32_bits(hdr_dma));
1429 rdesc->desc2 = cpu_to_le32(lower_32_bits(buf_dma));
1430 rdesc->desc3 = cpu_to_le32(upper_32_bits(buf_dma));
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001431
Lendacky, Thomas8dee19e2015-04-09 12:11:51 -05001432 XGMAC_SET_BITS_LE(rdesc->desc3, RX_NORMAL_DESC3, INTE, inte);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001433
1434 /* Since the Rx DMA engine is likely running, make sure everything
1435 * is written to the descriptor(s) before setting the OWN bit
1436 * for the descriptor
1437 */
Lendacky, Thomasceb8f6b2015-03-20 11:50:16 -05001438 dma_wmb();
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001439
1440 XGMAC_SET_BITS_LE(rdesc->desc3, RX_NORMAL_DESC3, OWN, 1);
1441
1442 /* Make sure ownership is written to the descriptor */
Lendacky, Thomasceb8f6b2015-03-20 11:50:16 -05001443 dma_wmb();
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001444}
1445
1446static void xgbe_rx_desc_init(struct xgbe_channel *channel)
1447{
1448 struct xgbe_prv_data *pdata = channel->pdata;
1449 struct xgbe_ring *ring = channel->rx_ring;
1450 struct xgbe_ring_data *rdata;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001451 unsigned int start_index = ring->cur;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001452 unsigned int i;
1453
1454 DBGPR("-->rx_desc_init\n");
1455
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001456 /* Initialize all descriptors */
1457 for (i = 0; i < ring->rdesc_count; i++) {
Lendacky, Thomasd0a8ba62014-06-24 16:19:06 -05001458 rdata = XGBE_GET_DESC_DATA(ring, i);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001459
Lendacky, Thomas08dcc472014-11-04 16:06:44 -06001460 /* Initialize Rx descriptor */
Lendacky, Thomas8dee19e2015-04-09 12:11:51 -05001461 xgbe_rx_desc_reset(pdata, rdata, i);
Lendacky, Thomas08dcc472014-11-04 16:06:44 -06001462 }
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001463
1464 /* Update the total number of Rx descriptors */
1465 XGMAC_DMA_IOWRITE(channel, DMA_CH_RDRLR, ring->rdesc_count - 1);
1466
1467 /* Update the starting address of descriptor ring */
Lendacky, Thomasd0a8ba62014-06-24 16:19:06 -05001468 rdata = XGBE_GET_DESC_DATA(ring, start_index);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001469 XGMAC_DMA_IOWRITE(channel, DMA_CH_RDLR_HI,
1470 upper_32_bits(rdata->rdesc_dma));
1471 XGMAC_DMA_IOWRITE(channel, DMA_CH_RDLR_LO,
1472 lower_32_bits(rdata->rdesc_dma));
1473
1474 /* Update the Rx Descriptor Tail Pointer */
Lendacky, Thomasd0a8ba62014-06-24 16:19:06 -05001475 rdata = XGBE_GET_DESC_DATA(ring, start_index + ring->rdesc_count - 1);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001476 XGMAC_DMA_IOWRITE(channel, DMA_CH_RDTR_LO,
1477 lower_32_bits(rdata->rdesc_dma));
1478
1479 DBGPR("<--rx_desc_init\n");
1480}
1481
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05001482static void xgbe_update_tstamp_addend(struct xgbe_prv_data *pdata,
1483 unsigned int addend)
1484{
Lendacky, Thomas9018ff52017-06-28 13:42:07 -05001485 unsigned int count = 10000;
1486
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05001487 /* Set the addend register value and tell the device */
1488 XGMAC_IOWRITE(pdata, MAC_TSAR, addend);
1489 XGMAC_IOWRITE_BITS(pdata, MAC_TSCR, TSADDREG, 1);
1490
1491 /* Wait for addend update to complete */
Lendacky, Thomas9018ff52017-06-28 13:42:07 -05001492 while (--count && XGMAC_IOREAD_BITS(pdata, MAC_TSCR, TSADDREG))
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05001493 udelay(5);
Lendacky, Thomas9018ff52017-06-28 13:42:07 -05001494
1495 if (!count)
1496 netdev_err(pdata->netdev,
1497 "timed out updating timestamp addend register\n");
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05001498}
1499
1500static void xgbe_set_tstamp_time(struct xgbe_prv_data *pdata, unsigned int sec,
1501 unsigned int nsec)
1502{
Lendacky, Thomas9018ff52017-06-28 13:42:07 -05001503 unsigned int count = 10000;
1504
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05001505 /* Set the time values and tell the device */
1506 XGMAC_IOWRITE(pdata, MAC_STSUR, sec);
1507 XGMAC_IOWRITE(pdata, MAC_STNUR, nsec);
1508 XGMAC_IOWRITE_BITS(pdata, MAC_TSCR, TSINIT, 1);
1509
1510 /* Wait for time update to complete */
Lendacky, Thomas9018ff52017-06-28 13:42:07 -05001511 while (--count && XGMAC_IOREAD_BITS(pdata, MAC_TSCR, TSINIT))
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05001512 udelay(5);
Lendacky, Thomas9018ff52017-06-28 13:42:07 -05001513
1514 if (!count)
1515 netdev_err(pdata->netdev, "timed out initializing timestamp\n");
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05001516}
1517
1518static u64 xgbe_get_tstamp_time(struct xgbe_prv_data *pdata)
1519{
1520 u64 nsec;
1521
1522 nsec = XGMAC_IOREAD(pdata, MAC_STSR);
1523 nsec *= NSEC_PER_SEC;
1524 nsec += XGMAC_IOREAD(pdata, MAC_STNR);
1525
1526 return nsec;
1527}
1528
1529static u64 xgbe_get_tx_tstamp(struct xgbe_prv_data *pdata)
1530{
Lendacky, Thomasaba97772016-11-10 17:09:45 -06001531 unsigned int tx_snr, tx_ssr;
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05001532 u64 nsec;
1533
Lendacky, Thomasaba97772016-11-10 17:09:45 -06001534 if (pdata->vdata->tx_tstamp_workaround) {
1535 tx_snr = XGMAC_IOREAD(pdata, MAC_TXSNR);
1536 tx_ssr = XGMAC_IOREAD(pdata, MAC_TXSSR);
1537 } else {
1538 tx_ssr = XGMAC_IOREAD(pdata, MAC_TXSSR);
1539 tx_snr = XGMAC_IOREAD(pdata, MAC_TXSNR);
1540 }
1541
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05001542 if (XGMAC_GET_BITS(tx_snr, MAC_TXSNR, TXTSSTSMIS))
1543 return 0;
1544
Lendacky, Thomasaba97772016-11-10 17:09:45 -06001545 nsec = tx_ssr;
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05001546 nsec *= NSEC_PER_SEC;
1547 nsec += tx_snr;
1548
1549 return nsec;
1550}
1551
1552static void xgbe_get_rx_tstamp(struct xgbe_packet_data *packet,
1553 struct xgbe_ring_desc *rdesc)
1554{
1555 u64 nsec;
1556
1557 if (XGMAC_GET_BITS_LE(rdesc->desc3, RX_CONTEXT_DESC3, TSA) &&
1558 !XGMAC_GET_BITS_LE(rdesc->desc3, RX_CONTEXT_DESC3, TSD)) {
1559 nsec = le32_to_cpu(rdesc->desc1);
1560 nsec <<= 32;
1561 nsec |= le32_to_cpu(rdesc->desc0);
1562 if (nsec != 0xffffffffffffffffULL) {
1563 packet->rx_tstamp = nsec;
1564 XGMAC_SET_BITS(packet->attributes, RX_PACKET_ATTRIBUTES,
1565 RX_TSTAMP, 1);
1566 }
1567 }
1568}
1569
1570static int xgbe_config_tstamp(struct xgbe_prv_data *pdata,
1571 unsigned int mac_tscr)
1572{
1573 /* Set one nano-second accuracy */
1574 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSCTRLSSR, 1);
1575
1576 /* Set fine timestamp update */
1577 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSCFUPDT, 1);
1578
1579 /* Overwrite earlier timestamps */
1580 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TXTSSTSM, 1);
1581
1582 XGMAC_IOWRITE(pdata, MAC_TSCR, mac_tscr);
1583
1584 /* Exit if timestamping is not enabled */
1585 if (!XGMAC_GET_BITS(mac_tscr, MAC_TSCR, TSENA))
1586 return 0;
1587
1588 /* Initialize time registers */
1589 XGMAC_IOWRITE_BITS(pdata, MAC_SSIR, SSINC, XGBE_TSTAMP_SSINC);
1590 XGMAC_IOWRITE_BITS(pdata, MAC_SSIR, SNSINC, XGBE_TSTAMP_SNSINC);
1591 xgbe_update_tstamp_addend(pdata, pdata->tstamp_addend);
1592 xgbe_set_tstamp_time(pdata, 0, 0);
1593
1594 /* Initialize the timecounter */
1595 timecounter_init(&pdata->tstamp_tc, &pdata->tstamp_cc,
1596 ktime_to_ns(ktime_get_real()));
1597
1598 return 0;
1599}
1600
Lendacky, Thomas16958a22014-11-20 11:04:08 -06001601static void xgbe_tx_start_xmit(struct xgbe_channel *channel,
1602 struct xgbe_ring *ring)
1603{
1604 struct xgbe_prv_data *pdata = channel->pdata;
1605 struct xgbe_ring_data *rdata;
1606
Lendacky, Thomasceb8f6b2015-03-20 11:50:16 -05001607 /* Make sure everything is written before the register write */
1608 wmb();
1609
Lendacky, Thomas16958a22014-11-20 11:04:08 -06001610 /* Issue a poll command to Tx DMA by writing address
1611 * of next immediate free descriptor */
1612 rdata = XGBE_GET_DESC_DATA(ring, ring->cur);
1613 XGMAC_DMA_IOWRITE(channel, DMA_CH_TDTR_LO,
1614 lower_32_bits(rdata->rdesc_dma));
1615
Lendacky, Thomasc635eaa2015-03-20 11:50:28 -05001616 /* Start the Tx timer */
Lendacky, Thomas16958a22014-11-20 11:04:08 -06001617 if (pdata->tx_usecs && !channel->tx_timer_active) {
1618 channel->tx_timer_active = 1;
Lendacky, Thomasc635eaa2015-03-20 11:50:28 -05001619 mod_timer(&channel->tx_timer,
1620 jiffies + usecs_to_jiffies(pdata->tx_usecs));
Lendacky, Thomas16958a22014-11-20 11:04:08 -06001621 }
1622
1623 ring->tx.xmit_more = 0;
1624}
1625
Lendacky, Thomasa9d41982014-11-04 16:06:32 -06001626static void xgbe_dev_xmit(struct xgbe_channel *channel)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001627{
1628 struct xgbe_prv_data *pdata = channel->pdata;
1629 struct xgbe_ring *ring = channel->tx_ring;
1630 struct xgbe_ring_data *rdata;
1631 struct xgbe_ring_desc *rdesc;
1632 struct xgbe_packet_data *packet = &ring->packet_data;
1633 unsigned int csum, tso, vlan;
1634 unsigned int tso_context, vlan_context;
Lendacky, Thomaseb79e642014-11-20 11:04:02 -06001635 unsigned int tx_set_ic;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001636 int start_index = ring->cur;
Lendacky, Thomasa83ef422015-01-16 12:46:55 -06001637 int cur_index = ring->cur;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001638 int i;
1639
Lendacky, Thomasa9d41982014-11-04 16:06:32 -06001640 DBGPR("-->xgbe_dev_xmit\n");
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001641
1642 csum = XGMAC_GET_BITS(packet->attributes, TX_PACKET_ATTRIBUTES,
1643 CSUM_ENABLE);
1644 tso = XGMAC_GET_BITS(packet->attributes, TX_PACKET_ATTRIBUTES,
1645 TSO_ENABLE);
1646 vlan = XGMAC_GET_BITS(packet->attributes, TX_PACKET_ATTRIBUTES,
1647 VLAN_CTAG);
1648
1649 if (tso && (packet->mss != ring->tx.cur_mss))
1650 tso_context = 1;
1651 else
1652 tso_context = 0;
1653
1654 if (vlan && (packet->vlan_ctag != ring->tx.cur_vlan_ctag))
1655 vlan_context = 1;
1656 else
1657 vlan_context = 0;
1658
Lendacky, Thomaseb79e642014-11-20 11:04:02 -06001659 /* Determine if an interrupt should be generated for this Tx:
1660 * Interrupt:
1661 * - Tx frame count exceeds the frame count setting
1662 * - Addition of Tx frame count to the frame count since the
1663 * last interrupt was set exceeds the frame count setting
1664 * No interrupt:
1665 * - No frame count setting specified (ethtool -C ethX tx-frames 0)
1666 * - Addition of Tx frame count to the frame count since the
1667 * last interrupt was set does not exceed the frame count setting
1668 */
1669 ring->coalesce_count += packet->tx_packets;
1670 if (!pdata->tx_frames)
1671 tx_set_ic = 0;
1672 else if (packet->tx_packets > pdata->tx_frames)
1673 tx_set_ic = 1;
1674 else if ((ring->coalesce_count % pdata->tx_frames) <
1675 packet->tx_packets)
1676 tx_set_ic = 1;
1677 else
1678 tx_set_ic = 0;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001679
Lendacky, Thomasa83ef422015-01-16 12:46:55 -06001680 rdata = XGBE_GET_DESC_DATA(ring, cur_index);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001681 rdesc = rdata->rdesc;
1682
1683 /* Create a context descriptor if this is a TSO packet */
1684 if (tso_context || vlan_context) {
1685 if (tso_context) {
Lendacky, Thomas34bf65d2015-05-14 11:44:03 -05001686 netif_dbg(pdata, tx_queued, pdata->netdev,
1687 "TSO context descriptor, mss=%u\n",
1688 packet->mss);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001689
1690 /* Set the MSS size */
1691 XGMAC_SET_BITS_LE(rdesc->desc2, TX_CONTEXT_DESC2,
1692 MSS, packet->mss);
1693
1694 /* Mark it as a CONTEXT descriptor */
1695 XGMAC_SET_BITS_LE(rdesc->desc3, TX_CONTEXT_DESC3,
1696 CTXT, 1);
1697
1698 /* Indicate this descriptor contains the MSS */
1699 XGMAC_SET_BITS_LE(rdesc->desc3, TX_CONTEXT_DESC3,
1700 TCMSSV, 1);
1701
1702 ring->tx.cur_mss = packet->mss;
1703 }
1704
1705 if (vlan_context) {
Lendacky, Thomas34bf65d2015-05-14 11:44:03 -05001706 netif_dbg(pdata, tx_queued, pdata->netdev,
1707 "VLAN context descriptor, ctag=%u\n",
1708 packet->vlan_ctag);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001709
1710 /* Mark it as a CONTEXT descriptor */
1711 XGMAC_SET_BITS_LE(rdesc->desc3, TX_CONTEXT_DESC3,
1712 CTXT, 1);
1713
1714 /* Set the VLAN tag */
1715 XGMAC_SET_BITS_LE(rdesc->desc3, TX_CONTEXT_DESC3,
1716 VT, packet->vlan_ctag);
1717
1718 /* Indicate this descriptor contains the VLAN tag */
1719 XGMAC_SET_BITS_LE(rdesc->desc3, TX_CONTEXT_DESC3,
1720 VLTV, 1);
1721
1722 ring->tx.cur_vlan_ctag = packet->vlan_ctag;
1723 }
1724
Lendacky, Thomasa83ef422015-01-16 12:46:55 -06001725 cur_index++;
1726 rdata = XGBE_GET_DESC_DATA(ring, cur_index);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001727 rdesc = rdata->rdesc;
1728 }
1729
1730 /* Update buffer address (for TSO this is the header) */
1731 rdesc->desc0 = cpu_to_le32(lower_32_bits(rdata->skb_dma));
1732 rdesc->desc1 = cpu_to_le32(upper_32_bits(rdata->skb_dma));
1733
1734 /* Update the buffer length */
1735 XGMAC_SET_BITS_LE(rdesc->desc2, TX_NORMAL_DESC2, HL_B1L,
1736 rdata->skb_dma_len);
1737
1738 /* VLAN tag insertion check */
1739 if (vlan)
1740 XGMAC_SET_BITS_LE(rdesc->desc2, TX_NORMAL_DESC2, VTIR,
1741 TX_NORMAL_DESC2_VLAN_INSERT);
1742
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05001743 /* Timestamp enablement check */
1744 if (XGMAC_GET_BITS(packet->attributes, TX_PACKET_ATTRIBUTES, PTP))
1745 XGMAC_SET_BITS_LE(rdesc->desc2, TX_NORMAL_DESC2, TTSE, 1);
1746
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001747 /* Mark it as First Descriptor */
1748 XGMAC_SET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, FD, 1);
1749
1750 /* Mark it as a NORMAL descriptor */
1751 XGMAC_SET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, CTXT, 0);
1752
1753 /* Set OWN bit if not the first descriptor */
Lendacky, Thomasa83ef422015-01-16 12:46:55 -06001754 if (cur_index != start_index)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001755 XGMAC_SET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, OWN, 1);
1756
1757 if (tso) {
1758 /* Enable TSO */
1759 XGMAC_SET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, TSE, 1);
1760 XGMAC_SET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, TCPPL,
1761 packet->tcp_payload_len);
1762 XGMAC_SET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, TCPHDRLEN,
1763 packet->tcp_header_len / 4);
Lendacky, Thomas5452b2d2015-05-14 11:43:57 -05001764
1765 pdata->ext_stats.tx_tso_packets++;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001766 } else {
1767 /* Enable CRC and Pad Insertion */
1768 XGMAC_SET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, CPC, 0);
1769
1770 /* Enable HW CSUM */
1771 if (csum)
1772 XGMAC_SET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3,
1773 CIC, 0x3);
1774
1775 /* Set the total length to be transmitted */
1776 XGMAC_SET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, FL,
1777 packet->length);
1778 }
1779
Lendacky, Thomasa83ef422015-01-16 12:46:55 -06001780 for (i = cur_index - start_index + 1; i < packet->rdesc_count; i++) {
1781 cur_index++;
1782 rdata = XGBE_GET_DESC_DATA(ring, cur_index);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001783 rdesc = rdata->rdesc;
1784
1785 /* Update buffer address */
1786 rdesc->desc0 = cpu_to_le32(lower_32_bits(rdata->skb_dma));
1787 rdesc->desc1 = cpu_to_le32(upper_32_bits(rdata->skb_dma));
1788
1789 /* Update the buffer length */
1790 XGMAC_SET_BITS_LE(rdesc->desc2, TX_NORMAL_DESC2, HL_B1L,
1791 rdata->skb_dma_len);
1792
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001793 /* Set OWN bit */
1794 XGMAC_SET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, OWN, 1);
1795
1796 /* Mark it as NORMAL descriptor */
1797 XGMAC_SET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, CTXT, 0);
1798
1799 /* Enable HW CSUM */
1800 if (csum)
1801 XGMAC_SET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3,
1802 CIC, 0x3);
1803 }
1804
1805 /* Set LAST bit for the last descriptor */
1806 XGMAC_SET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, LD, 1);
1807
Lendacky, Thomaseb79e642014-11-20 11:04:02 -06001808 /* Set IC bit based on Tx coalescing settings */
1809 if (tx_set_ic)
1810 XGMAC_SET_BITS_LE(rdesc->desc2, TX_NORMAL_DESC2, IC, 1);
1811
Lendacky, Thomas5fb4b862014-11-20 11:03:50 -06001812 /* Save the Tx info to report back during cleanup */
1813 rdata->tx.packets = packet->tx_packets;
1814 rdata->tx.bytes = packet->tx_bytes;
1815
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001816 /* In case the Tx DMA engine is running, make sure everything
1817 * is written to the descriptor(s) before setting the OWN bit
1818 * for the first descriptor
1819 */
Lendacky, Thomasceb8f6b2015-03-20 11:50:16 -05001820 dma_wmb();
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001821
1822 /* Set OWN bit for the first descriptor */
Lendacky, Thomasd0a8ba62014-06-24 16:19:06 -05001823 rdata = XGBE_GET_DESC_DATA(ring, start_index);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001824 rdesc = rdata->rdesc;
1825 XGMAC_SET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, OWN, 1);
1826
Lendacky, Thomas34bf65d2015-05-14 11:44:03 -05001827 if (netif_msg_tx_queued(pdata))
1828 xgbe_dump_tx_desc(pdata, ring, start_index,
1829 packet->rdesc_count, 1);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001830
1831 /* Make sure ownership is written to the descriptor */
Lendacky, Thomas20986ed2015-10-26 17:13:54 -05001832 smp_wmb();
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001833
Lendacky, Thomasa83ef422015-01-16 12:46:55 -06001834 ring->cur = cur_index + 1;
Lendacky, Thomas16958a22014-11-20 11:04:08 -06001835 if (!packet->skb->xmit_more ||
1836 netif_xmit_stopped(netdev_get_tx_queue(pdata->netdev,
1837 channel->queue_index)))
1838 xgbe_tx_start_xmit(channel, ring);
1839 else
1840 ring->tx.xmit_more = 1;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001841
1842 DBGPR(" %s: descriptors %u to %u written\n",
1843 channel->name, start_index & (ring->rdesc_count - 1),
1844 (ring->cur - 1) & (ring->rdesc_count - 1));
1845
Lendacky, Thomasa9d41982014-11-04 16:06:32 -06001846 DBGPR("<--xgbe_dev_xmit\n");
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001847}
1848
1849static int xgbe_dev_read(struct xgbe_channel *channel)
1850{
Lendacky, Thomas5452b2d2015-05-14 11:43:57 -05001851 struct xgbe_prv_data *pdata = channel->pdata;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001852 struct xgbe_ring *ring = channel->rx_ring;
1853 struct xgbe_ring_data *rdata;
1854 struct xgbe_ring_desc *rdesc;
1855 struct xgbe_packet_data *packet = &ring->packet_data;
Lendacky, Thomas5452b2d2015-05-14 11:43:57 -05001856 struct net_device *netdev = pdata->netdev;
Lendacky, Thomas5b9dfe22014-11-04 16:07:02 -06001857 unsigned int err, etlt, l34t;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001858
1859 DBGPR("-->xgbe_dev_read: cur = %d\n", ring->cur);
1860
Lendacky, Thomasd0a8ba62014-06-24 16:19:06 -05001861 rdata = XGBE_GET_DESC_DATA(ring, ring->cur);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001862 rdesc = rdata->rdesc;
1863
1864 /* Check for data availability */
1865 if (XGMAC_GET_BITS_LE(rdesc->desc3, RX_NORMAL_DESC3, OWN))
1866 return 1;
1867
Lendacky, Thomas5449e272014-11-20 11:03:26 -06001868 /* Make sure descriptor fields are read after reading the OWN bit */
Lendacky, Thomasceb8f6b2015-03-20 11:50:16 -05001869 dma_rmb();
Lendacky, Thomas5449e272014-11-20 11:03:26 -06001870
Lendacky, Thomas34bf65d2015-05-14 11:44:03 -05001871 if (netif_msg_rx_status(pdata))
1872 xgbe_dump_rx_desc(pdata, ring, ring->cur);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001873
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05001874 if (XGMAC_GET_BITS_LE(rdesc->desc3, RX_NORMAL_DESC3, CTXT)) {
1875 /* Timestamp Context Descriptor */
1876 xgbe_get_rx_tstamp(packet, rdesc);
1877
1878 XGMAC_SET_BITS(packet->attributes, RX_PACKET_ATTRIBUTES,
1879 CONTEXT, 1);
1880 XGMAC_SET_BITS(packet->attributes, RX_PACKET_ATTRIBUTES,
1881 CONTEXT_NEXT, 0);
1882 return 0;
1883 }
1884
1885 /* Normal Descriptor, be sure Context Descriptor bit is off */
1886 XGMAC_SET_BITS(packet->attributes, RX_PACKET_ATTRIBUTES, CONTEXT, 0);
1887
1888 /* Indicate if a Context Descriptor is next */
1889 if (XGMAC_GET_BITS_LE(rdesc->desc3, RX_NORMAL_DESC3, CDA))
1890 XGMAC_SET_BITS(packet->attributes, RX_PACKET_ATTRIBUTES,
1891 CONTEXT_NEXT, 1);
1892
Lendacky, Thomas174fd252014-11-04 16:06:50 -06001893 /* Get the header length */
Lendacky, Thomas5452b2d2015-05-14 11:43:57 -05001894 if (XGMAC_GET_BITS_LE(rdesc->desc3, RX_NORMAL_DESC3, FD)) {
Lendacky, Thomas622c36f2017-03-15 15:11:23 -05001895 XGMAC_SET_BITS(packet->attributes, RX_PACKET_ATTRIBUTES,
1896 FIRST, 1);
Lendacky, Thomasc9f140e2014-11-20 11:03:44 -06001897 rdata->rx.hdr_len = XGMAC_GET_BITS_LE(rdesc->desc2,
1898 RX_NORMAL_DESC2, HL);
Lendacky, Thomas5452b2d2015-05-14 11:43:57 -05001899 if (rdata->rx.hdr_len)
1900 pdata->ext_stats.rx_split_header_packets++;
Lendacky, Thomas622c36f2017-03-15 15:11:23 -05001901 } else {
1902 XGMAC_SET_BITS(packet->attributes, RX_PACKET_ATTRIBUTES,
1903 FIRST, 0);
Lendacky, Thomas5452b2d2015-05-14 11:43:57 -05001904 }
Lendacky, Thomas174fd252014-11-04 16:06:50 -06001905
Lendacky, Thomas5b9dfe22014-11-04 16:07:02 -06001906 /* Get the RSS hash */
1907 if (XGMAC_GET_BITS_LE(rdesc->desc3, RX_NORMAL_DESC3, RSV)) {
1908 XGMAC_SET_BITS(packet->attributes, RX_PACKET_ATTRIBUTES,
1909 RSS_HASH, 1);
1910
1911 packet->rss_hash = le32_to_cpu(rdesc->desc1);
1912
1913 l34t = XGMAC_GET_BITS_LE(rdesc->desc3, RX_NORMAL_DESC3, L34T);
1914 switch (l34t) {
1915 case RX_DESC3_L34T_IPV4_TCP:
1916 case RX_DESC3_L34T_IPV4_UDP:
1917 case RX_DESC3_L34T_IPV6_TCP:
1918 case RX_DESC3_L34T_IPV6_UDP:
1919 packet->rss_hash_type = PKT_HASH_TYPE_L4;
Dan Carpenterb6267d32014-11-13 09:19:06 +03001920 break;
Lendacky, Thomas5b9dfe22014-11-04 16:07:02 -06001921 default:
1922 packet->rss_hash_type = PKT_HASH_TYPE_L3;
1923 }
1924 }
1925
Lendacky, Thomas622c36f2017-03-15 15:11:23 -05001926 /* Not all the data has been transferred for this packet */
1927 if (!XGMAC_GET_BITS_LE(rdesc->desc3, RX_NORMAL_DESC3, LD))
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001928 return 0;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001929
1930 /* This is the last of the data for this packet */
1931 XGMAC_SET_BITS(packet->attributes, RX_PACKET_ATTRIBUTES,
Lendacky, Thomas622c36f2017-03-15 15:11:23 -05001932 LAST, 1);
1933
1934 /* Get the packet length */
1935 rdata->rx.len = XGMAC_GET_BITS_LE(rdesc->desc3, RX_NORMAL_DESC3, PL);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001936
1937 /* Set checksum done indicator as appropriate */
Lendacky, Thomas5452b2d2015-05-14 11:43:57 -05001938 if (netdev->features & NETIF_F_RXCSUM)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001939 XGMAC_SET_BITS(packet->attributes, RX_PACKET_ATTRIBUTES,
1940 CSUM_DONE, 1);
1941
1942 /* Check for errors (only valid in last descriptor) */
1943 err = XGMAC_GET_BITS_LE(rdesc->desc3, RX_NORMAL_DESC3, ES);
1944 etlt = XGMAC_GET_BITS_LE(rdesc->desc3, RX_NORMAL_DESC3, ETLT);
Lendacky, Thomas34bf65d2015-05-14 11:44:03 -05001945 netif_dbg(pdata, rx_status, netdev, "err=%u, etlt=%#x\n", err, etlt);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001946
Lendacky, Thomas7bba35b2014-11-20 11:03:38 -06001947 if (!err || !etlt) {
1948 /* No error if err is 0 or etlt is 0 */
Lendacky, Thomasc52e9c62014-06-24 16:19:18 -05001949 if ((etlt == 0x09) &&
1950 (netdev->features & NETIF_F_HW_VLAN_CTAG_RX)) {
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001951 XGMAC_SET_BITS(packet->attributes, RX_PACKET_ATTRIBUTES,
1952 VLAN_CTAG, 1);
1953 packet->vlan_ctag = XGMAC_GET_BITS_LE(rdesc->desc0,
1954 RX_NORMAL_DESC0,
1955 OVT);
Lendacky, Thomas34bf65d2015-05-14 11:44:03 -05001956 netif_dbg(pdata, rx_status, netdev, "vlan-ctag=%#06x\n",
1957 packet->vlan_ctag);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001958 }
1959 } else {
1960 if ((etlt == 0x05) || (etlt == 0x06))
1961 XGMAC_SET_BITS(packet->attributes, RX_PACKET_ATTRIBUTES,
1962 CSUM_DONE, 0);
1963 else
1964 XGMAC_SET_BITS(packet->errors, RX_PACKET_ERRORS,
1965 FRAME, 1);
1966 }
1967
1968 DBGPR("<--xgbe_dev_read: %s - descriptor=%u (cur=%d)\n", channel->name,
1969 ring->cur & (ring->rdesc_count - 1), ring->cur);
1970
1971 return 0;
1972}
1973
1974static int xgbe_is_context_desc(struct xgbe_ring_desc *rdesc)
1975{
1976 /* Rx and Tx share CTXT bit, so check TDES3.CTXT bit */
1977 return XGMAC_GET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, CTXT);
1978}
1979
1980static int xgbe_is_last_desc(struct xgbe_ring_desc *rdesc)
1981{
1982 /* Rx and Tx share LD bit, so check TDES3.LD bit */
1983 return XGMAC_GET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, LD);
1984}
1985
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001986static int xgbe_enable_int(struct xgbe_channel *channel,
1987 enum xgbe_int int_id)
1988{
Lendacky, Thomas9867e8f2014-07-02 13:04:46 -05001989 unsigned int dma_ch_ier;
1990
1991 dma_ch_ier = XGMAC_DMA_IOREAD(channel, DMA_CH_IER);
1992
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001993 switch (int_id) {
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001994 case XGMAC_INT_DMA_CH_SR_TI:
Lendacky, Thomas9867e8f2014-07-02 13:04:46 -05001995 XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, TIE, 1);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001996 break;
1997 case XGMAC_INT_DMA_CH_SR_TPS:
Lendacky, Thomas9867e8f2014-07-02 13:04:46 -05001998 XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, TXSE, 1);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001999 break;
2000 case XGMAC_INT_DMA_CH_SR_TBU:
Lendacky, Thomas9867e8f2014-07-02 13:04:46 -05002001 XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, TBUE, 1);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002002 break;
2003 case XGMAC_INT_DMA_CH_SR_RI:
Lendacky, Thomas9867e8f2014-07-02 13:04:46 -05002004 XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, RIE, 1);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002005 break;
2006 case XGMAC_INT_DMA_CH_SR_RBU:
Lendacky, Thomas9867e8f2014-07-02 13:04:46 -05002007 XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, RBUE, 1);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002008 break;
2009 case XGMAC_INT_DMA_CH_SR_RPS:
Lendacky, Thomas9867e8f2014-07-02 13:04:46 -05002010 XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, RSE, 1);
2011 break;
2012 case XGMAC_INT_DMA_CH_SR_TI_RI:
2013 XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, TIE, 1);
2014 XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, RIE, 1);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002015 break;
2016 case XGMAC_INT_DMA_CH_SR_FBE:
Lendacky, Thomas9867e8f2014-07-02 13:04:46 -05002017 XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, FBEE, 1);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002018 break;
2019 case XGMAC_INT_DMA_ALL:
Lendacky, Thomas9867e8f2014-07-02 13:04:46 -05002020 dma_ch_ier |= channel->saved_ier;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002021 break;
2022 default:
2023 return -1;
2024 }
2025
Lendacky, Thomas9867e8f2014-07-02 13:04:46 -05002026 XGMAC_DMA_IOWRITE(channel, DMA_CH_IER, dma_ch_ier);
2027
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002028 return 0;
2029}
2030
2031static int xgbe_disable_int(struct xgbe_channel *channel,
2032 enum xgbe_int int_id)
2033{
2034 unsigned int dma_ch_ier;
2035
Lendacky, Thomas9867e8f2014-07-02 13:04:46 -05002036 dma_ch_ier = XGMAC_DMA_IOREAD(channel, DMA_CH_IER);
2037
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002038 switch (int_id) {
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002039 case XGMAC_INT_DMA_CH_SR_TI:
Lendacky, Thomas9867e8f2014-07-02 13:04:46 -05002040 XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, TIE, 0);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002041 break;
2042 case XGMAC_INT_DMA_CH_SR_TPS:
Lendacky, Thomas9867e8f2014-07-02 13:04:46 -05002043 XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, TXSE, 0);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002044 break;
2045 case XGMAC_INT_DMA_CH_SR_TBU:
Lendacky, Thomas9867e8f2014-07-02 13:04:46 -05002046 XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, TBUE, 0);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002047 break;
2048 case XGMAC_INT_DMA_CH_SR_RI:
Lendacky, Thomas9867e8f2014-07-02 13:04:46 -05002049 XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, RIE, 0);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002050 break;
2051 case XGMAC_INT_DMA_CH_SR_RBU:
Lendacky, Thomas9867e8f2014-07-02 13:04:46 -05002052 XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, RBUE, 0);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002053 break;
2054 case XGMAC_INT_DMA_CH_SR_RPS:
Lendacky, Thomas9867e8f2014-07-02 13:04:46 -05002055 XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, RSE, 0);
2056 break;
2057 case XGMAC_INT_DMA_CH_SR_TI_RI:
2058 XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, TIE, 0);
2059 XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, RIE, 0);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002060 break;
2061 case XGMAC_INT_DMA_CH_SR_FBE:
Lendacky, Thomas9867e8f2014-07-02 13:04:46 -05002062 XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, FBEE, 0);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002063 break;
2064 case XGMAC_INT_DMA_ALL:
Lendacky, Thomas9867e8f2014-07-02 13:04:46 -05002065 channel->saved_ier = dma_ch_ier & XGBE_DMA_INTERRUPT_MASK;
Lendacky, Thomasd0a8ba62014-06-24 16:19:06 -05002066 dma_ch_ier &= ~XGBE_DMA_INTERRUPT_MASK;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002067 break;
2068 default:
2069 return -1;
2070 }
2071
Lendacky, Thomas9867e8f2014-07-02 13:04:46 -05002072 XGMAC_DMA_IOWRITE(channel, DMA_CH_IER, dma_ch_ier);
2073
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002074 return 0;
2075}
2076
Lendacky, Thomas5ffc0332016-11-10 17:09:29 -06002077static int __xgbe_exit(struct xgbe_prv_data *pdata)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002078{
2079 unsigned int count = 2000;
2080
2081 DBGPR("-->xgbe_exit\n");
2082
2083 /* Issue a software reset */
2084 XGMAC_IOWRITE_BITS(pdata, DMA_MR, SWR, 1);
2085 usleep_range(10, 15);
2086
2087 /* Poll Until Poll Condition */
Dan Carpenterc7557e62015-12-15 13:12:29 +03002088 while (--count && XGMAC_IOREAD_BITS(pdata, DMA_MR, SWR))
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002089 usleep_range(500, 600);
2090
2091 if (!count)
2092 return -EBUSY;
2093
2094 DBGPR("<--xgbe_exit\n");
2095
2096 return 0;
2097}
2098
Lendacky, Thomas5ffc0332016-11-10 17:09:29 -06002099static int xgbe_exit(struct xgbe_prv_data *pdata)
2100{
2101 int ret;
2102
2103 /* To guard against possible incorrectly generated interrupts,
2104 * issue the software reset twice.
2105 */
2106 ret = __xgbe_exit(pdata);
2107 if (ret)
2108 return ret;
2109
2110 return __xgbe_exit(pdata);
2111}
2112
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002113static int xgbe_flush_tx_queues(struct xgbe_prv_data *pdata)
2114{
2115 unsigned int i, count;
2116
Lendacky, Thomasa9a4a2d2014-08-29 13:16:50 -05002117 if (XGMAC_GET_BITS(pdata->hw_feat.version, MAC_VR, SNPSVER) < 0x21)
2118 return 0;
2119
Lendacky, Thomas853eb162014-07-29 08:57:31 -05002120 for (i = 0; i < pdata->tx_q_count; i++)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002121 XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_Q_TQOMR, FTQ, 1);
2122
2123 /* Poll Until Poll Condition */
Lendacky, Thomas853eb162014-07-29 08:57:31 -05002124 for (i = 0; i < pdata->tx_q_count; i++) {
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002125 count = 2000;
Dan Carpenterc7557e62015-12-15 13:12:29 +03002126 while (--count && XGMAC_MTL_IOREAD_BITS(pdata, i,
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002127 MTL_Q_TQOMR, FTQ))
2128 usleep_range(500, 600);
2129
2130 if (!count)
2131 return -EBUSY;
2132 }
2133
2134 return 0;
2135}
2136
2137static void xgbe_config_dma_bus(struct xgbe_prv_data *pdata)
2138{
2139 /* Set enhanced addressing mode */
2140 XGMAC_IOWRITE_BITS(pdata, DMA_SBMR, EAME, 1);
2141
2142 /* Set the System Bus mode */
2143 XGMAC_IOWRITE_BITS(pdata, DMA_SBMR, UNDEF, 1);
Lendacky, Thomas9867e8f2014-07-02 13:04:46 -05002144 XGMAC_IOWRITE_BITS(pdata, DMA_SBMR, BLEN_256, 1);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002145}
2146
2147static void xgbe_config_dma_cache(struct xgbe_prv_data *pdata)
2148{
2149 unsigned int arcache, awcache;
2150
2151 arcache = 0;
Lendacky, Thomascfa50c72014-07-02 13:04:57 -05002152 XGMAC_SET_BITS(arcache, DMA_AXIARCR, DRC, pdata->arcache);
2153 XGMAC_SET_BITS(arcache, DMA_AXIARCR, DRD, pdata->axdomain);
2154 XGMAC_SET_BITS(arcache, DMA_AXIARCR, TEC, pdata->arcache);
2155 XGMAC_SET_BITS(arcache, DMA_AXIARCR, TED, pdata->axdomain);
2156 XGMAC_SET_BITS(arcache, DMA_AXIARCR, THC, pdata->arcache);
2157 XGMAC_SET_BITS(arcache, DMA_AXIARCR, THD, pdata->axdomain);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002158 XGMAC_IOWRITE(pdata, DMA_AXIARCR, arcache);
2159
2160 awcache = 0;
Lendacky, Thomascfa50c72014-07-02 13:04:57 -05002161 XGMAC_SET_BITS(awcache, DMA_AXIAWCR, DWC, pdata->awcache);
2162 XGMAC_SET_BITS(awcache, DMA_AXIAWCR, DWD, pdata->axdomain);
2163 XGMAC_SET_BITS(awcache, DMA_AXIAWCR, RPC, pdata->awcache);
2164 XGMAC_SET_BITS(awcache, DMA_AXIAWCR, RPD, pdata->axdomain);
2165 XGMAC_SET_BITS(awcache, DMA_AXIAWCR, RHC, pdata->awcache);
2166 XGMAC_SET_BITS(awcache, DMA_AXIAWCR, RHD, pdata->axdomain);
2167 XGMAC_SET_BITS(awcache, DMA_AXIAWCR, TDC, pdata->awcache);
2168 XGMAC_SET_BITS(awcache, DMA_AXIAWCR, TDD, pdata->axdomain);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002169 XGMAC_IOWRITE(pdata, DMA_AXIAWCR, awcache);
2170}
2171
2172static void xgbe_config_mtl_mode(struct xgbe_prv_data *pdata)
2173{
2174 unsigned int i;
2175
Lendacky, Thomasfca2d992014-07-29 08:57:55 -05002176 /* Set Tx to weighted round robin scheduling algorithm */
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002177 XGMAC_IOWRITE_BITS(pdata, MTL_OMR, ETSALG, MTL_ETSALG_WRR);
2178
Lendacky, Thomasfca2d992014-07-29 08:57:55 -05002179 /* Set Tx traffic classes to use WRR algorithm with equal weights */
2180 for (i = 0; i < pdata->hw_feat.tc_cnt; i++) {
2181 XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_TC_ETSCR, TSA,
2182 MTL_TSA_ETS);
2183 XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_TC_QWR, QW, 1);
2184 }
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002185
2186 /* Set Rx to strict priority algorithm */
2187 XGMAC_IOWRITE_BITS(pdata, MTL_OMR, RAA, MTL_RAA_SP);
2188}
2189
Lendacky, Thomas43e0dcf2016-11-03 13:18:16 -05002190static void xgbe_queue_flow_control_threshold(struct xgbe_prv_data *pdata,
2191 unsigned int queue,
2192 unsigned int q_fifo_size)
2193{
2194 unsigned int frame_fifo_size;
2195 unsigned int rfa, rfd;
2196
2197 frame_fifo_size = XGMAC_FLOW_CONTROL_ALIGN(xgbe_get_max_frame(pdata));
2198
2199 if (pdata->pfcq[queue] && (q_fifo_size > pdata->pfc_rfa)) {
2200 /* PFC is active for this queue */
2201 rfa = pdata->pfc_rfa;
2202 rfd = rfa + frame_fifo_size;
2203 if (rfd > XGMAC_FLOW_CONTROL_MAX)
2204 rfd = XGMAC_FLOW_CONTROL_MAX;
2205 if (rfa >= XGMAC_FLOW_CONTROL_MAX)
2206 rfa = XGMAC_FLOW_CONTROL_MAX - XGMAC_FLOW_CONTROL_UNIT;
2207 } else {
2208 /* This path deals with just maximum frame sizes which are
2209 * limited to a jumbo frame of 9,000 (plus headers, etc.)
2210 * so we can never exceed the maximum allowable RFA/RFD
2211 * values.
2212 */
2213 if (q_fifo_size <= 2048) {
2214 /* rx_rfd to zero to signal no flow control */
2215 pdata->rx_rfa[queue] = 0;
2216 pdata->rx_rfd[queue] = 0;
2217 return;
2218 }
2219
2220 if (q_fifo_size <= 4096) {
2221 /* Between 2048 and 4096 */
2222 pdata->rx_rfa[queue] = 0; /* Full - 1024 bytes */
2223 pdata->rx_rfd[queue] = 1; /* Full - 1536 bytes */
2224 return;
2225 }
2226
2227 if (q_fifo_size <= frame_fifo_size) {
2228 /* Between 4096 and max-frame */
2229 pdata->rx_rfa[queue] = 2; /* Full - 2048 bytes */
2230 pdata->rx_rfd[queue] = 5; /* Full - 3584 bytes */
2231 return;
2232 }
2233
2234 if (q_fifo_size <= (frame_fifo_size * 3)) {
2235 /* Between max-frame and 3 max-frames,
2236 * trigger if we get just over a frame of data and
2237 * resume when we have just under half a frame left.
2238 */
2239 rfa = q_fifo_size - frame_fifo_size;
2240 rfd = rfa + (frame_fifo_size / 2);
2241 } else {
2242 /* Above 3 max-frames - trigger when just over
2243 * 2 frames of space available
2244 */
2245 rfa = frame_fifo_size * 2;
2246 rfa += XGMAC_FLOW_CONTROL_UNIT;
2247 rfd = rfa + frame_fifo_size;
2248 }
2249 }
2250
2251 pdata->rx_rfa[queue] = XGMAC_FLOW_CONTROL_VALUE(rfa);
2252 pdata->rx_rfd[queue] = XGMAC_FLOW_CONTROL_VALUE(rfd);
2253}
2254
2255static void xgbe_calculate_flow_control_threshold(struct xgbe_prv_data *pdata,
2256 unsigned int *fifo)
2257{
2258 unsigned int q_fifo_size;
2259 unsigned int i;
2260
2261 for (i = 0; i < pdata->rx_q_count; i++) {
2262 q_fifo_size = (fifo[i] + 1) * XGMAC_FIFO_UNIT;
2263
2264 xgbe_queue_flow_control_threshold(pdata, i, q_fifo_size);
2265 }
2266}
2267
2268static void xgbe_config_flow_control_threshold(struct xgbe_prv_data *pdata)
2269{
2270 unsigned int i;
2271
2272 for (i = 0; i < pdata->rx_q_count; i++) {
2273 XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_Q_RQFCR, RFA,
2274 pdata->rx_rfa[i]);
2275 XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_Q_RQFCR, RFD,
2276 pdata->rx_rfd[i]);
2277 }
2278}
2279
Lendacky, Thomas586e3cfb2016-11-03 13:17:48 -05002280static unsigned int xgbe_get_tx_fifo_size(struct xgbe_prv_data *pdata)
2281{
Lendacky, Thomas586e3cfb2016-11-03 13:17:48 -05002282 /* The configured value may not be the actual amount of fifo RAM */
Lendacky, Thomasbd8255d2016-11-03 13:19:27 -05002283 return min_t(unsigned int, pdata->tx_max_fifo_size,
2284 pdata->hw_feat.tx_fifo_size);
Lendacky, Thomas586e3cfb2016-11-03 13:17:48 -05002285}
2286
2287static unsigned int xgbe_get_rx_fifo_size(struct xgbe_prv_data *pdata)
2288{
Lendacky, Thomas586e3cfb2016-11-03 13:17:48 -05002289 /* The configured value may not be the actual amount of fifo RAM */
Lendacky, Thomasbd8255d2016-11-03 13:19:27 -05002290 return min_t(unsigned int, pdata->rx_max_fifo_size,
2291 pdata->hw_feat.rx_fifo_size);
Lendacky, Thomas586e3cfb2016-11-03 13:17:48 -05002292}
2293
2294static void xgbe_calculate_equal_fifo(unsigned int fifo_size,
2295 unsigned int queue_count,
2296 unsigned int *fifo)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002297{
Lendacky, Thomas9c439e42015-09-30 08:53:03 -05002298 unsigned int q_fifo_size;
2299 unsigned int p_fifo;
Lendacky, Thomas586e3cfb2016-11-03 13:17:48 -05002300 unsigned int i;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002301
Lendacky, Thomas586e3cfb2016-11-03 13:17:48 -05002302 q_fifo_size = fifo_size / queue_count;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002303
Lendacky, Thomas43e0dcf2016-11-03 13:18:16 -05002304 /* Calculate the fifo setting by dividing the queue's fifo size
2305 * by the fifo allocation increment (with 0 representing the
2306 * base allocation increment so decrement the result by 1).
Lendacky, Thomas9c439e42015-09-30 08:53:03 -05002307 */
Lendacky, Thomas43e0dcf2016-11-03 13:18:16 -05002308 p_fifo = q_fifo_size / XGMAC_FIFO_UNIT;
Lendacky, Thomas9c439e42015-09-30 08:53:03 -05002309 if (p_fifo)
2310 p_fifo--;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002311
Lendacky, Thomas43e0dcf2016-11-03 13:18:16 -05002312 /* Distribute the fifo equally amongst the queues */
Lendacky, Thomas586e3cfb2016-11-03 13:17:48 -05002313 for (i = 0; i < queue_count; i++)
2314 fifo[i] = p_fifo;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002315}
2316
Lendacky, Thomas43e0dcf2016-11-03 13:18:16 -05002317static unsigned int xgbe_set_nonprio_fifos(unsigned int fifo_size,
2318 unsigned int queue_count,
2319 unsigned int *fifo)
2320{
2321 unsigned int i;
2322
2323 BUILD_BUG_ON_NOT_POWER_OF_2(XGMAC_FIFO_MIN_ALLOC);
2324
2325 if (queue_count <= IEEE_8021QAZ_MAX_TCS)
2326 return fifo_size;
2327
2328 /* Rx queues 9 and up are for specialized packets,
2329 * such as PTP or DCB control packets, etc. and
2330 * don't require a large fifo
2331 */
2332 for (i = IEEE_8021QAZ_MAX_TCS; i < queue_count; i++) {
2333 fifo[i] = (XGMAC_FIFO_MIN_ALLOC / XGMAC_FIFO_UNIT) - 1;
2334 fifo_size -= XGMAC_FIFO_MIN_ALLOC;
2335 }
2336
2337 return fifo_size;
2338}
2339
2340static unsigned int xgbe_get_pfc_delay(struct xgbe_prv_data *pdata)
2341{
2342 unsigned int delay;
2343
2344 /* If a delay has been provided, use that */
2345 if (pdata->pfc->delay)
2346 return pdata->pfc->delay / 8;
2347
2348 /* Allow for two maximum size frames */
2349 delay = xgbe_get_max_frame(pdata);
2350 delay += XGMAC_ETH_PREAMBLE;
2351 delay *= 2;
2352
2353 /* Allow for PFC frame */
2354 delay += XGMAC_PFC_DATA_LEN;
2355 delay += ETH_HLEN + ETH_FCS_LEN;
2356 delay += XGMAC_ETH_PREAMBLE;
2357
2358 /* Allow for miscellaneous delays (LPI exit, cable, etc.) */
2359 delay += XGMAC_PFC_DELAYS;
2360
2361 return delay;
2362}
2363
2364static unsigned int xgbe_get_pfc_queues(struct xgbe_prv_data *pdata)
2365{
2366 unsigned int count, prio_queues;
2367 unsigned int i;
2368
2369 if (!pdata->pfc->pfc_en)
2370 return 0;
2371
2372 count = 0;
2373 prio_queues = XGMAC_PRIO_QUEUES(pdata->rx_q_count);
2374 for (i = 0; i < prio_queues; i++) {
2375 if (!xgbe_is_pfc_queue(pdata, i))
2376 continue;
2377
2378 pdata->pfcq[i] = 1;
2379 count++;
2380 }
2381
2382 return count;
2383}
2384
2385static void xgbe_calculate_dcb_fifo(struct xgbe_prv_data *pdata,
2386 unsigned int fifo_size,
2387 unsigned int *fifo)
2388{
2389 unsigned int q_fifo_size, rem_fifo, addn_fifo;
2390 unsigned int prio_queues;
2391 unsigned int pfc_count;
2392 unsigned int i;
2393
2394 q_fifo_size = XGMAC_FIFO_ALIGN(xgbe_get_max_frame(pdata));
2395 prio_queues = XGMAC_PRIO_QUEUES(pdata->rx_q_count);
2396 pfc_count = xgbe_get_pfc_queues(pdata);
2397
2398 if (!pfc_count || ((q_fifo_size * prio_queues) > fifo_size)) {
2399 /* No traffic classes with PFC enabled or can't do lossless */
2400 xgbe_calculate_equal_fifo(fifo_size, prio_queues, fifo);
2401 return;
2402 }
2403
2404 /* Calculate how much fifo we have to play with */
2405 rem_fifo = fifo_size - (q_fifo_size * prio_queues);
2406
2407 /* Calculate how much more than base fifo PFC needs, which also
2408 * becomes the threshold activation point (RFA)
2409 */
2410 pdata->pfc_rfa = xgbe_get_pfc_delay(pdata);
2411 pdata->pfc_rfa = XGMAC_FLOW_CONTROL_ALIGN(pdata->pfc_rfa);
2412
2413 if (pdata->pfc_rfa > q_fifo_size) {
2414 addn_fifo = pdata->pfc_rfa - q_fifo_size;
2415 addn_fifo = XGMAC_FIFO_ALIGN(addn_fifo);
2416 } else {
2417 addn_fifo = 0;
2418 }
2419
2420 /* Calculate DCB fifo settings:
2421 * - distribute remaining fifo between the VLAN priority
2422 * queues based on traffic class PFC enablement and overall
2423 * priority (0 is lowest priority, so start at highest)
2424 */
2425 i = prio_queues;
2426 while (i > 0) {
2427 i--;
2428
2429 fifo[i] = (q_fifo_size / XGMAC_FIFO_UNIT) - 1;
2430
2431 if (!pdata->pfcq[i] || !addn_fifo)
2432 continue;
2433
2434 if (addn_fifo > rem_fifo) {
2435 netdev_warn(pdata->netdev,
2436 "RXq%u cannot set needed fifo size\n", i);
2437 if (!rem_fifo)
2438 continue;
2439
2440 addn_fifo = rem_fifo;
2441 }
2442
2443 fifo[i] += (addn_fifo / XGMAC_FIFO_UNIT);
2444 rem_fifo -= addn_fifo;
2445 }
2446
2447 if (rem_fifo) {
2448 unsigned int inc_fifo = rem_fifo / prio_queues;
2449
2450 /* Distribute remaining fifo across queues */
2451 for (i = 0; i < prio_queues; i++)
2452 fifo[i] += (inc_fifo / XGMAC_FIFO_UNIT);
2453 }
2454}
2455
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002456static void xgbe_config_tx_fifo_size(struct xgbe_prv_data *pdata)
2457{
Lendacky, Thomas9c439e42015-09-30 08:53:03 -05002458 unsigned int fifo_size;
Lendacky, Thomas586e3cfb2016-11-03 13:17:48 -05002459 unsigned int fifo[XGBE_MAX_QUEUES];
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002460 unsigned int i;
2461
Lendacky, Thomas586e3cfb2016-11-03 13:17:48 -05002462 fifo_size = xgbe_get_tx_fifo_size(pdata);
2463
2464 xgbe_calculate_equal_fifo(fifo_size, pdata->tx_q_count, fifo);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002465
Lendacky, Thomas853eb162014-07-29 08:57:31 -05002466 for (i = 0; i < pdata->tx_q_count; i++)
Lendacky, Thomas586e3cfb2016-11-03 13:17:48 -05002467 XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_Q_TQOMR, TQS, fifo[i]);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002468
Lendacky, Thomas34bf65d2015-05-14 11:44:03 -05002469 netif_info(pdata, drv, pdata->netdev,
2470 "%d Tx hardware queues, %d byte fifo per queue\n",
Lendacky, Thomas43e0dcf2016-11-03 13:18:16 -05002471 pdata->tx_q_count, ((fifo[0] + 1) * XGMAC_FIFO_UNIT));
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002472}
2473
2474static void xgbe_config_rx_fifo_size(struct xgbe_prv_data *pdata)
2475{
Lendacky, Thomas9c439e42015-09-30 08:53:03 -05002476 unsigned int fifo_size;
Lendacky, Thomas586e3cfb2016-11-03 13:17:48 -05002477 unsigned int fifo[XGBE_MAX_QUEUES];
Lendacky, Thomas43e0dcf2016-11-03 13:18:16 -05002478 unsigned int prio_queues;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002479 unsigned int i;
2480
Lendacky, Thomas43e0dcf2016-11-03 13:18:16 -05002481 /* Clear any DCB related fifo/queue information */
2482 memset(pdata->pfcq, 0, sizeof(pdata->pfcq));
2483 pdata->pfc_rfa = 0;
Lendacky, Thomas586e3cfb2016-11-03 13:17:48 -05002484
Lendacky, Thomas43e0dcf2016-11-03 13:18:16 -05002485 fifo_size = xgbe_get_rx_fifo_size(pdata);
2486 prio_queues = XGMAC_PRIO_QUEUES(pdata->rx_q_count);
2487
2488 /* Assign a minimum fifo to the non-VLAN priority queues */
2489 fifo_size = xgbe_set_nonprio_fifos(fifo_size, pdata->rx_q_count, fifo);
2490
2491 if (pdata->pfc && pdata->ets)
2492 xgbe_calculate_dcb_fifo(pdata, fifo_size, fifo);
2493 else
2494 xgbe_calculate_equal_fifo(fifo_size, prio_queues, fifo);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002495
Lendacky, Thomas853eb162014-07-29 08:57:31 -05002496 for (i = 0; i < pdata->rx_q_count; i++)
Lendacky, Thomas586e3cfb2016-11-03 13:17:48 -05002497 XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_Q_RQOMR, RQS, fifo[i]);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002498
Lendacky, Thomas43e0dcf2016-11-03 13:18:16 -05002499 xgbe_calculate_flow_control_threshold(pdata, fifo);
2500 xgbe_config_flow_control_threshold(pdata);
2501
2502 if (pdata->pfc && pdata->ets && pdata->pfc->pfc_en) {
2503 netif_info(pdata, drv, pdata->netdev,
2504 "%u Rx hardware queues\n", pdata->rx_q_count);
2505 for (i = 0; i < pdata->rx_q_count; i++)
2506 netif_info(pdata, drv, pdata->netdev,
2507 "RxQ%u, %u byte fifo queue\n", i,
2508 ((fifo[i] + 1) * XGMAC_FIFO_UNIT));
2509 } else {
2510 netif_info(pdata, drv, pdata->netdev,
2511 "%u Rx hardware queues, %u byte fifo per queue\n",
2512 pdata->rx_q_count,
2513 ((fifo[0] + 1) * XGMAC_FIFO_UNIT));
2514 }
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002515}
2516
Lendacky, Thomasfca2d992014-07-29 08:57:55 -05002517static void xgbe_config_queue_mapping(struct xgbe_prv_data *pdata)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002518{
Lendacky, Thomasfca2d992014-07-29 08:57:55 -05002519 unsigned int qptc, qptc_extra, queue;
2520 unsigned int prio_queues;
2521 unsigned int ppq, ppq_extra, prio;
2522 unsigned int mask;
2523 unsigned int i, j, reg, reg_val;
2524
2525 /* Map the MTL Tx Queues to Traffic Classes
2526 * Note: Tx Queues >= Traffic Classes
2527 */
2528 qptc = pdata->tx_q_count / pdata->hw_feat.tc_cnt;
2529 qptc_extra = pdata->tx_q_count % pdata->hw_feat.tc_cnt;
2530
2531 for (i = 0, queue = 0; i < pdata->hw_feat.tc_cnt; i++) {
2532 for (j = 0; j < qptc; j++) {
Lendacky, Thomas34bf65d2015-05-14 11:44:03 -05002533 netif_dbg(pdata, drv, pdata->netdev,
2534 "TXq%u mapped to TC%u\n", queue, i);
Lendacky, Thomasfca2d992014-07-29 08:57:55 -05002535 XGMAC_MTL_IOWRITE_BITS(pdata, queue, MTL_Q_TQOMR,
2536 Q2TCMAP, i);
2537 pdata->q2tc_map[queue++] = i;
2538 }
2539
2540 if (i < qptc_extra) {
Lendacky, Thomas34bf65d2015-05-14 11:44:03 -05002541 netif_dbg(pdata, drv, pdata->netdev,
2542 "TXq%u mapped to TC%u\n", queue, i);
Lendacky, Thomasfca2d992014-07-29 08:57:55 -05002543 XGMAC_MTL_IOWRITE_BITS(pdata, queue, MTL_Q_TQOMR,
2544 Q2TCMAP, i);
2545 pdata->q2tc_map[queue++] = i;
2546 }
2547 }
2548
2549 /* Map the 8 VLAN priority values to available MTL Rx queues */
Lendacky, Thomas43e0dcf2016-11-03 13:18:16 -05002550 prio_queues = XGMAC_PRIO_QUEUES(pdata->rx_q_count);
Lendacky, Thomasfca2d992014-07-29 08:57:55 -05002551 ppq = IEEE_8021QAZ_MAX_TCS / prio_queues;
2552 ppq_extra = IEEE_8021QAZ_MAX_TCS % prio_queues;
2553
2554 reg = MAC_RQC2R;
2555 reg_val = 0;
2556 for (i = 0, prio = 0; i < prio_queues;) {
2557 mask = 0;
2558 for (j = 0; j < ppq; j++) {
Lendacky, Thomas34bf65d2015-05-14 11:44:03 -05002559 netif_dbg(pdata, drv, pdata->netdev,
2560 "PRIO%u mapped to RXq%u\n", prio, i);
Lendacky, Thomasfca2d992014-07-29 08:57:55 -05002561 mask |= (1 << prio);
2562 pdata->prio2q_map[prio++] = i;
2563 }
2564
2565 if (i < ppq_extra) {
Lendacky, Thomas34bf65d2015-05-14 11:44:03 -05002566 netif_dbg(pdata, drv, pdata->netdev,
2567 "PRIO%u mapped to RXq%u\n", prio, i);
Lendacky, Thomasfca2d992014-07-29 08:57:55 -05002568 mask |= (1 << prio);
2569 pdata->prio2q_map[prio++] = i;
2570 }
2571
2572 reg_val |= (mask << ((i++ % MAC_RQC2_Q_PER_REG) << 3));
2573
2574 if ((i % MAC_RQC2_Q_PER_REG) && (i != prio_queues))
2575 continue;
2576
2577 XGMAC_IOWRITE(pdata, reg, reg_val);
2578 reg += MAC_RQC2_INC;
2579 reg_val = 0;
2580 }
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002581
2582 /* Select dynamic mapping of MTL Rx queue to DMA Rx channel */
2583 reg = MTL_RQDCM0R;
2584 reg_val = 0;
Lendacky, Thomasfca2d992014-07-29 08:57:55 -05002585 for (i = 0; i < pdata->rx_q_count;) {
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002586 reg_val |= (0x80 << ((i++ % MTL_RQDCM_Q_PER_REG) << 3));
2587
Lendacky, Thomasfca2d992014-07-29 08:57:55 -05002588 if ((i % MTL_RQDCM_Q_PER_REG) && (i != pdata->rx_q_count))
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002589 continue;
2590
2591 XGMAC_IOWRITE(pdata, reg, reg_val);
2592
2593 reg += MTL_RQDCM_INC;
2594 reg_val = 0;
2595 }
2596}
2597
Lendacky, Thomas43e0dcf2016-11-03 13:18:16 -05002598static void xgbe_config_tc(struct xgbe_prv_data *pdata)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002599{
Lendacky, Thomas43e0dcf2016-11-03 13:18:16 -05002600 unsigned int offset, queue, prio;
2601 u8 i;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002602
Lendacky, Thomas43e0dcf2016-11-03 13:18:16 -05002603 netdev_reset_tc(pdata->netdev);
2604 if (!pdata->num_tcs)
2605 return;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002606
Lendacky, Thomas43e0dcf2016-11-03 13:18:16 -05002607 netdev_set_num_tc(pdata->netdev, pdata->num_tcs);
2608
2609 for (i = 0, queue = 0, offset = 0; i < pdata->num_tcs; i++) {
2610 while ((queue < pdata->tx_q_count) &&
2611 (pdata->q2tc_map[queue] == i))
2612 queue++;
2613
2614 netif_dbg(pdata, drv, pdata->netdev, "TC%u using TXq%u-%u\n",
2615 i, offset, queue - 1);
2616 netdev_set_tc_queue(pdata->netdev, i, queue - offset, offset);
2617 offset = queue;
2618 }
2619
2620 if (!pdata->ets)
2621 return;
2622
2623 for (prio = 0; prio < IEEE_8021QAZ_MAX_TCS; prio++)
2624 netdev_set_prio_tc_map(pdata->netdev, prio,
2625 pdata->ets->prio_tc[prio]);
2626}
2627
2628static void xgbe_config_dcb_tc(struct xgbe_prv_data *pdata)
2629{
2630 struct ieee_ets *ets = pdata->ets;
2631 unsigned int total_weight, min_weight, weight;
2632 unsigned int mask, reg, reg_val;
2633 unsigned int i, prio;
2634
2635 if (!ets)
2636 return;
2637
2638 /* Set Tx to deficit weighted round robin scheduling algorithm (when
2639 * traffic class is using ETS algorithm)
2640 */
2641 XGMAC_IOWRITE_BITS(pdata, MTL_OMR, ETSALG, MTL_ETSALG_DWRR);
2642
2643 /* Set Traffic Class algorithms */
2644 total_weight = pdata->netdev->mtu * pdata->hw_feat.tc_cnt;
2645 min_weight = total_weight / 100;
2646 if (!min_weight)
2647 min_weight = 1;
2648
2649 for (i = 0; i < pdata->hw_feat.tc_cnt; i++) {
2650 /* Map the priorities to the traffic class */
2651 mask = 0;
2652 for (prio = 0; prio < IEEE_8021QAZ_MAX_TCS; prio++) {
2653 if (ets->prio_tc[prio] == i)
2654 mask |= (1 << prio);
2655 }
2656 mask &= 0xff;
2657
2658 netif_dbg(pdata, drv, pdata->netdev, "TC%u PRIO mask=%#x\n",
2659 i, mask);
2660 reg = MTL_TCPM0R + (MTL_TCPM_INC * (i / MTL_TCPM_TC_PER_REG));
2661 reg_val = XGMAC_IOREAD(pdata, reg);
2662
2663 reg_val &= ~(0xff << ((i % MTL_TCPM_TC_PER_REG) << 3));
2664 reg_val |= (mask << ((i % MTL_TCPM_TC_PER_REG) << 3));
2665
2666 XGMAC_IOWRITE(pdata, reg, reg_val);
2667
2668 /* Set the traffic class algorithm */
2669 switch (ets->tc_tsa[i]) {
2670 case IEEE_8021QAZ_TSA_STRICT:
2671 netif_dbg(pdata, drv, pdata->netdev,
2672 "TC%u using SP\n", i);
2673 XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_TC_ETSCR, TSA,
2674 MTL_TSA_SP);
2675 break;
2676 case IEEE_8021QAZ_TSA_ETS:
2677 weight = total_weight * ets->tc_tx_bw[i] / 100;
2678 weight = clamp(weight, min_weight, total_weight);
2679
2680 netif_dbg(pdata, drv, pdata->netdev,
2681 "TC%u using DWRR (weight %u)\n", i, weight);
2682 XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_TC_ETSCR, TSA,
2683 MTL_TSA_ETS);
2684 XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_TC_QWR, QW,
2685 weight);
2686 break;
2687 }
2688 }
2689
2690 xgbe_config_tc(pdata);
2691}
2692
2693static void xgbe_config_dcb_pfc(struct xgbe_prv_data *pdata)
2694{
2695 if (!test_bit(XGBE_DOWN, &pdata->dev_state)) {
2696 /* Just stop the Tx queues while Rx fifo is changed */
2697 netif_tx_stop_all_queues(pdata->netdev);
2698
2699 /* Suspend Rx so that fifo's can be adjusted */
2700 pdata->hw_if.disable_rx(pdata);
2701 }
2702
2703 xgbe_config_rx_fifo_size(pdata);
2704 xgbe_config_flow_control(pdata);
2705
2706 if (!test_bit(XGBE_DOWN, &pdata->dev_state)) {
2707 /* Resume Rx */
2708 pdata->hw_if.enable_rx(pdata);
2709
2710 /* Resume Tx queues */
2711 netif_tx_start_all_queues(pdata->netdev);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002712 }
2713}
2714
2715static void xgbe_config_mac_address(struct xgbe_prv_data *pdata)
2716{
2717 xgbe_set_mac_address(pdata, pdata->netdev->dev_addr);
Lendacky, Thomasb85e4d82014-06-24 16:19:29 -05002718
2719 /* Filtering is done using perfect filtering and hash filtering */
2720 if (pdata->hw_feat.hash_table_size) {
2721 XGMAC_IOWRITE_BITS(pdata, MAC_PFR, HPF, 1);
2722 XGMAC_IOWRITE_BITS(pdata, MAC_PFR, HUC, 1);
2723 XGMAC_IOWRITE_BITS(pdata, MAC_PFR, HMC, 1);
2724 }
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002725}
2726
2727static void xgbe_config_jumbo_enable(struct xgbe_prv_data *pdata)
2728{
2729 unsigned int val;
2730
2731 val = (pdata->netdev->mtu > XGMAC_STD_PACKET_MTU) ? 1 : 0;
2732
2733 XGMAC_IOWRITE_BITS(pdata, MAC_RCR, JE, val);
2734}
2735
Lendacky, Thomas916102c2015-01-16 12:46:45 -06002736static void xgbe_config_mac_speed(struct xgbe_prv_data *pdata)
2737{
Lendacky, Thomase57f7a32016-11-03 13:18:27 -05002738 xgbe_set_speed(pdata, pdata->phy_speed);
Lendacky, Thomas916102c2015-01-16 12:46:45 -06002739}
2740
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002741static void xgbe_config_checksum_offload(struct xgbe_prv_data *pdata)
2742{
2743 if (pdata->netdev->features & NETIF_F_RXCSUM)
2744 xgbe_enable_rx_csum(pdata);
2745 else
2746 xgbe_disable_rx_csum(pdata);
2747}
2748
2749static void xgbe_config_vlan_support(struct xgbe_prv_data *pdata)
2750{
Lendacky, Thomas6e5eed02014-06-24 16:19:12 -05002751 /* Indicate that VLAN Tx CTAGs come from context descriptors */
2752 XGMAC_IOWRITE_BITS(pdata, MAC_VLANIR, CSVL, 0);
2753 XGMAC_IOWRITE_BITS(pdata, MAC_VLANIR, VLTI, 1);
2754
Lendacky, Thomas801c62d2014-06-24 16:19:24 -05002755 /* Set the current VLAN Hash Table register value */
2756 xgbe_update_vlan_hash_table(pdata);
2757
2758 if (pdata->netdev->features & NETIF_F_HW_VLAN_CTAG_FILTER)
2759 xgbe_enable_rx_vlan_filtering(pdata);
2760 else
2761 xgbe_disable_rx_vlan_filtering(pdata);
2762
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002763 if (pdata->netdev->features & NETIF_F_HW_VLAN_CTAG_RX)
2764 xgbe_enable_rx_vlan_stripping(pdata);
2765 else
2766 xgbe_disable_rx_vlan_stripping(pdata);
2767}
2768
Lendacky, Thomas60265102014-09-05 18:02:30 -05002769static u64 xgbe_mmc_read(struct xgbe_prv_data *pdata, unsigned int reg_lo)
2770{
2771 bool read_hi;
2772 u64 val;
2773
Lendacky, Thomase5a20b92016-11-03 13:19:07 -05002774 if (pdata->vdata->mmc_64bit) {
2775 switch (reg_lo) {
2776 /* These registers are always 32 bit */
2777 case MMC_RXRUNTERROR:
2778 case MMC_RXJABBERERROR:
2779 case MMC_RXUNDERSIZE_G:
2780 case MMC_RXOVERSIZE_G:
2781 case MMC_RXWATCHDOGERROR:
2782 read_hi = false;
2783 break;
Lendacky, Thomas60265102014-09-05 18:02:30 -05002784
Lendacky, Thomase5a20b92016-11-03 13:19:07 -05002785 default:
2786 read_hi = true;
2787 }
2788 } else {
2789 switch (reg_lo) {
2790 /* These registers are always 64 bit */
2791 case MMC_TXOCTETCOUNT_GB_LO:
2792 case MMC_TXOCTETCOUNT_G_LO:
2793 case MMC_RXOCTETCOUNT_GB_LO:
2794 case MMC_RXOCTETCOUNT_G_LO:
2795 read_hi = true;
2796 break;
2797
2798 default:
2799 read_hi = false;
2800 }
Lendacky, Thomas3947d782015-09-30 08:52:38 -05002801 }
Lendacky, Thomas60265102014-09-05 18:02:30 -05002802
2803 val = XGMAC_IOREAD(pdata, reg_lo);
2804
2805 if (read_hi)
2806 val |= ((u64)XGMAC_IOREAD(pdata, reg_lo + 4) << 32);
2807
2808 return val;
2809}
2810
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002811static void xgbe_tx_mmc_int(struct xgbe_prv_data *pdata)
2812{
2813 struct xgbe_mmc_stats *stats = &pdata->mmc_stats;
2814 unsigned int mmc_isr = XGMAC_IOREAD(pdata, MMC_TISR);
2815
2816 if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TXOCTETCOUNT_GB))
2817 stats->txoctetcount_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002818 xgbe_mmc_read(pdata, MMC_TXOCTETCOUNT_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002819
2820 if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TXFRAMECOUNT_GB))
2821 stats->txframecount_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002822 xgbe_mmc_read(pdata, MMC_TXFRAMECOUNT_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002823
2824 if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TXBROADCASTFRAMES_G))
2825 stats->txbroadcastframes_g +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002826 xgbe_mmc_read(pdata, MMC_TXBROADCASTFRAMES_G_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002827
2828 if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TXMULTICASTFRAMES_G))
2829 stats->txmulticastframes_g +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002830 xgbe_mmc_read(pdata, MMC_TXMULTICASTFRAMES_G_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002831
2832 if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TX64OCTETS_GB))
2833 stats->tx64octets_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002834 xgbe_mmc_read(pdata, MMC_TX64OCTETS_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002835
2836 if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TX65TO127OCTETS_GB))
2837 stats->tx65to127octets_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002838 xgbe_mmc_read(pdata, MMC_TX65TO127OCTETS_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002839
2840 if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TX128TO255OCTETS_GB))
2841 stats->tx128to255octets_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002842 xgbe_mmc_read(pdata, MMC_TX128TO255OCTETS_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002843
2844 if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TX256TO511OCTETS_GB))
2845 stats->tx256to511octets_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002846 xgbe_mmc_read(pdata, MMC_TX256TO511OCTETS_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002847
2848 if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TX512TO1023OCTETS_GB))
2849 stats->tx512to1023octets_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002850 xgbe_mmc_read(pdata, MMC_TX512TO1023OCTETS_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002851
2852 if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TX1024TOMAXOCTETS_GB))
2853 stats->tx1024tomaxoctets_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002854 xgbe_mmc_read(pdata, MMC_TX1024TOMAXOCTETS_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002855
2856 if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TXUNICASTFRAMES_GB))
2857 stats->txunicastframes_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002858 xgbe_mmc_read(pdata, MMC_TXUNICASTFRAMES_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002859
2860 if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TXMULTICASTFRAMES_GB))
2861 stats->txmulticastframes_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002862 xgbe_mmc_read(pdata, MMC_TXMULTICASTFRAMES_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002863
2864 if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TXBROADCASTFRAMES_GB))
2865 stats->txbroadcastframes_g +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002866 xgbe_mmc_read(pdata, MMC_TXBROADCASTFRAMES_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002867
2868 if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TXUNDERFLOWERROR))
2869 stats->txunderflowerror +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002870 xgbe_mmc_read(pdata, MMC_TXUNDERFLOWERROR_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002871
2872 if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TXOCTETCOUNT_G))
2873 stats->txoctetcount_g +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002874 xgbe_mmc_read(pdata, MMC_TXOCTETCOUNT_G_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002875
2876 if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TXFRAMECOUNT_G))
2877 stats->txframecount_g +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002878 xgbe_mmc_read(pdata, MMC_TXFRAMECOUNT_G_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002879
2880 if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TXPAUSEFRAMES))
2881 stats->txpauseframes +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002882 xgbe_mmc_read(pdata, MMC_TXPAUSEFRAMES_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002883
2884 if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TXVLANFRAMES_G))
2885 stats->txvlanframes_g +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002886 xgbe_mmc_read(pdata, MMC_TXVLANFRAMES_G_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002887}
2888
2889static void xgbe_rx_mmc_int(struct xgbe_prv_data *pdata)
2890{
2891 struct xgbe_mmc_stats *stats = &pdata->mmc_stats;
2892 unsigned int mmc_isr = XGMAC_IOREAD(pdata, MMC_RISR);
2893
2894 if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXFRAMECOUNT_GB))
2895 stats->rxframecount_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002896 xgbe_mmc_read(pdata, MMC_RXFRAMECOUNT_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002897
2898 if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXOCTETCOUNT_GB))
2899 stats->rxoctetcount_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002900 xgbe_mmc_read(pdata, MMC_RXOCTETCOUNT_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002901
2902 if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXOCTETCOUNT_G))
2903 stats->rxoctetcount_g +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002904 xgbe_mmc_read(pdata, MMC_RXOCTETCOUNT_G_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002905
2906 if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXBROADCASTFRAMES_G))
2907 stats->rxbroadcastframes_g +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002908 xgbe_mmc_read(pdata, MMC_RXBROADCASTFRAMES_G_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002909
2910 if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXMULTICASTFRAMES_G))
2911 stats->rxmulticastframes_g +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002912 xgbe_mmc_read(pdata, MMC_RXMULTICASTFRAMES_G_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002913
2914 if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXCRCERROR))
2915 stats->rxcrcerror +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002916 xgbe_mmc_read(pdata, MMC_RXCRCERROR_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002917
2918 if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXRUNTERROR))
2919 stats->rxrunterror +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002920 xgbe_mmc_read(pdata, MMC_RXRUNTERROR);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002921
2922 if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXJABBERERROR))
2923 stats->rxjabbererror +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002924 xgbe_mmc_read(pdata, MMC_RXJABBERERROR);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002925
2926 if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXUNDERSIZE_G))
2927 stats->rxundersize_g +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002928 xgbe_mmc_read(pdata, MMC_RXUNDERSIZE_G);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002929
2930 if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXOVERSIZE_G))
2931 stats->rxoversize_g +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002932 xgbe_mmc_read(pdata, MMC_RXOVERSIZE_G);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002933
2934 if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RX64OCTETS_GB))
2935 stats->rx64octets_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002936 xgbe_mmc_read(pdata, MMC_RX64OCTETS_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002937
2938 if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RX65TO127OCTETS_GB))
2939 stats->rx65to127octets_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002940 xgbe_mmc_read(pdata, MMC_RX65TO127OCTETS_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002941
2942 if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RX128TO255OCTETS_GB))
2943 stats->rx128to255octets_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002944 xgbe_mmc_read(pdata, MMC_RX128TO255OCTETS_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002945
2946 if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RX256TO511OCTETS_GB))
2947 stats->rx256to511octets_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002948 xgbe_mmc_read(pdata, MMC_RX256TO511OCTETS_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002949
2950 if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RX512TO1023OCTETS_GB))
2951 stats->rx512to1023octets_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002952 xgbe_mmc_read(pdata, MMC_RX512TO1023OCTETS_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002953
2954 if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RX1024TOMAXOCTETS_GB))
2955 stats->rx1024tomaxoctets_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002956 xgbe_mmc_read(pdata, MMC_RX1024TOMAXOCTETS_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002957
2958 if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXUNICASTFRAMES_G))
2959 stats->rxunicastframes_g +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002960 xgbe_mmc_read(pdata, MMC_RXUNICASTFRAMES_G_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002961
2962 if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXLENGTHERROR))
2963 stats->rxlengtherror +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002964 xgbe_mmc_read(pdata, MMC_RXLENGTHERROR_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002965
2966 if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXOUTOFRANGETYPE))
2967 stats->rxoutofrangetype +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002968 xgbe_mmc_read(pdata, MMC_RXOUTOFRANGETYPE_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002969
2970 if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXPAUSEFRAMES))
2971 stats->rxpauseframes +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002972 xgbe_mmc_read(pdata, MMC_RXPAUSEFRAMES_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002973
2974 if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXFIFOOVERFLOW))
2975 stats->rxfifooverflow +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002976 xgbe_mmc_read(pdata, MMC_RXFIFOOVERFLOW_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002977
2978 if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXVLANFRAMES_GB))
2979 stats->rxvlanframes_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002980 xgbe_mmc_read(pdata, MMC_RXVLANFRAMES_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002981
2982 if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXWATCHDOGERROR))
2983 stats->rxwatchdogerror +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002984 xgbe_mmc_read(pdata, MMC_RXWATCHDOGERROR);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002985}
2986
2987static void xgbe_read_mmc_stats(struct xgbe_prv_data *pdata)
2988{
2989 struct xgbe_mmc_stats *stats = &pdata->mmc_stats;
2990
2991 /* Freeze counters */
2992 XGMAC_IOWRITE_BITS(pdata, MMC_CR, MCF, 1);
2993
2994 stats->txoctetcount_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002995 xgbe_mmc_read(pdata, MMC_TXOCTETCOUNT_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002996
2997 stats->txframecount_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002998 xgbe_mmc_read(pdata, MMC_TXFRAMECOUNT_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002999
3000 stats->txbroadcastframes_g +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05003001 xgbe_mmc_read(pdata, MMC_TXBROADCASTFRAMES_G_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003002
3003 stats->txmulticastframes_g +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05003004 xgbe_mmc_read(pdata, MMC_TXMULTICASTFRAMES_G_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003005
3006 stats->tx64octets_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05003007 xgbe_mmc_read(pdata, MMC_TX64OCTETS_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003008
3009 stats->tx65to127octets_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05003010 xgbe_mmc_read(pdata, MMC_TX65TO127OCTETS_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003011
3012 stats->tx128to255octets_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05003013 xgbe_mmc_read(pdata, MMC_TX128TO255OCTETS_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003014
3015 stats->tx256to511octets_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05003016 xgbe_mmc_read(pdata, MMC_TX256TO511OCTETS_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003017
3018 stats->tx512to1023octets_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05003019 xgbe_mmc_read(pdata, MMC_TX512TO1023OCTETS_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003020
3021 stats->tx1024tomaxoctets_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05003022 xgbe_mmc_read(pdata, MMC_TX1024TOMAXOCTETS_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003023
3024 stats->txunicastframes_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05003025 xgbe_mmc_read(pdata, MMC_TXUNICASTFRAMES_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003026
3027 stats->txmulticastframes_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05003028 xgbe_mmc_read(pdata, MMC_TXMULTICASTFRAMES_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003029
3030 stats->txbroadcastframes_g +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05003031 xgbe_mmc_read(pdata, MMC_TXBROADCASTFRAMES_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003032
3033 stats->txunderflowerror +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05003034 xgbe_mmc_read(pdata, MMC_TXUNDERFLOWERROR_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003035
3036 stats->txoctetcount_g +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05003037 xgbe_mmc_read(pdata, MMC_TXOCTETCOUNT_G_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003038
3039 stats->txframecount_g +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05003040 xgbe_mmc_read(pdata, MMC_TXFRAMECOUNT_G_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003041
3042 stats->txpauseframes +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05003043 xgbe_mmc_read(pdata, MMC_TXPAUSEFRAMES_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003044
3045 stats->txvlanframes_g +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05003046 xgbe_mmc_read(pdata, MMC_TXVLANFRAMES_G_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003047
3048 stats->rxframecount_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05003049 xgbe_mmc_read(pdata, MMC_RXFRAMECOUNT_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003050
3051 stats->rxoctetcount_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05003052 xgbe_mmc_read(pdata, MMC_RXOCTETCOUNT_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003053
3054 stats->rxoctetcount_g +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05003055 xgbe_mmc_read(pdata, MMC_RXOCTETCOUNT_G_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003056
3057 stats->rxbroadcastframes_g +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05003058 xgbe_mmc_read(pdata, MMC_RXBROADCASTFRAMES_G_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003059
3060 stats->rxmulticastframes_g +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05003061 xgbe_mmc_read(pdata, MMC_RXMULTICASTFRAMES_G_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003062
3063 stats->rxcrcerror +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05003064 xgbe_mmc_read(pdata, MMC_RXCRCERROR_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003065
3066 stats->rxrunterror +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05003067 xgbe_mmc_read(pdata, MMC_RXRUNTERROR);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003068
3069 stats->rxjabbererror +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05003070 xgbe_mmc_read(pdata, MMC_RXJABBERERROR);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003071
3072 stats->rxundersize_g +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05003073 xgbe_mmc_read(pdata, MMC_RXUNDERSIZE_G);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003074
3075 stats->rxoversize_g +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05003076 xgbe_mmc_read(pdata, MMC_RXOVERSIZE_G);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003077
3078 stats->rx64octets_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05003079 xgbe_mmc_read(pdata, MMC_RX64OCTETS_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003080
3081 stats->rx65to127octets_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05003082 xgbe_mmc_read(pdata, MMC_RX65TO127OCTETS_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003083
3084 stats->rx128to255octets_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05003085 xgbe_mmc_read(pdata, MMC_RX128TO255OCTETS_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003086
3087 stats->rx256to511octets_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05003088 xgbe_mmc_read(pdata, MMC_RX256TO511OCTETS_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003089
3090 stats->rx512to1023octets_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05003091 xgbe_mmc_read(pdata, MMC_RX512TO1023OCTETS_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003092
3093 stats->rx1024tomaxoctets_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05003094 xgbe_mmc_read(pdata, MMC_RX1024TOMAXOCTETS_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003095
3096 stats->rxunicastframes_g +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05003097 xgbe_mmc_read(pdata, MMC_RXUNICASTFRAMES_G_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003098
3099 stats->rxlengtherror +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05003100 xgbe_mmc_read(pdata, MMC_RXLENGTHERROR_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003101
3102 stats->rxoutofrangetype +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05003103 xgbe_mmc_read(pdata, MMC_RXOUTOFRANGETYPE_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003104
3105 stats->rxpauseframes +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05003106 xgbe_mmc_read(pdata, MMC_RXPAUSEFRAMES_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003107
3108 stats->rxfifooverflow +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05003109 xgbe_mmc_read(pdata, MMC_RXFIFOOVERFLOW_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003110
3111 stats->rxvlanframes_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05003112 xgbe_mmc_read(pdata, MMC_RXVLANFRAMES_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003113
3114 stats->rxwatchdogerror +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05003115 xgbe_mmc_read(pdata, MMC_RXWATCHDOGERROR);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003116
3117 /* Un-freeze counters */
3118 XGMAC_IOWRITE_BITS(pdata, MMC_CR, MCF, 0);
3119}
3120
3121static void xgbe_config_mmc(struct xgbe_prv_data *pdata)
3122{
3123 /* Set counters to reset on read */
3124 XGMAC_IOWRITE_BITS(pdata, MMC_CR, ROR, 1);
3125
3126 /* Reset the counters */
3127 XGMAC_IOWRITE_BITS(pdata, MMC_CR, CR, 1);
3128}
3129
Lendacky, Thomas4b8acdf2016-11-03 13:19:17 -05003130static void xgbe_txq_prepare_tx_stop(struct xgbe_prv_data *pdata,
3131 unsigned int queue)
3132{
3133 unsigned int tx_status;
3134 unsigned long tx_timeout;
3135
3136 /* The Tx engine cannot be stopped if it is actively processing
3137 * packets. Wait for the Tx queue to empty the Tx fifo. Don't
3138 * wait forever though...
3139 */
3140 tx_timeout = jiffies + (XGBE_DMA_STOP_TIMEOUT * HZ);
3141 while (time_before(jiffies, tx_timeout)) {
3142 tx_status = XGMAC_MTL_IOREAD(pdata, queue, MTL_Q_TQDR);
3143 if ((XGMAC_GET_BITS(tx_status, MTL_Q_TQDR, TRCSTS) != 1) &&
3144 (XGMAC_GET_BITS(tx_status, MTL_Q_TQDR, TXQSTS) == 0))
3145 break;
3146
3147 usleep_range(500, 1000);
3148 }
3149
3150 if (!time_before(jiffies, tx_timeout))
3151 netdev_info(pdata->netdev,
3152 "timed out waiting for Tx queue %u to empty\n",
3153 queue);
3154}
3155
Lendacky, Thomas16edd342014-11-20 11:03:32 -06003156static void xgbe_prepare_tx_stop(struct xgbe_prv_data *pdata,
Lendacky, Thomas4b8acdf2016-11-03 13:19:17 -05003157 unsigned int queue)
Lendacky, Thomas16edd342014-11-20 11:03:32 -06003158{
3159 unsigned int tx_dsr, tx_pos, tx_qidx;
3160 unsigned int tx_status;
3161 unsigned long tx_timeout;
3162
Lendacky, Thomas4b8acdf2016-11-03 13:19:17 -05003163 if (XGMAC_GET_BITS(pdata->hw_feat.version, MAC_VR, SNPSVER) > 0x20)
3164 return xgbe_txq_prepare_tx_stop(pdata, queue);
3165
Lendacky, Thomas16edd342014-11-20 11:03:32 -06003166 /* Calculate the status register to read and the position within */
Lendacky, Thomas4b8acdf2016-11-03 13:19:17 -05003167 if (queue < DMA_DSRX_FIRST_QUEUE) {
Lendacky, Thomas16edd342014-11-20 11:03:32 -06003168 tx_dsr = DMA_DSR0;
Lendacky, Thomas4b8acdf2016-11-03 13:19:17 -05003169 tx_pos = (queue * DMA_DSR_Q_WIDTH) + DMA_DSR0_TPS_START;
Lendacky, Thomas16edd342014-11-20 11:03:32 -06003170 } else {
Lendacky, Thomas4b8acdf2016-11-03 13:19:17 -05003171 tx_qidx = queue - DMA_DSRX_FIRST_QUEUE;
Lendacky, Thomas16edd342014-11-20 11:03:32 -06003172
3173 tx_dsr = DMA_DSR1 + ((tx_qidx / DMA_DSRX_QPR) * DMA_DSRX_INC);
3174 tx_pos = ((tx_qidx % DMA_DSRX_QPR) * DMA_DSR_Q_WIDTH) +
3175 DMA_DSRX_TPS_START;
3176 }
3177
3178 /* The Tx engine cannot be stopped if it is actively processing
3179 * descriptors. Wait for the Tx engine to enter the stopped or
3180 * suspended state. Don't wait forever though...
3181 */
3182 tx_timeout = jiffies + (XGBE_DMA_STOP_TIMEOUT * HZ);
3183 while (time_before(jiffies, tx_timeout)) {
3184 tx_status = XGMAC_IOREAD(pdata, tx_dsr);
3185 tx_status = GET_BITS(tx_status, tx_pos, DMA_DSR_TPS_WIDTH);
3186 if ((tx_status == DMA_TPS_STOPPED) ||
3187 (tx_status == DMA_TPS_SUSPENDED))
3188 break;
3189
3190 usleep_range(500, 1000);
3191 }
3192
3193 if (!time_before(jiffies, tx_timeout))
3194 netdev_info(pdata->netdev,
3195 "timed out waiting for Tx DMA channel %u to stop\n",
Lendacky, Thomas4b8acdf2016-11-03 13:19:17 -05003196 queue);
Lendacky, Thomas16edd342014-11-20 11:03:32 -06003197}
3198
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003199static void xgbe_enable_tx(struct xgbe_prv_data *pdata)
3200{
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003201 unsigned int i;
3202
3203 /* Enable each Tx DMA channel */
Lendacky, Thomas18f9f0a2017-06-28 13:42:51 -05003204 for (i = 0; i < pdata->channel_count; i++) {
3205 if (!pdata->channel[i]->tx_ring)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003206 break;
3207
Lendacky, Thomas18f9f0a2017-06-28 13:42:51 -05003208 XGMAC_DMA_IOWRITE_BITS(pdata->channel[i], DMA_CH_TCR, ST, 1);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003209 }
3210
3211 /* Enable each Tx queue */
Lendacky, Thomas853eb162014-07-29 08:57:31 -05003212 for (i = 0; i < pdata->tx_q_count; i++)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003213 XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_Q_TQOMR, TXQEN,
3214 MTL_Q_ENABLED);
3215
3216 /* Enable MAC Tx */
3217 XGMAC_IOWRITE_BITS(pdata, MAC_TCR, TE, 1);
3218}
3219
3220static void xgbe_disable_tx(struct xgbe_prv_data *pdata)
3221{
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003222 unsigned int i;
3223
Lendacky, Thomas16edd342014-11-20 11:03:32 -06003224 /* Prepare for Tx DMA channel stop */
Lendacky, Thomas4b8acdf2016-11-03 13:19:17 -05003225 for (i = 0; i < pdata->tx_q_count; i++)
3226 xgbe_prepare_tx_stop(pdata, i);
Lendacky, Thomas16edd342014-11-20 11:03:32 -06003227
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003228 /* Disable MAC Tx */
3229 XGMAC_IOWRITE_BITS(pdata, MAC_TCR, TE, 0);
3230
3231 /* Disable each Tx queue */
Lendacky, Thomas853eb162014-07-29 08:57:31 -05003232 for (i = 0; i < pdata->tx_q_count; i++)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003233 XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_Q_TQOMR, TXQEN, 0);
3234
3235 /* Disable each Tx DMA channel */
Lendacky, Thomas18f9f0a2017-06-28 13:42:51 -05003236 for (i = 0; i < pdata->channel_count; i++) {
3237 if (!pdata->channel[i]->tx_ring)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003238 break;
3239
Lendacky, Thomas18f9f0a2017-06-28 13:42:51 -05003240 XGMAC_DMA_IOWRITE_BITS(pdata->channel[i], DMA_CH_TCR, ST, 0);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003241 }
3242}
3243
Lendacky, Thomasc3727d62016-02-17 11:49:16 -06003244static void xgbe_prepare_rx_stop(struct xgbe_prv_data *pdata,
3245 unsigned int queue)
3246{
3247 unsigned int rx_status;
3248 unsigned long rx_timeout;
3249
3250 /* The Rx engine cannot be stopped if it is actively processing
3251 * packets. Wait for the Rx queue to empty the Rx fifo. Don't
3252 * wait forever though...
3253 */
3254 rx_timeout = jiffies + (XGBE_DMA_STOP_TIMEOUT * HZ);
3255 while (time_before(jiffies, rx_timeout)) {
3256 rx_status = XGMAC_MTL_IOREAD(pdata, queue, MTL_Q_RQDR);
3257 if ((XGMAC_GET_BITS(rx_status, MTL_Q_RQDR, PRXQ) == 0) &&
3258 (XGMAC_GET_BITS(rx_status, MTL_Q_RQDR, RXQSTS) == 0))
3259 break;
3260
3261 usleep_range(500, 1000);
3262 }
3263
3264 if (!time_before(jiffies, rx_timeout))
3265 netdev_info(pdata->netdev,
3266 "timed out waiting for Rx queue %u to empty\n",
3267 queue);
3268}
3269
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003270static void xgbe_enable_rx(struct xgbe_prv_data *pdata)
3271{
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003272 unsigned int reg_val, i;
3273
3274 /* Enable each Rx DMA channel */
Lendacky, Thomas18f9f0a2017-06-28 13:42:51 -05003275 for (i = 0; i < pdata->channel_count; i++) {
3276 if (!pdata->channel[i]->rx_ring)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003277 break;
3278
Lendacky, Thomas18f9f0a2017-06-28 13:42:51 -05003279 XGMAC_DMA_IOWRITE_BITS(pdata->channel[i], DMA_CH_RCR, SR, 1);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003280 }
3281
3282 /* Enable each Rx queue */
3283 reg_val = 0;
Lendacky, Thomas853eb162014-07-29 08:57:31 -05003284 for (i = 0; i < pdata->rx_q_count; i++)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003285 reg_val |= (0x02 << (i << 1));
3286 XGMAC_IOWRITE(pdata, MAC_RQC0R, reg_val);
3287
3288 /* Enable MAC Rx */
3289 XGMAC_IOWRITE_BITS(pdata, MAC_RCR, DCRCC, 1);
3290 XGMAC_IOWRITE_BITS(pdata, MAC_RCR, CST, 1);
3291 XGMAC_IOWRITE_BITS(pdata, MAC_RCR, ACS, 1);
3292 XGMAC_IOWRITE_BITS(pdata, MAC_RCR, RE, 1);
3293}
3294
3295static void xgbe_disable_rx(struct xgbe_prv_data *pdata)
3296{
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003297 unsigned int i;
3298
3299 /* Disable MAC Rx */
3300 XGMAC_IOWRITE_BITS(pdata, MAC_RCR, DCRCC, 0);
3301 XGMAC_IOWRITE_BITS(pdata, MAC_RCR, CST, 0);
3302 XGMAC_IOWRITE_BITS(pdata, MAC_RCR, ACS, 0);
3303 XGMAC_IOWRITE_BITS(pdata, MAC_RCR, RE, 0);
3304
Lendacky, Thomasc3727d62016-02-17 11:49:16 -06003305 /* Prepare for Rx DMA channel stop */
3306 for (i = 0; i < pdata->rx_q_count; i++)
3307 xgbe_prepare_rx_stop(pdata, i);
3308
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003309 /* Disable each Rx queue */
3310 XGMAC_IOWRITE(pdata, MAC_RQC0R, 0);
3311
3312 /* Disable each Rx DMA channel */
Lendacky, Thomas18f9f0a2017-06-28 13:42:51 -05003313 for (i = 0; i < pdata->channel_count; i++) {
3314 if (!pdata->channel[i]->rx_ring)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003315 break;
3316
Lendacky, Thomas18f9f0a2017-06-28 13:42:51 -05003317 XGMAC_DMA_IOWRITE_BITS(pdata->channel[i], DMA_CH_RCR, SR, 0);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003318 }
3319}
3320
3321static void xgbe_powerup_tx(struct xgbe_prv_data *pdata)
3322{
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003323 unsigned int i;
3324
3325 /* Enable each Tx DMA channel */
Lendacky, Thomas18f9f0a2017-06-28 13:42:51 -05003326 for (i = 0; i < pdata->channel_count; i++) {
3327 if (!pdata->channel[i]->tx_ring)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003328 break;
3329
Lendacky, Thomas18f9f0a2017-06-28 13:42:51 -05003330 XGMAC_DMA_IOWRITE_BITS(pdata->channel[i], DMA_CH_TCR, ST, 1);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003331 }
3332
3333 /* Enable MAC Tx */
3334 XGMAC_IOWRITE_BITS(pdata, MAC_TCR, TE, 1);
3335}
3336
3337static void xgbe_powerdown_tx(struct xgbe_prv_data *pdata)
3338{
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003339 unsigned int i;
3340
Lendacky, Thomas16edd342014-11-20 11:03:32 -06003341 /* Prepare for Tx DMA channel stop */
Lendacky, Thomas4b8acdf2016-11-03 13:19:17 -05003342 for (i = 0; i < pdata->tx_q_count; i++)
3343 xgbe_prepare_tx_stop(pdata, i);
Lendacky, Thomas16edd342014-11-20 11:03:32 -06003344
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003345 /* Disable MAC Tx */
3346 XGMAC_IOWRITE_BITS(pdata, MAC_TCR, TE, 0);
3347
3348 /* Disable each Tx DMA channel */
Lendacky, Thomas18f9f0a2017-06-28 13:42:51 -05003349 for (i = 0; i < pdata->channel_count; i++) {
3350 if (!pdata->channel[i]->tx_ring)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003351 break;
3352
Lendacky, Thomas18f9f0a2017-06-28 13:42:51 -05003353 XGMAC_DMA_IOWRITE_BITS(pdata->channel[i], DMA_CH_TCR, ST, 0);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003354 }
3355}
3356
3357static void xgbe_powerup_rx(struct xgbe_prv_data *pdata)
3358{
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003359 unsigned int i;
3360
3361 /* Enable each Rx DMA channel */
Lendacky, Thomas18f9f0a2017-06-28 13:42:51 -05003362 for (i = 0; i < pdata->channel_count; i++) {
3363 if (!pdata->channel[i]->rx_ring)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003364 break;
3365
Lendacky, Thomas18f9f0a2017-06-28 13:42:51 -05003366 XGMAC_DMA_IOWRITE_BITS(pdata->channel[i], DMA_CH_RCR, SR, 1);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003367 }
3368}
3369
3370static void xgbe_powerdown_rx(struct xgbe_prv_data *pdata)
3371{
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003372 unsigned int i;
3373
3374 /* Disable each Rx DMA channel */
Lendacky, Thomas18f9f0a2017-06-28 13:42:51 -05003375 for (i = 0; i < pdata->channel_count; i++) {
3376 if (!pdata->channel[i]->rx_ring)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003377 break;
3378
Lendacky, Thomas18f9f0a2017-06-28 13:42:51 -05003379 XGMAC_DMA_IOWRITE_BITS(pdata->channel[i], DMA_CH_RCR, SR, 0);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003380 }
3381}
3382
3383static int xgbe_init(struct xgbe_prv_data *pdata)
3384{
3385 struct xgbe_desc_if *desc_if = &pdata->desc_if;
3386 int ret;
3387
3388 DBGPR("-->xgbe_init\n");
3389
3390 /* Flush Tx queues */
3391 ret = xgbe_flush_tx_queues(pdata);
Lendacky, Thomas738f7f62017-01-20 12:14:13 -06003392 if (ret) {
3393 netdev_err(pdata->netdev, "error flushing TX queues\n");
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003394 return ret;
Lendacky, Thomas738f7f62017-01-20 12:14:13 -06003395 }
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003396
3397 /*
3398 * Initialize DMA related features
3399 */
3400 xgbe_config_dma_bus(pdata);
3401 xgbe_config_dma_cache(pdata);
3402 xgbe_config_osp_mode(pdata);
3403 xgbe_config_pblx8(pdata);
3404 xgbe_config_tx_pbl_val(pdata);
3405 xgbe_config_rx_pbl_val(pdata);
3406 xgbe_config_rx_coalesce(pdata);
3407 xgbe_config_tx_coalesce(pdata);
3408 xgbe_config_rx_buffer_size(pdata);
3409 xgbe_config_tso_mode(pdata);
Lendacky, Thomas174fd252014-11-04 16:06:50 -06003410 xgbe_config_sph_mode(pdata);
Lendacky, Thomas5b9dfe22014-11-04 16:07:02 -06003411 xgbe_config_rss(pdata);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003412 desc_if->wrapper_tx_desc_init(pdata);
3413 desc_if->wrapper_rx_desc_init(pdata);
3414 xgbe_enable_dma_interrupts(pdata);
3415
3416 /*
3417 * Initialize MTL related features
3418 */
3419 xgbe_config_mtl_mode(pdata);
Lendacky, Thomasfca2d992014-07-29 08:57:55 -05003420 xgbe_config_queue_mapping(pdata);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003421 xgbe_config_tsf_mode(pdata, pdata->tx_sf_mode);
3422 xgbe_config_rsf_mode(pdata, pdata->rx_sf_mode);
3423 xgbe_config_tx_threshold(pdata, pdata->tx_threshold);
3424 xgbe_config_rx_threshold(pdata, pdata->rx_threshold);
3425 xgbe_config_tx_fifo_size(pdata);
3426 xgbe_config_rx_fifo_size(pdata);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003427 /*TODO: Error Packet and undersized good Packet forwarding enable
3428 (FEP and FUP)
3429 */
Lendacky, Thomasfca2d992014-07-29 08:57:55 -05003430 xgbe_config_dcb_tc(pdata);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003431 xgbe_enable_mtl_interrupts(pdata);
3432
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003433 /*
3434 * Initialize MAC related features
3435 */
3436 xgbe_config_mac_address(pdata);
Lendacky, Thomasb8763822015-04-09 12:11:57 -05003437 xgbe_config_rx_mode(pdata);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003438 xgbe_config_jumbo_enable(pdata);
3439 xgbe_config_flow_control(pdata);
Lendacky, Thomas916102c2015-01-16 12:46:45 -06003440 xgbe_config_mac_speed(pdata);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003441 xgbe_config_checksum_offload(pdata);
3442 xgbe_config_vlan_support(pdata);
3443 xgbe_config_mmc(pdata);
3444 xgbe_enable_mac_interrupts(pdata);
3445
Lendacky, Thomase78332b2016-11-10 17:10:26 -06003446 /*
3447 * Initialize ECC related features
3448 */
3449 xgbe_enable_ecc_interrupts(pdata);
3450
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003451 DBGPR("<--xgbe_init\n");
3452
3453 return 0;
3454}
3455
3456void xgbe_init_function_ptrs_dev(struct xgbe_hw_if *hw_if)
3457{
3458 DBGPR("-->xgbe_init_function_ptrs\n");
3459
3460 hw_if->tx_complete = xgbe_tx_complete;
3461
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003462 hw_if->set_mac_address = xgbe_set_mac_address;
Lendacky, Thomasb8763822015-04-09 12:11:57 -05003463 hw_if->config_rx_mode = xgbe_config_rx_mode;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003464
3465 hw_if->enable_rx_csum = xgbe_enable_rx_csum;
3466 hw_if->disable_rx_csum = xgbe_disable_rx_csum;
3467
3468 hw_if->enable_rx_vlan_stripping = xgbe_enable_rx_vlan_stripping;
3469 hw_if->disable_rx_vlan_stripping = xgbe_disable_rx_vlan_stripping;
Lendacky, Thomas801c62d2014-06-24 16:19:24 -05003470 hw_if->enable_rx_vlan_filtering = xgbe_enable_rx_vlan_filtering;
3471 hw_if->disable_rx_vlan_filtering = xgbe_disable_rx_vlan_filtering;
3472 hw_if->update_vlan_hash_table = xgbe_update_vlan_hash_table;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003473
3474 hw_if->read_mmd_regs = xgbe_read_mmd_regs;
3475 hw_if->write_mmd_regs = xgbe_write_mmd_regs;
3476
Lendacky, Thomase57f7a32016-11-03 13:18:27 -05003477 hw_if->set_speed = xgbe_set_speed;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003478
Lendacky, Thomas732f2ab2016-11-10 17:11:14 -06003479 hw_if->set_ext_mii_mode = xgbe_set_ext_mii_mode;
3480 hw_if->read_ext_mii_regs = xgbe_read_ext_mii_regs;
3481 hw_if->write_ext_mii_regs = xgbe_write_ext_mii_regs;
3482
3483 hw_if->set_gpio = xgbe_set_gpio;
3484 hw_if->clr_gpio = xgbe_clr_gpio;
3485
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003486 hw_if->enable_tx = xgbe_enable_tx;
3487 hw_if->disable_tx = xgbe_disable_tx;
3488 hw_if->enable_rx = xgbe_enable_rx;
3489 hw_if->disable_rx = xgbe_disable_rx;
3490
3491 hw_if->powerup_tx = xgbe_powerup_tx;
3492 hw_if->powerdown_tx = xgbe_powerdown_tx;
3493 hw_if->powerup_rx = xgbe_powerup_rx;
3494 hw_if->powerdown_rx = xgbe_powerdown_rx;
3495
Lendacky, Thomasa9d41982014-11-04 16:06:32 -06003496 hw_if->dev_xmit = xgbe_dev_xmit;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003497 hw_if->dev_read = xgbe_dev_read;
3498 hw_if->enable_int = xgbe_enable_int;
3499 hw_if->disable_int = xgbe_disable_int;
3500 hw_if->init = xgbe_init;
3501 hw_if->exit = xgbe_exit;
3502
3503 /* Descriptor related Sequences have to be initialized here */
3504 hw_if->tx_desc_init = xgbe_tx_desc_init;
3505 hw_if->rx_desc_init = xgbe_rx_desc_init;
3506 hw_if->tx_desc_reset = xgbe_tx_desc_reset;
3507 hw_if->rx_desc_reset = xgbe_rx_desc_reset;
3508 hw_if->is_last_desc = xgbe_is_last_desc;
3509 hw_if->is_context_desc = xgbe_is_context_desc;
Lendacky, Thomas16958a22014-11-20 11:04:08 -06003510 hw_if->tx_start_xmit = xgbe_tx_start_xmit;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003511
3512 /* For FLOW ctrl */
3513 hw_if->config_tx_flow_control = xgbe_config_tx_flow_control;
3514 hw_if->config_rx_flow_control = xgbe_config_rx_flow_control;
3515
3516 /* For RX coalescing */
3517 hw_if->config_rx_coalesce = xgbe_config_rx_coalesce;
3518 hw_if->config_tx_coalesce = xgbe_config_tx_coalesce;
3519 hw_if->usec_to_riwt = xgbe_usec_to_riwt;
3520 hw_if->riwt_to_usec = xgbe_riwt_to_usec;
3521
3522 /* For RX and TX threshold config */
3523 hw_if->config_rx_threshold = xgbe_config_rx_threshold;
3524 hw_if->config_tx_threshold = xgbe_config_tx_threshold;
3525
3526 /* For RX and TX Store and Forward Mode config */
3527 hw_if->config_rsf_mode = xgbe_config_rsf_mode;
3528 hw_if->config_tsf_mode = xgbe_config_tsf_mode;
3529
3530 /* For TX DMA Operating on Second Frame config */
3531 hw_if->config_osp_mode = xgbe_config_osp_mode;
3532
3533 /* For RX and TX PBL config */
3534 hw_if->config_rx_pbl_val = xgbe_config_rx_pbl_val;
3535 hw_if->get_rx_pbl_val = xgbe_get_rx_pbl_val;
3536 hw_if->config_tx_pbl_val = xgbe_config_tx_pbl_val;
3537 hw_if->get_tx_pbl_val = xgbe_get_tx_pbl_val;
3538 hw_if->config_pblx8 = xgbe_config_pblx8;
3539
3540 /* For MMC statistics support */
3541 hw_if->tx_mmc_int = xgbe_tx_mmc_int;
3542 hw_if->rx_mmc_int = xgbe_rx_mmc_int;
3543 hw_if->read_mmc_stats = xgbe_read_mmc_stats;
3544
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05003545 /* For PTP config */
3546 hw_if->config_tstamp = xgbe_config_tstamp;
3547 hw_if->update_tstamp_addend = xgbe_update_tstamp_addend;
3548 hw_if->set_tstamp_time = xgbe_set_tstamp_time;
3549 hw_if->get_tstamp_time = xgbe_get_tstamp_time;
3550 hw_if->get_tx_tstamp = xgbe_get_tx_tstamp;
3551
Lendacky, Thomasfca2d992014-07-29 08:57:55 -05003552 /* For Data Center Bridging config */
Lendacky, Thomasb3b71592016-02-17 11:49:08 -06003553 hw_if->config_tc = xgbe_config_tc;
Lendacky, Thomasfca2d992014-07-29 08:57:55 -05003554 hw_if->config_dcb_tc = xgbe_config_dcb_tc;
3555 hw_if->config_dcb_pfc = xgbe_config_dcb_pfc;
3556
Lendacky, Thomas5b9dfe22014-11-04 16:07:02 -06003557 /* For Receive Side Scaling */
3558 hw_if->enable_rss = xgbe_enable_rss;
3559 hw_if->disable_rss = xgbe_disable_rss;
Lendacky, Thomasf6ac8622014-11-04 16:07:23 -06003560 hw_if->set_rss_hash_key = xgbe_set_rss_hash_key;
3561 hw_if->set_rss_lookup_table = xgbe_set_rss_lookup_table;
Lendacky, Thomas5b9dfe22014-11-04 16:07:02 -06003562
Lendacky, Thomase78332b2016-11-10 17:10:26 -06003563 /* For ECC */
3564 hw_if->disable_ecc_ded = xgbe_disable_ecc_ded;
3565 hw_if->disable_ecc_sec = xgbe_disable_ecc_sec;
3566
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003567 DBGPR("<--xgbe_init_function_ptrs\n");
3568}