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