atlx.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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. netdev->addr_assign_type &= ~NET_ADDR_RANDOM;
  78. atlx_set_mac_addr(&adapter->hw);
  79. return 0;
  80. }
  81. static void atlx_check_for_link(struct atlx_adapter *adapter)
  82. {
  83. struct net_device *netdev = adapter->netdev;
  84. u16 phy_data = 0;
  85. spin_lock(&adapter->lock);
  86. adapter->phy_timer_pending = false;
  87. atlx_read_phy_reg(&adapter->hw, MII_BMSR, &phy_data);
  88. atlx_read_phy_reg(&adapter->hw, MII_BMSR, &phy_data);
  89. spin_unlock(&adapter->lock);
  90. /* notify upper layer link down ASAP */
  91. if (!(phy_data & BMSR_LSTATUS)) {
  92. /* Link Down */
  93. if (netif_carrier_ok(netdev)) {
  94. /* old link state: Up */
  95. dev_info(&adapter->pdev->dev, "%s link is down\n",
  96. netdev->name);
  97. adapter->link_speed = SPEED_0;
  98. netif_carrier_off(netdev);
  99. }
  100. }
  101. schedule_work(&adapter->link_chg_task);
  102. }
  103. /*
  104. * atlx_set_multi - Multicast and Promiscuous mode set
  105. * @netdev: network interface device structure
  106. *
  107. * The set_multi entry point is called whenever the multicast address
  108. * list or the network interface flags are updated. This routine is
  109. * responsible for configuring the hardware for proper multicast,
  110. * promiscuous mode, and all-multi behavior.
  111. */
  112. static void atlx_set_multi(struct net_device *netdev)
  113. {
  114. struct atlx_adapter *adapter = netdev_priv(netdev);
  115. struct atlx_hw *hw = &adapter->hw;
  116. struct netdev_hw_addr *ha;
  117. u32 rctl;
  118. u32 hash_value;
  119. /* Check for Promiscuous and All Multicast modes */
  120. rctl = ioread32(hw->hw_addr + REG_MAC_CTRL);
  121. if (netdev->flags & IFF_PROMISC)
  122. rctl |= MAC_CTRL_PROMIS_EN;
  123. else if (netdev->flags & IFF_ALLMULTI) {
  124. rctl |= MAC_CTRL_MC_ALL_EN;
  125. rctl &= ~MAC_CTRL_PROMIS_EN;
  126. } else
  127. rctl &= ~(MAC_CTRL_PROMIS_EN | MAC_CTRL_MC_ALL_EN);
  128. iowrite32(rctl, hw->hw_addr + REG_MAC_CTRL);
  129. /* clear the old settings from the multicast hash table */
  130. iowrite32(0, hw->hw_addr + REG_RX_HASH_TABLE);
  131. iowrite32(0, (hw->hw_addr + REG_RX_HASH_TABLE) + (1 << 2));
  132. /* compute mc addresses' hash value ,and put it into hash table */
  133. netdev_for_each_mc_addr(ha, netdev) {
  134. hash_value = atlx_hash_mc_addr(hw, ha->addr);
  135. atlx_hash_set(hw, hash_value);
  136. }
  137. }
  138. /*
  139. * atlx_irq_enable - Enable default interrupt generation settings
  140. * @adapter: board private structure
  141. */
  142. static void atlx_irq_enable(struct atlx_adapter *adapter)
  143. {
  144. iowrite32(IMR_NORMAL_MASK, adapter->hw.hw_addr + REG_IMR);
  145. ioread32(adapter->hw.hw_addr + REG_IMR);
  146. }
  147. /*
  148. * atlx_irq_disable - Mask off interrupt generation on the NIC
  149. * @adapter: board private structure
  150. */
  151. static void atlx_irq_disable(struct atlx_adapter *adapter)
  152. {
  153. iowrite32(0, adapter->hw.hw_addr + REG_IMR);
  154. ioread32(adapter->hw.hw_addr + REG_IMR);
  155. synchronize_irq(adapter->pdev->irq);
  156. }
  157. static void atlx_clear_phy_int(struct atlx_adapter *adapter)
  158. {
  159. u16 phy_data;
  160. unsigned long flags;
  161. spin_lock_irqsave(&adapter->lock, flags);
  162. atlx_read_phy_reg(&adapter->hw, 19, &phy_data);
  163. spin_unlock_irqrestore(&adapter->lock, flags);
  164. }
  165. /*
  166. * atlx_tx_timeout - Respond to a Tx Hang
  167. * @netdev: network interface device structure
  168. */
  169. static void atlx_tx_timeout(struct net_device *netdev)
  170. {
  171. struct atlx_adapter *adapter = netdev_priv(netdev);
  172. /* Do the reset outside of interrupt context */
  173. schedule_work(&adapter->reset_dev_task);
  174. }
  175. /*
  176. * atlx_link_chg_task - deal with link change event Out of interrupt context
  177. */
  178. static void atlx_link_chg_task(struct work_struct *work)
  179. {
  180. struct atlx_adapter *adapter;
  181. unsigned long flags;
  182. adapter = container_of(work, struct atlx_adapter, link_chg_task);
  183. spin_lock_irqsave(&adapter->lock, flags);
  184. atlx_check_link(adapter);
  185. spin_unlock_irqrestore(&adapter->lock, flags);
  186. }
  187. static void __atlx_vlan_mode(netdev_features_t features, u32 *ctrl)
  188. {
  189. if (features & NETIF_F_HW_VLAN_RX) {
  190. /* enable VLAN tag insert/strip */
  191. *ctrl |= MAC_CTRL_RMV_VLAN;
  192. } else {
  193. /* disable VLAN tag insert/strip */
  194. *ctrl &= ~MAC_CTRL_RMV_VLAN;
  195. }
  196. }
  197. static void atlx_vlan_mode(struct net_device *netdev,
  198. netdev_features_t features)
  199. {
  200. struct atlx_adapter *adapter = netdev_priv(netdev);
  201. unsigned long flags;
  202. u32 ctrl;
  203. spin_lock_irqsave(&adapter->lock, flags);
  204. /* atlx_irq_disable(adapter); FIXME: confirm/remove */
  205. ctrl = ioread32(adapter->hw.hw_addr + REG_MAC_CTRL);
  206. __atlx_vlan_mode(features, &ctrl);
  207. iowrite32(ctrl, adapter->hw.hw_addr + REG_MAC_CTRL);
  208. /* atlx_irq_enable(adapter); FIXME */
  209. spin_unlock_irqrestore(&adapter->lock, flags);
  210. }
  211. static void atlx_restore_vlan(struct atlx_adapter *adapter)
  212. {
  213. atlx_vlan_mode(adapter->netdev, adapter->netdev->features);
  214. }
  215. static netdev_features_t atlx_fix_features(struct net_device *netdev,
  216. netdev_features_t features)
  217. {
  218. /*
  219. * Since there is no support for separate rx/tx vlan accel
  220. * enable/disable make sure tx flag is always in same state as rx.
  221. */
  222. if (features & NETIF_F_HW_VLAN_RX)
  223. features |= NETIF_F_HW_VLAN_TX;
  224. else
  225. features &= ~NETIF_F_HW_VLAN_TX;
  226. return features;
  227. }
  228. static int atlx_set_features(struct net_device *netdev,
  229. netdev_features_t features)
  230. {
  231. netdev_features_t changed = netdev->features ^ features;
  232. if (changed & NETIF_F_HW_VLAN_RX)
  233. atlx_vlan_mode(netdev, features);
  234. return 0;
  235. }
  236. #endif /* ATLX_C */