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