igc_main.c 91 KB

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