Merge tag 'mips_fixes_5.10_1' of git://git.kernel.org/pub/scm/linux/kernel/git/mips...
[linux-2.6-microblaze.git] / drivers / net / ethernet / microchip / lan743x_main.c
1 /* SPDX-License-Identifier: GPL-2.0+ */
2 /* Copyright (C) 2018 Microchip Technology Inc. */
3
4 #include <linux/module.h>
5 #include <linux/pci.h>
6 #include <linux/netdevice.h>
7 #include <linux/etherdevice.h>
8 #include <linux/crc32.h>
9 #include <linux/microchipphy.h>
10 #include <linux/net_tstamp.h>
11 #include <linux/of_mdio.h>
12 #include <linux/of_net.h>
13 #include <linux/phy.h>
14 #include <linux/phy_fixed.h>
15 #include <linux/rtnetlink.h>
16 #include <linux/iopoll.h>
17 #include <linux/crc16.h>
18 #include "lan743x_main.h"
19 #include "lan743x_ethtool.h"
20
21 static void lan743x_pci_cleanup(struct lan743x_adapter *adapter)
22 {
23         pci_release_selected_regions(adapter->pdev,
24                                      pci_select_bars(adapter->pdev,
25                                                      IORESOURCE_MEM));
26         pci_disable_device(adapter->pdev);
27 }
28
29 static int lan743x_pci_init(struct lan743x_adapter *adapter,
30                             struct pci_dev *pdev)
31 {
32         unsigned long bars = 0;
33         int ret;
34
35         adapter->pdev = pdev;
36         ret = pci_enable_device_mem(pdev);
37         if (ret)
38                 goto return_error;
39
40         netif_info(adapter, probe, adapter->netdev,
41                    "PCI: Vendor ID = 0x%04X, Device ID = 0x%04X\n",
42                    pdev->vendor, pdev->device);
43         bars = pci_select_bars(pdev, IORESOURCE_MEM);
44         if (!test_bit(0, &bars))
45                 goto disable_device;
46
47         ret = pci_request_selected_regions(pdev, bars, DRIVER_NAME);
48         if (ret)
49                 goto disable_device;
50
51         pci_set_master(pdev);
52         return 0;
53
54 disable_device:
55         pci_disable_device(adapter->pdev);
56
57 return_error:
58         return ret;
59 }
60
61 u32 lan743x_csr_read(struct lan743x_adapter *adapter, int offset)
62 {
63         return ioread32(&adapter->csr.csr_address[offset]);
64 }
65
66 void lan743x_csr_write(struct lan743x_adapter *adapter, int offset,
67                        u32 data)
68 {
69         iowrite32(data, &adapter->csr.csr_address[offset]);
70 }
71
72 #define LAN743X_CSR_READ_OP(offset)     lan743x_csr_read(adapter, offset)
73
74 static int lan743x_csr_light_reset(struct lan743x_adapter *adapter)
75 {
76         u32 data;
77
78         data = lan743x_csr_read(adapter, HW_CFG);
79         data |= HW_CFG_LRST_;
80         lan743x_csr_write(adapter, HW_CFG, data);
81
82         return readx_poll_timeout(LAN743X_CSR_READ_OP, HW_CFG, data,
83                                   !(data & HW_CFG_LRST_), 100000, 10000000);
84 }
85
86 static int lan743x_csr_wait_for_bit(struct lan743x_adapter *adapter,
87                                     int offset, u32 bit_mask,
88                                     int target_value, int usleep_min,
89                                     int usleep_max, int count)
90 {
91         u32 data;
92
93         return readx_poll_timeout(LAN743X_CSR_READ_OP, offset, data,
94                                   target_value == ((data & bit_mask) ? 1 : 0),
95                                   usleep_max, usleep_min * count);
96 }
97
98 static int lan743x_csr_init(struct lan743x_adapter *adapter)
99 {
100         struct lan743x_csr *csr = &adapter->csr;
101         resource_size_t bar_start, bar_length;
102         int result;
103
104         bar_start = pci_resource_start(adapter->pdev, 0);
105         bar_length = pci_resource_len(adapter->pdev, 0);
106         csr->csr_address = devm_ioremap(&adapter->pdev->dev,
107                                         bar_start, bar_length);
108         if (!csr->csr_address) {
109                 result = -ENOMEM;
110                 goto clean_up;
111         }
112
113         csr->id_rev = lan743x_csr_read(adapter, ID_REV);
114         csr->fpga_rev = lan743x_csr_read(adapter, FPGA_REV);
115         netif_info(adapter, probe, adapter->netdev,
116                    "ID_REV = 0x%08X, FPGA_REV = %d.%d\n",
117                    csr->id_rev, FPGA_REV_GET_MAJOR_(csr->fpga_rev),
118                    FPGA_REV_GET_MINOR_(csr->fpga_rev));
119         if (!ID_REV_IS_VALID_CHIP_ID_(csr->id_rev)) {
120                 result = -ENODEV;
121                 goto clean_up;
122         }
123
124         csr->flags = LAN743X_CSR_FLAG_SUPPORTS_INTR_AUTO_SET_CLR;
125         switch (csr->id_rev & ID_REV_CHIP_REV_MASK_) {
126         case ID_REV_CHIP_REV_A0_:
127                 csr->flags |= LAN743X_CSR_FLAG_IS_A0;
128                 csr->flags &= ~LAN743X_CSR_FLAG_SUPPORTS_INTR_AUTO_SET_CLR;
129                 break;
130         case ID_REV_CHIP_REV_B0_:
131                 csr->flags |= LAN743X_CSR_FLAG_IS_B0;
132                 break;
133         }
134
135         result = lan743x_csr_light_reset(adapter);
136         if (result)
137                 goto clean_up;
138         return 0;
139 clean_up:
140         return result;
141 }
142
143 static void lan743x_intr_software_isr(void *context)
144 {
145         struct lan743x_adapter *adapter = context;
146         struct lan743x_intr *intr = &adapter->intr;
147         u32 int_sts;
148
149         int_sts = lan743x_csr_read(adapter, INT_STS);
150         if (int_sts & INT_BIT_SW_GP_) {
151                 lan743x_csr_write(adapter, INT_STS, INT_BIT_SW_GP_);
152                 intr->software_isr_flag = 1;
153         }
154 }
155
156 static void lan743x_tx_isr(void *context, u32 int_sts, u32 flags)
157 {
158         struct lan743x_tx *tx = context;
159         struct lan743x_adapter *adapter = tx->adapter;
160         bool enable_flag = true;
161
162         lan743x_csr_read(adapter, INT_EN_SET);
163         if (flags & LAN743X_VECTOR_FLAG_SOURCE_ENABLE_CLEAR) {
164                 lan743x_csr_write(adapter, INT_EN_CLR,
165                                   INT_BIT_DMA_TX_(tx->channel_number));
166         }
167
168         if (int_sts & INT_BIT_DMA_TX_(tx->channel_number)) {
169                 u32 ioc_bit = DMAC_INT_BIT_TX_IOC_(tx->channel_number);
170                 u32 dmac_int_sts;
171                 u32 dmac_int_en;
172
173                 if (flags & LAN743X_VECTOR_FLAG_SOURCE_STATUS_READ)
174                         dmac_int_sts = lan743x_csr_read(adapter, DMAC_INT_STS);
175                 else
176                         dmac_int_sts = ioc_bit;
177                 if (flags & LAN743X_VECTOR_FLAG_SOURCE_ENABLE_CHECK)
178                         dmac_int_en = lan743x_csr_read(adapter,
179                                                        DMAC_INT_EN_SET);
180                 else
181                         dmac_int_en = ioc_bit;
182
183                 dmac_int_en &= ioc_bit;
184                 dmac_int_sts &= dmac_int_en;
185                 if (dmac_int_sts & ioc_bit) {
186                         napi_schedule(&tx->napi);
187                         enable_flag = false;/* poll func will enable later */
188                 }
189         }
190
191         if (enable_flag)
192                 /* enable isr */
193                 lan743x_csr_write(adapter, INT_EN_SET,
194                                   INT_BIT_DMA_TX_(tx->channel_number));
195 }
196
197 static void lan743x_rx_isr(void *context, u32 int_sts, u32 flags)
198 {
199         struct lan743x_rx *rx = context;
200         struct lan743x_adapter *adapter = rx->adapter;
201         bool enable_flag = true;
202
203         if (flags & LAN743X_VECTOR_FLAG_SOURCE_ENABLE_CLEAR) {
204                 lan743x_csr_write(adapter, INT_EN_CLR,
205                                   INT_BIT_DMA_RX_(rx->channel_number));
206         }
207
208         if (int_sts & INT_BIT_DMA_RX_(rx->channel_number)) {
209                 u32 rx_frame_bit = DMAC_INT_BIT_RXFRM_(rx->channel_number);
210                 u32 dmac_int_sts;
211                 u32 dmac_int_en;
212
213                 if (flags & LAN743X_VECTOR_FLAG_SOURCE_STATUS_READ)
214                         dmac_int_sts = lan743x_csr_read(adapter, DMAC_INT_STS);
215                 else
216                         dmac_int_sts = rx_frame_bit;
217                 if (flags & LAN743X_VECTOR_FLAG_SOURCE_ENABLE_CHECK)
218                         dmac_int_en = lan743x_csr_read(adapter,
219                                                        DMAC_INT_EN_SET);
220                 else
221                         dmac_int_en = rx_frame_bit;
222
223                 dmac_int_en &= rx_frame_bit;
224                 dmac_int_sts &= dmac_int_en;
225                 if (dmac_int_sts & rx_frame_bit) {
226                         napi_schedule(&rx->napi);
227                         enable_flag = false;/* poll funct will enable later */
228                 }
229         }
230
231         if (enable_flag) {
232                 /* enable isr */
233                 lan743x_csr_write(adapter, INT_EN_SET,
234                                   INT_BIT_DMA_RX_(rx->channel_number));
235         }
236 }
237
238 static void lan743x_intr_shared_isr(void *context, u32 int_sts, u32 flags)
239 {
240         struct lan743x_adapter *adapter = context;
241         unsigned int channel;
242
243         if (int_sts & INT_BIT_ALL_RX_) {
244                 for (channel = 0; channel < LAN743X_USED_RX_CHANNELS;
245                         channel++) {
246                         u32 int_bit = INT_BIT_DMA_RX_(channel);
247
248                         if (int_sts & int_bit) {
249                                 lan743x_rx_isr(&adapter->rx[channel],
250                                                int_bit, flags);
251                                 int_sts &= ~int_bit;
252                         }
253                 }
254         }
255         if (int_sts & INT_BIT_ALL_TX_) {
256                 for (channel = 0; channel < LAN743X_USED_TX_CHANNELS;
257                         channel++) {
258                         u32 int_bit = INT_BIT_DMA_TX_(channel);
259
260                         if (int_sts & int_bit) {
261                                 lan743x_tx_isr(&adapter->tx[channel],
262                                                int_bit, flags);
263                                 int_sts &= ~int_bit;
264                         }
265                 }
266         }
267         if (int_sts & INT_BIT_ALL_OTHER_) {
268                 if (int_sts & INT_BIT_SW_GP_) {
269                         lan743x_intr_software_isr(adapter);
270                         int_sts &= ~INT_BIT_SW_GP_;
271                 }
272                 if (int_sts & INT_BIT_1588_) {
273                         lan743x_ptp_isr(adapter);
274                         int_sts &= ~INT_BIT_1588_;
275                 }
276         }
277         if (int_sts)
278                 lan743x_csr_write(adapter, INT_EN_CLR, int_sts);
279 }
280
281 static irqreturn_t lan743x_intr_entry_isr(int irq, void *ptr)
282 {
283         struct lan743x_vector *vector = ptr;
284         struct lan743x_adapter *adapter = vector->adapter;
285         irqreturn_t result = IRQ_NONE;
286         u32 int_enables;
287         u32 int_sts;
288
289         if (vector->flags & LAN743X_VECTOR_FLAG_SOURCE_STATUS_READ) {
290                 int_sts = lan743x_csr_read(adapter, INT_STS);
291         } else if (vector->flags &
292                    (LAN743X_VECTOR_FLAG_SOURCE_STATUS_R2C |
293                    LAN743X_VECTOR_FLAG_SOURCE_ENABLE_R2C)) {
294                 int_sts = lan743x_csr_read(adapter, INT_STS_R2C);
295         } else {
296                 /* use mask as implied status */
297                 int_sts = vector->int_mask | INT_BIT_MAS_;
298         }
299
300         if (!(int_sts & INT_BIT_MAS_))
301                 goto irq_done;
302
303         if (vector->flags & LAN743X_VECTOR_FLAG_VECTOR_ENABLE_ISR_CLEAR)
304                 /* disable vector interrupt */
305                 lan743x_csr_write(adapter,
306                                   INT_VEC_EN_CLR,
307                                   INT_VEC_EN_(vector->vector_index));
308
309         if (vector->flags & LAN743X_VECTOR_FLAG_MASTER_ENABLE_CLEAR)
310                 /* disable master interrupt */
311                 lan743x_csr_write(adapter, INT_EN_CLR, INT_BIT_MAS_);
312
313         if (vector->flags & LAN743X_VECTOR_FLAG_SOURCE_ENABLE_CHECK) {
314                 int_enables = lan743x_csr_read(adapter, INT_EN_SET);
315         } else {
316                 /*  use vector mask as implied enable mask */
317                 int_enables = vector->int_mask;
318         }
319
320         int_sts &= int_enables;
321         int_sts &= vector->int_mask;
322         if (int_sts) {
323                 if (vector->handler) {
324                         vector->handler(vector->context,
325                                         int_sts, vector->flags);
326                 } else {
327                         /* disable interrupts on this vector */
328                         lan743x_csr_write(adapter, INT_EN_CLR,
329                                           vector->int_mask);
330                 }
331                 result = IRQ_HANDLED;
332         }
333
334         if (vector->flags & LAN743X_VECTOR_FLAG_MASTER_ENABLE_SET)
335                 /* enable master interrupt */
336                 lan743x_csr_write(adapter, INT_EN_SET, INT_BIT_MAS_);
337
338         if (vector->flags & LAN743X_VECTOR_FLAG_VECTOR_ENABLE_ISR_SET)
339                 /* enable vector interrupt */
340                 lan743x_csr_write(adapter,
341                                   INT_VEC_EN_SET,
342                                   INT_VEC_EN_(vector->vector_index));
343 irq_done:
344         return result;
345 }
346
347 static int lan743x_intr_test_isr(struct lan743x_adapter *adapter)
348 {
349         struct lan743x_intr *intr = &adapter->intr;
350         int result = -ENODEV;
351         int timeout = 10;
352
353         intr->software_isr_flag = 0;
354
355         /* enable interrupt */
356         lan743x_csr_write(adapter, INT_EN_SET, INT_BIT_SW_GP_);
357
358         /* activate interrupt here */
359         lan743x_csr_write(adapter, INT_SET, INT_BIT_SW_GP_);
360         while ((timeout > 0) && (!(intr->software_isr_flag))) {
361                 usleep_range(1000, 20000);
362                 timeout--;
363         }
364
365         if (intr->software_isr_flag)
366                 result = 0;
367
368         /* disable interrupts */
369         lan743x_csr_write(adapter, INT_EN_CLR, INT_BIT_SW_GP_);
370         return result;
371 }
372
373 static int lan743x_intr_register_isr(struct lan743x_adapter *adapter,
374                                      int vector_index, u32 flags,
375                                      u32 int_mask,
376                                      lan743x_vector_handler handler,
377                                      void *context)
378 {
379         struct lan743x_vector *vector = &adapter->intr.vector_list
380                                         [vector_index];
381         int ret;
382
383         vector->adapter = adapter;
384         vector->flags = flags;
385         vector->vector_index = vector_index;
386         vector->int_mask = int_mask;
387         vector->handler = handler;
388         vector->context = context;
389
390         ret = request_irq(vector->irq,
391                           lan743x_intr_entry_isr,
392                           (flags & LAN743X_VECTOR_FLAG_IRQ_SHARED) ?
393                           IRQF_SHARED : 0, DRIVER_NAME, vector);
394         if (ret) {
395                 vector->handler = NULL;
396                 vector->context = NULL;
397                 vector->int_mask = 0;
398                 vector->flags = 0;
399         }
400         return ret;
401 }
402
403 static void lan743x_intr_unregister_isr(struct lan743x_adapter *adapter,
404                                         int vector_index)
405 {
406         struct lan743x_vector *vector = &adapter->intr.vector_list
407                                         [vector_index];
408
409         free_irq(vector->irq, vector);
410         vector->handler = NULL;
411         vector->context = NULL;
412         vector->int_mask = 0;
413         vector->flags = 0;
414 }
415
416 static u32 lan743x_intr_get_vector_flags(struct lan743x_adapter *adapter,
417                                          u32 int_mask)
418 {
419         int index;
420
421         for (index = 0; index < LAN743X_MAX_VECTOR_COUNT; index++) {
422                 if (adapter->intr.vector_list[index].int_mask & int_mask)
423                         return adapter->intr.vector_list[index].flags;
424         }
425         return 0;
426 }
427
428 static void lan743x_intr_close(struct lan743x_adapter *adapter)
429 {
430         struct lan743x_intr *intr = &adapter->intr;
431         int index = 0;
432
433         lan743x_csr_write(adapter, INT_EN_CLR, INT_BIT_MAS_);
434         lan743x_csr_write(adapter, INT_VEC_EN_CLR, 0x000000FF);
435
436         for (index = 0; index < LAN743X_MAX_VECTOR_COUNT; index++) {
437                 if (intr->flags & INTR_FLAG_IRQ_REQUESTED(index)) {
438                         lan743x_intr_unregister_isr(adapter, index);
439                         intr->flags &= ~INTR_FLAG_IRQ_REQUESTED(index);
440                 }
441         }
442
443         if (intr->flags & INTR_FLAG_MSI_ENABLED) {
444                 pci_disable_msi(adapter->pdev);
445                 intr->flags &= ~INTR_FLAG_MSI_ENABLED;
446         }
447
448         if (intr->flags & INTR_FLAG_MSIX_ENABLED) {
449                 pci_disable_msix(adapter->pdev);
450                 intr->flags &= ~INTR_FLAG_MSIX_ENABLED;
451         }
452 }
453
454 static int lan743x_intr_open(struct lan743x_adapter *adapter)
455 {
456         struct msix_entry msix_entries[LAN743X_MAX_VECTOR_COUNT];
457         struct lan743x_intr *intr = &adapter->intr;
458         u32 int_vec_en_auto_clr = 0;
459         u32 int_vec_map0 = 0;
460         u32 int_vec_map1 = 0;
461         int ret = -ENODEV;
462         int index = 0;
463         u32 flags = 0;
464
465         intr->number_of_vectors = 0;
466
467         /* Try to set up MSIX interrupts */
468         memset(&msix_entries[0], 0,
469                sizeof(struct msix_entry) * LAN743X_MAX_VECTOR_COUNT);
470         for (index = 0; index < LAN743X_MAX_VECTOR_COUNT; index++)
471                 msix_entries[index].entry = index;
472         ret = pci_enable_msix_range(adapter->pdev,
473                                     msix_entries, 1,
474                                     1 + LAN743X_USED_TX_CHANNELS +
475                                     LAN743X_USED_RX_CHANNELS);
476
477         if (ret > 0) {
478                 intr->flags |= INTR_FLAG_MSIX_ENABLED;
479                 intr->number_of_vectors = ret;
480                 intr->using_vectors = true;
481                 for (index = 0; index < intr->number_of_vectors; index++)
482                         intr->vector_list[index].irq = msix_entries
483                                                        [index].vector;
484                 netif_info(adapter, ifup, adapter->netdev,
485                            "using MSIX interrupts, number of vectors = %d\n",
486                            intr->number_of_vectors);
487         }
488
489         /* If MSIX failed try to setup using MSI interrupts */
490         if (!intr->number_of_vectors) {
491                 if (!(adapter->csr.flags & LAN743X_CSR_FLAG_IS_A0)) {
492                         if (!pci_enable_msi(adapter->pdev)) {
493                                 intr->flags |= INTR_FLAG_MSI_ENABLED;
494                                 intr->number_of_vectors = 1;
495                                 intr->using_vectors = true;
496                                 intr->vector_list[0].irq =
497                                         adapter->pdev->irq;
498                                 netif_info(adapter, ifup, adapter->netdev,
499                                            "using MSI interrupts, number of vectors = %d\n",
500                                            intr->number_of_vectors);
501                         }
502                 }
503         }
504
505         /* If MSIX, and MSI failed, setup using legacy interrupt */
506         if (!intr->number_of_vectors) {
507                 intr->number_of_vectors = 1;
508                 intr->using_vectors = false;
509                 intr->vector_list[0].irq = intr->irq;
510                 netif_info(adapter, ifup, adapter->netdev,
511                            "using legacy interrupts\n");
512         }
513
514         /* At this point we must have at least one irq */
515         lan743x_csr_write(adapter, INT_VEC_EN_CLR, 0xFFFFFFFF);
516
517         /* map all interrupts to vector 0 */
518         lan743x_csr_write(adapter, INT_VEC_MAP0, 0x00000000);
519         lan743x_csr_write(adapter, INT_VEC_MAP1, 0x00000000);
520         lan743x_csr_write(adapter, INT_VEC_MAP2, 0x00000000);
521         flags = LAN743X_VECTOR_FLAG_SOURCE_STATUS_READ |
522                 LAN743X_VECTOR_FLAG_SOURCE_STATUS_W2C |
523                 LAN743X_VECTOR_FLAG_SOURCE_ENABLE_CHECK |
524                 LAN743X_VECTOR_FLAG_SOURCE_ENABLE_CLEAR;
525
526         if (intr->using_vectors) {
527                 flags |= LAN743X_VECTOR_FLAG_VECTOR_ENABLE_ISR_CLEAR |
528                          LAN743X_VECTOR_FLAG_VECTOR_ENABLE_ISR_SET;
529         } else {
530                 flags |= LAN743X_VECTOR_FLAG_MASTER_ENABLE_CLEAR |
531                          LAN743X_VECTOR_FLAG_MASTER_ENABLE_SET |
532                          LAN743X_VECTOR_FLAG_IRQ_SHARED;
533         }
534
535         if (adapter->csr.flags & LAN743X_CSR_FLAG_SUPPORTS_INTR_AUTO_SET_CLR) {
536                 flags &= ~LAN743X_VECTOR_FLAG_SOURCE_STATUS_READ;
537                 flags &= ~LAN743X_VECTOR_FLAG_SOURCE_STATUS_W2C;
538                 flags &= ~LAN743X_VECTOR_FLAG_SOURCE_ENABLE_CLEAR;
539                 flags &= ~LAN743X_VECTOR_FLAG_SOURCE_ENABLE_CHECK;
540                 flags |= LAN743X_VECTOR_FLAG_SOURCE_STATUS_R2C;
541                 flags |= LAN743X_VECTOR_FLAG_SOURCE_ENABLE_R2C;
542         }
543
544         ret = lan743x_intr_register_isr(adapter, 0, flags,
545                                         INT_BIT_ALL_RX_ | INT_BIT_ALL_TX_ |
546                                         INT_BIT_ALL_OTHER_,
547                                         lan743x_intr_shared_isr, adapter);
548         if (ret)
549                 goto clean_up;
550         intr->flags |= INTR_FLAG_IRQ_REQUESTED(0);
551
552         if (intr->using_vectors)
553                 lan743x_csr_write(adapter, INT_VEC_EN_SET,
554                                   INT_VEC_EN_(0));
555
556         if (!(adapter->csr.flags & LAN743X_CSR_FLAG_IS_A0)) {
557                 lan743x_csr_write(adapter, INT_MOD_CFG0, LAN743X_INT_MOD);
558                 lan743x_csr_write(adapter, INT_MOD_CFG1, LAN743X_INT_MOD);
559                 lan743x_csr_write(adapter, INT_MOD_CFG2, LAN743X_INT_MOD);
560                 lan743x_csr_write(adapter, INT_MOD_CFG3, LAN743X_INT_MOD);
561                 lan743x_csr_write(adapter, INT_MOD_CFG4, LAN743X_INT_MOD);
562                 lan743x_csr_write(adapter, INT_MOD_CFG5, LAN743X_INT_MOD);
563                 lan743x_csr_write(adapter, INT_MOD_CFG6, LAN743X_INT_MOD);
564                 lan743x_csr_write(adapter, INT_MOD_CFG7, LAN743X_INT_MOD);
565                 lan743x_csr_write(adapter, INT_MOD_MAP0, 0x00005432);
566                 lan743x_csr_write(adapter, INT_MOD_MAP1, 0x00000001);
567                 lan743x_csr_write(adapter, INT_MOD_MAP2, 0x00FFFFFF);
568         }
569
570         /* enable interrupts */
571         lan743x_csr_write(adapter, INT_EN_SET, INT_BIT_MAS_);
572         ret = lan743x_intr_test_isr(adapter);
573         if (ret)
574                 goto clean_up;
575
576         if (intr->number_of_vectors > 1) {
577                 int number_of_tx_vectors = intr->number_of_vectors - 1;
578
579                 if (number_of_tx_vectors > LAN743X_USED_TX_CHANNELS)
580                         number_of_tx_vectors = LAN743X_USED_TX_CHANNELS;
581                 flags = LAN743X_VECTOR_FLAG_SOURCE_STATUS_READ |
582                         LAN743X_VECTOR_FLAG_SOURCE_STATUS_W2C |
583                         LAN743X_VECTOR_FLAG_SOURCE_ENABLE_CHECK |
584                         LAN743X_VECTOR_FLAG_SOURCE_ENABLE_CLEAR |
585                         LAN743X_VECTOR_FLAG_VECTOR_ENABLE_ISR_CLEAR |
586                         LAN743X_VECTOR_FLAG_VECTOR_ENABLE_ISR_SET;
587
588                 if (adapter->csr.flags &
589                    LAN743X_CSR_FLAG_SUPPORTS_INTR_AUTO_SET_CLR) {
590                         flags = LAN743X_VECTOR_FLAG_VECTOR_ENABLE_AUTO_SET |
591                                 LAN743X_VECTOR_FLAG_SOURCE_ENABLE_AUTO_SET |
592                                 LAN743X_VECTOR_FLAG_SOURCE_ENABLE_AUTO_CLEAR |
593                                 LAN743X_VECTOR_FLAG_SOURCE_STATUS_AUTO_CLEAR;
594                 }
595
596                 for (index = 0; index < number_of_tx_vectors; index++) {
597                         u32 int_bit = INT_BIT_DMA_TX_(index);
598                         int vector = index + 1;
599
600                         /* map TX interrupt to vector */
601                         int_vec_map1 |= INT_VEC_MAP1_TX_VEC_(index, vector);
602                         lan743x_csr_write(adapter, INT_VEC_MAP1, int_vec_map1);
603
604                         /* Remove TX interrupt from shared mask */
605                         intr->vector_list[0].int_mask &= ~int_bit;
606                         ret = lan743x_intr_register_isr(adapter, vector, flags,
607                                                         int_bit, lan743x_tx_isr,
608                                                         &adapter->tx[index]);
609                         if (ret)
610                                 goto clean_up;
611                         intr->flags |= INTR_FLAG_IRQ_REQUESTED(vector);
612                         if (!(flags &
613                             LAN743X_VECTOR_FLAG_VECTOR_ENABLE_AUTO_SET))
614                                 lan743x_csr_write(adapter, INT_VEC_EN_SET,
615                                                   INT_VEC_EN_(vector));
616                 }
617         }
618         if ((intr->number_of_vectors - LAN743X_USED_TX_CHANNELS) > 1) {
619                 int number_of_rx_vectors = intr->number_of_vectors -
620                                            LAN743X_USED_TX_CHANNELS - 1;
621
622                 if (number_of_rx_vectors > LAN743X_USED_RX_CHANNELS)
623                         number_of_rx_vectors = LAN743X_USED_RX_CHANNELS;
624
625                 flags = LAN743X_VECTOR_FLAG_SOURCE_STATUS_READ |
626                         LAN743X_VECTOR_FLAG_SOURCE_STATUS_W2C |
627                         LAN743X_VECTOR_FLAG_SOURCE_ENABLE_CHECK |
628                         LAN743X_VECTOR_FLAG_SOURCE_ENABLE_CLEAR |
629                         LAN743X_VECTOR_FLAG_VECTOR_ENABLE_ISR_CLEAR |
630                         LAN743X_VECTOR_FLAG_VECTOR_ENABLE_ISR_SET;
631
632                 if (adapter->csr.flags &
633                     LAN743X_CSR_FLAG_SUPPORTS_INTR_AUTO_SET_CLR) {
634                         flags = LAN743X_VECTOR_FLAG_VECTOR_ENABLE_AUTO_CLEAR |
635                                 LAN743X_VECTOR_FLAG_VECTOR_ENABLE_AUTO_SET |
636                                 LAN743X_VECTOR_FLAG_SOURCE_ENABLE_AUTO_SET |
637                                 LAN743X_VECTOR_FLAG_SOURCE_ENABLE_AUTO_CLEAR |
638                                 LAN743X_VECTOR_FLAG_SOURCE_STATUS_AUTO_CLEAR;
639                 }
640                 for (index = 0; index < number_of_rx_vectors; index++) {
641                         int vector = index + 1 + LAN743X_USED_TX_CHANNELS;
642                         u32 int_bit = INT_BIT_DMA_RX_(index);
643
644                         /* map RX interrupt to vector */
645                         int_vec_map0 |= INT_VEC_MAP0_RX_VEC_(index, vector);
646                         lan743x_csr_write(adapter, INT_VEC_MAP0, int_vec_map0);
647                         if (flags &
648                             LAN743X_VECTOR_FLAG_VECTOR_ENABLE_AUTO_CLEAR) {
649                                 int_vec_en_auto_clr |= INT_VEC_EN_(vector);
650                                 lan743x_csr_write(adapter, INT_VEC_EN_AUTO_CLR,
651                                                   int_vec_en_auto_clr);
652                         }
653
654                         /* Remove RX interrupt from shared mask */
655                         intr->vector_list[0].int_mask &= ~int_bit;
656                         ret = lan743x_intr_register_isr(adapter, vector, flags,
657                                                         int_bit, lan743x_rx_isr,
658                                                         &adapter->rx[index]);
659                         if (ret)
660                                 goto clean_up;
661                         intr->flags |= INTR_FLAG_IRQ_REQUESTED(vector);
662
663                         lan743x_csr_write(adapter, INT_VEC_EN_SET,
664                                           INT_VEC_EN_(vector));
665                 }
666         }
667         return 0;
668
669 clean_up:
670         lan743x_intr_close(adapter);
671         return ret;
672 }
673
674 static int lan743x_dp_write(struct lan743x_adapter *adapter,
675                             u32 select, u32 addr, u32 length, u32 *buf)
676 {
677         u32 dp_sel;
678         int i;
679
680         if (lan743x_csr_wait_for_bit(adapter, DP_SEL, DP_SEL_DPRDY_,
681                                      1, 40, 100, 100))
682                 return -EIO;
683         dp_sel = lan743x_csr_read(adapter, DP_SEL);
684         dp_sel &= ~DP_SEL_MASK_;
685         dp_sel |= select;
686         lan743x_csr_write(adapter, DP_SEL, dp_sel);
687
688         for (i = 0; i < length; i++) {
689                 lan743x_csr_write(adapter, DP_ADDR, addr + i);
690                 lan743x_csr_write(adapter, DP_DATA_0, buf[i]);
691                 lan743x_csr_write(adapter, DP_CMD, DP_CMD_WRITE_);
692                 if (lan743x_csr_wait_for_bit(adapter, DP_SEL, DP_SEL_DPRDY_,
693                                              1, 40, 100, 100))
694                         return -EIO;
695         }
696
697         return 0;
698 }
699
700 static u32 lan743x_mac_mii_access(u16 id, u16 index, int read)
701 {
702         u32 ret;
703
704         ret = (id << MAC_MII_ACC_PHY_ADDR_SHIFT_) &
705                 MAC_MII_ACC_PHY_ADDR_MASK_;
706         ret |= (index << MAC_MII_ACC_MIIRINDA_SHIFT_) &
707                 MAC_MII_ACC_MIIRINDA_MASK_;
708
709         if (read)
710                 ret |= MAC_MII_ACC_MII_READ_;
711         else
712                 ret |= MAC_MII_ACC_MII_WRITE_;
713         ret |= MAC_MII_ACC_MII_BUSY_;
714
715         return ret;
716 }
717
718 static int lan743x_mac_mii_wait_till_not_busy(struct lan743x_adapter *adapter)
719 {
720         u32 data;
721
722         return readx_poll_timeout(LAN743X_CSR_READ_OP, MAC_MII_ACC, data,
723                                   !(data & MAC_MII_ACC_MII_BUSY_), 0, 1000000);
724 }
725
726 static int lan743x_mdiobus_read(struct mii_bus *bus, int phy_id, int index)
727 {
728         struct lan743x_adapter *adapter = bus->priv;
729         u32 val, mii_access;
730         int ret;
731
732         /* comfirm MII not busy */
733         ret = lan743x_mac_mii_wait_till_not_busy(adapter);
734         if (ret < 0)
735                 return ret;
736
737         /* set the address, index & direction (read from PHY) */
738         mii_access = lan743x_mac_mii_access(phy_id, index, MAC_MII_READ);
739         lan743x_csr_write(adapter, MAC_MII_ACC, mii_access);
740         ret = lan743x_mac_mii_wait_till_not_busy(adapter);
741         if (ret < 0)
742                 return ret;
743
744         val = lan743x_csr_read(adapter, MAC_MII_DATA);
745         return (int)(val & 0xFFFF);
746 }
747
748 static int lan743x_mdiobus_write(struct mii_bus *bus,
749                                  int phy_id, int index, u16 regval)
750 {
751         struct lan743x_adapter *adapter = bus->priv;
752         u32 val, mii_access;
753         int ret;
754
755         /* confirm MII not busy */
756         ret = lan743x_mac_mii_wait_till_not_busy(adapter);
757         if (ret < 0)
758                 return ret;
759         val = (u32)regval;
760         lan743x_csr_write(adapter, MAC_MII_DATA, val);
761
762         /* set the address, index & direction (write to PHY) */
763         mii_access = lan743x_mac_mii_access(phy_id, index, MAC_MII_WRITE);
764         lan743x_csr_write(adapter, MAC_MII_ACC, mii_access);
765         ret = lan743x_mac_mii_wait_till_not_busy(adapter);
766         return ret;
767 }
768
769 static void lan743x_mac_set_address(struct lan743x_adapter *adapter,
770                                     u8 *addr)
771 {
772         u32 addr_lo, addr_hi;
773
774         addr_lo = addr[0] |
775                 addr[1] << 8 |
776                 addr[2] << 16 |
777                 addr[3] << 24;
778         addr_hi = addr[4] |
779                 addr[5] << 8;
780         lan743x_csr_write(adapter, MAC_RX_ADDRL, addr_lo);
781         lan743x_csr_write(adapter, MAC_RX_ADDRH, addr_hi);
782
783         ether_addr_copy(adapter->mac_address, addr);
784         netif_info(adapter, drv, adapter->netdev,
785                    "MAC address set to %pM\n", addr);
786 }
787
788 static int lan743x_mac_init(struct lan743x_adapter *adapter)
789 {
790         bool mac_address_valid = true;
791         struct net_device *netdev;
792         u32 mac_addr_hi = 0;
793         u32 mac_addr_lo = 0;
794         u32 data;
795
796         netdev = adapter->netdev;
797
798         /* disable auto duplex, and speed detection. Phylib does that */
799         data = lan743x_csr_read(adapter, MAC_CR);
800         data &= ~(MAC_CR_ADD_ | MAC_CR_ASD_);
801         data |= MAC_CR_CNTR_RST_;
802         lan743x_csr_write(adapter, MAC_CR, data);
803
804         if (!is_valid_ether_addr(adapter->mac_address)) {
805                 mac_addr_hi = lan743x_csr_read(adapter, MAC_RX_ADDRH);
806                 mac_addr_lo = lan743x_csr_read(adapter, MAC_RX_ADDRL);
807                 adapter->mac_address[0] = mac_addr_lo & 0xFF;
808                 adapter->mac_address[1] = (mac_addr_lo >> 8) & 0xFF;
809                 adapter->mac_address[2] = (mac_addr_lo >> 16) & 0xFF;
810                 adapter->mac_address[3] = (mac_addr_lo >> 24) & 0xFF;
811                 adapter->mac_address[4] = mac_addr_hi & 0xFF;
812                 adapter->mac_address[5] = (mac_addr_hi >> 8) & 0xFF;
813
814                 if (((mac_addr_hi & 0x0000FFFF) == 0x0000FFFF) &&
815                     mac_addr_lo == 0xFFFFFFFF) {
816                         mac_address_valid = false;
817                 } else if (!is_valid_ether_addr(adapter->mac_address)) {
818                         mac_address_valid = false;
819                 }
820
821                 if (!mac_address_valid)
822                         eth_random_addr(adapter->mac_address);
823         }
824         lan743x_mac_set_address(adapter, adapter->mac_address);
825         ether_addr_copy(netdev->dev_addr, adapter->mac_address);
826
827         return 0;
828 }
829
830 static int lan743x_mac_open(struct lan743x_adapter *adapter)
831 {
832         int ret = 0;
833         u32 temp;
834
835         temp = lan743x_csr_read(adapter, MAC_RX);
836         lan743x_csr_write(adapter, MAC_RX, temp | MAC_RX_RXEN_);
837         temp = lan743x_csr_read(adapter, MAC_TX);
838         lan743x_csr_write(adapter, MAC_TX, temp | MAC_TX_TXEN_);
839         return ret;
840 }
841
842 static void lan743x_mac_close(struct lan743x_adapter *adapter)
843 {
844         u32 temp;
845
846         temp = lan743x_csr_read(adapter, MAC_TX);
847         temp &= ~MAC_TX_TXEN_;
848         lan743x_csr_write(adapter, MAC_TX, temp);
849         lan743x_csr_wait_for_bit(adapter, MAC_TX, MAC_TX_TXD_,
850                                  1, 1000, 20000, 100);
851
852         temp = lan743x_csr_read(adapter, MAC_RX);
853         temp &= ~MAC_RX_RXEN_;
854         lan743x_csr_write(adapter, MAC_RX, temp);
855         lan743x_csr_wait_for_bit(adapter, MAC_RX, MAC_RX_RXD_,
856                                  1, 1000, 20000, 100);
857 }
858
859 static void lan743x_mac_flow_ctrl_set_enables(struct lan743x_adapter *adapter,
860                                               bool tx_enable, bool rx_enable)
861 {
862         u32 flow_setting = 0;
863
864         /* set maximum pause time because when fifo space frees
865          * up a zero value pause frame will be sent to release the pause
866          */
867         flow_setting = MAC_FLOW_CR_FCPT_MASK_;
868         if (tx_enable)
869                 flow_setting |= MAC_FLOW_CR_TX_FCEN_;
870         if (rx_enable)
871                 flow_setting |= MAC_FLOW_CR_RX_FCEN_;
872         lan743x_csr_write(adapter, MAC_FLOW, flow_setting);
873 }
874
875 static int lan743x_mac_set_mtu(struct lan743x_adapter *adapter, int new_mtu)
876 {
877         int enabled = 0;
878         u32 mac_rx = 0;
879
880         mac_rx = lan743x_csr_read(adapter, MAC_RX);
881         if (mac_rx & MAC_RX_RXEN_) {
882                 enabled = 1;
883                 if (mac_rx & MAC_RX_RXD_) {
884                         lan743x_csr_write(adapter, MAC_RX, mac_rx);
885                         mac_rx &= ~MAC_RX_RXD_;
886                 }
887                 mac_rx &= ~MAC_RX_RXEN_;
888                 lan743x_csr_write(adapter, MAC_RX, mac_rx);
889                 lan743x_csr_wait_for_bit(adapter, MAC_RX, MAC_RX_RXD_,
890                                          1, 1000, 20000, 100);
891                 lan743x_csr_write(adapter, MAC_RX, mac_rx | MAC_RX_RXD_);
892         }
893
894         mac_rx &= ~(MAC_RX_MAX_SIZE_MASK_);
895         mac_rx |= (((new_mtu + ETH_HLEN + 4) << MAC_RX_MAX_SIZE_SHIFT_) &
896                   MAC_RX_MAX_SIZE_MASK_);
897         lan743x_csr_write(adapter, MAC_RX, mac_rx);
898
899         if (enabled) {
900                 mac_rx |= MAC_RX_RXEN_;
901                 lan743x_csr_write(adapter, MAC_RX, mac_rx);
902         }
903         return 0;
904 }
905
906 /* PHY */
907 static int lan743x_phy_reset(struct lan743x_adapter *adapter)
908 {
909         u32 data;
910
911         /* Only called with in probe, and before mdiobus_register */
912
913         data = lan743x_csr_read(adapter, PMT_CTL);
914         data |= PMT_CTL_ETH_PHY_RST_;
915         lan743x_csr_write(adapter, PMT_CTL, data);
916
917         return readx_poll_timeout(LAN743X_CSR_READ_OP, PMT_CTL, data,
918                                   (!(data & PMT_CTL_ETH_PHY_RST_) &&
919                                   (data & PMT_CTL_READY_)),
920                                   50000, 1000000);
921 }
922
923 static void lan743x_phy_update_flowcontrol(struct lan743x_adapter *adapter,
924                                            u8 duplex, u16 local_adv,
925                                            u16 remote_adv)
926 {
927         struct lan743x_phy *phy = &adapter->phy;
928         u8 cap;
929
930         if (phy->fc_autoneg)
931                 cap = mii_resolve_flowctrl_fdx(local_adv, remote_adv);
932         else
933                 cap = phy->fc_request_control;
934
935         lan743x_mac_flow_ctrl_set_enables(adapter,
936                                           cap & FLOW_CTRL_TX,
937                                           cap & FLOW_CTRL_RX);
938 }
939
940 static int lan743x_phy_init(struct lan743x_adapter *adapter)
941 {
942         return lan743x_phy_reset(adapter);
943 }
944
945 static void lan743x_phy_link_status_change(struct net_device *netdev)
946 {
947         struct lan743x_adapter *adapter = netdev_priv(netdev);
948         struct phy_device *phydev = netdev->phydev;
949         u32 data;
950
951         phy_print_status(phydev);
952         if (phydev->state == PHY_RUNNING) {
953                 struct ethtool_link_ksettings ksettings;
954                 int remote_advertisement = 0;
955                 int local_advertisement = 0;
956
957                 data = lan743x_csr_read(adapter, MAC_CR);
958
959                 /* set interface mode */
960                 if (phy_interface_mode_is_rgmii(adapter->phy_mode))
961                         /* RGMII */
962                         data &= ~MAC_CR_MII_EN_;
963                 else
964                         /* GMII */
965                         data |= MAC_CR_MII_EN_;
966
967                 /* set duplex mode */
968                 if (phydev->duplex)
969                         data |= MAC_CR_DPX_;
970                 else
971                         data &= ~MAC_CR_DPX_;
972
973                 /* set bus speed */
974                 switch (phydev->speed) {
975                 case SPEED_10:
976                         data &= ~MAC_CR_CFG_H_;
977                         data &= ~MAC_CR_CFG_L_;
978                 break;
979                 case SPEED_100:
980                         data &= ~MAC_CR_CFG_H_;
981                         data |= MAC_CR_CFG_L_;
982                 break;
983                 case SPEED_1000:
984                         data |= MAC_CR_CFG_H_;
985                         data &= ~MAC_CR_CFG_L_;
986                 break;
987                 }
988                 lan743x_csr_write(adapter, MAC_CR, data);
989
990                 memset(&ksettings, 0, sizeof(ksettings));
991                 phy_ethtool_get_link_ksettings(netdev, &ksettings);
992                 local_advertisement =
993                         linkmode_adv_to_mii_adv_t(phydev->advertising);
994                 remote_advertisement =
995                         linkmode_adv_to_mii_adv_t(phydev->lp_advertising);
996
997                 lan743x_phy_update_flowcontrol(adapter,
998                                                ksettings.base.duplex,
999                                                local_advertisement,
1000                                                remote_advertisement);
1001                 lan743x_ptp_update_latency(adapter, ksettings.base.speed);
1002         }
1003 }
1004
1005 static void lan743x_phy_close(struct lan743x_adapter *adapter)
1006 {
1007         struct net_device *netdev = adapter->netdev;
1008
1009         phy_stop(netdev->phydev);
1010         phy_disconnect(netdev->phydev);
1011         netdev->phydev = NULL;
1012 }
1013
1014 static int lan743x_phy_open(struct lan743x_adapter *adapter)
1015 {
1016         struct lan743x_phy *phy = &adapter->phy;
1017         struct phy_device *phydev = NULL;
1018         struct device_node *phynode;
1019         struct net_device *netdev;
1020         int ret = -EIO;
1021
1022         netdev = adapter->netdev;
1023         phynode = of_node_get(adapter->pdev->dev.of_node);
1024
1025         if (phynode) {
1026                 /* try devicetree phy, or fixed link */
1027                 of_get_phy_mode(phynode, &adapter->phy_mode);
1028
1029                 if (of_phy_is_fixed_link(phynode)) {
1030                         ret = of_phy_register_fixed_link(phynode);
1031                         if (ret) {
1032                                 netdev_err(netdev,
1033                                            "cannot register fixed PHY\n");
1034                                 of_node_put(phynode);
1035                                 goto return_error;
1036                         }
1037                 }
1038                 phydev = of_phy_connect(netdev, phynode,
1039                                         lan743x_phy_link_status_change, 0,
1040                                         adapter->phy_mode);
1041                 of_node_put(phynode);
1042         }
1043
1044         if (!phydev) {
1045                 /* try internal phy */
1046                 phydev = phy_find_first(adapter->mdiobus);
1047                 if (!phydev)
1048                         goto return_error;
1049
1050                 adapter->phy_mode = PHY_INTERFACE_MODE_GMII;
1051                 ret = phy_connect_direct(netdev, phydev,
1052                                          lan743x_phy_link_status_change,
1053                                          adapter->phy_mode);
1054                 if (ret)
1055                         goto return_error;
1056         }
1057
1058         /* MAC doesn't support 1000T Half */
1059         phy_remove_link_mode(phydev, ETHTOOL_LINK_MODE_1000baseT_Half_BIT);
1060
1061         /* support both flow controls */
1062         phy_support_asym_pause(phydev);
1063         phy->fc_request_control = (FLOW_CTRL_RX | FLOW_CTRL_TX);
1064         phy->fc_autoneg = phydev->autoneg;
1065
1066         phy_start(phydev);
1067         phy_start_aneg(phydev);
1068         return 0;
1069
1070 return_error:
1071         return ret;
1072 }
1073
1074 static void lan743x_rfe_open(struct lan743x_adapter *adapter)
1075 {
1076         lan743x_csr_write(adapter, RFE_RSS_CFG,
1077                 RFE_RSS_CFG_UDP_IPV6_EX_ |
1078                 RFE_RSS_CFG_TCP_IPV6_EX_ |
1079                 RFE_RSS_CFG_IPV6_EX_ |
1080                 RFE_RSS_CFG_UDP_IPV6_ |
1081                 RFE_RSS_CFG_TCP_IPV6_ |
1082                 RFE_RSS_CFG_IPV6_ |
1083                 RFE_RSS_CFG_UDP_IPV4_ |
1084                 RFE_RSS_CFG_TCP_IPV4_ |
1085                 RFE_RSS_CFG_IPV4_ |
1086                 RFE_RSS_CFG_VALID_HASH_BITS_ |
1087                 RFE_RSS_CFG_RSS_QUEUE_ENABLE_ |
1088                 RFE_RSS_CFG_RSS_HASH_STORE_ |
1089                 RFE_RSS_CFG_RSS_ENABLE_);
1090 }
1091
1092 static void lan743x_rfe_update_mac_address(struct lan743x_adapter *adapter)
1093 {
1094         u8 *mac_addr;
1095         u32 mac_addr_hi = 0;
1096         u32 mac_addr_lo = 0;
1097
1098         /* Add mac address to perfect Filter */
1099         mac_addr = adapter->mac_address;
1100         mac_addr_lo = ((((u32)(mac_addr[0])) << 0) |
1101                       (((u32)(mac_addr[1])) << 8) |
1102                       (((u32)(mac_addr[2])) << 16) |
1103                       (((u32)(mac_addr[3])) << 24));
1104         mac_addr_hi = ((((u32)(mac_addr[4])) << 0) |
1105                       (((u32)(mac_addr[5])) << 8));
1106
1107         lan743x_csr_write(adapter, RFE_ADDR_FILT_LO(0), mac_addr_lo);
1108         lan743x_csr_write(adapter, RFE_ADDR_FILT_HI(0),
1109                           mac_addr_hi | RFE_ADDR_FILT_HI_VALID_);
1110 }
1111
1112 static void lan743x_rfe_set_multicast(struct lan743x_adapter *adapter)
1113 {
1114         struct net_device *netdev = adapter->netdev;
1115         u32 hash_table[DP_SEL_VHF_HASH_LEN];
1116         u32 rfctl;
1117         u32 data;
1118
1119         rfctl = lan743x_csr_read(adapter, RFE_CTL);
1120         rfctl &= ~(RFE_CTL_AU_ | RFE_CTL_AM_ |
1121                  RFE_CTL_DA_PERFECT_ | RFE_CTL_MCAST_HASH_);
1122         rfctl |= RFE_CTL_AB_;
1123         if (netdev->flags & IFF_PROMISC) {
1124                 rfctl |= RFE_CTL_AM_ | RFE_CTL_AU_;
1125         } else {
1126                 if (netdev->flags & IFF_ALLMULTI)
1127                         rfctl |= RFE_CTL_AM_;
1128         }
1129
1130         memset(hash_table, 0, DP_SEL_VHF_HASH_LEN * sizeof(u32));
1131         if (netdev_mc_count(netdev)) {
1132                 struct netdev_hw_addr *ha;
1133                 int i;
1134
1135                 rfctl |= RFE_CTL_DA_PERFECT_;
1136                 i = 1;
1137                 netdev_for_each_mc_addr(ha, netdev) {
1138                         /* set first 32 into Perfect Filter */
1139                         if (i < 33) {
1140                                 lan743x_csr_write(adapter,
1141                                                   RFE_ADDR_FILT_HI(i), 0);
1142                                 data = ha->addr[3];
1143                                 data = ha->addr[2] | (data << 8);
1144                                 data = ha->addr[1] | (data << 8);
1145                                 data = ha->addr[0] | (data << 8);
1146                                 lan743x_csr_write(adapter,
1147                                                   RFE_ADDR_FILT_LO(i), data);
1148                                 data = ha->addr[5];
1149                                 data = ha->addr[4] | (data << 8);
1150                                 data |= RFE_ADDR_FILT_HI_VALID_;
1151                                 lan743x_csr_write(adapter,
1152                                                   RFE_ADDR_FILT_HI(i), data);
1153                         } else {
1154                                 u32 bitnum = (ether_crc(ETH_ALEN, ha->addr) >>
1155                                              23) & 0x1FF;
1156                                 hash_table[bitnum / 32] |= (1 << (bitnum % 32));
1157                                 rfctl |= RFE_CTL_MCAST_HASH_;
1158                         }
1159                         i++;
1160                 }
1161         }
1162
1163         lan743x_dp_write(adapter, DP_SEL_RFE_RAM,
1164                          DP_SEL_VHF_VLAN_LEN,
1165                          DP_SEL_VHF_HASH_LEN, hash_table);
1166         lan743x_csr_write(adapter, RFE_CTL, rfctl);
1167 }
1168
1169 static int lan743x_dmac_init(struct lan743x_adapter *adapter)
1170 {
1171         u32 data = 0;
1172
1173         lan743x_csr_write(adapter, DMAC_CMD, DMAC_CMD_SWR_);
1174         lan743x_csr_wait_for_bit(adapter, DMAC_CMD, DMAC_CMD_SWR_,
1175                                  0, 1000, 20000, 100);
1176         switch (DEFAULT_DMA_DESCRIPTOR_SPACING) {
1177         case DMA_DESCRIPTOR_SPACING_16:
1178                 data = DMAC_CFG_MAX_DSPACE_16_;
1179                 break;
1180         case DMA_DESCRIPTOR_SPACING_32:
1181                 data = DMAC_CFG_MAX_DSPACE_32_;
1182                 break;
1183         case DMA_DESCRIPTOR_SPACING_64:
1184                 data = DMAC_CFG_MAX_DSPACE_64_;
1185                 break;
1186         case DMA_DESCRIPTOR_SPACING_128:
1187                 data = DMAC_CFG_MAX_DSPACE_128_;
1188                 break;
1189         default:
1190                 return -EPERM;
1191         }
1192         if (!(adapter->csr.flags & LAN743X_CSR_FLAG_IS_A0))
1193                 data |= DMAC_CFG_COAL_EN_;
1194         data |= DMAC_CFG_CH_ARB_SEL_RX_HIGH_;
1195         data |= DMAC_CFG_MAX_READ_REQ_SET_(6);
1196         lan743x_csr_write(adapter, DMAC_CFG, data);
1197         data = DMAC_COAL_CFG_TIMER_LIMIT_SET_(1);
1198         data |= DMAC_COAL_CFG_TIMER_TX_START_;
1199         data |= DMAC_COAL_CFG_FLUSH_INTS_;
1200         data |= DMAC_COAL_CFG_INT_EXIT_COAL_;
1201         data |= DMAC_COAL_CFG_CSR_EXIT_COAL_;
1202         data |= DMAC_COAL_CFG_TX_THRES_SET_(0x0A);
1203         data |= DMAC_COAL_CFG_RX_THRES_SET_(0x0C);
1204         lan743x_csr_write(adapter, DMAC_COAL_CFG, data);
1205         data = DMAC_OBFF_TX_THRES_SET_(0x08);
1206         data |= DMAC_OBFF_RX_THRES_SET_(0x0A);
1207         lan743x_csr_write(adapter, DMAC_OBFF_CFG, data);
1208         return 0;
1209 }
1210
1211 static int lan743x_dmac_tx_get_state(struct lan743x_adapter *adapter,
1212                                      int tx_channel)
1213 {
1214         u32 dmac_cmd = 0;
1215
1216         dmac_cmd = lan743x_csr_read(adapter, DMAC_CMD);
1217         return DMAC_CHANNEL_STATE_SET((dmac_cmd &
1218                                       DMAC_CMD_START_T_(tx_channel)),
1219                                       (dmac_cmd &
1220                                       DMAC_CMD_STOP_T_(tx_channel)));
1221 }
1222
1223 static int lan743x_dmac_tx_wait_till_stopped(struct lan743x_adapter *adapter,
1224                                              int tx_channel)
1225 {
1226         int timeout = 100;
1227         int result = 0;
1228
1229         while (timeout &&
1230                ((result = lan743x_dmac_tx_get_state(adapter, tx_channel)) ==
1231                DMAC_CHANNEL_STATE_STOP_PENDING)) {
1232                 usleep_range(1000, 20000);
1233                 timeout--;
1234         }
1235         if (result == DMAC_CHANNEL_STATE_STOP_PENDING)
1236                 result = -ENODEV;
1237         return result;
1238 }
1239
1240 static int lan743x_dmac_rx_get_state(struct lan743x_adapter *adapter,
1241                                      int rx_channel)
1242 {
1243         u32 dmac_cmd = 0;
1244
1245         dmac_cmd = lan743x_csr_read(adapter, DMAC_CMD);
1246         return DMAC_CHANNEL_STATE_SET((dmac_cmd &
1247                                       DMAC_CMD_START_R_(rx_channel)),
1248                                       (dmac_cmd &
1249                                       DMAC_CMD_STOP_R_(rx_channel)));
1250 }
1251
1252 static int lan743x_dmac_rx_wait_till_stopped(struct lan743x_adapter *adapter,
1253                                              int rx_channel)
1254 {
1255         int timeout = 100;
1256         int result = 0;
1257
1258         while (timeout &&
1259                ((result = lan743x_dmac_rx_get_state(adapter, rx_channel)) ==
1260                DMAC_CHANNEL_STATE_STOP_PENDING)) {
1261                 usleep_range(1000, 20000);
1262                 timeout--;
1263         }
1264         if (result == DMAC_CHANNEL_STATE_STOP_PENDING)
1265                 result = -ENODEV;
1266         return result;
1267 }
1268
1269 static void lan743x_tx_release_desc(struct lan743x_tx *tx,
1270                                     int descriptor_index, bool cleanup)
1271 {
1272         struct lan743x_tx_buffer_info *buffer_info = NULL;
1273         struct lan743x_tx_descriptor *descriptor = NULL;
1274         u32 descriptor_type = 0;
1275         bool ignore_sync;
1276
1277         descriptor = &tx->ring_cpu_ptr[descriptor_index];
1278         buffer_info = &tx->buffer_info[descriptor_index];
1279         if (!(buffer_info->flags & TX_BUFFER_INFO_FLAG_ACTIVE))
1280                 goto done;
1281
1282         descriptor_type = (descriptor->data0) &
1283                           TX_DESC_DATA0_DTYPE_MASK_;
1284         if (descriptor_type == TX_DESC_DATA0_DTYPE_DATA_)
1285                 goto clean_up_data_descriptor;
1286         else
1287                 goto clear_active;
1288
1289 clean_up_data_descriptor:
1290         if (buffer_info->dma_ptr) {
1291                 if (buffer_info->flags &
1292                     TX_BUFFER_INFO_FLAG_SKB_FRAGMENT) {
1293                         dma_unmap_page(&tx->adapter->pdev->dev,
1294                                        buffer_info->dma_ptr,
1295                                        buffer_info->buffer_length,
1296                                        DMA_TO_DEVICE);
1297                 } else {
1298                         dma_unmap_single(&tx->adapter->pdev->dev,
1299                                          buffer_info->dma_ptr,
1300                                          buffer_info->buffer_length,
1301                                          DMA_TO_DEVICE);
1302                 }
1303                 buffer_info->dma_ptr = 0;
1304                 buffer_info->buffer_length = 0;
1305         }
1306         if (!buffer_info->skb)
1307                 goto clear_active;
1308
1309         if (!(buffer_info->flags & TX_BUFFER_INFO_FLAG_TIMESTAMP_REQUESTED)) {
1310                 dev_kfree_skb(buffer_info->skb);
1311                 goto clear_skb;
1312         }
1313
1314         if (cleanup) {
1315                 lan743x_ptp_unrequest_tx_timestamp(tx->adapter);
1316                 dev_kfree_skb(buffer_info->skb);
1317         } else {
1318                 ignore_sync = (buffer_info->flags &
1319                                TX_BUFFER_INFO_FLAG_IGNORE_SYNC) != 0;
1320                 lan743x_ptp_tx_timestamp_skb(tx->adapter,
1321                                              buffer_info->skb, ignore_sync);
1322         }
1323
1324 clear_skb:
1325         buffer_info->skb = NULL;
1326
1327 clear_active:
1328         buffer_info->flags &= ~TX_BUFFER_INFO_FLAG_ACTIVE;
1329
1330 done:
1331         memset(buffer_info, 0, sizeof(*buffer_info));
1332         memset(descriptor, 0, sizeof(*descriptor));
1333 }
1334
1335 static int lan743x_tx_next_index(struct lan743x_tx *tx, int index)
1336 {
1337         return ((++index) % tx->ring_size);
1338 }
1339
1340 static void lan743x_tx_release_completed_descriptors(struct lan743x_tx *tx)
1341 {
1342         while ((*tx->head_cpu_ptr) != (tx->last_head)) {
1343                 lan743x_tx_release_desc(tx, tx->last_head, false);
1344                 tx->last_head = lan743x_tx_next_index(tx, tx->last_head);
1345         }
1346 }
1347
1348 static void lan743x_tx_release_all_descriptors(struct lan743x_tx *tx)
1349 {
1350         u32 original_head = 0;
1351
1352         original_head = tx->last_head;
1353         do {
1354                 lan743x_tx_release_desc(tx, tx->last_head, true);
1355                 tx->last_head = lan743x_tx_next_index(tx, tx->last_head);
1356         } while (tx->last_head != original_head);
1357         memset(tx->ring_cpu_ptr, 0,
1358                sizeof(*tx->ring_cpu_ptr) * (tx->ring_size));
1359         memset(tx->buffer_info, 0,
1360                sizeof(*tx->buffer_info) * (tx->ring_size));
1361 }
1362
1363 static int lan743x_tx_get_desc_cnt(struct lan743x_tx *tx,
1364                                    struct sk_buff *skb)
1365 {
1366         int result = 1; /* 1 for the main skb buffer */
1367         int nr_frags = 0;
1368
1369         if (skb_is_gso(skb))
1370                 result++; /* requires an extension descriptor */
1371         nr_frags = skb_shinfo(skb)->nr_frags;
1372         result += nr_frags; /* 1 for each fragment buffer */
1373         return result;
1374 }
1375
1376 static int lan743x_tx_get_avail_desc(struct lan743x_tx *tx)
1377 {
1378         int last_head = tx->last_head;
1379         int last_tail = tx->last_tail;
1380
1381         if (last_tail >= last_head)
1382                 return tx->ring_size - last_tail + last_head - 1;
1383         else
1384                 return last_head - last_tail - 1;
1385 }
1386
1387 void lan743x_tx_set_timestamping_mode(struct lan743x_tx *tx,
1388                                       bool enable_timestamping,
1389                                       bool enable_onestep_sync)
1390 {
1391         if (enable_timestamping)
1392                 tx->ts_flags |= TX_TS_FLAG_TIMESTAMPING_ENABLED;
1393         else
1394                 tx->ts_flags &= ~TX_TS_FLAG_TIMESTAMPING_ENABLED;
1395         if (enable_onestep_sync)
1396                 tx->ts_flags |= TX_TS_FLAG_ONE_STEP_SYNC;
1397         else
1398                 tx->ts_flags &= ~TX_TS_FLAG_ONE_STEP_SYNC;
1399 }
1400
1401 static int lan743x_tx_frame_start(struct lan743x_tx *tx,
1402                                   unsigned char *first_buffer,
1403                                   unsigned int first_buffer_length,
1404                                   unsigned int frame_length,
1405                                   bool time_stamp,
1406                                   bool check_sum)
1407 {
1408         /* called only from within lan743x_tx_xmit_frame.
1409          * assuming tx->ring_lock has already been acquired.
1410          */
1411         struct lan743x_tx_descriptor *tx_descriptor = NULL;
1412         struct lan743x_tx_buffer_info *buffer_info = NULL;
1413         struct lan743x_adapter *adapter = tx->adapter;
1414         struct device *dev = &adapter->pdev->dev;
1415         dma_addr_t dma_ptr;
1416
1417         tx->frame_flags |= TX_FRAME_FLAG_IN_PROGRESS;
1418         tx->frame_first = tx->last_tail;
1419         tx->frame_tail = tx->frame_first;
1420
1421         tx_descriptor = &tx->ring_cpu_ptr[tx->frame_tail];
1422         buffer_info = &tx->buffer_info[tx->frame_tail];
1423         dma_ptr = dma_map_single(dev, first_buffer, first_buffer_length,
1424                                  DMA_TO_DEVICE);
1425         if (dma_mapping_error(dev, dma_ptr))
1426                 return -ENOMEM;
1427
1428         tx_descriptor->data1 = DMA_ADDR_LOW32(dma_ptr);
1429         tx_descriptor->data2 = DMA_ADDR_HIGH32(dma_ptr);
1430         tx_descriptor->data3 = (frame_length << 16) &
1431                 TX_DESC_DATA3_FRAME_LENGTH_MSS_MASK_;
1432
1433         buffer_info->skb = NULL;
1434         buffer_info->dma_ptr = dma_ptr;
1435         buffer_info->buffer_length = first_buffer_length;
1436         buffer_info->flags |= TX_BUFFER_INFO_FLAG_ACTIVE;
1437
1438         tx->frame_data0 = (first_buffer_length &
1439                 TX_DESC_DATA0_BUF_LENGTH_MASK_) |
1440                 TX_DESC_DATA0_DTYPE_DATA_ |
1441                 TX_DESC_DATA0_FS_ |
1442                 TX_DESC_DATA0_FCS_;
1443         if (time_stamp)
1444                 tx->frame_data0 |= TX_DESC_DATA0_TSE_;
1445
1446         if (check_sum)
1447                 tx->frame_data0 |= TX_DESC_DATA0_ICE_ |
1448                                    TX_DESC_DATA0_IPE_ |
1449                                    TX_DESC_DATA0_TPE_;
1450
1451         /* data0 will be programmed in one of other frame assembler functions */
1452         return 0;
1453 }
1454
1455 static void lan743x_tx_frame_add_lso(struct lan743x_tx *tx,
1456                                      unsigned int frame_length,
1457                                      int nr_frags)
1458 {
1459         /* called only from within lan743x_tx_xmit_frame.
1460          * assuming tx->ring_lock has already been acquired.
1461          */
1462         struct lan743x_tx_descriptor *tx_descriptor = NULL;
1463         struct lan743x_tx_buffer_info *buffer_info = NULL;
1464
1465         /* wrap up previous descriptor */
1466         tx->frame_data0 |= TX_DESC_DATA0_EXT_;
1467         if (nr_frags <= 0) {
1468                 tx->frame_data0 |= TX_DESC_DATA0_LS_;
1469                 tx->frame_data0 |= TX_DESC_DATA0_IOC_;
1470         }
1471         tx_descriptor = &tx->ring_cpu_ptr[tx->frame_tail];
1472         tx_descriptor->data0 = tx->frame_data0;
1473
1474         /* move to next descriptor */
1475         tx->frame_tail = lan743x_tx_next_index(tx, tx->frame_tail);
1476         tx_descriptor = &tx->ring_cpu_ptr[tx->frame_tail];
1477         buffer_info = &tx->buffer_info[tx->frame_tail];
1478
1479         /* add extension descriptor */
1480         tx_descriptor->data1 = 0;
1481         tx_descriptor->data2 = 0;
1482         tx_descriptor->data3 = 0;
1483
1484         buffer_info->skb = NULL;
1485         buffer_info->dma_ptr = 0;
1486         buffer_info->buffer_length = 0;
1487         buffer_info->flags |= TX_BUFFER_INFO_FLAG_ACTIVE;
1488
1489         tx->frame_data0 = (frame_length & TX_DESC_DATA0_EXT_PAY_LENGTH_MASK_) |
1490                           TX_DESC_DATA0_DTYPE_EXT_ |
1491                           TX_DESC_DATA0_EXT_LSO_;
1492
1493         /* data0 will be programmed in one of other frame assembler functions */
1494 }
1495
1496 static int lan743x_tx_frame_add_fragment(struct lan743x_tx *tx,
1497                                          const skb_frag_t *fragment,
1498                                          unsigned int frame_length)
1499 {
1500         /* called only from within lan743x_tx_xmit_frame
1501          * assuming tx->ring_lock has already been acquired
1502          */
1503         struct lan743x_tx_descriptor *tx_descriptor = NULL;
1504         struct lan743x_tx_buffer_info *buffer_info = NULL;
1505         struct lan743x_adapter *adapter = tx->adapter;
1506         struct device *dev = &adapter->pdev->dev;
1507         unsigned int fragment_length = 0;
1508         dma_addr_t dma_ptr;
1509
1510         fragment_length = skb_frag_size(fragment);
1511         if (!fragment_length)
1512                 return 0;
1513
1514         /* wrap up previous descriptor */
1515         tx_descriptor = &tx->ring_cpu_ptr[tx->frame_tail];
1516         tx_descriptor->data0 = tx->frame_data0;
1517
1518         /* move to next descriptor */
1519         tx->frame_tail = lan743x_tx_next_index(tx, tx->frame_tail);
1520         tx_descriptor = &tx->ring_cpu_ptr[tx->frame_tail];
1521         buffer_info = &tx->buffer_info[tx->frame_tail];
1522         dma_ptr = skb_frag_dma_map(dev, fragment,
1523                                    0, fragment_length,
1524                                    DMA_TO_DEVICE);
1525         if (dma_mapping_error(dev, dma_ptr)) {
1526                 int desc_index;
1527
1528                 /* cleanup all previously setup descriptors */
1529                 desc_index = tx->frame_first;
1530                 while (desc_index != tx->frame_tail) {
1531                         lan743x_tx_release_desc(tx, desc_index, true);
1532                         desc_index = lan743x_tx_next_index(tx, desc_index);
1533                 }
1534                 dma_wmb();
1535                 tx->frame_flags &= ~TX_FRAME_FLAG_IN_PROGRESS;
1536                 tx->frame_first = 0;
1537                 tx->frame_data0 = 0;
1538                 tx->frame_tail = 0;
1539                 return -ENOMEM;
1540         }
1541
1542         tx_descriptor->data1 = DMA_ADDR_LOW32(dma_ptr);
1543         tx_descriptor->data2 = DMA_ADDR_HIGH32(dma_ptr);
1544         tx_descriptor->data3 = (frame_length << 16) &
1545                                TX_DESC_DATA3_FRAME_LENGTH_MSS_MASK_;
1546
1547         buffer_info->skb = NULL;
1548         buffer_info->dma_ptr = dma_ptr;
1549         buffer_info->buffer_length = fragment_length;
1550         buffer_info->flags |= TX_BUFFER_INFO_FLAG_ACTIVE;
1551         buffer_info->flags |= TX_BUFFER_INFO_FLAG_SKB_FRAGMENT;
1552
1553         tx->frame_data0 = (fragment_length & TX_DESC_DATA0_BUF_LENGTH_MASK_) |
1554                           TX_DESC_DATA0_DTYPE_DATA_ |
1555                           TX_DESC_DATA0_FCS_;
1556
1557         /* data0 will be programmed in one of other frame assembler functions */
1558         return 0;
1559 }
1560
1561 static void lan743x_tx_frame_end(struct lan743x_tx *tx,
1562                                  struct sk_buff *skb,
1563                                  bool time_stamp,
1564                                  bool ignore_sync)
1565 {
1566         /* called only from within lan743x_tx_xmit_frame
1567          * assuming tx->ring_lock has already been acquired
1568          */
1569         struct lan743x_tx_descriptor *tx_descriptor = NULL;
1570         struct lan743x_tx_buffer_info *buffer_info = NULL;
1571         struct lan743x_adapter *adapter = tx->adapter;
1572         u32 tx_tail_flags = 0;
1573
1574         /* wrap up previous descriptor */
1575         if ((tx->frame_data0 & TX_DESC_DATA0_DTYPE_MASK_) ==
1576             TX_DESC_DATA0_DTYPE_DATA_) {
1577                 tx->frame_data0 |= TX_DESC_DATA0_LS_;
1578                 tx->frame_data0 |= TX_DESC_DATA0_IOC_;
1579         }
1580
1581         tx_descriptor = &tx->ring_cpu_ptr[tx->frame_tail];
1582         buffer_info = &tx->buffer_info[tx->frame_tail];
1583         buffer_info->skb = skb;
1584         if (time_stamp)
1585                 buffer_info->flags |= TX_BUFFER_INFO_FLAG_TIMESTAMP_REQUESTED;
1586         if (ignore_sync)
1587                 buffer_info->flags |= TX_BUFFER_INFO_FLAG_IGNORE_SYNC;
1588
1589         tx_descriptor->data0 = tx->frame_data0;
1590         tx->frame_tail = lan743x_tx_next_index(tx, tx->frame_tail);
1591         tx->last_tail = tx->frame_tail;
1592
1593         dma_wmb();
1594
1595         if (tx->vector_flags & LAN743X_VECTOR_FLAG_VECTOR_ENABLE_AUTO_SET)
1596                 tx_tail_flags |= TX_TAIL_SET_TOP_INT_VEC_EN_;
1597         if (tx->vector_flags & LAN743X_VECTOR_FLAG_SOURCE_ENABLE_AUTO_SET)
1598                 tx_tail_flags |= TX_TAIL_SET_DMAC_INT_EN_ |
1599                 TX_TAIL_SET_TOP_INT_EN_;
1600
1601         lan743x_csr_write(adapter, TX_TAIL(tx->channel_number),
1602                           tx_tail_flags | tx->frame_tail);
1603         tx->frame_flags &= ~TX_FRAME_FLAG_IN_PROGRESS;
1604 }
1605
1606 static netdev_tx_t lan743x_tx_xmit_frame(struct lan743x_tx *tx,
1607                                          struct sk_buff *skb)
1608 {
1609         int required_number_of_descriptors = 0;
1610         unsigned int start_frame_length = 0;
1611         unsigned int frame_length = 0;
1612         unsigned int head_length = 0;
1613         unsigned long irq_flags = 0;
1614         bool do_timestamp = false;
1615         bool ignore_sync = false;
1616         int nr_frags = 0;
1617         bool gso = false;
1618         int j;
1619
1620         required_number_of_descriptors = lan743x_tx_get_desc_cnt(tx, skb);
1621
1622         spin_lock_irqsave(&tx->ring_lock, irq_flags);
1623         if (required_number_of_descriptors >
1624                 lan743x_tx_get_avail_desc(tx)) {
1625                 if (required_number_of_descriptors > (tx->ring_size - 1)) {
1626                         dev_kfree_skb(skb);
1627                 } else {
1628                         /* save to overflow buffer */
1629                         tx->overflow_skb = skb;
1630                         netif_stop_queue(tx->adapter->netdev);
1631                 }
1632                 goto unlock;
1633         }
1634
1635         /* space available, transmit skb  */
1636         if ((skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) &&
1637             (tx->ts_flags & TX_TS_FLAG_TIMESTAMPING_ENABLED) &&
1638             (lan743x_ptp_request_tx_timestamp(tx->adapter))) {
1639                 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
1640                 do_timestamp = true;
1641                 if (tx->ts_flags & TX_TS_FLAG_ONE_STEP_SYNC)
1642                         ignore_sync = true;
1643         }
1644         head_length = skb_headlen(skb);
1645         frame_length = skb_pagelen(skb);
1646         nr_frags = skb_shinfo(skb)->nr_frags;
1647         start_frame_length = frame_length;
1648         gso = skb_is_gso(skb);
1649         if (gso) {
1650                 start_frame_length = max(skb_shinfo(skb)->gso_size,
1651                                          (unsigned short)8);
1652         }
1653
1654         if (lan743x_tx_frame_start(tx,
1655                                    skb->data, head_length,
1656                                    start_frame_length,
1657                                    do_timestamp,
1658                                    skb->ip_summed == CHECKSUM_PARTIAL)) {
1659                 dev_kfree_skb(skb);
1660                 goto unlock;
1661         }
1662
1663         if (gso)
1664                 lan743x_tx_frame_add_lso(tx, frame_length, nr_frags);
1665
1666         if (nr_frags <= 0)
1667                 goto finish;
1668
1669         for (j = 0; j < nr_frags; j++) {
1670                 const skb_frag_t *frag = &(skb_shinfo(skb)->frags[j]);
1671
1672                 if (lan743x_tx_frame_add_fragment(tx, frag, frame_length)) {
1673                         /* upon error no need to call
1674                          *      lan743x_tx_frame_end
1675                          * frame assembler clean up was performed inside
1676                          *      lan743x_tx_frame_add_fragment
1677                          */
1678                         dev_kfree_skb(skb);
1679                         goto unlock;
1680                 }
1681         }
1682
1683 finish:
1684         lan743x_tx_frame_end(tx, skb, do_timestamp, ignore_sync);
1685
1686 unlock:
1687         spin_unlock_irqrestore(&tx->ring_lock, irq_flags);
1688         return NETDEV_TX_OK;
1689 }
1690
1691 static int lan743x_tx_napi_poll(struct napi_struct *napi, int weight)
1692 {
1693         struct lan743x_tx *tx = container_of(napi, struct lan743x_tx, napi);
1694         struct lan743x_adapter *adapter = tx->adapter;
1695         bool start_transmitter = false;
1696         unsigned long irq_flags = 0;
1697         u32 ioc_bit = 0;
1698
1699         ioc_bit = DMAC_INT_BIT_TX_IOC_(tx->channel_number);
1700         lan743x_csr_read(adapter, DMAC_INT_STS);
1701         if (tx->vector_flags & LAN743X_VECTOR_FLAG_SOURCE_STATUS_W2C)
1702                 lan743x_csr_write(adapter, DMAC_INT_STS, ioc_bit);
1703         spin_lock_irqsave(&tx->ring_lock, irq_flags);
1704
1705         /* clean up tx ring */
1706         lan743x_tx_release_completed_descriptors(tx);
1707         if (netif_queue_stopped(adapter->netdev)) {
1708                 if (tx->overflow_skb) {
1709                         if (lan743x_tx_get_desc_cnt(tx, tx->overflow_skb) <=
1710                                 lan743x_tx_get_avail_desc(tx))
1711                                 start_transmitter = true;
1712                 } else {
1713                         netif_wake_queue(adapter->netdev);
1714                 }
1715         }
1716         spin_unlock_irqrestore(&tx->ring_lock, irq_flags);
1717
1718         if (start_transmitter) {
1719                 /* space is now available, transmit overflow skb */
1720                 lan743x_tx_xmit_frame(tx, tx->overflow_skb);
1721                 tx->overflow_skb = NULL;
1722                 netif_wake_queue(adapter->netdev);
1723         }
1724
1725         if (!napi_complete(napi))
1726                 goto done;
1727
1728         /* enable isr */
1729         lan743x_csr_write(adapter, INT_EN_SET,
1730                           INT_BIT_DMA_TX_(tx->channel_number));
1731         lan743x_csr_read(adapter, INT_STS);
1732
1733 done:
1734         return 0;
1735 }
1736
1737 static void lan743x_tx_ring_cleanup(struct lan743x_tx *tx)
1738 {
1739         if (tx->head_cpu_ptr) {
1740                 dma_free_coherent(&tx->adapter->pdev->dev,
1741                                   sizeof(*tx->head_cpu_ptr), tx->head_cpu_ptr,
1742                                   tx->head_dma_ptr);
1743                 tx->head_cpu_ptr = NULL;
1744                 tx->head_dma_ptr = 0;
1745         }
1746         kfree(tx->buffer_info);
1747         tx->buffer_info = NULL;
1748
1749         if (tx->ring_cpu_ptr) {
1750                 dma_free_coherent(&tx->adapter->pdev->dev,
1751                                   tx->ring_allocation_size, tx->ring_cpu_ptr,
1752                                   tx->ring_dma_ptr);
1753                 tx->ring_allocation_size = 0;
1754                 tx->ring_cpu_ptr = NULL;
1755                 tx->ring_dma_ptr = 0;
1756         }
1757         tx->ring_size = 0;
1758 }
1759
1760 static int lan743x_tx_ring_init(struct lan743x_tx *tx)
1761 {
1762         size_t ring_allocation_size = 0;
1763         void *cpu_ptr = NULL;
1764         dma_addr_t dma_ptr;
1765         int ret = -ENOMEM;
1766
1767         tx->ring_size = LAN743X_TX_RING_SIZE;
1768         if (tx->ring_size & ~TX_CFG_B_TX_RING_LEN_MASK_) {
1769                 ret = -EINVAL;
1770                 goto cleanup;
1771         }
1772         ring_allocation_size = ALIGN(tx->ring_size *
1773                                      sizeof(struct lan743x_tx_descriptor),
1774                                      PAGE_SIZE);
1775         dma_ptr = 0;
1776         cpu_ptr = dma_alloc_coherent(&tx->adapter->pdev->dev,
1777                                      ring_allocation_size, &dma_ptr, GFP_KERNEL);
1778         if (!cpu_ptr) {
1779                 ret = -ENOMEM;
1780                 goto cleanup;
1781         }
1782
1783         tx->ring_allocation_size = ring_allocation_size;
1784         tx->ring_cpu_ptr = (struct lan743x_tx_descriptor *)cpu_ptr;
1785         tx->ring_dma_ptr = dma_ptr;
1786
1787         cpu_ptr = kcalloc(tx->ring_size, sizeof(*tx->buffer_info), GFP_KERNEL);
1788         if (!cpu_ptr) {
1789                 ret = -ENOMEM;
1790                 goto cleanup;
1791         }
1792         tx->buffer_info = (struct lan743x_tx_buffer_info *)cpu_ptr;
1793         dma_ptr = 0;
1794         cpu_ptr = dma_alloc_coherent(&tx->adapter->pdev->dev,
1795                                      sizeof(*tx->head_cpu_ptr), &dma_ptr,
1796                                      GFP_KERNEL);
1797         if (!cpu_ptr) {
1798                 ret = -ENOMEM;
1799                 goto cleanup;
1800         }
1801
1802         tx->head_cpu_ptr = cpu_ptr;
1803         tx->head_dma_ptr = dma_ptr;
1804         if (tx->head_dma_ptr & 0x3) {
1805                 ret = -ENOMEM;
1806                 goto cleanup;
1807         }
1808
1809         return 0;
1810
1811 cleanup:
1812         lan743x_tx_ring_cleanup(tx);
1813         return ret;
1814 }
1815
1816 static void lan743x_tx_close(struct lan743x_tx *tx)
1817 {
1818         struct lan743x_adapter *adapter = tx->adapter;
1819
1820         lan743x_csr_write(adapter,
1821                           DMAC_CMD,
1822                           DMAC_CMD_STOP_T_(tx->channel_number));
1823         lan743x_dmac_tx_wait_till_stopped(adapter, tx->channel_number);
1824
1825         lan743x_csr_write(adapter,
1826                           DMAC_INT_EN_CLR,
1827                           DMAC_INT_BIT_TX_IOC_(tx->channel_number));
1828         lan743x_csr_write(adapter, INT_EN_CLR,
1829                           INT_BIT_DMA_TX_(tx->channel_number));
1830         napi_disable(&tx->napi);
1831         netif_napi_del(&tx->napi);
1832
1833         lan743x_csr_write(adapter, FCT_TX_CTL,
1834                           FCT_TX_CTL_DIS_(tx->channel_number));
1835         lan743x_csr_wait_for_bit(adapter, FCT_TX_CTL,
1836                                  FCT_TX_CTL_EN_(tx->channel_number),
1837                                  0, 1000, 20000, 100);
1838
1839         lan743x_tx_release_all_descriptors(tx);
1840
1841         if (tx->overflow_skb) {
1842                 dev_kfree_skb(tx->overflow_skb);
1843                 tx->overflow_skb = NULL;
1844         }
1845
1846         lan743x_tx_ring_cleanup(tx);
1847 }
1848
1849 static int lan743x_tx_open(struct lan743x_tx *tx)
1850 {
1851         struct lan743x_adapter *adapter = NULL;
1852         u32 data = 0;
1853         int ret;
1854
1855         adapter = tx->adapter;
1856         ret = lan743x_tx_ring_init(tx);
1857         if (ret)
1858                 return ret;
1859
1860         /* initialize fifo */
1861         lan743x_csr_write(adapter, FCT_TX_CTL,
1862                           FCT_TX_CTL_RESET_(tx->channel_number));
1863         lan743x_csr_wait_for_bit(adapter, FCT_TX_CTL,
1864                                  FCT_TX_CTL_RESET_(tx->channel_number),
1865                                  0, 1000, 20000, 100);
1866
1867         /* enable fifo */
1868         lan743x_csr_write(adapter, FCT_TX_CTL,
1869                           FCT_TX_CTL_EN_(tx->channel_number));
1870
1871         /* reset tx channel */
1872         lan743x_csr_write(adapter, DMAC_CMD,
1873                           DMAC_CMD_TX_SWR_(tx->channel_number));
1874         lan743x_csr_wait_for_bit(adapter, DMAC_CMD,
1875                                  DMAC_CMD_TX_SWR_(tx->channel_number),
1876                                  0, 1000, 20000, 100);
1877
1878         /* Write TX_BASE_ADDR */
1879         lan743x_csr_write(adapter,
1880                           TX_BASE_ADDRH(tx->channel_number),
1881                           DMA_ADDR_HIGH32(tx->ring_dma_ptr));
1882         lan743x_csr_write(adapter,
1883                           TX_BASE_ADDRL(tx->channel_number),
1884                           DMA_ADDR_LOW32(tx->ring_dma_ptr));
1885
1886         /* Write TX_CFG_B */
1887         data = lan743x_csr_read(adapter, TX_CFG_B(tx->channel_number));
1888         data &= ~TX_CFG_B_TX_RING_LEN_MASK_;
1889         data |= ((tx->ring_size) & TX_CFG_B_TX_RING_LEN_MASK_);
1890         if (!(adapter->csr.flags & LAN743X_CSR_FLAG_IS_A0))
1891                 data |= TX_CFG_B_TDMABL_512_;
1892         lan743x_csr_write(adapter, TX_CFG_B(tx->channel_number), data);
1893
1894         /* Write TX_CFG_A */
1895         data = TX_CFG_A_TX_TMR_HPWB_SEL_IOC_ | TX_CFG_A_TX_HP_WB_EN_;
1896         if (!(adapter->csr.flags & LAN743X_CSR_FLAG_IS_A0)) {
1897                 data |= TX_CFG_A_TX_HP_WB_ON_INT_TMR_;
1898                 data |= TX_CFG_A_TX_PF_THRES_SET_(0x10);
1899                 data |= TX_CFG_A_TX_PF_PRI_THRES_SET_(0x04);
1900                 data |= TX_CFG_A_TX_HP_WB_THRES_SET_(0x07);
1901         }
1902         lan743x_csr_write(adapter, TX_CFG_A(tx->channel_number), data);
1903
1904         /* Write TX_HEAD_WRITEBACK_ADDR */
1905         lan743x_csr_write(adapter,
1906                           TX_HEAD_WRITEBACK_ADDRH(tx->channel_number),
1907                           DMA_ADDR_HIGH32(tx->head_dma_ptr));
1908         lan743x_csr_write(adapter,
1909                           TX_HEAD_WRITEBACK_ADDRL(tx->channel_number),
1910                           DMA_ADDR_LOW32(tx->head_dma_ptr));
1911
1912         /* set last head */
1913         tx->last_head = lan743x_csr_read(adapter, TX_HEAD(tx->channel_number));
1914
1915         /* write TX_TAIL */
1916         tx->last_tail = 0;
1917         lan743x_csr_write(adapter, TX_TAIL(tx->channel_number),
1918                           (u32)(tx->last_tail));
1919         tx->vector_flags = lan743x_intr_get_vector_flags(adapter,
1920                                                          INT_BIT_DMA_TX_
1921                                                          (tx->channel_number));
1922         netif_tx_napi_add(adapter->netdev,
1923                           &tx->napi, lan743x_tx_napi_poll,
1924                           tx->ring_size - 1);
1925         napi_enable(&tx->napi);
1926
1927         data = 0;
1928         if (tx->vector_flags & LAN743X_VECTOR_FLAG_SOURCE_ENABLE_AUTO_CLEAR)
1929                 data |= TX_CFG_C_TX_TOP_INT_EN_AUTO_CLR_;
1930         if (tx->vector_flags & LAN743X_VECTOR_FLAG_SOURCE_STATUS_AUTO_CLEAR)
1931                 data |= TX_CFG_C_TX_DMA_INT_STS_AUTO_CLR_;
1932         if (tx->vector_flags & LAN743X_VECTOR_FLAG_SOURCE_STATUS_R2C)
1933                 data |= TX_CFG_C_TX_INT_STS_R2C_MODE_MASK_;
1934         if (tx->vector_flags & LAN743X_VECTOR_FLAG_SOURCE_ENABLE_R2C)
1935                 data |= TX_CFG_C_TX_INT_EN_R2C_;
1936         lan743x_csr_write(adapter, TX_CFG_C(tx->channel_number), data);
1937
1938         if (!(tx->vector_flags & LAN743X_VECTOR_FLAG_SOURCE_ENABLE_AUTO_SET))
1939                 lan743x_csr_write(adapter, INT_EN_SET,
1940                                   INT_BIT_DMA_TX_(tx->channel_number));
1941         lan743x_csr_write(adapter, DMAC_INT_EN_SET,
1942                           DMAC_INT_BIT_TX_IOC_(tx->channel_number));
1943
1944         /*  start dmac channel */
1945         lan743x_csr_write(adapter, DMAC_CMD,
1946                           DMAC_CMD_START_T_(tx->channel_number));
1947         return 0;
1948 }
1949
1950 static int lan743x_rx_next_index(struct lan743x_rx *rx, int index)
1951 {
1952         return ((++index) % rx->ring_size);
1953 }
1954
1955 static struct sk_buff *lan743x_rx_allocate_skb(struct lan743x_rx *rx)
1956 {
1957         int length = 0;
1958
1959         length = (LAN743X_MAX_FRAME_SIZE + ETH_HLEN + 4 + RX_HEAD_PADDING);
1960         return __netdev_alloc_skb(rx->adapter->netdev,
1961                                   length, GFP_ATOMIC | GFP_DMA);
1962 }
1963
1964 static int lan743x_rx_init_ring_element(struct lan743x_rx *rx, int index,
1965                                         struct sk_buff *skb)
1966 {
1967         struct lan743x_rx_buffer_info *buffer_info;
1968         struct lan743x_rx_descriptor *descriptor;
1969         int length = 0;
1970
1971         length = (LAN743X_MAX_FRAME_SIZE + ETH_HLEN + 4 + RX_HEAD_PADDING);
1972         descriptor = &rx->ring_cpu_ptr[index];
1973         buffer_info = &rx->buffer_info[index];
1974         buffer_info->skb = skb;
1975         if (!(buffer_info->skb))
1976                 return -ENOMEM;
1977         buffer_info->dma_ptr = dma_map_single(&rx->adapter->pdev->dev,
1978                                               buffer_info->skb->data,
1979                                               length,
1980                                               DMA_FROM_DEVICE);
1981         if (dma_mapping_error(&rx->adapter->pdev->dev,
1982                               buffer_info->dma_ptr)) {
1983                 buffer_info->dma_ptr = 0;
1984                 return -ENOMEM;
1985         }
1986
1987         buffer_info->buffer_length = length;
1988         descriptor->data1 = DMA_ADDR_LOW32(buffer_info->dma_ptr);
1989         descriptor->data2 = DMA_ADDR_HIGH32(buffer_info->dma_ptr);
1990         descriptor->data3 = 0;
1991         descriptor->data0 = (RX_DESC_DATA0_OWN_ |
1992                             (length & RX_DESC_DATA0_BUF_LENGTH_MASK_));
1993         skb_reserve(buffer_info->skb, RX_HEAD_PADDING);
1994
1995         return 0;
1996 }
1997
1998 static void lan743x_rx_reuse_ring_element(struct lan743x_rx *rx, int index)
1999 {
2000         struct lan743x_rx_buffer_info *buffer_info;
2001         struct lan743x_rx_descriptor *descriptor;
2002
2003         descriptor = &rx->ring_cpu_ptr[index];
2004         buffer_info = &rx->buffer_info[index];
2005
2006         descriptor->data1 = DMA_ADDR_LOW32(buffer_info->dma_ptr);
2007         descriptor->data2 = DMA_ADDR_HIGH32(buffer_info->dma_ptr);
2008         descriptor->data3 = 0;
2009         descriptor->data0 = (RX_DESC_DATA0_OWN_ |
2010                             ((buffer_info->buffer_length) &
2011                             RX_DESC_DATA0_BUF_LENGTH_MASK_));
2012 }
2013
2014 static void lan743x_rx_release_ring_element(struct lan743x_rx *rx, int index)
2015 {
2016         struct lan743x_rx_buffer_info *buffer_info;
2017         struct lan743x_rx_descriptor *descriptor;
2018
2019         descriptor = &rx->ring_cpu_ptr[index];
2020         buffer_info = &rx->buffer_info[index];
2021
2022         memset(descriptor, 0, sizeof(*descriptor));
2023
2024         if (buffer_info->dma_ptr) {
2025                 dma_unmap_single(&rx->adapter->pdev->dev,
2026                                  buffer_info->dma_ptr,
2027                                  buffer_info->buffer_length,
2028                                  DMA_FROM_DEVICE);
2029                 buffer_info->dma_ptr = 0;
2030         }
2031
2032         if (buffer_info->skb) {
2033                 dev_kfree_skb(buffer_info->skb);
2034                 buffer_info->skb = NULL;
2035         }
2036
2037         memset(buffer_info, 0, sizeof(*buffer_info));
2038 }
2039
2040 static int lan743x_rx_process_packet(struct lan743x_rx *rx)
2041 {
2042         struct skb_shared_hwtstamps *hwtstamps = NULL;
2043         int result = RX_PROCESS_RESULT_NOTHING_TO_DO;
2044         int current_head_index = *rx->head_cpu_ptr;
2045         struct lan743x_rx_buffer_info *buffer_info;
2046         struct lan743x_rx_descriptor *descriptor;
2047         int extension_index = -1;
2048         int first_index = -1;
2049         int last_index = -1;
2050
2051         if (current_head_index < 0 || current_head_index >= rx->ring_size)
2052                 goto done;
2053
2054         if (rx->last_head < 0 || rx->last_head >= rx->ring_size)
2055                 goto done;
2056
2057         if (rx->last_head != current_head_index) {
2058                 descriptor = &rx->ring_cpu_ptr[rx->last_head];
2059                 if (descriptor->data0 & RX_DESC_DATA0_OWN_)
2060                         goto done;
2061
2062                 if (!(descriptor->data0 & RX_DESC_DATA0_FS_))
2063                         goto done;
2064
2065                 first_index = rx->last_head;
2066                 if (descriptor->data0 & RX_DESC_DATA0_LS_) {
2067                         last_index = rx->last_head;
2068                 } else {
2069                         int index;
2070
2071                         index = lan743x_rx_next_index(rx, first_index);
2072                         while (index != current_head_index) {
2073                                 descriptor = &rx->ring_cpu_ptr[index];
2074                                 if (descriptor->data0 & RX_DESC_DATA0_OWN_)
2075                                         goto done;
2076
2077                                 if (descriptor->data0 & RX_DESC_DATA0_LS_) {
2078                                         last_index = index;
2079                                         break;
2080                                 }
2081                                 index = lan743x_rx_next_index(rx, index);
2082                         }
2083                 }
2084                 if (last_index >= 0) {
2085                         descriptor = &rx->ring_cpu_ptr[last_index];
2086                         if (descriptor->data0 & RX_DESC_DATA0_EXT_) {
2087                                 /* extension is expected to follow */
2088                                 int index = lan743x_rx_next_index(rx,
2089                                                                   last_index);
2090                                 if (index != current_head_index) {
2091                                         descriptor = &rx->ring_cpu_ptr[index];
2092                                         if (descriptor->data0 &
2093                                             RX_DESC_DATA0_OWN_) {
2094                                                 goto done;
2095                                         }
2096                                         if (descriptor->data0 &
2097                                             RX_DESC_DATA0_EXT_) {
2098                                                 extension_index = index;
2099                                         } else {
2100                                                 goto done;
2101                                         }
2102                                 } else {
2103                                         /* extension is not yet available */
2104                                         /* prevent processing of this packet */
2105                                         first_index = -1;
2106                                         last_index = -1;
2107                                 }
2108                         }
2109                 }
2110         }
2111         if (first_index >= 0 && last_index >= 0) {
2112                 int real_last_index = last_index;
2113                 struct sk_buff *skb = NULL;
2114                 u32 ts_sec = 0;
2115                 u32 ts_nsec = 0;
2116
2117                 /* packet is available */
2118                 if (first_index == last_index) {
2119                         /* single buffer packet */
2120                         struct sk_buff *new_skb = NULL;
2121                         int packet_length;
2122
2123                         new_skb = lan743x_rx_allocate_skb(rx);
2124                         if (!new_skb) {
2125                                 /* failed to allocate next skb.
2126                                  * Memory is very low.
2127                                  * Drop this packet and reuse buffer.
2128                                  */
2129                                 lan743x_rx_reuse_ring_element(rx, first_index);
2130                                 goto process_extension;
2131                         }
2132
2133                         buffer_info = &rx->buffer_info[first_index];
2134                         skb = buffer_info->skb;
2135                         descriptor = &rx->ring_cpu_ptr[first_index];
2136
2137                         /* unmap from dma */
2138                         if (buffer_info->dma_ptr) {
2139                                 dma_unmap_single(&rx->adapter->pdev->dev,
2140                                                  buffer_info->dma_ptr,
2141                                                  buffer_info->buffer_length,
2142                                                  DMA_FROM_DEVICE);
2143                                 buffer_info->dma_ptr = 0;
2144                                 buffer_info->buffer_length = 0;
2145                         }
2146                         buffer_info->skb = NULL;
2147                         packet_length = RX_DESC_DATA0_FRAME_LENGTH_GET_
2148                                         (descriptor->data0);
2149                         skb_put(skb, packet_length - 4);
2150                         skb->protocol = eth_type_trans(skb,
2151                                                        rx->adapter->netdev);
2152                         lan743x_rx_init_ring_element(rx, first_index, new_skb);
2153                 } else {
2154                         int index = first_index;
2155
2156                         /* multi buffer packet not supported */
2157                         /* this should not happen since
2158                          * buffers are allocated to be at least jumbo size
2159                          */
2160
2161                         /* clean up buffers */
2162                         if (first_index <= last_index) {
2163                                 while ((index >= first_index) &&
2164                                        (index <= last_index)) {
2165                                         lan743x_rx_reuse_ring_element(rx,
2166                                                                       index);
2167                                         index = lan743x_rx_next_index(rx,
2168                                                                       index);
2169                                 }
2170                         } else {
2171                                 while ((index >= first_index) ||
2172                                        (index <= last_index)) {
2173                                         lan743x_rx_reuse_ring_element(rx,
2174                                                                       index);
2175                                         index = lan743x_rx_next_index(rx,
2176                                                                       index);
2177                                 }
2178                         }
2179                 }
2180
2181 process_extension:
2182                 if (extension_index >= 0) {
2183                         descriptor = &rx->ring_cpu_ptr[extension_index];
2184                         buffer_info = &rx->buffer_info[extension_index];
2185
2186                         ts_sec = descriptor->data1;
2187                         ts_nsec = (descriptor->data2 &
2188                                   RX_DESC_DATA2_TS_NS_MASK_);
2189                         lan743x_rx_reuse_ring_element(rx, extension_index);
2190                         real_last_index = extension_index;
2191                 }
2192
2193                 if (!skb) {
2194                         result = RX_PROCESS_RESULT_PACKET_DROPPED;
2195                         goto move_forward;
2196                 }
2197
2198                 if (extension_index < 0)
2199                         goto pass_packet_to_os;
2200                 hwtstamps = skb_hwtstamps(skb);
2201                 if (hwtstamps)
2202                         hwtstamps->hwtstamp = ktime_set(ts_sec, ts_nsec);
2203
2204 pass_packet_to_os:
2205                 /* pass packet to OS */
2206                 napi_gro_receive(&rx->napi, skb);
2207                 result = RX_PROCESS_RESULT_PACKET_RECEIVED;
2208
2209 move_forward:
2210                 /* push tail and head forward */
2211                 rx->last_tail = real_last_index;
2212                 rx->last_head = lan743x_rx_next_index(rx, real_last_index);
2213         }
2214 done:
2215         return result;
2216 }
2217
2218 static int lan743x_rx_napi_poll(struct napi_struct *napi, int weight)
2219 {
2220         struct lan743x_rx *rx = container_of(napi, struct lan743x_rx, napi);
2221         struct lan743x_adapter *adapter = rx->adapter;
2222         u32 rx_tail_flags = 0;
2223         int count;
2224
2225         if (rx->vector_flags & LAN743X_VECTOR_FLAG_SOURCE_STATUS_W2C) {
2226                 /* clear int status bit before reading packet */
2227                 lan743x_csr_write(adapter, DMAC_INT_STS,
2228                                   DMAC_INT_BIT_RXFRM_(rx->channel_number));
2229         }
2230         count = 0;
2231         while (count < weight) {
2232                 int rx_process_result = lan743x_rx_process_packet(rx);
2233
2234                 if (rx_process_result == RX_PROCESS_RESULT_PACKET_RECEIVED) {
2235                         count++;
2236                 } else if (rx_process_result ==
2237                         RX_PROCESS_RESULT_NOTHING_TO_DO) {
2238                         break;
2239                 } else if (rx_process_result ==
2240                         RX_PROCESS_RESULT_PACKET_DROPPED) {
2241                         continue;
2242                 }
2243         }
2244         rx->frame_count += count;
2245         if (count == weight)
2246                 goto done;
2247
2248         if (!napi_complete_done(napi, count))
2249                 goto done;
2250
2251         if (rx->vector_flags & LAN743X_VECTOR_FLAG_VECTOR_ENABLE_AUTO_SET)
2252                 rx_tail_flags |= RX_TAIL_SET_TOP_INT_VEC_EN_;
2253         if (rx->vector_flags & LAN743X_VECTOR_FLAG_SOURCE_ENABLE_AUTO_SET) {
2254                 rx_tail_flags |= RX_TAIL_SET_TOP_INT_EN_;
2255         } else {
2256                 lan743x_csr_write(adapter, INT_EN_SET,
2257                                   INT_BIT_DMA_RX_(rx->channel_number));
2258         }
2259
2260         /* update RX_TAIL */
2261         lan743x_csr_write(adapter, RX_TAIL(rx->channel_number),
2262                           rx_tail_flags | rx->last_tail);
2263 done:
2264         return count;
2265 }
2266
2267 static void lan743x_rx_ring_cleanup(struct lan743x_rx *rx)
2268 {
2269         if (rx->buffer_info && rx->ring_cpu_ptr) {
2270                 int index;
2271
2272                 for (index = 0; index < rx->ring_size; index++)
2273                         lan743x_rx_release_ring_element(rx, index);
2274         }
2275
2276         if (rx->head_cpu_ptr) {
2277                 dma_free_coherent(&rx->adapter->pdev->dev,
2278                                   sizeof(*rx->head_cpu_ptr), rx->head_cpu_ptr,
2279                                   rx->head_dma_ptr);
2280                 rx->head_cpu_ptr = NULL;
2281                 rx->head_dma_ptr = 0;
2282         }
2283
2284         kfree(rx->buffer_info);
2285         rx->buffer_info = NULL;
2286
2287         if (rx->ring_cpu_ptr) {
2288                 dma_free_coherent(&rx->adapter->pdev->dev,
2289                                   rx->ring_allocation_size, rx->ring_cpu_ptr,
2290                                   rx->ring_dma_ptr);
2291                 rx->ring_allocation_size = 0;
2292                 rx->ring_cpu_ptr = NULL;
2293                 rx->ring_dma_ptr = 0;
2294         }
2295
2296         rx->ring_size = 0;
2297         rx->last_head = 0;
2298 }
2299
2300 static int lan743x_rx_ring_init(struct lan743x_rx *rx)
2301 {
2302         size_t ring_allocation_size = 0;
2303         dma_addr_t dma_ptr = 0;
2304         void *cpu_ptr = NULL;
2305         int ret = -ENOMEM;
2306         int index = 0;
2307
2308         rx->ring_size = LAN743X_RX_RING_SIZE;
2309         if (rx->ring_size <= 1) {
2310                 ret = -EINVAL;
2311                 goto cleanup;
2312         }
2313         if (rx->ring_size & ~RX_CFG_B_RX_RING_LEN_MASK_) {
2314                 ret = -EINVAL;
2315                 goto cleanup;
2316         }
2317         ring_allocation_size = ALIGN(rx->ring_size *
2318                                      sizeof(struct lan743x_rx_descriptor),
2319                                      PAGE_SIZE);
2320         dma_ptr = 0;
2321         cpu_ptr = dma_alloc_coherent(&rx->adapter->pdev->dev,
2322                                      ring_allocation_size, &dma_ptr, GFP_KERNEL);
2323         if (!cpu_ptr) {
2324                 ret = -ENOMEM;
2325                 goto cleanup;
2326         }
2327         rx->ring_allocation_size = ring_allocation_size;
2328         rx->ring_cpu_ptr = (struct lan743x_rx_descriptor *)cpu_ptr;
2329         rx->ring_dma_ptr = dma_ptr;
2330
2331         cpu_ptr = kcalloc(rx->ring_size, sizeof(*rx->buffer_info),
2332                           GFP_KERNEL);
2333         if (!cpu_ptr) {
2334                 ret = -ENOMEM;
2335                 goto cleanup;
2336         }
2337         rx->buffer_info = (struct lan743x_rx_buffer_info *)cpu_ptr;
2338         dma_ptr = 0;
2339         cpu_ptr = dma_alloc_coherent(&rx->adapter->pdev->dev,
2340                                      sizeof(*rx->head_cpu_ptr), &dma_ptr,
2341                                      GFP_KERNEL);
2342         if (!cpu_ptr) {
2343                 ret = -ENOMEM;
2344                 goto cleanup;
2345         }
2346
2347         rx->head_cpu_ptr = cpu_ptr;
2348         rx->head_dma_ptr = dma_ptr;
2349         if (rx->head_dma_ptr & 0x3) {
2350                 ret = -ENOMEM;
2351                 goto cleanup;
2352         }
2353
2354         rx->last_head = 0;
2355         for (index = 0; index < rx->ring_size; index++) {
2356                 struct sk_buff *new_skb = lan743x_rx_allocate_skb(rx);
2357
2358                 ret = lan743x_rx_init_ring_element(rx, index, new_skb);
2359                 if (ret)
2360                         goto cleanup;
2361         }
2362         return 0;
2363
2364 cleanup:
2365         lan743x_rx_ring_cleanup(rx);
2366         return ret;
2367 }
2368
2369 static void lan743x_rx_close(struct lan743x_rx *rx)
2370 {
2371         struct lan743x_adapter *adapter = rx->adapter;
2372
2373         lan743x_csr_write(adapter, FCT_RX_CTL,
2374                           FCT_RX_CTL_DIS_(rx->channel_number));
2375         lan743x_csr_wait_for_bit(adapter, FCT_RX_CTL,
2376                                  FCT_RX_CTL_EN_(rx->channel_number),
2377                                  0, 1000, 20000, 100);
2378
2379         lan743x_csr_write(adapter, DMAC_CMD,
2380                           DMAC_CMD_STOP_R_(rx->channel_number));
2381         lan743x_dmac_rx_wait_till_stopped(adapter, rx->channel_number);
2382
2383         lan743x_csr_write(adapter, DMAC_INT_EN_CLR,
2384                           DMAC_INT_BIT_RXFRM_(rx->channel_number));
2385         lan743x_csr_write(adapter, INT_EN_CLR,
2386                           INT_BIT_DMA_RX_(rx->channel_number));
2387         napi_disable(&rx->napi);
2388
2389         netif_napi_del(&rx->napi);
2390
2391         lan743x_rx_ring_cleanup(rx);
2392 }
2393
2394 static int lan743x_rx_open(struct lan743x_rx *rx)
2395 {
2396         struct lan743x_adapter *adapter = rx->adapter;
2397         u32 data = 0;
2398         int ret;
2399
2400         rx->frame_count = 0;
2401         ret = lan743x_rx_ring_init(rx);
2402         if (ret)
2403                 goto return_error;
2404
2405         netif_napi_add(adapter->netdev,
2406                        &rx->napi, lan743x_rx_napi_poll,
2407                        rx->ring_size - 1);
2408
2409         lan743x_csr_write(adapter, DMAC_CMD,
2410                           DMAC_CMD_RX_SWR_(rx->channel_number));
2411         lan743x_csr_wait_for_bit(adapter, DMAC_CMD,
2412                                  DMAC_CMD_RX_SWR_(rx->channel_number),
2413                                  0, 1000, 20000, 100);
2414
2415         /* set ring base address */
2416         lan743x_csr_write(adapter,
2417                           RX_BASE_ADDRH(rx->channel_number),
2418                           DMA_ADDR_HIGH32(rx->ring_dma_ptr));
2419         lan743x_csr_write(adapter,
2420                           RX_BASE_ADDRL(rx->channel_number),
2421                           DMA_ADDR_LOW32(rx->ring_dma_ptr));
2422
2423         /* set rx write back address */
2424         lan743x_csr_write(adapter,
2425                           RX_HEAD_WRITEBACK_ADDRH(rx->channel_number),
2426                           DMA_ADDR_HIGH32(rx->head_dma_ptr));
2427         lan743x_csr_write(adapter,
2428                           RX_HEAD_WRITEBACK_ADDRL(rx->channel_number),
2429                           DMA_ADDR_LOW32(rx->head_dma_ptr));
2430         data = RX_CFG_A_RX_HP_WB_EN_;
2431         if (!(adapter->csr.flags & LAN743X_CSR_FLAG_IS_A0)) {
2432                 data |= (RX_CFG_A_RX_WB_ON_INT_TMR_ |
2433                         RX_CFG_A_RX_WB_THRES_SET_(0x7) |
2434                         RX_CFG_A_RX_PF_THRES_SET_(16) |
2435                         RX_CFG_A_RX_PF_PRI_THRES_SET_(4));
2436         }
2437
2438         /* set RX_CFG_A */
2439         lan743x_csr_write(adapter,
2440                           RX_CFG_A(rx->channel_number), data);
2441
2442         /* set RX_CFG_B */
2443         data = lan743x_csr_read(adapter, RX_CFG_B(rx->channel_number));
2444         data &= ~RX_CFG_B_RX_PAD_MASK_;
2445         if (!RX_HEAD_PADDING)
2446                 data |= RX_CFG_B_RX_PAD_0_;
2447         else
2448                 data |= RX_CFG_B_RX_PAD_2_;
2449         data &= ~RX_CFG_B_RX_RING_LEN_MASK_;
2450         data |= ((rx->ring_size) & RX_CFG_B_RX_RING_LEN_MASK_);
2451         data |= RX_CFG_B_TS_ALL_RX_;
2452         if (!(adapter->csr.flags & LAN743X_CSR_FLAG_IS_A0))
2453                 data |= RX_CFG_B_RDMABL_512_;
2454
2455         lan743x_csr_write(adapter, RX_CFG_B(rx->channel_number), data);
2456         rx->vector_flags = lan743x_intr_get_vector_flags(adapter,
2457                                                          INT_BIT_DMA_RX_
2458                                                          (rx->channel_number));
2459
2460         /* set RX_CFG_C */
2461         data = 0;
2462         if (rx->vector_flags & LAN743X_VECTOR_FLAG_SOURCE_ENABLE_AUTO_CLEAR)
2463                 data |= RX_CFG_C_RX_TOP_INT_EN_AUTO_CLR_;
2464         if (rx->vector_flags & LAN743X_VECTOR_FLAG_SOURCE_STATUS_AUTO_CLEAR)
2465                 data |= RX_CFG_C_RX_DMA_INT_STS_AUTO_CLR_;
2466         if (rx->vector_flags & LAN743X_VECTOR_FLAG_SOURCE_STATUS_R2C)
2467                 data |= RX_CFG_C_RX_INT_STS_R2C_MODE_MASK_;
2468         if (rx->vector_flags & LAN743X_VECTOR_FLAG_SOURCE_ENABLE_R2C)
2469                 data |= RX_CFG_C_RX_INT_EN_R2C_;
2470         lan743x_csr_write(adapter, RX_CFG_C(rx->channel_number), data);
2471
2472         rx->last_tail = ((u32)(rx->ring_size - 1));
2473         lan743x_csr_write(adapter, RX_TAIL(rx->channel_number),
2474                           rx->last_tail);
2475         rx->last_head = lan743x_csr_read(adapter, RX_HEAD(rx->channel_number));
2476         if (rx->last_head) {
2477                 ret = -EIO;
2478                 goto napi_delete;
2479         }
2480
2481         napi_enable(&rx->napi);
2482
2483         lan743x_csr_write(adapter, INT_EN_SET,
2484                           INT_BIT_DMA_RX_(rx->channel_number));
2485         lan743x_csr_write(adapter, DMAC_INT_STS,
2486                           DMAC_INT_BIT_RXFRM_(rx->channel_number));
2487         lan743x_csr_write(adapter, DMAC_INT_EN_SET,
2488                           DMAC_INT_BIT_RXFRM_(rx->channel_number));
2489         lan743x_csr_write(adapter, DMAC_CMD,
2490                           DMAC_CMD_START_R_(rx->channel_number));
2491
2492         /* initialize fifo */
2493         lan743x_csr_write(adapter, FCT_RX_CTL,
2494                           FCT_RX_CTL_RESET_(rx->channel_number));
2495         lan743x_csr_wait_for_bit(adapter, FCT_RX_CTL,
2496                                  FCT_RX_CTL_RESET_(rx->channel_number),
2497                                  0, 1000, 20000, 100);
2498         lan743x_csr_write(adapter, FCT_FLOW(rx->channel_number),
2499                           FCT_FLOW_CTL_REQ_EN_ |
2500                           FCT_FLOW_CTL_ON_THRESHOLD_SET_(0x2A) |
2501                           FCT_FLOW_CTL_OFF_THRESHOLD_SET_(0xA));
2502
2503         /* enable fifo */
2504         lan743x_csr_write(adapter, FCT_RX_CTL,
2505                           FCT_RX_CTL_EN_(rx->channel_number));
2506         return 0;
2507
2508 napi_delete:
2509         netif_napi_del(&rx->napi);
2510         lan743x_rx_ring_cleanup(rx);
2511
2512 return_error:
2513         return ret;
2514 }
2515
2516 static int lan743x_netdev_close(struct net_device *netdev)
2517 {
2518         struct lan743x_adapter *adapter = netdev_priv(netdev);
2519         int index;
2520
2521         lan743x_tx_close(&adapter->tx[0]);
2522
2523         for (index = 0; index < LAN743X_USED_RX_CHANNELS; index++)
2524                 lan743x_rx_close(&adapter->rx[index]);
2525
2526         lan743x_ptp_close(adapter);
2527
2528         lan743x_phy_close(adapter);
2529
2530         lan743x_mac_close(adapter);
2531
2532         lan743x_intr_close(adapter);
2533
2534         return 0;
2535 }
2536
2537 static int lan743x_netdev_open(struct net_device *netdev)
2538 {
2539         struct lan743x_adapter *adapter = netdev_priv(netdev);
2540         int index;
2541         int ret;
2542
2543         ret = lan743x_intr_open(adapter);
2544         if (ret)
2545                 goto return_error;
2546
2547         ret = lan743x_mac_open(adapter);
2548         if (ret)
2549                 goto close_intr;
2550
2551         ret = lan743x_phy_open(adapter);
2552         if (ret)
2553                 goto close_mac;
2554
2555         ret = lan743x_ptp_open(adapter);
2556         if (ret)
2557                 goto close_phy;
2558
2559         lan743x_rfe_open(adapter);
2560
2561         for (index = 0; index < LAN743X_USED_RX_CHANNELS; index++) {
2562                 ret = lan743x_rx_open(&adapter->rx[index]);
2563                 if (ret)
2564                         goto close_rx;
2565         }
2566
2567         ret = lan743x_tx_open(&adapter->tx[0]);
2568         if (ret)
2569                 goto close_rx;
2570
2571         return 0;
2572
2573 close_rx:
2574         for (index = 0; index < LAN743X_USED_RX_CHANNELS; index++) {
2575                 if (adapter->rx[index].ring_cpu_ptr)
2576                         lan743x_rx_close(&adapter->rx[index]);
2577         }
2578         lan743x_ptp_close(adapter);
2579
2580 close_phy:
2581         lan743x_phy_close(adapter);
2582
2583 close_mac:
2584         lan743x_mac_close(adapter);
2585
2586 close_intr:
2587         lan743x_intr_close(adapter);
2588
2589 return_error:
2590         netif_warn(adapter, ifup, adapter->netdev,
2591                    "Error opening LAN743x\n");
2592         return ret;
2593 }
2594
2595 static netdev_tx_t lan743x_netdev_xmit_frame(struct sk_buff *skb,
2596                                              struct net_device *netdev)
2597 {
2598         struct lan743x_adapter *adapter = netdev_priv(netdev);
2599
2600         return lan743x_tx_xmit_frame(&adapter->tx[0], skb);
2601 }
2602
2603 static int lan743x_netdev_ioctl(struct net_device *netdev,
2604                                 struct ifreq *ifr, int cmd)
2605 {
2606         if (!netif_running(netdev))
2607                 return -EINVAL;
2608         if (cmd == SIOCSHWTSTAMP)
2609                 return lan743x_ptp_ioctl(netdev, ifr, cmd);
2610         return phy_mii_ioctl(netdev->phydev, ifr, cmd);
2611 }
2612
2613 static void lan743x_netdev_set_multicast(struct net_device *netdev)
2614 {
2615         struct lan743x_adapter *adapter = netdev_priv(netdev);
2616
2617         lan743x_rfe_set_multicast(adapter);
2618 }
2619
2620 static int lan743x_netdev_change_mtu(struct net_device *netdev, int new_mtu)
2621 {
2622         struct lan743x_adapter *adapter = netdev_priv(netdev);
2623         int ret = 0;
2624
2625         ret = lan743x_mac_set_mtu(adapter, new_mtu);
2626         if (!ret)
2627                 netdev->mtu = new_mtu;
2628         return ret;
2629 }
2630
2631 static void lan743x_netdev_get_stats64(struct net_device *netdev,
2632                                        struct rtnl_link_stats64 *stats)
2633 {
2634         struct lan743x_adapter *adapter = netdev_priv(netdev);
2635
2636         stats->rx_packets = lan743x_csr_read(adapter, STAT_RX_TOTAL_FRAMES);
2637         stats->tx_packets = lan743x_csr_read(adapter, STAT_TX_TOTAL_FRAMES);
2638         stats->rx_bytes = lan743x_csr_read(adapter,
2639                                            STAT_RX_UNICAST_BYTE_COUNT) +
2640                           lan743x_csr_read(adapter,
2641                                            STAT_RX_BROADCAST_BYTE_COUNT) +
2642                           lan743x_csr_read(adapter,
2643                                            STAT_RX_MULTICAST_BYTE_COUNT);
2644         stats->tx_bytes = lan743x_csr_read(adapter,
2645                                            STAT_TX_UNICAST_BYTE_COUNT) +
2646                           lan743x_csr_read(adapter,
2647                                            STAT_TX_BROADCAST_BYTE_COUNT) +
2648                           lan743x_csr_read(adapter,
2649                                            STAT_TX_MULTICAST_BYTE_COUNT);
2650         stats->rx_errors = lan743x_csr_read(adapter, STAT_RX_FCS_ERRORS) +
2651                            lan743x_csr_read(adapter,
2652                                             STAT_RX_ALIGNMENT_ERRORS) +
2653                            lan743x_csr_read(adapter, STAT_RX_JABBER_ERRORS) +
2654                            lan743x_csr_read(adapter,
2655                                             STAT_RX_UNDERSIZE_FRAME_ERRORS) +
2656                            lan743x_csr_read(adapter,
2657                                             STAT_RX_OVERSIZE_FRAME_ERRORS);
2658         stats->tx_errors = lan743x_csr_read(adapter, STAT_TX_FCS_ERRORS) +
2659                            lan743x_csr_read(adapter,
2660                                             STAT_TX_EXCESS_DEFERRAL_ERRORS) +
2661                            lan743x_csr_read(adapter, STAT_TX_CARRIER_ERRORS);
2662         stats->rx_dropped = lan743x_csr_read(adapter,
2663                                              STAT_RX_DROPPED_FRAMES);
2664         stats->tx_dropped = lan743x_csr_read(adapter,
2665                                              STAT_TX_EXCESSIVE_COLLISION);
2666         stats->multicast = lan743x_csr_read(adapter,
2667                                             STAT_RX_MULTICAST_FRAMES) +
2668                            lan743x_csr_read(adapter,
2669                                             STAT_TX_MULTICAST_FRAMES);
2670         stats->collisions = lan743x_csr_read(adapter,
2671                                              STAT_TX_SINGLE_COLLISIONS) +
2672                             lan743x_csr_read(adapter,
2673                                              STAT_TX_MULTIPLE_COLLISIONS) +
2674                             lan743x_csr_read(adapter,
2675                                              STAT_TX_LATE_COLLISIONS);
2676 }
2677
2678 static int lan743x_netdev_set_mac_address(struct net_device *netdev,
2679                                           void *addr)
2680 {
2681         struct lan743x_adapter *adapter = netdev_priv(netdev);
2682         struct sockaddr *sock_addr = addr;
2683         int ret;
2684
2685         ret = eth_prepare_mac_addr_change(netdev, sock_addr);
2686         if (ret)
2687                 return ret;
2688         ether_addr_copy(netdev->dev_addr, sock_addr->sa_data);
2689         lan743x_mac_set_address(adapter, sock_addr->sa_data);
2690         lan743x_rfe_update_mac_address(adapter);
2691         return 0;
2692 }
2693
2694 static const struct net_device_ops lan743x_netdev_ops = {
2695         .ndo_open               = lan743x_netdev_open,
2696         .ndo_stop               = lan743x_netdev_close,
2697         .ndo_start_xmit         = lan743x_netdev_xmit_frame,
2698         .ndo_do_ioctl           = lan743x_netdev_ioctl,
2699         .ndo_set_rx_mode        = lan743x_netdev_set_multicast,
2700         .ndo_change_mtu         = lan743x_netdev_change_mtu,
2701         .ndo_get_stats64        = lan743x_netdev_get_stats64,
2702         .ndo_set_mac_address    = lan743x_netdev_set_mac_address,
2703 };
2704
2705 static void lan743x_hardware_cleanup(struct lan743x_adapter *adapter)
2706 {
2707         lan743x_csr_write(adapter, INT_EN_CLR, 0xFFFFFFFF);
2708 }
2709
2710 static void lan743x_mdiobus_cleanup(struct lan743x_adapter *adapter)
2711 {
2712         mdiobus_unregister(adapter->mdiobus);
2713 }
2714
2715 static void lan743x_full_cleanup(struct lan743x_adapter *adapter)
2716 {
2717         unregister_netdev(adapter->netdev);
2718
2719         lan743x_mdiobus_cleanup(adapter);
2720         lan743x_hardware_cleanup(adapter);
2721         lan743x_pci_cleanup(adapter);
2722 }
2723
2724 static int lan743x_hardware_init(struct lan743x_adapter *adapter,
2725                                  struct pci_dev *pdev)
2726 {
2727         struct lan743x_tx *tx;
2728         int index;
2729         int ret;
2730
2731         adapter->intr.irq = adapter->pdev->irq;
2732         lan743x_csr_write(adapter, INT_EN_CLR, 0xFFFFFFFF);
2733
2734         ret = lan743x_gpio_init(adapter);
2735         if (ret)
2736                 return ret;
2737
2738         ret = lan743x_mac_init(adapter);
2739         if (ret)
2740                 return ret;
2741
2742         ret = lan743x_phy_init(adapter);
2743         if (ret)
2744                 return ret;
2745
2746         ret = lan743x_ptp_init(adapter);
2747         if (ret)
2748                 return ret;
2749
2750         lan743x_rfe_update_mac_address(adapter);
2751
2752         ret = lan743x_dmac_init(adapter);
2753         if (ret)
2754                 return ret;
2755
2756         for (index = 0; index < LAN743X_USED_RX_CHANNELS; index++) {
2757                 adapter->rx[index].adapter = adapter;
2758                 adapter->rx[index].channel_number = index;
2759         }
2760
2761         tx = &adapter->tx[0];
2762         tx->adapter = adapter;
2763         tx->channel_number = 0;
2764         spin_lock_init(&tx->ring_lock);
2765         return 0;
2766 }
2767
2768 static int lan743x_mdiobus_init(struct lan743x_adapter *adapter)
2769 {
2770         int ret;
2771
2772         adapter->mdiobus = devm_mdiobus_alloc(&adapter->pdev->dev);
2773         if (!(adapter->mdiobus)) {
2774                 ret = -ENOMEM;
2775                 goto return_error;
2776         }
2777
2778         adapter->mdiobus->priv = (void *)adapter;
2779         adapter->mdiobus->read = lan743x_mdiobus_read;
2780         adapter->mdiobus->write = lan743x_mdiobus_write;
2781         adapter->mdiobus->name = "lan743x-mdiobus";
2782         snprintf(adapter->mdiobus->id, MII_BUS_ID_SIZE,
2783                  "pci-%s", pci_name(adapter->pdev));
2784
2785         if ((adapter->csr.id_rev & ID_REV_ID_MASK_) == ID_REV_ID_LAN7430_)
2786                 /* LAN7430 uses internal phy at address 1 */
2787                 adapter->mdiobus->phy_mask = ~(u32)BIT(1);
2788
2789         /* register mdiobus */
2790         ret = mdiobus_register(adapter->mdiobus);
2791         if (ret < 0)
2792                 goto return_error;
2793         return 0;
2794
2795 return_error:
2796         return ret;
2797 }
2798
2799 /* lan743x_pcidev_probe - Device Initialization Routine
2800  * @pdev: PCI device information struct
2801  * @id: entry in lan743x_pci_tbl
2802  *
2803  * Returns 0 on success, negative on failure
2804  *
2805  * initializes an adapter identified by a pci_dev structure.
2806  * The OS initialization, configuring of the adapter private structure,
2807  * and a hardware reset occur.
2808  **/
2809 static int lan743x_pcidev_probe(struct pci_dev *pdev,
2810                                 const struct pci_device_id *id)
2811 {
2812         struct lan743x_adapter *adapter = NULL;
2813         struct net_device *netdev = NULL;
2814         const void *mac_addr;
2815         int ret = -ENODEV;
2816
2817         netdev = devm_alloc_etherdev(&pdev->dev,
2818                                      sizeof(struct lan743x_adapter));
2819         if (!netdev)
2820                 goto return_error;
2821
2822         SET_NETDEV_DEV(netdev, &pdev->dev);
2823         pci_set_drvdata(pdev, netdev);
2824         adapter = netdev_priv(netdev);
2825         adapter->netdev = netdev;
2826         adapter->msg_enable = NETIF_MSG_DRV | NETIF_MSG_PROBE |
2827                               NETIF_MSG_LINK | NETIF_MSG_IFUP |
2828                               NETIF_MSG_IFDOWN | NETIF_MSG_TX_QUEUED;
2829         netdev->max_mtu = LAN743X_MAX_FRAME_SIZE;
2830
2831         mac_addr = of_get_mac_address(pdev->dev.of_node);
2832         if (!IS_ERR(mac_addr))
2833                 ether_addr_copy(adapter->mac_address, mac_addr);
2834
2835         ret = lan743x_pci_init(adapter, pdev);
2836         if (ret)
2837                 goto return_error;
2838
2839         ret = lan743x_csr_init(adapter);
2840         if (ret)
2841                 goto cleanup_pci;
2842
2843         ret = lan743x_hardware_init(adapter, pdev);
2844         if (ret)
2845                 goto cleanup_pci;
2846
2847         ret = lan743x_mdiobus_init(adapter);
2848         if (ret)
2849                 goto cleanup_hardware;
2850
2851         adapter->netdev->netdev_ops = &lan743x_netdev_ops;
2852         adapter->netdev->ethtool_ops = &lan743x_ethtool_ops;
2853         adapter->netdev->features = NETIF_F_SG | NETIF_F_TSO | NETIF_F_HW_CSUM;
2854         adapter->netdev->hw_features = adapter->netdev->features;
2855
2856         /* carrier off reporting is important to ethtool even BEFORE open */
2857         netif_carrier_off(netdev);
2858
2859         ret = register_netdev(adapter->netdev);
2860         if (ret < 0)
2861                 goto cleanup_mdiobus;
2862         return 0;
2863
2864 cleanup_mdiobus:
2865         lan743x_mdiobus_cleanup(adapter);
2866
2867 cleanup_hardware:
2868         lan743x_hardware_cleanup(adapter);
2869
2870 cleanup_pci:
2871         lan743x_pci_cleanup(adapter);
2872
2873 return_error:
2874         pr_warn("Initialization failed\n");
2875         return ret;
2876 }
2877
2878 /**
2879  * lan743x_pcidev_remove - Device Removal Routine
2880  * @pdev: PCI device information struct
2881  *
2882  * this is called by the PCI subsystem to alert the driver
2883  * that it should release a PCI device.  This could be caused by a
2884  * Hot-Plug event, or because the driver is going to be removed from
2885  * memory.
2886  **/
2887 static void lan743x_pcidev_remove(struct pci_dev *pdev)
2888 {
2889         struct net_device *netdev = pci_get_drvdata(pdev);
2890         struct lan743x_adapter *adapter = netdev_priv(netdev);
2891
2892         lan743x_full_cleanup(adapter);
2893 }
2894
2895 static void lan743x_pcidev_shutdown(struct pci_dev *pdev)
2896 {
2897         struct net_device *netdev = pci_get_drvdata(pdev);
2898         struct lan743x_adapter *adapter = netdev_priv(netdev);
2899
2900         rtnl_lock();
2901         netif_device_detach(netdev);
2902
2903         /* close netdev when netdev is at running state.
2904          * For instance, it is true when system goes to sleep by pm-suspend
2905          * However, it is false when system goes to sleep by suspend GUI menu
2906          */
2907         if (netif_running(netdev))
2908                 lan743x_netdev_close(netdev);
2909         rtnl_unlock();
2910
2911 #ifdef CONFIG_PM
2912         pci_save_state(pdev);
2913 #endif
2914
2915         /* clean up lan743x portion */
2916         lan743x_hardware_cleanup(adapter);
2917 }
2918
2919 #ifdef CONFIG_PM_SLEEP
2920 static u16 lan743x_pm_wakeframe_crc16(const u8 *buf, int len)
2921 {
2922         return bitrev16(crc16(0xFFFF, buf, len));
2923 }
2924
2925 static void lan743x_pm_set_wol(struct lan743x_adapter *adapter)
2926 {
2927         const u8 ipv4_multicast[3] = { 0x01, 0x00, 0x5E };
2928         const u8 ipv6_multicast[3] = { 0x33, 0x33 };
2929         const u8 arp_type[2] = { 0x08, 0x06 };
2930         int mask_index;
2931         u32 pmtctl;
2932         u32 wucsr;
2933         u32 macrx;
2934         u16 crc;
2935
2936         for (mask_index = 0; mask_index < MAC_NUM_OF_WUF_CFG; mask_index++)
2937                 lan743x_csr_write(adapter, MAC_WUF_CFG(mask_index), 0);
2938
2939         /* clear wake settings */
2940         pmtctl = lan743x_csr_read(adapter, PMT_CTL);
2941         pmtctl |= PMT_CTL_WUPS_MASK_;
2942         pmtctl &= ~(PMT_CTL_GPIO_WAKEUP_EN_ | PMT_CTL_EEE_WAKEUP_EN_ |
2943                 PMT_CTL_WOL_EN_ | PMT_CTL_MAC_D3_RX_CLK_OVR_ |
2944                 PMT_CTL_RX_FCT_RFE_D3_CLK_OVR_ | PMT_CTL_ETH_PHY_WAKE_EN_);
2945
2946         macrx = lan743x_csr_read(adapter, MAC_RX);
2947
2948         wucsr = 0;
2949         mask_index = 0;
2950
2951         pmtctl |= PMT_CTL_ETH_PHY_D3_COLD_OVR_ | PMT_CTL_ETH_PHY_D3_OVR_;
2952
2953         if (adapter->wolopts & WAKE_PHY) {
2954                 pmtctl |= PMT_CTL_ETH_PHY_EDPD_PLL_CTL_;
2955                 pmtctl |= PMT_CTL_ETH_PHY_WAKE_EN_;
2956         }
2957         if (adapter->wolopts & WAKE_MAGIC) {
2958                 wucsr |= MAC_WUCSR_MPEN_;
2959                 macrx |= MAC_RX_RXEN_;
2960                 pmtctl |= PMT_CTL_WOL_EN_ | PMT_CTL_MAC_D3_RX_CLK_OVR_;
2961         }
2962         if (adapter->wolopts & WAKE_UCAST) {
2963                 wucsr |= MAC_WUCSR_RFE_WAKE_EN_ | MAC_WUCSR_PFDA_EN_;
2964                 macrx |= MAC_RX_RXEN_;
2965                 pmtctl |= PMT_CTL_WOL_EN_ | PMT_CTL_MAC_D3_RX_CLK_OVR_;
2966                 pmtctl |= PMT_CTL_RX_FCT_RFE_D3_CLK_OVR_;
2967         }
2968         if (adapter->wolopts & WAKE_BCAST) {
2969                 wucsr |= MAC_WUCSR_RFE_WAKE_EN_ | MAC_WUCSR_BCST_EN_;
2970                 macrx |= MAC_RX_RXEN_;
2971                 pmtctl |= PMT_CTL_WOL_EN_ | PMT_CTL_MAC_D3_RX_CLK_OVR_;
2972                 pmtctl |= PMT_CTL_RX_FCT_RFE_D3_CLK_OVR_;
2973         }
2974         if (adapter->wolopts & WAKE_MCAST) {
2975                 /* IPv4 multicast */
2976                 crc = lan743x_pm_wakeframe_crc16(ipv4_multicast, 3);
2977                 lan743x_csr_write(adapter, MAC_WUF_CFG(mask_index),
2978                                   MAC_WUF_CFG_EN_ | MAC_WUF_CFG_TYPE_MCAST_ |
2979                                   (0 << MAC_WUF_CFG_OFFSET_SHIFT_) |
2980                                   (crc & MAC_WUF_CFG_CRC16_MASK_));
2981                 lan743x_csr_write(adapter, MAC_WUF_MASK0(mask_index), 7);
2982                 lan743x_csr_write(adapter, MAC_WUF_MASK1(mask_index), 0);
2983                 lan743x_csr_write(adapter, MAC_WUF_MASK2(mask_index), 0);
2984                 lan743x_csr_write(adapter, MAC_WUF_MASK3(mask_index), 0);
2985                 mask_index++;
2986
2987                 /* IPv6 multicast */
2988                 crc = lan743x_pm_wakeframe_crc16(ipv6_multicast, 2);
2989                 lan743x_csr_write(adapter, MAC_WUF_CFG(mask_index),
2990                                   MAC_WUF_CFG_EN_ | MAC_WUF_CFG_TYPE_MCAST_ |
2991                                   (0 << MAC_WUF_CFG_OFFSET_SHIFT_) |
2992                                   (crc & MAC_WUF_CFG_CRC16_MASK_));
2993                 lan743x_csr_write(adapter, MAC_WUF_MASK0(mask_index), 3);
2994                 lan743x_csr_write(adapter, MAC_WUF_MASK1(mask_index), 0);
2995                 lan743x_csr_write(adapter, MAC_WUF_MASK2(mask_index), 0);
2996                 lan743x_csr_write(adapter, MAC_WUF_MASK3(mask_index), 0);
2997                 mask_index++;
2998
2999                 wucsr |= MAC_WUCSR_RFE_WAKE_EN_ | MAC_WUCSR_WAKE_EN_;
3000                 macrx |= MAC_RX_RXEN_;
3001                 pmtctl |= PMT_CTL_WOL_EN_ | PMT_CTL_MAC_D3_RX_CLK_OVR_;
3002                 pmtctl |= PMT_CTL_RX_FCT_RFE_D3_CLK_OVR_;
3003         }
3004         if (adapter->wolopts & WAKE_ARP) {
3005                 /* set MAC_WUF_CFG & WUF_MASK
3006                  * for packettype (offset 12,13) = ARP (0x0806)
3007                  */
3008                 crc = lan743x_pm_wakeframe_crc16(arp_type, 2);
3009                 lan743x_csr_write(adapter, MAC_WUF_CFG(mask_index),
3010                                   MAC_WUF_CFG_EN_ | MAC_WUF_CFG_TYPE_ALL_ |
3011                                   (0 << MAC_WUF_CFG_OFFSET_SHIFT_) |
3012                                   (crc & MAC_WUF_CFG_CRC16_MASK_));
3013                 lan743x_csr_write(adapter, MAC_WUF_MASK0(mask_index), 0x3000);
3014                 lan743x_csr_write(adapter, MAC_WUF_MASK1(mask_index), 0);
3015                 lan743x_csr_write(adapter, MAC_WUF_MASK2(mask_index), 0);
3016                 lan743x_csr_write(adapter, MAC_WUF_MASK3(mask_index), 0);
3017                 mask_index++;
3018
3019                 wucsr |= MAC_WUCSR_RFE_WAKE_EN_ | MAC_WUCSR_WAKE_EN_;
3020                 macrx |= MAC_RX_RXEN_;
3021                 pmtctl |= PMT_CTL_WOL_EN_ | PMT_CTL_MAC_D3_RX_CLK_OVR_;
3022                 pmtctl |= PMT_CTL_RX_FCT_RFE_D3_CLK_OVR_;
3023         }
3024
3025         lan743x_csr_write(adapter, MAC_WUCSR, wucsr);
3026         lan743x_csr_write(adapter, PMT_CTL, pmtctl);
3027         lan743x_csr_write(adapter, MAC_RX, macrx);
3028 }
3029
3030 static int lan743x_pm_suspend(struct device *dev)
3031 {
3032         struct pci_dev *pdev = to_pci_dev(dev);
3033         struct net_device *netdev = pci_get_drvdata(pdev);
3034         struct lan743x_adapter *adapter = netdev_priv(netdev);
3035
3036         lan743x_pcidev_shutdown(pdev);
3037
3038         /* clear all wakes */
3039         lan743x_csr_write(adapter, MAC_WUCSR, 0);
3040         lan743x_csr_write(adapter, MAC_WUCSR2, 0);
3041         lan743x_csr_write(adapter, MAC_WK_SRC, 0xFFFFFFFF);
3042
3043         if (adapter->wolopts)
3044                 lan743x_pm_set_wol(adapter);
3045
3046         /* Host sets PME_En, put D3hot */
3047         return pci_prepare_to_sleep(pdev);;
3048 }
3049
3050 static int lan743x_pm_resume(struct device *dev)
3051 {
3052         struct pci_dev *pdev = to_pci_dev(dev);
3053         struct net_device *netdev = pci_get_drvdata(pdev);
3054         struct lan743x_adapter *adapter = netdev_priv(netdev);
3055         int ret;
3056
3057         pci_set_power_state(pdev, PCI_D0);
3058         pci_restore_state(pdev);
3059         pci_save_state(pdev);
3060
3061         ret = lan743x_hardware_init(adapter, pdev);
3062         if (ret) {
3063                 netif_err(adapter, probe, adapter->netdev,
3064                           "lan743x_hardware_init returned %d\n", ret);
3065         }
3066
3067         /* open netdev when netdev is at running state while resume.
3068          * For instance, it is true when system wakesup after pm-suspend
3069          * However, it is false when system wakes up after suspend GUI menu
3070          */
3071         if (netif_running(netdev))
3072                 lan743x_netdev_open(netdev);
3073
3074         netif_device_attach(netdev);
3075
3076         return 0;
3077 }
3078
3079 static const struct dev_pm_ops lan743x_pm_ops = {
3080         SET_SYSTEM_SLEEP_PM_OPS(lan743x_pm_suspend, lan743x_pm_resume)
3081 };
3082 #endif /* CONFIG_PM_SLEEP */
3083
3084 static const struct pci_device_id lan743x_pcidev_tbl[] = {
3085         { PCI_DEVICE(PCI_VENDOR_ID_SMSC, PCI_DEVICE_ID_SMSC_LAN7430) },
3086         { PCI_DEVICE(PCI_VENDOR_ID_SMSC, PCI_DEVICE_ID_SMSC_LAN7431) },
3087         { 0, }
3088 };
3089
3090 MODULE_DEVICE_TABLE(pci, lan743x_pcidev_tbl);
3091
3092 static struct pci_driver lan743x_pcidev_driver = {
3093         .name     = DRIVER_NAME,
3094         .id_table = lan743x_pcidev_tbl,
3095         .probe    = lan743x_pcidev_probe,
3096         .remove   = lan743x_pcidev_remove,
3097 #ifdef CONFIG_PM_SLEEP
3098         .driver.pm = &lan743x_pm_ops,
3099 #endif
3100         .shutdown = lan743x_pcidev_shutdown,
3101 };
3102
3103 module_pci_driver(lan743x_pcidev_driver);
3104
3105 MODULE_AUTHOR(DRIVER_AUTHOR);
3106 MODULE_DESCRIPTION(DRIVER_DESC);
3107 MODULE_LICENSE("GPL");