atlx.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. /* atlx.c -- common functions for Attansic network drivers
  2. *
  3. * Copyright(c) 2005 - 2006 Attansic Corporation. All rights reserved.
  4. * Copyright(c) 2006 - 2007 Chris Snook <csnook@redhat.com>
  5. * Copyright(c) 2006 - 2008 Jay Cliburn <jcliburn@gmail.com>
  6. * Copyright(c) 2007 Atheros Corporation. All rights reserved.
  7. *
  8. * Derived from Intel e1000 driver
  9. * Copyright(c) 1999 - 2005 Intel Corporation. All rights reserved.
  10. *
  11. * This program is free software; you can redistribute it and/or modify it
  12. * under the terms of the GNU General Public License as published by the Free
  13. * Software Foundation; either version 2 of the License, or (at your option)
  14. * any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful, but WITHOUT
  17. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  18. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  19. * more details.
  20. *
  21. * You should have received a copy of the GNU General Public License along with
  22. * this program; if not, write to the Free Software Foundation, Inc., 59
  23. * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  24. */
  25. /* Including this file like a header is a temporary hack, I promise. -- CHS */
  26. #ifndef ATLX_C
  27. #define ATLX_C
  28. #include <linux/device.h>
  29. #include <linux/errno.h>
  30. #include <linux/etherdevice.h>
  31. #include <linux/if.h>
  32. #include <linux/netdevice.h>
  33. #include <linux/socket.h>
  34. #include <linux/sockios.h>
  35. #include <linux/spinlock.h>
  36. #include <linux/string.h>
  37. #include <linux/types.h>
  38. #include <linux/workqueue.h>
  39. #include "atlx.h"
  40. static s32 atlx_read_phy_reg(struct atl1_hw *hw, u16 reg_addr, u16 *phy_data);
  41. static u32 atlx_hash_mc_addr(struct atl1_hw *hw, u8 *mc_addr);
  42. static void atlx_set_mac_addr(struct atl1_hw *hw);
  43. static struct atlx_spi_flash_dev flash_table[] = {
  44. /* MFR_NAME WRSR READ PRGM WREN WRDI RDSR RDID SEC_ERS CHIP_ERS */
  45. {"Atmel", 0x00, 0x03, 0x02, 0x06, 0x04, 0x05, 0x15, 0x52, 0x62},
  46. {"SST", 0x01, 0x03, 0x02, 0x06, 0x04, 0x05, 0x90, 0x20, 0x60},
  47. {"ST", 0x01, 0x03, 0x02, 0x06, 0x04, 0x05, 0xAB, 0xD8, 0xC7},
  48. };
  49. static int atlx_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
  50. {
  51. switch (cmd) {
  52. case SIOCGMIIPHY:
  53. case SIOCGMIIREG:
  54. case SIOCSMIIREG:
  55. return atlx_mii_ioctl(netdev, ifr, cmd);
  56. default:
  57. return -EOPNOTSUPP;
  58. }
  59. }
  60. /**
  61. * atlx_set_mac - Change the Ethernet Address of the NIC
  62. * @netdev: network interface device structure
  63. * @p: pointer to an address structure
  64. *
  65. * Returns 0 on success, negative on failure
  66. */
  67. static int atlx_set_mac(struct net_device *netdev, void *p)
  68. {
  69. struct atlx_adapter *adapter = netdev_priv(netdev);
  70. struct sockaddr *addr = p;
  71. if (netif_running(netdev))
  72. return -EBUSY;
  73. if (!is_valid_ether_addr(addr->sa_data))
  74. return -EADDRNOTAVAIL;
  75. memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
  76. memcpy(adapter->hw.mac_addr, addr->sa_data, netdev->addr_len);
  77. atlx_set_mac_addr(&adapter->hw);
  78. return 0;
  79. }
  80. static void atlx_check_for_link(struct atlx_adapter *adapter)
  81. {
  82. struct net_device *netdev = adapter->netdev;
  83. u16 phy_data = 0;
  84. spin_lock(&adapter->lock);
  85. adapter->phy_timer_pending = false;
  86. atlx_read_phy_reg(&adapter->hw, MII_BMSR, &phy_data);
  87. atlx_read_phy_reg(&adapter->hw, MII_BMSR, &phy_data);
  88. spin_unlock(&adapter->lock);
  89. /* notify upper layer link down ASAP */
  90. if (!(phy_data & BMSR_LSTATUS)) {
  91. /* Link Down */
  92. if (netif_carrier_ok(netdev)) {
  93. /* old link state: Up */
  94. dev_info(&adapter->pdev->dev, "%s link is down\n",
  95. netdev->name);
  96. adapter->link_speed = SPEED_0;
  97. netif_carrier_off(netdev);
  98. }
  99. }
  100. schedule_work(&adapter->link_chg_task);
  101. }
  102. /**
  103. * atlx_set_multi - Multicast and Promiscuous mode set
  104. * @netdev: network interface device structure
  105. *
  106. * The set_multi entry point is called whenever the multicast address
  107. * list or the network interface flags are updated. This routine is
  108. * responsible for configuring the hardware for proper multicast,
  109. * promiscuous mode, and all-multi behavior.
  110. */
  111. static void atlx_set_multi(struct net_device *netdev)
  112. {
  113. struct atlx_adapter *adapter = netdev_priv(netdev);
  114. struct atlx_hw *hw = &adapter->hw;
  115. struct netdev_hw_addr *ha;
  116. u32 rctl;
  117. u32 hash_value;
  118. /* Check for Promiscuous and All Multicast modes */
  119. rctl = ioread32(hw->hw_addr + REG_MAC_CTRL);
  120. if (netdev->flags & IFF_PROMISC)
  121. rctl |= MAC_CTRL_PROMIS_EN;
  122. else if (netdev->flags & IFF_ALLMULTI) {
  123. rctl |= MAC_CTRL_MC_ALL_EN;
  124. rctl &= ~MAC_CTRL_PROMIS_EN;
  125. } else
  126. rctl &= ~(MAC_CTRL_PROMIS_EN | MAC_CTRL_MC_ALL_EN);
  127. iowrite32(rctl, hw->hw_addr + REG_MAC_CTRL);
  128. /* clear the old settings from the multicast hash table */
  129. iowrite32(0, hw->hw_addr + REG_RX_HASH_TABLE);
  130. iowrite32(0, (hw->hw_addr + REG_RX_HASH_TABLE) + (1 << 2));
  131. /* compute mc addresses' hash value ,and put it into hash table */
  132. netdev_for_each_mc_addr(ha, netdev) {
  133. hash_value = atlx_hash_mc_addr(hw, ha->addr);
  134. atlx_hash_set(hw, hash_value);
  135. }
  136. }
  137. static inline void atlx_imr_set(struct atlx_adapter *adapter,
  138. unsigned int imr)
  139. {
  140. iowrite32(imr, adapter->hw.hw_addr + REG_IMR);
  141. ioread32(adapter->hw.hw_addr + REG_IMR);
  142. }
  143. /**
  144. * atlx_irq_enable - Enable default interrupt generation settings
  145. * @adapter: board private structure
  146. */
  147. static void atlx_irq_enable(struct atlx_adapter *adapter)
  148. {
  149. atlx_imr_set(adapter, IMR_NORMAL_MASK);
  150. adapter->int_enabled = true;
  151. }
  152. /**
  153. * atlx_irq_disable - Mask off interrupt generation on the NIC
  154. * @adapter: board private structure
  155. */
  156. static void atlx_irq_disable(struct atlx_adapter *adapter)
  157. {
  158. adapter->int_enabled = false;
  159. atlx_imr_set(adapter, 0);
  160. synchronize_irq(adapter->pdev->irq);
  161. }
  162. static void atlx_clear_phy_int(struct atlx_adapter *adapter)
  163. {
  164. u16 phy_data;
  165. unsigned long flags;
  166. spin_lock_irqsave(&adapter->lock, flags);
  167. atlx_read_phy_reg(&adapter->hw, 19, &phy_data);
  168. spin_unlock_irqrestore(&adapter->lock, flags);
  169. }
  170. /**
  171. * atlx_tx_timeout - Respond to a Tx Hang
  172. * @netdev: network interface device structure
  173. */
  174. static void atlx_tx_timeout(struct net_device *netdev)
  175. {
  176. struct atlx_adapter *adapter = netdev_priv(netdev);
  177. /* Do the reset outside of interrupt context */
  178. schedule_work(&adapter->reset_dev_task);
  179. }
  180. /*
  181. * atlx_link_chg_task - deal with link change event Out of interrupt context
  182. */
  183. static void atlx_link_chg_task(struct work_struct *work)
  184. {
  185. struct atlx_adapter *adapter;
  186. unsigned long flags;
  187. adapter = container_of(work, struct atlx_adapter, link_chg_task);
  188. spin_lock_irqsave(&adapter->lock, flags);
  189. atlx_check_link(adapter);
  190. spin_unlock_irqrestore(&adapter->lock, flags);
  191. }
  192. static void __atlx_vlan_mode(netdev_features_t features, u32 *ctrl)
  193. {
  194. if (features & NETIF_F_HW_VLAN_CTAG_RX) {
  195. /* enable VLAN tag insert/strip */
  196. *ctrl |= MAC_CTRL_RMV_VLAN;
  197. } else {
  198. /* disable VLAN tag insert/strip */
  199. *ctrl &= ~MAC_CTRL_RMV_VLAN;
  200. }
  201. }
  202. static void atlx_vlan_mode(struct net_device *netdev,
  203. netdev_features_t features)
  204. {
  205. struct atlx_adapter *adapter = netdev_priv(netdev);
  206. unsigned long flags;
  207. u32 ctrl;
  208. spin_lock_irqsave(&adapter->lock, flags);
  209. /* atlx_irq_disable(adapter); FIXME: confirm/remove */
  210. ctrl = ioread32(adapter->hw.hw_addr + REG_MAC_CTRL);
  211. __atlx_vlan_mode(features, &ctrl);
  212. iowrite32(ctrl, adapter->hw.hw_addr + REG_MAC_CTRL);
  213. /* atlx_irq_enable(adapter); FIXME */
  214. spin_unlock_irqrestore(&adapter->lock, flags);
  215. }
  216. static void atlx_restore_vlan(struct atlx_adapter *adapter)
  217. {
  218. atlx_vlan_mode(adapter->netdev, adapter->netdev->features);
  219. }
  220. static netdev_features_t atlx_fix_features(struct net_device *netdev,
  221. netdev_features_t features)
  222. {
  223. /*
  224. * Since there is no support for separate rx/tx vlan accel
  225. * enable/disable make sure tx flag is always in same state as rx.
  226. */
  227. if (features & NETIF_F_HW_VLAN_CTAG_RX)
  228. features |= NETIF_F_HW_VLAN_CTAG_TX;
  229. else
  230. features &= ~NETIF_F_HW_VLAN_CTAG_TX;
  231. return features;
  232. }
  233. static int atlx_set_features(struct net_device *netdev,
  234. netdev_features_t features)
  235. {
  236. netdev_features_t changed = netdev->features ^ features;
  237. if (changed & NETIF_F_HW_VLAN_CTAG_RX)
  238. atlx_vlan_mode(netdev, features);
  239. return 0;
  240. }
  241. #endif /* ATLX_C */