Linux 4.19.133
[linux/fpc-iii.git] / drivers / net / ethernet / microchip / lan743x_main.c
blob208341541087ea4a4658261f5f844f85aa47f089
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 ethtool_adv_to_mii_adv_t(phydev->advertising);
960 remote_advertisement =
961 ethtool_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;
986 u32 mii_adv;
988 netdev = adapter->netdev;
989 phydev = phy_find_first(adapter->mdiobus);
990 if (!phydev)
991 goto return_error;
993 ret = phy_connect_direct(netdev, phydev,
994 lan743x_phy_link_status_change,
995 PHY_INTERFACE_MODE_GMII);
996 if (ret)
997 goto return_error;
999 /* MAC doesn't support 1000T Half */
1000 phydev->supported &= ~SUPPORTED_1000baseT_Half;
1002 /* support both flow controls */
1003 phy->fc_request_control = (FLOW_CTRL_RX | FLOW_CTRL_TX);
1004 phydev->advertising &= ~(ADVERTISED_Pause | ADVERTISED_Asym_Pause);
1005 mii_adv = (u32)mii_advertise_flowctrl(phy->fc_request_control);
1006 phydev->advertising |= mii_adv_to_ethtool_adv_t(mii_adv);
1007 phy->fc_autoneg = phydev->autoneg;
1009 phy_start(phydev);
1010 phy_start_aneg(phydev);
1011 return 0;
1013 return_error:
1014 return ret;
1017 static void lan743x_rfe_open(struct lan743x_adapter *adapter)
1019 lan743x_csr_write(adapter, RFE_RSS_CFG,
1020 RFE_RSS_CFG_UDP_IPV6_EX_ |
1021 RFE_RSS_CFG_TCP_IPV6_EX_ |
1022 RFE_RSS_CFG_IPV6_EX_ |
1023 RFE_RSS_CFG_UDP_IPV6_ |
1024 RFE_RSS_CFG_TCP_IPV6_ |
1025 RFE_RSS_CFG_IPV6_ |
1026 RFE_RSS_CFG_UDP_IPV4_ |
1027 RFE_RSS_CFG_TCP_IPV4_ |
1028 RFE_RSS_CFG_IPV4_ |
1029 RFE_RSS_CFG_VALID_HASH_BITS_ |
1030 RFE_RSS_CFG_RSS_QUEUE_ENABLE_ |
1031 RFE_RSS_CFG_RSS_HASH_STORE_ |
1032 RFE_RSS_CFG_RSS_ENABLE_);
1035 static void lan743x_rfe_update_mac_address(struct lan743x_adapter *adapter)
1037 u8 *mac_addr;
1038 u32 mac_addr_hi = 0;
1039 u32 mac_addr_lo = 0;
1041 /* Add mac address to perfect Filter */
1042 mac_addr = adapter->mac_address;
1043 mac_addr_lo = ((((u32)(mac_addr[0])) << 0) |
1044 (((u32)(mac_addr[1])) << 8) |
1045 (((u32)(mac_addr[2])) << 16) |
1046 (((u32)(mac_addr[3])) << 24));
1047 mac_addr_hi = ((((u32)(mac_addr[4])) << 0) |
1048 (((u32)(mac_addr[5])) << 8));
1050 lan743x_csr_write(adapter, RFE_ADDR_FILT_LO(0), mac_addr_lo);
1051 lan743x_csr_write(adapter, RFE_ADDR_FILT_HI(0),
1052 mac_addr_hi | RFE_ADDR_FILT_HI_VALID_);
1055 static void lan743x_rfe_set_multicast(struct lan743x_adapter *adapter)
1057 struct net_device *netdev = adapter->netdev;
1058 u32 hash_table[DP_SEL_VHF_HASH_LEN];
1059 u32 rfctl;
1060 u32 data;
1062 rfctl = lan743x_csr_read(adapter, RFE_CTL);
1063 rfctl &= ~(RFE_CTL_AU_ | RFE_CTL_AM_ |
1064 RFE_CTL_DA_PERFECT_ | RFE_CTL_MCAST_HASH_);
1065 rfctl |= RFE_CTL_AB_;
1066 if (netdev->flags & IFF_PROMISC) {
1067 rfctl |= RFE_CTL_AM_ | RFE_CTL_AU_;
1068 } else {
1069 if (netdev->flags & IFF_ALLMULTI)
1070 rfctl |= RFE_CTL_AM_;
1073 memset(hash_table, 0, DP_SEL_VHF_HASH_LEN * sizeof(u32));
1074 if (netdev_mc_count(netdev)) {
1075 struct netdev_hw_addr *ha;
1076 int i;
1078 rfctl |= RFE_CTL_DA_PERFECT_;
1079 i = 1;
1080 netdev_for_each_mc_addr(ha, netdev) {
1081 /* set first 32 into Perfect Filter */
1082 if (i < 33) {
1083 lan743x_csr_write(adapter,
1084 RFE_ADDR_FILT_HI(i), 0);
1085 data = ha->addr[3];
1086 data = ha->addr[2] | (data << 8);
1087 data = ha->addr[1] | (data << 8);
1088 data = ha->addr[0] | (data << 8);
1089 lan743x_csr_write(adapter,
1090 RFE_ADDR_FILT_LO(i), data);
1091 data = ha->addr[5];
1092 data = ha->addr[4] | (data << 8);
1093 data |= RFE_ADDR_FILT_HI_VALID_;
1094 lan743x_csr_write(adapter,
1095 RFE_ADDR_FILT_HI(i), data);
1096 } else {
1097 u32 bitnum = (ether_crc(ETH_ALEN, ha->addr) >>
1098 23) & 0x1FF;
1099 hash_table[bitnum / 32] |= (1 << (bitnum % 32));
1100 rfctl |= RFE_CTL_MCAST_HASH_;
1102 i++;
1106 lan743x_dp_write(adapter, DP_SEL_RFE_RAM,
1107 DP_SEL_VHF_VLAN_LEN,
1108 DP_SEL_VHF_HASH_LEN, hash_table);
1109 lan743x_csr_write(adapter, RFE_CTL, rfctl);
1112 static int lan743x_dmac_init(struct lan743x_adapter *adapter)
1114 u32 data = 0;
1116 lan743x_csr_write(adapter, DMAC_CMD, DMAC_CMD_SWR_);
1117 lan743x_csr_wait_for_bit(adapter, DMAC_CMD, DMAC_CMD_SWR_,
1118 0, 1000, 20000, 100);
1119 switch (DEFAULT_DMA_DESCRIPTOR_SPACING) {
1120 case DMA_DESCRIPTOR_SPACING_16:
1121 data = DMAC_CFG_MAX_DSPACE_16_;
1122 break;
1123 case DMA_DESCRIPTOR_SPACING_32:
1124 data = DMAC_CFG_MAX_DSPACE_32_;
1125 break;
1126 case DMA_DESCRIPTOR_SPACING_64:
1127 data = DMAC_CFG_MAX_DSPACE_64_;
1128 break;
1129 case DMA_DESCRIPTOR_SPACING_128:
1130 data = DMAC_CFG_MAX_DSPACE_128_;
1131 break;
1132 default:
1133 return -EPERM;
1135 if (!(adapter->csr.flags & LAN743X_CSR_FLAG_IS_A0))
1136 data |= DMAC_CFG_COAL_EN_;
1137 data |= DMAC_CFG_CH_ARB_SEL_RX_HIGH_;
1138 data |= DMAC_CFG_MAX_READ_REQ_SET_(6);
1139 lan743x_csr_write(adapter, DMAC_CFG, data);
1140 data = DMAC_COAL_CFG_TIMER_LIMIT_SET_(1);
1141 data |= DMAC_COAL_CFG_TIMER_TX_START_;
1142 data |= DMAC_COAL_CFG_FLUSH_INTS_;
1143 data |= DMAC_COAL_CFG_INT_EXIT_COAL_;
1144 data |= DMAC_COAL_CFG_CSR_EXIT_COAL_;
1145 data |= DMAC_COAL_CFG_TX_THRES_SET_(0x0A);
1146 data |= DMAC_COAL_CFG_RX_THRES_SET_(0x0C);
1147 lan743x_csr_write(adapter, DMAC_COAL_CFG, data);
1148 data = DMAC_OBFF_TX_THRES_SET_(0x08);
1149 data |= DMAC_OBFF_RX_THRES_SET_(0x0A);
1150 lan743x_csr_write(adapter, DMAC_OBFF_CFG, data);
1151 return 0;
1154 static int lan743x_dmac_tx_get_state(struct lan743x_adapter *adapter,
1155 int tx_channel)
1157 u32 dmac_cmd = 0;
1159 dmac_cmd = lan743x_csr_read(adapter, DMAC_CMD);
1160 return DMAC_CHANNEL_STATE_SET((dmac_cmd &
1161 DMAC_CMD_START_T_(tx_channel)),
1162 (dmac_cmd &
1163 DMAC_CMD_STOP_T_(tx_channel)));
1166 static int lan743x_dmac_tx_wait_till_stopped(struct lan743x_adapter *adapter,
1167 int tx_channel)
1169 int timeout = 100;
1170 int result = 0;
1172 while (timeout &&
1173 ((result = lan743x_dmac_tx_get_state(adapter, tx_channel)) ==
1174 DMAC_CHANNEL_STATE_STOP_PENDING)) {
1175 usleep_range(1000, 20000);
1176 timeout--;
1178 if (result == DMAC_CHANNEL_STATE_STOP_PENDING)
1179 result = -ENODEV;
1180 return result;
1183 static int lan743x_dmac_rx_get_state(struct lan743x_adapter *adapter,
1184 int rx_channel)
1186 u32 dmac_cmd = 0;
1188 dmac_cmd = lan743x_csr_read(adapter, DMAC_CMD);
1189 return DMAC_CHANNEL_STATE_SET((dmac_cmd &
1190 DMAC_CMD_START_R_(rx_channel)),
1191 (dmac_cmd &
1192 DMAC_CMD_STOP_R_(rx_channel)));
1195 static int lan743x_dmac_rx_wait_till_stopped(struct lan743x_adapter *adapter,
1196 int rx_channel)
1198 int timeout = 100;
1199 int result = 0;
1201 while (timeout &&
1202 ((result = lan743x_dmac_rx_get_state(adapter, rx_channel)) ==
1203 DMAC_CHANNEL_STATE_STOP_PENDING)) {
1204 usleep_range(1000, 20000);
1205 timeout--;
1207 if (result == DMAC_CHANNEL_STATE_STOP_PENDING)
1208 result = -ENODEV;
1209 return result;
1212 static void lan743x_tx_release_desc(struct lan743x_tx *tx,
1213 int descriptor_index, bool cleanup)
1215 struct lan743x_tx_buffer_info *buffer_info = NULL;
1216 struct lan743x_tx_descriptor *descriptor = NULL;
1217 u32 descriptor_type = 0;
1218 bool ignore_sync;
1220 descriptor = &tx->ring_cpu_ptr[descriptor_index];
1221 buffer_info = &tx->buffer_info[descriptor_index];
1222 if (!(buffer_info->flags & TX_BUFFER_INFO_FLAG_ACTIVE))
1223 goto done;
1225 descriptor_type = (descriptor->data0) &
1226 TX_DESC_DATA0_DTYPE_MASK_;
1227 if (descriptor_type == TX_DESC_DATA0_DTYPE_DATA_)
1228 goto clean_up_data_descriptor;
1229 else
1230 goto clear_active;
1232 clean_up_data_descriptor:
1233 if (buffer_info->dma_ptr) {
1234 if (buffer_info->flags &
1235 TX_BUFFER_INFO_FLAG_SKB_FRAGMENT) {
1236 dma_unmap_page(&tx->adapter->pdev->dev,
1237 buffer_info->dma_ptr,
1238 buffer_info->buffer_length,
1239 DMA_TO_DEVICE);
1240 } else {
1241 dma_unmap_single(&tx->adapter->pdev->dev,
1242 buffer_info->dma_ptr,
1243 buffer_info->buffer_length,
1244 DMA_TO_DEVICE);
1246 buffer_info->dma_ptr = 0;
1247 buffer_info->buffer_length = 0;
1249 if (!buffer_info->skb)
1250 goto clear_active;
1252 if (!(buffer_info->flags & TX_BUFFER_INFO_FLAG_TIMESTAMP_REQUESTED)) {
1253 dev_kfree_skb(buffer_info->skb);
1254 goto clear_skb;
1257 if (cleanup) {
1258 lan743x_ptp_unrequest_tx_timestamp(tx->adapter);
1259 dev_kfree_skb(buffer_info->skb);
1260 } else {
1261 ignore_sync = (buffer_info->flags &
1262 TX_BUFFER_INFO_FLAG_IGNORE_SYNC) != 0;
1263 lan743x_ptp_tx_timestamp_skb(tx->adapter,
1264 buffer_info->skb, ignore_sync);
1267 clear_skb:
1268 buffer_info->skb = NULL;
1270 clear_active:
1271 buffer_info->flags &= ~TX_BUFFER_INFO_FLAG_ACTIVE;
1273 done:
1274 memset(buffer_info, 0, sizeof(*buffer_info));
1275 memset(descriptor, 0, sizeof(*descriptor));
1278 static int lan743x_tx_next_index(struct lan743x_tx *tx, int index)
1280 return ((++index) % tx->ring_size);
1283 static void lan743x_tx_release_completed_descriptors(struct lan743x_tx *tx)
1285 while ((*tx->head_cpu_ptr) != (tx->last_head)) {
1286 lan743x_tx_release_desc(tx, tx->last_head, false);
1287 tx->last_head = lan743x_tx_next_index(tx, tx->last_head);
1291 static void lan743x_tx_release_all_descriptors(struct lan743x_tx *tx)
1293 u32 original_head = 0;
1295 original_head = tx->last_head;
1296 do {
1297 lan743x_tx_release_desc(tx, tx->last_head, true);
1298 tx->last_head = lan743x_tx_next_index(tx, tx->last_head);
1299 } while (tx->last_head != original_head);
1300 memset(tx->ring_cpu_ptr, 0,
1301 sizeof(*tx->ring_cpu_ptr) * (tx->ring_size));
1302 memset(tx->buffer_info, 0,
1303 sizeof(*tx->buffer_info) * (tx->ring_size));
1306 static int lan743x_tx_get_desc_cnt(struct lan743x_tx *tx,
1307 struct sk_buff *skb)
1309 int result = 1; /* 1 for the main skb buffer */
1310 int nr_frags = 0;
1312 if (skb_is_gso(skb))
1313 result++; /* requires an extension descriptor */
1314 nr_frags = skb_shinfo(skb)->nr_frags;
1315 result += nr_frags; /* 1 for each fragment buffer */
1316 return result;
1319 static int lan743x_tx_get_avail_desc(struct lan743x_tx *tx)
1321 int last_head = tx->last_head;
1322 int last_tail = tx->last_tail;
1324 if (last_tail >= last_head)
1325 return tx->ring_size - last_tail + last_head - 1;
1326 else
1327 return last_head - last_tail - 1;
1330 void lan743x_tx_set_timestamping_mode(struct lan743x_tx *tx,
1331 bool enable_timestamping,
1332 bool enable_onestep_sync)
1334 if (enable_timestamping)
1335 tx->ts_flags |= TX_TS_FLAG_TIMESTAMPING_ENABLED;
1336 else
1337 tx->ts_flags &= ~TX_TS_FLAG_TIMESTAMPING_ENABLED;
1338 if (enable_onestep_sync)
1339 tx->ts_flags |= TX_TS_FLAG_ONE_STEP_SYNC;
1340 else
1341 tx->ts_flags &= ~TX_TS_FLAG_ONE_STEP_SYNC;
1344 static int lan743x_tx_frame_start(struct lan743x_tx *tx,
1345 unsigned char *first_buffer,
1346 unsigned int first_buffer_length,
1347 unsigned int frame_length,
1348 bool time_stamp,
1349 bool check_sum)
1351 /* called only from within lan743x_tx_xmit_frame.
1352 * assuming tx->ring_lock has already been acquired.
1354 struct lan743x_tx_descriptor *tx_descriptor = NULL;
1355 struct lan743x_tx_buffer_info *buffer_info = NULL;
1356 struct lan743x_adapter *adapter = tx->adapter;
1357 struct device *dev = &adapter->pdev->dev;
1358 dma_addr_t dma_ptr;
1360 tx->frame_flags |= TX_FRAME_FLAG_IN_PROGRESS;
1361 tx->frame_first = tx->last_tail;
1362 tx->frame_tail = tx->frame_first;
1364 tx_descriptor = &tx->ring_cpu_ptr[tx->frame_tail];
1365 buffer_info = &tx->buffer_info[tx->frame_tail];
1366 dma_ptr = dma_map_single(dev, first_buffer, first_buffer_length,
1367 DMA_TO_DEVICE);
1368 if (dma_mapping_error(dev, dma_ptr))
1369 return -ENOMEM;
1371 tx_descriptor->data1 = DMA_ADDR_LOW32(dma_ptr);
1372 tx_descriptor->data2 = DMA_ADDR_HIGH32(dma_ptr);
1373 tx_descriptor->data3 = (frame_length << 16) &
1374 TX_DESC_DATA3_FRAME_LENGTH_MSS_MASK_;
1376 buffer_info->skb = NULL;
1377 buffer_info->dma_ptr = dma_ptr;
1378 buffer_info->buffer_length = first_buffer_length;
1379 buffer_info->flags |= TX_BUFFER_INFO_FLAG_ACTIVE;
1381 tx->frame_data0 = (first_buffer_length &
1382 TX_DESC_DATA0_BUF_LENGTH_MASK_) |
1383 TX_DESC_DATA0_DTYPE_DATA_ |
1384 TX_DESC_DATA0_FS_ |
1385 TX_DESC_DATA0_FCS_;
1386 if (time_stamp)
1387 tx->frame_data0 |= TX_DESC_DATA0_TSE_;
1389 if (check_sum)
1390 tx->frame_data0 |= TX_DESC_DATA0_ICE_ |
1391 TX_DESC_DATA0_IPE_ |
1392 TX_DESC_DATA0_TPE_;
1394 /* data0 will be programmed in one of other frame assembler functions */
1395 return 0;
1398 static void lan743x_tx_frame_add_lso(struct lan743x_tx *tx,
1399 unsigned int frame_length,
1400 int nr_frags)
1402 /* called only from within lan743x_tx_xmit_frame.
1403 * assuming tx->ring_lock has already been acquired.
1405 struct lan743x_tx_descriptor *tx_descriptor = NULL;
1406 struct lan743x_tx_buffer_info *buffer_info = NULL;
1408 /* wrap up previous descriptor */
1409 tx->frame_data0 |= TX_DESC_DATA0_EXT_;
1410 if (nr_frags <= 0) {
1411 tx->frame_data0 |= TX_DESC_DATA0_LS_;
1412 tx->frame_data0 |= TX_DESC_DATA0_IOC_;
1414 tx_descriptor = &tx->ring_cpu_ptr[tx->frame_tail];
1415 tx_descriptor->data0 = tx->frame_data0;
1417 /* move to next descriptor */
1418 tx->frame_tail = lan743x_tx_next_index(tx, tx->frame_tail);
1419 tx_descriptor = &tx->ring_cpu_ptr[tx->frame_tail];
1420 buffer_info = &tx->buffer_info[tx->frame_tail];
1422 /* add extension descriptor */
1423 tx_descriptor->data1 = 0;
1424 tx_descriptor->data2 = 0;
1425 tx_descriptor->data3 = 0;
1427 buffer_info->skb = NULL;
1428 buffer_info->dma_ptr = 0;
1429 buffer_info->buffer_length = 0;
1430 buffer_info->flags |= TX_BUFFER_INFO_FLAG_ACTIVE;
1432 tx->frame_data0 = (frame_length & TX_DESC_DATA0_EXT_PAY_LENGTH_MASK_) |
1433 TX_DESC_DATA0_DTYPE_EXT_ |
1434 TX_DESC_DATA0_EXT_LSO_;
1436 /* data0 will be programmed in one of other frame assembler functions */
1439 static int lan743x_tx_frame_add_fragment(struct lan743x_tx *tx,
1440 const struct skb_frag_struct *fragment,
1441 unsigned int frame_length)
1443 /* called only from within lan743x_tx_xmit_frame
1444 * assuming tx->ring_lock has already been acquired
1446 struct lan743x_tx_descriptor *tx_descriptor = NULL;
1447 struct lan743x_tx_buffer_info *buffer_info = NULL;
1448 struct lan743x_adapter *adapter = tx->adapter;
1449 struct device *dev = &adapter->pdev->dev;
1450 unsigned int fragment_length = 0;
1451 dma_addr_t dma_ptr;
1453 fragment_length = skb_frag_size(fragment);
1454 if (!fragment_length)
1455 return 0;
1457 /* wrap up previous descriptor */
1458 tx_descriptor = &tx->ring_cpu_ptr[tx->frame_tail];
1459 tx_descriptor->data0 = tx->frame_data0;
1461 /* move to next descriptor */
1462 tx->frame_tail = lan743x_tx_next_index(tx, tx->frame_tail);
1463 tx_descriptor = &tx->ring_cpu_ptr[tx->frame_tail];
1464 buffer_info = &tx->buffer_info[tx->frame_tail];
1465 dma_ptr = skb_frag_dma_map(dev, fragment,
1466 0, fragment_length,
1467 DMA_TO_DEVICE);
1468 if (dma_mapping_error(dev, dma_ptr)) {
1469 int desc_index;
1471 /* cleanup all previously setup descriptors */
1472 desc_index = tx->frame_first;
1473 while (desc_index != tx->frame_tail) {
1474 lan743x_tx_release_desc(tx, desc_index, true);
1475 desc_index = lan743x_tx_next_index(tx, desc_index);
1477 dma_wmb();
1478 tx->frame_flags &= ~TX_FRAME_FLAG_IN_PROGRESS;
1479 tx->frame_first = 0;
1480 tx->frame_data0 = 0;
1481 tx->frame_tail = 0;
1482 return -ENOMEM;
1485 tx_descriptor->data1 = DMA_ADDR_LOW32(dma_ptr);
1486 tx_descriptor->data2 = DMA_ADDR_HIGH32(dma_ptr);
1487 tx_descriptor->data3 = (frame_length << 16) &
1488 TX_DESC_DATA3_FRAME_LENGTH_MSS_MASK_;
1490 buffer_info->skb = NULL;
1491 buffer_info->dma_ptr = dma_ptr;
1492 buffer_info->buffer_length = fragment_length;
1493 buffer_info->flags |= TX_BUFFER_INFO_FLAG_ACTIVE;
1494 buffer_info->flags |= TX_BUFFER_INFO_FLAG_SKB_FRAGMENT;
1496 tx->frame_data0 = (fragment_length & TX_DESC_DATA0_BUF_LENGTH_MASK_) |
1497 TX_DESC_DATA0_DTYPE_DATA_ |
1498 TX_DESC_DATA0_FCS_;
1500 /* data0 will be programmed in one of other frame assembler functions */
1501 return 0;
1504 static void lan743x_tx_frame_end(struct lan743x_tx *tx,
1505 struct sk_buff *skb,
1506 bool time_stamp,
1507 bool ignore_sync)
1509 /* called only from within lan743x_tx_xmit_frame
1510 * assuming tx->ring_lock has already been acquired
1512 struct lan743x_tx_descriptor *tx_descriptor = NULL;
1513 struct lan743x_tx_buffer_info *buffer_info = NULL;
1514 struct lan743x_adapter *adapter = tx->adapter;
1515 u32 tx_tail_flags = 0;
1517 /* wrap up previous descriptor */
1518 if ((tx->frame_data0 & TX_DESC_DATA0_DTYPE_MASK_) ==
1519 TX_DESC_DATA0_DTYPE_DATA_) {
1520 tx->frame_data0 |= TX_DESC_DATA0_LS_;
1521 tx->frame_data0 |= TX_DESC_DATA0_IOC_;
1524 tx_descriptor = &tx->ring_cpu_ptr[tx->frame_tail];
1525 buffer_info = &tx->buffer_info[tx->frame_tail];
1526 buffer_info->skb = skb;
1527 if (time_stamp)
1528 buffer_info->flags |= TX_BUFFER_INFO_FLAG_TIMESTAMP_REQUESTED;
1529 if (ignore_sync)
1530 buffer_info->flags |= TX_BUFFER_INFO_FLAG_IGNORE_SYNC;
1532 tx_descriptor->data0 = tx->frame_data0;
1533 tx->frame_tail = lan743x_tx_next_index(tx, tx->frame_tail);
1534 tx->last_tail = tx->frame_tail;
1536 dma_wmb();
1538 if (tx->vector_flags & LAN743X_VECTOR_FLAG_VECTOR_ENABLE_AUTO_SET)
1539 tx_tail_flags |= TX_TAIL_SET_TOP_INT_VEC_EN_;
1540 if (tx->vector_flags & LAN743X_VECTOR_FLAG_SOURCE_ENABLE_AUTO_SET)
1541 tx_tail_flags |= TX_TAIL_SET_DMAC_INT_EN_ |
1542 TX_TAIL_SET_TOP_INT_EN_;
1544 lan743x_csr_write(adapter, TX_TAIL(tx->channel_number),
1545 tx_tail_flags | tx->frame_tail);
1546 tx->frame_flags &= ~TX_FRAME_FLAG_IN_PROGRESS;
1549 static netdev_tx_t lan743x_tx_xmit_frame(struct lan743x_tx *tx,
1550 struct sk_buff *skb)
1552 int required_number_of_descriptors = 0;
1553 unsigned int start_frame_length = 0;
1554 unsigned int frame_length = 0;
1555 unsigned int head_length = 0;
1556 unsigned long irq_flags = 0;
1557 bool do_timestamp = false;
1558 bool ignore_sync = false;
1559 int nr_frags = 0;
1560 bool gso = false;
1561 int j;
1563 required_number_of_descriptors = lan743x_tx_get_desc_cnt(tx, skb);
1565 spin_lock_irqsave(&tx->ring_lock, irq_flags);
1566 if (required_number_of_descriptors >
1567 lan743x_tx_get_avail_desc(tx)) {
1568 if (required_number_of_descriptors > (tx->ring_size - 1)) {
1569 dev_kfree_skb(skb);
1570 } else {
1571 /* save to overflow buffer */
1572 tx->overflow_skb = skb;
1573 netif_stop_queue(tx->adapter->netdev);
1575 goto unlock;
1578 /* space available, transmit skb */
1579 if ((skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) &&
1580 (tx->ts_flags & TX_TS_FLAG_TIMESTAMPING_ENABLED) &&
1581 (lan743x_ptp_request_tx_timestamp(tx->adapter))) {
1582 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
1583 do_timestamp = true;
1584 if (tx->ts_flags & TX_TS_FLAG_ONE_STEP_SYNC)
1585 ignore_sync = true;
1587 head_length = skb_headlen(skb);
1588 frame_length = skb_pagelen(skb);
1589 nr_frags = skb_shinfo(skb)->nr_frags;
1590 start_frame_length = frame_length;
1591 gso = skb_is_gso(skb);
1592 if (gso) {
1593 start_frame_length = max(skb_shinfo(skb)->gso_size,
1594 (unsigned short)8);
1597 if (lan743x_tx_frame_start(tx,
1598 skb->data, head_length,
1599 start_frame_length,
1600 do_timestamp,
1601 skb->ip_summed == CHECKSUM_PARTIAL)) {
1602 dev_kfree_skb(skb);
1603 goto unlock;
1606 if (gso)
1607 lan743x_tx_frame_add_lso(tx, frame_length, nr_frags);
1609 if (nr_frags <= 0)
1610 goto finish;
1612 for (j = 0; j < nr_frags; j++) {
1613 const struct skb_frag_struct *frag;
1615 frag = &(skb_shinfo(skb)->frags[j]);
1616 if (lan743x_tx_frame_add_fragment(tx, frag, frame_length)) {
1617 /* upon error no need to call
1618 * lan743x_tx_frame_end
1619 * frame assembler clean up was performed inside
1620 * lan743x_tx_frame_add_fragment
1622 dev_kfree_skb(skb);
1623 goto unlock;
1627 finish:
1628 lan743x_tx_frame_end(tx, skb, do_timestamp, ignore_sync);
1630 unlock:
1631 spin_unlock_irqrestore(&tx->ring_lock, irq_flags);
1632 return NETDEV_TX_OK;
1635 static int lan743x_tx_napi_poll(struct napi_struct *napi, int weight)
1637 struct lan743x_tx *tx = container_of(napi, struct lan743x_tx, napi);
1638 struct lan743x_adapter *adapter = tx->adapter;
1639 bool start_transmitter = false;
1640 unsigned long irq_flags = 0;
1641 u32 ioc_bit = 0;
1642 u32 int_sts = 0;
1644 ioc_bit = DMAC_INT_BIT_TX_IOC_(tx->channel_number);
1645 int_sts = lan743x_csr_read(adapter, DMAC_INT_STS);
1646 if (tx->vector_flags & LAN743X_VECTOR_FLAG_SOURCE_STATUS_W2C)
1647 lan743x_csr_write(adapter, DMAC_INT_STS, ioc_bit);
1648 spin_lock_irqsave(&tx->ring_lock, irq_flags);
1650 /* clean up tx ring */
1651 lan743x_tx_release_completed_descriptors(tx);
1652 if (netif_queue_stopped(adapter->netdev)) {
1653 if (tx->overflow_skb) {
1654 if (lan743x_tx_get_desc_cnt(tx, tx->overflow_skb) <=
1655 lan743x_tx_get_avail_desc(tx))
1656 start_transmitter = true;
1657 } else {
1658 netif_wake_queue(adapter->netdev);
1661 spin_unlock_irqrestore(&tx->ring_lock, irq_flags);
1663 if (start_transmitter) {
1664 /* space is now available, transmit overflow skb */
1665 lan743x_tx_xmit_frame(tx, tx->overflow_skb);
1666 tx->overflow_skb = NULL;
1667 netif_wake_queue(adapter->netdev);
1670 if (!napi_complete(napi))
1671 goto done;
1673 /* enable isr */
1674 lan743x_csr_write(adapter, INT_EN_SET,
1675 INT_BIT_DMA_TX_(tx->channel_number));
1676 lan743x_csr_read(adapter, INT_STS);
1678 done:
1679 return 0;
1682 static void lan743x_tx_ring_cleanup(struct lan743x_tx *tx)
1684 if (tx->head_cpu_ptr) {
1685 pci_free_consistent(tx->adapter->pdev,
1686 sizeof(*tx->head_cpu_ptr),
1687 (void *)(tx->head_cpu_ptr),
1688 tx->head_dma_ptr);
1689 tx->head_cpu_ptr = NULL;
1690 tx->head_dma_ptr = 0;
1692 kfree(tx->buffer_info);
1693 tx->buffer_info = NULL;
1695 if (tx->ring_cpu_ptr) {
1696 pci_free_consistent(tx->adapter->pdev,
1697 tx->ring_allocation_size,
1698 tx->ring_cpu_ptr,
1699 tx->ring_dma_ptr);
1700 tx->ring_allocation_size = 0;
1701 tx->ring_cpu_ptr = NULL;
1702 tx->ring_dma_ptr = 0;
1704 tx->ring_size = 0;
1707 static int lan743x_tx_ring_init(struct lan743x_tx *tx)
1709 size_t ring_allocation_size = 0;
1710 void *cpu_ptr = NULL;
1711 dma_addr_t dma_ptr;
1712 int ret = -ENOMEM;
1714 tx->ring_size = LAN743X_TX_RING_SIZE;
1715 if (tx->ring_size & ~TX_CFG_B_TX_RING_LEN_MASK_) {
1716 ret = -EINVAL;
1717 goto cleanup;
1719 ring_allocation_size = ALIGN(tx->ring_size *
1720 sizeof(struct lan743x_tx_descriptor),
1721 PAGE_SIZE);
1722 dma_ptr = 0;
1723 cpu_ptr = pci_zalloc_consistent(tx->adapter->pdev,
1724 ring_allocation_size, &dma_ptr);
1725 if (!cpu_ptr) {
1726 ret = -ENOMEM;
1727 goto cleanup;
1730 tx->ring_allocation_size = ring_allocation_size;
1731 tx->ring_cpu_ptr = (struct lan743x_tx_descriptor *)cpu_ptr;
1732 tx->ring_dma_ptr = dma_ptr;
1734 cpu_ptr = kcalloc(tx->ring_size, sizeof(*tx->buffer_info), GFP_KERNEL);
1735 if (!cpu_ptr) {
1736 ret = -ENOMEM;
1737 goto cleanup;
1739 tx->buffer_info = (struct lan743x_tx_buffer_info *)cpu_ptr;
1740 dma_ptr = 0;
1741 cpu_ptr = pci_zalloc_consistent(tx->adapter->pdev,
1742 sizeof(*tx->head_cpu_ptr), &dma_ptr);
1743 if (!cpu_ptr) {
1744 ret = -ENOMEM;
1745 goto cleanup;
1748 tx->head_cpu_ptr = cpu_ptr;
1749 tx->head_dma_ptr = dma_ptr;
1750 if (tx->head_dma_ptr & 0x3) {
1751 ret = -ENOMEM;
1752 goto cleanup;
1755 return 0;
1757 cleanup:
1758 lan743x_tx_ring_cleanup(tx);
1759 return ret;
1762 static void lan743x_tx_close(struct lan743x_tx *tx)
1764 struct lan743x_adapter *adapter = tx->adapter;
1766 lan743x_csr_write(adapter,
1767 DMAC_CMD,
1768 DMAC_CMD_STOP_T_(tx->channel_number));
1769 lan743x_dmac_tx_wait_till_stopped(adapter, tx->channel_number);
1771 lan743x_csr_write(adapter,
1772 DMAC_INT_EN_CLR,
1773 DMAC_INT_BIT_TX_IOC_(tx->channel_number));
1774 lan743x_csr_write(adapter, INT_EN_CLR,
1775 INT_BIT_DMA_TX_(tx->channel_number));
1776 napi_disable(&tx->napi);
1777 netif_napi_del(&tx->napi);
1779 lan743x_csr_write(adapter, FCT_TX_CTL,
1780 FCT_TX_CTL_DIS_(tx->channel_number));
1781 lan743x_csr_wait_for_bit(adapter, FCT_TX_CTL,
1782 FCT_TX_CTL_EN_(tx->channel_number),
1783 0, 1000, 20000, 100);
1785 lan743x_tx_release_all_descriptors(tx);
1787 if (tx->overflow_skb) {
1788 dev_kfree_skb(tx->overflow_skb);
1789 tx->overflow_skb = NULL;
1792 lan743x_tx_ring_cleanup(tx);
1795 static int lan743x_tx_open(struct lan743x_tx *tx)
1797 struct lan743x_adapter *adapter = NULL;
1798 u32 data = 0;
1799 int ret;
1801 adapter = tx->adapter;
1802 ret = lan743x_tx_ring_init(tx);
1803 if (ret)
1804 return ret;
1806 /* initialize fifo */
1807 lan743x_csr_write(adapter, FCT_TX_CTL,
1808 FCT_TX_CTL_RESET_(tx->channel_number));
1809 lan743x_csr_wait_for_bit(adapter, FCT_TX_CTL,
1810 FCT_TX_CTL_RESET_(tx->channel_number),
1811 0, 1000, 20000, 100);
1813 /* enable fifo */
1814 lan743x_csr_write(adapter, FCT_TX_CTL,
1815 FCT_TX_CTL_EN_(tx->channel_number));
1817 /* reset tx channel */
1818 lan743x_csr_write(adapter, DMAC_CMD,
1819 DMAC_CMD_TX_SWR_(tx->channel_number));
1820 lan743x_csr_wait_for_bit(adapter, DMAC_CMD,
1821 DMAC_CMD_TX_SWR_(tx->channel_number),
1822 0, 1000, 20000, 100);
1824 /* Write TX_BASE_ADDR */
1825 lan743x_csr_write(adapter,
1826 TX_BASE_ADDRH(tx->channel_number),
1827 DMA_ADDR_HIGH32(tx->ring_dma_ptr));
1828 lan743x_csr_write(adapter,
1829 TX_BASE_ADDRL(tx->channel_number),
1830 DMA_ADDR_LOW32(tx->ring_dma_ptr));
1832 /* Write TX_CFG_B */
1833 data = lan743x_csr_read(adapter, TX_CFG_B(tx->channel_number));
1834 data &= ~TX_CFG_B_TX_RING_LEN_MASK_;
1835 data |= ((tx->ring_size) & TX_CFG_B_TX_RING_LEN_MASK_);
1836 if (!(adapter->csr.flags & LAN743X_CSR_FLAG_IS_A0))
1837 data |= TX_CFG_B_TDMABL_512_;
1838 lan743x_csr_write(adapter, TX_CFG_B(tx->channel_number), data);
1840 /* Write TX_CFG_A */
1841 data = TX_CFG_A_TX_TMR_HPWB_SEL_IOC_ | TX_CFG_A_TX_HP_WB_EN_;
1842 if (!(adapter->csr.flags & LAN743X_CSR_FLAG_IS_A0)) {
1843 data |= TX_CFG_A_TX_HP_WB_ON_INT_TMR_;
1844 data |= TX_CFG_A_TX_PF_THRES_SET_(0x10);
1845 data |= TX_CFG_A_TX_PF_PRI_THRES_SET_(0x04);
1846 data |= TX_CFG_A_TX_HP_WB_THRES_SET_(0x07);
1848 lan743x_csr_write(adapter, TX_CFG_A(tx->channel_number), data);
1850 /* Write TX_HEAD_WRITEBACK_ADDR */
1851 lan743x_csr_write(adapter,
1852 TX_HEAD_WRITEBACK_ADDRH(tx->channel_number),
1853 DMA_ADDR_HIGH32(tx->head_dma_ptr));
1854 lan743x_csr_write(adapter,
1855 TX_HEAD_WRITEBACK_ADDRL(tx->channel_number),
1856 DMA_ADDR_LOW32(tx->head_dma_ptr));
1858 /* set last head */
1859 tx->last_head = lan743x_csr_read(adapter, TX_HEAD(tx->channel_number));
1861 /* write TX_TAIL */
1862 tx->last_tail = 0;
1863 lan743x_csr_write(adapter, TX_TAIL(tx->channel_number),
1864 (u32)(tx->last_tail));
1865 tx->vector_flags = lan743x_intr_get_vector_flags(adapter,
1866 INT_BIT_DMA_TX_
1867 (tx->channel_number));
1868 netif_tx_napi_add(adapter->netdev,
1869 &tx->napi, lan743x_tx_napi_poll,
1870 tx->ring_size - 1);
1871 napi_enable(&tx->napi);
1873 data = 0;
1874 if (tx->vector_flags & LAN743X_VECTOR_FLAG_SOURCE_ENABLE_AUTO_CLEAR)
1875 data |= TX_CFG_C_TX_TOP_INT_EN_AUTO_CLR_;
1876 if (tx->vector_flags & LAN743X_VECTOR_FLAG_SOURCE_STATUS_AUTO_CLEAR)
1877 data |= TX_CFG_C_TX_DMA_INT_STS_AUTO_CLR_;
1878 if (tx->vector_flags & LAN743X_VECTOR_FLAG_SOURCE_STATUS_R2C)
1879 data |= TX_CFG_C_TX_INT_STS_R2C_MODE_MASK_;
1880 if (tx->vector_flags & LAN743X_VECTOR_FLAG_SOURCE_ENABLE_R2C)
1881 data |= TX_CFG_C_TX_INT_EN_R2C_;
1882 lan743x_csr_write(adapter, TX_CFG_C(tx->channel_number), data);
1884 if (!(tx->vector_flags & LAN743X_VECTOR_FLAG_SOURCE_ENABLE_AUTO_SET))
1885 lan743x_csr_write(adapter, INT_EN_SET,
1886 INT_BIT_DMA_TX_(tx->channel_number));
1887 lan743x_csr_write(adapter, DMAC_INT_EN_SET,
1888 DMAC_INT_BIT_TX_IOC_(tx->channel_number));
1890 /* start dmac channel */
1891 lan743x_csr_write(adapter, DMAC_CMD,
1892 DMAC_CMD_START_T_(tx->channel_number));
1893 return 0;
1896 static int lan743x_rx_next_index(struct lan743x_rx *rx, int index)
1898 return ((++index) % rx->ring_size);
1901 static struct sk_buff *lan743x_rx_allocate_skb(struct lan743x_rx *rx)
1903 int length = 0;
1905 length = (LAN743X_MAX_FRAME_SIZE + ETH_HLEN + 4 + RX_HEAD_PADDING);
1906 return __netdev_alloc_skb(rx->adapter->netdev,
1907 length, GFP_ATOMIC | GFP_DMA);
1910 static int lan743x_rx_init_ring_element(struct lan743x_rx *rx, int index,
1911 struct sk_buff *skb)
1913 struct lan743x_rx_buffer_info *buffer_info;
1914 struct lan743x_rx_descriptor *descriptor;
1915 int length = 0;
1917 length = (LAN743X_MAX_FRAME_SIZE + ETH_HLEN + 4 + RX_HEAD_PADDING);
1918 descriptor = &rx->ring_cpu_ptr[index];
1919 buffer_info = &rx->buffer_info[index];
1920 buffer_info->skb = skb;
1921 if (!(buffer_info->skb))
1922 return -ENOMEM;
1923 buffer_info->dma_ptr = dma_map_single(&rx->adapter->pdev->dev,
1924 buffer_info->skb->data,
1925 length,
1926 DMA_FROM_DEVICE);
1927 if (dma_mapping_error(&rx->adapter->pdev->dev,
1928 buffer_info->dma_ptr)) {
1929 buffer_info->dma_ptr = 0;
1930 return -ENOMEM;
1933 buffer_info->buffer_length = length;
1934 descriptor->data1 = DMA_ADDR_LOW32(buffer_info->dma_ptr);
1935 descriptor->data2 = DMA_ADDR_HIGH32(buffer_info->dma_ptr);
1936 descriptor->data3 = 0;
1937 descriptor->data0 = (RX_DESC_DATA0_OWN_ |
1938 (length & RX_DESC_DATA0_BUF_LENGTH_MASK_));
1939 skb_reserve(buffer_info->skb, RX_HEAD_PADDING);
1941 return 0;
1944 static void lan743x_rx_reuse_ring_element(struct lan743x_rx *rx, int index)
1946 struct lan743x_rx_buffer_info *buffer_info;
1947 struct lan743x_rx_descriptor *descriptor;
1949 descriptor = &rx->ring_cpu_ptr[index];
1950 buffer_info = &rx->buffer_info[index];
1952 descriptor->data1 = DMA_ADDR_LOW32(buffer_info->dma_ptr);
1953 descriptor->data2 = DMA_ADDR_HIGH32(buffer_info->dma_ptr);
1954 descriptor->data3 = 0;
1955 descriptor->data0 = (RX_DESC_DATA0_OWN_ |
1956 ((buffer_info->buffer_length) &
1957 RX_DESC_DATA0_BUF_LENGTH_MASK_));
1960 static void lan743x_rx_release_ring_element(struct lan743x_rx *rx, int index)
1962 struct lan743x_rx_buffer_info *buffer_info;
1963 struct lan743x_rx_descriptor *descriptor;
1965 descriptor = &rx->ring_cpu_ptr[index];
1966 buffer_info = &rx->buffer_info[index];
1968 memset(descriptor, 0, sizeof(*descriptor));
1970 if (buffer_info->dma_ptr) {
1971 dma_unmap_single(&rx->adapter->pdev->dev,
1972 buffer_info->dma_ptr,
1973 buffer_info->buffer_length,
1974 DMA_FROM_DEVICE);
1975 buffer_info->dma_ptr = 0;
1978 if (buffer_info->skb) {
1979 dev_kfree_skb(buffer_info->skb);
1980 buffer_info->skb = NULL;
1983 memset(buffer_info, 0, sizeof(*buffer_info));
1986 static int lan743x_rx_process_packet(struct lan743x_rx *rx)
1988 struct skb_shared_hwtstamps *hwtstamps = NULL;
1989 int result = RX_PROCESS_RESULT_NOTHING_TO_DO;
1990 struct lan743x_rx_buffer_info *buffer_info;
1991 struct lan743x_rx_descriptor *descriptor;
1992 int current_head_index = -1;
1993 int extension_index = -1;
1994 int first_index = -1;
1995 int last_index = -1;
1997 current_head_index = *rx->head_cpu_ptr;
1998 if (current_head_index < 0 || current_head_index >= rx->ring_size)
1999 goto done;
2001 if (rx->last_head < 0 || rx->last_head >= rx->ring_size)
2002 goto done;
2004 if (rx->last_head != current_head_index) {
2005 descriptor = &rx->ring_cpu_ptr[rx->last_head];
2006 if (descriptor->data0 & RX_DESC_DATA0_OWN_)
2007 goto done;
2009 if (!(descriptor->data0 & RX_DESC_DATA0_FS_))
2010 goto done;
2012 first_index = rx->last_head;
2013 if (descriptor->data0 & RX_DESC_DATA0_LS_) {
2014 last_index = rx->last_head;
2015 } else {
2016 int index;
2018 index = lan743x_rx_next_index(rx, first_index);
2019 while (index != current_head_index) {
2020 descriptor = &rx->ring_cpu_ptr[index];
2021 if (descriptor->data0 & RX_DESC_DATA0_OWN_)
2022 goto done;
2024 if (descriptor->data0 & RX_DESC_DATA0_LS_) {
2025 last_index = index;
2026 break;
2028 index = lan743x_rx_next_index(rx, index);
2031 if (last_index >= 0) {
2032 descriptor = &rx->ring_cpu_ptr[last_index];
2033 if (descriptor->data0 & RX_DESC_DATA0_EXT_) {
2034 /* extension is expected to follow */
2035 int index = lan743x_rx_next_index(rx,
2036 last_index);
2037 if (index != current_head_index) {
2038 descriptor = &rx->ring_cpu_ptr[index];
2039 if (descriptor->data0 &
2040 RX_DESC_DATA0_OWN_) {
2041 goto done;
2043 if (descriptor->data0 &
2044 RX_DESC_DATA0_EXT_) {
2045 extension_index = index;
2046 } else {
2047 goto done;
2049 } else {
2050 /* extension is not yet available */
2051 /* prevent processing of this packet */
2052 first_index = -1;
2053 last_index = -1;
2058 if (first_index >= 0 && last_index >= 0) {
2059 int real_last_index = last_index;
2060 struct sk_buff *skb = NULL;
2061 u32 ts_sec = 0;
2062 u32 ts_nsec = 0;
2064 /* packet is available */
2065 if (first_index == last_index) {
2066 /* single buffer packet */
2067 struct sk_buff *new_skb = NULL;
2068 int packet_length;
2070 new_skb = lan743x_rx_allocate_skb(rx);
2071 if (!new_skb) {
2072 /* failed to allocate next skb.
2073 * Memory is very low.
2074 * Drop this packet and reuse buffer.
2076 lan743x_rx_reuse_ring_element(rx, first_index);
2077 goto process_extension;
2080 buffer_info = &rx->buffer_info[first_index];
2081 skb = buffer_info->skb;
2082 descriptor = &rx->ring_cpu_ptr[first_index];
2084 /* unmap from dma */
2085 if (buffer_info->dma_ptr) {
2086 dma_unmap_single(&rx->adapter->pdev->dev,
2087 buffer_info->dma_ptr,
2088 buffer_info->buffer_length,
2089 DMA_FROM_DEVICE);
2090 buffer_info->dma_ptr = 0;
2091 buffer_info->buffer_length = 0;
2093 buffer_info->skb = NULL;
2094 packet_length = RX_DESC_DATA0_FRAME_LENGTH_GET_
2095 (descriptor->data0);
2096 skb_put(skb, packet_length - 4);
2097 skb->protocol = eth_type_trans(skb,
2098 rx->adapter->netdev);
2099 lan743x_rx_init_ring_element(rx, first_index, new_skb);
2100 } else {
2101 int index = first_index;
2103 /* multi buffer packet not supported */
2104 /* this should not happen since
2105 * buffers are allocated to be at least jumbo size
2108 /* clean up buffers */
2109 if (first_index <= last_index) {
2110 while ((index >= first_index) &&
2111 (index <= last_index)) {
2112 lan743x_rx_reuse_ring_element(rx,
2113 index);
2114 index = lan743x_rx_next_index(rx,
2115 index);
2117 } else {
2118 while ((index >= first_index) ||
2119 (index <= last_index)) {
2120 lan743x_rx_reuse_ring_element(rx,
2121 index);
2122 index = lan743x_rx_next_index(rx,
2123 index);
2128 process_extension:
2129 if (extension_index >= 0) {
2130 descriptor = &rx->ring_cpu_ptr[extension_index];
2131 buffer_info = &rx->buffer_info[extension_index];
2133 ts_sec = descriptor->data1;
2134 ts_nsec = (descriptor->data2 &
2135 RX_DESC_DATA2_TS_NS_MASK_);
2136 lan743x_rx_reuse_ring_element(rx, extension_index);
2137 real_last_index = extension_index;
2140 if (!skb) {
2141 result = RX_PROCESS_RESULT_PACKET_DROPPED;
2142 goto move_forward;
2145 if (extension_index < 0)
2146 goto pass_packet_to_os;
2147 hwtstamps = skb_hwtstamps(skb);
2148 if (hwtstamps)
2149 hwtstamps->hwtstamp = ktime_set(ts_sec, ts_nsec);
2151 pass_packet_to_os:
2152 /* pass packet to OS */
2153 napi_gro_receive(&rx->napi, skb);
2154 result = RX_PROCESS_RESULT_PACKET_RECEIVED;
2156 move_forward:
2157 /* push tail and head forward */
2158 rx->last_tail = real_last_index;
2159 rx->last_head = lan743x_rx_next_index(rx, real_last_index);
2161 done:
2162 return result;
2165 static int lan743x_rx_napi_poll(struct napi_struct *napi, int weight)
2167 struct lan743x_rx *rx = container_of(napi, struct lan743x_rx, napi);
2168 struct lan743x_adapter *adapter = rx->adapter;
2169 u32 rx_tail_flags = 0;
2170 int count;
2172 if (rx->vector_flags & LAN743X_VECTOR_FLAG_SOURCE_STATUS_W2C) {
2173 /* clear int status bit before reading packet */
2174 lan743x_csr_write(adapter, DMAC_INT_STS,
2175 DMAC_INT_BIT_RXFRM_(rx->channel_number));
2177 count = 0;
2178 while (count < weight) {
2179 int rx_process_result = -1;
2181 rx_process_result = lan743x_rx_process_packet(rx);
2182 if (rx_process_result == RX_PROCESS_RESULT_PACKET_RECEIVED) {
2183 count++;
2184 } else if (rx_process_result ==
2185 RX_PROCESS_RESULT_NOTHING_TO_DO) {
2186 break;
2187 } else if (rx_process_result ==
2188 RX_PROCESS_RESULT_PACKET_DROPPED) {
2189 continue;
2192 rx->frame_count += count;
2193 if (count == weight)
2194 goto done;
2196 if (!napi_complete_done(napi, count))
2197 goto done;
2199 if (rx->vector_flags & LAN743X_VECTOR_FLAG_VECTOR_ENABLE_AUTO_SET)
2200 rx_tail_flags |= RX_TAIL_SET_TOP_INT_VEC_EN_;
2201 if (rx->vector_flags & LAN743X_VECTOR_FLAG_SOURCE_ENABLE_AUTO_SET) {
2202 rx_tail_flags |= RX_TAIL_SET_TOP_INT_EN_;
2203 } else {
2204 lan743x_csr_write(adapter, INT_EN_SET,
2205 INT_BIT_DMA_RX_(rx->channel_number));
2208 /* update RX_TAIL */
2209 lan743x_csr_write(adapter, RX_TAIL(rx->channel_number),
2210 rx_tail_flags | rx->last_tail);
2211 done:
2212 return count;
2215 static void lan743x_rx_ring_cleanup(struct lan743x_rx *rx)
2217 if (rx->buffer_info && rx->ring_cpu_ptr) {
2218 int index;
2220 for (index = 0; index < rx->ring_size; index++)
2221 lan743x_rx_release_ring_element(rx, index);
2224 if (rx->head_cpu_ptr) {
2225 pci_free_consistent(rx->adapter->pdev,
2226 sizeof(*rx->head_cpu_ptr),
2227 rx->head_cpu_ptr,
2228 rx->head_dma_ptr);
2229 rx->head_cpu_ptr = NULL;
2230 rx->head_dma_ptr = 0;
2233 kfree(rx->buffer_info);
2234 rx->buffer_info = NULL;
2236 if (rx->ring_cpu_ptr) {
2237 pci_free_consistent(rx->adapter->pdev,
2238 rx->ring_allocation_size,
2239 rx->ring_cpu_ptr,
2240 rx->ring_dma_ptr);
2241 rx->ring_allocation_size = 0;
2242 rx->ring_cpu_ptr = NULL;
2243 rx->ring_dma_ptr = 0;
2246 rx->ring_size = 0;
2247 rx->last_head = 0;
2250 static int lan743x_rx_ring_init(struct lan743x_rx *rx)
2252 size_t ring_allocation_size = 0;
2253 dma_addr_t dma_ptr = 0;
2254 void *cpu_ptr = NULL;
2255 int ret = -ENOMEM;
2256 int index = 0;
2258 rx->ring_size = LAN743X_RX_RING_SIZE;
2259 if (rx->ring_size <= 1) {
2260 ret = -EINVAL;
2261 goto cleanup;
2263 if (rx->ring_size & ~RX_CFG_B_RX_RING_LEN_MASK_) {
2264 ret = -EINVAL;
2265 goto cleanup;
2267 ring_allocation_size = ALIGN(rx->ring_size *
2268 sizeof(struct lan743x_rx_descriptor),
2269 PAGE_SIZE);
2270 dma_ptr = 0;
2271 cpu_ptr = pci_zalloc_consistent(rx->adapter->pdev,
2272 ring_allocation_size, &dma_ptr);
2273 if (!cpu_ptr) {
2274 ret = -ENOMEM;
2275 goto cleanup;
2277 rx->ring_allocation_size = ring_allocation_size;
2278 rx->ring_cpu_ptr = (struct lan743x_rx_descriptor *)cpu_ptr;
2279 rx->ring_dma_ptr = dma_ptr;
2281 cpu_ptr = kcalloc(rx->ring_size, sizeof(*rx->buffer_info),
2282 GFP_KERNEL);
2283 if (!cpu_ptr) {
2284 ret = -ENOMEM;
2285 goto cleanup;
2287 rx->buffer_info = (struct lan743x_rx_buffer_info *)cpu_ptr;
2288 dma_ptr = 0;
2289 cpu_ptr = pci_zalloc_consistent(rx->adapter->pdev,
2290 sizeof(*rx->head_cpu_ptr), &dma_ptr);
2291 if (!cpu_ptr) {
2292 ret = -ENOMEM;
2293 goto cleanup;
2296 rx->head_cpu_ptr = cpu_ptr;
2297 rx->head_dma_ptr = dma_ptr;
2298 if (rx->head_dma_ptr & 0x3) {
2299 ret = -ENOMEM;
2300 goto cleanup;
2303 rx->last_head = 0;
2304 for (index = 0; index < rx->ring_size; index++) {
2305 struct sk_buff *new_skb = lan743x_rx_allocate_skb(rx);
2307 ret = lan743x_rx_init_ring_element(rx, index, new_skb);
2308 if (ret)
2309 goto cleanup;
2311 return 0;
2313 cleanup:
2314 lan743x_rx_ring_cleanup(rx);
2315 return ret;
2318 static void lan743x_rx_close(struct lan743x_rx *rx)
2320 struct lan743x_adapter *adapter = rx->adapter;
2322 lan743x_csr_write(adapter, FCT_RX_CTL,
2323 FCT_RX_CTL_DIS_(rx->channel_number));
2324 lan743x_csr_wait_for_bit(adapter, FCT_RX_CTL,
2325 FCT_RX_CTL_EN_(rx->channel_number),
2326 0, 1000, 20000, 100);
2328 lan743x_csr_write(adapter, DMAC_CMD,
2329 DMAC_CMD_STOP_R_(rx->channel_number));
2330 lan743x_dmac_rx_wait_till_stopped(adapter, rx->channel_number);
2332 lan743x_csr_write(adapter, DMAC_INT_EN_CLR,
2333 DMAC_INT_BIT_RXFRM_(rx->channel_number));
2334 lan743x_csr_write(adapter, INT_EN_CLR,
2335 INT_BIT_DMA_RX_(rx->channel_number));
2336 napi_disable(&rx->napi);
2338 netif_napi_del(&rx->napi);
2340 lan743x_rx_ring_cleanup(rx);
2343 static int lan743x_rx_open(struct lan743x_rx *rx)
2345 struct lan743x_adapter *adapter = rx->adapter;
2346 u32 data = 0;
2347 int ret;
2349 rx->frame_count = 0;
2350 ret = lan743x_rx_ring_init(rx);
2351 if (ret)
2352 goto return_error;
2354 netif_napi_add(adapter->netdev,
2355 &rx->napi, lan743x_rx_napi_poll,
2356 rx->ring_size - 1);
2358 lan743x_csr_write(adapter, DMAC_CMD,
2359 DMAC_CMD_RX_SWR_(rx->channel_number));
2360 lan743x_csr_wait_for_bit(adapter, DMAC_CMD,
2361 DMAC_CMD_RX_SWR_(rx->channel_number),
2362 0, 1000, 20000, 100);
2364 /* set ring base address */
2365 lan743x_csr_write(adapter,
2366 RX_BASE_ADDRH(rx->channel_number),
2367 DMA_ADDR_HIGH32(rx->ring_dma_ptr));
2368 lan743x_csr_write(adapter,
2369 RX_BASE_ADDRL(rx->channel_number),
2370 DMA_ADDR_LOW32(rx->ring_dma_ptr));
2372 /* set rx write back address */
2373 lan743x_csr_write(adapter,
2374 RX_HEAD_WRITEBACK_ADDRH(rx->channel_number),
2375 DMA_ADDR_HIGH32(rx->head_dma_ptr));
2376 lan743x_csr_write(adapter,
2377 RX_HEAD_WRITEBACK_ADDRL(rx->channel_number),
2378 DMA_ADDR_LOW32(rx->head_dma_ptr));
2379 data = RX_CFG_A_RX_HP_WB_EN_;
2380 if (!(adapter->csr.flags & LAN743X_CSR_FLAG_IS_A0)) {
2381 data |= (RX_CFG_A_RX_WB_ON_INT_TMR_ |
2382 RX_CFG_A_RX_WB_THRES_SET_(0x7) |
2383 RX_CFG_A_RX_PF_THRES_SET_(16) |
2384 RX_CFG_A_RX_PF_PRI_THRES_SET_(4));
2387 /* set RX_CFG_A */
2388 lan743x_csr_write(adapter,
2389 RX_CFG_A(rx->channel_number), data);
2391 /* set RX_CFG_B */
2392 data = lan743x_csr_read(adapter, RX_CFG_B(rx->channel_number));
2393 data &= ~RX_CFG_B_RX_PAD_MASK_;
2394 if (!RX_HEAD_PADDING)
2395 data |= RX_CFG_B_RX_PAD_0_;
2396 else
2397 data |= RX_CFG_B_RX_PAD_2_;
2398 data &= ~RX_CFG_B_RX_RING_LEN_MASK_;
2399 data |= ((rx->ring_size) & RX_CFG_B_RX_RING_LEN_MASK_);
2400 data |= RX_CFG_B_TS_ALL_RX_;
2401 if (!(adapter->csr.flags & LAN743X_CSR_FLAG_IS_A0))
2402 data |= RX_CFG_B_RDMABL_512_;
2404 lan743x_csr_write(adapter, RX_CFG_B(rx->channel_number), data);
2405 rx->vector_flags = lan743x_intr_get_vector_flags(adapter,
2406 INT_BIT_DMA_RX_
2407 (rx->channel_number));
2409 /* set RX_CFG_C */
2410 data = 0;
2411 if (rx->vector_flags & LAN743X_VECTOR_FLAG_SOURCE_ENABLE_AUTO_CLEAR)
2412 data |= RX_CFG_C_RX_TOP_INT_EN_AUTO_CLR_;
2413 if (rx->vector_flags & LAN743X_VECTOR_FLAG_SOURCE_STATUS_AUTO_CLEAR)
2414 data |= RX_CFG_C_RX_DMA_INT_STS_AUTO_CLR_;
2415 if (rx->vector_flags & LAN743X_VECTOR_FLAG_SOURCE_STATUS_R2C)
2416 data |= RX_CFG_C_RX_INT_STS_R2C_MODE_MASK_;
2417 if (rx->vector_flags & LAN743X_VECTOR_FLAG_SOURCE_ENABLE_R2C)
2418 data |= RX_CFG_C_RX_INT_EN_R2C_;
2419 lan743x_csr_write(adapter, RX_CFG_C(rx->channel_number), data);
2421 rx->last_tail = ((u32)(rx->ring_size - 1));
2422 lan743x_csr_write(adapter, RX_TAIL(rx->channel_number),
2423 rx->last_tail);
2424 rx->last_head = lan743x_csr_read(adapter, RX_HEAD(rx->channel_number));
2425 if (rx->last_head) {
2426 ret = -EIO;
2427 goto napi_delete;
2430 napi_enable(&rx->napi);
2432 lan743x_csr_write(adapter, INT_EN_SET,
2433 INT_BIT_DMA_RX_(rx->channel_number));
2434 lan743x_csr_write(adapter, DMAC_INT_STS,
2435 DMAC_INT_BIT_RXFRM_(rx->channel_number));
2436 lan743x_csr_write(adapter, DMAC_INT_EN_SET,
2437 DMAC_INT_BIT_RXFRM_(rx->channel_number));
2438 lan743x_csr_write(adapter, DMAC_CMD,
2439 DMAC_CMD_START_R_(rx->channel_number));
2441 /* initialize fifo */
2442 lan743x_csr_write(adapter, FCT_RX_CTL,
2443 FCT_RX_CTL_RESET_(rx->channel_number));
2444 lan743x_csr_wait_for_bit(adapter, FCT_RX_CTL,
2445 FCT_RX_CTL_RESET_(rx->channel_number),
2446 0, 1000, 20000, 100);
2447 lan743x_csr_write(adapter, FCT_FLOW(rx->channel_number),
2448 FCT_FLOW_CTL_REQ_EN_ |
2449 FCT_FLOW_CTL_ON_THRESHOLD_SET_(0x2A) |
2450 FCT_FLOW_CTL_OFF_THRESHOLD_SET_(0xA));
2452 /* enable fifo */
2453 lan743x_csr_write(adapter, FCT_RX_CTL,
2454 FCT_RX_CTL_EN_(rx->channel_number));
2455 return 0;
2457 napi_delete:
2458 netif_napi_del(&rx->napi);
2459 lan743x_rx_ring_cleanup(rx);
2461 return_error:
2462 return ret;
2465 static int lan743x_netdev_close(struct net_device *netdev)
2467 struct lan743x_adapter *adapter = netdev_priv(netdev);
2468 int index;
2470 lan743x_tx_close(&adapter->tx[0]);
2472 for (index = 0; index < LAN743X_USED_RX_CHANNELS; index++)
2473 lan743x_rx_close(&adapter->rx[index]);
2475 lan743x_ptp_close(adapter);
2477 lan743x_phy_close(adapter);
2479 lan743x_mac_close(adapter);
2481 lan743x_intr_close(adapter);
2483 return 0;
2486 static int lan743x_netdev_open(struct net_device *netdev)
2488 struct lan743x_adapter *adapter = netdev_priv(netdev);
2489 int index;
2490 int ret;
2492 ret = lan743x_intr_open(adapter);
2493 if (ret)
2494 goto return_error;
2496 ret = lan743x_mac_open(adapter);
2497 if (ret)
2498 goto close_intr;
2500 ret = lan743x_phy_open(adapter);
2501 if (ret)
2502 goto close_mac;
2504 ret = lan743x_ptp_open(adapter);
2505 if (ret)
2506 goto close_phy;
2508 lan743x_rfe_open(adapter);
2510 for (index = 0; index < LAN743X_USED_RX_CHANNELS; index++) {
2511 ret = lan743x_rx_open(&adapter->rx[index]);
2512 if (ret)
2513 goto close_rx;
2516 ret = lan743x_tx_open(&adapter->tx[0]);
2517 if (ret)
2518 goto close_rx;
2520 return 0;
2522 close_rx:
2523 for (index = 0; index < LAN743X_USED_RX_CHANNELS; index++) {
2524 if (adapter->rx[index].ring_cpu_ptr)
2525 lan743x_rx_close(&adapter->rx[index]);
2527 lan743x_ptp_close(adapter);
2529 close_phy:
2530 lan743x_phy_close(adapter);
2532 close_mac:
2533 lan743x_mac_close(adapter);
2535 close_intr:
2536 lan743x_intr_close(adapter);
2538 return_error:
2539 netif_warn(adapter, ifup, adapter->netdev,
2540 "Error opening LAN743x\n");
2541 return ret;
2544 static netdev_tx_t lan743x_netdev_xmit_frame(struct sk_buff *skb,
2545 struct net_device *netdev)
2547 struct lan743x_adapter *adapter = netdev_priv(netdev);
2549 return lan743x_tx_xmit_frame(&adapter->tx[0], skb);
2552 static int lan743x_netdev_ioctl(struct net_device *netdev,
2553 struct ifreq *ifr, int cmd)
2555 if (!netif_running(netdev))
2556 return -EINVAL;
2557 if (cmd == SIOCSHWTSTAMP)
2558 return lan743x_ptp_ioctl(netdev, ifr, cmd);
2559 return phy_mii_ioctl(netdev->phydev, ifr, cmd);
2562 static void lan743x_netdev_set_multicast(struct net_device *netdev)
2564 struct lan743x_adapter *adapter = netdev_priv(netdev);
2566 lan743x_rfe_set_multicast(adapter);
2569 static int lan743x_netdev_change_mtu(struct net_device *netdev, int new_mtu)
2571 struct lan743x_adapter *adapter = netdev_priv(netdev);
2572 int ret = 0;
2574 ret = lan743x_mac_set_mtu(adapter, new_mtu);
2575 if (!ret)
2576 netdev->mtu = new_mtu;
2577 return ret;
2580 static void lan743x_netdev_get_stats64(struct net_device *netdev,
2581 struct rtnl_link_stats64 *stats)
2583 struct lan743x_adapter *adapter = netdev_priv(netdev);
2585 stats->rx_packets = lan743x_csr_read(adapter, STAT_RX_TOTAL_FRAMES);
2586 stats->tx_packets = lan743x_csr_read(adapter, STAT_TX_TOTAL_FRAMES);
2587 stats->rx_bytes = lan743x_csr_read(adapter,
2588 STAT_RX_UNICAST_BYTE_COUNT) +
2589 lan743x_csr_read(adapter,
2590 STAT_RX_BROADCAST_BYTE_COUNT) +
2591 lan743x_csr_read(adapter,
2592 STAT_RX_MULTICAST_BYTE_COUNT);
2593 stats->tx_bytes = lan743x_csr_read(adapter,
2594 STAT_TX_UNICAST_BYTE_COUNT) +
2595 lan743x_csr_read(adapter,
2596 STAT_TX_BROADCAST_BYTE_COUNT) +
2597 lan743x_csr_read(adapter,
2598 STAT_TX_MULTICAST_BYTE_COUNT);
2599 stats->rx_errors = lan743x_csr_read(adapter, STAT_RX_FCS_ERRORS) +
2600 lan743x_csr_read(adapter,
2601 STAT_RX_ALIGNMENT_ERRORS) +
2602 lan743x_csr_read(adapter, STAT_RX_JABBER_ERRORS) +
2603 lan743x_csr_read(adapter,
2604 STAT_RX_UNDERSIZE_FRAME_ERRORS) +
2605 lan743x_csr_read(adapter,
2606 STAT_RX_OVERSIZE_FRAME_ERRORS);
2607 stats->tx_errors = lan743x_csr_read(adapter, STAT_TX_FCS_ERRORS) +
2608 lan743x_csr_read(adapter,
2609 STAT_TX_EXCESS_DEFERRAL_ERRORS) +
2610 lan743x_csr_read(adapter, STAT_TX_CARRIER_ERRORS);
2611 stats->rx_dropped = lan743x_csr_read(adapter,
2612 STAT_RX_DROPPED_FRAMES);
2613 stats->tx_dropped = lan743x_csr_read(adapter,
2614 STAT_TX_EXCESSIVE_COLLISION);
2615 stats->multicast = lan743x_csr_read(adapter,
2616 STAT_RX_MULTICAST_FRAMES) +
2617 lan743x_csr_read(adapter,
2618 STAT_TX_MULTICAST_FRAMES);
2619 stats->collisions = lan743x_csr_read(adapter,
2620 STAT_TX_SINGLE_COLLISIONS) +
2621 lan743x_csr_read(adapter,
2622 STAT_TX_MULTIPLE_COLLISIONS) +
2623 lan743x_csr_read(adapter,
2624 STAT_TX_LATE_COLLISIONS);
2627 static int lan743x_netdev_set_mac_address(struct net_device *netdev,
2628 void *addr)
2630 struct lan743x_adapter *adapter = netdev_priv(netdev);
2631 struct sockaddr *sock_addr = addr;
2632 int ret;
2634 ret = eth_prepare_mac_addr_change(netdev, sock_addr);
2635 if (ret)
2636 return ret;
2637 ether_addr_copy(netdev->dev_addr, sock_addr->sa_data);
2638 lan743x_mac_set_address(adapter, sock_addr->sa_data);
2639 lan743x_rfe_update_mac_address(adapter);
2640 return 0;
2643 static const struct net_device_ops lan743x_netdev_ops = {
2644 .ndo_open = lan743x_netdev_open,
2645 .ndo_stop = lan743x_netdev_close,
2646 .ndo_start_xmit = lan743x_netdev_xmit_frame,
2647 .ndo_do_ioctl = lan743x_netdev_ioctl,
2648 .ndo_set_rx_mode = lan743x_netdev_set_multicast,
2649 .ndo_change_mtu = lan743x_netdev_change_mtu,
2650 .ndo_get_stats64 = lan743x_netdev_get_stats64,
2651 .ndo_set_mac_address = lan743x_netdev_set_mac_address,
2654 static void lan743x_hardware_cleanup(struct lan743x_adapter *adapter)
2656 lan743x_csr_write(adapter, INT_EN_CLR, 0xFFFFFFFF);
2659 static void lan743x_mdiobus_cleanup(struct lan743x_adapter *adapter)
2661 mdiobus_unregister(adapter->mdiobus);
2664 static void lan743x_full_cleanup(struct lan743x_adapter *adapter)
2666 unregister_netdev(adapter->netdev);
2668 lan743x_mdiobus_cleanup(adapter);
2669 lan743x_hardware_cleanup(adapter);
2670 lan743x_pci_cleanup(adapter);
2673 static int lan743x_hardware_init(struct lan743x_adapter *adapter,
2674 struct pci_dev *pdev)
2676 struct lan743x_tx *tx;
2677 int index;
2678 int ret;
2680 adapter->intr.irq = adapter->pdev->irq;
2681 lan743x_csr_write(adapter, INT_EN_CLR, 0xFFFFFFFF);
2682 mutex_init(&adapter->dp_lock);
2684 ret = lan743x_gpio_init(adapter);
2685 if (ret)
2686 return ret;
2688 ret = lan743x_mac_init(adapter);
2689 if (ret)
2690 return ret;
2692 ret = lan743x_phy_init(adapter);
2693 if (ret)
2694 return ret;
2696 ret = lan743x_ptp_init(adapter);
2697 if (ret)
2698 return ret;
2700 lan743x_rfe_update_mac_address(adapter);
2702 ret = lan743x_dmac_init(adapter);
2703 if (ret)
2704 return ret;
2706 for (index = 0; index < LAN743X_USED_RX_CHANNELS; index++) {
2707 adapter->rx[index].adapter = adapter;
2708 adapter->rx[index].channel_number = index;
2711 tx = &adapter->tx[0];
2712 tx->adapter = adapter;
2713 tx->channel_number = 0;
2714 spin_lock_init(&tx->ring_lock);
2715 return 0;
2718 static int lan743x_mdiobus_init(struct lan743x_adapter *adapter)
2720 int ret;
2722 adapter->mdiobus = devm_mdiobus_alloc(&adapter->pdev->dev);
2723 if (!(adapter->mdiobus)) {
2724 ret = -ENOMEM;
2725 goto return_error;
2728 adapter->mdiobus->priv = (void *)adapter;
2729 adapter->mdiobus->read = lan743x_mdiobus_read;
2730 adapter->mdiobus->write = lan743x_mdiobus_write;
2731 adapter->mdiobus->name = "lan743x-mdiobus";
2732 snprintf(adapter->mdiobus->id, MII_BUS_ID_SIZE,
2733 "pci-%s", pci_name(adapter->pdev));
2735 if ((adapter->csr.id_rev & ID_REV_ID_MASK_) == ID_REV_ID_LAN7430_)
2736 /* LAN7430 uses internal phy at address 1 */
2737 adapter->mdiobus->phy_mask = ~(u32)BIT(1);
2739 /* register mdiobus */
2740 ret = mdiobus_register(adapter->mdiobus);
2741 if (ret < 0)
2742 goto return_error;
2743 return 0;
2745 return_error:
2746 return ret;
2749 /* lan743x_pcidev_probe - Device Initialization Routine
2750 * @pdev: PCI device information struct
2751 * @id: entry in lan743x_pci_tbl
2753 * Returns 0 on success, negative on failure
2755 * initializes an adapter identified by a pci_dev structure.
2756 * The OS initialization, configuring of the adapter private structure,
2757 * and a hardware reset occur.
2759 static int lan743x_pcidev_probe(struct pci_dev *pdev,
2760 const struct pci_device_id *id)
2762 struct lan743x_adapter *adapter = NULL;
2763 struct net_device *netdev = NULL;
2764 int ret = -ENODEV;
2766 netdev = devm_alloc_etherdev(&pdev->dev,
2767 sizeof(struct lan743x_adapter));
2768 if (!netdev)
2769 goto return_error;
2771 SET_NETDEV_DEV(netdev, &pdev->dev);
2772 pci_set_drvdata(pdev, netdev);
2773 adapter = netdev_priv(netdev);
2774 adapter->netdev = netdev;
2775 adapter->msg_enable = NETIF_MSG_DRV | NETIF_MSG_PROBE |
2776 NETIF_MSG_LINK | NETIF_MSG_IFUP |
2777 NETIF_MSG_IFDOWN | NETIF_MSG_TX_QUEUED;
2778 netdev->max_mtu = LAN743X_MAX_FRAME_SIZE;
2780 ret = lan743x_pci_init(adapter, pdev);
2781 if (ret)
2782 goto return_error;
2784 ret = lan743x_csr_init(adapter);
2785 if (ret)
2786 goto cleanup_pci;
2788 ret = lan743x_hardware_init(adapter, pdev);
2789 if (ret)
2790 goto cleanup_pci;
2792 ret = lan743x_mdiobus_init(adapter);
2793 if (ret)
2794 goto cleanup_hardware;
2796 adapter->netdev->netdev_ops = &lan743x_netdev_ops;
2797 adapter->netdev->ethtool_ops = &lan743x_ethtool_ops;
2798 adapter->netdev->features = NETIF_F_SG | NETIF_F_TSO | NETIF_F_HW_CSUM;
2799 adapter->netdev->hw_features = adapter->netdev->features;
2801 /* carrier off reporting is important to ethtool even BEFORE open */
2802 netif_carrier_off(netdev);
2804 ret = register_netdev(adapter->netdev);
2805 if (ret < 0)
2806 goto cleanup_mdiobus;
2807 return 0;
2809 cleanup_mdiobus:
2810 lan743x_mdiobus_cleanup(adapter);
2812 cleanup_hardware:
2813 lan743x_hardware_cleanup(adapter);
2815 cleanup_pci:
2816 lan743x_pci_cleanup(adapter);
2818 return_error:
2819 pr_warn("Initialization failed\n");
2820 return ret;
2824 * lan743x_pcidev_remove - Device Removal Routine
2825 * @pdev: PCI device information struct
2827 * this is called by the PCI subsystem to alert the driver
2828 * that it should release a PCI device. This could be caused by a
2829 * Hot-Plug event, or because the driver is going to be removed from
2830 * memory.
2832 static void lan743x_pcidev_remove(struct pci_dev *pdev)
2834 struct net_device *netdev = pci_get_drvdata(pdev);
2835 struct lan743x_adapter *adapter = netdev_priv(netdev);
2837 lan743x_full_cleanup(adapter);
2840 static void lan743x_pcidev_shutdown(struct pci_dev *pdev)
2842 struct net_device *netdev = pci_get_drvdata(pdev);
2843 struct lan743x_adapter *adapter = netdev_priv(netdev);
2845 rtnl_lock();
2846 netif_device_detach(netdev);
2848 /* close netdev when netdev is at running state.
2849 * For instance, it is true when system goes to sleep by pm-suspend
2850 * However, it is false when system goes to sleep by suspend GUI menu
2852 if (netif_running(netdev))
2853 lan743x_netdev_close(netdev);
2854 rtnl_unlock();
2856 #ifdef CONFIG_PM
2857 pci_save_state(pdev);
2858 #endif
2860 /* clean up lan743x portion */
2861 lan743x_hardware_cleanup(adapter);
2864 #ifdef CONFIG_PM_SLEEP
2865 static u16 lan743x_pm_wakeframe_crc16(const u8 *buf, int len)
2867 return bitrev16(crc16(0xFFFF, buf, len));
2870 static void lan743x_pm_set_wol(struct lan743x_adapter *adapter)
2872 const u8 ipv4_multicast[3] = { 0x01, 0x00, 0x5E };
2873 const u8 ipv6_multicast[3] = { 0x33, 0x33 };
2874 const u8 arp_type[2] = { 0x08, 0x06 };
2875 int mask_index;
2876 u32 pmtctl;
2877 u32 wucsr;
2878 u32 macrx;
2879 u16 crc;
2881 for (mask_index = 0; mask_index < MAC_NUM_OF_WUF_CFG; mask_index++)
2882 lan743x_csr_write(adapter, MAC_WUF_CFG(mask_index), 0);
2884 /* clear wake settings */
2885 pmtctl = lan743x_csr_read(adapter, PMT_CTL);
2886 pmtctl |= PMT_CTL_WUPS_MASK_;
2887 pmtctl &= ~(PMT_CTL_GPIO_WAKEUP_EN_ | PMT_CTL_EEE_WAKEUP_EN_ |
2888 PMT_CTL_WOL_EN_ | PMT_CTL_MAC_D3_RX_CLK_OVR_ |
2889 PMT_CTL_RX_FCT_RFE_D3_CLK_OVR_ | PMT_CTL_ETH_PHY_WAKE_EN_);
2891 macrx = lan743x_csr_read(adapter, MAC_RX);
2893 wucsr = 0;
2894 mask_index = 0;
2896 pmtctl |= PMT_CTL_ETH_PHY_D3_COLD_OVR_ | PMT_CTL_ETH_PHY_D3_OVR_;
2898 if (adapter->wolopts & WAKE_PHY) {
2899 pmtctl |= PMT_CTL_ETH_PHY_EDPD_PLL_CTL_;
2900 pmtctl |= PMT_CTL_ETH_PHY_WAKE_EN_;
2902 if (adapter->wolopts & WAKE_MAGIC) {
2903 wucsr |= MAC_WUCSR_MPEN_;
2904 macrx |= MAC_RX_RXEN_;
2905 pmtctl |= PMT_CTL_WOL_EN_ | PMT_CTL_MAC_D3_RX_CLK_OVR_;
2907 if (adapter->wolopts & WAKE_UCAST) {
2908 wucsr |= MAC_WUCSR_RFE_WAKE_EN_ | MAC_WUCSR_PFDA_EN_;
2909 macrx |= MAC_RX_RXEN_;
2910 pmtctl |= PMT_CTL_WOL_EN_ | PMT_CTL_MAC_D3_RX_CLK_OVR_;
2911 pmtctl |= PMT_CTL_RX_FCT_RFE_D3_CLK_OVR_;
2913 if (adapter->wolopts & WAKE_BCAST) {
2914 wucsr |= MAC_WUCSR_RFE_WAKE_EN_ | MAC_WUCSR_BCST_EN_;
2915 macrx |= MAC_RX_RXEN_;
2916 pmtctl |= PMT_CTL_WOL_EN_ | PMT_CTL_MAC_D3_RX_CLK_OVR_;
2917 pmtctl |= PMT_CTL_RX_FCT_RFE_D3_CLK_OVR_;
2919 if (adapter->wolopts & WAKE_MCAST) {
2920 /* IPv4 multicast */
2921 crc = lan743x_pm_wakeframe_crc16(ipv4_multicast, 3);
2922 lan743x_csr_write(adapter, MAC_WUF_CFG(mask_index),
2923 MAC_WUF_CFG_EN_ | MAC_WUF_CFG_TYPE_MCAST_ |
2924 (0 << MAC_WUF_CFG_OFFSET_SHIFT_) |
2925 (crc & MAC_WUF_CFG_CRC16_MASK_));
2926 lan743x_csr_write(adapter, MAC_WUF_MASK0(mask_index), 7);
2927 lan743x_csr_write(adapter, MAC_WUF_MASK1(mask_index), 0);
2928 lan743x_csr_write(adapter, MAC_WUF_MASK2(mask_index), 0);
2929 lan743x_csr_write(adapter, MAC_WUF_MASK3(mask_index), 0);
2930 mask_index++;
2932 /* IPv6 multicast */
2933 crc = lan743x_pm_wakeframe_crc16(ipv6_multicast, 2);
2934 lan743x_csr_write(adapter, MAC_WUF_CFG(mask_index),
2935 MAC_WUF_CFG_EN_ | MAC_WUF_CFG_TYPE_MCAST_ |
2936 (0 << MAC_WUF_CFG_OFFSET_SHIFT_) |
2937 (crc & MAC_WUF_CFG_CRC16_MASK_));
2938 lan743x_csr_write(adapter, MAC_WUF_MASK0(mask_index), 3);
2939 lan743x_csr_write(adapter, MAC_WUF_MASK1(mask_index), 0);
2940 lan743x_csr_write(adapter, MAC_WUF_MASK2(mask_index), 0);
2941 lan743x_csr_write(adapter, MAC_WUF_MASK3(mask_index), 0);
2942 mask_index++;
2944 wucsr |= MAC_WUCSR_RFE_WAKE_EN_ | MAC_WUCSR_WAKE_EN_;
2945 macrx |= MAC_RX_RXEN_;
2946 pmtctl |= PMT_CTL_WOL_EN_ | PMT_CTL_MAC_D3_RX_CLK_OVR_;
2947 pmtctl |= PMT_CTL_RX_FCT_RFE_D3_CLK_OVR_;
2949 if (adapter->wolopts & WAKE_ARP) {
2950 /* set MAC_WUF_CFG & WUF_MASK
2951 * for packettype (offset 12,13) = ARP (0x0806)
2953 crc = lan743x_pm_wakeframe_crc16(arp_type, 2);
2954 lan743x_csr_write(adapter, MAC_WUF_CFG(mask_index),
2955 MAC_WUF_CFG_EN_ | MAC_WUF_CFG_TYPE_ALL_ |
2956 (0 << MAC_WUF_CFG_OFFSET_SHIFT_) |
2957 (crc & MAC_WUF_CFG_CRC16_MASK_));
2958 lan743x_csr_write(adapter, MAC_WUF_MASK0(mask_index), 0x3000);
2959 lan743x_csr_write(adapter, MAC_WUF_MASK1(mask_index), 0);
2960 lan743x_csr_write(adapter, MAC_WUF_MASK2(mask_index), 0);
2961 lan743x_csr_write(adapter, MAC_WUF_MASK3(mask_index), 0);
2962 mask_index++;
2964 wucsr |= MAC_WUCSR_RFE_WAKE_EN_ | MAC_WUCSR_WAKE_EN_;
2965 macrx |= MAC_RX_RXEN_;
2966 pmtctl |= PMT_CTL_WOL_EN_ | PMT_CTL_MAC_D3_RX_CLK_OVR_;
2967 pmtctl |= PMT_CTL_RX_FCT_RFE_D3_CLK_OVR_;
2970 lan743x_csr_write(adapter, MAC_WUCSR, wucsr);
2971 lan743x_csr_write(adapter, PMT_CTL, pmtctl);
2972 lan743x_csr_write(adapter, MAC_RX, macrx);
2975 static int lan743x_pm_suspend(struct device *dev)
2977 struct pci_dev *pdev = to_pci_dev(dev);
2978 struct net_device *netdev = pci_get_drvdata(pdev);
2979 struct lan743x_adapter *adapter = netdev_priv(netdev);
2980 int ret;
2982 lan743x_pcidev_shutdown(pdev);
2984 /* clear all wakes */
2985 lan743x_csr_write(adapter, MAC_WUCSR, 0);
2986 lan743x_csr_write(adapter, MAC_WUCSR2, 0);
2987 lan743x_csr_write(adapter, MAC_WK_SRC, 0xFFFFFFFF);
2989 if (adapter->wolopts)
2990 lan743x_pm_set_wol(adapter);
2992 /* Host sets PME_En, put D3hot */
2993 ret = pci_prepare_to_sleep(pdev);
2995 return 0;
2998 static int lan743x_pm_resume(struct device *dev)
3000 struct pci_dev *pdev = to_pci_dev(dev);
3001 struct net_device *netdev = pci_get_drvdata(pdev);
3002 struct lan743x_adapter *adapter = netdev_priv(netdev);
3003 int ret;
3005 pci_set_power_state(pdev, PCI_D0);
3006 pci_restore_state(pdev);
3007 pci_save_state(pdev);
3009 ret = lan743x_hardware_init(adapter, pdev);
3010 if (ret) {
3011 netif_err(adapter, probe, adapter->netdev,
3012 "lan743x_hardware_init returned %d\n", ret);
3015 /* open netdev when netdev is at running state while resume.
3016 * For instance, it is true when system wakesup after pm-suspend
3017 * However, it is false when system wakes up after suspend GUI menu
3019 if (netif_running(netdev))
3020 lan743x_netdev_open(netdev);
3022 netif_device_attach(netdev);
3024 return 0;
3027 static const struct dev_pm_ops lan743x_pm_ops = {
3028 SET_SYSTEM_SLEEP_PM_OPS(lan743x_pm_suspend, lan743x_pm_resume)
3030 #endif /* CONFIG_PM_SLEEP */
3032 static const struct pci_device_id lan743x_pcidev_tbl[] = {
3033 { PCI_DEVICE(PCI_VENDOR_ID_SMSC, PCI_DEVICE_ID_SMSC_LAN7430) },
3034 { PCI_DEVICE(PCI_VENDOR_ID_SMSC, PCI_DEVICE_ID_SMSC_LAN7431) },
3035 { 0, }
3038 static struct pci_driver lan743x_pcidev_driver = {
3039 .name = DRIVER_NAME,
3040 .id_table = lan743x_pcidev_tbl,
3041 .probe = lan743x_pcidev_probe,
3042 .remove = lan743x_pcidev_remove,
3043 #ifdef CONFIG_PM_SLEEP
3044 .driver.pm = &lan743x_pm_ops,
3045 #endif
3046 .shutdown = lan743x_pcidev_shutdown,
3049 module_pci_driver(lan743x_pcidev_driver);
3051 MODULE_AUTHOR(DRIVER_AUTHOR);
3052 MODULE_DESCRIPTION(DRIVER_DESC);
3053 MODULE_LICENSE("GPL");