ixgb_main.c 56 KB

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