igc_main.c 91 KB

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