igc_main.c 92 KB

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