amd-xgbe: Use page allocations for Rx buffers
[firefly-linux-kernel-4.4.55.git] / drivers / net / ethernet / amd / xgbe / xgbe-dev.c
1 /*
2  * AMD 10Gb Ethernet driver
3  *
4  * This file is available to you under your choice of the following two
5  * licenses:
6  *
7  * License 1: GPLv2
8  *
9  * Copyright (c) 2014 Advanced Micro Devices, Inc.
10  *
11  * This file is free software; you may copy, redistribute and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation, either version 2 of the License, or (at
14  * your option) any later version.
15  *
16  * This file is distributed in the hope that it will be useful, but
17  * WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23  *
24  * This file incorporates work covered by the following copyright and
25  * permission notice:
26  *     The Synopsys DWC ETHER XGMAC Software Driver and documentation
27  *     (hereinafter "Software") is an unsupported proprietary work of Synopsys,
28  *     Inc. unless otherwise expressly agreed to in writing between Synopsys
29  *     and you.
30  *
31  *     The Software IS NOT an item of Licensed Software or Licensed Product
32  *     under any End User Software License Agreement or Agreement for Licensed
33  *     Product with Synopsys or any supplement thereto.  Permission is hereby
34  *     granted, free of charge, to any person obtaining a copy of this software
35  *     annotated with this license and the Software, to deal in the Software
36  *     without restriction, including without limitation the rights to use,
37  *     copy, modify, merge, publish, distribute, sublicense, and/or sell copies
38  *     of the Software, and to permit persons to whom the Software is furnished
39  *     to do so, subject to the following conditions:
40  *
41  *     The above copyright notice and this permission notice shall be included
42  *     in all copies or substantial portions of the Software.
43  *
44  *     THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"
45  *     BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
46  *     TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
47  *     PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS
48  *     BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
49  *     CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
50  *     SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
51  *     INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
52  *     CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
53  *     ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
54  *     THE POSSIBILITY OF SUCH DAMAGE.
55  *
56  *
57  * License 2: Modified BSD
58  *
59  * Copyright (c) 2014 Advanced Micro Devices, Inc.
60  * All rights reserved.
61  *
62  * Redistribution and use in source and binary forms, with or without
63  * modification, are permitted provided that the following conditions are met:
64  *     * Redistributions of source code must retain the above copyright
65  *       notice, this list of conditions and the following disclaimer.
66  *     * Redistributions in binary form must reproduce the above copyright
67  *       notice, this list of conditions and the following disclaimer in the
68  *       documentation and/or other materials provided with the distribution.
69  *     * Neither the name of Advanced Micro Devices, Inc. nor the
70  *       names of its contributors may be used to endorse or promote products
71  *       derived from this software without specific prior written permission.
72  *
73  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
74  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
75  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
76  * ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
77  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
78  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
79  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
80  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
81  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
82  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
83  *
84  * This file incorporates work covered by the following copyright and
85  * permission notice:
86  *     The Synopsys DWC ETHER XGMAC Software Driver and documentation
87  *     (hereinafter "Software") is an unsupported proprietary work of Synopsys,
88  *     Inc. unless otherwise expressly agreed to in writing between Synopsys
89  *     and you.
90  *
91  *     The Software IS NOT an item of Licensed Software or Licensed Product
92  *     under any End User Software License Agreement or Agreement for Licensed
93  *     Product with Synopsys or any supplement thereto.  Permission is hereby
94  *     granted, free of charge, to any person obtaining a copy of this software
95  *     annotated with this license and the Software, to deal in the Software
96  *     without restriction, including without limitation the rights to use,
97  *     copy, modify, merge, publish, distribute, sublicense, and/or sell copies
98  *     of the Software, and to permit persons to whom the Software is furnished
99  *     to do so, subject to the following conditions:
100  *
101  *     The above copyright notice and this permission notice shall be included
102  *     in all copies or substantial portions of the Software.
103  *
104  *     THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"
105  *     BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
106  *     TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
107  *     PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS
108  *     BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
109  *     CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
110  *     SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
111  *     INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
112  *     CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
113  *     ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
114  *     THE POSSIBILITY OF SUCH DAMAGE.
115  */
116
117 #include <linux/phy.h>
118 #include <linux/clk.h>
119 #include <linux/bitrev.h>
120 #include <linux/crc32.h>
121
122 #include "xgbe.h"
123 #include "xgbe-common.h"
124
125 static unsigned int xgbe_usec_to_riwt(struct xgbe_prv_data *pdata,
126                                       unsigned int usec)
127 {
128         unsigned long rate;
129         unsigned int ret;
130
131         DBGPR("-->xgbe_usec_to_riwt\n");
132
133         rate = clk_get_rate(pdata->sysclk);
134
135         /*
136          * Convert the input usec value to the watchdog timer value. Each
137          * watchdog timer value is equivalent to 256 clock cycles.
138          * Calculate the required value as:
139          *   ( usec * ( system_clock_mhz / 10^6 ) / 256
140          */
141         ret = (usec * (rate / 1000000)) / 256;
142
143         DBGPR("<--xgbe_usec_to_riwt\n");
144
145         return ret;
146 }
147
148 static unsigned int xgbe_riwt_to_usec(struct xgbe_prv_data *pdata,
149                                       unsigned int riwt)
150 {
151         unsigned long rate;
152         unsigned int ret;
153
154         DBGPR("-->xgbe_riwt_to_usec\n");
155
156         rate = clk_get_rate(pdata->sysclk);
157
158         /*
159          * Convert the input watchdog timer value to the usec value. Each
160          * watchdog timer value is equivalent to 256 clock cycles.
161          * Calculate the required value as:
162          *   ( riwt * 256 ) / ( system_clock_mhz / 10^6 )
163          */
164         ret = (riwt * 256) / (rate / 1000000);
165
166         DBGPR("<--xgbe_riwt_to_usec\n");
167
168         return ret;
169 }
170
171 static int xgbe_config_pblx8(struct xgbe_prv_data *pdata)
172 {
173         struct xgbe_channel *channel;
174         unsigned int i;
175
176         channel = pdata->channel;
177         for (i = 0; i < pdata->channel_count; i++, channel++)
178                 XGMAC_DMA_IOWRITE_BITS(channel, DMA_CH_CR, PBLX8,
179                                        pdata->pblx8);
180
181         return 0;
182 }
183
184 static int xgbe_get_tx_pbl_val(struct xgbe_prv_data *pdata)
185 {
186         return XGMAC_DMA_IOREAD_BITS(pdata->channel, DMA_CH_TCR, PBL);
187 }
188
189 static int xgbe_config_tx_pbl_val(struct xgbe_prv_data *pdata)
190 {
191         struct xgbe_channel *channel;
192         unsigned int i;
193
194         channel = pdata->channel;
195         for (i = 0; i < pdata->channel_count; i++, channel++) {
196                 if (!channel->tx_ring)
197                         break;
198
199                 XGMAC_DMA_IOWRITE_BITS(channel, DMA_CH_TCR, PBL,
200                                        pdata->tx_pbl);
201         }
202
203         return 0;
204 }
205
206 static int xgbe_get_rx_pbl_val(struct xgbe_prv_data *pdata)
207 {
208         return XGMAC_DMA_IOREAD_BITS(pdata->channel, DMA_CH_RCR, PBL);
209 }
210
211 static int xgbe_config_rx_pbl_val(struct xgbe_prv_data *pdata)
212 {
213         struct xgbe_channel *channel;
214         unsigned int i;
215
216         channel = pdata->channel;
217         for (i = 0; i < pdata->channel_count; i++, channel++) {
218                 if (!channel->rx_ring)
219                         break;
220
221                 XGMAC_DMA_IOWRITE_BITS(channel, DMA_CH_RCR, PBL,
222                                        pdata->rx_pbl);
223         }
224
225         return 0;
226 }
227
228 static int xgbe_config_osp_mode(struct xgbe_prv_data *pdata)
229 {
230         struct xgbe_channel *channel;
231         unsigned int i;
232
233         channel = pdata->channel;
234         for (i = 0; i < pdata->channel_count; i++, channel++) {
235                 if (!channel->tx_ring)
236                         break;
237
238                 XGMAC_DMA_IOWRITE_BITS(channel, DMA_CH_TCR, OSP,
239                                        pdata->tx_osp_mode);
240         }
241
242         return 0;
243 }
244
245 static int xgbe_config_rsf_mode(struct xgbe_prv_data *pdata, unsigned int val)
246 {
247         unsigned int i;
248
249         for (i = 0; i < pdata->rx_q_count; i++)
250                 XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_Q_RQOMR, RSF, val);
251
252         return 0;
253 }
254
255 static int xgbe_config_tsf_mode(struct xgbe_prv_data *pdata, unsigned int val)
256 {
257         unsigned int i;
258
259         for (i = 0; i < pdata->tx_q_count; i++)
260                 XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_Q_TQOMR, TSF, val);
261
262         return 0;
263 }
264
265 static int xgbe_config_rx_threshold(struct xgbe_prv_data *pdata,
266                                     unsigned int val)
267 {
268         unsigned int i;
269
270         for (i = 0; i < pdata->rx_q_count; i++)
271                 XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_Q_RQOMR, RTC, val);
272
273         return 0;
274 }
275
276 static int xgbe_config_tx_threshold(struct xgbe_prv_data *pdata,
277                                     unsigned int val)
278 {
279         unsigned int i;
280
281         for (i = 0; i < pdata->tx_q_count; i++)
282                 XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_Q_TQOMR, TTC, val);
283
284         return 0;
285 }
286
287 static int xgbe_config_rx_coalesce(struct xgbe_prv_data *pdata)
288 {
289         struct xgbe_channel *channel;
290         unsigned int i;
291
292         channel = pdata->channel;
293         for (i = 0; i < pdata->channel_count; i++, channel++) {
294                 if (!channel->rx_ring)
295                         break;
296
297                 XGMAC_DMA_IOWRITE_BITS(channel, DMA_CH_RIWT, RWT,
298                                        pdata->rx_riwt);
299         }
300
301         return 0;
302 }
303
304 static int xgbe_config_tx_coalesce(struct xgbe_prv_data *pdata)
305 {
306         return 0;
307 }
308
309 static void xgbe_config_rx_buffer_size(struct xgbe_prv_data *pdata)
310 {
311         struct xgbe_channel *channel;
312         unsigned int i;
313
314         channel = pdata->channel;
315         for (i = 0; i < pdata->channel_count; i++, channel++) {
316                 if (!channel->rx_ring)
317                         break;
318
319                 XGMAC_DMA_IOWRITE_BITS(channel, DMA_CH_RCR, RBSZ,
320                                        pdata->rx_buf_size);
321         }
322 }
323
324 static void xgbe_config_tso_mode(struct xgbe_prv_data *pdata)
325 {
326         struct xgbe_channel *channel;
327         unsigned int i;
328
329         channel = pdata->channel;
330         for (i = 0; i < pdata->channel_count; i++, channel++) {
331                 if (!channel->tx_ring)
332                         break;
333
334                 XGMAC_DMA_IOWRITE_BITS(channel, DMA_CH_TCR, TSE, 1);
335         }
336 }
337
338 static int xgbe_disable_tx_flow_control(struct xgbe_prv_data *pdata)
339 {
340         unsigned int max_q_count, q_count;
341         unsigned int reg, reg_val;
342         unsigned int i;
343
344         /* Clear MTL flow control */
345         for (i = 0; i < pdata->rx_q_count; i++)
346                 XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_Q_RQOMR, EHFC, 0);
347
348         /* Clear MAC flow control */
349         max_q_count = XGMAC_MAX_FLOW_CONTROL_QUEUES;
350         q_count = min_t(unsigned int, pdata->tx_q_count, max_q_count);
351         reg = MAC_Q0TFCR;
352         for (i = 0; i < q_count; i++) {
353                 reg_val = XGMAC_IOREAD(pdata, reg);
354                 XGMAC_SET_BITS(reg_val, MAC_Q0TFCR, TFE, 0);
355                 XGMAC_IOWRITE(pdata, reg, reg_val);
356
357                 reg += MAC_QTFCR_INC;
358         }
359
360         return 0;
361 }
362
363 static int xgbe_enable_tx_flow_control(struct xgbe_prv_data *pdata)
364 {
365         unsigned int max_q_count, q_count;
366         unsigned int reg, reg_val;
367         unsigned int i;
368
369         /* Set MTL flow control */
370         for (i = 0; i < pdata->rx_q_count; i++)
371                 XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_Q_RQOMR, EHFC, 1);
372
373         /* Set MAC flow control */
374         max_q_count = XGMAC_MAX_FLOW_CONTROL_QUEUES;
375         q_count = min_t(unsigned int, pdata->tx_q_count, max_q_count);
376         reg = MAC_Q0TFCR;
377         for (i = 0; i < q_count; i++) {
378                 reg_val = XGMAC_IOREAD(pdata, reg);
379
380                 /* Enable transmit flow control */
381                 XGMAC_SET_BITS(reg_val, MAC_Q0TFCR, TFE, 1);
382                 /* Set pause time */
383                 XGMAC_SET_BITS(reg_val, MAC_Q0TFCR, PT, 0xffff);
384
385                 XGMAC_IOWRITE(pdata, reg, reg_val);
386
387                 reg += MAC_QTFCR_INC;
388         }
389
390         return 0;
391 }
392
393 static int xgbe_disable_rx_flow_control(struct xgbe_prv_data *pdata)
394 {
395         XGMAC_IOWRITE_BITS(pdata, MAC_RFCR, RFE, 0);
396
397         return 0;
398 }
399
400 static int xgbe_enable_rx_flow_control(struct xgbe_prv_data *pdata)
401 {
402         XGMAC_IOWRITE_BITS(pdata, MAC_RFCR, RFE, 1);
403
404         return 0;
405 }
406
407 static int xgbe_config_tx_flow_control(struct xgbe_prv_data *pdata)
408 {
409         struct ieee_pfc *pfc = pdata->pfc;
410
411         if (pdata->tx_pause || (pfc && pfc->pfc_en))
412                 xgbe_enable_tx_flow_control(pdata);
413         else
414                 xgbe_disable_tx_flow_control(pdata);
415
416         return 0;
417 }
418
419 static int xgbe_config_rx_flow_control(struct xgbe_prv_data *pdata)
420 {
421         struct ieee_pfc *pfc = pdata->pfc;
422
423         if (pdata->rx_pause || (pfc && pfc->pfc_en))
424                 xgbe_enable_rx_flow_control(pdata);
425         else
426                 xgbe_disable_rx_flow_control(pdata);
427
428         return 0;
429 }
430
431 static void xgbe_config_flow_control(struct xgbe_prv_data *pdata)
432 {
433         struct ieee_pfc *pfc = pdata->pfc;
434
435         xgbe_config_tx_flow_control(pdata);
436         xgbe_config_rx_flow_control(pdata);
437
438         XGMAC_IOWRITE_BITS(pdata, MAC_RFCR, PFCE,
439                            (pfc && pfc->pfc_en) ? 1 : 0);
440 }
441
442 static void xgbe_enable_dma_interrupts(struct xgbe_prv_data *pdata)
443 {
444         struct xgbe_channel *channel;
445         unsigned int dma_ch_isr, dma_ch_ier;
446         unsigned int i;
447
448         channel = pdata->channel;
449         for (i = 0; i < pdata->channel_count; i++, channel++) {
450                 /* Clear all the interrupts which are set */
451                 dma_ch_isr = XGMAC_DMA_IOREAD(channel, DMA_CH_SR);
452                 XGMAC_DMA_IOWRITE(channel, DMA_CH_SR, dma_ch_isr);
453
454                 /* Clear all interrupt enable bits */
455                 dma_ch_ier = 0;
456
457                 /* Enable following interrupts
458                  *   NIE  - Normal Interrupt Summary Enable
459                  *   AIE  - Abnormal Interrupt Summary Enable
460                  *   FBEE - Fatal Bus Error Enable
461                  */
462                 XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, NIE, 1);
463                 XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, AIE, 1);
464                 XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, FBEE, 1);
465
466                 if (channel->tx_ring) {
467                         /* Enable the following Tx interrupts
468                          *   TIE  - Transmit Interrupt Enable (unless polling)
469                          */
470                         XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, TIE, 1);
471                 }
472                 if (channel->rx_ring) {
473                         /* Enable following Rx interrupts
474                          *   RBUE - Receive Buffer Unavailable Enable
475                          *   RIE  - Receive Interrupt Enable
476                          */
477                         XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, RBUE, 1);
478                         XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, RIE, 1);
479                 }
480
481                 XGMAC_DMA_IOWRITE(channel, DMA_CH_IER, dma_ch_ier);
482         }
483 }
484
485 static void xgbe_enable_mtl_interrupts(struct xgbe_prv_data *pdata)
486 {
487         unsigned int mtl_q_isr;
488         unsigned int q_count, i;
489
490         q_count = max(pdata->hw_feat.tx_q_cnt, pdata->hw_feat.rx_q_cnt);
491         for (i = 0; i < q_count; i++) {
492                 /* Clear all the interrupts which are set */
493                 mtl_q_isr = XGMAC_MTL_IOREAD(pdata, i, MTL_Q_ISR);
494                 XGMAC_MTL_IOWRITE(pdata, i, MTL_Q_ISR, mtl_q_isr);
495
496                 /* No MTL interrupts to be enabled */
497                 XGMAC_MTL_IOWRITE(pdata, i, MTL_Q_IER, 0);
498         }
499 }
500
501 static void xgbe_enable_mac_interrupts(struct xgbe_prv_data *pdata)
502 {
503         unsigned int mac_ier = 0;
504
505         /* Enable Timestamp interrupt */
506         XGMAC_SET_BITS(mac_ier, MAC_IER, TSIE, 1);
507
508         XGMAC_IOWRITE(pdata, MAC_IER, mac_ier);
509
510         /* Enable all counter interrupts */
511         XGMAC_IOWRITE_BITS(pdata, MMC_RIER, ALL_INTERRUPTS, 0xffffffff);
512         XGMAC_IOWRITE_BITS(pdata, MMC_TIER, ALL_INTERRUPTS, 0xffffffff);
513 }
514
515 static int xgbe_set_gmii_speed(struct xgbe_prv_data *pdata)
516 {
517         XGMAC_IOWRITE_BITS(pdata, MAC_TCR, SS, 0x3);
518
519         return 0;
520 }
521
522 static int xgbe_set_gmii_2500_speed(struct xgbe_prv_data *pdata)
523 {
524         XGMAC_IOWRITE_BITS(pdata, MAC_TCR, SS, 0x2);
525
526         return 0;
527 }
528
529 static int xgbe_set_xgmii_speed(struct xgbe_prv_data *pdata)
530 {
531         XGMAC_IOWRITE_BITS(pdata, MAC_TCR, SS, 0);
532
533         return 0;
534 }
535
536 static int xgbe_set_promiscuous_mode(struct xgbe_prv_data *pdata,
537                                      unsigned int enable)
538 {
539         unsigned int val = enable ? 1 : 0;
540
541         if (XGMAC_IOREAD_BITS(pdata, MAC_PFR, PR) == val)
542                 return 0;
543
544         DBGPR("  %s promiscuous mode\n", enable ? "entering" : "leaving");
545         XGMAC_IOWRITE_BITS(pdata, MAC_PFR, PR, val);
546
547         return 0;
548 }
549
550 static int xgbe_set_all_multicast_mode(struct xgbe_prv_data *pdata,
551                                        unsigned int enable)
552 {
553         unsigned int val = enable ? 1 : 0;
554
555         if (XGMAC_IOREAD_BITS(pdata, MAC_PFR, PM) == val)
556                 return 0;
557
558         DBGPR("  %s allmulti mode\n", enable ? "entering" : "leaving");
559         XGMAC_IOWRITE_BITS(pdata, MAC_PFR, PM, val);
560
561         return 0;
562 }
563
564 static void xgbe_set_mac_reg(struct xgbe_prv_data *pdata,
565                              struct netdev_hw_addr *ha, unsigned int *mac_reg)
566 {
567         unsigned int mac_addr_hi, mac_addr_lo;
568         u8 *mac_addr;
569
570         mac_addr_lo = 0;
571         mac_addr_hi = 0;
572
573         if (ha) {
574                 mac_addr = (u8 *)&mac_addr_lo;
575                 mac_addr[0] = ha->addr[0];
576                 mac_addr[1] = ha->addr[1];
577                 mac_addr[2] = ha->addr[2];
578                 mac_addr[3] = ha->addr[3];
579                 mac_addr = (u8 *)&mac_addr_hi;
580                 mac_addr[0] = ha->addr[4];
581                 mac_addr[1] = ha->addr[5];
582
583                 DBGPR("  adding mac address %pM at 0x%04x\n", ha->addr,
584                       *mac_reg);
585
586                 XGMAC_SET_BITS(mac_addr_hi, MAC_MACA1HR, AE, 1);
587         }
588
589         XGMAC_IOWRITE(pdata, *mac_reg, mac_addr_hi);
590         *mac_reg += MAC_MACA_INC;
591         XGMAC_IOWRITE(pdata, *mac_reg, mac_addr_lo);
592         *mac_reg += MAC_MACA_INC;
593 }
594
595 static void xgbe_set_mac_addn_addrs(struct xgbe_prv_data *pdata)
596 {
597         struct net_device *netdev = pdata->netdev;
598         struct netdev_hw_addr *ha;
599         unsigned int mac_reg;
600         unsigned int addn_macs;
601
602         mac_reg = MAC_MACA1HR;
603         addn_macs = pdata->hw_feat.addn_mac;
604
605         if (netdev_uc_count(netdev) > addn_macs) {
606                 xgbe_set_promiscuous_mode(pdata, 1);
607         } else {
608                 netdev_for_each_uc_addr(ha, netdev) {
609                         xgbe_set_mac_reg(pdata, ha, &mac_reg);
610                         addn_macs--;
611                 }
612
613                 if (netdev_mc_count(netdev) > addn_macs) {
614                         xgbe_set_all_multicast_mode(pdata, 1);
615                 } else {
616                         netdev_for_each_mc_addr(ha, netdev) {
617                                 xgbe_set_mac_reg(pdata, ha, &mac_reg);
618                                 addn_macs--;
619                         }
620                 }
621         }
622
623         /* Clear remaining additional MAC address entries */
624         while (addn_macs--)
625                 xgbe_set_mac_reg(pdata, NULL, &mac_reg);
626 }
627
628 static void xgbe_set_mac_hash_table(struct xgbe_prv_data *pdata)
629 {
630         struct net_device *netdev = pdata->netdev;
631         struct netdev_hw_addr *ha;
632         unsigned int hash_reg;
633         unsigned int hash_table_shift, hash_table_count;
634         u32 hash_table[XGBE_MAC_HASH_TABLE_SIZE];
635         u32 crc;
636         unsigned int i;
637
638         hash_table_shift = 26 - (pdata->hw_feat.hash_table_size >> 7);
639         hash_table_count = pdata->hw_feat.hash_table_size / 32;
640         memset(hash_table, 0, sizeof(hash_table));
641
642         /* Build the MAC Hash Table register values */
643         netdev_for_each_uc_addr(ha, netdev) {
644                 crc = bitrev32(~crc32_le(~0, ha->addr, ETH_ALEN));
645                 crc >>= hash_table_shift;
646                 hash_table[crc >> 5] |= (1 << (crc & 0x1f));
647         }
648
649         netdev_for_each_mc_addr(ha, netdev) {
650                 crc = bitrev32(~crc32_le(~0, ha->addr, ETH_ALEN));
651                 crc >>= hash_table_shift;
652                 hash_table[crc >> 5] |= (1 << (crc & 0x1f));
653         }
654
655         /* Set the MAC Hash Table registers */
656         hash_reg = MAC_HTR0;
657         for (i = 0; i < hash_table_count; i++) {
658                 XGMAC_IOWRITE(pdata, hash_reg, hash_table[i]);
659                 hash_reg += MAC_HTR_INC;
660         }
661 }
662
663 static int xgbe_add_mac_addresses(struct xgbe_prv_data *pdata)
664 {
665         if (pdata->hw_feat.hash_table_size)
666                 xgbe_set_mac_hash_table(pdata);
667         else
668                 xgbe_set_mac_addn_addrs(pdata);
669
670         return 0;
671 }
672
673 static int xgbe_set_mac_address(struct xgbe_prv_data *pdata, u8 *addr)
674 {
675         unsigned int mac_addr_hi, mac_addr_lo;
676
677         mac_addr_hi = (addr[5] <<  8) | (addr[4] <<  0);
678         mac_addr_lo = (addr[3] << 24) | (addr[2] << 16) |
679                       (addr[1] <<  8) | (addr[0] <<  0);
680
681         XGMAC_IOWRITE(pdata, MAC_MACA0HR, mac_addr_hi);
682         XGMAC_IOWRITE(pdata, MAC_MACA0LR, mac_addr_lo);
683
684         return 0;
685 }
686
687 static int xgbe_read_mmd_regs(struct xgbe_prv_data *pdata, int prtad,
688                               int mmd_reg)
689 {
690         unsigned int mmd_address;
691         int mmd_data;
692
693         if (mmd_reg & MII_ADDR_C45)
694                 mmd_address = mmd_reg & ~MII_ADDR_C45;
695         else
696                 mmd_address = (pdata->mdio_mmd << 16) | (mmd_reg & 0xffff);
697
698         /* The PCS registers are accessed using mmio. The underlying APB3
699          * management interface uses indirect addressing to access the MMD
700          * register sets. This requires accessing of the PCS register in two
701          * phases, an address phase and a data phase.
702          *
703          * The mmio interface is based on 32-bit offsets and values. All
704          * register offsets must therefore be adjusted by left shifting the
705          * offset 2 bits and reading 32 bits of data.
706          */
707         mutex_lock(&pdata->xpcs_mutex);
708         XPCS_IOWRITE(pdata, PCS_MMD_SELECT << 2, mmd_address >> 8);
709         mmd_data = XPCS_IOREAD(pdata, (mmd_address & 0xff) << 2);
710         mutex_unlock(&pdata->xpcs_mutex);
711
712         return mmd_data;
713 }
714
715 static void xgbe_write_mmd_regs(struct xgbe_prv_data *pdata, int prtad,
716                                 int mmd_reg, int mmd_data)
717 {
718         unsigned int mmd_address;
719
720         if (mmd_reg & MII_ADDR_C45)
721                 mmd_address = mmd_reg & ~MII_ADDR_C45;
722         else
723                 mmd_address = (pdata->mdio_mmd << 16) | (mmd_reg & 0xffff);
724
725         /* The PCS registers are accessed using mmio. The underlying APB3
726          * management interface uses indirect addressing to access the MMD
727          * register sets. This requires accessing of the PCS register in two
728          * phases, an address phase and a data phase.
729          *
730          * The mmio interface is based on 32-bit offsets and values. All
731          * register offsets must therefore be adjusted by left shifting the
732          * offset 2 bits and reading 32 bits of data.
733          */
734         mutex_lock(&pdata->xpcs_mutex);
735         XPCS_IOWRITE(pdata, PCS_MMD_SELECT << 2, mmd_address >> 8);
736         XPCS_IOWRITE(pdata, (mmd_address & 0xff) << 2, mmd_data);
737         mutex_unlock(&pdata->xpcs_mutex);
738 }
739
740 static int xgbe_tx_complete(struct xgbe_ring_desc *rdesc)
741 {
742         return !XGMAC_GET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, OWN);
743 }
744
745 static int xgbe_disable_rx_csum(struct xgbe_prv_data *pdata)
746 {
747         XGMAC_IOWRITE_BITS(pdata, MAC_RCR, IPC, 0);
748
749         return 0;
750 }
751
752 static int xgbe_enable_rx_csum(struct xgbe_prv_data *pdata)
753 {
754         XGMAC_IOWRITE_BITS(pdata, MAC_RCR, IPC, 1);
755
756         return 0;
757 }
758
759 static int xgbe_enable_rx_vlan_stripping(struct xgbe_prv_data *pdata)
760 {
761         /* Put the VLAN tag in the Rx descriptor */
762         XGMAC_IOWRITE_BITS(pdata, MAC_VLANTR, EVLRXS, 1);
763
764         /* Don't check the VLAN type */
765         XGMAC_IOWRITE_BITS(pdata, MAC_VLANTR, DOVLTC, 1);
766
767         /* Check only C-TAG (0x8100) packets */
768         XGMAC_IOWRITE_BITS(pdata, MAC_VLANTR, ERSVLM, 0);
769
770         /* Don't consider an S-TAG (0x88A8) packet as a VLAN packet */
771         XGMAC_IOWRITE_BITS(pdata, MAC_VLANTR, ESVL, 0);
772
773         /* Enable VLAN tag stripping */
774         XGMAC_IOWRITE_BITS(pdata, MAC_VLANTR, EVLS, 0x3);
775
776         return 0;
777 }
778
779 static int xgbe_disable_rx_vlan_stripping(struct xgbe_prv_data *pdata)
780 {
781         XGMAC_IOWRITE_BITS(pdata, MAC_VLANTR, EVLS, 0);
782
783         return 0;
784 }
785
786 static int xgbe_enable_rx_vlan_filtering(struct xgbe_prv_data *pdata)
787 {
788         /* Enable VLAN filtering */
789         XGMAC_IOWRITE_BITS(pdata, MAC_PFR, VTFE, 1);
790
791         /* Enable VLAN Hash Table filtering */
792         XGMAC_IOWRITE_BITS(pdata, MAC_VLANTR, VTHM, 1);
793
794         /* Disable VLAN tag inverse matching */
795         XGMAC_IOWRITE_BITS(pdata, MAC_VLANTR, VTIM, 0);
796
797         /* Only filter on the lower 12-bits of the VLAN tag */
798         XGMAC_IOWRITE_BITS(pdata, MAC_VLANTR, ETV, 1);
799
800         /* In order for the VLAN Hash Table filtering to be effective,
801          * the VLAN tag identifier in the VLAN Tag Register must not
802          * be zero.  Set the VLAN tag identifier to "1" to enable the
803          * VLAN Hash Table filtering.  This implies that a VLAN tag of
804          * 1 will always pass filtering.
805          */
806         XGMAC_IOWRITE_BITS(pdata, MAC_VLANTR, VL, 1);
807
808         return 0;
809 }
810
811 static int xgbe_disable_rx_vlan_filtering(struct xgbe_prv_data *pdata)
812 {
813         /* Disable VLAN filtering */
814         XGMAC_IOWRITE_BITS(pdata, MAC_PFR, VTFE, 0);
815
816         return 0;
817 }
818
819 #ifndef CRCPOLY_LE
820 #define CRCPOLY_LE 0xedb88320
821 #endif
822 static u32 xgbe_vid_crc32_le(__le16 vid_le)
823 {
824         u32 poly = CRCPOLY_LE;
825         u32 crc = ~0;
826         u32 temp = 0;
827         unsigned char *data = (unsigned char *)&vid_le;
828         unsigned char data_byte = 0;
829         int i, bits;
830
831         bits = get_bitmask_order(VLAN_VID_MASK);
832         for (i = 0; i < bits; i++) {
833                 if ((i % 8) == 0)
834                         data_byte = data[i / 8];
835
836                 temp = ((crc & 1) ^ data_byte) & 1;
837                 crc >>= 1;
838                 data_byte >>= 1;
839
840                 if (temp)
841                         crc ^= poly;
842         }
843
844         return crc;
845 }
846
847 static int xgbe_update_vlan_hash_table(struct xgbe_prv_data *pdata)
848 {
849         u32 crc;
850         u16 vid;
851         __le16 vid_le;
852         u16 vlan_hash_table = 0;
853
854         /* Generate the VLAN Hash Table value */
855         for_each_set_bit(vid, pdata->active_vlans, VLAN_N_VID) {
856                 /* Get the CRC32 value of the VLAN ID */
857                 vid_le = cpu_to_le16(vid);
858                 crc = bitrev32(~xgbe_vid_crc32_le(vid_le)) >> 28;
859
860                 vlan_hash_table |= (1 << crc);
861         }
862
863         /* Set the VLAN Hash Table filtering register */
864         XGMAC_IOWRITE_BITS(pdata, MAC_VLANHTR, VLHT, vlan_hash_table);
865
866         return 0;
867 }
868
869 static void xgbe_tx_desc_reset(struct xgbe_ring_data *rdata)
870 {
871         struct xgbe_ring_desc *rdesc = rdata->rdesc;
872
873         /* Reset the Tx descriptor
874          *   Set buffer 1 (lo) address to zero
875          *   Set buffer 1 (hi) address to zero
876          *   Reset all other control bits (IC, TTSE, B2L & B1L)
877          *   Reset all other control bits (OWN, CTXT, FD, LD, CPC, CIC, etc)
878          */
879         rdesc->desc0 = 0;
880         rdesc->desc1 = 0;
881         rdesc->desc2 = 0;
882         rdesc->desc3 = 0;
883
884         /* Make sure ownership is written to the descriptor */
885         wmb();
886 }
887
888 static void xgbe_tx_desc_init(struct xgbe_channel *channel)
889 {
890         struct xgbe_ring *ring = channel->tx_ring;
891         struct xgbe_ring_data *rdata;
892         int i;
893         int start_index = ring->cur;
894
895         DBGPR("-->tx_desc_init\n");
896
897         /* Initialze all descriptors */
898         for (i = 0; i < ring->rdesc_count; i++) {
899                 rdata = XGBE_GET_DESC_DATA(ring, i);
900
901                 /* Initialize Tx descriptor */
902                 xgbe_tx_desc_reset(rdata);
903         }
904
905         /* Update the total number of Tx descriptors */
906         XGMAC_DMA_IOWRITE(channel, DMA_CH_TDRLR, ring->rdesc_count - 1);
907
908         /* Update the starting address of descriptor ring */
909         rdata = XGBE_GET_DESC_DATA(ring, start_index);
910         XGMAC_DMA_IOWRITE(channel, DMA_CH_TDLR_HI,
911                           upper_32_bits(rdata->rdesc_dma));
912         XGMAC_DMA_IOWRITE(channel, DMA_CH_TDLR_LO,
913                           lower_32_bits(rdata->rdesc_dma));
914
915         DBGPR("<--tx_desc_init\n");
916 }
917
918 static void xgbe_rx_desc_reset(struct xgbe_ring_data *rdata)
919 {
920         struct xgbe_ring_desc *rdesc = rdata->rdesc;
921
922         /* Reset the Rx descriptor
923          *   Set buffer 1 (lo) address to dma address (lo)
924          *   Set buffer 1 (hi) address to dma address (hi)
925          *   Set buffer 2 (lo) address to zero
926          *   Set buffer 2 (hi) address to zero and set control bits
927          *     OWN and INTE
928          */
929         rdesc->desc0 = cpu_to_le32(lower_32_bits(rdata->rx_dma));
930         rdesc->desc1 = cpu_to_le32(upper_32_bits(rdata->rx_dma));
931         rdesc->desc2 = 0;
932
933         rdesc->desc3 = 0;
934         if (rdata->interrupt)
935                 XGMAC_SET_BITS_LE(rdesc->desc3, RX_NORMAL_DESC3, INTE, 1);
936
937         /* Since the Rx DMA engine is likely running, make sure everything
938          * is written to the descriptor(s) before setting the OWN bit
939          * for the descriptor
940          */
941         wmb();
942
943         XGMAC_SET_BITS_LE(rdesc->desc3, RX_NORMAL_DESC3, OWN, 1);
944
945         /* Make sure ownership is written to the descriptor */
946         wmb();
947 }
948
949 static void xgbe_rx_desc_init(struct xgbe_channel *channel)
950 {
951         struct xgbe_prv_data *pdata = channel->pdata;
952         struct xgbe_ring *ring = channel->rx_ring;
953         struct xgbe_ring_data *rdata;
954         unsigned int start_index = ring->cur;
955         unsigned int rx_coalesce, rx_frames;
956         unsigned int i;
957
958         DBGPR("-->rx_desc_init\n");
959
960         rx_coalesce = (pdata->rx_riwt || pdata->rx_frames) ? 1 : 0;
961         rx_frames = pdata->rx_frames;
962
963         /* Initialize all descriptors */
964         for (i = 0; i < ring->rdesc_count; i++) {
965                 rdata = XGBE_GET_DESC_DATA(ring, i);
966
967                 /* Set interrupt on completion bit as appropriate */
968                 if (rx_coalesce && (!rx_frames || ((i + 1) % rx_frames)))
969                         rdata->interrupt = 0;
970                 else
971                         rdata->interrupt = 1;
972
973                 /* Initialize Rx descriptor */
974                 xgbe_rx_desc_reset(rdata);
975         }
976
977         /* Update the total number of Rx descriptors */
978         XGMAC_DMA_IOWRITE(channel, DMA_CH_RDRLR, ring->rdesc_count - 1);
979
980         /* Update the starting address of descriptor ring */
981         rdata = XGBE_GET_DESC_DATA(ring, start_index);
982         XGMAC_DMA_IOWRITE(channel, DMA_CH_RDLR_HI,
983                           upper_32_bits(rdata->rdesc_dma));
984         XGMAC_DMA_IOWRITE(channel, DMA_CH_RDLR_LO,
985                           lower_32_bits(rdata->rdesc_dma));
986
987         /* Update the Rx Descriptor Tail Pointer */
988         rdata = XGBE_GET_DESC_DATA(ring, start_index + ring->rdesc_count - 1);
989         XGMAC_DMA_IOWRITE(channel, DMA_CH_RDTR_LO,
990                           lower_32_bits(rdata->rdesc_dma));
991
992         DBGPR("<--rx_desc_init\n");
993 }
994
995 static void xgbe_update_tstamp_addend(struct xgbe_prv_data *pdata,
996                                       unsigned int addend)
997 {
998         /* Set the addend register value and tell the device */
999         XGMAC_IOWRITE(pdata, MAC_TSAR, addend);
1000         XGMAC_IOWRITE_BITS(pdata, MAC_TSCR, TSADDREG, 1);
1001
1002         /* Wait for addend update to complete */
1003         while (XGMAC_IOREAD_BITS(pdata, MAC_TSCR, TSADDREG))
1004                 udelay(5);
1005 }
1006
1007 static void xgbe_set_tstamp_time(struct xgbe_prv_data *pdata, unsigned int sec,
1008                                  unsigned int nsec)
1009 {
1010         /* Set the time values and tell the device */
1011         XGMAC_IOWRITE(pdata, MAC_STSUR, sec);
1012         XGMAC_IOWRITE(pdata, MAC_STNUR, nsec);
1013         XGMAC_IOWRITE_BITS(pdata, MAC_TSCR, TSINIT, 1);
1014
1015         /* Wait for time update to complete */
1016         while (XGMAC_IOREAD_BITS(pdata, MAC_TSCR, TSINIT))
1017                 udelay(5);
1018 }
1019
1020 static u64 xgbe_get_tstamp_time(struct xgbe_prv_data *pdata)
1021 {
1022         u64 nsec;
1023
1024         nsec = XGMAC_IOREAD(pdata, MAC_STSR);
1025         nsec *= NSEC_PER_SEC;
1026         nsec += XGMAC_IOREAD(pdata, MAC_STNR);
1027
1028         return nsec;
1029 }
1030
1031 static u64 xgbe_get_tx_tstamp(struct xgbe_prv_data *pdata)
1032 {
1033         unsigned int tx_snr;
1034         u64 nsec;
1035
1036         tx_snr = XGMAC_IOREAD(pdata, MAC_TXSNR);
1037         if (XGMAC_GET_BITS(tx_snr, MAC_TXSNR, TXTSSTSMIS))
1038                 return 0;
1039
1040         nsec = XGMAC_IOREAD(pdata, MAC_TXSSR);
1041         nsec *= NSEC_PER_SEC;
1042         nsec += tx_snr;
1043
1044         return nsec;
1045 }
1046
1047 static void xgbe_get_rx_tstamp(struct xgbe_packet_data *packet,
1048                                struct xgbe_ring_desc *rdesc)
1049 {
1050         u64 nsec;
1051
1052         if (XGMAC_GET_BITS_LE(rdesc->desc3, RX_CONTEXT_DESC3, TSA) &&
1053             !XGMAC_GET_BITS_LE(rdesc->desc3, RX_CONTEXT_DESC3, TSD)) {
1054                 nsec = le32_to_cpu(rdesc->desc1);
1055                 nsec <<= 32;
1056                 nsec |= le32_to_cpu(rdesc->desc0);
1057                 if (nsec != 0xffffffffffffffffULL) {
1058                         packet->rx_tstamp = nsec;
1059                         XGMAC_SET_BITS(packet->attributes, RX_PACKET_ATTRIBUTES,
1060                                        RX_TSTAMP, 1);
1061                 }
1062         }
1063 }
1064
1065 static int xgbe_config_tstamp(struct xgbe_prv_data *pdata,
1066                               unsigned int mac_tscr)
1067 {
1068         /* Set one nano-second accuracy */
1069         XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSCTRLSSR, 1);
1070
1071         /* Set fine timestamp update */
1072         XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSCFUPDT, 1);
1073
1074         /* Overwrite earlier timestamps */
1075         XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TXTSSTSM, 1);
1076
1077         XGMAC_IOWRITE(pdata, MAC_TSCR, mac_tscr);
1078
1079         /* Exit if timestamping is not enabled */
1080         if (!XGMAC_GET_BITS(mac_tscr, MAC_TSCR, TSENA))
1081                 return 0;
1082
1083         /* Initialize time registers */
1084         XGMAC_IOWRITE_BITS(pdata, MAC_SSIR, SSINC, XGBE_TSTAMP_SSINC);
1085         XGMAC_IOWRITE_BITS(pdata, MAC_SSIR, SNSINC, XGBE_TSTAMP_SNSINC);
1086         xgbe_update_tstamp_addend(pdata, pdata->tstamp_addend);
1087         xgbe_set_tstamp_time(pdata, 0, 0);
1088
1089         /* Initialize the timecounter */
1090         timecounter_init(&pdata->tstamp_tc, &pdata->tstamp_cc,
1091                          ktime_to_ns(ktime_get_real()));
1092
1093         return 0;
1094 }
1095
1096 static void xgbe_config_dcb_tc(struct xgbe_prv_data *pdata)
1097 {
1098         struct ieee_ets *ets = pdata->ets;
1099         unsigned int total_weight, min_weight, weight;
1100         unsigned int i;
1101
1102         if (!ets)
1103                 return;
1104
1105         /* Set Tx to deficit weighted round robin scheduling algorithm (when
1106          * traffic class is using ETS algorithm)
1107          */
1108         XGMAC_IOWRITE_BITS(pdata, MTL_OMR, ETSALG, MTL_ETSALG_DWRR);
1109
1110         /* Set Traffic Class algorithms */
1111         total_weight = pdata->netdev->mtu * pdata->hw_feat.tc_cnt;
1112         min_weight = total_weight / 100;
1113         if (!min_weight)
1114                 min_weight = 1;
1115
1116         for (i = 0; i < pdata->hw_feat.tc_cnt; i++) {
1117                 switch (ets->tc_tsa[i]) {
1118                 case IEEE_8021QAZ_TSA_STRICT:
1119                         DBGPR("  TC%u using SP\n", i);
1120                         XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_TC_ETSCR, TSA,
1121                                                MTL_TSA_SP);
1122                         break;
1123                 case IEEE_8021QAZ_TSA_ETS:
1124                         weight = total_weight * ets->tc_tx_bw[i] / 100;
1125                         weight = clamp(weight, min_weight, total_weight);
1126
1127                         DBGPR("  TC%u using DWRR (weight %u)\n", i, weight);
1128                         XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_TC_ETSCR, TSA,
1129                                                MTL_TSA_ETS);
1130                         XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_TC_QWR, QW,
1131                                                weight);
1132                         break;
1133                 }
1134         }
1135 }
1136
1137 static void xgbe_config_dcb_pfc(struct xgbe_prv_data *pdata)
1138 {
1139         struct ieee_pfc *pfc = pdata->pfc;
1140         struct ieee_ets *ets = pdata->ets;
1141         unsigned int mask, reg, reg_val;
1142         unsigned int tc, prio;
1143
1144         if (!pfc || !ets)
1145                 return;
1146
1147         for (tc = 0; tc < pdata->hw_feat.tc_cnt; tc++) {
1148                 mask = 0;
1149                 for (prio = 0; prio < IEEE_8021QAZ_MAX_TCS; prio++) {
1150                         if ((pfc->pfc_en & (1 << prio)) &&
1151                             (ets->prio_tc[prio] == tc))
1152                                 mask |= (1 << prio);
1153                 }
1154                 mask &= 0xff;
1155
1156                 DBGPR("  TC%u PFC mask=%#x\n", tc, mask);
1157                 reg = MTL_TCPM0R + (MTL_TCPM_INC * (tc / MTL_TCPM_TC_PER_REG));
1158                 reg_val = XGMAC_IOREAD(pdata, reg);
1159
1160                 reg_val &= ~(0xff << ((tc % MTL_TCPM_TC_PER_REG) << 3));
1161                 reg_val |= (mask << ((tc % MTL_TCPM_TC_PER_REG) << 3));
1162
1163                 XGMAC_IOWRITE(pdata, reg, reg_val);
1164         }
1165
1166         xgbe_config_flow_control(pdata);
1167 }
1168
1169 static void xgbe_dev_xmit(struct xgbe_channel *channel)
1170 {
1171         struct xgbe_prv_data *pdata = channel->pdata;
1172         struct xgbe_ring *ring = channel->tx_ring;
1173         struct xgbe_ring_data *rdata;
1174         struct xgbe_ring_desc *rdesc;
1175         struct xgbe_packet_data *packet = &ring->packet_data;
1176         unsigned int csum, tso, vlan;
1177         unsigned int tso_context, vlan_context;
1178         unsigned int tx_coalesce, tx_frames;
1179         int start_index = ring->cur;
1180         int i;
1181
1182         DBGPR("-->xgbe_dev_xmit\n");
1183
1184         csum = XGMAC_GET_BITS(packet->attributes, TX_PACKET_ATTRIBUTES,
1185                               CSUM_ENABLE);
1186         tso = XGMAC_GET_BITS(packet->attributes, TX_PACKET_ATTRIBUTES,
1187                              TSO_ENABLE);
1188         vlan = XGMAC_GET_BITS(packet->attributes, TX_PACKET_ATTRIBUTES,
1189                               VLAN_CTAG);
1190
1191         if (tso && (packet->mss != ring->tx.cur_mss))
1192                 tso_context = 1;
1193         else
1194                 tso_context = 0;
1195
1196         if (vlan && (packet->vlan_ctag != ring->tx.cur_vlan_ctag))
1197                 vlan_context = 1;
1198         else
1199                 vlan_context = 0;
1200
1201         tx_coalesce = (pdata->tx_usecs || pdata->tx_frames) ? 1 : 0;
1202         tx_frames = pdata->tx_frames;
1203         if (tx_coalesce && !channel->tx_timer_active)
1204                 ring->coalesce_count = 0;
1205
1206         rdata = XGBE_GET_DESC_DATA(ring, ring->cur);
1207         rdesc = rdata->rdesc;
1208
1209         /* Create a context descriptor if this is a TSO packet */
1210         if (tso_context || vlan_context) {
1211                 if (tso_context) {
1212                         DBGPR("  TSO context descriptor, mss=%u\n",
1213                               packet->mss);
1214
1215                         /* Set the MSS size */
1216                         XGMAC_SET_BITS_LE(rdesc->desc2, TX_CONTEXT_DESC2,
1217                                           MSS, packet->mss);
1218
1219                         /* Mark it as a CONTEXT descriptor */
1220                         XGMAC_SET_BITS_LE(rdesc->desc3, TX_CONTEXT_DESC3,
1221                                           CTXT, 1);
1222
1223                         /* Indicate this descriptor contains the MSS */
1224                         XGMAC_SET_BITS_LE(rdesc->desc3, TX_CONTEXT_DESC3,
1225                                           TCMSSV, 1);
1226
1227                         ring->tx.cur_mss = packet->mss;
1228                 }
1229
1230                 if (vlan_context) {
1231                         DBGPR("  VLAN context descriptor, ctag=%u\n",
1232                               packet->vlan_ctag);
1233
1234                         /* Mark it as a CONTEXT descriptor */
1235                         XGMAC_SET_BITS_LE(rdesc->desc3, TX_CONTEXT_DESC3,
1236                                           CTXT, 1);
1237
1238                         /* Set the VLAN tag */
1239                         XGMAC_SET_BITS_LE(rdesc->desc3, TX_CONTEXT_DESC3,
1240                                           VT, packet->vlan_ctag);
1241
1242                         /* Indicate this descriptor contains the VLAN tag */
1243                         XGMAC_SET_BITS_LE(rdesc->desc3, TX_CONTEXT_DESC3,
1244                                           VLTV, 1);
1245
1246                         ring->tx.cur_vlan_ctag = packet->vlan_ctag;
1247                 }
1248
1249                 ring->cur++;
1250                 rdata = XGBE_GET_DESC_DATA(ring, ring->cur);
1251                 rdesc = rdata->rdesc;
1252         }
1253
1254         /* Update buffer address (for TSO this is the header) */
1255         rdesc->desc0 =  cpu_to_le32(lower_32_bits(rdata->skb_dma));
1256         rdesc->desc1 =  cpu_to_le32(upper_32_bits(rdata->skb_dma));
1257
1258         /* Update the buffer length */
1259         XGMAC_SET_BITS_LE(rdesc->desc2, TX_NORMAL_DESC2, HL_B1L,
1260                           rdata->skb_dma_len);
1261
1262         /* VLAN tag insertion check */
1263         if (vlan)
1264                 XGMAC_SET_BITS_LE(rdesc->desc2, TX_NORMAL_DESC2, VTIR,
1265                                   TX_NORMAL_DESC2_VLAN_INSERT);
1266
1267         /* Timestamp enablement check */
1268         if (XGMAC_GET_BITS(packet->attributes, TX_PACKET_ATTRIBUTES, PTP))
1269                 XGMAC_SET_BITS_LE(rdesc->desc2, TX_NORMAL_DESC2, TTSE, 1);
1270
1271         /* Set IC bit based on Tx coalescing settings */
1272         XGMAC_SET_BITS_LE(rdesc->desc2, TX_NORMAL_DESC2, IC, 1);
1273         if (tx_coalesce && (!tx_frames ||
1274                             (++ring->coalesce_count % tx_frames)))
1275                 /* Clear IC bit */
1276                 XGMAC_SET_BITS_LE(rdesc->desc2, TX_NORMAL_DESC2, IC, 0);
1277
1278         /* Mark it as First Descriptor */
1279         XGMAC_SET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, FD, 1);
1280
1281         /* Mark it as a NORMAL descriptor */
1282         XGMAC_SET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, CTXT, 0);
1283
1284         /* Set OWN bit if not the first descriptor */
1285         if (ring->cur != start_index)
1286                 XGMAC_SET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, OWN, 1);
1287
1288         if (tso) {
1289                 /* Enable TSO */
1290                 XGMAC_SET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, TSE, 1);
1291                 XGMAC_SET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, TCPPL,
1292                                   packet->tcp_payload_len);
1293                 XGMAC_SET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, TCPHDRLEN,
1294                                   packet->tcp_header_len / 4);
1295         } else {
1296                 /* Enable CRC and Pad Insertion */
1297                 XGMAC_SET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, CPC, 0);
1298
1299                 /* Enable HW CSUM */
1300                 if (csum)
1301                         XGMAC_SET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3,
1302                                           CIC, 0x3);
1303
1304                 /* Set the total length to be transmitted */
1305                 XGMAC_SET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, FL,
1306                                   packet->length);
1307         }
1308
1309         for (i = ring->cur - start_index + 1; i < packet->rdesc_count; i++) {
1310                 ring->cur++;
1311                 rdata = XGBE_GET_DESC_DATA(ring, ring->cur);
1312                 rdesc = rdata->rdesc;
1313
1314                 /* Update buffer address */
1315                 rdesc->desc0 = cpu_to_le32(lower_32_bits(rdata->skb_dma));
1316                 rdesc->desc1 = cpu_to_le32(upper_32_bits(rdata->skb_dma));
1317
1318                 /* Update the buffer length */
1319                 XGMAC_SET_BITS_LE(rdesc->desc2, TX_NORMAL_DESC2, HL_B1L,
1320                                   rdata->skb_dma_len);
1321
1322                 /* Set IC bit based on Tx coalescing settings */
1323                 XGMAC_SET_BITS_LE(rdesc->desc2, TX_NORMAL_DESC2, IC, 1);
1324                 if (tx_coalesce && (!tx_frames ||
1325                                     (++ring->coalesce_count % tx_frames)))
1326                         /* Clear IC bit */
1327                         XGMAC_SET_BITS_LE(rdesc->desc2, TX_NORMAL_DESC2, IC, 0);
1328
1329                 /* Set OWN bit */
1330                 XGMAC_SET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, OWN, 1);
1331
1332                 /* Mark it as NORMAL descriptor */
1333                 XGMAC_SET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, CTXT, 0);
1334
1335                 /* Enable HW CSUM */
1336                 if (csum)
1337                         XGMAC_SET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3,
1338                                           CIC, 0x3);
1339         }
1340
1341         /* Set LAST bit for the last descriptor */
1342         XGMAC_SET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, LD, 1);
1343
1344         /* In case the Tx DMA engine is running, make sure everything
1345          * is written to the descriptor(s) before setting the OWN bit
1346          * for the first descriptor
1347          */
1348         wmb();
1349
1350         /* Set OWN bit for the first descriptor */
1351         rdata = XGBE_GET_DESC_DATA(ring, start_index);
1352         rdesc = rdata->rdesc;
1353         XGMAC_SET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, OWN, 1);
1354
1355 #ifdef XGMAC_ENABLE_TX_DESC_DUMP
1356         xgbe_dump_tx_desc(ring, start_index, packet->rdesc_count, 1);
1357 #endif
1358
1359         /* Make sure ownership is written to the descriptor */
1360         wmb();
1361
1362         /* Issue a poll command to Tx DMA by writing address
1363          * of next immediate free descriptor */
1364         ring->cur++;
1365         rdata = XGBE_GET_DESC_DATA(ring, ring->cur);
1366         XGMAC_DMA_IOWRITE(channel, DMA_CH_TDTR_LO,
1367                           lower_32_bits(rdata->rdesc_dma));
1368
1369         /* Start the Tx coalescing timer */
1370         if (tx_coalesce && !channel->tx_timer_active) {
1371                 channel->tx_timer_active = 1;
1372                 hrtimer_start(&channel->tx_timer,
1373                               ktime_set(0, pdata->tx_usecs * NSEC_PER_USEC),
1374                               HRTIMER_MODE_REL);
1375         }
1376
1377         DBGPR("  %s: descriptors %u to %u written\n",
1378               channel->name, start_index & (ring->rdesc_count - 1),
1379               (ring->cur - 1) & (ring->rdesc_count - 1));
1380
1381         DBGPR("<--xgbe_dev_xmit\n");
1382 }
1383
1384 static int xgbe_dev_read(struct xgbe_channel *channel)
1385 {
1386         struct xgbe_ring *ring = channel->rx_ring;
1387         struct xgbe_ring_data *rdata;
1388         struct xgbe_ring_desc *rdesc;
1389         struct xgbe_packet_data *packet = &ring->packet_data;
1390         struct net_device *netdev = channel->pdata->netdev;
1391         unsigned int err, etlt;
1392
1393         DBGPR("-->xgbe_dev_read: cur = %d\n", ring->cur);
1394
1395         rdata = XGBE_GET_DESC_DATA(ring, ring->cur);
1396         rdesc = rdata->rdesc;
1397
1398         /* Check for data availability */
1399         if (XGMAC_GET_BITS_LE(rdesc->desc3, RX_NORMAL_DESC3, OWN))
1400                 return 1;
1401
1402 #ifdef XGMAC_ENABLE_RX_DESC_DUMP
1403         xgbe_dump_rx_desc(ring, rdesc, ring->cur);
1404 #endif
1405
1406         if (XGMAC_GET_BITS_LE(rdesc->desc3, RX_NORMAL_DESC3, CTXT)) {
1407                 /* Timestamp Context Descriptor */
1408                 xgbe_get_rx_tstamp(packet, rdesc);
1409
1410                 XGMAC_SET_BITS(packet->attributes, RX_PACKET_ATTRIBUTES,
1411                                CONTEXT, 1);
1412                 XGMAC_SET_BITS(packet->attributes, RX_PACKET_ATTRIBUTES,
1413                                CONTEXT_NEXT, 0);
1414                 return 0;
1415         }
1416
1417         /* Normal Descriptor, be sure Context Descriptor bit is off */
1418         XGMAC_SET_BITS(packet->attributes, RX_PACKET_ATTRIBUTES, CONTEXT, 0);
1419
1420         /* Indicate if a Context Descriptor is next */
1421         if (XGMAC_GET_BITS_LE(rdesc->desc3, RX_NORMAL_DESC3, CDA))
1422                 XGMAC_SET_BITS(packet->attributes, RX_PACKET_ATTRIBUTES,
1423                                CONTEXT_NEXT, 1);
1424
1425         /* Get the packet length */
1426         rdata->len = XGMAC_GET_BITS_LE(rdesc->desc3, RX_NORMAL_DESC3, PL);
1427
1428         if (!XGMAC_GET_BITS_LE(rdesc->desc3, RX_NORMAL_DESC3, LD)) {
1429                 /* Not all the data has been transferred for this packet */
1430                 XGMAC_SET_BITS(packet->attributes, RX_PACKET_ATTRIBUTES,
1431                                INCOMPLETE, 1);
1432                 return 0;
1433         }
1434
1435         /* This is the last of the data for this packet */
1436         XGMAC_SET_BITS(packet->attributes, RX_PACKET_ATTRIBUTES,
1437                        INCOMPLETE, 0);
1438
1439         /* Set checksum done indicator as appropriate */
1440         if (channel->pdata->netdev->features & NETIF_F_RXCSUM)
1441                 XGMAC_SET_BITS(packet->attributes, RX_PACKET_ATTRIBUTES,
1442                                CSUM_DONE, 1);
1443
1444         /* Check for errors (only valid in last descriptor) */
1445         err = XGMAC_GET_BITS_LE(rdesc->desc3, RX_NORMAL_DESC3, ES);
1446         etlt = XGMAC_GET_BITS_LE(rdesc->desc3, RX_NORMAL_DESC3, ETLT);
1447         DBGPR("  err=%u, etlt=%#x\n", err, etlt);
1448
1449         if (!err || (err && !etlt)) {
1450                 if ((etlt == 0x09) &&
1451                     (netdev->features & NETIF_F_HW_VLAN_CTAG_RX)) {
1452                         XGMAC_SET_BITS(packet->attributes, RX_PACKET_ATTRIBUTES,
1453                                        VLAN_CTAG, 1);
1454                         packet->vlan_ctag = XGMAC_GET_BITS_LE(rdesc->desc0,
1455                                                               RX_NORMAL_DESC0,
1456                                                               OVT);
1457                         DBGPR("  vlan-ctag=0x%04x\n", packet->vlan_ctag);
1458                 }
1459         } else {
1460                 if ((etlt == 0x05) || (etlt == 0x06))
1461                         XGMAC_SET_BITS(packet->attributes, RX_PACKET_ATTRIBUTES,
1462                                        CSUM_DONE, 0);
1463                 else
1464                         XGMAC_SET_BITS(packet->errors, RX_PACKET_ERRORS,
1465                                        FRAME, 1);
1466         }
1467
1468         DBGPR("<--xgbe_dev_read: %s - descriptor=%u (cur=%d)\n", channel->name,
1469               ring->cur & (ring->rdesc_count - 1), ring->cur);
1470
1471         return 0;
1472 }
1473
1474 static int xgbe_is_context_desc(struct xgbe_ring_desc *rdesc)
1475 {
1476         /* Rx and Tx share CTXT bit, so check TDES3.CTXT bit */
1477         return XGMAC_GET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, CTXT);
1478 }
1479
1480 static int xgbe_is_last_desc(struct xgbe_ring_desc *rdesc)
1481 {
1482         /* Rx and Tx share LD bit, so check TDES3.LD bit */
1483         return XGMAC_GET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, LD);
1484 }
1485
1486 static int xgbe_enable_int(struct xgbe_channel *channel,
1487                            enum xgbe_int int_id)
1488 {
1489         unsigned int dma_ch_ier;
1490
1491         dma_ch_ier = XGMAC_DMA_IOREAD(channel, DMA_CH_IER);
1492
1493         switch (int_id) {
1494         case XGMAC_INT_DMA_CH_SR_TI:
1495                 XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, TIE, 1);
1496                 break;
1497         case XGMAC_INT_DMA_CH_SR_TPS:
1498                 XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, TXSE, 1);
1499                 break;
1500         case XGMAC_INT_DMA_CH_SR_TBU:
1501                 XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, TBUE, 1);
1502                 break;
1503         case XGMAC_INT_DMA_CH_SR_RI:
1504                 XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, RIE, 1);
1505                 break;
1506         case XGMAC_INT_DMA_CH_SR_RBU:
1507                 XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, RBUE, 1);
1508                 break;
1509         case XGMAC_INT_DMA_CH_SR_RPS:
1510                 XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, RSE, 1);
1511                 break;
1512         case XGMAC_INT_DMA_CH_SR_TI_RI:
1513                 XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, TIE, 1);
1514                 XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, RIE, 1);
1515                 break;
1516         case XGMAC_INT_DMA_CH_SR_FBE:
1517                 XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, FBEE, 1);
1518                 break;
1519         case XGMAC_INT_DMA_ALL:
1520                 dma_ch_ier |= channel->saved_ier;
1521                 break;
1522         default:
1523                 return -1;
1524         }
1525
1526         XGMAC_DMA_IOWRITE(channel, DMA_CH_IER, dma_ch_ier);
1527
1528         return 0;
1529 }
1530
1531 static int xgbe_disable_int(struct xgbe_channel *channel,
1532                             enum xgbe_int int_id)
1533 {
1534         unsigned int dma_ch_ier;
1535
1536         dma_ch_ier = XGMAC_DMA_IOREAD(channel, DMA_CH_IER);
1537
1538         switch (int_id) {
1539         case XGMAC_INT_DMA_CH_SR_TI:
1540                 XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, TIE, 0);
1541                 break;
1542         case XGMAC_INT_DMA_CH_SR_TPS:
1543                 XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, TXSE, 0);
1544                 break;
1545         case XGMAC_INT_DMA_CH_SR_TBU:
1546                 XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, TBUE, 0);
1547                 break;
1548         case XGMAC_INT_DMA_CH_SR_RI:
1549                 XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, RIE, 0);
1550                 break;
1551         case XGMAC_INT_DMA_CH_SR_RBU:
1552                 XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, RBUE, 0);
1553                 break;
1554         case XGMAC_INT_DMA_CH_SR_RPS:
1555                 XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, RSE, 0);
1556                 break;
1557         case XGMAC_INT_DMA_CH_SR_TI_RI:
1558                 XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, TIE, 0);
1559                 XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, RIE, 0);
1560                 break;
1561         case XGMAC_INT_DMA_CH_SR_FBE:
1562                 XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, FBEE, 0);
1563                 break;
1564         case XGMAC_INT_DMA_ALL:
1565                 channel->saved_ier = dma_ch_ier & XGBE_DMA_INTERRUPT_MASK;
1566                 dma_ch_ier &= ~XGBE_DMA_INTERRUPT_MASK;
1567                 break;
1568         default:
1569                 return -1;
1570         }
1571
1572         XGMAC_DMA_IOWRITE(channel, DMA_CH_IER, dma_ch_ier);
1573
1574         return 0;
1575 }
1576
1577 static int xgbe_exit(struct xgbe_prv_data *pdata)
1578 {
1579         unsigned int count = 2000;
1580
1581         DBGPR("-->xgbe_exit\n");
1582
1583         /* Issue a software reset */
1584         XGMAC_IOWRITE_BITS(pdata, DMA_MR, SWR, 1);
1585         usleep_range(10, 15);
1586
1587         /* Poll Until Poll Condition */
1588         while (count-- && XGMAC_IOREAD_BITS(pdata, DMA_MR, SWR))
1589                 usleep_range(500, 600);
1590
1591         if (!count)
1592                 return -EBUSY;
1593
1594         DBGPR("<--xgbe_exit\n");
1595
1596         return 0;
1597 }
1598
1599 static int xgbe_flush_tx_queues(struct xgbe_prv_data *pdata)
1600 {
1601         unsigned int i, count;
1602
1603         if (XGMAC_GET_BITS(pdata->hw_feat.version, MAC_VR, SNPSVER) < 0x21)
1604                 return 0;
1605
1606         for (i = 0; i < pdata->tx_q_count; i++)
1607                 XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_Q_TQOMR, FTQ, 1);
1608
1609         /* Poll Until Poll Condition */
1610         for (i = 0; i < pdata->tx_q_count; i++) {
1611                 count = 2000;
1612                 while (count-- && XGMAC_MTL_IOREAD_BITS(pdata, i,
1613                                                         MTL_Q_TQOMR, FTQ))
1614                         usleep_range(500, 600);
1615
1616                 if (!count)
1617                         return -EBUSY;
1618         }
1619
1620         return 0;
1621 }
1622
1623 static void xgbe_config_dma_bus(struct xgbe_prv_data *pdata)
1624 {
1625         /* Set enhanced addressing mode */
1626         XGMAC_IOWRITE_BITS(pdata, DMA_SBMR, EAME, 1);
1627
1628         /* Set the System Bus mode */
1629         XGMAC_IOWRITE_BITS(pdata, DMA_SBMR, UNDEF, 1);
1630         XGMAC_IOWRITE_BITS(pdata, DMA_SBMR, BLEN_256, 1);
1631 }
1632
1633 static void xgbe_config_dma_cache(struct xgbe_prv_data *pdata)
1634 {
1635         unsigned int arcache, awcache;
1636
1637         arcache = 0;
1638         XGMAC_SET_BITS(arcache, DMA_AXIARCR, DRC, pdata->arcache);
1639         XGMAC_SET_BITS(arcache, DMA_AXIARCR, DRD, pdata->axdomain);
1640         XGMAC_SET_BITS(arcache, DMA_AXIARCR, TEC, pdata->arcache);
1641         XGMAC_SET_BITS(arcache, DMA_AXIARCR, TED, pdata->axdomain);
1642         XGMAC_SET_BITS(arcache, DMA_AXIARCR, THC, pdata->arcache);
1643         XGMAC_SET_BITS(arcache, DMA_AXIARCR, THD, pdata->axdomain);
1644         XGMAC_IOWRITE(pdata, DMA_AXIARCR, arcache);
1645
1646         awcache = 0;
1647         XGMAC_SET_BITS(awcache, DMA_AXIAWCR, DWC, pdata->awcache);
1648         XGMAC_SET_BITS(awcache, DMA_AXIAWCR, DWD, pdata->axdomain);
1649         XGMAC_SET_BITS(awcache, DMA_AXIAWCR, RPC, pdata->awcache);
1650         XGMAC_SET_BITS(awcache, DMA_AXIAWCR, RPD, pdata->axdomain);
1651         XGMAC_SET_BITS(awcache, DMA_AXIAWCR, RHC, pdata->awcache);
1652         XGMAC_SET_BITS(awcache, DMA_AXIAWCR, RHD, pdata->axdomain);
1653         XGMAC_SET_BITS(awcache, DMA_AXIAWCR, TDC, pdata->awcache);
1654         XGMAC_SET_BITS(awcache, DMA_AXIAWCR, TDD, pdata->axdomain);
1655         XGMAC_IOWRITE(pdata, DMA_AXIAWCR, awcache);
1656 }
1657
1658 static void xgbe_config_mtl_mode(struct xgbe_prv_data *pdata)
1659 {
1660         unsigned int i;
1661
1662         /* Set Tx to weighted round robin scheduling algorithm */
1663         XGMAC_IOWRITE_BITS(pdata, MTL_OMR, ETSALG, MTL_ETSALG_WRR);
1664
1665         /* Set Tx traffic classes to use WRR algorithm with equal weights */
1666         for (i = 0; i < pdata->hw_feat.tc_cnt; i++) {
1667                 XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_TC_ETSCR, TSA,
1668                                        MTL_TSA_ETS);
1669                 XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_TC_QWR, QW, 1);
1670         }
1671
1672         /* Set Rx to strict priority algorithm */
1673         XGMAC_IOWRITE_BITS(pdata, MTL_OMR, RAA, MTL_RAA_SP);
1674 }
1675
1676 static unsigned int xgbe_calculate_per_queue_fifo(unsigned int fifo_size,
1677                                                   unsigned int queue_count)
1678 {
1679         unsigned int q_fifo_size = 0;
1680         enum xgbe_mtl_fifo_size p_fifo = XGMAC_MTL_FIFO_SIZE_256;
1681
1682         /* Calculate Tx/Rx fifo share per queue */
1683         switch (fifo_size) {
1684         case 0:
1685                 q_fifo_size = XGBE_FIFO_SIZE_B(128);
1686                 break;
1687         case 1:
1688                 q_fifo_size = XGBE_FIFO_SIZE_B(256);
1689                 break;
1690         case 2:
1691                 q_fifo_size = XGBE_FIFO_SIZE_B(512);
1692                 break;
1693         case 3:
1694                 q_fifo_size = XGBE_FIFO_SIZE_KB(1);
1695                 break;
1696         case 4:
1697                 q_fifo_size = XGBE_FIFO_SIZE_KB(2);
1698                 break;
1699         case 5:
1700                 q_fifo_size = XGBE_FIFO_SIZE_KB(4);
1701                 break;
1702         case 6:
1703                 q_fifo_size = XGBE_FIFO_SIZE_KB(8);
1704                 break;
1705         case 7:
1706                 q_fifo_size = XGBE_FIFO_SIZE_KB(16);
1707                 break;
1708         case 8:
1709                 q_fifo_size = XGBE_FIFO_SIZE_KB(32);
1710                 break;
1711         case 9:
1712                 q_fifo_size = XGBE_FIFO_SIZE_KB(64);
1713                 break;
1714         case 10:
1715                 q_fifo_size = XGBE_FIFO_SIZE_KB(128);
1716                 break;
1717         case 11:
1718                 q_fifo_size = XGBE_FIFO_SIZE_KB(256);
1719                 break;
1720         }
1721
1722         /* The configured value is not the actual amount of fifo RAM */
1723         q_fifo_size = min_t(unsigned int, XGBE_FIFO_MAX, q_fifo_size);
1724
1725         q_fifo_size = q_fifo_size / queue_count;
1726
1727         /* Set the queue fifo size programmable value */
1728         if (q_fifo_size >= XGBE_FIFO_SIZE_KB(256))
1729                 p_fifo = XGMAC_MTL_FIFO_SIZE_256K;
1730         else if (q_fifo_size >= XGBE_FIFO_SIZE_KB(128))
1731                 p_fifo = XGMAC_MTL_FIFO_SIZE_128K;
1732         else if (q_fifo_size >= XGBE_FIFO_SIZE_KB(64))
1733                 p_fifo = XGMAC_MTL_FIFO_SIZE_64K;
1734         else if (q_fifo_size >= XGBE_FIFO_SIZE_KB(32))
1735                 p_fifo = XGMAC_MTL_FIFO_SIZE_32K;
1736         else if (q_fifo_size >= XGBE_FIFO_SIZE_KB(16))
1737                 p_fifo = XGMAC_MTL_FIFO_SIZE_16K;
1738         else if (q_fifo_size >= XGBE_FIFO_SIZE_KB(8))
1739                 p_fifo = XGMAC_MTL_FIFO_SIZE_8K;
1740         else if (q_fifo_size >= XGBE_FIFO_SIZE_KB(4))
1741                 p_fifo = XGMAC_MTL_FIFO_SIZE_4K;
1742         else if (q_fifo_size >= XGBE_FIFO_SIZE_KB(2))
1743                 p_fifo = XGMAC_MTL_FIFO_SIZE_2K;
1744         else if (q_fifo_size >= XGBE_FIFO_SIZE_KB(1))
1745                 p_fifo = XGMAC_MTL_FIFO_SIZE_1K;
1746         else if (q_fifo_size >= XGBE_FIFO_SIZE_B(512))
1747                 p_fifo = XGMAC_MTL_FIFO_SIZE_512;
1748         else if (q_fifo_size >= XGBE_FIFO_SIZE_B(256))
1749                 p_fifo = XGMAC_MTL_FIFO_SIZE_256;
1750
1751         return p_fifo;
1752 }
1753
1754 static void xgbe_config_tx_fifo_size(struct xgbe_prv_data *pdata)
1755 {
1756         enum xgbe_mtl_fifo_size fifo_size;
1757         unsigned int i;
1758
1759         fifo_size = xgbe_calculate_per_queue_fifo(pdata->hw_feat.tx_fifo_size,
1760                                                   pdata->tx_q_count);
1761
1762         for (i = 0; i < pdata->tx_q_count; i++)
1763                 XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_Q_TQOMR, TQS, fifo_size);
1764
1765         netdev_notice(pdata->netdev, "%d Tx queues, %d byte fifo per queue\n",
1766                       pdata->tx_q_count, ((fifo_size + 1) * 256));
1767 }
1768
1769 static void xgbe_config_rx_fifo_size(struct xgbe_prv_data *pdata)
1770 {
1771         enum xgbe_mtl_fifo_size fifo_size;
1772         unsigned int i;
1773
1774         fifo_size = xgbe_calculate_per_queue_fifo(pdata->hw_feat.rx_fifo_size,
1775                                                   pdata->rx_q_count);
1776
1777         for (i = 0; i < pdata->rx_q_count; i++)
1778                 XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_Q_RQOMR, RQS, fifo_size);
1779
1780         netdev_notice(pdata->netdev, "%d Rx queues, %d byte fifo per queue\n",
1781                       pdata->rx_q_count, ((fifo_size + 1) * 256));
1782 }
1783
1784 static void xgbe_config_queue_mapping(struct xgbe_prv_data *pdata)
1785 {
1786         unsigned int qptc, qptc_extra, queue;
1787         unsigned int prio_queues;
1788         unsigned int ppq, ppq_extra, prio;
1789         unsigned int mask;
1790         unsigned int i, j, reg, reg_val;
1791
1792         /* Map the MTL Tx Queues to Traffic Classes
1793          *   Note: Tx Queues >= Traffic Classes
1794          */
1795         qptc = pdata->tx_q_count / pdata->hw_feat.tc_cnt;
1796         qptc_extra = pdata->tx_q_count % pdata->hw_feat.tc_cnt;
1797
1798         for (i = 0, queue = 0; i < pdata->hw_feat.tc_cnt; i++) {
1799                 for (j = 0; j < qptc; j++) {
1800                         DBGPR("  TXq%u mapped to TC%u\n", queue, i);
1801                         XGMAC_MTL_IOWRITE_BITS(pdata, queue, MTL_Q_TQOMR,
1802                                                Q2TCMAP, i);
1803                         pdata->q2tc_map[queue++] = i;
1804                 }
1805
1806                 if (i < qptc_extra) {
1807                         DBGPR("  TXq%u mapped to TC%u\n", queue, i);
1808                         XGMAC_MTL_IOWRITE_BITS(pdata, queue, MTL_Q_TQOMR,
1809                                                Q2TCMAP, i);
1810                         pdata->q2tc_map[queue++] = i;
1811                 }
1812         }
1813
1814         /* Map the 8 VLAN priority values to available MTL Rx queues */
1815         prio_queues = min_t(unsigned int, IEEE_8021QAZ_MAX_TCS,
1816                             pdata->rx_q_count);
1817         ppq = IEEE_8021QAZ_MAX_TCS / prio_queues;
1818         ppq_extra = IEEE_8021QAZ_MAX_TCS % prio_queues;
1819
1820         reg = MAC_RQC2R;
1821         reg_val = 0;
1822         for (i = 0, prio = 0; i < prio_queues;) {
1823                 mask = 0;
1824                 for (j = 0; j < ppq; j++) {
1825                         DBGPR("  PRIO%u mapped to RXq%u\n", prio, i);
1826                         mask |= (1 << prio);
1827                         pdata->prio2q_map[prio++] = i;
1828                 }
1829
1830                 if (i < ppq_extra) {
1831                         DBGPR("  PRIO%u mapped to RXq%u\n", prio, i);
1832                         mask |= (1 << prio);
1833                         pdata->prio2q_map[prio++] = i;
1834                 }
1835
1836                 reg_val |= (mask << ((i++ % MAC_RQC2_Q_PER_REG) << 3));
1837
1838                 if ((i % MAC_RQC2_Q_PER_REG) && (i != prio_queues))
1839                         continue;
1840
1841                 XGMAC_IOWRITE(pdata, reg, reg_val);
1842                 reg += MAC_RQC2_INC;
1843                 reg_val = 0;
1844         }
1845
1846         /* Select dynamic mapping of MTL Rx queue to DMA Rx channel */
1847         reg = MTL_RQDCM0R;
1848         reg_val = 0;
1849         for (i = 0; i < pdata->rx_q_count;) {
1850                 reg_val |= (0x80 << ((i++ % MTL_RQDCM_Q_PER_REG) << 3));
1851
1852                 if ((i % MTL_RQDCM_Q_PER_REG) && (i != pdata->rx_q_count))
1853                         continue;
1854
1855                 XGMAC_IOWRITE(pdata, reg, reg_val);
1856
1857                 reg += MTL_RQDCM_INC;
1858                 reg_val = 0;
1859         }
1860 }
1861
1862 static void xgbe_config_flow_control_threshold(struct xgbe_prv_data *pdata)
1863 {
1864         unsigned int i;
1865
1866         for (i = 0; i < pdata->rx_q_count; i++) {
1867                 /* Activate flow control when less than 4k left in fifo */
1868                 XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_Q_RQOMR, RFA, 2);
1869
1870                 /* De-activate flow control when more than 6k left in fifo */
1871                 XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_Q_RQOMR, RFD, 4);
1872         }
1873 }
1874
1875 static void xgbe_config_mac_address(struct xgbe_prv_data *pdata)
1876 {
1877         xgbe_set_mac_address(pdata, pdata->netdev->dev_addr);
1878
1879         /* Filtering is done using perfect filtering and hash filtering */
1880         if (pdata->hw_feat.hash_table_size) {
1881                 XGMAC_IOWRITE_BITS(pdata, MAC_PFR, HPF, 1);
1882                 XGMAC_IOWRITE_BITS(pdata, MAC_PFR, HUC, 1);
1883                 XGMAC_IOWRITE_BITS(pdata, MAC_PFR, HMC, 1);
1884         }
1885 }
1886
1887 static void xgbe_config_jumbo_enable(struct xgbe_prv_data *pdata)
1888 {
1889         unsigned int val;
1890
1891         val = (pdata->netdev->mtu > XGMAC_STD_PACKET_MTU) ? 1 : 0;
1892
1893         XGMAC_IOWRITE_BITS(pdata, MAC_RCR, JE, val);
1894 }
1895
1896 static void xgbe_config_checksum_offload(struct xgbe_prv_data *pdata)
1897 {
1898         if (pdata->netdev->features & NETIF_F_RXCSUM)
1899                 xgbe_enable_rx_csum(pdata);
1900         else
1901                 xgbe_disable_rx_csum(pdata);
1902 }
1903
1904 static void xgbe_config_vlan_support(struct xgbe_prv_data *pdata)
1905 {
1906         /* Indicate that VLAN Tx CTAGs come from context descriptors */
1907         XGMAC_IOWRITE_BITS(pdata, MAC_VLANIR, CSVL, 0);
1908         XGMAC_IOWRITE_BITS(pdata, MAC_VLANIR, VLTI, 1);
1909
1910         /* Set the current VLAN Hash Table register value */
1911         xgbe_update_vlan_hash_table(pdata);
1912
1913         if (pdata->netdev->features & NETIF_F_HW_VLAN_CTAG_FILTER)
1914                 xgbe_enable_rx_vlan_filtering(pdata);
1915         else
1916                 xgbe_disable_rx_vlan_filtering(pdata);
1917
1918         if (pdata->netdev->features & NETIF_F_HW_VLAN_CTAG_RX)
1919                 xgbe_enable_rx_vlan_stripping(pdata);
1920         else
1921                 xgbe_disable_rx_vlan_stripping(pdata);
1922 }
1923
1924 static u64 xgbe_mmc_read(struct xgbe_prv_data *pdata, unsigned int reg_lo)
1925 {
1926         bool read_hi;
1927         u64 val;
1928
1929         switch (reg_lo) {
1930         /* These registers are always 64 bit */
1931         case MMC_TXOCTETCOUNT_GB_LO:
1932         case MMC_TXOCTETCOUNT_G_LO:
1933         case MMC_RXOCTETCOUNT_GB_LO:
1934         case MMC_RXOCTETCOUNT_G_LO:
1935                 read_hi = true;
1936                 break;
1937
1938         default:
1939                 read_hi = false;
1940         };
1941
1942         val = XGMAC_IOREAD(pdata, reg_lo);
1943
1944         if (read_hi)
1945                 val |= ((u64)XGMAC_IOREAD(pdata, reg_lo + 4) << 32);
1946
1947         return val;
1948 }
1949
1950 static void xgbe_tx_mmc_int(struct xgbe_prv_data *pdata)
1951 {
1952         struct xgbe_mmc_stats *stats = &pdata->mmc_stats;
1953         unsigned int mmc_isr = XGMAC_IOREAD(pdata, MMC_TISR);
1954
1955         if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TXOCTETCOUNT_GB))
1956                 stats->txoctetcount_gb +=
1957                         xgbe_mmc_read(pdata, MMC_TXOCTETCOUNT_GB_LO);
1958
1959         if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TXFRAMECOUNT_GB))
1960                 stats->txframecount_gb +=
1961                         xgbe_mmc_read(pdata, MMC_TXFRAMECOUNT_GB_LO);
1962
1963         if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TXBROADCASTFRAMES_G))
1964                 stats->txbroadcastframes_g +=
1965                         xgbe_mmc_read(pdata, MMC_TXBROADCASTFRAMES_G_LO);
1966
1967         if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TXMULTICASTFRAMES_G))
1968                 stats->txmulticastframes_g +=
1969                         xgbe_mmc_read(pdata, MMC_TXMULTICASTFRAMES_G_LO);
1970
1971         if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TX64OCTETS_GB))
1972                 stats->tx64octets_gb +=
1973                         xgbe_mmc_read(pdata, MMC_TX64OCTETS_GB_LO);
1974
1975         if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TX65TO127OCTETS_GB))
1976                 stats->tx65to127octets_gb +=
1977                         xgbe_mmc_read(pdata, MMC_TX65TO127OCTETS_GB_LO);
1978
1979         if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TX128TO255OCTETS_GB))
1980                 stats->tx128to255octets_gb +=
1981                         xgbe_mmc_read(pdata, MMC_TX128TO255OCTETS_GB_LO);
1982
1983         if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TX256TO511OCTETS_GB))
1984                 stats->tx256to511octets_gb +=
1985                         xgbe_mmc_read(pdata, MMC_TX256TO511OCTETS_GB_LO);
1986
1987         if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TX512TO1023OCTETS_GB))
1988                 stats->tx512to1023octets_gb +=
1989                         xgbe_mmc_read(pdata, MMC_TX512TO1023OCTETS_GB_LO);
1990
1991         if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TX1024TOMAXOCTETS_GB))
1992                 stats->tx1024tomaxoctets_gb +=
1993                         xgbe_mmc_read(pdata, MMC_TX1024TOMAXOCTETS_GB_LO);
1994
1995         if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TXUNICASTFRAMES_GB))
1996                 stats->txunicastframes_gb +=
1997                         xgbe_mmc_read(pdata, MMC_TXUNICASTFRAMES_GB_LO);
1998
1999         if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TXMULTICASTFRAMES_GB))
2000                 stats->txmulticastframes_gb +=
2001                         xgbe_mmc_read(pdata, MMC_TXMULTICASTFRAMES_GB_LO);
2002
2003         if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TXBROADCASTFRAMES_GB))
2004                 stats->txbroadcastframes_g +=
2005                         xgbe_mmc_read(pdata, MMC_TXBROADCASTFRAMES_GB_LO);
2006
2007         if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TXUNDERFLOWERROR))
2008                 stats->txunderflowerror +=
2009                         xgbe_mmc_read(pdata, MMC_TXUNDERFLOWERROR_LO);
2010
2011         if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TXOCTETCOUNT_G))
2012                 stats->txoctetcount_g +=
2013                         xgbe_mmc_read(pdata, MMC_TXOCTETCOUNT_G_LO);
2014
2015         if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TXFRAMECOUNT_G))
2016                 stats->txframecount_g +=
2017                         xgbe_mmc_read(pdata, MMC_TXFRAMECOUNT_G_LO);
2018
2019         if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TXPAUSEFRAMES))
2020                 stats->txpauseframes +=
2021                         xgbe_mmc_read(pdata, MMC_TXPAUSEFRAMES_LO);
2022
2023         if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TXVLANFRAMES_G))
2024                 stats->txvlanframes_g +=
2025                         xgbe_mmc_read(pdata, MMC_TXVLANFRAMES_G_LO);
2026 }
2027
2028 static void xgbe_rx_mmc_int(struct xgbe_prv_data *pdata)
2029 {
2030         struct xgbe_mmc_stats *stats = &pdata->mmc_stats;
2031         unsigned int mmc_isr = XGMAC_IOREAD(pdata, MMC_RISR);
2032
2033         if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXFRAMECOUNT_GB))
2034                 stats->rxframecount_gb +=
2035                         xgbe_mmc_read(pdata, MMC_RXFRAMECOUNT_GB_LO);
2036
2037         if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXOCTETCOUNT_GB))
2038                 stats->rxoctetcount_gb +=
2039                         xgbe_mmc_read(pdata, MMC_RXOCTETCOUNT_GB_LO);
2040
2041         if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXOCTETCOUNT_G))
2042                 stats->rxoctetcount_g +=
2043                         xgbe_mmc_read(pdata, MMC_RXOCTETCOUNT_G_LO);
2044
2045         if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXBROADCASTFRAMES_G))
2046                 stats->rxbroadcastframes_g +=
2047                         xgbe_mmc_read(pdata, MMC_RXBROADCASTFRAMES_G_LO);
2048
2049         if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXMULTICASTFRAMES_G))
2050                 stats->rxmulticastframes_g +=
2051                         xgbe_mmc_read(pdata, MMC_RXMULTICASTFRAMES_G_LO);
2052
2053         if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXCRCERROR))
2054                 stats->rxcrcerror +=
2055                         xgbe_mmc_read(pdata, MMC_RXCRCERROR_LO);
2056
2057         if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXRUNTERROR))
2058                 stats->rxrunterror +=
2059                         xgbe_mmc_read(pdata, MMC_RXRUNTERROR);
2060
2061         if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXJABBERERROR))
2062                 stats->rxjabbererror +=
2063                         xgbe_mmc_read(pdata, MMC_RXJABBERERROR);
2064
2065         if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXUNDERSIZE_G))
2066                 stats->rxundersize_g +=
2067                         xgbe_mmc_read(pdata, MMC_RXUNDERSIZE_G);
2068
2069         if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXOVERSIZE_G))
2070                 stats->rxoversize_g +=
2071                         xgbe_mmc_read(pdata, MMC_RXOVERSIZE_G);
2072
2073         if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RX64OCTETS_GB))
2074                 stats->rx64octets_gb +=
2075                         xgbe_mmc_read(pdata, MMC_RX64OCTETS_GB_LO);
2076
2077         if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RX65TO127OCTETS_GB))
2078                 stats->rx65to127octets_gb +=
2079                         xgbe_mmc_read(pdata, MMC_RX65TO127OCTETS_GB_LO);
2080
2081         if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RX128TO255OCTETS_GB))
2082                 stats->rx128to255octets_gb +=
2083                         xgbe_mmc_read(pdata, MMC_RX128TO255OCTETS_GB_LO);
2084
2085         if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RX256TO511OCTETS_GB))
2086                 stats->rx256to511octets_gb +=
2087                         xgbe_mmc_read(pdata, MMC_RX256TO511OCTETS_GB_LO);
2088
2089         if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RX512TO1023OCTETS_GB))
2090                 stats->rx512to1023octets_gb +=
2091                         xgbe_mmc_read(pdata, MMC_RX512TO1023OCTETS_GB_LO);
2092
2093         if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RX1024TOMAXOCTETS_GB))
2094                 stats->rx1024tomaxoctets_gb +=
2095                         xgbe_mmc_read(pdata, MMC_RX1024TOMAXOCTETS_GB_LO);
2096
2097         if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXUNICASTFRAMES_G))
2098                 stats->rxunicastframes_g +=
2099                         xgbe_mmc_read(pdata, MMC_RXUNICASTFRAMES_G_LO);
2100
2101         if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXLENGTHERROR))
2102                 stats->rxlengtherror +=
2103                         xgbe_mmc_read(pdata, MMC_RXLENGTHERROR_LO);
2104
2105         if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXOUTOFRANGETYPE))
2106                 stats->rxoutofrangetype +=
2107                         xgbe_mmc_read(pdata, MMC_RXOUTOFRANGETYPE_LO);
2108
2109         if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXPAUSEFRAMES))
2110                 stats->rxpauseframes +=
2111                         xgbe_mmc_read(pdata, MMC_RXPAUSEFRAMES_LO);
2112
2113         if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXFIFOOVERFLOW))
2114                 stats->rxfifooverflow +=
2115                         xgbe_mmc_read(pdata, MMC_RXFIFOOVERFLOW_LO);
2116
2117         if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXVLANFRAMES_GB))
2118                 stats->rxvlanframes_gb +=
2119                         xgbe_mmc_read(pdata, MMC_RXVLANFRAMES_GB_LO);
2120
2121         if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXWATCHDOGERROR))
2122                 stats->rxwatchdogerror +=
2123                         xgbe_mmc_read(pdata, MMC_RXWATCHDOGERROR);
2124 }
2125
2126 static void xgbe_read_mmc_stats(struct xgbe_prv_data *pdata)
2127 {
2128         struct xgbe_mmc_stats *stats = &pdata->mmc_stats;
2129
2130         /* Freeze counters */
2131         XGMAC_IOWRITE_BITS(pdata, MMC_CR, MCF, 1);
2132
2133         stats->txoctetcount_gb +=
2134                 xgbe_mmc_read(pdata, MMC_TXOCTETCOUNT_GB_LO);
2135
2136         stats->txframecount_gb +=
2137                 xgbe_mmc_read(pdata, MMC_TXFRAMECOUNT_GB_LO);
2138
2139         stats->txbroadcastframes_g +=
2140                 xgbe_mmc_read(pdata, MMC_TXBROADCASTFRAMES_G_LO);
2141
2142         stats->txmulticastframes_g +=
2143                 xgbe_mmc_read(pdata, MMC_TXMULTICASTFRAMES_G_LO);
2144
2145         stats->tx64octets_gb +=
2146                 xgbe_mmc_read(pdata, MMC_TX64OCTETS_GB_LO);
2147
2148         stats->tx65to127octets_gb +=
2149                 xgbe_mmc_read(pdata, MMC_TX65TO127OCTETS_GB_LO);
2150
2151         stats->tx128to255octets_gb +=
2152                 xgbe_mmc_read(pdata, MMC_TX128TO255OCTETS_GB_LO);
2153
2154         stats->tx256to511octets_gb +=
2155                 xgbe_mmc_read(pdata, MMC_TX256TO511OCTETS_GB_LO);
2156
2157         stats->tx512to1023octets_gb +=
2158                 xgbe_mmc_read(pdata, MMC_TX512TO1023OCTETS_GB_LO);
2159
2160         stats->tx1024tomaxoctets_gb +=
2161                 xgbe_mmc_read(pdata, MMC_TX1024TOMAXOCTETS_GB_LO);
2162
2163         stats->txunicastframes_gb +=
2164                 xgbe_mmc_read(pdata, MMC_TXUNICASTFRAMES_GB_LO);
2165
2166         stats->txmulticastframes_gb +=
2167                 xgbe_mmc_read(pdata, MMC_TXMULTICASTFRAMES_GB_LO);
2168
2169         stats->txbroadcastframes_g +=
2170                 xgbe_mmc_read(pdata, MMC_TXBROADCASTFRAMES_GB_LO);
2171
2172         stats->txunderflowerror +=
2173                 xgbe_mmc_read(pdata, MMC_TXUNDERFLOWERROR_LO);
2174
2175         stats->txoctetcount_g +=
2176                 xgbe_mmc_read(pdata, MMC_TXOCTETCOUNT_G_LO);
2177
2178         stats->txframecount_g +=
2179                 xgbe_mmc_read(pdata, MMC_TXFRAMECOUNT_G_LO);
2180
2181         stats->txpauseframes +=
2182                 xgbe_mmc_read(pdata, MMC_TXPAUSEFRAMES_LO);
2183
2184         stats->txvlanframes_g +=
2185                 xgbe_mmc_read(pdata, MMC_TXVLANFRAMES_G_LO);
2186
2187         stats->rxframecount_gb +=
2188                 xgbe_mmc_read(pdata, MMC_RXFRAMECOUNT_GB_LO);
2189
2190         stats->rxoctetcount_gb +=
2191                 xgbe_mmc_read(pdata, MMC_RXOCTETCOUNT_GB_LO);
2192
2193         stats->rxoctetcount_g +=
2194                 xgbe_mmc_read(pdata, MMC_RXOCTETCOUNT_G_LO);
2195
2196         stats->rxbroadcastframes_g +=
2197                 xgbe_mmc_read(pdata, MMC_RXBROADCASTFRAMES_G_LO);
2198
2199         stats->rxmulticastframes_g +=
2200                 xgbe_mmc_read(pdata, MMC_RXMULTICASTFRAMES_G_LO);
2201
2202         stats->rxcrcerror +=
2203                 xgbe_mmc_read(pdata, MMC_RXCRCERROR_LO);
2204
2205         stats->rxrunterror +=
2206                 xgbe_mmc_read(pdata, MMC_RXRUNTERROR);
2207
2208         stats->rxjabbererror +=
2209                 xgbe_mmc_read(pdata, MMC_RXJABBERERROR);
2210
2211         stats->rxundersize_g +=
2212                 xgbe_mmc_read(pdata, MMC_RXUNDERSIZE_G);
2213
2214         stats->rxoversize_g +=
2215                 xgbe_mmc_read(pdata, MMC_RXOVERSIZE_G);
2216
2217         stats->rx64octets_gb +=
2218                 xgbe_mmc_read(pdata, MMC_RX64OCTETS_GB_LO);
2219
2220         stats->rx65to127octets_gb +=
2221                 xgbe_mmc_read(pdata, MMC_RX65TO127OCTETS_GB_LO);
2222
2223         stats->rx128to255octets_gb +=
2224                 xgbe_mmc_read(pdata, MMC_RX128TO255OCTETS_GB_LO);
2225
2226         stats->rx256to511octets_gb +=
2227                 xgbe_mmc_read(pdata, MMC_RX256TO511OCTETS_GB_LO);
2228
2229         stats->rx512to1023octets_gb +=
2230                 xgbe_mmc_read(pdata, MMC_RX512TO1023OCTETS_GB_LO);
2231
2232         stats->rx1024tomaxoctets_gb +=
2233                 xgbe_mmc_read(pdata, MMC_RX1024TOMAXOCTETS_GB_LO);
2234
2235         stats->rxunicastframes_g +=
2236                 xgbe_mmc_read(pdata, MMC_RXUNICASTFRAMES_G_LO);
2237
2238         stats->rxlengtherror +=
2239                 xgbe_mmc_read(pdata, MMC_RXLENGTHERROR_LO);
2240
2241         stats->rxoutofrangetype +=
2242                 xgbe_mmc_read(pdata, MMC_RXOUTOFRANGETYPE_LO);
2243
2244         stats->rxpauseframes +=
2245                 xgbe_mmc_read(pdata, MMC_RXPAUSEFRAMES_LO);
2246
2247         stats->rxfifooverflow +=
2248                 xgbe_mmc_read(pdata, MMC_RXFIFOOVERFLOW_LO);
2249
2250         stats->rxvlanframes_gb +=
2251                 xgbe_mmc_read(pdata, MMC_RXVLANFRAMES_GB_LO);
2252
2253         stats->rxwatchdogerror +=
2254                 xgbe_mmc_read(pdata, MMC_RXWATCHDOGERROR);
2255
2256         /* Un-freeze counters */
2257         XGMAC_IOWRITE_BITS(pdata, MMC_CR, MCF, 0);
2258 }
2259
2260 static void xgbe_config_mmc(struct xgbe_prv_data *pdata)
2261 {
2262         /* Set counters to reset on read */
2263         XGMAC_IOWRITE_BITS(pdata, MMC_CR, ROR, 1);
2264
2265         /* Reset the counters */
2266         XGMAC_IOWRITE_BITS(pdata, MMC_CR, CR, 1);
2267 }
2268
2269 static void xgbe_enable_tx(struct xgbe_prv_data *pdata)
2270 {
2271         struct xgbe_channel *channel;
2272         unsigned int i;
2273
2274         /* Enable each Tx DMA channel */
2275         channel = pdata->channel;
2276         for (i = 0; i < pdata->channel_count; i++, channel++) {
2277                 if (!channel->tx_ring)
2278                         break;
2279
2280                 XGMAC_DMA_IOWRITE_BITS(channel, DMA_CH_TCR, ST, 1);
2281         }
2282
2283         /* Enable each Tx queue */
2284         for (i = 0; i < pdata->tx_q_count; i++)
2285                 XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_Q_TQOMR, TXQEN,
2286                                        MTL_Q_ENABLED);
2287
2288         /* Enable MAC Tx */
2289         XGMAC_IOWRITE_BITS(pdata, MAC_TCR, TE, 1);
2290 }
2291
2292 static void xgbe_disable_tx(struct xgbe_prv_data *pdata)
2293 {
2294         struct xgbe_channel *channel;
2295         unsigned int i;
2296
2297         /* Disable MAC Tx */
2298         XGMAC_IOWRITE_BITS(pdata, MAC_TCR, TE, 0);
2299
2300         /* Disable each Tx queue */
2301         for (i = 0; i < pdata->tx_q_count; i++)
2302                 XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_Q_TQOMR, TXQEN, 0);
2303
2304         /* Disable each Tx DMA channel */
2305         channel = pdata->channel;
2306         for (i = 0; i < pdata->channel_count; i++, channel++) {
2307                 if (!channel->tx_ring)
2308                         break;
2309
2310                 XGMAC_DMA_IOWRITE_BITS(channel, DMA_CH_TCR, ST, 0);
2311         }
2312 }
2313
2314 static void xgbe_enable_rx(struct xgbe_prv_data *pdata)
2315 {
2316         struct xgbe_channel *channel;
2317         unsigned int reg_val, i;
2318
2319         /* Enable each Rx DMA channel */
2320         channel = pdata->channel;
2321         for (i = 0; i < pdata->channel_count; i++, channel++) {
2322                 if (!channel->rx_ring)
2323                         break;
2324
2325                 XGMAC_DMA_IOWRITE_BITS(channel, DMA_CH_RCR, SR, 1);
2326         }
2327
2328         /* Enable each Rx queue */
2329         reg_val = 0;
2330         for (i = 0; i < pdata->rx_q_count; i++)
2331                 reg_val |= (0x02 << (i << 1));
2332         XGMAC_IOWRITE(pdata, MAC_RQC0R, reg_val);
2333
2334         /* Enable MAC Rx */
2335         XGMAC_IOWRITE_BITS(pdata, MAC_RCR, DCRCC, 1);
2336         XGMAC_IOWRITE_BITS(pdata, MAC_RCR, CST, 1);
2337         XGMAC_IOWRITE_BITS(pdata, MAC_RCR, ACS, 1);
2338         XGMAC_IOWRITE_BITS(pdata, MAC_RCR, RE, 1);
2339 }
2340
2341 static void xgbe_disable_rx(struct xgbe_prv_data *pdata)
2342 {
2343         struct xgbe_channel *channel;
2344         unsigned int i;
2345
2346         /* Disable MAC Rx */
2347         XGMAC_IOWRITE_BITS(pdata, MAC_RCR, DCRCC, 0);
2348         XGMAC_IOWRITE_BITS(pdata, MAC_RCR, CST, 0);
2349         XGMAC_IOWRITE_BITS(pdata, MAC_RCR, ACS, 0);
2350         XGMAC_IOWRITE_BITS(pdata, MAC_RCR, RE, 0);
2351
2352         /* Disable each Rx queue */
2353         XGMAC_IOWRITE(pdata, MAC_RQC0R, 0);
2354
2355         /* Disable each Rx DMA channel */
2356         channel = pdata->channel;
2357         for (i = 0; i < pdata->channel_count; i++, channel++) {
2358                 if (!channel->rx_ring)
2359                         break;
2360
2361                 XGMAC_DMA_IOWRITE_BITS(channel, DMA_CH_RCR, SR, 0);
2362         }
2363 }
2364
2365 static void xgbe_powerup_tx(struct xgbe_prv_data *pdata)
2366 {
2367         struct xgbe_channel *channel;
2368         unsigned int i;
2369
2370         /* Enable each Tx DMA channel */
2371         channel = pdata->channel;
2372         for (i = 0; i < pdata->channel_count; i++, channel++) {
2373                 if (!channel->tx_ring)
2374                         break;
2375
2376                 XGMAC_DMA_IOWRITE_BITS(channel, DMA_CH_TCR, ST, 1);
2377         }
2378
2379         /* Enable MAC Tx */
2380         XGMAC_IOWRITE_BITS(pdata, MAC_TCR, TE, 1);
2381 }
2382
2383 static void xgbe_powerdown_tx(struct xgbe_prv_data *pdata)
2384 {
2385         struct xgbe_channel *channel;
2386         unsigned int i;
2387
2388         /* Disable MAC Tx */
2389         XGMAC_IOWRITE_BITS(pdata, MAC_TCR, TE, 0);
2390
2391         /* Disable each Tx DMA channel */
2392         channel = pdata->channel;
2393         for (i = 0; i < pdata->channel_count; i++, channel++) {
2394                 if (!channel->tx_ring)
2395                         break;
2396
2397                 XGMAC_DMA_IOWRITE_BITS(channel, DMA_CH_TCR, ST, 0);
2398         }
2399 }
2400
2401 static void xgbe_powerup_rx(struct xgbe_prv_data *pdata)
2402 {
2403         struct xgbe_channel *channel;
2404         unsigned int i;
2405
2406         /* Enable each Rx DMA channel */
2407         channel = pdata->channel;
2408         for (i = 0; i < pdata->channel_count; i++, channel++) {
2409                 if (!channel->rx_ring)
2410                         break;
2411
2412                 XGMAC_DMA_IOWRITE_BITS(channel, DMA_CH_RCR, SR, 1);
2413         }
2414 }
2415
2416 static void xgbe_powerdown_rx(struct xgbe_prv_data *pdata)
2417 {
2418         struct xgbe_channel *channel;
2419         unsigned int i;
2420
2421         /* Disable each Rx DMA channel */
2422         channel = pdata->channel;
2423         for (i = 0; i < pdata->channel_count; i++, channel++) {
2424                 if (!channel->rx_ring)
2425                         break;
2426
2427                 XGMAC_DMA_IOWRITE_BITS(channel, DMA_CH_RCR, SR, 0);
2428         }
2429 }
2430
2431 static int xgbe_init(struct xgbe_prv_data *pdata)
2432 {
2433         struct xgbe_desc_if *desc_if = &pdata->desc_if;
2434         int ret;
2435
2436         DBGPR("-->xgbe_init\n");
2437
2438         /* Flush Tx queues */
2439         ret = xgbe_flush_tx_queues(pdata);
2440         if (ret)
2441                 return ret;
2442
2443         /*
2444          * Initialize DMA related features
2445          */
2446         xgbe_config_dma_bus(pdata);
2447         xgbe_config_dma_cache(pdata);
2448         xgbe_config_osp_mode(pdata);
2449         xgbe_config_pblx8(pdata);
2450         xgbe_config_tx_pbl_val(pdata);
2451         xgbe_config_rx_pbl_val(pdata);
2452         xgbe_config_rx_coalesce(pdata);
2453         xgbe_config_tx_coalesce(pdata);
2454         xgbe_config_rx_buffer_size(pdata);
2455         xgbe_config_tso_mode(pdata);
2456         desc_if->wrapper_tx_desc_init(pdata);
2457         desc_if->wrapper_rx_desc_init(pdata);
2458         xgbe_enable_dma_interrupts(pdata);
2459
2460         /*
2461          * Initialize MTL related features
2462          */
2463         xgbe_config_mtl_mode(pdata);
2464         xgbe_config_queue_mapping(pdata);
2465         xgbe_config_tsf_mode(pdata, pdata->tx_sf_mode);
2466         xgbe_config_rsf_mode(pdata, pdata->rx_sf_mode);
2467         xgbe_config_tx_threshold(pdata, pdata->tx_threshold);
2468         xgbe_config_rx_threshold(pdata, pdata->rx_threshold);
2469         xgbe_config_tx_fifo_size(pdata);
2470         xgbe_config_rx_fifo_size(pdata);
2471         xgbe_config_flow_control_threshold(pdata);
2472         /*TODO: Error Packet and undersized good Packet forwarding enable
2473                 (FEP and FUP)
2474          */
2475         xgbe_config_dcb_tc(pdata);
2476         xgbe_config_dcb_pfc(pdata);
2477         xgbe_enable_mtl_interrupts(pdata);
2478
2479         /*
2480          * Initialize MAC related features
2481          */
2482         xgbe_config_mac_address(pdata);
2483         xgbe_config_jumbo_enable(pdata);
2484         xgbe_config_flow_control(pdata);
2485         xgbe_config_checksum_offload(pdata);
2486         xgbe_config_vlan_support(pdata);
2487         xgbe_config_mmc(pdata);
2488         xgbe_enable_mac_interrupts(pdata);
2489
2490         DBGPR("<--xgbe_init\n");
2491
2492         return 0;
2493 }
2494
2495 void xgbe_init_function_ptrs_dev(struct xgbe_hw_if *hw_if)
2496 {
2497         DBGPR("-->xgbe_init_function_ptrs\n");
2498
2499         hw_if->tx_complete = xgbe_tx_complete;
2500
2501         hw_if->set_promiscuous_mode = xgbe_set_promiscuous_mode;
2502         hw_if->set_all_multicast_mode = xgbe_set_all_multicast_mode;
2503         hw_if->add_mac_addresses = xgbe_add_mac_addresses;
2504         hw_if->set_mac_address = xgbe_set_mac_address;
2505
2506         hw_if->enable_rx_csum = xgbe_enable_rx_csum;
2507         hw_if->disable_rx_csum = xgbe_disable_rx_csum;
2508
2509         hw_if->enable_rx_vlan_stripping = xgbe_enable_rx_vlan_stripping;
2510         hw_if->disable_rx_vlan_stripping = xgbe_disable_rx_vlan_stripping;
2511         hw_if->enable_rx_vlan_filtering = xgbe_enable_rx_vlan_filtering;
2512         hw_if->disable_rx_vlan_filtering = xgbe_disable_rx_vlan_filtering;
2513         hw_if->update_vlan_hash_table = xgbe_update_vlan_hash_table;
2514
2515         hw_if->read_mmd_regs = xgbe_read_mmd_regs;
2516         hw_if->write_mmd_regs = xgbe_write_mmd_regs;
2517
2518         hw_if->set_gmii_speed = xgbe_set_gmii_speed;
2519         hw_if->set_gmii_2500_speed = xgbe_set_gmii_2500_speed;
2520         hw_if->set_xgmii_speed = xgbe_set_xgmii_speed;
2521
2522         hw_if->enable_tx = xgbe_enable_tx;
2523         hw_if->disable_tx = xgbe_disable_tx;
2524         hw_if->enable_rx = xgbe_enable_rx;
2525         hw_if->disable_rx = xgbe_disable_rx;
2526
2527         hw_if->powerup_tx = xgbe_powerup_tx;
2528         hw_if->powerdown_tx = xgbe_powerdown_tx;
2529         hw_if->powerup_rx = xgbe_powerup_rx;
2530         hw_if->powerdown_rx = xgbe_powerdown_rx;
2531
2532         hw_if->dev_xmit = xgbe_dev_xmit;
2533         hw_if->dev_read = xgbe_dev_read;
2534         hw_if->enable_int = xgbe_enable_int;
2535         hw_if->disable_int = xgbe_disable_int;
2536         hw_if->init = xgbe_init;
2537         hw_if->exit = xgbe_exit;
2538
2539         /* Descriptor related Sequences have to be initialized here */
2540         hw_if->tx_desc_init = xgbe_tx_desc_init;
2541         hw_if->rx_desc_init = xgbe_rx_desc_init;
2542         hw_if->tx_desc_reset = xgbe_tx_desc_reset;
2543         hw_if->rx_desc_reset = xgbe_rx_desc_reset;
2544         hw_if->is_last_desc = xgbe_is_last_desc;
2545         hw_if->is_context_desc = xgbe_is_context_desc;
2546
2547         /* For FLOW ctrl */
2548         hw_if->config_tx_flow_control = xgbe_config_tx_flow_control;
2549         hw_if->config_rx_flow_control = xgbe_config_rx_flow_control;
2550
2551         /* For RX coalescing */
2552         hw_if->config_rx_coalesce = xgbe_config_rx_coalesce;
2553         hw_if->config_tx_coalesce = xgbe_config_tx_coalesce;
2554         hw_if->usec_to_riwt = xgbe_usec_to_riwt;
2555         hw_if->riwt_to_usec = xgbe_riwt_to_usec;
2556
2557         /* For RX and TX threshold config */
2558         hw_if->config_rx_threshold = xgbe_config_rx_threshold;
2559         hw_if->config_tx_threshold = xgbe_config_tx_threshold;
2560
2561         /* For RX and TX Store and Forward Mode config */
2562         hw_if->config_rsf_mode = xgbe_config_rsf_mode;
2563         hw_if->config_tsf_mode = xgbe_config_tsf_mode;
2564
2565         /* For TX DMA Operating on Second Frame config */
2566         hw_if->config_osp_mode = xgbe_config_osp_mode;
2567
2568         /* For RX and TX PBL config */
2569         hw_if->config_rx_pbl_val = xgbe_config_rx_pbl_val;
2570         hw_if->get_rx_pbl_val = xgbe_get_rx_pbl_val;
2571         hw_if->config_tx_pbl_val = xgbe_config_tx_pbl_val;
2572         hw_if->get_tx_pbl_val = xgbe_get_tx_pbl_val;
2573         hw_if->config_pblx8 = xgbe_config_pblx8;
2574
2575         /* For MMC statistics support */
2576         hw_if->tx_mmc_int = xgbe_tx_mmc_int;
2577         hw_if->rx_mmc_int = xgbe_rx_mmc_int;
2578         hw_if->read_mmc_stats = xgbe_read_mmc_stats;
2579
2580         /* For PTP config */
2581         hw_if->config_tstamp = xgbe_config_tstamp;
2582         hw_if->update_tstamp_addend = xgbe_update_tstamp_addend;
2583         hw_if->set_tstamp_time = xgbe_set_tstamp_time;
2584         hw_if->get_tstamp_time = xgbe_get_tstamp_time;
2585         hw_if->get_tx_tstamp = xgbe_get_tx_tstamp;
2586
2587         /* For Data Center Bridging config */
2588         hw_if->config_dcb_tc = xgbe_config_dcb_tc;
2589         hw_if->config_dcb_pfc = xgbe_config_dcb_pfc;
2590
2591         DBGPR("<--xgbe_init_function_ptrs\n");
2592 }