Linux Audio

Check our new training course

Linux BSP upgrade and security maintenance

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