xref: /openbmc/linux/drivers/net/ethernet/microchip/lan743x_main.c (revision 22fc4c4c9fd60427bcda00878cee94e7622cfa7a)
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 
806 	netdev = adapter->netdev;
807 
808 	/* setup auto duplex, and speed detection */
809 	data = lan743x_csr_read(adapter, MAC_CR);
810 	data |= MAC_CR_ADD_ | MAC_CR_ASD_;
811 	data |= MAC_CR_CNTR_RST_;
812 	lan743x_csr_write(adapter, MAC_CR, data);
813 
814 	mac_addr_hi = lan743x_csr_read(adapter, MAC_RX_ADDRH);
815 	mac_addr_lo = lan743x_csr_read(adapter, MAC_RX_ADDRL);
816 	adapter->mac_address[0] = mac_addr_lo & 0xFF;
817 	adapter->mac_address[1] = (mac_addr_lo >> 8) & 0xFF;
818 	adapter->mac_address[2] = (mac_addr_lo >> 16) & 0xFF;
819 	adapter->mac_address[3] = (mac_addr_lo >> 24) & 0xFF;
820 	adapter->mac_address[4] = mac_addr_hi & 0xFF;
821 	adapter->mac_address[5] = (mac_addr_hi >> 8) & 0xFF;
822 
823 	if (((mac_addr_hi & 0x0000FFFF) == 0x0000FFFF) &&
824 	    mac_addr_lo == 0xFFFFFFFF) {
825 		mac_address_valid = false;
826 	} else if (!is_valid_ether_addr(adapter->mac_address)) {
827 		mac_address_valid = false;
828 	}
829 
830 	if (!mac_address_valid)
831 		eth_random_addr(adapter->mac_address);
832 	lan743x_mac_set_address(adapter, adapter->mac_address);
833 	ether_addr_copy(netdev->dev_addr, adapter->mac_address);
834 	return 0;
835 }
836 
837 static int lan743x_mac_open(struct lan743x_adapter *adapter)
838 {
839 	int ret = 0;
840 	u32 temp;
841 
842 	temp = lan743x_csr_read(adapter, MAC_RX);
843 	lan743x_csr_write(adapter, MAC_RX, temp | MAC_RX_RXEN_);
844 	temp = lan743x_csr_read(adapter, MAC_TX);
845 	lan743x_csr_write(adapter, MAC_TX, temp | MAC_TX_TXEN_);
846 	return ret;
847 }
848 
849 static void lan743x_mac_close(struct lan743x_adapter *adapter)
850 {
851 	u32 temp;
852 
853 	temp = lan743x_csr_read(adapter, MAC_TX);
854 	temp &= ~MAC_TX_TXEN_;
855 	lan743x_csr_write(adapter, MAC_TX, temp);
856 	lan743x_csr_wait_for_bit(adapter, MAC_TX, MAC_TX_TXD_,
857 				 1, 1000, 20000, 100);
858 
859 	temp = lan743x_csr_read(adapter, MAC_RX);
860 	temp &= ~MAC_RX_RXEN_;
861 	lan743x_csr_write(adapter, MAC_RX, temp);
862 	lan743x_csr_wait_for_bit(adapter, MAC_RX, MAC_RX_RXD_,
863 				 1, 1000, 20000, 100);
864 }
865 
866 static void lan743x_mac_flow_ctrl_set_enables(struct lan743x_adapter *adapter,
867 					      bool tx_enable, bool rx_enable)
868 {
869 	u32 flow_setting = 0;
870 
871 	/* set maximum pause time because when fifo space frees
872 	 * up a zero value pause frame will be sent to release the pause
873 	 */
874 	flow_setting = MAC_FLOW_CR_FCPT_MASK_;
875 	if (tx_enable)
876 		flow_setting |= MAC_FLOW_CR_TX_FCEN_;
877 	if (rx_enable)
878 		flow_setting |= MAC_FLOW_CR_RX_FCEN_;
879 	lan743x_csr_write(adapter, MAC_FLOW, flow_setting);
880 }
881 
882 static int lan743x_mac_set_mtu(struct lan743x_adapter *adapter, int new_mtu)
883 {
884 	int enabled = 0;
885 	u32 mac_rx = 0;
886 
887 	mac_rx = lan743x_csr_read(adapter, MAC_RX);
888 	if (mac_rx & MAC_RX_RXEN_) {
889 		enabled = 1;
890 		if (mac_rx & MAC_RX_RXD_) {
891 			lan743x_csr_write(adapter, MAC_RX, mac_rx);
892 			mac_rx &= ~MAC_RX_RXD_;
893 		}
894 		mac_rx &= ~MAC_RX_RXEN_;
895 		lan743x_csr_write(adapter, MAC_RX, mac_rx);
896 		lan743x_csr_wait_for_bit(adapter, MAC_RX, MAC_RX_RXD_,
897 					 1, 1000, 20000, 100);
898 		lan743x_csr_write(adapter, MAC_RX, mac_rx | MAC_RX_RXD_);
899 	}
900 
901 	mac_rx &= ~(MAC_RX_MAX_SIZE_MASK_);
902 	mac_rx |= (((new_mtu + ETH_HLEN + 4) << MAC_RX_MAX_SIZE_SHIFT_) &
903 		  MAC_RX_MAX_SIZE_MASK_);
904 	lan743x_csr_write(adapter, MAC_RX, mac_rx);
905 
906 	if (enabled) {
907 		mac_rx |= MAC_RX_RXEN_;
908 		lan743x_csr_write(adapter, MAC_RX, mac_rx);
909 	}
910 	return 0;
911 }
912 
913 /* PHY */
914 static int lan743x_phy_reset(struct lan743x_adapter *adapter)
915 {
916 	u32 data;
917 
918 	/* Only called with in probe, and before mdiobus_register */
919 
920 	data = lan743x_csr_read(adapter, PMT_CTL);
921 	data |= PMT_CTL_ETH_PHY_RST_;
922 	lan743x_csr_write(adapter, PMT_CTL, data);
923 
924 	return readx_poll_timeout(LAN743X_CSR_READ_OP, PMT_CTL, data,
925 				  (!(data & PMT_CTL_ETH_PHY_RST_) &&
926 				  (data & PMT_CTL_READY_)),
927 				  50000, 1000000);
928 }
929 
930 static void lan743x_phy_update_flowcontrol(struct lan743x_adapter *adapter,
931 					   u8 duplex, u16 local_adv,
932 					   u16 remote_adv)
933 {
934 	struct lan743x_phy *phy = &adapter->phy;
935 	u8 cap;
936 
937 	if (phy->fc_autoneg)
938 		cap = mii_resolve_flowctrl_fdx(local_adv, remote_adv);
939 	else
940 		cap = phy->fc_request_control;
941 
942 	lan743x_mac_flow_ctrl_set_enables(adapter,
943 					  cap & FLOW_CTRL_TX,
944 					  cap & FLOW_CTRL_RX);
945 }
946 
947 static int lan743x_phy_init(struct lan743x_adapter *adapter)
948 {
949 	return lan743x_phy_reset(adapter);
950 }
951 
952 static void lan743x_phy_link_status_change(struct net_device *netdev)
953 {
954 	struct lan743x_adapter *adapter = netdev_priv(netdev);
955 	struct phy_device *phydev = netdev->phydev;
956 
957 	phy_print_status(phydev);
958 	if (phydev->state == PHY_RUNNING) {
959 		struct ethtool_link_ksettings ksettings;
960 		int remote_advertisement = 0;
961 		int local_advertisement = 0;
962 
963 		memset(&ksettings, 0, sizeof(ksettings));
964 		phy_ethtool_get_link_ksettings(netdev, &ksettings);
965 		local_advertisement =
966 			linkmode_adv_to_mii_adv_t(phydev->advertising);
967 		remote_advertisement =
968 			linkmode_adv_to_mii_adv_t(phydev->lp_advertising);
969 
970 		lan743x_phy_update_flowcontrol(adapter,
971 					       ksettings.base.duplex,
972 					       local_advertisement,
973 					       remote_advertisement);
974 		lan743x_ptp_update_latency(adapter, ksettings.base.speed);
975 	}
976 }
977 
978 static void lan743x_phy_close(struct lan743x_adapter *adapter)
979 {
980 	struct net_device *netdev = adapter->netdev;
981 
982 	phy_stop(netdev->phydev);
983 	phy_disconnect(netdev->phydev);
984 	netdev->phydev = NULL;
985 }
986 
987 static int lan743x_phy_open(struct lan743x_adapter *adapter)
988 {
989 	struct lan743x_phy *phy = &adapter->phy;
990 	struct phy_device *phydev;
991 	struct net_device *netdev;
992 	int ret = -EIO;
993 
994 	netdev = adapter->netdev;
995 	phydev = phy_find_first(adapter->mdiobus);
996 	if (!phydev)
997 		goto return_error;
998 
999 	ret = phy_connect_direct(netdev, phydev,
1000 				 lan743x_phy_link_status_change,
1001 				 PHY_INTERFACE_MODE_GMII);
1002 	if (ret)
1003 		goto return_error;
1004 
1005 	/* MAC doesn't support 1000T Half */
1006 	phy_remove_link_mode(phydev, ETHTOOL_LINK_MODE_1000baseT_Half_BIT);
1007 
1008 	/* support both flow controls */
1009 	phy_support_asym_pause(phydev);
1010 	phy->fc_request_control = (FLOW_CTRL_RX | FLOW_CTRL_TX);
1011 	phy->fc_autoneg = phydev->autoneg;
1012 
1013 	phy_start(phydev);
1014 	phy_start_aneg(phydev);
1015 	return 0;
1016 
1017 return_error:
1018 	return ret;
1019 }
1020 
1021 static void lan743x_rfe_open(struct lan743x_adapter *adapter)
1022 {
1023 	lan743x_csr_write(adapter, RFE_RSS_CFG,
1024 		RFE_RSS_CFG_UDP_IPV6_EX_ |
1025 		RFE_RSS_CFG_TCP_IPV6_EX_ |
1026 		RFE_RSS_CFG_IPV6_EX_ |
1027 		RFE_RSS_CFG_UDP_IPV6_ |
1028 		RFE_RSS_CFG_TCP_IPV6_ |
1029 		RFE_RSS_CFG_IPV6_ |
1030 		RFE_RSS_CFG_UDP_IPV4_ |
1031 		RFE_RSS_CFG_TCP_IPV4_ |
1032 		RFE_RSS_CFG_IPV4_ |
1033 		RFE_RSS_CFG_VALID_HASH_BITS_ |
1034 		RFE_RSS_CFG_RSS_QUEUE_ENABLE_ |
1035 		RFE_RSS_CFG_RSS_HASH_STORE_ |
1036 		RFE_RSS_CFG_RSS_ENABLE_);
1037 }
1038 
1039 static void lan743x_rfe_update_mac_address(struct lan743x_adapter *adapter)
1040 {
1041 	u8 *mac_addr;
1042 	u32 mac_addr_hi = 0;
1043 	u32 mac_addr_lo = 0;
1044 
1045 	/* Add mac address to perfect Filter */
1046 	mac_addr = adapter->mac_address;
1047 	mac_addr_lo = ((((u32)(mac_addr[0])) << 0) |
1048 		      (((u32)(mac_addr[1])) << 8) |
1049 		      (((u32)(mac_addr[2])) << 16) |
1050 		      (((u32)(mac_addr[3])) << 24));
1051 	mac_addr_hi = ((((u32)(mac_addr[4])) << 0) |
1052 		      (((u32)(mac_addr[5])) << 8));
1053 
1054 	lan743x_csr_write(adapter, RFE_ADDR_FILT_LO(0), mac_addr_lo);
1055 	lan743x_csr_write(adapter, RFE_ADDR_FILT_HI(0),
1056 			  mac_addr_hi | RFE_ADDR_FILT_HI_VALID_);
1057 }
1058 
1059 static void lan743x_rfe_set_multicast(struct lan743x_adapter *adapter)
1060 {
1061 	struct net_device *netdev = adapter->netdev;
1062 	u32 hash_table[DP_SEL_VHF_HASH_LEN];
1063 	u32 rfctl;
1064 	u32 data;
1065 
1066 	rfctl = lan743x_csr_read(adapter, RFE_CTL);
1067 	rfctl &= ~(RFE_CTL_AU_ | RFE_CTL_AM_ |
1068 		 RFE_CTL_DA_PERFECT_ | RFE_CTL_MCAST_HASH_);
1069 	rfctl |= RFE_CTL_AB_;
1070 	if (netdev->flags & IFF_PROMISC) {
1071 		rfctl |= RFE_CTL_AM_ | RFE_CTL_AU_;
1072 	} else {
1073 		if (netdev->flags & IFF_ALLMULTI)
1074 			rfctl |= RFE_CTL_AM_;
1075 	}
1076 
1077 	memset(hash_table, 0, DP_SEL_VHF_HASH_LEN * sizeof(u32));
1078 	if (netdev_mc_count(netdev)) {
1079 		struct netdev_hw_addr *ha;
1080 		int i;
1081 
1082 		rfctl |= RFE_CTL_DA_PERFECT_;
1083 		i = 1;
1084 		netdev_for_each_mc_addr(ha, netdev) {
1085 			/* set first 32 into Perfect Filter */
1086 			if (i < 33) {
1087 				lan743x_csr_write(adapter,
1088 						  RFE_ADDR_FILT_HI(i), 0);
1089 				data = ha->addr[3];
1090 				data = ha->addr[2] | (data << 8);
1091 				data = ha->addr[1] | (data << 8);
1092 				data = ha->addr[0] | (data << 8);
1093 				lan743x_csr_write(adapter,
1094 						  RFE_ADDR_FILT_LO(i), data);
1095 				data = ha->addr[5];
1096 				data = ha->addr[4] | (data << 8);
1097 				data |= RFE_ADDR_FILT_HI_VALID_;
1098 				lan743x_csr_write(adapter,
1099 						  RFE_ADDR_FILT_HI(i), data);
1100 			} else {
1101 				u32 bitnum = (ether_crc(ETH_ALEN, ha->addr) >>
1102 					     23) & 0x1FF;
1103 				hash_table[bitnum / 32] |= (1 << (bitnum % 32));
1104 				rfctl |= RFE_CTL_MCAST_HASH_;
1105 			}
1106 			i++;
1107 		}
1108 	}
1109 
1110 	lan743x_dp_write(adapter, DP_SEL_RFE_RAM,
1111 			 DP_SEL_VHF_VLAN_LEN,
1112 			 DP_SEL_VHF_HASH_LEN, hash_table);
1113 	lan743x_csr_write(adapter, RFE_CTL, rfctl);
1114 }
1115 
1116 static int lan743x_dmac_init(struct lan743x_adapter *adapter)
1117 {
1118 	u32 data = 0;
1119 
1120 	lan743x_csr_write(adapter, DMAC_CMD, DMAC_CMD_SWR_);
1121 	lan743x_csr_wait_for_bit(adapter, DMAC_CMD, DMAC_CMD_SWR_,
1122 				 0, 1000, 20000, 100);
1123 	switch (DEFAULT_DMA_DESCRIPTOR_SPACING) {
1124 	case DMA_DESCRIPTOR_SPACING_16:
1125 		data = DMAC_CFG_MAX_DSPACE_16_;
1126 		break;
1127 	case DMA_DESCRIPTOR_SPACING_32:
1128 		data = DMAC_CFG_MAX_DSPACE_32_;
1129 		break;
1130 	case DMA_DESCRIPTOR_SPACING_64:
1131 		data = DMAC_CFG_MAX_DSPACE_64_;
1132 		break;
1133 	case DMA_DESCRIPTOR_SPACING_128:
1134 		data = DMAC_CFG_MAX_DSPACE_128_;
1135 		break;
1136 	default:
1137 		return -EPERM;
1138 	}
1139 	if (!(adapter->csr.flags & LAN743X_CSR_FLAG_IS_A0))
1140 		data |= DMAC_CFG_COAL_EN_;
1141 	data |= DMAC_CFG_CH_ARB_SEL_RX_HIGH_;
1142 	data |= DMAC_CFG_MAX_READ_REQ_SET_(6);
1143 	lan743x_csr_write(adapter, DMAC_CFG, data);
1144 	data = DMAC_COAL_CFG_TIMER_LIMIT_SET_(1);
1145 	data |= DMAC_COAL_CFG_TIMER_TX_START_;
1146 	data |= DMAC_COAL_CFG_FLUSH_INTS_;
1147 	data |= DMAC_COAL_CFG_INT_EXIT_COAL_;
1148 	data |= DMAC_COAL_CFG_CSR_EXIT_COAL_;
1149 	data |= DMAC_COAL_CFG_TX_THRES_SET_(0x0A);
1150 	data |= DMAC_COAL_CFG_RX_THRES_SET_(0x0C);
1151 	lan743x_csr_write(adapter, DMAC_COAL_CFG, data);
1152 	data = DMAC_OBFF_TX_THRES_SET_(0x08);
1153 	data |= DMAC_OBFF_RX_THRES_SET_(0x0A);
1154 	lan743x_csr_write(adapter, DMAC_OBFF_CFG, data);
1155 	return 0;
1156 }
1157 
1158 static int lan743x_dmac_tx_get_state(struct lan743x_adapter *adapter,
1159 				     int tx_channel)
1160 {
1161 	u32 dmac_cmd = 0;
1162 
1163 	dmac_cmd = lan743x_csr_read(adapter, DMAC_CMD);
1164 	return DMAC_CHANNEL_STATE_SET((dmac_cmd &
1165 				      DMAC_CMD_START_T_(tx_channel)),
1166 				      (dmac_cmd &
1167 				      DMAC_CMD_STOP_T_(tx_channel)));
1168 }
1169 
1170 static int lan743x_dmac_tx_wait_till_stopped(struct lan743x_adapter *adapter,
1171 					     int tx_channel)
1172 {
1173 	int timeout = 100;
1174 	int result = 0;
1175 
1176 	while (timeout &&
1177 	       ((result = lan743x_dmac_tx_get_state(adapter, tx_channel)) ==
1178 	       DMAC_CHANNEL_STATE_STOP_PENDING)) {
1179 		usleep_range(1000, 20000);
1180 		timeout--;
1181 	}
1182 	if (result == DMAC_CHANNEL_STATE_STOP_PENDING)
1183 		result = -ENODEV;
1184 	return result;
1185 }
1186 
1187 static int lan743x_dmac_rx_get_state(struct lan743x_adapter *adapter,
1188 				     int rx_channel)
1189 {
1190 	u32 dmac_cmd = 0;
1191 
1192 	dmac_cmd = lan743x_csr_read(adapter, DMAC_CMD);
1193 	return DMAC_CHANNEL_STATE_SET((dmac_cmd &
1194 				      DMAC_CMD_START_R_(rx_channel)),
1195 				      (dmac_cmd &
1196 				      DMAC_CMD_STOP_R_(rx_channel)));
1197 }
1198 
1199 static int lan743x_dmac_rx_wait_till_stopped(struct lan743x_adapter *adapter,
1200 					     int rx_channel)
1201 {
1202 	int timeout = 100;
1203 	int result = 0;
1204 
1205 	while (timeout &&
1206 	       ((result = lan743x_dmac_rx_get_state(adapter, rx_channel)) ==
1207 	       DMAC_CHANNEL_STATE_STOP_PENDING)) {
1208 		usleep_range(1000, 20000);
1209 		timeout--;
1210 	}
1211 	if (result == DMAC_CHANNEL_STATE_STOP_PENDING)
1212 		result = -ENODEV;
1213 	return result;
1214 }
1215 
1216 static void lan743x_tx_release_desc(struct lan743x_tx *tx,
1217 				    int descriptor_index, bool cleanup)
1218 {
1219 	struct lan743x_tx_buffer_info *buffer_info = NULL;
1220 	struct lan743x_tx_descriptor *descriptor = NULL;
1221 	u32 descriptor_type = 0;
1222 	bool ignore_sync;
1223 
1224 	descriptor = &tx->ring_cpu_ptr[descriptor_index];
1225 	buffer_info = &tx->buffer_info[descriptor_index];
1226 	if (!(buffer_info->flags & TX_BUFFER_INFO_FLAG_ACTIVE))
1227 		goto done;
1228 
1229 	descriptor_type = (descriptor->data0) &
1230 			  TX_DESC_DATA0_DTYPE_MASK_;
1231 	if (descriptor_type == TX_DESC_DATA0_DTYPE_DATA_)
1232 		goto clean_up_data_descriptor;
1233 	else
1234 		goto clear_active;
1235 
1236 clean_up_data_descriptor:
1237 	if (buffer_info->dma_ptr) {
1238 		if (buffer_info->flags &
1239 		    TX_BUFFER_INFO_FLAG_SKB_FRAGMENT) {
1240 			dma_unmap_page(&tx->adapter->pdev->dev,
1241 				       buffer_info->dma_ptr,
1242 				       buffer_info->buffer_length,
1243 				       DMA_TO_DEVICE);
1244 		} else {
1245 			dma_unmap_single(&tx->adapter->pdev->dev,
1246 					 buffer_info->dma_ptr,
1247 					 buffer_info->buffer_length,
1248 					 DMA_TO_DEVICE);
1249 		}
1250 		buffer_info->dma_ptr = 0;
1251 		buffer_info->buffer_length = 0;
1252 	}
1253 	if (!buffer_info->skb)
1254 		goto clear_active;
1255 
1256 	if (!(buffer_info->flags & TX_BUFFER_INFO_FLAG_TIMESTAMP_REQUESTED)) {
1257 		dev_kfree_skb(buffer_info->skb);
1258 		goto clear_skb;
1259 	}
1260 
1261 	if (cleanup) {
1262 		lan743x_ptp_unrequest_tx_timestamp(tx->adapter);
1263 		dev_kfree_skb(buffer_info->skb);
1264 	} else {
1265 		ignore_sync = (buffer_info->flags &
1266 			       TX_BUFFER_INFO_FLAG_IGNORE_SYNC) != 0;
1267 		lan743x_ptp_tx_timestamp_skb(tx->adapter,
1268 					     buffer_info->skb, ignore_sync);
1269 	}
1270 
1271 clear_skb:
1272 	buffer_info->skb = NULL;
1273 
1274 clear_active:
1275 	buffer_info->flags &= ~TX_BUFFER_INFO_FLAG_ACTIVE;
1276 
1277 done:
1278 	memset(buffer_info, 0, sizeof(*buffer_info));
1279 	memset(descriptor, 0, sizeof(*descriptor));
1280 }
1281 
1282 static int lan743x_tx_next_index(struct lan743x_tx *tx, int index)
1283 {
1284 	return ((++index) % tx->ring_size);
1285 }
1286 
1287 static void lan743x_tx_release_completed_descriptors(struct lan743x_tx *tx)
1288 {
1289 	while ((*tx->head_cpu_ptr) != (tx->last_head)) {
1290 		lan743x_tx_release_desc(tx, tx->last_head, false);
1291 		tx->last_head = lan743x_tx_next_index(tx, tx->last_head);
1292 	}
1293 }
1294 
1295 static void lan743x_tx_release_all_descriptors(struct lan743x_tx *tx)
1296 {
1297 	u32 original_head = 0;
1298 
1299 	original_head = tx->last_head;
1300 	do {
1301 		lan743x_tx_release_desc(tx, tx->last_head, true);
1302 		tx->last_head = lan743x_tx_next_index(tx, tx->last_head);
1303 	} while (tx->last_head != original_head);
1304 	memset(tx->ring_cpu_ptr, 0,
1305 	       sizeof(*tx->ring_cpu_ptr) * (tx->ring_size));
1306 	memset(tx->buffer_info, 0,
1307 	       sizeof(*tx->buffer_info) * (tx->ring_size));
1308 }
1309 
1310 static int lan743x_tx_get_desc_cnt(struct lan743x_tx *tx,
1311 				   struct sk_buff *skb)
1312 {
1313 	int result = 1; /* 1 for the main skb buffer */
1314 	int nr_frags = 0;
1315 
1316 	if (skb_is_gso(skb))
1317 		result++; /* requires an extension descriptor */
1318 	nr_frags = skb_shinfo(skb)->nr_frags;
1319 	result += nr_frags; /* 1 for each fragment buffer */
1320 	return result;
1321 }
1322 
1323 static int lan743x_tx_get_avail_desc(struct lan743x_tx *tx)
1324 {
1325 	int last_head = tx->last_head;
1326 	int last_tail = tx->last_tail;
1327 
1328 	if (last_tail >= last_head)
1329 		return tx->ring_size - last_tail + last_head - 1;
1330 	else
1331 		return last_head - last_tail - 1;
1332 }
1333 
1334 void lan743x_tx_set_timestamping_mode(struct lan743x_tx *tx,
1335 				      bool enable_timestamping,
1336 				      bool enable_onestep_sync)
1337 {
1338 	if (enable_timestamping)
1339 		tx->ts_flags |= TX_TS_FLAG_TIMESTAMPING_ENABLED;
1340 	else
1341 		tx->ts_flags &= ~TX_TS_FLAG_TIMESTAMPING_ENABLED;
1342 	if (enable_onestep_sync)
1343 		tx->ts_flags |= TX_TS_FLAG_ONE_STEP_SYNC;
1344 	else
1345 		tx->ts_flags &= ~TX_TS_FLAG_ONE_STEP_SYNC;
1346 }
1347 
1348 static int lan743x_tx_frame_start(struct lan743x_tx *tx,
1349 				  unsigned char *first_buffer,
1350 				  unsigned int first_buffer_length,
1351 				  unsigned int frame_length,
1352 				  bool time_stamp,
1353 				  bool check_sum)
1354 {
1355 	/* called only from within lan743x_tx_xmit_frame.
1356 	 * assuming tx->ring_lock has already been acquired.
1357 	 */
1358 	struct lan743x_tx_descriptor *tx_descriptor = NULL;
1359 	struct lan743x_tx_buffer_info *buffer_info = NULL;
1360 	struct lan743x_adapter *adapter = tx->adapter;
1361 	struct device *dev = &adapter->pdev->dev;
1362 	dma_addr_t dma_ptr;
1363 
1364 	tx->frame_flags |= TX_FRAME_FLAG_IN_PROGRESS;
1365 	tx->frame_first = tx->last_tail;
1366 	tx->frame_tail = tx->frame_first;
1367 
1368 	tx_descriptor = &tx->ring_cpu_ptr[tx->frame_tail];
1369 	buffer_info = &tx->buffer_info[tx->frame_tail];
1370 	dma_ptr = dma_map_single(dev, first_buffer, first_buffer_length,
1371 				 DMA_TO_DEVICE);
1372 	if (dma_mapping_error(dev, dma_ptr))
1373 		return -ENOMEM;
1374 
1375 	tx_descriptor->data1 = DMA_ADDR_LOW32(dma_ptr);
1376 	tx_descriptor->data2 = DMA_ADDR_HIGH32(dma_ptr);
1377 	tx_descriptor->data3 = (frame_length << 16) &
1378 		TX_DESC_DATA3_FRAME_LENGTH_MSS_MASK_;
1379 
1380 	buffer_info->skb = NULL;
1381 	buffer_info->dma_ptr = dma_ptr;
1382 	buffer_info->buffer_length = first_buffer_length;
1383 	buffer_info->flags |= TX_BUFFER_INFO_FLAG_ACTIVE;
1384 
1385 	tx->frame_data0 = (first_buffer_length &
1386 		TX_DESC_DATA0_BUF_LENGTH_MASK_) |
1387 		TX_DESC_DATA0_DTYPE_DATA_ |
1388 		TX_DESC_DATA0_FS_ |
1389 		TX_DESC_DATA0_FCS_;
1390 	if (time_stamp)
1391 		tx->frame_data0 |= TX_DESC_DATA0_TSE_;
1392 
1393 	if (check_sum)
1394 		tx->frame_data0 |= TX_DESC_DATA0_ICE_ |
1395 				   TX_DESC_DATA0_IPE_ |
1396 				   TX_DESC_DATA0_TPE_;
1397 
1398 	/* data0 will be programmed in one of other frame assembler functions */
1399 	return 0;
1400 }
1401 
1402 static void lan743x_tx_frame_add_lso(struct lan743x_tx *tx,
1403 				     unsigned int frame_length)
1404 {
1405 	/* called only from within lan743x_tx_xmit_frame.
1406 	 * assuming tx->ring_lock has already been acquired.
1407 	 */
1408 	struct lan743x_tx_descriptor *tx_descriptor = NULL;
1409 	struct lan743x_tx_buffer_info *buffer_info = NULL;
1410 
1411 	/* wrap up previous descriptor */
1412 	tx->frame_data0 |= TX_DESC_DATA0_EXT_;
1413 	tx_descriptor = &tx->ring_cpu_ptr[tx->frame_tail];
1414 	tx_descriptor->data0 = tx->frame_data0;
1415 
1416 	/* move to next descriptor */
1417 	tx->frame_tail = lan743x_tx_next_index(tx, tx->frame_tail);
1418 	tx_descriptor = &tx->ring_cpu_ptr[tx->frame_tail];
1419 	buffer_info = &tx->buffer_info[tx->frame_tail];
1420 
1421 	/* add extension descriptor */
1422 	tx_descriptor->data1 = 0;
1423 	tx_descriptor->data2 = 0;
1424 	tx_descriptor->data3 = 0;
1425 
1426 	buffer_info->skb = NULL;
1427 	buffer_info->dma_ptr = 0;
1428 	buffer_info->buffer_length = 0;
1429 	buffer_info->flags |= TX_BUFFER_INFO_FLAG_ACTIVE;
1430 
1431 	tx->frame_data0 = (frame_length & TX_DESC_DATA0_EXT_PAY_LENGTH_MASK_) |
1432 			  TX_DESC_DATA0_DTYPE_EXT_ |
1433 			  TX_DESC_DATA0_EXT_LSO_;
1434 
1435 	/* data0 will be programmed in one of other frame assembler functions */
1436 }
1437 
1438 static int lan743x_tx_frame_add_fragment(struct lan743x_tx *tx,
1439 					 const struct skb_frag_struct *fragment,
1440 					 unsigned int frame_length)
1441 {
1442 	/* called only from within lan743x_tx_xmit_frame
1443 	 * assuming tx->ring_lock has already been acquired
1444 	 */
1445 	struct lan743x_tx_descriptor *tx_descriptor = NULL;
1446 	struct lan743x_tx_buffer_info *buffer_info = NULL;
1447 	struct lan743x_adapter *adapter = tx->adapter;
1448 	struct device *dev = &adapter->pdev->dev;
1449 	unsigned int fragment_length = 0;
1450 	dma_addr_t dma_ptr;
1451 
1452 	fragment_length = skb_frag_size(fragment);
1453 	if (!fragment_length)
1454 		return 0;
1455 
1456 	/* wrap up previous descriptor */
1457 	tx_descriptor = &tx->ring_cpu_ptr[tx->frame_tail];
1458 	tx_descriptor->data0 = tx->frame_data0;
1459 
1460 	/* move to next descriptor */
1461 	tx->frame_tail = lan743x_tx_next_index(tx, tx->frame_tail);
1462 	tx_descriptor = &tx->ring_cpu_ptr[tx->frame_tail];
1463 	buffer_info = &tx->buffer_info[tx->frame_tail];
1464 	dma_ptr = skb_frag_dma_map(dev, fragment,
1465 				   0, fragment_length,
1466 				   DMA_TO_DEVICE);
1467 	if (dma_mapping_error(dev, dma_ptr)) {
1468 		int desc_index;
1469 
1470 		/* cleanup all previously setup descriptors */
1471 		desc_index = tx->frame_first;
1472 		while (desc_index != tx->frame_tail) {
1473 			lan743x_tx_release_desc(tx, desc_index, true);
1474 			desc_index = lan743x_tx_next_index(tx, desc_index);
1475 		}
1476 		dma_wmb();
1477 		tx->frame_flags &= ~TX_FRAME_FLAG_IN_PROGRESS;
1478 		tx->frame_first = 0;
1479 		tx->frame_data0 = 0;
1480 		tx->frame_tail = 0;
1481 		return -ENOMEM;
1482 	}
1483 
1484 	tx_descriptor->data1 = DMA_ADDR_LOW32(dma_ptr);
1485 	tx_descriptor->data2 = DMA_ADDR_HIGH32(dma_ptr);
1486 	tx_descriptor->data3 = (frame_length << 16) &
1487 			       TX_DESC_DATA3_FRAME_LENGTH_MSS_MASK_;
1488 
1489 	buffer_info->skb = NULL;
1490 	buffer_info->dma_ptr = dma_ptr;
1491 	buffer_info->buffer_length = fragment_length;
1492 	buffer_info->flags |= TX_BUFFER_INFO_FLAG_ACTIVE;
1493 	buffer_info->flags |= TX_BUFFER_INFO_FLAG_SKB_FRAGMENT;
1494 
1495 	tx->frame_data0 = (fragment_length & TX_DESC_DATA0_BUF_LENGTH_MASK_) |
1496 			  TX_DESC_DATA0_DTYPE_DATA_ |
1497 			  TX_DESC_DATA0_FCS_;
1498 
1499 	/* data0 will be programmed in one of other frame assembler functions */
1500 	return 0;
1501 }
1502 
1503 static void lan743x_tx_frame_end(struct lan743x_tx *tx,
1504 				 struct sk_buff *skb,
1505 				 bool time_stamp,
1506 				 bool ignore_sync)
1507 {
1508 	/* called only from within lan743x_tx_xmit_frame
1509 	 * assuming tx->ring_lock has already been acquired
1510 	 */
1511 	struct lan743x_tx_descriptor *tx_descriptor = NULL;
1512 	struct lan743x_tx_buffer_info *buffer_info = NULL;
1513 	struct lan743x_adapter *adapter = tx->adapter;
1514 	u32 tx_tail_flags = 0;
1515 
1516 	/* wrap up previous descriptor */
1517 	tx->frame_data0 |= TX_DESC_DATA0_LS_;
1518 	tx->frame_data0 |= TX_DESC_DATA0_IOC_;
1519 
1520 	tx_descriptor = &tx->ring_cpu_ptr[tx->frame_tail];
1521 	buffer_info = &tx->buffer_info[tx->frame_tail];
1522 	buffer_info->skb = skb;
1523 	if (time_stamp)
1524 		buffer_info->flags |= TX_BUFFER_INFO_FLAG_TIMESTAMP_REQUESTED;
1525 	if (ignore_sync)
1526 		buffer_info->flags |= TX_BUFFER_INFO_FLAG_IGNORE_SYNC;
1527 
1528 	tx_descriptor->data0 = tx->frame_data0;
1529 	tx->frame_tail = lan743x_tx_next_index(tx, tx->frame_tail);
1530 	tx->last_tail = tx->frame_tail;
1531 
1532 	dma_wmb();
1533 
1534 	if (tx->vector_flags & LAN743X_VECTOR_FLAG_VECTOR_ENABLE_AUTO_SET)
1535 		tx_tail_flags |= TX_TAIL_SET_TOP_INT_VEC_EN_;
1536 	if (tx->vector_flags & LAN743X_VECTOR_FLAG_SOURCE_ENABLE_AUTO_SET)
1537 		tx_tail_flags |= TX_TAIL_SET_DMAC_INT_EN_ |
1538 		TX_TAIL_SET_TOP_INT_EN_;
1539 
1540 	lan743x_csr_write(adapter, TX_TAIL(tx->channel_number),
1541 			  tx_tail_flags | tx->frame_tail);
1542 	tx->frame_flags &= ~TX_FRAME_FLAG_IN_PROGRESS;
1543 }
1544 
1545 static netdev_tx_t lan743x_tx_xmit_frame(struct lan743x_tx *tx,
1546 					 struct sk_buff *skb)
1547 {
1548 	int required_number_of_descriptors = 0;
1549 	unsigned int start_frame_length = 0;
1550 	unsigned int frame_length = 0;
1551 	unsigned int head_length = 0;
1552 	unsigned long irq_flags = 0;
1553 	bool do_timestamp = false;
1554 	bool ignore_sync = false;
1555 	int nr_frags = 0;
1556 	bool gso = false;
1557 	int j;
1558 
1559 	required_number_of_descriptors = lan743x_tx_get_desc_cnt(tx, skb);
1560 
1561 	spin_lock_irqsave(&tx->ring_lock, irq_flags);
1562 	if (required_number_of_descriptors >
1563 		lan743x_tx_get_avail_desc(tx)) {
1564 		if (required_number_of_descriptors > (tx->ring_size - 1)) {
1565 			dev_kfree_skb(skb);
1566 		} else {
1567 			/* save to overflow buffer */
1568 			tx->overflow_skb = skb;
1569 			netif_stop_queue(tx->adapter->netdev);
1570 		}
1571 		goto unlock;
1572 	}
1573 
1574 	/* space available, transmit skb  */
1575 	if ((skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) &&
1576 	    (tx->ts_flags & TX_TS_FLAG_TIMESTAMPING_ENABLED) &&
1577 	    (lan743x_ptp_request_tx_timestamp(tx->adapter))) {
1578 		skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
1579 		do_timestamp = true;
1580 		if (tx->ts_flags & TX_TS_FLAG_ONE_STEP_SYNC)
1581 			ignore_sync = true;
1582 	}
1583 	head_length = skb_headlen(skb);
1584 	frame_length = skb_pagelen(skb);
1585 	nr_frags = skb_shinfo(skb)->nr_frags;
1586 	start_frame_length = frame_length;
1587 	gso = skb_is_gso(skb);
1588 	if (gso) {
1589 		start_frame_length = max(skb_shinfo(skb)->gso_size,
1590 					 (unsigned short)8);
1591 	}
1592 
1593 	if (lan743x_tx_frame_start(tx,
1594 				   skb->data, head_length,
1595 				   start_frame_length,
1596 				   do_timestamp,
1597 				   skb->ip_summed == CHECKSUM_PARTIAL)) {
1598 		dev_kfree_skb(skb);
1599 		goto unlock;
1600 	}
1601 
1602 	if (gso)
1603 		lan743x_tx_frame_add_lso(tx, frame_length);
1604 
1605 	if (nr_frags <= 0)
1606 		goto finish;
1607 
1608 	for (j = 0; j < nr_frags; j++) {
1609 		const struct skb_frag_struct *frag;
1610 
1611 		frag = &(skb_shinfo(skb)->frags[j]);
1612 		if (lan743x_tx_frame_add_fragment(tx, frag, frame_length)) {
1613 			/* upon error no need to call
1614 			 *	lan743x_tx_frame_end
1615 			 * frame assembler clean up was performed inside
1616 			 *	lan743x_tx_frame_add_fragment
1617 			 */
1618 			dev_kfree_skb(skb);
1619 			goto unlock;
1620 		}
1621 	}
1622 
1623 finish:
1624 	lan743x_tx_frame_end(tx, skb, do_timestamp, ignore_sync);
1625 
1626 unlock:
1627 	spin_unlock_irqrestore(&tx->ring_lock, irq_flags);
1628 	return NETDEV_TX_OK;
1629 }
1630 
1631 static int lan743x_tx_napi_poll(struct napi_struct *napi, int weight)
1632 {
1633 	struct lan743x_tx *tx = container_of(napi, struct lan743x_tx, napi);
1634 	struct lan743x_adapter *adapter = tx->adapter;
1635 	bool start_transmitter = false;
1636 	unsigned long irq_flags = 0;
1637 	u32 ioc_bit = 0;
1638 	u32 int_sts = 0;
1639 
1640 	ioc_bit = DMAC_INT_BIT_TX_IOC_(tx->channel_number);
1641 	int_sts = lan743x_csr_read(adapter, DMAC_INT_STS);
1642 	if (tx->vector_flags & LAN743X_VECTOR_FLAG_SOURCE_STATUS_W2C)
1643 		lan743x_csr_write(adapter, DMAC_INT_STS, ioc_bit);
1644 	spin_lock_irqsave(&tx->ring_lock, irq_flags);
1645 
1646 	/* clean up tx ring */
1647 	lan743x_tx_release_completed_descriptors(tx);
1648 	if (netif_queue_stopped(adapter->netdev)) {
1649 		if (tx->overflow_skb) {
1650 			if (lan743x_tx_get_desc_cnt(tx, tx->overflow_skb) <=
1651 				lan743x_tx_get_avail_desc(tx))
1652 				start_transmitter = true;
1653 		} else {
1654 			netif_wake_queue(adapter->netdev);
1655 		}
1656 	}
1657 	spin_unlock_irqrestore(&tx->ring_lock, irq_flags);
1658 
1659 	if (start_transmitter) {
1660 		/* space is now available, transmit overflow skb */
1661 		lan743x_tx_xmit_frame(tx, tx->overflow_skb);
1662 		tx->overflow_skb = NULL;
1663 		netif_wake_queue(adapter->netdev);
1664 	}
1665 
1666 	if (!napi_complete(napi))
1667 		goto done;
1668 
1669 	/* enable isr */
1670 	lan743x_csr_write(adapter, INT_EN_SET,
1671 			  INT_BIT_DMA_TX_(tx->channel_number));
1672 	lan743x_csr_read(adapter, INT_STS);
1673 
1674 done:
1675 	return 0;
1676 }
1677 
1678 static void lan743x_tx_ring_cleanup(struct lan743x_tx *tx)
1679 {
1680 	if (tx->head_cpu_ptr) {
1681 		pci_free_consistent(tx->adapter->pdev,
1682 				    sizeof(*tx->head_cpu_ptr),
1683 				    (void *)(tx->head_cpu_ptr),
1684 				    tx->head_dma_ptr);
1685 		tx->head_cpu_ptr = NULL;
1686 		tx->head_dma_ptr = 0;
1687 	}
1688 	kfree(tx->buffer_info);
1689 	tx->buffer_info = NULL;
1690 
1691 	if (tx->ring_cpu_ptr) {
1692 		pci_free_consistent(tx->adapter->pdev,
1693 				    tx->ring_allocation_size,
1694 				    tx->ring_cpu_ptr,
1695 				    tx->ring_dma_ptr);
1696 		tx->ring_allocation_size = 0;
1697 		tx->ring_cpu_ptr = NULL;
1698 		tx->ring_dma_ptr = 0;
1699 	}
1700 	tx->ring_size = 0;
1701 }
1702 
1703 static int lan743x_tx_ring_init(struct lan743x_tx *tx)
1704 {
1705 	size_t ring_allocation_size = 0;
1706 	void *cpu_ptr = NULL;
1707 	dma_addr_t dma_ptr;
1708 	int ret = -ENOMEM;
1709 
1710 	tx->ring_size = LAN743X_TX_RING_SIZE;
1711 	if (tx->ring_size & ~TX_CFG_B_TX_RING_LEN_MASK_) {
1712 		ret = -EINVAL;
1713 		goto cleanup;
1714 	}
1715 	ring_allocation_size = ALIGN(tx->ring_size *
1716 				     sizeof(struct lan743x_tx_descriptor),
1717 				     PAGE_SIZE);
1718 	dma_ptr = 0;
1719 	cpu_ptr = pci_zalloc_consistent(tx->adapter->pdev,
1720 					ring_allocation_size, &dma_ptr);
1721 	if (!cpu_ptr) {
1722 		ret = -ENOMEM;
1723 		goto cleanup;
1724 	}
1725 
1726 	tx->ring_allocation_size = ring_allocation_size;
1727 	tx->ring_cpu_ptr = (struct lan743x_tx_descriptor *)cpu_ptr;
1728 	tx->ring_dma_ptr = dma_ptr;
1729 
1730 	cpu_ptr = kcalloc(tx->ring_size, sizeof(*tx->buffer_info), GFP_KERNEL);
1731 	if (!cpu_ptr) {
1732 		ret = -ENOMEM;
1733 		goto cleanup;
1734 	}
1735 	tx->buffer_info = (struct lan743x_tx_buffer_info *)cpu_ptr;
1736 	dma_ptr = 0;
1737 	cpu_ptr = pci_zalloc_consistent(tx->adapter->pdev,
1738 					sizeof(*tx->head_cpu_ptr), &dma_ptr);
1739 	if (!cpu_ptr) {
1740 		ret = -ENOMEM;
1741 		goto cleanup;
1742 	}
1743 
1744 	tx->head_cpu_ptr = cpu_ptr;
1745 	tx->head_dma_ptr = dma_ptr;
1746 	if (tx->head_dma_ptr & 0x3) {
1747 		ret = -ENOMEM;
1748 		goto cleanup;
1749 	}
1750 
1751 	return 0;
1752 
1753 cleanup:
1754 	lan743x_tx_ring_cleanup(tx);
1755 	return ret;
1756 }
1757 
1758 static void lan743x_tx_close(struct lan743x_tx *tx)
1759 {
1760 	struct lan743x_adapter *adapter = tx->adapter;
1761 
1762 	lan743x_csr_write(adapter,
1763 			  DMAC_CMD,
1764 			  DMAC_CMD_STOP_T_(tx->channel_number));
1765 	lan743x_dmac_tx_wait_till_stopped(adapter, tx->channel_number);
1766 
1767 	lan743x_csr_write(adapter,
1768 			  DMAC_INT_EN_CLR,
1769 			  DMAC_INT_BIT_TX_IOC_(tx->channel_number));
1770 	lan743x_csr_write(adapter, INT_EN_CLR,
1771 			  INT_BIT_DMA_TX_(tx->channel_number));
1772 	napi_disable(&tx->napi);
1773 	netif_napi_del(&tx->napi);
1774 
1775 	lan743x_csr_write(adapter, FCT_TX_CTL,
1776 			  FCT_TX_CTL_DIS_(tx->channel_number));
1777 	lan743x_csr_wait_for_bit(adapter, FCT_TX_CTL,
1778 				 FCT_TX_CTL_EN_(tx->channel_number),
1779 				 0, 1000, 20000, 100);
1780 
1781 	lan743x_tx_release_all_descriptors(tx);
1782 
1783 	if (tx->overflow_skb) {
1784 		dev_kfree_skb(tx->overflow_skb);
1785 		tx->overflow_skb = NULL;
1786 	}
1787 
1788 	lan743x_tx_ring_cleanup(tx);
1789 }
1790 
1791 static int lan743x_tx_open(struct lan743x_tx *tx)
1792 {
1793 	struct lan743x_adapter *adapter = NULL;
1794 	u32 data = 0;
1795 	int ret;
1796 
1797 	adapter = tx->adapter;
1798 	ret = lan743x_tx_ring_init(tx);
1799 	if (ret)
1800 		return ret;
1801 
1802 	/* initialize fifo */
1803 	lan743x_csr_write(adapter, FCT_TX_CTL,
1804 			  FCT_TX_CTL_RESET_(tx->channel_number));
1805 	lan743x_csr_wait_for_bit(adapter, FCT_TX_CTL,
1806 				 FCT_TX_CTL_RESET_(tx->channel_number),
1807 				 0, 1000, 20000, 100);
1808 
1809 	/* enable fifo */
1810 	lan743x_csr_write(adapter, FCT_TX_CTL,
1811 			  FCT_TX_CTL_EN_(tx->channel_number));
1812 
1813 	/* reset tx channel */
1814 	lan743x_csr_write(adapter, DMAC_CMD,
1815 			  DMAC_CMD_TX_SWR_(tx->channel_number));
1816 	lan743x_csr_wait_for_bit(adapter, DMAC_CMD,
1817 				 DMAC_CMD_TX_SWR_(tx->channel_number),
1818 				 0, 1000, 20000, 100);
1819 
1820 	/* Write TX_BASE_ADDR */
1821 	lan743x_csr_write(adapter,
1822 			  TX_BASE_ADDRH(tx->channel_number),
1823 			  DMA_ADDR_HIGH32(tx->ring_dma_ptr));
1824 	lan743x_csr_write(adapter,
1825 			  TX_BASE_ADDRL(tx->channel_number),
1826 			  DMA_ADDR_LOW32(tx->ring_dma_ptr));
1827 
1828 	/* Write TX_CFG_B */
1829 	data = lan743x_csr_read(adapter, TX_CFG_B(tx->channel_number));
1830 	data &= ~TX_CFG_B_TX_RING_LEN_MASK_;
1831 	data |= ((tx->ring_size) & TX_CFG_B_TX_RING_LEN_MASK_);
1832 	if (!(adapter->csr.flags & LAN743X_CSR_FLAG_IS_A0))
1833 		data |= TX_CFG_B_TDMABL_512_;
1834 	lan743x_csr_write(adapter, TX_CFG_B(tx->channel_number), data);
1835 
1836 	/* Write TX_CFG_A */
1837 	data = TX_CFG_A_TX_TMR_HPWB_SEL_IOC_ | TX_CFG_A_TX_HP_WB_EN_;
1838 	if (!(adapter->csr.flags & LAN743X_CSR_FLAG_IS_A0)) {
1839 		data |= TX_CFG_A_TX_HP_WB_ON_INT_TMR_;
1840 		data |= TX_CFG_A_TX_PF_THRES_SET_(0x10);
1841 		data |= TX_CFG_A_TX_PF_PRI_THRES_SET_(0x04);
1842 		data |= TX_CFG_A_TX_HP_WB_THRES_SET_(0x07);
1843 	}
1844 	lan743x_csr_write(adapter, TX_CFG_A(tx->channel_number), data);
1845 
1846 	/* Write TX_HEAD_WRITEBACK_ADDR */
1847 	lan743x_csr_write(adapter,
1848 			  TX_HEAD_WRITEBACK_ADDRH(tx->channel_number),
1849 			  DMA_ADDR_HIGH32(tx->head_dma_ptr));
1850 	lan743x_csr_write(adapter,
1851 			  TX_HEAD_WRITEBACK_ADDRL(tx->channel_number),
1852 			  DMA_ADDR_LOW32(tx->head_dma_ptr));
1853 
1854 	/* set last head */
1855 	tx->last_head = lan743x_csr_read(adapter, TX_HEAD(tx->channel_number));
1856 
1857 	/* write TX_TAIL */
1858 	tx->last_tail = 0;
1859 	lan743x_csr_write(adapter, TX_TAIL(tx->channel_number),
1860 			  (u32)(tx->last_tail));
1861 	tx->vector_flags = lan743x_intr_get_vector_flags(adapter,
1862 							 INT_BIT_DMA_TX_
1863 							 (tx->channel_number));
1864 	netif_tx_napi_add(adapter->netdev,
1865 			  &tx->napi, lan743x_tx_napi_poll,
1866 			  tx->ring_size - 1);
1867 	napi_enable(&tx->napi);
1868 
1869 	data = 0;
1870 	if (tx->vector_flags & LAN743X_VECTOR_FLAG_SOURCE_ENABLE_AUTO_CLEAR)
1871 		data |= TX_CFG_C_TX_TOP_INT_EN_AUTO_CLR_;
1872 	if (tx->vector_flags & LAN743X_VECTOR_FLAG_SOURCE_STATUS_AUTO_CLEAR)
1873 		data |= TX_CFG_C_TX_DMA_INT_STS_AUTO_CLR_;
1874 	if (tx->vector_flags & LAN743X_VECTOR_FLAG_SOURCE_STATUS_R2C)
1875 		data |= TX_CFG_C_TX_INT_STS_R2C_MODE_MASK_;
1876 	if (tx->vector_flags & LAN743X_VECTOR_FLAG_SOURCE_ENABLE_R2C)
1877 		data |= TX_CFG_C_TX_INT_EN_R2C_;
1878 	lan743x_csr_write(adapter, TX_CFG_C(tx->channel_number), data);
1879 
1880 	if (!(tx->vector_flags & LAN743X_VECTOR_FLAG_SOURCE_ENABLE_AUTO_SET))
1881 		lan743x_csr_write(adapter, INT_EN_SET,
1882 				  INT_BIT_DMA_TX_(tx->channel_number));
1883 	lan743x_csr_write(adapter, DMAC_INT_EN_SET,
1884 			  DMAC_INT_BIT_TX_IOC_(tx->channel_number));
1885 
1886 	/*  start dmac channel */
1887 	lan743x_csr_write(adapter, DMAC_CMD,
1888 			  DMAC_CMD_START_T_(tx->channel_number));
1889 	return 0;
1890 }
1891 
1892 static int lan743x_rx_next_index(struct lan743x_rx *rx, int index)
1893 {
1894 	return ((++index) % rx->ring_size);
1895 }
1896 
1897 static int lan743x_rx_allocate_ring_element(struct lan743x_rx *rx, int index)
1898 {
1899 	struct lan743x_rx_buffer_info *buffer_info;
1900 	struct lan743x_rx_descriptor *descriptor;
1901 	int length = 0;
1902 
1903 	length = (LAN743X_MAX_FRAME_SIZE + ETH_HLEN + 4 + RX_HEAD_PADDING);
1904 	descriptor = &rx->ring_cpu_ptr[index];
1905 	buffer_info = &rx->buffer_info[index];
1906 	buffer_info->skb = __netdev_alloc_skb(rx->adapter->netdev,
1907 					      length,
1908 					      GFP_ATOMIC | GFP_DMA);
1909 	if (!(buffer_info->skb))
1910 		return -ENOMEM;
1911 	buffer_info->dma_ptr = dma_map_single(&rx->adapter->pdev->dev,
1912 					      buffer_info->skb->data,
1913 					      length,
1914 					      DMA_FROM_DEVICE);
1915 	if (dma_mapping_error(&rx->adapter->pdev->dev,
1916 			      buffer_info->dma_ptr)) {
1917 		buffer_info->dma_ptr = 0;
1918 		return -ENOMEM;
1919 	}
1920 
1921 	buffer_info->buffer_length = length;
1922 	descriptor->data1 = DMA_ADDR_LOW32(buffer_info->dma_ptr);
1923 	descriptor->data2 = DMA_ADDR_HIGH32(buffer_info->dma_ptr);
1924 	descriptor->data3 = 0;
1925 	descriptor->data0 = (RX_DESC_DATA0_OWN_ |
1926 			    (length & RX_DESC_DATA0_BUF_LENGTH_MASK_));
1927 	skb_reserve(buffer_info->skb, RX_HEAD_PADDING);
1928 
1929 	return 0;
1930 }
1931 
1932 static void lan743x_rx_reuse_ring_element(struct lan743x_rx *rx, int index)
1933 {
1934 	struct lan743x_rx_buffer_info *buffer_info;
1935 	struct lan743x_rx_descriptor *descriptor;
1936 
1937 	descriptor = &rx->ring_cpu_ptr[index];
1938 	buffer_info = &rx->buffer_info[index];
1939 
1940 	descriptor->data1 = DMA_ADDR_LOW32(buffer_info->dma_ptr);
1941 	descriptor->data2 = DMA_ADDR_HIGH32(buffer_info->dma_ptr);
1942 	descriptor->data3 = 0;
1943 	descriptor->data0 = (RX_DESC_DATA0_OWN_ |
1944 			    ((buffer_info->buffer_length) &
1945 			    RX_DESC_DATA0_BUF_LENGTH_MASK_));
1946 }
1947 
1948 static void lan743x_rx_release_ring_element(struct lan743x_rx *rx, int index)
1949 {
1950 	struct lan743x_rx_buffer_info *buffer_info;
1951 	struct lan743x_rx_descriptor *descriptor;
1952 
1953 	descriptor = &rx->ring_cpu_ptr[index];
1954 	buffer_info = &rx->buffer_info[index];
1955 
1956 	memset(descriptor, 0, sizeof(*descriptor));
1957 
1958 	if (buffer_info->dma_ptr) {
1959 		dma_unmap_single(&rx->adapter->pdev->dev,
1960 				 buffer_info->dma_ptr,
1961 				 buffer_info->buffer_length,
1962 				 DMA_FROM_DEVICE);
1963 		buffer_info->dma_ptr = 0;
1964 	}
1965 
1966 	if (buffer_info->skb) {
1967 		dev_kfree_skb(buffer_info->skb);
1968 		buffer_info->skb = NULL;
1969 	}
1970 
1971 	memset(buffer_info, 0, sizeof(*buffer_info));
1972 }
1973 
1974 static int lan743x_rx_process_packet(struct lan743x_rx *rx)
1975 {
1976 	struct skb_shared_hwtstamps *hwtstamps = NULL;
1977 	int result = RX_PROCESS_RESULT_NOTHING_TO_DO;
1978 	struct lan743x_rx_buffer_info *buffer_info;
1979 	struct lan743x_rx_descriptor *descriptor;
1980 	int current_head_index = -1;
1981 	int extension_index = -1;
1982 	int first_index = -1;
1983 	int last_index = -1;
1984 
1985 	current_head_index = *rx->head_cpu_ptr;
1986 	if (current_head_index < 0 || current_head_index >= rx->ring_size)
1987 		goto done;
1988 
1989 	if (rx->last_head < 0 || rx->last_head >= rx->ring_size)
1990 		goto done;
1991 
1992 	if (rx->last_head != current_head_index) {
1993 		descriptor = &rx->ring_cpu_ptr[rx->last_head];
1994 		if (descriptor->data0 & RX_DESC_DATA0_OWN_)
1995 			goto done;
1996 
1997 		if (!(descriptor->data0 & RX_DESC_DATA0_FS_))
1998 			goto done;
1999 
2000 		first_index = rx->last_head;
2001 		if (descriptor->data0 & RX_DESC_DATA0_LS_) {
2002 			last_index = rx->last_head;
2003 		} else {
2004 			int index;
2005 
2006 			index = lan743x_rx_next_index(rx, first_index);
2007 			while (index != current_head_index) {
2008 				descriptor = &rx->ring_cpu_ptr[index];
2009 				if (descriptor->data0 & RX_DESC_DATA0_OWN_)
2010 					goto done;
2011 
2012 				if (descriptor->data0 & RX_DESC_DATA0_LS_) {
2013 					last_index = index;
2014 					break;
2015 				}
2016 				index = lan743x_rx_next_index(rx, index);
2017 			}
2018 		}
2019 		if (last_index >= 0) {
2020 			descriptor = &rx->ring_cpu_ptr[last_index];
2021 			if (descriptor->data0 & RX_DESC_DATA0_EXT_) {
2022 				/* extension is expected to follow */
2023 				int index = lan743x_rx_next_index(rx,
2024 								  last_index);
2025 				if (index != current_head_index) {
2026 					descriptor = &rx->ring_cpu_ptr[index];
2027 					if (descriptor->data0 &
2028 					    RX_DESC_DATA0_OWN_) {
2029 						goto done;
2030 					}
2031 					if (descriptor->data0 &
2032 					    RX_DESC_DATA0_EXT_) {
2033 						extension_index = index;
2034 					} else {
2035 						goto done;
2036 					}
2037 				} else {
2038 					/* extension is not yet available */
2039 					/* prevent processing of this packet */
2040 					first_index = -1;
2041 					last_index = -1;
2042 				}
2043 			}
2044 		}
2045 	}
2046 	if (first_index >= 0 && last_index >= 0) {
2047 		int real_last_index = last_index;
2048 		struct sk_buff *skb = NULL;
2049 		u32 ts_sec = 0;
2050 		u32 ts_nsec = 0;
2051 
2052 		/* packet is available */
2053 		if (first_index == last_index) {
2054 			/* single buffer packet */
2055 			int packet_length;
2056 
2057 			buffer_info = &rx->buffer_info[first_index];
2058 			skb = buffer_info->skb;
2059 			descriptor = &rx->ring_cpu_ptr[first_index];
2060 
2061 			/* unmap from dma */
2062 			if (buffer_info->dma_ptr) {
2063 				dma_unmap_single(&rx->adapter->pdev->dev,
2064 						 buffer_info->dma_ptr,
2065 						 buffer_info->buffer_length,
2066 						 DMA_FROM_DEVICE);
2067 				buffer_info->dma_ptr = 0;
2068 				buffer_info->buffer_length = 0;
2069 			}
2070 			buffer_info->skb = NULL;
2071 			packet_length =	RX_DESC_DATA0_FRAME_LENGTH_GET_
2072 					(descriptor->data0);
2073 			skb_put(skb, packet_length - 4);
2074 			skb->protocol = eth_type_trans(skb,
2075 						       rx->adapter->netdev);
2076 			lan743x_rx_allocate_ring_element(rx, first_index);
2077 		} else {
2078 			int index = first_index;
2079 
2080 			/* multi buffer packet not supported */
2081 			/* this should not happen since
2082 			 * buffers are allocated to be at least jumbo size
2083 			 */
2084 
2085 			/* clean up buffers */
2086 			if (first_index <= last_index) {
2087 				while ((index >= first_index) &&
2088 				       (index <= last_index)) {
2089 					lan743x_rx_release_ring_element(rx,
2090 									index);
2091 					lan743x_rx_allocate_ring_element(rx,
2092 									 index);
2093 					index = lan743x_rx_next_index(rx,
2094 								      index);
2095 				}
2096 			} else {
2097 				while ((index >= first_index) ||
2098 				       (index <= last_index)) {
2099 					lan743x_rx_release_ring_element(rx,
2100 									index);
2101 					lan743x_rx_allocate_ring_element(rx,
2102 									 index);
2103 					index = lan743x_rx_next_index(rx,
2104 								      index);
2105 				}
2106 			}
2107 		}
2108 
2109 		if (extension_index >= 0) {
2110 			descriptor = &rx->ring_cpu_ptr[extension_index];
2111 			buffer_info = &rx->buffer_info[extension_index];
2112 
2113 			ts_sec = descriptor->data1;
2114 			ts_nsec = (descriptor->data2 &
2115 				  RX_DESC_DATA2_TS_NS_MASK_);
2116 			lan743x_rx_reuse_ring_element(rx, extension_index);
2117 			real_last_index = extension_index;
2118 		}
2119 
2120 		if (!skb) {
2121 			result = RX_PROCESS_RESULT_PACKET_DROPPED;
2122 			goto move_forward;
2123 		}
2124 
2125 		if (extension_index < 0)
2126 			goto pass_packet_to_os;
2127 		hwtstamps = skb_hwtstamps(skb);
2128 		if (hwtstamps)
2129 			hwtstamps->hwtstamp = ktime_set(ts_sec, ts_nsec);
2130 
2131 pass_packet_to_os:
2132 		/* pass packet to OS */
2133 		napi_gro_receive(&rx->napi, skb);
2134 		result = RX_PROCESS_RESULT_PACKET_RECEIVED;
2135 
2136 move_forward:
2137 		/* push tail and head forward */
2138 		rx->last_tail = real_last_index;
2139 		rx->last_head = lan743x_rx_next_index(rx, real_last_index);
2140 	}
2141 done:
2142 	return result;
2143 }
2144 
2145 static int lan743x_rx_napi_poll(struct napi_struct *napi, int weight)
2146 {
2147 	struct lan743x_rx *rx = container_of(napi, struct lan743x_rx, napi);
2148 	struct lan743x_adapter *adapter = rx->adapter;
2149 	u32 rx_tail_flags = 0;
2150 	int count;
2151 
2152 	if (rx->vector_flags & LAN743X_VECTOR_FLAG_SOURCE_STATUS_W2C) {
2153 		/* clear int status bit before reading packet */
2154 		lan743x_csr_write(adapter, DMAC_INT_STS,
2155 				  DMAC_INT_BIT_RXFRM_(rx->channel_number));
2156 	}
2157 	count = 0;
2158 	while (count < weight) {
2159 		int rx_process_result = -1;
2160 
2161 		rx_process_result = lan743x_rx_process_packet(rx);
2162 		if (rx_process_result == RX_PROCESS_RESULT_PACKET_RECEIVED) {
2163 			count++;
2164 		} else if (rx_process_result ==
2165 			RX_PROCESS_RESULT_NOTHING_TO_DO) {
2166 			break;
2167 		} else if (rx_process_result ==
2168 			RX_PROCESS_RESULT_PACKET_DROPPED) {
2169 			continue;
2170 		}
2171 	}
2172 	rx->frame_count += count;
2173 	if (count == weight)
2174 		goto done;
2175 
2176 	if (!napi_complete_done(napi, count))
2177 		goto done;
2178 
2179 	if (rx->vector_flags & LAN743X_VECTOR_FLAG_VECTOR_ENABLE_AUTO_SET)
2180 		rx_tail_flags |= RX_TAIL_SET_TOP_INT_VEC_EN_;
2181 	if (rx->vector_flags & LAN743X_VECTOR_FLAG_SOURCE_ENABLE_AUTO_SET) {
2182 		rx_tail_flags |= RX_TAIL_SET_TOP_INT_EN_;
2183 	} else {
2184 		lan743x_csr_write(adapter, INT_EN_SET,
2185 				  INT_BIT_DMA_RX_(rx->channel_number));
2186 	}
2187 
2188 	/* update RX_TAIL */
2189 	lan743x_csr_write(adapter, RX_TAIL(rx->channel_number),
2190 			  rx_tail_flags | rx->last_tail);
2191 done:
2192 	return count;
2193 }
2194 
2195 static void lan743x_rx_ring_cleanup(struct lan743x_rx *rx)
2196 {
2197 	if (rx->buffer_info && rx->ring_cpu_ptr) {
2198 		int index;
2199 
2200 		for (index = 0; index < rx->ring_size; index++)
2201 			lan743x_rx_release_ring_element(rx, index);
2202 	}
2203 
2204 	if (rx->head_cpu_ptr) {
2205 		pci_free_consistent(rx->adapter->pdev,
2206 				    sizeof(*rx->head_cpu_ptr),
2207 				    rx->head_cpu_ptr,
2208 				    rx->head_dma_ptr);
2209 		rx->head_cpu_ptr = NULL;
2210 		rx->head_dma_ptr = 0;
2211 	}
2212 
2213 	kfree(rx->buffer_info);
2214 	rx->buffer_info = NULL;
2215 
2216 	if (rx->ring_cpu_ptr) {
2217 		pci_free_consistent(rx->adapter->pdev,
2218 				    rx->ring_allocation_size,
2219 				    rx->ring_cpu_ptr,
2220 				    rx->ring_dma_ptr);
2221 		rx->ring_allocation_size = 0;
2222 		rx->ring_cpu_ptr = NULL;
2223 		rx->ring_dma_ptr = 0;
2224 	}
2225 
2226 	rx->ring_size = 0;
2227 	rx->last_head = 0;
2228 }
2229 
2230 static int lan743x_rx_ring_init(struct lan743x_rx *rx)
2231 {
2232 	size_t ring_allocation_size = 0;
2233 	dma_addr_t dma_ptr = 0;
2234 	void *cpu_ptr = NULL;
2235 	int ret = -ENOMEM;
2236 	int index = 0;
2237 
2238 	rx->ring_size = LAN743X_RX_RING_SIZE;
2239 	if (rx->ring_size <= 1) {
2240 		ret = -EINVAL;
2241 		goto cleanup;
2242 	}
2243 	if (rx->ring_size & ~RX_CFG_B_RX_RING_LEN_MASK_) {
2244 		ret = -EINVAL;
2245 		goto cleanup;
2246 	}
2247 	ring_allocation_size = ALIGN(rx->ring_size *
2248 				     sizeof(struct lan743x_rx_descriptor),
2249 				     PAGE_SIZE);
2250 	dma_ptr = 0;
2251 	cpu_ptr = pci_zalloc_consistent(rx->adapter->pdev,
2252 					ring_allocation_size, &dma_ptr);
2253 	if (!cpu_ptr) {
2254 		ret = -ENOMEM;
2255 		goto cleanup;
2256 	}
2257 	rx->ring_allocation_size = ring_allocation_size;
2258 	rx->ring_cpu_ptr = (struct lan743x_rx_descriptor *)cpu_ptr;
2259 	rx->ring_dma_ptr = dma_ptr;
2260 
2261 	cpu_ptr = kcalloc(rx->ring_size, sizeof(*rx->buffer_info),
2262 			  GFP_KERNEL);
2263 	if (!cpu_ptr) {
2264 		ret = -ENOMEM;
2265 		goto cleanup;
2266 	}
2267 	rx->buffer_info = (struct lan743x_rx_buffer_info *)cpu_ptr;
2268 	dma_ptr = 0;
2269 	cpu_ptr = pci_zalloc_consistent(rx->adapter->pdev,
2270 					sizeof(*rx->head_cpu_ptr), &dma_ptr);
2271 	if (!cpu_ptr) {
2272 		ret = -ENOMEM;
2273 		goto cleanup;
2274 	}
2275 
2276 	rx->head_cpu_ptr = cpu_ptr;
2277 	rx->head_dma_ptr = dma_ptr;
2278 	if (rx->head_dma_ptr & 0x3) {
2279 		ret = -ENOMEM;
2280 		goto cleanup;
2281 	}
2282 
2283 	rx->last_head = 0;
2284 	for (index = 0; index < rx->ring_size; index++) {
2285 		ret = lan743x_rx_allocate_ring_element(rx, index);
2286 		if (ret)
2287 			goto cleanup;
2288 	}
2289 	return 0;
2290 
2291 cleanup:
2292 	lan743x_rx_ring_cleanup(rx);
2293 	return ret;
2294 }
2295 
2296 static void lan743x_rx_close(struct lan743x_rx *rx)
2297 {
2298 	struct lan743x_adapter *adapter = rx->adapter;
2299 
2300 	lan743x_csr_write(adapter, FCT_RX_CTL,
2301 			  FCT_RX_CTL_DIS_(rx->channel_number));
2302 	lan743x_csr_wait_for_bit(adapter, FCT_RX_CTL,
2303 				 FCT_RX_CTL_EN_(rx->channel_number),
2304 				 0, 1000, 20000, 100);
2305 
2306 	lan743x_csr_write(adapter, DMAC_CMD,
2307 			  DMAC_CMD_STOP_R_(rx->channel_number));
2308 	lan743x_dmac_rx_wait_till_stopped(adapter, rx->channel_number);
2309 
2310 	lan743x_csr_write(adapter, DMAC_INT_EN_CLR,
2311 			  DMAC_INT_BIT_RXFRM_(rx->channel_number));
2312 	lan743x_csr_write(adapter, INT_EN_CLR,
2313 			  INT_BIT_DMA_RX_(rx->channel_number));
2314 	napi_disable(&rx->napi);
2315 
2316 	netif_napi_del(&rx->napi);
2317 
2318 	lan743x_rx_ring_cleanup(rx);
2319 }
2320 
2321 static int lan743x_rx_open(struct lan743x_rx *rx)
2322 {
2323 	struct lan743x_adapter *adapter = rx->adapter;
2324 	u32 data = 0;
2325 	int ret;
2326 
2327 	rx->frame_count = 0;
2328 	ret = lan743x_rx_ring_init(rx);
2329 	if (ret)
2330 		goto return_error;
2331 
2332 	netif_napi_add(adapter->netdev,
2333 		       &rx->napi, lan743x_rx_napi_poll,
2334 		       rx->ring_size - 1);
2335 
2336 	lan743x_csr_write(adapter, DMAC_CMD,
2337 			  DMAC_CMD_RX_SWR_(rx->channel_number));
2338 	lan743x_csr_wait_for_bit(adapter, DMAC_CMD,
2339 				 DMAC_CMD_RX_SWR_(rx->channel_number),
2340 				 0, 1000, 20000, 100);
2341 
2342 	/* set ring base address */
2343 	lan743x_csr_write(adapter,
2344 			  RX_BASE_ADDRH(rx->channel_number),
2345 			  DMA_ADDR_HIGH32(rx->ring_dma_ptr));
2346 	lan743x_csr_write(adapter,
2347 			  RX_BASE_ADDRL(rx->channel_number),
2348 			  DMA_ADDR_LOW32(rx->ring_dma_ptr));
2349 
2350 	/* set rx write back address */
2351 	lan743x_csr_write(adapter,
2352 			  RX_HEAD_WRITEBACK_ADDRH(rx->channel_number),
2353 			  DMA_ADDR_HIGH32(rx->head_dma_ptr));
2354 	lan743x_csr_write(adapter,
2355 			  RX_HEAD_WRITEBACK_ADDRL(rx->channel_number),
2356 			  DMA_ADDR_LOW32(rx->head_dma_ptr));
2357 	data = RX_CFG_A_RX_HP_WB_EN_;
2358 	if (!(adapter->csr.flags & LAN743X_CSR_FLAG_IS_A0)) {
2359 		data |= (RX_CFG_A_RX_WB_ON_INT_TMR_ |
2360 			RX_CFG_A_RX_WB_THRES_SET_(0x7) |
2361 			RX_CFG_A_RX_PF_THRES_SET_(16) |
2362 			RX_CFG_A_RX_PF_PRI_THRES_SET_(4));
2363 	}
2364 
2365 	/* set RX_CFG_A */
2366 	lan743x_csr_write(adapter,
2367 			  RX_CFG_A(rx->channel_number), data);
2368 
2369 	/* set RX_CFG_B */
2370 	data = lan743x_csr_read(adapter, RX_CFG_B(rx->channel_number));
2371 	data &= ~RX_CFG_B_RX_PAD_MASK_;
2372 	if (!RX_HEAD_PADDING)
2373 		data |= RX_CFG_B_RX_PAD_0_;
2374 	else
2375 		data |= RX_CFG_B_RX_PAD_2_;
2376 	data &= ~RX_CFG_B_RX_RING_LEN_MASK_;
2377 	data |= ((rx->ring_size) & RX_CFG_B_RX_RING_LEN_MASK_);
2378 	data |= RX_CFG_B_TS_ALL_RX_;
2379 	if (!(adapter->csr.flags & LAN743X_CSR_FLAG_IS_A0))
2380 		data |= RX_CFG_B_RDMABL_512_;
2381 
2382 	lan743x_csr_write(adapter, RX_CFG_B(rx->channel_number), data);
2383 	rx->vector_flags = lan743x_intr_get_vector_flags(adapter,
2384 							 INT_BIT_DMA_RX_
2385 							 (rx->channel_number));
2386 
2387 	/* set RX_CFG_C */
2388 	data = 0;
2389 	if (rx->vector_flags & LAN743X_VECTOR_FLAG_SOURCE_ENABLE_AUTO_CLEAR)
2390 		data |= RX_CFG_C_RX_TOP_INT_EN_AUTO_CLR_;
2391 	if (rx->vector_flags & LAN743X_VECTOR_FLAG_SOURCE_STATUS_AUTO_CLEAR)
2392 		data |= RX_CFG_C_RX_DMA_INT_STS_AUTO_CLR_;
2393 	if (rx->vector_flags & LAN743X_VECTOR_FLAG_SOURCE_STATUS_R2C)
2394 		data |= RX_CFG_C_RX_INT_STS_R2C_MODE_MASK_;
2395 	if (rx->vector_flags & LAN743X_VECTOR_FLAG_SOURCE_ENABLE_R2C)
2396 		data |= RX_CFG_C_RX_INT_EN_R2C_;
2397 	lan743x_csr_write(adapter, RX_CFG_C(rx->channel_number), data);
2398 
2399 	rx->last_tail = ((u32)(rx->ring_size - 1));
2400 	lan743x_csr_write(adapter, RX_TAIL(rx->channel_number),
2401 			  rx->last_tail);
2402 	rx->last_head = lan743x_csr_read(adapter, RX_HEAD(rx->channel_number));
2403 	if (rx->last_head) {
2404 		ret = -EIO;
2405 		goto napi_delete;
2406 	}
2407 
2408 	napi_enable(&rx->napi);
2409 
2410 	lan743x_csr_write(adapter, INT_EN_SET,
2411 			  INT_BIT_DMA_RX_(rx->channel_number));
2412 	lan743x_csr_write(adapter, DMAC_INT_STS,
2413 			  DMAC_INT_BIT_RXFRM_(rx->channel_number));
2414 	lan743x_csr_write(adapter, DMAC_INT_EN_SET,
2415 			  DMAC_INT_BIT_RXFRM_(rx->channel_number));
2416 	lan743x_csr_write(adapter, DMAC_CMD,
2417 			  DMAC_CMD_START_R_(rx->channel_number));
2418 
2419 	/* initialize fifo */
2420 	lan743x_csr_write(adapter, FCT_RX_CTL,
2421 			  FCT_RX_CTL_RESET_(rx->channel_number));
2422 	lan743x_csr_wait_for_bit(adapter, FCT_RX_CTL,
2423 				 FCT_RX_CTL_RESET_(rx->channel_number),
2424 				 0, 1000, 20000, 100);
2425 	lan743x_csr_write(adapter, FCT_FLOW(rx->channel_number),
2426 			  FCT_FLOW_CTL_REQ_EN_ |
2427 			  FCT_FLOW_CTL_ON_THRESHOLD_SET_(0x2A) |
2428 			  FCT_FLOW_CTL_OFF_THRESHOLD_SET_(0xA));
2429 
2430 	/* enable fifo */
2431 	lan743x_csr_write(adapter, FCT_RX_CTL,
2432 			  FCT_RX_CTL_EN_(rx->channel_number));
2433 	return 0;
2434 
2435 napi_delete:
2436 	netif_napi_del(&rx->napi);
2437 	lan743x_rx_ring_cleanup(rx);
2438 
2439 return_error:
2440 	return ret;
2441 }
2442 
2443 static int lan743x_netdev_close(struct net_device *netdev)
2444 {
2445 	struct lan743x_adapter *adapter = netdev_priv(netdev);
2446 	int index;
2447 
2448 	lan743x_tx_close(&adapter->tx[0]);
2449 
2450 	for (index = 0; index < LAN743X_USED_RX_CHANNELS; index++)
2451 		lan743x_rx_close(&adapter->rx[index]);
2452 
2453 	lan743x_ptp_close(adapter);
2454 
2455 	lan743x_phy_close(adapter);
2456 
2457 	lan743x_mac_close(adapter);
2458 
2459 	lan743x_intr_close(adapter);
2460 
2461 	return 0;
2462 }
2463 
2464 static int lan743x_netdev_open(struct net_device *netdev)
2465 {
2466 	struct lan743x_adapter *adapter = netdev_priv(netdev);
2467 	int index;
2468 	int ret;
2469 
2470 	ret = lan743x_intr_open(adapter);
2471 	if (ret)
2472 		goto return_error;
2473 
2474 	ret = lan743x_mac_open(adapter);
2475 	if (ret)
2476 		goto close_intr;
2477 
2478 	ret = lan743x_phy_open(adapter);
2479 	if (ret)
2480 		goto close_mac;
2481 
2482 	ret = lan743x_ptp_open(adapter);
2483 	if (ret)
2484 		goto close_phy;
2485 
2486 	lan743x_rfe_open(adapter);
2487 
2488 	for (index = 0; index < LAN743X_USED_RX_CHANNELS; index++) {
2489 		ret = lan743x_rx_open(&adapter->rx[index]);
2490 		if (ret)
2491 			goto close_rx;
2492 	}
2493 
2494 	ret = lan743x_tx_open(&adapter->tx[0]);
2495 	if (ret)
2496 		goto close_rx;
2497 
2498 	return 0;
2499 
2500 close_rx:
2501 	for (index = 0; index < LAN743X_USED_RX_CHANNELS; index++) {
2502 		if (adapter->rx[index].ring_cpu_ptr)
2503 			lan743x_rx_close(&adapter->rx[index]);
2504 	}
2505 	lan743x_ptp_close(adapter);
2506 
2507 close_phy:
2508 	lan743x_phy_close(adapter);
2509 
2510 close_mac:
2511 	lan743x_mac_close(adapter);
2512 
2513 close_intr:
2514 	lan743x_intr_close(adapter);
2515 
2516 return_error:
2517 	netif_warn(adapter, ifup, adapter->netdev,
2518 		   "Error opening LAN743x\n");
2519 	return ret;
2520 }
2521 
2522 static netdev_tx_t lan743x_netdev_xmit_frame(struct sk_buff *skb,
2523 					     struct net_device *netdev)
2524 {
2525 	struct lan743x_adapter *adapter = netdev_priv(netdev);
2526 
2527 	return lan743x_tx_xmit_frame(&adapter->tx[0], skb);
2528 }
2529 
2530 static int lan743x_netdev_ioctl(struct net_device *netdev,
2531 				struct ifreq *ifr, int cmd)
2532 {
2533 	if (!netif_running(netdev))
2534 		return -EINVAL;
2535 	if (cmd == SIOCSHWTSTAMP)
2536 		return lan743x_ptp_ioctl(netdev, ifr, cmd);
2537 	return phy_mii_ioctl(netdev->phydev, ifr, cmd);
2538 }
2539 
2540 static void lan743x_netdev_set_multicast(struct net_device *netdev)
2541 {
2542 	struct lan743x_adapter *adapter = netdev_priv(netdev);
2543 
2544 	lan743x_rfe_set_multicast(adapter);
2545 }
2546 
2547 static int lan743x_netdev_change_mtu(struct net_device *netdev, int new_mtu)
2548 {
2549 	struct lan743x_adapter *adapter = netdev_priv(netdev);
2550 	int ret = 0;
2551 
2552 	ret = lan743x_mac_set_mtu(adapter, new_mtu);
2553 	if (!ret)
2554 		netdev->mtu = new_mtu;
2555 	return ret;
2556 }
2557 
2558 static void lan743x_netdev_get_stats64(struct net_device *netdev,
2559 				       struct rtnl_link_stats64 *stats)
2560 {
2561 	struct lan743x_adapter *adapter = netdev_priv(netdev);
2562 
2563 	stats->rx_packets = lan743x_csr_read(adapter, STAT_RX_TOTAL_FRAMES);
2564 	stats->tx_packets = lan743x_csr_read(adapter, STAT_TX_TOTAL_FRAMES);
2565 	stats->rx_bytes = lan743x_csr_read(adapter,
2566 					   STAT_RX_UNICAST_BYTE_COUNT) +
2567 			  lan743x_csr_read(adapter,
2568 					   STAT_RX_BROADCAST_BYTE_COUNT) +
2569 			  lan743x_csr_read(adapter,
2570 					   STAT_RX_MULTICAST_BYTE_COUNT);
2571 	stats->tx_bytes = lan743x_csr_read(adapter,
2572 					   STAT_TX_UNICAST_BYTE_COUNT) +
2573 			  lan743x_csr_read(adapter,
2574 					   STAT_TX_BROADCAST_BYTE_COUNT) +
2575 			  lan743x_csr_read(adapter,
2576 					   STAT_TX_MULTICAST_BYTE_COUNT);
2577 	stats->rx_errors = lan743x_csr_read(adapter, STAT_RX_FCS_ERRORS) +
2578 			   lan743x_csr_read(adapter,
2579 					    STAT_RX_ALIGNMENT_ERRORS) +
2580 			   lan743x_csr_read(adapter, STAT_RX_JABBER_ERRORS) +
2581 			   lan743x_csr_read(adapter,
2582 					    STAT_RX_UNDERSIZE_FRAME_ERRORS) +
2583 			   lan743x_csr_read(adapter,
2584 					    STAT_RX_OVERSIZE_FRAME_ERRORS);
2585 	stats->tx_errors = lan743x_csr_read(adapter, STAT_TX_FCS_ERRORS) +
2586 			   lan743x_csr_read(adapter,
2587 					    STAT_TX_EXCESS_DEFERRAL_ERRORS) +
2588 			   lan743x_csr_read(adapter, STAT_TX_CARRIER_ERRORS);
2589 	stats->rx_dropped = lan743x_csr_read(adapter,
2590 					     STAT_RX_DROPPED_FRAMES);
2591 	stats->tx_dropped = lan743x_csr_read(adapter,
2592 					     STAT_TX_EXCESSIVE_COLLISION);
2593 	stats->multicast = lan743x_csr_read(adapter,
2594 					    STAT_RX_MULTICAST_FRAMES) +
2595 			   lan743x_csr_read(adapter,
2596 					    STAT_TX_MULTICAST_FRAMES);
2597 	stats->collisions = lan743x_csr_read(adapter,
2598 					     STAT_TX_SINGLE_COLLISIONS) +
2599 			    lan743x_csr_read(adapter,
2600 					     STAT_TX_MULTIPLE_COLLISIONS) +
2601 			    lan743x_csr_read(adapter,
2602 					     STAT_TX_LATE_COLLISIONS);
2603 }
2604 
2605 static int lan743x_netdev_set_mac_address(struct net_device *netdev,
2606 					  void *addr)
2607 {
2608 	struct lan743x_adapter *adapter = netdev_priv(netdev);
2609 	struct sockaddr *sock_addr = addr;
2610 	int ret;
2611 
2612 	ret = eth_prepare_mac_addr_change(netdev, sock_addr);
2613 	if (ret)
2614 		return ret;
2615 	ether_addr_copy(netdev->dev_addr, sock_addr->sa_data);
2616 	lan743x_mac_set_address(adapter, sock_addr->sa_data);
2617 	lan743x_rfe_update_mac_address(adapter);
2618 	return 0;
2619 }
2620 
2621 static const struct net_device_ops lan743x_netdev_ops = {
2622 	.ndo_open		= lan743x_netdev_open,
2623 	.ndo_stop		= lan743x_netdev_close,
2624 	.ndo_start_xmit		= lan743x_netdev_xmit_frame,
2625 	.ndo_do_ioctl		= lan743x_netdev_ioctl,
2626 	.ndo_set_rx_mode	= lan743x_netdev_set_multicast,
2627 	.ndo_change_mtu		= lan743x_netdev_change_mtu,
2628 	.ndo_get_stats64	= lan743x_netdev_get_stats64,
2629 	.ndo_set_mac_address	= lan743x_netdev_set_mac_address,
2630 };
2631 
2632 static void lan743x_hardware_cleanup(struct lan743x_adapter *adapter)
2633 {
2634 	lan743x_csr_write(adapter, INT_EN_CLR, 0xFFFFFFFF);
2635 }
2636 
2637 static void lan743x_mdiobus_cleanup(struct lan743x_adapter *adapter)
2638 {
2639 	mdiobus_unregister(adapter->mdiobus);
2640 }
2641 
2642 static void lan743x_full_cleanup(struct lan743x_adapter *adapter)
2643 {
2644 	unregister_netdev(adapter->netdev);
2645 
2646 	lan743x_mdiobus_cleanup(adapter);
2647 	lan743x_hardware_cleanup(adapter);
2648 	lan743x_pci_cleanup(adapter);
2649 }
2650 
2651 static int lan743x_hardware_init(struct lan743x_adapter *adapter,
2652 				 struct pci_dev *pdev)
2653 {
2654 	struct lan743x_tx *tx;
2655 	int index;
2656 	int ret;
2657 
2658 	adapter->intr.irq = adapter->pdev->irq;
2659 	lan743x_csr_write(adapter, INT_EN_CLR, 0xFFFFFFFF);
2660 	mutex_init(&adapter->dp_lock);
2661 
2662 	ret = lan743x_gpio_init(adapter);
2663 	if (ret)
2664 		return ret;
2665 
2666 	ret = lan743x_mac_init(adapter);
2667 	if (ret)
2668 		return ret;
2669 
2670 	ret = lan743x_phy_init(adapter);
2671 	if (ret)
2672 		return ret;
2673 
2674 	ret = lan743x_ptp_init(adapter);
2675 	if (ret)
2676 		return ret;
2677 
2678 	lan743x_rfe_update_mac_address(adapter);
2679 
2680 	ret = lan743x_dmac_init(adapter);
2681 	if (ret)
2682 		return ret;
2683 
2684 	for (index = 0; index < LAN743X_USED_RX_CHANNELS; index++) {
2685 		adapter->rx[index].adapter = adapter;
2686 		adapter->rx[index].channel_number = index;
2687 	}
2688 
2689 	tx = &adapter->tx[0];
2690 	tx->adapter = adapter;
2691 	tx->channel_number = 0;
2692 	spin_lock_init(&tx->ring_lock);
2693 	return 0;
2694 }
2695 
2696 static int lan743x_mdiobus_init(struct lan743x_adapter *adapter)
2697 {
2698 	int ret;
2699 
2700 	adapter->mdiobus = devm_mdiobus_alloc(&adapter->pdev->dev);
2701 	if (!(adapter->mdiobus)) {
2702 		ret = -ENOMEM;
2703 		goto return_error;
2704 	}
2705 
2706 	adapter->mdiobus->priv = (void *)adapter;
2707 	adapter->mdiobus->read = lan743x_mdiobus_read;
2708 	adapter->mdiobus->write = lan743x_mdiobus_write;
2709 	adapter->mdiobus->name = "lan743x-mdiobus";
2710 	snprintf(adapter->mdiobus->id, MII_BUS_ID_SIZE,
2711 		 "pci-%s", pci_name(adapter->pdev));
2712 
2713 	if ((adapter->csr.id_rev & ID_REV_ID_MASK_) == ID_REV_ID_LAN7430_)
2714 		/* LAN7430 uses internal phy at address 1 */
2715 		adapter->mdiobus->phy_mask = ~(u32)BIT(1);
2716 
2717 	/* register mdiobus */
2718 	ret = mdiobus_register(adapter->mdiobus);
2719 	if (ret < 0)
2720 		goto return_error;
2721 	return 0;
2722 
2723 return_error:
2724 	return ret;
2725 }
2726 
2727 /* lan743x_pcidev_probe - Device Initialization Routine
2728  * @pdev: PCI device information struct
2729  * @id: entry in lan743x_pci_tbl
2730  *
2731  * Returns 0 on success, negative on failure
2732  *
2733  * initializes an adapter identified by a pci_dev structure.
2734  * The OS initialization, configuring of the adapter private structure,
2735  * and a hardware reset occur.
2736  **/
2737 static int lan743x_pcidev_probe(struct pci_dev *pdev,
2738 				const struct pci_device_id *id)
2739 {
2740 	struct lan743x_adapter *adapter = NULL;
2741 	struct net_device *netdev = NULL;
2742 	int ret = -ENODEV;
2743 
2744 	netdev = devm_alloc_etherdev(&pdev->dev,
2745 				     sizeof(struct lan743x_adapter));
2746 	if (!netdev)
2747 		goto return_error;
2748 
2749 	SET_NETDEV_DEV(netdev, &pdev->dev);
2750 	pci_set_drvdata(pdev, netdev);
2751 	adapter = netdev_priv(netdev);
2752 	adapter->netdev = netdev;
2753 	adapter->msg_enable = NETIF_MSG_DRV | NETIF_MSG_PROBE |
2754 			      NETIF_MSG_LINK | NETIF_MSG_IFUP |
2755 			      NETIF_MSG_IFDOWN | NETIF_MSG_TX_QUEUED;
2756 	netdev->max_mtu = LAN743X_MAX_FRAME_SIZE;
2757 
2758 	ret = lan743x_pci_init(adapter, pdev);
2759 	if (ret)
2760 		goto return_error;
2761 
2762 	ret = lan743x_csr_init(adapter);
2763 	if (ret)
2764 		goto cleanup_pci;
2765 
2766 	ret = lan743x_hardware_init(adapter, pdev);
2767 	if (ret)
2768 		goto cleanup_pci;
2769 
2770 	ret = lan743x_mdiobus_init(adapter);
2771 	if (ret)
2772 		goto cleanup_hardware;
2773 
2774 	adapter->netdev->netdev_ops = &lan743x_netdev_ops;
2775 	adapter->netdev->ethtool_ops = &lan743x_ethtool_ops;
2776 	adapter->netdev->features = NETIF_F_SG | NETIF_F_TSO | NETIF_F_HW_CSUM;
2777 	adapter->netdev->hw_features = adapter->netdev->features;
2778 
2779 	/* carrier off reporting is important to ethtool even BEFORE open */
2780 	netif_carrier_off(netdev);
2781 
2782 	ret = register_netdev(adapter->netdev);
2783 	if (ret < 0)
2784 		goto cleanup_mdiobus;
2785 	return 0;
2786 
2787 cleanup_mdiobus:
2788 	lan743x_mdiobus_cleanup(adapter);
2789 
2790 cleanup_hardware:
2791 	lan743x_hardware_cleanup(adapter);
2792 
2793 cleanup_pci:
2794 	lan743x_pci_cleanup(adapter);
2795 
2796 return_error:
2797 	pr_warn("Initialization failed\n");
2798 	return ret;
2799 }
2800 
2801 /**
2802  * lan743x_pcidev_remove - Device Removal Routine
2803  * @pdev: PCI device information struct
2804  *
2805  * this is called by the PCI subsystem to alert the driver
2806  * that it should release a PCI device.  This could be caused by a
2807  * Hot-Plug event, or because the driver is going to be removed from
2808  * memory.
2809  **/
2810 static void lan743x_pcidev_remove(struct pci_dev *pdev)
2811 {
2812 	struct net_device *netdev = pci_get_drvdata(pdev);
2813 	struct lan743x_adapter *adapter = netdev_priv(netdev);
2814 
2815 	lan743x_full_cleanup(adapter);
2816 }
2817 
2818 static void lan743x_pcidev_shutdown(struct pci_dev *pdev)
2819 {
2820 	struct net_device *netdev = pci_get_drvdata(pdev);
2821 	struct lan743x_adapter *adapter = netdev_priv(netdev);
2822 
2823 	rtnl_lock();
2824 	netif_device_detach(netdev);
2825 
2826 	/* close netdev when netdev is at running state.
2827 	 * For instance, it is true when system goes to sleep by pm-suspend
2828 	 * However, it is false when system goes to sleep by suspend GUI menu
2829 	 */
2830 	if (netif_running(netdev))
2831 		lan743x_netdev_close(netdev);
2832 	rtnl_unlock();
2833 
2834 #ifdef CONFIG_PM
2835 	pci_save_state(pdev);
2836 #endif
2837 
2838 	/* clean up lan743x portion */
2839 	lan743x_hardware_cleanup(adapter);
2840 }
2841 
2842 #ifdef CONFIG_PM_SLEEP
2843 static u16 lan743x_pm_wakeframe_crc16(const u8 *buf, int len)
2844 {
2845 	return bitrev16(crc16(0xFFFF, buf, len));
2846 }
2847 
2848 static void lan743x_pm_set_wol(struct lan743x_adapter *adapter)
2849 {
2850 	const u8 ipv4_multicast[3] = { 0x01, 0x00, 0x5E };
2851 	const u8 ipv6_multicast[3] = { 0x33, 0x33 };
2852 	const u8 arp_type[2] = { 0x08, 0x06 };
2853 	int mask_index;
2854 	u32 pmtctl;
2855 	u32 wucsr;
2856 	u32 macrx;
2857 	u16 crc;
2858 
2859 	for (mask_index = 0; mask_index < MAC_NUM_OF_WUF_CFG; mask_index++)
2860 		lan743x_csr_write(adapter, MAC_WUF_CFG(mask_index), 0);
2861 
2862 	/* clear wake settings */
2863 	pmtctl = lan743x_csr_read(adapter, PMT_CTL);
2864 	pmtctl |= PMT_CTL_WUPS_MASK_;
2865 	pmtctl &= ~(PMT_CTL_GPIO_WAKEUP_EN_ | PMT_CTL_EEE_WAKEUP_EN_ |
2866 		PMT_CTL_WOL_EN_ | PMT_CTL_MAC_D3_RX_CLK_OVR_ |
2867 		PMT_CTL_RX_FCT_RFE_D3_CLK_OVR_ | PMT_CTL_ETH_PHY_WAKE_EN_);
2868 
2869 	macrx = lan743x_csr_read(adapter, MAC_RX);
2870 
2871 	wucsr = 0;
2872 	mask_index = 0;
2873 
2874 	pmtctl |= PMT_CTL_ETH_PHY_D3_COLD_OVR_ | PMT_CTL_ETH_PHY_D3_OVR_;
2875 
2876 	if (adapter->wolopts & WAKE_PHY) {
2877 		pmtctl |= PMT_CTL_ETH_PHY_EDPD_PLL_CTL_;
2878 		pmtctl |= PMT_CTL_ETH_PHY_WAKE_EN_;
2879 	}
2880 	if (adapter->wolopts & WAKE_MAGIC) {
2881 		wucsr |= MAC_WUCSR_MPEN_;
2882 		macrx |= MAC_RX_RXEN_;
2883 		pmtctl |= PMT_CTL_WOL_EN_ | PMT_CTL_MAC_D3_RX_CLK_OVR_;
2884 	}
2885 	if (adapter->wolopts & WAKE_UCAST) {
2886 		wucsr |= MAC_WUCSR_RFE_WAKE_EN_ | MAC_WUCSR_PFDA_EN_;
2887 		macrx |= MAC_RX_RXEN_;
2888 		pmtctl |= PMT_CTL_WOL_EN_ | PMT_CTL_MAC_D3_RX_CLK_OVR_;
2889 		pmtctl |= PMT_CTL_RX_FCT_RFE_D3_CLK_OVR_;
2890 	}
2891 	if (adapter->wolopts & WAKE_BCAST) {
2892 		wucsr |= MAC_WUCSR_RFE_WAKE_EN_ | MAC_WUCSR_BCST_EN_;
2893 		macrx |= MAC_RX_RXEN_;
2894 		pmtctl |= PMT_CTL_WOL_EN_ | PMT_CTL_MAC_D3_RX_CLK_OVR_;
2895 		pmtctl |= PMT_CTL_RX_FCT_RFE_D3_CLK_OVR_;
2896 	}
2897 	if (adapter->wolopts & WAKE_MCAST) {
2898 		/* IPv4 multicast */
2899 		crc = lan743x_pm_wakeframe_crc16(ipv4_multicast, 3);
2900 		lan743x_csr_write(adapter, MAC_WUF_CFG(mask_index),
2901 				  MAC_WUF_CFG_EN_ | MAC_WUF_CFG_TYPE_MCAST_ |
2902 				  (0 << MAC_WUF_CFG_OFFSET_SHIFT_) |
2903 				  (crc & MAC_WUF_CFG_CRC16_MASK_));
2904 		lan743x_csr_write(adapter, MAC_WUF_MASK0(mask_index), 7);
2905 		lan743x_csr_write(adapter, MAC_WUF_MASK1(mask_index), 0);
2906 		lan743x_csr_write(adapter, MAC_WUF_MASK2(mask_index), 0);
2907 		lan743x_csr_write(adapter, MAC_WUF_MASK3(mask_index), 0);
2908 		mask_index++;
2909 
2910 		/* IPv6 multicast */
2911 		crc = lan743x_pm_wakeframe_crc16(ipv6_multicast, 2);
2912 		lan743x_csr_write(adapter, MAC_WUF_CFG(mask_index),
2913 				  MAC_WUF_CFG_EN_ | MAC_WUF_CFG_TYPE_MCAST_ |
2914 				  (0 << MAC_WUF_CFG_OFFSET_SHIFT_) |
2915 				  (crc & MAC_WUF_CFG_CRC16_MASK_));
2916 		lan743x_csr_write(adapter, MAC_WUF_MASK0(mask_index), 3);
2917 		lan743x_csr_write(adapter, MAC_WUF_MASK1(mask_index), 0);
2918 		lan743x_csr_write(adapter, MAC_WUF_MASK2(mask_index), 0);
2919 		lan743x_csr_write(adapter, MAC_WUF_MASK3(mask_index), 0);
2920 		mask_index++;
2921 
2922 		wucsr |= MAC_WUCSR_RFE_WAKE_EN_ | MAC_WUCSR_WAKE_EN_;
2923 		macrx |= MAC_RX_RXEN_;
2924 		pmtctl |= PMT_CTL_WOL_EN_ | PMT_CTL_MAC_D3_RX_CLK_OVR_;
2925 		pmtctl |= PMT_CTL_RX_FCT_RFE_D3_CLK_OVR_;
2926 	}
2927 	if (adapter->wolopts & WAKE_ARP) {
2928 		/* set MAC_WUF_CFG & WUF_MASK
2929 		 * for packettype (offset 12,13) = ARP (0x0806)
2930 		 */
2931 		crc = lan743x_pm_wakeframe_crc16(arp_type, 2);
2932 		lan743x_csr_write(adapter, MAC_WUF_CFG(mask_index),
2933 				  MAC_WUF_CFG_EN_ | MAC_WUF_CFG_TYPE_ALL_ |
2934 				  (0 << MAC_WUF_CFG_OFFSET_SHIFT_) |
2935 				  (crc & MAC_WUF_CFG_CRC16_MASK_));
2936 		lan743x_csr_write(adapter, MAC_WUF_MASK0(mask_index), 0x3000);
2937 		lan743x_csr_write(adapter, MAC_WUF_MASK1(mask_index), 0);
2938 		lan743x_csr_write(adapter, MAC_WUF_MASK2(mask_index), 0);
2939 		lan743x_csr_write(adapter, MAC_WUF_MASK3(mask_index), 0);
2940 		mask_index++;
2941 
2942 		wucsr |= MAC_WUCSR_RFE_WAKE_EN_ | MAC_WUCSR_WAKE_EN_;
2943 		macrx |= MAC_RX_RXEN_;
2944 		pmtctl |= PMT_CTL_WOL_EN_ | PMT_CTL_MAC_D3_RX_CLK_OVR_;
2945 		pmtctl |= PMT_CTL_RX_FCT_RFE_D3_CLK_OVR_;
2946 	}
2947 
2948 	lan743x_csr_write(adapter, MAC_WUCSR, wucsr);
2949 	lan743x_csr_write(adapter, PMT_CTL, pmtctl);
2950 	lan743x_csr_write(adapter, MAC_RX, macrx);
2951 }
2952 
2953 static int lan743x_pm_suspend(struct device *dev)
2954 {
2955 	struct pci_dev *pdev = to_pci_dev(dev);
2956 	struct net_device *netdev = pci_get_drvdata(pdev);
2957 	struct lan743x_adapter *adapter = netdev_priv(netdev);
2958 	int ret;
2959 
2960 	lan743x_pcidev_shutdown(pdev);
2961 
2962 	/* clear all wakes */
2963 	lan743x_csr_write(adapter, MAC_WUCSR, 0);
2964 	lan743x_csr_write(adapter, MAC_WUCSR2, 0);
2965 	lan743x_csr_write(adapter, MAC_WK_SRC, 0xFFFFFFFF);
2966 
2967 	if (adapter->wolopts)
2968 		lan743x_pm_set_wol(adapter);
2969 
2970 	/* Host sets PME_En, put D3hot */
2971 	ret = pci_prepare_to_sleep(pdev);
2972 
2973 	return 0;
2974 }
2975 
2976 static int lan743x_pm_resume(struct device *dev)
2977 {
2978 	struct pci_dev *pdev = to_pci_dev(dev);
2979 	struct net_device *netdev = pci_get_drvdata(pdev);
2980 	struct lan743x_adapter *adapter = netdev_priv(netdev);
2981 	int ret;
2982 
2983 	pci_set_power_state(pdev, PCI_D0);
2984 	pci_restore_state(pdev);
2985 	pci_save_state(pdev);
2986 
2987 	ret = lan743x_hardware_init(adapter, pdev);
2988 	if (ret) {
2989 		netif_err(adapter, probe, adapter->netdev,
2990 			  "lan743x_hardware_init returned %d\n", ret);
2991 	}
2992 
2993 	/* open netdev when netdev is at running state while resume.
2994 	 * For instance, it is true when system wakesup after pm-suspend
2995 	 * However, it is false when system wakes up after suspend GUI menu
2996 	 */
2997 	if (netif_running(netdev))
2998 		lan743x_netdev_open(netdev);
2999 
3000 	netif_device_attach(netdev);
3001 
3002 	return 0;
3003 }
3004 
3005 static const struct dev_pm_ops lan743x_pm_ops = {
3006 	SET_SYSTEM_SLEEP_PM_OPS(lan743x_pm_suspend, lan743x_pm_resume)
3007 };
3008 #endif /* CONFIG_PM_SLEEP */
3009 
3010 static const struct pci_device_id lan743x_pcidev_tbl[] = {
3011 	{ PCI_DEVICE(PCI_VENDOR_ID_SMSC, PCI_DEVICE_ID_SMSC_LAN7430) },
3012 	{ PCI_DEVICE(PCI_VENDOR_ID_SMSC, PCI_DEVICE_ID_SMSC_LAN7431) },
3013 	{ 0, }
3014 };
3015 
3016 static struct pci_driver lan743x_pcidev_driver = {
3017 	.name     = DRIVER_NAME,
3018 	.id_table = lan743x_pcidev_tbl,
3019 	.probe    = lan743x_pcidev_probe,
3020 	.remove   = lan743x_pcidev_remove,
3021 #ifdef CONFIG_PM_SLEEP
3022 	.driver.pm = &lan743x_pm_ops,
3023 #endif
3024 	.shutdown = lan743x_pcidev_shutdown,
3025 };
3026 
3027 module_pci_driver(lan743x_pcidev_driver);
3028 
3029 MODULE_AUTHOR(DRIVER_AUTHOR);
3030 MODULE_DESCRIPTION(DRIVER_DESC);
3031 MODULE_LICENSE("GPL");
3032