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