atlx.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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. /*
  245. * flash_vendor
  246. *
  247. * Valid Range: 0-2
  248. *
  249. * 0 - Atmel
  250. * 1 - SST
  251. * 2 - ST
  252. *
  253. * Default Value: 0
  254. */
  255. static int __devinitdata flash_vendor[ATL1_MAX_NIC+1] = ATL1_PARAM_INIT;
  256. static int num_flash_vendor;
  257. module_param_array_named(flash_vendor, flash_vendor, int, &num_flash_vendor, 0);
  258. MODULE_PARM_DESC(flash_vendor, "SPI flash vendor");
  259. #define DEFAULT_INT_MOD_CNT 100 /* 200us */
  260. #define MAX_INT_MOD_CNT 65000
  261. #define MIN_INT_MOD_CNT 50
  262. #define FLASH_VENDOR_DEFAULT 0
  263. #define FLASH_VENDOR_MIN 0
  264. #define FLASH_VENDOR_MAX 2
  265. struct atl1_option {
  266. enum { enable_option, range_option, list_option } type;
  267. char *name;
  268. char *err;
  269. int def;
  270. union {
  271. struct { /* range_option info */
  272. int min;
  273. int max;
  274. } r;
  275. struct { /* list_option info */
  276. int nr;
  277. struct atl1_opt_list {
  278. int i;
  279. char *str;
  280. } *p;
  281. } l;
  282. } arg;
  283. };
  284. static int __devinit atl1_validate_option(int *value, struct atl1_option *opt,
  285. struct pci_dev *pdev)
  286. {
  287. if (*value == OPTION_UNSET) {
  288. *value = opt->def;
  289. return 0;
  290. }
  291. switch (opt->type) {
  292. case enable_option:
  293. switch (*value) {
  294. case OPTION_ENABLED:
  295. dev_info(&pdev->dev, "%s enabled\n", opt->name);
  296. return 0;
  297. case OPTION_DISABLED:
  298. dev_info(&pdev->dev, "%s disabled\n", opt->name);
  299. return 0;
  300. }
  301. break;
  302. case range_option:
  303. if (*value >= opt->arg.r.min && *value <= opt->arg.r.max) {
  304. dev_info(&pdev->dev, "%s set to %i\n", opt->name,
  305. *value);
  306. return 0;
  307. }
  308. break;
  309. case list_option:{
  310. int i;
  311. struct atl1_opt_list *ent;
  312. for (i = 0; i < opt->arg.l.nr; i++) {
  313. ent = &opt->arg.l.p[i];
  314. if (*value == ent->i) {
  315. if (ent->str[0] != '\0')
  316. dev_info(&pdev->dev, "%s\n",
  317. ent->str);
  318. return 0;
  319. }
  320. }
  321. }
  322. break;
  323. default:
  324. break;
  325. }
  326. dev_info(&pdev->dev, "invalid %s specified (%i) %s\n",
  327. opt->name, *value, opt->err);
  328. *value = opt->def;
  329. return -1;
  330. }
  331. /*
  332. * atl1_check_options - Range Checking for Command Line Parameters
  333. * @adapter: board private structure
  334. *
  335. * This routine checks all command line parameters for valid user
  336. * input. If an invalid value is given, or if no user specified
  337. * value exists, a default value is used. The final value is stored
  338. * in a variable in the adapter structure.
  339. */
  340. void __devinit atl1_check_options(struct atl1_adapter *adapter)
  341. {
  342. struct pci_dev *pdev = adapter->pdev;
  343. int bd = adapter->bd_number;
  344. if (bd >= ATL1_MAX_NIC) {
  345. dev_notice(&pdev->dev, "no configuration for board#%i\n", bd);
  346. dev_notice(&pdev->dev, "using defaults for all values\n");
  347. }
  348. { /* Interrupt Moderate Timer */
  349. struct atl1_option opt = {
  350. .type = range_option,
  351. .name = "Interrupt Moderator Timer",
  352. .err = "using default of "
  353. __MODULE_STRING(DEFAULT_INT_MOD_CNT),
  354. .def = DEFAULT_INT_MOD_CNT,
  355. .arg = {.r = {.min = MIN_INT_MOD_CNT,
  356. .max = MAX_INT_MOD_CNT} }
  357. };
  358. int val;
  359. if (num_int_mod_timer > bd) {
  360. val = int_mod_timer[bd];
  361. atl1_validate_option(&val, &opt, pdev);
  362. adapter->imt = (u16) val;
  363. } else
  364. adapter->imt = (u16) (opt.def);
  365. }
  366. { /* Flash Vendor */
  367. struct atl1_option opt = {
  368. .type = range_option,
  369. .name = "SPI Flash Vendor",
  370. .err = "using default of "
  371. __MODULE_STRING(FLASH_VENDOR_DEFAULT),
  372. .def = DEFAULT_INT_MOD_CNT,
  373. .arg = {.r = {.min = FLASH_VENDOR_MIN,
  374. .max = FLASH_VENDOR_MAX} }
  375. };
  376. int val;
  377. if (num_flash_vendor > bd) {
  378. val = flash_vendor[bd];
  379. atl1_validate_option(&val, &opt, pdev);
  380. adapter->hw.flash_vendor = (u8) val;
  381. } else
  382. adapter->hw.flash_vendor = (u8) (opt.def);
  383. }
  384. }
  385. #endif /* ATLX_C */