ixgbevf_main.c 118 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373
  1. /*******************************************************************************
  2. Intel 82599 Virtual Function driver
  3. Copyright(c) 1999 - 2015 Intel Corporation.
  4. This program is free software; you can redistribute it and/or modify it
  5. under the terms and conditions of the GNU General Public License,
  6. version 2, as published by the Free Software Foundation.
  7. This program is distributed in the hope it will be useful, but WITHOUT
  8. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  9. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  10. more details.
  11. You should have received a copy of the GNU General Public License along with
  12. this program; if not, see <http://www.gnu.org/licenses/>.
  13. The full GNU General Public License is included in this distribution in
  14. the file called "COPYING".
  15. Contact Information:
  16. e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
  17. Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  18. *******************************************************************************/
  19. /******************************************************************************
  20. Copyright (c)2006 - 2007 Myricom, Inc. for some LRO specific code
  21. ******************************************************************************/
  22. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  23. #include <linux/types.h>
  24. #include <linux/bitops.h>
  25. #include <linux/module.h>
  26. #include <linux/pci.h>
  27. #include <linux/netdevice.h>
  28. #include <linux/vmalloc.h>
  29. #include <linux/string.h>
  30. #include <linux/in.h>
  31. #include <linux/ip.h>
  32. #include <linux/tcp.h>
  33. #include <linux/sctp.h>
  34. #include <linux/ipv6.h>
  35. #include <linux/slab.h>
  36. #include <net/checksum.h>
  37. #include <net/ip6_checksum.h>
  38. #include <linux/ethtool.h>
  39. #include <linux/if.h>
  40. #include <linux/if_vlan.h>
  41. #include <linux/prefetch.h>
  42. #include "ixgbevf.h"
  43. const char ixgbevf_driver_name[] = "ixgbevf";
  44. static const char ixgbevf_driver_string[] =
  45. "Intel(R) 10 Gigabit PCI Express Virtual Function Network Driver";
  46. #define DRV_VERSION "3.2.2-k"
  47. const char ixgbevf_driver_version[] = DRV_VERSION;
  48. static char ixgbevf_copyright[] =
  49. "Copyright (c) 2009 - 2015 Intel Corporation.";
  50. static const struct ixgbevf_info *ixgbevf_info_tbl[] = {
  51. [board_82599_vf] = &ixgbevf_82599_vf_info,
  52. [board_82599_vf_hv] = &ixgbevf_82599_vf_hv_info,
  53. [board_X540_vf] = &ixgbevf_X540_vf_info,
  54. [board_X540_vf_hv] = &ixgbevf_X540_vf_hv_info,
  55. [board_X550_vf] = &ixgbevf_X550_vf_info,
  56. [board_X550_vf_hv] = &ixgbevf_X550_vf_hv_info,
  57. [board_X550EM_x_vf] = &ixgbevf_X550EM_x_vf_info,
  58. [board_X550EM_x_vf_hv] = &ixgbevf_X550EM_x_vf_hv_info,
  59. [board_x550em_a_vf] = &ixgbevf_x550em_a_vf_info,
  60. };
  61. /* ixgbevf_pci_tbl - PCI Device ID Table
  62. *
  63. * Wildcard entries (PCI_ANY_ID) should come last
  64. * Last entry must be all 0s
  65. *
  66. * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
  67. * Class, Class Mask, private data (not used) }
  68. */
  69. static const struct pci_device_id ixgbevf_pci_tbl[] = {
  70. {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_82599_VF), board_82599_vf },
  71. {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_82599_VF_HV), board_82599_vf_hv },
  72. {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_X540_VF), board_X540_vf },
  73. {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_X540_VF_HV), board_X540_vf_hv },
  74. {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_X550_VF), board_X550_vf },
  75. {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_X550_VF_HV), board_X550_vf_hv },
  76. {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_X550EM_X_VF), board_X550EM_x_vf },
  77. {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_X550EM_X_VF_HV), board_X550EM_x_vf_hv},
  78. {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_X550EM_A_VF), board_x550em_a_vf },
  79. /* required last entry */
  80. {0, }
  81. };
  82. MODULE_DEVICE_TABLE(pci, ixgbevf_pci_tbl);
  83. MODULE_AUTHOR("Intel Corporation, <linux.nics@intel.com>");
  84. MODULE_DESCRIPTION("Intel(R) 10 Gigabit Virtual Function Network Driver");
  85. MODULE_LICENSE("GPL");
  86. MODULE_VERSION(DRV_VERSION);
  87. #define DEFAULT_MSG_ENABLE (NETIF_MSG_DRV|NETIF_MSG_PROBE|NETIF_MSG_LINK)
  88. static int debug = -1;
  89. module_param(debug, int, 0);
  90. MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all)");
  91. static struct workqueue_struct *ixgbevf_wq;
  92. static void ixgbevf_service_event_schedule(struct ixgbevf_adapter *adapter)
  93. {
  94. if (!test_bit(__IXGBEVF_DOWN, &adapter->state) &&
  95. !test_bit(__IXGBEVF_REMOVING, &adapter->state) &&
  96. !test_and_set_bit(__IXGBEVF_SERVICE_SCHED, &adapter->state))
  97. queue_work(ixgbevf_wq, &adapter->service_task);
  98. }
  99. static void ixgbevf_service_event_complete(struct ixgbevf_adapter *adapter)
  100. {
  101. BUG_ON(!test_bit(__IXGBEVF_SERVICE_SCHED, &adapter->state));
  102. /* flush memory to make sure state is correct before next watchdog */
  103. smp_mb__before_atomic();
  104. clear_bit(__IXGBEVF_SERVICE_SCHED, &adapter->state);
  105. }
  106. /* forward decls */
  107. static void ixgbevf_queue_reset_subtask(struct ixgbevf_adapter *adapter);
  108. static void ixgbevf_set_itr(struct ixgbevf_q_vector *q_vector);
  109. static void ixgbevf_free_all_rx_resources(struct ixgbevf_adapter *adapter);
  110. static void ixgbevf_remove_adapter(struct ixgbe_hw *hw)
  111. {
  112. struct ixgbevf_adapter *adapter = hw->back;
  113. if (!hw->hw_addr)
  114. return;
  115. hw->hw_addr = NULL;
  116. dev_err(&adapter->pdev->dev, "Adapter removed\n");
  117. if (test_bit(__IXGBEVF_SERVICE_INITED, &adapter->state))
  118. ixgbevf_service_event_schedule(adapter);
  119. }
  120. static void ixgbevf_check_remove(struct ixgbe_hw *hw, u32 reg)
  121. {
  122. u32 value;
  123. /* The following check not only optimizes a bit by not
  124. * performing a read on the status register when the
  125. * register just read was a status register read that
  126. * returned IXGBE_FAILED_READ_REG. It also blocks any
  127. * potential recursion.
  128. */
  129. if (reg == IXGBE_VFSTATUS) {
  130. ixgbevf_remove_adapter(hw);
  131. return;
  132. }
  133. value = ixgbevf_read_reg(hw, IXGBE_VFSTATUS);
  134. if (value == IXGBE_FAILED_READ_REG)
  135. ixgbevf_remove_adapter(hw);
  136. }
  137. u32 ixgbevf_read_reg(struct ixgbe_hw *hw, u32 reg)
  138. {
  139. u8 __iomem *reg_addr = ACCESS_ONCE(hw->hw_addr);
  140. u32 value;
  141. if (IXGBE_REMOVED(reg_addr))
  142. return IXGBE_FAILED_READ_REG;
  143. value = readl(reg_addr + reg);
  144. if (unlikely(value == IXGBE_FAILED_READ_REG))
  145. ixgbevf_check_remove(hw, reg);
  146. return value;
  147. }
  148. /**
  149. * ixgbevf_set_ivar - set IVAR registers - maps interrupt causes to vectors
  150. * @adapter: pointer to adapter struct
  151. * @direction: 0 for Rx, 1 for Tx, -1 for other causes
  152. * @queue: queue to map the corresponding interrupt to
  153. * @msix_vector: the vector to map to the corresponding queue
  154. **/
  155. static void ixgbevf_set_ivar(struct ixgbevf_adapter *adapter, s8 direction,
  156. u8 queue, u8 msix_vector)
  157. {
  158. u32 ivar, index;
  159. struct ixgbe_hw *hw = &adapter->hw;
  160. if (direction == -1) {
  161. /* other causes */
  162. msix_vector |= IXGBE_IVAR_ALLOC_VAL;
  163. ivar = IXGBE_READ_REG(hw, IXGBE_VTIVAR_MISC);
  164. ivar &= ~0xFF;
  165. ivar |= msix_vector;
  166. IXGBE_WRITE_REG(hw, IXGBE_VTIVAR_MISC, ivar);
  167. } else {
  168. /* Tx or Rx causes */
  169. msix_vector |= IXGBE_IVAR_ALLOC_VAL;
  170. index = ((16 * (queue & 1)) + (8 * direction));
  171. ivar = IXGBE_READ_REG(hw, IXGBE_VTIVAR(queue >> 1));
  172. ivar &= ~(0xFF << index);
  173. ivar |= (msix_vector << index);
  174. IXGBE_WRITE_REG(hw, IXGBE_VTIVAR(queue >> 1), ivar);
  175. }
  176. }
  177. static void ixgbevf_unmap_and_free_tx_resource(struct ixgbevf_ring *tx_ring,
  178. struct ixgbevf_tx_buffer *tx_buffer)
  179. {
  180. if (tx_buffer->skb) {
  181. dev_kfree_skb_any(tx_buffer->skb);
  182. if (dma_unmap_len(tx_buffer, len))
  183. dma_unmap_single(tx_ring->dev,
  184. dma_unmap_addr(tx_buffer, dma),
  185. dma_unmap_len(tx_buffer, len),
  186. DMA_TO_DEVICE);
  187. } else if (dma_unmap_len(tx_buffer, len)) {
  188. dma_unmap_page(tx_ring->dev,
  189. dma_unmap_addr(tx_buffer, dma),
  190. dma_unmap_len(tx_buffer, len),
  191. DMA_TO_DEVICE);
  192. }
  193. tx_buffer->next_to_watch = NULL;
  194. tx_buffer->skb = NULL;
  195. dma_unmap_len_set(tx_buffer, len, 0);
  196. /* tx_buffer must be completely set up in the transmit path */
  197. }
  198. static u64 ixgbevf_get_tx_completed(struct ixgbevf_ring *ring)
  199. {
  200. return ring->stats.packets;
  201. }
  202. static u32 ixgbevf_get_tx_pending(struct ixgbevf_ring *ring)
  203. {
  204. struct ixgbevf_adapter *adapter = netdev_priv(ring->netdev);
  205. struct ixgbe_hw *hw = &adapter->hw;
  206. u32 head = IXGBE_READ_REG(hw, IXGBE_VFTDH(ring->reg_idx));
  207. u32 tail = IXGBE_READ_REG(hw, IXGBE_VFTDT(ring->reg_idx));
  208. if (head != tail)
  209. return (head < tail) ?
  210. tail - head : (tail + ring->count - head);
  211. return 0;
  212. }
  213. static inline bool ixgbevf_check_tx_hang(struct ixgbevf_ring *tx_ring)
  214. {
  215. u32 tx_done = ixgbevf_get_tx_completed(tx_ring);
  216. u32 tx_done_old = tx_ring->tx_stats.tx_done_old;
  217. u32 tx_pending = ixgbevf_get_tx_pending(tx_ring);
  218. clear_check_for_tx_hang(tx_ring);
  219. /* Check for a hung queue, but be thorough. This verifies
  220. * that a transmit has been completed since the previous
  221. * check AND there is at least one packet pending. The
  222. * ARMED bit is set to indicate a potential hang.
  223. */
  224. if ((tx_done_old == tx_done) && tx_pending) {
  225. /* make sure it is true for two checks in a row */
  226. return test_and_set_bit(__IXGBEVF_HANG_CHECK_ARMED,
  227. &tx_ring->state);
  228. }
  229. /* reset the countdown */
  230. clear_bit(__IXGBEVF_HANG_CHECK_ARMED, &tx_ring->state);
  231. /* update completed stats and continue */
  232. tx_ring->tx_stats.tx_done_old = tx_done;
  233. return false;
  234. }
  235. static void ixgbevf_tx_timeout_reset(struct ixgbevf_adapter *adapter)
  236. {
  237. /* Do the reset outside of interrupt context */
  238. if (!test_bit(__IXGBEVF_DOWN, &adapter->state)) {
  239. set_bit(__IXGBEVF_RESET_REQUESTED, &adapter->state);
  240. ixgbevf_service_event_schedule(adapter);
  241. }
  242. }
  243. /**
  244. * ixgbevf_tx_timeout - Respond to a Tx Hang
  245. * @netdev: network interface device structure
  246. **/
  247. static void ixgbevf_tx_timeout(struct net_device *netdev)
  248. {
  249. struct ixgbevf_adapter *adapter = netdev_priv(netdev);
  250. ixgbevf_tx_timeout_reset(adapter);
  251. }
  252. /**
  253. * ixgbevf_clean_tx_irq - Reclaim resources after transmit completes
  254. * @q_vector: board private structure
  255. * @tx_ring: tx ring to clean
  256. * @napi_budget: Used to determine if we are in netpoll
  257. **/
  258. static bool ixgbevf_clean_tx_irq(struct ixgbevf_q_vector *q_vector,
  259. struct ixgbevf_ring *tx_ring, int napi_budget)
  260. {
  261. struct ixgbevf_adapter *adapter = q_vector->adapter;
  262. struct ixgbevf_tx_buffer *tx_buffer;
  263. union ixgbe_adv_tx_desc *tx_desc;
  264. unsigned int total_bytes = 0, total_packets = 0;
  265. unsigned int budget = tx_ring->count / 2;
  266. unsigned int i = tx_ring->next_to_clean;
  267. if (test_bit(__IXGBEVF_DOWN, &adapter->state))
  268. return true;
  269. tx_buffer = &tx_ring->tx_buffer_info[i];
  270. tx_desc = IXGBEVF_TX_DESC(tx_ring, i);
  271. i -= tx_ring->count;
  272. do {
  273. union ixgbe_adv_tx_desc *eop_desc = tx_buffer->next_to_watch;
  274. /* if next_to_watch is not set then there is no work pending */
  275. if (!eop_desc)
  276. break;
  277. /* prevent any other reads prior to eop_desc */
  278. smp_rmb();
  279. /* if DD is not set pending work has not been completed */
  280. if (!(eop_desc->wb.status & cpu_to_le32(IXGBE_TXD_STAT_DD)))
  281. break;
  282. /* clear next_to_watch to prevent false hangs */
  283. tx_buffer->next_to_watch = NULL;
  284. /* update the statistics for this packet */
  285. total_bytes += tx_buffer->bytecount;
  286. total_packets += tx_buffer->gso_segs;
  287. /* free the skb */
  288. napi_consume_skb(tx_buffer->skb, napi_budget);
  289. /* unmap skb header data */
  290. dma_unmap_single(tx_ring->dev,
  291. dma_unmap_addr(tx_buffer, dma),
  292. dma_unmap_len(tx_buffer, len),
  293. DMA_TO_DEVICE);
  294. /* clear tx_buffer data */
  295. tx_buffer->skb = NULL;
  296. dma_unmap_len_set(tx_buffer, len, 0);
  297. /* unmap remaining buffers */
  298. while (tx_desc != eop_desc) {
  299. tx_buffer++;
  300. tx_desc++;
  301. i++;
  302. if (unlikely(!i)) {
  303. i -= tx_ring->count;
  304. tx_buffer = tx_ring->tx_buffer_info;
  305. tx_desc = IXGBEVF_TX_DESC(tx_ring, 0);
  306. }
  307. /* unmap any remaining paged data */
  308. if (dma_unmap_len(tx_buffer, len)) {
  309. dma_unmap_page(tx_ring->dev,
  310. dma_unmap_addr(tx_buffer, dma),
  311. dma_unmap_len(tx_buffer, len),
  312. DMA_TO_DEVICE);
  313. dma_unmap_len_set(tx_buffer, len, 0);
  314. }
  315. }
  316. /* move us one more past the eop_desc for start of next pkt */
  317. tx_buffer++;
  318. tx_desc++;
  319. i++;
  320. if (unlikely(!i)) {
  321. i -= tx_ring->count;
  322. tx_buffer = tx_ring->tx_buffer_info;
  323. tx_desc = IXGBEVF_TX_DESC(tx_ring, 0);
  324. }
  325. /* issue prefetch for next Tx descriptor */
  326. prefetch(tx_desc);
  327. /* update budget accounting */
  328. budget--;
  329. } while (likely(budget));
  330. i += tx_ring->count;
  331. tx_ring->next_to_clean = i;
  332. u64_stats_update_begin(&tx_ring->syncp);
  333. tx_ring->stats.bytes += total_bytes;
  334. tx_ring->stats.packets += total_packets;
  335. u64_stats_update_end(&tx_ring->syncp);
  336. q_vector->tx.total_bytes += total_bytes;
  337. q_vector->tx.total_packets += total_packets;
  338. if (check_for_tx_hang(tx_ring) && ixgbevf_check_tx_hang(tx_ring)) {
  339. struct ixgbe_hw *hw = &adapter->hw;
  340. union ixgbe_adv_tx_desc *eop_desc;
  341. eop_desc = tx_ring->tx_buffer_info[i].next_to_watch;
  342. pr_err("Detected Tx Unit Hang\n"
  343. " Tx Queue <%d>\n"
  344. " TDH, TDT <%x>, <%x>\n"
  345. " next_to_use <%x>\n"
  346. " next_to_clean <%x>\n"
  347. "tx_buffer_info[next_to_clean]\n"
  348. " next_to_watch <%p>\n"
  349. " eop_desc->wb.status <%x>\n"
  350. " time_stamp <%lx>\n"
  351. " jiffies <%lx>\n",
  352. tx_ring->queue_index,
  353. IXGBE_READ_REG(hw, IXGBE_VFTDH(tx_ring->reg_idx)),
  354. IXGBE_READ_REG(hw, IXGBE_VFTDT(tx_ring->reg_idx)),
  355. tx_ring->next_to_use, i,
  356. eop_desc, (eop_desc ? eop_desc->wb.status : 0),
  357. tx_ring->tx_buffer_info[i].time_stamp, jiffies);
  358. netif_stop_subqueue(tx_ring->netdev, tx_ring->queue_index);
  359. /* schedule immediate reset if we believe we hung */
  360. ixgbevf_tx_timeout_reset(adapter);
  361. return true;
  362. }
  363. #define TX_WAKE_THRESHOLD (DESC_NEEDED * 2)
  364. if (unlikely(total_packets && netif_carrier_ok(tx_ring->netdev) &&
  365. (ixgbevf_desc_unused(tx_ring) >= TX_WAKE_THRESHOLD))) {
  366. /* Make sure that anybody stopping the queue after this
  367. * sees the new next_to_clean.
  368. */
  369. smp_mb();
  370. if (__netif_subqueue_stopped(tx_ring->netdev,
  371. tx_ring->queue_index) &&
  372. !test_bit(__IXGBEVF_DOWN, &adapter->state)) {
  373. netif_wake_subqueue(tx_ring->netdev,
  374. tx_ring->queue_index);
  375. ++tx_ring->tx_stats.restart_queue;
  376. }
  377. }
  378. return !!budget;
  379. }
  380. /**
  381. * ixgbevf_rx_skb - Helper function to determine proper Rx method
  382. * @q_vector: structure containing interrupt and ring information
  383. * @skb: packet to send up
  384. **/
  385. static void ixgbevf_rx_skb(struct ixgbevf_q_vector *q_vector,
  386. struct sk_buff *skb)
  387. {
  388. #ifdef CONFIG_NET_RX_BUSY_POLL
  389. skb_mark_napi_id(skb, &q_vector->napi);
  390. if (ixgbevf_qv_busy_polling(q_vector)) {
  391. netif_receive_skb(skb);
  392. /* exit early if we busy polled */
  393. return;
  394. }
  395. #endif /* CONFIG_NET_RX_BUSY_POLL */
  396. napi_gro_receive(&q_vector->napi, skb);
  397. }
  398. #define IXGBE_RSS_L4_TYPES_MASK \
  399. ((1ul << IXGBE_RXDADV_RSSTYPE_IPV4_TCP) | \
  400. (1ul << IXGBE_RXDADV_RSSTYPE_IPV4_UDP) | \
  401. (1ul << IXGBE_RXDADV_RSSTYPE_IPV6_TCP) | \
  402. (1ul << IXGBE_RXDADV_RSSTYPE_IPV6_UDP))
  403. static inline void ixgbevf_rx_hash(struct ixgbevf_ring *ring,
  404. union ixgbe_adv_rx_desc *rx_desc,
  405. struct sk_buff *skb)
  406. {
  407. u16 rss_type;
  408. if (!(ring->netdev->features & NETIF_F_RXHASH))
  409. return;
  410. rss_type = le16_to_cpu(rx_desc->wb.lower.lo_dword.hs_rss.pkt_info) &
  411. IXGBE_RXDADV_RSSTYPE_MASK;
  412. if (!rss_type)
  413. return;
  414. skb_set_hash(skb, le32_to_cpu(rx_desc->wb.lower.hi_dword.rss),
  415. (IXGBE_RSS_L4_TYPES_MASK & (1ul << rss_type)) ?
  416. PKT_HASH_TYPE_L4 : PKT_HASH_TYPE_L3);
  417. }
  418. /**
  419. * ixgbevf_rx_checksum - indicate in skb if hw indicated a good cksum
  420. * @ring: structure containig ring specific data
  421. * @rx_desc: current Rx descriptor being processed
  422. * @skb: skb currently being received and modified
  423. **/
  424. static inline void ixgbevf_rx_checksum(struct ixgbevf_ring *ring,
  425. union ixgbe_adv_rx_desc *rx_desc,
  426. struct sk_buff *skb)
  427. {
  428. skb_checksum_none_assert(skb);
  429. /* Rx csum disabled */
  430. if (!(ring->netdev->features & NETIF_F_RXCSUM))
  431. return;
  432. /* if IP and error */
  433. if (ixgbevf_test_staterr(rx_desc, IXGBE_RXD_STAT_IPCS) &&
  434. ixgbevf_test_staterr(rx_desc, IXGBE_RXDADV_ERR_IPE)) {
  435. ring->rx_stats.csum_err++;
  436. return;
  437. }
  438. if (!ixgbevf_test_staterr(rx_desc, IXGBE_RXD_STAT_L4CS))
  439. return;
  440. if (ixgbevf_test_staterr(rx_desc, IXGBE_RXDADV_ERR_TCPE)) {
  441. ring->rx_stats.csum_err++;
  442. return;
  443. }
  444. /* It must be a TCP or UDP packet with a valid checksum */
  445. skb->ip_summed = CHECKSUM_UNNECESSARY;
  446. }
  447. /**
  448. * ixgbevf_process_skb_fields - Populate skb header fields from Rx descriptor
  449. * @rx_ring: rx descriptor ring packet is being transacted on
  450. * @rx_desc: pointer to the EOP Rx descriptor
  451. * @skb: pointer to current skb being populated
  452. *
  453. * This function checks the ring, descriptor, and packet information in
  454. * order to populate the checksum, VLAN, protocol, and other fields within
  455. * the skb.
  456. **/
  457. static void ixgbevf_process_skb_fields(struct ixgbevf_ring *rx_ring,
  458. union ixgbe_adv_rx_desc *rx_desc,
  459. struct sk_buff *skb)
  460. {
  461. ixgbevf_rx_hash(rx_ring, rx_desc, skb);
  462. ixgbevf_rx_checksum(rx_ring, rx_desc, skb);
  463. if (ixgbevf_test_staterr(rx_desc, IXGBE_RXD_STAT_VP)) {
  464. u16 vid = le16_to_cpu(rx_desc->wb.upper.vlan);
  465. unsigned long *active_vlans = netdev_priv(rx_ring->netdev);
  466. if (test_bit(vid & VLAN_VID_MASK, active_vlans))
  467. __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vid);
  468. }
  469. skb->protocol = eth_type_trans(skb, rx_ring->netdev);
  470. }
  471. /**
  472. * ixgbevf_is_non_eop - process handling of non-EOP buffers
  473. * @rx_ring: Rx ring being processed
  474. * @rx_desc: Rx descriptor for current buffer
  475. * @skb: current socket buffer containing buffer in progress
  476. *
  477. * This function updates next to clean. If the buffer is an EOP buffer
  478. * this function exits returning false, otherwise it will place the
  479. * sk_buff in the next buffer to be chained and return true indicating
  480. * that this is in fact a non-EOP buffer.
  481. **/
  482. static bool ixgbevf_is_non_eop(struct ixgbevf_ring *rx_ring,
  483. union ixgbe_adv_rx_desc *rx_desc)
  484. {
  485. u32 ntc = rx_ring->next_to_clean + 1;
  486. /* fetch, update, and store next to clean */
  487. ntc = (ntc < rx_ring->count) ? ntc : 0;
  488. rx_ring->next_to_clean = ntc;
  489. prefetch(IXGBEVF_RX_DESC(rx_ring, ntc));
  490. if (likely(ixgbevf_test_staterr(rx_desc, IXGBE_RXD_STAT_EOP)))
  491. return false;
  492. return true;
  493. }
  494. static bool ixgbevf_alloc_mapped_page(struct ixgbevf_ring *rx_ring,
  495. struct ixgbevf_rx_buffer *bi)
  496. {
  497. struct page *page = bi->page;
  498. dma_addr_t dma = bi->dma;
  499. /* since we are recycling buffers we should seldom need to alloc */
  500. if (likely(page))
  501. return true;
  502. /* alloc new page for storage */
  503. page = dev_alloc_page();
  504. if (unlikely(!page)) {
  505. rx_ring->rx_stats.alloc_rx_page_failed++;
  506. return false;
  507. }
  508. /* map page for use */
  509. dma = dma_map_page(rx_ring->dev, page, 0,
  510. PAGE_SIZE, DMA_FROM_DEVICE);
  511. /* if mapping failed free memory back to system since
  512. * there isn't much point in holding memory we can't use
  513. */
  514. if (dma_mapping_error(rx_ring->dev, dma)) {
  515. __free_page(page);
  516. rx_ring->rx_stats.alloc_rx_buff_failed++;
  517. return false;
  518. }
  519. bi->dma = dma;
  520. bi->page = page;
  521. bi->page_offset = 0;
  522. return true;
  523. }
  524. /**
  525. * ixgbevf_alloc_rx_buffers - Replace used receive buffers; packet split
  526. * @rx_ring: rx descriptor ring (for a specific queue) to setup buffers on
  527. * @cleaned_count: number of buffers to replace
  528. **/
  529. static void ixgbevf_alloc_rx_buffers(struct ixgbevf_ring *rx_ring,
  530. u16 cleaned_count)
  531. {
  532. union ixgbe_adv_rx_desc *rx_desc;
  533. struct ixgbevf_rx_buffer *bi;
  534. unsigned int i = rx_ring->next_to_use;
  535. /* nothing to do or no valid netdev defined */
  536. if (!cleaned_count || !rx_ring->netdev)
  537. return;
  538. rx_desc = IXGBEVF_RX_DESC(rx_ring, i);
  539. bi = &rx_ring->rx_buffer_info[i];
  540. i -= rx_ring->count;
  541. do {
  542. if (!ixgbevf_alloc_mapped_page(rx_ring, bi))
  543. break;
  544. /* Refresh the desc even if pkt_addr didn't change
  545. * because each write-back erases this info.
  546. */
  547. rx_desc->read.pkt_addr = cpu_to_le64(bi->dma + bi->page_offset);
  548. rx_desc++;
  549. bi++;
  550. i++;
  551. if (unlikely(!i)) {
  552. rx_desc = IXGBEVF_RX_DESC(rx_ring, 0);
  553. bi = rx_ring->rx_buffer_info;
  554. i -= rx_ring->count;
  555. }
  556. /* clear the hdr_addr for the next_to_use descriptor */
  557. rx_desc->read.hdr_addr = 0;
  558. cleaned_count--;
  559. } while (cleaned_count);
  560. i += rx_ring->count;
  561. if (rx_ring->next_to_use != i) {
  562. /* record the next descriptor to use */
  563. rx_ring->next_to_use = i;
  564. /* update next to alloc since we have filled the ring */
  565. rx_ring->next_to_alloc = i;
  566. /* Force memory writes to complete before letting h/w
  567. * know there are new descriptors to fetch. (Only
  568. * applicable for weak-ordered memory model archs,
  569. * such as IA-64).
  570. */
  571. wmb();
  572. ixgbevf_write_tail(rx_ring, i);
  573. }
  574. }
  575. /**
  576. * ixgbevf_cleanup_headers - Correct corrupted or empty headers
  577. * @rx_ring: rx descriptor ring packet is being transacted on
  578. * @rx_desc: pointer to the EOP Rx descriptor
  579. * @skb: pointer to current skb being fixed
  580. *
  581. * Check for corrupted packet headers caused by senders on the local L2
  582. * embedded NIC switch not setting up their Tx Descriptors right. These
  583. * should be very rare.
  584. *
  585. * Also address the case where we are pulling data in on pages only
  586. * and as such no data is present in the skb header.
  587. *
  588. * In addition if skb is not at least 60 bytes we need to pad it so that
  589. * it is large enough to qualify as a valid Ethernet frame.
  590. *
  591. * Returns true if an error was encountered and skb was freed.
  592. **/
  593. static bool ixgbevf_cleanup_headers(struct ixgbevf_ring *rx_ring,
  594. union ixgbe_adv_rx_desc *rx_desc,
  595. struct sk_buff *skb)
  596. {
  597. /* verify that the packet does not have any known errors */
  598. if (unlikely(ixgbevf_test_staterr(rx_desc,
  599. IXGBE_RXDADV_ERR_FRAME_ERR_MASK))) {
  600. struct net_device *netdev = rx_ring->netdev;
  601. if (!(netdev->features & NETIF_F_RXALL)) {
  602. dev_kfree_skb_any(skb);
  603. return true;
  604. }
  605. }
  606. /* if eth_skb_pad returns an error the skb was freed */
  607. if (eth_skb_pad(skb))
  608. return true;
  609. return false;
  610. }
  611. /**
  612. * ixgbevf_reuse_rx_page - page flip buffer and store it back on the ring
  613. * @rx_ring: rx descriptor ring to store buffers on
  614. * @old_buff: donor buffer to have page reused
  615. *
  616. * Synchronizes page for reuse by the adapter
  617. **/
  618. static void ixgbevf_reuse_rx_page(struct ixgbevf_ring *rx_ring,
  619. struct ixgbevf_rx_buffer *old_buff)
  620. {
  621. struct ixgbevf_rx_buffer *new_buff;
  622. u16 nta = rx_ring->next_to_alloc;
  623. new_buff = &rx_ring->rx_buffer_info[nta];
  624. /* update, and store next to alloc */
  625. nta++;
  626. rx_ring->next_to_alloc = (nta < rx_ring->count) ? nta : 0;
  627. /* transfer page from old buffer to new buffer */
  628. new_buff->page = old_buff->page;
  629. new_buff->dma = old_buff->dma;
  630. new_buff->page_offset = old_buff->page_offset;
  631. /* sync the buffer for use by the device */
  632. dma_sync_single_range_for_device(rx_ring->dev, new_buff->dma,
  633. new_buff->page_offset,
  634. IXGBEVF_RX_BUFSZ,
  635. DMA_FROM_DEVICE);
  636. }
  637. static inline bool ixgbevf_page_is_reserved(struct page *page)
  638. {
  639. return (page_to_nid(page) != numa_mem_id()) || page_is_pfmemalloc(page);
  640. }
  641. /**
  642. * ixgbevf_add_rx_frag - Add contents of Rx buffer to sk_buff
  643. * @rx_ring: rx descriptor ring to transact packets on
  644. * @rx_buffer: buffer containing page to add
  645. * @rx_desc: descriptor containing length of buffer written by hardware
  646. * @skb: sk_buff to place the data into
  647. *
  648. * This function will add the data contained in rx_buffer->page to the skb.
  649. * This is done either through a direct copy if the data in the buffer is
  650. * less than the skb header size, otherwise it will just attach the page as
  651. * a frag to the skb.
  652. *
  653. * The function will then update the page offset if necessary and return
  654. * true if the buffer can be reused by the adapter.
  655. **/
  656. static bool ixgbevf_add_rx_frag(struct ixgbevf_ring *rx_ring,
  657. struct ixgbevf_rx_buffer *rx_buffer,
  658. union ixgbe_adv_rx_desc *rx_desc,
  659. struct sk_buff *skb)
  660. {
  661. struct page *page = rx_buffer->page;
  662. unsigned char *va = page_address(page) + rx_buffer->page_offset;
  663. unsigned int size = le16_to_cpu(rx_desc->wb.upper.length);
  664. #if (PAGE_SIZE < 8192)
  665. unsigned int truesize = IXGBEVF_RX_BUFSZ;
  666. #else
  667. unsigned int truesize = ALIGN(size, L1_CACHE_BYTES);
  668. #endif
  669. unsigned int pull_len;
  670. if (unlikely(skb_is_nonlinear(skb)))
  671. goto add_tail_frag;
  672. if (likely(size <= IXGBEVF_RX_HDR_SIZE)) {
  673. memcpy(__skb_put(skb, size), va, ALIGN(size, sizeof(long)));
  674. /* page is not reserved, we can reuse buffer as is */
  675. if (likely(!ixgbevf_page_is_reserved(page)))
  676. return true;
  677. /* this page cannot be reused so discard it */
  678. put_page(page);
  679. return false;
  680. }
  681. /* we need the header to contain the greater of either ETH_HLEN or
  682. * 60 bytes if the skb->len is less than 60 for skb_pad.
  683. */
  684. pull_len = eth_get_headlen(va, IXGBEVF_RX_HDR_SIZE);
  685. /* align pull length to size of long to optimize memcpy performance */
  686. memcpy(__skb_put(skb, pull_len), va, ALIGN(pull_len, sizeof(long)));
  687. /* update all of the pointers */
  688. va += pull_len;
  689. size -= pull_len;
  690. add_tail_frag:
  691. skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, page,
  692. (unsigned long)va & ~PAGE_MASK, size, truesize);
  693. /* avoid re-using remote pages */
  694. if (unlikely(ixgbevf_page_is_reserved(page)))
  695. return false;
  696. #if (PAGE_SIZE < 8192)
  697. /* if we are only owner of page we can reuse it */
  698. if (unlikely(page_count(page) != 1))
  699. return false;
  700. /* flip page offset to other buffer */
  701. rx_buffer->page_offset ^= IXGBEVF_RX_BUFSZ;
  702. #else
  703. /* move offset up to the next cache line */
  704. rx_buffer->page_offset += truesize;
  705. if (rx_buffer->page_offset > (PAGE_SIZE - IXGBEVF_RX_BUFSZ))
  706. return false;
  707. #endif
  708. /* Even if we own the page, we are not allowed to use atomic_set()
  709. * This would break get_page_unless_zero() users.
  710. */
  711. page_ref_inc(page);
  712. return true;
  713. }
  714. static struct sk_buff *ixgbevf_fetch_rx_buffer(struct ixgbevf_ring *rx_ring,
  715. union ixgbe_adv_rx_desc *rx_desc,
  716. struct sk_buff *skb)
  717. {
  718. struct ixgbevf_rx_buffer *rx_buffer;
  719. struct page *page;
  720. rx_buffer = &rx_ring->rx_buffer_info[rx_ring->next_to_clean];
  721. page = rx_buffer->page;
  722. prefetchw(page);
  723. if (likely(!skb)) {
  724. void *page_addr = page_address(page) +
  725. rx_buffer->page_offset;
  726. /* prefetch first cache line of first page */
  727. prefetch(page_addr);
  728. #if L1_CACHE_BYTES < 128
  729. prefetch(page_addr + L1_CACHE_BYTES);
  730. #endif
  731. /* allocate a skb to store the frags */
  732. skb = netdev_alloc_skb_ip_align(rx_ring->netdev,
  733. IXGBEVF_RX_HDR_SIZE);
  734. if (unlikely(!skb)) {
  735. rx_ring->rx_stats.alloc_rx_buff_failed++;
  736. return NULL;
  737. }
  738. /* we will be copying header into skb->data in
  739. * pskb_may_pull so it is in our interest to prefetch
  740. * it now to avoid a possible cache miss
  741. */
  742. prefetchw(skb->data);
  743. }
  744. /* we are reusing so sync this buffer for CPU use */
  745. dma_sync_single_range_for_cpu(rx_ring->dev,
  746. rx_buffer->dma,
  747. rx_buffer->page_offset,
  748. IXGBEVF_RX_BUFSZ,
  749. DMA_FROM_DEVICE);
  750. /* pull page into skb */
  751. if (ixgbevf_add_rx_frag(rx_ring, rx_buffer, rx_desc, skb)) {
  752. /* hand second half of page back to the ring */
  753. ixgbevf_reuse_rx_page(rx_ring, rx_buffer);
  754. } else {
  755. /* we are not reusing the buffer so unmap it */
  756. dma_unmap_page(rx_ring->dev, rx_buffer->dma,
  757. PAGE_SIZE, DMA_FROM_DEVICE);
  758. }
  759. /* clear contents of buffer_info */
  760. rx_buffer->dma = 0;
  761. rx_buffer->page = NULL;
  762. return skb;
  763. }
  764. static inline void ixgbevf_irq_enable_queues(struct ixgbevf_adapter *adapter,
  765. u32 qmask)
  766. {
  767. struct ixgbe_hw *hw = &adapter->hw;
  768. IXGBE_WRITE_REG(hw, IXGBE_VTEIMS, qmask);
  769. }
  770. static int ixgbevf_clean_rx_irq(struct ixgbevf_q_vector *q_vector,
  771. struct ixgbevf_ring *rx_ring,
  772. int budget)
  773. {
  774. unsigned int total_rx_bytes = 0, total_rx_packets = 0;
  775. u16 cleaned_count = ixgbevf_desc_unused(rx_ring);
  776. struct sk_buff *skb = rx_ring->skb;
  777. while (likely(total_rx_packets < budget)) {
  778. union ixgbe_adv_rx_desc *rx_desc;
  779. /* return some buffers to hardware, one at a time is too slow */
  780. if (cleaned_count >= IXGBEVF_RX_BUFFER_WRITE) {
  781. ixgbevf_alloc_rx_buffers(rx_ring, cleaned_count);
  782. cleaned_count = 0;
  783. }
  784. rx_desc = IXGBEVF_RX_DESC(rx_ring, rx_ring->next_to_clean);
  785. if (!ixgbevf_test_staterr(rx_desc, IXGBE_RXD_STAT_DD))
  786. break;
  787. /* This memory barrier is needed to keep us from reading
  788. * any other fields out of the rx_desc until we know the
  789. * RXD_STAT_DD bit is set
  790. */
  791. rmb();
  792. /* retrieve a buffer from the ring */
  793. skb = ixgbevf_fetch_rx_buffer(rx_ring, rx_desc, skb);
  794. /* exit if we failed to retrieve a buffer */
  795. if (!skb)
  796. break;
  797. cleaned_count++;
  798. /* fetch next buffer in frame if non-eop */
  799. if (ixgbevf_is_non_eop(rx_ring, rx_desc))
  800. continue;
  801. /* verify the packet layout is correct */
  802. if (ixgbevf_cleanup_headers(rx_ring, rx_desc, skb)) {
  803. skb = NULL;
  804. continue;
  805. }
  806. /* probably a little skewed due to removing CRC */
  807. total_rx_bytes += skb->len;
  808. /* Workaround hardware that can't do proper VEPA multicast
  809. * source pruning.
  810. */
  811. if ((skb->pkt_type == PACKET_BROADCAST ||
  812. skb->pkt_type == PACKET_MULTICAST) &&
  813. ether_addr_equal(rx_ring->netdev->dev_addr,
  814. eth_hdr(skb)->h_source)) {
  815. dev_kfree_skb_irq(skb);
  816. continue;
  817. }
  818. /* populate checksum, VLAN, and protocol */
  819. ixgbevf_process_skb_fields(rx_ring, rx_desc, skb);
  820. ixgbevf_rx_skb(q_vector, skb);
  821. /* reset skb pointer */
  822. skb = NULL;
  823. /* update budget accounting */
  824. total_rx_packets++;
  825. }
  826. /* place incomplete frames back on ring for completion */
  827. rx_ring->skb = skb;
  828. u64_stats_update_begin(&rx_ring->syncp);
  829. rx_ring->stats.packets += total_rx_packets;
  830. rx_ring->stats.bytes += total_rx_bytes;
  831. u64_stats_update_end(&rx_ring->syncp);
  832. q_vector->rx.total_packets += total_rx_packets;
  833. q_vector->rx.total_bytes += total_rx_bytes;
  834. return total_rx_packets;
  835. }
  836. /**
  837. * ixgbevf_poll - NAPI polling calback
  838. * @napi: napi struct with our devices info in it
  839. * @budget: amount of work driver is allowed to do this pass, in packets
  840. *
  841. * This function will clean more than one or more rings associated with a
  842. * q_vector.
  843. **/
  844. static int ixgbevf_poll(struct napi_struct *napi, int budget)
  845. {
  846. struct ixgbevf_q_vector *q_vector =
  847. container_of(napi, struct ixgbevf_q_vector, napi);
  848. struct ixgbevf_adapter *adapter = q_vector->adapter;
  849. struct ixgbevf_ring *ring;
  850. int per_ring_budget, work_done = 0;
  851. bool clean_complete = true;
  852. ixgbevf_for_each_ring(ring, q_vector->tx) {
  853. if (!ixgbevf_clean_tx_irq(q_vector, ring, budget))
  854. clean_complete = false;
  855. }
  856. if (budget <= 0)
  857. return budget;
  858. #ifdef CONFIG_NET_RX_BUSY_POLL
  859. if (!ixgbevf_qv_lock_napi(q_vector))
  860. return budget;
  861. #endif
  862. /* attempt to distribute budget to each queue fairly, but don't allow
  863. * the budget to go below 1 because we'll exit polling
  864. */
  865. if (q_vector->rx.count > 1)
  866. per_ring_budget = max(budget/q_vector->rx.count, 1);
  867. else
  868. per_ring_budget = budget;
  869. ixgbevf_for_each_ring(ring, q_vector->rx) {
  870. int cleaned = ixgbevf_clean_rx_irq(q_vector, ring,
  871. per_ring_budget);
  872. work_done += cleaned;
  873. if (cleaned >= per_ring_budget)
  874. clean_complete = false;
  875. }
  876. #ifdef CONFIG_NET_RX_BUSY_POLL
  877. ixgbevf_qv_unlock_napi(q_vector);
  878. #endif
  879. /* If all work not completed, return budget and keep polling */
  880. if (!clean_complete)
  881. return budget;
  882. /* all work done, exit the polling mode */
  883. napi_complete_done(napi, work_done);
  884. if (adapter->rx_itr_setting == 1)
  885. ixgbevf_set_itr(q_vector);
  886. if (!test_bit(__IXGBEVF_DOWN, &adapter->state) &&
  887. !test_bit(__IXGBEVF_REMOVING, &adapter->state))
  888. ixgbevf_irq_enable_queues(adapter,
  889. BIT(q_vector->v_idx));
  890. return 0;
  891. }
  892. /**
  893. * ixgbevf_write_eitr - write VTEITR register in hardware specific way
  894. * @q_vector: structure containing interrupt and ring information
  895. **/
  896. void ixgbevf_write_eitr(struct ixgbevf_q_vector *q_vector)
  897. {
  898. struct ixgbevf_adapter *adapter = q_vector->adapter;
  899. struct ixgbe_hw *hw = &adapter->hw;
  900. int v_idx = q_vector->v_idx;
  901. u32 itr_reg = q_vector->itr & IXGBE_MAX_EITR;
  902. /* set the WDIS bit to not clear the timer bits and cause an
  903. * immediate assertion of the interrupt
  904. */
  905. itr_reg |= IXGBE_EITR_CNT_WDIS;
  906. IXGBE_WRITE_REG(hw, IXGBE_VTEITR(v_idx), itr_reg);
  907. }
  908. #ifdef CONFIG_NET_RX_BUSY_POLL
  909. /* must be called with local_bh_disable()d */
  910. static int ixgbevf_busy_poll_recv(struct napi_struct *napi)
  911. {
  912. struct ixgbevf_q_vector *q_vector =
  913. container_of(napi, struct ixgbevf_q_vector, napi);
  914. struct ixgbevf_adapter *adapter = q_vector->adapter;
  915. struct ixgbevf_ring *ring;
  916. int found = 0;
  917. if (test_bit(__IXGBEVF_DOWN, &adapter->state))
  918. return LL_FLUSH_FAILED;
  919. if (!ixgbevf_qv_lock_poll(q_vector))
  920. return LL_FLUSH_BUSY;
  921. ixgbevf_for_each_ring(ring, q_vector->rx) {
  922. found = ixgbevf_clean_rx_irq(q_vector, ring, 4);
  923. #ifdef BP_EXTENDED_STATS
  924. if (found)
  925. ring->stats.cleaned += found;
  926. else
  927. ring->stats.misses++;
  928. #endif
  929. if (found)
  930. break;
  931. }
  932. ixgbevf_qv_unlock_poll(q_vector);
  933. return found;
  934. }
  935. #endif /* CONFIG_NET_RX_BUSY_POLL */
  936. /**
  937. * ixgbevf_configure_msix - Configure MSI-X hardware
  938. * @adapter: board private structure
  939. *
  940. * ixgbevf_configure_msix sets up the hardware to properly generate MSI-X
  941. * interrupts.
  942. **/
  943. static void ixgbevf_configure_msix(struct ixgbevf_adapter *adapter)
  944. {
  945. struct ixgbevf_q_vector *q_vector;
  946. int q_vectors, v_idx;
  947. q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
  948. adapter->eims_enable_mask = 0;
  949. /* Populate the IVAR table and set the ITR values to the
  950. * corresponding register.
  951. */
  952. for (v_idx = 0; v_idx < q_vectors; v_idx++) {
  953. struct ixgbevf_ring *ring;
  954. q_vector = adapter->q_vector[v_idx];
  955. ixgbevf_for_each_ring(ring, q_vector->rx)
  956. ixgbevf_set_ivar(adapter, 0, ring->reg_idx, v_idx);
  957. ixgbevf_for_each_ring(ring, q_vector->tx)
  958. ixgbevf_set_ivar(adapter, 1, ring->reg_idx, v_idx);
  959. if (q_vector->tx.ring && !q_vector->rx.ring) {
  960. /* Tx only vector */
  961. if (adapter->tx_itr_setting == 1)
  962. q_vector->itr = IXGBE_12K_ITR;
  963. else
  964. q_vector->itr = adapter->tx_itr_setting;
  965. } else {
  966. /* Rx or Rx/Tx vector */
  967. if (adapter->rx_itr_setting == 1)
  968. q_vector->itr = IXGBE_20K_ITR;
  969. else
  970. q_vector->itr = adapter->rx_itr_setting;
  971. }
  972. /* add q_vector eims value to global eims_enable_mask */
  973. adapter->eims_enable_mask |= BIT(v_idx);
  974. ixgbevf_write_eitr(q_vector);
  975. }
  976. ixgbevf_set_ivar(adapter, -1, 1, v_idx);
  977. /* setup eims_other and add value to global eims_enable_mask */
  978. adapter->eims_other = BIT(v_idx);
  979. adapter->eims_enable_mask |= adapter->eims_other;
  980. }
  981. enum latency_range {
  982. lowest_latency = 0,
  983. low_latency = 1,
  984. bulk_latency = 2,
  985. latency_invalid = 255
  986. };
  987. /**
  988. * ixgbevf_update_itr - update the dynamic ITR value based on statistics
  989. * @q_vector: structure containing interrupt and ring information
  990. * @ring_container: structure containing ring performance data
  991. *
  992. * Stores a new ITR value based on packets and byte
  993. * counts during the last interrupt. The advantage of per interrupt
  994. * computation is faster updates and more accurate ITR for the current
  995. * traffic pattern. Constants in this function were computed
  996. * based on theoretical maximum wire speed and thresholds were set based
  997. * on testing data as well as attempting to minimize response time
  998. * while increasing bulk throughput.
  999. **/
  1000. static void ixgbevf_update_itr(struct ixgbevf_q_vector *q_vector,
  1001. struct ixgbevf_ring_container *ring_container)
  1002. {
  1003. int bytes = ring_container->total_bytes;
  1004. int packets = ring_container->total_packets;
  1005. u32 timepassed_us;
  1006. u64 bytes_perint;
  1007. u8 itr_setting = ring_container->itr;
  1008. if (packets == 0)
  1009. return;
  1010. /* simple throttle rate management
  1011. * 0-20MB/s lowest (100000 ints/s)
  1012. * 20-100MB/s low (20000 ints/s)
  1013. * 100-1249MB/s bulk (12000 ints/s)
  1014. */
  1015. /* what was last interrupt timeslice? */
  1016. timepassed_us = q_vector->itr >> 2;
  1017. bytes_perint = bytes / timepassed_us; /* bytes/usec */
  1018. switch (itr_setting) {
  1019. case lowest_latency:
  1020. if (bytes_perint > 10)
  1021. itr_setting = low_latency;
  1022. break;
  1023. case low_latency:
  1024. if (bytes_perint > 20)
  1025. itr_setting = bulk_latency;
  1026. else if (bytes_perint <= 10)
  1027. itr_setting = lowest_latency;
  1028. break;
  1029. case bulk_latency:
  1030. if (bytes_perint <= 20)
  1031. itr_setting = low_latency;
  1032. break;
  1033. }
  1034. /* clear work counters since we have the values we need */
  1035. ring_container->total_bytes = 0;
  1036. ring_container->total_packets = 0;
  1037. /* write updated itr to ring container */
  1038. ring_container->itr = itr_setting;
  1039. }
  1040. static void ixgbevf_set_itr(struct ixgbevf_q_vector *q_vector)
  1041. {
  1042. u32 new_itr = q_vector->itr;
  1043. u8 current_itr;
  1044. ixgbevf_update_itr(q_vector, &q_vector->tx);
  1045. ixgbevf_update_itr(q_vector, &q_vector->rx);
  1046. current_itr = max(q_vector->rx.itr, q_vector->tx.itr);
  1047. switch (current_itr) {
  1048. /* counts and packets in update_itr are dependent on these numbers */
  1049. case lowest_latency:
  1050. new_itr = IXGBE_100K_ITR;
  1051. break;
  1052. case low_latency:
  1053. new_itr = IXGBE_20K_ITR;
  1054. break;
  1055. case bulk_latency:
  1056. new_itr = IXGBE_12K_ITR;
  1057. break;
  1058. default:
  1059. break;
  1060. }
  1061. if (new_itr != q_vector->itr) {
  1062. /* do an exponential smoothing */
  1063. new_itr = (10 * new_itr * q_vector->itr) /
  1064. ((9 * new_itr) + q_vector->itr);
  1065. /* save the algorithm value here */
  1066. q_vector->itr = new_itr;
  1067. ixgbevf_write_eitr(q_vector);
  1068. }
  1069. }
  1070. static irqreturn_t ixgbevf_msix_other(int irq, void *data)
  1071. {
  1072. struct ixgbevf_adapter *adapter = data;
  1073. struct ixgbe_hw *hw = &adapter->hw;
  1074. hw->mac.get_link_status = 1;
  1075. ixgbevf_service_event_schedule(adapter);
  1076. IXGBE_WRITE_REG(hw, IXGBE_VTEIMS, adapter->eims_other);
  1077. return IRQ_HANDLED;
  1078. }
  1079. /**
  1080. * ixgbevf_msix_clean_rings - single unshared vector rx clean (all queues)
  1081. * @irq: unused
  1082. * @data: pointer to our q_vector struct for this interrupt vector
  1083. **/
  1084. static irqreturn_t ixgbevf_msix_clean_rings(int irq, void *data)
  1085. {
  1086. struct ixgbevf_q_vector *q_vector = data;
  1087. /* EIAM disabled interrupts (on this vector) for us */
  1088. if (q_vector->rx.ring || q_vector->tx.ring)
  1089. napi_schedule_irqoff(&q_vector->napi);
  1090. return IRQ_HANDLED;
  1091. }
  1092. static inline void map_vector_to_rxq(struct ixgbevf_adapter *a, int v_idx,
  1093. int r_idx)
  1094. {
  1095. struct ixgbevf_q_vector *q_vector = a->q_vector[v_idx];
  1096. a->rx_ring[r_idx]->next = q_vector->rx.ring;
  1097. q_vector->rx.ring = a->rx_ring[r_idx];
  1098. q_vector->rx.count++;
  1099. }
  1100. static inline void map_vector_to_txq(struct ixgbevf_adapter *a, int v_idx,
  1101. int t_idx)
  1102. {
  1103. struct ixgbevf_q_vector *q_vector = a->q_vector[v_idx];
  1104. a->tx_ring[t_idx]->next = q_vector->tx.ring;
  1105. q_vector->tx.ring = a->tx_ring[t_idx];
  1106. q_vector->tx.count++;
  1107. }
  1108. /**
  1109. * ixgbevf_map_rings_to_vectors - Maps descriptor rings to vectors
  1110. * @adapter: board private structure to initialize
  1111. *
  1112. * This function maps descriptor rings to the queue-specific vectors
  1113. * we were allotted through the MSI-X enabling code. Ideally, we'd have
  1114. * one vector per ring/queue, but on a constrained vector budget, we
  1115. * group the rings as "efficiently" as possible. You would add new
  1116. * mapping configurations in here.
  1117. **/
  1118. static int ixgbevf_map_rings_to_vectors(struct ixgbevf_adapter *adapter)
  1119. {
  1120. int q_vectors;
  1121. int v_start = 0;
  1122. int rxr_idx = 0, txr_idx = 0;
  1123. int rxr_remaining = adapter->num_rx_queues;
  1124. int txr_remaining = adapter->num_tx_queues;
  1125. int i, j;
  1126. int rqpv, tqpv;
  1127. q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
  1128. /* The ideal configuration...
  1129. * We have enough vectors to map one per queue.
  1130. */
  1131. if (q_vectors == adapter->num_rx_queues + adapter->num_tx_queues) {
  1132. for (; rxr_idx < rxr_remaining; v_start++, rxr_idx++)
  1133. map_vector_to_rxq(adapter, v_start, rxr_idx);
  1134. for (; txr_idx < txr_remaining; v_start++, txr_idx++)
  1135. map_vector_to_txq(adapter, v_start, txr_idx);
  1136. return 0;
  1137. }
  1138. /* If we don't have enough vectors for a 1-to-1
  1139. * mapping, we'll have to group them so there are
  1140. * multiple queues per vector.
  1141. */
  1142. /* Re-adjusting *qpv takes care of the remainder. */
  1143. for (i = v_start; i < q_vectors; i++) {
  1144. rqpv = DIV_ROUND_UP(rxr_remaining, q_vectors - i);
  1145. for (j = 0; j < rqpv; j++) {
  1146. map_vector_to_rxq(adapter, i, rxr_idx);
  1147. rxr_idx++;
  1148. rxr_remaining--;
  1149. }
  1150. }
  1151. for (i = v_start; i < q_vectors; i++) {
  1152. tqpv = DIV_ROUND_UP(txr_remaining, q_vectors - i);
  1153. for (j = 0; j < tqpv; j++) {
  1154. map_vector_to_txq(adapter, i, txr_idx);
  1155. txr_idx++;
  1156. txr_remaining--;
  1157. }
  1158. }
  1159. return 0;
  1160. }
  1161. /**
  1162. * ixgbevf_request_msix_irqs - Initialize MSI-X interrupts
  1163. * @adapter: board private structure
  1164. *
  1165. * ixgbevf_request_msix_irqs allocates MSI-X vectors and requests
  1166. * interrupts from the kernel.
  1167. **/
  1168. static int ixgbevf_request_msix_irqs(struct ixgbevf_adapter *adapter)
  1169. {
  1170. struct net_device *netdev = adapter->netdev;
  1171. int q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
  1172. int vector, err;
  1173. int ri = 0, ti = 0;
  1174. for (vector = 0; vector < q_vectors; vector++) {
  1175. struct ixgbevf_q_vector *q_vector = adapter->q_vector[vector];
  1176. struct msix_entry *entry = &adapter->msix_entries[vector];
  1177. if (q_vector->tx.ring && q_vector->rx.ring) {
  1178. snprintf(q_vector->name, sizeof(q_vector->name) - 1,
  1179. "%s-%s-%d", netdev->name, "TxRx", ri++);
  1180. ti++;
  1181. } else if (q_vector->rx.ring) {
  1182. snprintf(q_vector->name, sizeof(q_vector->name) - 1,
  1183. "%s-%s-%d", netdev->name, "rx", ri++);
  1184. } else if (q_vector->tx.ring) {
  1185. snprintf(q_vector->name, sizeof(q_vector->name) - 1,
  1186. "%s-%s-%d", netdev->name, "tx", ti++);
  1187. } else {
  1188. /* skip this unused q_vector */
  1189. continue;
  1190. }
  1191. err = request_irq(entry->vector, &ixgbevf_msix_clean_rings, 0,
  1192. q_vector->name, q_vector);
  1193. if (err) {
  1194. hw_dbg(&adapter->hw,
  1195. "request_irq failed for MSIX interrupt Error: %d\n",
  1196. err);
  1197. goto free_queue_irqs;
  1198. }
  1199. }
  1200. err = request_irq(adapter->msix_entries[vector].vector,
  1201. &ixgbevf_msix_other, 0, netdev->name, adapter);
  1202. if (err) {
  1203. hw_dbg(&adapter->hw, "request_irq for msix_other failed: %d\n",
  1204. err);
  1205. goto free_queue_irqs;
  1206. }
  1207. return 0;
  1208. free_queue_irqs:
  1209. while (vector) {
  1210. vector--;
  1211. free_irq(adapter->msix_entries[vector].vector,
  1212. adapter->q_vector[vector]);
  1213. }
  1214. /* This failure is non-recoverable - it indicates the system is
  1215. * out of MSIX vector resources and the VF driver cannot run
  1216. * without them. Set the number of msix vectors to zero
  1217. * indicating that not enough can be allocated. The error
  1218. * will be returned to the user indicating device open failed.
  1219. * Any further attempts to force the driver to open will also
  1220. * fail. The only way to recover is to unload the driver and
  1221. * reload it again. If the system has recovered some MSIX
  1222. * vectors then it may succeed.
  1223. */
  1224. adapter->num_msix_vectors = 0;
  1225. return err;
  1226. }
  1227. static inline void ixgbevf_reset_q_vectors(struct ixgbevf_adapter *adapter)
  1228. {
  1229. int i, q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
  1230. for (i = 0; i < q_vectors; i++) {
  1231. struct ixgbevf_q_vector *q_vector = adapter->q_vector[i];
  1232. q_vector->rx.ring = NULL;
  1233. q_vector->tx.ring = NULL;
  1234. q_vector->rx.count = 0;
  1235. q_vector->tx.count = 0;
  1236. }
  1237. }
  1238. /**
  1239. * ixgbevf_request_irq - initialize interrupts
  1240. * @adapter: board private structure
  1241. *
  1242. * Attempts to configure interrupts using the best available
  1243. * capabilities of the hardware and kernel.
  1244. **/
  1245. static int ixgbevf_request_irq(struct ixgbevf_adapter *adapter)
  1246. {
  1247. int err = ixgbevf_request_msix_irqs(adapter);
  1248. if (err)
  1249. hw_dbg(&adapter->hw, "request_irq failed, Error %d\n", err);
  1250. return err;
  1251. }
  1252. static void ixgbevf_free_irq(struct ixgbevf_adapter *adapter)
  1253. {
  1254. int i, q_vectors;
  1255. q_vectors = adapter->num_msix_vectors;
  1256. i = q_vectors - 1;
  1257. free_irq(adapter->msix_entries[i].vector, adapter);
  1258. i--;
  1259. for (; i >= 0; i--) {
  1260. /* free only the irqs that were actually requested */
  1261. if (!adapter->q_vector[i]->rx.ring &&
  1262. !adapter->q_vector[i]->tx.ring)
  1263. continue;
  1264. free_irq(adapter->msix_entries[i].vector,
  1265. adapter->q_vector[i]);
  1266. }
  1267. ixgbevf_reset_q_vectors(adapter);
  1268. }
  1269. /**
  1270. * ixgbevf_irq_disable - Mask off interrupt generation on the NIC
  1271. * @adapter: board private structure
  1272. **/
  1273. static inline void ixgbevf_irq_disable(struct ixgbevf_adapter *adapter)
  1274. {
  1275. struct ixgbe_hw *hw = &adapter->hw;
  1276. int i;
  1277. IXGBE_WRITE_REG(hw, IXGBE_VTEIAM, 0);
  1278. IXGBE_WRITE_REG(hw, IXGBE_VTEIMC, ~0);
  1279. IXGBE_WRITE_REG(hw, IXGBE_VTEIAC, 0);
  1280. IXGBE_WRITE_FLUSH(hw);
  1281. for (i = 0; i < adapter->num_msix_vectors; i++)
  1282. synchronize_irq(adapter->msix_entries[i].vector);
  1283. }
  1284. /**
  1285. * ixgbevf_irq_enable - Enable default interrupt generation settings
  1286. * @adapter: board private structure
  1287. **/
  1288. static inline void ixgbevf_irq_enable(struct ixgbevf_adapter *adapter)
  1289. {
  1290. struct ixgbe_hw *hw = &adapter->hw;
  1291. IXGBE_WRITE_REG(hw, IXGBE_VTEIAM, adapter->eims_enable_mask);
  1292. IXGBE_WRITE_REG(hw, IXGBE_VTEIAC, adapter->eims_enable_mask);
  1293. IXGBE_WRITE_REG(hw, IXGBE_VTEIMS, adapter->eims_enable_mask);
  1294. }
  1295. /**
  1296. * ixgbevf_configure_tx_ring - Configure 82599 VF Tx ring after Reset
  1297. * @adapter: board private structure
  1298. * @ring: structure containing ring specific data
  1299. *
  1300. * Configure the Tx descriptor ring after a reset.
  1301. **/
  1302. static void ixgbevf_configure_tx_ring(struct ixgbevf_adapter *adapter,
  1303. struct ixgbevf_ring *ring)
  1304. {
  1305. struct ixgbe_hw *hw = &adapter->hw;
  1306. u64 tdba = ring->dma;
  1307. int wait_loop = 10;
  1308. u32 txdctl = IXGBE_TXDCTL_ENABLE;
  1309. u8 reg_idx = ring->reg_idx;
  1310. /* disable queue to avoid issues while updating state */
  1311. IXGBE_WRITE_REG(hw, IXGBE_VFTXDCTL(reg_idx), IXGBE_TXDCTL_SWFLSH);
  1312. IXGBE_WRITE_FLUSH(hw);
  1313. IXGBE_WRITE_REG(hw, IXGBE_VFTDBAL(reg_idx), tdba & DMA_BIT_MASK(32));
  1314. IXGBE_WRITE_REG(hw, IXGBE_VFTDBAH(reg_idx), tdba >> 32);
  1315. IXGBE_WRITE_REG(hw, IXGBE_VFTDLEN(reg_idx),
  1316. ring->count * sizeof(union ixgbe_adv_tx_desc));
  1317. /* disable head writeback */
  1318. IXGBE_WRITE_REG(hw, IXGBE_VFTDWBAH(reg_idx), 0);
  1319. IXGBE_WRITE_REG(hw, IXGBE_VFTDWBAL(reg_idx), 0);
  1320. /* enable relaxed ordering */
  1321. IXGBE_WRITE_REG(hw, IXGBE_VFDCA_TXCTRL(reg_idx),
  1322. (IXGBE_DCA_TXCTRL_DESC_RRO_EN |
  1323. IXGBE_DCA_TXCTRL_DATA_RRO_EN));
  1324. /* reset head and tail pointers */
  1325. IXGBE_WRITE_REG(hw, IXGBE_VFTDH(reg_idx), 0);
  1326. IXGBE_WRITE_REG(hw, IXGBE_VFTDT(reg_idx), 0);
  1327. ring->tail = adapter->io_addr + IXGBE_VFTDT(reg_idx);
  1328. /* reset ntu and ntc to place SW in sync with hardwdare */
  1329. ring->next_to_clean = 0;
  1330. ring->next_to_use = 0;
  1331. /* In order to avoid issues WTHRESH + PTHRESH should always be equal
  1332. * to or less than the number of on chip descriptors, which is
  1333. * currently 40.
  1334. */
  1335. txdctl |= (8 << 16); /* WTHRESH = 8 */
  1336. /* Setting PTHRESH to 32 both improves performance */
  1337. txdctl |= (1u << 8) | /* HTHRESH = 1 */
  1338. 32; /* PTHRESH = 32 */
  1339. clear_bit(__IXGBEVF_HANG_CHECK_ARMED, &ring->state);
  1340. IXGBE_WRITE_REG(hw, IXGBE_VFTXDCTL(reg_idx), txdctl);
  1341. /* poll to verify queue is enabled */
  1342. do {
  1343. usleep_range(1000, 2000);
  1344. txdctl = IXGBE_READ_REG(hw, IXGBE_VFTXDCTL(reg_idx));
  1345. } while (--wait_loop && !(txdctl & IXGBE_TXDCTL_ENABLE));
  1346. if (!wait_loop)
  1347. hw_dbg(hw, "Could not enable Tx Queue %d\n", reg_idx);
  1348. }
  1349. /**
  1350. * ixgbevf_configure_tx - Configure 82599 VF Transmit Unit after Reset
  1351. * @adapter: board private structure
  1352. *
  1353. * Configure the Tx unit of the MAC after a reset.
  1354. **/
  1355. static void ixgbevf_configure_tx(struct ixgbevf_adapter *adapter)
  1356. {
  1357. u32 i;
  1358. /* Setup the HW Tx Head and Tail descriptor pointers */
  1359. for (i = 0; i < adapter->num_tx_queues; i++)
  1360. ixgbevf_configure_tx_ring(adapter, adapter->tx_ring[i]);
  1361. }
  1362. #define IXGBE_SRRCTL_BSIZEHDRSIZE_SHIFT 2
  1363. static void ixgbevf_configure_srrctl(struct ixgbevf_adapter *adapter, int index)
  1364. {
  1365. struct ixgbe_hw *hw = &adapter->hw;
  1366. u32 srrctl;
  1367. srrctl = IXGBE_SRRCTL_DROP_EN;
  1368. srrctl |= IXGBEVF_RX_HDR_SIZE << IXGBE_SRRCTL_BSIZEHDRSIZE_SHIFT;
  1369. srrctl |= IXGBEVF_RX_BUFSZ >> IXGBE_SRRCTL_BSIZEPKT_SHIFT;
  1370. srrctl |= IXGBE_SRRCTL_DESCTYPE_ADV_ONEBUF;
  1371. IXGBE_WRITE_REG(hw, IXGBE_VFSRRCTL(index), srrctl);
  1372. }
  1373. static void ixgbevf_setup_psrtype(struct ixgbevf_adapter *adapter)
  1374. {
  1375. struct ixgbe_hw *hw = &adapter->hw;
  1376. /* PSRTYPE must be initialized in 82599 */
  1377. u32 psrtype = IXGBE_PSRTYPE_TCPHDR | IXGBE_PSRTYPE_UDPHDR |
  1378. IXGBE_PSRTYPE_IPV4HDR | IXGBE_PSRTYPE_IPV6HDR |
  1379. IXGBE_PSRTYPE_L2HDR;
  1380. if (adapter->num_rx_queues > 1)
  1381. psrtype |= BIT(29);
  1382. IXGBE_WRITE_REG(hw, IXGBE_VFPSRTYPE, psrtype);
  1383. }
  1384. #define IXGBEVF_MAX_RX_DESC_POLL 10
  1385. static void ixgbevf_disable_rx_queue(struct ixgbevf_adapter *adapter,
  1386. struct ixgbevf_ring *ring)
  1387. {
  1388. struct ixgbe_hw *hw = &adapter->hw;
  1389. int wait_loop = IXGBEVF_MAX_RX_DESC_POLL;
  1390. u32 rxdctl;
  1391. u8 reg_idx = ring->reg_idx;
  1392. if (IXGBE_REMOVED(hw->hw_addr))
  1393. return;
  1394. rxdctl = IXGBE_READ_REG(hw, IXGBE_VFRXDCTL(reg_idx));
  1395. rxdctl &= ~IXGBE_RXDCTL_ENABLE;
  1396. /* write value back with RXDCTL.ENABLE bit cleared */
  1397. IXGBE_WRITE_REG(hw, IXGBE_VFRXDCTL(reg_idx), rxdctl);
  1398. /* the hardware may take up to 100us to really disable the Rx queue */
  1399. do {
  1400. udelay(10);
  1401. rxdctl = IXGBE_READ_REG(hw, IXGBE_VFRXDCTL(reg_idx));
  1402. } while (--wait_loop && (rxdctl & IXGBE_RXDCTL_ENABLE));
  1403. if (!wait_loop)
  1404. pr_err("RXDCTL.ENABLE queue %d not cleared while polling\n",
  1405. reg_idx);
  1406. }
  1407. static void ixgbevf_rx_desc_queue_enable(struct ixgbevf_adapter *adapter,
  1408. struct ixgbevf_ring *ring)
  1409. {
  1410. struct ixgbe_hw *hw = &adapter->hw;
  1411. int wait_loop = IXGBEVF_MAX_RX_DESC_POLL;
  1412. u32 rxdctl;
  1413. u8 reg_idx = ring->reg_idx;
  1414. if (IXGBE_REMOVED(hw->hw_addr))
  1415. return;
  1416. do {
  1417. usleep_range(1000, 2000);
  1418. rxdctl = IXGBE_READ_REG(hw, IXGBE_VFRXDCTL(reg_idx));
  1419. } while (--wait_loop && !(rxdctl & IXGBE_RXDCTL_ENABLE));
  1420. if (!wait_loop)
  1421. pr_err("RXDCTL.ENABLE queue %d not set while polling\n",
  1422. reg_idx);
  1423. }
  1424. static void ixgbevf_setup_vfmrqc(struct ixgbevf_adapter *adapter)
  1425. {
  1426. struct ixgbe_hw *hw = &adapter->hw;
  1427. u32 vfmrqc = 0, vfreta = 0;
  1428. u16 rss_i = adapter->num_rx_queues;
  1429. u8 i, j;
  1430. /* Fill out hash function seeds */
  1431. netdev_rss_key_fill(adapter->rss_key, sizeof(adapter->rss_key));
  1432. for (i = 0; i < IXGBEVF_VFRSSRK_REGS; i++)
  1433. IXGBE_WRITE_REG(hw, IXGBE_VFRSSRK(i), adapter->rss_key[i]);
  1434. for (i = 0, j = 0; i < IXGBEVF_X550_VFRETA_SIZE; i++, j++) {
  1435. if (j == rss_i)
  1436. j = 0;
  1437. adapter->rss_indir_tbl[i] = j;
  1438. vfreta |= j << (i & 0x3) * 8;
  1439. if ((i & 3) == 3) {
  1440. IXGBE_WRITE_REG(hw, IXGBE_VFRETA(i >> 2), vfreta);
  1441. vfreta = 0;
  1442. }
  1443. }
  1444. /* Perform hash on these packet types */
  1445. vfmrqc |= IXGBE_VFMRQC_RSS_FIELD_IPV4 |
  1446. IXGBE_VFMRQC_RSS_FIELD_IPV4_TCP |
  1447. IXGBE_VFMRQC_RSS_FIELD_IPV6 |
  1448. IXGBE_VFMRQC_RSS_FIELD_IPV6_TCP;
  1449. vfmrqc |= IXGBE_VFMRQC_RSSEN;
  1450. IXGBE_WRITE_REG(hw, IXGBE_VFMRQC, vfmrqc);
  1451. }
  1452. static void ixgbevf_configure_rx_ring(struct ixgbevf_adapter *adapter,
  1453. struct ixgbevf_ring *ring)
  1454. {
  1455. struct ixgbe_hw *hw = &adapter->hw;
  1456. u64 rdba = ring->dma;
  1457. u32 rxdctl;
  1458. u8 reg_idx = ring->reg_idx;
  1459. /* disable queue to avoid issues while updating state */
  1460. rxdctl = IXGBE_READ_REG(hw, IXGBE_VFRXDCTL(reg_idx));
  1461. ixgbevf_disable_rx_queue(adapter, ring);
  1462. IXGBE_WRITE_REG(hw, IXGBE_VFRDBAL(reg_idx), rdba & DMA_BIT_MASK(32));
  1463. IXGBE_WRITE_REG(hw, IXGBE_VFRDBAH(reg_idx), rdba >> 32);
  1464. IXGBE_WRITE_REG(hw, IXGBE_VFRDLEN(reg_idx),
  1465. ring->count * sizeof(union ixgbe_adv_rx_desc));
  1466. #ifndef CONFIG_SPARC
  1467. /* enable relaxed ordering */
  1468. IXGBE_WRITE_REG(hw, IXGBE_VFDCA_RXCTRL(reg_idx),
  1469. IXGBE_DCA_RXCTRL_DESC_RRO_EN);
  1470. #else
  1471. IXGBE_WRITE_REG(hw, IXGBE_VFDCA_RXCTRL(reg_idx),
  1472. IXGBE_DCA_RXCTRL_DESC_RRO_EN |
  1473. IXGBE_DCA_RXCTRL_DATA_WRO_EN);
  1474. #endif
  1475. /* reset head and tail pointers */
  1476. IXGBE_WRITE_REG(hw, IXGBE_VFRDH(reg_idx), 0);
  1477. IXGBE_WRITE_REG(hw, IXGBE_VFRDT(reg_idx), 0);
  1478. ring->tail = adapter->io_addr + IXGBE_VFRDT(reg_idx);
  1479. /* reset ntu and ntc to place SW in sync with hardwdare */
  1480. ring->next_to_clean = 0;
  1481. ring->next_to_use = 0;
  1482. ring->next_to_alloc = 0;
  1483. ixgbevf_configure_srrctl(adapter, reg_idx);
  1484. /* allow any size packet since we can handle overflow */
  1485. rxdctl &= ~IXGBE_RXDCTL_RLPML_EN;
  1486. rxdctl |= IXGBE_RXDCTL_ENABLE | IXGBE_RXDCTL_VME;
  1487. IXGBE_WRITE_REG(hw, IXGBE_VFRXDCTL(reg_idx), rxdctl);
  1488. ixgbevf_rx_desc_queue_enable(adapter, ring);
  1489. ixgbevf_alloc_rx_buffers(ring, ixgbevf_desc_unused(ring));
  1490. }
  1491. /**
  1492. * ixgbevf_configure_rx - Configure 82599 VF Receive Unit after Reset
  1493. * @adapter: board private structure
  1494. *
  1495. * Configure the Rx unit of the MAC after a reset.
  1496. **/
  1497. static void ixgbevf_configure_rx(struct ixgbevf_adapter *adapter)
  1498. {
  1499. struct ixgbe_hw *hw = &adapter->hw;
  1500. struct net_device *netdev = adapter->netdev;
  1501. int i, ret;
  1502. ixgbevf_setup_psrtype(adapter);
  1503. if (hw->mac.type >= ixgbe_mac_X550_vf)
  1504. ixgbevf_setup_vfmrqc(adapter);
  1505. spin_lock_bh(&adapter->mbx_lock);
  1506. /* notify the PF of our intent to use this size of frame */
  1507. ret = hw->mac.ops.set_rlpml(hw, netdev->mtu + ETH_HLEN + ETH_FCS_LEN);
  1508. spin_unlock_bh(&adapter->mbx_lock);
  1509. if (ret)
  1510. dev_err(&adapter->pdev->dev,
  1511. "Failed to set MTU at %d\n", netdev->mtu);
  1512. /* Setup the HW Rx Head and Tail Descriptor Pointers and
  1513. * the Base and Length of the Rx Descriptor Ring
  1514. */
  1515. for (i = 0; i < adapter->num_rx_queues; i++)
  1516. ixgbevf_configure_rx_ring(adapter, adapter->rx_ring[i]);
  1517. }
  1518. static int ixgbevf_vlan_rx_add_vid(struct net_device *netdev,
  1519. __be16 proto, u16 vid)
  1520. {
  1521. struct ixgbevf_adapter *adapter = netdev_priv(netdev);
  1522. struct ixgbe_hw *hw = &adapter->hw;
  1523. int err;
  1524. spin_lock_bh(&adapter->mbx_lock);
  1525. /* add VID to filter table */
  1526. err = hw->mac.ops.set_vfta(hw, vid, 0, true);
  1527. spin_unlock_bh(&adapter->mbx_lock);
  1528. /* translate error return types so error makes sense */
  1529. if (err == IXGBE_ERR_MBX)
  1530. return -EIO;
  1531. if (err == IXGBE_ERR_INVALID_ARGUMENT)
  1532. return -EACCES;
  1533. set_bit(vid, adapter->active_vlans);
  1534. return err;
  1535. }
  1536. static int ixgbevf_vlan_rx_kill_vid(struct net_device *netdev,
  1537. __be16 proto, u16 vid)
  1538. {
  1539. struct ixgbevf_adapter *adapter = netdev_priv(netdev);
  1540. struct ixgbe_hw *hw = &adapter->hw;
  1541. int err;
  1542. spin_lock_bh(&adapter->mbx_lock);
  1543. /* remove VID from filter table */
  1544. err = hw->mac.ops.set_vfta(hw, vid, 0, false);
  1545. spin_unlock_bh(&adapter->mbx_lock);
  1546. clear_bit(vid, adapter->active_vlans);
  1547. return err;
  1548. }
  1549. static void ixgbevf_restore_vlan(struct ixgbevf_adapter *adapter)
  1550. {
  1551. u16 vid;
  1552. for_each_set_bit(vid, adapter->active_vlans, VLAN_N_VID)
  1553. ixgbevf_vlan_rx_add_vid(adapter->netdev,
  1554. htons(ETH_P_8021Q), vid);
  1555. }
  1556. static int ixgbevf_write_uc_addr_list(struct net_device *netdev)
  1557. {
  1558. struct ixgbevf_adapter *adapter = netdev_priv(netdev);
  1559. struct ixgbe_hw *hw = &adapter->hw;
  1560. int count = 0;
  1561. if ((netdev_uc_count(netdev)) > 10) {
  1562. pr_err("Too many unicast filters - No Space\n");
  1563. return -ENOSPC;
  1564. }
  1565. if (!netdev_uc_empty(netdev)) {
  1566. struct netdev_hw_addr *ha;
  1567. netdev_for_each_uc_addr(ha, netdev) {
  1568. hw->mac.ops.set_uc_addr(hw, ++count, ha->addr);
  1569. udelay(200);
  1570. }
  1571. } else {
  1572. /* If the list is empty then send message to PF driver to
  1573. * clear all MAC VLANs on this VF.
  1574. */
  1575. hw->mac.ops.set_uc_addr(hw, 0, NULL);
  1576. }
  1577. return count;
  1578. }
  1579. /**
  1580. * ixgbevf_set_rx_mode - Multicast and unicast set
  1581. * @netdev: network interface device structure
  1582. *
  1583. * The set_rx_method entry point is called whenever the multicast address
  1584. * list, unicast address list or the network interface flags are updated.
  1585. * This routine is responsible for configuring the hardware for proper
  1586. * multicast mode and configuring requested unicast filters.
  1587. **/
  1588. static void ixgbevf_set_rx_mode(struct net_device *netdev)
  1589. {
  1590. struct ixgbevf_adapter *adapter = netdev_priv(netdev);
  1591. struct ixgbe_hw *hw = &adapter->hw;
  1592. unsigned int flags = netdev->flags;
  1593. int xcast_mode;
  1594. xcast_mode = (flags & IFF_ALLMULTI) ? IXGBEVF_XCAST_MODE_ALLMULTI :
  1595. (flags & (IFF_BROADCAST | IFF_MULTICAST)) ?
  1596. IXGBEVF_XCAST_MODE_MULTI : IXGBEVF_XCAST_MODE_NONE;
  1597. spin_lock_bh(&adapter->mbx_lock);
  1598. hw->mac.ops.update_xcast_mode(hw, xcast_mode);
  1599. /* reprogram multicast list */
  1600. hw->mac.ops.update_mc_addr_list(hw, netdev);
  1601. ixgbevf_write_uc_addr_list(netdev);
  1602. spin_unlock_bh(&adapter->mbx_lock);
  1603. }
  1604. static void ixgbevf_napi_enable_all(struct ixgbevf_adapter *adapter)
  1605. {
  1606. int q_idx;
  1607. struct ixgbevf_q_vector *q_vector;
  1608. int q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
  1609. for (q_idx = 0; q_idx < q_vectors; q_idx++) {
  1610. q_vector = adapter->q_vector[q_idx];
  1611. #ifdef CONFIG_NET_RX_BUSY_POLL
  1612. ixgbevf_qv_init_lock(adapter->q_vector[q_idx]);
  1613. #endif
  1614. napi_enable(&q_vector->napi);
  1615. }
  1616. }
  1617. static void ixgbevf_napi_disable_all(struct ixgbevf_adapter *adapter)
  1618. {
  1619. int q_idx;
  1620. struct ixgbevf_q_vector *q_vector;
  1621. int q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
  1622. for (q_idx = 0; q_idx < q_vectors; q_idx++) {
  1623. q_vector = adapter->q_vector[q_idx];
  1624. napi_disable(&q_vector->napi);
  1625. #ifdef CONFIG_NET_RX_BUSY_POLL
  1626. while (!ixgbevf_qv_disable(adapter->q_vector[q_idx])) {
  1627. pr_info("QV %d locked\n", q_idx);
  1628. usleep_range(1000, 20000);
  1629. }
  1630. #endif /* CONFIG_NET_RX_BUSY_POLL */
  1631. }
  1632. }
  1633. static int ixgbevf_configure_dcb(struct ixgbevf_adapter *adapter)
  1634. {
  1635. struct ixgbe_hw *hw = &adapter->hw;
  1636. unsigned int def_q = 0;
  1637. unsigned int num_tcs = 0;
  1638. unsigned int num_rx_queues = adapter->num_rx_queues;
  1639. unsigned int num_tx_queues = adapter->num_tx_queues;
  1640. int err;
  1641. spin_lock_bh(&adapter->mbx_lock);
  1642. /* fetch queue configuration from the PF */
  1643. err = ixgbevf_get_queues(hw, &num_tcs, &def_q);
  1644. spin_unlock_bh(&adapter->mbx_lock);
  1645. if (err)
  1646. return err;
  1647. if (num_tcs > 1) {
  1648. /* we need only one Tx queue */
  1649. num_tx_queues = 1;
  1650. /* update default Tx ring register index */
  1651. adapter->tx_ring[0]->reg_idx = def_q;
  1652. /* we need as many queues as traffic classes */
  1653. num_rx_queues = num_tcs;
  1654. }
  1655. /* if we have a bad config abort request queue reset */
  1656. if ((adapter->num_rx_queues != num_rx_queues) ||
  1657. (adapter->num_tx_queues != num_tx_queues)) {
  1658. /* force mailbox timeout to prevent further messages */
  1659. hw->mbx.timeout = 0;
  1660. /* wait for watchdog to come around and bail us out */
  1661. set_bit(__IXGBEVF_QUEUE_RESET_REQUESTED, &adapter->state);
  1662. }
  1663. return 0;
  1664. }
  1665. static void ixgbevf_configure(struct ixgbevf_adapter *adapter)
  1666. {
  1667. ixgbevf_configure_dcb(adapter);
  1668. ixgbevf_set_rx_mode(adapter->netdev);
  1669. ixgbevf_restore_vlan(adapter);
  1670. ixgbevf_configure_tx(adapter);
  1671. ixgbevf_configure_rx(adapter);
  1672. }
  1673. static void ixgbevf_save_reset_stats(struct ixgbevf_adapter *adapter)
  1674. {
  1675. /* Only save pre-reset stats if there are some */
  1676. if (adapter->stats.vfgprc || adapter->stats.vfgptc) {
  1677. adapter->stats.saved_reset_vfgprc += adapter->stats.vfgprc -
  1678. adapter->stats.base_vfgprc;
  1679. adapter->stats.saved_reset_vfgptc += adapter->stats.vfgptc -
  1680. adapter->stats.base_vfgptc;
  1681. adapter->stats.saved_reset_vfgorc += adapter->stats.vfgorc -
  1682. adapter->stats.base_vfgorc;
  1683. adapter->stats.saved_reset_vfgotc += adapter->stats.vfgotc -
  1684. adapter->stats.base_vfgotc;
  1685. adapter->stats.saved_reset_vfmprc += adapter->stats.vfmprc -
  1686. adapter->stats.base_vfmprc;
  1687. }
  1688. }
  1689. static void ixgbevf_init_last_counter_stats(struct ixgbevf_adapter *adapter)
  1690. {
  1691. struct ixgbe_hw *hw = &adapter->hw;
  1692. adapter->stats.last_vfgprc = IXGBE_READ_REG(hw, IXGBE_VFGPRC);
  1693. adapter->stats.last_vfgorc = IXGBE_READ_REG(hw, IXGBE_VFGORC_LSB);
  1694. adapter->stats.last_vfgorc |=
  1695. (((u64)(IXGBE_READ_REG(hw, IXGBE_VFGORC_MSB))) << 32);
  1696. adapter->stats.last_vfgptc = IXGBE_READ_REG(hw, IXGBE_VFGPTC);
  1697. adapter->stats.last_vfgotc = IXGBE_READ_REG(hw, IXGBE_VFGOTC_LSB);
  1698. adapter->stats.last_vfgotc |=
  1699. (((u64)(IXGBE_READ_REG(hw, IXGBE_VFGOTC_MSB))) << 32);
  1700. adapter->stats.last_vfmprc = IXGBE_READ_REG(hw, IXGBE_VFMPRC);
  1701. adapter->stats.base_vfgprc = adapter->stats.last_vfgprc;
  1702. adapter->stats.base_vfgorc = adapter->stats.last_vfgorc;
  1703. adapter->stats.base_vfgptc = adapter->stats.last_vfgptc;
  1704. adapter->stats.base_vfgotc = adapter->stats.last_vfgotc;
  1705. adapter->stats.base_vfmprc = adapter->stats.last_vfmprc;
  1706. }
  1707. static void ixgbevf_negotiate_api(struct ixgbevf_adapter *adapter)
  1708. {
  1709. struct ixgbe_hw *hw = &adapter->hw;
  1710. int api[] = { ixgbe_mbox_api_12,
  1711. ixgbe_mbox_api_11,
  1712. ixgbe_mbox_api_10,
  1713. ixgbe_mbox_api_unknown };
  1714. int err, idx = 0;
  1715. spin_lock_bh(&adapter->mbx_lock);
  1716. while (api[idx] != ixgbe_mbox_api_unknown) {
  1717. err = hw->mac.ops.negotiate_api_version(hw, api[idx]);
  1718. if (!err)
  1719. break;
  1720. idx++;
  1721. }
  1722. spin_unlock_bh(&adapter->mbx_lock);
  1723. }
  1724. static void ixgbevf_up_complete(struct ixgbevf_adapter *adapter)
  1725. {
  1726. struct net_device *netdev = adapter->netdev;
  1727. struct ixgbe_hw *hw = &adapter->hw;
  1728. ixgbevf_configure_msix(adapter);
  1729. spin_lock_bh(&adapter->mbx_lock);
  1730. if (is_valid_ether_addr(hw->mac.addr))
  1731. hw->mac.ops.set_rar(hw, 0, hw->mac.addr, 0);
  1732. else
  1733. hw->mac.ops.set_rar(hw, 0, hw->mac.perm_addr, 0);
  1734. spin_unlock_bh(&adapter->mbx_lock);
  1735. smp_mb__before_atomic();
  1736. clear_bit(__IXGBEVF_DOWN, &adapter->state);
  1737. ixgbevf_napi_enable_all(adapter);
  1738. /* clear any pending interrupts, may auto mask */
  1739. IXGBE_READ_REG(hw, IXGBE_VTEICR);
  1740. ixgbevf_irq_enable(adapter);
  1741. /* enable transmits */
  1742. netif_tx_start_all_queues(netdev);
  1743. ixgbevf_save_reset_stats(adapter);
  1744. ixgbevf_init_last_counter_stats(adapter);
  1745. hw->mac.get_link_status = 1;
  1746. mod_timer(&adapter->service_timer, jiffies);
  1747. }
  1748. void ixgbevf_up(struct ixgbevf_adapter *adapter)
  1749. {
  1750. ixgbevf_configure(adapter);
  1751. ixgbevf_up_complete(adapter);
  1752. }
  1753. /**
  1754. * ixgbevf_clean_rx_ring - Free Rx Buffers per Queue
  1755. * @rx_ring: ring to free buffers from
  1756. **/
  1757. static void ixgbevf_clean_rx_ring(struct ixgbevf_ring *rx_ring)
  1758. {
  1759. struct device *dev = rx_ring->dev;
  1760. unsigned long size;
  1761. unsigned int i;
  1762. /* Free Rx ring sk_buff */
  1763. if (rx_ring->skb) {
  1764. dev_kfree_skb(rx_ring->skb);
  1765. rx_ring->skb = NULL;
  1766. }
  1767. /* ring already cleared, nothing to do */
  1768. if (!rx_ring->rx_buffer_info)
  1769. return;
  1770. /* Free all the Rx ring pages */
  1771. for (i = 0; i < rx_ring->count; i++) {
  1772. struct ixgbevf_rx_buffer *rx_buffer;
  1773. rx_buffer = &rx_ring->rx_buffer_info[i];
  1774. if (rx_buffer->dma)
  1775. dma_unmap_page(dev, rx_buffer->dma,
  1776. PAGE_SIZE, DMA_FROM_DEVICE);
  1777. rx_buffer->dma = 0;
  1778. if (rx_buffer->page)
  1779. __free_page(rx_buffer->page);
  1780. rx_buffer->page = NULL;
  1781. }
  1782. size = sizeof(struct ixgbevf_rx_buffer) * rx_ring->count;
  1783. memset(rx_ring->rx_buffer_info, 0, size);
  1784. /* Zero out the descriptor ring */
  1785. memset(rx_ring->desc, 0, rx_ring->size);
  1786. }
  1787. /**
  1788. * ixgbevf_clean_tx_ring - Free Tx Buffers
  1789. * @tx_ring: ring to be cleaned
  1790. **/
  1791. static void ixgbevf_clean_tx_ring(struct ixgbevf_ring *tx_ring)
  1792. {
  1793. struct ixgbevf_tx_buffer *tx_buffer_info;
  1794. unsigned long size;
  1795. unsigned int i;
  1796. if (!tx_ring->tx_buffer_info)
  1797. return;
  1798. /* Free all the Tx ring sk_buffs */
  1799. for (i = 0; i < tx_ring->count; i++) {
  1800. tx_buffer_info = &tx_ring->tx_buffer_info[i];
  1801. ixgbevf_unmap_and_free_tx_resource(tx_ring, tx_buffer_info);
  1802. }
  1803. size = sizeof(struct ixgbevf_tx_buffer) * tx_ring->count;
  1804. memset(tx_ring->tx_buffer_info, 0, size);
  1805. memset(tx_ring->desc, 0, tx_ring->size);
  1806. }
  1807. /**
  1808. * ixgbevf_clean_all_rx_rings - Free Rx Buffers for all queues
  1809. * @adapter: board private structure
  1810. **/
  1811. static void ixgbevf_clean_all_rx_rings(struct ixgbevf_adapter *adapter)
  1812. {
  1813. int i;
  1814. for (i = 0; i < adapter->num_rx_queues; i++)
  1815. ixgbevf_clean_rx_ring(adapter->rx_ring[i]);
  1816. }
  1817. /**
  1818. * ixgbevf_clean_all_tx_rings - Free Tx Buffers for all queues
  1819. * @adapter: board private structure
  1820. **/
  1821. static void ixgbevf_clean_all_tx_rings(struct ixgbevf_adapter *adapter)
  1822. {
  1823. int i;
  1824. for (i = 0; i < adapter->num_tx_queues; i++)
  1825. ixgbevf_clean_tx_ring(adapter->tx_ring[i]);
  1826. }
  1827. void ixgbevf_down(struct ixgbevf_adapter *adapter)
  1828. {
  1829. struct net_device *netdev = adapter->netdev;
  1830. struct ixgbe_hw *hw = &adapter->hw;
  1831. int i;
  1832. /* signal that we are down to the interrupt handler */
  1833. if (test_and_set_bit(__IXGBEVF_DOWN, &adapter->state))
  1834. return; /* do nothing if already down */
  1835. /* disable all enabled Rx queues */
  1836. for (i = 0; i < adapter->num_rx_queues; i++)
  1837. ixgbevf_disable_rx_queue(adapter, adapter->rx_ring[i]);
  1838. usleep_range(10000, 20000);
  1839. netif_tx_stop_all_queues(netdev);
  1840. /* call carrier off first to avoid false dev_watchdog timeouts */
  1841. netif_carrier_off(netdev);
  1842. netif_tx_disable(netdev);
  1843. ixgbevf_irq_disable(adapter);
  1844. ixgbevf_napi_disable_all(adapter);
  1845. del_timer_sync(&adapter->service_timer);
  1846. /* disable transmits in the hardware now that interrupts are off */
  1847. for (i = 0; i < adapter->num_tx_queues; i++) {
  1848. u8 reg_idx = adapter->tx_ring[i]->reg_idx;
  1849. IXGBE_WRITE_REG(hw, IXGBE_VFTXDCTL(reg_idx),
  1850. IXGBE_TXDCTL_SWFLSH);
  1851. }
  1852. if (!pci_channel_offline(adapter->pdev))
  1853. ixgbevf_reset(adapter);
  1854. ixgbevf_clean_all_tx_rings(adapter);
  1855. ixgbevf_clean_all_rx_rings(adapter);
  1856. }
  1857. void ixgbevf_reinit_locked(struct ixgbevf_adapter *adapter)
  1858. {
  1859. WARN_ON(in_interrupt());
  1860. while (test_and_set_bit(__IXGBEVF_RESETTING, &adapter->state))
  1861. msleep(1);
  1862. ixgbevf_down(adapter);
  1863. ixgbevf_up(adapter);
  1864. clear_bit(__IXGBEVF_RESETTING, &adapter->state);
  1865. }
  1866. void ixgbevf_reset(struct ixgbevf_adapter *adapter)
  1867. {
  1868. struct ixgbe_hw *hw = &adapter->hw;
  1869. struct net_device *netdev = adapter->netdev;
  1870. if (hw->mac.ops.reset_hw(hw)) {
  1871. hw_dbg(hw, "PF still resetting\n");
  1872. } else {
  1873. hw->mac.ops.init_hw(hw);
  1874. ixgbevf_negotiate_api(adapter);
  1875. }
  1876. if (is_valid_ether_addr(adapter->hw.mac.addr)) {
  1877. ether_addr_copy(netdev->dev_addr, adapter->hw.mac.addr);
  1878. ether_addr_copy(netdev->perm_addr, adapter->hw.mac.addr);
  1879. }
  1880. adapter->last_reset = jiffies;
  1881. }
  1882. static int ixgbevf_acquire_msix_vectors(struct ixgbevf_adapter *adapter,
  1883. int vectors)
  1884. {
  1885. int vector_threshold;
  1886. /* We'll want at least 2 (vector_threshold):
  1887. * 1) TxQ[0] + RxQ[0] handler
  1888. * 2) Other (Link Status Change, etc.)
  1889. */
  1890. vector_threshold = MIN_MSIX_COUNT;
  1891. /* The more we get, the more we will assign to Tx/Rx Cleanup
  1892. * for the separate queues...where Rx Cleanup >= Tx Cleanup.
  1893. * Right now, we simply care about how many we'll get; we'll
  1894. * set them up later while requesting irq's.
  1895. */
  1896. vectors = pci_enable_msix_range(adapter->pdev, adapter->msix_entries,
  1897. vector_threshold, vectors);
  1898. if (vectors < 0) {
  1899. dev_err(&adapter->pdev->dev,
  1900. "Unable to allocate MSI-X interrupts\n");
  1901. kfree(adapter->msix_entries);
  1902. adapter->msix_entries = NULL;
  1903. return vectors;
  1904. }
  1905. /* Adjust for only the vectors we'll use, which is minimum
  1906. * of max_msix_q_vectors + NON_Q_VECTORS, or the number of
  1907. * vectors we were allocated.
  1908. */
  1909. adapter->num_msix_vectors = vectors;
  1910. return 0;
  1911. }
  1912. /**
  1913. * ixgbevf_set_num_queues - Allocate queues for device, feature dependent
  1914. * @adapter: board private structure to initialize
  1915. *
  1916. * This is the top level queue allocation routine. The order here is very
  1917. * important, starting with the "most" number of features turned on at once,
  1918. * and ending with the smallest set of features. This way large combinations
  1919. * can be allocated if they're turned on, and smaller combinations are the
  1920. * fallthrough conditions.
  1921. *
  1922. **/
  1923. static void ixgbevf_set_num_queues(struct ixgbevf_adapter *adapter)
  1924. {
  1925. struct ixgbe_hw *hw = &adapter->hw;
  1926. unsigned int def_q = 0;
  1927. unsigned int num_tcs = 0;
  1928. int err;
  1929. /* Start with base case */
  1930. adapter->num_rx_queues = 1;
  1931. adapter->num_tx_queues = 1;
  1932. spin_lock_bh(&adapter->mbx_lock);
  1933. /* fetch queue configuration from the PF */
  1934. err = ixgbevf_get_queues(hw, &num_tcs, &def_q);
  1935. spin_unlock_bh(&adapter->mbx_lock);
  1936. if (err)
  1937. return;
  1938. /* we need as many queues as traffic classes */
  1939. if (num_tcs > 1) {
  1940. adapter->num_rx_queues = num_tcs;
  1941. } else {
  1942. u16 rss = min_t(u16, num_online_cpus(), IXGBEVF_MAX_RSS_QUEUES);
  1943. switch (hw->api_version) {
  1944. case ixgbe_mbox_api_11:
  1945. case ixgbe_mbox_api_12:
  1946. adapter->num_rx_queues = rss;
  1947. adapter->num_tx_queues = rss;
  1948. default:
  1949. break;
  1950. }
  1951. }
  1952. }
  1953. /**
  1954. * ixgbevf_alloc_queues - Allocate memory for all rings
  1955. * @adapter: board private structure to initialize
  1956. *
  1957. * We allocate one ring per queue at run-time since we don't know the
  1958. * number of queues at compile-time. The polling_netdev array is
  1959. * intended for Multiqueue, but should work fine with a single queue.
  1960. **/
  1961. static int ixgbevf_alloc_queues(struct ixgbevf_adapter *adapter)
  1962. {
  1963. struct ixgbevf_ring *ring;
  1964. int rx = 0, tx = 0;
  1965. for (; tx < adapter->num_tx_queues; tx++) {
  1966. ring = kzalloc(sizeof(*ring), GFP_KERNEL);
  1967. if (!ring)
  1968. goto err_allocation;
  1969. ring->dev = &adapter->pdev->dev;
  1970. ring->netdev = adapter->netdev;
  1971. ring->count = adapter->tx_ring_count;
  1972. ring->queue_index = tx;
  1973. ring->reg_idx = tx;
  1974. adapter->tx_ring[tx] = ring;
  1975. }
  1976. for (; rx < adapter->num_rx_queues; rx++) {
  1977. ring = kzalloc(sizeof(*ring), GFP_KERNEL);
  1978. if (!ring)
  1979. goto err_allocation;
  1980. ring->dev = &adapter->pdev->dev;
  1981. ring->netdev = adapter->netdev;
  1982. ring->count = adapter->rx_ring_count;
  1983. ring->queue_index = rx;
  1984. ring->reg_idx = rx;
  1985. adapter->rx_ring[rx] = ring;
  1986. }
  1987. return 0;
  1988. err_allocation:
  1989. while (tx) {
  1990. kfree(adapter->tx_ring[--tx]);
  1991. adapter->tx_ring[tx] = NULL;
  1992. }
  1993. while (rx) {
  1994. kfree(adapter->rx_ring[--rx]);
  1995. adapter->rx_ring[rx] = NULL;
  1996. }
  1997. return -ENOMEM;
  1998. }
  1999. /**
  2000. * ixgbevf_set_interrupt_capability - set MSI-X or FAIL if not supported
  2001. * @adapter: board private structure to initialize
  2002. *
  2003. * Attempt to configure the interrupts using the best available
  2004. * capabilities of the hardware and the kernel.
  2005. **/
  2006. static int ixgbevf_set_interrupt_capability(struct ixgbevf_adapter *adapter)
  2007. {
  2008. struct net_device *netdev = adapter->netdev;
  2009. int err;
  2010. int vector, v_budget;
  2011. /* It's easy to be greedy for MSI-X vectors, but it really
  2012. * doesn't do us much good if we have a lot more vectors
  2013. * than CPU's. So let's be conservative and only ask for
  2014. * (roughly) the same number of vectors as there are CPU's.
  2015. * The default is to use pairs of vectors.
  2016. */
  2017. v_budget = max(adapter->num_rx_queues, adapter->num_tx_queues);
  2018. v_budget = min_t(int, v_budget, num_online_cpus());
  2019. v_budget += NON_Q_VECTORS;
  2020. /* A failure in MSI-X entry allocation isn't fatal, but it does
  2021. * mean we disable MSI-X capabilities of the adapter.
  2022. */
  2023. adapter->msix_entries = kcalloc(v_budget,
  2024. sizeof(struct msix_entry), GFP_KERNEL);
  2025. if (!adapter->msix_entries)
  2026. return -ENOMEM;
  2027. for (vector = 0; vector < v_budget; vector++)
  2028. adapter->msix_entries[vector].entry = vector;
  2029. err = ixgbevf_acquire_msix_vectors(adapter, v_budget);
  2030. if (err)
  2031. return err;
  2032. err = netif_set_real_num_tx_queues(netdev, adapter->num_tx_queues);
  2033. if (err)
  2034. return err;
  2035. return netif_set_real_num_rx_queues(netdev, adapter->num_rx_queues);
  2036. }
  2037. /**
  2038. * ixgbevf_alloc_q_vectors - Allocate memory for interrupt vectors
  2039. * @adapter: board private structure to initialize
  2040. *
  2041. * We allocate one q_vector per queue interrupt. If allocation fails we
  2042. * return -ENOMEM.
  2043. **/
  2044. static int ixgbevf_alloc_q_vectors(struct ixgbevf_adapter *adapter)
  2045. {
  2046. int q_idx, num_q_vectors;
  2047. struct ixgbevf_q_vector *q_vector;
  2048. num_q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
  2049. for (q_idx = 0; q_idx < num_q_vectors; q_idx++) {
  2050. q_vector = kzalloc(sizeof(struct ixgbevf_q_vector), GFP_KERNEL);
  2051. if (!q_vector)
  2052. goto err_out;
  2053. q_vector->adapter = adapter;
  2054. q_vector->v_idx = q_idx;
  2055. netif_napi_add(adapter->netdev, &q_vector->napi,
  2056. ixgbevf_poll, 64);
  2057. adapter->q_vector[q_idx] = q_vector;
  2058. }
  2059. return 0;
  2060. err_out:
  2061. while (q_idx) {
  2062. q_idx--;
  2063. q_vector = adapter->q_vector[q_idx];
  2064. #ifdef CONFIG_NET_RX_BUSY_POLL
  2065. napi_hash_del(&q_vector->napi);
  2066. #endif
  2067. netif_napi_del(&q_vector->napi);
  2068. kfree(q_vector);
  2069. adapter->q_vector[q_idx] = NULL;
  2070. }
  2071. return -ENOMEM;
  2072. }
  2073. /**
  2074. * ixgbevf_free_q_vectors - Free memory allocated for interrupt vectors
  2075. * @adapter: board private structure to initialize
  2076. *
  2077. * This function frees the memory allocated to the q_vectors. In addition if
  2078. * NAPI is enabled it will delete any references to the NAPI struct prior
  2079. * to freeing the q_vector.
  2080. **/
  2081. static void ixgbevf_free_q_vectors(struct ixgbevf_adapter *adapter)
  2082. {
  2083. int q_idx, num_q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
  2084. for (q_idx = 0; q_idx < num_q_vectors; q_idx++) {
  2085. struct ixgbevf_q_vector *q_vector = adapter->q_vector[q_idx];
  2086. adapter->q_vector[q_idx] = NULL;
  2087. #ifdef CONFIG_NET_RX_BUSY_POLL
  2088. napi_hash_del(&q_vector->napi);
  2089. #endif
  2090. netif_napi_del(&q_vector->napi);
  2091. kfree(q_vector);
  2092. }
  2093. }
  2094. /**
  2095. * ixgbevf_reset_interrupt_capability - Reset MSIX setup
  2096. * @adapter: board private structure
  2097. *
  2098. **/
  2099. static void ixgbevf_reset_interrupt_capability(struct ixgbevf_adapter *adapter)
  2100. {
  2101. pci_disable_msix(adapter->pdev);
  2102. kfree(adapter->msix_entries);
  2103. adapter->msix_entries = NULL;
  2104. }
  2105. /**
  2106. * ixgbevf_init_interrupt_scheme - Determine if MSIX is supported and init
  2107. * @adapter: board private structure to initialize
  2108. *
  2109. **/
  2110. static int ixgbevf_init_interrupt_scheme(struct ixgbevf_adapter *adapter)
  2111. {
  2112. int err;
  2113. /* Number of supported queues */
  2114. ixgbevf_set_num_queues(adapter);
  2115. err = ixgbevf_set_interrupt_capability(adapter);
  2116. if (err) {
  2117. hw_dbg(&adapter->hw,
  2118. "Unable to setup interrupt capabilities\n");
  2119. goto err_set_interrupt;
  2120. }
  2121. err = ixgbevf_alloc_q_vectors(adapter);
  2122. if (err) {
  2123. hw_dbg(&adapter->hw, "Unable to allocate memory for queue vectors\n");
  2124. goto err_alloc_q_vectors;
  2125. }
  2126. err = ixgbevf_alloc_queues(adapter);
  2127. if (err) {
  2128. pr_err("Unable to allocate memory for queues\n");
  2129. goto err_alloc_queues;
  2130. }
  2131. hw_dbg(&adapter->hw, "Multiqueue %s: Rx Queue count = %u, Tx Queue count = %u\n",
  2132. (adapter->num_rx_queues > 1) ? "Enabled" :
  2133. "Disabled", adapter->num_rx_queues, adapter->num_tx_queues);
  2134. set_bit(__IXGBEVF_DOWN, &adapter->state);
  2135. return 0;
  2136. err_alloc_queues:
  2137. ixgbevf_free_q_vectors(adapter);
  2138. err_alloc_q_vectors:
  2139. ixgbevf_reset_interrupt_capability(adapter);
  2140. err_set_interrupt:
  2141. return err;
  2142. }
  2143. /**
  2144. * ixgbevf_clear_interrupt_scheme - Clear the current interrupt scheme settings
  2145. * @adapter: board private structure to clear interrupt scheme on
  2146. *
  2147. * We go through and clear interrupt specific resources and reset the structure
  2148. * to pre-load conditions
  2149. **/
  2150. static void ixgbevf_clear_interrupt_scheme(struct ixgbevf_adapter *adapter)
  2151. {
  2152. int i;
  2153. for (i = 0; i < adapter->num_tx_queues; i++) {
  2154. kfree(adapter->tx_ring[i]);
  2155. adapter->tx_ring[i] = NULL;
  2156. }
  2157. for (i = 0; i < adapter->num_rx_queues; i++) {
  2158. kfree(adapter->rx_ring[i]);
  2159. adapter->rx_ring[i] = NULL;
  2160. }
  2161. adapter->num_tx_queues = 0;
  2162. adapter->num_rx_queues = 0;
  2163. ixgbevf_free_q_vectors(adapter);
  2164. ixgbevf_reset_interrupt_capability(adapter);
  2165. }
  2166. /**
  2167. * ixgbevf_sw_init - Initialize general software structures
  2168. * @adapter: board private structure to initialize
  2169. *
  2170. * ixgbevf_sw_init initializes the Adapter private data structure.
  2171. * Fields are initialized based on PCI device information and
  2172. * OS network device settings (MTU size).
  2173. **/
  2174. static int ixgbevf_sw_init(struct ixgbevf_adapter *adapter)
  2175. {
  2176. struct ixgbe_hw *hw = &adapter->hw;
  2177. struct pci_dev *pdev = adapter->pdev;
  2178. struct net_device *netdev = adapter->netdev;
  2179. int err;
  2180. /* PCI config space info */
  2181. hw->vendor_id = pdev->vendor;
  2182. hw->device_id = pdev->device;
  2183. hw->revision_id = pdev->revision;
  2184. hw->subsystem_vendor_id = pdev->subsystem_vendor;
  2185. hw->subsystem_device_id = pdev->subsystem_device;
  2186. hw->mbx.ops.init_params(hw);
  2187. /* assume legacy case in which PF would only give VF 2 queues */
  2188. hw->mac.max_tx_queues = 2;
  2189. hw->mac.max_rx_queues = 2;
  2190. /* lock to protect mailbox accesses */
  2191. spin_lock_init(&adapter->mbx_lock);
  2192. err = hw->mac.ops.reset_hw(hw);
  2193. if (err) {
  2194. dev_info(&pdev->dev,
  2195. "PF still in reset state. Is the PF interface up?\n");
  2196. } else {
  2197. err = hw->mac.ops.init_hw(hw);
  2198. if (err) {
  2199. pr_err("init_shared_code failed: %d\n", err);
  2200. goto out;
  2201. }
  2202. ixgbevf_negotiate_api(adapter);
  2203. err = hw->mac.ops.get_mac_addr(hw, hw->mac.addr);
  2204. if (err)
  2205. dev_info(&pdev->dev, "Error reading MAC address\n");
  2206. else if (is_zero_ether_addr(adapter->hw.mac.addr))
  2207. dev_info(&pdev->dev,
  2208. "MAC address not assigned by administrator.\n");
  2209. ether_addr_copy(netdev->dev_addr, hw->mac.addr);
  2210. }
  2211. if (!is_valid_ether_addr(netdev->dev_addr)) {
  2212. dev_info(&pdev->dev, "Assigning random MAC address\n");
  2213. eth_hw_addr_random(netdev);
  2214. ether_addr_copy(hw->mac.addr, netdev->dev_addr);
  2215. ether_addr_copy(hw->mac.perm_addr, netdev->dev_addr);
  2216. }
  2217. /* Enable dynamic interrupt throttling rates */
  2218. adapter->rx_itr_setting = 1;
  2219. adapter->tx_itr_setting = 1;
  2220. /* set default ring sizes */
  2221. adapter->tx_ring_count = IXGBEVF_DEFAULT_TXD;
  2222. adapter->rx_ring_count = IXGBEVF_DEFAULT_RXD;
  2223. set_bit(__IXGBEVF_DOWN, &adapter->state);
  2224. return 0;
  2225. out:
  2226. return err;
  2227. }
  2228. #define UPDATE_VF_COUNTER_32bit(reg, last_counter, counter) \
  2229. { \
  2230. u32 current_counter = IXGBE_READ_REG(hw, reg); \
  2231. if (current_counter < last_counter) \
  2232. counter += 0x100000000LL; \
  2233. last_counter = current_counter; \
  2234. counter &= 0xFFFFFFFF00000000LL; \
  2235. counter |= current_counter; \
  2236. }
  2237. #define UPDATE_VF_COUNTER_36bit(reg_lsb, reg_msb, last_counter, counter) \
  2238. { \
  2239. u64 current_counter_lsb = IXGBE_READ_REG(hw, reg_lsb); \
  2240. u64 current_counter_msb = IXGBE_READ_REG(hw, reg_msb); \
  2241. u64 current_counter = (current_counter_msb << 32) | \
  2242. current_counter_lsb; \
  2243. if (current_counter < last_counter) \
  2244. counter += 0x1000000000LL; \
  2245. last_counter = current_counter; \
  2246. counter &= 0xFFFFFFF000000000LL; \
  2247. counter |= current_counter; \
  2248. }
  2249. /**
  2250. * ixgbevf_update_stats - Update the board statistics counters.
  2251. * @adapter: board private structure
  2252. **/
  2253. void ixgbevf_update_stats(struct ixgbevf_adapter *adapter)
  2254. {
  2255. struct ixgbe_hw *hw = &adapter->hw;
  2256. int i;
  2257. if (test_bit(__IXGBEVF_DOWN, &adapter->state) ||
  2258. test_bit(__IXGBEVF_RESETTING, &adapter->state))
  2259. return;
  2260. UPDATE_VF_COUNTER_32bit(IXGBE_VFGPRC, adapter->stats.last_vfgprc,
  2261. adapter->stats.vfgprc);
  2262. UPDATE_VF_COUNTER_32bit(IXGBE_VFGPTC, adapter->stats.last_vfgptc,
  2263. adapter->stats.vfgptc);
  2264. UPDATE_VF_COUNTER_36bit(IXGBE_VFGORC_LSB, IXGBE_VFGORC_MSB,
  2265. adapter->stats.last_vfgorc,
  2266. adapter->stats.vfgorc);
  2267. UPDATE_VF_COUNTER_36bit(IXGBE_VFGOTC_LSB, IXGBE_VFGOTC_MSB,
  2268. adapter->stats.last_vfgotc,
  2269. adapter->stats.vfgotc);
  2270. UPDATE_VF_COUNTER_32bit(IXGBE_VFMPRC, adapter->stats.last_vfmprc,
  2271. adapter->stats.vfmprc);
  2272. for (i = 0; i < adapter->num_rx_queues; i++) {
  2273. adapter->hw_csum_rx_error +=
  2274. adapter->rx_ring[i]->hw_csum_rx_error;
  2275. adapter->rx_ring[i]->hw_csum_rx_error = 0;
  2276. }
  2277. }
  2278. /**
  2279. * ixgbevf_service_timer - Timer Call-back
  2280. * @data: pointer to adapter cast into an unsigned long
  2281. **/
  2282. static void ixgbevf_service_timer(unsigned long data)
  2283. {
  2284. struct ixgbevf_adapter *adapter = (struct ixgbevf_adapter *)data;
  2285. /* Reset the timer */
  2286. mod_timer(&adapter->service_timer, (HZ * 2) + jiffies);
  2287. ixgbevf_service_event_schedule(adapter);
  2288. }
  2289. static void ixgbevf_reset_subtask(struct ixgbevf_adapter *adapter)
  2290. {
  2291. if (!test_and_clear_bit(__IXGBEVF_RESET_REQUESTED, &adapter->state))
  2292. return;
  2293. /* If we're already down or resetting, just bail */
  2294. if (test_bit(__IXGBEVF_DOWN, &adapter->state) ||
  2295. test_bit(__IXGBEVF_REMOVING, &adapter->state) ||
  2296. test_bit(__IXGBEVF_RESETTING, &adapter->state))
  2297. return;
  2298. adapter->tx_timeout_count++;
  2299. rtnl_lock();
  2300. ixgbevf_reinit_locked(adapter);
  2301. rtnl_unlock();
  2302. }
  2303. /**
  2304. * ixgbevf_check_hang_subtask - check for hung queues and dropped interrupts
  2305. * @adapter: pointer to the device adapter structure
  2306. *
  2307. * This function serves two purposes. First it strobes the interrupt lines
  2308. * in order to make certain interrupts are occurring. Secondly it sets the
  2309. * bits needed to check for TX hangs. As a result we should immediately
  2310. * determine if a hang has occurred.
  2311. **/
  2312. static void ixgbevf_check_hang_subtask(struct ixgbevf_adapter *adapter)
  2313. {
  2314. struct ixgbe_hw *hw = &adapter->hw;
  2315. u32 eics = 0;
  2316. int i;
  2317. /* If we're down or resetting, just bail */
  2318. if (test_bit(__IXGBEVF_DOWN, &adapter->state) ||
  2319. test_bit(__IXGBEVF_RESETTING, &adapter->state))
  2320. return;
  2321. /* Force detection of hung controller */
  2322. if (netif_carrier_ok(adapter->netdev)) {
  2323. for (i = 0; i < adapter->num_tx_queues; i++)
  2324. set_check_for_tx_hang(adapter->tx_ring[i]);
  2325. }
  2326. /* get one bit for every active Tx/Rx interrupt vector */
  2327. for (i = 0; i < adapter->num_msix_vectors - NON_Q_VECTORS; i++) {
  2328. struct ixgbevf_q_vector *qv = adapter->q_vector[i];
  2329. if (qv->rx.ring || qv->tx.ring)
  2330. eics |= BIT(i);
  2331. }
  2332. /* Cause software interrupt to ensure rings are cleaned */
  2333. IXGBE_WRITE_REG(hw, IXGBE_VTEICS, eics);
  2334. }
  2335. /**
  2336. * ixgbevf_watchdog_update_link - update the link status
  2337. * @adapter: pointer to the device adapter structure
  2338. **/
  2339. static void ixgbevf_watchdog_update_link(struct ixgbevf_adapter *adapter)
  2340. {
  2341. struct ixgbe_hw *hw = &adapter->hw;
  2342. u32 link_speed = adapter->link_speed;
  2343. bool link_up = adapter->link_up;
  2344. s32 err;
  2345. spin_lock_bh(&adapter->mbx_lock);
  2346. err = hw->mac.ops.check_link(hw, &link_speed, &link_up, false);
  2347. spin_unlock_bh(&adapter->mbx_lock);
  2348. /* if check for link returns error we will need to reset */
  2349. if (err && time_after(jiffies, adapter->last_reset + (10 * HZ))) {
  2350. set_bit(__IXGBEVF_RESET_REQUESTED, &adapter->state);
  2351. link_up = false;
  2352. }
  2353. adapter->link_up = link_up;
  2354. adapter->link_speed = link_speed;
  2355. }
  2356. /**
  2357. * ixgbevf_watchdog_link_is_up - update netif_carrier status and
  2358. * print link up message
  2359. * @adapter: pointer to the device adapter structure
  2360. **/
  2361. static void ixgbevf_watchdog_link_is_up(struct ixgbevf_adapter *adapter)
  2362. {
  2363. struct net_device *netdev = adapter->netdev;
  2364. /* only continue if link was previously down */
  2365. if (netif_carrier_ok(netdev))
  2366. return;
  2367. dev_info(&adapter->pdev->dev, "NIC Link is Up %s\n",
  2368. (adapter->link_speed == IXGBE_LINK_SPEED_10GB_FULL) ?
  2369. "10 Gbps" :
  2370. (adapter->link_speed == IXGBE_LINK_SPEED_1GB_FULL) ?
  2371. "1 Gbps" :
  2372. (adapter->link_speed == IXGBE_LINK_SPEED_100_FULL) ?
  2373. "100 Mbps" :
  2374. "unknown speed");
  2375. netif_carrier_on(netdev);
  2376. }
  2377. /**
  2378. * ixgbevf_watchdog_link_is_down - update netif_carrier status and
  2379. * print link down message
  2380. * @adapter: pointer to the adapter structure
  2381. **/
  2382. static void ixgbevf_watchdog_link_is_down(struct ixgbevf_adapter *adapter)
  2383. {
  2384. struct net_device *netdev = adapter->netdev;
  2385. adapter->link_speed = 0;
  2386. /* only continue if link was up previously */
  2387. if (!netif_carrier_ok(netdev))
  2388. return;
  2389. dev_info(&adapter->pdev->dev, "NIC Link is Down\n");
  2390. netif_carrier_off(netdev);
  2391. }
  2392. /**
  2393. * ixgbevf_watchdog_subtask - worker thread to bring link up
  2394. * @work: pointer to work_struct containing our data
  2395. **/
  2396. static void ixgbevf_watchdog_subtask(struct ixgbevf_adapter *adapter)
  2397. {
  2398. /* if interface is down do nothing */
  2399. if (test_bit(__IXGBEVF_DOWN, &adapter->state) ||
  2400. test_bit(__IXGBEVF_RESETTING, &adapter->state))
  2401. return;
  2402. ixgbevf_watchdog_update_link(adapter);
  2403. if (adapter->link_up)
  2404. ixgbevf_watchdog_link_is_up(adapter);
  2405. else
  2406. ixgbevf_watchdog_link_is_down(adapter);
  2407. ixgbevf_update_stats(adapter);
  2408. }
  2409. /**
  2410. * ixgbevf_service_task - manages and runs subtasks
  2411. * @work: pointer to work_struct containing our data
  2412. **/
  2413. static void ixgbevf_service_task(struct work_struct *work)
  2414. {
  2415. struct ixgbevf_adapter *adapter = container_of(work,
  2416. struct ixgbevf_adapter,
  2417. service_task);
  2418. struct ixgbe_hw *hw = &adapter->hw;
  2419. if (IXGBE_REMOVED(hw->hw_addr)) {
  2420. if (!test_bit(__IXGBEVF_DOWN, &adapter->state)) {
  2421. rtnl_lock();
  2422. ixgbevf_down(adapter);
  2423. rtnl_unlock();
  2424. }
  2425. return;
  2426. }
  2427. ixgbevf_queue_reset_subtask(adapter);
  2428. ixgbevf_reset_subtask(adapter);
  2429. ixgbevf_watchdog_subtask(adapter);
  2430. ixgbevf_check_hang_subtask(adapter);
  2431. ixgbevf_service_event_complete(adapter);
  2432. }
  2433. /**
  2434. * ixgbevf_free_tx_resources - Free Tx Resources per Queue
  2435. * @tx_ring: Tx descriptor ring for a specific queue
  2436. *
  2437. * Free all transmit software resources
  2438. **/
  2439. void ixgbevf_free_tx_resources(struct ixgbevf_ring *tx_ring)
  2440. {
  2441. ixgbevf_clean_tx_ring(tx_ring);
  2442. vfree(tx_ring->tx_buffer_info);
  2443. tx_ring->tx_buffer_info = NULL;
  2444. /* if not set, then don't free */
  2445. if (!tx_ring->desc)
  2446. return;
  2447. dma_free_coherent(tx_ring->dev, tx_ring->size, tx_ring->desc,
  2448. tx_ring->dma);
  2449. tx_ring->desc = NULL;
  2450. }
  2451. /**
  2452. * ixgbevf_free_all_tx_resources - Free Tx Resources for All Queues
  2453. * @adapter: board private structure
  2454. *
  2455. * Free all transmit software resources
  2456. **/
  2457. static void ixgbevf_free_all_tx_resources(struct ixgbevf_adapter *adapter)
  2458. {
  2459. int i;
  2460. for (i = 0; i < adapter->num_tx_queues; i++)
  2461. if (adapter->tx_ring[i]->desc)
  2462. ixgbevf_free_tx_resources(adapter->tx_ring[i]);
  2463. }
  2464. /**
  2465. * ixgbevf_setup_tx_resources - allocate Tx resources (Descriptors)
  2466. * @tx_ring: Tx descriptor ring (for a specific queue) to setup
  2467. *
  2468. * Return 0 on success, negative on failure
  2469. **/
  2470. int ixgbevf_setup_tx_resources(struct ixgbevf_ring *tx_ring)
  2471. {
  2472. struct ixgbevf_adapter *adapter = netdev_priv(tx_ring->netdev);
  2473. int size;
  2474. size = sizeof(struct ixgbevf_tx_buffer) * tx_ring->count;
  2475. tx_ring->tx_buffer_info = vzalloc(size);
  2476. if (!tx_ring->tx_buffer_info)
  2477. goto err;
  2478. /* round up to nearest 4K */
  2479. tx_ring->size = tx_ring->count * sizeof(union ixgbe_adv_tx_desc);
  2480. tx_ring->size = ALIGN(tx_ring->size, 4096);
  2481. tx_ring->desc = dma_alloc_coherent(tx_ring->dev, tx_ring->size,
  2482. &tx_ring->dma, GFP_KERNEL);
  2483. if (!tx_ring->desc)
  2484. goto err;
  2485. return 0;
  2486. err:
  2487. vfree(tx_ring->tx_buffer_info);
  2488. tx_ring->tx_buffer_info = NULL;
  2489. hw_dbg(&adapter->hw, "Unable to allocate memory for the transmit descriptor ring\n");
  2490. return -ENOMEM;
  2491. }
  2492. /**
  2493. * ixgbevf_setup_all_tx_resources - allocate all queues Tx resources
  2494. * @adapter: board private structure
  2495. *
  2496. * If this function returns with an error, then it's possible one or
  2497. * more of the rings is populated (while the rest are not). It is the
  2498. * callers duty to clean those orphaned rings.
  2499. *
  2500. * Return 0 on success, negative on failure
  2501. **/
  2502. static int ixgbevf_setup_all_tx_resources(struct ixgbevf_adapter *adapter)
  2503. {
  2504. int i, err = 0;
  2505. for (i = 0; i < adapter->num_tx_queues; i++) {
  2506. err = ixgbevf_setup_tx_resources(adapter->tx_ring[i]);
  2507. if (!err)
  2508. continue;
  2509. hw_dbg(&adapter->hw, "Allocation for Tx Queue %u failed\n", i);
  2510. break;
  2511. }
  2512. return err;
  2513. }
  2514. /**
  2515. * ixgbevf_setup_rx_resources - allocate Rx resources (Descriptors)
  2516. * @rx_ring: Rx descriptor ring (for a specific queue) to setup
  2517. *
  2518. * Returns 0 on success, negative on failure
  2519. **/
  2520. int ixgbevf_setup_rx_resources(struct ixgbevf_ring *rx_ring)
  2521. {
  2522. int size;
  2523. size = sizeof(struct ixgbevf_rx_buffer) * rx_ring->count;
  2524. rx_ring->rx_buffer_info = vzalloc(size);
  2525. if (!rx_ring->rx_buffer_info)
  2526. goto err;
  2527. /* Round up to nearest 4K */
  2528. rx_ring->size = rx_ring->count * sizeof(union ixgbe_adv_rx_desc);
  2529. rx_ring->size = ALIGN(rx_ring->size, 4096);
  2530. rx_ring->desc = dma_alloc_coherent(rx_ring->dev, rx_ring->size,
  2531. &rx_ring->dma, GFP_KERNEL);
  2532. if (!rx_ring->desc)
  2533. goto err;
  2534. return 0;
  2535. err:
  2536. vfree(rx_ring->rx_buffer_info);
  2537. rx_ring->rx_buffer_info = NULL;
  2538. dev_err(rx_ring->dev, "Unable to allocate memory for the Rx descriptor ring\n");
  2539. return -ENOMEM;
  2540. }
  2541. /**
  2542. * ixgbevf_setup_all_rx_resources - allocate all queues Rx resources
  2543. * @adapter: board private structure
  2544. *
  2545. * If this function returns with an error, then it's possible one or
  2546. * more of the rings is populated (while the rest are not). It is the
  2547. * callers duty to clean those orphaned rings.
  2548. *
  2549. * Return 0 on success, negative on failure
  2550. **/
  2551. static int ixgbevf_setup_all_rx_resources(struct ixgbevf_adapter *adapter)
  2552. {
  2553. int i, err = 0;
  2554. for (i = 0; i < adapter->num_rx_queues; i++) {
  2555. err = ixgbevf_setup_rx_resources(adapter->rx_ring[i]);
  2556. if (!err)
  2557. continue;
  2558. hw_dbg(&adapter->hw, "Allocation for Rx Queue %u failed\n", i);
  2559. break;
  2560. }
  2561. return err;
  2562. }
  2563. /**
  2564. * ixgbevf_free_rx_resources - Free Rx Resources
  2565. * @rx_ring: ring to clean the resources from
  2566. *
  2567. * Free all receive software resources
  2568. **/
  2569. void ixgbevf_free_rx_resources(struct ixgbevf_ring *rx_ring)
  2570. {
  2571. ixgbevf_clean_rx_ring(rx_ring);
  2572. vfree(rx_ring->rx_buffer_info);
  2573. rx_ring->rx_buffer_info = NULL;
  2574. dma_free_coherent(rx_ring->dev, rx_ring->size, rx_ring->desc,
  2575. rx_ring->dma);
  2576. rx_ring->desc = NULL;
  2577. }
  2578. /**
  2579. * ixgbevf_free_all_rx_resources - Free Rx Resources for All Queues
  2580. * @adapter: board private structure
  2581. *
  2582. * Free all receive software resources
  2583. **/
  2584. static void ixgbevf_free_all_rx_resources(struct ixgbevf_adapter *adapter)
  2585. {
  2586. int i;
  2587. for (i = 0; i < adapter->num_rx_queues; i++)
  2588. if (adapter->rx_ring[i]->desc)
  2589. ixgbevf_free_rx_resources(adapter->rx_ring[i]);
  2590. }
  2591. /**
  2592. * ixgbevf_open - Called when a network interface is made active
  2593. * @netdev: network interface device structure
  2594. *
  2595. * Returns 0 on success, negative value on failure
  2596. *
  2597. * The open entry point is called when a network interface is made
  2598. * active by the system (IFF_UP). At this point all resources needed
  2599. * for transmit and receive operations are allocated, the interrupt
  2600. * handler is registered with the OS, the watchdog timer is started,
  2601. * and the stack is notified that the interface is ready.
  2602. **/
  2603. int ixgbevf_open(struct net_device *netdev)
  2604. {
  2605. struct ixgbevf_adapter *adapter = netdev_priv(netdev);
  2606. struct ixgbe_hw *hw = &adapter->hw;
  2607. int err;
  2608. /* A previous failure to open the device because of a lack of
  2609. * available MSIX vector resources may have reset the number
  2610. * of msix vectors variable to zero. The only way to recover
  2611. * is to unload/reload the driver and hope that the system has
  2612. * been able to recover some MSIX vector resources.
  2613. */
  2614. if (!adapter->num_msix_vectors)
  2615. return -ENOMEM;
  2616. if (hw->adapter_stopped) {
  2617. ixgbevf_reset(adapter);
  2618. /* if adapter is still stopped then PF isn't up and
  2619. * the VF can't start.
  2620. */
  2621. if (hw->adapter_stopped) {
  2622. err = IXGBE_ERR_MBX;
  2623. pr_err("Unable to start - perhaps the PF Driver isn't up yet\n");
  2624. goto err_setup_reset;
  2625. }
  2626. }
  2627. /* disallow open during test */
  2628. if (test_bit(__IXGBEVF_TESTING, &adapter->state))
  2629. return -EBUSY;
  2630. netif_carrier_off(netdev);
  2631. /* allocate transmit descriptors */
  2632. err = ixgbevf_setup_all_tx_resources(adapter);
  2633. if (err)
  2634. goto err_setup_tx;
  2635. /* allocate receive descriptors */
  2636. err = ixgbevf_setup_all_rx_resources(adapter);
  2637. if (err)
  2638. goto err_setup_rx;
  2639. ixgbevf_configure(adapter);
  2640. /* Map the Tx/Rx rings to the vectors we were allotted.
  2641. * if request_irq will be called in this function map_rings
  2642. * must be called *before* up_complete
  2643. */
  2644. ixgbevf_map_rings_to_vectors(adapter);
  2645. err = ixgbevf_request_irq(adapter);
  2646. if (err)
  2647. goto err_req_irq;
  2648. ixgbevf_up_complete(adapter);
  2649. return 0;
  2650. err_req_irq:
  2651. ixgbevf_down(adapter);
  2652. err_setup_rx:
  2653. ixgbevf_free_all_rx_resources(adapter);
  2654. err_setup_tx:
  2655. ixgbevf_free_all_tx_resources(adapter);
  2656. ixgbevf_reset(adapter);
  2657. err_setup_reset:
  2658. return err;
  2659. }
  2660. /**
  2661. * ixgbevf_close - Disables a network interface
  2662. * @netdev: network interface device structure
  2663. *
  2664. * Returns 0, this is not allowed to fail
  2665. *
  2666. * The close entry point is called when an interface is de-activated
  2667. * by the OS. The hardware is still under the drivers control, but
  2668. * needs to be disabled. A global MAC reset is issued to stop the
  2669. * hardware, and all transmit and receive resources are freed.
  2670. **/
  2671. int ixgbevf_close(struct net_device *netdev)
  2672. {
  2673. struct ixgbevf_adapter *adapter = netdev_priv(netdev);
  2674. ixgbevf_down(adapter);
  2675. ixgbevf_free_irq(adapter);
  2676. ixgbevf_free_all_tx_resources(adapter);
  2677. ixgbevf_free_all_rx_resources(adapter);
  2678. return 0;
  2679. }
  2680. static void ixgbevf_queue_reset_subtask(struct ixgbevf_adapter *adapter)
  2681. {
  2682. struct net_device *dev = adapter->netdev;
  2683. if (!test_and_clear_bit(__IXGBEVF_QUEUE_RESET_REQUESTED,
  2684. &adapter->state))
  2685. return;
  2686. /* if interface is down do nothing */
  2687. if (test_bit(__IXGBEVF_DOWN, &adapter->state) ||
  2688. test_bit(__IXGBEVF_RESETTING, &adapter->state))
  2689. return;
  2690. /* Hardware has to reinitialize queues and interrupts to
  2691. * match packet buffer alignment. Unfortunately, the
  2692. * hardware is not flexible enough to do this dynamically.
  2693. */
  2694. if (netif_running(dev))
  2695. ixgbevf_close(dev);
  2696. ixgbevf_clear_interrupt_scheme(adapter);
  2697. ixgbevf_init_interrupt_scheme(adapter);
  2698. if (netif_running(dev))
  2699. ixgbevf_open(dev);
  2700. }
  2701. static void ixgbevf_tx_ctxtdesc(struct ixgbevf_ring *tx_ring,
  2702. u32 vlan_macip_lens, u32 type_tucmd,
  2703. u32 mss_l4len_idx)
  2704. {
  2705. struct ixgbe_adv_tx_context_desc *context_desc;
  2706. u16 i = tx_ring->next_to_use;
  2707. context_desc = IXGBEVF_TX_CTXTDESC(tx_ring, i);
  2708. i++;
  2709. tx_ring->next_to_use = (i < tx_ring->count) ? i : 0;
  2710. /* set bits to identify this as an advanced context descriptor */
  2711. type_tucmd |= IXGBE_TXD_CMD_DEXT | IXGBE_ADVTXD_DTYP_CTXT;
  2712. context_desc->vlan_macip_lens = cpu_to_le32(vlan_macip_lens);
  2713. context_desc->seqnum_seed = 0;
  2714. context_desc->type_tucmd_mlhl = cpu_to_le32(type_tucmd);
  2715. context_desc->mss_l4len_idx = cpu_to_le32(mss_l4len_idx);
  2716. }
  2717. static int ixgbevf_tso(struct ixgbevf_ring *tx_ring,
  2718. struct ixgbevf_tx_buffer *first,
  2719. u8 *hdr_len)
  2720. {
  2721. u32 vlan_macip_lens, type_tucmd, mss_l4len_idx;
  2722. struct sk_buff *skb = first->skb;
  2723. union {
  2724. struct iphdr *v4;
  2725. struct ipv6hdr *v6;
  2726. unsigned char *hdr;
  2727. } ip;
  2728. union {
  2729. struct tcphdr *tcp;
  2730. unsigned char *hdr;
  2731. } l4;
  2732. u32 paylen, l4_offset;
  2733. int err;
  2734. if (skb->ip_summed != CHECKSUM_PARTIAL)
  2735. return 0;
  2736. if (!skb_is_gso(skb))
  2737. return 0;
  2738. err = skb_cow_head(skb, 0);
  2739. if (err < 0)
  2740. return err;
  2741. ip.hdr = skb_network_header(skb);
  2742. l4.hdr = skb_checksum_start(skb);
  2743. /* ADV DTYP TUCMD MKRLOC/ISCSIHEDLEN */
  2744. type_tucmd = IXGBE_ADVTXD_TUCMD_L4T_TCP;
  2745. /* initialize outer IP header fields */
  2746. if (ip.v4->version == 4) {
  2747. unsigned char *csum_start = skb_checksum_start(skb);
  2748. unsigned char *trans_start = ip.hdr + (ip.v4->ihl * 4);
  2749. /* IP header will have to cancel out any data that
  2750. * is not a part of the outer IP header
  2751. */
  2752. ip.v4->check = csum_fold(csum_partial(trans_start,
  2753. csum_start - trans_start,
  2754. 0));
  2755. type_tucmd |= IXGBE_ADVTXD_TUCMD_IPV4;
  2756. ip.v4->tot_len = 0;
  2757. first->tx_flags |= IXGBE_TX_FLAGS_TSO |
  2758. IXGBE_TX_FLAGS_CSUM |
  2759. IXGBE_TX_FLAGS_IPV4;
  2760. } else {
  2761. ip.v6->payload_len = 0;
  2762. first->tx_flags |= IXGBE_TX_FLAGS_TSO |
  2763. IXGBE_TX_FLAGS_CSUM;
  2764. }
  2765. /* determine offset of inner transport header */
  2766. l4_offset = l4.hdr - skb->data;
  2767. /* compute length of segmentation header */
  2768. *hdr_len = (l4.tcp->doff * 4) + l4_offset;
  2769. /* remove payload length from inner checksum */
  2770. paylen = skb->len - l4_offset;
  2771. csum_replace_by_diff(&l4.tcp->check, htonl(paylen));
  2772. /* update gso size and bytecount with header size */
  2773. first->gso_segs = skb_shinfo(skb)->gso_segs;
  2774. first->bytecount += (first->gso_segs - 1) * *hdr_len;
  2775. /* mss_l4len_id: use 1 as index for TSO */
  2776. mss_l4len_idx = (*hdr_len - l4_offset) << IXGBE_ADVTXD_L4LEN_SHIFT;
  2777. mss_l4len_idx |= skb_shinfo(skb)->gso_size << IXGBE_ADVTXD_MSS_SHIFT;
  2778. mss_l4len_idx |= (1u << IXGBE_ADVTXD_IDX_SHIFT);
  2779. /* vlan_macip_lens: HEADLEN, MACLEN, VLAN tag */
  2780. vlan_macip_lens = l4.hdr - ip.hdr;
  2781. vlan_macip_lens |= (ip.hdr - skb->data) << IXGBE_ADVTXD_MACLEN_SHIFT;
  2782. vlan_macip_lens |= first->tx_flags & IXGBE_TX_FLAGS_VLAN_MASK;
  2783. ixgbevf_tx_ctxtdesc(tx_ring, vlan_macip_lens,
  2784. type_tucmd, mss_l4len_idx);
  2785. return 1;
  2786. }
  2787. static inline bool ixgbevf_ipv6_csum_is_sctp(struct sk_buff *skb)
  2788. {
  2789. unsigned int offset = 0;
  2790. ipv6_find_hdr(skb, &offset, IPPROTO_SCTP, NULL, NULL);
  2791. return offset == skb_checksum_start_offset(skb);
  2792. }
  2793. static void ixgbevf_tx_csum(struct ixgbevf_ring *tx_ring,
  2794. struct ixgbevf_tx_buffer *first)
  2795. {
  2796. struct sk_buff *skb = first->skb;
  2797. u32 vlan_macip_lens = 0;
  2798. u32 type_tucmd = 0;
  2799. if (skb->ip_summed != CHECKSUM_PARTIAL)
  2800. goto no_csum;
  2801. switch (skb->csum_offset) {
  2802. case offsetof(struct tcphdr, check):
  2803. type_tucmd = IXGBE_ADVTXD_TUCMD_L4T_TCP;
  2804. /* fall through */
  2805. case offsetof(struct udphdr, check):
  2806. break;
  2807. case offsetof(struct sctphdr, checksum):
  2808. /* validate that this is actually an SCTP request */
  2809. if (((first->protocol == htons(ETH_P_IP)) &&
  2810. (ip_hdr(skb)->protocol == IPPROTO_SCTP)) ||
  2811. ((first->protocol == htons(ETH_P_IPV6)) &&
  2812. ixgbevf_ipv6_csum_is_sctp(skb))) {
  2813. type_tucmd = IXGBE_ADVTXD_TUCMD_L4T_SCTP;
  2814. break;
  2815. }
  2816. /* fall through */
  2817. default:
  2818. skb_checksum_help(skb);
  2819. goto no_csum;
  2820. }
  2821. /* update TX checksum flag */
  2822. first->tx_flags |= IXGBE_TX_FLAGS_CSUM;
  2823. vlan_macip_lens = skb_checksum_start_offset(skb) -
  2824. skb_network_offset(skb);
  2825. no_csum:
  2826. /* vlan_macip_lens: MACLEN, VLAN tag */
  2827. vlan_macip_lens |= skb_network_offset(skb) << IXGBE_ADVTXD_MACLEN_SHIFT;
  2828. vlan_macip_lens |= first->tx_flags & IXGBE_TX_FLAGS_VLAN_MASK;
  2829. ixgbevf_tx_ctxtdesc(tx_ring, vlan_macip_lens, type_tucmd, 0);
  2830. }
  2831. static __le32 ixgbevf_tx_cmd_type(u32 tx_flags)
  2832. {
  2833. /* set type for advanced descriptor with frame checksum insertion */
  2834. __le32 cmd_type = cpu_to_le32(IXGBE_ADVTXD_DTYP_DATA |
  2835. IXGBE_ADVTXD_DCMD_IFCS |
  2836. IXGBE_ADVTXD_DCMD_DEXT);
  2837. /* set HW VLAN bit if VLAN is present */
  2838. if (tx_flags & IXGBE_TX_FLAGS_VLAN)
  2839. cmd_type |= cpu_to_le32(IXGBE_ADVTXD_DCMD_VLE);
  2840. /* set segmentation enable bits for TSO/FSO */
  2841. if (tx_flags & IXGBE_TX_FLAGS_TSO)
  2842. cmd_type |= cpu_to_le32(IXGBE_ADVTXD_DCMD_TSE);
  2843. return cmd_type;
  2844. }
  2845. static void ixgbevf_tx_olinfo_status(union ixgbe_adv_tx_desc *tx_desc,
  2846. u32 tx_flags, unsigned int paylen)
  2847. {
  2848. __le32 olinfo_status = cpu_to_le32(paylen << IXGBE_ADVTXD_PAYLEN_SHIFT);
  2849. /* enable L4 checksum for TSO and TX checksum offload */
  2850. if (tx_flags & IXGBE_TX_FLAGS_CSUM)
  2851. olinfo_status |= cpu_to_le32(IXGBE_ADVTXD_POPTS_TXSM);
  2852. /* enble IPv4 checksum for TSO */
  2853. if (tx_flags & IXGBE_TX_FLAGS_IPV4)
  2854. olinfo_status |= cpu_to_le32(IXGBE_ADVTXD_POPTS_IXSM);
  2855. /* use index 1 context for TSO/FSO/FCOE */
  2856. if (tx_flags & IXGBE_TX_FLAGS_TSO)
  2857. olinfo_status |= cpu_to_le32(1u << IXGBE_ADVTXD_IDX_SHIFT);
  2858. /* Check Context must be set if Tx switch is enabled, which it
  2859. * always is for case where virtual functions are running
  2860. */
  2861. olinfo_status |= cpu_to_le32(IXGBE_ADVTXD_CC);
  2862. tx_desc->read.olinfo_status = olinfo_status;
  2863. }
  2864. static void ixgbevf_tx_map(struct ixgbevf_ring *tx_ring,
  2865. struct ixgbevf_tx_buffer *first,
  2866. const u8 hdr_len)
  2867. {
  2868. dma_addr_t dma;
  2869. struct sk_buff *skb = first->skb;
  2870. struct ixgbevf_tx_buffer *tx_buffer;
  2871. union ixgbe_adv_tx_desc *tx_desc;
  2872. struct skb_frag_struct *frag = &skb_shinfo(skb)->frags[0];
  2873. unsigned int data_len = skb->data_len;
  2874. unsigned int size = skb_headlen(skb);
  2875. unsigned int paylen = skb->len - hdr_len;
  2876. u32 tx_flags = first->tx_flags;
  2877. __le32 cmd_type;
  2878. u16 i = tx_ring->next_to_use;
  2879. tx_desc = IXGBEVF_TX_DESC(tx_ring, i);
  2880. ixgbevf_tx_olinfo_status(tx_desc, tx_flags, paylen);
  2881. cmd_type = ixgbevf_tx_cmd_type(tx_flags);
  2882. dma = dma_map_single(tx_ring->dev, skb->data, size, DMA_TO_DEVICE);
  2883. if (dma_mapping_error(tx_ring->dev, dma))
  2884. goto dma_error;
  2885. /* record length, and DMA address */
  2886. dma_unmap_len_set(first, len, size);
  2887. dma_unmap_addr_set(first, dma, dma);
  2888. tx_desc->read.buffer_addr = cpu_to_le64(dma);
  2889. for (;;) {
  2890. while (unlikely(size > IXGBE_MAX_DATA_PER_TXD)) {
  2891. tx_desc->read.cmd_type_len =
  2892. cmd_type | cpu_to_le32(IXGBE_MAX_DATA_PER_TXD);
  2893. i++;
  2894. tx_desc++;
  2895. if (i == tx_ring->count) {
  2896. tx_desc = IXGBEVF_TX_DESC(tx_ring, 0);
  2897. i = 0;
  2898. }
  2899. dma += IXGBE_MAX_DATA_PER_TXD;
  2900. size -= IXGBE_MAX_DATA_PER_TXD;
  2901. tx_desc->read.buffer_addr = cpu_to_le64(dma);
  2902. tx_desc->read.olinfo_status = 0;
  2903. }
  2904. if (likely(!data_len))
  2905. break;
  2906. tx_desc->read.cmd_type_len = cmd_type | cpu_to_le32(size);
  2907. i++;
  2908. tx_desc++;
  2909. if (i == tx_ring->count) {
  2910. tx_desc = IXGBEVF_TX_DESC(tx_ring, 0);
  2911. i = 0;
  2912. }
  2913. size = skb_frag_size(frag);
  2914. data_len -= size;
  2915. dma = skb_frag_dma_map(tx_ring->dev, frag, 0, size,
  2916. DMA_TO_DEVICE);
  2917. if (dma_mapping_error(tx_ring->dev, dma))
  2918. goto dma_error;
  2919. tx_buffer = &tx_ring->tx_buffer_info[i];
  2920. dma_unmap_len_set(tx_buffer, len, size);
  2921. dma_unmap_addr_set(tx_buffer, dma, dma);
  2922. tx_desc->read.buffer_addr = cpu_to_le64(dma);
  2923. tx_desc->read.olinfo_status = 0;
  2924. frag++;
  2925. }
  2926. /* write last descriptor with RS and EOP bits */
  2927. cmd_type |= cpu_to_le32(size) | cpu_to_le32(IXGBE_TXD_CMD);
  2928. tx_desc->read.cmd_type_len = cmd_type;
  2929. /* set the timestamp */
  2930. first->time_stamp = jiffies;
  2931. /* Force memory writes to complete before letting h/w know there
  2932. * are new descriptors to fetch. (Only applicable for weak-ordered
  2933. * memory model archs, such as IA-64).
  2934. *
  2935. * We also need this memory barrier (wmb) to make certain all of the
  2936. * status bits have been updated before next_to_watch is written.
  2937. */
  2938. wmb();
  2939. /* set next_to_watch value indicating a packet is present */
  2940. first->next_to_watch = tx_desc;
  2941. i++;
  2942. if (i == tx_ring->count)
  2943. i = 0;
  2944. tx_ring->next_to_use = i;
  2945. /* notify HW of packet */
  2946. ixgbevf_write_tail(tx_ring, i);
  2947. return;
  2948. dma_error:
  2949. dev_err(tx_ring->dev, "TX DMA map failed\n");
  2950. /* clear dma mappings for failed tx_buffer_info map */
  2951. for (;;) {
  2952. tx_buffer = &tx_ring->tx_buffer_info[i];
  2953. ixgbevf_unmap_and_free_tx_resource(tx_ring, tx_buffer);
  2954. if (tx_buffer == first)
  2955. break;
  2956. if (i == 0)
  2957. i = tx_ring->count;
  2958. i--;
  2959. }
  2960. tx_ring->next_to_use = i;
  2961. }
  2962. static int __ixgbevf_maybe_stop_tx(struct ixgbevf_ring *tx_ring, int size)
  2963. {
  2964. netif_stop_subqueue(tx_ring->netdev, tx_ring->queue_index);
  2965. /* Herbert's original patch had:
  2966. * smp_mb__after_netif_stop_queue();
  2967. * but since that doesn't exist yet, just open code it.
  2968. */
  2969. smp_mb();
  2970. /* We need to check again in a case another CPU has just
  2971. * made room available.
  2972. */
  2973. if (likely(ixgbevf_desc_unused(tx_ring) < size))
  2974. return -EBUSY;
  2975. /* A reprieve! - use start_queue because it doesn't call schedule */
  2976. netif_start_subqueue(tx_ring->netdev, tx_ring->queue_index);
  2977. ++tx_ring->tx_stats.restart_queue;
  2978. return 0;
  2979. }
  2980. static int ixgbevf_maybe_stop_tx(struct ixgbevf_ring *tx_ring, int size)
  2981. {
  2982. if (likely(ixgbevf_desc_unused(tx_ring) >= size))
  2983. return 0;
  2984. return __ixgbevf_maybe_stop_tx(tx_ring, size);
  2985. }
  2986. static int ixgbevf_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
  2987. {
  2988. struct ixgbevf_adapter *adapter = netdev_priv(netdev);
  2989. struct ixgbevf_tx_buffer *first;
  2990. struct ixgbevf_ring *tx_ring;
  2991. int tso;
  2992. u32 tx_flags = 0;
  2993. u16 count = TXD_USE_COUNT(skb_headlen(skb));
  2994. #if PAGE_SIZE > IXGBE_MAX_DATA_PER_TXD
  2995. unsigned short f;
  2996. #endif
  2997. u8 hdr_len = 0;
  2998. u8 *dst_mac = skb_header_pointer(skb, 0, 0, NULL);
  2999. if (!dst_mac || is_link_local_ether_addr(dst_mac)) {
  3000. dev_kfree_skb_any(skb);
  3001. return NETDEV_TX_OK;
  3002. }
  3003. tx_ring = adapter->tx_ring[skb->queue_mapping];
  3004. /* need: 1 descriptor per page * PAGE_SIZE/IXGBE_MAX_DATA_PER_TXD,
  3005. * + 1 desc for skb_headlen/IXGBE_MAX_DATA_PER_TXD,
  3006. * + 2 desc gap to keep tail from touching head,
  3007. * + 1 desc for context descriptor,
  3008. * otherwise try next time
  3009. */
  3010. #if PAGE_SIZE > IXGBE_MAX_DATA_PER_TXD
  3011. for (f = 0; f < skb_shinfo(skb)->nr_frags; f++)
  3012. count += TXD_USE_COUNT(skb_shinfo(skb)->frags[f].size);
  3013. #else
  3014. count += skb_shinfo(skb)->nr_frags;
  3015. #endif
  3016. if (ixgbevf_maybe_stop_tx(tx_ring, count + 3)) {
  3017. tx_ring->tx_stats.tx_busy++;
  3018. return NETDEV_TX_BUSY;
  3019. }
  3020. /* record the location of the first descriptor for this packet */
  3021. first = &tx_ring->tx_buffer_info[tx_ring->next_to_use];
  3022. first->skb = skb;
  3023. first->bytecount = skb->len;
  3024. first->gso_segs = 1;
  3025. if (skb_vlan_tag_present(skb)) {
  3026. tx_flags |= skb_vlan_tag_get(skb);
  3027. tx_flags <<= IXGBE_TX_FLAGS_VLAN_SHIFT;
  3028. tx_flags |= IXGBE_TX_FLAGS_VLAN;
  3029. }
  3030. /* record initial flags and protocol */
  3031. first->tx_flags = tx_flags;
  3032. first->protocol = vlan_get_protocol(skb);
  3033. tso = ixgbevf_tso(tx_ring, first, &hdr_len);
  3034. if (tso < 0)
  3035. goto out_drop;
  3036. else if (!tso)
  3037. ixgbevf_tx_csum(tx_ring, first);
  3038. ixgbevf_tx_map(tx_ring, first, hdr_len);
  3039. ixgbevf_maybe_stop_tx(tx_ring, DESC_NEEDED);
  3040. return NETDEV_TX_OK;
  3041. out_drop:
  3042. dev_kfree_skb_any(first->skb);
  3043. first->skb = NULL;
  3044. return NETDEV_TX_OK;
  3045. }
  3046. /**
  3047. * ixgbevf_set_mac - Change the Ethernet Address of the NIC
  3048. * @netdev: network interface device structure
  3049. * @p: pointer to an address structure
  3050. *
  3051. * Returns 0 on success, negative on failure
  3052. **/
  3053. static int ixgbevf_set_mac(struct net_device *netdev, void *p)
  3054. {
  3055. struct ixgbevf_adapter *adapter = netdev_priv(netdev);
  3056. struct ixgbe_hw *hw = &adapter->hw;
  3057. struct sockaddr *addr = p;
  3058. int err;
  3059. if (!is_valid_ether_addr(addr->sa_data))
  3060. return -EADDRNOTAVAIL;
  3061. spin_lock_bh(&adapter->mbx_lock);
  3062. err = hw->mac.ops.set_rar(hw, 0, addr->sa_data, 0);
  3063. spin_unlock_bh(&adapter->mbx_lock);
  3064. if (err)
  3065. return -EPERM;
  3066. ether_addr_copy(hw->mac.addr, addr->sa_data);
  3067. ether_addr_copy(hw->mac.perm_addr, addr->sa_data);
  3068. ether_addr_copy(netdev->dev_addr, addr->sa_data);
  3069. return 0;
  3070. }
  3071. /**
  3072. * ixgbevf_change_mtu - Change the Maximum Transfer Unit
  3073. * @netdev: network interface device structure
  3074. * @new_mtu: new value for maximum frame size
  3075. *
  3076. * Returns 0 on success, negative on failure
  3077. **/
  3078. static int ixgbevf_change_mtu(struct net_device *netdev, int new_mtu)
  3079. {
  3080. struct ixgbevf_adapter *adapter = netdev_priv(netdev);
  3081. struct ixgbe_hw *hw = &adapter->hw;
  3082. int max_frame = new_mtu + ETH_HLEN + ETH_FCS_LEN;
  3083. int max_possible_frame = MAXIMUM_ETHERNET_VLAN_SIZE;
  3084. int ret;
  3085. switch (adapter->hw.api_version) {
  3086. case ixgbe_mbox_api_11:
  3087. case ixgbe_mbox_api_12:
  3088. max_possible_frame = IXGBE_MAX_JUMBO_FRAME_SIZE;
  3089. break;
  3090. default:
  3091. if (adapter->hw.mac.type != ixgbe_mac_82599_vf)
  3092. max_possible_frame = IXGBE_MAX_JUMBO_FRAME_SIZE;
  3093. break;
  3094. }
  3095. /* MTU < 68 is an error and causes problems on some kernels */
  3096. if ((new_mtu < 68) || (max_frame > max_possible_frame))
  3097. return -EINVAL;
  3098. spin_lock_bh(&adapter->mbx_lock);
  3099. /* notify the PF of our intent to use this size of frame */
  3100. ret = hw->mac.ops.set_rlpml(hw, max_frame);
  3101. spin_unlock_bh(&adapter->mbx_lock);
  3102. if (ret)
  3103. return -EINVAL;
  3104. hw_dbg(hw, "changing MTU from %d to %d\n",
  3105. netdev->mtu, new_mtu);
  3106. /* must set new MTU before calling down or up */
  3107. netdev->mtu = new_mtu;
  3108. return 0;
  3109. }
  3110. #ifdef CONFIG_NET_POLL_CONTROLLER
  3111. /* Polling 'interrupt' - used by things like netconsole to send skbs
  3112. * without having to re-enable interrupts. It's not called while
  3113. * the interrupt routine is executing.
  3114. */
  3115. static void ixgbevf_netpoll(struct net_device *netdev)
  3116. {
  3117. struct ixgbevf_adapter *adapter = netdev_priv(netdev);
  3118. int i;
  3119. /* if interface is down do nothing */
  3120. if (test_bit(__IXGBEVF_DOWN, &adapter->state))
  3121. return;
  3122. for (i = 0; i < adapter->num_rx_queues; i++)
  3123. ixgbevf_msix_clean_rings(0, adapter->q_vector[i]);
  3124. }
  3125. #endif /* CONFIG_NET_POLL_CONTROLLER */
  3126. static int ixgbevf_suspend(struct pci_dev *pdev, pm_message_t state)
  3127. {
  3128. struct net_device *netdev = pci_get_drvdata(pdev);
  3129. struct ixgbevf_adapter *adapter = netdev_priv(netdev);
  3130. #ifdef CONFIG_PM
  3131. int retval = 0;
  3132. #endif
  3133. netif_device_detach(netdev);
  3134. if (netif_running(netdev)) {
  3135. rtnl_lock();
  3136. ixgbevf_down(adapter);
  3137. ixgbevf_free_irq(adapter);
  3138. ixgbevf_free_all_tx_resources(adapter);
  3139. ixgbevf_free_all_rx_resources(adapter);
  3140. rtnl_unlock();
  3141. }
  3142. ixgbevf_clear_interrupt_scheme(adapter);
  3143. #ifdef CONFIG_PM
  3144. retval = pci_save_state(pdev);
  3145. if (retval)
  3146. return retval;
  3147. #endif
  3148. if (!test_and_set_bit(__IXGBEVF_DISABLED, &adapter->state))
  3149. pci_disable_device(pdev);
  3150. return 0;
  3151. }
  3152. #ifdef CONFIG_PM
  3153. static int ixgbevf_resume(struct pci_dev *pdev)
  3154. {
  3155. struct net_device *netdev = pci_get_drvdata(pdev);
  3156. struct ixgbevf_adapter *adapter = netdev_priv(netdev);
  3157. u32 err;
  3158. pci_restore_state(pdev);
  3159. /* pci_restore_state clears dev->state_saved so call
  3160. * pci_save_state to restore it.
  3161. */
  3162. pci_save_state(pdev);
  3163. err = pci_enable_device_mem(pdev);
  3164. if (err) {
  3165. dev_err(&pdev->dev, "Cannot enable PCI device from suspend\n");
  3166. return err;
  3167. }
  3168. smp_mb__before_atomic();
  3169. clear_bit(__IXGBEVF_DISABLED, &adapter->state);
  3170. pci_set_master(pdev);
  3171. ixgbevf_reset(adapter);
  3172. rtnl_lock();
  3173. err = ixgbevf_init_interrupt_scheme(adapter);
  3174. rtnl_unlock();
  3175. if (err) {
  3176. dev_err(&pdev->dev, "Cannot initialize interrupts\n");
  3177. return err;
  3178. }
  3179. if (netif_running(netdev)) {
  3180. err = ixgbevf_open(netdev);
  3181. if (err)
  3182. return err;
  3183. }
  3184. netif_device_attach(netdev);
  3185. return err;
  3186. }
  3187. #endif /* CONFIG_PM */
  3188. static void ixgbevf_shutdown(struct pci_dev *pdev)
  3189. {
  3190. ixgbevf_suspend(pdev, PMSG_SUSPEND);
  3191. }
  3192. static struct rtnl_link_stats64 *ixgbevf_get_stats(struct net_device *netdev,
  3193. struct rtnl_link_stats64 *stats)
  3194. {
  3195. struct ixgbevf_adapter *adapter = netdev_priv(netdev);
  3196. unsigned int start;
  3197. u64 bytes, packets;
  3198. const struct ixgbevf_ring *ring;
  3199. int i;
  3200. ixgbevf_update_stats(adapter);
  3201. stats->multicast = adapter->stats.vfmprc - adapter->stats.base_vfmprc;
  3202. for (i = 0; i < adapter->num_rx_queues; i++) {
  3203. ring = adapter->rx_ring[i];
  3204. do {
  3205. start = u64_stats_fetch_begin_irq(&ring->syncp);
  3206. bytes = ring->stats.bytes;
  3207. packets = ring->stats.packets;
  3208. } while (u64_stats_fetch_retry_irq(&ring->syncp, start));
  3209. stats->rx_bytes += bytes;
  3210. stats->rx_packets += packets;
  3211. }
  3212. for (i = 0; i < adapter->num_tx_queues; i++) {
  3213. ring = adapter->tx_ring[i];
  3214. do {
  3215. start = u64_stats_fetch_begin_irq(&ring->syncp);
  3216. bytes = ring->stats.bytes;
  3217. packets = ring->stats.packets;
  3218. } while (u64_stats_fetch_retry_irq(&ring->syncp, start));
  3219. stats->tx_bytes += bytes;
  3220. stats->tx_packets += packets;
  3221. }
  3222. return stats;
  3223. }
  3224. #define IXGBEVF_MAX_MAC_HDR_LEN 127
  3225. #define IXGBEVF_MAX_NETWORK_HDR_LEN 511
  3226. static netdev_features_t
  3227. ixgbevf_features_check(struct sk_buff *skb, struct net_device *dev,
  3228. netdev_features_t features)
  3229. {
  3230. unsigned int network_hdr_len, mac_hdr_len;
  3231. /* Make certain the headers can be described by a context descriptor */
  3232. mac_hdr_len = skb_network_header(skb) - skb->data;
  3233. if (unlikely(mac_hdr_len > IXGBEVF_MAX_MAC_HDR_LEN))
  3234. return features & ~(NETIF_F_HW_CSUM |
  3235. NETIF_F_SCTP_CRC |
  3236. NETIF_F_HW_VLAN_CTAG_TX |
  3237. NETIF_F_TSO |
  3238. NETIF_F_TSO6);
  3239. network_hdr_len = skb_checksum_start(skb) - skb_network_header(skb);
  3240. if (unlikely(network_hdr_len > IXGBEVF_MAX_NETWORK_HDR_LEN))
  3241. return features & ~(NETIF_F_HW_CSUM |
  3242. NETIF_F_SCTP_CRC |
  3243. NETIF_F_TSO |
  3244. NETIF_F_TSO6);
  3245. /* We can only support IPV4 TSO in tunnels if we can mangle the
  3246. * inner IP ID field, so strip TSO if MANGLEID is not supported.
  3247. */
  3248. if (skb->encapsulation && !(features & NETIF_F_TSO_MANGLEID))
  3249. features &= ~NETIF_F_TSO;
  3250. return features;
  3251. }
  3252. static const struct net_device_ops ixgbevf_netdev_ops = {
  3253. .ndo_open = ixgbevf_open,
  3254. .ndo_stop = ixgbevf_close,
  3255. .ndo_start_xmit = ixgbevf_xmit_frame,
  3256. .ndo_set_rx_mode = ixgbevf_set_rx_mode,
  3257. .ndo_get_stats64 = ixgbevf_get_stats,
  3258. .ndo_validate_addr = eth_validate_addr,
  3259. .ndo_set_mac_address = ixgbevf_set_mac,
  3260. .ndo_change_mtu = ixgbevf_change_mtu,
  3261. .ndo_tx_timeout = ixgbevf_tx_timeout,
  3262. .ndo_vlan_rx_add_vid = ixgbevf_vlan_rx_add_vid,
  3263. .ndo_vlan_rx_kill_vid = ixgbevf_vlan_rx_kill_vid,
  3264. #ifdef CONFIG_NET_RX_BUSY_POLL
  3265. .ndo_busy_poll = ixgbevf_busy_poll_recv,
  3266. #endif
  3267. #ifdef CONFIG_NET_POLL_CONTROLLER
  3268. .ndo_poll_controller = ixgbevf_netpoll,
  3269. #endif
  3270. .ndo_features_check = ixgbevf_features_check,
  3271. };
  3272. static void ixgbevf_assign_netdev_ops(struct net_device *dev)
  3273. {
  3274. dev->netdev_ops = &ixgbevf_netdev_ops;
  3275. ixgbevf_set_ethtool_ops(dev);
  3276. dev->watchdog_timeo = 5 * HZ;
  3277. }
  3278. /**
  3279. * ixgbevf_probe - Device Initialization Routine
  3280. * @pdev: PCI device information struct
  3281. * @ent: entry in ixgbevf_pci_tbl
  3282. *
  3283. * Returns 0 on success, negative on failure
  3284. *
  3285. * ixgbevf_probe initializes an adapter identified by a pci_dev structure.
  3286. * The OS initialization, configuring of the adapter private structure,
  3287. * and a hardware reset occur.
  3288. **/
  3289. static int ixgbevf_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
  3290. {
  3291. struct net_device *netdev;
  3292. struct ixgbevf_adapter *adapter = NULL;
  3293. struct ixgbe_hw *hw = NULL;
  3294. const struct ixgbevf_info *ii = ixgbevf_info_tbl[ent->driver_data];
  3295. int err, pci_using_dac;
  3296. bool disable_dev = false;
  3297. err = pci_enable_device(pdev);
  3298. if (err)
  3299. return err;
  3300. if (!dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64))) {
  3301. pci_using_dac = 1;
  3302. } else {
  3303. err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
  3304. if (err) {
  3305. dev_err(&pdev->dev, "No usable DMA configuration, aborting\n");
  3306. goto err_dma;
  3307. }
  3308. pci_using_dac = 0;
  3309. }
  3310. err = pci_request_regions(pdev, ixgbevf_driver_name);
  3311. if (err) {
  3312. dev_err(&pdev->dev, "pci_request_regions failed 0x%x\n", err);
  3313. goto err_pci_reg;
  3314. }
  3315. pci_set_master(pdev);
  3316. netdev = alloc_etherdev_mq(sizeof(struct ixgbevf_adapter),
  3317. MAX_TX_QUEUES);
  3318. if (!netdev) {
  3319. err = -ENOMEM;
  3320. goto err_alloc_etherdev;
  3321. }
  3322. SET_NETDEV_DEV(netdev, &pdev->dev);
  3323. adapter = netdev_priv(netdev);
  3324. adapter->netdev = netdev;
  3325. adapter->pdev = pdev;
  3326. hw = &adapter->hw;
  3327. hw->back = adapter;
  3328. adapter->msg_enable = netif_msg_init(debug, DEFAULT_MSG_ENABLE);
  3329. /* call save state here in standalone driver because it relies on
  3330. * adapter struct to exist, and needs to call netdev_priv
  3331. */
  3332. pci_save_state(pdev);
  3333. hw->hw_addr = ioremap(pci_resource_start(pdev, 0),
  3334. pci_resource_len(pdev, 0));
  3335. adapter->io_addr = hw->hw_addr;
  3336. if (!hw->hw_addr) {
  3337. err = -EIO;
  3338. goto err_ioremap;
  3339. }
  3340. ixgbevf_assign_netdev_ops(netdev);
  3341. /* Setup HW API */
  3342. memcpy(&hw->mac.ops, ii->mac_ops, sizeof(hw->mac.ops));
  3343. hw->mac.type = ii->mac;
  3344. memcpy(&hw->mbx.ops, &ixgbevf_mbx_ops,
  3345. sizeof(struct ixgbe_mbx_operations));
  3346. /* setup the private structure */
  3347. err = ixgbevf_sw_init(adapter);
  3348. if (err)
  3349. goto err_sw_init;
  3350. /* The HW MAC address was set and/or determined in sw_init */
  3351. if (!is_valid_ether_addr(netdev->dev_addr)) {
  3352. pr_err("invalid MAC address\n");
  3353. err = -EIO;
  3354. goto err_sw_init;
  3355. }
  3356. netdev->hw_features = NETIF_F_SG |
  3357. NETIF_F_TSO |
  3358. NETIF_F_TSO6 |
  3359. NETIF_F_RXCSUM |
  3360. NETIF_F_HW_CSUM |
  3361. NETIF_F_SCTP_CRC;
  3362. #define IXGBEVF_GSO_PARTIAL_FEATURES (NETIF_F_GSO_GRE | \
  3363. NETIF_F_GSO_GRE_CSUM | \
  3364. NETIF_F_GSO_IPXIP4 | \
  3365. NETIF_F_GSO_IPXIP6 | \
  3366. NETIF_F_GSO_UDP_TUNNEL | \
  3367. NETIF_F_GSO_UDP_TUNNEL_CSUM)
  3368. netdev->gso_partial_features = IXGBEVF_GSO_PARTIAL_FEATURES;
  3369. netdev->hw_features |= NETIF_F_GSO_PARTIAL |
  3370. IXGBEVF_GSO_PARTIAL_FEATURES;
  3371. netdev->features = netdev->hw_features;
  3372. if (pci_using_dac)
  3373. netdev->features |= NETIF_F_HIGHDMA;
  3374. netdev->vlan_features |= netdev->features | NETIF_F_TSO_MANGLEID;
  3375. netdev->mpls_features |= NETIF_F_HW_CSUM;
  3376. netdev->hw_enc_features |= netdev->vlan_features;
  3377. /* set this bit last since it cannot be part of vlan_features */
  3378. netdev->features |= NETIF_F_HW_VLAN_CTAG_FILTER |
  3379. NETIF_F_HW_VLAN_CTAG_RX |
  3380. NETIF_F_HW_VLAN_CTAG_TX;
  3381. netdev->priv_flags |= IFF_UNICAST_FLT;
  3382. if (IXGBE_REMOVED(hw->hw_addr)) {
  3383. err = -EIO;
  3384. goto err_sw_init;
  3385. }
  3386. setup_timer(&adapter->service_timer, &ixgbevf_service_timer,
  3387. (unsigned long)adapter);
  3388. INIT_WORK(&adapter->service_task, ixgbevf_service_task);
  3389. set_bit(__IXGBEVF_SERVICE_INITED, &adapter->state);
  3390. clear_bit(__IXGBEVF_SERVICE_SCHED, &adapter->state);
  3391. err = ixgbevf_init_interrupt_scheme(adapter);
  3392. if (err)
  3393. goto err_sw_init;
  3394. strcpy(netdev->name, "eth%d");
  3395. err = register_netdev(netdev);
  3396. if (err)
  3397. goto err_register;
  3398. pci_set_drvdata(pdev, netdev);
  3399. netif_carrier_off(netdev);
  3400. ixgbevf_init_last_counter_stats(adapter);
  3401. /* print the VF info */
  3402. dev_info(&pdev->dev, "%pM\n", netdev->dev_addr);
  3403. dev_info(&pdev->dev, "MAC: %d\n", hw->mac.type);
  3404. switch (hw->mac.type) {
  3405. case ixgbe_mac_X550_vf:
  3406. dev_info(&pdev->dev, "Intel(R) X550 Virtual Function\n");
  3407. break;
  3408. case ixgbe_mac_X540_vf:
  3409. dev_info(&pdev->dev, "Intel(R) X540 Virtual Function\n");
  3410. break;
  3411. case ixgbe_mac_82599_vf:
  3412. default:
  3413. dev_info(&pdev->dev, "Intel(R) 82599 Virtual Function\n");
  3414. break;
  3415. }
  3416. return 0;
  3417. err_register:
  3418. ixgbevf_clear_interrupt_scheme(adapter);
  3419. err_sw_init:
  3420. ixgbevf_reset_interrupt_capability(adapter);
  3421. iounmap(adapter->io_addr);
  3422. err_ioremap:
  3423. disable_dev = !test_and_set_bit(__IXGBEVF_DISABLED, &adapter->state);
  3424. free_netdev(netdev);
  3425. err_alloc_etherdev:
  3426. pci_release_regions(pdev);
  3427. err_pci_reg:
  3428. err_dma:
  3429. if (!adapter || disable_dev)
  3430. pci_disable_device(pdev);
  3431. return err;
  3432. }
  3433. /**
  3434. * ixgbevf_remove - Device Removal Routine
  3435. * @pdev: PCI device information struct
  3436. *
  3437. * ixgbevf_remove is called by the PCI subsystem to alert the driver
  3438. * that it should release a PCI device. The could be caused by a
  3439. * Hot-Plug event, or because the driver is going to be removed from
  3440. * memory.
  3441. **/
  3442. static void ixgbevf_remove(struct pci_dev *pdev)
  3443. {
  3444. struct net_device *netdev = pci_get_drvdata(pdev);
  3445. struct ixgbevf_adapter *adapter;
  3446. bool disable_dev;
  3447. if (!netdev)
  3448. return;
  3449. adapter = netdev_priv(netdev);
  3450. set_bit(__IXGBEVF_REMOVING, &adapter->state);
  3451. cancel_work_sync(&adapter->service_task);
  3452. if (netdev->reg_state == NETREG_REGISTERED)
  3453. unregister_netdev(netdev);
  3454. ixgbevf_clear_interrupt_scheme(adapter);
  3455. ixgbevf_reset_interrupt_capability(adapter);
  3456. iounmap(adapter->io_addr);
  3457. pci_release_regions(pdev);
  3458. hw_dbg(&adapter->hw, "Remove complete\n");
  3459. disable_dev = !test_and_set_bit(__IXGBEVF_DISABLED, &adapter->state);
  3460. free_netdev(netdev);
  3461. if (disable_dev)
  3462. pci_disable_device(pdev);
  3463. }
  3464. /**
  3465. * ixgbevf_io_error_detected - called when PCI error is detected
  3466. * @pdev: Pointer to PCI device
  3467. * @state: The current pci connection state
  3468. *
  3469. * This function is called after a PCI bus error affecting
  3470. * this device has been detected.
  3471. **/
  3472. static pci_ers_result_t ixgbevf_io_error_detected(struct pci_dev *pdev,
  3473. pci_channel_state_t state)
  3474. {
  3475. struct net_device *netdev = pci_get_drvdata(pdev);
  3476. struct ixgbevf_adapter *adapter = netdev_priv(netdev);
  3477. if (!test_bit(__IXGBEVF_SERVICE_INITED, &adapter->state))
  3478. return PCI_ERS_RESULT_DISCONNECT;
  3479. rtnl_lock();
  3480. netif_device_detach(netdev);
  3481. if (state == pci_channel_io_perm_failure) {
  3482. rtnl_unlock();
  3483. return PCI_ERS_RESULT_DISCONNECT;
  3484. }
  3485. if (netif_running(netdev))
  3486. ixgbevf_down(adapter);
  3487. if (!test_and_set_bit(__IXGBEVF_DISABLED, &adapter->state))
  3488. pci_disable_device(pdev);
  3489. rtnl_unlock();
  3490. /* Request a slot slot reset. */
  3491. return PCI_ERS_RESULT_NEED_RESET;
  3492. }
  3493. /**
  3494. * ixgbevf_io_slot_reset - called after the pci bus has been reset.
  3495. * @pdev: Pointer to PCI device
  3496. *
  3497. * Restart the card from scratch, as if from a cold-boot. Implementation
  3498. * resembles the first-half of the ixgbevf_resume routine.
  3499. **/
  3500. static pci_ers_result_t ixgbevf_io_slot_reset(struct pci_dev *pdev)
  3501. {
  3502. struct net_device *netdev = pci_get_drvdata(pdev);
  3503. struct ixgbevf_adapter *adapter = netdev_priv(netdev);
  3504. if (pci_enable_device_mem(pdev)) {
  3505. dev_err(&pdev->dev,
  3506. "Cannot re-enable PCI device after reset.\n");
  3507. return PCI_ERS_RESULT_DISCONNECT;
  3508. }
  3509. smp_mb__before_atomic();
  3510. clear_bit(__IXGBEVF_DISABLED, &adapter->state);
  3511. pci_set_master(pdev);
  3512. ixgbevf_reset(adapter);
  3513. return PCI_ERS_RESULT_RECOVERED;
  3514. }
  3515. /**
  3516. * ixgbevf_io_resume - called when traffic can start flowing again.
  3517. * @pdev: Pointer to PCI device
  3518. *
  3519. * This callback is called when the error recovery driver tells us that
  3520. * its OK to resume normal operation. Implementation resembles the
  3521. * second-half of the ixgbevf_resume routine.
  3522. **/
  3523. static void ixgbevf_io_resume(struct pci_dev *pdev)
  3524. {
  3525. struct net_device *netdev = pci_get_drvdata(pdev);
  3526. struct ixgbevf_adapter *adapter = netdev_priv(netdev);
  3527. if (netif_running(netdev))
  3528. ixgbevf_up(adapter);
  3529. netif_device_attach(netdev);
  3530. }
  3531. /* PCI Error Recovery (ERS) */
  3532. static const struct pci_error_handlers ixgbevf_err_handler = {
  3533. .error_detected = ixgbevf_io_error_detected,
  3534. .slot_reset = ixgbevf_io_slot_reset,
  3535. .resume = ixgbevf_io_resume,
  3536. };
  3537. static struct pci_driver ixgbevf_driver = {
  3538. .name = ixgbevf_driver_name,
  3539. .id_table = ixgbevf_pci_tbl,
  3540. .probe = ixgbevf_probe,
  3541. .remove = ixgbevf_remove,
  3542. #ifdef CONFIG_PM
  3543. /* Power Management Hooks */
  3544. .suspend = ixgbevf_suspend,
  3545. .resume = ixgbevf_resume,
  3546. #endif
  3547. .shutdown = ixgbevf_shutdown,
  3548. .err_handler = &ixgbevf_err_handler
  3549. };
  3550. /**
  3551. * ixgbevf_init_module - Driver Registration Routine
  3552. *
  3553. * ixgbevf_init_module is the first routine called when the driver is
  3554. * loaded. All it does is register with the PCI subsystem.
  3555. **/
  3556. static int __init ixgbevf_init_module(void)
  3557. {
  3558. pr_info("%s - version %s\n", ixgbevf_driver_string,
  3559. ixgbevf_driver_version);
  3560. pr_info("%s\n", ixgbevf_copyright);
  3561. ixgbevf_wq = create_singlethread_workqueue(ixgbevf_driver_name);
  3562. if (!ixgbevf_wq) {
  3563. pr_err("%s: Failed to create workqueue\n", ixgbevf_driver_name);
  3564. return -ENOMEM;
  3565. }
  3566. return pci_register_driver(&ixgbevf_driver);
  3567. }
  3568. module_init(ixgbevf_init_module);
  3569. /**
  3570. * ixgbevf_exit_module - Driver Exit Cleanup Routine
  3571. *
  3572. * ixgbevf_exit_module is called just before the driver is removed
  3573. * from memory.
  3574. **/
  3575. static void __exit ixgbevf_exit_module(void)
  3576. {
  3577. pci_unregister_driver(&ixgbevf_driver);
  3578. if (ixgbevf_wq) {
  3579. destroy_workqueue(ixgbevf_wq);
  3580. ixgbevf_wq = NULL;
  3581. }
  3582. }
  3583. #ifdef DEBUG
  3584. /**
  3585. * ixgbevf_get_hw_dev_name - return device name string
  3586. * used by hardware layer to print debugging information
  3587. **/
  3588. char *ixgbevf_get_hw_dev_name(struct ixgbe_hw *hw)
  3589. {
  3590. struct ixgbevf_adapter *adapter = hw->back;
  3591. return adapter->netdev->name;
  3592. }
  3593. #endif
  3594. module_exit(ixgbevf_exit_module);
  3595. /* ixgbevf_main.c */