ixgb_main.c 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176
  1. /*******************************************************************************
  2. Copyright(c) 1999 - 2005 Intel Corporation. All rights reserved.
  3. This program is free software; you can redistribute it and/or modify it
  4. under the terms of the GNU General Public License as published by the Free
  5. Software Foundation; either version 2 of the License, or (at your option)
  6. any later version.
  7. This program is distributed in the hope that it will be useful, but WITHOUT
  8. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  9. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  10. more details.
  11. You should have received a copy of the GNU General Public License along with
  12. this program; if not, write to the Free Software Foundation, Inc., 59
  13. Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  14. The full GNU General Public License is included in this distribution in the
  15. file called LICENSE.
  16. Contact Information:
  17. Linux NICS <linux.nics@intel.com>
  18. Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  19. *******************************************************************************/
  20. #include "ixgb.h"
  21. /* Change Log
  22. * 1.0.96 04/19/05
  23. * - Make needlessly global code static -- bunk@stusta.de
  24. * - ethtool cleanup -- shemminger@osdl.org
  25. * - Support for MODULE_VERSION -- linville@tuxdriver.com
  26. * - add skb_header_cloned check to the tso path -- herbert@apana.org.au
  27. * 1.0.88 01/05/05
  28. * - include fix to the condition that determines when to quit NAPI - Robert Olsson
  29. * - use netif_poll_{disable/enable} to synchronize between NAPI and i/f up/down
  30. * 1.0.84 10/26/04
  31. * - reset buffer_info->dma in Tx resource cleanup logic
  32. * 1.0.83 10/12/04
  33. * - sparse cleanup - shemminger@osdl.org
  34. * - fix tx resource cleanup logic
  35. */
  36. char ixgb_driver_name[] = "ixgb";
  37. static char ixgb_driver_string[] = "Intel(R) PRO/10GbE Network Driver";
  38. #ifndef CONFIG_IXGB_NAPI
  39. #define DRIVERNAPI
  40. #else
  41. #define DRIVERNAPI "-NAPI"
  42. #endif
  43. #define DRV_VERSION "1.0.100-k2"DRIVERNAPI
  44. char ixgb_driver_version[] = DRV_VERSION;
  45. static char ixgb_copyright[] = "Copyright (c) 1999-2005 Intel Corporation.";
  46. /* ixgb_pci_tbl - PCI Device ID Table
  47. *
  48. * Wildcard entries (PCI_ANY_ID) should come last
  49. * Last entry must be all 0s
  50. *
  51. * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
  52. * Class, Class Mask, private data (not used) }
  53. */
  54. static struct pci_device_id ixgb_pci_tbl[] = {
  55. {INTEL_VENDOR_ID, IXGB_DEVICE_ID_82597EX,
  56. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
  57. {INTEL_VENDOR_ID, IXGB_DEVICE_ID_82597EX_CX4,
  58. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
  59. {INTEL_VENDOR_ID, IXGB_DEVICE_ID_82597EX_SR,
  60. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
  61. {INTEL_VENDOR_ID, IXGB_DEVICE_ID_82597EX_LR,
  62. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
  63. /* required last entry */
  64. {0,}
  65. };
  66. MODULE_DEVICE_TABLE(pci, ixgb_pci_tbl);
  67. /* Local Function Prototypes */
  68. int ixgb_up(struct ixgb_adapter *adapter);
  69. void ixgb_down(struct ixgb_adapter *adapter, boolean_t kill_watchdog);
  70. void ixgb_reset(struct ixgb_adapter *adapter);
  71. int ixgb_setup_tx_resources(struct ixgb_adapter *adapter);
  72. int ixgb_setup_rx_resources(struct ixgb_adapter *adapter);
  73. void ixgb_free_tx_resources(struct ixgb_adapter *adapter);
  74. void ixgb_free_rx_resources(struct ixgb_adapter *adapter);
  75. void ixgb_update_stats(struct ixgb_adapter *adapter);
  76. static int ixgb_init_module(void);
  77. static void ixgb_exit_module(void);
  78. static int ixgb_probe(struct pci_dev *pdev, const struct pci_device_id *ent);
  79. static void __devexit ixgb_remove(struct pci_dev *pdev);
  80. static int ixgb_sw_init(struct ixgb_adapter *adapter);
  81. static int ixgb_open(struct net_device *netdev);
  82. static int ixgb_close(struct net_device *netdev);
  83. static void ixgb_configure_tx(struct ixgb_adapter *adapter);
  84. static void ixgb_configure_rx(struct ixgb_adapter *adapter);
  85. static void ixgb_setup_rctl(struct ixgb_adapter *adapter);
  86. static void ixgb_clean_tx_ring(struct ixgb_adapter *adapter);
  87. static void ixgb_clean_rx_ring(struct ixgb_adapter *adapter);
  88. static void ixgb_set_multi(struct net_device *netdev);
  89. static void ixgb_watchdog(unsigned long data);
  90. static int ixgb_xmit_frame(struct sk_buff *skb, struct net_device *netdev);
  91. static struct net_device_stats *ixgb_get_stats(struct net_device *netdev);
  92. static int ixgb_change_mtu(struct net_device *netdev, int new_mtu);
  93. static int ixgb_set_mac(struct net_device *netdev, void *p);
  94. static irqreturn_t ixgb_intr(int irq, void *data, struct pt_regs *regs);
  95. static boolean_t ixgb_clean_tx_irq(struct ixgb_adapter *adapter);
  96. #ifdef CONFIG_IXGB_NAPI
  97. static int ixgb_clean(struct net_device *netdev, int *budget);
  98. static boolean_t ixgb_clean_rx_irq(struct ixgb_adapter *adapter,
  99. int *work_done, int work_to_do);
  100. #else
  101. static boolean_t ixgb_clean_rx_irq(struct ixgb_adapter *adapter);
  102. #endif
  103. static void ixgb_alloc_rx_buffers(struct ixgb_adapter *adapter);
  104. void ixgb_set_ethtool_ops(struct net_device *netdev);
  105. static void ixgb_tx_timeout(struct net_device *dev);
  106. static void ixgb_tx_timeout_task(struct net_device *dev);
  107. static void ixgb_vlan_rx_register(struct net_device *netdev,
  108. struct vlan_group *grp);
  109. static void ixgb_vlan_rx_add_vid(struct net_device *netdev, uint16_t vid);
  110. static void ixgb_vlan_rx_kill_vid(struct net_device *netdev, uint16_t vid);
  111. static void ixgb_restore_vlan(struct ixgb_adapter *adapter);
  112. #ifdef CONFIG_NET_POLL_CONTROLLER
  113. /* for netdump / net console */
  114. static void ixgb_netpoll(struct net_device *dev);
  115. #endif
  116. /* Exported from other modules */
  117. extern void ixgb_check_options(struct ixgb_adapter *adapter);
  118. static struct pci_driver ixgb_driver = {
  119. .name = ixgb_driver_name,
  120. .id_table = ixgb_pci_tbl,
  121. .probe = ixgb_probe,
  122. .remove = __devexit_p(ixgb_remove),
  123. };
  124. MODULE_AUTHOR("Intel Corporation, <linux.nics@intel.com>");
  125. MODULE_DESCRIPTION("Intel(R) PRO/10GbE Network Driver");
  126. MODULE_LICENSE("GPL");
  127. MODULE_VERSION(DRV_VERSION);
  128. /* some defines for controlling descriptor fetches in h/w */
  129. #define RXDCTL_WTHRESH_DEFAULT 16 /* chip writes back at this many or RXT0 */
  130. #define RXDCTL_PTHRESH_DEFAULT 0 /* chip considers prefech below
  131. * this */
  132. #define RXDCTL_HTHRESH_DEFAULT 0 /* chip will only prefetch if tail
  133. * is pushed this many descriptors
  134. * from head */
  135. /**
  136. * ixgb_init_module - Driver Registration Routine
  137. *
  138. * ixgb_init_module is the first routine called when the driver is
  139. * loaded. All it does is register with the PCI subsystem.
  140. **/
  141. static int __init
  142. ixgb_init_module(void)
  143. {
  144. printk(KERN_INFO "%s - version %s\n",
  145. ixgb_driver_string, ixgb_driver_version);
  146. printk(KERN_INFO "%s\n", ixgb_copyright);
  147. return pci_module_init(&ixgb_driver);
  148. }
  149. module_init(ixgb_init_module);
  150. /**
  151. * ixgb_exit_module - Driver Exit Cleanup Routine
  152. *
  153. * ixgb_exit_module is called just before the driver is removed
  154. * from memory.
  155. **/
  156. static void __exit
  157. ixgb_exit_module(void)
  158. {
  159. pci_unregister_driver(&ixgb_driver);
  160. }
  161. module_exit(ixgb_exit_module);
  162. /**
  163. * ixgb_irq_disable - Mask off interrupt generation on the NIC
  164. * @adapter: board private structure
  165. **/
  166. static inline void
  167. ixgb_irq_disable(struct ixgb_adapter *adapter)
  168. {
  169. atomic_inc(&adapter->irq_sem);
  170. IXGB_WRITE_REG(&adapter->hw, IMC, ~0);
  171. IXGB_WRITE_FLUSH(&adapter->hw);
  172. synchronize_irq(adapter->pdev->irq);
  173. }
  174. /**
  175. * ixgb_irq_enable - Enable default interrupt generation settings
  176. * @adapter: board private structure
  177. **/
  178. static inline void
  179. ixgb_irq_enable(struct ixgb_adapter *adapter)
  180. {
  181. if(atomic_dec_and_test(&adapter->irq_sem)) {
  182. IXGB_WRITE_REG(&adapter->hw, IMS,
  183. IXGB_INT_RXT0 | IXGB_INT_RXDMT0 | IXGB_INT_TXDW |
  184. IXGB_INT_LSC);
  185. IXGB_WRITE_FLUSH(&adapter->hw);
  186. }
  187. }
  188. int
  189. ixgb_up(struct ixgb_adapter *adapter)
  190. {
  191. struct net_device *netdev = adapter->netdev;
  192. int err;
  193. int max_frame = netdev->mtu + ENET_HEADER_SIZE + ENET_FCS_LENGTH;
  194. struct ixgb_hw *hw = &adapter->hw;
  195. /* hardware has been reset, we need to reload some things */
  196. ixgb_set_multi(netdev);
  197. ixgb_restore_vlan(adapter);
  198. ixgb_configure_tx(adapter);
  199. ixgb_setup_rctl(adapter);
  200. ixgb_configure_rx(adapter);
  201. ixgb_alloc_rx_buffers(adapter);
  202. #ifdef CONFIG_PCI_MSI
  203. {
  204. boolean_t pcix = (IXGB_READ_REG(&adapter->hw, STATUS) &
  205. IXGB_STATUS_PCIX_MODE) ? TRUE : FALSE;
  206. adapter->have_msi = TRUE;
  207. if (!pcix)
  208. adapter->have_msi = FALSE;
  209. else if((err = pci_enable_msi(adapter->pdev))) {
  210. printk (KERN_ERR
  211. "Unable to allocate MSI interrupt Error: %d\n", err);
  212. adapter->have_msi = FALSE;
  213. /* proceed to try to request regular interrupt */
  214. }
  215. }
  216. #endif
  217. if((err = request_irq(adapter->pdev->irq, &ixgb_intr,
  218. SA_SHIRQ | SA_SAMPLE_RANDOM,
  219. netdev->name, netdev)))
  220. return err;
  221. /* disable interrupts and get the hardware into a known state */
  222. IXGB_WRITE_REG(&adapter->hw, IMC, 0xffffffff);
  223. if((hw->max_frame_size != max_frame) ||
  224. (hw->max_frame_size !=
  225. (IXGB_READ_REG(hw, MFS) >> IXGB_MFS_SHIFT))) {
  226. hw->max_frame_size = max_frame;
  227. IXGB_WRITE_REG(hw, MFS, hw->max_frame_size << IXGB_MFS_SHIFT);
  228. if(hw->max_frame_size >
  229. IXGB_MAX_ENET_FRAME_SIZE_WITHOUT_FCS + ENET_FCS_LENGTH) {
  230. uint32_t ctrl0 = IXGB_READ_REG(hw, CTRL0);
  231. if(!(ctrl0 & IXGB_CTRL0_JFE)) {
  232. ctrl0 |= IXGB_CTRL0_JFE;
  233. IXGB_WRITE_REG(hw, CTRL0, ctrl0);
  234. }
  235. }
  236. }
  237. mod_timer(&adapter->watchdog_timer, jiffies);
  238. ixgb_irq_enable(adapter);
  239. #ifdef CONFIG_IXGB_NAPI
  240. netif_poll_enable(netdev);
  241. #endif
  242. return 0;
  243. }
  244. void
  245. ixgb_down(struct ixgb_adapter *adapter, boolean_t kill_watchdog)
  246. {
  247. struct net_device *netdev = adapter->netdev;
  248. ixgb_irq_disable(adapter);
  249. free_irq(adapter->pdev->irq, netdev);
  250. #ifdef CONFIG_PCI_MSI
  251. if(adapter->have_msi == TRUE)
  252. pci_disable_msi(adapter->pdev);
  253. #endif
  254. if(kill_watchdog)
  255. del_timer_sync(&adapter->watchdog_timer);
  256. #ifdef CONFIG_IXGB_NAPI
  257. netif_poll_disable(netdev);
  258. #endif
  259. adapter->link_speed = 0;
  260. adapter->link_duplex = 0;
  261. netif_carrier_off(netdev);
  262. netif_stop_queue(netdev);
  263. ixgb_reset(adapter);
  264. ixgb_clean_tx_ring(adapter);
  265. ixgb_clean_rx_ring(adapter);
  266. }
  267. void
  268. ixgb_reset(struct ixgb_adapter *adapter)
  269. {
  270. ixgb_adapter_stop(&adapter->hw);
  271. if(!ixgb_init_hw(&adapter->hw))
  272. IXGB_DBG("ixgb_init_hw failed.\n");
  273. }
  274. /**
  275. * ixgb_probe - Device Initialization Routine
  276. * @pdev: PCI device information struct
  277. * @ent: entry in ixgb_pci_tbl
  278. *
  279. * Returns 0 on success, negative on failure
  280. *
  281. * ixgb_probe initializes an adapter identified by a pci_dev structure.
  282. * The OS initialization, configuring of the adapter private structure,
  283. * and a hardware reset occur.
  284. **/
  285. static int __devinit
  286. ixgb_probe(struct pci_dev *pdev,
  287. const struct pci_device_id *ent)
  288. {
  289. struct net_device *netdev = NULL;
  290. struct ixgb_adapter *adapter;
  291. static int cards_found = 0;
  292. unsigned long mmio_start;
  293. int mmio_len;
  294. int pci_using_dac;
  295. int i;
  296. int err;
  297. if((err = pci_enable_device(pdev)))
  298. return err;
  299. if(!(err = pci_set_dma_mask(pdev, DMA_64BIT_MASK)) &&
  300. !(err = pci_set_consistent_dma_mask(pdev, DMA_64BIT_MASK))) {
  301. pci_using_dac = 1;
  302. } else {
  303. if((err = pci_set_dma_mask(pdev, DMA_32BIT_MASK)) ||
  304. (err = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK))) {
  305. IXGB_ERR("No usable DMA configuration, aborting\n");
  306. goto err_dma_mask;
  307. }
  308. pci_using_dac = 0;
  309. }
  310. if((err = pci_request_regions(pdev, ixgb_driver_name)))
  311. goto err_request_regions;
  312. pci_set_master(pdev);
  313. netdev = alloc_etherdev(sizeof(struct ixgb_adapter));
  314. if(!netdev) {
  315. err = -ENOMEM;
  316. goto err_alloc_etherdev;
  317. }
  318. SET_MODULE_OWNER(netdev);
  319. SET_NETDEV_DEV(netdev, &pdev->dev);
  320. pci_set_drvdata(pdev, netdev);
  321. adapter = netdev_priv(netdev);
  322. adapter->netdev = netdev;
  323. adapter->pdev = pdev;
  324. adapter->hw.back = adapter;
  325. mmio_start = pci_resource_start(pdev, BAR_0);
  326. mmio_len = pci_resource_len(pdev, BAR_0);
  327. adapter->hw.hw_addr = ioremap(mmio_start, mmio_len);
  328. if(!adapter->hw.hw_addr) {
  329. err = -EIO;
  330. goto err_ioremap;
  331. }
  332. for(i = BAR_1; i <= BAR_5; i++) {
  333. if(pci_resource_len(pdev, i) == 0)
  334. continue;
  335. if(pci_resource_flags(pdev, i) & IORESOURCE_IO) {
  336. adapter->hw.io_base = pci_resource_start(pdev, i);
  337. break;
  338. }
  339. }
  340. netdev->open = &ixgb_open;
  341. netdev->stop = &ixgb_close;
  342. netdev->hard_start_xmit = &ixgb_xmit_frame;
  343. netdev->get_stats = &ixgb_get_stats;
  344. netdev->set_multicast_list = &ixgb_set_multi;
  345. netdev->set_mac_address = &ixgb_set_mac;
  346. netdev->change_mtu = &ixgb_change_mtu;
  347. ixgb_set_ethtool_ops(netdev);
  348. netdev->tx_timeout = &ixgb_tx_timeout;
  349. netdev->watchdog_timeo = HZ;
  350. #ifdef CONFIG_IXGB_NAPI
  351. netdev->poll = &ixgb_clean;
  352. netdev->weight = 64;
  353. #endif
  354. netdev->vlan_rx_register = ixgb_vlan_rx_register;
  355. netdev->vlan_rx_add_vid = ixgb_vlan_rx_add_vid;
  356. netdev->vlan_rx_kill_vid = ixgb_vlan_rx_kill_vid;
  357. #ifdef CONFIG_NET_POLL_CONTROLLER
  358. netdev->poll_controller = ixgb_netpoll;
  359. #endif
  360. netdev->mem_start = mmio_start;
  361. netdev->mem_end = mmio_start + mmio_len;
  362. netdev->base_addr = adapter->hw.io_base;
  363. adapter->bd_number = cards_found;
  364. adapter->link_speed = 0;
  365. adapter->link_duplex = 0;
  366. /* setup the private structure */
  367. if((err = ixgb_sw_init(adapter)))
  368. goto err_sw_init;
  369. netdev->features = NETIF_F_SG |
  370. NETIF_F_HW_CSUM |
  371. NETIF_F_HW_VLAN_TX |
  372. NETIF_F_HW_VLAN_RX |
  373. NETIF_F_HW_VLAN_FILTER;
  374. #ifdef NETIF_F_TSO
  375. netdev->features |= NETIF_F_TSO;
  376. #endif
  377. #ifdef NETIF_F_LLTX
  378. netdev->features |= NETIF_F_LLTX;
  379. #endif
  380. if(pci_using_dac)
  381. netdev->features |= NETIF_F_HIGHDMA;
  382. /* make sure the EEPROM is good */
  383. if(!ixgb_validate_eeprom_checksum(&adapter->hw)) {
  384. printk(KERN_ERR "The EEPROM Checksum Is Not Valid\n");
  385. err = -EIO;
  386. goto err_eeprom;
  387. }
  388. ixgb_get_ee_mac_addr(&adapter->hw, netdev->dev_addr);
  389. memcpy(netdev->perm_addr, netdev->dev_addr, netdev->addr_len);
  390. if(!is_valid_ether_addr(netdev->perm_addr)) {
  391. err = -EIO;
  392. goto err_eeprom;
  393. }
  394. adapter->part_num = ixgb_get_ee_pba_number(&adapter->hw);
  395. init_timer(&adapter->watchdog_timer);
  396. adapter->watchdog_timer.function = &ixgb_watchdog;
  397. adapter->watchdog_timer.data = (unsigned long)adapter;
  398. INIT_WORK(&adapter->tx_timeout_task,
  399. (void (*)(void *))ixgb_tx_timeout_task, netdev);
  400. if((err = register_netdev(netdev)))
  401. goto err_register;
  402. /* we're going to reset, so assume we have no link for now */
  403. netif_carrier_off(netdev);
  404. netif_stop_queue(netdev);
  405. printk(KERN_INFO "%s: Intel(R) PRO/10GbE Network Connection\n",
  406. netdev->name);
  407. ixgb_check_options(adapter);
  408. /* reset the hardware with the new settings */
  409. ixgb_reset(adapter);
  410. cards_found++;
  411. return 0;
  412. err_register:
  413. err_sw_init:
  414. err_eeprom:
  415. iounmap(adapter->hw.hw_addr);
  416. err_ioremap:
  417. free_netdev(netdev);
  418. err_alloc_etherdev:
  419. pci_release_regions(pdev);
  420. err_request_regions:
  421. err_dma_mask:
  422. pci_disable_device(pdev);
  423. return err;
  424. }
  425. /**
  426. * ixgb_remove - Device Removal Routine
  427. * @pdev: PCI device information struct
  428. *
  429. * ixgb_remove is called by the PCI subsystem to alert the driver
  430. * that it should release a PCI device. The could be caused by a
  431. * Hot-Plug event, or because the driver is going to be removed from
  432. * memory.
  433. **/
  434. static void __devexit
  435. ixgb_remove(struct pci_dev *pdev)
  436. {
  437. struct net_device *netdev = pci_get_drvdata(pdev);
  438. struct ixgb_adapter *adapter = netdev_priv(netdev);
  439. unregister_netdev(netdev);
  440. iounmap(adapter->hw.hw_addr);
  441. pci_release_regions(pdev);
  442. free_netdev(netdev);
  443. }
  444. /**
  445. * ixgb_sw_init - Initialize general software structures (struct ixgb_adapter)
  446. * @adapter: board private structure to initialize
  447. *
  448. * ixgb_sw_init initializes the Adapter private data structure.
  449. * Fields are initialized based on PCI device information and
  450. * OS network device settings (MTU size).
  451. **/
  452. static int __devinit
  453. ixgb_sw_init(struct ixgb_adapter *adapter)
  454. {
  455. struct ixgb_hw *hw = &adapter->hw;
  456. struct net_device *netdev = adapter->netdev;
  457. struct pci_dev *pdev = adapter->pdev;
  458. /* PCI config space info */
  459. hw->vendor_id = pdev->vendor;
  460. hw->device_id = pdev->device;
  461. hw->subsystem_vendor_id = pdev->subsystem_vendor;
  462. hw->subsystem_id = pdev->subsystem_device;
  463. adapter->rx_buffer_len = IXGB_RXBUFFER_2048;
  464. hw->max_frame_size = netdev->mtu + ENET_HEADER_SIZE + ENET_FCS_LENGTH;
  465. if((hw->device_id == IXGB_DEVICE_ID_82597EX)
  466. || (hw->device_id == IXGB_DEVICE_ID_82597EX_CX4)
  467. || (hw->device_id == IXGB_DEVICE_ID_82597EX_LR)
  468. || (hw->device_id == IXGB_DEVICE_ID_82597EX_SR))
  469. hw->mac_type = ixgb_82597;
  470. else {
  471. /* should never have loaded on this device */
  472. printk(KERN_ERR "ixgb: unsupported device id\n");
  473. }
  474. /* enable flow control to be programmed */
  475. hw->fc.send_xon = 1;
  476. atomic_set(&adapter->irq_sem, 1);
  477. spin_lock_init(&adapter->tx_lock);
  478. return 0;
  479. }
  480. /**
  481. * ixgb_open - Called when a network interface is made active
  482. * @netdev: network interface device structure
  483. *
  484. * Returns 0 on success, negative value on failure
  485. *
  486. * The open entry point is called when a network interface is made
  487. * active by the system (IFF_UP). At this point all resources needed
  488. * for transmit and receive operations are allocated, the interrupt
  489. * handler is registered with the OS, the watchdog timer is started,
  490. * and the stack is notified that the interface is ready.
  491. **/
  492. static int
  493. ixgb_open(struct net_device *netdev)
  494. {
  495. struct ixgb_adapter *adapter = netdev_priv(netdev);
  496. int err;
  497. /* allocate transmit descriptors */
  498. if((err = ixgb_setup_tx_resources(adapter)))
  499. goto err_setup_tx;
  500. /* allocate receive descriptors */
  501. if((err = ixgb_setup_rx_resources(adapter)))
  502. goto err_setup_rx;
  503. if((err = ixgb_up(adapter)))
  504. goto err_up;
  505. return 0;
  506. err_up:
  507. ixgb_free_rx_resources(adapter);
  508. err_setup_rx:
  509. ixgb_free_tx_resources(adapter);
  510. err_setup_tx:
  511. ixgb_reset(adapter);
  512. return err;
  513. }
  514. /**
  515. * ixgb_close - Disables a network interface
  516. * @netdev: network interface device structure
  517. *
  518. * Returns 0, this is not allowed to fail
  519. *
  520. * The close entry point is called when an interface is de-activated
  521. * by the OS. The hardware is still under the drivers control, but
  522. * needs to be disabled. A global MAC reset is issued to stop the
  523. * hardware, and all transmit and receive resources are freed.
  524. **/
  525. static int
  526. ixgb_close(struct net_device *netdev)
  527. {
  528. struct ixgb_adapter *adapter = netdev_priv(netdev);
  529. ixgb_down(adapter, TRUE);
  530. ixgb_free_tx_resources(adapter);
  531. ixgb_free_rx_resources(adapter);
  532. return 0;
  533. }
  534. /**
  535. * ixgb_setup_tx_resources - allocate Tx resources (Descriptors)
  536. * @adapter: board private structure
  537. *
  538. * Return 0 on success, negative on failure
  539. **/
  540. int
  541. ixgb_setup_tx_resources(struct ixgb_adapter *adapter)
  542. {
  543. struct ixgb_desc_ring *txdr = &adapter->tx_ring;
  544. struct pci_dev *pdev = adapter->pdev;
  545. int size;
  546. size = sizeof(struct ixgb_buffer) * txdr->count;
  547. txdr->buffer_info = vmalloc(size);
  548. if(!txdr->buffer_info) {
  549. return -ENOMEM;
  550. }
  551. memset(txdr->buffer_info, 0, size);
  552. /* round up to nearest 4K */
  553. txdr->size = txdr->count * sizeof(struct ixgb_tx_desc);
  554. IXGB_ROUNDUP(txdr->size, 4096);
  555. txdr->desc = pci_alloc_consistent(pdev, txdr->size, &txdr->dma);
  556. if(!txdr->desc) {
  557. vfree(txdr->buffer_info);
  558. return -ENOMEM;
  559. }
  560. memset(txdr->desc, 0, txdr->size);
  561. txdr->next_to_use = 0;
  562. txdr->next_to_clean = 0;
  563. return 0;
  564. }
  565. /**
  566. * ixgb_configure_tx - Configure 82597 Transmit Unit after Reset.
  567. * @adapter: board private structure
  568. *
  569. * Configure the Tx unit of the MAC after a reset.
  570. **/
  571. static void
  572. ixgb_configure_tx(struct ixgb_adapter *adapter)
  573. {
  574. uint64_t tdba = adapter->tx_ring.dma;
  575. uint32_t tdlen = adapter->tx_ring.count * sizeof(struct ixgb_tx_desc);
  576. uint32_t tctl;
  577. struct ixgb_hw *hw = &adapter->hw;
  578. /* Setup the Base and Length of the Tx Descriptor Ring
  579. * tx_ring.dma can be either a 32 or 64 bit value
  580. */
  581. IXGB_WRITE_REG(hw, TDBAL, (tdba & 0x00000000ffffffffULL));
  582. IXGB_WRITE_REG(hw, TDBAH, (tdba >> 32));
  583. IXGB_WRITE_REG(hw, TDLEN, tdlen);
  584. /* Setup the HW Tx Head and Tail descriptor pointers */
  585. IXGB_WRITE_REG(hw, TDH, 0);
  586. IXGB_WRITE_REG(hw, TDT, 0);
  587. /* don't set up txdctl, it induces performance problems if configured
  588. * incorrectly */
  589. /* Set the Tx Interrupt Delay register */
  590. IXGB_WRITE_REG(hw, TIDV, adapter->tx_int_delay);
  591. /* Program the Transmit Control Register */
  592. tctl = IXGB_TCTL_TCE | IXGB_TCTL_TXEN | IXGB_TCTL_TPDE;
  593. IXGB_WRITE_REG(hw, TCTL, tctl);
  594. /* Setup Transmit Descriptor Settings for this adapter */
  595. adapter->tx_cmd_type =
  596. IXGB_TX_DESC_TYPE
  597. | (adapter->tx_int_delay_enable ? IXGB_TX_DESC_CMD_IDE : 0);
  598. }
  599. /**
  600. * ixgb_setup_rx_resources - allocate Rx resources (Descriptors)
  601. * @adapter: board private structure
  602. *
  603. * Returns 0 on success, negative on failure
  604. **/
  605. int
  606. ixgb_setup_rx_resources(struct ixgb_adapter *adapter)
  607. {
  608. struct ixgb_desc_ring *rxdr = &adapter->rx_ring;
  609. struct pci_dev *pdev = adapter->pdev;
  610. int size;
  611. size = sizeof(struct ixgb_buffer) * rxdr->count;
  612. rxdr->buffer_info = vmalloc(size);
  613. if(!rxdr->buffer_info) {
  614. return -ENOMEM;
  615. }
  616. memset(rxdr->buffer_info, 0, size);
  617. /* Round up to nearest 4K */
  618. rxdr->size = rxdr->count * sizeof(struct ixgb_rx_desc);
  619. IXGB_ROUNDUP(rxdr->size, 4096);
  620. rxdr->desc = pci_alloc_consistent(pdev, rxdr->size, &rxdr->dma);
  621. if(!rxdr->desc) {
  622. vfree(rxdr->buffer_info);
  623. return -ENOMEM;
  624. }
  625. memset(rxdr->desc, 0, rxdr->size);
  626. rxdr->next_to_clean = 0;
  627. rxdr->next_to_use = 0;
  628. return 0;
  629. }
  630. /**
  631. * ixgb_setup_rctl - configure the receive control register
  632. * @adapter: Board private structure
  633. **/
  634. static void
  635. ixgb_setup_rctl(struct ixgb_adapter *adapter)
  636. {
  637. uint32_t rctl;
  638. rctl = IXGB_READ_REG(&adapter->hw, RCTL);
  639. rctl &= ~(3 << IXGB_RCTL_MO_SHIFT);
  640. rctl |=
  641. IXGB_RCTL_BAM | IXGB_RCTL_RDMTS_1_2 |
  642. IXGB_RCTL_RXEN | IXGB_RCTL_CFF |
  643. (adapter->hw.mc_filter_type << IXGB_RCTL_MO_SHIFT);
  644. rctl |= IXGB_RCTL_SECRC;
  645. switch (adapter->rx_buffer_len) {
  646. case IXGB_RXBUFFER_2048:
  647. default:
  648. rctl |= IXGB_RCTL_BSIZE_2048;
  649. break;
  650. case IXGB_RXBUFFER_4096:
  651. rctl |= IXGB_RCTL_BSIZE_4096;
  652. break;
  653. case IXGB_RXBUFFER_8192:
  654. rctl |= IXGB_RCTL_BSIZE_8192;
  655. break;
  656. case IXGB_RXBUFFER_16384:
  657. rctl |= IXGB_RCTL_BSIZE_16384;
  658. break;
  659. }
  660. IXGB_WRITE_REG(&adapter->hw, RCTL, rctl);
  661. }
  662. /**
  663. * ixgb_configure_rx - Configure 82597 Receive Unit after Reset.
  664. * @adapter: board private structure
  665. *
  666. * Configure the Rx unit of the MAC after a reset.
  667. **/
  668. static void
  669. ixgb_configure_rx(struct ixgb_adapter *adapter)
  670. {
  671. uint64_t rdba = adapter->rx_ring.dma;
  672. uint32_t rdlen = adapter->rx_ring.count * sizeof(struct ixgb_rx_desc);
  673. struct ixgb_hw *hw = &adapter->hw;
  674. uint32_t rctl;
  675. uint32_t rxcsum;
  676. uint32_t rxdctl;
  677. /* make sure receives are disabled while setting up the descriptors */
  678. rctl = IXGB_READ_REG(hw, RCTL);
  679. IXGB_WRITE_REG(hw, RCTL, rctl & ~IXGB_RCTL_RXEN);
  680. /* set the Receive Delay Timer Register */
  681. IXGB_WRITE_REG(hw, RDTR, adapter->rx_int_delay);
  682. /* Setup the Base and Length of the Rx Descriptor Ring */
  683. IXGB_WRITE_REG(hw, RDBAL, (rdba & 0x00000000ffffffffULL));
  684. IXGB_WRITE_REG(hw, RDBAH, (rdba >> 32));
  685. IXGB_WRITE_REG(hw, RDLEN, rdlen);
  686. /* Setup the HW Rx Head and Tail Descriptor Pointers */
  687. IXGB_WRITE_REG(hw, RDH, 0);
  688. IXGB_WRITE_REG(hw, RDT, 0);
  689. /* set up pre-fetching of receive buffers so we get some before we
  690. * run out (default hardware behavior is to run out before fetching
  691. * more). This sets up to fetch if HTHRESH rx descriptors are avail
  692. * and the descriptors in hw cache are below PTHRESH. This avoids
  693. * the hardware behavior of fetching <=512 descriptors in a single
  694. * burst that pre-empts all other activity, usually causing fifo
  695. * overflows. */
  696. /* use WTHRESH to burst write 16 descriptors or burst when RXT0 */
  697. rxdctl = RXDCTL_WTHRESH_DEFAULT << IXGB_RXDCTL_WTHRESH_SHIFT |
  698. RXDCTL_HTHRESH_DEFAULT << IXGB_RXDCTL_HTHRESH_SHIFT |
  699. RXDCTL_PTHRESH_DEFAULT << IXGB_RXDCTL_PTHRESH_SHIFT;
  700. IXGB_WRITE_REG(hw, RXDCTL, rxdctl);
  701. /* Enable Receive Checksum Offload for TCP and UDP */
  702. if(adapter->rx_csum == TRUE) {
  703. rxcsum = IXGB_READ_REG(hw, RXCSUM);
  704. rxcsum |= IXGB_RXCSUM_TUOFL;
  705. IXGB_WRITE_REG(hw, RXCSUM, rxcsum);
  706. }
  707. /* Enable Receives */
  708. IXGB_WRITE_REG(hw, RCTL, rctl);
  709. }
  710. /**
  711. * ixgb_free_tx_resources - Free Tx Resources
  712. * @adapter: board private structure
  713. *
  714. * Free all transmit software resources
  715. **/
  716. void
  717. ixgb_free_tx_resources(struct ixgb_adapter *adapter)
  718. {
  719. struct pci_dev *pdev = adapter->pdev;
  720. ixgb_clean_tx_ring(adapter);
  721. vfree(adapter->tx_ring.buffer_info);
  722. adapter->tx_ring.buffer_info = NULL;
  723. pci_free_consistent(pdev, adapter->tx_ring.size,
  724. adapter->tx_ring.desc, adapter->tx_ring.dma);
  725. adapter->tx_ring.desc = NULL;
  726. }
  727. static inline void
  728. ixgb_unmap_and_free_tx_resource(struct ixgb_adapter *adapter,
  729. struct ixgb_buffer *buffer_info)
  730. {
  731. struct pci_dev *pdev = adapter->pdev;
  732. if(buffer_info->dma) {
  733. pci_unmap_page(pdev,
  734. buffer_info->dma,
  735. buffer_info->length,
  736. PCI_DMA_TODEVICE);
  737. buffer_info->dma = 0;
  738. }
  739. if(buffer_info->skb) {
  740. dev_kfree_skb_any(buffer_info->skb);
  741. buffer_info->skb = NULL;
  742. }
  743. }
  744. /**
  745. * ixgb_clean_tx_ring - Free Tx Buffers
  746. * @adapter: board private structure
  747. **/
  748. static void
  749. ixgb_clean_tx_ring(struct ixgb_adapter *adapter)
  750. {
  751. struct ixgb_desc_ring *tx_ring = &adapter->tx_ring;
  752. struct ixgb_buffer *buffer_info;
  753. unsigned long size;
  754. unsigned int i;
  755. /* Free all the Tx ring sk_buffs */
  756. for(i = 0; i < tx_ring->count; i++) {
  757. buffer_info = &tx_ring->buffer_info[i];
  758. ixgb_unmap_and_free_tx_resource(adapter, buffer_info);
  759. }
  760. size = sizeof(struct ixgb_buffer) * tx_ring->count;
  761. memset(tx_ring->buffer_info, 0, size);
  762. /* Zero out the descriptor ring */
  763. memset(tx_ring->desc, 0, tx_ring->size);
  764. tx_ring->next_to_use = 0;
  765. tx_ring->next_to_clean = 0;
  766. IXGB_WRITE_REG(&adapter->hw, TDH, 0);
  767. IXGB_WRITE_REG(&adapter->hw, TDT, 0);
  768. }
  769. /**
  770. * ixgb_free_rx_resources - Free Rx Resources
  771. * @adapter: board private structure
  772. *
  773. * Free all receive software resources
  774. **/
  775. void
  776. ixgb_free_rx_resources(struct ixgb_adapter *adapter)
  777. {
  778. struct ixgb_desc_ring *rx_ring = &adapter->rx_ring;
  779. struct pci_dev *pdev = adapter->pdev;
  780. ixgb_clean_rx_ring(adapter);
  781. vfree(rx_ring->buffer_info);
  782. rx_ring->buffer_info = NULL;
  783. pci_free_consistent(pdev, rx_ring->size, rx_ring->desc, rx_ring->dma);
  784. rx_ring->desc = NULL;
  785. }
  786. /**
  787. * ixgb_clean_rx_ring - Free Rx Buffers
  788. * @adapter: board private structure
  789. **/
  790. static void
  791. ixgb_clean_rx_ring(struct ixgb_adapter *adapter)
  792. {
  793. struct ixgb_desc_ring *rx_ring = &adapter->rx_ring;
  794. struct ixgb_buffer *buffer_info;
  795. struct pci_dev *pdev = adapter->pdev;
  796. unsigned long size;
  797. unsigned int i;
  798. /* Free all the Rx ring sk_buffs */
  799. for(i = 0; i < rx_ring->count; i++) {
  800. buffer_info = &rx_ring->buffer_info[i];
  801. if(buffer_info->skb) {
  802. pci_unmap_single(pdev,
  803. buffer_info->dma,
  804. buffer_info->length,
  805. PCI_DMA_FROMDEVICE);
  806. dev_kfree_skb(buffer_info->skb);
  807. buffer_info->skb = NULL;
  808. }
  809. }
  810. size = sizeof(struct ixgb_buffer) * rx_ring->count;
  811. memset(rx_ring->buffer_info, 0, size);
  812. /* Zero out the descriptor ring */
  813. memset(rx_ring->desc, 0, rx_ring->size);
  814. rx_ring->next_to_clean = 0;
  815. rx_ring->next_to_use = 0;
  816. IXGB_WRITE_REG(&adapter->hw, RDH, 0);
  817. IXGB_WRITE_REG(&adapter->hw, RDT, 0);
  818. }
  819. /**
  820. * ixgb_set_mac - Change the Ethernet Address of the NIC
  821. * @netdev: network interface device structure
  822. * @p: pointer to an address structure
  823. *
  824. * Returns 0 on success, negative on failure
  825. **/
  826. static int
  827. ixgb_set_mac(struct net_device *netdev, void *p)
  828. {
  829. struct ixgb_adapter *adapter = netdev_priv(netdev);
  830. struct sockaddr *addr = p;
  831. if(!is_valid_ether_addr(addr->sa_data))
  832. return -EADDRNOTAVAIL;
  833. memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
  834. ixgb_rar_set(&adapter->hw, addr->sa_data, 0);
  835. return 0;
  836. }
  837. /**
  838. * ixgb_set_multi - Multicast and Promiscuous mode set
  839. * @netdev: network interface device structure
  840. *
  841. * The set_multi entry point is called whenever the multicast address
  842. * list or the network interface flags are updated. This routine is
  843. * responsible for configuring the hardware for proper multicast,
  844. * promiscuous mode, and all-multi behavior.
  845. **/
  846. static void
  847. ixgb_set_multi(struct net_device *netdev)
  848. {
  849. struct ixgb_adapter *adapter = netdev_priv(netdev);
  850. struct ixgb_hw *hw = &adapter->hw;
  851. struct dev_mc_list *mc_ptr;
  852. uint32_t rctl;
  853. int i;
  854. /* Check for Promiscuous and All Multicast modes */
  855. rctl = IXGB_READ_REG(hw, RCTL);
  856. if(netdev->flags & IFF_PROMISC) {
  857. rctl |= (IXGB_RCTL_UPE | IXGB_RCTL_MPE);
  858. } else if(netdev->flags & IFF_ALLMULTI) {
  859. rctl |= IXGB_RCTL_MPE;
  860. rctl &= ~IXGB_RCTL_UPE;
  861. } else {
  862. rctl &= ~(IXGB_RCTL_UPE | IXGB_RCTL_MPE);
  863. }
  864. if(netdev->mc_count > IXGB_MAX_NUM_MULTICAST_ADDRESSES) {
  865. rctl |= IXGB_RCTL_MPE;
  866. IXGB_WRITE_REG(hw, RCTL, rctl);
  867. } else {
  868. uint8_t mta[netdev->mc_count * IXGB_ETH_LENGTH_OF_ADDRESS];
  869. IXGB_WRITE_REG(hw, RCTL, rctl);
  870. for(i = 0, mc_ptr = netdev->mc_list; mc_ptr;
  871. i++, mc_ptr = mc_ptr->next)
  872. memcpy(&mta[i * IXGB_ETH_LENGTH_OF_ADDRESS],
  873. mc_ptr->dmi_addr, IXGB_ETH_LENGTH_OF_ADDRESS);
  874. ixgb_mc_addr_list_update(hw, mta, netdev->mc_count, 0);
  875. }
  876. }
  877. /**
  878. * ixgb_watchdog - Timer Call-back
  879. * @data: pointer to netdev cast into an unsigned long
  880. **/
  881. static void
  882. ixgb_watchdog(unsigned long data)
  883. {
  884. struct ixgb_adapter *adapter = (struct ixgb_adapter *)data;
  885. struct net_device *netdev = adapter->netdev;
  886. struct ixgb_desc_ring *txdr = &adapter->tx_ring;
  887. ixgb_check_for_link(&adapter->hw);
  888. if (ixgb_check_for_bad_link(&adapter->hw)) {
  889. /* force the reset path */
  890. netif_stop_queue(netdev);
  891. }
  892. if(adapter->hw.link_up) {
  893. if(!netif_carrier_ok(netdev)) {
  894. printk(KERN_INFO "ixgb: %s NIC Link is Up %d Mbps %s\n",
  895. netdev->name, 10000, "Full Duplex");
  896. adapter->link_speed = 10000;
  897. adapter->link_duplex = FULL_DUPLEX;
  898. netif_carrier_on(netdev);
  899. netif_wake_queue(netdev);
  900. }
  901. } else {
  902. if(netif_carrier_ok(netdev)) {
  903. adapter->link_speed = 0;
  904. adapter->link_duplex = 0;
  905. printk(KERN_INFO
  906. "ixgb: %s NIC Link is Down\n",
  907. netdev->name);
  908. netif_carrier_off(netdev);
  909. netif_stop_queue(netdev);
  910. }
  911. }
  912. ixgb_update_stats(adapter);
  913. if(!netif_carrier_ok(netdev)) {
  914. if(IXGB_DESC_UNUSED(txdr) + 1 < txdr->count) {
  915. /* We've lost link, so the controller stops DMA,
  916. * but we've got queued Tx work that's never going
  917. * to get done, so reset controller to flush Tx.
  918. * (Do the reset outside of interrupt context). */
  919. schedule_work(&adapter->tx_timeout_task);
  920. }
  921. }
  922. /* Force detection of hung controller every watchdog period */
  923. adapter->detect_tx_hung = TRUE;
  924. /* generate an interrupt to force clean up of any stragglers */
  925. IXGB_WRITE_REG(&adapter->hw, ICS, IXGB_INT_TXDW);
  926. /* Reset the timer */
  927. mod_timer(&adapter->watchdog_timer, jiffies + 2 * HZ);
  928. }
  929. #define IXGB_TX_FLAGS_CSUM 0x00000001
  930. #define IXGB_TX_FLAGS_VLAN 0x00000002
  931. #define IXGB_TX_FLAGS_TSO 0x00000004
  932. static inline int
  933. ixgb_tso(struct ixgb_adapter *adapter, struct sk_buff *skb)
  934. {
  935. #ifdef NETIF_F_TSO
  936. struct ixgb_context_desc *context_desc;
  937. unsigned int i;
  938. uint8_t ipcss, ipcso, tucss, tucso, hdr_len;
  939. uint16_t ipcse, tucse, mss;
  940. int err;
  941. if(likely(skb_shinfo(skb)->tso_size)) {
  942. if (skb_header_cloned(skb)) {
  943. err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
  944. if (err)
  945. return err;
  946. }
  947. hdr_len = ((skb->h.raw - skb->data) + (skb->h.th->doff << 2));
  948. mss = skb_shinfo(skb)->tso_size;
  949. skb->nh.iph->tot_len = 0;
  950. skb->nh.iph->check = 0;
  951. skb->h.th->check = ~csum_tcpudp_magic(skb->nh.iph->saddr,
  952. skb->nh.iph->daddr,
  953. 0, IPPROTO_TCP, 0);
  954. ipcss = skb->nh.raw - skb->data;
  955. ipcso = (void *)&(skb->nh.iph->check) - (void *)skb->data;
  956. ipcse = skb->h.raw - skb->data - 1;
  957. tucss = skb->h.raw - skb->data;
  958. tucso = (void *)&(skb->h.th->check) - (void *)skb->data;
  959. tucse = 0;
  960. i = adapter->tx_ring.next_to_use;
  961. context_desc = IXGB_CONTEXT_DESC(adapter->tx_ring, i);
  962. context_desc->ipcss = ipcss;
  963. context_desc->ipcso = ipcso;
  964. context_desc->ipcse = cpu_to_le16(ipcse);
  965. context_desc->tucss = tucss;
  966. context_desc->tucso = tucso;
  967. context_desc->tucse = cpu_to_le16(tucse);
  968. context_desc->mss = cpu_to_le16(mss);
  969. context_desc->hdr_len = hdr_len;
  970. context_desc->status = 0;
  971. context_desc->cmd_type_len = cpu_to_le32(
  972. IXGB_CONTEXT_DESC_TYPE
  973. | IXGB_CONTEXT_DESC_CMD_TSE
  974. | IXGB_CONTEXT_DESC_CMD_IP
  975. | IXGB_CONTEXT_DESC_CMD_TCP
  976. | IXGB_CONTEXT_DESC_CMD_IDE
  977. | (skb->len - (hdr_len)));
  978. if(++i == adapter->tx_ring.count) i = 0;
  979. adapter->tx_ring.next_to_use = i;
  980. return 1;
  981. }
  982. #endif
  983. return 0;
  984. }
  985. static inline boolean_t
  986. ixgb_tx_csum(struct ixgb_adapter *adapter, struct sk_buff *skb)
  987. {
  988. struct ixgb_context_desc *context_desc;
  989. unsigned int i;
  990. uint8_t css, cso;
  991. if(likely(skb->ip_summed == CHECKSUM_HW)) {
  992. css = skb->h.raw - skb->data;
  993. cso = (skb->h.raw + skb->csum) - skb->data;
  994. i = adapter->tx_ring.next_to_use;
  995. context_desc = IXGB_CONTEXT_DESC(adapter->tx_ring, i);
  996. context_desc->tucss = css;
  997. context_desc->tucso = cso;
  998. context_desc->tucse = 0;
  999. /* zero out any previously existing data in one instruction */
  1000. *(uint32_t *)&(context_desc->ipcss) = 0;
  1001. context_desc->status = 0;
  1002. context_desc->hdr_len = 0;
  1003. context_desc->mss = 0;
  1004. context_desc->cmd_type_len =
  1005. cpu_to_le32(IXGB_CONTEXT_DESC_TYPE
  1006. | IXGB_TX_DESC_CMD_IDE);
  1007. if(++i == adapter->tx_ring.count) i = 0;
  1008. adapter->tx_ring.next_to_use = i;
  1009. return TRUE;
  1010. }
  1011. return FALSE;
  1012. }
  1013. #define IXGB_MAX_TXD_PWR 14
  1014. #define IXGB_MAX_DATA_PER_TXD (1<<IXGB_MAX_TXD_PWR)
  1015. static inline int
  1016. ixgb_tx_map(struct ixgb_adapter *adapter, struct sk_buff *skb,
  1017. unsigned int first)
  1018. {
  1019. struct ixgb_desc_ring *tx_ring = &adapter->tx_ring;
  1020. struct ixgb_buffer *buffer_info;
  1021. int len = skb->len;
  1022. unsigned int offset = 0, size, count = 0, i;
  1023. unsigned int mss = skb_shinfo(skb)->tso_size;
  1024. unsigned int nr_frags = skb_shinfo(skb)->nr_frags;
  1025. unsigned int f;
  1026. len -= skb->data_len;
  1027. i = tx_ring->next_to_use;
  1028. while(len) {
  1029. buffer_info = &tx_ring->buffer_info[i];
  1030. size = min(len, IXGB_MAX_JUMBO_FRAME_SIZE);
  1031. /* Workaround for premature desc write-backs
  1032. * in TSO mode. Append 4-byte sentinel desc */
  1033. if(unlikely(mss && !nr_frags && size == len && size > 8))
  1034. size -= 4;
  1035. buffer_info->length = size;
  1036. buffer_info->dma =
  1037. pci_map_single(adapter->pdev,
  1038. skb->data + offset,
  1039. size,
  1040. PCI_DMA_TODEVICE);
  1041. buffer_info->time_stamp = jiffies;
  1042. len -= size;
  1043. offset += size;
  1044. count++;
  1045. if(++i == tx_ring->count) i = 0;
  1046. }
  1047. for(f = 0; f < nr_frags; f++) {
  1048. struct skb_frag_struct *frag;
  1049. frag = &skb_shinfo(skb)->frags[f];
  1050. len = frag->size;
  1051. offset = 0;
  1052. while(len) {
  1053. buffer_info = &tx_ring->buffer_info[i];
  1054. size = min(len, IXGB_MAX_JUMBO_FRAME_SIZE);
  1055. /* Workaround for premature desc write-backs
  1056. * in TSO mode. Append 4-byte sentinel desc */
  1057. if(unlikely(mss && (f == (nr_frags-1)) && (size == len)
  1058. && (size > 8)))
  1059. size -= 4;
  1060. buffer_info->length = size;
  1061. buffer_info->dma =
  1062. pci_map_page(adapter->pdev,
  1063. frag->page,
  1064. frag->page_offset + offset,
  1065. size,
  1066. PCI_DMA_TODEVICE);
  1067. buffer_info->time_stamp = jiffies;
  1068. len -= size;
  1069. offset += size;
  1070. count++;
  1071. if(++i == tx_ring->count) i = 0;
  1072. }
  1073. }
  1074. i = (i == 0) ? tx_ring->count - 1 : i - 1;
  1075. tx_ring->buffer_info[i].skb = skb;
  1076. tx_ring->buffer_info[first].next_to_watch = i;
  1077. return count;
  1078. }
  1079. static inline void
  1080. ixgb_tx_queue(struct ixgb_adapter *adapter, int count, int vlan_id,int tx_flags)
  1081. {
  1082. struct ixgb_desc_ring *tx_ring = &adapter->tx_ring;
  1083. struct ixgb_tx_desc *tx_desc = NULL;
  1084. struct ixgb_buffer *buffer_info;
  1085. uint32_t cmd_type_len = adapter->tx_cmd_type;
  1086. uint8_t status = 0;
  1087. uint8_t popts = 0;
  1088. unsigned int i;
  1089. if(tx_flags & IXGB_TX_FLAGS_TSO) {
  1090. cmd_type_len |= IXGB_TX_DESC_CMD_TSE;
  1091. popts |= (IXGB_TX_DESC_POPTS_IXSM | IXGB_TX_DESC_POPTS_TXSM);
  1092. }
  1093. if(tx_flags & IXGB_TX_FLAGS_CSUM)
  1094. popts |= IXGB_TX_DESC_POPTS_TXSM;
  1095. if(tx_flags & IXGB_TX_FLAGS_VLAN) {
  1096. cmd_type_len |= IXGB_TX_DESC_CMD_VLE;
  1097. }
  1098. i = tx_ring->next_to_use;
  1099. while(count--) {
  1100. buffer_info = &tx_ring->buffer_info[i];
  1101. tx_desc = IXGB_TX_DESC(*tx_ring, i);
  1102. tx_desc->buff_addr = cpu_to_le64(buffer_info->dma);
  1103. tx_desc->cmd_type_len =
  1104. cpu_to_le32(cmd_type_len | buffer_info->length);
  1105. tx_desc->status = status;
  1106. tx_desc->popts = popts;
  1107. tx_desc->vlan = cpu_to_le16(vlan_id);
  1108. if(++i == tx_ring->count) i = 0;
  1109. }
  1110. tx_desc->cmd_type_len |= cpu_to_le32(IXGB_TX_DESC_CMD_EOP
  1111. | IXGB_TX_DESC_CMD_RS );
  1112. /* Force memory writes to complete before letting h/w
  1113. * know there are new descriptors to fetch. (Only
  1114. * applicable for weak-ordered memory model archs,
  1115. * such as IA-64). */
  1116. wmb();
  1117. tx_ring->next_to_use = i;
  1118. IXGB_WRITE_REG(&adapter->hw, TDT, i);
  1119. }
  1120. /* Tx Descriptors needed, worst case */
  1121. #define TXD_USE_COUNT(S) (((S) >> IXGB_MAX_TXD_PWR) + \
  1122. (((S) & (IXGB_MAX_DATA_PER_TXD - 1)) ? 1 : 0))
  1123. #define DESC_NEEDED TXD_USE_COUNT(IXGB_MAX_DATA_PER_TXD) + \
  1124. MAX_SKB_FRAGS * TXD_USE_COUNT(PAGE_SIZE) + 1 \
  1125. /* one more for TSO workaround */ + 1
  1126. static int
  1127. ixgb_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
  1128. {
  1129. struct ixgb_adapter *adapter = netdev_priv(netdev);
  1130. unsigned int first;
  1131. unsigned int tx_flags = 0;
  1132. unsigned long flags;
  1133. int vlan_id = 0;
  1134. int tso;
  1135. if(skb->len <= 0) {
  1136. dev_kfree_skb_any(skb);
  1137. return 0;
  1138. }
  1139. #ifdef NETIF_F_LLTX
  1140. local_irq_save(flags);
  1141. if (!spin_trylock(&adapter->tx_lock)) {
  1142. /* Collision - tell upper layer to requeue */
  1143. local_irq_restore(flags);
  1144. return NETDEV_TX_LOCKED;
  1145. }
  1146. #else
  1147. spin_lock_irqsave(&adapter->tx_lock, flags);
  1148. #endif
  1149. if(unlikely(IXGB_DESC_UNUSED(&adapter->tx_ring) < DESC_NEEDED)) {
  1150. netif_stop_queue(netdev);
  1151. spin_unlock_irqrestore(&adapter->tx_lock, flags);
  1152. return NETDEV_TX_BUSY;
  1153. }
  1154. #ifndef NETIF_F_LLTX
  1155. spin_unlock_irqrestore(&adapter->tx_lock, flags);
  1156. #endif
  1157. if(adapter->vlgrp && vlan_tx_tag_present(skb)) {
  1158. tx_flags |= IXGB_TX_FLAGS_VLAN;
  1159. vlan_id = vlan_tx_tag_get(skb);
  1160. }
  1161. first = adapter->tx_ring.next_to_use;
  1162. tso = ixgb_tso(adapter, skb);
  1163. if (tso < 0) {
  1164. dev_kfree_skb_any(skb);
  1165. #ifdef NETIF_F_LLTX
  1166. spin_unlock_irqrestore(&adapter->tx_lock, flags);
  1167. #endif
  1168. return NETDEV_TX_OK;
  1169. }
  1170. if (likely(tso))
  1171. tx_flags |= IXGB_TX_FLAGS_TSO;
  1172. else if(ixgb_tx_csum(adapter, skb))
  1173. tx_flags |= IXGB_TX_FLAGS_CSUM;
  1174. ixgb_tx_queue(adapter, ixgb_tx_map(adapter, skb, first), vlan_id,
  1175. tx_flags);
  1176. netdev->trans_start = jiffies;
  1177. #ifdef NETIF_F_LLTX
  1178. /* Make sure there is space in the ring for the next send. */
  1179. if(unlikely(IXGB_DESC_UNUSED(&adapter->tx_ring) < DESC_NEEDED))
  1180. netif_stop_queue(netdev);
  1181. spin_unlock_irqrestore(&adapter->tx_lock, flags);
  1182. #endif
  1183. return NETDEV_TX_OK;
  1184. }
  1185. /**
  1186. * ixgb_tx_timeout - Respond to a Tx Hang
  1187. * @netdev: network interface device structure
  1188. **/
  1189. static void
  1190. ixgb_tx_timeout(struct net_device *netdev)
  1191. {
  1192. struct ixgb_adapter *adapter = netdev_priv(netdev);
  1193. /* Do the reset outside of interrupt context */
  1194. schedule_work(&adapter->tx_timeout_task);
  1195. }
  1196. static void
  1197. ixgb_tx_timeout_task(struct net_device *netdev)
  1198. {
  1199. struct ixgb_adapter *adapter = netdev_priv(netdev);
  1200. ixgb_down(adapter, TRUE);
  1201. ixgb_up(adapter);
  1202. }
  1203. /**
  1204. * ixgb_get_stats - Get System Network Statistics
  1205. * @netdev: network interface device structure
  1206. *
  1207. * Returns the address of the device statistics structure.
  1208. * The statistics are actually updated from the timer callback.
  1209. **/
  1210. static struct net_device_stats *
  1211. ixgb_get_stats(struct net_device *netdev)
  1212. {
  1213. struct ixgb_adapter *adapter = netdev_priv(netdev);
  1214. return &adapter->net_stats;
  1215. }
  1216. /**
  1217. * ixgb_change_mtu - Change the Maximum Transfer Unit
  1218. * @netdev: network interface device structure
  1219. * @new_mtu: new value for maximum frame size
  1220. *
  1221. * Returns 0 on success, negative on failure
  1222. **/
  1223. static int
  1224. ixgb_change_mtu(struct net_device *netdev, int new_mtu)
  1225. {
  1226. struct ixgb_adapter *adapter = netdev_priv(netdev);
  1227. int max_frame = new_mtu + ENET_HEADER_SIZE + ENET_FCS_LENGTH;
  1228. int old_max_frame = netdev->mtu + ENET_HEADER_SIZE + ENET_FCS_LENGTH;
  1229. if((max_frame < IXGB_MIN_ENET_FRAME_SIZE_WITHOUT_FCS + ENET_FCS_LENGTH)
  1230. || (max_frame > IXGB_MAX_JUMBO_FRAME_SIZE + ENET_FCS_LENGTH)) {
  1231. IXGB_ERR("Invalid MTU setting\n");
  1232. return -EINVAL;
  1233. }
  1234. if((max_frame <= IXGB_MAX_ENET_FRAME_SIZE_WITHOUT_FCS + ENET_FCS_LENGTH)
  1235. || (max_frame <= IXGB_RXBUFFER_2048)) {
  1236. adapter->rx_buffer_len = IXGB_RXBUFFER_2048;
  1237. } else if(max_frame <= IXGB_RXBUFFER_4096) {
  1238. adapter->rx_buffer_len = IXGB_RXBUFFER_4096;
  1239. } else if(max_frame <= IXGB_RXBUFFER_8192) {
  1240. adapter->rx_buffer_len = IXGB_RXBUFFER_8192;
  1241. } else {
  1242. adapter->rx_buffer_len = IXGB_RXBUFFER_16384;
  1243. }
  1244. netdev->mtu = new_mtu;
  1245. if(old_max_frame != max_frame && netif_running(netdev)) {
  1246. ixgb_down(adapter, TRUE);
  1247. ixgb_up(adapter);
  1248. }
  1249. return 0;
  1250. }
  1251. /**
  1252. * ixgb_update_stats - Update the board statistics counters.
  1253. * @adapter: board private structure
  1254. **/
  1255. void
  1256. ixgb_update_stats(struct ixgb_adapter *adapter)
  1257. {
  1258. struct net_device *netdev = adapter->netdev;
  1259. if((netdev->flags & IFF_PROMISC) || (netdev->flags & IFF_ALLMULTI) ||
  1260. (netdev->mc_count > IXGB_MAX_NUM_MULTICAST_ADDRESSES)) {
  1261. u64 multi = IXGB_READ_REG(&adapter->hw, MPRCL);
  1262. u32 bcast_l = IXGB_READ_REG(&adapter->hw, BPRCL);
  1263. u32 bcast_h = IXGB_READ_REG(&adapter->hw, BPRCH);
  1264. u64 bcast = ((u64)bcast_h << 32) | bcast_l;
  1265. multi |= ((u64)IXGB_READ_REG(&adapter->hw, MPRCH) << 32);
  1266. /* fix up multicast stats by removing broadcasts */
  1267. if(multi >= bcast)
  1268. multi -= bcast;
  1269. adapter->stats.mprcl += (multi & 0xFFFFFFFF);
  1270. adapter->stats.mprch += (multi >> 32);
  1271. adapter->stats.bprcl += bcast_l;
  1272. adapter->stats.bprch += bcast_h;
  1273. } else {
  1274. adapter->stats.mprcl += IXGB_READ_REG(&adapter->hw, MPRCL);
  1275. adapter->stats.mprch += IXGB_READ_REG(&adapter->hw, MPRCH);
  1276. adapter->stats.bprcl += IXGB_READ_REG(&adapter->hw, BPRCL);
  1277. adapter->stats.bprch += IXGB_READ_REG(&adapter->hw, BPRCH);
  1278. }
  1279. adapter->stats.tprl += IXGB_READ_REG(&adapter->hw, TPRL);
  1280. adapter->stats.tprh += IXGB_READ_REG(&adapter->hw, TPRH);
  1281. adapter->stats.gprcl += IXGB_READ_REG(&adapter->hw, GPRCL);
  1282. adapter->stats.gprch += IXGB_READ_REG(&adapter->hw, GPRCH);
  1283. adapter->stats.uprcl += IXGB_READ_REG(&adapter->hw, UPRCL);
  1284. adapter->stats.uprch += IXGB_READ_REG(&adapter->hw, UPRCH);
  1285. adapter->stats.vprcl += IXGB_READ_REG(&adapter->hw, VPRCL);
  1286. adapter->stats.vprch += IXGB_READ_REG(&adapter->hw, VPRCH);
  1287. adapter->stats.jprcl += IXGB_READ_REG(&adapter->hw, JPRCL);
  1288. adapter->stats.jprch += IXGB_READ_REG(&adapter->hw, JPRCH);
  1289. adapter->stats.gorcl += IXGB_READ_REG(&adapter->hw, GORCL);
  1290. adapter->stats.gorch += IXGB_READ_REG(&adapter->hw, GORCH);
  1291. adapter->stats.torl += IXGB_READ_REG(&adapter->hw, TORL);
  1292. adapter->stats.torh += IXGB_READ_REG(&adapter->hw, TORH);
  1293. adapter->stats.rnbc += IXGB_READ_REG(&adapter->hw, RNBC);
  1294. adapter->stats.ruc += IXGB_READ_REG(&adapter->hw, RUC);
  1295. adapter->stats.roc += IXGB_READ_REG(&adapter->hw, ROC);
  1296. adapter->stats.rlec += IXGB_READ_REG(&adapter->hw, RLEC);
  1297. adapter->stats.crcerrs += IXGB_READ_REG(&adapter->hw, CRCERRS);
  1298. adapter->stats.icbc += IXGB_READ_REG(&adapter->hw, ICBC);
  1299. adapter->stats.ecbc += IXGB_READ_REG(&adapter->hw, ECBC);
  1300. adapter->stats.mpc += IXGB_READ_REG(&adapter->hw, MPC);
  1301. adapter->stats.tptl += IXGB_READ_REG(&adapter->hw, TPTL);
  1302. adapter->stats.tpth += IXGB_READ_REG(&adapter->hw, TPTH);
  1303. adapter->stats.gptcl += IXGB_READ_REG(&adapter->hw, GPTCL);
  1304. adapter->stats.gptch += IXGB_READ_REG(&adapter->hw, GPTCH);
  1305. adapter->stats.bptcl += IXGB_READ_REG(&adapter->hw, BPTCL);
  1306. adapter->stats.bptch += IXGB_READ_REG(&adapter->hw, BPTCH);
  1307. adapter->stats.mptcl += IXGB_READ_REG(&adapter->hw, MPTCL);
  1308. adapter->stats.mptch += IXGB_READ_REG(&adapter->hw, MPTCH);
  1309. adapter->stats.uptcl += IXGB_READ_REG(&adapter->hw, UPTCL);
  1310. adapter->stats.uptch += IXGB_READ_REG(&adapter->hw, UPTCH);
  1311. adapter->stats.vptcl += IXGB_READ_REG(&adapter->hw, VPTCL);
  1312. adapter->stats.vptch += IXGB_READ_REG(&adapter->hw, VPTCH);
  1313. adapter->stats.jptcl += IXGB_READ_REG(&adapter->hw, JPTCL);
  1314. adapter->stats.jptch += IXGB_READ_REG(&adapter->hw, JPTCH);
  1315. adapter->stats.gotcl += IXGB_READ_REG(&adapter->hw, GOTCL);
  1316. adapter->stats.gotch += IXGB_READ_REG(&adapter->hw, GOTCH);
  1317. adapter->stats.totl += IXGB_READ_REG(&adapter->hw, TOTL);
  1318. adapter->stats.toth += IXGB_READ_REG(&adapter->hw, TOTH);
  1319. adapter->stats.dc += IXGB_READ_REG(&adapter->hw, DC);
  1320. adapter->stats.plt64c += IXGB_READ_REG(&adapter->hw, PLT64C);
  1321. adapter->stats.tsctc += IXGB_READ_REG(&adapter->hw, TSCTC);
  1322. adapter->stats.tsctfc += IXGB_READ_REG(&adapter->hw, TSCTFC);
  1323. adapter->stats.ibic += IXGB_READ_REG(&adapter->hw, IBIC);
  1324. adapter->stats.rfc += IXGB_READ_REG(&adapter->hw, RFC);
  1325. adapter->stats.lfc += IXGB_READ_REG(&adapter->hw, LFC);
  1326. adapter->stats.pfrc += IXGB_READ_REG(&adapter->hw, PFRC);
  1327. adapter->stats.pftc += IXGB_READ_REG(&adapter->hw, PFTC);
  1328. adapter->stats.mcfrc += IXGB_READ_REG(&adapter->hw, MCFRC);
  1329. adapter->stats.mcftc += IXGB_READ_REG(&adapter->hw, MCFTC);
  1330. adapter->stats.xonrxc += IXGB_READ_REG(&adapter->hw, XONRXC);
  1331. adapter->stats.xontxc += IXGB_READ_REG(&adapter->hw, XONTXC);
  1332. adapter->stats.xoffrxc += IXGB_READ_REG(&adapter->hw, XOFFRXC);
  1333. adapter->stats.xofftxc += IXGB_READ_REG(&adapter->hw, XOFFTXC);
  1334. adapter->stats.rjc += IXGB_READ_REG(&adapter->hw, RJC);
  1335. /* Fill out the OS statistics structure */
  1336. adapter->net_stats.rx_packets = adapter->stats.gprcl;
  1337. adapter->net_stats.tx_packets = adapter->stats.gptcl;
  1338. adapter->net_stats.rx_bytes = adapter->stats.gorcl;
  1339. adapter->net_stats.tx_bytes = adapter->stats.gotcl;
  1340. adapter->net_stats.multicast = adapter->stats.mprcl;
  1341. adapter->net_stats.collisions = 0;
  1342. /* ignore RLEC as it reports errors for padded (<64bytes) frames
  1343. * with a length in the type/len field */
  1344. adapter->net_stats.rx_errors =
  1345. /* adapter->stats.rnbc + */ adapter->stats.crcerrs +
  1346. adapter->stats.ruc +
  1347. adapter->stats.roc /*+ adapter->stats.rlec */ +
  1348. adapter->stats.icbc +
  1349. adapter->stats.ecbc + adapter->stats.mpc;
  1350. /* see above
  1351. * adapter->net_stats.rx_length_errors = adapter->stats.rlec;
  1352. */
  1353. adapter->net_stats.rx_crc_errors = adapter->stats.crcerrs;
  1354. adapter->net_stats.rx_fifo_errors = adapter->stats.mpc;
  1355. adapter->net_stats.rx_missed_errors = adapter->stats.mpc;
  1356. adapter->net_stats.rx_over_errors = adapter->stats.mpc;
  1357. adapter->net_stats.tx_errors = 0;
  1358. adapter->net_stats.rx_frame_errors = 0;
  1359. adapter->net_stats.tx_aborted_errors = 0;
  1360. adapter->net_stats.tx_carrier_errors = 0;
  1361. adapter->net_stats.tx_fifo_errors = 0;
  1362. adapter->net_stats.tx_heartbeat_errors = 0;
  1363. adapter->net_stats.tx_window_errors = 0;
  1364. }
  1365. #define IXGB_MAX_INTR 10
  1366. /**
  1367. * ixgb_intr - Interrupt Handler
  1368. * @irq: interrupt number
  1369. * @data: pointer to a network interface device structure
  1370. * @pt_regs: CPU registers structure
  1371. **/
  1372. static irqreturn_t
  1373. ixgb_intr(int irq, void *data, struct pt_regs *regs)
  1374. {
  1375. struct net_device *netdev = data;
  1376. struct ixgb_adapter *adapter = netdev_priv(netdev);
  1377. struct ixgb_hw *hw = &adapter->hw;
  1378. uint32_t icr = IXGB_READ_REG(hw, ICR);
  1379. #ifndef CONFIG_IXGB_NAPI
  1380. unsigned int i;
  1381. #endif
  1382. if(unlikely(!icr))
  1383. return IRQ_NONE; /* Not our interrupt */
  1384. if(unlikely(icr & (IXGB_INT_RXSEQ | IXGB_INT_LSC))) {
  1385. mod_timer(&adapter->watchdog_timer, jiffies);
  1386. }
  1387. #ifdef CONFIG_IXGB_NAPI
  1388. if(netif_rx_schedule_prep(netdev)) {
  1389. /* Disable interrupts and register for poll. The flush
  1390. of the posted write is intentionally left out.
  1391. */
  1392. atomic_inc(&adapter->irq_sem);
  1393. IXGB_WRITE_REG(&adapter->hw, IMC, ~0);
  1394. __netif_rx_schedule(netdev);
  1395. }
  1396. #else
  1397. /* yes, that is actually a & and it is meant to make sure that
  1398. * every pass through this for loop checks both receive and
  1399. * transmit queues for completed descriptors, intended to
  1400. * avoid starvation issues and assist tx/rx fairness. */
  1401. for(i = 0; i < IXGB_MAX_INTR; i++)
  1402. if(!ixgb_clean_rx_irq(adapter) &
  1403. !ixgb_clean_tx_irq(adapter))
  1404. break;
  1405. #endif
  1406. return IRQ_HANDLED;
  1407. }
  1408. #ifdef CONFIG_IXGB_NAPI
  1409. /**
  1410. * ixgb_clean - NAPI Rx polling callback
  1411. * @adapter: board private structure
  1412. **/
  1413. static int
  1414. ixgb_clean(struct net_device *netdev, int *budget)
  1415. {
  1416. struct ixgb_adapter *adapter = netdev_priv(netdev);
  1417. int work_to_do = min(*budget, netdev->quota);
  1418. int tx_cleaned;
  1419. int work_done = 0;
  1420. tx_cleaned = ixgb_clean_tx_irq(adapter);
  1421. ixgb_clean_rx_irq(adapter, &work_done, work_to_do);
  1422. *budget -= work_done;
  1423. netdev->quota -= work_done;
  1424. /* if no Tx and not enough Rx work done, exit the polling mode */
  1425. if((!tx_cleaned && (work_done == 0)) || !netif_running(netdev)) {
  1426. netif_rx_complete(netdev);
  1427. ixgb_irq_enable(adapter);
  1428. return 0;
  1429. }
  1430. return 1;
  1431. }
  1432. #endif
  1433. /**
  1434. * ixgb_clean_tx_irq - Reclaim resources after transmit completes
  1435. * @adapter: board private structure
  1436. **/
  1437. static boolean_t
  1438. ixgb_clean_tx_irq(struct ixgb_adapter *adapter)
  1439. {
  1440. struct ixgb_desc_ring *tx_ring = &adapter->tx_ring;
  1441. struct net_device *netdev = adapter->netdev;
  1442. struct ixgb_tx_desc *tx_desc, *eop_desc;
  1443. struct ixgb_buffer *buffer_info;
  1444. unsigned int i, eop;
  1445. boolean_t cleaned = FALSE;
  1446. i = tx_ring->next_to_clean;
  1447. eop = tx_ring->buffer_info[i].next_to_watch;
  1448. eop_desc = IXGB_TX_DESC(*tx_ring, eop);
  1449. while(eop_desc->status & IXGB_TX_DESC_STATUS_DD) {
  1450. for(cleaned = FALSE; !cleaned; ) {
  1451. tx_desc = IXGB_TX_DESC(*tx_ring, i);
  1452. buffer_info = &tx_ring->buffer_info[i];
  1453. if (tx_desc->popts
  1454. & (IXGB_TX_DESC_POPTS_TXSM |
  1455. IXGB_TX_DESC_POPTS_IXSM))
  1456. adapter->hw_csum_tx_good++;
  1457. ixgb_unmap_and_free_tx_resource(adapter, buffer_info);
  1458. *(uint32_t *)&(tx_desc->status) = 0;
  1459. cleaned = (i == eop);
  1460. if(++i == tx_ring->count) i = 0;
  1461. }
  1462. eop = tx_ring->buffer_info[i].next_to_watch;
  1463. eop_desc = IXGB_TX_DESC(*tx_ring, eop);
  1464. }
  1465. tx_ring->next_to_clean = i;
  1466. spin_lock(&adapter->tx_lock);
  1467. if(cleaned && netif_queue_stopped(netdev) && netif_carrier_ok(netdev) &&
  1468. (IXGB_DESC_UNUSED(tx_ring) > IXGB_TX_QUEUE_WAKE)) {
  1469. netif_wake_queue(netdev);
  1470. }
  1471. spin_unlock(&adapter->tx_lock);
  1472. if(adapter->detect_tx_hung) {
  1473. /* detect a transmit hang in hardware, this serializes the
  1474. * check with the clearing of time_stamp and movement of i */
  1475. adapter->detect_tx_hung = FALSE;
  1476. if(tx_ring->buffer_info[i].dma &&
  1477. time_after(jiffies, tx_ring->buffer_info[i].time_stamp + HZ)
  1478. && !(IXGB_READ_REG(&adapter->hw, STATUS) &
  1479. IXGB_STATUS_TXOFF))
  1480. netif_stop_queue(netdev);
  1481. }
  1482. return cleaned;
  1483. }
  1484. /**
  1485. * ixgb_rx_checksum - Receive Checksum Offload for 82597.
  1486. * @adapter: board private structure
  1487. * @rx_desc: receive descriptor
  1488. * @sk_buff: socket buffer with received data
  1489. **/
  1490. static inline void
  1491. ixgb_rx_checksum(struct ixgb_adapter *adapter,
  1492. struct ixgb_rx_desc *rx_desc,
  1493. struct sk_buff *skb)
  1494. {
  1495. /* Ignore Checksum bit is set OR
  1496. * TCP Checksum has not been calculated
  1497. */
  1498. if((rx_desc->status & IXGB_RX_DESC_STATUS_IXSM) ||
  1499. (!(rx_desc->status & IXGB_RX_DESC_STATUS_TCPCS))) {
  1500. skb->ip_summed = CHECKSUM_NONE;
  1501. return;
  1502. }
  1503. /* At this point we know the hardware did the TCP checksum */
  1504. /* now look at the TCP checksum error bit */
  1505. if(rx_desc->errors & IXGB_RX_DESC_ERRORS_TCPE) {
  1506. /* let the stack verify checksum errors */
  1507. skb->ip_summed = CHECKSUM_NONE;
  1508. adapter->hw_csum_rx_error++;
  1509. } else {
  1510. /* TCP checksum is good */
  1511. skb->ip_summed = CHECKSUM_UNNECESSARY;
  1512. adapter->hw_csum_rx_good++;
  1513. }
  1514. }
  1515. /**
  1516. * ixgb_clean_rx_irq - Send received data up the network stack,
  1517. * @adapter: board private structure
  1518. **/
  1519. static boolean_t
  1520. #ifdef CONFIG_IXGB_NAPI
  1521. ixgb_clean_rx_irq(struct ixgb_adapter *adapter, int *work_done, int work_to_do)
  1522. #else
  1523. ixgb_clean_rx_irq(struct ixgb_adapter *adapter)
  1524. #endif
  1525. {
  1526. struct ixgb_desc_ring *rx_ring = &adapter->rx_ring;
  1527. struct net_device *netdev = adapter->netdev;
  1528. struct pci_dev *pdev = adapter->pdev;
  1529. struct ixgb_rx_desc *rx_desc, *next_rxd;
  1530. struct ixgb_buffer *buffer_info, *next_buffer, *next2_buffer;
  1531. uint32_t length;
  1532. unsigned int i, j;
  1533. boolean_t cleaned = FALSE;
  1534. i = rx_ring->next_to_clean;
  1535. rx_desc = IXGB_RX_DESC(*rx_ring, i);
  1536. buffer_info = &rx_ring->buffer_info[i];
  1537. while(rx_desc->status & IXGB_RX_DESC_STATUS_DD) {
  1538. struct sk_buff *skb, *next_skb;
  1539. u8 status;
  1540. #ifdef CONFIG_IXGB_NAPI
  1541. if(*work_done >= work_to_do)
  1542. break;
  1543. (*work_done)++;
  1544. #endif
  1545. status = rx_desc->status;
  1546. skb = buffer_info->skb;
  1547. prefetch(skb->data);
  1548. if(++i == rx_ring->count) i = 0;
  1549. next_rxd = IXGB_RX_DESC(*rx_ring, i);
  1550. prefetch(next_rxd);
  1551. if((j = i + 1) == rx_ring->count) j = 0;
  1552. next2_buffer = &rx_ring->buffer_info[j];
  1553. prefetch(next2_buffer);
  1554. next_buffer = &rx_ring->buffer_info[i];
  1555. next_skb = next_buffer->skb;
  1556. prefetch(next_skb);
  1557. cleaned = TRUE;
  1558. pci_unmap_single(pdev,
  1559. buffer_info->dma,
  1560. buffer_info->length,
  1561. PCI_DMA_FROMDEVICE);
  1562. length = le16_to_cpu(rx_desc->length);
  1563. if(unlikely(!(status & IXGB_RX_DESC_STATUS_EOP))) {
  1564. /* All receives must fit into a single buffer */
  1565. IXGB_DBG("Receive packet consumed multiple buffers "
  1566. "length<%x>\n", length);
  1567. dev_kfree_skb_irq(skb);
  1568. goto rxdesc_done;
  1569. }
  1570. if (unlikely(rx_desc->errors
  1571. & (IXGB_RX_DESC_ERRORS_CE | IXGB_RX_DESC_ERRORS_SE
  1572. | IXGB_RX_DESC_ERRORS_P |
  1573. IXGB_RX_DESC_ERRORS_RXE))) {
  1574. dev_kfree_skb_irq(skb);
  1575. goto rxdesc_done;
  1576. }
  1577. /* Good Receive */
  1578. skb_put(skb, length);
  1579. /* Receive Checksum Offload */
  1580. ixgb_rx_checksum(adapter, rx_desc, skb);
  1581. skb->protocol = eth_type_trans(skb, netdev);
  1582. #ifdef CONFIG_IXGB_NAPI
  1583. if(adapter->vlgrp && (status & IXGB_RX_DESC_STATUS_VP)) {
  1584. vlan_hwaccel_receive_skb(skb, adapter->vlgrp,
  1585. le16_to_cpu(rx_desc->special) &
  1586. IXGB_RX_DESC_SPECIAL_VLAN_MASK);
  1587. } else {
  1588. netif_receive_skb(skb);
  1589. }
  1590. #else /* CONFIG_IXGB_NAPI */
  1591. if(adapter->vlgrp && (status & IXGB_RX_DESC_STATUS_VP)) {
  1592. vlan_hwaccel_rx(skb, adapter->vlgrp,
  1593. le16_to_cpu(rx_desc->special) &
  1594. IXGB_RX_DESC_SPECIAL_VLAN_MASK);
  1595. } else {
  1596. netif_rx(skb);
  1597. }
  1598. #endif /* CONFIG_IXGB_NAPI */
  1599. netdev->last_rx = jiffies;
  1600. rxdesc_done:
  1601. /* clean up descriptor, might be written over by hw */
  1602. rx_desc->status = 0;
  1603. buffer_info->skb = NULL;
  1604. /* use prefetched values */
  1605. rx_desc = next_rxd;
  1606. buffer_info = next_buffer;
  1607. }
  1608. rx_ring->next_to_clean = i;
  1609. ixgb_alloc_rx_buffers(adapter);
  1610. return cleaned;
  1611. }
  1612. /**
  1613. * ixgb_alloc_rx_buffers - Replace used receive buffers
  1614. * @adapter: address of board private structure
  1615. **/
  1616. static void
  1617. ixgb_alloc_rx_buffers(struct ixgb_adapter *adapter)
  1618. {
  1619. struct ixgb_desc_ring *rx_ring = &adapter->rx_ring;
  1620. struct net_device *netdev = adapter->netdev;
  1621. struct pci_dev *pdev = adapter->pdev;
  1622. struct ixgb_rx_desc *rx_desc;
  1623. struct ixgb_buffer *buffer_info;
  1624. struct sk_buff *skb;
  1625. unsigned int i;
  1626. int num_group_tail_writes;
  1627. long cleancount;
  1628. i = rx_ring->next_to_use;
  1629. buffer_info = &rx_ring->buffer_info[i];
  1630. cleancount = IXGB_DESC_UNUSED(rx_ring);
  1631. num_group_tail_writes = IXGB_RX_BUFFER_WRITE;
  1632. /* leave three descriptors unused */
  1633. while(--cleancount > 2) {
  1634. rx_desc = IXGB_RX_DESC(*rx_ring, i);
  1635. skb = dev_alloc_skb(adapter->rx_buffer_len + NET_IP_ALIGN);
  1636. if(unlikely(!skb)) {
  1637. /* Better luck next round */
  1638. break;
  1639. }
  1640. /* Make buffer alignment 2 beyond a 16 byte boundary
  1641. * this will result in a 16 byte aligned IP header after
  1642. * the 14 byte MAC header is removed
  1643. */
  1644. skb_reserve(skb, NET_IP_ALIGN);
  1645. skb->dev = netdev;
  1646. buffer_info->skb = skb;
  1647. buffer_info->length = adapter->rx_buffer_len;
  1648. buffer_info->dma =
  1649. pci_map_single(pdev,
  1650. skb->data,
  1651. adapter->rx_buffer_len,
  1652. PCI_DMA_FROMDEVICE);
  1653. rx_desc->buff_addr = cpu_to_le64(buffer_info->dma);
  1654. /* guarantee DD bit not set now before h/w gets descriptor
  1655. * this is the rest of the workaround for h/w double
  1656. * writeback. */
  1657. rx_desc->status = 0;
  1658. if((i & ~(num_group_tail_writes- 1)) == i) {
  1659. /* Force memory writes to complete before letting h/w
  1660. * know there are new descriptors to fetch. (Only
  1661. * applicable for weak-ordered memory model archs,
  1662. * such as IA-64). */
  1663. wmb();
  1664. IXGB_WRITE_REG(&adapter->hw, RDT, i);
  1665. }
  1666. if(++i == rx_ring->count) i = 0;
  1667. buffer_info = &rx_ring->buffer_info[i];
  1668. }
  1669. rx_ring->next_to_use = i;
  1670. }
  1671. /**
  1672. * ixgb_vlan_rx_register - enables or disables vlan tagging/stripping.
  1673. *
  1674. * @param netdev network interface device structure
  1675. * @param grp indicates to enable or disable tagging/stripping
  1676. **/
  1677. static void
  1678. ixgb_vlan_rx_register(struct net_device *netdev, struct vlan_group *grp)
  1679. {
  1680. struct ixgb_adapter *adapter = netdev_priv(netdev);
  1681. uint32_t ctrl, rctl;
  1682. ixgb_irq_disable(adapter);
  1683. adapter->vlgrp = grp;
  1684. if(grp) {
  1685. /* enable VLAN tag insert/strip */
  1686. ctrl = IXGB_READ_REG(&adapter->hw, CTRL0);
  1687. ctrl |= IXGB_CTRL0_VME;
  1688. IXGB_WRITE_REG(&adapter->hw, CTRL0, ctrl);
  1689. /* enable VLAN receive filtering */
  1690. rctl = IXGB_READ_REG(&adapter->hw, RCTL);
  1691. rctl |= IXGB_RCTL_VFE;
  1692. rctl &= ~IXGB_RCTL_CFIEN;
  1693. IXGB_WRITE_REG(&adapter->hw, RCTL, rctl);
  1694. } else {
  1695. /* disable VLAN tag insert/strip */
  1696. ctrl = IXGB_READ_REG(&adapter->hw, CTRL0);
  1697. ctrl &= ~IXGB_CTRL0_VME;
  1698. IXGB_WRITE_REG(&adapter->hw, CTRL0, ctrl);
  1699. /* disable VLAN filtering */
  1700. rctl = IXGB_READ_REG(&adapter->hw, RCTL);
  1701. rctl &= ~IXGB_RCTL_VFE;
  1702. IXGB_WRITE_REG(&adapter->hw, RCTL, rctl);
  1703. }
  1704. ixgb_irq_enable(adapter);
  1705. }
  1706. static void
  1707. ixgb_vlan_rx_add_vid(struct net_device *netdev, uint16_t vid)
  1708. {
  1709. struct ixgb_adapter *adapter = netdev_priv(netdev);
  1710. uint32_t vfta, index;
  1711. /* add VID to filter table */
  1712. index = (vid >> 5) & 0x7F;
  1713. vfta = IXGB_READ_REG_ARRAY(&adapter->hw, VFTA, index);
  1714. vfta |= (1 << (vid & 0x1F));
  1715. ixgb_write_vfta(&adapter->hw, index, vfta);
  1716. }
  1717. static void
  1718. ixgb_vlan_rx_kill_vid(struct net_device *netdev, uint16_t vid)
  1719. {
  1720. struct ixgb_adapter *adapter = netdev_priv(netdev);
  1721. uint32_t vfta, index;
  1722. ixgb_irq_disable(adapter);
  1723. if(adapter->vlgrp)
  1724. adapter->vlgrp->vlan_devices[vid] = NULL;
  1725. ixgb_irq_enable(adapter);
  1726. /* remove VID from filter table*/
  1727. index = (vid >> 5) & 0x7F;
  1728. vfta = IXGB_READ_REG_ARRAY(&adapter->hw, VFTA, index);
  1729. vfta &= ~(1 << (vid & 0x1F));
  1730. ixgb_write_vfta(&adapter->hw, index, vfta);
  1731. }
  1732. static void
  1733. ixgb_restore_vlan(struct ixgb_adapter *adapter)
  1734. {
  1735. ixgb_vlan_rx_register(adapter->netdev, adapter->vlgrp);
  1736. if(adapter->vlgrp) {
  1737. uint16_t vid;
  1738. for(vid = 0; vid < VLAN_GROUP_ARRAY_LEN; vid++) {
  1739. if(!adapter->vlgrp->vlan_devices[vid])
  1740. continue;
  1741. ixgb_vlan_rx_add_vid(adapter->netdev, vid);
  1742. }
  1743. }
  1744. }
  1745. #ifdef CONFIG_NET_POLL_CONTROLLER
  1746. /*
  1747. * Polling 'interrupt' - used by things like netconsole to send skbs
  1748. * without having to re-enable interrupts. It's not called while
  1749. * the interrupt routine is executing.
  1750. */
  1751. static void ixgb_netpoll(struct net_device *dev)
  1752. {
  1753. struct ixgb_adapter *adapter = dev->priv;
  1754. disable_irq(adapter->pdev->irq);
  1755. ixgb_intr(adapter->pdev->irq, dev, NULL);
  1756. enable_irq(adapter->pdev->irq);
  1757. }
  1758. #endif
  1759. /* ixgb_main.c */