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