ixgb_main.c 63 KB

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