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