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