1 /* SPDX-License-Identifier: GPL-2.0+ */
2 /* Copyright (C) 2018 Microchip Technology Inc. */
4 #include <linux/module.h>
6 #include <linux/netdevice.h>
7 #include <linux/etherdevice.h>
8 #include <linux/crc32.h>
9 #include <linux/microchipphy.h>
10 #include <linux/net_tstamp.h>
11 #include <linux/of_mdio.h>
12 #include <linux/of_net.h>
13 #include <linux/phy.h>
14 #include <linux/phy_fixed.h>
15 #include <linux/rtnetlink.h>
16 #include <linux/iopoll.h>
17 #include <linux/crc16.h>
18 #include <linux/phylink.h>
19 #include "lan743x_main.h"
20 #include "lan743x_ethtool.h"
22 #define MMD_ACCESS_ADDRESS 0
23 #define MMD_ACCESS_WRITE 1
24 #define MMD_ACCESS_READ 2
25 #define MMD_ACCESS_READ_INC 3
26 #define PCS_POWER_STATE_DOWN 0x6
27 #define PCS_POWER_STATE_UP 0x4
29 #define RFE_RD_FIFO_TH_3_DWORDS 0x3
31 static void pci11x1x_strap_get_status(struct lan743x_adapter
*adapter
)
39 /* Timeout = 100 (i.e. 1 sec (10 msce * 100)) */
40 ret
= lan743x_hs_syslock_acquire(adapter
, 100);
42 netif_err(adapter
, drv
, adapter
->netdev
,
43 "Sys Lock acquire failed ret:%d\n", ret
);
47 cfg_load
= lan743x_csr_read(adapter
, ETH_SYS_CONFIG_LOAD_STARTED_REG
);
48 lan743x_hs_syslock_release(adapter
);
49 hw_cfg
= lan743x_csr_read(adapter
, HW_CFG
);
51 if (cfg_load
& GEN_SYS_LOAD_STARTED_REG_ETH_
||
52 hw_cfg
& HW_CFG_RST_PROTECT_
) {
53 strap
= lan743x_csr_read(adapter
, STRAP_READ
);
54 if (strap
& STRAP_READ_SGMII_EN_
)
55 adapter
->is_sgmii_en
= true;
57 adapter
->is_sgmii_en
= false;
59 chip_rev
= lan743x_csr_read(adapter
, FPGA_REV
);
61 if (chip_rev
& FPGA_SGMII_OP
)
62 adapter
->is_sgmii_en
= true;
64 adapter
->is_sgmii_en
= false;
66 adapter
->is_sgmii_en
= false;
69 netif_dbg(adapter
, drv
, adapter
->netdev
,
70 "SGMII I/F %sable\n", adapter
->is_sgmii_en
? "En" : "Dis");
73 static bool is_pci11x1x_chip(struct lan743x_adapter
*adapter
)
75 struct lan743x_csr
*csr
= &adapter
->csr
;
76 u32 id_rev
= csr
->id_rev
;
78 if (((id_rev
& 0xFFFF0000) == ID_REV_ID_A011_
) ||
79 ((id_rev
& 0xFFFF0000) == ID_REV_ID_A041_
)) {
85 static void lan743x_pci_cleanup(struct lan743x_adapter
*adapter
)
87 pci_release_selected_regions(adapter
->pdev
,
88 pci_select_bars(adapter
->pdev
,
90 pci_disable_device(adapter
->pdev
);
93 static int lan743x_pci_init(struct lan743x_adapter
*adapter
,
96 unsigned long bars
= 0;
100 ret
= pci_enable_device_mem(pdev
);
104 netif_info(adapter
, probe
, adapter
->netdev
,
105 "PCI: Vendor ID = 0x%04X, Device ID = 0x%04X\n",
106 pdev
->vendor
, pdev
->device
);
107 bars
= pci_select_bars(pdev
, IORESOURCE_MEM
);
108 if (!test_bit(0, &bars
))
111 ret
= pci_request_selected_regions(pdev
, bars
, DRIVER_NAME
);
115 pci_set_master(pdev
);
119 pci_disable_device(adapter
->pdev
);
125 u32
lan743x_csr_read(struct lan743x_adapter
*adapter
, int offset
)
127 return ioread32(&adapter
->csr
.csr_address
[offset
]);
130 void lan743x_csr_write(struct lan743x_adapter
*adapter
, int offset
,
133 iowrite32(data
, &adapter
->csr
.csr_address
[offset
]);
136 #define LAN743X_CSR_READ_OP(offset) lan743x_csr_read(adapter, offset)
138 static int lan743x_csr_light_reset(struct lan743x_adapter
*adapter
)
142 data
= lan743x_csr_read(adapter
, HW_CFG
);
143 data
|= HW_CFG_LRST_
;
144 lan743x_csr_write(adapter
, HW_CFG
, data
);
146 return readx_poll_timeout(LAN743X_CSR_READ_OP
, HW_CFG
, data
,
147 !(data
& HW_CFG_LRST_
), 100000, 10000000);
150 static int lan743x_csr_wait_for_bit_atomic(struct lan743x_adapter
*adapter
,
151 int offset
, u32 bit_mask
,
152 int target_value
, int udelay_min
,
153 int udelay_max
, int count
)
157 return readx_poll_timeout_atomic(LAN743X_CSR_READ_OP
, offset
, data
,
158 target_value
== !!(data
& bit_mask
),
159 udelay_max
, udelay_min
* count
);
162 static int lan743x_csr_wait_for_bit(struct lan743x_adapter
*adapter
,
163 int offset
, u32 bit_mask
,
164 int target_value
, int usleep_min
,
165 int usleep_max
, int count
)
169 return readx_poll_timeout(LAN743X_CSR_READ_OP
, offset
, data
,
170 target_value
== !!(data
& bit_mask
),
171 usleep_max
, usleep_min
* count
);
174 static int lan743x_csr_init(struct lan743x_adapter
*adapter
)
176 struct lan743x_csr
*csr
= &adapter
->csr
;
177 resource_size_t bar_start
, bar_length
;
179 bar_start
= pci_resource_start(adapter
->pdev
, 0);
180 bar_length
= pci_resource_len(adapter
->pdev
, 0);
181 csr
->csr_address
= devm_ioremap(&adapter
->pdev
->dev
,
182 bar_start
, bar_length
);
183 if (!csr
->csr_address
)
186 csr
->id_rev
= lan743x_csr_read(adapter
, ID_REV
);
187 csr
->fpga_rev
= lan743x_csr_read(adapter
, FPGA_REV
);
188 netif_info(adapter
, probe
, adapter
->netdev
,
189 "ID_REV = 0x%08X, FPGA_REV = %d.%d\n",
190 csr
->id_rev
, FPGA_REV_GET_MAJOR_(csr
->fpga_rev
),
191 FPGA_REV_GET_MINOR_(csr
->fpga_rev
));
192 if (!ID_REV_IS_VALID_CHIP_ID_(csr
->id_rev
))
195 csr
->flags
= LAN743X_CSR_FLAG_SUPPORTS_INTR_AUTO_SET_CLR
;
196 switch (csr
->id_rev
& ID_REV_CHIP_REV_MASK_
) {
197 case ID_REV_CHIP_REV_A0_
:
198 csr
->flags
|= LAN743X_CSR_FLAG_IS_A0
;
199 csr
->flags
&= ~LAN743X_CSR_FLAG_SUPPORTS_INTR_AUTO_SET_CLR
;
201 case ID_REV_CHIP_REV_B0_
:
202 csr
->flags
|= LAN743X_CSR_FLAG_IS_B0
;
206 return lan743x_csr_light_reset(adapter
);
209 static void lan743x_intr_software_isr(struct lan743x_adapter
*adapter
)
211 struct lan743x_intr
*intr
= &adapter
->intr
;
213 /* disable the interrupt to prevent repeated re-triggering */
214 lan743x_csr_write(adapter
, INT_EN_CLR
, INT_BIT_SW_GP_
);
215 intr
->software_isr_flag
= true;
216 wake_up(&intr
->software_isr_wq
);
219 static void lan743x_tx_isr(void *context
, u32 int_sts
, u32 flags
)
221 struct lan743x_tx
*tx
= context
;
222 struct lan743x_adapter
*adapter
= tx
->adapter
;
223 bool enable_flag
= true;
225 lan743x_csr_read(adapter
, INT_EN_SET
);
226 if (flags
& LAN743X_VECTOR_FLAG_SOURCE_ENABLE_CLEAR
) {
227 lan743x_csr_write(adapter
, INT_EN_CLR
,
228 INT_BIT_DMA_TX_(tx
->channel_number
));
231 if (int_sts
& INT_BIT_DMA_TX_(tx
->channel_number
)) {
232 u32 ioc_bit
= DMAC_INT_BIT_TX_IOC_(tx
->channel_number
);
236 if (flags
& LAN743X_VECTOR_FLAG_SOURCE_STATUS_READ
)
237 dmac_int_sts
= lan743x_csr_read(adapter
, DMAC_INT_STS
);
239 dmac_int_sts
= ioc_bit
;
240 if (flags
& LAN743X_VECTOR_FLAG_SOURCE_ENABLE_CHECK
)
241 dmac_int_en
= lan743x_csr_read(adapter
,
244 dmac_int_en
= ioc_bit
;
246 dmac_int_en
&= ioc_bit
;
247 dmac_int_sts
&= dmac_int_en
;
248 if (dmac_int_sts
& ioc_bit
) {
249 napi_schedule(&tx
->napi
);
250 enable_flag
= false;/* poll func will enable later */
256 lan743x_csr_write(adapter
, INT_EN_SET
,
257 INT_BIT_DMA_TX_(tx
->channel_number
));
260 static void lan743x_rx_isr(void *context
, u32 int_sts
, u32 flags
)
262 struct lan743x_rx
*rx
= context
;
263 struct lan743x_adapter
*adapter
= rx
->adapter
;
264 bool enable_flag
= true;
266 if (flags
& LAN743X_VECTOR_FLAG_SOURCE_ENABLE_CLEAR
) {
267 lan743x_csr_write(adapter
, INT_EN_CLR
,
268 INT_BIT_DMA_RX_(rx
->channel_number
));
271 if (int_sts
& INT_BIT_DMA_RX_(rx
->channel_number
)) {
272 u32 rx_frame_bit
= DMAC_INT_BIT_RXFRM_(rx
->channel_number
);
276 if (flags
& LAN743X_VECTOR_FLAG_SOURCE_STATUS_READ
)
277 dmac_int_sts
= lan743x_csr_read(adapter
, DMAC_INT_STS
);
279 dmac_int_sts
= rx_frame_bit
;
280 if (flags
& LAN743X_VECTOR_FLAG_SOURCE_ENABLE_CHECK
)
281 dmac_int_en
= lan743x_csr_read(adapter
,
284 dmac_int_en
= rx_frame_bit
;
286 dmac_int_en
&= rx_frame_bit
;
287 dmac_int_sts
&= dmac_int_en
;
288 if (dmac_int_sts
& rx_frame_bit
) {
289 napi_schedule(&rx
->napi
);
290 enable_flag
= false;/* poll funct will enable later */
296 lan743x_csr_write(adapter
, INT_EN_SET
,
297 INT_BIT_DMA_RX_(rx
->channel_number
));
301 static void lan743x_intr_shared_isr(void *context
, u32 int_sts
, u32 flags
)
303 struct lan743x_adapter
*adapter
= context
;
304 unsigned int channel
;
306 if (int_sts
& INT_BIT_ALL_RX_
) {
307 for (channel
= 0; channel
< LAN743X_USED_RX_CHANNELS
;
309 u32 int_bit
= INT_BIT_DMA_RX_(channel
);
311 if (int_sts
& int_bit
) {
312 lan743x_rx_isr(&adapter
->rx
[channel
],
318 if (int_sts
& INT_BIT_ALL_TX_
) {
319 for (channel
= 0; channel
< adapter
->used_tx_channels
;
321 u32 int_bit
= INT_BIT_DMA_TX_(channel
);
323 if (int_sts
& int_bit
) {
324 lan743x_tx_isr(&adapter
->tx
[channel
],
330 if (int_sts
& INT_BIT_ALL_OTHER_
) {
331 if (int_sts
& INT_BIT_SW_GP_
) {
332 lan743x_intr_software_isr(adapter
);
333 int_sts
&= ~INT_BIT_SW_GP_
;
335 if (int_sts
& INT_BIT_1588_
) {
336 lan743x_ptp_isr(adapter
);
337 int_sts
&= ~INT_BIT_1588_
;
341 lan743x_csr_write(adapter
, INT_EN_CLR
, int_sts
);
344 static irqreturn_t
lan743x_intr_entry_isr(int irq
, void *ptr
)
346 struct lan743x_vector
*vector
= ptr
;
347 struct lan743x_adapter
*adapter
= vector
->adapter
;
348 irqreturn_t result
= IRQ_NONE
;
352 if (vector
->flags
& LAN743X_VECTOR_FLAG_SOURCE_STATUS_READ
) {
353 int_sts
= lan743x_csr_read(adapter
, INT_STS
);
354 } else if (vector
->flags
&
355 (LAN743X_VECTOR_FLAG_SOURCE_STATUS_R2C
|
356 LAN743X_VECTOR_FLAG_SOURCE_ENABLE_R2C
)) {
357 int_sts
= lan743x_csr_read(adapter
, INT_STS_R2C
);
359 /* use mask as implied status */
360 int_sts
= vector
->int_mask
| INT_BIT_MAS_
;
363 if (!(int_sts
& INT_BIT_MAS_
))
366 if (vector
->flags
& LAN743X_VECTOR_FLAG_VECTOR_ENABLE_ISR_CLEAR
)
367 /* disable vector interrupt */
368 lan743x_csr_write(adapter
,
370 INT_VEC_EN_(vector
->vector_index
));
372 if (vector
->flags
& LAN743X_VECTOR_FLAG_MASTER_ENABLE_CLEAR
)
373 /* disable master interrupt */
374 lan743x_csr_write(adapter
, INT_EN_CLR
, INT_BIT_MAS_
);
376 if (vector
->flags
& LAN743X_VECTOR_FLAG_SOURCE_ENABLE_CHECK
) {
377 int_enables
= lan743x_csr_read(adapter
, INT_EN_SET
);
379 /* use vector mask as implied enable mask */
380 int_enables
= vector
->int_mask
;
383 int_sts
&= int_enables
;
384 int_sts
&= vector
->int_mask
;
386 if (vector
->handler
) {
387 vector
->handler(vector
->context
,
388 int_sts
, vector
->flags
);
390 /* disable interrupts on this vector */
391 lan743x_csr_write(adapter
, INT_EN_CLR
,
394 result
= IRQ_HANDLED
;
397 if (vector
->flags
& LAN743X_VECTOR_FLAG_MASTER_ENABLE_SET
)
398 /* enable master interrupt */
399 lan743x_csr_write(adapter
, INT_EN_SET
, INT_BIT_MAS_
);
401 if (vector
->flags
& LAN743X_VECTOR_FLAG_VECTOR_ENABLE_ISR_SET
)
402 /* enable vector interrupt */
403 lan743x_csr_write(adapter
,
405 INT_VEC_EN_(vector
->vector_index
));
410 static int lan743x_intr_test_isr(struct lan743x_adapter
*adapter
)
412 struct lan743x_intr
*intr
= &adapter
->intr
;
415 intr
->software_isr_flag
= false;
417 /* enable and activate test interrupt */
418 lan743x_csr_write(adapter
, INT_EN_SET
, INT_BIT_SW_GP_
);
419 lan743x_csr_write(adapter
, INT_SET
, INT_BIT_SW_GP_
);
421 ret
= wait_event_timeout(intr
->software_isr_wq
,
422 intr
->software_isr_flag
,
423 msecs_to_jiffies(200));
425 /* disable test interrupt */
426 lan743x_csr_write(adapter
, INT_EN_CLR
, INT_BIT_SW_GP_
);
428 return ret
> 0 ? 0 : -ENODEV
;
431 static int lan743x_intr_register_isr(struct lan743x_adapter
*adapter
,
432 int vector_index
, u32 flags
,
434 lan743x_vector_handler handler
,
437 struct lan743x_vector
*vector
= &adapter
->intr
.vector_list
441 vector
->adapter
= adapter
;
442 vector
->flags
= flags
;
443 vector
->vector_index
= vector_index
;
444 vector
->int_mask
= int_mask
;
445 vector
->handler
= handler
;
446 vector
->context
= context
;
448 ret
= request_irq(vector
->irq
,
449 lan743x_intr_entry_isr
,
450 (flags
& LAN743X_VECTOR_FLAG_IRQ_SHARED
) ?
451 IRQF_SHARED
: 0, DRIVER_NAME
, vector
);
453 vector
->handler
= NULL
;
454 vector
->context
= NULL
;
455 vector
->int_mask
= 0;
461 static void lan743x_intr_unregister_isr(struct lan743x_adapter
*adapter
,
464 struct lan743x_vector
*vector
= &adapter
->intr
.vector_list
467 free_irq(vector
->irq
, vector
);
468 vector
->handler
= NULL
;
469 vector
->context
= NULL
;
470 vector
->int_mask
= 0;
474 static u32
lan743x_intr_get_vector_flags(struct lan743x_adapter
*adapter
,
479 for (index
= 0; index
< adapter
->max_vector_count
; index
++) {
480 if (adapter
->intr
.vector_list
[index
].int_mask
& int_mask
)
481 return adapter
->intr
.vector_list
[index
].flags
;
486 static void lan743x_intr_close(struct lan743x_adapter
*adapter
)
488 struct lan743x_intr
*intr
= &adapter
->intr
;
491 lan743x_csr_write(adapter
, INT_EN_CLR
, INT_BIT_MAS_
);
492 if (adapter
->is_pci11x1x
)
493 lan743x_csr_write(adapter
, INT_VEC_EN_CLR
, 0x0000FFFF);
495 lan743x_csr_write(adapter
, INT_VEC_EN_CLR
, 0x000000FF);
497 for (index
= 0; index
< intr
->number_of_vectors
; index
++) {
498 if (intr
->flags
& INTR_FLAG_IRQ_REQUESTED(index
)) {
499 lan743x_intr_unregister_isr(adapter
, index
);
500 intr
->flags
&= ~INTR_FLAG_IRQ_REQUESTED(index
);
504 if (intr
->flags
& INTR_FLAG_MSI_ENABLED
) {
505 pci_disable_msi(adapter
->pdev
);
506 intr
->flags
&= ~INTR_FLAG_MSI_ENABLED
;
509 if (intr
->flags
& INTR_FLAG_MSIX_ENABLED
) {
510 pci_disable_msix(adapter
->pdev
);
511 intr
->flags
&= ~INTR_FLAG_MSIX_ENABLED
;
515 static int lan743x_intr_open(struct lan743x_adapter
*adapter
)
517 struct msix_entry msix_entries
[PCI11X1X_MAX_VECTOR_COUNT
];
518 struct lan743x_intr
*intr
= &adapter
->intr
;
519 unsigned int used_tx_channels
;
520 u32 int_vec_en_auto_clr
= 0;
522 u32 int_vec_map0
= 0;
523 u32 int_vec_map1
= 0;
528 intr
->number_of_vectors
= 0;
530 /* Try to set up MSIX interrupts */
531 max_vector_count
= adapter
->max_vector_count
;
532 memset(&msix_entries
[0], 0,
533 sizeof(struct msix_entry
) * max_vector_count
);
534 for (index
= 0; index
< max_vector_count
; index
++)
535 msix_entries
[index
].entry
= index
;
536 used_tx_channels
= adapter
->used_tx_channels
;
537 ret
= pci_enable_msix_range(adapter
->pdev
,
539 1 + used_tx_channels
+
540 LAN743X_USED_RX_CHANNELS
);
543 intr
->flags
|= INTR_FLAG_MSIX_ENABLED
;
544 intr
->number_of_vectors
= ret
;
545 intr
->using_vectors
= true;
546 for (index
= 0; index
< intr
->number_of_vectors
; index
++)
547 intr
->vector_list
[index
].irq
= msix_entries
549 netif_info(adapter
, ifup
, adapter
->netdev
,
550 "using MSIX interrupts, number of vectors = %d\n",
551 intr
->number_of_vectors
);
554 /* If MSIX failed try to setup using MSI interrupts */
555 if (!intr
->number_of_vectors
) {
556 if (!(adapter
->csr
.flags
& LAN743X_CSR_FLAG_IS_A0
)) {
557 if (!pci_enable_msi(adapter
->pdev
)) {
558 intr
->flags
|= INTR_FLAG_MSI_ENABLED
;
559 intr
->number_of_vectors
= 1;
560 intr
->using_vectors
= true;
561 intr
->vector_list
[0].irq
=
563 netif_info(adapter
, ifup
, adapter
->netdev
,
564 "using MSI interrupts, number of vectors = %d\n",
565 intr
->number_of_vectors
);
570 /* If MSIX, and MSI failed, setup using legacy interrupt */
571 if (!intr
->number_of_vectors
) {
572 intr
->number_of_vectors
= 1;
573 intr
->using_vectors
= false;
574 intr
->vector_list
[0].irq
= intr
->irq
;
575 netif_info(adapter
, ifup
, adapter
->netdev
,
576 "using legacy interrupts\n");
579 /* At this point we must have at least one irq */
580 lan743x_csr_write(adapter
, INT_VEC_EN_CLR
, 0xFFFFFFFF);
582 /* map all interrupts to vector 0 */
583 lan743x_csr_write(adapter
, INT_VEC_MAP0
, 0x00000000);
584 lan743x_csr_write(adapter
, INT_VEC_MAP1
, 0x00000000);
585 lan743x_csr_write(adapter
, INT_VEC_MAP2
, 0x00000000);
586 flags
= LAN743X_VECTOR_FLAG_SOURCE_STATUS_READ
|
587 LAN743X_VECTOR_FLAG_SOURCE_STATUS_W2C
|
588 LAN743X_VECTOR_FLAG_SOURCE_ENABLE_CHECK
|
589 LAN743X_VECTOR_FLAG_SOURCE_ENABLE_CLEAR
;
591 if (intr
->using_vectors
) {
592 flags
|= LAN743X_VECTOR_FLAG_VECTOR_ENABLE_ISR_CLEAR
|
593 LAN743X_VECTOR_FLAG_VECTOR_ENABLE_ISR_SET
;
595 flags
|= LAN743X_VECTOR_FLAG_MASTER_ENABLE_CLEAR
|
596 LAN743X_VECTOR_FLAG_MASTER_ENABLE_SET
|
597 LAN743X_VECTOR_FLAG_IRQ_SHARED
;
600 if (adapter
->csr
.flags
& LAN743X_CSR_FLAG_SUPPORTS_INTR_AUTO_SET_CLR
) {
601 flags
&= ~LAN743X_VECTOR_FLAG_SOURCE_STATUS_READ
;
602 flags
&= ~LAN743X_VECTOR_FLAG_SOURCE_STATUS_W2C
;
603 flags
&= ~LAN743X_VECTOR_FLAG_SOURCE_ENABLE_CLEAR
;
604 flags
&= ~LAN743X_VECTOR_FLAG_SOURCE_ENABLE_CHECK
;
605 flags
|= LAN743X_VECTOR_FLAG_SOURCE_STATUS_R2C
;
606 flags
|= LAN743X_VECTOR_FLAG_SOURCE_ENABLE_R2C
;
609 init_waitqueue_head(&intr
->software_isr_wq
);
611 ret
= lan743x_intr_register_isr(adapter
, 0, flags
,
612 INT_BIT_ALL_RX_
| INT_BIT_ALL_TX_
|
614 lan743x_intr_shared_isr
, adapter
);
617 intr
->flags
|= INTR_FLAG_IRQ_REQUESTED(0);
619 if (intr
->using_vectors
)
620 lan743x_csr_write(adapter
, INT_VEC_EN_SET
,
623 if (!(adapter
->csr
.flags
& LAN743X_CSR_FLAG_IS_A0
)) {
624 lan743x_csr_write(adapter
, INT_MOD_CFG0
, LAN743X_INT_MOD
);
625 lan743x_csr_write(adapter
, INT_MOD_CFG1
, LAN743X_INT_MOD
);
626 lan743x_csr_write(adapter
, INT_MOD_CFG2
, LAN743X_INT_MOD
);
627 lan743x_csr_write(adapter
, INT_MOD_CFG3
, LAN743X_INT_MOD
);
628 lan743x_csr_write(adapter
, INT_MOD_CFG4
, LAN743X_INT_MOD
);
629 lan743x_csr_write(adapter
, INT_MOD_CFG5
, LAN743X_INT_MOD
);
630 lan743x_csr_write(adapter
, INT_MOD_CFG6
, LAN743X_INT_MOD
);
631 lan743x_csr_write(adapter
, INT_MOD_CFG7
, LAN743X_INT_MOD
);
632 if (adapter
->is_pci11x1x
) {
633 lan743x_csr_write(adapter
, INT_MOD_CFG8
, LAN743X_INT_MOD
);
634 lan743x_csr_write(adapter
, INT_MOD_CFG9
, LAN743X_INT_MOD
);
635 lan743x_csr_write(adapter
, INT_MOD_MAP0
, 0x00007654);
636 lan743x_csr_write(adapter
, INT_MOD_MAP1
, 0x00003210);
638 lan743x_csr_write(adapter
, INT_MOD_MAP0
, 0x00005432);
639 lan743x_csr_write(adapter
, INT_MOD_MAP1
, 0x00000001);
641 lan743x_csr_write(adapter
, INT_MOD_MAP2
, 0x00FFFFFF);
644 /* enable interrupts */
645 lan743x_csr_write(adapter
, INT_EN_SET
, INT_BIT_MAS_
);
646 ret
= lan743x_intr_test_isr(adapter
);
650 if (intr
->number_of_vectors
> 1) {
651 int number_of_tx_vectors
= intr
->number_of_vectors
- 1;
653 if (number_of_tx_vectors
> used_tx_channels
)
654 number_of_tx_vectors
= used_tx_channels
;
655 flags
= LAN743X_VECTOR_FLAG_SOURCE_STATUS_READ
|
656 LAN743X_VECTOR_FLAG_SOURCE_STATUS_W2C
|
657 LAN743X_VECTOR_FLAG_SOURCE_ENABLE_CHECK
|
658 LAN743X_VECTOR_FLAG_SOURCE_ENABLE_CLEAR
|
659 LAN743X_VECTOR_FLAG_VECTOR_ENABLE_ISR_CLEAR
|
660 LAN743X_VECTOR_FLAG_VECTOR_ENABLE_ISR_SET
;
662 if (adapter
->csr
.flags
&
663 LAN743X_CSR_FLAG_SUPPORTS_INTR_AUTO_SET_CLR
) {
664 flags
= LAN743X_VECTOR_FLAG_VECTOR_ENABLE_AUTO_SET
|
665 LAN743X_VECTOR_FLAG_SOURCE_ENABLE_AUTO_SET
|
666 LAN743X_VECTOR_FLAG_SOURCE_ENABLE_AUTO_CLEAR
|
667 LAN743X_VECTOR_FLAG_SOURCE_STATUS_AUTO_CLEAR
;
670 for (index
= 0; index
< number_of_tx_vectors
; index
++) {
671 u32 int_bit
= INT_BIT_DMA_TX_(index
);
672 int vector
= index
+ 1;
674 /* map TX interrupt to vector */
675 int_vec_map1
|= INT_VEC_MAP1_TX_VEC_(index
, vector
);
676 lan743x_csr_write(adapter
, INT_VEC_MAP1
, int_vec_map1
);
678 /* Remove TX interrupt from shared mask */
679 intr
->vector_list
[0].int_mask
&= ~int_bit
;
680 ret
= lan743x_intr_register_isr(adapter
, vector
, flags
,
681 int_bit
, lan743x_tx_isr
,
682 &adapter
->tx
[index
]);
685 intr
->flags
|= INTR_FLAG_IRQ_REQUESTED(vector
);
687 LAN743X_VECTOR_FLAG_VECTOR_ENABLE_AUTO_SET
))
688 lan743x_csr_write(adapter
, INT_VEC_EN_SET
,
689 INT_VEC_EN_(vector
));
692 if ((intr
->number_of_vectors
- used_tx_channels
) > 1) {
693 int number_of_rx_vectors
= intr
->number_of_vectors
-
694 used_tx_channels
- 1;
696 if (number_of_rx_vectors
> LAN743X_USED_RX_CHANNELS
)
697 number_of_rx_vectors
= LAN743X_USED_RX_CHANNELS
;
699 flags
= LAN743X_VECTOR_FLAG_SOURCE_STATUS_READ
|
700 LAN743X_VECTOR_FLAG_SOURCE_STATUS_W2C
|
701 LAN743X_VECTOR_FLAG_SOURCE_ENABLE_CHECK
|
702 LAN743X_VECTOR_FLAG_SOURCE_ENABLE_CLEAR
|
703 LAN743X_VECTOR_FLAG_VECTOR_ENABLE_ISR_CLEAR
|
704 LAN743X_VECTOR_FLAG_VECTOR_ENABLE_ISR_SET
;
706 if (adapter
->csr
.flags
&
707 LAN743X_CSR_FLAG_SUPPORTS_INTR_AUTO_SET_CLR
) {
708 flags
= LAN743X_VECTOR_FLAG_VECTOR_ENABLE_AUTO_CLEAR
|
709 LAN743X_VECTOR_FLAG_VECTOR_ENABLE_AUTO_SET
|
710 LAN743X_VECTOR_FLAG_SOURCE_ENABLE_AUTO_SET
|
711 LAN743X_VECTOR_FLAG_SOURCE_ENABLE_AUTO_CLEAR
|
712 LAN743X_VECTOR_FLAG_SOURCE_STATUS_AUTO_CLEAR
;
714 for (index
= 0; index
< number_of_rx_vectors
; index
++) {
715 int vector
= index
+ 1 + used_tx_channels
;
716 u32 int_bit
= INT_BIT_DMA_RX_(index
);
718 /* map RX interrupt to vector */
719 int_vec_map0
|= INT_VEC_MAP0_RX_VEC_(index
, vector
);
720 lan743x_csr_write(adapter
, INT_VEC_MAP0
, int_vec_map0
);
722 LAN743X_VECTOR_FLAG_VECTOR_ENABLE_AUTO_CLEAR
) {
723 int_vec_en_auto_clr
|= INT_VEC_EN_(vector
);
724 lan743x_csr_write(adapter
, INT_VEC_EN_AUTO_CLR
,
725 int_vec_en_auto_clr
);
728 /* Remove RX interrupt from shared mask */
729 intr
->vector_list
[0].int_mask
&= ~int_bit
;
730 ret
= lan743x_intr_register_isr(adapter
, vector
, flags
,
731 int_bit
, lan743x_rx_isr
,
732 &adapter
->rx
[index
]);
735 intr
->flags
|= INTR_FLAG_IRQ_REQUESTED(vector
);
737 lan743x_csr_write(adapter
, INT_VEC_EN_SET
,
738 INT_VEC_EN_(vector
));
744 lan743x_intr_close(adapter
);
748 static int lan743x_dp_write(struct lan743x_adapter
*adapter
,
749 u32 select
, u32 addr
, u32 length
, u32
*buf
)
754 if (lan743x_csr_wait_for_bit_atomic(adapter
, DP_SEL
, DP_SEL_DPRDY_
,
757 dp_sel
= lan743x_csr_read(adapter
, DP_SEL
);
758 dp_sel
&= ~DP_SEL_MASK_
;
760 lan743x_csr_write(adapter
, DP_SEL
, dp_sel
);
762 for (i
= 0; i
< length
; i
++) {
763 lan743x_csr_write(adapter
, DP_ADDR
, addr
+ i
);
764 lan743x_csr_write(adapter
, DP_DATA_0
, buf
[i
]);
765 lan743x_csr_write(adapter
, DP_CMD
, DP_CMD_WRITE_
);
766 if (lan743x_csr_wait_for_bit_atomic(adapter
, DP_SEL
,
775 static u32
lan743x_mac_mii_access(u16 id
, u16 index
, int read
)
779 ret
= (id
<< MAC_MII_ACC_PHY_ADDR_SHIFT_
) &
780 MAC_MII_ACC_PHY_ADDR_MASK_
;
781 ret
|= (index
<< MAC_MII_ACC_MIIRINDA_SHIFT_
) &
782 MAC_MII_ACC_MIIRINDA_MASK_
;
785 ret
|= MAC_MII_ACC_MII_READ_
;
787 ret
|= MAC_MII_ACC_MII_WRITE_
;
788 ret
|= MAC_MII_ACC_MII_BUSY_
;
793 static int lan743x_mac_mii_wait_till_not_busy(struct lan743x_adapter
*adapter
)
797 return readx_poll_timeout(LAN743X_CSR_READ_OP
, MAC_MII_ACC
, data
,
798 !(data
& MAC_MII_ACC_MII_BUSY_
), 0, 1000000);
801 static int lan743x_mdiobus_read_c22(struct mii_bus
*bus
, int phy_id
, int index
)
803 struct lan743x_adapter
*adapter
= bus
->priv
;
807 /* confirm MII not busy */
808 ret
= lan743x_mac_mii_wait_till_not_busy(adapter
);
812 /* set the address, index & direction (read from PHY) */
813 mii_access
= lan743x_mac_mii_access(phy_id
, index
, MAC_MII_READ
);
814 lan743x_csr_write(adapter
, MAC_MII_ACC
, mii_access
);
815 ret
= lan743x_mac_mii_wait_till_not_busy(adapter
);
819 val
= lan743x_csr_read(adapter
, MAC_MII_DATA
);
820 return (int)(val
& 0xFFFF);
823 static int lan743x_mdiobus_write_c22(struct mii_bus
*bus
,
824 int phy_id
, int index
, u16 regval
)
826 struct lan743x_adapter
*adapter
= bus
->priv
;
830 /* confirm MII not busy */
831 ret
= lan743x_mac_mii_wait_till_not_busy(adapter
);
835 lan743x_csr_write(adapter
, MAC_MII_DATA
, val
);
837 /* set the address, index & direction (write to PHY) */
838 mii_access
= lan743x_mac_mii_access(phy_id
, index
, MAC_MII_WRITE
);
839 lan743x_csr_write(adapter
, MAC_MII_ACC
, mii_access
);
840 ret
= lan743x_mac_mii_wait_till_not_busy(adapter
);
844 static u32
lan743x_mac_mmd_access(int id
, int dev_addr
, int op
)
848 ret
= (id
<< MAC_MII_ACC_PHY_ADDR_SHIFT_
) &
849 MAC_MII_ACC_PHY_ADDR_MASK_
;
850 ret
|= (dev_addr
<< MAC_MII_ACC_MIIMMD_SHIFT_
) &
851 MAC_MII_ACC_MIIMMD_MASK_
;
852 if (op
== MMD_ACCESS_WRITE
)
853 ret
|= MAC_MII_ACC_MIICMD_WRITE_
;
854 else if (op
== MMD_ACCESS_READ
)
855 ret
|= MAC_MII_ACC_MIICMD_READ_
;
856 else if (op
== MMD_ACCESS_READ_INC
)
857 ret
|= MAC_MII_ACC_MIICMD_READ_INC_
;
859 ret
|= MAC_MII_ACC_MIICMD_ADDR_
;
860 ret
|= (MAC_MII_ACC_MII_BUSY_
| MAC_MII_ACC_MIICL45_
);
865 static int lan743x_mdiobus_read_c45(struct mii_bus
*bus
, int phy_id
,
866 int dev_addr
, int index
)
868 struct lan743x_adapter
*adapter
= bus
->priv
;
872 /* confirm MII not busy */
873 ret
= lan743x_mac_mii_wait_till_not_busy(adapter
);
877 /* Load Register Address */
878 lan743x_csr_write(adapter
, MAC_MII_DATA
, index
);
879 mmd_access
= lan743x_mac_mmd_access(phy_id
, dev_addr
,
881 lan743x_csr_write(adapter
, MAC_MII_ACC
, mmd_access
);
882 ret
= lan743x_mac_mii_wait_till_not_busy(adapter
);
887 mmd_access
= lan743x_mac_mmd_access(phy_id
, dev_addr
,
889 lan743x_csr_write(adapter
, MAC_MII_ACC
, mmd_access
);
890 ret
= lan743x_mac_mii_wait_till_not_busy(adapter
);
894 ret
= lan743x_csr_read(adapter
, MAC_MII_DATA
);
895 return (int)(ret
& 0xFFFF);
898 static int lan743x_mdiobus_write_c45(struct mii_bus
*bus
, int phy_id
,
899 int dev_addr
, int index
, u16 regval
)
901 struct lan743x_adapter
*adapter
= bus
->priv
;
905 /* confirm MII not busy */
906 ret
= lan743x_mac_mii_wait_till_not_busy(adapter
);
910 /* Load Register Address */
911 lan743x_csr_write(adapter
, MAC_MII_DATA
, (u32
)index
);
912 mmd_access
= lan743x_mac_mmd_access(phy_id
, dev_addr
,
914 lan743x_csr_write(adapter
, MAC_MII_ACC
, mmd_access
);
915 ret
= lan743x_mac_mii_wait_till_not_busy(adapter
);
920 lan743x_csr_write(adapter
, MAC_MII_DATA
, (u32
)regval
);
921 mmd_access
= lan743x_mac_mmd_access(phy_id
, dev_addr
,
923 lan743x_csr_write(adapter
, MAC_MII_ACC
, mmd_access
);
925 return lan743x_mac_mii_wait_till_not_busy(adapter
);
928 static int lan743x_sgmii_wait_till_not_busy(struct lan743x_adapter
*adapter
)
933 ret
= readx_poll_timeout(LAN743X_CSR_READ_OP
, SGMII_ACC
, data
,
934 !(data
& SGMII_ACC_SGMII_BZY_
), 100, 1000000);
936 netif_err(adapter
, drv
, adapter
->netdev
,
937 "%s: error %d sgmii wait timeout\n", __func__
, ret
);
942 int lan743x_sgmii_read(struct lan743x_adapter
*adapter
, u8 mmd
, u16 addr
)
949 netif_err(adapter
, probe
, adapter
->netdev
,
950 "%s mmd should <= 31\n", __func__
);
954 mutex_lock(&adapter
->sgmii_rw_lock
);
955 /* Load Register Address */
956 mmd_access
= mmd
<< SGMII_ACC_SGMII_MMD_SHIFT_
;
957 mmd_access
|= (addr
| SGMII_ACC_SGMII_BZY_
);
958 lan743x_csr_write(adapter
, SGMII_ACC
, mmd_access
);
959 ret
= lan743x_sgmii_wait_till_not_busy(adapter
);
963 val
= lan743x_csr_read(adapter
, SGMII_DATA
);
964 ret
= (int)(val
& SGMII_DATA_MASK_
);
967 mutex_unlock(&adapter
->sgmii_rw_lock
);
972 static int lan743x_sgmii_write(struct lan743x_adapter
*adapter
,
973 u8 mmd
, u16 addr
, u16 val
)
979 netif_err(adapter
, probe
, adapter
->netdev
,
980 "%s mmd should <= 31\n", __func__
);
983 mutex_lock(&adapter
->sgmii_rw_lock
);
984 /* Load Register Data */
985 lan743x_csr_write(adapter
, SGMII_DATA
, (u32
)(val
& SGMII_DATA_MASK_
));
986 /* Load Register Address */
987 mmd_access
= mmd
<< SGMII_ACC_SGMII_MMD_SHIFT_
;
988 mmd_access
|= (addr
| SGMII_ACC_SGMII_BZY_
| SGMII_ACC_SGMII_WR_
);
989 lan743x_csr_write(adapter
, SGMII_ACC
, mmd_access
);
990 ret
= lan743x_sgmii_wait_till_not_busy(adapter
);
991 mutex_unlock(&adapter
->sgmii_rw_lock
);
996 static int lan743x_get_lsd(int speed
, int duplex
, u8 mss
)
1002 if (mss
== MASTER_SLAVE_STATE_SLAVE
)
1003 lsd
= LINK_2500_SLAVE
;
1005 lsd
= LINK_2500_MASTER
;
1008 if (mss
== MASTER_SLAVE_STATE_SLAVE
)
1009 lsd
= LINK_1000_SLAVE
;
1011 lsd
= LINK_1000_MASTER
;
1014 if (duplex
== DUPLEX_FULL
)
1020 if (duplex
== DUPLEX_FULL
)
1032 static int lan743x_sgmii_mpll_set(struct lan743x_adapter
*adapter
,
1040 mpllctrl0
= lan743x_sgmii_read(adapter
, MDIO_MMD_VEND2
,
1041 VR_MII_GEN2_4_MPLL_CTRL0
);
1045 mpllctrl0
&= ~VR_MII_MPLL_CTRL0_USE_REFCLK_PAD_
;
1046 if (baud
== VR_MII_BAUD_RATE_1P25GBPS
) {
1047 mpllctrl1
= VR_MII_MPLL_MULTIPLIER_100
;
1048 /* mpll_baud_clk/4 */
1051 mpllctrl1
= VR_MII_MPLL_MULTIPLIER_125
;
1052 /* mpll_baud_clk/2 */
1056 ret
= lan743x_sgmii_write(adapter
, MDIO_MMD_VEND2
,
1057 VR_MII_GEN2_4_MPLL_CTRL0
, mpllctrl0
);
1061 ret
= lan743x_sgmii_write(adapter
, MDIO_MMD_VEND2
,
1062 VR_MII_GEN2_4_MPLL_CTRL1
, mpllctrl1
);
1066 return lan743x_sgmii_write(adapter
, MDIO_MMD_VEND2
,
1067 VR_MII_GEN2_4_MISC_CTRL1
, miscctrl1
);
1070 static int lan743x_sgmii_2_5G_mode_set(struct lan743x_adapter
*adapter
,
1074 return lan743x_sgmii_mpll_set(adapter
,
1075 VR_MII_BAUD_RATE_3P125GBPS
);
1077 return lan743x_sgmii_mpll_set(adapter
,
1078 VR_MII_BAUD_RATE_1P25GBPS
);
1081 static int lan743x_serdes_clock_and_aneg_update(struct lan743x_adapter
*adapter
)
1083 enum lan743x_sgmii_lsd lsd
= adapter
->sgmii_lsd
;
1089 if (lsd
== LINK_2500_MASTER
|| lsd
== LINK_2500_SLAVE
)
1090 /* Switch to 2.5 Gbps */
1091 ret
= lan743x_sgmii_2_5G_mode_set(adapter
, true);
1093 /* Switch to 10/100/1000 Mbps clock */
1094 ret
= lan743x_sgmii_2_5G_mode_set(adapter
, false);
1098 /* Enable SGMII Auto NEG */
1099 mii_ctrl
= lan743x_sgmii_read(adapter
, MDIO_MMD_VEND2
, MII_BMCR
);
1103 an_ctrl
= lan743x_sgmii_read(adapter
, MDIO_MMD_VEND2
, VR_MII_AN_CTRL
);
1107 dgt_ctrl
= lan743x_sgmii_read(adapter
, MDIO_MMD_VEND2
,
1112 if (lsd
== LINK_2500_MASTER
|| lsd
== LINK_2500_SLAVE
) {
1113 mii_ctrl
&= ~(BMCR_ANENABLE
| BMCR_ANRESTART
| BMCR_SPEED100
);
1114 mii_ctrl
|= BMCR_SPEED1000
;
1115 dgt_ctrl
|= VR_MII_DIG_CTRL1_CL37_TMR_OVR_RIDE_
;
1116 dgt_ctrl
&= ~VR_MII_DIG_CTRL1_MAC_AUTO_SW_
;
1117 /* In order for Auto-Negotiation to operate properly at
1118 * 2.5 Gbps the 1.6ms link timer values must be adjusted
1119 * The VR_MII_LINK_TIMER_CTRL Register must be set to
1120 * 16'h7A1 and The CL37_TMR_OVR_RIDE bit of the
1121 * VR_MII_DIG_CTRL1 Register set to 1
1123 ret
= lan743x_sgmii_write(adapter
, MDIO_MMD_VEND2
,
1124 VR_MII_LINK_TIMER_CTRL
, 0x7A1);
1128 mii_ctrl
|= (BMCR_ANENABLE
| BMCR_ANRESTART
);
1129 an_ctrl
&= ~VR_MII_AN_CTRL_SGMII_LINK_STS_
;
1130 dgt_ctrl
&= ~VR_MII_DIG_CTRL1_CL37_TMR_OVR_RIDE_
;
1131 dgt_ctrl
|= VR_MII_DIG_CTRL1_MAC_AUTO_SW_
;
1134 ret
= lan743x_sgmii_write(adapter
, MDIO_MMD_VEND2
, MII_BMCR
,
1139 ret
= lan743x_sgmii_write(adapter
, MDIO_MMD_VEND2
,
1140 VR_MII_DIG_CTRL1
, dgt_ctrl
);
1144 return lan743x_sgmii_write(adapter
, MDIO_MMD_VEND2
,
1145 VR_MII_AN_CTRL
, an_ctrl
);
1148 static int lan743x_pcs_seq_state(struct lan743x_adapter
*adapter
, u8 state
)
1154 dig_sts
= lan743x_sgmii_read(adapter
, MDIO_MMD_VEND2
,
1156 if (((dig_sts
& VR_MII_DIG_STS_PSEQ_STATE_MASK_
) >>
1157 VR_MII_DIG_STS_PSEQ_STATE_POS_
) == state
)
1159 usleep_range(1000, 2000);
1160 } while (wait_cnt
++ < 10);
1168 static int lan743x_pcs_power_reset(struct lan743x_adapter
*adapter
)
1173 /* SGMII/1000/2500BASE-X PCS power down */
1174 mii_ctl
= lan743x_sgmii_read(adapter
, MDIO_MMD_VEND2
, MII_BMCR
);
1178 mii_ctl
|= BMCR_PDOWN
;
1179 ret
= lan743x_sgmii_write(adapter
, MDIO_MMD_VEND2
, MII_BMCR
, mii_ctl
);
1183 ret
= lan743x_pcs_seq_state(adapter
, PCS_POWER_STATE_DOWN
);
1187 /* SGMII/1000/2500BASE-X PCS power up */
1188 mii_ctl
&= ~BMCR_PDOWN
;
1189 ret
= lan743x_sgmii_write(adapter
, MDIO_MMD_VEND2
, MII_BMCR
, mii_ctl
);
1193 return lan743x_pcs_seq_state(adapter
, PCS_POWER_STATE_UP
);
1196 static void lan743x_mac_set_address(struct lan743x_adapter
*adapter
,
1199 u32 addr_lo
, addr_hi
;
1207 lan743x_csr_write(adapter
, MAC_RX_ADDRL
, addr_lo
);
1208 lan743x_csr_write(adapter
, MAC_RX_ADDRH
, addr_hi
);
1210 ether_addr_copy(adapter
->mac_address
, addr
);
1211 netif_info(adapter
, drv
, adapter
->netdev
,
1212 "MAC address set to %pM\n", addr
);
1215 static int lan743x_mac_init(struct lan743x_adapter
*adapter
)
1217 bool mac_address_valid
= true;
1218 struct net_device
*netdev
;
1219 u32 mac_addr_hi
= 0;
1220 u32 mac_addr_lo
= 0;
1223 netdev
= adapter
->netdev
;
1225 /* disable auto duplex, and speed detection. Phylib does that */
1226 data
= lan743x_csr_read(adapter
, MAC_CR
);
1227 data
&= ~(MAC_CR_ADD_
| MAC_CR_ASD_
);
1228 data
|= MAC_CR_CNTR_RST_
;
1229 lan743x_csr_write(adapter
, MAC_CR
, data
);
1231 if (!is_valid_ether_addr(adapter
->mac_address
)) {
1232 mac_addr_hi
= lan743x_csr_read(adapter
, MAC_RX_ADDRH
);
1233 mac_addr_lo
= lan743x_csr_read(adapter
, MAC_RX_ADDRL
);
1234 adapter
->mac_address
[0] = mac_addr_lo
& 0xFF;
1235 adapter
->mac_address
[1] = (mac_addr_lo
>> 8) & 0xFF;
1236 adapter
->mac_address
[2] = (mac_addr_lo
>> 16) & 0xFF;
1237 adapter
->mac_address
[3] = (mac_addr_lo
>> 24) & 0xFF;
1238 adapter
->mac_address
[4] = mac_addr_hi
& 0xFF;
1239 adapter
->mac_address
[5] = (mac_addr_hi
>> 8) & 0xFF;
1241 if (((mac_addr_hi
& 0x0000FFFF) == 0x0000FFFF) &&
1242 mac_addr_lo
== 0xFFFFFFFF) {
1243 mac_address_valid
= false;
1244 } else if (!is_valid_ether_addr(adapter
->mac_address
)) {
1245 mac_address_valid
= false;
1248 if (!mac_address_valid
)
1249 eth_random_addr(adapter
->mac_address
);
1251 lan743x_mac_set_address(adapter
, adapter
->mac_address
);
1252 eth_hw_addr_set(netdev
, adapter
->mac_address
);
1257 static int lan743x_mac_open(struct lan743x_adapter
*adapter
)
1261 temp
= lan743x_csr_read(adapter
, MAC_RX
);
1262 lan743x_csr_write(adapter
, MAC_RX
, temp
| MAC_RX_RXEN_
);
1263 temp
= lan743x_csr_read(adapter
, MAC_TX
);
1264 lan743x_csr_write(adapter
, MAC_TX
, temp
| MAC_TX_TXEN_
);
1268 static void lan743x_mac_close(struct lan743x_adapter
*adapter
)
1272 temp
= lan743x_csr_read(adapter
, MAC_TX
);
1273 temp
&= ~MAC_TX_TXEN_
;
1274 lan743x_csr_write(adapter
, MAC_TX
, temp
);
1275 lan743x_csr_wait_for_bit(adapter
, MAC_TX
, MAC_TX_TXD_
,
1276 1, 1000, 20000, 100);
1278 temp
= lan743x_csr_read(adapter
, MAC_RX
);
1279 temp
&= ~MAC_RX_RXEN_
;
1280 lan743x_csr_write(adapter
, MAC_RX
, temp
);
1281 lan743x_csr_wait_for_bit(adapter
, MAC_RX
, MAC_RX_RXD_
,
1282 1, 1000, 20000, 100);
1285 void lan743x_mac_flow_ctrl_set_enables(struct lan743x_adapter
*adapter
,
1286 bool tx_enable
, bool rx_enable
)
1288 u32 flow_setting
= 0;
1290 /* set maximum pause time because when fifo space frees
1291 * up a zero value pause frame will be sent to release the pause
1293 flow_setting
= MAC_FLOW_CR_FCPT_MASK_
;
1295 flow_setting
|= MAC_FLOW_CR_TX_FCEN_
;
1297 flow_setting
|= MAC_FLOW_CR_RX_FCEN_
;
1298 lan743x_csr_write(adapter
, MAC_FLOW
, flow_setting
);
1301 static int lan743x_mac_set_mtu(struct lan743x_adapter
*adapter
, int new_mtu
)
1306 mac_rx
= lan743x_csr_read(adapter
, MAC_RX
);
1307 if (mac_rx
& MAC_RX_RXEN_
) {
1309 if (mac_rx
& MAC_RX_RXD_
) {
1310 lan743x_csr_write(adapter
, MAC_RX
, mac_rx
);
1311 mac_rx
&= ~MAC_RX_RXD_
;
1313 mac_rx
&= ~MAC_RX_RXEN_
;
1314 lan743x_csr_write(adapter
, MAC_RX
, mac_rx
);
1315 lan743x_csr_wait_for_bit(adapter
, MAC_RX
, MAC_RX_RXD_
,
1316 1, 1000, 20000, 100);
1317 lan743x_csr_write(adapter
, MAC_RX
, mac_rx
| MAC_RX_RXD_
);
1320 mac_rx
&= ~(MAC_RX_MAX_SIZE_MASK_
);
1321 mac_rx
|= (((new_mtu
+ ETH_HLEN
+ ETH_FCS_LEN
)
1322 << MAC_RX_MAX_SIZE_SHIFT_
) & MAC_RX_MAX_SIZE_MASK_
);
1323 lan743x_csr_write(adapter
, MAC_RX
, mac_rx
);
1326 mac_rx
|= MAC_RX_RXEN_
;
1327 lan743x_csr_write(adapter
, MAC_RX
, mac_rx
);
1333 static int lan743x_phy_reset(struct lan743x_adapter
*adapter
)
1337 /* Only called with in probe, and before mdiobus_register */
1339 data
= lan743x_csr_read(adapter
, PMT_CTL
);
1340 data
|= PMT_CTL_ETH_PHY_RST_
;
1341 lan743x_csr_write(adapter
, PMT_CTL
, data
);
1343 return readx_poll_timeout(LAN743X_CSR_READ_OP
, PMT_CTL
, data
,
1344 (!(data
& PMT_CTL_ETH_PHY_RST_
) &&
1345 (data
& PMT_CTL_READY_
)),
1349 static int lan743x_phy_init(struct lan743x_adapter
*adapter
)
1351 return lan743x_phy_reset(adapter
);
1354 static void lan743x_phy_interface_select(struct lan743x_adapter
*adapter
)
1359 data
= lan743x_csr_read(adapter
, MAC_CR
);
1360 id_rev
= adapter
->csr
.id_rev
& ID_REV_ID_MASK_
;
1362 if (adapter
->is_pci11x1x
&& adapter
->is_sgmii_en
)
1363 adapter
->phy_interface
= PHY_INTERFACE_MODE_SGMII
;
1364 else if (id_rev
== ID_REV_ID_LAN7430_
)
1365 adapter
->phy_interface
= PHY_INTERFACE_MODE_GMII
;
1366 else if ((id_rev
== ID_REV_ID_LAN7431_
) && (data
& MAC_CR_MII_EN_
))
1367 adapter
->phy_interface
= PHY_INTERFACE_MODE_MII
;
1369 adapter
->phy_interface
= PHY_INTERFACE_MODE_RGMII
;
1371 netif_dbg(adapter
, drv
, adapter
->netdev
,
1372 "selected phy interface: 0x%X\n", adapter
->phy_interface
);
1375 static void lan743x_rfe_open(struct lan743x_adapter
*adapter
)
1377 lan743x_csr_write(adapter
, RFE_RSS_CFG
,
1378 RFE_RSS_CFG_UDP_IPV6_EX_
|
1379 RFE_RSS_CFG_TCP_IPV6_EX_
|
1380 RFE_RSS_CFG_IPV6_EX_
|
1381 RFE_RSS_CFG_UDP_IPV6_
|
1382 RFE_RSS_CFG_TCP_IPV6_
|
1384 RFE_RSS_CFG_UDP_IPV4_
|
1385 RFE_RSS_CFG_TCP_IPV4_
|
1387 RFE_RSS_CFG_VALID_HASH_BITS_
|
1388 RFE_RSS_CFG_RSS_QUEUE_ENABLE_
|
1389 RFE_RSS_CFG_RSS_HASH_STORE_
|
1390 RFE_RSS_CFG_RSS_ENABLE_
);
1393 static void lan743x_rfe_update_mac_address(struct lan743x_adapter
*adapter
)
1396 u32 mac_addr_hi
= 0;
1397 u32 mac_addr_lo
= 0;
1399 /* Add mac address to perfect Filter */
1400 mac_addr
= adapter
->mac_address
;
1401 mac_addr_lo
= ((((u32
)(mac_addr
[0])) << 0) |
1402 (((u32
)(mac_addr
[1])) << 8) |
1403 (((u32
)(mac_addr
[2])) << 16) |
1404 (((u32
)(mac_addr
[3])) << 24));
1405 mac_addr_hi
= ((((u32
)(mac_addr
[4])) << 0) |
1406 (((u32
)(mac_addr
[5])) << 8));
1408 lan743x_csr_write(adapter
, RFE_ADDR_FILT_LO(0), mac_addr_lo
);
1409 lan743x_csr_write(adapter
, RFE_ADDR_FILT_HI(0),
1410 mac_addr_hi
| RFE_ADDR_FILT_HI_VALID_
);
1413 static void lan743x_rfe_set_multicast(struct lan743x_adapter
*adapter
)
1415 struct net_device
*netdev
= adapter
->netdev
;
1416 u32 hash_table
[DP_SEL_VHF_HASH_LEN
];
1420 rfctl
= lan743x_csr_read(adapter
, RFE_CTL
);
1421 rfctl
&= ~(RFE_CTL_AU_
| RFE_CTL_AM_
|
1422 RFE_CTL_DA_PERFECT_
| RFE_CTL_MCAST_HASH_
);
1423 rfctl
|= RFE_CTL_AB_
;
1424 if (netdev
->flags
& IFF_PROMISC
) {
1425 rfctl
|= RFE_CTL_AM_
| RFE_CTL_AU_
;
1427 if (netdev
->flags
& IFF_ALLMULTI
)
1428 rfctl
|= RFE_CTL_AM_
;
1431 if (netdev
->features
& NETIF_F_RXCSUM
)
1432 rfctl
|= RFE_CTL_IP_COE_
| RFE_CTL_TCP_UDP_COE_
;
1434 memset(hash_table
, 0, DP_SEL_VHF_HASH_LEN
* sizeof(u32
));
1435 if (netdev_mc_count(netdev
)) {
1436 struct netdev_hw_addr
*ha
;
1439 rfctl
|= RFE_CTL_DA_PERFECT_
;
1441 netdev_for_each_mc_addr(ha
, netdev
) {
1442 /* set first 32 into Perfect Filter */
1444 lan743x_csr_write(adapter
,
1445 RFE_ADDR_FILT_HI(i
), 0);
1447 data
= ha
->addr
[2] | (data
<< 8);
1448 data
= ha
->addr
[1] | (data
<< 8);
1449 data
= ha
->addr
[0] | (data
<< 8);
1450 lan743x_csr_write(adapter
,
1451 RFE_ADDR_FILT_LO(i
), data
);
1453 data
= ha
->addr
[4] | (data
<< 8);
1454 data
|= RFE_ADDR_FILT_HI_VALID_
;
1455 lan743x_csr_write(adapter
,
1456 RFE_ADDR_FILT_HI(i
), data
);
1458 u32 bitnum
= (ether_crc(ETH_ALEN
, ha
->addr
) >>
1460 hash_table
[bitnum
/ 32] |= (1 << (bitnum
% 32));
1461 rfctl
|= RFE_CTL_MCAST_HASH_
;
1467 lan743x_dp_write(adapter
, DP_SEL_RFE_RAM
,
1468 DP_SEL_VHF_VLAN_LEN
,
1469 DP_SEL_VHF_HASH_LEN
, hash_table
);
1470 lan743x_csr_write(adapter
, RFE_CTL
, rfctl
);
1473 static int lan743x_dmac_init(struct lan743x_adapter
*adapter
)
1477 lan743x_csr_write(adapter
, DMAC_CMD
, DMAC_CMD_SWR_
);
1478 lan743x_csr_wait_for_bit(adapter
, DMAC_CMD
, DMAC_CMD_SWR_
,
1479 0, 1000, 20000, 100);
1480 switch (DEFAULT_DMA_DESCRIPTOR_SPACING
) {
1481 case DMA_DESCRIPTOR_SPACING_16
:
1482 data
= DMAC_CFG_MAX_DSPACE_16_
;
1484 case DMA_DESCRIPTOR_SPACING_32
:
1485 data
= DMAC_CFG_MAX_DSPACE_32_
;
1487 case DMA_DESCRIPTOR_SPACING_64
:
1488 data
= DMAC_CFG_MAX_DSPACE_64_
;
1490 case DMA_DESCRIPTOR_SPACING_128
:
1491 data
= DMAC_CFG_MAX_DSPACE_128_
;
1496 if (!(adapter
->csr
.flags
& LAN743X_CSR_FLAG_IS_A0
))
1497 data
|= DMAC_CFG_COAL_EN_
;
1498 data
|= DMAC_CFG_CH_ARB_SEL_RX_HIGH_
;
1499 data
|= DMAC_CFG_MAX_READ_REQ_SET_(6);
1500 lan743x_csr_write(adapter
, DMAC_CFG
, data
);
1501 data
= DMAC_COAL_CFG_TIMER_LIMIT_SET_(1);
1502 data
|= DMAC_COAL_CFG_TIMER_TX_START_
;
1503 data
|= DMAC_COAL_CFG_FLUSH_INTS_
;
1504 data
|= DMAC_COAL_CFG_INT_EXIT_COAL_
;
1505 data
|= DMAC_COAL_CFG_CSR_EXIT_COAL_
;
1506 data
|= DMAC_COAL_CFG_TX_THRES_SET_(0x0A);
1507 data
|= DMAC_COAL_CFG_RX_THRES_SET_(0x0C);
1508 lan743x_csr_write(adapter
, DMAC_COAL_CFG
, data
);
1509 data
= DMAC_OBFF_TX_THRES_SET_(0x08);
1510 data
|= DMAC_OBFF_RX_THRES_SET_(0x0A);
1511 lan743x_csr_write(adapter
, DMAC_OBFF_CFG
, data
);
1515 static int lan743x_dmac_tx_get_state(struct lan743x_adapter
*adapter
,
1520 dmac_cmd
= lan743x_csr_read(adapter
, DMAC_CMD
);
1521 return DMAC_CHANNEL_STATE_SET((dmac_cmd
&
1522 DMAC_CMD_START_T_(tx_channel
)),
1524 DMAC_CMD_STOP_T_(tx_channel
)));
1527 static int lan743x_dmac_tx_wait_till_stopped(struct lan743x_adapter
*adapter
,
1534 ((result
= lan743x_dmac_tx_get_state(adapter
, tx_channel
)) ==
1535 DMAC_CHANNEL_STATE_STOP_PENDING
)) {
1536 usleep_range(1000, 20000);
1539 if (result
== DMAC_CHANNEL_STATE_STOP_PENDING
)
1544 static int lan743x_dmac_rx_get_state(struct lan743x_adapter
*adapter
,
1549 dmac_cmd
= lan743x_csr_read(adapter
, DMAC_CMD
);
1550 return DMAC_CHANNEL_STATE_SET((dmac_cmd
&
1551 DMAC_CMD_START_R_(rx_channel
)),
1553 DMAC_CMD_STOP_R_(rx_channel
)));
1556 static int lan743x_dmac_rx_wait_till_stopped(struct lan743x_adapter
*adapter
,
1563 ((result
= lan743x_dmac_rx_get_state(adapter
, rx_channel
)) ==
1564 DMAC_CHANNEL_STATE_STOP_PENDING
)) {
1565 usleep_range(1000, 20000);
1568 if (result
== DMAC_CHANNEL_STATE_STOP_PENDING
)
1573 static void lan743x_tx_release_desc(struct lan743x_tx
*tx
,
1574 int descriptor_index
, bool cleanup
)
1576 struct lan743x_tx_buffer_info
*buffer_info
= NULL
;
1577 struct lan743x_tx_descriptor
*descriptor
= NULL
;
1578 u32 descriptor_type
= 0;
1581 descriptor
= &tx
->ring_cpu_ptr
[descriptor_index
];
1582 buffer_info
= &tx
->buffer_info
[descriptor_index
];
1583 if (!(buffer_info
->flags
& TX_BUFFER_INFO_FLAG_ACTIVE
))
1586 descriptor_type
= le32_to_cpu(descriptor
->data0
) &
1587 TX_DESC_DATA0_DTYPE_MASK_
;
1588 if (descriptor_type
== TX_DESC_DATA0_DTYPE_DATA_
)
1589 goto clean_up_data_descriptor
;
1593 clean_up_data_descriptor
:
1594 if (buffer_info
->dma_ptr
) {
1595 if (buffer_info
->flags
&
1596 TX_BUFFER_INFO_FLAG_SKB_FRAGMENT
) {
1597 dma_unmap_page(&tx
->adapter
->pdev
->dev
,
1598 buffer_info
->dma_ptr
,
1599 buffer_info
->buffer_length
,
1602 dma_unmap_single(&tx
->adapter
->pdev
->dev
,
1603 buffer_info
->dma_ptr
,
1604 buffer_info
->buffer_length
,
1607 buffer_info
->dma_ptr
= 0;
1608 buffer_info
->buffer_length
= 0;
1610 if (!buffer_info
->skb
)
1613 if (!(buffer_info
->flags
& TX_BUFFER_INFO_FLAG_TIMESTAMP_REQUESTED
)) {
1614 dev_kfree_skb_any(buffer_info
->skb
);
1619 lan743x_ptp_unrequest_tx_timestamp(tx
->adapter
);
1620 dev_kfree_skb_any(buffer_info
->skb
);
1622 ignore_sync
= (buffer_info
->flags
&
1623 TX_BUFFER_INFO_FLAG_IGNORE_SYNC
) != 0;
1624 lan743x_ptp_tx_timestamp_skb(tx
->adapter
,
1625 buffer_info
->skb
, ignore_sync
);
1629 buffer_info
->skb
= NULL
;
1632 buffer_info
->flags
&= ~TX_BUFFER_INFO_FLAG_ACTIVE
;
1635 memset(buffer_info
, 0, sizeof(*buffer_info
));
1636 memset(descriptor
, 0, sizeof(*descriptor
));
1639 static int lan743x_tx_next_index(struct lan743x_tx
*tx
, int index
)
1641 return ((++index
) % tx
->ring_size
);
1644 static void lan743x_tx_release_completed_descriptors(struct lan743x_tx
*tx
)
1646 while (le32_to_cpu(*tx
->head_cpu_ptr
) != (tx
->last_head
)) {
1647 lan743x_tx_release_desc(tx
, tx
->last_head
, false);
1648 tx
->last_head
= lan743x_tx_next_index(tx
, tx
->last_head
);
1652 static void lan743x_tx_release_all_descriptors(struct lan743x_tx
*tx
)
1654 u32 original_head
= 0;
1656 original_head
= tx
->last_head
;
1658 lan743x_tx_release_desc(tx
, tx
->last_head
, true);
1659 tx
->last_head
= lan743x_tx_next_index(tx
, tx
->last_head
);
1660 } while (tx
->last_head
!= original_head
);
1661 memset(tx
->ring_cpu_ptr
, 0,
1662 sizeof(*tx
->ring_cpu_ptr
) * (tx
->ring_size
));
1663 memset(tx
->buffer_info
, 0,
1664 sizeof(*tx
->buffer_info
) * (tx
->ring_size
));
1667 static int lan743x_tx_get_desc_cnt(struct lan743x_tx
*tx
,
1668 struct sk_buff
*skb
)
1670 int result
= 1; /* 1 for the main skb buffer */
1673 if (skb_is_gso(skb
))
1674 result
++; /* requires an extension descriptor */
1675 nr_frags
= skb_shinfo(skb
)->nr_frags
;
1676 result
+= nr_frags
; /* 1 for each fragment buffer */
1680 static int lan743x_tx_get_avail_desc(struct lan743x_tx
*tx
)
1682 int last_head
= tx
->last_head
;
1683 int last_tail
= tx
->last_tail
;
1685 if (last_tail
>= last_head
)
1686 return tx
->ring_size
- last_tail
+ last_head
- 1;
1688 return last_head
- last_tail
- 1;
1691 static void lan743x_rx_cfg_b_tstamp_config(struct lan743x_adapter
*adapter
,
1698 for (index
= 0; index
< LAN743X_USED_RX_CHANNELS
; index
++) {
1699 channel_number
= adapter
->rx
[index
].channel_number
;
1700 data
= lan743x_csr_read(adapter
, RX_CFG_B(channel_number
));
1701 data
&= RX_CFG_B_TS_MASK_
;
1702 data
|= rx_ts_config
;
1703 lan743x_csr_write(adapter
, RX_CFG_B(channel_number
),
1708 int lan743x_rx_set_tstamp_mode(struct lan743x_adapter
*adapter
,
1713 switch (rx_filter
) {
1714 case HWTSTAMP_FILTER_PTP_V2_EVENT
:
1715 lan743x_rx_cfg_b_tstamp_config(adapter
,
1716 RX_CFG_B_TS_DESCR_EN_
);
1717 data
= lan743x_csr_read(adapter
, PTP_RX_TS_CFG
);
1718 data
|= PTP_RX_TS_CFG_EVENT_MSGS_
;
1719 lan743x_csr_write(adapter
, PTP_RX_TS_CFG
, data
);
1721 case HWTSTAMP_FILTER_NONE
:
1722 lan743x_rx_cfg_b_tstamp_config(adapter
,
1725 case HWTSTAMP_FILTER_ALL
:
1726 lan743x_rx_cfg_b_tstamp_config(adapter
,
1727 RX_CFG_B_TS_ALL_RX_
);
1735 void lan743x_tx_set_timestamping_mode(struct lan743x_tx
*tx
,
1736 bool enable_timestamping
,
1737 bool enable_onestep_sync
)
1739 if (enable_timestamping
)
1740 tx
->ts_flags
|= TX_TS_FLAG_TIMESTAMPING_ENABLED
;
1742 tx
->ts_flags
&= ~TX_TS_FLAG_TIMESTAMPING_ENABLED
;
1743 if (enable_onestep_sync
)
1744 tx
->ts_flags
|= TX_TS_FLAG_ONE_STEP_SYNC
;
1746 tx
->ts_flags
&= ~TX_TS_FLAG_ONE_STEP_SYNC
;
1749 static int lan743x_tx_frame_start(struct lan743x_tx
*tx
,
1750 unsigned char *first_buffer
,
1751 unsigned int first_buffer_length
,
1752 unsigned int frame_length
,
1756 /* called only from within lan743x_tx_xmit_frame.
1757 * assuming tx->ring_lock has already been acquired.
1759 struct lan743x_tx_descriptor
*tx_descriptor
= NULL
;
1760 struct lan743x_tx_buffer_info
*buffer_info
= NULL
;
1761 struct lan743x_adapter
*adapter
= tx
->adapter
;
1762 struct device
*dev
= &adapter
->pdev
->dev
;
1765 tx
->frame_flags
|= TX_FRAME_FLAG_IN_PROGRESS
;
1766 tx
->frame_first
= tx
->last_tail
;
1767 tx
->frame_tail
= tx
->frame_first
;
1769 tx_descriptor
= &tx
->ring_cpu_ptr
[tx
->frame_tail
];
1770 buffer_info
= &tx
->buffer_info
[tx
->frame_tail
];
1771 dma_ptr
= dma_map_single(dev
, first_buffer
, first_buffer_length
,
1773 if (dma_mapping_error(dev
, dma_ptr
))
1776 tx_descriptor
->data1
= cpu_to_le32(DMA_ADDR_LOW32(dma_ptr
));
1777 tx_descriptor
->data2
= cpu_to_le32(DMA_ADDR_HIGH32(dma_ptr
));
1778 tx_descriptor
->data3
= cpu_to_le32((frame_length
<< 16) &
1779 TX_DESC_DATA3_FRAME_LENGTH_MSS_MASK_
);
1781 buffer_info
->skb
= NULL
;
1782 buffer_info
->dma_ptr
= dma_ptr
;
1783 buffer_info
->buffer_length
= first_buffer_length
;
1784 buffer_info
->flags
|= TX_BUFFER_INFO_FLAG_ACTIVE
;
1786 tx
->frame_data0
= (first_buffer_length
&
1787 TX_DESC_DATA0_BUF_LENGTH_MASK_
) |
1788 TX_DESC_DATA0_DTYPE_DATA_
|
1792 tx
->frame_data0
|= TX_DESC_DATA0_TSE_
;
1795 tx
->frame_data0
|= TX_DESC_DATA0_ICE_
|
1796 TX_DESC_DATA0_IPE_
|
1799 /* data0 will be programmed in one of other frame assembler functions */
1803 static void lan743x_tx_frame_add_lso(struct lan743x_tx
*tx
,
1804 unsigned int frame_length
,
1807 /* called only from within lan743x_tx_xmit_frame.
1808 * assuming tx->ring_lock has already been acquired.
1810 struct lan743x_tx_descriptor
*tx_descriptor
= NULL
;
1811 struct lan743x_tx_buffer_info
*buffer_info
= NULL
;
1813 /* wrap up previous descriptor */
1814 tx
->frame_data0
|= TX_DESC_DATA0_EXT_
;
1815 if (nr_frags
<= 0) {
1816 tx
->frame_data0
|= TX_DESC_DATA0_LS_
;
1817 tx
->frame_data0
|= TX_DESC_DATA0_IOC_
;
1819 tx_descriptor
= &tx
->ring_cpu_ptr
[tx
->frame_tail
];
1820 tx_descriptor
->data0
= cpu_to_le32(tx
->frame_data0
);
1822 /* move to next descriptor */
1823 tx
->frame_tail
= lan743x_tx_next_index(tx
, tx
->frame_tail
);
1824 tx_descriptor
= &tx
->ring_cpu_ptr
[tx
->frame_tail
];
1825 buffer_info
= &tx
->buffer_info
[tx
->frame_tail
];
1827 /* add extension descriptor */
1828 tx_descriptor
->data1
= 0;
1829 tx_descriptor
->data2
= 0;
1830 tx_descriptor
->data3
= 0;
1832 buffer_info
->skb
= NULL
;
1833 buffer_info
->dma_ptr
= 0;
1834 buffer_info
->buffer_length
= 0;
1835 buffer_info
->flags
|= TX_BUFFER_INFO_FLAG_ACTIVE
;
1837 tx
->frame_data0
= (frame_length
& TX_DESC_DATA0_EXT_PAY_LENGTH_MASK_
) |
1838 TX_DESC_DATA0_DTYPE_EXT_
|
1839 TX_DESC_DATA0_EXT_LSO_
;
1841 /* data0 will be programmed in one of other frame assembler functions */
1844 static int lan743x_tx_frame_add_fragment(struct lan743x_tx
*tx
,
1845 const skb_frag_t
*fragment
,
1846 unsigned int frame_length
)
1848 /* called only from within lan743x_tx_xmit_frame
1849 * assuming tx->ring_lock has already been acquired
1851 struct lan743x_tx_descriptor
*tx_descriptor
= NULL
;
1852 struct lan743x_tx_buffer_info
*buffer_info
= NULL
;
1853 struct lan743x_adapter
*adapter
= tx
->adapter
;
1854 struct device
*dev
= &adapter
->pdev
->dev
;
1855 unsigned int fragment_length
= 0;
1858 fragment_length
= skb_frag_size(fragment
);
1859 if (!fragment_length
)
1862 /* wrap up previous descriptor */
1863 tx_descriptor
= &tx
->ring_cpu_ptr
[tx
->frame_tail
];
1864 tx_descriptor
->data0
= cpu_to_le32(tx
->frame_data0
);
1866 /* move to next descriptor */
1867 tx
->frame_tail
= lan743x_tx_next_index(tx
, tx
->frame_tail
);
1868 tx_descriptor
= &tx
->ring_cpu_ptr
[tx
->frame_tail
];
1869 buffer_info
= &tx
->buffer_info
[tx
->frame_tail
];
1870 dma_ptr
= skb_frag_dma_map(dev
, fragment
,
1873 if (dma_mapping_error(dev
, dma_ptr
)) {
1876 /* cleanup all previously setup descriptors */
1877 desc_index
= tx
->frame_first
;
1878 while (desc_index
!= tx
->frame_tail
) {
1879 lan743x_tx_release_desc(tx
, desc_index
, true);
1880 desc_index
= lan743x_tx_next_index(tx
, desc_index
);
1883 tx
->frame_flags
&= ~TX_FRAME_FLAG_IN_PROGRESS
;
1884 tx
->frame_first
= 0;
1885 tx
->frame_data0
= 0;
1890 tx_descriptor
->data1
= cpu_to_le32(DMA_ADDR_LOW32(dma_ptr
));
1891 tx_descriptor
->data2
= cpu_to_le32(DMA_ADDR_HIGH32(dma_ptr
));
1892 tx_descriptor
->data3
= cpu_to_le32((frame_length
<< 16) &
1893 TX_DESC_DATA3_FRAME_LENGTH_MSS_MASK_
);
1895 buffer_info
->skb
= NULL
;
1896 buffer_info
->dma_ptr
= dma_ptr
;
1897 buffer_info
->buffer_length
= fragment_length
;
1898 buffer_info
->flags
|= TX_BUFFER_INFO_FLAG_ACTIVE
;
1899 buffer_info
->flags
|= TX_BUFFER_INFO_FLAG_SKB_FRAGMENT
;
1901 tx
->frame_data0
= (fragment_length
& TX_DESC_DATA0_BUF_LENGTH_MASK_
) |
1902 TX_DESC_DATA0_DTYPE_DATA_
|
1905 /* data0 will be programmed in one of other frame assembler functions */
1909 static void lan743x_tx_frame_end(struct lan743x_tx
*tx
,
1910 struct sk_buff
*skb
,
1914 /* called only from within lan743x_tx_xmit_frame
1915 * assuming tx->ring_lock has already been acquired
1917 struct lan743x_tx_descriptor
*tx_descriptor
= NULL
;
1918 struct lan743x_tx_buffer_info
*buffer_info
= NULL
;
1919 struct lan743x_adapter
*adapter
= tx
->adapter
;
1920 u32 tx_tail_flags
= 0;
1922 /* wrap up previous descriptor */
1923 if ((tx
->frame_data0
& TX_DESC_DATA0_DTYPE_MASK_
) ==
1924 TX_DESC_DATA0_DTYPE_DATA_
) {
1925 tx
->frame_data0
|= TX_DESC_DATA0_LS_
;
1926 tx
->frame_data0
|= TX_DESC_DATA0_IOC_
;
1929 tx_descriptor
= &tx
->ring_cpu_ptr
[tx
->frame_tail
];
1930 buffer_info
= &tx
->buffer_info
[tx
->frame_tail
];
1931 buffer_info
->skb
= skb
;
1933 buffer_info
->flags
|= TX_BUFFER_INFO_FLAG_TIMESTAMP_REQUESTED
;
1935 buffer_info
->flags
|= TX_BUFFER_INFO_FLAG_IGNORE_SYNC
;
1937 tx_descriptor
->data0
= cpu_to_le32(tx
->frame_data0
);
1938 tx
->frame_tail
= lan743x_tx_next_index(tx
, tx
->frame_tail
);
1939 tx
->last_tail
= tx
->frame_tail
;
1943 if (tx
->vector_flags
& LAN743X_VECTOR_FLAG_VECTOR_ENABLE_AUTO_SET
)
1944 tx_tail_flags
|= TX_TAIL_SET_TOP_INT_VEC_EN_
;
1945 if (tx
->vector_flags
& LAN743X_VECTOR_FLAG_SOURCE_ENABLE_AUTO_SET
)
1946 tx_tail_flags
|= TX_TAIL_SET_DMAC_INT_EN_
|
1947 TX_TAIL_SET_TOP_INT_EN_
;
1949 lan743x_csr_write(adapter
, TX_TAIL(tx
->channel_number
),
1950 tx_tail_flags
| tx
->frame_tail
);
1951 tx
->frame_flags
&= ~TX_FRAME_FLAG_IN_PROGRESS
;
1954 static netdev_tx_t
lan743x_tx_xmit_frame(struct lan743x_tx
*tx
,
1955 struct sk_buff
*skb
)
1957 int required_number_of_descriptors
= 0;
1958 unsigned int start_frame_length
= 0;
1959 netdev_tx_t retval
= NETDEV_TX_OK
;
1960 unsigned int frame_length
= 0;
1961 unsigned int head_length
= 0;
1962 unsigned long irq_flags
= 0;
1963 bool do_timestamp
= false;
1964 bool ignore_sync
= false;
1965 struct netdev_queue
*txq
;
1970 required_number_of_descriptors
= lan743x_tx_get_desc_cnt(tx
, skb
);
1972 spin_lock_irqsave(&tx
->ring_lock
, irq_flags
);
1973 if (required_number_of_descriptors
>
1974 lan743x_tx_get_avail_desc(tx
)) {
1975 if (required_number_of_descriptors
> (tx
->ring_size
- 1)) {
1976 dev_kfree_skb_irq(skb
);
1978 /* save how many descriptors we needed to restart the queue */
1979 tx
->rqd_descriptors
= required_number_of_descriptors
;
1980 retval
= NETDEV_TX_BUSY
;
1981 txq
= netdev_get_tx_queue(tx
->adapter
->netdev
,
1982 tx
->channel_number
);
1983 netif_tx_stop_queue(txq
);
1988 /* space available, transmit skb */
1989 if ((skb_shinfo(skb
)->tx_flags
& SKBTX_HW_TSTAMP
) &&
1990 (tx
->ts_flags
& TX_TS_FLAG_TIMESTAMPING_ENABLED
) &&
1991 (lan743x_ptp_request_tx_timestamp(tx
->adapter
))) {
1992 skb_shinfo(skb
)->tx_flags
|= SKBTX_IN_PROGRESS
;
1993 do_timestamp
= true;
1994 if (tx
->ts_flags
& TX_TS_FLAG_ONE_STEP_SYNC
)
1997 head_length
= skb_headlen(skb
);
1998 frame_length
= skb_pagelen(skb
);
1999 nr_frags
= skb_shinfo(skb
)->nr_frags
;
2000 start_frame_length
= frame_length
;
2001 gso
= skb_is_gso(skb
);
2003 start_frame_length
= max(skb_shinfo(skb
)->gso_size
,
2007 if (lan743x_tx_frame_start(tx
,
2008 skb
->data
, head_length
,
2011 skb
->ip_summed
== CHECKSUM_PARTIAL
)) {
2012 dev_kfree_skb_irq(skb
);
2018 lan743x_tx_frame_add_lso(tx
, frame_length
, nr_frags
);
2023 for (j
= 0; j
< nr_frags
; j
++) {
2024 const skb_frag_t
*frag
= &(skb_shinfo(skb
)->frags
[j
]);
2026 if (lan743x_tx_frame_add_fragment(tx
, frag
, frame_length
)) {
2027 /* upon error no need to call
2028 * lan743x_tx_frame_end
2029 * frame assembler clean up was performed inside
2030 * lan743x_tx_frame_add_fragment
2032 dev_kfree_skb_irq(skb
);
2038 lan743x_tx_frame_end(tx
, skb
, do_timestamp
, ignore_sync
);
2041 spin_unlock_irqrestore(&tx
->ring_lock
, irq_flags
);
2045 static int lan743x_tx_napi_poll(struct napi_struct
*napi
, int weight
)
2047 struct lan743x_tx
*tx
= container_of(napi
, struct lan743x_tx
, napi
);
2048 struct lan743x_adapter
*adapter
= tx
->adapter
;
2049 unsigned long irq_flags
= 0;
2050 struct netdev_queue
*txq
;
2053 ioc_bit
= DMAC_INT_BIT_TX_IOC_(tx
->channel_number
);
2054 lan743x_csr_read(adapter
, DMAC_INT_STS
);
2055 if (tx
->vector_flags
& LAN743X_VECTOR_FLAG_SOURCE_STATUS_W2C
)
2056 lan743x_csr_write(adapter
, DMAC_INT_STS
, ioc_bit
);
2057 spin_lock_irqsave(&tx
->ring_lock
, irq_flags
);
2059 /* clean up tx ring */
2060 lan743x_tx_release_completed_descriptors(tx
);
2061 txq
= netdev_get_tx_queue(adapter
->netdev
, tx
->channel_number
);
2062 if (netif_tx_queue_stopped(txq
)) {
2063 if (tx
->rqd_descriptors
) {
2064 if (tx
->rqd_descriptors
<=
2065 lan743x_tx_get_avail_desc(tx
)) {
2066 tx
->rqd_descriptors
= 0;
2067 netif_tx_wake_queue(txq
);
2070 netif_tx_wake_queue(txq
);
2073 spin_unlock_irqrestore(&tx
->ring_lock
, irq_flags
);
2075 if (!napi_complete(napi
))
2079 lan743x_csr_write(adapter
, INT_EN_SET
,
2080 INT_BIT_DMA_TX_(tx
->channel_number
));
2081 lan743x_csr_read(adapter
, INT_STS
);
2087 static void lan743x_tx_ring_cleanup(struct lan743x_tx
*tx
)
2089 if (tx
->head_cpu_ptr
) {
2090 dma_free_coherent(&tx
->adapter
->pdev
->dev
,
2091 sizeof(*tx
->head_cpu_ptr
), tx
->head_cpu_ptr
,
2093 tx
->head_cpu_ptr
= NULL
;
2094 tx
->head_dma_ptr
= 0;
2096 kfree(tx
->buffer_info
);
2097 tx
->buffer_info
= NULL
;
2099 if (tx
->ring_cpu_ptr
) {
2100 dma_free_coherent(&tx
->adapter
->pdev
->dev
,
2101 tx
->ring_allocation_size
, tx
->ring_cpu_ptr
,
2103 tx
->ring_allocation_size
= 0;
2104 tx
->ring_cpu_ptr
= NULL
;
2105 tx
->ring_dma_ptr
= 0;
2110 static int lan743x_tx_ring_init(struct lan743x_tx
*tx
)
2112 size_t ring_allocation_size
= 0;
2113 void *cpu_ptr
= NULL
;
2117 tx
->ring_size
= LAN743X_TX_RING_SIZE
;
2118 if (tx
->ring_size
& ~TX_CFG_B_TX_RING_LEN_MASK_
) {
2122 if (dma_set_mask_and_coherent(&tx
->adapter
->pdev
->dev
,
2123 DMA_BIT_MASK(64))) {
2124 dev_warn(&tx
->adapter
->pdev
->dev
,
2125 "lan743x_: No suitable DMA available\n");
2129 ring_allocation_size
= ALIGN(tx
->ring_size
*
2130 sizeof(struct lan743x_tx_descriptor
),
2133 cpu_ptr
= dma_alloc_coherent(&tx
->adapter
->pdev
->dev
,
2134 ring_allocation_size
, &dma_ptr
, GFP_KERNEL
);
2140 tx
->ring_allocation_size
= ring_allocation_size
;
2141 tx
->ring_cpu_ptr
= (struct lan743x_tx_descriptor
*)cpu_ptr
;
2142 tx
->ring_dma_ptr
= dma_ptr
;
2144 cpu_ptr
= kcalloc(tx
->ring_size
, sizeof(*tx
->buffer_info
), GFP_KERNEL
);
2149 tx
->buffer_info
= (struct lan743x_tx_buffer_info
*)cpu_ptr
;
2151 cpu_ptr
= dma_alloc_coherent(&tx
->adapter
->pdev
->dev
,
2152 sizeof(*tx
->head_cpu_ptr
), &dma_ptr
,
2159 tx
->head_cpu_ptr
= cpu_ptr
;
2160 tx
->head_dma_ptr
= dma_ptr
;
2161 if (tx
->head_dma_ptr
& 0x3) {
2169 lan743x_tx_ring_cleanup(tx
);
2173 static void lan743x_tx_close(struct lan743x_tx
*tx
)
2175 struct lan743x_adapter
*adapter
= tx
->adapter
;
2177 lan743x_csr_write(adapter
,
2179 DMAC_CMD_STOP_T_(tx
->channel_number
));
2180 lan743x_dmac_tx_wait_till_stopped(adapter
, tx
->channel_number
);
2182 lan743x_csr_write(adapter
,
2184 DMAC_INT_BIT_TX_IOC_(tx
->channel_number
));
2185 lan743x_csr_write(adapter
, INT_EN_CLR
,
2186 INT_BIT_DMA_TX_(tx
->channel_number
));
2187 napi_disable(&tx
->napi
);
2188 netif_napi_del(&tx
->napi
);
2190 lan743x_csr_write(adapter
, FCT_TX_CTL
,
2191 FCT_TX_CTL_DIS_(tx
->channel_number
));
2192 lan743x_csr_wait_for_bit(adapter
, FCT_TX_CTL
,
2193 FCT_TX_CTL_EN_(tx
->channel_number
),
2194 0, 1000, 20000, 100);
2196 lan743x_tx_release_all_descriptors(tx
);
2198 tx
->rqd_descriptors
= 0;
2200 lan743x_tx_ring_cleanup(tx
);
2203 static int lan743x_tx_open(struct lan743x_tx
*tx
)
2205 struct lan743x_adapter
*adapter
= NULL
;
2209 adapter
= tx
->adapter
;
2210 ret
= lan743x_tx_ring_init(tx
);
2214 /* initialize fifo */
2215 lan743x_csr_write(adapter
, FCT_TX_CTL
,
2216 FCT_TX_CTL_RESET_(tx
->channel_number
));
2217 lan743x_csr_wait_for_bit(adapter
, FCT_TX_CTL
,
2218 FCT_TX_CTL_RESET_(tx
->channel_number
),
2219 0, 1000, 20000, 100);
2222 lan743x_csr_write(adapter
, FCT_TX_CTL
,
2223 FCT_TX_CTL_EN_(tx
->channel_number
));
2225 /* reset tx channel */
2226 lan743x_csr_write(adapter
, DMAC_CMD
,
2227 DMAC_CMD_TX_SWR_(tx
->channel_number
));
2228 lan743x_csr_wait_for_bit(adapter
, DMAC_CMD
,
2229 DMAC_CMD_TX_SWR_(tx
->channel_number
),
2230 0, 1000, 20000, 100);
2232 /* Write TX_BASE_ADDR */
2233 lan743x_csr_write(adapter
,
2234 TX_BASE_ADDRH(tx
->channel_number
),
2235 DMA_ADDR_HIGH32(tx
->ring_dma_ptr
));
2236 lan743x_csr_write(adapter
,
2237 TX_BASE_ADDRL(tx
->channel_number
),
2238 DMA_ADDR_LOW32(tx
->ring_dma_ptr
));
2240 /* Write TX_CFG_B */
2241 data
= lan743x_csr_read(adapter
, TX_CFG_B(tx
->channel_number
));
2242 data
&= ~TX_CFG_B_TX_RING_LEN_MASK_
;
2243 data
|= ((tx
->ring_size
) & TX_CFG_B_TX_RING_LEN_MASK_
);
2244 if (!(adapter
->csr
.flags
& LAN743X_CSR_FLAG_IS_A0
))
2245 data
|= TX_CFG_B_TDMABL_512_
;
2246 lan743x_csr_write(adapter
, TX_CFG_B(tx
->channel_number
), data
);
2248 /* Write TX_CFG_A */
2249 data
= TX_CFG_A_TX_TMR_HPWB_SEL_IOC_
| TX_CFG_A_TX_HP_WB_EN_
;
2250 if (!(adapter
->csr
.flags
& LAN743X_CSR_FLAG_IS_A0
)) {
2251 data
|= TX_CFG_A_TX_HP_WB_ON_INT_TMR_
;
2252 data
|= TX_CFG_A_TX_PF_THRES_SET_(0x10);
2253 data
|= TX_CFG_A_TX_PF_PRI_THRES_SET_(0x04);
2254 data
|= TX_CFG_A_TX_HP_WB_THRES_SET_(0x07);
2256 lan743x_csr_write(adapter
, TX_CFG_A(tx
->channel_number
), data
);
2258 /* Write TX_HEAD_WRITEBACK_ADDR */
2259 lan743x_csr_write(adapter
,
2260 TX_HEAD_WRITEBACK_ADDRH(tx
->channel_number
),
2261 DMA_ADDR_HIGH32(tx
->head_dma_ptr
));
2262 lan743x_csr_write(adapter
,
2263 TX_HEAD_WRITEBACK_ADDRL(tx
->channel_number
),
2264 DMA_ADDR_LOW32(tx
->head_dma_ptr
));
2267 tx
->last_head
= lan743x_csr_read(adapter
, TX_HEAD(tx
->channel_number
));
2271 lan743x_csr_write(adapter
, TX_TAIL(tx
->channel_number
),
2272 (u32
)(tx
->last_tail
));
2273 tx
->vector_flags
= lan743x_intr_get_vector_flags(adapter
,
2275 (tx
->channel_number
));
2276 netif_napi_add_tx_weight(adapter
->netdev
,
2277 &tx
->napi
, lan743x_tx_napi_poll
,
2279 napi_enable(&tx
->napi
);
2282 if (tx
->vector_flags
& LAN743X_VECTOR_FLAG_SOURCE_ENABLE_AUTO_CLEAR
)
2283 data
|= TX_CFG_C_TX_TOP_INT_EN_AUTO_CLR_
;
2284 if (tx
->vector_flags
& LAN743X_VECTOR_FLAG_SOURCE_STATUS_AUTO_CLEAR
)
2285 data
|= TX_CFG_C_TX_DMA_INT_STS_AUTO_CLR_
;
2286 if (tx
->vector_flags
& LAN743X_VECTOR_FLAG_SOURCE_STATUS_R2C
)
2287 data
|= TX_CFG_C_TX_INT_STS_R2C_MODE_MASK_
;
2288 if (tx
->vector_flags
& LAN743X_VECTOR_FLAG_SOURCE_ENABLE_R2C
)
2289 data
|= TX_CFG_C_TX_INT_EN_R2C_
;
2290 lan743x_csr_write(adapter
, TX_CFG_C(tx
->channel_number
), data
);
2292 if (!(tx
->vector_flags
& LAN743X_VECTOR_FLAG_SOURCE_ENABLE_AUTO_SET
))
2293 lan743x_csr_write(adapter
, INT_EN_SET
,
2294 INT_BIT_DMA_TX_(tx
->channel_number
));
2295 lan743x_csr_write(adapter
, DMAC_INT_EN_SET
,
2296 DMAC_INT_BIT_TX_IOC_(tx
->channel_number
));
2298 /* start dmac channel */
2299 lan743x_csr_write(adapter
, DMAC_CMD
,
2300 DMAC_CMD_START_T_(tx
->channel_number
));
2304 static int lan743x_rx_next_index(struct lan743x_rx
*rx
, int index
)
2306 return ((++index
) % rx
->ring_size
);
2309 static void lan743x_rx_update_tail(struct lan743x_rx
*rx
, int index
)
2311 /* update the tail once per 8 descriptors */
2312 if ((index
& 7) == 7)
2313 lan743x_csr_write(rx
->adapter
, RX_TAIL(rx
->channel_number
),
2317 static int lan743x_rx_init_ring_element(struct lan743x_rx
*rx
, int index
,
2320 struct net_device
*netdev
= rx
->adapter
->netdev
;
2321 struct device
*dev
= &rx
->adapter
->pdev
->dev
;
2322 struct lan743x_rx_buffer_info
*buffer_info
;
2323 unsigned int buffer_length
, used_length
;
2324 struct lan743x_rx_descriptor
*descriptor
;
2325 struct sk_buff
*skb
;
2328 buffer_length
= netdev
->mtu
+ ETH_HLEN
+ ETH_FCS_LEN
+ RX_HEAD_PADDING
;
2330 descriptor
= &rx
->ring_cpu_ptr
[index
];
2331 buffer_info
= &rx
->buffer_info
[index
];
2332 skb
= __netdev_alloc_skb(netdev
, buffer_length
, gfp
);
2335 dma_ptr
= dma_map_single(dev
, skb
->data
, buffer_length
, DMA_FROM_DEVICE
);
2336 if (dma_mapping_error(dev
, dma_ptr
)) {
2337 dev_kfree_skb_any(skb
);
2340 if (buffer_info
->dma_ptr
) {
2341 /* sync used area of buffer only */
2342 if (le32_to_cpu(descriptor
->data0
) & RX_DESC_DATA0_LS_
)
2343 /* frame length is valid only if LS bit is set.
2344 * it's a safe upper bound for the used area in this
2347 used_length
= min(RX_DESC_DATA0_FRAME_LENGTH_GET_
2348 (le32_to_cpu(descriptor
->data0
)),
2349 buffer_info
->buffer_length
);
2351 used_length
= buffer_info
->buffer_length
;
2352 dma_sync_single_for_cpu(dev
, buffer_info
->dma_ptr
,
2355 dma_unmap_single_attrs(dev
, buffer_info
->dma_ptr
,
2356 buffer_info
->buffer_length
,
2358 DMA_ATTR_SKIP_CPU_SYNC
);
2361 buffer_info
->skb
= skb
;
2362 buffer_info
->dma_ptr
= dma_ptr
;
2363 buffer_info
->buffer_length
= buffer_length
;
2364 descriptor
->data1
= cpu_to_le32(DMA_ADDR_LOW32(buffer_info
->dma_ptr
));
2365 descriptor
->data2
= cpu_to_le32(DMA_ADDR_HIGH32(buffer_info
->dma_ptr
));
2366 descriptor
->data3
= 0;
2367 descriptor
->data0
= cpu_to_le32((RX_DESC_DATA0_OWN_
|
2368 (buffer_length
& RX_DESC_DATA0_BUF_LENGTH_MASK_
)));
2369 lan743x_rx_update_tail(rx
, index
);
2374 static void lan743x_rx_reuse_ring_element(struct lan743x_rx
*rx
, int index
)
2376 struct lan743x_rx_buffer_info
*buffer_info
;
2377 struct lan743x_rx_descriptor
*descriptor
;
2379 descriptor
= &rx
->ring_cpu_ptr
[index
];
2380 buffer_info
= &rx
->buffer_info
[index
];
2382 descriptor
->data1
= cpu_to_le32(DMA_ADDR_LOW32(buffer_info
->dma_ptr
));
2383 descriptor
->data2
= cpu_to_le32(DMA_ADDR_HIGH32(buffer_info
->dma_ptr
));
2384 descriptor
->data3
= 0;
2385 descriptor
->data0
= cpu_to_le32((RX_DESC_DATA0_OWN_
|
2386 ((buffer_info
->buffer_length
) &
2387 RX_DESC_DATA0_BUF_LENGTH_MASK_
)));
2388 lan743x_rx_update_tail(rx
, index
);
2391 static void lan743x_rx_release_ring_element(struct lan743x_rx
*rx
, int index
)
2393 struct lan743x_rx_buffer_info
*buffer_info
;
2394 struct lan743x_rx_descriptor
*descriptor
;
2396 descriptor
= &rx
->ring_cpu_ptr
[index
];
2397 buffer_info
= &rx
->buffer_info
[index
];
2399 memset(descriptor
, 0, sizeof(*descriptor
));
2401 if (buffer_info
->dma_ptr
) {
2402 dma_unmap_single(&rx
->adapter
->pdev
->dev
,
2403 buffer_info
->dma_ptr
,
2404 buffer_info
->buffer_length
,
2406 buffer_info
->dma_ptr
= 0;
2409 if (buffer_info
->skb
) {
2410 dev_kfree_skb(buffer_info
->skb
);
2411 buffer_info
->skb
= NULL
;
2414 memset(buffer_info
, 0, sizeof(*buffer_info
));
2417 static struct sk_buff
*
2418 lan743x_rx_trim_skb(struct sk_buff
*skb
, int frame_length
)
2420 if (skb_linearize(skb
)) {
2421 dev_kfree_skb_irq(skb
);
2424 frame_length
= max_t(int, 0, frame_length
- ETH_FCS_LEN
);
2425 if (skb
->len
> frame_length
) {
2426 skb
->tail
-= skb
->len
- frame_length
;
2427 skb
->len
= frame_length
;
2432 static int lan743x_rx_process_buffer(struct lan743x_rx
*rx
)
2434 int current_head_index
= le32_to_cpu(*rx
->head_cpu_ptr
);
2435 struct lan743x_rx_descriptor
*descriptor
, *desc_ext
;
2436 struct net_device
*netdev
= rx
->adapter
->netdev
;
2437 int result
= RX_PROCESS_RESULT_NOTHING_TO_DO
;
2438 struct lan743x_rx_buffer_info
*buffer_info
;
2439 int frame_length
, buffer_length
;
2440 bool is_ice
, is_tce
, is_icsm
;
2441 int extension_index
= -1;
2442 bool is_last
, is_first
;
2443 struct sk_buff
*skb
;
2445 if (current_head_index
< 0 || current_head_index
>= rx
->ring_size
)
2448 if (rx
->last_head
< 0 || rx
->last_head
>= rx
->ring_size
)
2451 if (rx
->last_head
== current_head_index
)
2454 descriptor
= &rx
->ring_cpu_ptr
[rx
->last_head
];
2455 if (le32_to_cpu(descriptor
->data0
) & RX_DESC_DATA0_OWN_
)
2457 buffer_info
= &rx
->buffer_info
[rx
->last_head
];
2459 is_last
= le32_to_cpu(descriptor
->data0
) & RX_DESC_DATA0_LS_
;
2460 is_first
= le32_to_cpu(descriptor
->data0
) & RX_DESC_DATA0_FS_
;
2462 if (is_last
&& le32_to_cpu(descriptor
->data0
) & RX_DESC_DATA0_EXT_
) {
2463 /* extension is expected to follow */
2464 int index
= lan743x_rx_next_index(rx
, rx
->last_head
);
2466 if (index
== current_head_index
)
2467 /* extension not yet available */
2469 desc_ext
= &rx
->ring_cpu_ptr
[index
];
2470 if (le32_to_cpu(desc_ext
->data0
) & RX_DESC_DATA0_OWN_
)
2471 /* extension not yet available */
2473 if (!(le32_to_cpu(desc_ext
->data0
) & RX_DESC_DATA0_EXT_
))
2475 extension_index
= index
;
2478 /* Only the last buffer in a multi-buffer frame contains the total frame
2479 * length. The chip occasionally sends more buffers than strictly
2480 * required to reach the total frame length.
2481 * Handle this by adding all buffers to the skb in their entirety.
2482 * Once the real frame length is known, trim the skb.
2485 RX_DESC_DATA0_FRAME_LENGTH_GET_(le32_to_cpu(descriptor
->data0
));
2486 buffer_length
= buffer_info
->buffer_length
;
2487 is_ice
= le32_to_cpu(descriptor
->data1
) & RX_DESC_DATA1_STATUS_ICE_
;
2488 is_tce
= le32_to_cpu(descriptor
->data1
) & RX_DESC_DATA1_STATUS_TCE_
;
2489 is_icsm
= le32_to_cpu(descriptor
->data1
) & RX_DESC_DATA1_STATUS_ICSM_
;
2491 netdev_dbg(netdev
, "%s%schunk: %d/%d",
2492 is_first
? "first " : " ",
2493 is_last
? "last " : " ",
2494 frame_length
, buffer_length
);
2496 /* save existing skb, allocate new skb and map to dma */
2497 skb
= buffer_info
->skb
;
2498 if (lan743x_rx_init_ring_element(rx
, rx
->last_head
,
2499 GFP_ATOMIC
| GFP_DMA
)) {
2500 /* failed to allocate next skb.
2501 * Memory is very low.
2502 * Drop this packet and reuse buffer.
2504 lan743x_rx_reuse_ring_element(rx
, rx
->last_head
);
2505 /* drop packet that was being assembled */
2506 dev_kfree_skb_irq(rx
->skb_head
);
2507 rx
->skb_head
= NULL
;
2508 goto process_extension
;
2511 /* add buffers to skb via skb->frag_list */
2513 skb_reserve(skb
, RX_HEAD_PADDING
);
2514 skb_put(skb
, buffer_length
- RX_HEAD_PADDING
);
2516 dev_kfree_skb_irq(rx
->skb_head
);
2518 } else if (rx
->skb_head
) {
2519 skb_put(skb
, buffer_length
);
2520 if (skb_shinfo(rx
->skb_head
)->frag_list
)
2521 rx
->skb_tail
->next
= skb
;
2523 skb_shinfo(rx
->skb_head
)->frag_list
= skb
;
2525 rx
->skb_head
->len
+= skb
->len
;
2526 rx
->skb_head
->data_len
+= skb
->len
;
2527 rx
->skb_head
->truesize
+= skb
->truesize
;
2529 /* packet to assemble has already been dropped because one or
2530 * more of its buffers could not be allocated
2532 netdev_dbg(netdev
, "drop buffer intended for dropped packet");
2533 dev_kfree_skb_irq(skb
);
2537 if (extension_index
>= 0) {
2541 ts_sec
= le32_to_cpu(desc_ext
->data1
);
2542 ts_nsec
= (le32_to_cpu(desc_ext
->data2
) &
2543 RX_DESC_DATA2_TS_NS_MASK_
);
2545 skb_hwtstamps(rx
->skb_head
)->hwtstamp
=
2546 ktime_set(ts_sec
, ts_nsec
);
2547 lan743x_rx_reuse_ring_element(rx
, extension_index
);
2548 rx
->last_head
= extension_index
;
2549 netdev_dbg(netdev
, "process extension");
2552 if (is_last
&& rx
->skb_head
)
2553 rx
->skb_head
= lan743x_rx_trim_skb(rx
->skb_head
, frame_length
);
2555 if (is_last
&& rx
->skb_head
) {
2556 rx
->skb_head
->protocol
= eth_type_trans(rx
->skb_head
,
2557 rx
->adapter
->netdev
);
2558 if (rx
->adapter
->netdev
->features
& NETIF_F_RXCSUM
) {
2559 if (!is_ice
&& !is_tce
&& !is_icsm
)
2560 skb
->ip_summed
= CHECKSUM_UNNECESSARY
;
2562 netdev_dbg(netdev
, "sending %d byte frame to OS",
2564 napi_gro_receive(&rx
->napi
, rx
->skb_head
);
2565 rx
->skb_head
= NULL
;
2569 /* push tail and head forward */
2570 rx
->last_tail
= rx
->last_head
;
2571 rx
->last_head
= lan743x_rx_next_index(rx
, rx
->last_head
);
2572 result
= RX_PROCESS_RESULT_BUFFER_RECEIVED
;
2577 static int lan743x_rx_napi_poll(struct napi_struct
*napi
, int weight
)
2579 struct lan743x_rx
*rx
= container_of(napi
, struct lan743x_rx
, napi
);
2580 struct lan743x_adapter
*adapter
= rx
->adapter
;
2581 int result
= RX_PROCESS_RESULT_NOTHING_TO_DO
;
2582 u32 rx_tail_flags
= 0;
2585 if (rx
->vector_flags
& LAN743X_VECTOR_FLAG_SOURCE_STATUS_W2C
) {
2586 /* clear int status bit before reading packet */
2587 lan743x_csr_write(adapter
, DMAC_INT_STS
,
2588 DMAC_INT_BIT_RXFRM_(rx
->channel_number
));
2590 for (count
= 0; count
< weight
; count
++) {
2591 result
= lan743x_rx_process_buffer(rx
);
2592 if (result
== RX_PROCESS_RESULT_NOTHING_TO_DO
)
2595 rx
->frame_count
+= count
;
2596 if (count
== weight
|| result
== RX_PROCESS_RESULT_BUFFER_RECEIVED
)
2599 if (!napi_complete_done(napi
, count
))
2602 /* re-arm interrupts, must write to rx tail on some chip variants */
2603 if (rx
->vector_flags
& LAN743X_VECTOR_FLAG_VECTOR_ENABLE_AUTO_SET
)
2604 rx_tail_flags
|= RX_TAIL_SET_TOP_INT_VEC_EN_
;
2605 if (rx
->vector_flags
& LAN743X_VECTOR_FLAG_SOURCE_ENABLE_AUTO_SET
) {
2606 rx_tail_flags
|= RX_TAIL_SET_TOP_INT_EN_
;
2608 lan743x_csr_write(adapter
, INT_EN_SET
,
2609 INT_BIT_DMA_RX_(rx
->channel_number
));
2613 lan743x_csr_write(adapter
, RX_TAIL(rx
->channel_number
),
2614 rx_tail_flags
| rx
->last_tail
);
2619 static void lan743x_rx_ring_cleanup(struct lan743x_rx
*rx
)
2621 if (rx
->buffer_info
&& rx
->ring_cpu_ptr
) {
2624 for (index
= 0; index
< rx
->ring_size
; index
++)
2625 lan743x_rx_release_ring_element(rx
, index
);
2628 if (rx
->head_cpu_ptr
) {
2629 dma_free_coherent(&rx
->adapter
->pdev
->dev
,
2630 sizeof(*rx
->head_cpu_ptr
), rx
->head_cpu_ptr
,
2632 rx
->head_cpu_ptr
= NULL
;
2633 rx
->head_dma_ptr
= 0;
2636 kfree(rx
->buffer_info
);
2637 rx
->buffer_info
= NULL
;
2639 if (rx
->ring_cpu_ptr
) {
2640 dma_free_coherent(&rx
->adapter
->pdev
->dev
,
2641 rx
->ring_allocation_size
, rx
->ring_cpu_ptr
,
2643 rx
->ring_allocation_size
= 0;
2644 rx
->ring_cpu_ptr
= NULL
;
2645 rx
->ring_dma_ptr
= 0;
2652 static int lan743x_rx_ring_init(struct lan743x_rx
*rx
)
2654 size_t ring_allocation_size
= 0;
2655 dma_addr_t dma_ptr
= 0;
2656 void *cpu_ptr
= NULL
;
2660 rx
->ring_size
= LAN743X_RX_RING_SIZE
;
2661 if (rx
->ring_size
<= 1) {
2665 if (rx
->ring_size
& ~RX_CFG_B_RX_RING_LEN_MASK_
) {
2669 if (dma_set_mask_and_coherent(&rx
->adapter
->pdev
->dev
,
2670 DMA_BIT_MASK(64))) {
2671 dev_warn(&rx
->adapter
->pdev
->dev
,
2672 "lan743x_: No suitable DMA available\n");
2676 ring_allocation_size
= ALIGN(rx
->ring_size
*
2677 sizeof(struct lan743x_rx_descriptor
),
2680 cpu_ptr
= dma_alloc_coherent(&rx
->adapter
->pdev
->dev
,
2681 ring_allocation_size
, &dma_ptr
, GFP_KERNEL
);
2686 rx
->ring_allocation_size
= ring_allocation_size
;
2687 rx
->ring_cpu_ptr
= (struct lan743x_rx_descriptor
*)cpu_ptr
;
2688 rx
->ring_dma_ptr
= dma_ptr
;
2690 cpu_ptr
= kcalloc(rx
->ring_size
, sizeof(*rx
->buffer_info
),
2696 rx
->buffer_info
= (struct lan743x_rx_buffer_info
*)cpu_ptr
;
2698 cpu_ptr
= dma_alloc_coherent(&rx
->adapter
->pdev
->dev
,
2699 sizeof(*rx
->head_cpu_ptr
), &dma_ptr
,
2706 rx
->head_cpu_ptr
= cpu_ptr
;
2707 rx
->head_dma_ptr
= dma_ptr
;
2708 if (rx
->head_dma_ptr
& 0x3) {
2714 for (index
= 0; index
< rx
->ring_size
; index
++) {
2715 ret
= lan743x_rx_init_ring_element(rx
, index
, GFP_KERNEL
);
2722 netif_warn(rx
->adapter
, ifup
, rx
->adapter
->netdev
,
2723 "Error allocating memory for LAN743x\n");
2725 lan743x_rx_ring_cleanup(rx
);
2729 static void lan743x_rx_close(struct lan743x_rx
*rx
)
2731 struct lan743x_adapter
*adapter
= rx
->adapter
;
2733 lan743x_csr_write(adapter
, FCT_RX_CTL
,
2734 FCT_RX_CTL_DIS_(rx
->channel_number
));
2735 lan743x_csr_wait_for_bit(adapter
, FCT_RX_CTL
,
2736 FCT_RX_CTL_EN_(rx
->channel_number
),
2737 0, 1000, 20000, 100);
2739 lan743x_csr_write(adapter
, DMAC_CMD
,
2740 DMAC_CMD_STOP_R_(rx
->channel_number
));
2741 lan743x_dmac_rx_wait_till_stopped(adapter
, rx
->channel_number
);
2743 lan743x_csr_write(adapter
, DMAC_INT_EN_CLR
,
2744 DMAC_INT_BIT_RXFRM_(rx
->channel_number
));
2745 lan743x_csr_write(adapter
, INT_EN_CLR
,
2746 INT_BIT_DMA_RX_(rx
->channel_number
));
2747 napi_disable(&rx
->napi
);
2749 netif_napi_del(&rx
->napi
);
2751 lan743x_rx_ring_cleanup(rx
);
2754 static int lan743x_rx_open(struct lan743x_rx
*rx
)
2756 struct lan743x_adapter
*adapter
= rx
->adapter
;
2760 rx
->frame_count
= 0;
2761 ret
= lan743x_rx_ring_init(rx
);
2765 netif_napi_add(adapter
->netdev
, &rx
->napi
, lan743x_rx_napi_poll
);
2767 lan743x_csr_write(adapter
, DMAC_CMD
,
2768 DMAC_CMD_RX_SWR_(rx
->channel_number
));
2769 lan743x_csr_wait_for_bit(adapter
, DMAC_CMD
,
2770 DMAC_CMD_RX_SWR_(rx
->channel_number
),
2771 0, 1000, 20000, 100);
2773 /* set ring base address */
2774 lan743x_csr_write(adapter
,
2775 RX_BASE_ADDRH(rx
->channel_number
),
2776 DMA_ADDR_HIGH32(rx
->ring_dma_ptr
));
2777 lan743x_csr_write(adapter
,
2778 RX_BASE_ADDRL(rx
->channel_number
),
2779 DMA_ADDR_LOW32(rx
->ring_dma_ptr
));
2781 /* set rx write back address */
2782 lan743x_csr_write(adapter
,
2783 RX_HEAD_WRITEBACK_ADDRH(rx
->channel_number
),
2784 DMA_ADDR_HIGH32(rx
->head_dma_ptr
));
2785 lan743x_csr_write(adapter
,
2786 RX_HEAD_WRITEBACK_ADDRL(rx
->channel_number
),
2787 DMA_ADDR_LOW32(rx
->head_dma_ptr
));
2788 data
= RX_CFG_A_RX_HP_WB_EN_
;
2789 if (!(adapter
->csr
.flags
& LAN743X_CSR_FLAG_IS_A0
)) {
2790 data
|= (RX_CFG_A_RX_WB_ON_INT_TMR_
|
2791 RX_CFG_A_RX_WB_THRES_SET_(0x7) |
2792 RX_CFG_A_RX_PF_THRES_SET_(16) |
2793 RX_CFG_A_RX_PF_PRI_THRES_SET_(4));
2797 lan743x_csr_write(adapter
,
2798 RX_CFG_A(rx
->channel_number
), data
);
2801 data
= lan743x_csr_read(adapter
, RX_CFG_B(rx
->channel_number
));
2802 data
&= ~RX_CFG_B_RX_PAD_MASK_
;
2803 if (!RX_HEAD_PADDING
)
2804 data
|= RX_CFG_B_RX_PAD_0_
;
2806 data
|= RX_CFG_B_RX_PAD_2_
;
2807 data
&= ~RX_CFG_B_RX_RING_LEN_MASK_
;
2808 data
|= ((rx
->ring_size
) & RX_CFG_B_RX_RING_LEN_MASK_
);
2809 if (!(adapter
->csr
.flags
& LAN743X_CSR_FLAG_IS_A0
))
2810 data
|= RX_CFG_B_RDMABL_512_
;
2812 lan743x_csr_write(adapter
, RX_CFG_B(rx
->channel_number
), data
);
2813 rx
->vector_flags
= lan743x_intr_get_vector_flags(adapter
,
2815 (rx
->channel_number
));
2819 if (rx
->vector_flags
& LAN743X_VECTOR_FLAG_SOURCE_ENABLE_AUTO_CLEAR
)
2820 data
|= RX_CFG_C_RX_TOP_INT_EN_AUTO_CLR_
;
2821 if (rx
->vector_flags
& LAN743X_VECTOR_FLAG_SOURCE_STATUS_AUTO_CLEAR
)
2822 data
|= RX_CFG_C_RX_DMA_INT_STS_AUTO_CLR_
;
2823 if (rx
->vector_flags
& LAN743X_VECTOR_FLAG_SOURCE_STATUS_R2C
)
2824 data
|= RX_CFG_C_RX_INT_STS_R2C_MODE_MASK_
;
2825 if (rx
->vector_flags
& LAN743X_VECTOR_FLAG_SOURCE_ENABLE_R2C
)
2826 data
|= RX_CFG_C_RX_INT_EN_R2C_
;
2827 lan743x_csr_write(adapter
, RX_CFG_C(rx
->channel_number
), data
);
2829 rx
->last_tail
= ((u32
)(rx
->ring_size
- 1));
2830 lan743x_csr_write(adapter
, RX_TAIL(rx
->channel_number
),
2832 rx
->last_head
= lan743x_csr_read(adapter
, RX_HEAD(rx
->channel_number
));
2833 if (rx
->last_head
) {
2838 napi_enable(&rx
->napi
);
2840 lan743x_csr_write(adapter
, INT_EN_SET
,
2841 INT_BIT_DMA_RX_(rx
->channel_number
));
2842 lan743x_csr_write(adapter
, DMAC_INT_STS
,
2843 DMAC_INT_BIT_RXFRM_(rx
->channel_number
));
2844 lan743x_csr_write(adapter
, DMAC_INT_EN_SET
,
2845 DMAC_INT_BIT_RXFRM_(rx
->channel_number
));
2846 lan743x_csr_write(adapter
, DMAC_CMD
,
2847 DMAC_CMD_START_R_(rx
->channel_number
));
2849 /* initialize fifo */
2850 lan743x_csr_write(adapter
, FCT_RX_CTL
,
2851 FCT_RX_CTL_RESET_(rx
->channel_number
));
2852 lan743x_csr_wait_for_bit(adapter
, FCT_RX_CTL
,
2853 FCT_RX_CTL_RESET_(rx
->channel_number
),
2854 0, 1000, 20000, 100);
2855 lan743x_csr_write(adapter
, FCT_FLOW(rx
->channel_number
),
2856 FCT_FLOW_CTL_REQ_EN_
|
2857 FCT_FLOW_CTL_ON_THRESHOLD_SET_(0x2A) |
2858 FCT_FLOW_CTL_OFF_THRESHOLD_SET_(0xA));
2861 lan743x_csr_write(adapter
, FCT_RX_CTL
,
2862 FCT_RX_CTL_EN_(rx
->channel_number
));
2866 netif_napi_del(&rx
->napi
);
2867 lan743x_rx_ring_cleanup(rx
);
2873 static int lan743x_phylink_sgmii_config(struct lan743x_adapter
*adapter
)
2878 ret
= lan743x_get_lsd(SPEED_1000
, DUPLEX_FULL
,
2879 MASTER_SLAVE_STATE_MASTER
);
2881 netif_err(adapter
, drv
, adapter
->netdev
,
2882 "error %d link-speed-duplex(LSD) invalid\n", ret
);
2886 adapter
->sgmii_lsd
= ret
;
2887 netif_dbg(adapter
, drv
, adapter
->netdev
,
2888 "Link Speed Duplex (lsd) : 0x%X\n", adapter
->sgmii_lsd
);
2890 /* LINK_STATUS_SOURCE from the External PHY via SGMII */
2891 sgmii_ctl
= lan743x_csr_read(adapter
, SGMII_CTL
);
2892 sgmii_ctl
&= ~SGMII_CTL_LINK_STATUS_SOURCE_
;
2893 lan743x_csr_write(adapter
, SGMII_CTL
, sgmii_ctl
);
2895 ret
= lan743x_serdes_clock_and_aneg_update(adapter
);
2897 netif_err(adapter
, drv
, adapter
->netdev
,
2898 "error %d sgmii aneg update failed\n", ret
);
2902 return lan743x_pcs_power_reset(adapter
);
2905 static int lan743x_phylink_1000basex_config(struct lan743x_adapter
*adapter
)
2910 ret
= lan743x_get_lsd(SPEED_1000
, DUPLEX_FULL
,
2911 MASTER_SLAVE_STATE_MASTER
);
2913 netif_err(adapter
, drv
, adapter
->netdev
,
2914 "error %d link-speed-duplex(LSD) invalid\n", ret
);
2918 adapter
->sgmii_lsd
= ret
;
2919 netif_dbg(adapter
, drv
, adapter
->netdev
,
2920 "Link Speed Duplex (lsd) : 0x%X\n", adapter
->sgmii_lsd
);
2922 /* LINK_STATUS_SOURCE from 1000BASE-X PCS link status */
2923 sgmii_ctl
= lan743x_csr_read(adapter
, SGMII_CTL
);
2924 sgmii_ctl
|= SGMII_CTL_LINK_STATUS_SOURCE_
;
2925 lan743x_csr_write(adapter
, SGMII_CTL
, sgmii_ctl
);
2927 ret
= lan743x_serdes_clock_and_aneg_update(adapter
);
2929 netif_err(adapter
, drv
, adapter
->netdev
,
2930 "error %d 1000basex aneg update failed\n", ret
);
2934 return lan743x_pcs_power_reset(adapter
);
2937 static int lan743x_phylink_2500basex_config(struct lan743x_adapter
*adapter
)
2942 ret
= lan743x_get_lsd(SPEED_2500
, DUPLEX_FULL
,
2943 MASTER_SLAVE_STATE_MASTER
);
2945 netif_err(adapter
, drv
, adapter
->netdev
,
2946 "error %d link-speed-duplex(LSD) invalid\n", ret
);
2950 adapter
->sgmii_lsd
= ret
;
2951 netif_dbg(adapter
, drv
, adapter
->netdev
,
2952 "Link Speed Duplex (lsd) : 0x%X\n", adapter
->sgmii_lsd
);
2954 /* LINK_STATUS_SOURCE from 2500BASE-X PCS link status */
2955 sgmii_ctl
= lan743x_csr_read(adapter
, SGMII_CTL
);
2956 sgmii_ctl
|= SGMII_CTL_LINK_STATUS_SOURCE_
;
2957 lan743x_csr_write(adapter
, SGMII_CTL
, sgmii_ctl
);
2959 ret
= lan743x_serdes_clock_and_aneg_update(adapter
);
2961 netif_err(adapter
, drv
, adapter
->netdev
,
2962 "error %d 2500basex aneg update failed\n", ret
);
2966 return lan743x_pcs_power_reset(adapter
);
2969 void lan743x_mac_eee_enable(struct lan743x_adapter
*adapter
, bool enable
)
2973 mac_cr
= lan743x_csr_read(adapter
, MAC_CR
);
2975 mac_cr
|= MAC_CR_EEE_EN_
;
2977 mac_cr
&= ~MAC_CR_EEE_EN_
;
2978 lan743x_csr_write(adapter
, MAC_CR
, mac_cr
);
2981 static void lan743x_phylink_mac_config(struct phylink_config
*config
,
2982 unsigned int link_an_mode
,
2983 const struct phylink_link_state
*state
)
2985 struct net_device
*netdev
= to_net_dev(config
->dev
);
2986 struct lan743x_adapter
*adapter
= netdev_priv(netdev
);
2989 switch (state
->interface
) {
2990 case PHY_INTERFACE_MODE_2500BASEX
:
2991 ret
= lan743x_phylink_2500basex_config(adapter
);
2993 netif_err(adapter
, drv
, adapter
->netdev
,
2994 "2500BASEX config failed. Error %d\n", ret
);
2996 netif_dbg(adapter
, drv
, adapter
->netdev
,
2997 "2500BASEX mode selected and configured\n");
2999 case PHY_INTERFACE_MODE_1000BASEX
:
3000 ret
= lan743x_phylink_1000basex_config(adapter
);
3002 netif_err(adapter
, drv
, adapter
->netdev
,
3003 "1000BASEX config failed. Error %d\n", ret
);
3005 netif_dbg(adapter
, drv
, adapter
->netdev
,
3006 "1000BASEX mode selected and configured\n");
3008 case PHY_INTERFACE_MODE_SGMII
:
3009 ret
= lan743x_phylink_sgmii_config(adapter
);
3011 netif_err(adapter
, drv
, adapter
->netdev
,
3012 "SGMII config failed. Error %d\n", ret
);
3014 netif_dbg(adapter
, drv
, adapter
->netdev
,
3015 "SGMII mode selected and configured\n");
3018 netif_dbg(adapter
, drv
, adapter
->netdev
,
3019 "RGMII/GMII/MII(0x%X) mode enable\n",
3025 static void lan743x_phylink_mac_link_down(struct phylink_config
*config
,
3026 unsigned int link_an_mode
,
3027 phy_interface_t interface
)
3029 struct net_device
*netdev
= to_net_dev(config
->dev
);
3030 struct lan743x_adapter
*adapter
= netdev_priv(netdev
);
3032 netif_tx_stop_all_queues(to_net_dev(config
->dev
));
3033 lan743x_mac_eee_enable(adapter
, false);
3036 static void lan743x_phylink_mac_link_up(struct phylink_config
*config
,
3037 struct phy_device
*phydev
,
3038 unsigned int link_an_mode
,
3039 phy_interface_t interface
,
3040 int speed
, int duplex
,
3041 bool tx_pause
, bool rx_pause
)
3043 struct net_device
*netdev
= to_net_dev(config
->dev
);
3044 struct lan743x_adapter
*adapter
= netdev_priv(netdev
);
3048 mac_cr
= lan743x_csr_read(adapter
, MAC_CR
);
3049 /* Pre-initialize register bits.
3050 * Resulting value corresponds to SPEED_10
3052 mac_cr
&= ~(MAC_CR_CFG_H_
| MAC_CR_CFG_L_
);
3053 if (speed
== SPEED_2500
)
3054 mac_cr
|= MAC_CR_CFG_H_
| MAC_CR_CFG_L_
;
3055 else if (speed
== SPEED_1000
)
3056 mac_cr
|= MAC_CR_CFG_H_
;
3057 else if (speed
== SPEED_100
)
3058 mac_cr
|= MAC_CR_CFG_L_
;
3060 lan743x_csr_write(adapter
, MAC_CR
, mac_cr
);
3062 lan743x_ptp_update_latency(adapter
, speed
);
3064 /* Flow Control operation */
3067 cap
|= FLOW_CTRL_TX
;
3069 cap
|= FLOW_CTRL_RX
;
3071 lan743x_mac_flow_ctrl_set_enables(adapter
,
3073 cap
& FLOW_CTRL_RX
);
3076 lan743x_mac_eee_enable(adapter
, phydev
->enable_tx_lpi
);
3078 netif_tx_wake_all_queues(netdev
);
3081 static const struct phylink_mac_ops lan743x_phylink_mac_ops
= {
3082 .mac_config
= lan743x_phylink_mac_config
,
3083 .mac_link_down
= lan743x_phylink_mac_link_down
,
3084 .mac_link_up
= lan743x_phylink_mac_link_up
,
3087 static int lan743x_phylink_create(struct lan743x_adapter
*adapter
)
3089 struct net_device
*netdev
= adapter
->netdev
;
3092 adapter
->phylink_config
.dev
= &netdev
->dev
;
3093 adapter
->phylink_config
.type
= PHYLINK_NETDEV
;
3094 adapter
->phylink_config
.mac_managed_pm
= false;
3096 adapter
->phylink_config
.mac_capabilities
= MAC_ASYM_PAUSE
|
3097 MAC_SYM_PAUSE
| MAC_10
| MAC_100
| MAC_1000FD
;
3099 lan743x_phy_interface_select(adapter
);
3101 switch (adapter
->phy_interface
) {
3102 case PHY_INTERFACE_MODE_SGMII
:
3103 __set_bit(PHY_INTERFACE_MODE_SGMII
,
3104 adapter
->phylink_config
.supported_interfaces
);
3105 __set_bit(PHY_INTERFACE_MODE_1000BASEX
,
3106 adapter
->phylink_config
.supported_interfaces
);
3107 __set_bit(PHY_INTERFACE_MODE_2500BASEX
,
3108 adapter
->phylink_config
.supported_interfaces
);
3109 adapter
->phylink_config
.mac_capabilities
|= MAC_2500FD
;
3111 case PHY_INTERFACE_MODE_GMII
:
3112 __set_bit(PHY_INTERFACE_MODE_GMII
,
3113 adapter
->phylink_config
.supported_interfaces
);
3115 case PHY_INTERFACE_MODE_MII
:
3116 __set_bit(PHY_INTERFACE_MODE_MII
,
3117 adapter
->phylink_config
.supported_interfaces
);
3120 phy_interface_set_rgmii(adapter
->phylink_config
.supported_interfaces
);
3123 pl
= phylink_create(&adapter
->phylink_config
, NULL
,
3124 adapter
->phy_interface
, &lan743x_phylink_mac_ops
);
3127 netdev_err(netdev
, "Could not create phylink (%pe)\n", pl
);
3131 adapter
->phylink
= pl
;
3132 netdev_dbg(netdev
, "lan743x phylink created");
3137 static bool lan743x_phy_handle_exists(struct device_node
*dn
)
3139 dn
= of_parse_phandle(dn
, "phy-handle", 0);
3144 static int lan743x_phylink_connect(struct lan743x_adapter
*adapter
)
3146 struct device_node
*dn
= adapter
->pdev
->dev
.of_node
;
3147 struct net_device
*dev
= adapter
->netdev
;
3148 struct phy_device
*phydev
;
3152 ret
= phylink_of_phy_connect(adapter
->phylink
, dn
, 0);
3154 if (!dn
|| (ret
&& !lan743x_phy_handle_exists(dn
))) {
3155 phydev
= phy_find_first(adapter
->mdiobus
);
3157 /* attach the mac to the phy */
3158 ret
= phylink_connect_phy(adapter
->phylink
, phydev
);
3159 } else if (((adapter
->csr
.id_rev
& ID_REV_ID_MASK_
) ==
3160 ID_REV_ID_LAN7431_
) || adapter
->is_pci11x1x
) {
3161 struct phylink_link_state state
;
3164 caps
= adapter
->phylink_config
.mac_capabilities
;
3165 if (caps
& MAC_2500FD
) {
3166 state
.speed
= SPEED_2500
;
3167 state
.duplex
= DUPLEX_FULL
;
3168 } else if (caps
& MAC_1000FD
) {
3169 state
.speed
= SPEED_1000
;
3170 state
.duplex
= DUPLEX_FULL
;
3172 state
.speed
= SPEED_UNKNOWN
;
3173 state
.duplex
= DUPLEX_UNKNOWN
;
3176 ret
= phylink_set_fixed_link(adapter
->phylink
, &state
);
3178 netdev_err(dev
, "Could not set fixed link\n");
3182 netdev_err(dev
, "no PHY found\n");
3188 netdev_err(dev
, "Could not attach PHY (%d)\n", ret
);
3192 phylink_start(adapter
->phylink
);
3197 static void lan743x_phylink_disconnect(struct lan743x_adapter
*adapter
)
3199 phylink_stop(adapter
->phylink
);
3200 phylink_disconnect_phy(adapter
->phylink
);
3203 static int lan743x_netdev_close(struct net_device
*netdev
)
3205 struct lan743x_adapter
*adapter
= netdev_priv(netdev
);
3208 for (index
= 0; index
< adapter
->used_tx_channels
; index
++)
3209 lan743x_tx_close(&adapter
->tx
[index
]);
3211 for (index
= 0; index
< LAN743X_USED_RX_CHANNELS
; index
++)
3212 lan743x_rx_close(&adapter
->rx
[index
]);
3214 lan743x_ptp_close(adapter
);
3216 lan743x_phylink_disconnect(adapter
);
3218 lan743x_mac_close(adapter
);
3220 lan743x_intr_close(adapter
);
3225 static int lan743x_netdev_open(struct net_device
*netdev
)
3227 struct lan743x_adapter
*adapter
= netdev_priv(netdev
);
3231 ret
= lan743x_intr_open(adapter
);
3235 ret
= lan743x_mac_open(adapter
);
3239 ret
= lan743x_phylink_connect(adapter
);
3243 ret
= lan743x_ptp_open(adapter
);
3247 lan743x_rfe_open(adapter
);
3249 for (index
= 0; index
< LAN743X_USED_RX_CHANNELS
; index
++) {
3250 ret
= lan743x_rx_open(&adapter
->rx
[index
]);
3255 for (index
= 0; index
< adapter
->used_tx_channels
; index
++) {
3256 ret
= lan743x_tx_open(&adapter
->tx
[index
]);
3262 phy_support_eee(netdev
->phydev
);
3265 if (adapter
->netdev
->phydev
) {
3266 struct ethtool_wolinfo wol
= { .cmd
= ETHTOOL_GWOL
};
3268 phy_ethtool_get_wol(netdev
->phydev
, &wol
);
3269 adapter
->phy_wol_supported
= wol
.supported
;
3270 adapter
->phy_wolopts
= wol
.wolopts
;
3277 for (index
= 0; index
< adapter
->used_tx_channels
; index
++) {
3278 if (adapter
->tx
[index
].ring_cpu_ptr
)
3279 lan743x_tx_close(&adapter
->tx
[index
]);
3283 for (index
= 0; index
< LAN743X_USED_RX_CHANNELS
; index
++) {
3284 if (adapter
->rx
[index
].ring_cpu_ptr
)
3285 lan743x_rx_close(&adapter
->rx
[index
]);
3287 lan743x_ptp_close(adapter
);
3288 if (adapter
->phylink
)
3289 lan743x_phylink_disconnect(adapter
);
3292 lan743x_mac_close(adapter
);
3295 lan743x_intr_close(adapter
);
3298 netif_warn(adapter
, ifup
, adapter
->netdev
,
3299 "Error opening LAN743x\n");
3303 static netdev_tx_t
lan743x_netdev_xmit_frame(struct sk_buff
*skb
,
3304 struct net_device
*netdev
)
3306 struct lan743x_adapter
*adapter
= netdev_priv(netdev
);
3309 if (adapter
->is_pci11x1x
)
3310 ch
= skb
->queue_mapping
% PCI11X1X_USED_TX_CHANNELS
;
3312 return lan743x_tx_xmit_frame(&adapter
->tx
[ch
], skb
);
3315 static int lan743x_netdev_ioctl(struct net_device
*netdev
,
3316 struct ifreq
*ifr
, int cmd
)
3318 struct lan743x_adapter
*adapter
= netdev_priv(netdev
);
3320 if (!netif_running(netdev
))
3322 if (cmd
== SIOCSHWTSTAMP
)
3323 return lan743x_ptp_ioctl(netdev
, ifr
, cmd
);
3325 return phylink_mii_ioctl(adapter
->phylink
, ifr
, cmd
);
3328 static void lan743x_netdev_set_multicast(struct net_device
*netdev
)
3330 struct lan743x_adapter
*adapter
= netdev_priv(netdev
);
3332 lan743x_rfe_set_multicast(adapter
);
3335 static int lan743x_netdev_change_mtu(struct net_device
*netdev
, int new_mtu
)
3337 struct lan743x_adapter
*adapter
= netdev_priv(netdev
);
3340 ret
= lan743x_mac_set_mtu(adapter
, new_mtu
);
3342 WRITE_ONCE(netdev
->mtu
, new_mtu
);
3346 static void lan743x_netdev_get_stats64(struct net_device
*netdev
,
3347 struct rtnl_link_stats64
*stats
)
3349 struct lan743x_adapter
*adapter
= netdev_priv(netdev
);
3351 stats
->rx_packets
= lan743x_csr_read(adapter
, STAT_RX_TOTAL_FRAMES
);
3352 stats
->tx_packets
= lan743x_csr_read(adapter
, STAT_TX_TOTAL_FRAMES
);
3353 stats
->rx_bytes
= lan743x_csr_read(adapter
,
3354 STAT_RX_UNICAST_BYTE_COUNT
) +
3355 lan743x_csr_read(adapter
,
3356 STAT_RX_BROADCAST_BYTE_COUNT
) +
3357 lan743x_csr_read(adapter
,
3358 STAT_RX_MULTICAST_BYTE_COUNT
);
3359 stats
->tx_bytes
= lan743x_csr_read(adapter
,
3360 STAT_TX_UNICAST_BYTE_COUNT
) +
3361 lan743x_csr_read(adapter
,
3362 STAT_TX_BROADCAST_BYTE_COUNT
) +
3363 lan743x_csr_read(adapter
,
3364 STAT_TX_MULTICAST_BYTE_COUNT
);
3365 stats
->rx_errors
= lan743x_csr_read(adapter
, STAT_RX_FCS_ERRORS
) +
3366 lan743x_csr_read(adapter
,
3367 STAT_RX_ALIGNMENT_ERRORS
) +
3368 lan743x_csr_read(adapter
, STAT_RX_JABBER_ERRORS
) +
3369 lan743x_csr_read(adapter
,
3370 STAT_RX_UNDERSIZE_FRAME_ERRORS
) +
3371 lan743x_csr_read(adapter
,
3372 STAT_RX_OVERSIZE_FRAME_ERRORS
);
3373 stats
->tx_errors
= lan743x_csr_read(adapter
, STAT_TX_FCS_ERRORS
) +
3374 lan743x_csr_read(adapter
,
3375 STAT_TX_EXCESS_DEFERRAL_ERRORS
) +
3376 lan743x_csr_read(adapter
, STAT_TX_CARRIER_ERRORS
);
3377 stats
->rx_dropped
= lan743x_csr_read(adapter
,
3378 STAT_RX_DROPPED_FRAMES
);
3379 stats
->tx_dropped
= lan743x_csr_read(adapter
,
3380 STAT_TX_EXCESSIVE_COLLISION
);
3381 stats
->multicast
= lan743x_csr_read(adapter
,
3382 STAT_RX_MULTICAST_FRAMES
) +
3383 lan743x_csr_read(adapter
,
3384 STAT_TX_MULTICAST_FRAMES
);
3385 stats
->collisions
= lan743x_csr_read(adapter
,
3386 STAT_TX_SINGLE_COLLISIONS
) +
3387 lan743x_csr_read(adapter
,
3388 STAT_TX_MULTIPLE_COLLISIONS
) +
3389 lan743x_csr_read(adapter
,
3390 STAT_TX_LATE_COLLISIONS
);
3393 static int lan743x_netdev_set_mac_address(struct net_device
*netdev
,
3396 struct lan743x_adapter
*adapter
= netdev_priv(netdev
);
3397 struct sockaddr
*sock_addr
= addr
;
3400 ret
= eth_prepare_mac_addr_change(netdev
, sock_addr
);
3403 eth_hw_addr_set(netdev
, sock_addr
->sa_data
);
3404 lan743x_mac_set_address(adapter
, sock_addr
->sa_data
);
3405 lan743x_rfe_update_mac_address(adapter
);
3409 static const struct net_device_ops lan743x_netdev_ops
= {
3410 .ndo_open
= lan743x_netdev_open
,
3411 .ndo_stop
= lan743x_netdev_close
,
3412 .ndo_start_xmit
= lan743x_netdev_xmit_frame
,
3413 .ndo_eth_ioctl
= lan743x_netdev_ioctl
,
3414 .ndo_set_rx_mode
= lan743x_netdev_set_multicast
,
3415 .ndo_change_mtu
= lan743x_netdev_change_mtu
,
3416 .ndo_get_stats64
= lan743x_netdev_get_stats64
,
3417 .ndo_set_mac_address
= lan743x_netdev_set_mac_address
,
3420 static void lan743x_hardware_cleanup(struct lan743x_adapter
*adapter
)
3422 lan743x_csr_write(adapter
, INT_EN_CLR
, 0xFFFFFFFF);
3425 static void lan743x_mdiobus_cleanup(struct lan743x_adapter
*adapter
)
3427 mdiobus_unregister(adapter
->mdiobus
);
3430 static void lan743x_destroy_phylink(struct lan743x_adapter
*adapter
)
3432 phylink_destroy(adapter
->phylink
);
3433 adapter
->phylink
= NULL
;
3436 static void lan743x_full_cleanup(struct lan743x_adapter
*adapter
)
3438 unregister_netdev(adapter
->netdev
);
3440 lan743x_destroy_phylink(adapter
);
3441 lan743x_mdiobus_cleanup(adapter
);
3442 lan743x_hardware_cleanup(adapter
);
3443 lan743x_pci_cleanup(adapter
);
3446 static void pci11x1x_set_rfe_rd_fifo_threshold(struct lan743x_adapter
*adapter
)
3448 u16 rev
= adapter
->csr
.id_rev
& ID_REV_CHIP_REV_MASK_
;
3450 if (rev
== ID_REV_CHIP_REV_PCI11X1X_B0_
) {
3453 misc_ctl
= lan743x_csr_read(adapter
, MISC_CTL_0
);
3454 misc_ctl
&= ~MISC_CTL_0_RFE_READ_FIFO_MASK_
;
3455 misc_ctl
|= FIELD_PREP(MISC_CTL_0_RFE_READ_FIFO_MASK_
,
3456 RFE_RD_FIFO_TH_3_DWORDS
);
3457 lan743x_csr_write(adapter
, MISC_CTL_0
, misc_ctl
);
3461 static int lan743x_hardware_init(struct lan743x_adapter
*adapter
,
3462 struct pci_dev
*pdev
)
3464 struct lan743x_tx
*tx
;
3468 adapter
->is_pci11x1x
= is_pci11x1x_chip(adapter
);
3469 if (adapter
->is_pci11x1x
) {
3470 adapter
->max_tx_channels
= PCI11X1X_MAX_TX_CHANNELS
;
3471 adapter
->used_tx_channels
= PCI11X1X_USED_TX_CHANNELS
;
3472 adapter
->max_vector_count
= PCI11X1X_MAX_VECTOR_COUNT
;
3473 pci11x1x_strap_get_status(adapter
);
3474 spin_lock_init(&adapter
->eth_syslock_spinlock
);
3475 mutex_init(&adapter
->sgmii_rw_lock
);
3476 pci11x1x_set_rfe_rd_fifo_threshold(adapter
);
3478 adapter
->max_tx_channels
= LAN743X_MAX_TX_CHANNELS
;
3479 adapter
->used_tx_channels
= LAN743X_USED_TX_CHANNELS
;
3480 adapter
->max_vector_count
= LAN743X_MAX_VECTOR_COUNT
;
3483 adapter
->intr
.irq
= adapter
->pdev
->irq
;
3484 lan743x_csr_write(adapter
, INT_EN_CLR
, 0xFFFFFFFF);
3486 ret
= lan743x_gpio_init(adapter
);
3490 ret
= lan743x_mac_init(adapter
);
3494 ret
= lan743x_phy_init(adapter
);
3498 ret
= lan743x_ptp_init(adapter
);
3502 lan743x_rfe_update_mac_address(adapter
);
3504 ret
= lan743x_dmac_init(adapter
);
3508 for (index
= 0; index
< LAN743X_USED_RX_CHANNELS
; index
++) {
3509 adapter
->rx
[index
].adapter
= adapter
;
3510 adapter
->rx
[index
].channel_number
= index
;
3513 for (index
= 0; index
< adapter
->used_tx_channels
; index
++) {
3514 tx
= &adapter
->tx
[index
];
3515 tx
->adapter
= adapter
;
3516 tx
->channel_number
= index
;
3517 spin_lock_init(&tx
->ring_lock
);
3523 static int lan743x_mdiobus_init(struct lan743x_adapter
*adapter
)
3528 adapter
->mdiobus
= devm_mdiobus_alloc(&adapter
->pdev
->dev
);
3529 if (!(adapter
->mdiobus
)) {
3534 adapter
->mdiobus
->priv
= (void *)adapter
;
3535 if (adapter
->is_pci11x1x
) {
3536 if (adapter
->is_sgmii_en
) {
3537 sgmii_ctl
= lan743x_csr_read(adapter
, SGMII_CTL
);
3538 sgmii_ctl
|= SGMII_CTL_SGMII_ENABLE_
;
3539 sgmii_ctl
&= ~SGMII_CTL_SGMII_POWER_DN_
;
3540 lan743x_csr_write(adapter
, SGMII_CTL
, sgmii_ctl
);
3541 netif_dbg(adapter
, drv
, adapter
->netdev
,
3542 "SGMII operation\n");
3543 adapter
->mdiobus
->read
= lan743x_mdiobus_read_c22
;
3544 adapter
->mdiobus
->write
= lan743x_mdiobus_write_c22
;
3545 adapter
->mdiobus
->read_c45
= lan743x_mdiobus_read_c45
;
3546 adapter
->mdiobus
->write_c45
= lan743x_mdiobus_write_c45
;
3547 adapter
->mdiobus
->name
= "lan743x-mdiobus-c45";
3548 netif_dbg(adapter
, drv
, adapter
->netdev
,
3549 "lan743x-mdiobus-c45\n");
3551 sgmii_ctl
= lan743x_csr_read(adapter
, SGMII_CTL
);
3552 sgmii_ctl
&= ~SGMII_CTL_SGMII_ENABLE_
;
3553 sgmii_ctl
|= SGMII_CTL_SGMII_POWER_DN_
;
3554 lan743x_csr_write(adapter
, SGMII_CTL
, sgmii_ctl
);
3555 netif_dbg(adapter
, drv
, adapter
->netdev
,
3556 "RGMII operation\n");
3557 // Only C22 support when RGMII I/F
3558 adapter
->mdiobus
->read
= lan743x_mdiobus_read_c22
;
3559 adapter
->mdiobus
->write
= lan743x_mdiobus_write_c22
;
3560 adapter
->mdiobus
->name
= "lan743x-mdiobus";
3561 netif_dbg(adapter
, drv
, adapter
->netdev
,
3562 "lan743x-mdiobus\n");
3565 adapter
->mdiobus
->read
= lan743x_mdiobus_read_c22
;
3566 adapter
->mdiobus
->write
= lan743x_mdiobus_write_c22
;
3567 adapter
->mdiobus
->name
= "lan743x-mdiobus";
3568 netif_dbg(adapter
, drv
, adapter
->netdev
, "lan743x-mdiobus\n");
3571 snprintf(adapter
->mdiobus
->id
, MII_BUS_ID_SIZE
,
3572 "pci-%s", pci_name(adapter
->pdev
));
3574 if ((adapter
->csr
.id_rev
& ID_REV_ID_MASK_
) == ID_REV_ID_LAN7430_
)
3575 /* LAN7430 uses internal phy at address 1 */
3576 adapter
->mdiobus
->phy_mask
= ~(u32
)BIT(1);
3578 /* register mdiobus */
3579 ret
= mdiobus_register(adapter
->mdiobus
);
3588 /* lan743x_pcidev_probe - Device Initialization Routine
3589 * @pdev: PCI device information struct
3590 * @id: entry in lan743x_pci_tbl
3592 * Returns 0 on success, negative on failure
3594 * initializes an adapter identified by a pci_dev structure.
3595 * The OS initialization, configuring of the adapter private structure,
3596 * and a hardware reset occur.
3598 static int lan743x_pcidev_probe(struct pci_dev
*pdev
,
3599 const struct pci_device_id
*id
)
3601 struct lan743x_adapter
*adapter
= NULL
;
3602 struct net_device
*netdev
= NULL
;
3605 if (id
->device
== PCI_DEVICE_ID_SMSC_A011
||
3606 id
->device
== PCI_DEVICE_ID_SMSC_A041
) {
3607 netdev
= devm_alloc_etherdev_mqs(&pdev
->dev
,
3608 sizeof(struct lan743x_adapter
),
3609 PCI11X1X_USED_TX_CHANNELS
,
3610 LAN743X_USED_RX_CHANNELS
);
3612 netdev
= devm_alloc_etherdev_mqs(&pdev
->dev
,
3613 sizeof(struct lan743x_adapter
),
3614 LAN743X_USED_TX_CHANNELS
,
3615 LAN743X_USED_RX_CHANNELS
);
3621 SET_NETDEV_DEV(netdev
, &pdev
->dev
);
3622 pci_set_drvdata(pdev
, netdev
);
3623 adapter
= netdev_priv(netdev
);
3624 adapter
->netdev
= netdev
;
3625 adapter
->msg_enable
= NETIF_MSG_DRV
| NETIF_MSG_PROBE
|
3626 NETIF_MSG_LINK
| NETIF_MSG_IFUP
|
3627 NETIF_MSG_IFDOWN
| NETIF_MSG_TX_QUEUED
;
3628 netdev
->max_mtu
= LAN743X_MAX_FRAME_SIZE
;
3630 of_get_mac_address(pdev
->dev
.of_node
, adapter
->mac_address
);
3632 ret
= lan743x_pci_init(adapter
, pdev
);
3636 ret
= lan743x_csr_init(adapter
);
3640 ret
= lan743x_hardware_init(adapter
, pdev
);
3644 ret
= lan743x_mdiobus_init(adapter
);
3646 goto cleanup_hardware
;
3648 adapter
->netdev
->netdev_ops
= &lan743x_netdev_ops
;
3649 adapter
->netdev
->ethtool_ops
= &lan743x_ethtool_ops
;
3650 adapter
->netdev
->features
= NETIF_F_SG
| NETIF_F_TSO
|
3651 NETIF_F_HW_CSUM
| NETIF_F_RXCSUM
;
3652 adapter
->netdev
->hw_features
= adapter
->netdev
->features
;
3654 ret
= lan743x_phylink_create(adapter
);
3656 netif_err(adapter
, probe
, netdev
,
3657 "failed to setup phylink (%d)\n", ret
);
3658 goto cleanup_mdiobus
;
3661 ret
= register_netdev(adapter
->netdev
);
3663 goto cleanup_phylink
;
3667 lan743x_destroy_phylink(adapter
);
3670 lan743x_mdiobus_cleanup(adapter
);
3673 lan743x_hardware_cleanup(adapter
);
3676 lan743x_pci_cleanup(adapter
);
3679 pr_warn("Initialization failed\n");
3684 * lan743x_pcidev_remove - Device Removal Routine
3685 * @pdev: PCI device information struct
3687 * this is called by the PCI subsystem to alert the driver
3688 * that it should release a PCI device. This could be caused by a
3689 * Hot-Plug event, or because the driver is going to be removed from
3692 static void lan743x_pcidev_remove(struct pci_dev
*pdev
)
3694 struct net_device
*netdev
= pci_get_drvdata(pdev
);
3695 struct lan743x_adapter
*adapter
= netdev_priv(netdev
);
3697 lan743x_full_cleanup(adapter
);
3700 static void lan743x_pcidev_shutdown(struct pci_dev
*pdev
)
3702 struct net_device
*netdev
= pci_get_drvdata(pdev
);
3703 struct lan743x_adapter
*adapter
= netdev_priv(netdev
);
3706 netif_device_detach(netdev
);
3708 /* close netdev when netdev is at running state.
3709 * For instance, it is true when system goes to sleep by pm-suspend
3710 * However, it is false when system goes to sleep by suspend GUI menu
3712 if (netif_running(netdev
))
3713 lan743x_netdev_close(netdev
);
3717 pci_save_state(pdev
);
3720 /* clean up lan743x portion */
3721 lan743x_hardware_cleanup(adapter
);
3724 #ifdef CONFIG_PM_SLEEP
3725 static u16
lan743x_pm_wakeframe_crc16(const u8
*buf
, int len
)
3727 return bitrev16(crc16(0xFFFF, buf
, len
));
3730 static void lan743x_pm_set_wol(struct lan743x_adapter
*adapter
)
3732 const u8 ipv4_multicast
[3] = { 0x01, 0x00, 0x5E };
3733 const u8 ipv6_multicast
[3] = { 0x33, 0x33 };
3734 const u8 arp_type
[2] = { 0x08, 0x06 };
3742 for (mask_index
= 0; mask_index
< MAC_NUM_OF_WUF_CFG
; mask_index
++)
3743 lan743x_csr_write(adapter
, MAC_WUF_CFG(mask_index
), 0);
3745 /* clear wake settings */
3746 pmtctl
= lan743x_csr_read(adapter
, PMT_CTL
);
3747 pmtctl
|= PMT_CTL_WUPS_MASK_
| PMT_CTL_RES_CLR_WKP_MASK_
;
3748 pmtctl
&= ~(PMT_CTL_GPIO_WAKEUP_EN_
| PMT_CTL_EEE_WAKEUP_EN_
|
3749 PMT_CTL_WOL_EN_
| PMT_CTL_MAC_D3_RX_CLK_OVR_
|
3750 PMT_CTL_RX_FCT_RFE_D3_CLK_OVR_
| PMT_CTL_ETH_PHY_WAKE_EN_
);
3752 macrx
= lan743x_csr_read(adapter
, MAC_RX
);
3757 pmtctl
|= PMT_CTL_ETH_PHY_D3_COLD_OVR_
| PMT_CTL_ETH_PHY_D3_OVR_
;
3759 if (adapter
->phy_wolopts
)
3760 pmtctl
|= PMT_CTL_ETH_PHY_WAKE_EN_
;
3762 if (adapter
->wolopts
& WAKE_MAGIC
) {
3763 wucsr
|= MAC_WUCSR_MPEN_
;
3764 macrx
|= MAC_RX_RXEN_
;
3765 pmtctl
|= PMT_CTL_WOL_EN_
| PMT_CTL_MAC_D3_RX_CLK_OVR_
;
3767 if (adapter
->wolopts
& WAKE_UCAST
) {
3768 wucsr
|= MAC_WUCSR_RFE_WAKE_EN_
| MAC_WUCSR_PFDA_EN_
;
3769 macrx
|= MAC_RX_RXEN_
;
3770 pmtctl
|= PMT_CTL_WOL_EN_
| PMT_CTL_MAC_D3_RX_CLK_OVR_
;
3771 pmtctl
|= PMT_CTL_RX_FCT_RFE_D3_CLK_OVR_
;
3773 if (adapter
->wolopts
& WAKE_BCAST
) {
3774 wucsr
|= MAC_WUCSR_RFE_WAKE_EN_
| MAC_WUCSR_BCST_EN_
;
3775 macrx
|= MAC_RX_RXEN_
;
3776 pmtctl
|= PMT_CTL_WOL_EN_
| PMT_CTL_MAC_D3_RX_CLK_OVR_
;
3777 pmtctl
|= PMT_CTL_RX_FCT_RFE_D3_CLK_OVR_
;
3779 if (adapter
->wolopts
& WAKE_MCAST
) {
3780 /* IPv4 multicast */
3781 crc
= lan743x_pm_wakeframe_crc16(ipv4_multicast
, 3);
3782 lan743x_csr_write(adapter
, MAC_WUF_CFG(mask_index
),
3783 MAC_WUF_CFG_EN_
| MAC_WUF_CFG_TYPE_MCAST_
|
3784 (0 << MAC_WUF_CFG_OFFSET_SHIFT_
) |
3785 (crc
& MAC_WUF_CFG_CRC16_MASK_
));
3786 lan743x_csr_write(adapter
, MAC_WUF_MASK0(mask_index
), 7);
3787 lan743x_csr_write(adapter
, MAC_WUF_MASK1(mask_index
), 0);
3788 lan743x_csr_write(adapter
, MAC_WUF_MASK2(mask_index
), 0);
3789 lan743x_csr_write(adapter
, MAC_WUF_MASK3(mask_index
), 0);
3792 /* IPv6 multicast */
3793 crc
= lan743x_pm_wakeframe_crc16(ipv6_multicast
, 2);
3794 lan743x_csr_write(adapter
, MAC_WUF_CFG(mask_index
),
3795 MAC_WUF_CFG_EN_
| MAC_WUF_CFG_TYPE_MCAST_
|
3796 (0 << MAC_WUF_CFG_OFFSET_SHIFT_
) |
3797 (crc
& MAC_WUF_CFG_CRC16_MASK_
));
3798 lan743x_csr_write(adapter
, MAC_WUF_MASK0(mask_index
), 3);
3799 lan743x_csr_write(adapter
, MAC_WUF_MASK1(mask_index
), 0);
3800 lan743x_csr_write(adapter
, MAC_WUF_MASK2(mask_index
), 0);
3801 lan743x_csr_write(adapter
, MAC_WUF_MASK3(mask_index
), 0);
3804 wucsr
|= MAC_WUCSR_RFE_WAKE_EN_
| MAC_WUCSR_WAKE_EN_
;
3805 macrx
|= MAC_RX_RXEN_
;
3806 pmtctl
|= PMT_CTL_WOL_EN_
| PMT_CTL_MAC_D3_RX_CLK_OVR_
;
3807 pmtctl
|= PMT_CTL_RX_FCT_RFE_D3_CLK_OVR_
;
3809 if (adapter
->wolopts
& WAKE_ARP
) {
3810 /* set MAC_WUF_CFG & WUF_MASK
3811 * for packettype (offset 12,13) = ARP (0x0806)
3813 crc
= lan743x_pm_wakeframe_crc16(arp_type
, 2);
3814 lan743x_csr_write(adapter
, MAC_WUF_CFG(mask_index
),
3815 MAC_WUF_CFG_EN_
| MAC_WUF_CFG_TYPE_ALL_
|
3816 (0 << MAC_WUF_CFG_OFFSET_SHIFT_
) |
3817 (crc
& MAC_WUF_CFG_CRC16_MASK_
));
3818 lan743x_csr_write(adapter
, MAC_WUF_MASK0(mask_index
), 0x3000);
3819 lan743x_csr_write(adapter
, MAC_WUF_MASK1(mask_index
), 0);
3820 lan743x_csr_write(adapter
, MAC_WUF_MASK2(mask_index
), 0);
3821 lan743x_csr_write(adapter
, MAC_WUF_MASK3(mask_index
), 0);
3824 wucsr
|= MAC_WUCSR_RFE_WAKE_EN_
| MAC_WUCSR_WAKE_EN_
;
3825 macrx
|= MAC_RX_RXEN_
;
3826 pmtctl
|= PMT_CTL_WOL_EN_
| PMT_CTL_MAC_D3_RX_CLK_OVR_
;
3827 pmtctl
|= PMT_CTL_RX_FCT_RFE_D3_CLK_OVR_
;
3830 if (adapter
->wolopts
& WAKE_MAGICSECURE
) {
3831 sopass
= *(u32
*)adapter
->sopass
;
3832 lan743x_csr_write(adapter
, MAC_MP_SO_LO
, sopass
);
3833 sopass
= *(u16
*)&adapter
->sopass
[4];
3834 lan743x_csr_write(adapter
, MAC_MP_SO_HI
, sopass
);
3835 wucsr
|= MAC_MP_SO_EN_
;
3838 lan743x_csr_write(adapter
, MAC_WUCSR
, wucsr
);
3839 lan743x_csr_write(adapter
, PMT_CTL
, pmtctl
);
3840 lan743x_csr_write(adapter
, MAC_RX
, macrx
);
3843 static int lan743x_pm_suspend(struct device
*dev
)
3845 struct pci_dev
*pdev
= to_pci_dev(dev
);
3846 struct net_device
*netdev
= pci_get_drvdata(pdev
);
3847 struct lan743x_adapter
*adapter
= netdev_priv(netdev
);
3850 lan743x_pcidev_shutdown(pdev
);
3852 /* clear all wakes */
3853 lan743x_csr_write(adapter
, MAC_WUCSR
, 0);
3854 lan743x_csr_write(adapter
, MAC_WUCSR2
, 0);
3855 lan743x_csr_write(adapter
, MAC_WK_SRC
, 0xFFFFFFFF);
3857 if (adapter
->wolopts
|| adapter
->phy_wolopts
)
3858 lan743x_pm_set_wol(adapter
);
3860 if (adapter
->is_pci11x1x
) {
3861 /* Save HW_CFG to config again in PM resume */
3862 data
= lan743x_csr_read(adapter
, HW_CFG
);
3863 adapter
->hw_cfg
= data
;
3864 data
|= (HW_CFG_RST_PROTECT_PCIE_
|
3865 HW_CFG_D3_RESET_DIS_
|
3866 HW_CFG_D3_VAUX_OVR_
|
3867 HW_CFG_HOT_RESET_DIS_
|
3868 HW_CFG_RST_PROTECT_
);
3869 lan743x_csr_write(adapter
, HW_CFG
, data
);
3872 /* Host sets PME_En, put D3hot */
3873 return pci_prepare_to_sleep(pdev
);
3876 static int lan743x_pm_resume(struct device
*dev
)
3878 struct pci_dev
*pdev
= to_pci_dev(dev
);
3879 struct net_device
*netdev
= pci_get_drvdata(pdev
);
3880 struct lan743x_adapter
*adapter
= netdev_priv(netdev
);
3884 pci_set_power_state(pdev
, PCI_D0
);
3885 pci_restore_state(pdev
);
3886 pci_save_state(pdev
);
3888 /* Restore HW_CFG that was saved during pm suspend */
3889 if (adapter
->is_pci11x1x
)
3890 lan743x_csr_write(adapter
, HW_CFG
, adapter
->hw_cfg
);
3892 ret
= lan743x_hardware_init(adapter
, pdev
);
3894 netif_err(adapter
, probe
, adapter
->netdev
,
3895 "lan743x_hardware_init returned %d\n", ret
);
3896 lan743x_pci_cleanup(adapter
);
3900 ret
= lan743x_csr_read(adapter
, MAC_WK_SRC
);
3901 netif_dbg(adapter
, drv
, adapter
->netdev
,
3902 "Wakeup source : 0x%08X\n", ret
);
3904 /* Clear the wol configuration and status bits. Note that
3905 * the status bits are "Write One to Clear (W1C)"
3907 data
= MAC_WUCSR_EEE_TX_WAKE_
| MAC_WUCSR_EEE_RX_WAKE_
|
3908 MAC_WUCSR_RFE_WAKE_FR_
| MAC_WUCSR_PFDA_FR_
| MAC_WUCSR_WUFR_
|
3909 MAC_WUCSR_MPR_
| MAC_WUCSR_BCAST_FR_
;
3910 lan743x_csr_write(adapter
, MAC_WUCSR
, data
);
3912 data
= MAC_WUCSR2_NS_RCD_
| MAC_WUCSR2_ARP_RCD_
|
3913 MAC_WUCSR2_IPV6_TCPSYN_RCD_
| MAC_WUCSR2_IPV4_TCPSYN_RCD_
;
3914 lan743x_csr_write(adapter
, MAC_WUCSR2
, data
);
3916 data
= MAC_WK_SRC_ETH_PHY_WK_
| MAC_WK_SRC_IPV6_TCPSYN_RCD_WK_
|
3917 MAC_WK_SRC_IPV4_TCPSYN_RCD_WK_
| MAC_WK_SRC_EEE_TX_WK_
|
3918 MAC_WK_SRC_EEE_RX_WK_
| MAC_WK_SRC_RFE_FR_WK_
|
3919 MAC_WK_SRC_PFDA_FR_WK_
| MAC_WK_SRC_MP_FR_WK_
|
3920 MAC_WK_SRC_BCAST_FR_WK_
| MAC_WK_SRC_WU_FR_WK_
|
3921 MAC_WK_SRC_WK_FR_SAVED_
;
3922 lan743x_csr_write(adapter
, MAC_WK_SRC
, data
);
3925 /* open netdev when netdev is at running state while resume.
3926 * For instance, it is true when system wakesup after pm-suspend
3927 * However, it is false when system wakes up after suspend GUI menu
3929 if (netif_running(netdev
))
3930 lan743x_netdev_open(netdev
);
3932 netif_device_attach(netdev
);
3938 static const struct dev_pm_ops lan743x_pm_ops
= {
3939 SET_SYSTEM_SLEEP_PM_OPS(lan743x_pm_suspend
, lan743x_pm_resume
)
3941 #endif /* CONFIG_PM_SLEEP */
3943 static const struct pci_device_id lan743x_pcidev_tbl
[] = {
3944 { PCI_DEVICE(PCI_VENDOR_ID_SMSC
, PCI_DEVICE_ID_SMSC_LAN7430
) },
3945 { PCI_DEVICE(PCI_VENDOR_ID_SMSC
, PCI_DEVICE_ID_SMSC_LAN7431
) },
3946 { PCI_DEVICE(PCI_VENDOR_ID_SMSC
, PCI_DEVICE_ID_SMSC_A011
) },
3947 { PCI_DEVICE(PCI_VENDOR_ID_SMSC
, PCI_DEVICE_ID_SMSC_A041
) },
3951 MODULE_DEVICE_TABLE(pci
, lan743x_pcidev_tbl
);
3953 static struct pci_driver lan743x_pcidev_driver
= {
3954 .name
= DRIVER_NAME
,
3955 .id_table
= lan743x_pcidev_tbl
,
3956 .probe
= lan743x_pcidev_probe
,
3957 .remove
= lan743x_pcidev_remove
,
3958 #ifdef CONFIG_PM_SLEEP
3959 .driver
.pm
= &lan743x_pm_ops
,
3961 .shutdown
= lan743x_pcidev_shutdown
,
3964 module_pci_driver(lan743x_pcidev_driver
);
3966 MODULE_AUTHOR(DRIVER_AUTHOR
);
3967 MODULE_DESCRIPTION(DRIVER_DESC
);
3968 MODULE_LICENSE("GPL");