igc_main.c 92 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright (c) 2018 Intel Corporation */
  3. #include <linux/module.h>
  4. #include <linux/types.h>
  5. #include <linux/if_vlan.h>
  6. #include <linux/aer.h>
  7. #include "igc.h"
  8. #include "igc_hw.h"
  9. #define DRV_VERSION "0.0.1-k"
  10. #define DRV_SUMMARY "Intel(R) 2.5G Ethernet Linux Driver"
  11. static int debug = -1;
  12. MODULE_AUTHOR("Intel Corporation, <linux.nics@intel.com>");
  13. MODULE_DESCRIPTION(DRV_SUMMARY);
  14. MODULE_LICENSE("GPL v2");
  15. MODULE_VERSION(DRV_VERSION);
  16. module_param(debug, int, 0);
  17. MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all)");
  18. char igc_driver_name[] = "igc";
  19. char igc_driver_version[] = DRV_VERSION;
  20. static const char igc_driver_string[] = DRV_SUMMARY;
  21. static const char igc_copyright[] =
  22. "Copyright(c) 2018 Intel Corporation.";
  23. static const struct igc_info *igc_info_tbl[] = {
  24. [board_base] = &igc_base_info,
  25. };
  26. static const struct pci_device_id igc_pci_tbl[] = {
  27. { PCI_VDEVICE(INTEL, IGC_DEV_ID_I225_LM), board_base },
  28. { PCI_VDEVICE(INTEL, IGC_DEV_ID_I225_V), board_base },
  29. /* required last entry */
  30. {0, }
  31. };
  32. MODULE_DEVICE_TABLE(pci, igc_pci_tbl);
  33. /* forward declaration */
  34. static void igc_clean_tx_ring(struct igc_ring *tx_ring);
  35. static int igc_sw_init(struct igc_adapter *);
  36. static void igc_configure(struct igc_adapter *adapter);
  37. static void igc_power_down_link(struct igc_adapter *adapter);
  38. static void igc_set_default_mac_filter(struct igc_adapter *adapter);
  39. static void igc_set_rx_mode(struct net_device *netdev);
  40. static void igc_write_itr(struct igc_q_vector *q_vector);
  41. static void igc_assign_vector(struct igc_q_vector *q_vector, int msix_vector);
  42. static void igc_free_q_vector(struct igc_adapter *adapter, int v_idx);
  43. static void igc_set_interrupt_capability(struct igc_adapter *adapter,
  44. bool msix);
  45. static void igc_free_q_vectors(struct igc_adapter *adapter);
  46. static void igc_irq_disable(struct igc_adapter *adapter);
  47. static void igc_irq_enable(struct igc_adapter *adapter);
  48. static void igc_configure_msix(struct igc_adapter *adapter);
  49. static bool igc_alloc_mapped_page(struct igc_ring *rx_ring,
  50. struct igc_rx_buffer *bi);
  51. enum latency_range {
  52. lowest_latency = 0,
  53. low_latency = 1,
  54. bulk_latency = 2,
  55. latency_invalid = 255
  56. };
  57. static void igc_reset(struct igc_adapter *adapter)
  58. {
  59. struct pci_dev *pdev = adapter->pdev;
  60. struct igc_hw *hw = &adapter->hw;
  61. hw->mac.ops.reset_hw(hw);
  62. if (hw->mac.ops.init_hw(hw))
  63. dev_err(&pdev->dev, "Hardware Error\n");
  64. if (!netif_running(adapter->netdev))
  65. igc_power_down_link(adapter);
  66. igc_get_phy_info(hw);
  67. }
  68. /**
  69. * igc_power_up_link - Power up the phy/serdes link
  70. * @adapter: address of board private structure
  71. */
  72. static void igc_power_up_link(struct igc_adapter *adapter)
  73. {
  74. igc_reset_phy(&adapter->hw);
  75. if (adapter->hw.phy.media_type == igc_media_type_copper)
  76. igc_power_up_phy_copper(&adapter->hw);
  77. igc_setup_link(&adapter->hw);
  78. }
  79. /**
  80. * igc_power_down_link - Power down the phy/serdes link
  81. * @adapter: address of board private structure
  82. */
  83. static void igc_power_down_link(struct igc_adapter *adapter)
  84. {
  85. if (adapter->hw.phy.media_type == igc_media_type_copper)
  86. igc_power_down_phy_copper_base(&adapter->hw);
  87. }
  88. /**
  89. * igc_release_hw_control - release control of the h/w to f/w
  90. * @adapter: address of board private structure
  91. *
  92. * igc_release_hw_control resets CTRL_EXT:DRV_LOAD bit.
  93. * For ASF and Pass Through versions of f/w this means that the
  94. * driver is no longer loaded.
  95. */
  96. static void igc_release_hw_control(struct igc_adapter *adapter)
  97. {
  98. struct igc_hw *hw = &adapter->hw;
  99. u32 ctrl_ext;
  100. /* Let firmware take over control of h/w */
  101. ctrl_ext = rd32(IGC_CTRL_EXT);
  102. wr32(IGC_CTRL_EXT,
  103. ctrl_ext & ~IGC_CTRL_EXT_DRV_LOAD);
  104. }
  105. /**
  106. * igc_get_hw_control - get control of the h/w from f/w
  107. * @adapter: address of board private structure
  108. *
  109. * igc_get_hw_control sets CTRL_EXT:DRV_LOAD bit.
  110. * For ASF and Pass Through versions of f/w this means that
  111. * the driver is loaded.
  112. */
  113. static void igc_get_hw_control(struct igc_adapter *adapter)
  114. {
  115. struct igc_hw *hw = &adapter->hw;
  116. u32 ctrl_ext;
  117. /* Let firmware know the driver has taken over */
  118. ctrl_ext = rd32(IGC_CTRL_EXT);
  119. wr32(IGC_CTRL_EXT,
  120. ctrl_ext | IGC_CTRL_EXT_DRV_LOAD);
  121. }
  122. /**
  123. * igc_free_tx_resources - Free Tx Resources per Queue
  124. * @tx_ring: Tx descriptor ring for a specific queue
  125. *
  126. * Free all transmit software resources
  127. */
  128. static void igc_free_tx_resources(struct igc_ring *tx_ring)
  129. {
  130. igc_clean_tx_ring(tx_ring);
  131. vfree(tx_ring->tx_buffer_info);
  132. tx_ring->tx_buffer_info = NULL;
  133. /* if not set, then don't free */
  134. if (!tx_ring->desc)
  135. return;
  136. dma_free_coherent(tx_ring->dev, tx_ring->size,
  137. tx_ring->desc, tx_ring->dma);
  138. tx_ring->desc = NULL;
  139. }
  140. /**
  141. * igc_free_all_tx_resources - Free Tx Resources for All Queues
  142. * @adapter: board private structure
  143. *
  144. * Free all transmit software resources
  145. */
  146. static void igc_free_all_tx_resources(struct igc_adapter *adapter)
  147. {
  148. int i;
  149. for (i = 0; i < adapter->num_tx_queues; i++)
  150. igc_free_tx_resources(adapter->tx_ring[i]);
  151. }
  152. /**
  153. * igc_clean_tx_ring - Free Tx Buffers
  154. * @tx_ring: ring to be cleaned
  155. */
  156. static void igc_clean_tx_ring(struct igc_ring *tx_ring)
  157. {
  158. u16 i = tx_ring->next_to_clean;
  159. struct igc_tx_buffer *tx_buffer = &tx_ring->tx_buffer_info[i];
  160. while (i != tx_ring->next_to_use) {
  161. union igc_adv_tx_desc *eop_desc, *tx_desc;
  162. /* Free all the Tx ring sk_buffs */
  163. dev_kfree_skb_any(tx_buffer->skb);
  164. /* unmap skb header data */
  165. dma_unmap_single(tx_ring->dev,
  166. dma_unmap_addr(tx_buffer, dma),
  167. dma_unmap_len(tx_buffer, len),
  168. DMA_TO_DEVICE);
  169. /* check for eop_desc to determine the end of the packet */
  170. eop_desc = tx_buffer->next_to_watch;
  171. tx_desc = IGC_TX_DESC(tx_ring, i);
  172. /* unmap remaining buffers */
  173. while (tx_desc != eop_desc) {
  174. tx_buffer++;
  175. tx_desc++;
  176. i++;
  177. if (unlikely(i == tx_ring->count)) {
  178. i = 0;
  179. tx_buffer = tx_ring->tx_buffer_info;
  180. tx_desc = IGC_TX_DESC(tx_ring, 0);
  181. }
  182. /* unmap any remaining paged data */
  183. if (dma_unmap_len(tx_buffer, len))
  184. dma_unmap_page(tx_ring->dev,
  185. dma_unmap_addr(tx_buffer, dma),
  186. dma_unmap_len(tx_buffer, len),
  187. DMA_TO_DEVICE);
  188. }
  189. /* move us one more past the eop_desc for start of next pkt */
  190. tx_buffer++;
  191. i++;
  192. if (unlikely(i == tx_ring->count)) {
  193. i = 0;
  194. tx_buffer = tx_ring->tx_buffer_info;
  195. }
  196. }
  197. /* reset BQL for queue */
  198. netdev_tx_reset_queue(txring_txq(tx_ring));
  199. /* reset next_to_use and next_to_clean */
  200. tx_ring->next_to_use = 0;
  201. tx_ring->next_to_clean = 0;
  202. }
  203. /**
  204. * igc_clean_all_tx_rings - Free Tx Buffers for all queues
  205. * @adapter: board private structure
  206. */
  207. static void igc_clean_all_tx_rings(struct igc_adapter *adapter)
  208. {
  209. int i;
  210. for (i = 0; i < adapter->num_tx_queues; i++)
  211. if (adapter->tx_ring[i])
  212. igc_clean_tx_ring(adapter->tx_ring[i]);
  213. }
  214. /**
  215. * igc_setup_tx_resources - allocate Tx resources (Descriptors)
  216. * @tx_ring: tx descriptor ring (for a specific queue) to setup
  217. *
  218. * Return 0 on success, negative on failure
  219. */
  220. static int igc_setup_tx_resources(struct igc_ring *tx_ring)
  221. {
  222. struct device *dev = tx_ring->dev;
  223. int size = 0;
  224. size = sizeof(struct igc_tx_buffer) * tx_ring->count;
  225. tx_ring->tx_buffer_info = vzalloc(size);
  226. if (!tx_ring->tx_buffer_info)
  227. goto err;
  228. /* round up to nearest 4K */
  229. tx_ring->size = tx_ring->count * sizeof(union igc_adv_tx_desc);
  230. tx_ring->size = ALIGN(tx_ring->size, 4096);
  231. tx_ring->desc = dma_alloc_coherent(dev, tx_ring->size,
  232. &tx_ring->dma, GFP_KERNEL);
  233. if (!tx_ring->desc)
  234. goto err;
  235. tx_ring->next_to_use = 0;
  236. tx_ring->next_to_clean = 0;
  237. return 0;
  238. err:
  239. vfree(tx_ring->tx_buffer_info);
  240. dev_err(dev,
  241. "Unable to allocate memory for the transmit descriptor ring\n");
  242. return -ENOMEM;
  243. }
  244. /**
  245. * igc_setup_all_tx_resources - wrapper to allocate Tx resources for all queues
  246. * @adapter: board private structure
  247. *
  248. * Return 0 on success, negative on failure
  249. */
  250. static int igc_setup_all_tx_resources(struct igc_adapter *adapter)
  251. {
  252. struct pci_dev *pdev = adapter->pdev;
  253. int i, err = 0;
  254. for (i = 0; i < adapter->num_tx_queues; i++) {
  255. err = igc_setup_tx_resources(adapter->tx_ring[i]);
  256. if (err) {
  257. dev_err(&pdev->dev,
  258. "Allocation for Tx Queue %u failed\n", i);
  259. for (i--; i >= 0; i--)
  260. igc_free_tx_resources(adapter->tx_ring[i]);
  261. break;
  262. }
  263. }
  264. return err;
  265. }
  266. /**
  267. * igc_clean_rx_ring - Free Rx Buffers per Queue
  268. * @rx_ring: ring to free buffers from
  269. */
  270. static void igc_clean_rx_ring(struct igc_ring *rx_ring)
  271. {
  272. u16 i = rx_ring->next_to_clean;
  273. if (rx_ring->skb)
  274. dev_kfree_skb(rx_ring->skb);
  275. rx_ring->skb = NULL;
  276. /* Free all the Rx ring sk_buffs */
  277. while (i != rx_ring->next_to_alloc) {
  278. struct igc_rx_buffer *buffer_info = &rx_ring->rx_buffer_info[i];
  279. /* Invalidate cache lines that may have been written to by
  280. * device so that we avoid corrupting memory.
  281. */
  282. dma_sync_single_range_for_cpu(rx_ring->dev,
  283. buffer_info->dma,
  284. buffer_info->page_offset,
  285. igc_rx_bufsz(rx_ring),
  286. DMA_FROM_DEVICE);
  287. /* free resources associated with mapping */
  288. dma_unmap_page_attrs(rx_ring->dev,
  289. buffer_info->dma,
  290. igc_rx_pg_size(rx_ring),
  291. DMA_FROM_DEVICE,
  292. IGC_RX_DMA_ATTR);
  293. __page_frag_cache_drain(buffer_info->page,
  294. buffer_info->pagecnt_bias);
  295. i++;
  296. if (i == rx_ring->count)
  297. i = 0;
  298. }
  299. rx_ring->next_to_alloc = 0;
  300. rx_ring->next_to_clean = 0;
  301. rx_ring->next_to_use = 0;
  302. }
  303. /**
  304. * igc_clean_all_rx_rings - Free Rx Buffers for all queues
  305. * @adapter: board private structure
  306. */
  307. static void igc_clean_all_rx_rings(struct igc_adapter *adapter)
  308. {
  309. int i;
  310. for (i = 0; i < adapter->num_rx_queues; i++)
  311. if (adapter->rx_ring[i])
  312. igc_clean_rx_ring(adapter->rx_ring[i]);
  313. }
  314. /**
  315. * igc_free_rx_resources - Free Rx Resources
  316. * @rx_ring: ring to clean the resources from
  317. *
  318. * Free all receive software resources
  319. */
  320. static void igc_free_rx_resources(struct igc_ring *rx_ring)
  321. {
  322. igc_clean_rx_ring(rx_ring);
  323. vfree(rx_ring->rx_buffer_info);
  324. rx_ring->rx_buffer_info = NULL;
  325. /* if not set, then don't free */
  326. if (!rx_ring->desc)
  327. return;
  328. dma_free_coherent(rx_ring->dev, rx_ring->size,
  329. rx_ring->desc, rx_ring->dma);
  330. rx_ring->desc = NULL;
  331. }
  332. /**
  333. * igc_free_all_rx_resources - Free Rx Resources for All Queues
  334. * @adapter: board private structure
  335. *
  336. * Free all receive software resources
  337. */
  338. static void igc_free_all_rx_resources(struct igc_adapter *adapter)
  339. {
  340. int i;
  341. for (i = 0; i < adapter->num_rx_queues; i++)
  342. igc_free_rx_resources(adapter->rx_ring[i]);
  343. }
  344. /**
  345. * igc_setup_rx_resources - allocate Rx resources (Descriptors)
  346. * @rx_ring: rx descriptor ring (for a specific queue) to setup
  347. *
  348. * Returns 0 on success, negative on failure
  349. */
  350. static int igc_setup_rx_resources(struct igc_ring *rx_ring)
  351. {
  352. struct device *dev = rx_ring->dev;
  353. int size, desc_len;
  354. size = sizeof(struct igc_rx_buffer) * rx_ring->count;
  355. rx_ring->rx_buffer_info = vzalloc(size);
  356. if (!rx_ring->rx_buffer_info)
  357. goto err;
  358. desc_len = sizeof(union igc_adv_rx_desc);
  359. /* Round up to nearest 4K */
  360. rx_ring->size = rx_ring->count * desc_len;
  361. rx_ring->size = ALIGN(rx_ring->size, 4096);
  362. rx_ring->desc = dma_alloc_coherent(dev, rx_ring->size,
  363. &rx_ring->dma, GFP_KERNEL);
  364. if (!rx_ring->desc)
  365. goto err;
  366. rx_ring->next_to_alloc = 0;
  367. rx_ring->next_to_clean = 0;
  368. rx_ring->next_to_use = 0;
  369. return 0;
  370. err:
  371. vfree(rx_ring->rx_buffer_info);
  372. rx_ring->rx_buffer_info = NULL;
  373. dev_err(dev,
  374. "Unable to allocate memory for the receive descriptor ring\n");
  375. return -ENOMEM;
  376. }
  377. /**
  378. * igc_setup_all_rx_resources - wrapper to allocate Rx resources
  379. * (Descriptors) for all queues
  380. * @adapter: board private structure
  381. *
  382. * Return 0 on success, negative on failure
  383. */
  384. static int igc_setup_all_rx_resources(struct igc_adapter *adapter)
  385. {
  386. struct pci_dev *pdev = adapter->pdev;
  387. int i, err = 0;
  388. for (i = 0; i < adapter->num_rx_queues; i++) {
  389. err = igc_setup_rx_resources(adapter->rx_ring[i]);
  390. if (err) {
  391. dev_err(&pdev->dev,
  392. "Allocation for Rx Queue %u failed\n", i);
  393. for (i--; i >= 0; i--)
  394. igc_free_rx_resources(adapter->rx_ring[i]);
  395. break;
  396. }
  397. }
  398. return err;
  399. }
  400. /**
  401. * igc_configure_rx_ring - Configure a receive ring after Reset
  402. * @adapter: board private structure
  403. * @ring: receive ring to be configured
  404. *
  405. * Configure the Rx unit of the MAC after a reset.
  406. */
  407. static void igc_configure_rx_ring(struct igc_adapter *adapter,
  408. struct igc_ring *ring)
  409. {
  410. struct igc_hw *hw = &adapter->hw;
  411. union igc_adv_rx_desc *rx_desc;
  412. int reg_idx = ring->reg_idx;
  413. u32 srrctl = 0, rxdctl = 0;
  414. u64 rdba = ring->dma;
  415. /* disable the queue */
  416. wr32(IGC_RXDCTL(reg_idx), 0);
  417. /* Set DMA base address registers */
  418. wr32(IGC_RDBAL(reg_idx),
  419. rdba & 0x00000000ffffffffULL);
  420. wr32(IGC_RDBAH(reg_idx), rdba >> 32);
  421. wr32(IGC_RDLEN(reg_idx),
  422. ring->count * sizeof(union igc_adv_rx_desc));
  423. /* initialize head and tail */
  424. ring->tail = adapter->io_addr + IGC_RDT(reg_idx);
  425. wr32(IGC_RDH(reg_idx), 0);
  426. writel(0, ring->tail);
  427. /* reset next-to- use/clean to place SW in sync with hardware */
  428. ring->next_to_clean = 0;
  429. ring->next_to_use = 0;
  430. /* set descriptor configuration */
  431. srrctl = IGC_RX_HDR_LEN << IGC_SRRCTL_BSIZEHDRSIZE_SHIFT;
  432. if (ring_uses_large_buffer(ring))
  433. srrctl |= IGC_RXBUFFER_3072 >> IGC_SRRCTL_BSIZEPKT_SHIFT;
  434. else
  435. srrctl |= IGC_RXBUFFER_2048 >> IGC_SRRCTL_BSIZEPKT_SHIFT;
  436. srrctl |= IGC_SRRCTL_DESCTYPE_ADV_ONEBUF;
  437. wr32(IGC_SRRCTL(reg_idx), srrctl);
  438. rxdctl |= IGC_RX_PTHRESH;
  439. rxdctl |= IGC_RX_HTHRESH << 8;
  440. rxdctl |= IGC_RX_WTHRESH << 16;
  441. /* initialize rx_buffer_info */
  442. memset(ring->rx_buffer_info, 0,
  443. sizeof(struct igc_rx_buffer) * ring->count);
  444. /* initialize Rx descriptor 0 */
  445. rx_desc = IGC_RX_DESC(ring, 0);
  446. rx_desc->wb.upper.length = 0;
  447. /* enable receive descriptor fetching */
  448. rxdctl |= IGC_RXDCTL_QUEUE_ENABLE;
  449. wr32(IGC_RXDCTL(reg_idx), rxdctl);
  450. }
  451. /**
  452. * igc_configure_rx - Configure receive Unit after Reset
  453. * @adapter: board private structure
  454. *
  455. * Configure the Rx unit of the MAC after a reset.
  456. */
  457. static void igc_configure_rx(struct igc_adapter *adapter)
  458. {
  459. int i;
  460. /* Setup the HW Rx Head and Tail Descriptor Pointers and
  461. * the Base and Length of the Rx Descriptor Ring
  462. */
  463. for (i = 0; i < adapter->num_rx_queues; i++)
  464. igc_configure_rx_ring(adapter, adapter->rx_ring[i]);
  465. }
  466. /**
  467. * igc_configure_tx_ring - Configure transmit ring after Reset
  468. * @adapter: board private structure
  469. * @ring: tx ring to configure
  470. *
  471. * Configure a transmit ring after a reset.
  472. */
  473. static void igc_configure_tx_ring(struct igc_adapter *adapter,
  474. struct igc_ring *ring)
  475. {
  476. struct igc_hw *hw = &adapter->hw;
  477. int reg_idx = ring->reg_idx;
  478. u64 tdba = ring->dma;
  479. u32 txdctl = 0;
  480. /* disable the queue */
  481. wr32(IGC_TXDCTL(reg_idx), 0);
  482. wrfl();
  483. mdelay(10);
  484. wr32(IGC_TDLEN(reg_idx),
  485. ring->count * sizeof(union igc_adv_tx_desc));
  486. wr32(IGC_TDBAL(reg_idx),
  487. tdba & 0x00000000ffffffffULL);
  488. wr32(IGC_TDBAH(reg_idx), tdba >> 32);
  489. ring->tail = adapter->io_addr + IGC_TDT(reg_idx);
  490. wr32(IGC_TDH(reg_idx), 0);
  491. writel(0, ring->tail);
  492. txdctl |= IGC_TX_PTHRESH;
  493. txdctl |= IGC_TX_HTHRESH << 8;
  494. txdctl |= IGC_TX_WTHRESH << 16;
  495. txdctl |= IGC_TXDCTL_QUEUE_ENABLE;
  496. wr32(IGC_TXDCTL(reg_idx), txdctl);
  497. }
  498. /**
  499. * igc_configure_tx - Configure transmit Unit after Reset
  500. * @adapter: board private structure
  501. *
  502. * Configure the Tx unit of the MAC after a reset.
  503. */
  504. static void igc_configure_tx(struct igc_adapter *adapter)
  505. {
  506. int i;
  507. for (i = 0; i < adapter->num_tx_queues; i++)
  508. igc_configure_tx_ring(adapter, adapter->tx_ring[i]);
  509. }
  510. /**
  511. * igc_setup_mrqc - configure the multiple receive queue control registers
  512. * @adapter: Board private structure
  513. */
  514. static void igc_setup_mrqc(struct igc_adapter *adapter)
  515. {
  516. }
  517. /**
  518. * igc_setup_rctl - configure the receive control registers
  519. * @adapter: Board private structure
  520. */
  521. static void igc_setup_rctl(struct igc_adapter *adapter)
  522. {
  523. struct igc_hw *hw = &adapter->hw;
  524. u32 rctl;
  525. rctl = rd32(IGC_RCTL);
  526. rctl &= ~(3 << IGC_RCTL_MO_SHIFT);
  527. rctl &= ~(IGC_RCTL_LBM_TCVR | IGC_RCTL_LBM_MAC);
  528. rctl |= IGC_RCTL_EN | IGC_RCTL_BAM | IGC_RCTL_RDMTS_HALF |
  529. (hw->mac.mc_filter_type << IGC_RCTL_MO_SHIFT);
  530. /* enable stripping of CRC. Newer features require
  531. * that the HW strips the CRC.
  532. */
  533. rctl |= IGC_RCTL_SECRC;
  534. /* disable store bad packets and clear size bits. */
  535. rctl &= ~(IGC_RCTL_SBP | IGC_RCTL_SZ_256);
  536. /* enable LPE to allow for reception of jumbo frames */
  537. rctl |= IGC_RCTL_LPE;
  538. /* disable queue 0 to prevent tail write w/o re-config */
  539. wr32(IGC_RXDCTL(0), 0);
  540. /* This is useful for sniffing bad packets. */
  541. if (adapter->netdev->features & NETIF_F_RXALL) {
  542. /* UPE and MPE will be handled by normal PROMISC logic
  543. * in set_rx_mode
  544. */
  545. rctl |= (IGC_RCTL_SBP | /* Receive bad packets */
  546. IGC_RCTL_BAM | /* RX All Bcast Pkts */
  547. IGC_RCTL_PMCF); /* RX All MAC Ctrl Pkts */
  548. rctl &= ~(IGC_RCTL_DPF | /* Allow filtered pause */
  549. IGC_RCTL_CFIEN); /* Disable VLAN CFIEN Filter */
  550. }
  551. wr32(IGC_RCTL, rctl);
  552. }
  553. /**
  554. * igc_setup_tctl - configure the transmit control registers
  555. * @adapter: Board private structure
  556. */
  557. static void igc_setup_tctl(struct igc_adapter *adapter)
  558. {
  559. struct igc_hw *hw = &adapter->hw;
  560. u32 tctl;
  561. /* disable queue 0 which icould be enabled by default */
  562. wr32(IGC_TXDCTL(0), 0);
  563. /* Program the Transmit Control Register */
  564. tctl = rd32(IGC_TCTL);
  565. tctl &= ~IGC_TCTL_CT;
  566. tctl |= IGC_TCTL_PSP | IGC_TCTL_RTLC |
  567. (IGC_COLLISION_THRESHOLD << IGC_CT_SHIFT);
  568. /* Enable transmits */
  569. tctl |= IGC_TCTL_EN;
  570. wr32(IGC_TCTL, tctl);
  571. }
  572. /**
  573. * igc_set_mac - Change the Ethernet Address of the NIC
  574. * @netdev: network interface device structure
  575. * @p: pointer to an address structure
  576. *
  577. * Returns 0 on success, negative on failure
  578. */
  579. static int igc_set_mac(struct net_device *netdev, void *p)
  580. {
  581. struct igc_adapter *adapter = netdev_priv(netdev);
  582. struct igc_hw *hw = &adapter->hw;
  583. struct sockaddr *addr = p;
  584. if (!is_valid_ether_addr(addr->sa_data))
  585. return -EADDRNOTAVAIL;
  586. memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
  587. memcpy(hw->mac.addr, addr->sa_data, netdev->addr_len);
  588. /* set the correct pool for the new PF MAC address in entry 0 */
  589. igc_set_default_mac_filter(adapter);
  590. return 0;
  591. }
  592. static void igc_tx_csum(struct igc_ring *tx_ring, struct igc_tx_buffer *first)
  593. {
  594. }
  595. static int __igc_maybe_stop_tx(struct igc_ring *tx_ring, const u16 size)
  596. {
  597. struct net_device *netdev = tx_ring->netdev;
  598. netif_stop_subqueue(netdev, tx_ring->queue_index);
  599. /* memory barriier comment */
  600. smp_mb();
  601. /* We need to check again in a case another CPU has just
  602. * made room available.
  603. */
  604. if (igc_desc_unused(tx_ring) < size)
  605. return -EBUSY;
  606. /* A reprieve! */
  607. netif_wake_subqueue(netdev, tx_ring->queue_index);
  608. u64_stats_update_begin(&tx_ring->tx_syncp2);
  609. tx_ring->tx_stats.restart_queue2++;
  610. u64_stats_update_end(&tx_ring->tx_syncp2);
  611. return 0;
  612. }
  613. static inline int igc_maybe_stop_tx(struct igc_ring *tx_ring, const u16 size)
  614. {
  615. if (igc_desc_unused(tx_ring) >= size)
  616. return 0;
  617. return __igc_maybe_stop_tx(tx_ring, size);
  618. }
  619. static u32 igc_tx_cmd_type(struct sk_buff *skb, u32 tx_flags)
  620. {
  621. /* set type for advanced descriptor with frame checksum insertion */
  622. u32 cmd_type = IGC_ADVTXD_DTYP_DATA |
  623. IGC_ADVTXD_DCMD_DEXT |
  624. IGC_ADVTXD_DCMD_IFCS;
  625. return cmd_type;
  626. }
  627. static void igc_tx_olinfo_status(struct igc_ring *tx_ring,
  628. union igc_adv_tx_desc *tx_desc,
  629. u32 tx_flags, unsigned int paylen)
  630. {
  631. u32 olinfo_status = paylen << IGC_ADVTXD_PAYLEN_SHIFT;
  632. /* insert L4 checksum */
  633. olinfo_status |= (tx_flags & IGC_TX_FLAGS_CSUM) *
  634. ((IGC_TXD_POPTS_TXSM << 8) /
  635. IGC_TX_FLAGS_CSUM);
  636. /* insert IPv4 checksum */
  637. olinfo_status |= (tx_flags & IGC_TX_FLAGS_IPV4) *
  638. (((IGC_TXD_POPTS_IXSM << 8)) /
  639. IGC_TX_FLAGS_IPV4);
  640. tx_desc->read.olinfo_status = cpu_to_le32(olinfo_status);
  641. }
  642. static int igc_tx_map(struct igc_ring *tx_ring,
  643. struct igc_tx_buffer *first,
  644. const u8 hdr_len)
  645. {
  646. struct sk_buff *skb = first->skb;
  647. struct igc_tx_buffer *tx_buffer;
  648. union igc_adv_tx_desc *tx_desc;
  649. u32 tx_flags = first->tx_flags;
  650. struct skb_frag_struct *frag;
  651. u16 i = tx_ring->next_to_use;
  652. unsigned int data_len, size;
  653. dma_addr_t dma;
  654. u32 cmd_type = igc_tx_cmd_type(skb, tx_flags);
  655. tx_desc = IGC_TX_DESC(tx_ring, i);
  656. igc_tx_olinfo_status(tx_ring, tx_desc, tx_flags, skb->len - hdr_len);
  657. size = skb_headlen(skb);
  658. data_len = skb->data_len;
  659. dma = dma_map_single(tx_ring->dev, skb->data, size, DMA_TO_DEVICE);
  660. tx_buffer = first;
  661. for (frag = &skb_shinfo(skb)->frags[0];; frag++) {
  662. if (dma_mapping_error(tx_ring->dev, dma))
  663. goto dma_error;
  664. /* record length, and DMA address */
  665. dma_unmap_len_set(tx_buffer, len, size);
  666. dma_unmap_addr_set(tx_buffer, dma, dma);
  667. tx_desc->read.buffer_addr = cpu_to_le64(dma);
  668. while (unlikely(size > IGC_MAX_DATA_PER_TXD)) {
  669. tx_desc->read.cmd_type_len =
  670. cpu_to_le32(cmd_type ^ IGC_MAX_DATA_PER_TXD);
  671. i++;
  672. tx_desc++;
  673. if (i == tx_ring->count) {
  674. tx_desc = IGC_TX_DESC(tx_ring, 0);
  675. i = 0;
  676. }
  677. tx_desc->read.olinfo_status = 0;
  678. dma += IGC_MAX_DATA_PER_TXD;
  679. size -= IGC_MAX_DATA_PER_TXD;
  680. tx_desc->read.buffer_addr = cpu_to_le64(dma);
  681. }
  682. if (likely(!data_len))
  683. break;
  684. tx_desc->read.cmd_type_len = cpu_to_le32(cmd_type ^ size);
  685. i++;
  686. tx_desc++;
  687. if (i == tx_ring->count) {
  688. tx_desc = IGC_TX_DESC(tx_ring, 0);
  689. i = 0;
  690. }
  691. tx_desc->read.olinfo_status = 0;
  692. size = skb_frag_size(frag);
  693. data_len -= size;
  694. dma = skb_frag_dma_map(tx_ring->dev, frag, 0,
  695. size, DMA_TO_DEVICE);
  696. tx_buffer = &tx_ring->tx_buffer_info[i];
  697. }
  698. /* write last descriptor with RS and EOP bits */
  699. cmd_type |= size | IGC_TXD_DCMD;
  700. tx_desc->read.cmd_type_len = cpu_to_le32(cmd_type);
  701. netdev_tx_sent_queue(txring_txq(tx_ring), first->bytecount);
  702. /* set the timestamp */
  703. first->time_stamp = jiffies;
  704. /* Force memory writes to complete before letting h/w know there
  705. * are new descriptors to fetch. (Only applicable for weak-ordered
  706. * memory model archs, such as IA-64).
  707. *
  708. * We also need this memory barrier to make certain all of the
  709. * status bits have been updated before next_to_watch is written.
  710. */
  711. wmb();
  712. /* set next_to_watch value indicating a packet is present */
  713. first->next_to_watch = tx_desc;
  714. i++;
  715. if (i == tx_ring->count)
  716. i = 0;
  717. tx_ring->next_to_use = i;
  718. /* Make sure there is space in the ring for the next send. */
  719. igc_maybe_stop_tx(tx_ring, DESC_NEEDED);
  720. if (netif_xmit_stopped(txring_txq(tx_ring)) || !skb->xmit_more) {
  721. writel(i, tx_ring->tail);
  722. /* we need this if more than one processor can write to our tail
  723. * at a time, it synchronizes IO on IA64/Altix systems
  724. */
  725. mmiowb();
  726. }
  727. return 0;
  728. dma_error:
  729. dev_err(tx_ring->dev, "TX DMA map failed\n");
  730. tx_buffer = &tx_ring->tx_buffer_info[i];
  731. /* clear dma mappings for failed tx_buffer_info map */
  732. while (tx_buffer != first) {
  733. if (dma_unmap_len(tx_buffer, len))
  734. dma_unmap_page(tx_ring->dev,
  735. dma_unmap_addr(tx_buffer, dma),
  736. dma_unmap_len(tx_buffer, len),
  737. DMA_TO_DEVICE);
  738. dma_unmap_len_set(tx_buffer, len, 0);
  739. if (i-- == 0)
  740. i += tx_ring->count;
  741. tx_buffer = &tx_ring->tx_buffer_info[i];
  742. }
  743. if (dma_unmap_len(tx_buffer, len))
  744. dma_unmap_single(tx_ring->dev,
  745. dma_unmap_addr(tx_buffer, dma),
  746. dma_unmap_len(tx_buffer, len),
  747. DMA_TO_DEVICE);
  748. dma_unmap_len_set(tx_buffer, len, 0);
  749. dev_kfree_skb_any(tx_buffer->skb);
  750. tx_buffer->skb = NULL;
  751. tx_ring->next_to_use = i;
  752. return -1;
  753. }
  754. static netdev_tx_t igc_xmit_frame_ring(struct sk_buff *skb,
  755. struct igc_ring *tx_ring)
  756. {
  757. u16 count = TXD_USE_COUNT(skb_headlen(skb));
  758. __be16 protocol = vlan_get_protocol(skb);
  759. struct igc_tx_buffer *first;
  760. u32 tx_flags = 0;
  761. unsigned short f;
  762. u8 hdr_len = 0;
  763. /* need: 1 descriptor per page * PAGE_SIZE/IGC_MAX_DATA_PER_TXD,
  764. * + 1 desc for skb_headlen/IGC_MAX_DATA_PER_TXD,
  765. * + 2 desc gap to keep tail from touching head,
  766. * + 1 desc for context descriptor,
  767. * otherwise try next time
  768. */
  769. for (f = 0; f < skb_shinfo(skb)->nr_frags; f++)
  770. count += TXD_USE_COUNT(skb_shinfo(skb)->frags[f].size);
  771. if (igc_maybe_stop_tx(tx_ring, count + 3)) {
  772. /* this is a hard error */
  773. return NETDEV_TX_BUSY;
  774. }
  775. /* record the location of the first descriptor for this packet */
  776. first = &tx_ring->tx_buffer_info[tx_ring->next_to_use];
  777. first->skb = skb;
  778. first->bytecount = skb->len;
  779. first->gso_segs = 1;
  780. skb_tx_timestamp(skb);
  781. /* record initial flags and protocol */
  782. first->tx_flags = tx_flags;
  783. first->protocol = protocol;
  784. igc_tx_csum(tx_ring, first);
  785. igc_tx_map(tx_ring, first, hdr_len);
  786. return NETDEV_TX_OK;
  787. }
  788. static inline struct igc_ring *igc_tx_queue_mapping(struct igc_adapter *adapter,
  789. struct sk_buff *skb)
  790. {
  791. unsigned int r_idx = skb->queue_mapping;
  792. if (r_idx >= adapter->num_tx_queues)
  793. r_idx = r_idx % adapter->num_tx_queues;
  794. return adapter->tx_ring[r_idx];
  795. }
  796. static netdev_tx_t igc_xmit_frame(struct sk_buff *skb,
  797. struct net_device *netdev)
  798. {
  799. struct igc_adapter *adapter = netdev_priv(netdev);
  800. /* The minimum packet size with TCTL.PSP set is 17 so pad the skb
  801. * in order to meet this minimum size requirement.
  802. */
  803. if (skb->len < 17) {
  804. if (skb_padto(skb, 17))
  805. return NETDEV_TX_OK;
  806. skb->len = 17;
  807. }
  808. return igc_xmit_frame_ring(skb, igc_tx_queue_mapping(adapter, skb));
  809. }
  810. static inline void igc_rx_hash(struct igc_ring *ring,
  811. union igc_adv_rx_desc *rx_desc,
  812. struct sk_buff *skb)
  813. {
  814. if (ring->netdev->features & NETIF_F_RXHASH)
  815. skb_set_hash(skb,
  816. le32_to_cpu(rx_desc->wb.lower.hi_dword.rss),
  817. PKT_HASH_TYPE_L3);
  818. }
  819. /**
  820. * igc_process_skb_fields - Populate skb header fields from Rx descriptor
  821. * @rx_ring: rx descriptor ring packet is being transacted on
  822. * @rx_desc: pointer to the EOP Rx descriptor
  823. * @skb: pointer to current skb being populated
  824. *
  825. * This function checks the ring, descriptor, and packet information in
  826. * order to populate the hash, checksum, VLAN, timestamp, protocol, and
  827. * other fields within the skb.
  828. */
  829. static void igc_process_skb_fields(struct igc_ring *rx_ring,
  830. union igc_adv_rx_desc *rx_desc,
  831. struct sk_buff *skb)
  832. {
  833. igc_rx_hash(rx_ring, rx_desc, skb);
  834. skb_record_rx_queue(skb, rx_ring->queue_index);
  835. skb->protocol = eth_type_trans(skb, rx_ring->netdev);
  836. }
  837. static struct igc_rx_buffer *igc_get_rx_buffer(struct igc_ring *rx_ring,
  838. const unsigned int size)
  839. {
  840. struct igc_rx_buffer *rx_buffer;
  841. rx_buffer = &rx_ring->rx_buffer_info[rx_ring->next_to_clean];
  842. prefetchw(rx_buffer->page);
  843. /* we are reusing so sync this buffer for CPU use */
  844. dma_sync_single_range_for_cpu(rx_ring->dev,
  845. rx_buffer->dma,
  846. rx_buffer->page_offset,
  847. size,
  848. DMA_FROM_DEVICE);
  849. rx_buffer->pagecnt_bias--;
  850. return rx_buffer;
  851. }
  852. /**
  853. * igc_add_rx_frag - Add contents of Rx buffer to sk_buff
  854. * @rx_ring: rx descriptor ring to transact packets on
  855. * @rx_buffer: buffer containing page to add
  856. * @skb: sk_buff to place the data into
  857. * @size: size of buffer to be added
  858. *
  859. * This function will add the data contained in rx_buffer->page to the skb.
  860. */
  861. static void igc_add_rx_frag(struct igc_ring *rx_ring,
  862. struct igc_rx_buffer *rx_buffer,
  863. struct sk_buff *skb,
  864. unsigned int size)
  865. {
  866. #if (PAGE_SIZE < 8192)
  867. unsigned int truesize = igc_rx_pg_size(rx_ring) / 2;
  868. skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, rx_buffer->page,
  869. rx_buffer->page_offset, size, truesize);
  870. rx_buffer->page_offset ^= truesize;
  871. #else
  872. unsigned int truesize = ring_uses_build_skb(rx_ring) ?
  873. SKB_DATA_ALIGN(IGC_SKB_PAD + size) :
  874. SKB_DATA_ALIGN(size);
  875. skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, rx_buffer->page,
  876. rx_buffer->page_offset, size, truesize);
  877. rx_buffer->page_offset += truesize;
  878. #endif
  879. }
  880. static struct sk_buff *igc_build_skb(struct igc_ring *rx_ring,
  881. struct igc_rx_buffer *rx_buffer,
  882. union igc_adv_rx_desc *rx_desc,
  883. unsigned int size)
  884. {
  885. void *va = page_address(rx_buffer->page) + rx_buffer->page_offset;
  886. #if (PAGE_SIZE < 8192)
  887. unsigned int truesize = igc_rx_pg_size(rx_ring) / 2;
  888. #else
  889. unsigned int truesize = SKB_DATA_ALIGN(sizeof(struct skb_shared_info)) +
  890. SKB_DATA_ALIGN(IGC_SKB_PAD + size);
  891. #endif
  892. struct sk_buff *skb;
  893. /* prefetch first cache line of first page */
  894. prefetch(va);
  895. #if L1_CACHE_BYTES < 128
  896. prefetch(va + L1_CACHE_BYTES);
  897. #endif
  898. /* build an skb around the page buffer */
  899. skb = build_skb(va - IGC_SKB_PAD, truesize);
  900. if (unlikely(!skb))
  901. return NULL;
  902. /* update pointers within the skb to store the data */
  903. skb_reserve(skb, IGC_SKB_PAD);
  904. __skb_put(skb, size);
  905. /* update buffer offset */
  906. #if (PAGE_SIZE < 8192)
  907. rx_buffer->page_offset ^= truesize;
  908. #else
  909. rx_buffer->page_offset += truesize;
  910. #endif
  911. return skb;
  912. }
  913. static struct sk_buff *igc_construct_skb(struct igc_ring *rx_ring,
  914. struct igc_rx_buffer *rx_buffer,
  915. union igc_adv_rx_desc *rx_desc,
  916. unsigned int size)
  917. {
  918. void *va = page_address(rx_buffer->page) + rx_buffer->page_offset;
  919. #if (PAGE_SIZE < 8192)
  920. unsigned int truesize = igc_rx_pg_size(rx_ring) / 2;
  921. #else
  922. unsigned int truesize = SKB_DATA_ALIGN(size);
  923. #endif
  924. unsigned int headlen;
  925. struct sk_buff *skb;
  926. /* prefetch first cache line of first page */
  927. prefetch(va);
  928. #if L1_CACHE_BYTES < 128
  929. prefetch(va + L1_CACHE_BYTES);
  930. #endif
  931. /* allocate a skb to store the frags */
  932. skb = napi_alloc_skb(&rx_ring->q_vector->napi, IGC_RX_HDR_LEN);
  933. if (unlikely(!skb))
  934. return NULL;
  935. /* Determine available headroom for copy */
  936. headlen = size;
  937. if (headlen > IGC_RX_HDR_LEN)
  938. headlen = eth_get_headlen(va, IGC_RX_HDR_LEN);
  939. /* align pull length to size of long to optimize memcpy performance */
  940. memcpy(__skb_put(skb, headlen), va, ALIGN(headlen, sizeof(long)));
  941. /* update all of the pointers */
  942. size -= headlen;
  943. if (size) {
  944. skb_add_rx_frag(skb, 0, rx_buffer->page,
  945. (va + headlen) - page_address(rx_buffer->page),
  946. size, truesize);
  947. #if (PAGE_SIZE < 8192)
  948. rx_buffer->page_offset ^= truesize;
  949. #else
  950. rx_buffer->page_offset += truesize;
  951. #endif
  952. } else {
  953. rx_buffer->pagecnt_bias++;
  954. }
  955. return skb;
  956. }
  957. /**
  958. * igc_reuse_rx_page - page flip buffer and store it back on the ring
  959. * @rx_ring: rx descriptor ring to store buffers on
  960. * @old_buff: donor buffer to have page reused
  961. *
  962. * Synchronizes page for reuse by the adapter
  963. */
  964. static void igc_reuse_rx_page(struct igc_ring *rx_ring,
  965. struct igc_rx_buffer *old_buff)
  966. {
  967. u16 nta = rx_ring->next_to_alloc;
  968. struct igc_rx_buffer *new_buff;
  969. new_buff = &rx_ring->rx_buffer_info[nta];
  970. /* update, and store next to alloc */
  971. nta++;
  972. rx_ring->next_to_alloc = (nta < rx_ring->count) ? nta : 0;
  973. /* Transfer page from old buffer to new buffer.
  974. * Move each member individually to avoid possible store
  975. * forwarding stalls.
  976. */
  977. new_buff->dma = old_buff->dma;
  978. new_buff->page = old_buff->page;
  979. new_buff->page_offset = old_buff->page_offset;
  980. new_buff->pagecnt_bias = old_buff->pagecnt_bias;
  981. }
  982. static inline bool igc_page_is_reserved(struct page *page)
  983. {
  984. return (page_to_nid(page) != numa_mem_id()) || page_is_pfmemalloc(page);
  985. }
  986. static bool igc_can_reuse_rx_page(struct igc_rx_buffer *rx_buffer)
  987. {
  988. unsigned int pagecnt_bias = rx_buffer->pagecnt_bias;
  989. struct page *page = rx_buffer->page;
  990. /* avoid re-using remote pages */
  991. if (unlikely(igc_page_is_reserved(page)))
  992. return false;
  993. #if (PAGE_SIZE < 8192)
  994. /* if we are only owner of page we can reuse it */
  995. if (unlikely((page_ref_count(page) - pagecnt_bias) > 1))
  996. return false;
  997. #else
  998. #define IGC_LAST_OFFSET \
  999. (SKB_WITH_OVERHEAD(PAGE_SIZE) - IGC_RXBUFFER_2048)
  1000. if (rx_buffer->page_offset > IGC_LAST_OFFSET)
  1001. return false;
  1002. #endif
  1003. /* If we have drained the page fragment pool we need to update
  1004. * the pagecnt_bias and page count so that we fully restock the
  1005. * number of references the driver holds.
  1006. */
  1007. if (unlikely(!pagecnt_bias)) {
  1008. page_ref_add(page, USHRT_MAX);
  1009. rx_buffer->pagecnt_bias = USHRT_MAX;
  1010. }
  1011. return true;
  1012. }
  1013. /**
  1014. * igc_is_non_eop - process handling of non-EOP buffers
  1015. * @rx_ring: Rx ring being processed
  1016. * @rx_desc: Rx descriptor for current buffer
  1017. * @skb: current socket buffer containing buffer in progress
  1018. *
  1019. * This function updates next to clean. If the buffer is an EOP buffer
  1020. * this function exits returning false, otherwise it will place the
  1021. * sk_buff in the next buffer to be chained and return true indicating
  1022. * that this is in fact a non-EOP buffer.
  1023. */
  1024. static bool igc_is_non_eop(struct igc_ring *rx_ring,
  1025. union igc_adv_rx_desc *rx_desc)
  1026. {
  1027. u32 ntc = rx_ring->next_to_clean + 1;
  1028. /* fetch, update, and store next to clean */
  1029. ntc = (ntc < rx_ring->count) ? ntc : 0;
  1030. rx_ring->next_to_clean = ntc;
  1031. prefetch(IGC_RX_DESC(rx_ring, ntc));
  1032. if (likely(igc_test_staterr(rx_desc, IGC_RXD_STAT_EOP)))
  1033. return false;
  1034. return true;
  1035. }
  1036. /**
  1037. * igc_cleanup_headers - Correct corrupted or empty headers
  1038. * @rx_ring: rx descriptor ring packet is being transacted on
  1039. * @rx_desc: pointer to the EOP Rx descriptor
  1040. * @skb: pointer to current skb being fixed
  1041. *
  1042. * Address the case where we are pulling data in on pages only
  1043. * and as such no data is present in the skb header.
  1044. *
  1045. * In addition if skb is not at least 60 bytes we need to pad it so that
  1046. * it is large enough to qualify as a valid Ethernet frame.
  1047. *
  1048. * Returns true if an error was encountered and skb was freed.
  1049. */
  1050. static bool igc_cleanup_headers(struct igc_ring *rx_ring,
  1051. union igc_adv_rx_desc *rx_desc,
  1052. struct sk_buff *skb)
  1053. {
  1054. if (unlikely((igc_test_staterr(rx_desc,
  1055. IGC_RXDEXT_ERR_FRAME_ERR_MASK)))) {
  1056. struct net_device *netdev = rx_ring->netdev;
  1057. if (!(netdev->features & NETIF_F_RXALL)) {
  1058. dev_kfree_skb_any(skb);
  1059. return true;
  1060. }
  1061. }
  1062. /* if eth_skb_pad returns an error the skb was freed */
  1063. if (eth_skb_pad(skb))
  1064. return true;
  1065. return false;
  1066. }
  1067. static void igc_put_rx_buffer(struct igc_ring *rx_ring,
  1068. struct igc_rx_buffer *rx_buffer)
  1069. {
  1070. if (igc_can_reuse_rx_page(rx_buffer)) {
  1071. /* hand second half of page back to the ring */
  1072. igc_reuse_rx_page(rx_ring, rx_buffer);
  1073. } else {
  1074. /* We are not reusing the buffer so unmap it and free
  1075. * any references we are holding to it
  1076. */
  1077. dma_unmap_page_attrs(rx_ring->dev, rx_buffer->dma,
  1078. igc_rx_pg_size(rx_ring), DMA_FROM_DEVICE,
  1079. IGC_RX_DMA_ATTR);
  1080. __page_frag_cache_drain(rx_buffer->page,
  1081. rx_buffer->pagecnt_bias);
  1082. }
  1083. /* clear contents of rx_buffer */
  1084. rx_buffer->page = NULL;
  1085. }
  1086. /**
  1087. * igc_alloc_rx_buffers - Replace used receive buffers; packet split
  1088. * @adapter: address of board private structure
  1089. */
  1090. static void igc_alloc_rx_buffers(struct igc_ring *rx_ring, u16 cleaned_count)
  1091. {
  1092. union igc_adv_rx_desc *rx_desc;
  1093. u16 i = rx_ring->next_to_use;
  1094. struct igc_rx_buffer *bi;
  1095. u16 bufsz;
  1096. /* nothing to do */
  1097. if (!cleaned_count)
  1098. return;
  1099. rx_desc = IGC_RX_DESC(rx_ring, i);
  1100. bi = &rx_ring->rx_buffer_info[i];
  1101. i -= rx_ring->count;
  1102. bufsz = igc_rx_bufsz(rx_ring);
  1103. do {
  1104. if (!igc_alloc_mapped_page(rx_ring, bi))
  1105. break;
  1106. /* sync the buffer for use by the device */
  1107. dma_sync_single_range_for_device(rx_ring->dev, bi->dma,
  1108. bi->page_offset, bufsz,
  1109. DMA_FROM_DEVICE);
  1110. /* Refresh the desc even if buffer_addrs didn't change
  1111. * because each write-back erases this info.
  1112. */
  1113. rx_desc->read.pkt_addr = cpu_to_le64(bi->dma + bi->page_offset);
  1114. rx_desc++;
  1115. bi++;
  1116. i++;
  1117. if (unlikely(!i)) {
  1118. rx_desc = IGC_RX_DESC(rx_ring, 0);
  1119. bi = rx_ring->rx_buffer_info;
  1120. i -= rx_ring->count;
  1121. }
  1122. /* clear the length for the next_to_use descriptor */
  1123. rx_desc->wb.upper.length = 0;
  1124. cleaned_count--;
  1125. } while (cleaned_count);
  1126. i += rx_ring->count;
  1127. if (rx_ring->next_to_use != i) {
  1128. /* record the next descriptor to use */
  1129. rx_ring->next_to_use = i;
  1130. /* update next to alloc since we have filled the ring */
  1131. rx_ring->next_to_alloc = i;
  1132. /* Force memory writes to complete before letting h/w
  1133. * know there are new descriptors to fetch. (Only
  1134. * applicable for weak-ordered memory model archs,
  1135. * such as IA-64).
  1136. */
  1137. wmb();
  1138. writel(i, rx_ring->tail);
  1139. }
  1140. }
  1141. static int igc_clean_rx_irq(struct igc_q_vector *q_vector, const int budget)
  1142. {
  1143. unsigned int total_bytes = 0, total_packets = 0;
  1144. struct igc_ring *rx_ring = q_vector->rx.ring;
  1145. struct sk_buff *skb = rx_ring->skb;
  1146. u16 cleaned_count = igc_desc_unused(rx_ring);
  1147. while (likely(total_packets < budget)) {
  1148. union igc_adv_rx_desc *rx_desc;
  1149. struct igc_rx_buffer *rx_buffer;
  1150. unsigned int size;
  1151. /* return some buffers to hardware, one at a time is too slow */
  1152. if (cleaned_count >= IGC_RX_BUFFER_WRITE) {
  1153. igc_alloc_rx_buffers(rx_ring, cleaned_count);
  1154. cleaned_count = 0;
  1155. }
  1156. rx_desc = IGC_RX_DESC(rx_ring, rx_ring->next_to_clean);
  1157. size = le16_to_cpu(rx_desc->wb.upper.length);
  1158. if (!size)
  1159. break;
  1160. /* This memory barrier is needed to keep us from reading
  1161. * any other fields out of the rx_desc until we know the
  1162. * descriptor has been written back
  1163. */
  1164. dma_rmb();
  1165. rx_buffer = igc_get_rx_buffer(rx_ring, size);
  1166. /* retrieve a buffer from the ring */
  1167. if (skb)
  1168. igc_add_rx_frag(rx_ring, rx_buffer, skb, size);
  1169. else if (ring_uses_build_skb(rx_ring))
  1170. skb = igc_build_skb(rx_ring, rx_buffer, rx_desc, size);
  1171. else
  1172. skb = igc_construct_skb(rx_ring, rx_buffer,
  1173. rx_desc, size);
  1174. /* exit if we failed to retrieve a buffer */
  1175. if (!skb) {
  1176. rx_ring->rx_stats.alloc_failed++;
  1177. rx_buffer->pagecnt_bias++;
  1178. break;
  1179. }
  1180. igc_put_rx_buffer(rx_ring, rx_buffer);
  1181. cleaned_count++;
  1182. /* fetch next buffer in frame if non-eop */
  1183. if (igc_is_non_eop(rx_ring, rx_desc))
  1184. continue;
  1185. /* verify the packet layout is correct */
  1186. if (igc_cleanup_headers(rx_ring, rx_desc, skb)) {
  1187. skb = NULL;
  1188. continue;
  1189. }
  1190. /* probably a little skewed due to removing CRC */
  1191. total_bytes += skb->len;
  1192. /* populate checksum, timestamp, VLAN, and protocol */
  1193. igc_process_skb_fields(rx_ring, rx_desc, skb);
  1194. napi_gro_receive(&q_vector->napi, skb);
  1195. /* reset skb pointer */
  1196. skb = NULL;
  1197. /* update budget accounting */
  1198. total_packets++;
  1199. }
  1200. /* place incomplete frames back on ring for completion */
  1201. rx_ring->skb = skb;
  1202. u64_stats_update_begin(&rx_ring->rx_syncp);
  1203. rx_ring->rx_stats.packets += total_packets;
  1204. rx_ring->rx_stats.bytes += total_bytes;
  1205. u64_stats_update_end(&rx_ring->rx_syncp);
  1206. q_vector->rx.total_packets += total_packets;
  1207. q_vector->rx.total_bytes += total_bytes;
  1208. if (cleaned_count)
  1209. igc_alloc_rx_buffers(rx_ring, cleaned_count);
  1210. return total_packets;
  1211. }
  1212. static inline unsigned int igc_rx_offset(struct igc_ring *rx_ring)
  1213. {
  1214. return ring_uses_build_skb(rx_ring) ? IGC_SKB_PAD : 0;
  1215. }
  1216. static bool igc_alloc_mapped_page(struct igc_ring *rx_ring,
  1217. struct igc_rx_buffer *bi)
  1218. {
  1219. struct page *page = bi->page;
  1220. dma_addr_t dma;
  1221. /* since we are recycling buffers we should seldom need to alloc */
  1222. if (likely(page))
  1223. return true;
  1224. /* alloc new page for storage */
  1225. page = dev_alloc_pages(igc_rx_pg_order(rx_ring));
  1226. if (unlikely(!page)) {
  1227. rx_ring->rx_stats.alloc_failed++;
  1228. return false;
  1229. }
  1230. /* map page for use */
  1231. dma = dma_map_page_attrs(rx_ring->dev, page, 0,
  1232. igc_rx_pg_size(rx_ring),
  1233. DMA_FROM_DEVICE,
  1234. IGC_RX_DMA_ATTR);
  1235. /* if mapping failed free memory back to system since
  1236. * there isn't much point in holding memory we can't use
  1237. */
  1238. if (dma_mapping_error(rx_ring->dev, dma)) {
  1239. __free_page(page);
  1240. rx_ring->rx_stats.alloc_failed++;
  1241. return false;
  1242. }
  1243. bi->dma = dma;
  1244. bi->page = page;
  1245. bi->page_offset = igc_rx_offset(rx_ring);
  1246. bi->pagecnt_bias = 1;
  1247. return true;
  1248. }
  1249. /**
  1250. * igc_clean_tx_irq - Reclaim resources after transmit completes
  1251. * @q_vector: pointer to q_vector containing needed info
  1252. * @napi_budget: Used to determine if we are in netpoll
  1253. *
  1254. * returns true if ring is completely cleaned
  1255. */
  1256. static bool igc_clean_tx_irq(struct igc_q_vector *q_vector, int napi_budget)
  1257. {
  1258. struct igc_adapter *adapter = q_vector->adapter;
  1259. unsigned int total_bytes = 0, total_packets = 0;
  1260. unsigned int budget = q_vector->tx.work_limit;
  1261. struct igc_ring *tx_ring = q_vector->tx.ring;
  1262. unsigned int i = tx_ring->next_to_clean;
  1263. struct igc_tx_buffer *tx_buffer;
  1264. union igc_adv_tx_desc *tx_desc;
  1265. if (test_bit(__IGC_DOWN, &adapter->state))
  1266. return true;
  1267. tx_buffer = &tx_ring->tx_buffer_info[i];
  1268. tx_desc = IGC_TX_DESC(tx_ring, i);
  1269. i -= tx_ring->count;
  1270. do {
  1271. union igc_adv_tx_desc *eop_desc = tx_buffer->next_to_watch;
  1272. /* if next_to_watch is not set then there is no work pending */
  1273. if (!eop_desc)
  1274. break;
  1275. /* prevent any other reads prior to eop_desc */
  1276. smp_rmb();
  1277. /* if DD is not set pending work has not been completed */
  1278. if (!(eop_desc->wb.status & cpu_to_le32(IGC_TXD_STAT_DD)))
  1279. break;
  1280. /* clear next_to_watch to prevent false hangs */
  1281. tx_buffer->next_to_watch = NULL;
  1282. /* update the statistics for this packet */
  1283. total_bytes += tx_buffer->bytecount;
  1284. total_packets += tx_buffer->gso_segs;
  1285. /* free the skb */
  1286. napi_consume_skb(tx_buffer->skb, napi_budget);
  1287. /* unmap skb header data */
  1288. dma_unmap_single(tx_ring->dev,
  1289. dma_unmap_addr(tx_buffer, dma),
  1290. dma_unmap_len(tx_buffer, len),
  1291. DMA_TO_DEVICE);
  1292. /* clear tx_buffer data */
  1293. dma_unmap_len_set(tx_buffer, len, 0);
  1294. /* clear last DMA location and unmap remaining buffers */
  1295. while (tx_desc != eop_desc) {
  1296. tx_buffer++;
  1297. tx_desc++;
  1298. i++;
  1299. if (unlikely(!i)) {
  1300. i -= tx_ring->count;
  1301. tx_buffer = tx_ring->tx_buffer_info;
  1302. tx_desc = IGC_TX_DESC(tx_ring, 0);
  1303. }
  1304. /* unmap any remaining paged data */
  1305. if (dma_unmap_len(tx_buffer, len)) {
  1306. dma_unmap_page(tx_ring->dev,
  1307. dma_unmap_addr(tx_buffer, dma),
  1308. dma_unmap_len(tx_buffer, len),
  1309. DMA_TO_DEVICE);
  1310. dma_unmap_len_set(tx_buffer, len, 0);
  1311. }
  1312. }
  1313. /* move us one more past the eop_desc for start of next pkt */
  1314. tx_buffer++;
  1315. tx_desc++;
  1316. i++;
  1317. if (unlikely(!i)) {
  1318. i -= tx_ring->count;
  1319. tx_buffer = tx_ring->tx_buffer_info;
  1320. tx_desc = IGC_TX_DESC(tx_ring, 0);
  1321. }
  1322. /* issue prefetch for next Tx descriptor */
  1323. prefetch(tx_desc);
  1324. /* update budget accounting */
  1325. budget--;
  1326. } while (likely(budget));
  1327. netdev_tx_completed_queue(txring_txq(tx_ring),
  1328. total_packets, total_bytes);
  1329. i += tx_ring->count;
  1330. tx_ring->next_to_clean = i;
  1331. u64_stats_update_begin(&tx_ring->tx_syncp);
  1332. tx_ring->tx_stats.bytes += total_bytes;
  1333. tx_ring->tx_stats.packets += total_packets;
  1334. u64_stats_update_end(&tx_ring->tx_syncp);
  1335. q_vector->tx.total_bytes += total_bytes;
  1336. q_vector->tx.total_packets += total_packets;
  1337. if (test_bit(IGC_RING_FLAG_TX_DETECT_HANG, &tx_ring->flags)) {
  1338. struct igc_hw *hw = &adapter->hw;
  1339. /* Detect a transmit hang in hardware, this serializes the
  1340. * check with the clearing of time_stamp and movement of i
  1341. */
  1342. clear_bit(IGC_RING_FLAG_TX_DETECT_HANG, &tx_ring->flags);
  1343. if (tx_buffer->next_to_watch &&
  1344. time_after(jiffies, tx_buffer->time_stamp +
  1345. (adapter->tx_timeout_factor * HZ)) &&
  1346. !(rd32(IGC_STATUS) & IGC_STATUS_TXOFF)) {
  1347. /* detected Tx unit hang */
  1348. dev_err(tx_ring->dev,
  1349. "Detected Tx Unit Hang\n"
  1350. " Tx Queue <%d>\n"
  1351. " TDH <%x>\n"
  1352. " TDT <%x>\n"
  1353. " next_to_use <%x>\n"
  1354. " next_to_clean <%x>\n"
  1355. "buffer_info[next_to_clean]\n"
  1356. " time_stamp <%lx>\n"
  1357. " next_to_watch <%p>\n"
  1358. " jiffies <%lx>\n"
  1359. " desc.status <%x>\n",
  1360. tx_ring->queue_index,
  1361. rd32(IGC_TDH(tx_ring->reg_idx)),
  1362. readl(tx_ring->tail),
  1363. tx_ring->next_to_use,
  1364. tx_ring->next_to_clean,
  1365. tx_buffer->time_stamp,
  1366. tx_buffer->next_to_watch,
  1367. jiffies,
  1368. tx_buffer->next_to_watch->wb.status);
  1369. netif_stop_subqueue(tx_ring->netdev,
  1370. tx_ring->queue_index);
  1371. /* we are about to reset, no point in enabling stuff */
  1372. return true;
  1373. }
  1374. }
  1375. #define TX_WAKE_THRESHOLD (DESC_NEEDED * 2)
  1376. if (unlikely(total_packets &&
  1377. netif_carrier_ok(tx_ring->netdev) &&
  1378. igc_desc_unused(tx_ring) >= TX_WAKE_THRESHOLD)) {
  1379. /* Make sure that anybody stopping the queue after this
  1380. * sees the new next_to_clean.
  1381. */
  1382. smp_mb();
  1383. if (__netif_subqueue_stopped(tx_ring->netdev,
  1384. tx_ring->queue_index) &&
  1385. !(test_bit(__IGC_DOWN, &adapter->state))) {
  1386. netif_wake_subqueue(tx_ring->netdev,
  1387. tx_ring->queue_index);
  1388. u64_stats_update_begin(&tx_ring->tx_syncp);
  1389. tx_ring->tx_stats.restart_queue++;
  1390. u64_stats_update_end(&tx_ring->tx_syncp);
  1391. }
  1392. }
  1393. return !!budget;
  1394. }
  1395. /**
  1396. * igc_ioctl - I/O control method
  1397. * @netdev: network interface device structure
  1398. * @ifreq: frequency
  1399. * @cmd: command
  1400. */
  1401. static int igc_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
  1402. {
  1403. switch (cmd) {
  1404. default:
  1405. return -EOPNOTSUPP;
  1406. }
  1407. }
  1408. /**
  1409. * igc_up - Open the interface and prepare it to handle traffic
  1410. * @adapter: board private structure
  1411. */
  1412. static void igc_up(struct igc_adapter *adapter)
  1413. {
  1414. struct igc_hw *hw = &adapter->hw;
  1415. int i = 0;
  1416. /* hardware has been reset, we need to reload some things */
  1417. igc_configure(adapter);
  1418. clear_bit(__IGC_DOWN, &adapter->state);
  1419. for (i = 0; i < adapter->num_q_vectors; i++)
  1420. napi_enable(&adapter->q_vector[i]->napi);
  1421. if (adapter->msix_entries)
  1422. igc_configure_msix(adapter);
  1423. else
  1424. igc_assign_vector(adapter->q_vector[0], 0);
  1425. /* Clear any pending interrupts. */
  1426. rd32(IGC_ICR);
  1427. igc_irq_enable(adapter);
  1428. netif_tx_start_all_queues(adapter->netdev);
  1429. /* start the watchdog. */
  1430. hw->mac.get_link_status = 1;
  1431. }
  1432. /**
  1433. * igc_update_stats - Update the board statistics counters
  1434. * @adapter: board private structure
  1435. */
  1436. static void igc_update_stats(struct igc_adapter *adapter)
  1437. {
  1438. }
  1439. static void igc_nfc_filter_exit(struct igc_adapter *adapter)
  1440. {
  1441. }
  1442. /**
  1443. * igc_down - Close the interface
  1444. * @adapter: board private structure
  1445. */
  1446. static void igc_down(struct igc_adapter *adapter)
  1447. {
  1448. struct net_device *netdev = adapter->netdev;
  1449. struct igc_hw *hw = &adapter->hw;
  1450. u32 tctl, rctl;
  1451. int i = 0;
  1452. set_bit(__IGC_DOWN, &adapter->state);
  1453. /* disable receives in the hardware */
  1454. rctl = rd32(IGC_RCTL);
  1455. wr32(IGC_RCTL, rctl & ~IGC_RCTL_EN);
  1456. /* flush and sleep below */
  1457. igc_nfc_filter_exit(adapter);
  1458. /* set trans_start so we don't get spurious watchdogs during reset */
  1459. netif_trans_update(netdev);
  1460. netif_carrier_off(netdev);
  1461. netif_tx_stop_all_queues(netdev);
  1462. /* disable transmits in the hardware */
  1463. tctl = rd32(IGC_TCTL);
  1464. tctl &= ~IGC_TCTL_EN;
  1465. wr32(IGC_TCTL, tctl);
  1466. /* flush both disables and wait for them to finish */
  1467. wrfl();
  1468. usleep_range(10000, 20000);
  1469. igc_irq_disable(adapter);
  1470. adapter->flags &= ~IGC_FLAG_NEED_LINK_UPDATE;
  1471. for (i = 0; i < adapter->num_q_vectors; i++) {
  1472. if (adapter->q_vector[i]) {
  1473. napi_synchronize(&adapter->q_vector[i]->napi);
  1474. napi_disable(&adapter->q_vector[i]->napi);
  1475. }
  1476. }
  1477. del_timer_sync(&adapter->watchdog_timer);
  1478. del_timer_sync(&adapter->phy_info_timer);
  1479. /* record the stats before reset*/
  1480. spin_lock(&adapter->stats64_lock);
  1481. igc_update_stats(adapter);
  1482. spin_unlock(&adapter->stats64_lock);
  1483. adapter->link_speed = 0;
  1484. adapter->link_duplex = 0;
  1485. if (!pci_channel_offline(adapter->pdev))
  1486. igc_reset(adapter);
  1487. /* clear VLAN promisc flag so VFTA will be updated if necessary */
  1488. adapter->flags &= ~IGC_FLAG_VLAN_PROMISC;
  1489. igc_clean_all_tx_rings(adapter);
  1490. igc_clean_all_rx_rings(adapter);
  1491. }
  1492. static void igc_reinit_locked(struct igc_adapter *adapter)
  1493. {
  1494. WARN_ON(in_interrupt());
  1495. while (test_and_set_bit(__IGC_RESETTING, &adapter->state))
  1496. usleep_range(1000, 2000);
  1497. igc_down(adapter);
  1498. igc_up(adapter);
  1499. clear_bit(__IGC_RESETTING, &adapter->state);
  1500. }
  1501. static void igc_reset_task(struct work_struct *work)
  1502. {
  1503. struct igc_adapter *adapter;
  1504. adapter = container_of(work, struct igc_adapter, reset_task);
  1505. netdev_err(adapter->netdev, "Reset adapter\n");
  1506. igc_reinit_locked(adapter);
  1507. }
  1508. /**
  1509. * igc_change_mtu - Change the Maximum Transfer Unit
  1510. * @netdev: network interface device structure
  1511. * @new_mtu: new value for maximum frame size
  1512. *
  1513. * Returns 0 on success, negative on failure
  1514. */
  1515. static int igc_change_mtu(struct net_device *netdev, int new_mtu)
  1516. {
  1517. int max_frame = new_mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
  1518. struct igc_adapter *adapter = netdev_priv(netdev);
  1519. struct pci_dev *pdev = adapter->pdev;
  1520. /* adjust max frame to be at least the size of a standard frame */
  1521. if (max_frame < (ETH_FRAME_LEN + ETH_FCS_LEN))
  1522. max_frame = ETH_FRAME_LEN + ETH_FCS_LEN;
  1523. while (test_and_set_bit(__IGC_RESETTING, &adapter->state))
  1524. usleep_range(1000, 2000);
  1525. /* igc_down has a dependency on max_frame_size */
  1526. adapter->max_frame_size = max_frame;
  1527. if (netif_running(netdev))
  1528. igc_down(adapter);
  1529. dev_info(&pdev->dev, "changing MTU from %d to %d\n",
  1530. netdev->mtu, new_mtu);
  1531. netdev->mtu = new_mtu;
  1532. if (netif_running(netdev))
  1533. igc_up(adapter);
  1534. else
  1535. igc_reset(adapter);
  1536. clear_bit(__IGC_RESETTING, &adapter->state);
  1537. return 0;
  1538. }
  1539. /**
  1540. * igc_get_stats - Get System Network Statistics
  1541. * @netdev: network interface device structure
  1542. *
  1543. * Returns the address of the device statistics structure.
  1544. * The statistics are updated here and also from the timer callback.
  1545. */
  1546. static struct net_device_stats *igc_get_stats(struct net_device *netdev)
  1547. {
  1548. struct igc_adapter *adapter = netdev_priv(netdev);
  1549. if (!test_bit(__IGC_RESETTING, &adapter->state))
  1550. igc_update_stats(adapter);
  1551. /* only return the current stats */
  1552. return &netdev->stats;
  1553. }
  1554. /**
  1555. * igc_configure - configure the hardware for RX and TX
  1556. * @adapter: private board structure
  1557. */
  1558. static void igc_configure(struct igc_adapter *adapter)
  1559. {
  1560. struct net_device *netdev = adapter->netdev;
  1561. int i = 0;
  1562. igc_get_hw_control(adapter);
  1563. igc_set_rx_mode(netdev);
  1564. igc_setup_tctl(adapter);
  1565. igc_setup_mrqc(adapter);
  1566. igc_setup_rctl(adapter);
  1567. igc_configure_tx(adapter);
  1568. igc_configure_rx(adapter);
  1569. igc_rx_fifo_flush_base(&adapter->hw);
  1570. /* call igc_desc_unused which always leaves
  1571. * at least 1 descriptor unused to make sure
  1572. * next_to_use != next_to_clean
  1573. */
  1574. for (i = 0; i < adapter->num_rx_queues; i++) {
  1575. struct igc_ring *ring = adapter->rx_ring[i];
  1576. igc_alloc_rx_buffers(ring, igc_desc_unused(ring));
  1577. }
  1578. }
  1579. /**
  1580. * igc_rar_set_index - Sync RAL[index] and RAH[index] registers with MAC table
  1581. * @adapter: Pointer to adapter structure
  1582. * @index: Index of the RAR entry which need to be synced with MAC table
  1583. */
  1584. static void igc_rar_set_index(struct igc_adapter *adapter, u32 index)
  1585. {
  1586. u8 *addr = adapter->mac_table[index].addr;
  1587. struct igc_hw *hw = &adapter->hw;
  1588. u32 rar_low, rar_high;
  1589. /* HW expects these to be in network order when they are plugged
  1590. * into the registers which are little endian. In order to guarantee
  1591. * that ordering we need to do an leXX_to_cpup here in order to be
  1592. * ready for the byteswap that occurs with writel
  1593. */
  1594. rar_low = le32_to_cpup((__le32 *)(addr));
  1595. rar_high = le16_to_cpup((__le16 *)(addr + 4));
  1596. /* Indicate to hardware the Address is Valid. */
  1597. if (adapter->mac_table[index].state & IGC_MAC_STATE_IN_USE) {
  1598. if (is_valid_ether_addr(addr))
  1599. rar_high |= IGC_RAH_AV;
  1600. rar_high |= IGC_RAH_POOL_1 <<
  1601. adapter->mac_table[index].queue;
  1602. }
  1603. wr32(IGC_RAL(index), rar_low);
  1604. wrfl();
  1605. wr32(IGC_RAH(index), rar_high);
  1606. wrfl();
  1607. }
  1608. /* Set default MAC address for the PF in the first RAR entry */
  1609. static void igc_set_default_mac_filter(struct igc_adapter *adapter)
  1610. {
  1611. struct igc_mac_addr *mac_table = &adapter->mac_table[0];
  1612. ether_addr_copy(mac_table->addr, adapter->hw.mac.addr);
  1613. mac_table->state = IGC_MAC_STATE_DEFAULT | IGC_MAC_STATE_IN_USE;
  1614. igc_rar_set_index(adapter, 0);
  1615. }
  1616. /**
  1617. * igc_set_rx_mode - Secondary Unicast, Multicast and Promiscuous mode set
  1618. * @netdev: network interface device structure
  1619. *
  1620. * The set_rx_mode entry point is called whenever the unicast or multicast
  1621. * address lists or the network interface flags are updated. This routine is
  1622. * responsible for configuring the hardware for proper unicast, multicast,
  1623. * promiscuous mode, and all-multi behavior.
  1624. */
  1625. static void igc_set_rx_mode(struct net_device *netdev)
  1626. {
  1627. }
  1628. /**
  1629. * igc_msix_other - msix other interrupt handler
  1630. * @irq: interrupt number
  1631. * @data: pointer to a q_vector
  1632. */
  1633. static irqreturn_t igc_msix_other(int irq, void *data)
  1634. {
  1635. struct igc_adapter *adapter = data;
  1636. struct igc_hw *hw = &adapter->hw;
  1637. u32 icr = rd32(IGC_ICR);
  1638. /* reading ICR causes bit 31 of EICR to be cleared */
  1639. if (icr & IGC_ICR_DRSTA)
  1640. schedule_work(&adapter->reset_task);
  1641. if (icr & IGC_ICR_DOUTSYNC) {
  1642. /* HW is reporting DMA is out of sync */
  1643. adapter->stats.doosync++;
  1644. }
  1645. if (icr & IGC_ICR_LSC) {
  1646. hw->mac.get_link_status = 1;
  1647. /* guard against interrupt when we're going down */
  1648. if (!test_bit(__IGC_DOWN, &adapter->state))
  1649. mod_timer(&adapter->watchdog_timer, jiffies + 1);
  1650. }
  1651. wr32(IGC_EIMS, adapter->eims_other);
  1652. return IRQ_HANDLED;
  1653. }
  1654. /**
  1655. * igc_write_ivar - configure ivar for given MSI-X vector
  1656. * @hw: pointer to the HW structure
  1657. * @msix_vector: vector number we are allocating to a given ring
  1658. * @index: row index of IVAR register to write within IVAR table
  1659. * @offset: column offset of in IVAR, should be multiple of 8
  1660. *
  1661. * The IVAR table consists of 2 columns,
  1662. * each containing an cause allocation for an Rx and Tx ring, and a
  1663. * variable number of rows depending on the number of queues supported.
  1664. */
  1665. static void igc_write_ivar(struct igc_hw *hw, int msix_vector,
  1666. int index, int offset)
  1667. {
  1668. u32 ivar = array_rd32(IGC_IVAR0, index);
  1669. /* clear any bits that are currently set */
  1670. ivar &= ~((u32)0xFF << offset);
  1671. /* write vector and valid bit */
  1672. ivar |= (msix_vector | IGC_IVAR_VALID) << offset;
  1673. array_wr32(IGC_IVAR0, index, ivar);
  1674. }
  1675. static void igc_assign_vector(struct igc_q_vector *q_vector, int msix_vector)
  1676. {
  1677. struct igc_adapter *adapter = q_vector->adapter;
  1678. struct igc_hw *hw = &adapter->hw;
  1679. int rx_queue = IGC_N0_QUEUE;
  1680. int tx_queue = IGC_N0_QUEUE;
  1681. if (q_vector->rx.ring)
  1682. rx_queue = q_vector->rx.ring->reg_idx;
  1683. if (q_vector->tx.ring)
  1684. tx_queue = q_vector->tx.ring->reg_idx;
  1685. switch (hw->mac.type) {
  1686. case igc_i225:
  1687. if (rx_queue > IGC_N0_QUEUE)
  1688. igc_write_ivar(hw, msix_vector,
  1689. rx_queue >> 1,
  1690. (rx_queue & 0x1) << 4);
  1691. if (tx_queue > IGC_N0_QUEUE)
  1692. igc_write_ivar(hw, msix_vector,
  1693. tx_queue >> 1,
  1694. ((tx_queue & 0x1) << 4) + 8);
  1695. q_vector->eims_value = BIT(msix_vector);
  1696. break;
  1697. default:
  1698. WARN_ONCE(hw->mac.type != igc_i225, "Wrong MAC type\n");
  1699. break;
  1700. }
  1701. /* add q_vector eims value to global eims_enable_mask */
  1702. adapter->eims_enable_mask |= q_vector->eims_value;
  1703. /* configure q_vector to set itr on first interrupt */
  1704. q_vector->set_itr = 1;
  1705. }
  1706. /**
  1707. * igc_configure_msix - Configure MSI-X hardware
  1708. * @adapter: Pointer to adapter structure
  1709. *
  1710. * igc_configure_msix sets up the hardware to properly
  1711. * generate MSI-X interrupts.
  1712. */
  1713. static void igc_configure_msix(struct igc_adapter *adapter)
  1714. {
  1715. struct igc_hw *hw = &adapter->hw;
  1716. int i, vector = 0;
  1717. u32 tmp;
  1718. adapter->eims_enable_mask = 0;
  1719. /* set vector for other causes, i.e. link changes */
  1720. switch (hw->mac.type) {
  1721. case igc_i225:
  1722. /* Turn on MSI-X capability first, or our settings
  1723. * won't stick. And it will take days to debug.
  1724. */
  1725. wr32(IGC_GPIE, IGC_GPIE_MSIX_MODE |
  1726. IGC_GPIE_PBA | IGC_GPIE_EIAME |
  1727. IGC_GPIE_NSICR);
  1728. /* enable msix_other interrupt */
  1729. adapter->eims_other = BIT(vector);
  1730. tmp = (vector++ | IGC_IVAR_VALID) << 8;
  1731. wr32(IGC_IVAR_MISC, tmp);
  1732. break;
  1733. default:
  1734. /* do nothing, since nothing else supports MSI-X */
  1735. break;
  1736. } /* switch (hw->mac.type) */
  1737. adapter->eims_enable_mask |= adapter->eims_other;
  1738. for (i = 0; i < adapter->num_q_vectors; i++)
  1739. igc_assign_vector(adapter->q_vector[i], vector++);
  1740. wrfl();
  1741. }
  1742. static irqreturn_t igc_msix_ring(int irq, void *data)
  1743. {
  1744. struct igc_q_vector *q_vector = data;
  1745. /* Write the ITR value calculated from the previous interrupt. */
  1746. igc_write_itr(q_vector);
  1747. napi_schedule(&q_vector->napi);
  1748. return IRQ_HANDLED;
  1749. }
  1750. /**
  1751. * igc_request_msix - Initialize MSI-X interrupts
  1752. * @adapter: Pointer to adapter structure
  1753. *
  1754. * igc_request_msix allocates MSI-X vectors and requests interrupts from the
  1755. * kernel.
  1756. */
  1757. static int igc_request_msix(struct igc_adapter *adapter)
  1758. {
  1759. int i = 0, err = 0, vector = 0, free_vector = 0;
  1760. struct net_device *netdev = adapter->netdev;
  1761. err = request_irq(adapter->msix_entries[vector].vector,
  1762. &igc_msix_other, 0, netdev->name, adapter);
  1763. if (err)
  1764. goto err_out;
  1765. for (i = 0; i < adapter->num_q_vectors; i++) {
  1766. struct igc_q_vector *q_vector = adapter->q_vector[i];
  1767. vector++;
  1768. q_vector->itr_register = adapter->io_addr + IGC_EITR(vector);
  1769. if (q_vector->rx.ring && q_vector->tx.ring)
  1770. sprintf(q_vector->name, "%s-TxRx-%u", netdev->name,
  1771. q_vector->rx.ring->queue_index);
  1772. else if (q_vector->tx.ring)
  1773. sprintf(q_vector->name, "%s-tx-%u", netdev->name,
  1774. q_vector->tx.ring->queue_index);
  1775. else if (q_vector->rx.ring)
  1776. sprintf(q_vector->name, "%s-rx-%u", netdev->name,
  1777. q_vector->rx.ring->queue_index);
  1778. else
  1779. sprintf(q_vector->name, "%s-unused", netdev->name);
  1780. err = request_irq(adapter->msix_entries[vector].vector,
  1781. igc_msix_ring, 0, q_vector->name,
  1782. q_vector);
  1783. if (err)
  1784. goto err_free;
  1785. }
  1786. igc_configure_msix(adapter);
  1787. return 0;
  1788. err_free:
  1789. /* free already assigned IRQs */
  1790. free_irq(adapter->msix_entries[free_vector++].vector, adapter);
  1791. vector--;
  1792. for (i = 0; i < vector; i++) {
  1793. free_irq(adapter->msix_entries[free_vector++].vector,
  1794. adapter->q_vector[i]);
  1795. }
  1796. err_out:
  1797. return err;
  1798. }
  1799. /**
  1800. * igc_reset_q_vector - Reset config for interrupt vector
  1801. * @adapter: board private structure to initialize
  1802. * @v_idx: Index of vector to be reset
  1803. *
  1804. * If NAPI is enabled it will delete any references to the
  1805. * NAPI struct. This is preparation for igc_free_q_vector.
  1806. */
  1807. static void igc_reset_q_vector(struct igc_adapter *adapter, int v_idx)
  1808. {
  1809. struct igc_q_vector *q_vector = adapter->q_vector[v_idx];
  1810. /* if we're coming from igc_set_interrupt_capability, the vectors are
  1811. * not yet allocated
  1812. */
  1813. if (!q_vector)
  1814. return;
  1815. if (q_vector->tx.ring)
  1816. adapter->tx_ring[q_vector->tx.ring->queue_index] = NULL;
  1817. if (q_vector->rx.ring)
  1818. adapter->rx_ring[q_vector->rx.ring->queue_index] = NULL;
  1819. netif_napi_del(&q_vector->napi);
  1820. }
  1821. static void igc_reset_interrupt_capability(struct igc_adapter *adapter)
  1822. {
  1823. int v_idx = adapter->num_q_vectors;
  1824. if (adapter->msix_entries) {
  1825. pci_disable_msix(adapter->pdev);
  1826. kfree(adapter->msix_entries);
  1827. adapter->msix_entries = NULL;
  1828. } else if (adapter->flags & IGC_FLAG_HAS_MSI) {
  1829. pci_disable_msi(adapter->pdev);
  1830. }
  1831. while (v_idx--)
  1832. igc_reset_q_vector(adapter, v_idx);
  1833. }
  1834. /**
  1835. * igc_clear_interrupt_scheme - reset the device to a state of no interrupts
  1836. * @adapter: Pointer to adapter structure
  1837. *
  1838. * This function resets the device so that it has 0 rx queues, tx queues, and
  1839. * MSI-X interrupts allocated.
  1840. */
  1841. static void igc_clear_interrupt_scheme(struct igc_adapter *adapter)
  1842. {
  1843. igc_free_q_vectors(adapter);
  1844. igc_reset_interrupt_capability(adapter);
  1845. }
  1846. /**
  1847. * igc_free_q_vectors - Free memory allocated for interrupt vectors
  1848. * @adapter: board private structure to initialize
  1849. *
  1850. * This function frees the memory allocated to the q_vectors. In addition if
  1851. * NAPI is enabled it will delete any references to the NAPI struct prior
  1852. * to freeing the q_vector.
  1853. */
  1854. static void igc_free_q_vectors(struct igc_adapter *adapter)
  1855. {
  1856. int v_idx = adapter->num_q_vectors;
  1857. adapter->num_tx_queues = 0;
  1858. adapter->num_rx_queues = 0;
  1859. adapter->num_q_vectors = 0;
  1860. while (v_idx--) {
  1861. igc_reset_q_vector(adapter, v_idx);
  1862. igc_free_q_vector(adapter, v_idx);
  1863. }
  1864. }
  1865. /**
  1866. * igc_free_q_vector - Free memory allocated for specific interrupt vector
  1867. * @adapter: board private structure to initialize
  1868. * @v_idx: Index of vector to be freed
  1869. *
  1870. * This function frees the memory allocated to the q_vector.
  1871. */
  1872. static void igc_free_q_vector(struct igc_adapter *adapter, int v_idx)
  1873. {
  1874. struct igc_q_vector *q_vector = adapter->q_vector[v_idx];
  1875. adapter->q_vector[v_idx] = NULL;
  1876. /* igc_get_stats64() might access the rings on this vector,
  1877. * we must wait a grace period before freeing it.
  1878. */
  1879. if (q_vector)
  1880. kfree_rcu(q_vector, rcu);
  1881. }
  1882. /**
  1883. * igc_watchdog - Timer Call-back
  1884. * @data: pointer to adapter cast into an unsigned long
  1885. */
  1886. static void igc_watchdog(struct timer_list *t)
  1887. {
  1888. struct igc_adapter *adapter = from_timer(adapter, t, watchdog_timer);
  1889. }
  1890. /**
  1891. * igc_update_ring_itr - update the dynamic ITR value based on packet size
  1892. * @q_vector: pointer to q_vector
  1893. *
  1894. * Stores a new ITR value based on strictly on packet size. This
  1895. * algorithm is less sophisticated than that used in igc_update_itr,
  1896. * due to the difficulty of synchronizing statistics across multiple
  1897. * receive rings. The divisors and thresholds used by this function
  1898. * were determined based on theoretical maximum wire speed and testing
  1899. * data, in order to minimize response time while increasing bulk
  1900. * throughput.
  1901. * NOTE: This function is called only when operating in a multiqueue
  1902. * receive environment.
  1903. */
  1904. static void igc_update_ring_itr(struct igc_q_vector *q_vector)
  1905. {
  1906. struct igc_adapter *adapter = q_vector->adapter;
  1907. int new_val = q_vector->itr_val;
  1908. int avg_wire_size = 0;
  1909. unsigned int packets;
  1910. /* For non-gigabit speeds, just fix the interrupt rate at 4000
  1911. * ints/sec - ITR timer value of 120 ticks.
  1912. */
  1913. switch (adapter->link_speed) {
  1914. case SPEED_10:
  1915. case SPEED_100:
  1916. new_val = IGC_4K_ITR;
  1917. goto set_itr_val;
  1918. default:
  1919. break;
  1920. }
  1921. packets = q_vector->rx.total_packets;
  1922. if (packets)
  1923. avg_wire_size = q_vector->rx.total_bytes / packets;
  1924. packets = q_vector->tx.total_packets;
  1925. if (packets)
  1926. avg_wire_size = max_t(u32, avg_wire_size,
  1927. q_vector->tx.total_bytes / packets);
  1928. /* if avg_wire_size isn't set no work was done */
  1929. if (!avg_wire_size)
  1930. goto clear_counts;
  1931. /* Add 24 bytes to size to account for CRC, preamble, and gap */
  1932. avg_wire_size += 24;
  1933. /* Don't starve jumbo frames */
  1934. avg_wire_size = min(avg_wire_size, 3000);
  1935. /* Give a little boost to mid-size frames */
  1936. if (avg_wire_size > 300 && avg_wire_size < 1200)
  1937. new_val = avg_wire_size / 3;
  1938. else
  1939. new_val = avg_wire_size / 2;
  1940. /* conservative mode (itr 3) eliminates the lowest_latency setting */
  1941. if (new_val < IGC_20K_ITR &&
  1942. ((q_vector->rx.ring && adapter->rx_itr_setting == 3) ||
  1943. (!q_vector->rx.ring && adapter->tx_itr_setting == 3)))
  1944. new_val = IGC_20K_ITR;
  1945. set_itr_val:
  1946. if (new_val != q_vector->itr_val) {
  1947. q_vector->itr_val = new_val;
  1948. q_vector->set_itr = 1;
  1949. }
  1950. clear_counts:
  1951. q_vector->rx.total_bytes = 0;
  1952. q_vector->rx.total_packets = 0;
  1953. q_vector->tx.total_bytes = 0;
  1954. q_vector->tx.total_packets = 0;
  1955. }
  1956. /**
  1957. * igc_update_itr - update the dynamic ITR value based on statistics
  1958. * @q_vector: pointer to q_vector
  1959. * @ring_container: ring info to update the itr for
  1960. *
  1961. * Stores a new ITR value based on packets and byte
  1962. * counts during the last interrupt. The advantage of per interrupt
  1963. * computation is faster updates and more accurate ITR for the current
  1964. * traffic pattern. Constants in this function were computed
  1965. * based on theoretical maximum wire speed and thresholds were set based
  1966. * on testing data as well as attempting to minimize response time
  1967. * while increasing bulk throughput.
  1968. * NOTE: These calculations are only valid when operating in a single-
  1969. * queue environment.
  1970. */
  1971. static void igc_update_itr(struct igc_q_vector *q_vector,
  1972. struct igc_ring_container *ring_container)
  1973. {
  1974. unsigned int packets = ring_container->total_packets;
  1975. unsigned int bytes = ring_container->total_bytes;
  1976. u8 itrval = ring_container->itr;
  1977. /* no packets, exit with status unchanged */
  1978. if (packets == 0)
  1979. return;
  1980. switch (itrval) {
  1981. case lowest_latency:
  1982. /* handle TSO and jumbo frames */
  1983. if (bytes / packets > 8000)
  1984. itrval = bulk_latency;
  1985. else if ((packets < 5) && (bytes > 512))
  1986. itrval = low_latency;
  1987. break;
  1988. case low_latency: /* 50 usec aka 20000 ints/s */
  1989. if (bytes > 10000) {
  1990. /* this if handles the TSO accounting */
  1991. if (bytes / packets > 8000)
  1992. itrval = bulk_latency;
  1993. else if ((packets < 10) || ((bytes / packets) > 1200))
  1994. itrval = bulk_latency;
  1995. else if ((packets > 35))
  1996. itrval = lowest_latency;
  1997. } else if (bytes / packets > 2000) {
  1998. itrval = bulk_latency;
  1999. } else if (packets <= 2 && bytes < 512) {
  2000. itrval = lowest_latency;
  2001. }
  2002. break;
  2003. case bulk_latency: /* 250 usec aka 4000 ints/s */
  2004. if (bytes > 25000) {
  2005. if (packets > 35)
  2006. itrval = low_latency;
  2007. } else if (bytes < 1500) {
  2008. itrval = low_latency;
  2009. }
  2010. break;
  2011. }
  2012. /* clear work counters since we have the values we need */
  2013. ring_container->total_bytes = 0;
  2014. ring_container->total_packets = 0;
  2015. /* write updated itr to ring container */
  2016. ring_container->itr = itrval;
  2017. }
  2018. /**
  2019. * igc_intr_msi - Interrupt Handler
  2020. * @irq: interrupt number
  2021. * @data: pointer to a network interface device structure
  2022. */
  2023. static irqreturn_t igc_intr_msi(int irq, void *data)
  2024. {
  2025. struct igc_adapter *adapter = data;
  2026. struct igc_q_vector *q_vector = adapter->q_vector[0];
  2027. struct igc_hw *hw = &adapter->hw;
  2028. /* read ICR disables interrupts using IAM */
  2029. u32 icr = rd32(IGC_ICR);
  2030. igc_write_itr(q_vector);
  2031. if (icr & IGC_ICR_DRSTA)
  2032. schedule_work(&adapter->reset_task);
  2033. if (icr & IGC_ICR_DOUTSYNC) {
  2034. /* HW is reporting DMA is out of sync */
  2035. adapter->stats.doosync++;
  2036. }
  2037. if (icr & (IGC_ICR_RXSEQ | IGC_ICR_LSC)) {
  2038. hw->mac.get_link_status = 1;
  2039. if (!test_bit(__IGC_DOWN, &adapter->state))
  2040. mod_timer(&adapter->watchdog_timer, jiffies + 1);
  2041. }
  2042. napi_schedule(&q_vector->napi);
  2043. return IRQ_HANDLED;
  2044. }
  2045. /**
  2046. * igc_intr - Legacy Interrupt Handler
  2047. * @irq: interrupt number
  2048. * @data: pointer to a network interface device structure
  2049. */
  2050. static irqreturn_t igc_intr(int irq, void *data)
  2051. {
  2052. struct igc_adapter *adapter = data;
  2053. struct igc_q_vector *q_vector = adapter->q_vector[0];
  2054. struct igc_hw *hw = &adapter->hw;
  2055. /* Interrupt Auto-Mask...upon reading ICR, interrupts are masked. No
  2056. * need for the IMC write
  2057. */
  2058. u32 icr = rd32(IGC_ICR);
  2059. /* IMS will not auto-mask if INT_ASSERTED is not set, and if it is
  2060. * not set, then the adapter didn't send an interrupt
  2061. */
  2062. if (!(icr & IGC_ICR_INT_ASSERTED))
  2063. return IRQ_NONE;
  2064. igc_write_itr(q_vector);
  2065. if (icr & IGC_ICR_DRSTA)
  2066. schedule_work(&adapter->reset_task);
  2067. if (icr & IGC_ICR_DOUTSYNC) {
  2068. /* HW is reporting DMA is out of sync */
  2069. adapter->stats.doosync++;
  2070. }
  2071. if (icr & (IGC_ICR_RXSEQ | IGC_ICR_LSC)) {
  2072. hw->mac.get_link_status = 1;
  2073. /* guard against interrupt when we're going down */
  2074. if (!test_bit(__IGC_DOWN, &adapter->state))
  2075. mod_timer(&adapter->watchdog_timer, jiffies + 1);
  2076. }
  2077. napi_schedule(&q_vector->napi);
  2078. return IRQ_HANDLED;
  2079. }
  2080. static void igc_set_itr(struct igc_q_vector *q_vector)
  2081. {
  2082. struct igc_adapter *adapter = q_vector->adapter;
  2083. u32 new_itr = q_vector->itr_val;
  2084. u8 current_itr = 0;
  2085. /* for non-gigabit speeds, just fix the interrupt rate at 4000 */
  2086. switch (adapter->link_speed) {
  2087. case SPEED_10:
  2088. case SPEED_100:
  2089. current_itr = 0;
  2090. new_itr = IGC_4K_ITR;
  2091. goto set_itr_now;
  2092. default:
  2093. break;
  2094. }
  2095. igc_update_itr(q_vector, &q_vector->tx);
  2096. igc_update_itr(q_vector, &q_vector->rx);
  2097. current_itr = max(q_vector->rx.itr, q_vector->tx.itr);
  2098. /* conservative mode (itr 3) eliminates the lowest_latency setting */
  2099. if (current_itr == lowest_latency &&
  2100. ((q_vector->rx.ring && adapter->rx_itr_setting == 3) ||
  2101. (!q_vector->rx.ring && adapter->tx_itr_setting == 3)))
  2102. current_itr = low_latency;
  2103. switch (current_itr) {
  2104. /* counts and packets in update_itr are dependent on these numbers */
  2105. case lowest_latency:
  2106. new_itr = IGC_70K_ITR; /* 70,000 ints/sec */
  2107. break;
  2108. case low_latency:
  2109. new_itr = IGC_20K_ITR; /* 20,000 ints/sec */
  2110. break;
  2111. case bulk_latency:
  2112. new_itr = IGC_4K_ITR; /* 4,000 ints/sec */
  2113. break;
  2114. default:
  2115. break;
  2116. }
  2117. set_itr_now:
  2118. if (new_itr != q_vector->itr_val) {
  2119. /* this attempts to bias the interrupt rate towards Bulk
  2120. * by adding intermediate steps when interrupt rate is
  2121. * increasing
  2122. */
  2123. new_itr = new_itr > q_vector->itr_val ?
  2124. max((new_itr * q_vector->itr_val) /
  2125. (new_itr + (q_vector->itr_val >> 2)),
  2126. new_itr) : new_itr;
  2127. /* Don't write the value here; it resets the adapter's
  2128. * internal timer, and causes us to delay far longer than
  2129. * we should between interrupts. Instead, we write the ITR
  2130. * value at the beginning of the next interrupt so the timing
  2131. * ends up being correct.
  2132. */
  2133. q_vector->itr_val = new_itr;
  2134. q_vector->set_itr = 1;
  2135. }
  2136. }
  2137. static void igc_ring_irq_enable(struct igc_q_vector *q_vector)
  2138. {
  2139. struct igc_adapter *adapter = q_vector->adapter;
  2140. struct igc_hw *hw = &adapter->hw;
  2141. if ((q_vector->rx.ring && (adapter->rx_itr_setting & 3)) ||
  2142. (!q_vector->rx.ring && (adapter->tx_itr_setting & 3))) {
  2143. if (adapter->num_q_vectors == 1)
  2144. igc_set_itr(q_vector);
  2145. else
  2146. igc_update_ring_itr(q_vector);
  2147. }
  2148. if (!test_bit(__IGC_DOWN, &adapter->state)) {
  2149. if (adapter->msix_entries)
  2150. wr32(IGC_EIMS, q_vector->eims_value);
  2151. else
  2152. igc_irq_enable(adapter);
  2153. }
  2154. }
  2155. /**
  2156. * igc_poll - NAPI Rx polling callback
  2157. * @napi: napi polling structure
  2158. * @budget: count of how many packets we should handle
  2159. */
  2160. static int igc_poll(struct napi_struct *napi, int budget)
  2161. {
  2162. struct igc_q_vector *q_vector = container_of(napi,
  2163. struct igc_q_vector,
  2164. napi);
  2165. bool clean_complete = true;
  2166. int work_done = 0;
  2167. if (q_vector->tx.ring)
  2168. clean_complete = igc_clean_tx_irq(q_vector, budget);
  2169. if (q_vector->rx.ring) {
  2170. int cleaned = igc_clean_rx_irq(q_vector, budget);
  2171. work_done += cleaned;
  2172. if (cleaned >= budget)
  2173. clean_complete = false;
  2174. }
  2175. /* If all work not completed, return budget and keep polling */
  2176. if (!clean_complete)
  2177. return budget;
  2178. /* If not enough Rx work done, exit the polling mode */
  2179. napi_complete_done(napi, work_done);
  2180. igc_ring_irq_enable(q_vector);
  2181. return 0;
  2182. }
  2183. /**
  2184. * igc_set_interrupt_capability - set MSI or MSI-X if supported
  2185. * @adapter: Pointer to adapter structure
  2186. *
  2187. * Attempt to configure interrupts using the best available
  2188. * capabilities of the hardware and kernel.
  2189. */
  2190. static void igc_set_interrupt_capability(struct igc_adapter *adapter,
  2191. bool msix)
  2192. {
  2193. int numvecs, i;
  2194. int err;
  2195. if (!msix)
  2196. goto msi_only;
  2197. adapter->flags |= IGC_FLAG_HAS_MSIX;
  2198. /* Number of supported queues. */
  2199. adapter->num_rx_queues = adapter->rss_queues;
  2200. adapter->num_tx_queues = adapter->rss_queues;
  2201. /* start with one vector for every Rx queue */
  2202. numvecs = adapter->num_rx_queues;
  2203. /* if Tx handler is separate add 1 for every Tx queue */
  2204. if (!(adapter->flags & IGC_FLAG_QUEUE_PAIRS))
  2205. numvecs += adapter->num_tx_queues;
  2206. /* store the number of vectors reserved for queues */
  2207. adapter->num_q_vectors = numvecs;
  2208. /* add 1 vector for link status interrupts */
  2209. numvecs++;
  2210. adapter->msix_entries = kcalloc(numvecs, sizeof(struct msix_entry),
  2211. GFP_KERNEL);
  2212. if (!adapter->msix_entries)
  2213. return;
  2214. /* populate entry values */
  2215. for (i = 0; i < numvecs; i++)
  2216. adapter->msix_entries[i].entry = i;
  2217. err = pci_enable_msix_range(adapter->pdev,
  2218. adapter->msix_entries,
  2219. numvecs,
  2220. numvecs);
  2221. if (err > 0)
  2222. return;
  2223. kfree(adapter->msix_entries);
  2224. adapter->msix_entries = NULL;
  2225. igc_reset_interrupt_capability(adapter);
  2226. msi_only:
  2227. adapter->flags &= ~IGC_FLAG_HAS_MSIX;
  2228. adapter->rss_queues = 1;
  2229. adapter->flags |= IGC_FLAG_QUEUE_PAIRS;
  2230. adapter->num_rx_queues = 1;
  2231. adapter->num_tx_queues = 1;
  2232. adapter->num_q_vectors = 1;
  2233. if (!pci_enable_msi(adapter->pdev))
  2234. adapter->flags |= IGC_FLAG_HAS_MSI;
  2235. }
  2236. static void igc_add_ring(struct igc_ring *ring,
  2237. struct igc_ring_container *head)
  2238. {
  2239. head->ring = ring;
  2240. head->count++;
  2241. }
  2242. /**
  2243. * igc_alloc_q_vector - Allocate memory for a single interrupt vector
  2244. * @adapter: board private structure to initialize
  2245. * @v_count: q_vectors allocated on adapter, used for ring interleaving
  2246. * @v_idx: index of vector in adapter struct
  2247. * @txr_count: total number of Tx rings to allocate
  2248. * @txr_idx: index of first Tx ring to allocate
  2249. * @rxr_count: total number of Rx rings to allocate
  2250. * @rxr_idx: index of first Rx ring to allocate
  2251. *
  2252. * We allocate one q_vector. If allocation fails we return -ENOMEM.
  2253. */
  2254. static int igc_alloc_q_vector(struct igc_adapter *adapter,
  2255. unsigned int v_count, unsigned int v_idx,
  2256. unsigned int txr_count, unsigned int txr_idx,
  2257. unsigned int rxr_count, unsigned int rxr_idx)
  2258. {
  2259. struct igc_q_vector *q_vector;
  2260. struct igc_ring *ring;
  2261. int ring_count, size;
  2262. /* igc only supports 1 Tx and/or 1 Rx queue per vector */
  2263. if (txr_count > 1 || rxr_count > 1)
  2264. return -ENOMEM;
  2265. ring_count = txr_count + rxr_count;
  2266. size = sizeof(struct igc_q_vector) +
  2267. (sizeof(struct igc_ring) * ring_count);
  2268. /* allocate q_vector and rings */
  2269. q_vector = adapter->q_vector[v_idx];
  2270. if (!q_vector)
  2271. q_vector = kzalloc(size, GFP_KERNEL);
  2272. else
  2273. memset(q_vector, 0, size);
  2274. if (!q_vector)
  2275. return -ENOMEM;
  2276. /* initialize NAPI */
  2277. netif_napi_add(adapter->netdev, &q_vector->napi,
  2278. igc_poll, 64);
  2279. /* tie q_vector and adapter together */
  2280. adapter->q_vector[v_idx] = q_vector;
  2281. q_vector->adapter = adapter;
  2282. /* initialize work limits */
  2283. q_vector->tx.work_limit = adapter->tx_work_limit;
  2284. /* initialize ITR configuration */
  2285. q_vector->itr_register = adapter->io_addr + IGC_EITR(0);
  2286. q_vector->itr_val = IGC_START_ITR;
  2287. /* initialize pointer to rings */
  2288. ring = q_vector->ring;
  2289. /* initialize ITR */
  2290. if (rxr_count) {
  2291. /* rx or rx/tx vector */
  2292. if (!adapter->rx_itr_setting || adapter->rx_itr_setting > 3)
  2293. q_vector->itr_val = adapter->rx_itr_setting;
  2294. } else {
  2295. /* tx only vector */
  2296. if (!adapter->tx_itr_setting || adapter->tx_itr_setting > 3)
  2297. q_vector->itr_val = adapter->tx_itr_setting;
  2298. }
  2299. if (txr_count) {
  2300. /* assign generic ring traits */
  2301. ring->dev = &adapter->pdev->dev;
  2302. ring->netdev = adapter->netdev;
  2303. /* configure backlink on ring */
  2304. ring->q_vector = q_vector;
  2305. /* update q_vector Tx values */
  2306. igc_add_ring(ring, &q_vector->tx);
  2307. /* apply Tx specific ring traits */
  2308. ring->count = adapter->tx_ring_count;
  2309. ring->queue_index = txr_idx;
  2310. /* assign ring to adapter */
  2311. adapter->tx_ring[txr_idx] = ring;
  2312. /* push pointer to next ring */
  2313. ring++;
  2314. }
  2315. if (rxr_count) {
  2316. /* assign generic ring traits */
  2317. ring->dev = &adapter->pdev->dev;
  2318. ring->netdev = adapter->netdev;
  2319. /* configure backlink on ring */
  2320. ring->q_vector = q_vector;
  2321. /* update q_vector Rx values */
  2322. igc_add_ring(ring, &q_vector->rx);
  2323. /* apply Rx specific ring traits */
  2324. ring->count = adapter->rx_ring_count;
  2325. ring->queue_index = rxr_idx;
  2326. /* assign ring to adapter */
  2327. adapter->rx_ring[rxr_idx] = ring;
  2328. }
  2329. return 0;
  2330. }
  2331. /**
  2332. * igc_alloc_q_vectors - Allocate memory for interrupt vectors
  2333. * @adapter: board private structure to initialize
  2334. *
  2335. * We allocate one q_vector per queue interrupt. If allocation fails we
  2336. * return -ENOMEM.
  2337. */
  2338. static int igc_alloc_q_vectors(struct igc_adapter *adapter)
  2339. {
  2340. int rxr_remaining = adapter->num_rx_queues;
  2341. int txr_remaining = adapter->num_tx_queues;
  2342. int rxr_idx = 0, txr_idx = 0, v_idx = 0;
  2343. int q_vectors = adapter->num_q_vectors;
  2344. int err;
  2345. if (q_vectors >= (rxr_remaining + txr_remaining)) {
  2346. for (; rxr_remaining; v_idx++) {
  2347. err = igc_alloc_q_vector(adapter, q_vectors, v_idx,
  2348. 0, 0, 1, rxr_idx);
  2349. if (err)
  2350. goto err_out;
  2351. /* update counts and index */
  2352. rxr_remaining--;
  2353. rxr_idx++;
  2354. }
  2355. }
  2356. for (; v_idx < q_vectors; v_idx++) {
  2357. int rqpv = DIV_ROUND_UP(rxr_remaining, q_vectors - v_idx);
  2358. int tqpv = DIV_ROUND_UP(txr_remaining, q_vectors - v_idx);
  2359. err = igc_alloc_q_vector(adapter, q_vectors, v_idx,
  2360. tqpv, txr_idx, rqpv, rxr_idx);
  2361. if (err)
  2362. goto err_out;
  2363. /* update counts and index */
  2364. rxr_remaining -= rqpv;
  2365. txr_remaining -= tqpv;
  2366. rxr_idx++;
  2367. txr_idx++;
  2368. }
  2369. return 0;
  2370. err_out:
  2371. adapter->num_tx_queues = 0;
  2372. adapter->num_rx_queues = 0;
  2373. adapter->num_q_vectors = 0;
  2374. while (v_idx--)
  2375. igc_free_q_vector(adapter, v_idx);
  2376. return -ENOMEM;
  2377. }
  2378. /**
  2379. * igc_cache_ring_register - Descriptor ring to register mapping
  2380. * @adapter: board private structure to initialize
  2381. *
  2382. * Once we know the feature-set enabled for the device, we'll cache
  2383. * the register offset the descriptor ring is assigned to.
  2384. */
  2385. static void igc_cache_ring_register(struct igc_adapter *adapter)
  2386. {
  2387. int i = 0, j = 0;
  2388. switch (adapter->hw.mac.type) {
  2389. case igc_i225:
  2390. /* Fall through */
  2391. default:
  2392. for (; i < adapter->num_rx_queues; i++)
  2393. adapter->rx_ring[i]->reg_idx = i;
  2394. for (; j < adapter->num_tx_queues; j++)
  2395. adapter->tx_ring[j]->reg_idx = j;
  2396. break;
  2397. }
  2398. }
  2399. /**
  2400. * igc_init_interrupt_scheme - initialize interrupts, allocate queues/vectors
  2401. * @adapter: Pointer to adapter structure
  2402. *
  2403. * This function initializes the interrupts and allocates all of the queues.
  2404. */
  2405. static int igc_init_interrupt_scheme(struct igc_adapter *adapter, bool msix)
  2406. {
  2407. struct pci_dev *pdev = adapter->pdev;
  2408. int err = 0;
  2409. igc_set_interrupt_capability(adapter, msix);
  2410. err = igc_alloc_q_vectors(adapter);
  2411. if (err) {
  2412. dev_err(&pdev->dev, "Unable to allocate memory for vectors\n");
  2413. goto err_alloc_q_vectors;
  2414. }
  2415. igc_cache_ring_register(adapter);
  2416. return 0;
  2417. err_alloc_q_vectors:
  2418. igc_reset_interrupt_capability(adapter);
  2419. return err;
  2420. }
  2421. static void igc_free_irq(struct igc_adapter *adapter)
  2422. {
  2423. if (adapter->msix_entries) {
  2424. int vector = 0, i;
  2425. free_irq(adapter->msix_entries[vector++].vector, adapter);
  2426. for (i = 0; i < adapter->num_q_vectors; i++)
  2427. free_irq(adapter->msix_entries[vector++].vector,
  2428. adapter->q_vector[i]);
  2429. } else {
  2430. free_irq(adapter->pdev->irq, adapter);
  2431. }
  2432. }
  2433. /**
  2434. * igc_irq_disable - Mask off interrupt generation on the NIC
  2435. * @adapter: board private structure
  2436. */
  2437. static void igc_irq_disable(struct igc_adapter *adapter)
  2438. {
  2439. struct igc_hw *hw = &adapter->hw;
  2440. if (adapter->msix_entries) {
  2441. u32 regval = rd32(IGC_EIAM);
  2442. wr32(IGC_EIAM, regval & ~adapter->eims_enable_mask);
  2443. wr32(IGC_EIMC, adapter->eims_enable_mask);
  2444. regval = rd32(IGC_EIAC);
  2445. wr32(IGC_EIAC, regval & ~adapter->eims_enable_mask);
  2446. }
  2447. wr32(IGC_IAM, 0);
  2448. wr32(IGC_IMC, ~0);
  2449. wrfl();
  2450. if (adapter->msix_entries) {
  2451. int vector = 0, i;
  2452. synchronize_irq(adapter->msix_entries[vector++].vector);
  2453. for (i = 0; i < adapter->num_q_vectors; i++)
  2454. synchronize_irq(adapter->msix_entries[vector++].vector);
  2455. } else {
  2456. synchronize_irq(adapter->pdev->irq);
  2457. }
  2458. }
  2459. /**
  2460. * igc_irq_enable - Enable default interrupt generation settings
  2461. * @adapter: board private structure
  2462. */
  2463. static void igc_irq_enable(struct igc_adapter *adapter)
  2464. {
  2465. struct igc_hw *hw = &adapter->hw;
  2466. if (adapter->msix_entries) {
  2467. u32 ims = IGC_IMS_LSC | IGC_IMS_DOUTSYNC | IGC_IMS_DRSTA;
  2468. u32 regval = rd32(IGC_EIAC);
  2469. wr32(IGC_EIAC, regval | adapter->eims_enable_mask);
  2470. regval = rd32(IGC_EIAM);
  2471. wr32(IGC_EIAM, regval | adapter->eims_enable_mask);
  2472. wr32(IGC_EIMS, adapter->eims_enable_mask);
  2473. wr32(IGC_IMS, ims);
  2474. } else {
  2475. wr32(IGC_IMS, IMS_ENABLE_MASK | IGC_IMS_DRSTA);
  2476. wr32(IGC_IAM, IMS_ENABLE_MASK | IGC_IMS_DRSTA);
  2477. }
  2478. }
  2479. /**
  2480. * igc_request_irq - initialize interrupts
  2481. * @adapter: Pointer to adapter structure
  2482. *
  2483. * Attempts to configure interrupts using the best available
  2484. * capabilities of the hardware and kernel.
  2485. */
  2486. static int igc_request_irq(struct igc_adapter *adapter)
  2487. {
  2488. struct net_device *netdev = adapter->netdev;
  2489. struct pci_dev *pdev = adapter->pdev;
  2490. int err = 0;
  2491. if (adapter->flags & IGC_FLAG_HAS_MSIX) {
  2492. err = igc_request_msix(adapter);
  2493. if (!err)
  2494. goto request_done;
  2495. /* fall back to MSI */
  2496. igc_free_all_tx_resources(adapter);
  2497. igc_free_all_rx_resources(adapter);
  2498. igc_clear_interrupt_scheme(adapter);
  2499. err = igc_init_interrupt_scheme(adapter, false);
  2500. if (err)
  2501. goto request_done;
  2502. igc_setup_all_tx_resources(adapter);
  2503. igc_setup_all_rx_resources(adapter);
  2504. igc_configure(adapter);
  2505. }
  2506. igc_assign_vector(adapter->q_vector[0], 0);
  2507. if (adapter->flags & IGC_FLAG_HAS_MSI) {
  2508. err = request_irq(pdev->irq, &igc_intr_msi, 0,
  2509. netdev->name, adapter);
  2510. if (!err)
  2511. goto request_done;
  2512. /* fall back to legacy interrupts */
  2513. igc_reset_interrupt_capability(adapter);
  2514. adapter->flags &= ~IGC_FLAG_HAS_MSI;
  2515. }
  2516. err = request_irq(pdev->irq, &igc_intr, IRQF_SHARED,
  2517. netdev->name, adapter);
  2518. if (err)
  2519. dev_err(&pdev->dev, "Error %d getting interrupt\n",
  2520. err);
  2521. request_done:
  2522. return err;
  2523. }
  2524. static void igc_write_itr(struct igc_q_vector *q_vector)
  2525. {
  2526. u32 itr_val = q_vector->itr_val & IGC_QVECTOR_MASK;
  2527. if (!q_vector->set_itr)
  2528. return;
  2529. if (!itr_val)
  2530. itr_val = IGC_ITR_VAL_MASK;
  2531. itr_val |= IGC_EITR_CNT_IGNR;
  2532. writel(itr_val, q_vector->itr_register);
  2533. q_vector->set_itr = 0;
  2534. }
  2535. /**
  2536. * igc_open - Called when a network interface is made active
  2537. * @netdev: network interface device structure
  2538. *
  2539. * Returns 0 on success, negative value on failure
  2540. *
  2541. * The open entry point is called when a network interface is made
  2542. * active by the system (IFF_UP). At this point all resources needed
  2543. * for transmit and receive operations are allocated, the interrupt
  2544. * handler is registered with the OS, the watchdog timer is started,
  2545. * and the stack is notified that the interface is ready.
  2546. */
  2547. static int __igc_open(struct net_device *netdev, bool resuming)
  2548. {
  2549. struct igc_adapter *adapter = netdev_priv(netdev);
  2550. struct igc_hw *hw = &adapter->hw;
  2551. int err = 0;
  2552. int i = 0;
  2553. /* disallow open during test */
  2554. if (test_bit(__IGC_TESTING, &adapter->state)) {
  2555. WARN_ON(resuming);
  2556. return -EBUSY;
  2557. }
  2558. netif_carrier_off(netdev);
  2559. /* allocate transmit descriptors */
  2560. err = igc_setup_all_tx_resources(adapter);
  2561. if (err)
  2562. goto err_setup_tx;
  2563. /* allocate receive descriptors */
  2564. err = igc_setup_all_rx_resources(adapter);
  2565. if (err)
  2566. goto err_setup_rx;
  2567. igc_power_up_link(adapter);
  2568. igc_configure(adapter);
  2569. err = igc_request_irq(adapter);
  2570. if (err)
  2571. goto err_req_irq;
  2572. /* Notify the stack of the actual queue counts. */
  2573. netif_set_real_num_tx_queues(netdev, adapter->num_tx_queues);
  2574. if (err)
  2575. goto err_set_queues;
  2576. err = netif_set_real_num_rx_queues(netdev, adapter->num_rx_queues);
  2577. if (err)
  2578. goto err_set_queues;
  2579. clear_bit(__IGC_DOWN, &adapter->state);
  2580. for (i = 0; i < adapter->num_q_vectors; i++)
  2581. napi_enable(&adapter->q_vector[i]->napi);
  2582. /* Clear any pending interrupts. */
  2583. rd32(IGC_ICR);
  2584. igc_irq_enable(adapter);
  2585. netif_tx_start_all_queues(netdev);
  2586. /* start the watchdog. */
  2587. hw->mac.get_link_status = 1;
  2588. return IGC_SUCCESS;
  2589. err_set_queues:
  2590. igc_free_irq(adapter);
  2591. err_req_irq:
  2592. igc_release_hw_control(adapter);
  2593. igc_power_down_link(adapter);
  2594. igc_free_all_rx_resources(adapter);
  2595. err_setup_rx:
  2596. igc_free_all_tx_resources(adapter);
  2597. err_setup_tx:
  2598. igc_reset(adapter);
  2599. return err;
  2600. }
  2601. static int igc_open(struct net_device *netdev)
  2602. {
  2603. return __igc_open(netdev, false);
  2604. }
  2605. /**
  2606. * igc_close - Disables a network interface
  2607. * @netdev: network interface device structure
  2608. *
  2609. * Returns 0, this is not allowed to fail
  2610. *
  2611. * The close entry point is called when an interface is de-activated
  2612. * by the OS. The hardware is still under the driver's control, but
  2613. * needs to be disabled. A global MAC reset is issued to stop the
  2614. * hardware, and all transmit and receive resources are freed.
  2615. */
  2616. static int __igc_close(struct net_device *netdev, bool suspending)
  2617. {
  2618. struct igc_adapter *adapter = netdev_priv(netdev);
  2619. WARN_ON(test_bit(__IGC_RESETTING, &adapter->state));
  2620. igc_down(adapter);
  2621. igc_release_hw_control(adapter);
  2622. igc_free_irq(adapter);
  2623. igc_free_all_tx_resources(adapter);
  2624. igc_free_all_rx_resources(adapter);
  2625. return 0;
  2626. }
  2627. static int igc_close(struct net_device *netdev)
  2628. {
  2629. if (netif_device_present(netdev) || netdev->dismantle)
  2630. return __igc_close(netdev, false);
  2631. return 0;
  2632. }
  2633. static const struct net_device_ops igc_netdev_ops = {
  2634. .ndo_open = igc_open,
  2635. .ndo_stop = igc_close,
  2636. .ndo_start_xmit = igc_xmit_frame,
  2637. .ndo_set_mac_address = igc_set_mac,
  2638. .ndo_change_mtu = igc_change_mtu,
  2639. .ndo_get_stats = igc_get_stats,
  2640. .ndo_do_ioctl = igc_ioctl,
  2641. };
  2642. /* PCIe configuration access */
  2643. void igc_read_pci_cfg(struct igc_hw *hw, u32 reg, u16 *value)
  2644. {
  2645. struct igc_adapter *adapter = hw->back;
  2646. pci_read_config_word(adapter->pdev, reg, value);
  2647. }
  2648. void igc_write_pci_cfg(struct igc_hw *hw, u32 reg, u16 *value)
  2649. {
  2650. struct igc_adapter *adapter = hw->back;
  2651. pci_write_config_word(adapter->pdev, reg, *value);
  2652. }
  2653. s32 igc_read_pcie_cap_reg(struct igc_hw *hw, u32 reg, u16 *value)
  2654. {
  2655. struct igc_adapter *adapter = hw->back;
  2656. u16 cap_offset;
  2657. cap_offset = pci_find_capability(adapter->pdev, PCI_CAP_ID_EXP);
  2658. if (!cap_offset)
  2659. return -IGC_ERR_CONFIG;
  2660. pci_read_config_word(adapter->pdev, cap_offset + reg, value);
  2661. return IGC_SUCCESS;
  2662. }
  2663. s32 igc_write_pcie_cap_reg(struct igc_hw *hw, u32 reg, u16 *value)
  2664. {
  2665. struct igc_adapter *adapter = hw->back;
  2666. u16 cap_offset;
  2667. cap_offset = pci_find_capability(adapter->pdev, PCI_CAP_ID_EXP);
  2668. if (!cap_offset)
  2669. return -IGC_ERR_CONFIG;
  2670. pci_write_config_word(adapter->pdev, cap_offset + reg, *value);
  2671. return IGC_SUCCESS;
  2672. }
  2673. u32 igc_rd32(struct igc_hw *hw, u32 reg)
  2674. {
  2675. struct igc_adapter *igc = container_of(hw, struct igc_adapter, hw);
  2676. u8 __iomem *hw_addr = READ_ONCE(hw->hw_addr);
  2677. u32 value = 0;
  2678. if (IGC_REMOVED(hw_addr))
  2679. return ~value;
  2680. value = readl(&hw_addr[reg]);
  2681. /* reads should not return all F's */
  2682. if (!(~value) && (!reg || !(~readl(hw_addr)))) {
  2683. struct net_device *netdev = igc->netdev;
  2684. hw->hw_addr = NULL;
  2685. netif_device_detach(netdev);
  2686. netdev_err(netdev, "PCIe link lost, device now detached\n");
  2687. }
  2688. return value;
  2689. }
  2690. /**
  2691. * igc_probe - Device Initialization Routine
  2692. * @pdev: PCI device information struct
  2693. * @ent: entry in igc_pci_tbl
  2694. *
  2695. * Returns 0 on success, negative on failure
  2696. *
  2697. * igc_probe initializes an adapter identified by a pci_dev structure.
  2698. * The OS initialization, configuring the adapter private structure,
  2699. * and a hardware reset occur.
  2700. */
  2701. static int igc_probe(struct pci_dev *pdev,
  2702. const struct pci_device_id *ent)
  2703. {
  2704. struct igc_adapter *adapter;
  2705. struct net_device *netdev;
  2706. struct igc_hw *hw;
  2707. const struct igc_info *ei = igc_info_tbl[ent->driver_data];
  2708. int err, pci_using_dac;
  2709. err = pci_enable_device_mem(pdev);
  2710. if (err)
  2711. return err;
  2712. pci_using_dac = 0;
  2713. err = dma_set_mask(&pdev->dev, DMA_BIT_MASK(64));
  2714. if (!err) {
  2715. err = dma_set_coherent_mask(&pdev->dev,
  2716. DMA_BIT_MASK(64));
  2717. if (!err)
  2718. pci_using_dac = 1;
  2719. } else {
  2720. err = dma_set_mask(&pdev->dev, DMA_BIT_MASK(32));
  2721. if (err) {
  2722. err = dma_set_coherent_mask(&pdev->dev,
  2723. DMA_BIT_MASK(32));
  2724. if (err) {
  2725. IGC_ERR("Wrong DMA configuration, aborting\n");
  2726. goto err_dma;
  2727. }
  2728. }
  2729. }
  2730. err = pci_request_selected_regions(pdev,
  2731. pci_select_bars(pdev,
  2732. IORESOURCE_MEM),
  2733. igc_driver_name);
  2734. if (err)
  2735. goto err_pci_reg;
  2736. pci_enable_pcie_error_reporting(pdev);
  2737. pci_set_master(pdev);
  2738. err = -ENOMEM;
  2739. netdev = alloc_etherdev_mq(sizeof(struct igc_adapter),
  2740. IGC_MAX_TX_QUEUES);
  2741. if (!netdev)
  2742. goto err_alloc_etherdev;
  2743. SET_NETDEV_DEV(netdev, &pdev->dev);
  2744. pci_set_drvdata(pdev, netdev);
  2745. adapter = netdev_priv(netdev);
  2746. adapter->netdev = netdev;
  2747. adapter->pdev = pdev;
  2748. hw = &adapter->hw;
  2749. hw->back = adapter;
  2750. adapter->port_num = hw->bus.func;
  2751. adapter->msg_enable = GENMASK(debug - 1, 0);
  2752. err = pci_save_state(pdev);
  2753. if (err)
  2754. goto err_ioremap;
  2755. err = -EIO;
  2756. adapter->io_addr = ioremap(pci_resource_start(pdev, 0),
  2757. pci_resource_len(pdev, 0));
  2758. if (!adapter->io_addr)
  2759. goto err_ioremap;
  2760. /* hw->hw_addr can be zeroed, so use adapter->io_addr for unmap */
  2761. hw->hw_addr = adapter->io_addr;
  2762. netdev->netdev_ops = &igc_netdev_ops;
  2763. netdev->watchdog_timeo = 5 * HZ;
  2764. netdev->mem_start = pci_resource_start(pdev, 0);
  2765. netdev->mem_end = pci_resource_end(pdev, 0);
  2766. /* PCI config space info */
  2767. hw->vendor_id = pdev->vendor;
  2768. hw->device_id = pdev->device;
  2769. hw->revision_id = pdev->revision;
  2770. hw->subsystem_vendor_id = pdev->subsystem_vendor;
  2771. hw->subsystem_device_id = pdev->subsystem_device;
  2772. /* Copy the default MAC and PHY function pointers */
  2773. memcpy(&hw->mac.ops, ei->mac_ops, sizeof(hw->mac.ops));
  2774. memcpy(&hw->phy.ops, ei->phy_ops, sizeof(hw->phy.ops));
  2775. /* Initialize skew-specific constants */
  2776. err = ei->get_invariants(hw);
  2777. if (err)
  2778. goto err_sw_init;
  2779. /* setup the private structure */
  2780. err = igc_sw_init(adapter);
  2781. if (err)
  2782. goto err_sw_init;
  2783. /* MTU range: 68 - 9216 */
  2784. netdev->min_mtu = ETH_MIN_MTU;
  2785. netdev->max_mtu = MAX_STD_JUMBO_FRAME_SIZE;
  2786. /* configure RXPBSIZE and TXPBSIZE */
  2787. wr32(IGC_RXPBS, I225_RXPBSIZE_DEFAULT);
  2788. wr32(IGC_TXPBS, I225_TXPBSIZE_DEFAULT);
  2789. timer_setup(&adapter->watchdog_timer, igc_watchdog, 0);
  2790. INIT_WORK(&adapter->reset_task, igc_reset_task);
  2791. /* reset the hardware with the new settings */
  2792. igc_reset(adapter);
  2793. /* let the f/w know that the h/w is now under the control of the
  2794. * driver.
  2795. */
  2796. igc_get_hw_control(adapter);
  2797. strncpy(netdev->name, "eth%d", IFNAMSIZ);
  2798. err = register_netdev(netdev);
  2799. if (err)
  2800. goto err_register;
  2801. /* carrier off reporting is important to ethtool even BEFORE open */
  2802. netif_carrier_off(netdev);
  2803. /* Check if Media Autosense is enabled */
  2804. adapter->ei = *ei;
  2805. /* print pcie link status and MAC address */
  2806. pcie_print_link_status(pdev);
  2807. netdev_info(netdev, "MAC: %pM\n", netdev->dev_addr);
  2808. return 0;
  2809. err_register:
  2810. igc_release_hw_control(adapter);
  2811. err_sw_init:
  2812. igc_clear_interrupt_scheme(adapter);
  2813. iounmap(adapter->io_addr);
  2814. err_ioremap:
  2815. free_netdev(netdev);
  2816. err_alloc_etherdev:
  2817. pci_release_selected_regions(pdev,
  2818. pci_select_bars(pdev, IORESOURCE_MEM));
  2819. err_pci_reg:
  2820. err_dma:
  2821. pci_disable_device(pdev);
  2822. return err;
  2823. }
  2824. /**
  2825. * igc_remove - Device Removal Routine
  2826. * @pdev: PCI device information struct
  2827. *
  2828. * igc_remove is called by the PCI subsystem to alert the driver
  2829. * that it should release a PCI device. This could be caused by a
  2830. * Hot-Plug event, or because the driver is going to be removed from
  2831. * memory.
  2832. */
  2833. static void igc_remove(struct pci_dev *pdev)
  2834. {
  2835. struct net_device *netdev = pci_get_drvdata(pdev);
  2836. struct igc_adapter *adapter = netdev_priv(netdev);
  2837. set_bit(__IGC_DOWN, &adapter->state);
  2838. del_timer_sync(&adapter->watchdog_timer);
  2839. cancel_work_sync(&adapter->reset_task);
  2840. /* Release control of h/w to f/w. If f/w is AMT enabled, this
  2841. * would have already happened in close and is redundant.
  2842. */
  2843. igc_release_hw_control(adapter);
  2844. unregister_netdev(netdev);
  2845. igc_clear_interrupt_scheme(adapter);
  2846. pci_iounmap(pdev, adapter->io_addr);
  2847. pci_release_mem_regions(pdev);
  2848. kfree(adapter->mac_table);
  2849. kfree(adapter->shadow_vfta);
  2850. free_netdev(netdev);
  2851. pci_disable_pcie_error_reporting(pdev);
  2852. pci_disable_device(pdev);
  2853. }
  2854. static struct pci_driver igc_driver = {
  2855. .name = igc_driver_name,
  2856. .id_table = igc_pci_tbl,
  2857. .probe = igc_probe,
  2858. .remove = igc_remove,
  2859. };
  2860. static void igc_set_flag_queue_pairs(struct igc_adapter *adapter,
  2861. const u32 max_rss_queues)
  2862. {
  2863. /* Determine if we need to pair queues. */
  2864. /* If rss_queues > half of max_rss_queues, pair the queues in
  2865. * order to conserve interrupts due to limited supply.
  2866. */
  2867. if (adapter->rss_queues > (max_rss_queues / 2))
  2868. adapter->flags |= IGC_FLAG_QUEUE_PAIRS;
  2869. else
  2870. adapter->flags &= ~IGC_FLAG_QUEUE_PAIRS;
  2871. }
  2872. static unsigned int igc_get_max_rss_queues(struct igc_adapter *adapter)
  2873. {
  2874. unsigned int max_rss_queues;
  2875. /* Determine the maximum number of RSS queues supported. */
  2876. max_rss_queues = IGC_MAX_RX_QUEUES;
  2877. return max_rss_queues;
  2878. }
  2879. static void igc_init_queue_configuration(struct igc_adapter *adapter)
  2880. {
  2881. u32 max_rss_queues;
  2882. max_rss_queues = igc_get_max_rss_queues(adapter);
  2883. adapter->rss_queues = min_t(u32, max_rss_queues, num_online_cpus());
  2884. igc_set_flag_queue_pairs(adapter, max_rss_queues);
  2885. }
  2886. /**
  2887. * igc_sw_init - Initialize general software structures (struct igc_adapter)
  2888. * @adapter: board private structure to initialize
  2889. *
  2890. * igc_sw_init initializes the Adapter private data structure.
  2891. * Fields are initialized based on PCI device information and
  2892. * OS network device settings (MTU size).
  2893. */
  2894. static int igc_sw_init(struct igc_adapter *adapter)
  2895. {
  2896. struct net_device *netdev = adapter->netdev;
  2897. struct pci_dev *pdev = adapter->pdev;
  2898. struct igc_hw *hw = &adapter->hw;
  2899. int size = sizeof(struct igc_mac_addr) * hw->mac.rar_entry_count;
  2900. pci_read_config_word(pdev, PCI_COMMAND, &hw->bus.pci_cmd_word);
  2901. /* set default ring sizes */
  2902. adapter->tx_ring_count = IGC_DEFAULT_TXD;
  2903. adapter->rx_ring_count = IGC_DEFAULT_RXD;
  2904. /* set default ITR values */
  2905. adapter->rx_itr_setting = IGC_DEFAULT_ITR;
  2906. adapter->tx_itr_setting = IGC_DEFAULT_ITR;
  2907. /* set default work limits */
  2908. adapter->tx_work_limit = IGC_DEFAULT_TX_WORK;
  2909. /* adjust max frame to be at least the size of a standard frame */
  2910. adapter->max_frame_size = netdev->mtu + ETH_HLEN + ETH_FCS_LEN +
  2911. VLAN_HLEN;
  2912. adapter->min_frame_size = ETH_ZLEN + ETH_FCS_LEN;
  2913. spin_lock_init(&adapter->nfc_lock);
  2914. spin_lock_init(&adapter->stats64_lock);
  2915. /* Assume MSI-X interrupts, will be checked during IRQ allocation */
  2916. adapter->flags |= IGC_FLAG_HAS_MSIX;
  2917. adapter->mac_table = kzalloc(size, GFP_ATOMIC);
  2918. if (!adapter->mac_table)
  2919. return -ENOMEM;
  2920. igc_init_queue_configuration(adapter);
  2921. /* This call may decrease the number of queues */
  2922. if (igc_init_interrupt_scheme(adapter, true)) {
  2923. dev_err(&pdev->dev, "Unable to allocate memory for queues\n");
  2924. return -ENOMEM;
  2925. }
  2926. /* Explicitly disable IRQ since the NIC can be in any state. */
  2927. igc_irq_disable(adapter);
  2928. set_bit(__IGC_DOWN, &adapter->state);
  2929. return 0;
  2930. }
  2931. /**
  2932. * igc_get_hw_dev - return device
  2933. * @hw: pointer to hardware structure
  2934. *
  2935. * used by hardware layer to print debugging information
  2936. */
  2937. struct net_device *igc_get_hw_dev(struct igc_hw *hw)
  2938. {
  2939. struct igc_adapter *adapter = hw->back;
  2940. return adapter->netdev;
  2941. }
  2942. /**
  2943. * igc_init_module - Driver Registration Routine
  2944. *
  2945. * igc_init_module is the first routine called when the driver is
  2946. * loaded. All it does is register with the PCI subsystem.
  2947. */
  2948. static int __init igc_init_module(void)
  2949. {
  2950. int ret;
  2951. pr_info("%s - version %s\n",
  2952. igc_driver_string, igc_driver_version);
  2953. pr_info("%s\n", igc_copyright);
  2954. ret = pci_register_driver(&igc_driver);
  2955. return ret;
  2956. }
  2957. module_init(igc_init_module);
  2958. /**
  2959. * igc_exit_module - Driver Exit Cleanup Routine
  2960. *
  2961. * igc_exit_module is called just before the driver is removed
  2962. * from memory.
  2963. */
  2964. static void __exit igc_exit_module(void)
  2965. {
  2966. pci_unregister_driver(&igc_driver);
  2967. }
  2968. module_exit(igc_exit_module);
  2969. /* igc_main.c */