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