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