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