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