atlx.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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 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. netif_stop_queue(netdev);
  96. }
  97. }
  98. schedule_work(&adapter->link_chg_task);
  99. }
  100. /*
  101. * atlx_set_multi - Multicast and Promiscuous mode set
  102. * @netdev: network interface device structure
  103. *
  104. * The set_multi entry point is called whenever the multicast address
  105. * list or the network interface flags are updated. This routine is
  106. * responsible for configuring the hardware for proper multicast,
  107. * promiscuous mode, and all-multi behavior.
  108. */
  109. static void atlx_set_multi(struct net_device *netdev)
  110. {
  111. struct atlx_adapter *adapter = netdev_priv(netdev);
  112. struct atlx_hw *hw = &adapter->hw;
  113. struct dev_mc_list *mc_ptr;
  114. u32 rctl;
  115. u32 hash_value;
  116. /* Check for Promiscuous and All Multicast modes */
  117. rctl = ioread32(hw->hw_addr + REG_MAC_CTRL);
  118. if (netdev->flags & IFF_PROMISC)
  119. rctl |= MAC_CTRL_PROMIS_EN;
  120. else if (netdev->flags & IFF_ALLMULTI) {
  121. rctl |= MAC_CTRL_MC_ALL_EN;
  122. rctl &= ~MAC_CTRL_PROMIS_EN;
  123. } else
  124. rctl &= ~(MAC_CTRL_PROMIS_EN | MAC_CTRL_MC_ALL_EN);
  125. iowrite32(rctl, hw->hw_addr + REG_MAC_CTRL);
  126. /* clear the old settings from the multicast hash table */
  127. iowrite32(0, hw->hw_addr + REG_RX_HASH_TABLE);
  128. iowrite32(0, (hw->hw_addr + REG_RX_HASH_TABLE) + (1 << 2));
  129. /* compute mc addresses' hash value ,and put it into hash table */
  130. for (mc_ptr = netdev->mc_list; mc_ptr; mc_ptr = mc_ptr->next) {
  131. hash_value = atlx_hash_mc_addr(hw, mc_ptr->dmi_addr);
  132. atlx_hash_set(hw, hash_value);
  133. }
  134. }
  135. /*
  136. * atlx_irq_enable - Enable default interrupt generation settings
  137. * @adapter: board private structure
  138. */
  139. static void atlx_irq_enable(struct atlx_adapter *adapter)
  140. {
  141. iowrite32(IMR_NORMAL_MASK, adapter->hw.hw_addr + REG_IMR);
  142. ioread32(adapter->hw.hw_addr + REG_IMR);
  143. }
  144. /*
  145. * atlx_irq_disable - Mask off interrupt generation on the NIC
  146. * @adapter: board private structure
  147. */
  148. static void atlx_irq_disable(struct atlx_adapter *adapter)
  149. {
  150. iowrite32(0, adapter->hw.hw_addr + REG_IMR);
  151. ioread32(adapter->hw.hw_addr + REG_IMR);
  152. synchronize_irq(adapter->pdev->irq);
  153. }
  154. static void atlx_clear_phy_int(struct atlx_adapter *adapter)
  155. {
  156. u16 phy_data;
  157. unsigned long flags;
  158. spin_lock_irqsave(&adapter->lock, flags);
  159. atlx_read_phy_reg(&adapter->hw, 19, &phy_data);
  160. spin_unlock_irqrestore(&adapter->lock, flags);
  161. }
  162. /*
  163. * atlx_get_stats - Get System Network Statistics
  164. * @netdev: network interface device structure
  165. *
  166. * Returns the address of the device statistics structure.
  167. * The statistics are actually updated from the timer callback.
  168. */
  169. static struct net_device_stats *atlx_get_stats(struct net_device *netdev)
  170. {
  171. struct atlx_adapter *adapter = netdev_priv(netdev);
  172. return &adapter->net_stats;
  173. }
  174. /*
  175. * atlx_tx_timeout - Respond to a Tx Hang
  176. * @netdev: network interface device structure
  177. */
  178. static void atlx_tx_timeout(struct net_device *netdev)
  179. {
  180. struct atlx_adapter *adapter = netdev_priv(netdev);
  181. /* Do the reset outside of interrupt context */
  182. schedule_work(&adapter->tx_timeout_task);
  183. }
  184. /*
  185. * atlx_link_chg_task - deal with link change event Out of interrupt context
  186. */
  187. static void atlx_link_chg_task(struct work_struct *work)
  188. {
  189. struct atlx_adapter *adapter;
  190. unsigned long flags;
  191. adapter = container_of(work, struct atlx_adapter, link_chg_task);
  192. spin_lock_irqsave(&adapter->lock, flags);
  193. atlx_check_link(adapter);
  194. spin_unlock_irqrestore(&adapter->lock, flags);
  195. }
  196. static void atlx_vlan_rx_register(struct net_device *netdev,
  197. struct vlan_group *grp)
  198. {
  199. struct atlx_adapter *adapter = netdev_priv(netdev);
  200. unsigned long flags;
  201. u32 ctrl;
  202. spin_lock_irqsave(&adapter->lock, flags);
  203. /* atlx_irq_disable(adapter); FIXME: confirm/remove */
  204. adapter->vlgrp = grp;
  205. if (grp) {
  206. /* enable VLAN tag insert/strip */
  207. ctrl = ioread32(adapter->hw.hw_addr + REG_MAC_CTRL);
  208. ctrl |= MAC_CTRL_RMV_VLAN;
  209. iowrite32(ctrl, adapter->hw.hw_addr + REG_MAC_CTRL);
  210. } else {
  211. /* disable VLAN tag insert/strip */
  212. ctrl = ioread32(adapter->hw.hw_addr + REG_MAC_CTRL);
  213. ctrl &= ~MAC_CTRL_RMV_VLAN;
  214. iowrite32(ctrl, adapter->hw.hw_addr + REG_MAC_CTRL);
  215. }
  216. /* atlx_irq_enable(adapter); FIXME */
  217. spin_unlock_irqrestore(&adapter->lock, flags);
  218. }
  219. static void atlx_restore_vlan(struct atlx_adapter *adapter)
  220. {
  221. atlx_vlan_rx_register(adapter->netdev, adapter->vlgrp);
  222. }
  223. /*
  224. * This is the only thing that needs to be changed to adjust the
  225. * maximum number of ports that the driver can manage.
  226. */
  227. #define ATL1_MAX_NIC 4
  228. #define OPTION_UNSET -1
  229. #define OPTION_DISABLED 0
  230. #define OPTION_ENABLED 1
  231. #define ATL1_PARAM_INIT { [0 ... ATL1_MAX_NIC] = OPTION_UNSET }
  232. /*
  233. * Interrupt Moderate Timer in units of 2 us
  234. *
  235. * Valid Range: 10-65535
  236. *
  237. * Default Value: 100 (200us)
  238. */
  239. static int __devinitdata int_mod_timer[ATL1_MAX_NIC+1] = ATL1_PARAM_INIT;
  240. static int num_int_mod_timer;
  241. module_param_array_named(int_mod_timer, int_mod_timer, int,
  242. &num_int_mod_timer, 0);
  243. MODULE_PARM_DESC(int_mod_timer, "Interrupt moderator timer");
  244. #define DEFAULT_INT_MOD_CNT 100 /* 200us */
  245. #define MAX_INT_MOD_CNT 65000
  246. #define MIN_INT_MOD_CNT 50
  247. struct atl1_option {
  248. enum { enable_option, range_option, list_option } type;
  249. char *name;
  250. char *err;
  251. int def;
  252. union {
  253. struct { /* range_option info */
  254. int min;
  255. int max;
  256. } r;
  257. struct { /* list_option info */
  258. int nr;
  259. struct atl1_opt_list {
  260. int i;
  261. char *str;
  262. } *p;
  263. } l;
  264. } arg;
  265. };
  266. static int __devinit atl1_validate_option(int *value, struct atl1_option *opt,
  267. struct pci_dev *pdev)
  268. {
  269. if (*value == OPTION_UNSET) {
  270. *value = opt->def;
  271. return 0;
  272. }
  273. switch (opt->type) {
  274. case enable_option:
  275. switch (*value) {
  276. case OPTION_ENABLED:
  277. dev_info(&pdev->dev, "%s enabled\n", opt->name);
  278. return 0;
  279. case OPTION_DISABLED:
  280. dev_info(&pdev->dev, "%s disabled\n", opt->name);
  281. return 0;
  282. }
  283. break;
  284. case range_option:
  285. if (*value >= opt->arg.r.min && *value <= opt->arg.r.max) {
  286. dev_info(&pdev->dev, "%s set to %i\n", opt->name,
  287. *value);
  288. return 0;
  289. }
  290. break;
  291. case list_option:{
  292. int i;
  293. struct atl1_opt_list *ent;
  294. for (i = 0; i < opt->arg.l.nr; i++) {
  295. ent = &opt->arg.l.p[i];
  296. if (*value == ent->i) {
  297. if (ent->str[0] != '\0')
  298. dev_info(&pdev->dev, "%s\n",
  299. ent->str);
  300. return 0;
  301. }
  302. }
  303. }
  304. break;
  305. default:
  306. break;
  307. }
  308. dev_info(&pdev->dev, "invalid %s specified (%i) %s\n",
  309. opt->name, *value, opt->err);
  310. *value = opt->def;
  311. return -1;
  312. }
  313. /*
  314. * atl1_check_options - Range Checking for Command Line Parameters
  315. * @adapter: board private structure
  316. *
  317. * This routine checks all command line parameters for valid user
  318. * input. If an invalid value is given, or if no user specified
  319. * value exists, a default value is used. The final value is stored
  320. * in a variable in the adapter structure.
  321. */
  322. void __devinit atl1_check_options(struct atl1_adapter *adapter)
  323. {
  324. struct pci_dev *pdev = adapter->pdev;
  325. int bd = adapter->bd_number;
  326. if (bd >= ATL1_MAX_NIC) {
  327. dev_notice(&pdev->dev, "no configuration for board#%i\n", bd);
  328. dev_notice(&pdev->dev, "using defaults for all values\n");
  329. }
  330. { /* Interrupt Moderate Timer */
  331. struct atl1_option opt = {
  332. .type = range_option,
  333. .name = "Interrupt Moderator Timer",
  334. .err = "using default of "
  335. __MODULE_STRING(DEFAULT_INT_MOD_CNT),
  336. .def = DEFAULT_INT_MOD_CNT,
  337. .arg = {.r = {.min = MIN_INT_MOD_CNT,
  338. .max = MAX_INT_MOD_CNT} }
  339. };
  340. int val;
  341. if (num_int_mod_timer > bd) {
  342. val = int_mod_timer[bd];
  343. atl1_validate_option(&val, &opt, pdev);
  344. adapter->imt = (u16) val;
  345. } else
  346. adapter->imt = (u16) (opt.def);
  347. }
  348. }
  349. #endif /* ATLX_C */