/drivers/net/ethernet/atheros/atl1c/atl1c_main.c

http://github.com/mirrors/linux · C · 2785 lines · 2053 code · 368 blank · 364 comment · 303 complexity · d075e108d721cacb22ccb1cc972e9fc6 MD5 · raw file

Large files are truncated click here to view the full file

  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright(c) 2008 - 2009 Atheros Corporation. All rights reserved.
  4. *
  5. * Derived from Intel e1000 driver
  6. * Copyright(c) 1999 - 2005 Intel Corporation. All rights reserved.
  7. */
  8. #include "atl1c.h"
  9. char atl1c_driver_name[] = "atl1c";
  10. /*
  11. * atl1c_pci_tbl - PCI Device ID Table
  12. *
  13. * Wildcard entries (PCI_ANY_ID) should come last
  14. * Last entry must be all 0s
  15. *
  16. * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
  17. * Class, Class Mask, private data (not used) }
  18. */
  19. static const struct pci_device_id atl1c_pci_tbl[] = {
  20. {PCI_DEVICE(PCI_VENDOR_ID_ATTANSIC, PCI_DEVICE_ID_ATTANSIC_L1C)},
  21. {PCI_DEVICE(PCI_VENDOR_ID_ATTANSIC, PCI_DEVICE_ID_ATTANSIC_L2C)},
  22. {PCI_DEVICE(PCI_VENDOR_ID_ATTANSIC, PCI_DEVICE_ID_ATHEROS_L2C_B)},
  23. {PCI_DEVICE(PCI_VENDOR_ID_ATTANSIC, PCI_DEVICE_ID_ATHEROS_L2C_B2)},
  24. {PCI_DEVICE(PCI_VENDOR_ID_ATTANSIC, PCI_DEVICE_ID_ATHEROS_L1D)},
  25. {PCI_DEVICE(PCI_VENDOR_ID_ATTANSIC, PCI_DEVICE_ID_ATHEROS_L1D_2_0)},
  26. /* required last entry */
  27. { 0 }
  28. };
  29. MODULE_DEVICE_TABLE(pci, atl1c_pci_tbl);
  30. MODULE_AUTHOR("Jie Yang");
  31. MODULE_AUTHOR("Qualcomm Atheros Inc., <nic-devel@qualcomm.com>");
  32. MODULE_DESCRIPTION("Qualcomm Atheros 100/1000M Ethernet Network Driver");
  33. MODULE_LICENSE("GPL");
  34. static int atl1c_stop_mac(struct atl1c_hw *hw);
  35. static void atl1c_disable_l0s_l1(struct atl1c_hw *hw);
  36. static void atl1c_set_aspm(struct atl1c_hw *hw, u16 link_speed);
  37. static void atl1c_start_mac(struct atl1c_adapter *adapter);
  38. static void atl1c_clean_rx_irq(struct atl1c_adapter *adapter,
  39. int *work_done, int work_to_do);
  40. static int atl1c_up(struct atl1c_adapter *adapter);
  41. static void atl1c_down(struct atl1c_adapter *adapter);
  42. static int atl1c_reset_mac(struct atl1c_hw *hw);
  43. static void atl1c_reset_dma_ring(struct atl1c_adapter *adapter);
  44. static int atl1c_configure(struct atl1c_adapter *adapter);
  45. static int atl1c_alloc_rx_buffer(struct atl1c_adapter *adapter);
  46. static const u32 atl1c_default_msg = NETIF_MSG_DRV | NETIF_MSG_PROBE |
  47. NETIF_MSG_LINK | NETIF_MSG_TIMER | NETIF_MSG_IFDOWN | NETIF_MSG_IFUP;
  48. static void atl1c_pcie_patch(struct atl1c_hw *hw)
  49. {
  50. u32 mst_data, data;
  51. /* pclk sel could switch to 25M */
  52. AT_READ_REG(hw, REG_MASTER_CTRL, &mst_data);
  53. mst_data &= ~MASTER_CTRL_CLK_SEL_DIS;
  54. AT_WRITE_REG(hw, REG_MASTER_CTRL, mst_data);
  55. /* WoL/PCIE related settings */
  56. if (hw->nic_type == athr_l1c || hw->nic_type == athr_l2c) {
  57. AT_READ_REG(hw, REG_PCIE_PHYMISC, &data);
  58. data |= PCIE_PHYMISC_FORCE_RCV_DET;
  59. AT_WRITE_REG(hw, REG_PCIE_PHYMISC, data);
  60. } else { /* new dev set bit5 of MASTER */
  61. if (!(mst_data & MASTER_CTRL_WAKEN_25M))
  62. AT_WRITE_REG(hw, REG_MASTER_CTRL,
  63. mst_data | MASTER_CTRL_WAKEN_25M);
  64. }
  65. /* aspm/PCIE setting only for l2cb 1.0 */
  66. if (hw->nic_type == athr_l2c_b && hw->revision_id == L2CB_V10) {
  67. AT_READ_REG(hw, REG_PCIE_PHYMISC2, &data);
  68. data = FIELD_SETX(data, PCIE_PHYMISC2_CDR_BW,
  69. L2CB1_PCIE_PHYMISC2_CDR_BW);
  70. data = FIELD_SETX(data, PCIE_PHYMISC2_L0S_TH,
  71. L2CB1_PCIE_PHYMISC2_L0S_TH);
  72. AT_WRITE_REG(hw, REG_PCIE_PHYMISC2, data);
  73. /* extend L1 sync timer */
  74. AT_READ_REG(hw, REG_LINK_CTRL, &data);
  75. data |= LINK_CTRL_EXT_SYNC;
  76. AT_WRITE_REG(hw, REG_LINK_CTRL, data);
  77. }
  78. /* l2cb 1.x & l1d 1.x */
  79. if (hw->nic_type == athr_l2c_b || hw->nic_type == athr_l1d) {
  80. AT_READ_REG(hw, REG_PM_CTRL, &data);
  81. data |= PM_CTRL_L0S_BUFSRX_EN;
  82. AT_WRITE_REG(hw, REG_PM_CTRL, data);
  83. /* clear vendor msg */
  84. AT_READ_REG(hw, REG_DMA_DBG, &data);
  85. AT_WRITE_REG(hw, REG_DMA_DBG, data & ~DMA_DBG_VENDOR_MSG);
  86. }
  87. }
  88. /* FIXME: no need any more ? */
  89. /*
  90. * atl1c_init_pcie - init PCIE module
  91. */
  92. static void atl1c_reset_pcie(struct atl1c_hw *hw, u32 flag)
  93. {
  94. u32 data;
  95. u32 pci_cmd;
  96. struct pci_dev *pdev = hw->adapter->pdev;
  97. int pos;
  98. AT_READ_REG(hw, PCI_COMMAND, &pci_cmd);
  99. pci_cmd &= ~PCI_COMMAND_INTX_DISABLE;
  100. pci_cmd |= (PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER |
  101. PCI_COMMAND_IO);
  102. AT_WRITE_REG(hw, PCI_COMMAND, pci_cmd);
  103. /*
  104. * Clear any PowerSaveing Settings
  105. */
  106. pci_enable_wake(pdev, PCI_D3hot, 0);
  107. pci_enable_wake(pdev, PCI_D3cold, 0);
  108. /* wol sts read-clear */
  109. AT_READ_REG(hw, REG_WOL_CTRL, &data);
  110. AT_WRITE_REG(hw, REG_WOL_CTRL, 0);
  111. /*
  112. * Mask some pcie error bits
  113. */
  114. pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_ERR);
  115. if (pos) {
  116. pci_read_config_dword(pdev, pos + PCI_ERR_UNCOR_SEVER, &data);
  117. data &= ~(PCI_ERR_UNC_DLP | PCI_ERR_UNC_FCP);
  118. pci_write_config_dword(pdev, pos + PCI_ERR_UNCOR_SEVER, data);
  119. }
  120. /* clear error status */
  121. pcie_capability_write_word(pdev, PCI_EXP_DEVSTA,
  122. PCI_EXP_DEVSTA_NFED |
  123. PCI_EXP_DEVSTA_FED |
  124. PCI_EXP_DEVSTA_CED |
  125. PCI_EXP_DEVSTA_URD);
  126. AT_READ_REG(hw, REG_LTSSM_ID_CTRL, &data);
  127. data &= ~LTSSM_ID_EN_WRO;
  128. AT_WRITE_REG(hw, REG_LTSSM_ID_CTRL, data);
  129. atl1c_pcie_patch(hw);
  130. if (flag & ATL1C_PCIE_L0S_L1_DISABLE)
  131. atl1c_disable_l0s_l1(hw);
  132. msleep(5);
  133. }
  134. /**
  135. * atl1c_irq_enable - Enable default interrupt generation settings
  136. * @adapter: board private structure
  137. */
  138. static inline void atl1c_irq_enable(struct atl1c_adapter *adapter)
  139. {
  140. if (likely(atomic_dec_and_test(&adapter->irq_sem))) {
  141. AT_WRITE_REG(&adapter->hw, REG_ISR, 0x7FFFFFFF);
  142. AT_WRITE_REG(&adapter->hw, REG_IMR, adapter->hw.intr_mask);
  143. AT_WRITE_FLUSH(&adapter->hw);
  144. }
  145. }
  146. /**
  147. * atl1c_irq_disable - Mask off interrupt generation on the NIC
  148. * @adapter: board private structure
  149. */
  150. static inline void atl1c_irq_disable(struct atl1c_adapter *adapter)
  151. {
  152. atomic_inc(&adapter->irq_sem);
  153. AT_WRITE_REG(&adapter->hw, REG_IMR, 0);
  154. AT_WRITE_REG(&adapter->hw, REG_ISR, ISR_DIS_INT);
  155. AT_WRITE_FLUSH(&adapter->hw);
  156. synchronize_irq(adapter->pdev->irq);
  157. }
  158. /**
  159. * atl1c_irq_reset - reset interrupt confiure on the NIC
  160. * @adapter: board private structure
  161. */
  162. static inline void atl1c_irq_reset(struct atl1c_adapter *adapter)
  163. {
  164. atomic_set(&adapter->irq_sem, 1);
  165. atl1c_irq_enable(adapter);
  166. }
  167. /*
  168. * atl1c_wait_until_idle - wait up to AT_HW_MAX_IDLE_DELAY reads
  169. * of the idle status register until the device is actually idle
  170. */
  171. static u32 atl1c_wait_until_idle(struct atl1c_hw *hw, u32 modu_ctrl)
  172. {
  173. int timeout;
  174. u32 data;
  175. for (timeout = 0; timeout < AT_HW_MAX_IDLE_DELAY; timeout++) {
  176. AT_READ_REG(hw, REG_IDLE_STATUS, &data);
  177. if ((data & modu_ctrl) == 0)
  178. return 0;
  179. msleep(1);
  180. }
  181. return data;
  182. }
  183. /**
  184. * atl1c_phy_config - Timer Call-back
  185. * @data: pointer to netdev cast into an unsigned long
  186. */
  187. static void atl1c_phy_config(struct timer_list *t)
  188. {
  189. struct atl1c_adapter *adapter = from_timer(adapter, t,
  190. phy_config_timer);
  191. struct atl1c_hw *hw = &adapter->hw;
  192. unsigned long flags;
  193. spin_lock_irqsave(&adapter->mdio_lock, flags);
  194. atl1c_restart_autoneg(hw);
  195. spin_unlock_irqrestore(&adapter->mdio_lock, flags);
  196. }
  197. void atl1c_reinit_locked(struct atl1c_adapter *adapter)
  198. {
  199. WARN_ON(in_interrupt());
  200. atl1c_down(adapter);
  201. atl1c_up(adapter);
  202. clear_bit(__AT_RESETTING, &adapter->flags);
  203. }
  204. static void atl1c_check_link_status(struct atl1c_adapter *adapter)
  205. {
  206. struct atl1c_hw *hw = &adapter->hw;
  207. struct net_device *netdev = adapter->netdev;
  208. struct pci_dev *pdev = adapter->pdev;
  209. int err;
  210. unsigned long flags;
  211. u16 speed, duplex, phy_data;
  212. spin_lock_irqsave(&adapter->mdio_lock, flags);
  213. /* MII_BMSR must read twise */
  214. atl1c_read_phy_reg(hw, MII_BMSR, &phy_data);
  215. atl1c_read_phy_reg(hw, MII_BMSR, &phy_data);
  216. spin_unlock_irqrestore(&adapter->mdio_lock, flags);
  217. if ((phy_data & BMSR_LSTATUS) == 0) {
  218. /* link down */
  219. netif_carrier_off(netdev);
  220. hw->hibernate = true;
  221. if (atl1c_reset_mac(hw) != 0)
  222. if (netif_msg_hw(adapter))
  223. dev_warn(&pdev->dev, "reset mac failed\n");
  224. atl1c_set_aspm(hw, SPEED_0);
  225. atl1c_post_phy_linkchg(hw, SPEED_0);
  226. atl1c_reset_dma_ring(adapter);
  227. atl1c_configure(adapter);
  228. } else {
  229. /* Link Up */
  230. hw->hibernate = false;
  231. spin_lock_irqsave(&adapter->mdio_lock, flags);
  232. err = atl1c_get_speed_and_duplex(hw, &speed, &duplex);
  233. spin_unlock_irqrestore(&adapter->mdio_lock, flags);
  234. if (unlikely(err))
  235. return;
  236. /* link result is our setting */
  237. if (adapter->link_speed != speed ||
  238. adapter->link_duplex != duplex) {
  239. adapter->link_speed = speed;
  240. adapter->link_duplex = duplex;
  241. atl1c_set_aspm(hw, speed);
  242. atl1c_post_phy_linkchg(hw, speed);
  243. atl1c_start_mac(adapter);
  244. if (netif_msg_link(adapter))
  245. dev_info(&pdev->dev,
  246. "%s: %s NIC Link is Up<%d Mbps %s>\n",
  247. atl1c_driver_name, netdev->name,
  248. adapter->link_speed,
  249. adapter->link_duplex == FULL_DUPLEX ?
  250. "Full Duplex" : "Half Duplex");
  251. }
  252. if (!netif_carrier_ok(netdev))
  253. netif_carrier_on(netdev);
  254. }
  255. }
  256. static void atl1c_link_chg_event(struct atl1c_adapter *adapter)
  257. {
  258. struct net_device *netdev = adapter->netdev;
  259. struct pci_dev *pdev = adapter->pdev;
  260. u16 phy_data;
  261. u16 link_up;
  262. spin_lock(&adapter->mdio_lock);
  263. atl1c_read_phy_reg(&adapter->hw, MII_BMSR, &phy_data);
  264. atl1c_read_phy_reg(&adapter->hw, MII_BMSR, &phy_data);
  265. spin_unlock(&adapter->mdio_lock);
  266. link_up = phy_data & BMSR_LSTATUS;
  267. /* notify upper layer link down ASAP */
  268. if (!link_up) {
  269. if (netif_carrier_ok(netdev)) {
  270. /* old link state: Up */
  271. netif_carrier_off(netdev);
  272. if (netif_msg_link(adapter))
  273. dev_info(&pdev->dev,
  274. "%s: %s NIC Link is Down\n",
  275. atl1c_driver_name, netdev->name);
  276. adapter->link_speed = SPEED_0;
  277. }
  278. }
  279. set_bit(ATL1C_WORK_EVENT_LINK_CHANGE, &adapter->work_event);
  280. schedule_work(&adapter->common_task);
  281. }
  282. static void atl1c_common_task(struct work_struct *work)
  283. {
  284. struct atl1c_adapter *adapter;
  285. struct net_device *netdev;
  286. adapter = container_of(work, struct atl1c_adapter, common_task);
  287. netdev = adapter->netdev;
  288. if (test_bit(__AT_DOWN, &adapter->flags))
  289. return;
  290. if (test_and_clear_bit(ATL1C_WORK_EVENT_RESET, &adapter->work_event)) {
  291. netif_device_detach(netdev);
  292. atl1c_down(adapter);
  293. atl1c_up(adapter);
  294. netif_device_attach(netdev);
  295. }
  296. if (test_and_clear_bit(ATL1C_WORK_EVENT_LINK_CHANGE,
  297. &adapter->work_event)) {
  298. atl1c_irq_disable(adapter);
  299. atl1c_check_link_status(adapter);
  300. atl1c_irq_enable(adapter);
  301. }
  302. }
  303. static void atl1c_del_timer(struct atl1c_adapter *adapter)
  304. {
  305. del_timer_sync(&adapter->phy_config_timer);
  306. }
  307. /**
  308. * atl1c_tx_timeout - Respond to a Tx Hang
  309. * @netdev: network interface device structure
  310. */
  311. static void atl1c_tx_timeout(struct net_device *netdev, unsigned int txqueue)
  312. {
  313. struct atl1c_adapter *adapter = netdev_priv(netdev);
  314. /* Do the reset outside of interrupt context */
  315. set_bit(ATL1C_WORK_EVENT_RESET, &adapter->work_event);
  316. schedule_work(&adapter->common_task);
  317. }
  318. /**
  319. * atl1c_set_multi - Multicast and Promiscuous mode set
  320. * @netdev: network interface device structure
  321. *
  322. * The set_multi entry point is called whenever the multicast address
  323. * list or the network interface flags are updated. This routine is
  324. * responsible for configuring the hardware for proper multicast,
  325. * promiscuous mode, and all-multi behavior.
  326. */
  327. static void atl1c_set_multi(struct net_device *netdev)
  328. {
  329. struct atl1c_adapter *adapter = netdev_priv(netdev);
  330. struct atl1c_hw *hw = &adapter->hw;
  331. struct netdev_hw_addr *ha;
  332. u32 mac_ctrl_data;
  333. u32 hash_value;
  334. /* Check for Promiscuous and All Multicast modes */
  335. AT_READ_REG(hw, REG_MAC_CTRL, &mac_ctrl_data);
  336. if (netdev->flags & IFF_PROMISC) {
  337. mac_ctrl_data |= MAC_CTRL_PROMIS_EN;
  338. } else if (netdev->flags & IFF_ALLMULTI) {
  339. mac_ctrl_data |= MAC_CTRL_MC_ALL_EN;
  340. mac_ctrl_data &= ~MAC_CTRL_PROMIS_EN;
  341. } else {
  342. mac_ctrl_data &= ~(MAC_CTRL_PROMIS_EN | MAC_CTRL_MC_ALL_EN);
  343. }
  344. AT_WRITE_REG(hw, REG_MAC_CTRL, mac_ctrl_data);
  345. /* clear the old settings from the multicast hash table */
  346. AT_WRITE_REG(hw, REG_RX_HASH_TABLE, 0);
  347. AT_WRITE_REG_ARRAY(hw, REG_RX_HASH_TABLE, 1, 0);
  348. /* comoute mc addresses' hash value ,and put it into hash table */
  349. netdev_for_each_mc_addr(ha, netdev) {
  350. hash_value = atl1c_hash_mc_addr(hw, ha->addr);
  351. atl1c_hash_set(hw, hash_value);
  352. }
  353. }
  354. static void __atl1c_vlan_mode(netdev_features_t features, u32 *mac_ctrl_data)
  355. {
  356. if (features & NETIF_F_HW_VLAN_CTAG_RX) {
  357. /* enable VLAN tag insert/strip */
  358. *mac_ctrl_data |= MAC_CTRL_RMV_VLAN;
  359. } else {
  360. /* disable VLAN tag insert/strip */
  361. *mac_ctrl_data &= ~MAC_CTRL_RMV_VLAN;
  362. }
  363. }
  364. static void atl1c_vlan_mode(struct net_device *netdev,
  365. netdev_features_t features)
  366. {
  367. struct atl1c_adapter *adapter = netdev_priv(netdev);
  368. struct pci_dev *pdev = adapter->pdev;
  369. u32 mac_ctrl_data = 0;
  370. if (netif_msg_pktdata(adapter))
  371. dev_dbg(&pdev->dev, "atl1c_vlan_mode\n");
  372. atl1c_irq_disable(adapter);
  373. AT_READ_REG(&adapter->hw, REG_MAC_CTRL, &mac_ctrl_data);
  374. __atl1c_vlan_mode(features, &mac_ctrl_data);
  375. AT_WRITE_REG(&adapter->hw, REG_MAC_CTRL, mac_ctrl_data);
  376. atl1c_irq_enable(adapter);
  377. }
  378. static void atl1c_restore_vlan(struct atl1c_adapter *adapter)
  379. {
  380. struct pci_dev *pdev = adapter->pdev;
  381. if (netif_msg_pktdata(adapter))
  382. dev_dbg(&pdev->dev, "atl1c_restore_vlan\n");
  383. atl1c_vlan_mode(adapter->netdev, adapter->netdev->features);
  384. }
  385. /**
  386. * atl1c_set_mac - Change the Ethernet Address of the NIC
  387. * @netdev: network interface device structure
  388. * @p: pointer to an address structure
  389. *
  390. * Returns 0 on success, negative on failure
  391. */
  392. static int atl1c_set_mac_addr(struct net_device *netdev, void *p)
  393. {
  394. struct atl1c_adapter *adapter = netdev_priv(netdev);
  395. struct sockaddr *addr = p;
  396. if (!is_valid_ether_addr(addr->sa_data))
  397. return -EADDRNOTAVAIL;
  398. if (netif_running(netdev))
  399. return -EBUSY;
  400. memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
  401. memcpy(adapter->hw.mac_addr, addr->sa_data, netdev->addr_len);
  402. atl1c_hw_set_mac_addr(&adapter->hw, adapter->hw.mac_addr);
  403. return 0;
  404. }
  405. static void atl1c_set_rxbufsize(struct atl1c_adapter *adapter,
  406. struct net_device *dev)
  407. {
  408. unsigned int head_size;
  409. int mtu = dev->mtu;
  410. adapter->rx_buffer_len = mtu > AT_RX_BUF_SIZE ?
  411. roundup(mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN, 8) : AT_RX_BUF_SIZE;
  412. head_size = SKB_DATA_ALIGN(adapter->rx_buffer_len + NET_SKB_PAD) +
  413. SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
  414. adapter->rx_frag_size = roundup_pow_of_two(head_size);
  415. }
  416. static netdev_features_t atl1c_fix_features(struct net_device *netdev,
  417. netdev_features_t features)
  418. {
  419. /*
  420. * Since there is no support for separate rx/tx vlan accel
  421. * enable/disable make sure tx flag is always in same state as rx.
  422. */
  423. if (features & NETIF_F_HW_VLAN_CTAG_RX)
  424. features |= NETIF_F_HW_VLAN_CTAG_TX;
  425. else
  426. features &= ~NETIF_F_HW_VLAN_CTAG_TX;
  427. if (netdev->mtu > MAX_TSO_FRAME_SIZE)
  428. features &= ~(NETIF_F_TSO | NETIF_F_TSO6);
  429. return features;
  430. }
  431. static int atl1c_set_features(struct net_device *netdev,
  432. netdev_features_t features)
  433. {
  434. netdev_features_t changed = netdev->features ^ features;
  435. if (changed & NETIF_F_HW_VLAN_CTAG_RX)
  436. atl1c_vlan_mode(netdev, features);
  437. return 0;
  438. }
  439. static void atl1c_set_max_mtu(struct net_device *netdev)
  440. {
  441. struct atl1c_adapter *adapter = netdev_priv(netdev);
  442. struct atl1c_hw *hw = &adapter->hw;
  443. switch (hw->nic_type) {
  444. /* These (GbE) devices support jumbo packets, max_mtu 6122 */
  445. case athr_l1c:
  446. case athr_l1d:
  447. case athr_l1d_2:
  448. netdev->max_mtu = MAX_JUMBO_FRAME_SIZE -
  449. (ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN);
  450. break;
  451. /* The 10/100 devices don't support jumbo packets, max_mtu 1500 */
  452. default:
  453. netdev->max_mtu = ETH_DATA_LEN;
  454. break;
  455. }
  456. }
  457. /**
  458. * atl1c_change_mtu - Change the Maximum Transfer Unit
  459. * @netdev: network interface device structure
  460. * @new_mtu: new value for maximum frame size
  461. *
  462. * Returns 0 on success, negative on failure
  463. */
  464. static int atl1c_change_mtu(struct net_device *netdev, int new_mtu)
  465. {
  466. struct atl1c_adapter *adapter = netdev_priv(netdev);
  467. /* set MTU */
  468. if (netif_running(netdev)) {
  469. while (test_and_set_bit(__AT_RESETTING, &adapter->flags))
  470. msleep(1);
  471. netdev->mtu = new_mtu;
  472. adapter->hw.max_frame_size = new_mtu;
  473. atl1c_set_rxbufsize(adapter, netdev);
  474. atl1c_down(adapter);
  475. netdev_update_features(netdev);
  476. atl1c_up(adapter);
  477. clear_bit(__AT_RESETTING, &adapter->flags);
  478. }
  479. return 0;
  480. }
  481. /*
  482. * caller should hold mdio_lock
  483. */
  484. static int atl1c_mdio_read(struct net_device *netdev, int phy_id, int reg_num)
  485. {
  486. struct atl1c_adapter *adapter = netdev_priv(netdev);
  487. u16 result;
  488. atl1c_read_phy_reg(&adapter->hw, reg_num, &result);
  489. return result;
  490. }
  491. static void atl1c_mdio_write(struct net_device *netdev, int phy_id,
  492. int reg_num, int val)
  493. {
  494. struct atl1c_adapter *adapter = netdev_priv(netdev);
  495. atl1c_write_phy_reg(&adapter->hw, reg_num, val);
  496. }
  497. static int atl1c_mii_ioctl(struct net_device *netdev,
  498. struct ifreq *ifr, int cmd)
  499. {
  500. struct atl1c_adapter *adapter = netdev_priv(netdev);
  501. struct pci_dev *pdev = adapter->pdev;
  502. struct mii_ioctl_data *data = if_mii(ifr);
  503. unsigned long flags;
  504. int retval = 0;
  505. if (!netif_running(netdev))
  506. return -EINVAL;
  507. spin_lock_irqsave(&adapter->mdio_lock, flags);
  508. switch (cmd) {
  509. case SIOCGMIIPHY:
  510. data->phy_id = 0;
  511. break;
  512. case SIOCGMIIREG:
  513. if (atl1c_read_phy_reg(&adapter->hw, data->reg_num & 0x1F,
  514. &data->val_out)) {
  515. retval = -EIO;
  516. goto out;
  517. }
  518. break;
  519. case SIOCSMIIREG:
  520. if (data->reg_num & ~(0x1F)) {
  521. retval = -EFAULT;
  522. goto out;
  523. }
  524. dev_dbg(&pdev->dev, "<atl1c_mii_ioctl> write %x %x",
  525. data->reg_num, data->val_in);
  526. if (atl1c_write_phy_reg(&adapter->hw,
  527. data->reg_num, data->val_in)) {
  528. retval = -EIO;
  529. goto out;
  530. }
  531. break;
  532. default:
  533. retval = -EOPNOTSUPP;
  534. break;
  535. }
  536. out:
  537. spin_unlock_irqrestore(&adapter->mdio_lock, flags);
  538. return retval;
  539. }
  540. static int atl1c_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
  541. {
  542. switch (cmd) {
  543. case SIOCGMIIPHY:
  544. case SIOCGMIIREG:
  545. case SIOCSMIIREG:
  546. return atl1c_mii_ioctl(netdev, ifr, cmd);
  547. default:
  548. return -EOPNOTSUPP;
  549. }
  550. }
  551. /**
  552. * atl1c_alloc_queues - Allocate memory for all rings
  553. * @adapter: board private structure to initialize
  554. *
  555. */
  556. static int atl1c_alloc_queues(struct atl1c_adapter *adapter)
  557. {
  558. return 0;
  559. }
  560. static void atl1c_set_mac_type(struct atl1c_hw *hw)
  561. {
  562. switch (hw->device_id) {
  563. case PCI_DEVICE_ID_ATTANSIC_L2C:
  564. hw->nic_type = athr_l2c;
  565. break;
  566. case PCI_DEVICE_ID_ATTANSIC_L1C:
  567. hw->nic_type = athr_l1c;
  568. break;
  569. case PCI_DEVICE_ID_ATHEROS_L2C_B:
  570. hw->nic_type = athr_l2c_b;
  571. break;
  572. case PCI_DEVICE_ID_ATHEROS_L2C_B2:
  573. hw->nic_type = athr_l2c_b2;
  574. break;
  575. case PCI_DEVICE_ID_ATHEROS_L1D:
  576. hw->nic_type = athr_l1d;
  577. break;
  578. case PCI_DEVICE_ID_ATHEROS_L1D_2_0:
  579. hw->nic_type = athr_l1d_2;
  580. break;
  581. default:
  582. break;
  583. }
  584. }
  585. static int atl1c_setup_mac_funcs(struct atl1c_hw *hw)
  586. {
  587. u32 link_ctrl_data;
  588. atl1c_set_mac_type(hw);
  589. AT_READ_REG(hw, REG_LINK_CTRL, &link_ctrl_data);
  590. hw->ctrl_flags = ATL1C_INTR_MODRT_ENABLE |
  591. ATL1C_TXQ_MODE_ENHANCE;
  592. hw->ctrl_flags |= ATL1C_ASPM_L0S_SUPPORT |
  593. ATL1C_ASPM_L1_SUPPORT;
  594. hw->ctrl_flags |= ATL1C_ASPM_CTRL_MON;
  595. if (hw->nic_type == athr_l1c ||
  596. hw->nic_type == athr_l1d ||
  597. hw->nic_type == athr_l1d_2)
  598. hw->link_cap_flags |= ATL1C_LINK_CAP_1000M;
  599. return 0;
  600. }
  601. struct atl1c_platform_patch {
  602. u16 pci_did;
  603. u8 pci_revid;
  604. u16 subsystem_vid;
  605. u16 subsystem_did;
  606. u32 patch_flag;
  607. #define ATL1C_LINK_PATCH 0x1
  608. };
  609. static const struct atl1c_platform_patch plats[] = {
  610. {0x2060, 0xC1, 0x1019, 0x8152, 0x1},
  611. {0x2060, 0xC1, 0x1019, 0x2060, 0x1},
  612. {0x2060, 0xC1, 0x1019, 0xE000, 0x1},
  613. {0x2062, 0xC0, 0x1019, 0x8152, 0x1},
  614. {0x2062, 0xC0, 0x1019, 0x2062, 0x1},
  615. {0x2062, 0xC0, 0x1458, 0xE000, 0x1},
  616. {0x2062, 0xC1, 0x1019, 0x8152, 0x1},
  617. {0x2062, 0xC1, 0x1019, 0x2062, 0x1},
  618. {0x2062, 0xC1, 0x1458, 0xE000, 0x1},
  619. {0x2062, 0xC1, 0x1565, 0x2802, 0x1},
  620. {0x2062, 0xC1, 0x1565, 0x2801, 0x1},
  621. {0x1073, 0xC0, 0x1019, 0x8151, 0x1},
  622. {0x1073, 0xC0, 0x1019, 0x1073, 0x1},
  623. {0x1073, 0xC0, 0x1458, 0xE000, 0x1},
  624. {0x1083, 0xC0, 0x1458, 0xE000, 0x1},
  625. {0x1083, 0xC0, 0x1019, 0x8151, 0x1},
  626. {0x1083, 0xC0, 0x1019, 0x1083, 0x1},
  627. {0x1083, 0xC0, 0x1462, 0x7680, 0x1},
  628. {0x1083, 0xC0, 0x1565, 0x2803, 0x1},
  629. {0},
  630. };
  631. static void atl1c_patch_assign(struct atl1c_hw *hw)
  632. {
  633. struct pci_dev *pdev = hw->adapter->pdev;
  634. u32 misc_ctrl;
  635. int i = 0;
  636. hw->msi_lnkpatch = false;
  637. while (plats[i].pci_did != 0) {
  638. if (plats[i].pci_did == hw->device_id &&
  639. plats[i].pci_revid == hw->revision_id &&
  640. plats[i].subsystem_vid == hw->subsystem_vendor_id &&
  641. plats[i].subsystem_did == hw->subsystem_id) {
  642. if (plats[i].patch_flag & ATL1C_LINK_PATCH)
  643. hw->msi_lnkpatch = true;
  644. }
  645. i++;
  646. }
  647. if (hw->device_id == PCI_DEVICE_ID_ATHEROS_L2C_B2 &&
  648. hw->revision_id == L2CB_V21) {
  649. /* config access mode */
  650. pci_write_config_dword(pdev, REG_PCIE_IND_ACC_ADDR,
  651. REG_PCIE_DEV_MISC_CTRL);
  652. pci_read_config_dword(pdev, REG_PCIE_IND_ACC_DATA, &misc_ctrl);
  653. misc_ctrl &= ~0x100;
  654. pci_write_config_dword(pdev, REG_PCIE_IND_ACC_ADDR,
  655. REG_PCIE_DEV_MISC_CTRL);
  656. pci_write_config_dword(pdev, REG_PCIE_IND_ACC_DATA, misc_ctrl);
  657. }
  658. }
  659. /**
  660. * atl1c_sw_init - Initialize general software structures (struct atl1c_adapter)
  661. * @adapter: board private structure to initialize
  662. *
  663. * atl1c_sw_init initializes the Adapter private data structure.
  664. * Fields are initialized based on PCI device information and
  665. * OS network device settings (MTU size).
  666. */
  667. static int atl1c_sw_init(struct atl1c_adapter *adapter)
  668. {
  669. struct atl1c_hw *hw = &adapter->hw;
  670. struct pci_dev *pdev = adapter->pdev;
  671. u32 revision;
  672. adapter->wol = 0;
  673. device_set_wakeup_enable(&pdev->dev, false);
  674. adapter->link_speed = SPEED_0;
  675. adapter->link_duplex = FULL_DUPLEX;
  676. adapter->tpd_ring[0].count = 1024;
  677. adapter->rfd_ring.count = 512;
  678. hw->vendor_id = pdev->vendor;
  679. hw->device_id = pdev->device;
  680. hw->subsystem_vendor_id = pdev->subsystem_vendor;
  681. hw->subsystem_id = pdev->subsystem_device;
  682. pci_read_config_dword(pdev, PCI_CLASS_REVISION, &revision);
  683. hw->revision_id = revision & 0xFF;
  684. /* before link up, we assume hibernate is true */
  685. hw->hibernate = true;
  686. hw->media_type = MEDIA_TYPE_AUTO_SENSOR;
  687. if (atl1c_setup_mac_funcs(hw) != 0) {
  688. dev_err(&pdev->dev, "set mac function pointers failed\n");
  689. return -1;
  690. }
  691. atl1c_patch_assign(hw);
  692. hw->intr_mask = IMR_NORMAL_MASK;
  693. hw->phy_configured = false;
  694. hw->preamble_len = 7;
  695. hw->max_frame_size = adapter->netdev->mtu;
  696. hw->autoneg_advertised = ADVERTISED_Autoneg;
  697. hw->indirect_tab = 0xE4E4E4E4;
  698. hw->base_cpu = 0;
  699. hw->ict = 50000; /* 100ms */
  700. hw->smb_timer = 200000; /* 400ms */
  701. hw->rx_imt = 200;
  702. hw->tx_imt = 1000;
  703. hw->tpd_burst = 5;
  704. hw->rfd_burst = 8;
  705. hw->dma_order = atl1c_dma_ord_out;
  706. hw->dmar_block = atl1c_dma_req_1024;
  707. if (atl1c_alloc_queues(adapter)) {
  708. dev_err(&pdev->dev, "Unable to allocate memory for queues\n");
  709. return -ENOMEM;
  710. }
  711. /* TODO */
  712. atl1c_set_rxbufsize(adapter, adapter->netdev);
  713. atomic_set(&adapter->irq_sem, 1);
  714. spin_lock_init(&adapter->mdio_lock);
  715. set_bit(__AT_DOWN, &adapter->flags);
  716. return 0;
  717. }
  718. static inline void atl1c_clean_buffer(struct pci_dev *pdev,
  719. struct atl1c_buffer *buffer_info)
  720. {
  721. u16 pci_driection;
  722. if (buffer_info->flags & ATL1C_BUFFER_FREE)
  723. return;
  724. if (buffer_info->dma) {
  725. if (buffer_info->flags & ATL1C_PCIMAP_FROMDEVICE)
  726. pci_driection = PCI_DMA_FROMDEVICE;
  727. else
  728. pci_driection = PCI_DMA_TODEVICE;
  729. if (buffer_info->flags & ATL1C_PCIMAP_SINGLE)
  730. pci_unmap_single(pdev, buffer_info->dma,
  731. buffer_info->length, pci_driection);
  732. else if (buffer_info->flags & ATL1C_PCIMAP_PAGE)
  733. pci_unmap_page(pdev, buffer_info->dma,
  734. buffer_info->length, pci_driection);
  735. }
  736. if (buffer_info->skb)
  737. dev_consume_skb_any(buffer_info->skb);
  738. buffer_info->dma = 0;
  739. buffer_info->skb = NULL;
  740. ATL1C_SET_BUFFER_STATE(buffer_info, ATL1C_BUFFER_FREE);
  741. }
  742. /**
  743. * atl1c_clean_tx_ring - Free Tx-skb
  744. * @adapter: board private structure
  745. */
  746. static void atl1c_clean_tx_ring(struct atl1c_adapter *adapter,
  747. enum atl1c_trans_queue type)
  748. {
  749. struct atl1c_tpd_ring *tpd_ring = &adapter->tpd_ring[type];
  750. struct atl1c_buffer *buffer_info;
  751. struct pci_dev *pdev = adapter->pdev;
  752. u16 index, ring_count;
  753. ring_count = tpd_ring->count;
  754. for (index = 0; index < ring_count; index++) {
  755. buffer_info = &tpd_ring->buffer_info[index];
  756. atl1c_clean_buffer(pdev, buffer_info);
  757. }
  758. netdev_reset_queue(adapter->netdev);
  759. /* Zero out Tx-buffers */
  760. memset(tpd_ring->desc, 0, sizeof(struct atl1c_tpd_desc) *
  761. ring_count);
  762. atomic_set(&tpd_ring->next_to_clean, 0);
  763. tpd_ring->next_to_use = 0;
  764. }
  765. /**
  766. * atl1c_clean_rx_ring - Free rx-reservation skbs
  767. * @adapter: board private structure
  768. */
  769. static void atl1c_clean_rx_ring(struct atl1c_adapter *adapter)
  770. {
  771. struct atl1c_rfd_ring *rfd_ring = &adapter->rfd_ring;
  772. struct atl1c_rrd_ring *rrd_ring = &adapter->rrd_ring;
  773. struct atl1c_buffer *buffer_info;
  774. struct pci_dev *pdev = adapter->pdev;
  775. int j;
  776. for (j = 0; j < rfd_ring->count; j++) {
  777. buffer_info = &rfd_ring->buffer_info[j];
  778. atl1c_clean_buffer(pdev, buffer_info);
  779. }
  780. /* zero out the descriptor ring */
  781. memset(rfd_ring->desc, 0, rfd_ring->size);
  782. rfd_ring->next_to_clean = 0;
  783. rfd_ring->next_to_use = 0;
  784. rrd_ring->next_to_use = 0;
  785. rrd_ring->next_to_clean = 0;
  786. }
  787. /*
  788. * Read / Write Ptr Initialize:
  789. */
  790. static void atl1c_init_ring_ptrs(struct atl1c_adapter *adapter)
  791. {
  792. struct atl1c_tpd_ring *tpd_ring = adapter->tpd_ring;
  793. struct atl1c_rfd_ring *rfd_ring = &adapter->rfd_ring;
  794. struct atl1c_rrd_ring *rrd_ring = &adapter->rrd_ring;
  795. struct atl1c_buffer *buffer_info;
  796. int i, j;
  797. for (i = 0; i < AT_MAX_TRANSMIT_QUEUE; i++) {
  798. tpd_ring[i].next_to_use = 0;
  799. atomic_set(&tpd_ring[i].next_to_clean, 0);
  800. buffer_info = tpd_ring[i].buffer_info;
  801. for (j = 0; j < tpd_ring->count; j++)
  802. ATL1C_SET_BUFFER_STATE(&buffer_info[i],
  803. ATL1C_BUFFER_FREE);
  804. }
  805. rfd_ring->next_to_use = 0;
  806. rfd_ring->next_to_clean = 0;
  807. rrd_ring->next_to_use = 0;
  808. rrd_ring->next_to_clean = 0;
  809. for (j = 0; j < rfd_ring->count; j++) {
  810. buffer_info = &rfd_ring->buffer_info[j];
  811. ATL1C_SET_BUFFER_STATE(buffer_info, ATL1C_BUFFER_FREE);
  812. }
  813. }
  814. /**
  815. * atl1c_free_ring_resources - Free Tx / RX descriptor Resources
  816. * @adapter: board private structure
  817. *
  818. * Free all transmit software resources
  819. */
  820. static void atl1c_free_ring_resources(struct atl1c_adapter *adapter)
  821. {
  822. struct pci_dev *pdev = adapter->pdev;
  823. pci_free_consistent(pdev, adapter->ring_header.size,
  824. adapter->ring_header.desc,
  825. adapter->ring_header.dma);
  826. adapter->ring_header.desc = NULL;
  827. /* Note: just free tdp_ring.buffer_info,
  828. * it contain rfd_ring.buffer_info, do not double free */
  829. if (adapter->tpd_ring[0].buffer_info) {
  830. kfree(adapter->tpd_ring[0].buffer_info);
  831. adapter->tpd_ring[0].buffer_info = NULL;
  832. }
  833. if (adapter->rx_page) {
  834. put_page(adapter->rx_page);
  835. adapter->rx_page = NULL;
  836. }
  837. }
  838. /**
  839. * atl1c_setup_mem_resources - allocate Tx / RX descriptor resources
  840. * @adapter: board private structure
  841. *
  842. * Return 0 on success, negative on failure
  843. */
  844. static int atl1c_setup_ring_resources(struct atl1c_adapter *adapter)
  845. {
  846. struct pci_dev *pdev = adapter->pdev;
  847. struct atl1c_tpd_ring *tpd_ring = adapter->tpd_ring;
  848. struct atl1c_rfd_ring *rfd_ring = &adapter->rfd_ring;
  849. struct atl1c_rrd_ring *rrd_ring = &adapter->rrd_ring;
  850. struct atl1c_ring_header *ring_header = &adapter->ring_header;
  851. int size;
  852. int i;
  853. int count = 0;
  854. int rx_desc_count = 0;
  855. u32 offset = 0;
  856. rrd_ring->count = rfd_ring->count;
  857. for (i = 1; i < AT_MAX_TRANSMIT_QUEUE; i++)
  858. tpd_ring[i].count = tpd_ring[0].count;
  859. /* 2 tpd queue, one high priority queue,
  860. * another normal priority queue */
  861. size = sizeof(struct atl1c_buffer) * (tpd_ring->count * 2 +
  862. rfd_ring->count);
  863. tpd_ring->buffer_info = kzalloc(size, GFP_KERNEL);
  864. if (unlikely(!tpd_ring->buffer_info))
  865. goto err_nomem;
  866. for (i = 0; i < AT_MAX_TRANSMIT_QUEUE; i++) {
  867. tpd_ring[i].buffer_info =
  868. (tpd_ring->buffer_info + count);
  869. count += tpd_ring[i].count;
  870. }
  871. rfd_ring->buffer_info =
  872. (tpd_ring->buffer_info + count);
  873. count += rfd_ring->count;
  874. rx_desc_count += rfd_ring->count;
  875. /*
  876. * real ring DMA buffer
  877. * each ring/block may need up to 8 bytes for alignment, hence the
  878. * additional bytes tacked onto the end.
  879. */
  880. ring_header->size = size =
  881. sizeof(struct atl1c_tpd_desc) * tpd_ring->count * 2 +
  882. sizeof(struct atl1c_rx_free_desc) * rx_desc_count +
  883. sizeof(struct atl1c_recv_ret_status) * rx_desc_count +
  884. 8 * 4;
  885. ring_header->desc = dma_alloc_coherent(&pdev->dev, ring_header->size,
  886. &ring_header->dma, GFP_KERNEL);
  887. if (unlikely(!ring_header->desc)) {
  888. dev_err(&pdev->dev, "could not get memory for DMA buffer\n");
  889. goto err_nomem;
  890. }
  891. /* init TPD ring */
  892. tpd_ring[0].dma = roundup(ring_header->dma, 8);
  893. offset = tpd_ring[0].dma - ring_header->dma;
  894. for (i = 0; i < AT_MAX_TRANSMIT_QUEUE; i++) {
  895. tpd_ring[i].dma = ring_header->dma + offset;
  896. tpd_ring[i].desc = (u8 *) ring_header->desc + offset;
  897. tpd_ring[i].size =
  898. sizeof(struct atl1c_tpd_desc) * tpd_ring[i].count;
  899. offset += roundup(tpd_ring[i].size, 8);
  900. }
  901. /* init RFD ring */
  902. rfd_ring->dma = ring_header->dma + offset;
  903. rfd_ring->desc = (u8 *) ring_header->desc + offset;
  904. rfd_ring->size = sizeof(struct atl1c_rx_free_desc) * rfd_ring->count;
  905. offset += roundup(rfd_ring->size, 8);
  906. /* init RRD ring */
  907. rrd_ring->dma = ring_header->dma + offset;
  908. rrd_ring->desc = (u8 *) ring_header->desc + offset;
  909. rrd_ring->size = sizeof(struct atl1c_recv_ret_status) *
  910. rrd_ring->count;
  911. offset += roundup(rrd_ring->size, 8);
  912. return 0;
  913. err_nomem:
  914. kfree(tpd_ring->buffer_info);
  915. return -ENOMEM;
  916. }
  917. static void atl1c_configure_des_ring(struct atl1c_adapter *adapter)
  918. {
  919. struct atl1c_hw *hw = &adapter->hw;
  920. struct atl1c_rfd_ring *rfd_ring = &adapter->rfd_ring;
  921. struct atl1c_rrd_ring *rrd_ring = &adapter->rrd_ring;
  922. struct atl1c_tpd_ring *tpd_ring = (struct atl1c_tpd_ring *)
  923. adapter->tpd_ring;
  924. /* TPD */
  925. AT_WRITE_REG(hw, REG_TX_BASE_ADDR_HI,
  926. (u32)((tpd_ring[atl1c_trans_normal].dma &
  927. AT_DMA_HI_ADDR_MASK) >> 32));
  928. /* just enable normal priority TX queue */
  929. AT_WRITE_REG(hw, REG_TPD_PRI0_ADDR_LO,
  930. (u32)(tpd_ring[atl1c_trans_normal].dma &
  931. AT_DMA_LO_ADDR_MASK));
  932. AT_WRITE_REG(hw, REG_TPD_PRI1_ADDR_LO,
  933. (u32)(tpd_ring[atl1c_trans_high].dma &
  934. AT_DMA_LO_ADDR_MASK));
  935. AT_WRITE_REG(hw, REG_TPD_RING_SIZE,
  936. (u32)(tpd_ring[0].count & TPD_RING_SIZE_MASK));
  937. /* RFD */
  938. AT_WRITE_REG(hw, REG_RX_BASE_ADDR_HI,
  939. (u32)((rfd_ring->dma & AT_DMA_HI_ADDR_MASK) >> 32));
  940. AT_WRITE_REG(hw, REG_RFD0_HEAD_ADDR_LO,
  941. (u32)(rfd_ring->dma & AT_DMA_LO_ADDR_MASK));
  942. AT_WRITE_REG(hw, REG_RFD_RING_SIZE,
  943. rfd_ring->count & RFD_RING_SIZE_MASK);
  944. AT_WRITE_REG(hw, REG_RX_BUF_SIZE,
  945. adapter->rx_buffer_len & RX_BUF_SIZE_MASK);
  946. /* RRD */
  947. AT_WRITE_REG(hw, REG_RRD0_HEAD_ADDR_LO,
  948. (u32)(rrd_ring->dma & AT_DMA_LO_ADDR_MASK));
  949. AT_WRITE_REG(hw, REG_RRD_RING_SIZE,
  950. (rrd_ring->count & RRD_RING_SIZE_MASK));
  951. if (hw->nic_type == athr_l2c_b) {
  952. AT_WRITE_REG(hw, REG_SRAM_RXF_LEN, 0x02a0L);
  953. AT_WRITE_REG(hw, REG_SRAM_TXF_LEN, 0x0100L);
  954. AT_WRITE_REG(hw, REG_SRAM_RXF_ADDR, 0x029f0000L);
  955. AT_WRITE_REG(hw, REG_SRAM_RFD0_INFO, 0x02bf02a0L);
  956. AT_WRITE_REG(hw, REG_SRAM_TXF_ADDR, 0x03bf02c0L);
  957. AT_WRITE_REG(hw, REG_SRAM_TRD_ADDR, 0x03df03c0L);
  958. AT_WRITE_REG(hw, REG_TXF_WATER_MARK, 0); /* TX watermark, to enter l1 state.*/
  959. AT_WRITE_REG(hw, REG_RXD_DMA_CTRL, 0); /* RXD threshold.*/
  960. }
  961. /* Load all of base address above */
  962. AT_WRITE_REG(hw, REG_LOAD_PTR, 1);
  963. }
  964. static void atl1c_configure_tx(struct atl1c_adapter *adapter)
  965. {
  966. struct atl1c_hw *hw = &adapter->hw;
  967. int max_pay_load;
  968. u16 tx_offload_thresh;
  969. u32 txq_ctrl_data;
  970. tx_offload_thresh = MAX_TSO_FRAME_SIZE;
  971. AT_WRITE_REG(hw, REG_TX_TSO_OFFLOAD_THRESH,
  972. (tx_offload_thresh >> 3) & TX_TSO_OFFLOAD_THRESH_MASK);
  973. max_pay_load = pcie_get_readrq(adapter->pdev) >> 8;
  974. hw->dmar_block = min_t(u32, max_pay_load, hw->dmar_block);
  975. /*
  976. * if BIOS had changed the dam-read-max-length to an invalid value,
  977. * restore it to default value
  978. */
  979. if (hw->dmar_block < DEVICE_CTRL_MAXRRS_MIN) {
  980. pcie_set_readrq(adapter->pdev, 128 << DEVICE_CTRL_MAXRRS_MIN);
  981. hw->dmar_block = DEVICE_CTRL_MAXRRS_MIN;
  982. }
  983. txq_ctrl_data =
  984. hw->nic_type == athr_l2c_b || hw->nic_type == athr_l2c_b2 ?
  985. L2CB_TXQ_CFGV : L1C_TXQ_CFGV;
  986. AT_WRITE_REG(hw, REG_TXQ_CTRL, txq_ctrl_data);
  987. }
  988. static void atl1c_configure_rx(struct atl1c_adapter *adapter)
  989. {
  990. struct atl1c_hw *hw = &adapter->hw;
  991. u32 rxq_ctrl_data;
  992. rxq_ctrl_data = (hw->rfd_burst & RXQ_RFD_BURST_NUM_MASK) <<
  993. RXQ_RFD_BURST_NUM_SHIFT;
  994. if (hw->ctrl_flags & ATL1C_RX_IPV6_CHKSUM)
  995. rxq_ctrl_data |= IPV6_CHKSUM_CTRL_EN;
  996. /* aspm for gigabit */
  997. if (hw->nic_type != athr_l1d_2 && (hw->device_id & 1) != 0)
  998. rxq_ctrl_data = FIELD_SETX(rxq_ctrl_data, ASPM_THRUPUT_LIMIT,
  999. ASPM_THRUPUT_LIMIT_100M);
  1000. AT_WRITE_REG(hw, REG_RXQ_CTRL, rxq_ctrl_data);
  1001. }
  1002. static void atl1c_configure_dma(struct atl1c_adapter *adapter)
  1003. {
  1004. struct atl1c_hw *hw = &adapter->hw;
  1005. u32 dma_ctrl_data;
  1006. dma_ctrl_data = FIELDX(DMA_CTRL_RORDER_MODE, DMA_CTRL_RORDER_MODE_OUT) |
  1007. DMA_CTRL_RREQ_PRI_DATA |
  1008. FIELDX(DMA_CTRL_RREQ_BLEN, hw->dmar_block) |
  1009. FIELDX(DMA_CTRL_WDLY_CNT, DMA_CTRL_WDLY_CNT_DEF) |
  1010. FIELDX(DMA_CTRL_RDLY_CNT, DMA_CTRL_RDLY_CNT_DEF);
  1011. AT_WRITE_REG(hw, REG_DMA_CTRL, dma_ctrl_data);
  1012. }
  1013. /*
  1014. * Stop the mac, transmit and receive units
  1015. * hw - Struct containing variables accessed by shared code
  1016. * return : 0 or idle status (if error)
  1017. */
  1018. static int atl1c_stop_mac(struct atl1c_hw *hw)
  1019. {
  1020. u32 data;
  1021. AT_READ_REG(hw, REG_RXQ_CTRL, &data);
  1022. data &= ~RXQ_CTRL_EN;
  1023. AT_WRITE_REG(hw, REG_RXQ_CTRL, data);
  1024. AT_READ_REG(hw, REG_TXQ_CTRL, &data);
  1025. data &= ~TXQ_CTRL_EN;
  1026. AT_WRITE_REG(hw, REG_TXQ_CTRL, data);
  1027. atl1c_wait_until_idle(hw, IDLE_STATUS_RXQ_BUSY | IDLE_STATUS_TXQ_BUSY);
  1028. AT_READ_REG(hw, REG_MAC_CTRL, &data);
  1029. data &= ~(MAC_CTRL_TX_EN | MAC_CTRL_RX_EN);
  1030. AT_WRITE_REG(hw, REG_MAC_CTRL, data);
  1031. return (int)atl1c_wait_until_idle(hw,
  1032. IDLE_STATUS_TXMAC_BUSY | IDLE_STATUS_RXMAC_BUSY);
  1033. }
  1034. static void atl1c_start_mac(struct atl1c_adapter *adapter)
  1035. {
  1036. struct atl1c_hw *hw = &adapter->hw;
  1037. u32 mac, txq, rxq;
  1038. hw->mac_duplex = adapter->link_duplex == FULL_DUPLEX ? true : false;
  1039. hw->mac_speed = adapter->link_speed == SPEED_1000 ?
  1040. atl1c_mac_speed_1000 : atl1c_mac_speed_10_100;
  1041. AT_READ_REG(hw, REG_TXQ_CTRL, &txq);
  1042. AT_READ_REG(hw, REG_RXQ_CTRL, &rxq);
  1043. AT_READ_REG(hw, REG_MAC_CTRL, &mac);
  1044. txq |= TXQ_CTRL_EN;
  1045. rxq |= RXQ_CTRL_EN;
  1046. mac |= MAC_CTRL_TX_EN | MAC_CTRL_TX_FLOW |
  1047. MAC_CTRL_RX_EN | MAC_CTRL_RX_FLOW |
  1048. MAC_CTRL_ADD_CRC | MAC_CTRL_PAD |
  1049. MAC_CTRL_BC_EN | MAC_CTRL_SINGLE_PAUSE_EN |
  1050. MAC_CTRL_HASH_ALG_CRC32;
  1051. if (hw->mac_duplex)
  1052. mac |= MAC_CTRL_DUPLX;
  1053. else
  1054. mac &= ~MAC_CTRL_DUPLX;
  1055. mac = FIELD_SETX(mac, MAC_CTRL_SPEED, hw->mac_speed);
  1056. mac = FIELD_SETX(mac, MAC_CTRL_PRMLEN, hw->preamble_len);
  1057. AT_WRITE_REG(hw, REG_TXQ_CTRL, txq);
  1058. AT_WRITE_REG(hw, REG_RXQ_CTRL, rxq);
  1059. AT_WRITE_REG(hw, REG_MAC_CTRL, mac);
  1060. }
  1061. /*
  1062. * Reset the transmit and receive units; mask and clear all interrupts.
  1063. * hw - Struct containing variables accessed by shared code
  1064. * return : 0 or idle status (if error)
  1065. */
  1066. static int atl1c_reset_mac(struct atl1c_hw *hw)
  1067. {
  1068. struct atl1c_adapter *adapter = hw->adapter;
  1069. struct pci_dev *pdev = adapter->pdev;
  1070. u32 ctrl_data = 0;
  1071. atl1c_stop_mac(hw);
  1072. /*
  1073. * Issue Soft Reset to the MAC. This will reset the chip's
  1074. * transmit, receive, DMA. It will not effect
  1075. * the current PCI configuration. The global reset bit is self-
  1076. * clearing, and should clear within a microsecond.
  1077. */
  1078. AT_READ_REG(hw, REG_MASTER_CTRL, &ctrl_data);
  1079. ctrl_data |= MASTER_CTRL_OOB_DIS;
  1080. AT_WRITE_REG(hw, REG_MASTER_CTRL, ctrl_data | MASTER_CTRL_SOFT_RST);
  1081. AT_WRITE_FLUSH(hw);
  1082. msleep(10);
  1083. /* Wait at least 10ms for All module to be Idle */
  1084. if (atl1c_wait_until_idle(hw, IDLE_STATUS_MASK)) {
  1085. dev_err(&pdev->dev,
  1086. "MAC state machine can't be idle since"
  1087. " disabled for 10ms second\n");
  1088. return -1;
  1089. }
  1090. AT_WRITE_REG(hw, REG_MASTER_CTRL, ctrl_data);
  1091. /* driver control speed/duplex */
  1092. AT_READ_REG(hw, REG_MAC_CTRL, &ctrl_data);
  1093. AT_WRITE_REG(hw, REG_MAC_CTRL, ctrl_data | MAC_CTRL_SPEED_MODE_SW);
  1094. /* clk switch setting */
  1095. AT_READ_REG(hw, REG_SERDES, &ctrl_data);
  1096. switch (hw->nic_type) {
  1097. case athr_l2c_b:
  1098. ctrl_data &= ~(SERDES_PHY_CLK_SLOWDOWN |
  1099. SERDES_MAC_CLK_SLOWDOWN);
  1100. AT_WRITE_REG(hw, REG_SERDES, ctrl_data);
  1101. break;
  1102. case athr_l2c_b2:
  1103. case athr_l1d_2:
  1104. ctrl_data |= SERDES_PHY_CLK_SLOWDOWN | SERDES_MAC_CLK_SLOWDOWN;
  1105. AT_WRITE_REG(hw, REG_SERDES, ctrl_data);
  1106. break;
  1107. default:
  1108. break;
  1109. }
  1110. return 0;
  1111. }
  1112. static void atl1c_disable_l0s_l1(struct atl1c_hw *hw)
  1113. {
  1114. u16 ctrl_flags = hw->ctrl_flags;
  1115. hw->ctrl_flags &= ~(ATL1C_ASPM_L0S_SUPPORT | ATL1C_ASPM_L1_SUPPORT);
  1116. atl1c_set_aspm(hw, SPEED_0);
  1117. hw->ctrl_flags = ctrl_flags;
  1118. }
  1119. /*
  1120. * Set ASPM state.
  1121. * Enable/disable L0s/L1 depend on link state.
  1122. */
  1123. static void atl1c_set_aspm(struct atl1c_hw *hw, u16 link_speed)
  1124. {
  1125. u32 pm_ctrl_data;
  1126. u32 link_l1_timer;
  1127. AT_READ_REG(hw, REG_PM_CTRL, &pm_ctrl_data);
  1128. pm_ctrl_data &= ~(PM_CTRL_ASPM_L1_EN |
  1129. PM_CTRL_ASPM_L0S_EN |
  1130. PM_CTRL_MAC_ASPM_CHK);
  1131. /* L1 timer */
  1132. if (hw->nic_type == athr_l2c_b2 || hw->nic_type == athr_l1d_2) {
  1133. pm_ctrl_data &= ~PMCTRL_TXL1_AFTER_L0S;
  1134. link_l1_timer =
  1135. link_speed == SPEED_1000 || link_speed == SPEED_100 ?
  1136. L1D_PMCTRL_L1_ENTRY_TM_16US : 1;
  1137. pm_ctrl_data = FIELD_SETX(pm_ctrl_data,
  1138. L1D_PMCTRL_L1_ENTRY_TM, link_l1_timer);
  1139. } else {
  1140. link_l1_timer = hw->nic_type == athr_l2c_b ?
  1141. L2CB1_PM_CTRL_L1_ENTRY_TM : L1C_PM_CTRL_L1_ENTRY_TM;
  1142. if (link_speed != SPEED_1000 && link_speed != SPEED_100)
  1143. link_l1_timer = 1;
  1144. pm_ctrl_data = FIELD_SETX(pm_ctrl_data,
  1145. PM_CTRL_L1_ENTRY_TIMER, link_l1_timer);
  1146. }
  1147. /* L0S/L1 enable */
  1148. if ((hw->ctrl_flags & ATL1C_ASPM_L0S_SUPPORT) && link_speed != SPEED_0)
  1149. pm_ctrl_data |= PM_CTRL_ASPM_L0S_EN | PM_CTRL_MAC_ASPM_CHK;
  1150. if (hw->ctrl_flags & ATL1C_ASPM_L1_SUPPORT)
  1151. pm_ctrl_data |= PM_CTRL_ASPM_L1_EN | PM_CTRL_MAC_ASPM_CHK;
  1152. /* l2cb & l1d & l2cb2 & l1d2 */
  1153. if (hw->nic_type == athr_l2c_b || hw->nic_type == athr_l1d ||
  1154. hw->nic_type == athr_l2c_b2 || hw->nic_type == athr_l1d_2) {
  1155. pm_ctrl_data = FIELD_SETX(pm_ctrl_data,
  1156. PM_CTRL_PM_REQ_TIMER, PM_CTRL_PM_REQ_TO_DEF);
  1157. pm_ctrl_data |= PM_CTRL_RCVR_WT_TIMER |
  1158. PM_CTRL_SERDES_PD_EX_L1 |
  1159. PM_CTRL_CLK_SWH_L1;
  1160. pm_ctrl_data &= ~(PM_CTRL_SERDES_L1_EN |
  1161. PM_CTRL_SERDES_PLL_L1_EN |
  1162. PM_CTRL_SERDES_BUFS_RX_L1_EN |
  1163. PM_CTRL_SA_DLY_EN |
  1164. PM_CTRL_HOTRST);
  1165. /* disable l0s if link down or l2cb */
  1166. if (link_speed == SPEED_0 || hw->nic_type == athr_l2c_b)
  1167. pm_ctrl_data &= ~PM_CTRL_ASPM_L0S_EN;
  1168. } else { /* l1c */
  1169. pm_ctrl_data =
  1170. FIELD_SETX(pm_ctrl_data, PM_CTRL_L1_ENTRY_TIMER, 0);
  1171. if (link_speed != SPEED_0) {
  1172. pm_ctrl_data |= PM_CTRL_SERDES_L1_EN |
  1173. PM_CTRL_SERDES_PLL_L1_EN |
  1174. PM_CTRL_SERDES_BUFS_RX_L1_EN;
  1175. pm_ctrl_data &= ~(PM_CTRL_SERDES_PD_EX_L1 |
  1176. PM_CTRL_CLK_SWH_L1 |
  1177. PM_CTRL_ASPM_L0S_EN |
  1178. PM_CTRL_ASPM_L1_EN);
  1179. } else { /* link down */
  1180. pm_ctrl_data |= PM_CTRL_CLK_SWH_L1;
  1181. pm_ctrl_data &= ~(PM_CTRL_SERDES_L1_EN |
  1182. PM_CTRL_SERDES_PLL_L1_EN |
  1183. PM_CTRL_SERDES_BUFS_RX_L1_EN |
  1184. PM_CTRL_ASPM_L0S_EN);
  1185. }
  1186. }
  1187. AT_WRITE_REG(hw, REG_PM_CTRL, pm_ctrl_data);
  1188. return;
  1189. }
  1190. /**
  1191. * atl1c_configure - Configure Transmit&Receive Unit after Reset
  1192. * @adapter: board private structure
  1193. *
  1194. * Configure the Tx /Rx unit of the MAC after a reset.
  1195. */
  1196. static int atl1c_configure_mac(struct atl1c_adapter *adapter)
  1197. {
  1198. struct atl1c_hw *hw = &adapter->hw;
  1199. u32 master_ctrl_data = 0;
  1200. u32 intr_modrt_data;
  1201. u32 data;
  1202. AT_READ_REG(hw, REG_MASTER_CTRL, &master_ctrl_data);
  1203. master_ctrl_data &= ~(MASTER_CTRL_TX_ITIMER_EN |
  1204. MASTER_CTRL_RX_ITIMER_EN |
  1205. MASTER_CTRL_INT_RDCLR);
  1206. /* clear interrupt status */
  1207. AT_WRITE_REG(hw, REG_ISR, 0xFFFFFFFF);
  1208. /* Clear any WOL status */
  1209. AT_WRITE_REG(hw, REG_WOL_CTRL, 0);
  1210. /* set Interrupt Clear Timer
  1211. * HW will enable self to assert interrupt event to system after
  1212. * waiting x-time for software to notify it accept interrupt.
  1213. */
  1214. data = CLK_GATING_EN_ALL;
  1215. if (hw->ctrl_flags & ATL1C_CLK_GATING_EN) {
  1216. if (hw->nic_type == athr_l2c_b)
  1217. data &= ~CLK_GATING_RXMAC_EN;
  1218. } else
  1219. data = 0;
  1220. AT_WRITE_REG(hw, REG_CLK_GATING_CTRL, data);
  1221. AT_WRITE_REG(hw, REG_INT_RETRIG_TIMER,
  1222. hw->ict & INT_RETRIG_TIMER_MASK);
  1223. atl1c_configure_des_ring(adapter);
  1224. if (hw->ctrl_flags & ATL1C_INTR_MODRT_ENABLE) {
  1225. intr_modrt_data = (hw->tx_imt & IRQ_MODRT_TIMER_MASK) <<
  1226. IRQ_MODRT_TX_TIMER_SHIFT;
  1227. intr_modrt_data |= (hw->rx_imt & IRQ_MODRT_TIMER_MASK) <<
  1228. IRQ_MODRT_RX_TIMER_SHIFT;
  1229. AT_WRITE_REG(hw, REG_IRQ_MODRT_TIMER_INIT, intr_modrt_data);
  1230. master_ctrl_data |=
  1231. MASTER_CTRL_TX_ITIMER_EN | MASTER_CTRL_RX_ITIMER_EN;
  1232. }
  1233. if (hw->ctrl_flags & ATL1C_INTR_CLEAR_ON_READ)
  1234. master_ctrl_data |= MASTER_CTRL_INT_RDCLR;
  1235. master_ctrl_data |= MASTER_CTRL_SA_TIMER_EN;
  1236. AT_WRITE_REG(hw, REG_MASTER_CTRL, master_ctrl_data);
  1237. AT_WRITE_REG(hw, REG_SMB_STAT_TIMER,
  1238. hw->smb_timer & SMB_STAT_TIMER_MASK);
  1239. /* set MTU */
  1240. AT_WRITE_REG(hw, REG_MTU, hw->max_frame_size + ETH_HLEN +
  1241. VLAN_HLEN + ETH_FCS_LEN);
  1242. atl1c_configure_tx(adapter);
  1243. atl1c_configure_rx(adapter);
  1244. atl1c_configure_dma(adapter);
  1245. return 0;
  1246. }
  1247. static int atl1c_configure(struct atl1c_adapter *adapter)
  1248. {
  1249. struct net_device *netdev = adapter->netdev;
  1250. int num;
  1251. atl1c_init_ring_ptrs(adapter);
  1252. atl1c_set_multi(netdev);
  1253. atl1c_restore_vlan(adapter);
  1254. num = atl1c_alloc_rx_buffer(adapter);
  1255. if (unlikely(num == 0))
  1256. return -ENOMEM;
  1257. if (atl1c_configure_mac(adapter))
  1258. return -EIO;
  1259. return 0;
  1260. }
  1261. static void atl1c_update_hw_stats(struct atl1c_adapter *adapter)
  1262. {
  1263. u16 hw_reg_addr = 0;
  1264. unsigned long *stats_item = NULL;
  1265. u32 data;
  1266. /* update rx status */
  1267. hw_reg_addr = REG_MAC_RX_STATUS_BIN;
  1268. stats_item = &adapter->hw_stats.rx_ok;
  1269. while (hw_reg_addr <= REG_MAC_RX_STATUS_END) {
  1270. AT_READ_REG(&adapter->hw, hw_reg_addr, &data);
  1271. *stats_item += data;
  1272. stats_item++;
  1273. hw_reg_addr += 4;
  1274. }
  1275. /* update tx status */
  1276. hw_reg_addr = REG_MAC_TX_STATUS_BIN;
  1277. stats_item = &adapter->hw_stats.tx_ok;
  1278. while (hw_reg_addr <= REG_MAC_TX_STATUS_END) {
  1279. AT_READ_REG(&adapter->hw, hw_reg_addr, &data);
  1280. *stats_item += data;
  1281. stats_item++;
  1282. hw_reg_addr += 4;
  1283. }
  1284. }
  1285. /**
  1286. * atl1c_get_stats - Get System Network Statistics
  1287. * @netdev: network interface device structure
  1288. *
  1289. * Returns the address of the device statistics structure.
  1290. * The statistics are actually updated from the timer callback.
  1291. */
  1292. static struct net_device_stats *atl1c_get_stats(struct net_device *netdev)
  1293. {
  1294. struct atl1c_adapter *adapter = netdev_priv(netdev);
  1295. struct atl1c_hw_stats *hw_stats = &adapter->hw_stats;
  1296. struct net_device_stats *net_stats = &netdev->stats;
  1297. atl1c_update_hw_stats(adapter);
  1298. net_stats->rx_bytes = hw_stats->rx_byte_cnt;
  1299. net_stats->tx_bytes = hw_stats->tx_byte_cnt;
  1300. net_stats->multicast = hw_stats->rx_mcast;
  1301. net_stats->collisions = hw_stats->tx_1_col +
  1302. hw_stats->tx_2_col +
  1303. hw_stats->tx_late_col +
  1304. hw_stats->tx_abort_col;
  1305. net_stats->rx_errors = hw_stats->rx_frag +
  1306. hw_stats->rx_fcs_err +
  1307. hw_stats->rx_len_err +
  1308. hw_stats->rx_sz_ov +
  1309. hw_stats->rx_rrd_ov +
  1310. hw_stats->rx_align_err +
  1311. hw_stats->rx_rxf_ov;
  1312. net_stats->rx_fifo_errors = hw_stats->rx_rxf_ov;
  1313. net_stats->rx_length_errors = hw_stats->rx_len_err;
  1314. net_stats->rx_crc_errors = hw_stats->rx_fcs_err;
  1315. net_stats->rx_frame_errors = hw_stats->rx_align_err;
  1316. net_stats->rx_dropped = hw_stats->rx_rrd_ov;
  1317. net_stats->tx_errors = hw_stats->tx_late_col +
  1318. hw_stats->tx_abort_col +
  1319. hw_stats->tx_underrun +
  1320. hw_stats->tx_trunc;
  1321. net_stats->tx_fifo_errors = hw_stats->tx_underrun;
  1322. net_stats->tx_aborted_errors = hw_stats->tx_abort_col;
  1323. net_stats->tx_window_errors = hw_stats->tx_late_col;
  1324. net_stats->rx_packets = hw_stats->rx_ok + net_stats->rx_errors;
  1325. net_stats->tx_packets = hw_stats->tx_ok + net_stats->tx_errors;
  1326. return net_stats;
  1327. }
  1328. static inline void atl1c_clear_phy_int(struct atl1c_adapter *adapter)
  1329. {
  1330. u16 phy_data;
  1331. spin_lock(&adapter->mdio_lock);
  1332. atl1c_read_phy_reg(&adapter->hw, MII_ISR, &phy_data);
  1333. spin_unlock(&adapter->mdio_lock);
  1334. }
  1335. static bool atl1c_clean_tx_irq(struct atl1c_adapter *adapter,
  1336. enum atl1c_trans_queue type)
  1337. {
  1338. struct atl1c_tpd_ring *tpd_ring = &adapter->tpd_ring[type];
  1339. struct atl1c_buffer *buffer_info;
  1340. struct pci_dev *pdev = adapter->pdev;
  1341. u16 next_to_clean = atomic_read(&tpd_ring->next_to_clean);
  1342. u16 hw_next_to_clean;
  1343. u16 reg;
  1344. unsigned int total_bytes = 0, total_packets = 0;
  1345. reg = type == atl1c_trans_high ? REG_TPD_PRI1_CIDX : REG_TPD_PRI0_CIDX;
  1346. AT_READ_REGW(&adapter->hw, reg, &hw_next_to_clean);
  1347. while (next_to_clean != hw_next_to_clean) {
  1348. buffer_info = &tpd_ring->buffer_info[next_to_clean];
  1349. if (buffer_info->skb) {
  1350. total_bytes += buffer_info->skb->len;
  1351. total_packets++;
  1352. }
  1353. atl1c_clean_buffer(pdev, buffer_info);
  1354. if (++next_to_clean == tpd_ring->count)
  1355. next_to_clean = 0;
  1356. atomic_set(&tpd_ring->next_to_clean, next_to_clean);
  1357. }
  1358. netdev_completed_queue(adapter->netdev, total_packets, total_bytes);
  1359. if (netif_queue_stopped(adapter->netdev) &&
  1360. netif_carrier_ok(adapter->netdev)) {
  1361. netif_wake_queue(adapter->netdev);
  1362. }
  1363. return true;
  1364. }
  1365. /**
  1366. * atl1c_intr - Interrupt Handler
  1367. * @irq: interrupt number
  1368. * @data: pointer to a network interface device structure
  1369. */
  1370. static irqreturn_t atl1c_intr(int irq, void *data)
  1371. {
  1372. struct net_device *netdev = data;
  1373. struct atl1c_adapter *adapter = netdev_priv(netdev);
  1374. struct pci_dev *pdev = adapter->pdev;
  1375. struct atl1c_hw *hw = &adapter->hw;
  1376. int max_ints = AT_MAX_INT_WORK;
  1377. int handled = IRQ_NONE;
  1378. u32 status;
  1379. u32 reg_data;
  1380. do {
  1381. AT_READ_REG(hw, REG_ISR, &reg_data);
  1382. status = reg_data & hw->intr_mask;
  1383. if (status == 0 || (status & ISR_DIS_INT) != 0) {
  1384. if (max_ints != AT_MAX_INT_WORK)
  1385. handled = IRQ_HANDLED;
  1386. break;
  1387. }
  1388. /* link event */
  1389. if (status & ISR_GPHY)
  1390. atl1c_clear_phy_int(adapter);
  1391. /* Ack ISR */
  1392. AT_WRITE_REG(hw, REG_ISR, status | ISR_DIS_INT);
  1393. if (status & ISR_RX_PKT) {
  1394. if (likely(napi_schedule_prep(&adapter->napi))) {
  1395. hw->intr_mask &= ~ISR_RX_PKT;
  1396. AT_WRITE_REG(hw, REG_IMR, hw->intr_mask);
  1397. __napi_schedule(&adapter->napi);
  1398. }
  1399. }
  1400. if (status & ISR_TX_PKT)
  1401. atl1c_clean_tx_irq(adapter, atl1c_trans_normal);
  1402. handled = IRQ_HANDLED;
  1403. /* check if PCIE PHY Link down */
  1404. if (status & ISR_ERROR) {
  1405. if (netif_msg_hw(adapter))
  1406. dev_err(&pdev->dev,
  1407. "atl1c hardware error (status = 0x%x)\n",
  1408. status & ISR_ERROR);
  1409. /* reset MAC */
  1410. set_bit(ATL1C_WORK_EVENT_RESET, &adapter->work_event);
  1411. schedule_work(&adapter->common_task);
  1412. return IRQ_HANDLED;
  1413. }
  1414. if (status & ISR_OVER)
  1415. if (netif_msg_intr(adapter))
  1416. dev_warn(&pdev->dev,
  1417. "TX/RX overflow (status = 0x%x)\n",
  1418. status & ISR_OVER);
  1419. /* link event */
  1420. if (status & (ISR_GPHY | ISR_MANUAL)) {
  1421. netdev->stats.tx_carrier_errors++;
  1422. atl1c_link_chg_event(adapter);
  1423. break;
  1424. }
  1425. } while (--max_ints > 0);
  1426. /* re-enable Interrupt*/
  1427. AT_WRITE_REG(&adapter->hw, REG_ISR, 0);
  1428. return handled;
  1429. }
  1430. static inline void atl1c_rx_checksum(struct atl1c_adapter *adapter,
  1431. struct sk_buff *skb, struct atl1c_recv_ret_status *prrs)
  1432. {
  1433. /*
  1434. * The pid field in RRS in not correct sometimes, so we
  1435. * cannot figure out if the packet is fragmented or not,
  1436. * so we tell the KERNEL CHECKSUM_NONE
  1437. */
  1438. skb_checksum_none_assert(skb);
  1439. }
  1440. static struct sk_buff *atl1c_alloc_skb(struct atl1c_adapter *adapter)
  1441. {
  1442. struct sk_buff *skb;
  1443. struct page *page;
  1444. if (adapter->rx_frag_size > PAGE_SIZE)
  1445. return netdev_alloc_skb(adapter->netdev,
  1446. adapter->rx_buffer_len);
  1447. page = adapter->rx_page;
  1448. if (!page) {
  1449. adapter->rx_page = page = alloc_page(GFP_ATOMIC);
  1450. if (unlikely(!page))
  1451. return NULL;
  1452. adapter->rx_page_offset = 0;
  1453. }
  1454. skb = build_skb(page_address(page) + adapter->rx_page_offset,
  1455. adapter->rx_frag_size);
  1456. if (likely(skb)) {
  1457. skb_reserve(skb, NET_SKB_PAD);
  1458. adapter->rx_page_offset += adapter->rx_frag_size;
  1459. if (adapter->rx_page_offset >= PAGE_SIZE)
  1460. adapter->rx_page = NULL;
  1461. else
  1462. get_page(page);
  1463. }
  1464. return skb;
  1465. }
  1466. static int atl1c_alloc_rx_buffer(struct atl1c_adapter *adapter)
  1467. {
  1468. struct atl1c_rfd_ring *rfd_ring = &adapter->rfd_ring;
  1469. struct pci_dev *pdev = adapter->pdev;
  1470. struct atl1c_buffer *buffer_info, *next_info;
  1471. struct sk_buff *skb;
  1472. void *vir_addr = NULL;
  1473. u16 num_alloc = 0;
  1474. u16 rfd_next_to_use, next_next;
  1475. struct atl1c_rx_free_desc *rfd_desc;
  1476. dma_addr_t mapping;
  1477. next_next = rfd_next_to_use = rfd_ring->next_to_use;
  1478. if (++next_next == rfd_ring->count)
  1479. next_next = 0;
  1480. buffer_info = &rfd_ring->buffer_info[rfd_next_to_use];
  1481. next_info = &rfd_ring->buffer_info[next_next];
  1482. while (next_info->flags & ATL1C_BUFFER_FREE) {
  1483. rfd_desc = ATL1C_RFD_DESC(rfd_ring, rfd_next_to_use);
  1484. skb = atl1c_alloc_skb(adapter);
  1485. if (unlikely(!skb)) {
  1486. if (netif_msg_rx_err(adapter))
  1487. dev_warn(&pdev->dev, "alloc rx buffer failed\n");
  1488. break;
  1489. }
  1490. /*
  1491. * Make buffer alignment 2 beyond a 16 byte boundary
  1492. * this will result in a 16 byte aligned IP header after
  1493. * the 14 byte MAC header is removed
  1494. */
  1495. vir_addr = skb->data;
  1496. ATL1C_SET_BUFFER_STATE(buffer_info, ATL1C_BUFFER_BUSY);
  1497. buffer_info->skb = skb;
  1498. buffer_info->length = adapter->rx_buffer_len;
  1499. mapping = pci_map_single(pdev, vir_addr,
  1500. buffer_info->length,
  1501. PCI_DMA_FROMDEVICE);
  1502. if (unlikely(pci_dma_mapping_error(pdev, mapping))) {
  1503. dev_kfree_skb(skb);
  1504. buffer_info->skb = NULL;
  1505. buffer_info->length = 0;
  1506. ATL1C_SET_BUFFER_STATE(buffer_info, ATL1C_BUFFER_FREE);
  1507. netif_warn(adapter, rx_err, adapter->netdev, "RX pci_map_single failed");
  1508. break;
  1509. }
  1510. buffer_info->dma = mapping;
  1511. ATL1C_SET_PCIMAP_TYPE(buffer_info, ATL1C_PCIMAP_SINGLE,
  1512. ATL1C_PCIMAP_FROMDEVICE);
  1513. rfd_desc->buffer_addr = cpu_to_le64(buffer_info->dma);
  1514. rfd_next_to_use = next_next;
  1515. if (++next_next == rfd_ring->count)
  1516. next_next = 0;
  1517. buffer_info = &rfd_ring->buffer_info[rfd_next_to_use];
  1518. next_info = &rfd_ring->buffer_info[next_next];
  1519. num_alloc++;
  1520. }
  1521. if (num_alloc) {
  1522. /* TODO: update mailbox here */
  1523. wmb();
  1524. rfd_ring->next_to_use = rfd_next_to_use;
  1525. AT_WRITE_REG(&adapter->hw, REG_MB_RFD0_PROD_IDX,
  1526. rfd_ring->next_to_use & MB_RFDX_PROD_IDX_MASK);
  1527. }
  1528. return num_alloc;
  1529. }
  1530. static void atl1c_clean_rrd(struct atl1c_rrd_ring *rrd_rin