atlx.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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 struct atlx_spi_flash_dev flash_table[] = {
  41. /* MFR_NAME WRSR READ PRGM WREN WRDI RDSR RDID SEC_ERS CHIP_ERS */
  42. {"Atmel", 0x00, 0x03, 0x02, 0x06, 0x04, 0x05, 0x15, 0x52, 0x62},
  43. {"SST", 0x01, 0x03, 0x02, 0x06, 0x04, 0x05, 0x90, 0x20, 0x60},
  44. {"ST", 0x01, 0x03, 0x02, 0x06, 0x04, 0x05, 0xAB, 0xD8, 0xC7},
  45. };
  46. static int atlx_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
  47. {
  48. switch (cmd) {
  49. case SIOCGMIIPHY:
  50. case SIOCGMIIREG:
  51. case SIOCSMIIREG:
  52. return atlx_mii_ioctl(netdev, ifr, cmd);
  53. default:
  54. return -EOPNOTSUPP;
  55. }
  56. }
  57. /*
  58. * atlx_set_mac - Change the Ethernet Address of the NIC
  59. * @netdev: network interface device structure
  60. * @p: pointer to an address structure
  61. *
  62. * Returns 0 on success, negative on failure
  63. */
  64. static int atlx_set_mac(struct net_device *netdev, void *p)
  65. {
  66. struct atlx_adapter *adapter = netdev_priv(netdev);
  67. struct sockaddr *addr = p;
  68. if (netif_running(netdev))
  69. return -EBUSY;
  70. if (!is_valid_ether_addr(addr->sa_data))
  71. return -EADDRNOTAVAIL;
  72. memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
  73. memcpy(adapter->hw.mac_addr, addr->sa_data, netdev->addr_len);
  74. atlx_set_mac_addr(&adapter->hw);
  75. return 0;
  76. }
  77. static void atlx_check_for_link(struct atlx_adapter *adapter)
  78. {
  79. struct net_device *netdev = adapter->netdev;
  80. u16 phy_data = 0;
  81. spin_lock(&adapter->lock);
  82. adapter->phy_timer_pending = false;
  83. atlx_read_phy_reg(&adapter->hw, MII_BMSR, &phy_data);
  84. atlx_read_phy_reg(&adapter->hw, MII_BMSR, &phy_data);
  85. spin_unlock(&adapter->lock);
  86. /* notify upper layer link down ASAP */
  87. if (!(phy_data & BMSR_LSTATUS)) {
  88. /* Link Down */
  89. if (netif_carrier_ok(netdev)) {
  90. /* old link state: Up */
  91. dev_info(&adapter->pdev->dev, "%s link is down\n",
  92. netdev->name);
  93. adapter->link_speed = SPEED_0;
  94. netif_carrier_off(netdev);
  95. }
  96. }
  97. schedule_work(&adapter->link_chg_task);
  98. }
  99. /*
  100. * atlx_set_multi - Multicast and Promiscuous mode set
  101. * @netdev: network interface device structure
  102. *
  103. * The set_multi entry point is called whenever the multicast address
  104. * list or the network interface flags are updated. This routine is
  105. * responsible for configuring the hardware for proper multicast,
  106. * promiscuous mode, and all-multi behavior.
  107. */
  108. static void atlx_set_multi(struct net_device *netdev)
  109. {
  110. struct atlx_adapter *adapter = netdev_priv(netdev);
  111. struct atlx_hw *hw = &adapter->hw;
  112. struct dev_mc_list *mc_ptr;
  113. u32 rctl;
  114. u32 hash_value;
  115. /* Check for Promiscuous and All Multicast modes */
  116. rctl = ioread32(hw->hw_addr + REG_MAC_CTRL);
  117. if (netdev->flags & IFF_PROMISC)
  118. rctl |= MAC_CTRL_PROMIS_EN;
  119. else if (netdev->flags & IFF_ALLMULTI) {
  120. rctl |= MAC_CTRL_MC_ALL_EN;
  121. rctl &= ~MAC_CTRL_PROMIS_EN;
  122. } else
  123. rctl &= ~(MAC_CTRL_PROMIS_EN | MAC_CTRL_MC_ALL_EN);
  124. iowrite32(rctl, hw->hw_addr + REG_MAC_CTRL);
  125. /* clear the old settings from the multicast hash table */
  126. iowrite32(0, hw->hw_addr + REG_RX_HASH_TABLE);
  127. iowrite32(0, (hw->hw_addr + REG_RX_HASH_TABLE) + (1 << 2));
  128. /* compute mc addresses' hash value ,and put it into hash table */
  129. for (mc_ptr = netdev->mc_list; mc_ptr; mc_ptr = mc_ptr->next) {
  130. hash_value = atlx_hash_mc_addr(hw, mc_ptr->dmi_addr);
  131. atlx_hash_set(hw, hash_value);
  132. }
  133. }
  134. /*
  135. * atlx_irq_enable - Enable default interrupt generation settings
  136. * @adapter: board private structure
  137. */
  138. static void atlx_irq_enable(struct atlx_adapter *adapter)
  139. {
  140. iowrite32(IMR_NORMAL_MASK, adapter->hw.hw_addr + REG_IMR);
  141. ioread32(adapter->hw.hw_addr + REG_IMR);
  142. }
  143. /*
  144. * atlx_irq_disable - Mask off interrupt generation on the NIC
  145. * @adapter: board private structure
  146. */
  147. static void atlx_irq_disable(struct atlx_adapter *adapter)
  148. {
  149. iowrite32(0, adapter->hw.hw_addr + REG_IMR);
  150. ioread32(adapter->hw.hw_addr + REG_IMR);
  151. synchronize_irq(adapter->pdev->irq);
  152. }
  153. static void atlx_clear_phy_int(struct atlx_adapter *adapter)
  154. {
  155. u16 phy_data;
  156. unsigned long flags;
  157. spin_lock_irqsave(&adapter->lock, flags);
  158. atlx_read_phy_reg(&adapter->hw, 19, &phy_data);
  159. spin_unlock_irqrestore(&adapter->lock, flags);
  160. }
  161. /*
  162. * atlx_tx_timeout - Respond to a Tx Hang
  163. * @netdev: network interface device structure
  164. */
  165. static void atlx_tx_timeout(struct net_device *netdev)
  166. {
  167. struct atlx_adapter *adapter = netdev_priv(netdev);
  168. /* Do the reset outside of interrupt context */
  169. schedule_work(&adapter->tx_timeout_task);
  170. }
  171. /*
  172. * atlx_link_chg_task - deal with link change event Out of interrupt context
  173. */
  174. static void atlx_link_chg_task(struct work_struct *work)
  175. {
  176. struct atlx_adapter *adapter;
  177. unsigned long flags;
  178. adapter = container_of(work, struct atlx_adapter, link_chg_task);
  179. spin_lock_irqsave(&adapter->lock, flags);
  180. atlx_check_link(adapter);
  181. spin_unlock_irqrestore(&adapter->lock, flags);
  182. }
  183. static void atlx_vlan_rx_register(struct net_device *netdev,
  184. struct vlan_group *grp)
  185. {
  186. struct atlx_adapter *adapter = netdev_priv(netdev);
  187. unsigned long flags;
  188. u32 ctrl;
  189. spin_lock_irqsave(&adapter->lock, flags);
  190. /* atlx_irq_disable(adapter); FIXME: confirm/remove */
  191. adapter->vlgrp = grp;
  192. if (grp) {
  193. /* enable VLAN tag insert/strip */
  194. ctrl = ioread32(adapter->hw.hw_addr + REG_MAC_CTRL);
  195. ctrl |= MAC_CTRL_RMV_VLAN;
  196. iowrite32(ctrl, adapter->hw.hw_addr + REG_MAC_CTRL);
  197. } else {
  198. /* disable VLAN tag insert/strip */
  199. ctrl = ioread32(adapter->hw.hw_addr + REG_MAC_CTRL);
  200. ctrl &= ~MAC_CTRL_RMV_VLAN;
  201. iowrite32(ctrl, adapter->hw.hw_addr + REG_MAC_CTRL);
  202. }
  203. /* atlx_irq_enable(adapter); FIXME */
  204. spin_unlock_irqrestore(&adapter->lock, flags);
  205. }
  206. static void atlx_restore_vlan(struct atlx_adapter *adapter)
  207. {
  208. atlx_vlan_rx_register(adapter->netdev, adapter->vlgrp);
  209. }
  210. #endif /* ATLX_C */