blob: a9784084202adce7774cbc939146c2372ecdeff2 [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
Lendacky, Thomas7e1e6b82017-06-28 13:43:18 -0500177static int xgbe_config_pbl_val(struct xgbe_prv_data *pdata)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500178{
Lendacky, Thomas7e1e6b82017-06-28 13:43:18 -0500179 unsigned int pblx8, pbl;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500180 unsigned int i;
181
Lendacky, Thomas7e1e6b82017-06-28 13:43:18 -0500182 pblx8 = DMA_PBL_X8_DISABLE;
183 pbl = pdata->pbl;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500184
Lendacky, Thomas7e1e6b82017-06-28 13:43:18 -0500185 if (pdata->pbl > 32) {
186 pblx8 = DMA_PBL_X8_ENABLE;
187 pbl >>= 3;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500188 }
189
Lendacky, Thomas18f9f0a2017-06-28 13:42:51 -0500190 for (i = 0; i < pdata->channel_count; i++) {
Lendacky, Thomas7e1e6b82017-06-28 13:43:18 -0500191 XGMAC_DMA_IOWRITE_BITS(pdata->channel[i], DMA_CH_CR, PBLX8,
192 pblx8);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500193
Lendacky, Thomas7e1e6b82017-06-28 13:43:18 -0500194 if (pdata->channel[i]->tx_ring)
195 XGMAC_DMA_IOWRITE_BITS(pdata->channel[i], DMA_CH_TCR,
196 PBL, pbl);
197
198 if (pdata->channel[i]->rx_ring)
199 XGMAC_DMA_IOWRITE_BITS(pdata->channel[i], DMA_CH_RCR,
200 PBL, pbl);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500201 }
202
203 return 0;
204}
205
206static int xgbe_config_osp_mode(struct xgbe_prv_data *pdata)
207{
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500208 unsigned int i;
209
Lendacky, Thomas18f9f0a2017-06-28 13:42:51 -0500210 for (i = 0; i < pdata->channel_count; i++) {
211 if (!pdata->channel[i]->tx_ring)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500212 break;
213
Lendacky, Thomas18f9f0a2017-06-28 13:42:51 -0500214 XGMAC_DMA_IOWRITE_BITS(pdata->channel[i], DMA_CH_TCR, OSP,
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500215 pdata->tx_osp_mode);
216 }
217
218 return 0;
219}
220
221static int xgbe_config_rsf_mode(struct xgbe_prv_data *pdata, unsigned int val)
222{
223 unsigned int i;
224
Lendacky, Thomas853eb162014-07-29 08:57:31 -0500225 for (i = 0; i < pdata->rx_q_count; i++)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500226 XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_Q_RQOMR, RSF, val);
227
228 return 0;
229}
230
231static int xgbe_config_tsf_mode(struct xgbe_prv_data *pdata, unsigned int val)
232{
233 unsigned int i;
234
Lendacky, Thomas853eb162014-07-29 08:57:31 -0500235 for (i = 0; i < pdata->tx_q_count; i++)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500236 XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_Q_TQOMR, TSF, val);
237
238 return 0;
239}
240
241static int xgbe_config_rx_threshold(struct xgbe_prv_data *pdata,
242 unsigned int val)
243{
244 unsigned int i;
245
Lendacky, Thomas853eb162014-07-29 08:57:31 -0500246 for (i = 0; i < pdata->rx_q_count; i++)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500247 XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_Q_RQOMR, RTC, val);
248
249 return 0;
250}
251
252static int xgbe_config_tx_threshold(struct xgbe_prv_data *pdata,
253 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, TTC, val);
259
260 return 0;
261}
262
263static int xgbe_config_rx_coalesce(struct xgbe_prv_data *pdata)
264{
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500265 unsigned int i;
266
Lendacky, Thomas18f9f0a2017-06-28 13:42:51 -0500267 for (i = 0; i < pdata->channel_count; i++) {
268 if (!pdata->channel[i]->rx_ring)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500269 break;
270
Lendacky, Thomas18f9f0a2017-06-28 13:42:51 -0500271 XGMAC_DMA_IOWRITE_BITS(pdata->channel[i], DMA_CH_RIWT, RWT,
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500272 pdata->rx_riwt);
273 }
274
275 return 0;
276}
277
278static int xgbe_config_tx_coalesce(struct xgbe_prv_data *pdata)
279{
280 return 0;
281}
282
283static void xgbe_config_rx_buffer_size(struct xgbe_prv_data *pdata)
284{
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500285 unsigned int i;
286
Lendacky, Thomas18f9f0a2017-06-28 13:42:51 -0500287 for (i = 0; i < pdata->channel_count; i++) {
288 if (!pdata->channel[i]->rx_ring)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500289 break;
290
Lendacky, Thomas18f9f0a2017-06-28 13:42:51 -0500291 XGMAC_DMA_IOWRITE_BITS(pdata->channel[i], DMA_CH_RCR, RBSZ,
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500292 pdata->rx_buf_size);
293 }
294}
295
296static void xgbe_config_tso_mode(struct xgbe_prv_data *pdata)
297{
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500298 unsigned int i;
299
Lendacky, Thomas18f9f0a2017-06-28 13:42:51 -0500300 for (i = 0; i < pdata->channel_count; i++) {
301 if (!pdata->channel[i]->tx_ring)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500302 break;
303
Lendacky, Thomas18f9f0a2017-06-28 13:42:51 -0500304 XGMAC_DMA_IOWRITE_BITS(pdata->channel[i], DMA_CH_TCR, TSE, 1);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500305 }
306}
307
Lendacky, Thomas174fd252014-11-04 16:06:50 -0600308static void xgbe_config_sph_mode(struct xgbe_prv_data *pdata)
309{
Lendacky, Thomas174fd252014-11-04 16:06:50 -0600310 unsigned int i;
311
Lendacky, Thomas18f9f0a2017-06-28 13:42:51 -0500312 for (i = 0; i < pdata->channel_count; i++) {
313 if (!pdata->channel[i]->rx_ring)
Lendacky, Thomas174fd252014-11-04 16:06:50 -0600314 break;
315
Lendacky, Thomas18f9f0a2017-06-28 13:42:51 -0500316 XGMAC_DMA_IOWRITE_BITS(pdata->channel[i], DMA_CH_CR, SPH, 1);
Lendacky, Thomas174fd252014-11-04 16:06:50 -0600317 }
318
319 XGMAC_IOWRITE_BITS(pdata, MAC_RCR, HDSMS, XGBE_SPH_HDSMS_SIZE);
320}
321
Lendacky, Thomas5b9dfe22014-11-04 16:07:02 -0600322static int xgbe_write_rss_reg(struct xgbe_prv_data *pdata, unsigned int type,
323 unsigned int index, unsigned int val)
324{
325 unsigned int wait;
326 int ret = 0;
327
328 mutex_lock(&pdata->rss_mutex);
329
330 if (XGMAC_IOREAD_BITS(pdata, MAC_RSSAR, OB)) {
331 ret = -EBUSY;
332 goto unlock;
333 }
334
335 XGMAC_IOWRITE(pdata, MAC_RSSDR, val);
336
337 XGMAC_IOWRITE_BITS(pdata, MAC_RSSAR, RSSIA, index);
338 XGMAC_IOWRITE_BITS(pdata, MAC_RSSAR, ADDRT, type);
339 XGMAC_IOWRITE_BITS(pdata, MAC_RSSAR, CT, 0);
340 XGMAC_IOWRITE_BITS(pdata, MAC_RSSAR, OB, 1);
341
342 wait = 1000;
343 while (wait--) {
344 if (!XGMAC_IOREAD_BITS(pdata, MAC_RSSAR, OB))
345 goto unlock;
346
347 usleep_range(1000, 1500);
348 }
349
350 ret = -EBUSY;
351
352unlock:
353 mutex_unlock(&pdata->rss_mutex);
354
355 return ret;
356}
357
358static int xgbe_write_rss_hash_key(struct xgbe_prv_data *pdata)
359{
360 unsigned int key_regs = sizeof(pdata->rss_key) / sizeof(u32);
361 unsigned int *key = (unsigned int *)&pdata->rss_key;
362 int ret;
363
364 while (key_regs--) {
365 ret = xgbe_write_rss_reg(pdata, XGBE_RSS_HASH_KEY_TYPE,
366 key_regs, *key++);
367 if (ret)
368 return ret;
369 }
370
371 return 0;
372}
373
374static int xgbe_write_rss_lookup_table(struct xgbe_prv_data *pdata)
375{
376 unsigned int i;
377 int ret;
378
379 for (i = 0; i < ARRAY_SIZE(pdata->rss_table); i++) {
380 ret = xgbe_write_rss_reg(pdata,
381 XGBE_RSS_LOOKUP_TABLE_TYPE, i,
382 pdata->rss_table[i]);
383 if (ret)
384 return ret;
385 }
386
387 return 0;
388}
389
Lendacky, Thomasf6ac8622014-11-04 16:07:23 -0600390static int xgbe_set_rss_hash_key(struct xgbe_prv_data *pdata, const u8 *key)
391{
392 memcpy(pdata->rss_key, key, sizeof(pdata->rss_key));
393
394 return xgbe_write_rss_hash_key(pdata);
395}
396
397static int xgbe_set_rss_lookup_table(struct xgbe_prv_data *pdata,
398 const u32 *table)
399{
400 unsigned int i;
401
402 for (i = 0; i < ARRAY_SIZE(pdata->rss_table); i++)
403 XGMAC_SET_BITS(pdata->rss_table[i], MAC_RSSDR, DMCH, table[i]);
404
405 return xgbe_write_rss_lookup_table(pdata);
406}
407
Lendacky, Thomas5b9dfe22014-11-04 16:07:02 -0600408static int xgbe_enable_rss(struct xgbe_prv_data *pdata)
409{
410 int ret;
411
412 if (!pdata->hw_feat.rss)
413 return -EOPNOTSUPP;
414
415 /* Program the hash key */
416 ret = xgbe_write_rss_hash_key(pdata);
417 if (ret)
418 return ret;
419
420 /* Program the lookup table */
421 ret = xgbe_write_rss_lookup_table(pdata);
422 if (ret)
423 return ret;
424
425 /* Set the RSS options */
426 XGMAC_IOWRITE(pdata, MAC_RSSCR, pdata->rss_options);
427
428 /* Enable RSS */
429 XGMAC_IOWRITE_BITS(pdata, MAC_RSSCR, RSSE, 1);
430
431 return 0;
432}
433
434static int xgbe_disable_rss(struct xgbe_prv_data *pdata)
435{
436 if (!pdata->hw_feat.rss)
437 return -EOPNOTSUPP;
438
439 XGMAC_IOWRITE_BITS(pdata, MAC_RSSCR, RSSE, 0);
440
441 return 0;
442}
443
444static void xgbe_config_rss(struct xgbe_prv_data *pdata)
445{
446 int ret;
447
448 if (!pdata->hw_feat.rss)
449 return;
450
451 if (pdata->netdev->features & NETIF_F_RXHASH)
452 ret = xgbe_enable_rss(pdata);
453 else
454 ret = xgbe_disable_rss(pdata);
455
456 if (ret)
457 netdev_err(pdata->netdev,
458 "error configuring RSS, RSS disabled\n");
459}
460
Lendacky, Thomas43e0dcf2016-11-03 13:18:16 -0500461static bool xgbe_is_pfc_queue(struct xgbe_prv_data *pdata,
462 unsigned int queue)
463{
464 unsigned int prio, tc;
465
466 for (prio = 0; prio < IEEE_8021QAZ_MAX_TCS; prio++) {
467 /* Does this queue handle the priority? */
468 if (pdata->prio2q_map[prio] != queue)
469 continue;
470
471 /* Get the Traffic Class for this priority */
472 tc = pdata->ets->prio_tc[prio];
473
474 /* Check if PFC is enabled for this traffic class */
475 if (pdata->pfc->pfc_en & (1 << tc))
476 return true;
477 }
478
479 return false;
480}
481
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500482static int xgbe_disable_tx_flow_control(struct xgbe_prv_data *pdata)
483{
484 unsigned int max_q_count, q_count;
485 unsigned int reg, reg_val;
486 unsigned int i;
487
488 /* Clear MTL flow control */
Lendacky, Thomas853eb162014-07-29 08:57:31 -0500489 for (i = 0; i < pdata->rx_q_count; i++)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500490 XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_Q_RQOMR, EHFC, 0);
491
492 /* Clear MAC flow control */
493 max_q_count = XGMAC_MAX_FLOW_CONTROL_QUEUES;
Lendacky, Thomas9fc69af2014-08-29 13:17:08 -0500494 q_count = min_t(unsigned int, pdata->tx_q_count, max_q_count);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500495 reg = MAC_Q0TFCR;
496 for (i = 0; i < q_count; i++) {
497 reg_val = XGMAC_IOREAD(pdata, reg);
498 XGMAC_SET_BITS(reg_val, MAC_Q0TFCR, TFE, 0);
499 XGMAC_IOWRITE(pdata, reg, reg_val);
500
501 reg += MAC_QTFCR_INC;
502 }
503
504 return 0;
505}
506
507static int xgbe_enable_tx_flow_control(struct xgbe_prv_data *pdata)
508{
Lendacky, Thomas8dba2a22016-02-17 11:48:48 -0600509 struct ieee_pfc *pfc = pdata->pfc;
510 struct ieee_ets *ets = pdata->ets;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500511 unsigned int max_q_count, q_count;
512 unsigned int reg, reg_val;
513 unsigned int i;
514
515 /* Set MTL flow control */
Lendacky, Thomas8dba2a22016-02-17 11:48:48 -0600516 for (i = 0; i < pdata->rx_q_count; i++) {
517 unsigned int ehfc = 0;
518
Lendacky, Thomas43e0dcf2016-11-03 13:18:16 -0500519 if (pdata->rx_rfd[i]) {
520 /* Flow control thresholds are established */
521 if (pfc && ets) {
522 if (xgbe_is_pfc_queue(pdata, i))
Lendacky, Thomas8dba2a22016-02-17 11:48:48 -0600523 ehfc = 1;
Lendacky, Thomas43e0dcf2016-11-03 13:18:16 -0500524 } else {
525 ehfc = 1;
Lendacky, Thomas8dba2a22016-02-17 11:48:48 -0600526 }
Lendacky, Thomas8dba2a22016-02-17 11:48:48 -0600527 }
528
529 XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_Q_RQOMR, EHFC, ehfc);
530
531 netif_dbg(pdata, drv, pdata->netdev,
532 "flow control %s for RXq%u\n",
533 ehfc ? "enabled" : "disabled", i);
534 }
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500535
536 /* Set MAC flow control */
537 max_q_count = XGMAC_MAX_FLOW_CONTROL_QUEUES;
Lendacky, Thomas9fc69af2014-08-29 13:17:08 -0500538 q_count = min_t(unsigned int, pdata->tx_q_count, max_q_count);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500539 reg = MAC_Q0TFCR;
540 for (i = 0; i < q_count; i++) {
541 reg_val = XGMAC_IOREAD(pdata, reg);
542
543 /* Enable transmit flow control */
544 XGMAC_SET_BITS(reg_val, MAC_Q0TFCR, TFE, 1);
545 /* Set pause time */
546 XGMAC_SET_BITS(reg_val, MAC_Q0TFCR, PT, 0xffff);
547
548 XGMAC_IOWRITE(pdata, reg, reg_val);
549
550 reg += MAC_QTFCR_INC;
551 }
552
553 return 0;
554}
555
556static int xgbe_disable_rx_flow_control(struct xgbe_prv_data *pdata)
557{
558 XGMAC_IOWRITE_BITS(pdata, MAC_RFCR, RFE, 0);
559
560 return 0;
561}
562
563static int xgbe_enable_rx_flow_control(struct xgbe_prv_data *pdata)
564{
565 XGMAC_IOWRITE_BITS(pdata, MAC_RFCR, RFE, 1);
566
567 return 0;
568}
569
570static int xgbe_config_tx_flow_control(struct xgbe_prv_data *pdata)
571{
Lendacky, Thomasfca2d992014-07-29 08:57:55 -0500572 struct ieee_pfc *pfc = pdata->pfc;
573
574 if (pdata->tx_pause || (pfc && pfc->pfc_en))
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500575 xgbe_enable_tx_flow_control(pdata);
576 else
577 xgbe_disable_tx_flow_control(pdata);
578
579 return 0;
580}
581
582static int xgbe_config_rx_flow_control(struct xgbe_prv_data *pdata)
583{
Lendacky, Thomasfca2d992014-07-29 08:57:55 -0500584 struct ieee_pfc *pfc = pdata->pfc;
585
586 if (pdata->rx_pause || (pfc && pfc->pfc_en))
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500587 xgbe_enable_rx_flow_control(pdata);
588 else
589 xgbe_disable_rx_flow_control(pdata);
590
591 return 0;
592}
593
594static void xgbe_config_flow_control(struct xgbe_prv_data *pdata)
595{
Lendacky, Thomasfca2d992014-07-29 08:57:55 -0500596 struct ieee_pfc *pfc = pdata->pfc;
597
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500598 xgbe_config_tx_flow_control(pdata);
599 xgbe_config_rx_flow_control(pdata);
Lendacky, Thomasfca2d992014-07-29 08:57:55 -0500600
601 XGMAC_IOWRITE_BITS(pdata, MAC_RFCR, PFCE,
602 (pfc && pfc->pfc_en) ? 1 : 0);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500603}
604
605static void xgbe_enable_dma_interrupts(struct xgbe_prv_data *pdata)
606{
607 struct xgbe_channel *channel;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500608 unsigned int i;
609
Lendacky, Thomas4c70dd82016-11-10 17:10:17 -0600610 /* Set the interrupt mode if supported */
611 if (pdata->channel_irq_mode)
612 XGMAC_IOWRITE_BITS(pdata, DMA_MR, INTM,
613 pdata->channel_irq_mode);
614
Lendacky, Thomas18f9f0a2017-06-28 13:42:51 -0500615 for (i = 0; i < pdata->channel_count; i++) {
616 channel = pdata->channel[i];
617
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500618 /* Clear all the interrupts which are set */
Lendacky, Thomascaa575af2017-08-18 09:03:17 -0500619 XGMAC_DMA_IOWRITE(channel, DMA_CH_SR,
620 XGMAC_DMA_IOREAD(channel, DMA_CH_SR));
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500621
622 /* Clear all interrupt enable bits */
Lendacky, Thomascaa575af2017-08-18 09:03:17 -0500623 channel->curr_ier = 0;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500624
625 /* Enable following interrupts
626 * NIE - Normal Interrupt Summary Enable
627 * AIE - Abnormal Interrupt Summary Enable
628 * FBEE - Fatal Bus Error Enable
629 */
Lendacky, Thomascaa575af2017-08-18 09:03:17 -0500630 XGMAC_SET_BITS(channel->curr_ier, DMA_CH_IER, NIE, 1);
631 XGMAC_SET_BITS(channel->curr_ier, DMA_CH_IER, AIE, 1);
632 XGMAC_SET_BITS(channel->curr_ier, DMA_CH_IER, FBEE, 1);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500633
634 if (channel->tx_ring) {
635 /* Enable the following Tx interrupts
Lendacky, Thomas9227dc52014-11-04 16:06:56 -0600636 * TIE - Transmit Interrupt Enable (unless using
Lendacky, Thomas4c70dd82016-11-10 17:10:17 -0600637 * per channel interrupts in edge triggered
638 * mode)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500639 */
Lendacky, Thomas4c70dd82016-11-10 17:10:17 -0600640 if (!pdata->per_channel_irq || pdata->channel_irq_mode)
Lendacky, Thomascaa575af2017-08-18 09:03:17 -0500641 XGMAC_SET_BITS(channel->curr_ier,
642 DMA_CH_IER, TIE, 1);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500643 }
644 if (channel->rx_ring) {
645 /* Enable following Rx interrupts
646 * RBUE - Receive Buffer Unavailable Enable
Lendacky, Thomas9227dc52014-11-04 16:06:56 -0600647 * RIE - Receive Interrupt Enable (unless using
Lendacky, Thomas4c70dd82016-11-10 17:10:17 -0600648 * per channel interrupts in edge triggered
649 * mode)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500650 */
Lendacky, Thomascaa575af2017-08-18 09:03:17 -0500651 XGMAC_SET_BITS(channel->curr_ier, DMA_CH_IER, RBUE, 1);
Lendacky, Thomas4c70dd82016-11-10 17:10:17 -0600652 if (!pdata->per_channel_irq || pdata->channel_irq_mode)
Lendacky, Thomascaa575af2017-08-18 09:03:17 -0500653 XGMAC_SET_BITS(channel->curr_ier,
654 DMA_CH_IER, RIE, 1);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500655 }
656
Lendacky, Thomascaa575af2017-08-18 09:03:17 -0500657 XGMAC_DMA_IOWRITE(channel, DMA_CH_IER, channel->curr_ier);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500658 }
659}
660
661static void xgbe_enable_mtl_interrupts(struct xgbe_prv_data *pdata)
662{
663 unsigned int mtl_q_isr;
664 unsigned int q_count, i;
665
666 q_count = max(pdata->hw_feat.tx_q_cnt, pdata->hw_feat.rx_q_cnt);
667 for (i = 0; i < q_count; i++) {
668 /* Clear all the interrupts which are set */
669 mtl_q_isr = XGMAC_MTL_IOREAD(pdata, i, MTL_Q_ISR);
670 XGMAC_MTL_IOWRITE(pdata, i, MTL_Q_ISR, mtl_q_isr);
671
672 /* No MTL interrupts to be enabled */
Lendacky, Thomas91f87342014-07-02 13:04:34 -0500673 XGMAC_MTL_IOWRITE(pdata, i, MTL_Q_IER, 0);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500674 }
675}
676
677static void xgbe_enable_mac_interrupts(struct xgbe_prv_data *pdata)
678{
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -0500679 unsigned int mac_ier = 0;
680
681 /* Enable Timestamp interrupt */
682 XGMAC_SET_BITS(mac_ier, MAC_IER, TSIE, 1);
683
684 XGMAC_IOWRITE(pdata, MAC_IER, mac_ier);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500685
686 /* Enable all counter interrupts */
Lendacky, Thomasa3ba7c92014-09-05 18:02:36 -0500687 XGMAC_IOWRITE_BITS(pdata, MMC_RIER, ALL_INTERRUPTS, 0xffffffff);
688 XGMAC_IOWRITE_BITS(pdata, MMC_TIER, ALL_INTERRUPTS, 0xffffffff);
Lendacky, Thomas732f2ab2016-11-10 17:11:14 -0600689
690 /* Enable MDIO single command completion interrupt */
691 XGMAC_IOWRITE_BITS(pdata, MAC_MDIOIER, SNGLCOMPIE, 1);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500692}
693
Lendacky, Thomase78332b2016-11-10 17:10:26 -0600694static void xgbe_enable_ecc_interrupts(struct xgbe_prv_data *pdata)
695{
696 unsigned int ecc_isr, ecc_ier = 0;
697
698 if (!pdata->vdata->ecc_support)
699 return;
700
701 /* Clear all the interrupts which are set */
702 ecc_isr = XP_IOREAD(pdata, XP_ECC_ISR);
703 XP_IOWRITE(pdata, XP_ECC_ISR, ecc_isr);
704
705 /* Enable ECC interrupts */
706 XP_SET_BITS(ecc_ier, XP_ECC_IER, TX_DED, 1);
707 XP_SET_BITS(ecc_ier, XP_ECC_IER, TX_SEC, 1);
708 XP_SET_BITS(ecc_ier, XP_ECC_IER, RX_DED, 1);
709 XP_SET_BITS(ecc_ier, XP_ECC_IER, RX_SEC, 1);
710 XP_SET_BITS(ecc_ier, XP_ECC_IER, DESC_DED, 1);
711 XP_SET_BITS(ecc_ier, XP_ECC_IER, DESC_SEC, 1);
712
713 XP_IOWRITE(pdata, XP_ECC_IER, ecc_ier);
714}
715
716static void xgbe_disable_ecc_ded(struct xgbe_prv_data *pdata)
717{
718 unsigned int ecc_ier;
719
720 ecc_ier = XP_IOREAD(pdata, XP_ECC_IER);
721
722 /* Disable ECC DED interrupts */
723 XP_SET_BITS(ecc_ier, XP_ECC_IER, TX_DED, 0);
724 XP_SET_BITS(ecc_ier, XP_ECC_IER, RX_DED, 0);
725 XP_SET_BITS(ecc_ier, XP_ECC_IER, DESC_DED, 0);
726
727 XP_IOWRITE(pdata, XP_ECC_IER, ecc_ier);
728}
729
730static void xgbe_disable_ecc_sec(struct xgbe_prv_data *pdata,
731 enum xgbe_ecc_sec sec)
732{
733 unsigned int ecc_ier;
734
735 ecc_ier = XP_IOREAD(pdata, XP_ECC_IER);
736
737 /* Disable ECC SEC interrupt */
738 switch (sec) {
739 case XGBE_ECC_SEC_TX:
740 XP_SET_BITS(ecc_ier, XP_ECC_IER, TX_SEC, 0);
741 break;
742 case XGBE_ECC_SEC_RX:
743 XP_SET_BITS(ecc_ier, XP_ECC_IER, RX_SEC, 0);
744 break;
745 case XGBE_ECC_SEC_DESC:
746 XP_SET_BITS(ecc_ier, XP_ECC_IER, DESC_SEC, 0);
747 break;
748 }
749
750 XP_IOWRITE(pdata, XP_ECC_IER, ecc_ier);
751}
752
Lendacky, Thomase57f7a32016-11-03 13:18:27 -0500753static int xgbe_set_speed(struct xgbe_prv_data *pdata, int speed)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500754{
Lendacky, Thomase57f7a32016-11-03 13:18:27 -0500755 unsigned int ss;
Lendacky, Thomasc3152d42015-01-16 12:47:00 -0600756
Lendacky, Thomase57f7a32016-11-03 13:18:27 -0500757 switch (speed) {
758 case SPEED_1000:
759 ss = 0x03;
760 break;
761 case SPEED_2500:
762 ss = 0x02;
763 break;
764 case SPEED_10000:
765 ss = 0x00;
766 break;
767 default:
768 return -EINVAL;
769 }
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500770
Lendacky, Thomase57f7a32016-11-03 13:18:27 -0500771 if (XGMAC_IOREAD_BITS(pdata, MAC_TCR, SS) != ss)
772 XGMAC_IOWRITE_BITS(pdata, MAC_TCR, SS, ss);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500773
774 return 0;
775}
776
Lendacky, Thomasb4eee842016-02-17 11:48:08 -0600777static int xgbe_enable_rx_vlan_stripping(struct xgbe_prv_data *pdata)
778{
779 /* Put the VLAN tag in the Rx descriptor */
780 XGMAC_IOWRITE_BITS(pdata, MAC_VLANTR, EVLRXS, 1);
781
782 /* Don't check the VLAN type */
783 XGMAC_IOWRITE_BITS(pdata, MAC_VLANTR, DOVLTC, 1);
784
785 /* Check only C-TAG (0x8100) packets */
786 XGMAC_IOWRITE_BITS(pdata, MAC_VLANTR, ERSVLM, 0);
787
788 /* Don't consider an S-TAG (0x88A8) packet as a VLAN packet */
789 XGMAC_IOWRITE_BITS(pdata, MAC_VLANTR, ESVL, 0);
790
791 /* Enable VLAN tag stripping */
792 XGMAC_IOWRITE_BITS(pdata, MAC_VLANTR, EVLS, 0x3);
793
794 return 0;
795}
796
797static int xgbe_disable_rx_vlan_stripping(struct xgbe_prv_data *pdata)
798{
799 XGMAC_IOWRITE_BITS(pdata, MAC_VLANTR, EVLS, 0);
800
801 return 0;
802}
803
804static int xgbe_enable_rx_vlan_filtering(struct xgbe_prv_data *pdata)
805{
806 /* Enable VLAN filtering */
807 XGMAC_IOWRITE_BITS(pdata, MAC_PFR, VTFE, 1);
808
809 /* Enable VLAN Hash Table filtering */
810 XGMAC_IOWRITE_BITS(pdata, MAC_VLANTR, VTHM, 1);
811
812 /* Disable VLAN tag inverse matching */
813 XGMAC_IOWRITE_BITS(pdata, MAC_VLANTR, VTIM, 0);
814
815 /* Only filter on the lower 12-bits of the VLAN tag */
816 XGMAC_IOWRITE_BITS(pdata, MAC_VLANTR, ETV, 1);
817
818 /* In order for the VLAN Hash Table filtering to be effective,
819 * the VLAN tag identifier in the VLAN Tag Register must not
820 * be zero. Set the VLAN tag identifier to "1" to enable the
821 * VLAN Hash Table filtering. This implies that a VLAN tag of
822 * 1 will always pass filtering.
823 */
824 XGMAC_IOWRITE_BITS(pdata, MAC_VLANTR, VL, 1);
825
826 return 0;
827}
828
829static int xgbe_disable_rx_vlan_filtering(struct xgbe_prv_data *pdata)
830{
831 /* Disable VLAN filtering */
832 XGMAC_IOWRITE_BITS(pdata, MAC_PFR, VTFE, 0);
833
834 return 0;
835}
836
837static u32 xgbe_vid_crc32_le(__le16 vid_le)
838{
839 u32 poly = 0xedb88320; /* CRCPOLY_LE */
840 u32 crc = ~0;
841 u32 temp = 0;
842 unsigned char *data = (unsigned char *)&vid_le;
843 unsigned char data_byte = 0;
844 int i, bits;
845
846 bits = get_bitmask_order(VLAN_VID_MASK);
847 for (i = 0; i < bits; i++) {
848 if ((i % 8) == 0)
849 data_byte = data[i / 8];
850
851 temp = ((crc & 1) ^ data_byte) & 1;
852 crc >>= 1;
853 data_byte >>= 1;
854
855 if (temp)
856 crc ^= poly;
857 }
858
859 return crc;
860}
861
862static int xgbe_update_vlan_hash_table(struct xgbe_prv_data *pdata)
863{
864 u32 crc;
865 u16 vid;
866 __le16 vid_le;
867 u16 vlan_hash_table = 0;
868
869 /* Generate the VLAN Hash Table value */
870 for_each_set_bit(vid, pdata->active_vlans, VLAN_N_VID) {
871 /* Get the CRC32 value of the VLAN ID */
872 vid_le = cpu_to_le16(vid);
873 crc = bitrev32(~xgbe_vid_crc32_le(vid_le)) >> 28;
874
875 vlan_hash_table |= (1 << crc);
876 }
877
878 /* Set the VLAN Hash Table filtering register */
879 XGMAC_IOWRITE_BITS(pdata, MAC_VLANHTR, VLHT, vlan_hash_table);
880
881 return 0;
882}
883
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500884static int xgbe_set_promiscuous_mode(struct xgbe_prv_data *pdata,
885 unsigned int enable)
886{
887 unsigned int val = enable ? 1 : 0;
888
889 if (XGMAC_IOREAD_BITS(pdata, MAC_PFR, PR) == val)
890 return 0;
891
Lendacky, Thomas34bf65d2015-05-14 11:44:03 -0500892 netif_dbg(pdata, drv, pdata->netdev, "%s promiscuous mode\n",
893 enable ? "entering" : "leaving");
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500894 XGMAC_IOWRITE_BITS(pdata, MAC_PFR, PR, val);
895
Lendacky, Thomasb4eee842016-02-17 11:48:08 -0600896 /* Hardware will still perform VLAN filtering in promiscuous mode */
897 if (enable) {
898 xgbe_disable_rx_vlan_filtering(pdata);
899 } else {
900 if (pdata->netdev->features & NETIF_F_HW_VLAN_CTAG_FILTER)
901 xgbe_enable_rx_vlan_filtering(pdata);
902 }
903
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500904 return 0;
905}
906
907static int xgbe_set_all_multicast_mode(struct xgbe_prv_data *pdata,
908 unsigned int enable)
909{
910 unsigned int val = enable ? 1 : 0;
911
912 if (XGMAC_IOREAD_BITS(pdata, MAC_PFR, PM) == val)
913 return 0;
914
Lendacky, Thomas34bf65d2015-05-14 11:44:03 -0500915 netif_dbg(pdata, drv, pdata->netdev, "%s allmulti mode\n",
916 enable ? "entering" : "leaving");
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500917 XGMAC_IOWRITE_BITS(pdata, MAC_PFR, PM, val);
918
919 return 0;
920}
921
Lendacky, Thomasb85e4d82014-06-24 16:19:29 -0500922static void xgbe_set_mac_reg(struct xgbe_prv_data *pdata,
923 struct netdev_hw_addr *ha, unsigned int *mac_reg)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500924{
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500925 unsigned int mac_addr_hi, mac_addr_lo;
926 u8 *mac_addr;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500927
Lendacky, Thomasb85e4d82014-06-24 16:19:29 -0500928 mac_addr_lo = 0;
929 mac_addr_hi = 0;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500930
Lendacky, Thomasb85e4d82014-06-24 16:19:29 -0500931 if (ha) {
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500932 mac_addr = (u8 *)&mac_addr_lo;
933 mac_addr[0] = ha->addr[0];
934 mac_addr[1] = ha->addr[1];
935 mac_addr[2] = ha->addr[2];
936 mac_addr[3] = ha->addr[3];
937 mac_addr = (u8 *)&mac_addr_hi;
938 mac_addr[0] = ha->addr[4];
939 mac_addr[1] = ha->addr[5];
940
Lendacky, Thomas34bf65d2015-05-14 11:44:03 -0500941 netif_dbg(pdata, drv, pdata->netdev,
942 "adding mac address %pM at %#x\n",
943 ha->addr, *mac_reg);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500944
945 XGMAC_SET_BITS(mac_addr_hi, MAC_MACA1HR, AE, 1);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500946 }
947
Lendacky, Thomasb85e4d82014-06-24 16:19:29 -0500948 XGMAC_IOWRITE(pdata, *mac_reg, mac_addr_hi);
949 *mac_reg += MAC_MACA_INC;
950 XGMAC_IOWRITE(pdata, *mac_reg, mac_addr_lo);
951 *mac_reg += MAC_MACA_INC;
952}
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500953
Lendacky, Thomasb85e4d82014-06-24 16:19:29 -0500954static void xgbe_set_mac_addn_addrs(struct xgbe_prv_data *pdata)
955{
956 struct net_device *netdev = pdata->netdev;
957 struct netdev_hw_addr *ha;
958 unsigned int mac_reg;
959 unsigned int addn_macs;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500960
Lendacky, Thomasb85e4d82014-06-24 16:19:29 -0500961 mac_reg = MAC_MACA1HR;
962 addn_macs = pdata->hw_feat.addn_mac;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500963
Lendacky, Thomasb85e4d82014-06-24 16:19:29 -0500964 if (netdev_uc_count(netdev) > addn_macs) {
965 xgbe_set_promiscuous_mode(pdata, 1);
966 } else {
967 netdev_for_each_uc_addr(ha, netdev) {
968 xgbe_set_mac_reg(pdata, ha, &mac_reg);
969 addn_macs--;
970 }
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500971
Lendacky, Thomasb85e4d82014-06-24 16:19:29 -0500972 if (netdev_mc_count(netdev) > addn_macs) {
973 xgbe_set_all_multicast_mode(pdata, 1);
974 } else {
975 netdev_for_each_mc_addr(ha, netdev) {
976 xgbe_set_mac_reg(pdata, ha, &mac_reg);
977 addn_macs--;
978 }
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500979 }
980 }
981
982 /* Clear remaining additional MAC address entries */
Lendacky, Thomasb85e4d82014-06-24 16:19:29 -0500983 while (addn_macs--)
984 xgbe_set_mac_reg(pdata, NULL, &mac_reg);
985}
986
987static void xgbe_set_mac_hash_table(struct xgbe_prv_data *pdata)
988{
989 struct net_device *netdev = pdata->netdev;
990 struct netdev_hw_addr *ha;
991 unsigned int hash_reg;
992 unsigned int hash_table_shift, hash_table_count;
993 u32 hash_table[XGBE_MAC_HASH_TABLE_SIZE];
994 u32 crc;
995 unsigned int i;
996
997 hash_table_shift = 26 - (pdata->hw_feat.hash_table_size >> 7);
998 hash_table_count = pdata->hw_feat.hash_table_size / 32;
999 memset(hash_table, 0, sizeof(hash_table));
1000
1001 /* Build the MAC Hash Table register values */
1002 netdev_for_each_uc_addr(ha, netdev) {
1003 crc = bitrev32(~crc32_le(~0, ha->addr, ETH_ALEN));
1004 crc >>= hash_table_shift;
1005 hash_table[crc >> 5] |= (1 << (crc & 0x1f));
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001006 }
1007
Lendacky, Thomasb85e4d82014-06-24 16:19:29 -05001008 netdev_for_each_mc_addr(ha, netdev) {
1009 crc = bitrev32(~crc32_le(~0, ha->addr, ETH_ALEN));
1010 crc >>= hash_table_shift;
1011 hash_table[crc >> 5] |= (1 << (crc & 0x1f));
1012 }
1013
1014 /* Set the MAC Hash Table registers */
1015 hash_reg = MAC_HTR0;
1016 for (i = 0; i < hash_table_count; i++) {
1017 XGMAC_IOWRITE(pdata, hash_reg, hash_table[i]);
1018 hash_reg += MAC_HTR_INC;
1019 }
1020}
1021
1022static int xgbe_add_mac_addresses(struct xgbe_prv_data *pdata)
1023{
1024 if (pdata->hw_feat.hash_table_size)
1025 xgbe_set_mac_hash_table(pdata);
1026 else
1027 xgbe_set_mac_addn_addrs(pdata);
1028
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001029 return 0;
1030}
1031
1032static int xgbe_set_mac_address(struct xgbe_prv_data *pdata, u8 *addr)
1033{
1034 unsigned int mac_addr_hi, mac_addr_lo;
1035
1036 mac_addr_hi = (addr[5] << 8) | (addr[4] << 0);
1037 mac_addr_lo = (addr[3] << 24) | (addr[2] << 16) |
1038 (addr[1] << 8) | (addr[0] << 0);
1039
1040 XGMAC_IOWRITE(pdata, MAC_MACA0HR, mac_addr_hi);
1041 XGMAC_IOWRITE(pdata, MAC_MACA0LR, mac_addr_lo);
1042
1043 return 0;
1044}
1045
Lendacky, Thomasb8763822015-04-09 12:11:57 -05001046static int xgbe_config_rx_mode(struct xgbe_prv_data *pdata)
1047{
1048 struct net_device *netdev = pdata->netdev;
1049 unsigned int pr_mode, am_mode;
1050
1051 pr_mode = ((netdev->flags & IFF_PROMISC) != 0);
1052 am_mode = ((netdev->flags & IFF_ALLMULTI) != 0);
1053
1054 xgbe_set_promiscuous_mode(pdata, pr_mode);
1055 xgbe_set_all_multicast_mode(pdata, am_mode);
1056
1057 xgbe_add_mac_addresses(pdata);
1058
1059 return 0;
1060}
1061
Lendacky, Thomas732f2ab2016-11-10 17:11:14 -06001062static int xgbe_clr_gpio(struct xgbe_prv_data *pdata, unsigned int gpio)
1063{
1064 unsigned int reg;
1065
Lendacky, Thomas1c1f6192016-11-15 16:11:15 -06001066 if (gpio > 15)
Lendacky, Thomas732f2ab2016-11-10 17:11:14 -06001067 return -EINVAL;
1068
1069 reg = XGMAC_IOREAD(pdata, MAC_GPIOSR);
1070
1071 reg &= ~(1 << (gpio + 16));
1072 XGMAC_IOWRITE(pdata, MAC_GPIOSR, reg);
1073
1074 return 0;
1075}
1076
1077static int xgbe_set_gpio(struct xgbe_prv_data *pdata, unsigned int gpio)
1078{
1079 unsigned int reg;
1080
Lendacky, Thomas1c1f6192016-11-15 16:11:15 -06001081 if (gpio > 15)
Lendacky, Thomas732f2ab2016-11-10 17:11:14 -06001082 return -EINVAL;
1083
1084 reg = XGMAC_IOREAD(pdata, MAC_GPIOSR);
1085
1086 reg |= (1 << (gpio + 16));
1087 XGMAC_IOWRITE(pdata, MAC_GPIOSR, reg);
1088
1089 return 0;
1090}
1091
Lendacky, Thomasb03a4a62016-11-03 13:18:56 -05001092static int xgbe_read_mmd_regs_v2(struct xgbe_prv_data *pdata, int prtad,
1093 int mmd_reg)
1094{
1095 unsigned long flags;
1096 unsigned int mmd_address, index, offset;
1097 int mmd_data;
1098
1099 if (mmd_reg & MII_ADDR_C45)
1100 mmd_address = mmd_reg & ~MII_ADDR_C45;
1101 else
1102 mmd_address = (pdata->mdio_mmd << 16) | (mmd_reg & 0xffff);
1103
1104 /* The PCS registers are accessed using mmio. The underlying
1105 * management interface uses indirect addressing to access the MMD
1106 * register sets. This requires accessing of the PCS register in two
1107 * phases, an address phase and a data phase.
1108 *
1109 * The mmio interface is based on 16-bit offsets and values. All
1110 * register offsets must therefore be adjusted by left shifting the
1111 * offset 1 bit and reading 16 bits of data.
1112 */
1113 mmd_address <<= 1;
1114 index = mmd_address & ~pdata->xpcs_window_mask;
1115 offset = pdata->xpcs_window + (mmd_address & pdata->xpcs_window_mask);
1116
1117 spin_lock_irqsave(&pdata->xpcs_lock, flags);
Lendacky, Thomas4eccbfc2017-01-20 12:14:03 -06001118 XPCS32_IOWRITE(pdata, pdata->xpcs_window_sel_reg, index);
Lendacky, Thomasb03a4a62016-11-03 13:18:56 -05001119 mmd_data = XPCS16_IOREAD(pdata, offset);
1120 spin_unlock_irqrestore(&pdata->xpcs_lock, flags);
1121
1122 return mmd_data;
1123}
1124
1125static void xgbe_write_mmd_regs_v2(struct xgbe_prv_data *pdata, int prtad,
1126 int mmd_reg, int mmd_data)
1127{
1128 unsigned long flags;
1129 unsigned int mmd_address, index, offset;
1130
1131 if (mmd_reg & MII_ADDR_C45)
1132 mmd_address = mmd_reg & ~MII_ADDR_C45;
1133 else
1134 mmd_address = (pdata->mdio_mmd << 16) | (mmd_reg & 0xffff);
1135
1136 /* The PCS registers are accessed using mmio. The underlying
1137 * management interface uses indirect addressing to access the MMD
1138 * register sets. This requires accessing of the PCS register in two
1139 * phases, an address phase and a data phase.
1140 *
1141 * The mmio interface is based on 16-bit offsets and values. All
1142 * register offsets must therefore be adjusted by left shifting the
1143 * offset 1 bit and writing 16 bits of data.
1144 */
1145 mmd_address <<= 1;
1146 index = mmd_address & ~pdata->xpcs_window_mask;
1147 offset = pdata->xpcs_window + (mmd_address & pdata->xpcs_window_mask);
1148
1149 spin_lock_irqsave(&pdata->xpcs_lock, flags);
Lendacky, Thomas4eccbfc2017-01-20 12:14:03 -06001150 XPCS32_IOWRITE(pdata, pdata->xpcs_window_sel_reg, index);
Lendacky, Thomasb03a4a62016-11-03 13:18:56 -05001151 XPCS16_IOWRITE(pdata, offset, mmd_data);
1152 spin_unlock_irqrestore(&pdata->xpcs_lock, flags);
1153}
1154
1155static int xgbe_read_mmd_regs_v1(struct xgbe_prv_data *pdata, int prtad,
1156 int mmd_reg)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001157{
Lendacky, Thomasced3fca2016-02-17 11:49:28 -06001158 unsigned long flags;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001159 unsigned int mmd_address;
1160 int mmd_data;
1161
1162 if (mmd_reg & MII_ADDR_C45)
1163 mmd_address = mmd_reg & ~MII_ADDR_C45;
1164 else
1165 mmd_address = (pdata->mdio_mmd << 16) | (mmd_reg & 0xffff);
1166
1167 /* The PCS registers are accessed using mmio. The underlying APB3
1168 * management interface uses indirect addressing to access the MMD
1169 * register sets. This requires accessing of the PCS register in two
1170 * phases, an address phase and a data phase.
1171 *
1172 * The mmio interface is based on 32-bit offsets and values. All
1173 * register offsets must therefore be adjusted by left shifting the
1174 * offset 2 bits and reading 32 bits of data.
1175 */
Lendacky, Thomasced3fca2016-02-17 11:49:28 -06001176 spin_lock_irqsave(&pdata->xpcs_lock, flags);
Lendacky, Thomasb03a4a62016-11-03 13:18:56 -05001177 XPCS32_IOWRITE(pdata, PCS_V1_WINDOW_SELECT, mmd_address >> 8);
1178 mmd_data = XPCS32_IOREAD(pdata, (mmd_address & 0xff) << 2);
Lendacky, Thomasced3fca2016-02-17 11:49:28 -06001179 spin_unlock_irqrestore(&pdata->xpcs_lock, flags);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001180
1181 return mmd_data;
1182}
1183
Lendacky, Thomasb03a4a62016-11-03 13:18:56 -05001184static void xgbe_write_mmd_regs_v1(struct xgbe_prv_data *pdata, int prtad,
1185 int mmd_reg, int mmd_data)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001186{
1187 unsigned int mmd_address;
Lendacky, Thomasced3fca2016-02-17 11:49:28 -06001188 unsigned long flags;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001189
1190 if (mmd_reg & MII_ADDR_C45)
1191 mmd_address = mmd_reg & ~MII_ADDR_C45;
1192 else
1193 mmd_address = (pdata->mdio_mmd << 16) | (mmd_reg & 0xffff);
1194
1195 /* The PCS registers are accessed using mmio. The underlying APB3
1196 * management interface uses indirect addressing to access the MMD
1197 * register sets. This requires accessing of the PCS register in two
1198 * phases, an address phase and a data phase.
1199 *
1200 * The mmio interface is based on 32-bit offsets and values. All
1201 * register offsets must therefore be adjusted by left shifting the
Lendacky, Thomasb03a4a62016-11-03 13:18:56 -05001202 * offset 2 bits and writing 32 bits of data.
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001203 */
Lendacky, Thomasced3fca2016-02-17 11:49:28 -06001204 spin_lock_irqsave(&pdata->xpcs_lock, flags);
Lendacky, Thomasb03a4a62016-11-03 13:18:56 -05001205 XPCS32_IOWRITE(pdata, PCS_V1_WINDOW_SELECT, mmd_address >> 8);
1206 XPCS32_IOWRITE(pdata, (mmd_address & 0xff) << 2, mmd_data);
Lendacky, Thomasced3fca2016-02-17 11:49:28 -06001207 spin_unlock_irqrestore(&pdata->xpcs_lock, flags);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001208}
1209
Lendacky, Thomasb03a4a62016-11-03 13:18:56 -05001210static int xgbe_read_mmd_regs(struct xgbe_prv_data *pdata, int prtad,
1211 int mmd_reg)
1212{
1213 switch (pdata->vdata->xpcs_access) {
1214 case XGBE_XPCS_ACCESS_V1:
1215 return xgbe_read_mmd_regs_v1(pdata, prtad, mmd_reg);
1216
1217 case XGBE_XPCS_ACCESS_V2:
1218 default:
1219 return xgbe_read_mmd_regs_v2(pdata, prtad, mmd_reg);
1220 }
1221}
1222
1223static void xgbe_write_mmd_regs(struct xgbe_prv_data *pdata, int prtad,
1224 int mmd_reg, int mmd_data)
1225{
1226 switch (pdata->vdata->xpcs_access) {
1227 case XGBE_XPCS_ACCESS_V1:
1228 return xgbe_write_mmd_regs_v1(pdata, prtad, mmd_reg, mmd_data);
1229
1230 case XGBE_XPCS_ACCESS_V2:
1231 default:
1232 return xgbe_write_mmd_regs_v2(pdata, prtad, mmd_reg, mmd_data);
1233 }
1234}
1235
Lendacky, Thomas732f2ab2016-11-10 17:11:14 -06001236static int xgbe_write_ext_mii_regs(struct xgbe_prv_data *pdata, int addr,
1237 int reg, u16 val)
1238{
1239 unsigned int mdio_sca, mdio_sccd;
1240
1241 reinit_completion(&pdata->mdio_complete);
1242
1243 mdio_sca = 0;
1244 XGMAC_SET_BITS(mdio_sca, MAC_MDIOSCAR, REG, reg);
1245 XGMAC_SET_BITS(mdio_sca, MAC_MDIOSCAR, DA, addr);
1246 XGMAC_IOWRITE(pdata, MAC_MDIOSCAR, mdio_sca);
1247
1248 mdio_sccd = 0;
1249 XGMAC_SET_BITS(mdio_sccd, MAC_MDIOSCCDR, DATA, val);
1250 XGMAC_SET_BITS(mdio_sccd, MAC_MDIOSCCDR, CMD, 1);
1251 XGMAC_SET_BITS(mdio_sccd, MAC_MDIOSCCDR, BUSY, 1);
1252 XGMAC_IOWRITE(pdata, MAC_MDIOSCCDR, mdio_sccd);
1253
1254 if (!wait_for_completion_timeout(&pdata->mdio_complete, HZ)) {
1255 netdev_err(pdata->netdev, "mdio write operation timed out\n");
1256 return -ETIMEDOUT;
1257 }
1258
1259 return 0;
1260}
1261
1262static int xgbe_read_ext_mii_regs(struct xgbe_prv_data *pdata, int addr,
1263 int reg)
1264{
1265 unsigned int mdio_sca, mdio_sccd;
1266
1267 reinit_completion(&pdata->mdio_complete);
1268
1269 mdio_sca = 0;
1270 XGMAC_SET_BITS(mdio_sca, MAC_MDIOSCAR, REG, reg);
1271 XGMAC_SET_BITS(mdio_sca, MAC_MDIOSCAR, DA, addr);
1272 XGMAC_IOWRITE(pdata, MAC_MDIOSCAR, mdio_sca);
1273
1274 mdio_sccd = 0;
1275 XGMAC_SET_BITS(mdio_sccd, MAC_MDIOSCCDR, CMD, 3);
1276 XGMAC_SET_BITS(mdio_sccd, MAC_MDIOSCCDR, BUSY, 1);
1277 XGMAC_IOWRITE(pdata, MAC_MDIOSCCDR, mdio_sccd);
1278
1279 if (!wait_for_completion_timeout(&pdata->mdio_complete, HZ)) {
1280 netdev_err(pdata->netdev, "mdio read operation timed out\n");
1281 return -ETIMEDOUT;
1282 }
1283
1284 return XGMAC_IOREAD_BITS(pdata, MAC_MDIOSCCDR, DATA);
1285}
1286
1287static int xgbe_set_ext_mii_mode(struct xgbe_prv_data *pdata, unsigned int port,
1288 enum xgbe_mdio_mode mode)
1289{
Lendacky, Thomasb42c6762017-02-28 15:03:01 -06001290 unsigned int reg_val = XGMAC_IOREAD(pdata, MAC_MDIOCL22R);
Lendacky, Thomas732f2ab2016-11-10 17:11:14 -06001291
1292 switch (mode) {
1293 case XGBE_MDIO_MODE_CL22:
1294 if (port > XGMAC_MAX_C22_PORT)
1295 return -EINVAL;
1296 reg_val |= (1 << port);
1297 break;
1298 case XGBE_MDIO_MODE_CL45:
1299 break;
1300 default:
1301 return -EINVAL;
1302 }
1303
1304 XGMAC_IOWRITE(pdata, MAC_MDIOCL22R, reg_val);
1305
1306 return 0;
1307}
1308
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001309static int xgbe_tx_complete(struct xgbe_ring_desc *rdesc)
1310{
1311 return !XGMAC_GET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, OWN);
1312}
1313
1314static int xgbe_disable_rx_csum(struct xgbe_prv_data *pdata)
1315{
1316 XGMAC_IOWRITE_BITS(pdata, MAC_RCR, IPC, 0);
1317
1318 return 0;
1319}
1320
1321static int xgbe_enable_rx_csum(struct xgbe_prv_data *pdata)
1322{
1323 XGMAC_IOWRITE_BITS(pdata, MAC_RCR, IPC, 1);
1324
1325 return 0;
1326}
1327
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001328static void xgbe_tx_desc_reset(struct xgbe_ring_data *rdata)
1329{
1330 struct xgbe_ring_desc *rdesc = rdata->rdesc;
1331
1332 /* Reset the Tx descriptor
1333 * Set buffer 1 (lo) address to zero
1334 * Set buffer 1 (hi) address to zero
1335 * Reset all other control bits (IC, TTSE, B2L & B1L)
1336 * Reset all other control bits (OWN, CTXT, FD, LD, CPC, CIC, etc)
1337 */
1338 rdesc->desc0 = 0;
1339 rdesc->desc1 = 0;
1340 rdesc->desc2 = 0;
1341 rdesc->desc3 = 0;
Lendacky, Thomas08dcc472014-11-04 16:06:44 -06001342
1343 /* Make sure ownership is written to the descriptor */
Lendacky, Thomasceb8f6b2015-03-20 11:50:16 -05001344 dma_wmb();
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001345}
1346
1347static void xgbe_tx_desc_init(struct xgbe_channel *channel)
1348{
1349 struct xgbe_ring *ring = channel->tx_ring;
1350 struct xgbe_ring_data *rdata;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001351 int i;
1352 int start_index = ring->cur;
1353
1354 DBGPR("-->tx_desc_init\n");
1355
1356 /* Initialze all descriptors */
1357 for (i = 0; i < ring->rdesc_count; i++) {
Lendacky, Thomasd0a8ba62014-06-24 16:19:06 -05001358 rdata = XGBE_GET_DESC_DATA(ring, i);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001359
Lendacky, Thomas08dcc472014-11-04 16:06:44 -06001360 /* Initialize Tx descriptor */
1361 xgbe_tx_desc_reset(rdata);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001362 }
1363
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001364 /* Update the total number of Tx descriptors */
1365 XGMAC_DMA_IOWRITE(channel, DMA_CH_TDRLR, ring->rdesc_count - 1);
1366
1367 /* Update the starting address of descriptor ring */
Lendacky, Thomasd0a8ba62014-06-24 16:19:06 -05001368 rdata = XGBE_GET_DESC_DATA(ring, start_index);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001369 XGMAC_DMA_IOWRITE(channel, DMA_CH_TDLR_HI,
1370 upper_32_bits(rdata->rdesc_dma));
1371 XGMAC_DMA_IOWRITE(channel, DMA_CH_TDLR_LO,
1372 lower_32_bits(rdata->rdesc_dma));
1373
1374 DBGPR("<--tx_desc_init\n");
1375}
1376
Lendacky, Thomas8dee19e2015-04-09 12:11:51 -05001377static void xgbe_rx_desc_reset(struct xgbe_prv_data *pdata,
1378 struct xgbe_ring_data *rdata, unsigned int index)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001379{
1380 struct xgbe_ring_desc *rdesc = rdata->rdesc;
Lendacky, Thomas8dee19e2015-04-09 12:11:51 -05001381 unsigned int rx_usecs = pdata->rx_usecs;
1382 unsigned int rx_frames = pdata->rx_frames;
1383 unsigned int inte;
Lendacky, Thomascfbfd862015-07-06 11:57:37 -05001384 dma_addr_t hdr_dma, buf_dma;
Lendacky, Thomas8dee19e2015-04-09 12:11:51 -05001385
1386 if (!rx_usecs && !rx_frames) {
1387 /* No coalescing, interrupt for every descriptor */
1388 inte = 1;
1389 } else {
1390 /* Set interrupt based on Rx frame coalescing setting */
1391 if (rx_frames && !((index + 1) % rx_frames))
1392 inte = 1;
1393 else
1394 inte = 0;
1395 }
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001396
1397 /* Reset the Rx descriptor
Lendacky, Thomas174fd252014-11-04 16:06:50 -06001398 * Set buffer 1 (lo) address to header dma address (lo)
1399 * Set buffer 1 (hi) address to header dma address (hi)
1400 * Set buffer 2 (lo) address to buffer dma address (lo)
1401 * Set buffer 2 (hi) address to buffer dma address (hi) and
1402 * set control bits OWN and INTE
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001403 */
Lendacky, Thomascfbfd862015-07-06 11:57:37 -05001404 hdr_dma = rdata->rx.hdr.dma_base + rdata->rx.hdr.dma_off;
1405 buf_dma = rdata->rx.buf.dma_base + rdata->rx.buf.dma_off;
1406 rdesc->desc0 = cpu_to_le32(lower_32_bits(hdr_dma));
1407 rdesc->desc1 = cpu_to_le32(upper_32_bits(hdr_dma));
1408 rdesc->desc2 = cpu_to_le32(lower_32_bits(buf_dma));
1409 rdesc->desc3 = cpu_to_le32(upper_32_bits(buf_dma));
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001410
Lendacky, Thomas8dee19e2015-04-09 12:11:51 -05001411 XGMAC_SET_BITS_LE(rdesc->desc3, RX_NORMAL_DESC3, INTE, inte);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001412
1413 /* Since the Rx DMA engine is likely running, make sure everything
1414 * is written to the descriptor(s) before setting the OWN bit
1415 * for the descriptor
1416 */
Lendacky, Thomasceb8f6b2015-03-20 11:50:16 -05001417 dma_wmb();
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001418
1419 XGMAC_SET_BITS_LE(rdesc->desc3, RX_NORMAL_DESC3, OWN, 1);
1420
1421 /* Make sure ownership is written to the descriptor */
Lendacky, Thomasceb8f6b2015-03-20 11:50:16 -05001422 dma_wmb();
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001423}
1424
1425static void xgbe_rx_desc_init(struct xgbe_channel *channel)
1426{
1427 struct xgbe_prv_data *pdata = channel->pdata;
1428 struct xgbe_ring *ring = channel->rx_ring;
1429 struct xgbe_ring_data *rdata;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001430 unsigned int start_index = ring->cur;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001431 unsigned int i;
1432
1433 DBGPR("-->rx_desc_init\n");
1434
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001435 /* Initialize all descriptors */
1436 for (i = 0; i < ring->rdesc_count; i++) {
Lendacky, Thomasd0a8ba62014-06-24 16:19:06 -05001437 rdata = XGBE_GET_DESC_DATA(ring, i);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001438
Lendacky, Thomas08dcc472014-11-04 16:06:44 -06001439 /* Initialize Rx descriptor */
Lendacky, Thomas8dee19e2015-04-09 12:11:51 -05001440 xgbe_rx_desc_reset(pdata, rdata, i);
Lendacky, Thomas08dcc472014-11-04 16:06:44 -06001441 }
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001442
1443 /* Update the total number of Rx descriptors */
1444 XGMAC_DMA_IOWRITE(channel, DMA_CH_RDRLR, ring->rdesc_count - 1);
1445
1446 /* Update the starting address of descriptor ring */
Lendacky, Thomasd0a8ba62014-06-24 16:19:06 -05001447 rdata = XGBE_GET_DESC_DATA(ring, start_index);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001448 XGMAC_DMA_IOWRITE(channel, DMA_CH_RDLR_HI,
1449 upper_32_bits(rdata->rdesc_dma));
1450 XGMAC_DMA_IOWRITE(channel, DMA_CH_RDLR_LO,
1451 lower_32_bits(rdata->rdesc_dma));
1452
1453 /* Update the Rx Descriptor Tail Pointer */
Lendacky, Thomasd0a8ba62014-06-24 16:19:06 -05001454 rdata = XGBE_GET_DESC_DATA(ring, start_index + ring->rdesc_count - 1);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001455 XGMAC_DMA_IOWRITE(channel, DMA_CH_RDTR_LO,
1456 lower_32_bits(rdata->rdesc_dma));
1457
1458 DBGPR("<--rx_desc_init\n");
1459}
1460
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05001461static void xgbe_update_tstamp_addend(struct xgbe_prv_data *pdata,
1462 unsigned int addend)
1463{
Lendacky, Thomas9018ff52017-06-28 13:42:07 -05001464 unsigned int count = 10000;
1465
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05001466 /* Set the addend register value and tell the device */
1467 XGMAC_IOWRITE(pdata, MAC_TSAR, addend);
1468 XGMAC_IOWRITE_BITS(pdata, MAC_TSCR, TSADDREG, 1);
1469
1470 /* Wait for addend update to complete */
Lendacky, Thomas9018ff52017-06-28 13:42:07 -05001471 while (--count && XGMAC_IOREAD_BITS(pdata, MAC_TSCR, TSADDREG))
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05001472 udelay(5);
Lendacky, Thomas9018ff52017-06-28 13:42:07 -05001473
1474 if (!count)
1475 netdev_err(pdata->netdev,
1476 "timed out updating timestamp addend register\n");
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05001477}
1478
1479static void xgbe_set_tstamp_time(struct xgbe_prv_data *pdata, unsigned int sec,
1480 unsigned int nsec)
1481{
Lendacky, Thomas9018ff52017-06-28 13:42:07 -05001482 unsigned int count = 10000;
1483
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05001484 /* Set the time values and tell the device */
1485 XGMAC_IOWRITE(pdata, MAC_STSUR, sec);
1486 XGMAC_IOWRITE(pdata, MAC_STNUR, nsec);
1487 XGMAC_IOWRITE_BITS(pdata, MAC_TSCR, TSINIT, 1);
1488
1489 /* Wait for time update to complete */
Lendacky, Thomas9018ff52017-06-28 13:42:07 -05001490 while (--count && XGMAC_IOREAD_BITS(pdata, MAC_TSCR, TSINIT))
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05001491 udelay(5);
Lendacky, Thomas9018ff52017-06-28 13:42:07 -05001492
1493 if (!count)
1494 netdev_err(pdata->netdev, "timed out initializing timestamp\n");
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05001495}
1496
1497static u64 xgbe_get_tstamp_time(struct xgbe_prv_data *pdata)
1498{
1499 u64 nsec;
1500
1501 nsec = XGMAC_IOREAD(pdata, MAC_STSR);
1502 nsec *= NSEC_PER_SEC;
1503 nsec += XGMAC_IOREAD(pdata, MAC_STNR);
1504
1505 return nsec;
1506}
1507
1508static u64 xgbe_get_tx_tstamp(struct xgbe_prv_data *pdata)
1509{
Lendacky, Thomasaba97772016-11-10 17:09:45 -06001510 unsigned int tx_snr, tx_ssr;
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05001511 u64 nsec;
1512
Lendacky, Thomasaba97772016-11-10 17:09:45 -06001513 if (pdata->vdata->tx_tstamp_workaround) {
1514 tx_snr = XGMAC_IOREAD(pdata, MAC_TXSNR);
1515 tx_ssr = XGMAC_IOREAD(pdata, MAC_TXSSR);
1516 } else {
1517 tx_ssr = XGMAC_IOREAD(pdata, MAC_TXSSR);
1518 tx_snr = XGMAC_IOREAD(pdata, MAC_TXSNR);
1519 }
1520
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05001521 if (XGMAC_GET_BITS(tx_snr, MAC_TXSNR, TXTSSTSMIS))
1522 return 0;
1523
Lendacky, Thomasaba97772016-11-10 17:09:45 -06001524 nsec = tx_ssr;
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05001525 nsec *= NSEC_PER_SEC;
1526 nsec += tx_snr;
1527
1528 return nsec;
1529}
1530
1531static void xgbe_get_rx_tstamp(struct xgbe_packet_data *packet,
1532 struct xgbe_ring_desc *rdesc)
1533{
1534 u64 nsec;
1535
1536 if (XGMAC_GET_BITS_LE(rdesc->desc3, RX_CONTEXT_DESC3, TSA) &&
1537 !XGMAC_GET_BITS_LE(rdesc->desc3, RX_CONTEXT_DESC3, TSD)) {
1538 nsec = le32_to_cpu(rdesc->desc1);
1539 nsec <<= 32;
1540 nsec |= le32_to_cpu(rdesc->desc0);
1541 if (nsec != 0xffffffffffffffffULL) {
1542 packet->rx_tstamp = nsec;
1543 XGMAC_SET_BITS(packet->attributes, RX_PACKET_ATTRIBUTES,
1544 RX_TSTAMP, 1);
1545 }
1546 }
1547}
1548
1549static int xgbe_config_tstamp(struct xgbe_prv_data *pdata,
1550 unsigned int mac_tscr)
1551{
1552 /* Set one nano-second accuracy */
1553 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSCTRLSSR, 1);
1554
1555 /* Set fine timestamp update */
1556 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSCFUPDT, 1);
1557
1558 /* Overwrite earlier timestamps */
1559 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TXTSSTSM, 1);
1560
1561 XGMAC_IOWRITE(pdata, MAC_TSCR, mac_tscr);
1562
1563 /* Exit if timestamping is not enabled */
1564 if (!XGMAC_GET_BITS(mac_tscr, MAC_TSCR, TSENA))
1565 return 0;
1566
1567 /* Initialize time registers */
1568 XGMAC_IOWRITE_BITS(pdata, MAC_SSIR, SSINC, XGBE_TSTAMP_SSINC);
1569 XGMAC_IOWRITE_BITS(pdata, MAC_SSIR, SNSINC, XGBE_TSTAMP_SNSINC);
1570 xgbe_update_tstamp_addend(pdata, pdata->tstamp_addend);
1571 xgbe_set_tstamp_time(pdata, 0, 0);
1572
1573 /* Initialize the timecounter */
1574 timecounter_init(&pdata->tstamp_tc, &pdata->tstamp_cc,
1575 ktime_to_ns(ktime_get_real()));
1576
1577 return 0;
1578}
1579
Lendacky, Thomas16958a22014-11-20 11:04:08 -06001580static void xgbe_tx_start_xmit(struct xgbe_channel *channel,
1581 struct xgbe_ring *ring)
1582{
1583 struct xgbe_prv_data *pdata = channel->pdata;
1584 struct xgbe_ring_data *rdata;
1585
Lendacky, Thomasceb8f6b2015-03-20 11:50:16 -05001586 /* Make sure everything is written before the register write */
1587 wmb();
1588
Lendacky, Thomas16958a22014-11-20 11:04:08 -06001589 /* Issue a poll command to Tx DMA by writing address
1590 * of next immediate free descriptor */
1591 rdata = XGBE_GET_DESC_DATA(ring, ring->cur);
1592 XGMAC_DMA_IOWRITE(channel, DMA_CH_TDTR_LO,
1593 lower_32_bits(rdata->rdesc_dma));
1594
Lendacky, Thomasc635eaa2015-03-20 11:50:28 -05001595 /* Start the Tx timer */
Lendacky, Thomas16958a22014-11-20 11:04:08 -06001596 if (pdata->tx_usecs && !channel->tx_timer_active) {
1597 channel->tx_timer_active = 1;
Lendacky, Thomasc635eaa2015-03-20 11:50:28 -05001598 mod_timer(&channel->tx_timer,
1599 jiffies + usecs_to_jiffies(pdata->tx_usecs));
Lendacky, Thomas16958a22014-11-20 11:04:08 -06001600 }
1601
1602 ring->tx.xmit_more = 0;
1603}
1604
Lendacky, Thomasa9d41982014-11-04 16:06:32 -06001605static void xgbe_dev_xmit(struct xgbe_channel *channel)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001606{
1607 struct xgbe_prv_data *pdata = channel->pdata;
1608 struct xgbe_ring *ring = channel->tx_ring;
1609 struct xgbe_ring_data *rdata;
1610 struct xgbe_ring_desc *rdesc;
1611 struct xgbe_packet_data *packet = &ring->packet_data;
Lendacky, Thomas80a788c2017-08-18 09:03:35 -05001612 unsigned int tx_packets, tx_bytes;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001613 unsigned int csum, tso, vlan;
1614 unsigned int tso_context, vlan_context;
Lendacky, Thomaseb79e642014-11-20 11:04:02 -06001615 unsigned int tx_set_ic;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001616 int start_index = ring->cur;
Lendacky, Thomasa83ef422015-01-16 12:46:55 -06001617 int cur_index = ring->cur;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001618 int i;
1619
Lendacky, Thomasa9d41982014-11-04 16:06:32 -06001620 DBGPR("-->xgbe_dev_xmit\n");
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001621
Lendacky, Thomas80a788c2017-08-18 09:03:35 -05001622 tx_packets = packet->tx_packets;
1623 tx_bytes = packet->tx_bytes;
1624
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001625 csum = XGMAC_GET_BITS(packet->attributes, TX_PACKET_ATTRIBUTES,
1626 CSUM_ENABLE);
1627 tso = XGMAC_GET_BITS(packet->attributes, TX_PACKET_ATTRIBUTES,
1628 TSO_ENABLE);
1629 vlan = XGMAC_GET_BITS(packet->attributes, TX_PACKET_ATTRIBUTES,
1630 VLAN_CTAG);
1631
1632 if (tso && (packet->mss != ring->tx.cur_mss))
1633 tso_context = 1;
1634 else
1635 tso_context = 0;
1636
1637 if (vlan && (packet->vlan_ctag != ring->tx.cur_vlan_ctag))
1638 vlan_context = 1;
1639 else
1640 vlan_context = 0;
1641
Lendacky, Thomaseb79e642014-11-20 11:04:02 -06001642 /* Determine if an interrupt should be generated for this Tx:
1643 * Interrupt:
1644 * - Tx frame count exceeds the frame count setting
1645 * - Addition of Tx frame count to the frame count since the
1646 * last interrupt was set exceeds the frame count setting
1647 * No interrupt:
1648 * - No frame count setting specified (ethtool -C ethX tx-frames 0)
1649 * - Addition of Tx frame count to the frame count since the
1650 * last interrupt was set does not exceed the frame count setting
1651 */
Lendacky, Thomas80a788c2017-08-18 09:03:35 -05001652 ring->coalesce_count += tx_packets;
Lendacky, Thomaseb79e642014-11-20 11:04:02 -06001653 if (!pdata->tx_frames)
1654 tx_set_ic = 0;
Lendacky, Thomas80a788c2017-08-18 09:03:35 -05001655 else if (tx_packets > pdata->tx_frames)
Lendacky, Thomaseb79e642014-11-20 11:04:02 -06001656 tx_set_ic = 1;
Lendacky, Thomas80a788c2017-08-18 09:03:35 -05001657 else if ((ring->coalesce_count % pdata->tx_frames) < tx_packets)
Lendacky, Thomaseb79e642014-11-20 11:04:02 -06001658 tx_set_ic = 1;
1659 else
1660 tx_set_ic = 0;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001661
Lendacky, Thomasa83ef422015-01-16 12:46:55 -06001662 rdata = XGBE_GET_DESC_DATA(ring, cur_index);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001663 rdesc = rdata->rdesc;
1664
1665 /* Create a context descriptor if this is a TSO packet */
1666 if (tso_context || vlan_context) {
1667 if (tso_context) {
Lendacky, Thomas34bf65d2015-05-14 11:44:03 -05001668 netif_dbg(pdata, tx_queued, pdata->netdev,
1669 "TSO context descriptor, mss=%u\n",
1670 packet->mss);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001671
1672 /* Set the MSS size */
1673 XGMAC_SET_BITS_LE(rdesc->desc2, TX_CONTEXT_DESC2,
1674 MSS, packet->mss);
1675
1676 /* Mark it as a CONTEXT descriptor */
1677 XGMAC_SET_BITS_LE(rdesc->desc3, TX_CONTEXT_DESC3,
1678 CTXT, 1);
1679
1680 /* Indicate this descriptor contains the MSS */
1681 XGMAC_SET_BITS_LE(rdesc->desc3, TX_CONTEXT_DESC3,
1682 TCMSSV, 1);
1683
1684 ring->tx.cur_mss = packet->mss;
1685 }
1686
1687 if (vlan_context) {
Lendacky, Thomas34bf65d2015-05-14 11:44:03 -05001688 netif_dbg(pdata, tx_queued, pdata->netdev,
1689 "VLAN context descriptor, ctag=%u\n",
1690 packet->vlan_ctag);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001691
1692 /* Mark it as a CONTEXT descriptor */
1693 XGMAC_SET_BITS_LE(rdesc->desc3, TX_CONTEXT_DESC3,
1694 CTXT, 1);
1695
1696 /* Set the VLAN tag */
1697 XGMAC_SET_BITS_LE(rdesc->desc3, TX_CONTEXT_DESC3,
1698 VT, packet->vlan_ctag);
1699
1700 /* Indicate this descriptor contains the VLAN tag */
1701 XGMAC_SET_BITS_LE(rdesc->desc3, TX_CONTEXT_DESC3,
1702 VLTV, 1);
1703
1704 ring->tx.cur_vlan_ctag = packet->vlan_ctag;
1705 }
1706
Lendacky, Thomasa83ef422015-01-16 12:46:55 -06001707 cur_index++;
1708 rdata = XGBE_GET_DESC_DATA(ring, cur_index);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001709 rdesc = rdata->rdesc;
1710 }
1711
1712 /* Update buffer address (for TSO this is the header) */
1713 rdesc->desc0 = cpu_to_le32(lower_32_bits(rdata->skb_dma));
1714 rdesc->desc1 = cpu_to_le32(upper_32_bits(rdata->skb_dma));
1715
1716 /* Update the buffer length */
1717 XGMAC_SET_BITS_LE(rdesc->desc2, TX_NORMAL_DESC2, HL_B1L,
1718 rdata->skb_dma_len);
1719
1720 /* VLAN tag insertion check */
1721 if (vlan)
1722 XGMAC_SET_BITS_LE(rdesc->desc2, TX_NORMAL_DESC2, VTIR,
1723 TX_NORMAL_DESC2_VLAN_INSERT);
1724
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05001725 /* Timestamp enablement check */
1726 if (XGMAC_GET_BITS(packet->attributes, TX_PACKET_ATTRIBUTES, PTP))
1727 XGMAC_SET_BITS_LE(rdesc->desc2, TX_NORMAL_DESC2, TTSE, 1);
1728
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001729 /* Mark it as First Descriptor */
1730 XGMAC_SET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, FD, 1);
1731
1732 /* Mark it as a NORMAL descriptor */
1733 XGMAC_SET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, CTXT, 0);
1734
1735 /* Set OWN bit if not the first descriptor */
Lendacky, Thomasa83ef422015-01-16 12:46:55 -06001736 if (cur_index != start_index)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001737 XGMAC_SET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, OWN, 1);
1738
1739 if (tso) {
1740 /* Enable TSO */
1741 XGMAC_SET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, TSE, 1);
1742 XGMAC_SET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, TCPPL,
1743 packet->tcp_payload_len);
1744 XGMAC_SET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, TCPHDRLEN,
1745 packet->tcp_header_len / 4);
Lendacky, Thomas5452b2d2015-05-14 11:43:57 -05001746
Lendacky, Thomas80a788c2017-08-18 09:03:35 -05001747 pdata->ext_stats.tx_tso_packets += tx_packets;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001748 } else {
1749 /* Enable CRC and Pad Insertion */
1750 XGMAC_SET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, CPC, 0);
1751
1752 /* Enable HW CSUM */
1753 if (csum)
1754 XGMAC_SET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3,
1755 CIC, 0x3);
1756
1757 /* Set the total length to be transmitted */
1758 XGMAC_SET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, FL,
1759 packet->length);
1760 }
1761
Lendacky, Thomasa83ef422015-01-16 12:46:55 -06001762 for (i = cur_index - start_index + 1; i < packet->rdesc_count; i++) {
1763 cur_index++;
1764 rdata = XGBE_GET_DESC_DATA(ring, cur_index);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001765 rdesc = rdata->rdesc;
1766
1767 /* Update buffer address */
1768 rdesc->desc0 = cpu_to_le32(lower_32_bits(rdata->skb_dma));
1769 rdesc->desc1 = cpu_to_le32(upper_32_bits(rdata->skb_dma));
1770
1771 /* Update the buffer length */
1772 XGMAC_SET_BITS_LE(rdesc->desc2, TX_NORMAL_DESC2, HL_B1L,
1773 rdata->skb_dma_len);
1774
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001775 /* Set OWN bit */
1776 XGMAC_SET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, OWN, 1);
1777
1778 /* Mark it as NORMAL descriptor */
1779 XGMAC_SET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, CTXT, 0);
1780
1781 /* Enable HW CSUM */
1782 if (csum)
1783 XGMAC_SET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3,
1784 CIC, 0x3);
1785 }
1786
1787 /* Set LAST bit for the last descriptor */
1788 XGMAC_SET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, LD, 1);
1789
Lendacky, Thomaseb79e642014-11-20 11:04:02 -06001790 /* Set IC bit based on Tx coalescing settings */
1791 if (tx_set_ic)
1792 XGMAC_SET_BITS_LE(rdesc->desc2, TX_NORMAL_DESC2, IC, 1);
1793
Lendacky, Thomas5fb4b862014-11-20 11:03:50 -06001794 /* Save the Tx info to report back during cleanup */
Lendacky, Thomas80a788c2017-08-18 09:03:35 -05001795 rdata->tx.packets = tx_packets;
1796 rdata->tx.bytes = tx_bytes;
1797
1798 pdata->ext_stats.txq_packets[channel->queue_index] += tx_packets;
1799 pdata->ext_stats.txq_bytes[channel->queue_index] += tx_bytes;
Lendacky, Thomas5fb4b862014-11-20 11:03:50 -06001800
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001801 /* In case the Tx DMA engine is running, make sure everything
1802 * is written to the descriptor(s) before setting the OWN bit
1803 * for the first descriptor
1804 */
Lendacky, Thomasceb8f6b2015-03-20 11:50:16 -05001805 dma_wmb();
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001806
1807 /* Set OWN bit for the first descriptor */
Lendacky, Thomasd0a8ba62014-06-24 16:19:06 -05001808 rdata = XGBE_GET_DESC_DATA(ring, start_index);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001809 rdesc = rdata->rdesc;
1810 XGMAC_SET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, OWN, 1);
1811
Lendacky, Thomas34bf65d2015-05-14 11:44:03 -05001812 if (netif_msg_tx_queued(pdata))
1813 xgbe_dump_tx_desc(pdata, ring, start_index,
1814 packet->rdesc_count, 1);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001815
1816 /* Make sure ownership is written to the descriptor */
Lendacky, Thomas20986ed2015-10-26 17:13:54 -05001817 smp_wmb();
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001818
Lendacky, Thomasa83ef422015-01-16 12:46:55 -06001819 ring->cur = cur_index + 1;
Lendacky, Thomas16958a22014-11-20 11:04:08 -06001820 if (!packet->skb->xmit_more ||
1821 netif_xmit_stopped(netdev_get_tx_queue(pdata->netdev,
1822 channel->queue_index)))
1823 xgbe_tx_start_xmit(channel, ring);
1824 else
1825 ring->tx.xmit_more = 1;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001826
1827 DBGPR(" %s: descriptors %u to %u written\n",
1828 channel->name, start_index & (ring->rdesc_count - 1),
1829 (ring->cur - 1) & (ring->rdesc_count - 1));
1830
Lendacky, Thomasa9d41982014-11-04 16:06:32 -06001831 DBGPR("<--xgbe_dev_xmit\n");
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001832}
1833
1834static int xgbe_dev_read(struct xgbe_channel *channel)
1835{
Lendacky, Thomas5452b2d2015-05-14 11:43:57 -05001836 struct xgbe_prv_data *pdata = channel->pdata;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001837 struct xgbe_ring *ring = channel->rx_ring;
1838 struct xgbe_ring_data *rdata;
1839 struct xgbe_ring_desc *rdesc;
1840 struct xgbe_packet_data *packet = &ring->packet_data;
Lendacky, Thomas5452b2d2015-05-14 11:43:57 -05001841 struct net_device *netdev = pdata->netdev;
Lendacky, Thomas5b9dfe22014-11-04 16:07:02 -06001842 unsigned int err, etlt, l34t;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001843
1844 DBGPR("-->xgbe_dev_read: cur = %d\n", ring->cur);
1845
Lendacky, Thomasd0a8ba62014-06-24 16:19:06 -05001846 rdata = XGBE_GET_DESC_DATA(ring, ring->cur);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001847 rdesc = rdata->rdesc;
1848
1849 /* Check for data availability */
1850 if (XGMAC_GET_BITS_LE(rdesc->desc3, RX_NORMAL_DESC3, OWN))
1851 return 1;
1852
Lendacky, Thomas5449e272014-11-20 11:03:26 -06001853 /* Make sure descriptor fields are read after reading the OWN bit */
Lendacky, Thomasceb8f6b2015-03-20 11:50:16 -05001854 dma_rmb();
Lendacky, Thomas5449e272014-11-20 11:03:26 -06001855
Lendacky, Thomas34bf65d2015-05-14 11:44:03 -05001856 if (netif_msg_rx_status(pdata))
1857 xgbe_dump_rx_desc(pdata, ring, ring->cur);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001858
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05001859 if (XGMAC_GET_BITS_LE(rdesc->desc3, RX_NORMAL_DESC3, CTXT)) {
1860 /* Timestamp Context Descriptor */
1861 xgbe_get_rx_tstamp(packet, rdesc);
1862
1863 XGMAC_SET_BITS(packet->attributes, RX_PACKET_ATTRIBUTES,
1864 CONTEXT, 1);
1865 XGMAC_SET_BITS(packet->attributes, RX_PACKET_ATTRIBUTES,
1866 CONTEXT_NEXT, 0);
1867 return 0;
1868 }
1869
1870 /* Normal Descriptor, be sure Context Descriptor bit is off */
1871 XGMAC_SET_BITS(packet->attributes, RX_PACKET_ATTRIBUTES, CONTEXT, 0);
1872
1873 /* Indicate if a Context Descriptor is next */
1874 if (XGMAC_GET_BITS_LE(rdesc->desc3, RX_NORMAL_DESC3, CDA))
1875 XGMAC_SET_BITS(packet->attributes, RX_PACKET_ATTRIBUTES,
1876 CONTEXT_NEXT, 1);
1877
Lendacky, Thomas174fd252014-11-04 16:06:50 -06001878 /* Get the header length */
Lendacky, Thomas5452b2d2015-05-14 11:43:57 -05001879 if (XGMAC_GET_BITS_LE(rdesc->desc3, RX_NORMAL_DESC3, FD)) {
Lendacky, Thomas622c36f2017-03-15 15:11:23 -05001880 XGMAC_SET_BITS(packet->attributes, RX_PACKET_ATTRIBUTES,
1881 FIRST, 1);
Lendacky, Thomasc9f140e2014-11-20 11:03:44 -06001882 rdata->rx.hdr_len = XGMAC_GET_BITS_LE(rdesc->desc2,
1883 RX_NORMAL_DESC2, HL);
Lendacky, Thomas5452b2d2015-05-14 11:43:57 -05001884 if (rdata->rx.hdr_len)
1885 pdata->ext_stats.rx_split_header_packets++;
Lendacky, Thomas622c36f2017-03-15 15:11:23 -05001886 } else {
1887 XGMAC_SET_BITS(packet->attributes, RX_PACKET_ATTRIBUTES,
1888 FIRST, 0);
Lendacky, Thomas5452b2d2015-05-14 11:43:57 -05001889 }
Lendacky, Thomas174fd252014-11-04 16:06:50 -06001890
Lendacky, Thomas5b9dfe22014-11-04 16:07:02 -06001891 /* Get the RSS hash */
1892 if (XGMAC_GET_BITS_LE(rdesc->desc3, RX_NORMAL_DESC3, RSV)) {
1893 XGMAC_SET_BITS(packet->attributes, RX_PACKET_ATTRIBUTES,
1894 RSS_HASH, 1);
1895
1896 packet->rss_hash = le32_to_cpu(rdesc->desc1);
1897
1898 l34t = XGMAC_GET_BITS_LE(rdesc->desc3, RX_NORMAL_DESC3, L34T);
1899 switch (l34t) {
1900 case RX_DESC3_L34T_IPV4_TCP:
1901 case RX_DESC3_L34T_IPV4_UDP:
1902 case RX_DESC3_L34T_IPV6_TCP:
1903 case RX_DESC3_L34T_IPV6_UDP:
1904 packet->rss_hash_type = PKT_HASH_TYPE_L4;
Dan Carpenterb6267d32014-11-13 09:19:06 +03001905 break;
Lendacky, Thomas5b9dfe22014-11-04 16:07:02 -06001906 default:
1907 packet->rss_hash_type = PKT_HASH_TYPE_L3;
1908 }
1909 }
1910
Lendacky, Thomas622c36f2017-03-15 15:11:23 -05001911 /* Not all the data has been transferred for this packet */
1912 if (!XGMAC_GET_BITS_LE(rdesc->desc3, RX_NORMAL_DESC3, LD))
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001913 return 0;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001914
1915 /* This is the last of the data for this packet */
1916 XGMAC_SET_BITS(packet->attributes, RX_PACKET_ATTRIBUTES,
Lendacky, Thomas622c36f2017-03-15 15:11:23 -05001917 LAST, 1);
1918
1919 /* Get the packet length */
1920 rdata->rx.len = XGMAC_GET_BITS_LE(rdesc->desc3, RX_NORMAL_DESC3, PL);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001921
1922 /* Set checksum done indicator as appropriate */
Lendacky, Thomas5452b2d2015-05-14 11:43:57 -05001923 if (netdev->features & NETIF_F_RXCSUM)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001924 XGMAC_SET_BITS(packet->attributes, RX_PACKET_ATTRIBUTES,
1925 CSUM_DONE, 1);
1926
1927 /* Check for errors (only valid in last descriptor) */
1928 err = XGMAC_GET_BITS_LE(rdesc->desc3, RX_NORMAL_DESC3, ES);
1929 etlt = XGMAC_GET_BITS_LE(rdesc->desc3, RX_NORMAL_DESC3, ETLT);
Lendacky, Thomas34bf65d2015-05-14 11:44:03 -05001930 netif_dbg(pdata, rx_status, netdev, "err=%u, etlt=%#x\n", err, etlt);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001931
Lendacky, Thomas7bba35b2014-11-20 11:03:38 -06001932 if (!err || !etlt) {
1933 /* No error if err is 0 or etlt is 0 */
Lendacky, Thomasc52e9c62014-06-24 16:19:18 -05001934 if ((etlt == 0x09) &&
1935 (netdev->features & NETIF_F_HW_VLAN_CTAG_RX)) {
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001936 XGMAC_SET_BITS(packet->attributes, RX_PACKET_ATTRIBUTES,
1937 VLAN_CTAG, 1);
1938 packet->vlan_ctag = XGMAC_GET_BITS_LE(rdesc->desc0,
1939 RX_NORMAL_DESC0,
1940 OVT);
Lendacky, Thomas34bf65d2015-05-14 11:44:03 -05001941 netif_dbg(pdata, rx_status, netdev, "vlan-ctag=%#06x\n",
1942 packet->vlan_ctag);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001943 }
1944 } else {
1945 if ((etlt == 0x05) || (etlt == 0x06))
1946 XGMAC_SET_BITS(packet->attributes, RX_PACKET_ATTRIBUTES,
1947 CSUM_DONE, 0);
1948 else
1949 XGMAC_SET_BITS(packet->errors, RX_PACKET_ERRORS,
1950 FRAME, 1);
1951 }
1952
Lendacky, Thomas80a788c2017-08-18 09:03:35 -05001953 pdata->ext_stats.rxq_packets[channel->queue_index]++;
1954 pdata->ext_stats.rxq_bytes[channel->queue_index] += rdata->rx.len;
1955
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001956 DBGPR("<--xgbe_dev_read: %s - descriptor=%u (cur=%d)\n", channel->name,
1957 ring->cur & (ring->rdesc_count - 1), ring->cur);
1958
1959 return 0;
1960}
1961
1962static int xgbe_is_context_desc(struct xgbe_ring_desc *rdesc)
1963{
1964 /* Rx and Tx share CTXT bit, so check TDES3.CTXT bit */
1965 return XGMAC_GET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, CTXT);
1966}
1967
1968static int xgbe_is_last_desc(struct xgbe_ring_desc *rdesc)
1969{
1970 /* Rx and Tx share LD bit, so check TDES3.LD bit */
1971 return XGMAC_GET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, LD);
1972}
1973
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001974static int xgbe_enable_int(struct xgbe_channel *channel,
1975 enum xgbe_int int_id)
1976{
1977 switch (int_id) {
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001978 case XGMAC_INT_DMA_CH_SR_TI:
Lendacky, Thomascaa575af2017-08-18 09:03:17 -05001979 XGMAC_SET_BITS(channel->curr_ier, DMA_CH_IER, TIE, 1);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001980 break;
1981 case XGMAC_INT_DMA_CH_SR_TPS:
Lendacky, Thomascaa575af2017-08-18 09:03:17 -05001982 XGMAC_SET_BITS(channel->curr_ier, DMA_CH_IER, TXSE, 1);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001983 break;
1984 case XGMAC_INT_DMA_CH_SR_TBU:
Lendacky, Thomascaa575af2017-08-18 09:03:17 -05001985 XGMAC_SET_BITS(channel->curr_ier, DMA_CH_IER, TBUE, 1);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001986 break;
1987 case XGMAC_INT_DMA_CH_SR_RI:
Lendacky, Thomascaa575af2017-08-18 09:03:17 -05001988 XGMAC_SET_BITS(channel->curr_ier, DMA_CH_IER, RIE, 1);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001989 break;
1990 case XGMAC_INT_DMA_CH_SR_RBU:
Lendacky, Thomascaa575af2017-08-18 09:03:17 -05001991 XGMAC_SET_BITS(channel->curr_ier, DMA_CH_IER, RBUE, 1);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001992 break;
1993 case XGMAC_INT_DMA_CH_SR_RPS:
Lendacky, Thomascaa575af2017-08-18 09:03:17 -05001994 XGMAC_SET_BITS(channel->curr_ier, DMA_CH_IER, RSE, 1);
Lendacky, Thomas9867e8f2014-07-02 13:04:46 -05001995 break;
1996 case XGMAC_INT_DMA_CH_SR_TI_RI:
Lendacky, Thomascaa575af2017-08-18 09:03:17 -05001997 XGMAC_SET_BITS(channel->curr_ier, DMA_CH_IER, TIE, 1);
1998 XGMAC_SET_BITS(channel->curr_ier, DMA_CH_IER, RIE, 1);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001999 break;
2000 case XGMAC_INT_DMA_CH_SR_FBE:
Lendacky, Thomascaa575af2017-08-18 09:03:17 -05002001 XGMAC_SET_BITS(channel->curr_ier, DMA_CH_IER, FBEE, 1);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002002 break;
2003 case XGMAC_INT_DMA_ALL:
Lendacky, Thomascaa575af2017-08-18 09:03:17 -05002004 channel->curr_ier |= channel->saved_ier;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002005 break;
2006 default:
2007 return -1;
2008 }
2009
Lendacky, Thomascaa575af2017-08-18 09:03:17 -05002010 XGMAC_DMA_IOWRITE(channel, DMA_CH_IER, channel->curr_ier);
Lendacky, Thomas9867e8f2014-07-02 13:04:46 -05002011
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002012 return 0;
2013}
2014
2015static int xgbe_disable_int(struct xgbe_channel *channel,
2016 enum xgbe_int int_id)
2017{
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002018 switch (int_id) {
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002019 case XGMAC_INT_DMA_CH_SR_TI:
Lendacky, Thomascaa575af2017-08-18 09:03:17 -05002020 XGMAC_SET_BITS(channel->curr_ier, DMA_CH_IER, TIE, 0);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002021 break;
2022 case XGMAC_INT_DMA_CH_SR_TPS:
Lendacky, Thomascaa575af2017-08-18 09:03:17 -05002023 XGMAC_SET_BITS(channel->curr_ier, DMA_CH_IER, TXSE, 0);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002024 break;
2025 case XGMAC_INT_DMA_CH_SR_TBU:
Lendacky, Thomascaa575af2017-08-18 09:03:17 -05002026 XGMAC_SET_BITS(channel->curr_ier, DMA_CH_IER, TBUE, 0);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002027 break;
2028 case XGMAC_INT_DMA_CH_SR_RI:
Lendacky, Thomascaa575af2017-08-18 09:03:17 -05002029 XGMAC_SET_BITS(channel->curr_ier, DMA_CH_IER, RIE, 0);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002030 break;
2031 case XGMAC_INT_DMA_CH_SR_RBU:
Lendacky, Thomascaa575af2017-08-18 09:03:17 -05002032 XGMAC_SET_BITS(channel->curr_ier, DMA_CH_IER, RBUE, 0);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002033 break;
2034 case XGMAC_INT_DMA_CH_SR_RPS:
Lendacky, Thomascaa575af2017-08-18 09:03:17 -05002035 XGMAC_SET_BITS(channel->curr_ier, DMA_CH_IER, RSE, 0);
Lendacky, Thomas9867e8f2014-07-02 13:04:46 -05002036 break;
2037 case XGMAC_INT_DMA_CH_SR_TI_RI:
Lendacky, Thomascaa575af2017-08-18 09:03:17 -05002038 XGMAC_SET_BITS(channel->curr_ier, DMA_CH_IER, TIE, 0);
2039 XGMAC_SET_BITS(channel->curr_ier, DMA_CH_IER, RIE, 0);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002040 break;
2041 case XGMAC_INT_DMA_CH_SR_FBE:
Lendacky, Thomascaa575af2017-08-18 09:03:17 -05002042 XGMAC_SET_BITS(channel->curr_ier, DMA_CH_IER, FBEE, 0);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002043 break;
2044 case XGMAC_INT_DMA_ALL:
Lendacky, Thomascaa575af2017-08-18 09:03:17 -05002045 channel->saved_ier = channel->curr_ier;
2046 channel->curr_ier = 0;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002047 break;
2048 default:
2049 return -1;
2050 }
2051
Lendacky, Thomascaa575af2017-08-18 09:03:17 -05002052 XGMAC_DMA_IOWRITE(channel, DMA_CH_IER, channel->curr_ier);
Lendacky, Thomas9867e8f2014-07-02 13:04:46 -05002053
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002054 return 0;
2055}
2056
Lendacky, Thomas5ffc0332016-11-10 17:09:29 -06002057static int __xgbe_exit(struct xgbe_prv_data *pdata)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002058{
2059 unsigned int count = 2000;
2060
2061 DBGPR("-->xgbe_exit\n");
2062
2063 /* Issue a software reset */
2064 XGMAC_IOWRITE_BITS(pdata, DMA_MR, SWR, 1);
2065 usleep_range(10, 15);
2066
2067 /* Poll Until Poll Condition */
Dan Carpenterc7557e62015-12-15 13:12:29 +03002068 while (--count && XGMAC_IOREAD_BITS(pdata, DMA_MR, SWR))
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002069 usleep_range(500, 600);
2070
2071 if (!count)
2072 return -EBUSY;
2073
2074 DBGPR("<--xgbe_exit\n");
2075
2076 return 0;
2077}
2078
Lendacky, Thomas5ffc0332016-11-10 17:09:29 -06002079static int xgbe_exit(struct xgbe_prv_data *pdata)
2080{
2081 int ret;
2082
2083 /* To guard against possible incorrectly generated interrupts,
2084 * issue the software reset twice.
2085 */
2086 ret = __xgbe_exit(pdata);
2087 if (ret)
2088 return ret;
2089
2090 return __xgbe_exit(pdata);
2091}
2092
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002093static int xgbe_flush_tx_queues(struct xgbe_prv_data *pdata)
2094{
2095 unsigned int i, count;
2096
Lendacky, Thomasa9a4a2d2014-08-29 13:16:50 -05002097 if (XGMAC_GET_BITS(pdata->hw_feat.version, MAC_VR, SNPSVER) < 0x21)
2098 return 0;
2099
Lendacky, Thomas853eb162014-07-29 08:57:31 -05002100 for (i = 0; i < pdata->tx_q_count; i++)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002101 XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_Q_TQOMR, FTQ, 1);
2102
2103 /* Poll Until Poll Condition */
Lendacky, Thomas853eb162014-07-29 08:57:31 -05002104 for (i = 0; i < pdata->tx_q_count; i++) {
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002105 count = 2000;
Dan Carpenterc7557e62015-12-15 13:12:29 +03002106 while (--count && XGMAC_MTL_IOREAD_BITS(pdata, i,
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002107 MTL_Q_TQOMR, FTQ))
2108 usleep_range(500, 600);
2109
2110 if (!count)
2111 return -EBUSY;
2112 }
2113
2114 return 0;
2115}
2116
2117static void xgbe_config_dma_bus(struct xgbe_prv_data *pdata)
2118{
Lendacky, Thomas6f595952017-06-28 13:43:26 -05002119 unsigned int sbmr;
2120
2121 sbmr = XGMAC_IOREAD(pdata, DMA_SBMR);
2122
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002123 /* Set enhanced addressing mode */
Lendacky, Thomas6f595952017-06-28 13:43:26 -05002124 XGMAC_SET_BITS(sbmr, DMA_SBMR, EAME, 1);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002125
2126 /* Set the System Bus mode */
Lendacky, Thomas6f595952017-06-28 13:43:26 -05002127 XGMAC_SET_BITS(sbmr, DMA_SBMR, UNDEF, 1);
2128 XGMAC_SET_BITS(sbmr, DMA_SBMR, BLEN, pdata->blen >> 2);
2129 XGMAC_SET_BITS(sbmr, DMA_SBMR, AAL, pdata->aal);
2130 XGMAC_SET_BITS(sbmr, DMA_SBMR, RD_OSR_LMT, pdata->rd_osr_limit - 1);
2131 XGMAC_SET_BITS(sbmr, DMA_SBMR, WR_OSR_LMT, pdata->wr_osr_limit - 1);
2132
2133 XGMAC_IOWRITE(pdata, DMA_SBMR, sbmr);
2134
2135 /* Set descriptor fetching threshold */
2136 if (pdata->vdata->tx_desc_prefetch)
2137 XGMAC_IOWRITE_BITS(pdata, DMA_TXEDMACR, TDPS,
2138 pdata->vdata->tx_desc_prefetch);
2139
2140 if (pdata->vdata->rx_desc_prefetch)
2141 XGMAC_IOWRITE_BITS(pdata, DMA_RXEDMACR, RDPS,
2142 pdata->vdata->rx_desc_prefetch);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002143}
2144
2145static void xgbe_config_dma_cache(struct xgbe_prv_data *pdata)
2146{
Lendacky, Thomas99167162017-06-28 13:43:09 -05002147 XGMAC_IOWRITE(pdata, DMA_AXIARCR, pdata->arcr);
2148 XGMAC_IOWRITE(pdata, DMA_AXIAWCR, pdata->awcr);
Lendacky, Thomas6f595952017-06-28 13:43:26 -05002149 if (pdata->awarcr)
2150 XGMAC_IOWRITE(pdata, DMA_AXIAWARCR, pdata->awarcr);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002151}
2152
2153static void xgbe_config_mtl_mode(struct xgbe_prv_data *pdata)
2154{
2155 unsigned int i;
2156
Lendacky, Thomasfca2d992014-07-29 08:57:55 -05002157 /* Set Tx to weighted round robin scheduling algorithm */
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002158 XGMAC_IOWRITE_BITS(pdata, MTL_OMR, ETSALG, MTL_ETSALG_WRR);
2159
Lendacky, Thomasfca2d992014-07-29 08:57:55 -05002160 /* Set Tx traffic classes to use WRR algorithm with equal weights */
2161 for (i = 0; i < pdata->hw_feat.tc_cnt; i++) {
2162 XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_TC_ETSCR, TSA,
2163 MTL_TSA_ETS);
2164 XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_TC_QWR, QW, 1);
2165 }
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002166
2167 /* Set Rx to strict priority algorithm */
2168 XGMAC_IOWRITE_BITS(pdata, MTL_OMR, RAA, MTL_RAA_SP);
2169}
2170
Lendacky, Thomas43e0dcf2016-11-03 13:18:16 -05002171static void xgbe_queue_flow_control_threshold(struct xgbe_prv_data *pdata,
2172 unsigned int queue,
2173 unsigned int q_fifo_size)
2174{
2175 unsigned int frame_fifo_size;
2176 unsigned int rfa, rfd;
2177
2178 frame_fifo_size = XGMAC_FLOW_CONTROL_ALIGN(xgbe_get_max_frame(pdata));
2179
2180 if (pdata->pfcq[queue] && (q_fifo_size > pdata->pfc_rfa)) {
2181 /* PFC is active for this queue */
2182 rfa = pdata->pfc_rfa;
2183 rfd = rfa + frame_fifo_size;
2184 if (rfd > XGMAC_FLOW_CONTROL_MAX)
2185 rfd = XGMAC_FLOW_CONTROL_MAX;
2186 if (rfa >= XGMAC_FLOW_CONTROL_MAX)
2187 rfa = XGMAC_FLOW_CONTROL_MAX - XGMAC_FLOW_CONTROL_UNIT;
2188 } else {
2189 /* This path deals with just maximum frame sizes which are
2190 * limited to a jumbo frame of 9,000 (plus headers, etc.)
2191 * so we can never exceed the maximum allowable RFA/RFD
2192 * values.
2193 */
2194 if (q_fifo_size <= 2048) {
2195 /* rx_rfd to zero to signal no flow control */
2196 pdata->rx_rfa[queue] = 0;
2197 pdata->rx_rfd[queue] = 0;
2198 return;
2199 }
2200
2201 if (q_fifo_size <= 4096) {
2202 /* Between 2048 and 4096 */
2203 pdata->rx_rfa[queue] = 0; /* Full - 1024 bytes */
2204 pdata->rx_rfd[queue] = 1; /* Full - 1536 bytes */
2205 return;
2206 }
2207
2208 if (q_fifo_size <= frame_fifo_size) {
2209 /* Between 4096 and max-frame */
2210 pdata->rx_rfa[queue] = 2; /* Full - 2048 bytes */
2211 pdata->rx_rfd[queue] = 5; /* Full - 3584 bytes */
2212 return;
2213 }
2214
2215 if (q_fifo_size <= (frame_fifo_size * 3)) {
2216 /* Between max-frame and 3 max-frames,
2217 * trigger if we get just over a frame of data and
2218 * resume when we have just under half a frame left.
2219 */
2220 rfa = q_fifo_size - frame_fifo_size;
2221 rfd = rfa + (frame_fifo_size / 2);
2222 } else {
2223 /* Above 3 max-frames - trigger when just over
2224 * 2 frames of space available
2225 */
2226 rfa = frame_fifo_size * 2;
2227 rfa += XGMAC_FLOW_CONTROL_UNIT;
2228 rfd = rfa + frame_fifo_size;
2229 }
2230 }
2231
2232 pdata->rx_rfa[queue] = XGMAC_FLOW_CONTROL_VALUE(rfa);
2233 pdata->rx_rfd[queue] = XGMAC_FLOW_CONTROL_VALUE(rfd);
2234}
2235
2236static void xgbe_calculate_flow_control_threshold(struct xgbe_prv_data *pdata,
2237 unsigned int *fifo)
2238{
2239 unsigned int q_fifo_size;
2240 unsigned int i;
2241
2242 for (i = 0; i < pdata->rx_q_count; i++) {
2243 q_fifo_size = (fifo[i] + 1) * XGMAC_FIFO_UNIT;
2244
2245 xgbe_queue_flow_control_threshold(pdata, i, q_fifo_size);
2246 }
2247}
2248
2249static void xgbe_config_flow_control_threshold(struct xgbe_prv_data *pdata)
2250{
2251 unsigned int i;
2252
2253 for (i = 0; i < pdata->rx_q_count; i++) {
2254 XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_Q_RQFCR, RFA,
2255 pdata->rx_rfa[i]);
2256 XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_Q_RQFCR, RFD,
2257 pdata->rx_rfd[i]);
2258 }
2259}
2260
Lendacky, Thomas586e3cfb2016-11-03 13:17:48 -05002261static unsigned int xgbe_get_tx_fifo_size(struct xgbe_prv_data *pdata)
2262{
Lendacky, Thomas586e3cfb2016-11-03 13:17:48 -05002263 /* The configured value may not be the actual amount of fifo RAM */
Lendacky, Thomasbd8255d2016-11-03 13:19:27 -05002264 return min_t(unsigned int, pdata->tx_max_fifo_size,
2265 pdata->hw_feat.tx_fifo_size);
Lendacky, Thomas586e3cfb2016-11-03 13:17:48 -05002266}
2267
2268static unsigned int xgbe_get_rx_fifo_size(struct xgbe_prv_data *pdata)
2269{
Lendacky, Thomas586e3cfb2016-11-03 13:17:48 -05002270 /* The configured value may not be the actual amount of fifo RAM */
Lendacky, Thomasbd8255d2016-11-03 13:19:27 -05002271 return min_t(unsigned int, pdata->rx_max_fifo_size,
2272 pdata->hw_feat.rx_fifo_size);
Lendacky, Thomas586e3cfb2016-11-03 13:17:48 -05002273}
2274
2275static void xgbe_calculate_equal_fifo(unsigned int fifo_size,
2276 unsigned int queue_count,
2277 unsigned int *fifo)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002278{
Lendacky, Thomas9c439e42015-09-30 08:53:03 -05002279 unsigned int q_fifo_size;
2280 unsigned int p_fifo;
Lendacky, Thomas586e3cfb2016-11-03 13:17:48 -05002281 unsigned int i;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002282
Lendacky, Thomas586e3cfb2016-11-03 13:17:48 -05002283 q_fifo_size = fifo_size / queue_count;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002284
Lendacky, Thomas43e0dcf2016-11-03 13:18:16 -05002285 /* Calculate the fifo setting by dividing the queue's fifo size
2286 * by the fifo allocation increment (with 0 representing the
2287 * base allocation increment so decrement the result by 1).
Lendacky, Thomas9c439e42015-09-30 08:53:03 -05002288 */
Lendacky, Thomas43e0dcf2016-11-03 13:18:16 -05002289 p_fifo = q_fifo_size / XGMAC_FIFO_UNIT;
Lendacky, Thomas9c439e42015-09-30 08:53:03 -05002290 if (p_fifo)
2291 p_fifo--;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002292
Lendacky, Thomas43e0dcf2016-11-03 13:18:16 -05002293 /* Distribute the fifo equally amongst the queues */
Lendacky, Thomas586e3cfb2016-11-03 13:17:48 -05002294 for (i = 0; i < queue_count; i++)
2295 fifo[i] = p_fifo;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002296}
2297
Lendacky, Thomas43e0dcf2016-11-03 13:18:16 -05002298static unsigned int xgbe_set_nonprio_fifos(unsigned int fifo_size,
2299 unsigned int queue_count,
2300 unsigned int *fifo)
2301{
2302 unsigned int i;
2303
2304 BUILD_BUG_ON_NOT_POWER_OF_2(XGMAC_FIFO_MIN_ALLOC);
2305
2306 if (queue_count <= IEEE_8021QAZ_MAX_TCS)
2307 return fifo_size;
2308
2309 /* Rx queues 9 and up are for specialized packets,
2310 * such as PTP or DCB control packets, etc. and
2311 * don't require a large fifo
2312 */
2313 for (i = IEEE_8021QAZ_MAX_TCS; i < queue_count; i++) {
2314 fifo[i] = (XGMAC_FIFO_MIN_ALLOC / XGMAC_FIFO_UNIT) - 1;
2315 fifo_size -= XGMAC_FIFO_MIN_ALLOC;
2316 }
2317
2318 return fifo_size;
2319}
2320
2321static unsigned int xgbe_get_pfc_delay(struct xgbe_prv_data *pdata)
2322{
2323 unsigned int delay;
2324
2325 /* If a delay has been provided, use that */
2326 if (pdata->pfc->delay)
2327 return pdata->pfc->delay / 8;
2328
2329 /* Allow for two maximum size frames */
2330 delay = xgbe_get_max_frame(pdata);
2331 delay += XGMAC_ETH_PREAMBLE;
2332 delay *= 2;
2333
2334 /* Allow for PFC frame */
2335 delay += XGMAC_PFC_DATA_LEN;
2336 delay += ETH_HLEN + ETH_FCS_LEN;
2337 delay += XGMAC_ETH_PREAMBLE;
2338
2339 /* Allow for miscellaneous delays (LPI exit, cable, etc.) */
2340 delay += XGMAC_PFC_DELAYS;
2341
2342 return delay;
2343}
2344
2345static unsigned int xgbe_get_pfc_queues(struct xgbe_prv_data *pdata)
2346{
2347 unsigned int count, prio_queues;
2348 unsigned int i;
2349
2350 if (!pdata->pfc->pfc_en)
2351 return 0;
2352
2353 count = 0;
2354 prio_queues = XGMAC_PRIO_QUEUES(pdata->rx_q_count);
2355 for (i = 0; i < prio_queues; i++) {
2356 if (!xgbe_is_pfc_queue(pdata, i))
2357 continue;
2358
2359 pdata->pfcq[i] = 1;
2360 count++;
2361 }
2362
2363 return count;
2364}
2365
2366static void xgbe_calculate_dcb_fifo(struct xgbe_prv_data *pdata,
2367 unsigned int fifo_size,
2368 unsigned int *fifo)
2369{
2370 unsigned int q_fifo_size, rem_fifo, addn_fifo;
2371 unsigned int prio_queues;
2372 unsigned int pfc_count;
2373 unsigned int i;
2374
2375 q_fifo_size = XGMAC_FIFO_ALIGN(xgbe_get_max_frame(pdata));
2376 prio_queues = XGMAC_PRIO_QUEUES(pdata->rx_q_count);
2377 pfc_count = xgbe_get_pfc_queues(pdata);
2378
2379 if (!pfc_count || ((q_fifo_size * prio_queues) > fifo_size)) {
2380 /* No traffic classes with PFC enabled or can't do lossless */
2381 xgbe_calculate_equal_fifo(fifo_size, prio_queues, fifo);
2382 return;
2383 }
2384
2385 /* Calculate how much fifo we have to play with */
2386 rem_fifo = fifo_size - (q_fifo_size * prio_queues);
2387
2388 /* Calculate how much more than base fifo PFC needs, which also
2389 * becomes the threshold activation point (RFA)
2390 */
2391 pdata->pfc_rfa = xgbe_get_pfc_delay(pdata);
2392 pdata->pfc_rfa = XGMAC_FLOW_CONTROL_ALIGN(pdata->pfc_rfa);
2393
2394 if (pdata->pfc_rfa > q_fifo_size) {
2395 addn_fifo = pdata->pfc_rfa - q_fifo_size;
2396 addn_fifo = XGMAC_FIFO_ALIGN(addn_fifo);
2397 } else {
2398 addn_fifo = 0;
2399 }
2400
2401 /* Calculate DCB fifo settings:
2402 * - distribute remaining fifo between the VLAN priority
2403 * queues based on traffic class PFC enablement and overall
2404 * priority (0 is lowest priority, so start at highest)
2405 */
2406 i = prio_queues;
2407 while (i > 0) {
2408 i--;
2409
2410 fifo[i] = (q_fifo_size / XGMAC_FIFO_UNIT) - 1;
2411
2412 if (!pdata->pfcq[i] || !addn_fifo)
2413 continue;
2414
2415 if (addn_fifo > rem_fifo) {
2416 netdev_warn(pdata->netdev,
2417 "RXq%u cannot set needed fifo size\n", i);
2418 if (!rem_fifo)
2419 continue;
2420
2421 addn_fifo = rem_fifo;
2422 }
2423
2424 fifo[i] += (addn_fifo / XGMAC_FIFO_UNIT);
2425 rem_fifo -= addn_fifo;
2426 }
2427
2428 if (rem_fifo) {
2429 unsigned int inc_fifo = rem_fifo / prio_queues;
2430
2431 /* Distribute remaining fifo across queues */
2432 for (i = 0; i < prio_queues; i++)
2433 fifo[i] += (inc_fifo / XGMAC_FIFO_UNIT);
2434 }
2435}
2436
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002437static void xgbe_config_tx_fifo_size(struct xgbe_prv_data *pdata)
2438{
Lendacky, Thomas9c439e42015-09-30 08:53:03 -05002439 unsigned int fifo_size;
Lendacky, Thomas586e3cfb2016-11-03 13:17:48 -05002440 unsigned int fifo[XGBE_MAX_QUEUES];
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002441 unsigned int i;
2442
Lendacky, Thomas586e3cfb2016-11-03 13:17:48 -05002443 fifo_size = xgbe_get_tx_fifo_size(pdata);
2444
2445 xgbe_calculate_equal_fifo(fifo_size, pdata->tx_q_count, fifo);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002446
Lendacky, Thomas853eb162014-07-29 08:57:31 -05002447 for (i = 0; i < pdata->tx_q_count; i++)
Lendacky, Thomas586e3cfb2016-11-03 13:17:48 -05002448 XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_Q_TQOMR, TQS, fifo[i]);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002449
Lendacky, Thomas34bf65d2015-05-14 11:44:03 -05002450 netif_info(pdata, drv, pdata->netdev,
2451 "%d Tx hardware queues, %d byte fifo per queue\n",
Lendacky, Thomas43e0dcf2016-11-03 13:18:16 -05002452 pdata->tx_q_count, ((fifo[0] + 1) * XGMAC_FIFO_UNIT));
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002453}
2454
2455static void xgbe_config_rx_fifo_size(struct xgbe_prv_data *pdata)
2456{
Lendacky, Thomas9c439e42015-09-30 08:53:03 -05002457 unsigned int fifo_size;
Lendacky, Thomas586e3cfb2016-11-03 13:17:48 -05002458 unsigned int fifo[XGBE_MAX_QUEUES];
Lendacky, Thomas43e0dcf2016-11-03 13:18:16 -05002459 unsigned int prio_queues;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002460 unsigned int i;
2461
Lendacky, Thomas43e0dcf2016-11-03 13:18:16 -05002462 /* Clear any DCB related fifo/queue information */
2463 memset(pdata->pfcq, 0, sizeof(pdata->pfcq));
2464 pdata->pfc_rfa = 0;
Lendacky, Thomas586e3cfb2016-11-03 13:17:48 -05002465
Lendacky, Thomas43e0dcf2016-11-03 13:18:16 -05002466 fifo_size = xgbe_get_rx_fifo_size(pdata);
2467 prio_queues = XGMAC_PRIO_QUEUES(pdata->rx_q_count);
2468
2469 /* Assign a minimum fifo to the non-VLAN priority queues */
2470 fifo_size = xgbe_set_nonprio_fifos(fifo_size, pdata->rx_q_count, fifo);
2471
2472 if (pdata->pfc && pdata->ets)
2473 xgbe_calculate_dcb_fifo(pdata, fifo_size, fifo);
2474 else
2475 xgbe_calculate_equal_fifo(fifo_size, prio_queues, fifo);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002476
Lendacky, Thomas853eb162014-07-29 08:57:31 -05002477 for (i = 0; i < pdata->rx_q_count; i++)
Lendacky, Thomas586e3cfb2016-11-03 13:17:48 -05002478 XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_Q_RQOMR, RQS, fifo[i]);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002479
Lendacky, Thomas43e0dcf2016-11-03 13:18:16 -05002480 xgbe_calculate_flow_control_threshold(pdata, fifo);
2481 xgbe_config_flow_control_threshold(pdata);
2482
2483 if (pdata->pfc && pdata->ets && pdata->pfc->pfc_en) {
2484 netif_info(pdata, drv, pdata->netdev,
2485 "%u Rx hardware queues\n", pdata->rx_q_count);
2486 for (i = 0; i < pdata->rx_q_count; i++)
2487 netif_info(pdata, drv, pdata->netdev,
2488 "RxQ%u, %u byte fifo queue\n", i,
2489 ((fifo[i] + 1) * XGMAC_FIFO_UNIT));
2490 } else {
2491 netif_info(pdata, drv, pdata->netdev,
2492 "%u Rx hardware queues, %u byte fifo per queue\n",
2493 pdata->rx_q_count,
2494 ((fifo[0] + 1) * XGMAC_FIFO_UNIT));
2495 }
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002496}
2497
Lendacky, Thomasfca2d992014-07-29 08:57:55 -05002498static void xgbe_config_queue_mapping(struct xgbe_prv_data *pdata)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002499{
Lendacky, Thomasfca2d992014-07-29 08:57:55 -05002500 unsigned int qptc, qptc_extra, queue;
2501 unsigned int prio_queues;
2502 unsigned int ppq, ppq_extra, prio;
2503 unsigned int mask;
2504 unsigned int i, j, reg, reg_val;
2505
2506 /* Map the MTL Tx Queues to Traffic Classes
2507 * Note: Tx Queues >= Traffic Classes
2508 */
2509 qptc = pdata->tx_q_count / pdata->hw_feat.tc_cnt;
2510 qptc_extra = pdata->tx_q_count % pdata->hw_feat.tc_cnt;
2511
2512 for (i = 0, queue = 0; i < pdata->hw_feat.tc_cnt; i++) {
2513 for (j = 0; j < qptc; j++) {
Lendacky, Thomas34bf65d2015-05-14 11:44:03 -05002514 netif_dbg(pdata, drv, pdata->netdev,
2515 "TXq%u mapped to TC%u\n", queue, i);
Lendacky, Thomasfca2d992014-07-29 08:57:55 -05002516 XGMAC_MTL_IOWRITE_BITS(pdata, queue, MTL_Q_TQOMR,
2517 Q2TCMAP, i);
2518 pdata->q2tc_map[queue++] = i;
2519 }
2520
2521 if (i < qptc_extra) {
Lendacky, Thomas34bf65d2015-05-14 11:44:03 -05002522 netif_dbg(pdata, drv, pdata->netdev,
2523 "TXq%u mapped to TC%u\n", queue, i);
Lendacky, Thomasfca2d992014-07-29 08:57:55 -05002524 XGMAC_MTL_IOWRITE_BITS(pdata, queue, MTL_Q_TQOMR,
2525 Q2TCMAP, i);
2526 pdata->q2tc_map[queue++] = i;
2527 }
2528 }
2529
2530 /* Map the 8 VLAN priority values to available MTL Rx queues */
Lendacky, Thomas43e0dcf2016-11-03 13:18:16 -05002531 prio_queues = XGMAC_PRIO_QUEUES(pdata->rx_q_count);
Lendacky, Thomasfca2d992014-07-29 08:57:55 -05002532 ppq = IEEE_8021QAZ_MAX_TCS / prio_queues;
2533 ppq_extra = IEEE_8021QAZ_MAX_TCS % prio_queues;
2534
2535 reg = MAC_RQC2R;
2536 reg_val = 0;
2537 for (i = 0, prio = 0; i < prio_queues;) {
2538 mask = 0;
2539 for (j = 0; j < ppq; j++) {
Lendacky, Thomas34bf65d2015-05-14 11:44:03 -05002540 netif_dbg(pdata, drv, pdata->netdev,
2541 "PRIO%u mapped to RXq%u\n", prio, i);
Lendacky, Thomasfca2d992014-07-29 08:57:55 -05002542 mask |= (1 << prio);
2543 pdata->prio2q_map[prio++] = i;
2544 }
2545
2546 if (i < ppq_extra) {
Lendacky, Thomas34bf65d2015-05-14 11:44:03 -05002547 netif_dbg(pdata, drv, pdata->netdev,
2548 "PRIO%u mapped to RXq%u\n", prio, i);
Lendacky, Thomasfca2d992014-07-29 08:57:55 -05002549 mask |= (1 << prio);
2550 pdata->prio2q_map[prio++] = i;
2551 }
2552
2553 reg_val |= (mask << ((i++ % MAC_RQC2_Q_PER_REG) << 3));
2554
2555 if ((i % MAC_RQC2_Q_PER_REG) && (i != prio_queues))
2556 continue;
2557
2558 XGMAC_IOWRITE(pdata, reg, reg_val);
2559 reg += MAC_RQC2_INC;
2560 reg_val = 0;
2561 }
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002562
2563 /* Select dynamic mapping of MTL Rx queue to DMA Rx channel */
2564 reg = MTL_RQDCM0R;
2565 reg_val = 0;
Lendacky, Thomasfca2d992014-07-29 08:57:55 -05002566 for (i = 0; i < pdata->rx_q_count;) {
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002567 reg_val |= (0x80 << ((i++ % MTL_RQDCM_Q_PER_REG) << 3));
2568
Lendacky, Thomasfca2d992014-07-29 08:57:55 -05002569 if ((i % MTL_RQDCM_Q_PER_REG) && (i != pdata->rx_q_count))
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002570 continue;
2571
2572 XGMAC_IOWRITE(pdata, reg, reg_val);
2573
2574 reg += MTL_RQDCM_INC;
2575 reg_val = 0;
2576 }
2577}
2578
Lendacky, Thomas43e0dcf2016-11-03 13:18:16 -05002579static void xgbe_config_tc(struct xgbe_prv_data *pdata)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002580{
Lendacky, Thomas43e0dcf2016-11-03 13:18:16 -05002581 unsigned int offset, queue, prio;
2582 u8 i;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002583
Lendacky, Thomas43e0dcf2016-11-03 13:18:16 -05002584 netdev_reset_tc(pdata->netdev);
2585 if (!pdata->num_tcs)
2586 return;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002587
Lendacky, Thomas43e0dcf2016-11-03 13:18:16 -05002588 netdev_set_num_tc(pdata->netdev, pdata->num_tcs);
2589
2590 for (i = 0, queue = 0, offset = 0; i < pdata->num_tcs; i++) {
2591 while ((queue < pdata->tx_q_count) &&
2592 (pdata->q2tc_map[queue] == i))
2593 queue++;
2594
2595 netif_dbg(pdata, drv, pdata->netdev, "TC%u using TXq%u-%u\n",
2596 i, offset, queue - 1);
2597 netdev_set_tc_queue(pdata->netdev, i, queue - offset, offset);
2598 offset = queue;
2599 }
2600
2601 if (!pdata->ets)
2602 return;
2603
2604 for (prio = 0; prio < IEEE_8021QAZ_MAX_TCS; prio++)
2605 netdev_set_prio_tc_map(pdata->netdev, prio,
2606 pdata->ets->prio_tc[prio]);
2607}
2608
2609static void xgbe_config_dcb_tc(struct xgbe_prv_data *pdata)
2610{
2611 struct ieee_ets *ets = pdata->ets;
2612 unsigned int total_weight, min_weight, weight;
2613 unsigned int mask, reg, reg_val;
2614 unsigned int i, prio;
2615
2616 if (!ets)
2617 return;
2618
2619 /* Set Tx to deficit weighted round robin scheduling algorithm (when
2620 * traffic class is using ETS algorithm)
2621 */
2622 XGMAC_IOWRITE_BITS(pdata, MTL_OMR, ETSALG, MTL_ETSALG_DWRR);
2623
2624 /* Set Traffic Class algorithms */
2625 total_weight = pdata->netdev->mtu * pdata->hw_feat.tc_cnt;
2626 min_weight = total_weight / 100;
2627 if (!min_weight)
2628 min_weight = 1;
2629
2630 for (i = 0; i < pdata->hw_feat.tc_cnt; i++) {
2631 /* Map the priorities to the traffic class */
2632 mask = 0;
2633 for (prio = 0; prio < IEEE_8021QAZ_MAX_TCS; prio++) {
2634 if (ets->prio_tc[prio] == i)
2635 mask |= (1 << prio);
2636 }
2637 mask &= 0xff;
2638
2639 netif_dbg(pdata, drv, pdata->netdev, "TC%u PRIO mask=%#x\n",
2640 i, mask);
2641 reg = MTL_TCPM0R + (MTL_TCPM_INC * (i / MTL_TCPM_TC_PER_REG));
2642 reg_val = XGMAC_IOREAD(pdata, reg);
2643
2644 reg_val &= ~(0xff << ((i % MTL_TCPM_TC_PER_REG) << 3));
2645 reg_val |= (mask << ((i % MTL_TCPM_TC_PER_REG) << 3));
2646
2647 XGMAC_IOWRITE(pdata, reg, reg_val);
2648
2649 /* Set the traffic class algorithm */
2650 switch (ets->tc_tsa[i]) {
2651 case IEEE_8021QAZ_TSA_STRICT:
2652 netif_dbg(pdata, drv, pdata->netdev,
2653 "TC%u using SP\n", i);
2654 XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_TC_ETSCR, TSA,
2655 MTL_TSA_SP);
2656 break;
2657 case IEEE_8021QAZ_TSA_ETS:
2658 weight = total_weight * ets->tc_tx_bw[i] / 100;
2659 weight = clamp(weight, min_weight, total_weight);
2660
2661 netif_dbg(pdata, drv, pdata->netdev,
2662 "TC%u using DWRR (weight %u)\n", i, weight);
2663 XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_TC_ETSCR, TSA,
2664 MTL_TSA_ETS);
2665 XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_TC_QWR, QW,
2666 weight);
2667 break;
2668 }
2669 }
2670
2671 xgbe_config_tc(pdata);
2672}
2673
2674static void xgbe_config_dcb_pfc(struct xgbe_prv_data *pdata)
2675{
2676 if (!test_bit(XGBE_DOWN, &pdata->dev_state)) {
2677 /* Just stop the Tx queues while Rx fifo is changed */
2678 netif_tx_stop_all_queues(pdata->netdev);
2679
2680 /* Suspend Rx so that fifo's can be adjusted */
2681 pdata->hw_if.disable_rx(pdata);
2682 }
2683
2684 xgbe_config_rx_fifo_size(pdata);
2685 xgbe_config_flow_control(pdata);
2686
2687 if (!test_bit(XGBE_DOWN, &pdata->dev_state)) {
2688 /* Resume Rx */
2689 pdata->hw_if.enable_rx(pdata);
2690
2691 /* Resume Tx queues */
2692 netif_tx_start_all_queues(pdata->netdev);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002693 }
2694}
2695
2696static void xgbe_config_mac_address(struct xgbe_prv_data *pdata)
2697{
2698 xgbe_set_mac_address(pdata, pdata->netdev->dev_addr);
Lendacky, Thomasb85e4d82014-06-24 16:19:29 -05002699
2700 /* Filtering is done using perfect filtering and hash filtering */
2701 if (pdata->hw_feat.hash_table_size) {
2702 XGMAC_IOWRITE_BITS(pdata, MAC_PFR, HPF, 1);
2703 XGMAC_IOWRITE_BITS(pdata, MAC_PFR, HUC, 1);
2704 XGMAC_IOWRITE_BITS(pdata, MAC_PFR, HMC, 1);
2705 }
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002706}
2707
2708static void xgbe_config_jumbo_enable(struct xgbe_prv_data *pdata)
2709{
2710 unsigned int val;
2711
2712 val = (pdata->netdev->mtu > XGMAC_STD_PACKET_MTU) ? 1 : 0;
2713
2714 XGMAC_IOWRITE_BITS(pdata, MAC_RCR, JE, val);
2715}
2716
Lendacky, Thomas916102c2015-01-16 12:46:45 -06002717static void xgbe_config_mac_speed(struct xgbe_prv_data *pdata)
2718{
Lendacky, Thomase57f7a32016-11-03 13:18:27 -05002719 xgbe_set_speed(pdata, pdata->phy_speed);
Lendacky, Thomas916102c2015-01-16 12:46:45 -06002720}
2721
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002722static void xgbe_config_checksum_offload(struct xgbe_prv_data *pdata)
2723{
2724 if (pdata->netdev->features & NETIF_F_RXCSUM)
2725 xgbe_enable_rx_csum(pdata);
2726 else
2727 xgbe_disable_rx_csum(pdata);
2728}
2729
2730static void xgbe_config_vlan_support(struct xgbe_prv_data *pdata)
2731{
Lendacky, Thomas6e5eed02014-06-24 16:19:12 -05002732 /* Indicate that VLAN Tx CTAGs come from context descriptors */
2733 XGMAC_IOWRITE_BITS(pdata, MAC_VLANIR, CSVL, 0);
2734 XGMAC_IOWRITE_BITS(pdata, MAC_VLANIR, VLTI, 1);
2735
Lendacky, Thomas801c62d2014-06-24 16:19:24 -05002736 /* Set the current VLAN Hash Table register value */
2737 xgbe_update_vlan_hash_table(pdata);
2738
2739 if (pdata->netdev->features & NETIF_F_HW_VLAN_CTAG_FILTER)
2740 xgbe_enable_rx_vlan_filtering(pdata);
2741 else
2742 xgbe_disable_rx_vlan_filtering(pdata);
2743
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002744 if (pdata->netdev->features & NETIF_F_HW_VLAN_CTAG_RX)
2745 xgbe_enable_rx_vlan_stripping(pdata);
2746 else
2747 xgbe_disable_rx_vlan_stripping(pdata);
2748}
2749
Lendacky, Thomas60265102014-09-05 18:02:30 -05002750static u64 xgbe_mmc_read(struct xgbe_prv_data *pdata, unsigned int reg_lo)
2751{
2752 bool read_hi;
2753 u64 val;
2754
Lendacky, Thomase5a20b92016-11-03 13:19:07 -05002755 if (pdata->vdata->mmc_64bit) {
2756 switch (reg_lo) {
2757 /* These registers are always 32 bit */
2758 case MMC_RXRUNTERROR:
2759 case MMC_RXJABBERERROR:
2760 case MMC_RXUNDERSIZE_G:
2761 case MMC_RXOVERSIZE_G:
2762 case MMC_RXWATCHDOGERROR:
2763 read_hi = false;
2764 break;
Lendacky, Thomas60265102014-09-05 18:02:30 -05002765
Lendacky, Thomase5a20b92016-11-03 13:19:07 -05002766 default:
2767 read_hi = true;
2768 }
2769 } else {
2770 switch (reg_lo) {
2771 /* These registers are always 64 bit */
2772 case MMC_TXOCTETCOUNT_GB_LO:
2773 case MMC_TXOCTETCOUNT_G_LO:
2774 case MMC_RXOCTETCOUNT_GB_LO:
2775 case MMC_RXOCTETCOUNT_G_LO:
2776 read_hi = true;
2777 break;
2778
2779 default:
2780 read_hi = false;
2781 }
Lendacky, Thomas3947d782015-09-30 08:52:38 -05002782 }
Lendacky, Thomas60265102014-09-05 18:02:30 -05002783
2784 val = XGMAC_IOREAD(pdata, reg_lo);
2785
2786 if (read_hi)
2787 val |= ((u64)XGMAC_IOREAD(pdata, reg_lo + 4) << 32);
2788
2789 return val;
2790}
2791
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002792static void xgbe_tx_mmc_int(struct xgbe_prv_data *pdata)
2793{
2794 struct xgbe_mmc_stats *stats = &pdata->mmc_stats;
2795 unsigned int mmc_isr = XGMAC_IOREAD(pdata, MMC_TISR);
2796
2797 if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TXOCTETCOUNT_GB))
2798 stats->txoctetcount_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002799 xgbe_mmc_read(pdata, MMC_TXOCTETCOUNT_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002800
2801 if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TXFRAMECOUNT_GB))
2802 stats->txframecount_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002803 xgbe_mmc_read(pdata, MMC_TXFRAMECOUNT_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002804
2805 if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TXBROADCASTFRAMES_G))
2806 stats->txbroadcastframes_g +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002807 xgbe_mmc_read(pdata, MMC_TXBROADCASTFRAMES_G_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002808
2809 if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TXMULTICASTFRAMES_G))
2810 stats->txmulticastframes_g +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002811 xgbe_mmc_read(pdata, MMC_TXMULTICASTFRAMES_G_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002812
2813 if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TX64OCTETS_GB))
2814 stats->tx64octets_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002815 xgbe_mmc_read(pdata, MMC_TX64OCTETS_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002816
2817 if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TX65TO127OCTETS_GB))
2818 stats->tx65to127octets_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002819 xgbe_mmc_read(pdata, MMC_TX65TO127OCTETS_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002820
2821 if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TX128TO255OCTETS_GB))
2822 stats->tx128to255octets_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002823 xgbe_mmc_read(pdata, MMC_TX128TO255OCTETS_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002824
2825 if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TX256TO511OCTETS_GB))
2826 stats->tx256to511octets_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002827 xgbe_mmc_read(pdata, MMC_TX256TO511OCTETS_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002828
2829 if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TX512TO1023OCTETS_GB))
2830 stats->tx512to1023octets_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002831 xgbe_mmc_read(pdata, MMC_TX512TO1023OCTETS_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002832
2833 if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TX1024TOMAXOCTETS_GB))
2834 stats->tx1024tomaxoctets_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002835 xgbe_mmc_read(pdata, MMC_TX1024TOMAXOCTETS_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002836
2837 if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TXUNICASTFRAMES_GB))
2838 stats->txunicastframes_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002839 xgbe_mmc_read(pdata, MMC_TXUNICASTFRAMES_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002840
2841 if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TXMULTICASTFRAMES_GB))
2842 stats->txmulticastframes_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002843 xgbe_mmc_read(pdata, MMC_TXMULTICASTFRAMES_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002844
2845 if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TXBROADCASTFRAMES_GB))
2846 stats->txbroadcastframes_g +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002847 xgbe_mmc_read(pdata, MMC_TXBROADCASTFRAMES_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002848
2849 if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TXUNDERFLOWERROR))
2850 stats->txunderflowerror +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002851 xgbe_mmc_read(pdata, MMC_TXUNDERFLOWERROR_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002852
2853 if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TXOCTETCOUNT_G))
2854 stats->txoctetcount_g +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002855 xgbe_mmc_read(pdata, MMC_TXOCTETCOUNT_G_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002856
2857 if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TXFRAMECOUNT_G))
2858 stats->txframecount_g +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002859 xgbe_mmc_read(pdata, MMC_TXFRAMECOUNT_G_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002860
2861 if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TXPAUSEFRAMES))
2862 stats->txpauseframes +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002863 xgbe_mmc_read(pdata, MMC_TXPAUSEFRAMES_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002864
2865 if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TXVLANFRAMES_G))
2866 stats->txvlanframes_g +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002867 xgbe_mmc_read(pdata, MMC_TXVLANFRAMES_G_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002868}
2869
2870static void xgbe_rx_mmc_int(struct xgbe_prv_data *pdata)
2871{
2872 struct xgbe_mmc_stats *stats = &pdata->mmc_stats;
2873 unsigned int mmc_isr = XGMAC_IOREAD(pdata, MMC_RISR);
2874
2875 if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXFRAMECOUNT_GB))
2876 stats->rxframecount_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002877 xgbe_mmc_read(pdata, MMC_RXFRAMECOUNT_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002878
2879 if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXOCTETCOUNT_GB))
2880 stats->rxoctetcount_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002881 xgbe_mmc_read(pdata, MMC_RXOCTETCOUNT_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002882
2883 if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXOCTETCOUNT_G))
2884 stats->rxoctetcount_g +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002885 xgbe_mmc_read(pdata, MMC_RXOCTETCOUNT_G_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002886
2887 if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXBROADCASTFRAMES_G))
2888 stats->rxbroadcastframes_g +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002889 xgbe_mmc_read(pdata, MMC_RXBROADCASTFRAMES_G_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002890
2891 if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXMULTICASTFRAMES_G))
2892 stats->rxmulticastframes_g +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002893 xgbe_mmc_read(pdata, MMC_RXMULTICASTFRAMES_G_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002894
2895 if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXCRCERROR))
2896 stats->rxcrcerror +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002897 xgbe_mmc_read(pdata, MMC_RXCRCERROR_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002898
2899 if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXRUNTERROR))
2900 stats->rxrunterror +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002901 xgbe_mmc_read(pdata, MMC_RXRUNTERROR);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002902
2903 if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXJABBERERROR))
2904 stats->rxjabbererror +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002905 xgbe_mmc_read(pdata, MMC_RXJABBERERROR);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002906
2907 if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXUNDERSIZE_G))
2908 stats->rxundersize_g +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002909 xgbe_mmc_read(pdata, MMC_RXUNDERSIZE_G);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002910
2911 if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXOVERSIZE_G))
2912 stats->rxoversize_g +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002913 xgbe_mmc_read(pdata, MMC_RXOVERSIZE_G);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002914
2915 if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RX64OCTETS_GB))
2916 stats->rx64octets_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002917 xgbe_mmc_read(pdata, MMC_RX64OCTETS_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002918
2919 if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RX65TO127OCTETS_GB))
2920 stats->rx65to127octets_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002921 xgbe_mmc_read(pdata, MMC_RX65TO127OCTETS_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002922
2923 if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RX128TO255OCTETS_GB))
2924 stats->rx128to255octets_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002925 xgbe_mmc_read(pdata, MMC_RX128TO255OCTETS_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002926
2927 if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RX256TO511OCTETS_GB))
2928 stats->rx256to511octets_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002929 xgbe_mmc_read(pdata, MMC_RX256TO511OCTETS_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002930
2931 if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RX512TO1023OCTETS_GB))
2932 stats->rx512to1023octets_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002933 xgbe_mmc_read(pdata, MMC_RX512TO1023OCTETS_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002934
2935 if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RX1024TOMAXOCTETS_GB))
2936 stats->rx1024tomaxoctets_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002937 xgbe_mmc_read(pdata, MMC_RX1024TOMAXOCTETS_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002938
2939 if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXUNICASTFRAMES_G))
2940 stats->rxunicastframes_g +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002941 xgbe_mmc_read(pdata, MMC_RXUNICASTFRAMES_G_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002942
2943 if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXLENGTHERROR))
2944 stats->rxlengtherror +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002945 xgbe_mmc_read(pdata, MMC_RXLENGTHERROR_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002946
2947 if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXOUTOFRANGETYPE))
2948 stats->rxoutofrangetype +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002949 xgbe_mmc_read(pdata, MMC_RXOUTOFRANGETYPE_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002950
2951 if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXPAUSEFRAMES))
2952 stats->rxpauseframes +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002953 xgbe_mmc_read(pdata, MMC_RXPAUSEFRAMES_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002954
2955 if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXFIFOOVERFLOW))
2956 stats->rxfifooverflow +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002957 xgbe_mmc_read(pdata, MMC_RXFIFOOVERFLOW_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002958
2959 if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXVLANFRAMES_GB))
2960 stats->rxvlanframes_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002961 xgbe_mmc_read(pdata, MMC_RXVLANFRAMES_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002962
2963 if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXWATCHDOGERROR))
2964 stats->rxwatchdogerror +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002965 xgbe_mmc_read(pdata, MMC_RXWATCHDOGERROR);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002966}
2967
2968static void xgbe_read_mmc_stats(struct xgbe_prv_data *pdata)
2969{
2970 struct xgbe_mmc_stats *stats = &pdata->mmc_stats;
2971
2972 /* Freeze counters */
2973 XGMAC_IOWRITE_BITS(pdata, MMC_CR, MCF, 1);
2974
2975 stats->txoctetcount_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002976 xgbe_mmc_read(pdata, MMC_TXOCTETCOUNT_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002977
2978 stats->txframecount_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002979 xgbe_mmc_read(pdata, MMC_TXFRAMECOUNT_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002980
2981 stats->txbroadcastframes_g +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002982 xgbe_mmc_read(pdata, MMC_TXBROADCASTFRAMES_G_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002983
2984 stats->txmulticastframes_g +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002985 xgbe_mmc_read(pdata, MMC_TXMULTICASTFRAMES_G_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002986
2987 stats->tx64octets_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002988 xgbe_mmc_read(pdata, MMC_TX64OCTETS_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002989
2990 stats->tx65to127octets_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002991 xgbe_mmc_read(pdata, MMC_TX65TO127OCTETS_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002992
2993 stats->tx128to255octets_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002994 xgbe_mmc_read(pdata, MMC_TX128TO255OCTETS_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002995
2996 stats->tx256to511octets_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002997 xgbe_mmc_read(pdata, MMC_TX256TO511OCTETS_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002998
2999 stats->tx512to1023octets_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05003000 xgbe_mmc_read(pdata, MMC_TX512TO1023OCTETS_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003001
3002 stats->tx1024tomaxoctets_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05003003 xgbe_mmc_read(pdata, MMC_TX1024TOMAXOCTETS_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003004
3005 stats->txunicastframes_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05003006 xgbe_mmc_read(pdata, MMC_TXUNICASTFRAMES_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003007
3008 stats->txmulticastframes_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05003009 xgbe_mmc_read(pdata, MMC_TXMULTICASTFRAMES_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003010
3011 stats->txbroadcastframes_g +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05003012 xgbe_mmc_read(pdata, MMC_TXBROADCASTFRAMES_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003013
3014 stats->txunderflowerror +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05003015 xgbe_mmc_read(pdata, MMC_TXUNDERFLOWERROR_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003016
3017 stats->txoctetcount_g +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05003018 xgbe_mmc_read(pdata, MMC_TXOCTETCOUNT_G_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003019
3020 stats->txframecount_g +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05003021 xgbe_mmc_read(pdata, MMC_TXFRAMECOUNT_G_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003022
3023 stats->txpauseframes +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05003024 xgbe_mmc_read(pdata, MMC_TXPAUSEFRAMES_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003025
3026 stats->txvlanframes_g +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05003027 xgbe_mmc_read(pdata, MMC_TXVLANFRAMES_G_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003028
3029 stats->rxframecount_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05003030 xgbe_mmc_read(pdata, MMC_RXFRAMECOUNT_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003031
3032 stats->rxoctetcount_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05003033 xgbe_mmc_read(pdata, MMC_RXOCTETCOUNT_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003034
3035 stats->rxoctetcount_g +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05003036 xgbe_mmc_read(pdata, MMC_RXOCTETCOUNT_G_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003037
3038 stats->rxbroadcastframes_g +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05003039 xgbe_mmc_read(pdata, MMC_RXBROADCASTFRAMES_G_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003040
3041 stats->rxmulticastframes_g +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05003042 xgbe_mmc_read(pdata, MMC_RXMULTICASTFRAMES_G_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003043
3044 stats->rxcrcerror +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05003045 xgbe_mmc_read(pdata, MMC_RXCRCERROR_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003046
3047 stats->rxrunterror +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05003048 xgbe_mmc_read(pdata, MMC_RXRUNTERROR);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003049
3050 stats->rxjabbererror +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05003051 xgbe_mmc_read(pdata, MMC_RXJABBERERROR);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003052
3053 stats->rxundersize_g +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05003054 xgbe_mmc_read(pdata, MMC_RXUNDERSIZE_G);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003055
3056 stats->rxoversize_g +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05003057 xgbe_mmc_read(pdata, MMC_RXOVERSIZE_G);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003058
3059 stats->rx64octets_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05003060 xgbe_mmc_read(pdata, MMC_RX64OCTETS_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003061
3062 stats->rx65to127octets_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05003063 xgbe_mmc_read(pdata, MMC_RX65TO127OCTETS_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003064
3065 stats->rx128to255octets_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05003066 xgbe_mmc_read(pdata, MMC_RX128TO255OCTETS_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003067
3068 stats->rx256to511octets_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05003069 xgbe_mmc_read(pdata, MMC_RX256TO511OCTETS_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003070
3071 stats->rx512to1023octets_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05003072 xgbe_mmc_read(pdata, MMC_RX512TO1023OCTETS_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003073
3074 stats->rx1024tomaxoctets_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05003075 xgbe_mmc_read(pdata, MMC_RX1024TOMAXOCTETS_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003076
3077 stats->rxunicastframes_g +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05003078 xgbe_mmc_read(pdata, MMC_RXUNICASTFRAMES_G_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003079
3080 stats->rxlengtherror +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05003081 xgbe_mmc_read(pdata, MMC_RXLENGTHERROR_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003082
3083 stats->rxoutofrangetype +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05003084 xgbe_mmc_read(pdata, MMC_RXOUTOFRANGETYPE_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003085
3086 stats->rxpauseframes +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05003087 xgbe_mmc_read(pdata, MMC_RXPAUSEFRAMES_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003088
3089 stats->rxfifooverflow +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05003090 xgbe_mmc_read(pdata, MMC_RXFIFOOVERFLOW_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003091
3092 stats->rxvlanframes_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05003093 xgbe_mmc_read(pdata, MMC_RXVLANFRAMES_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003094
3095 stats->rxwatchdogerror +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05003096 xgbe_mmc_read(pdata, MMC_RXWATCHDOGERROR);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003097
3098 /* Un-freeze counters */
3099 XGMAC_IOWRITE_BITS(pdata, MMC_CR, MCF, 0);
3100}
3101
3102static void xgbe_config_mmc(struct xgbe_prv_data *pdata)
3103{
3104 /* Set counters to reset on read */
3105 XGMAC_IOWRITE_BITS(pdata, MMC_CR, ROR, 1);
3106
3107 /* Reset the counters */
3108 XGMAC_IOWRITE_BITS(pdata, MMC_CR, CR, 1);
3109}
3110
Lendacky, Thomas4b8acdf2016-11-03 13:19:17 -05003111static void xgbe_txq_prepare_tx_stop(struct xgbe_prv_data *pdata,
3112 unsigned int queue)
3113{
3114 unsigned int tx_status;
3115 unsigned long tx_timeout;
3116
3117 /* The Tx engine cannot be stopped if it is actively processing
3118 * packets. Wait for the Tx queue to empty the Tx fifo. Don't
3119 * wait forever though...
3120 */
3121 tx_timeout = jiffies + (XGBE_DMA_STOP_TIMEOUT * HZ);
3122 while (time_before(jiffies, tx_timeout)) {
3123 tx_status = XGMAC_MTL_IOREAD(pdata, queue, MTL_Q_TQDR);
3124 if ((XGMAC_GET_BITS(tx_status, MTL_Q_TQDR, TRCSTS) != 1) &&
3125 (XGMAC_GET_BITS(tx_status, MTL_Q_TQDR, TXQSTS) == 0))
3126 break;
3127
3128 usleep_range(500, 1000);
3129 }
3130
3131 if (!time_before(jiffies, tx_timeout))
3132 netdev_info(pdata->netdev,
3133 "timed out waiting for Tx queue %u to empty\n",
3134 queue);
3135}
3136
Lendacky, Thomas16edd342014-11-20 11:03:32 -06003137static void xgbe_prepare_tx_stop(struct xgbe_prv_data *pdata,
Lendacky, Thomas4b8acdf2016-11-03 13:19:17 -05003138 unsigned int queue)
Lendacky, Thomas16edd342014-11-20 11:03:32 -06003139{
3140 unsigned int tx_dsr, tx_pos, tx_qidx;
3141 unsigned int tx_status;
3142 unsigned long tx_timeout;
3143
Lendacky, Thomas4b8acdf2016-11-03 13:19:17 -05003144 if (XGMAC_GET_BITS(pdata->hw_feat.version, MAC_VR, SNPSVER) > 0x20)
3145 return xgbe_txq_prepare_tx_stop(pdata, queue);
3146
Lendacky, Thomas16edd342014-11-20 11:03:32 -06003147 /* Calculate the status register to read and the position within */
Lendacky, Thomas4b8acdf2016-11-03 13:19:17 -05003148 if (queue < DMA_DSRX_FIRST_QUEUE) {
Lendacky, Thomas16edd342014-11-20 11:03:32 -06003149 tx_dsr = DMA_DSR0;
Lendacky, Thomas4b8acdf2016-11-03 13:19:17 -05003150 tx_pos = (queue * DMA_DSR_Q_WIDTH) + DMA_DSR0_TPS_START;
Lendacky, Thomas16edd342014-11-20 11:03:32 -06003151 } else {
Lendacky, Thomas4b8acdf2016-11-03 13:19:17 -05003152 tx_qidx = queue - DMA_DSRX_FIRST_QUEUE;
Lendacky, Thomas16edd342014-11-20 11:03:32 -06003153
3154 tx_dsr = DMA_DSR1 + ((tx_qidx / DMA_DSRX_QPR) * DMA_DSRX_INC);
3155 tx_pos = ((tx_qidx % DMA_DSRX_QPR) * DMA_DSR_Q_WIDTH) +
3156 DMA_DSRX_TPS_START;
3157 }
3158
3159 /* The Tx engine cannot be stopped if it is actively processing
3160 * descriptors. Wait for the Tx engine to enter the stopped or
3161 * suspended state. Don't wait forever though...
3162 */
3163 tx_timeout = jiffies + (XGBE_DMA_STOP_TIMEOUT * HZ);
3164 while (time_before(jiffies, tx_timeout)) {
3165 tx_status = XGMAC_IOREAD(pdata, tx_dsr);
3166 tx_status = GET_BITS(tx_status, tx_pos, DMA_DSR_TPS_WIDTH);
3167 if ((tx_status == DMA_TPS_STOPPED) ||
3168 (tx_status == DMA_TPS_SUSPENDED))
3169 break;
3170
3171 usleep_range(500, 1000);
3172 }
3173
3174 if (!time_before(jiffies, tx_timeout))
3175 netdev_info(pdata->netdev,
3176 "timed out waiting for Tx DMA channel %u to stop\n",
Lendacky, Thomas4b8acdf2016-11-03 13:19:17 -05003177 queue);
Lendacky, Thomas16edd342014-11-20 11:03:32 -06003178}
3179
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003180static void xgbe_enable_tx(struct xgbe_prv_data *pdata)
3181{
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003182 unsigned int i;
3183
3184 /* Enable each Tx DMA channel */
Lendacky, Thomas18f9f0a2017-06-28 13:42:51 -05003185 for (i = 0; i < pdata->channel_count; i++) {
3186 if (!pdata->channel[i]->tx_ring)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003187 break;
3188
Lendacky, Thomas18f9f0a2017-06-28 13:42:51 -05003189 XGMAC_DMA_IOWRITE_BITS(pdata->channel[i], DMA_CH_TCR, ST, 1);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003190 }
3191
3192 /* Enable each Tx queue */
Lendacky, Thomas853eb162014-07-29 08:57:31 -05003193 for (i = 0; i < pdata->tx_q_count; i++)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003194 XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_Q_TQOMR, TXQEN,
3195 MTL_Q_ENABLED);
3196
3197 /* Enable MAC Tx */
3198 XGMAC_IOWRITE_BITS(pdata, MAC_TCR, TE, 1);
3199}
3200
3201static void xgbe_disable_tx(struct xgbe_prv_data *pdata)
3202{
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003203 unsigned int i;
3204
Lendacky, Thomas16edd342014-11-20 11:03:32 -06003205 /* Prepare for Tx DMA channel stop */
Lendacky, Thomas4b8acdf2016-11-03 13:19:17 -05003206 for (i = 0; i < pdata->tx_q_count; i++)
3207 xgbe_prepare_tx_stop(pdata, i);
Lendacky, Thomas16edd342014-11-20 11:03:32 -06003208
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003209 /* Disable MAC Tx */
3210 XGMAC_IOWRITE_BITS(pdata, MAC_TCR, TE, 0);
3211
3212 /* Disable each Tx queue */
Lendacky, Thomas853eb162014-07-29 08:57:31 -05003213 for (i = 0; i < pdata->tx_q_count; i++)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003214 XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_Q_TQOMR, TXQEN, 0);
3215
3216 /* Disable each Tx DMA channel */
Lendacky, Thomas18f9f0a2017-06-28 13:42:51 -05003217 for (i = 0; i < pdata->channel_count; i++) {
3218 if (!pdata->channel[i]->tx_ring)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003219 break;
3220
Lendacky, Thomas18f9f0a2017-06-28 13:42:51 -05003221 XGMAC_DMA_IOWRITE_BITS(pdata->channel[i], DMA_CH_TCR, ST, 0);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003222 }
3223}
3224
Lendacky, Thomasc3727d62016-02-17 11:49:16 -06003225static void xgbe_prepare_rx_stop(struct xgbe_prv_data *pdata,
3226 unsigned int queue)
3227{
3228 unsigned int rx_status;
3229 unsigned long rx_timeout;
3230
3231 /* The Rx engine cannot be stopped if it is actively processing
3232 * packets. Wait for the Rx queue to empty the Rx fifo. Don't
3233 * wait forever though...
3234 */
3235 rx_timeout = jiffies + (XGBE_DMA_STOP_TIMEOUT * HZ);
3236 while (time_before(jiffies, rx_timeout)) {
3237 rx_status = XGMAC_MTL_IOREAD(pdata, queue, MTL_Q_RQDR);
3238 if ((XGMAC_GET_BITS(rx_status, MTL_Q_RQDR, PRXQ) == 0) &&
3239 (XGMAC_GET_BITS(rx_status, MTL_Q_RQDR, RXQSTS) == 0))
3240 break;
3241
3242 usleep_range(500, 1000);
3243 }
3244
3245 if (!time_before(jiffies, rx_timeout))
3246 netdev_info(pdata->netdev,
3247 "timed out waiting for Rx queue %u to empty\n",
3248 queue);
3249}
3250
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003251static void xgbe_enable_rx(struct xgbe_prv_data *pdata)
3252{
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003253 unsigned int reg_val, i;
3254
3255 /* Enable each Rx DMA channel */
Lendacky, Thomas18f9f0a2017-06-28 13:42:51 -05003256 for (i = 0; i < pdata->channel_count; i++) {
3257 if (!pdata->channel[i]->rx_ring)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003258 break;
3259
Lendacky, Thomas18f9f0a2017-06-28 13:42:51 -05003260 XGMAC_DMA_IOWRITE_BITS(pdata->channel[i], DMA_CH_RCR, SR, 1);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003261 }
3262
3263 /* Enable each Rx queue */
3264 reg_val = 0;
Lendacky, Thomas853eb162014-07-29 08:57:31 -05003265 for (i = 0; i < pdata->rx_q_count; i++)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003266 reg_val |= (0x02 << (i << 1));
3267 XGMAC_IOWRITE(pdata, MAC_RQC0R, reg_val);
3268
3269 /* Enable MAC Rx */
3270 XGMAC_IOWRITE_BITS(pdata, MAC_RCR, DCRCC, 1);
3271 XGMAC_IOWRITE_BITS(pdata, MAC_RCR, CST, 1);
3272 XGMAC_IOWRITE_BITS(pdata, MAC_RCR, ACS, 1);
3273 XGMAC_IOWRITE_BITS(pdata, MAC_RCR, RE, 1);
3274}
3275
3276static void xgbe_disable_rx(struct xgbe_prv_data *pdata)
3277{
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003278 unsigned int i;
3279
3280 /* Disable MAC Rx */
3281 XGMAC_IOWRITE_BITS(pdata, MAC_RCR, DCRCC, 0);
3282 XGMAC_IOWRITE_BITS(pdata, MAC_RCR, CST, 0);
3283 XGMAC_IOWRITE_BITS(pdata, MAC_RCR, ACS, 0);
3284 XGMAC_IOWRITE_BITS(pdata, MAC_RCR, RE, 0);
3285
Lendacky, Thomasc3727d62016-02-17 11:49:16 -06003286 /* Prepare for Rx DMA channel stop */
3287 for (i = 0; i < pdata->rx_q_count; i++)
3288 xgbe_prepare_rx_stop(pdata, i);
3289
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003290 /* Disable each Rx queue */
3291 XGMAC_IOWRITE(pdata, MAC_RQC0R, 0);
3292
3293 /* Disable each Rx DMA channel */
Lendacky, Thomas18f9f0a2017-06-28 13:42:51 -05003294 for (i = 0; i < pdata->channel_count; i++) {
3295 if (!pdata->channel[i]->rx_ring)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003296 break;
3297
Lendacky, Thomas18f9f0a2017-06-28 13:42:51 -05003298 XGMAC_DMA_IOWRITE_BITS(pdata->channel[i], DMA_CH_RCR, SR, 0);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003299 }
3300}
3301
3302static void xgbe_powerup_tx(struct xgbe_prv_data *pdata)
3303{
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003304 unsigned int i;
3305
3306 /* Enable each Tx DMA channel */
Lendacky, Thomas18f9f0a2017-06-28 13:42:51 -05003307 for (i = 0; i < pdata->channel_count; i++) {
3308 if (!pdata->channel[i]->tx_ring)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003309 break;
3310
Lendacky, Thomas18f9f0a2017-06-28 13:42:51 -05003311 XGMAC_DMA_IOWRITE_BITS(pdata->channel[i], DMA_CH_TCR, ST, 1);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003312 }
3313
3314 /* Enable MAC Tx */
3315 XGMAC_IOWRITE_BITS(pdata, MAC_TCR, TE, 1);
3316}
3317
3318static void xgbe_powerdown_tx(struct xgbe_prv_data *pdata)
3319{
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003320 unsigned int i;
3321
Lendacky, Thomas16edd342014-11-20 11:03:32 -06003322 /* Prepare for Tx DMA channel stop */
Lendacky, Thomas4b8acdf2016-11-03 13:19:17 -05003323 for (i = 0; i < pdata->tx_q_count; i++)
3324 xgbe_prepare_tx_stop(pdata, i);
Lendacky, Thomas16edd342014-11-20 11:03:32 -06003325
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003326 /* Disable MAC Tx */
3327 XGMAC_IOWRITE_BITS(pdata, MAC_TCR, TE, 0);
3328
3329 /* Disable each Tx DMA channel */
Lendacky, Thomas18f9f0a2017-06-28 13:42:51 -05003330 for (i = 0; i < pdata->channel_count; i++) {
3331 if (!pdata->channel[i]->tx_ring)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003332 break;
3333
Lendacky, Thomas18f9f0a2017-06-28 13:42:51 -05003334 XGMAC_DMA_IOWRITE_BITS(pdata->channel[i], DMA_CH_TCR, ST, 0);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003335 }
3336}
3337
3338static void xgbe_powerup_rx(struct xgbe_prv_data *pdata)
3339{
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003340 unsigned int i;
3341
3342 /* Enable each Rx DMA channel */
Lendacky, Thomas18f9f0a2017-06-28 13:42:51 -05003343 for (i = 0; i < pdata->channel_count; i++) {
3344 if (!pdata->channel[i]->rx_ring)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003345 break;
3346
Lendacky, Thomas18f9f0a2017-06-28 13:42:51 -05003347 XGMAC_DMA_IOWRITE_BITS(pdata->channel[i], DMA_CH_RCR, SR, 1);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003348 }
3349}
3350
3351static void xgbe_powerdown_rx(struct xgbe_prv_data *pdata)
3352{
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003353 unsigned int i;
3354
3355 /* Disable each Rx DMA channel */
Lendacky, Thomas18f9f0a2017-06-28 13:42:51 -05003356 for (i = 0; i < pdata->channel_count; i++) {
3357 if (!pdata->channel[i]->rx_ring)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003358 break;
3359
Lendacky, Thomas18f9f0a2017-06-28 13:42:51 -05003360 XGMAC_DMA_IOWRITE_BITS(pdata->channel[i], DMA_CH_RCR, SR, 0);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003361 }
3362}
3363
3364static int xgbe_init(struct xgbe_prv_data *pdata)
3365{
3366 struct xgbe_desc_if *desc_if = &pdata->desc_if;
3367 int ret;
3368
3369 DBGPR("-->xgbe_init\n");
3370
3371 /* Flush Tx queues */
3372 ret = xgbe_flush_tx_queues(pdata);
Lendacky, Thomas738f7f62017-01-20 12:14:13 -06003373 if (ret) {
3374 netdev_err(pdata->netdev, "error flushing TX queues\n");
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003375 return ret;
Lendacky, Thomas738f7f62017-01-20 12:14:13 -06003376 }
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003377
3378 /*
3379 * Initialize DMA related features
3380 */
3381 xgbe_config_dma_bus(pdata);
3382 xgbe_config_dma_cache(pdata);
3383 xgbe_config_osp_mode(pdata);
Lendacky, Thomas7e1e6b82017-06-28 13:43:18 -05003384 xgbe_config_pbl_val(pdata);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003385 xgbe_config_rx_coalesce(pdata);
3386 xgbe_config_tx_coalesce(pdata);
3387 xgbe_config_rx_buffer_size(pdata);
3388 xgbe_config_tso_mode(pdata);
Lendacky, Thomas174fd252014-11-04 16:06:50 -06003389 xgbe_config_sph_mode(pdata);
Lendacky, Thomas5b9dfe22014-11-04 16:07:02 -06003390 xgbe_config_rss(pdata);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003391 desc_if->wrapper_tx_desc_init(pdata);
3392 desc_if->wrapper_rx_desc_init(pdata);
3393 xgbe_enable_dma_interrupts(pdata);
3394
3395 /*
3396 * Initialize MTL related features
3397 */
3398 xgbe_config_mtl_mode(pdata);
Lendacky, Thomasfca2d992014-07-29 08:57:55 -05003399 xgbe_config_queue_mapping(pdata);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003400 xgbe_config_tsf_mode(pdata, pdata->tx_sf_mode);
3401 xgbe_config_rsf_mode(pdata, pdata->rx_sf_mode);
3402 xgbe_config_tx_threshold(pdata, pdata->tx_threshold);
3403 xgbe_config_rx_threshold(pdata, pdata->rx_threshold);
3404 xgbe_config_tx_fifo_size(pdata);
3405 xgbe_config_rx_fifo_size(pdata);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003406 /*TODO: Error Packet and undersized good Packet forwarding enable
3407 (FEP and FUP)
3408 */
Lendacky, Thomasfca2d992014-07-29 08:57:55 -05003409 xgbe_config_dcb_tc(pdata);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003410 xgbe_enable_mtl_interrupts(pdata);
3411
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003412 /*
3413 * Initialize MAC related features
3414 */
3415 xgbe_config_mac_address(pdata);
Lendacky, Thomasb8763822015-04-09 12:11:57 -05003416 xgbe_config_rx_mode(pdata);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003417 xgbe_config_jumbo_enable(pdata);
3418 xgbe_config_flow_control(pdata);
Lendacky, Thomas916102c2015-01-16 12:46:45 -06003419 xgbe_config_mac_speed(pdata);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003420 xgbe_config_checksum_offload(pdata);
3421 xgbe_config_vlan_support(pdata);
3422 xgbe_config_mmc(pdata);
3423 xgbe_enable_mac_interrupts(pdata);
3424
Lendacky, Thomase78332b2016-11-10 17:10:26 -06003425 /*
3426 * Initialize ECC related features
3427 */
3428 xgbe_enable_ecc_interrupts(pdata);
3429
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003430 DBGPR("<--xgbe_init\n");
3431
3432 return 0;
3433}
3434
3435void xgbe_init_function_ptrs_dev(struct xgbe_hw_if *hw_if)
3436{
3437 DBGPR("-->xgbe_init_function_ptrs\n");
3438
3439 hw_if->tx_complete = xgbe_tx_complete;
3440
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003441 hw_if->set_mac_address = xgbe_set_mac_address;
Lendacky, Thomasb8763822015-04-09 12:11:57 -05003442 hw_if->config_rx_mode = xgbe_config_rx_mode;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003443
3444 hw_if->enable_rx_csum = xgbe_enable_rx_csum;
3445 hw_if->disable_rx_csum = xgbe_disable_rx_csum;
3446
3447 hw_if->enable_rx_vlan_stripping = xgbe_enable_rx_vlan_stripping;
3448 hw_if->disable_rx_vlan_stripping = xgbe_disable_rx_vlan_stripping;
Lendacky, Thomas801c62d2014-06-24 16:19:24 -05003449 hw_if->enable_rx_vlan_filtering = xgbe_enable_rx_vlan_filtering;
3450 hw_if->disable_rx_vlan_filtering = xgbe_disable_rx_vlan_filtering;
3451 hw_if->update_vlan_hash_table = xgbe_update_vlan_hash_table;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003452
3453 hw_if->read_mmd_regs = xgbe_read_mmd_regs;
3454 hw_if->write_mmd_regs = xgbe_write_mmd_regs;
3455
Lendacky, Thomase57f7a32016-11-03 13:18:27 -05003456 hw_if->set_speed = xgbe_set_speed;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003457
Lendacky, Thomas732f2ab2016-11-10 17:11:14 -06003458 hw_if->set_ext_mii_mode = xgbe_set_ext_mii_mode;
3459 hw_if->read_ext_mii_regs = xgbe_read_ext_mii_regs;
3460 hw_if->write_ext_mii_regs = xgbe_write_ext_mii_regs;
3461
3462 hw_if->set_gpio = xgbe_set_gpio;
3463 hw_if->clr_gpio = xgbe_clr_gpio;
3464
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003465 hw_if->enable_tx = xgbe_enable_tx;
3466 hw_if->disable_tx = xgbe_disable_tx;
3467 hw_if->enable_rx = xgbe_enable_rx;
3468 hw_if->disable_rx = xgbe_disable_rx;
3469
3470 hw_if->powerup_tx = xgbe_powerup_tx;
3471 hw_if->powerdown_tx = xgbe_powerdown_tx;
3472 hw_if->powerup_rx = xgbe_powerup_rx;
3473 hw_if->powerdown_rx = xgbe_powerdown_rx;
3474
Lendacky, Thomasa9d41982014-11-04 16:06:32 -06003475 hw_if->dev_xmit = xgbe_dev_xmit;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003476 hw_if->dev_read = xgbe_dev_read;
3477 hw_if->enable_int = xgbe_enable_int;
3478 hw_if->disable_int = xgbe_disable_int;
3479 hw_if->init = xgbe_init;
3480 hw_if->exit = xgbe_exit;
3481
3482 /* Descriptor related Sequences have to be initialized here */
3483 hw_if->tx_desc_init = xgbe_tx_desc_init;
3484 hw_if->rx_desc_init = xgbe_rx_desc_init;
3485 hw_if->tx_desc_reset = xgbe_tx_desc_reset;
3486 hw_if->rx_desc_reset = xgbe_rx_desc_reset;
3487 hw_if->is_last_desc = xgbe_is_last_desc;
3488 hw_if->is_context_desc = xgbe_is_context_desc;
Lendacky, Thomas16958a22014-11-20 11:04:08 -06003489 hw_if->tx_start_xmit = xgbe_tx_start_xmit;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003490
3491 /* For FLOW ctrl */
3492 hw_if->config_tx_flow_control = xgbe_config_tx_flow_control;
3493 hw_if->config_rx_flow_control = xgbe_config_rx_flow_control;
3494
3495 /* For RX coalescing */
3496 hw_if->config_rx_coalesce = xgbe_config_rx_coalesce;
3497 hw_if->config_tx_coalesce = xgbe_config_tx_coalesce;
3498 hw_if->usec_to_riwt = xgbe_usec_to_riwt;
3499 hw_if->riwt_to_usec = xgbe_riwt_to_usec;
3500
3501 /* For RX and TX threshold config */
3502 hw_if->config_rx_threshold = xgbe_config_rx_threshold;
3503 hw_if->config_tx_threshold = xgbe_config_tx_threshold;
3504
3505 /* For RX and TX Store and Forward Mode config */
3506 hw_if->config_rsf_mode = xgbe_config_rsf_mode;
3507 hw_if->config_tsf_mode = xgbe_config_tsf_mode;
3508
3509 /* For TX DMA Operating on Second Frame config */
3510 hw_if->config_osp_mode = xgbe_config_osp_mode;
3511
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003512 /* For MMC statistics support */
3513 hw_if->tx_mmc_int = xgbe_tx_mmc_int;
3514 hw_if->rx_mmc_int = xgbe_rx_mmc_int;
3515 hw_if->read_mmc_stats = xgbe_read_mmc_stats;
3516
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05003517 /* For PTP config */
3518 hw_if->config_tstamp = xgbe_config_tstamp;
3519 hw_if->update_tstamp_addend = xgbe_update_tstamp_addend;
3520 hw_if->set_tstamp_time = xgbe_set_tstamp_time;
3521 hw_if->get_tstamp_time = xgbe_get_tstamp_time;
3522 hw_if->get_tx_tstamp = xgbe_get_tx_tstamp;
3523
Lendacky, Thomasfca2d992014-07-29 08:57:55 -05003524 /* For Data Center Bridging config */
Lendacky, Thomasb3b71592016-02-17 11:49:08 -06003525 hw_if->config_tc = xgbe_config_tc;
Lendacky, Thomasfca2d992014-07-29 08:57:55 -05003526 hw_if->config_dcb_tc = xgbe_config_dcb_tc;
3527 hw_if->config_dcb_pfc = xgbe_config_dcb_pfc;
3528
Lendacky, Thomas5b9dfe22014-11-04 16:07:02 -06003529 /* For Receive Side Scaling */
3530 hw_if->enable_rss = xgbe_enable_rss;
3531 hw_if->disable_rss = xgbe_disable_rss;
Lendacky, Thomasf6ac8622014-11-04 16:07:23 -06003532 hw_if->set_rss_hash_key = xgbe_set_rss_hash_key;
3533 hw_if->set_rss_lookup_table = xgbe_set_rss_lookup_table;
Lendacky, Thomas5b9dfe22014-11-04 16:07:02 -06003534
Lendacky, Thomase78332b2016-11-10 17:10:26 -06003535 /* For ECC */
3536 hw_if->disable_ecc_ded = xgbe_disable_ecc_ded;
3537 hw_if->disable_ecc_sec = xgbe_disable_ecc_sec;
3538
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003539 DBGPR("<--xgbe_init_function_ptrs\n");
3540}