igb_main.c 158 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783
  1. /*******************************************************************************
  2. Intel(R) Gigabit Ethernet Linux driver
  3. Copyright(c) 2007-2009 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, write to the Free Software Foundation, Inc.,
  13. 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  14. The full GNU General Public License is included in this distribution in
  15. the file called "COPYING".
  16. Contact Information:
  17. e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
  18. Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  19. *******************************************************************************/
  20. #include <linux/module.h>
  21. #include <linux/types.h>
  22. #include <linux/init.h>
  23. #include <linux/vmalloc.h>
  24. #include <linux/pagemap.h>
  25. #include <linux/netdevice.h>
  26. #include <linux/ipv6.h>
  27. #include <net/checksum.h>
  28. #include <net/ip6_checksum.h>
  29. #include <linux/net_tstamp.h>
  30. #include <linux/mii.h>
  31. #include <linux/ethtool.h>
  32. #include <linux/if_vlan.h>
  33. #include <linux/pci.h>
  34. #include <linux/pci-aspm.h>
  35. #include <linux/delay.h>
  36. #include <linux/interrupt.h>
  37. #include <linux/if_ether.h>
  38. #include <linux/aer.h>
  39. #ifdef CONFIG_IGB_DCA
  40. #include <linux/dca.h>
  41. #endif
  42. #include "igb.h"
  43. #define DRV_VERSION "1.3.16-k2"
  44. char igb_driver_name[] = "igb";
  45. char igb_driver_version[] = DRV_VERSION;
  46. static const char igb_driver_string[] =
  47. "Intel(R) Gigabit Ethernet Network Driver";
  48. static const char igb_copyright[] = "Copyright (c) 2007-2009 Intel Corporation.";
  49. static const struct e1000_info *igb_info_tbl[] = {
  50. [board_82575] = &e1000_82575_info,
  51. };
  52. static struct pci_device_id igb_pci_tbl[] = {
  53. { PCI_VDEVICE(INTEL, E1000_DEV_ID_82576), board_82575 },
  54. { PCI_VDEVICE(INTEL, E1000_DEV_ID_82576_NS), board_82575 },
  55. { PCI_VDEVICE(INTEL, E1000_DEV_ID_82576_NS_SERDES), board_82575 },
  56. { PCI_VDEVICE(INTEL, E1000_DEV_ID_82576_FIBER), board_82575 },
  57. { PCI_VDEVICE(INTEL, E1000_DEV_ID_82576_SERDES), board_82575 },
  58. { PCI_VDEVICE(INTEL, E1000_DEV_ID_82576_SERDES_QUAD), board_82575 },
  59. { PCI_VDEVICE(INTEL, E1000_DEV_ID_82576_QUAD_COPPER), board_82575 },
  60. { PCI_VDEVICE(INTEL, E1000_DEV_ID_82575EB_COPPER), board_82575 },
  61. { PCI_VDEVICE(INTEL, E1000_DEV_ID_82575EB_FIBER_SERDES), board_82575 },
  62. { PCI_VDEVICE(INTEL, E1000_DEV_ID_82575GB_QUAD_COPPER), board_82575 },
  63. /* required last entry */
  64. {0, }
  65. };
  66. MODULE_DEVICE_TABLE(pci, igb_pci_tbl);
  67. void igb_reset(struct igb_adapter *);
  68. static int igb_setup_all_tx_resources(struct igb_adapter *);
  69. static int igb_setup_all_rx_resources(struct igb_adapter *);
  70. static void igb_free_all_tx_resources(struct igb_adapter *);
  71. static void igb_free_all_rx_resources(struct igb_adapter *);
  72. static void igb_setup_mrqc(struct igb_adapter *);
  73. void igb_update_stats(struct igb_adapter *);
  74. static int igb_probe(struct pci_dev *, const struct pci_device_id *);
  75. static void __devexit igb_remove(struct pci_dev *pdev);
  76. static int igb_sw_init(struct igb_adapter *);
  77. static int igb_open(struct net_device *);
  78. static int igb_close(struct net_device *);
  79. static void igb_configure_tx(struct igb_adapter *);
  80. static void igb_configure_rx(struct igb_adapter *);
  81. static void igb_clean_all_tx_rings(struct igb_adapter *);
  82. static void igb_clean_all_rx_rings(struct igb_adapter *);
  83. static void igb_clean_tx_ring(struct igb_ring *);
  84. static void igb_clean_rx_ring(struct igb_ring *);
  85. static void igb_set_rx_mode(struct net_device *);
  86. static void igb_update_phy_info(unsigned long);
  87. static void igb_watchdog(unsigned long);
  88. static void igb_watchdog_task(struct work_struct *);
  89. static netdev_tx_t igb_xmit_frame_adv(struct sk_buff *skb, struct net_device *);
  90. static struct net_device_stats *igb_get_stats(struct net_device *);
  91. static int igb_change_mtu(struct net_device *, int);
  92. static int igb_set_mac(struct net_device *, void *);
  93. static void igb_set_uta(struct igb_adapter *adapter);
  94. static irqreturn_t igb_intr(int irq, void *);
  95. static irqreturn_t igb_intr_msi(int irq, void *);
  96. static irqreturn_t igb_msix_other(int irq, void *);
  97. static irqreturn_t igb_msix_ring(int irq, void *);
  98. #ifdef CONFIG_IGB_DCA
  99. static void igb_update_dca(struct igb_q_vector *);
  100. static void igb_setup_dca(struct igb_adapter *);
  101. #endif /* CONFIG_IGB_DCA */
  102. static bool igb_clean_tx_irq(struct igb_q_vector *);
  103. static int igb_poll(struct napi_struct *, int);
  104. static bool igb_clean_rx_irq_adv(struct igb_q_vector *, int *, int);
  105. static int igb_ioctl(struct net_device *, struct ifreq *, int cmd);
  106. static void igb_tx_timeout(struct net_device *);
  107. static void igb_reset_task(struct work_struct *);
  108. static void igb_vlan_rx_register(struct net_device *, struct vlan_group *);
  109. static void igb_vlan_rx_add_vid(struct net_device *, u16);
  110. static void igb_vlan_rx_kill_vid(struct net_device *, u16);
  111. static void igb_restore_vlan(struct igb_adapter *);
  112. static void igb_rar_set_qsel(struct igb_adapter *, u8 *, u32 , u8);
  113. static void igb_ping_all_vfs(struct igb_adapter *);
  114. static void igb_msg_task(struct igb_adapter *);
  115. static int igb_rcv_msg_from_vf(struct igb_adapter *, u32);
  116. static void igb_vmm_control(struct igb_adapter *);
  117. static int igb_set_vf_mac(struct igb_adapter *adapter, int, unsigned char *);
  118. static void igb_restore_vf_multicasts(struct igb_adapter *adapter);
  119. static inline void igb_set_vmolr(struct e1000_hw *hw, int vfn)
  120. {
  121. u32 reg_data;
  122. reg_data = rd32(E1000_VMOLR(vfn));
  123. reg_data |= E1000_VMOLR_BAM | /* Accept broadcast */
  124. E1000_VMOLR_ROMPE | /* Accept packets matched in MTA */
  125. E1000_VMOLR_AUPE | /* Accept untagged packets */
  126. E1000_VMOLR_STRVLAN; /* Strip vlan tags */
  127. wr32(E1000_VMOLR(vfn), reg_data);
  128. }
  129. static inline int igb_set_vf_rlpml(struct igb_adapter *adapter, int size,
  130. int vfn)
  131. {
  132. struct e1000_hw *hw = &adapter->hw;
  133. u32 vmolr;
  134. /* if it isn't the PF check to see if VFs are enabled and
  135. * increase the size to support vlan tags */
  136. if (vfn < adapter->vfs_allocated_count &&
  137. adapter->vf_data[vfn].vlans_enabled)
  138. size += VLAN_TAG_SIZE;
  139. vmolr = rd32(E1000_VMOLR(vfn));
  140. vmolr &= ~E1000_VMOLR_RLPML_MASK;
  141. vmolr |= size | E1000_VMOLR_LPE;
  142. wr32(E1000_VMOLR(vfn), vmolr);
  143. return 0;
  144. }
  145. #ifdef CONFIG_PM
  146. static int igb_suspend(struct pci_dev *, pm_message_t);
  147. static int igb_resume(struct pci_dev *);
  148. #endif
  149. static void igb_shutdown(struct pci_dev *);
  150. #ifdef CONFIG_IGB_DCA
  151. static int igb_notify_dca(struct notifier_block *, unsigned long, void *);
  152. static struct notifier_block dca_notifier = {
  153. .notifier_call = igb_notify_dca,
  154. .next = NULL,
  155. .priority = 0
  156. };
  157. #endif
  158. #ifdef CONFIG_NET_POLL_CONTROLLER
  159. /* for netdump / net console */
  160. static void igb_netpoll(struct net_device *);
  161. #endif
  162. #ifdef CONFIG_PCI_IOV
  163. static unsigned int max_vfs = 0;
  164. module_param(max_vfs, uint, 0);
  165. MODULE_PARM_DESC(max_vfs, "Maximum number of virtual functions to allocate "
  166. "per physical function");
  167. #endif /* CONFIG_PCI_IOV */
  168. static pci_ers_result_t igb_io_error_detected(struct pci_dev *,
  169. pci_channel_state_t);
  170. static pci_ers_result_t igb_io_slot_reset(struct pci_dev *);
  171. static void igb_io_resume(struct pci_dev *);
  172. static struct pci_error_handlers igb_err_handler = {
  173. .error_detected = igb_io_error_detected,
  174. .slot_reset = igb_io_slot_reset,
  175. .resume = igb_io_resume,
  176. };
  177. static struct pci_driver igb_driver = {
  178. .name = igb_driver_name,
  179. .id_table = igb_pci_tbl,
  180. .probe = igb_probe,
  181. .remove = __devexit_p(igb_remove),
  182. #ifdef CONFIG_PM
  183. /* Power Managment Hooks */
  184. .suspend = igb_suspend,
  185. .resume = igb_resume,
  186. #endif
  187. .shutdown = igb_shutdown,
  188. .err_handler = &igb_err_handler
  189. };
  190. static int global_quad_port_a; /* global quad port a indication */
  191. MODULE_AUTHOR("Intel Corporation, <e1000-devel@lists.sourceforge.net>");
  192. MODULE_DESCRIPTION("Intel(R) Gigabit Ethernet Network Driver");
  193. MODULE_LICENSE("GPL");
  194. MODULE_VERSION(DRV_VERSION);
  195. /**
  196. * igb_read_clock - read raw cycle counter (to be used by time counter)
  197. */
  198. static cycle_t igb_read_clock(const struct cyclecounter *tc)
  199. {
  200. struct igb_adapter *adapter =
  201. container_of(tc, struct igb_adapter, cycles);
  202. struct e1000_hw *hw = &adapter->hw;
  203. u64 stamp = 0;
  204. int shift = 0;
  205. stamp |= (u64)rd32(E1000_SYSTIML) << shift;
  206. stamp |= (u64)rd32(E1000_SYSTIMH) << (shift + 32);
  207. return stamp;
  208. }
  209. #ifdef DEBUG
  210. /**
  211. * igb_get_hw_dev_name - return device name string
  212. * used by hardware layer to print debugging information
  213. **/
  214. char *igb_get_hw_dev_name(struct e1000_hw *hw)
  215. {
  216. struct igb_adapter *adapter = hw->back;
  217. return adapter->netdev->name;
  218. }
  219. /**
  220. * igb_get_time_str - format current NIC and system time as string
  221. */
  222. static char *igb_get_time_str(struct igb_adapter *adapter,
  223. char buffer[160])
  224. {
  225. cycle_t hw = adapter->cycles.read(&adapter->cycles);
  226. struct timespec nic = ns_to_timespec(timecounter_read(&adapter->clock));
  227. struct timespec sys;
  228. struct timespec delta;
  229. getnstimeofday(&sys);
  230. delta = timespec_sub(nic, sys);
  231. sprintf(buffer,
  232. "HW %llu, NIC %ld.%09lus, SYS %ld.%09lus, NIC-SYS %lds + %09luns",
  233. hw,
  234. (long)nic.tv_sec, nic.tv_nsec,
  235. (long)sys.tv_sec, sys.tv_nsec,
  236. (long)delta.tv_sec, delta.tv_nsec);
  237. return buffer;
  238. }
  239. #endif
  240. /**
  241. * igb_init_module - Driver Registration Routine
  242. *
  243. * igb_init_module is the first routine called when the driver is
  244. * loaded. All it does is register with the PCI subsystem.
  245. **/
  246. static int __init igb_init_module(void)
  247. {
  248. int ret;
  249. printk(KERN_INFO "%s - version %s\n",
  250. igb_driver_string, igb_driver_version);
  251. printk(KERN_INFO "%s\n", igb_copyright);
  252. global_quad_port_a = 0;
  253. #ifdef CONFIG_IGB_DCA
  254. dca_register_notify(&dca_notifier);
  255. #endif
  256. ret = pci_register_driver(&igb_driver);
  257. return ret;
  258. }
  259. module_init(igb_init_module);
  260. /**
  261. * igb_exit_module - Driver Exit Cleanup Routine
  262. *
  263. * igb_exit_module is called just before the driver is removed
  264. * from memory.
  265. **/
  266. static void __exit igb_exit_module(void)
  267. {
  268. #ifdef CONFIG_IGB_DCA
  269. dca_unregister_notify(&dca_notifier);
  270. #endif
  271. pci_unregister_driver(&igb_driver);
  272. }
  273. module_exit(igb_exit_module);
  274. #define Q_IDX_82576(i) (((i & 0x1) << 3) + (i >> 1))
  275. /**
  276. * igb_cache_ring_register - Descriptor ring to register mapping
  277. * @adapter: board private structure to initialize
  278. *
  279. * Once we know the feature-set enabled for the device, we'll cache
  280. * the register offset the descriptor ring is assigned to.
  281. **/
  282. static void igb_cache_ring_register(struct igb_adapter *adapter)
  283. {
  284. int i;
  285. u32 rbase_offset = adapter->vfs_allocated_count;
  286. switch (adapter->hw.mac.type) {
  287. case e1000_82576:
  288. /* The queues are allocated for virtualization such that VF 0
  289. * is allocated queues 0 and 8, VF 1 queues 1 and 9, etc.
  290. * In order to avoid collision we start at the first free queue
  291. * and continue consuming queues in the same sequence
  292. */
  293. for (i = 0; i < adapter->num_rx_queues; i++)
  294. adapter->rx_ring[i].reg_idx = rbase_offset +
  295. Q_IDX_82576(i);
  296. for (i = 0; i < adapter->num_tx_queues; i++)
  297. adapter->tx_ring[i].reg_idx = rbase_offset +
  298. Q_IDX_82576(i);
  299. break;
  300. case e1000_82575:
  301. default:
  302. for (i = 0; i < adapter->num_rx_queues; i++)
  303. adapter->rx_ring[i].reg_idx = i;
  304. for (i = 0; i < adapter->num_tx_queues; i++)
  305. adapter->tx_ring[i].reg_idx = i;
  306. break;
  307. }
  308. }
  309. static void igb_free_queues(struct igb_adapter *adapter)
  310. {
  311. kfree(adapter->tx_ring);
  312. kfree(adapter->rx_ring);
  313. adapter->tx_ring = NULL;
  314. adapter->rx_ring = NULL;
  315. adapter->num_rx_queues = 0;
  316. adapter->num_tx_queues = 0;
  317. }
  318. /**
  319. * igb_alloc_queues - Allocate memory for all rings
  320. * @adapter: board private structure to initialize
  321. *
  322. * We allocate one ring per queue at run-time since we don't know the
  323. * number of queues at compile-time.
  324. **/
  325. static int igb_alloc_queues(struct igb_adapter *adapter)
  326. {
  327. int i;
  328. adapter->tx_ring = kcalloc(adapter->num_tx_queues,
  329. sizeof(struct igb_ring), GFP_KERNEL);
  330. if (!adapter->tx_ring)
  331. goto err;
  332. adapter->rx_ring = kcalloc(adapter->num_rx_queues,
  333. sizeof(struct igb_ring), GFP_KERNEL);
  334. if (!adapter->rx_ring)
  335. goto err;
  336. for (i = 0; i < adapter->num_tx_queues; i++) {
  337. struct igb_ring *ring = &(adapter->tx_ring[i]);
  338. ring->count = adapter->tx_ring_count;
  339. ring->queue_index = i;
  340. ring->pdev = adapter->pdev;
  341. ring->netdev = adapter->netdev;
  342. /* For 82575, context index must be unique per ring. */
  343. if (adapter->hw.mac.type == e1000_82575)
  344. ring->flags = IGB_RING_FLAG_TX_CTX_IDX;
  345. }
  346. for (i = 0; i < adapter->num_rx_queues; i++) {
  347. struct igb_ring *ring = &(adapter->rx_ring[i]);
  348. ring->count = adapter->rx_ring_count;
  349. ring->queue_index = i;
  350. ring->pdev = adapter->pdev;
  351. ring->netdev = adapter->netdev;
  352. ring->rx_buffer_len = MAXIMUM_ETHERNET_VLAN_SIZE;
  353. ring->flags = IGB_RING_FLAG_RX_CSUM; /* enable rx checksum */
  354. /* set flag indicating ring supports SCTP checksum offload */
  355. if (adapter->hw.mac.type >= e1000_82576)
  356. ring->flags |= IGB_RING_FLAG_RX_SCTP_CSUM;
  357. }
  358. igb_cache_ring_register(adapter);
  359. return 0;
  360. err:
  361. igb_free_queues(adapter);
  362. return -ENOMEM;
  363. }
  364. #define IGB_N0_QUEUE -1
  365. static void igb_assign_vector(struct igb_q_vector *q_vector, int msix_vector)
  366. {
  367. u32 msixbm = 0;
  368. struct igb_adapter *adapter = q_vector->adapter;
  369. struct e1000_hw *hw = &adapter->hw;
  370. u32 ivar, index;
  371. int rx_queue = IGB_N0_QUEUE;
  372. int tx_queue = IGB_N0_QUEUE;
  373. if (q_vector->rx_ring)
  374. rx_queue = q_vector->rx_ring->reg_idx;
  375. if (q_vector->tx_ring)
  376. tx_queue = q_vector->tx_ring->reg_idx;
  377. switch (hw->mac.type) {
  378. case e1000_82575:
  379. /* The 82575 assigns vectors using a bitmask, which matches the
  380. bitmask for the EICR/EIMS/EIMC registers. To assign one
  381. or more queues to a vector, we write the appropriate bits
  382. into the MSIXBM register for that vector. */
  383. if (rx_queue > IGB_N0_QUEUE)
  384. msixbm = E1000_EICR_RX_QUEUE0 << rx_queue;
  385. if (tx_queue > IGB_N0_QUEUE)
  386. msixbm |= E1000_EICR_TX_QUEUE0 << tx_queue;
  387. array_wr32(E1000_MSIXBM(0), msix_vector, msixbm);
  388. q_vector->eims_value = msixbm;
  389. break;
  390. case e1000_82576:
  391. /* 82576 uses a table-based method for assigning vectors.
  392. Each queue has a single entry in the table to which we write
  393. a vector number along with a "valid" bit. Sadly, the layout
  394. of the table is somewhat counterintuitive. */
  395. if (rx_queue > IGB_N0_QUEUE) {
  396. index = (rx_queue & 0x7);
  397. ivar = array_rd32(E1000_IVAR0, index);
  398. if (rx_queue < 8) {
  399. /* vector goes into low byte of register */
  400. ivar = ivar & 0xFFFFFF00;
  401. ivar |= msix_vector | E1000_IVAR_VALID;
  402. } else {
  403. /* vector goes into third byte of register */
  404. ivar = ivar & 0xFF00FFFF;
  405. ivar |= (msix_vector | E1000_IVAR_VALID) << 16;
  406. }
  407. array_wr32(E1000_IVAR0, index, ivar);
  408. }
  409. if (tx_queue > IGB_N0_QUEUE) {
  410. index = (tx_queue & 0x7);
  411. ivar = array_rd32(E1000_IVAR0, index);
  412. if (tx_queue < 8) {
  413. /* vector goes into second byte of register */
  414. ivar = ivar & 0xFFFF00FF;
  415. ivar |= (msix_vector | E1000_IVAR_VALID) << 8;
  416. } else {
  417. /* vector goes into high byte of register */
  418. ivar = ivar & 0x00FFFFFF;
  419. ivar |= (msix_vector | E1000_IVAR_VALID) << 24;
  420. }
  421. array_wr32(E1000_IVAR0, index, ivar);
  422. }
  423. q_vector->eims_value = 1 << msix_vector;
  424. break;
  425. default:
  426. BUG();
  427. break;
  428. }
  429. }
  430. /**
  431. * igb_configure_msix - Configure MSI-X hardware
  432. *
  433. * igb_configure_msix sets up the hardware to properly
  434. * generate MSI-X interrupts.
  435. **/
  436. static void igb_configure_msix(struct igb_adapter *adapter)
  437. {
  438. u32 tmp;
  439. int i, vector = 0;
  440. struct e1000_hw *hw = &adapter->hw;
  441. adapter->eims_enable_mask = 0;
  442. /* set vector for other causes, i.e. link changes */
  443. switch (hw->mac.type) {
  444. case e1000_82575:
  445. tmp = rd32(E1000_CTRL_EXT);
  446. /* enable MSI-X PBA support*/
  447. tmp |= E1000_CTRL_EXT_PBA_CLR;
  448. /* Auto-Mask interrupts upon ICR read. */
  449. tmp |= E1000_CTRL_EXT_EIAME;
  450. tmp |= E1000_CTRL_EXT_IRCA;
  451. wr32(E1000_CTRL_EXT, tmp);
  452. /* enable msix_other interrupt */
  453. array_wr32(E1000_MSIXBM(0), vector++,
  454. E1000_EIMS_OTHER);
  455. adapter->eims_other = E1000_EIMS_OTHER;
  456. break;
  457. case e1000_82576:
  458. /* Turn on MSI-X capability first, or our settings
  459. * won't stick. And it will take days to debug. */
  460. wr32(E1000_GPIE, E1000_GPIE_MSIX_MODE |
  461. E1000_GPIE_PBA | E1000_GPIE_EIAME |
  462. E1000_GPIE_NSICR);
  463. /* enable msix_other interrupt */
  464. adapter->eims_other = 1 << vector;
  465. tmp = (vector++ | E1000_IVAR_VALID) << 8;
  466. wr32(E1000_IVAR_MISC, tmp);
  467. break;
  468. default:
  469. /* do nothing, since nothing else supports MSI-X */
  470. break;
  471. } /* switch (hw->mac.type) */
  472. adapter->eims_enable_mask |= adapter->eims_other;
  473. for (i = 0; i < adapter->num_q_vectors; i++) {
  474. struct igb_q_vector *q_vector = adapter->q_vector[i];
  475. igb_assign_vector(q_vector, vector++);
  476. adapter->eims_enable_mask |= q_vector->eims_value;
  477. }
  478. wrfl();
  479. }
  480. /**
  481. * igb_request_msix - Initialize MSI-X interrupts
  482. *
  483. * igb_request_msix allocates MSI-X vectors and requests interrupts from the
  484. * kernel.
  485. **/
  486. static int igb_request_msix(struct igb_adapter *adapter)
  487. {
  488. struct net_device *netdev = adapter->netdev;
  489. struct e1000_hw *hw = &adapter->hw;
  490. int i, err = 0, vector = 0;
  491. err = request_irq(adapter->msix_entries[vector].vector,
  492. &igb_msix_other, 0, netdev->name, adapter);
  493. if (err)
  494. goto out;
  495. vector++;
  496. for (i = 0; i < adapter->num_q_vectors; i++) {
  497. struct igb_q_vector *q_vector = adapter->q_vector[i];
  498. q_vector->itr_register = hw->hw_addr + E1000_EITR(vector);
  499. if (q_vector->rx_ring && q_vector->tx_ring)
  500. sprintf(q_vector->name, "%s-TxRx-%u", netdev->name,
  501. q_vector->rx_ring->queue_index);
  502. else if (q_vector->tx_ring)
  503. sprintf(q_vector->name, "%s-tx-%u", netdev->name,
  504. q_vector->tx_ring->queue_index);
  505. else if (q_vector->rx_ring)
  506. sprintf(q_vector->name, "%s-rx-%u", netdev->name,
  507. q_vector->rx_ring->queue_index);
  508. else
  509. sprintf(q_vector->name, "%s-unused", netdev->name);
  510. err = request_irq(adapter->msix_entries[vector].vector,
  511. &igb_msix_ring, 0, q_vector->name,
  512. q_vector);
  513. if (err)
  514. goto out;
  515. vector++;
  516. }
  517. igb_configure_msix(adapter);
  518. return 0;
  519. out:
  520. return err;
  521. }
  522. static void igb_reset_interrupt_capability(struct igb_adapter *adapter)
  523. {
  524. if (adapter->msix_entries) {
  525. pci_disable_msix(adapter->pdev);
  526. kfree(adapter->msix_entries);
  527. adapter->msix_entries = NULL;
  528. } else if (adapter->flags & IGB_FLAG_HAS_MSI) {
  529. pci_disable_msi(adapter->pdev);
  530. }
  531. }
  532. /**
  533. * igb_free_q_vectors - Free memory allocated for interrupt vectors
  534. * @adapter: board private structure to initialize
  535. *
  536. * This function frees the memory allocated to the q_vectors. In addition if
  537. * NAPI is enabled it will delete any references to the NAPI struct prior
  538. * to freeing the q_vector.
  539. **/
  540. static void igb_free_q_vectors(struct igb_adapter *adapter)
  541. {
  542. int v_idx;
  543. for (v_idx = 0; v_idx < adapter->num_q_vectors; v_idx++) {
  544. struct igb_q_vector *q_vector = adapter->q_vector[v_idx];
  545. adapter->q_vector[v_idx] = NULL;
  546. netif_napi_del(&q_vector->napi);
  547. kfree(q_vector);
  548. }
  549. adapter->num_q_vectors = 0;
  550. }
  551. /**
  552. * igb_clear_interrupt_scheme - reset the device to a state of no interrupts
  553. *
  554. * This function resets the device so that it has 0 rx queues, tx queues, and
  555. * MSI-X interrupts allocated.
  556. */
  557. static void igb_clear_interrupt_scheme(struct igb_adapter *adapter)
  558. {
  559. igb_free_queues(adapter);
  560. igb_free_q_vectors(adapter);
  561. igb_reset_interrupt_capability(adapter);
  562. }
  563. /**
  564. * igb_set_interrupt_capability - set MSI or MSI-X if supported
  565. *
  566. * Attempt to configure interrupts using the best available
  567. * capabilities of the hardware and kernel.
  568. **/
  569. static void igb_set_interrupt_capability(struct igb_adapter *adapter)
  570. {
  571. int err;
  572. int numvecs, i;
  573. /* Number of supported queues. */
  574. adapter->num_rx_queues = min_t(u32, IGB_MAX_RX_QUEUES, num_online_cpus());
  575. adapter->num_tx_queues = min_t(u32, IGB_MAX_TX_QUEUES, num_online_cpus());
  576. /* start with one vector for every rx queue */
  577. numvecs = adapter->num_rx_queues;
  578. /* if tx handler is seperate add 1 for every tx queue */
  579. numvecs += adapter->num_tx_queues;
  580. /* store the number of vectors reserved for queues */
  581. adapter->num_q_vectors = numvecs;
  582. /* add 1 vector for link status interrupts */
  583. numvecs++;
  584. adapter->msix_entries = kcalloc(numvecs, sizeof(struct msix_entry),
  585. GFP_KERNEL);
  586. if (!adapter->msix_entries)
  587. goto msi_only;
  588. for (i = 0; i < numvecs; i++)
  589. adapter->msix_entries[i].entry = i;
  590. err = pci_enable_msix(adapter->pdev,
  591. adapter->msix_entries,
  592. numvecs);
  593. if (err == 0)
  594. goto out;
  595. igb_reset_interrupt_capability(adapter);
  596. /* If we can't do MSI-X, try MSI */
  597. msi_only:
  598. #ifdef CONFIG_PCI_IOV
  599. /* disable SR-IOV for non MSI-X configurations */
  600. if (adapter->vf_data) {
  601. struct e1000_hw *hw = &adapter->hw;
  602. /* disable iov and allow time for transactions to clear */
  603. pci_disable_sriov(adapter->pdev);
  604. msleep(500);
  605. kfree(adapter->vf_data);
  606. adapter->vf_data = NULL;
  607. wr32(E1000_IOVCTL, E1000_IOVCTL_REUSE_VFQ);
  608. msleep(100);
  609. dev_info(&adapter->pdev->dev, "IOV Disabled\n");
  610. }
  611. #endif
  612. adapter->vfs_allocated_count = 0;
  613. adapter->flags |= IGB_FLAG_QUEUE_PAIRS;
  614. adapter->num_rx_queues = 1;
  615. adapter->num_tx_queues = 1;
  616. adapter->num_q_vectors = 1;
  617. if (!pci_enable_msi(adapter->pdev))
  618. adapter->flags |= IGB_FLAG_HAS_MSI;
  619. out:
  620. /* Notify the stack of the (possibly) reduced Tx Queue count. */
  621. adapter->netdev->real_num_tx_queues = adapter->num_tx_queues;
  622. return;
  623. }
  624. /**
  625. * igb_alloc_q_vectors - Allocate memory for interrupt vectors
  626. * @adapter: board private structure to initialize
  627. *
  628. * We allocate one q_vector per queue interrupt. If allocation fails we
  629. * return -ENOMEM.
  630. **/
  631. static int igb_alloc_q_vectors(struct igb_adapter *adapter)
  632. {
  633. struct igb_q_vector *q_vector;
  634. struct e1000_hw *hw = &adapter->hw;
  635. int v_idx;
  636. for (v_idx = 0; v_idx < adapter->num_q_vectors; v_idx++) {
  637. q_vector = kzalloc(sizeof(struct igb_q_vector), GFP_KERNEL);
  638. if (!q_vector)
  639. goto err_out;
  640. q_vector->adapter = adapter;
  641. q_vector->itr_shift = (hw->mac.type == e1000_82575) ? 16 : 0;
  642. q_vector->itr_register = hw->hw_addr + E1000_EITR(0);
  643. q_vector->itr_val = IGB_START_ITR;
  644. q_vector->set_itr = 1;
  645. netif_napi_add(adapter->netdev, &q_vector->napi, igb_poll, 64);
  646. adapter->q_vector[v_idx] = q_vector;
  647. }
  648. return 0;
  649. err_out:
  650. while (v_idx) {
  651. v_idx--;
  652. q_vector = adapter->q_vector[v_idx];
  653. netif_napi_del(&q_vector->napi);
  654. kfree(q_vector);
  655. adapter->q_vector[v_idx] = NULL;
  656. }
  657. return -ENOMEM;
  658. }
  659. static void igb_map_rx_ring_to_vector(struct igb_adapter *adapter,
  660. int ring_idx, int v_idx)
  661. {
  662. struct igb_q_vector *q_vector;
  663. q_vector = adapter->q_vector[v_idx];
  664. q_vector->rx_ring = &adapter->rx_ring[ring_idx];
  665. q_vector->rx_ring->q_vector = q_vector;
  666. q_vector->itr_val = adapter->rx_itr_setting;
  667. if (q_vector->itr_val && q_vector->itr_val <= 3)
  668. q_vector->itr_val = IGB_START_ITR;
  669. }
  670. static void igb_map_tx_ring_to_vector(struct igb_adapter *adapter,
  671. int ring_idx, int v_idx)
  672. {
  673. struct igb_q_vector *q_vector;
  674. q_vector = adapter->q_vector[v_idx];
  675. q_vector->tx_ring = &adapter->tx_ring[ring_idx];
  676. q_vector->tx_ring->q_vector = q_vector;
  677. q_vector->itr_val = adapter->tx_itr_setting;
  678. if (q_vector->itr_val && q_vector->itr_val <= 3)
  679. q_vector->itr_val = IGB_START_ITR;
  680. }
  681. /**
  682. * igb_map_ring_to_vector - maps allocated queues to vectors
  683. *
  684. * This function maps the recently allocated queues to vectors.
  685. **/
  686. static int igb_map_ring_to_vector(struct igb_adapter *adapter)
  687. {
  688. int i;
  689. int v_idx = 0;
  690. if ((adapter->num_q_vectors < adapter->num_rx_queues) ||
  691. (adapter->num_q_vectors < adapter->num_tx_queues))
  692. return -ENOMEM;
  693. if (adapter->num_q_vectors >=
  694. (adapter->num_rx_queues + adapter->num_tx_queues)) {
  695. for (i = 0; i < adapter->num_rx_queues; i++)
  696. igb_map_rx_ring_to_vector(adapter, i, v_idx++);
  697. for (i = 0; i < adapter->num_tx_queues; i++)
  698. igb_map_tx_ring_to_vector(adapter, i, v_idx++);
  699. } else {
  700. for (i = 0; i < adapter->num_rx_queues; i++) {
  701. if (i < adapter->num_tx_queues)
  702. igb_map_tx_ring_to_vector(adapter, i, v_idx);
  703. igb_map_rx_ring_to_vector(adapter, i, v_idx++);
  704. }
  705. for (; i < adapter->num_tx_queues; i++)
  706. igb_map_tx_ring_to_vector(adapter, i, v_idx++);
  707. }
  708. return 0;
  709. }
  710. /**
  711. * igb_init_interrupt_scheme - initialize interrupts, allocate queues/vectors
  712. *
  713. * This function initializes the interrupts and allocates all of the queues.
  714. **/
  715. static int igb_init_interrupt_scheme(struct igb_adapter *adapter)
  716. {
  717. struct pci_dev *pdev = adapter->pdev;
  718. int err;
  719. igb_set_interrupt_capability(adapter);
  720. err = igb_alloc_q_vectors(adapter);
  721. if (err) {
  722. dev_err(&pdev->dev, "Unable to allocate memory for vectors\n");
  723. goto err_alloc_q_vectors;
  724. }
  725. err = igb_alloc_queues(adapter);
  726. if (err) {
  727. dev_err(&pdev->dev, "Unable to allocate memory for queues\n");
  728. goto err_alloc_queues;
  729. }
  730. err = igb_map_ring_to_vector(adapter);
  731. if (err) {
  732. dev_err(&pdev->dev, "Invalid q_vector to ring mapping\n");
  733. goto err_map_queues;
  734. }
  735. return 0;
  736. err_map_queues:
  737. igb_free_queues(adapter);
  738. err_alloc_queues:
  739. igb_free_q_vectors(adapter);
  740. err_alloc_q_vectors:
  741. igb_reset_interrupt_capability(adapter);
  742. return err;
  743. }
  744. /**
  745. * igb_request_irq - initialize interrupts
  746. *
  747. * Attempts to configure interrupts using the best available
  748. * capabilities of the hardware and kernel.
  749. **/
  750. static int igb_request_irq(struct igb_adapter *adapter)
  751. {
  752. struct net_device *netdev = adapter->netdev;
  753. struct pci_dev *pdev = adapter->pdev;
  754. struct e1000_hw *hw = &adapter->hw;
  755. int err = 0;
  756. if (adapter->msix_entries) {
  757. err = igb_request_msix(adapter);
  758. if (!err)
  759. goto request_done;
  760. /* fall back to MSI */
  761. igb_clear_interrupt_scheme(adapter);
  762. if (!pci_enable_msi(adapter->pdev))
  763. adapter->flags |= IGB_FLAG_HAS_MSI;
  764. igb_free_all_tx_resources(adapter);
  765. igb_free_all_rx_resources(adapter);
  766. adapter->num_tx_queues = 1;
  767. adapter->num_rx_queues = 1;
  768. adapter->num_q_vectors = 1;
  769. err = igb_alloc_q_vectors(adapter);
  770. if (err) {
  771. dev_err(&pdev->dev,
  772. "Unable to allocate memory for vectors\n");
  773. goto request_done;
  774. }
  775. err = igb_alloc_queues(adapter);
  776. if (err) {
  777. dev_err(&pdev->dev,
  778. "Unable to allocate memory for queues\n");
  779. igb_free_q_vectors(adapter);
  780. goto request_done;
  781. }
  782. igb_setup_all_tx_resources(adapter);
  783. igb_setup_all_rx_resources(adapter);
  784. } else {
  785. switch (hw->mac.type) {
  786. case e1000_82575:
  787. wr32(E1000_MSIXBM(0),
  788. (E1000_EICR_RX_QUEUE0 |
  789. E1000_EICR_TX_QUEUE0 |
  790. E1000_EIMS_OTHER));
  791. break;
  792. case e1000_82576:
  793. wr32(E1000_IVAR0, E1000_IVAR_VALID);
  794. break;
  795. default:
  796. break;
  797. }
  798. }
  799. if (adapter->flags & IGB_FLAG_HAS_MSI) {
  800. err = request_irq(adapter->pdev->irq, &igb_intr_msi, 0,
  801. netdev->name, adapter);
  802. if (!err)
  803. goto request_done;
  804. /* fall back to legacy interrupts */
  805. igb_reset_interrupt_capability(adapter);
  806. adapter->flags &= ~IGB_FLAG_HAS_MSI;
  807. }
  808. err = request_irq(adapter->pdev->irq, &igb_intr, IRQF_SHARED,
  809. netdev->name, adapter);
  810. if (err)
  811. dev_err(&adapter->pdev->dev, "Error %d getting interrupt\n",
  812. err);
  813. request_done:
  814. return err;
  815. }
  816. static void igb_free_irq(struct igb_adapter *adapter)
  817. {
  818. if (adapter->msix_entries) {
  819. int vector = 0, i;
  820. free_irq(adapter->msix_entries[vector++].vector, adapter);
  821. for (i = 0; i < adapter->num_q_vectors; i++) {
  822. struct igb_q_vector *q_vector = adapter->q_vector[i];
  823. free_irq(adapter->msix_entries[vector++].vector,
  824. q_vector);
  825. }
  826. } else {
  827. free_irq(adapter->pdev->irq, adapter);
  828. }
  829. }
  830. /**
  831. * igb_irq_disable - Mask off interrupt generation on the NIC
  832. * @adapter: board private structure
  833. **/
  834. static void igb_irq_disable(struct igb_adapter *adapter)
  835. {
  836. struct e1000_hw *hw = &adapter->hw;
  837. if (adapter->msix_entries) {
  838. u32 regval = rd32(E1000_EIAM);
  839. wr32(E1000_EIAM, regval & ~adapter->eims_enable_mask);
  840. wr32(E1000_EIMC, adapter->eims_enable_mask);
  841. regval = rd32(E1000_EIAC);
  842. wr32(E1000_EIAC, regval & ~adapter->eims_enable_mask);
  843. }
  844. wr32(E1000_IAM, 0);
  845. wr32(E1000_IMC, ~0);
  846. wrfl();
  847. synchronize_irq(adapter->pdev->irq);
  848. }
  849. /**
  850. * igb_irq_enable - Enable default interrupt generation settings
  851. * @adapter: board private structure
  852. **/
  853. static void igb_irq_enable(struct igb_adapter *adapter)
  854. {
  855. struct e1000_hw *hw = &adapter->hw;
  856. if (adapter->msix_entries) {
  857. u32 regval = rd32(E1000_EIAC);
  858. wr32(E1000_EIAC, regval | adapter->eims_enable_mask);
  859. regval = rd32(E1000_EIAM);
  860. wr32(E1000_EIAM, regval | adapter->eims_enable_mask);
  861. wr32(E1000_EIMS, adapter->eims_enable_mask);
  862. if (adapter->vfs_allocated_count)
  863. wr32(E1000_MBVFIMR, 0xFF);
  864. wr32(E1000_IMS, (E1000_IMS_LSC | E1000_IMS_VMMB |
  865. E1000_IMS_DOUTSYNC));
  866. } else {
  867. wr32(E1000_IMS, IMS_ENABLE_MASK);
  868. wr32(E1000_IAM, IMS_ENABLE_MASK);
  869. }
  870. }
  871. static void igb_update_mng_vlan(struct igb_adapter *adapter)
  872. {
  873. struct net_device *netdev = adapter->netdev;
  874. u16 vid = adapter->hw.mng_cookie.vlan_id;
  875. u16 old_vid = adapter->mng_vlan_id;
  876. if (adapter->vlgrp) {
  877. if (!vlan_group_get_device(adapter->vlgrp, vid)) {
  878. if (adapter->hw.mng_cookie.status &
  879. E1000_MNG_DHCP_COOKIE_STATUS_VLAN) {
  880. igb_vlan_rx_add_vid(netdev, vid);
  881. adapter->mng_vlan_id = vid;
  882. } else
  883. adapter->mng_vlan_id = IGB_MNG_VLAN_NONE;
  884. if ((old_vid != (u16)IGB_MNG_VLAN_NONE) &&
  885. (vid != old_vid) &&
  886. !vlan_group_get_device(adapter->vlgrp, old_vid))
  887. igb_vlan_rx_kill_vid(netdev, old_vid);
  888. } else
  889. adapter->mng_vlan_id = vid;
  890. }
  891. }
  892. /**
  893. * igb_release_hw_control - release control of the h/w to f/w
  894. * @adapter: address of board private structure
  895. *
  896. * igb_release_hw_control resets CTRL_EXT:DRV_LOAD bit.
  897. * For ASF and Pass Through versions of f/w this means that the
  898. * driver is no longer loaded.
  899. *
  900. **/
  901. static void igb_release_hw_control(struct igb_adapter *adapter)
  902. {
  903. struct e1000_hw *hw = &adapter->hw;
  904. u32 ctrl_ext;
  905. /* Let firmware take over control of h/w */
  906. ctrl_ext = rd32(E1000_CTRL_EXT);
  907. wr32(E1000_CTRL_EXT,
  908. ctrl_ext & ~E1000_CTRL_EXT_DRV_LOAD);
  909. }
  910. /**
  911. * igb_get_hw_control - get control of the h/w from f/w
  912. * @adapter: address of board private structure
  913. *
  914. * igb_get_hw_control sets CTRL_EXT:DRV_LOAD bit.
  915. * For ASF and Pass Through versions of f/w this means that
  916. * the driver is loaded.
  917. *
  918. **/
  919. static void igb_get_hw_control(struct igb_adapter *adapter)
  920. {
  921. struct e1000_hw *hw = &adapter->hw;
  922. u32 ctrl_ext;
  923. /* Let firmware know the driver has taken over */
  924. ctrl_ext = rd32(E1000_CTRL_EXT);
  925. wr32(E1000_CTRL_EXT,
  926. ctrl_ext | E1000_CTRL_EXT_DRV_LOAD);
  927. }
  928. /**
  929. * igb_configure - configure the hardware for RX and TX
  930. * @adapter: private board structure
  931. **/
  932. static void igb_configure(struct igb_adapter *adapter)
  933. {
  934. struct net_device *netdev = adapter->netdev;
  935. int i;
  936. igb_get_hw_control(adapter);
  937. igb_set_rx_mode(netdev);
  938. igb_restore_vlan(adapter);
  939. igb_setup_tctl(adapter);
  940. igb_setup_mrqc(adapter);
  941. igb_setup_rctl(adapter);
  942. igb_configure_tx(adapter);
  943. igb_configure_rx(adapter);
  944. igb_rx_fifo_flush_82575(&adapter->hw);
  945. /* call igb_desc_unused which always leaves
  946. * at least 1 descriptor unused to make sure
  947. * next_to_use != next_to_clean */
  948. for (i = 0; i < adapter->num_rx_queues; i++) {
  949. struct igb_ring *ring = &adapter->rx_ring[i];
  950. igb_alloc_rx_buffers_adv(ring, igb_desc_unused(ring));
  951. }
  952. adapter->tx_queue_len = netdev->tx_queue_len;
  953. }
  954. /**
  955. * igb_up - Open the interface and prepare it to handle traffic
  956. * @adapter: board private structure
  957. **/
  958. int igb_up(struct igb_adapter *adapter)
  959. {
  960. struct e1000_hw *hw = &adapter->hw;
  961. int i;
  962. /* hardware has been reset, we need to reload some things */
  963. igb_configure(adapter);
  964. clear_bit(__IGB_DOWN, &adapter->state);
  965. for (i = 0; i < adapter->num_q_vectors; i++) {
  966. struct igb_q_vector *q_vector = adapter->q_vector[i];
  967. napi_enable(&q_vector->napi);
  968. }
  969. if (adapter->msix_entries)
  970. igb_configure_msix(adapter);
  971. igb_set_vmolr(hw, adapter->vfs_allocated_count);
  972. /* Clear any pending interrupts. */
  973. rd32(E1000_ICR);
  974. igb_irq_enable(adapter);
  975. /* notify VFs that reset has been completed */
  976. if (adapter->vfs_allocated_count) {
  977. u32 reg_data = rd32(E1000_CTRL_EXT);
  978. reg_data |= E1000_CTRL_EXT_PFRSTD;
  979. wr32(E1000_CTRL_EXT, reg_data);
  980. }
  981. netif_tx_start_all_queues(adapter->netdev);
  982. /* Fire a link change interrupt to start the watchdog. */
  983. wr32(E1000_ICS, E1000_ICS_LSC);
  984. return 0;
  985. }
  986. void igb_down(struct igb_adapter *adapter)
  987. {
  988. struct e1000_hw *hw = &adapter->hw;
  989. struct net_device *netdev = adapter->netdev;
  990. u32 tctl, rctl;
  991. int i;
  992. /* signal that we're down so the interrupt handler does not
  993. * reschedule our watchdog timer */
  994. set_bit(__IGB_DOWN, &adapter->state);
  995. /* disable receives in the hardware */
  996. rctl = rd32(E1000_RCTL);
  997. wr32(E1000_RCTL, rctl & ~E1000_RCTL_EN);
  998. /* flush and sleep below */
  999. netif_tx_stop_all_queues(netdev);
  1000. /* disable transmits in the hardware */
  1001. tctl = rd32(E1000_TCTL);
  1002. tctl &= ~E1000_TCTL_EN;
  1003. wr32(E1000_TCTL, tctl);
  1004. /* flush both disables and wait for them to finish */
  1005. wrfl();
  1006. msleep(10);
  1007. for (i = 0; i < adapter->num_q_vectors; i++) {
  1008. struct igb_q_vector *q_vector = adapter->q_vector[i];
  1009. napi_disable(&q_vector->napi);
  1010. }
  1011. igb_irq_disable(adapter);
  1012. del_timer_sync(&adapter->watchdog_timer);
  1013. del_timer_sync(&adapter->phy_info_timer);
  1014. netdev->tx_queue_len = adapter->tx_queue_len;
  1015. netif_carrier_off(netdev);
  1016. /* record the stats before reset*/
  1017. igb_update_stats(adapter);
  1018. adapter->link_speed = 0;
  1019. adapter->link_duplex = 0;
  1020. if (!pci_channel_offline(adapter->pdev))
  1021. igb_reset(adapter);
  1022. igb_clean_all_tx_rings(adapter);
  1023. igb_clean_all_rx_rings(adapter);
  1024. #ifdef CONFIG_IGB_DCA
  1025. /* since we reset the hardware DCA settings were cleared */
  1026. igb_setup_dca(adapter);
  1027. #endif
  1028. }
  1029. void igb_reinit_locked(struct igb_adapter *adapter)
  1030. {
  1031. WARN_ON(in_interrupt());
  1032. while (test_and_set_bit(__IGB_RESETTING, &adapter->state))
  1033. msleep(1);
  1034. igb_down(adapter);
  1035. igb_up(adapter);
  1036. clear_bit(__IGB_RESETTING, &adapter->state);
  1037. }
  1038. void igb_reset(struct igb_adapter *adapter)
  1039. {
  1040. struct e1000_hw *hw = &adapter->hw;
  1041. struct e1000_mac_info *mac = &hw->mac;
  1042. struct e1000_fc_info *fc = &hw->fc;
  1043. u32 pba = 0, tx_space, min_tx_space, min_rx_space;
  1044. u16 hwm;
  1045. /* Repartition Pba for greater than 9k mtu
  1046. * To take effect CTRL.RST is required.
  1047. */
  1048. switch (mac->type) {
  1049. case e1000_82576:
  1050. pba = rd32(E1000_RXPBS);
  1051. pba &= E1000_RXPBS_SIZE_MASK_82576;
  1052. break;
  1053. case e1000_82575:
  1054. default:
  1055. pba = E1000_PBA_34K;
  1056. break;
  1057. }
  1058. if ((adapter->max_frame_size > ETH_FRAME_LEN + ETH_FCS_LEN) &&
  1059. (mac->type < e1000_82576)) {
  1060. /* adjust PBA for jumbo frames */
  1061. wr32(E1000_PBA, pba);
  1062. /* To maintain wire speed transmits, the Tx FIFO should be
  1063. * large enough to accommodate two full transmit packets,
  1064. * rounded up to the next 1KB and expressed in KB. Likewise,
  1065. * the Rx FIFO should be large enough to accommodate at least
  1066. * one full receive packet and is similarly rounded up and
  1067. * expressed in KB. */
  1068. pba = rd32(E1000_PBA);
  1069. /* upper 16 bits has Tx packet buffer allocation size in KB */
  1070. tx_space = pba >> 16;
  1071. /* lower 16 bits has Rx packet buffer allocation size in KB */
  1072. pba &= 0xffff;
  1073. /* the tx fifo also stores 16 bytes of information about the tx
  1074. * but don't include ethernet FCS because hardware appends it */
  1075. min_tx_space = (adapter->max_frame_size +
  1076. sizeof(union e1000_adv_tx_desc) -
  1077. ETH_FCS_LEN) * 2;
  1078. min_tx_space = ALIGN(min_tx_space, 1024);
  1079. min_tx_space >>= 10;
  1080. /* software strips receive CRC, so leave room for it */
  1081. min_rx_space = adapter->max_frame_size;
  1082. min_rx_space = ALIGN(min_rx_space, 1024);
  1083. min_rx_space >>= 10;
  1084. /* If current Tx allocation is less than the min Tx FIFO size,
  1085. * and the min Tx FIFO size is less than the current Rx FIFO
  1086. * allocation, take space away from current Rx allocation */
  1087. if (tx_space < min_tx_space &&
  1088. ((min_tx_space - tx_space) < pba)) {
  1089. pba = pba - (min_tx_space - tx_space);
  1090. /* if short on rx space, rx wins and must trump tx
  1091. * adjustment */
  1092. if (pba < min_rx_space)
  1093. pba = min_rx_space;
  1094. }
  1095. wr32(E1000_PBA, pba);
  1096. }
  1097. /* flow control settings */
  1098. /* The high water mark must be low enough to fit one full frame
  1099. * (or the size used for early receive) above it in the Rx FIFO.
  1100. * Set it to the lower of:
  1101. * - 90% of the Rx FIFO size, or
  1102. * - the full Rx FIFO size minus one full frame */
  1103. hwm = min(((pba << 10) * 9 / 10),
  1104. ((pba << 10) - 2 * adapter->max_frame_size));
  1105. if (mac->type < e1000_82576) {
  1106. fc->high_water = hwm & 0xFFF8; /* 8-byte granularity */
  1107. fc->low_water = fc->high_water - 8;
  1108. } else {
  1109. fc->high_water = hwm & 0xFFF0; /* 16-byte granularity */
  1110. fc->low_water = fc->high_water - 16;
  1111. }
  1112. fc->pause_time = 0xFFFF;
  1113. fc->send_xon = 1;
  1114. fc->current_mode = fc->requested_mode;
  1115. /* disable receive for all VFs and wait one second */
  1116. if (adapter->vfs_allocated_count) {
  1117. int i;
  1118. for (i = 0 ; i < adapter->vfs_allocated_count; i++)
  1119. adapter->vf_data[i].clear_to_send = false;
  1120. /* ping all the active vfs to let them know we are going down */
  1121. igb_ping_all_vfs(adapter);
  1122. /* disable transmits and receives */
  1123. wr32(E1000_VFRE, 0);
  1124. wr32(E1000_VFTE, 0);
  1125. }
  1126. /* Allow time for pending master requests to run */
  1127. adapter->hw.mac.ops.reset_hw(&adapter->hw);
  1128. wr32(E1000_WUC, 0);
  1129. if (adapter->hw.mac.ops.init_hw(&adapter->hw))
  1130. dev_err(&adapter->pdev->dev, "Hardware Error\n");
  1131. igb_update_mng_vlan(adapter);
  1132. /* Enable h/w to recognize an 802.1Q VLAN Ethernet packet */
  1133. wr32(E1000_VET, ETHERNET_IEEE_VLAN_TYPE);
  1134. igb_reset_adaptive(&adapter->hw);
  1135. igb_get_phy_info(&adapter->hw);
  1136. }
  1137. static const struct net_device_ops igb_netdev_ops = {
  1138. .ndo_open = igb_open,
  1139. .ndo_stop = igb_close,
  1140. .ndo_start_xmit = igb_xmit_frame_adv,
  1141. .ndo_get_stats = igb_get_stats,
  1142. .ndo_set_rx_mode = igb_set_rx_mode,
  1143. .ndo_set_multicast_list = igb_set_rx_mode,
  1144. .ndo_set_mac_address = igb_set_mac,
  1145. .ndo_change_mtu = igb_change_mtu,
  1146. .ndo_do_ioctl = igb_ioctl,
  1147. .ndo_tx_timeout = igb_tx_timeout,
  1148. .ndo_validate_addr = eth_validate_addr,
  1149. .ndo_vlan_rx_register = igb_vlan_rx_register,
  1150. .ndo_vlan_rx_add_vid = igb_vlan_rx_add_vid,
  1151. .ndo_vlan_rx_kill_vid = igb_vlan_rx_kill_vid,
  1152. #ifdef CONFIG_NET_POLL_CONTROLLER
  1153. .ndo_poll_controller = igb_netpoll,
  1154. #endif
  1155. };
  1156. /**
  1157. * igb_probe - Device Initialization Routine
  1158. * @pdev: PCI device information struct
  1159. * @ent: entry in igb_pci_tbl
  1160. *
  1161. * Returns 0 on success, negative on failure
  1162. *
  1163. * igb_probe initializes an adapter identified by a pci_dev structure.
  1164. * The OS initialization, configuring of the adapter private structure,
  1165. * and a hardware reset occur.
  1166. **/
  1167. static int __devinit igb_probe(struct pci_dev *pdev,
  1168. const struct pci_device_id *ent)
  1169. {
  1170. struct net_device *netdev;
  1171. struct igb_adapter *adapter;
  1172. struct e1000_hw *hw;
  1173. const struct e1000_info *ei = igb_info_tbl[ent->driver_data];
  1174. unsigned long mmio_start, mmio_len;
  1175. int err, pci_using_dac;
  1176. u16 eeprom_data = 0;
  1177. u16 eeprom_apme_mask = IGB_EEPROM_APME;
  1178. u32 part_num;
  1179. err = pci_enable_device_mem(pdev);
  1180. if (err)
  1181. return err;
  1182. pci_using_dac = 0;
  1183. err = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
  1184. if (!err) {
  1185. err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
  1186. if (!err)
  1187. pci_using_dac = 1;
  1188. } else {
  1189. err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  1190. if (err) {
  1191. err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
  1192. if (err) {
  1193. dev_err(&pdev->dev, "No usable DMA "
  1194. "configuration, aborting\n");
  1195. goto err_dma;
  1196. }
  1197. }
  1198. }
  1199. err = pci_request_selected_regions(pdev, pci_select_bars(pdev,
  1200. IORESOURCE_MEM),
  1201. igb_driver_name);
  1202. if (err)
  1203. goto err_pci_reg;
  1204. pci_enable_pcie_error_reporting(pdev);
  1205. pci_set_master(pdev);
  1206. pci_save_state(pdev);
  1207. err = -ENOMEM;
  1208. netdev = alloc_etherdev_mq(sizeof(struct igb_adapter),
  1209. IGB_ABS_MAX_TX_QUEUES);
  1210. if (!netdev)
  1211. goto err_alloc_etherdev;
  1212. SET_NETDEV_DEV(netdev, &pdev->dev);
  1213. pci_set_drvdata(pdev, netdev);
  1214. adapter = netdev_priv(netdev);
  1215. adapter->netdev = netdev;
  1216. adapter->pdev = pdev;
  1217. hw = &adapter->hw;
  1218. hw->back = adapter;
  1219. adapter->msg_enable = NETIF_MSG_DRV | NETIF_MSG_PROBE;
  1220. mmio_start = pci_resource_start(pdev, 0);
  1221. mmio_len = pci_resource_len(pdev, 0);
  1222. err = -EIO;
  1223. hw->hw_addr = ioremap(mmio_start, mmio_len);
  1224. if (!hw->hw_addr)
  1225. goto err_ioremap;
  1226. netdev->netdev_ops = &igb_netdev_ops;
  1227. igb_set_ethtool_ops(netdev);
  1228. netdev->watchdog_timeo = 5 * HZ;
  1229. strncpy(netdev->name, pci_name(pdev), sizeof(netdev->name) - 1);
  1230. netdev->mem_start = mmio_start;
  1231. netdev->mem_end = mmio_start + mmio_len;
  1232. /* PCI config space info */
  1233. hw->vendor_id = pdev->vendor;
  1234. hw->device_id = pdev->device;
  1235. hw->revision_id = pdev->revision;
  1236. hw->subsystem_vendor_id = pdev->subsystem_vendor;
  1237. hw->subsystem_device_id = pdev->subsystem_device;
  1238. /* setup the private structure */
  1239. hw->back = adapter;
  1240. /* Copy the default MAC, PHY and NVM function pointers */
  1241. memcpy(&hw->mac.ops, ei->mac_ops, sizeof(hw->mac.ops));
  1242. memcpy(&hw->phy.ops, ei->phy_ops, sizeof(hw->phy.ops));
  1243. memcpy(&hw->nvm.ops, ei->nvm_ops, sizeof(hw->nvm.ops));
  1244. /* Initialize skew-specific constants */
  1245. err = ei->get_invariants(hw);
  1246. if (err)
  1247. goto err_sw_init;
  1248. #ifdef CONFIG_PCI_IOV
  1249. /* since iov functionality isn't critical to base device function we
  1250. * can accept failure. If it fails we don't allow iov to be enabled */
  1251. if (hw->mac.type == e1000_82576) {
  1252. /* 82576 supports a maximum of 7 VFs in addition to the PF */
  1253. unsigned int num_vfs = (max_vfs > 7) ? 7 : max_vfs;
  1254. int i;
  1255. unsigned char mac_addr[ETH_ALEN];
  1256. if (num_vfs) {
  1257. adapter->vf_data = kcalloc(num_vfs,
  1258. sizeof(struct vf_data_storage),
  1259. GFP_KERNEL);
  1260. if (!adapter->vf_data) {
  1261. dev_err(&pdev->dev,
  1262. "Could not allocate VF private data - "
  1263. "IOV enable failed\n");
  1264. } else {
  1265. err = pci_enable_sriov(pdev, num_vfs);
  1266. if (!err) {
  1267. adapter->vfs_allocated_count = num_vfs;
  1268. dev_info(&pdev->dev,
  1269. "%d vfs allocated\n",
  1270. num_vfs);
  1271. for (i = 0;
  1272. i < adapter->vfs_allocated_count;
  1273. i++) {
  1274. random_ether_addr(mac_addr);
  1275. igb_set_vf_mac(adapter, i,
  1276. mac_addr);
  1277. }
  1278. } else {
  1279. kfree(adapter->vf_data);
  1280. adapter->vf_data = NULL;
  1281. }
  1282. }
  1283. }
  1284. }
  1285. #endif
  1286. /* setup the private structure */
  1287. err = igb_sw_init(adapter);
  1288. if (err)
  1289. goto err_sw_init;
  1290. igb_get_bus_info_pcie(hw);
  1291. hw->phy.autoneg_wait_to_complete = false;
  1292. hw->mac.adaptive_ifs = true;
  1293. /* Copper options */
  1294. if (hw->phy.media_type == e1000_media_type_copper) {
  1295. hw->phy.mdix = AUTO_ALL_MODES;
  1296. hw->phy.disable_polarity_correction = false;
  1297. hw->phy.ms_type = e1000_ms_hw_default;
  1298. }
  1299. if (igb_check_reset_block(hw))
  1300. dev_info(&pdev->dev,
  1301. "PHY reset is blocked due to SOL/IDER session.\n");
  1302. netdev->features = NETIF_F_SG |
  1303. NETIF_F_IP_CSUM |
  1304. NETIF_F_HW_VLAN_TX |
  1305. NETIF_F_HW_VLAN_RX |
  1306. NETIF_F_HW_VLAN_FILTER;
  1307. netdev->features |= NETIF_F_IPV6_CSUM;
  1308. netdev->features |= NETIF_F_TSO;
  1309. netdev->features |= NETIF_F_TSO6;
  1310. netdev->features |= NETIF_F_GRO;
  1311. netdev->vlan_features |= NETIF_F_TSO;
  1312. netdev->vlan_features |= NETIF_F_TSO6;
  1313. netdev->vlan_features |= NETIF_F_IP_CSUM;
  1314. netdev->vlan_features |= NETIF_F_IPV6_CSUM;
  1315. netdev->vlan_features |= NETIF_F_SG;
  1316. if (pci_using_dac)
  1317. netdev->features |= NETIF_F_HIGHDMA;
  1318. if (adapter->hw.mac.type == e1000_82576)
  1319. netdev->features |= NETIF_F_SCTP_CSUM;
  1320. adapter->en_mng_pt = igb_enable_mng_pass_thru(&adapter->hw);
  1321. /* before reading the NVM, reset the controller to put the device in a
  1322. * known good starting state */
  1323. hw->mac.ops.reset_hw(hw);
  1324. /* make sure the NVM is good */
  1325. if (igb_validate_nvm_checksum(hw) < 0) {
  1326. dev_err(&pdev->dev, "The NVM Checksum Is Not Valid\n");
  1327. err = -EIO;
  1328. goto err_eeprom;
  1329. }
  1330. /* copy the MAC address out of the NVM */
  1331. if (hw->mac.ops.read_mac_addr(hw))
  1332. dev_err(&pdev->dev, "NVM Read Error\n");
  1333. memcpy(netdev->dev_addr, hw->mac.addr, netdev->addr_len);
  1334. memcpy(netdev->perm_addr, hw->mac.addr, netdev->addr_len);
  1335. if (!is_valid_ether_addr(netdev->perm_addr)) {
  1336. dev_err(&pdev->dev, "Invalid MAC Address\n");
  1337. err = -EIO;
  1338. goto err_eeprom;
  1339. }
  1340. setup_timer(&adapter->watchdog_timer, &igb_watchdog,
  1341. (unsigned long) adapter);
  1342. setup_timer(&adapter->phy_info_timer, &igb_update_phy_info,
  1343. (unsigned long) adapter);
  1344. INIT_WORK(&adapter->reset_task, igb_reset_task);
  1345. INIT_WORK(&adapter->watchdog_task, igb_watchdog_task);
  1346. /* Initialize link properties that are user-changeable */
  1347. adapter->fc_autoneg = true;
  1348. hw->mac.autoneg = true;
  1349. hw->phy.autoneg_advertised = 0x2f;
  1350. hw->fc.requested_mode = e1000_fc_default;
  1351. hw->fc.current_mode = e1000_fc_default;
  1352. igb_validate_mdi_setting(hw);
  1353. /* Initial Wake on LAN setting If APM wake is enabled in the EEPROM,
  1354. * enable the ACPI Magic Packet filter
  1355. */
  1356. if (hw->bus.func == 0)
  1357. hw->nvm.ops.read(hw, NVM_INIT_CONTROL3_PORT_A, 1, &eeprom_data);
  1358. else if (hw->bus.func == 1)
  1359. hw->nvm.ops.read(hw, NVM_INIT_CONTROL3_PORT_B, 1, &eeprom_data);
  1360. if (eeprom_data & eeprom_apme_mask)
  1361. adapter->eeprom_wol |= E1000_WUFC_MAG;
  1362. /* now that we have the eeprom settings, apply the special cases where
  1363. * the eeprom may be wrong or the board simply won't support wake on
  1364. * lan on a particular port */
  1365. switch (pdev->device) {
  1366. case E1000_DEV_ID_82575GB_QUAD_COPPER:
  1367. adapter->eeprom_wol = 0;
  1368. break;
  1369. case E1000_DEV_ID_82575EB_FIBER_SERDES:
  1370. case E1000_DEV_ID_82576_FIBER:
  1371. case E1000_DEV_ID_82576_SERDES:
  1372. /* Wake events only supported on port A for dual fiber
  1373. * regardless of eeprom setting */
  1374. if (rd32(E1000_STATUS) & E1000_STATUS_FUNC_1)
  1375. adapter->eeprom_wol = 0;
  1376. break;
  1377. case E1000_DEV_ID_82576_QUAD_COPPER:
  1378. /* if quad port adapter, disable WoL on all but port A */
  1379. if (global_quad_port_a != 0)
  1380. adapter->eeprom_wol = 0;
  1381. else
  1382. adapter->flags |= IGB_FLAG_QUAD_PORT_A;
  1383. /* Reset for multiple quad port adapters */
  1384. if (++global_quad_port_a == 4)
  1385. global_quad_port_a = 0;
  1386. break;
  1387. }
  1388. /* initialize the wol settings based on the eeprom settings */
  1389. adapter->wol = adapter->eeprom_wol;
  1390. device_set_wakeup_enable(&adapter->pdev->dev, adapter->wol);
  1391. /* reset the hardware with the new settings */
  1392. igb_reset(adapter);
  1393. /* let the f/w know that the h/w is now under the control of the
  1394. * driver. */
  1395. igb_get_hw_control(adapter);
  1396. strcpy(netdev->name, "eth%d");
  1397. err = register_netdev(netdev);
  1398. if (err)
  1399. goto err_register;
  1400. /* carrier off reporting is important to ethtool even BEFORE open */
  1401. netif_carrier_off(netdev);
  1402. #ifdef CONFIG_IGB_DCA
  1403. if (dca_add_requester(&pdev->dev) == 0) {
  1404. adapter->flags |= IGB_FLAG_DCA_ENABLED;
  1405. dev_info(&pdev->dev, "DCA enabled\n");
  1406. igb_setup_dca(adapter);
  1407. }
  1408. #endif
  1409. switch (hw->mac.type) {
  1410. case e1000_82576:
  1411. /*
  1412. * Initialize hardware timer: we keep it running just in case
  1413. * that some program needs it later on.
  1414. */
  1415. memset(&adapter->cycles, 0, sizeof(adapter->cycles));
  1416. adapter->cycles.read = igb_read_clock;
  1417. adapter->cycles.mask = CLOCKSOURCE_MASK(64);
  1418. adapter->cycles.mult = 1;
  1419. /**
  1420. * Scale the NIC clock cycle by a large factor so that
  1421. * relatively small clock corrections can be added or
  1422. * substracted at each clock tick. The drawbacks of a large
  1423. * factor are a) that the clock register overflows more quickly
  1424. * (not such a big deal) and b) that the increment per tick has
  1425. * to fit into 24 bits. As a result we need to use a shift of
  1426. * 19 so we can fit a value of 16 into the TIMINCA register.
  1427. */
  1428. adapter->cycles.shift = IGB_82576_TSYNC_SHIFT;
  1429. wr32(E1000_TIMINCA,
  1430. (1 << E1000_TIMINCA_16NS_SHIFT) |
  1431. (16 << IGB_82576_TSYNC_SHIFT));
  1432. /* Set registers so that rollover occurs soon to test this. */
  1433. wr32(E1000_SYSTIML, 0x00000000);
  1434. wr32(E1000_SYSTIMH, 0xFF800000);
  1435. wrfl();
  1436. timecounter_init(&adapter->clock,
  1437. &adapter->cycles,
  1438. ktime_to_ns(ktime_get_real()));
  1439. /*
  1440. * Synchronize our NIC clock against system wall clock. NIC
  1441. * time stamp reading requires ~3us per sample, each sample
  1442. * was pretty stable even under load => only require 10
  1443. * samples for each offset comparison.
  1444. */
  1445. memset(&adapter->compare, 0, sizeof(adapter->compare));
  1446. adapter->compare.source = &adapter->clock;
  1447. adapter->compare.target = ktime_get_real;
  1448. adapter->compare.num_samples = 10;
  1449. timecompare_update(&adapter->compare, 0);
  1450. break;
  1451. case e1000_82575:
  1452. /* 82575 does not support timesync */
  1453. default:
  1454. break;
  1455. }
  1456. dev_info(&pdev->dev, "Intel(R) Gigabit Ethernet Network Connection\n");
  1457. /* print bus type/speed/width info */
  1458. dev_info(&pdev->dev, "%s: (PCIe:%s:%s) %pM\n",
  1459. netdev->name,
  1460. ((hw->bus.speed == e1000_bus_speed_2500)
  1461. ? "2.5Gb/s" : "unknown"),
  1462. ((hw->bus.width == e1000_bus_width_pcie_x4) ? "Width x4" :
  1463. (hw->bus.width == e1000_bus_width_pcie_x2) ? "Width x2" :
  1464. (hw->bus.width == e1000_bus_width_pcie_x1) ? "Width x1" :
  1465. "unknown"),
  1466. netdev->dev_addr);
  1467. igb_read_part_num(hw, &part_num);
  1468. dev_info(&pdev->dev, "%s: PBA No: %06x-%03x\n", netdev->name,
  1469. (part_num >> 8), (part_num & 0xff));
  1470. dev_info(&pdev->dev,
  1471. "Using %s interrupts. %d rx queue(s), %d tx queue(s)\n",
  1472. adapter->msix_entries ? "MSI-X" :
  1473. (adapter->flags & IGB_FLAG_HAS_MSI) ? "MSI" : "legacy",
  1474. adapter->num_rx_queues, adapter->num_tx_queues);
  1475. return 0;
  1476. err_register:
  1477. igb_release_hw_control(adapter);
  1478. err_eeprom:
  1479. if (!igb_check_reset_block(hw))
  1480. igb_reset_phy(hw);
  1481. if (hw->flash_address)
  1482. iounmap(hw->flash_address);
  1483. err_sw_init:
  1484. igb_clear_interrupt_scheme(adapter);
  1485. iounmap(hw->hw_addr);
  1486. err_ioremap:
  1487. free_netdev(netdev);
  1488. err_alloc_etherdev:
  1489. pci_release_selected_regions(pdev, pci_select_bars(pdev,
  1490. IORESOURCE_MEM));
  1491. err_pci_reg:
  1492. err_dma:
  1493. pci_disable_device(pdev);
  1494. return err;
  1495. }
  1496. /**
  1497. * igb_remove - Device Removal Routine
  1498. * @pdev: PCI device information struct
  1499. *
  1500. * igb_remove is called by the PCI subsystem to alert the driver
  1501. * that it should release a PCI device. The could be caused by a
  1502. * Hot-Plug event, or because the driver is going to be removed from
  1503. * memory.
  1504. **/
  1505. static void __devexit igb_remove(struct pci_dev *pdev)
  1506. {
  1507. struct net_device *netdev = pci_get_drvdata(pdev);
  1508. struct igb_adapter *adapter = netdev_priv(netdev);
  1509. struct e1000_hw *hw = &adapter->hw;
  1510. /* flush_scheduled work may reschedule our watchdog task, so
  1511. * explicitly disable watchdog tasks from being rescheduled */
  1512. set_bit(__IGB_DOWN, &adapter->state);
  1513. del_timer_sync(&adapter->watchdog_timer);
  1514. del_timer_sync(&adapter->phy_info_timer);
  1515. flush_scheduled_work();
  1516. #ifdef CONFIG_IGB_DCA
  1517. if (adapter->flags & IGB_FLAG_DCA_ENABLED) {
  1518. dev_info(&pdev->dev, "DCA disabled\n");
  1519. dca_remove_requester(&pdev->dev);
  1520. adapter->flags &= ~IGB_FLAG_DCA_ENABLED;
  1521. wr32(E1000_DCA_CTRL, E1000_DCA_CTRL_DCA_MODE_DISABLE);
  1522. }
  1523. #endif
  1524. /* Release control of h/w to f/w. If f/w is AMT enabled, this
  1525. * would have already happened in close and is redundant. */
  1526. igb_release_hw_control(adapter);
  1527. unregister_netdev(netdev);
  1528. if (!igb_check_reset_block(&adapter->hw))
  1529. igb_reset_phy(&adapter->hw);
  1530. igb_clear_interrupt_scheme(adapter);
  1531. #ifdef CONFIG_PCI_IOV
  1532. /* reclaim resources allocated to VFs */
  1533. if (adapter->vf_data) {
  1534. /* disable iov and allow time for transactions to clear */
  1535. pci_disable_sriov(pdev);
  1536. msleep(500);
  1537. kfree(adapter->vf_data);
  1538. adapter->vf_data = NULL;
  1539. wr32(E1000_IOVCTL, E1000_IOVCTL_REUSE_VFQ);
  1540. msleep(100);
  1541. dev_info(&pdev->dev, "IOV Disabled\n");
  1542. }
  1543. #endif
  1544. iounmap(hw->hw_addr);
  1545. if (hw->flash_address)
  1546. iounmap(hw->flash_address);
  1547. pci_release_selected_regions(pdev, pci_select_bars(pdev,
  1548. IORESOURCE_MEM));
  1549. free_netdev(netdev);
  1550. pci_disable_pcie_error_reporting(pdev);
  1551. pci_disable_device(pdev);
  1552. }
  1553. /**
  1554. * igb_sw_init - Initialize general software structures (struct igb_adapter)
  1555. * @adapter: board private structure to initialize
  1556. *
  1557. * igb_sw_init initializes the Adapter private data structure.
  1558. * Fields are initialized based on PCI device information and
  1559. * OS network device settings (MTU size).
  1560. **/
  1561. static int __devinit igb_sw_init(struct igb_adapter *adapter)
  1562. {
  1563. struct e1000_hw *hw = &adapter->hw;
  1564. struct net_device *netdev = adapter->netdev;
  1565. struct pci_dev *pdev = adapter->pdev;
  1566. pci_read_config_word(pdev, PCI_COMMAND, &hw->bus.pci_cmd_word);
  1567. adapter->tx_ring_count = IGB_DEFAULT_TXD;
  1568. adapter->rx_ring_count = IGB_DEFAULT_RXD;
  1569. adapter->rx_itr_setting = IGB_DEFAULT_ITR;
  1570. adapter->tx_itr_setting = IGB_DEFAULT_ITR;
  1571. adapter->max_frame_size = netdev->mtu + ETH_HLEN + ETH_FCS_LEN;
  1572. adapter->min_frame_size = ETH_ZLEN + ETH_FCS_LEN;
  1573. /* This call may decrease the number of queues depending on
  1574. * interrupt mode. */
  1575. if (igb_init_interrupt_scheme(adapter)) {
  1576. dev_err(&pdev->dev, "Unable to allocate memory for queues\n");
  1577. return -ENOMEM;
  1578. }
  1579. /* Explicitly disable IRQ since the NIC can be in any state. */
  1580. igb_irq_disable(adapter);
  1581. set_bit(__IGB_DOWN, &adapter->state);
  1582. return 0;
  1583. }
  1584. /**
  1585. * igb_open - Called when a network interface is made active
  1586. * @netdev: network interface device structure
  1587. *
  1588. * Returns 0 on success, negative value on failure
  1589. *
  1590. * The open entry point is called when a network interface is made
  1591. * active by the system (IFF_UP). At this point all resources needed
  1592. * for transmit and receive operations are allocated, the interrupt
  1593. * handler is registered with the OS, the watchdog timer is started,
  1594. * and the stack is notified that the interface is ready.
  1595. **/
  1596. static int igb_open(struct net_device *netdev)
  1597. {
  1598. struct igb_adapter *adapter = netdev_priv(netdev);
  1599. struct e1000_hw *hw = &adapter->hw;
  1600. int err;
  1601. int i;
  1602. /* disallow open during test */
  1603. if (test_bit(__IGB_TESTING, &adapter->state))
  1604. return -EBUSY;
  1605. netif_carrier_off(netdev);
  1606. /* allocate transmit descriptors */
  1607. err = igb_setup_all_tx_resources(adapter);
  1608. if (err)
  1609. goto err_setup_tx;
  1610. /* allocate receive descriptors */
  1611. err = igb_setup_all_rx_resources(adapter);
  1612. if (err)
  1613. goto err_setup_rx;
  1614. /* e1000_power_up_phy(adapter); */
  1615. adapter->mng_vlan_id = IGB_MNG_VLAN_NONE;
  1616. if ((adapter->hw.mng_cookie.status &
  1617. E1000_MNG_DHCP_COOKIE_STATUS_VLAN))
  1618. igb_update_mng_vlan(adapter);
  1619. /* before we allocate an interrupt, we must be ready to handle it.
  1620. * Setting DEBUG_SHIRQ in the kernel makes it fire an interrupt
  1621. * as soon as we call pci_request_irq, so we have to setup our
  1622. * clean_rx handler before we do so. */
  1623. igb_configure(adapter);
  1624. igb_set_vmolr(hw, adapter->vfs_allocated_count);
  1625. err = igb_request_irq(adapter);
  1626. if (err)
  1627. goto err_req_irq;
  1628. /* From here on the code is the same as igb_up() */
  1629. clear_bit(__IGB_DOWN, &adapter->state);
  1630. for (i = 0; i < adapter->num_q_vectors; i++) {
  1631. struct igb_q_vector *q_vector = adapter->q_vector[i];
  1632. napi_enable(&q_vector->napi);
  1633. }
  1634. /* Clear any pending interrupts. */
  1635. rd32(E1000_ICR);
  1636. igb_irq_enable(adapter);
  1637. /* notify VFs that reset has been completed */
  1638. if (adapter->vfs_allocated_count) {
  1639. u32 reg_data = rd32(E1000_CTRL_EXT);
  1640. reg_data |= E1000_CTRL_EXT_PFRSTD;
  1641. wr32(E1000_CTRL_EXT, reg_data);
  1642. }
  1643. netif_tx_start_all_queues(netdev);
  1644. /* Fire a link status change interrupt to start the watchdog. */
  1645. wr32(E1000_ICS, E1000_ICS_LSC);
  1646. return 0;
  1647. err_req_irq:
  1648. igb_release_hw_control(adapter);
  1649. /* e1000_power_down_phy(adapter); */
  1650. igb_free_all_rx_resources(adapter);
  1651. err_setup_rx:
  1652. igb_free_all_tx_resources(adapter);
  1653. err_setup_tx:
  1654. igb_reset(adapter);
  1655. return err;
  1656. }
  1657. /**
  1658. * igb_close - Disables a network interface
  1659. * @netdev: network interface device structure
  1660. *
  1661. * Returns 0, this is not allowed to fail
  1662. *
  1663. * The close entry point is called when an interface is de-activated
  1664. * by the OS. The hardware is still under the driver's control, but
  1665. * needs to be disabled. A global MAC reset is issued to stop the
  1666. * hardware, and all transmit and receive resources are freed.
  1667. **/
  1668. static int igb_close(struct net_device *netdev)
  1669. {
  1670. struct igb_adapter *adapter = netdev_priv(netdev);
  1671. WARN_ON(test_bit(__IGB_RESETTING, &adapter->state));
  1672. igb_down(adapter);
  1673. igb_free_irq(adapter);
  1674. igb_free_all_tx_resources(adapter);
  1675. igb_free_all_rx_resources(adapter);
  1676. /* kill manageability vlan ID if supported, but not if a vlan with
  1677. * the same ID is registered on the host OS (let 8021q kill it) */
  1678. if ((adapter->hw.mng_cookie.status &
  1679. E1000_MNG_DHCP_COOKIE_STATUS_VLAN) &&
  1680. !(adapter->vlgrp &&
  1681. vlan_group_get_device(adapter->vlgrp, adapter->mng_vlan_id)))
  1682. igb_vlan_rx_kill_vid(netdev, adapter->mng_vlan_id);
  1683. return 0;
  1684. }
  1685. /**
  1686. * igb_setup_tx_resources - allocate Tx resources (Descriptors)
  1687. * @tx_ring: tx descriptor ring (for a specific queue) to setup
  1688. *
  1689. * Return 0 on success, negative on failure
  1690. **/
  1691. int igb_setup_tx_resources(struct igb_ring *tx_ring)
  1692. {
  1693. struct pci_dev *pdev = tx_ring->pdev;
  1694. int size;
  1695. size = sizeof(struct igb_buffer) * tx_ring->count;
  1696. tx_ring->buffer_info = vmalloc(size);
  1697. if (!tx_ring->buffer_info)
  1698. goto err;
  1699. memset(tx_ring->buffer_info, 0, size);
  1700. /* round up to nearest 4K */
  1701. tx_ring->size = tx_ring->count * sizeof(union e1000_adv_tx_desc);
  1702. tx_ring->size = ALIGN(tx_ring->size, 4096);
  1703. tx_ring->desc = pci_alloc_consistent(pdev, tx_ring->size,
  1704. &tx_ring->dma);
  1705. if (!tx_ring->desc)
  1706. goto err;
  1707. tx_ring->next_to_use = 0;
  1708. tx_ring->next_to_clean = 0;
  1709. return 0;
  1710. err:
  1711. vfree(tx_ring->buffer_info);
  1712. dev_err(&pdev->dev,
  1713. "Unable to allocate memory for the transmit descriptor ring\n");
  1714. return -ENOMEM;
  1715. }
  1716. /**
  1717. * igb_setup_all_tx_resources - wrapper to allocate Tx resources
  1718. * (Descriptors) for all queues
  1719. * @adapter: board private structure
  1720. *
  1721. * Return 0 on success, negative on failure
  1722. **/
  1723. static int igb_setup_all_tx_resources(struct igb_adapter *adapter)
  1724. {
  1725. int i, err = 0;
  1726. int r_idx;
  1727. for (i = 0; i < adapter->num_tx_queues; i++) {
  1728. err = igb_setup_tx_resources(&adapter->tx_ring[i]);
  1729. if (err) {
  1730. dev_err(&adapter->pdev->dev,
  1731. "Allocation for Tx Queue %u failed\n", i);
  1732. for (i--; i >= 0; i--)
  1733. igb_free_tx_resources(&adapter->tx_ring[i]);
  1734. break;
  1735. }
  1736. }
  1737. for (i = 0; i < IGB_MAX_TX_QUEUES; i++) {
  1738. r_idx = i % adapter->num_tx_queues;
  1739. adapter->multi_tx_table[i] = &adapter->tx_ring[r_idx];
  1740. }
  1741. return err;
  1742. }
  1743. /**
  1744. * igb_setup_tctl - configure the transmit control registers
  1745. * @adapter: Board private structure
  1746. **/
  1747. void igb_setup_tctl(struct igb_adapter *adapter)
  1748. {
  1749. struct e1000_hw *hw = &adapter->hw;
  1750. u32 tctl;
  1751. /* disable queue 0 which is enabled by default on 82575 and 82576 */
  1752. wr32(E1000_TXDCTL(0), 0);
  1753. /* Program the Transmit Control Register */
  1754. tctl = rd32(E1000_TCTL);
  1755. tctl &= ~E1000_TCTL_CT;
  1756. tctl |= E1000_TCTL_PSP | E1000_TCTL_RTLC |
  1757. (E1000_COLLISION_THRESHOLD << E1000_CT_SHIFT);
  1758. igb_config_collision_dist(hw);
  1759. /* Enable transmits */
  1760. tctl |= E1000_TCTL_EN;
  1761. wr32(E1000_TCTL, tctl);
  1762. }
  1763. /**
  1764. * igb_configure_tx_ring - Configure transmit ring after Reset
  1765. * @adapter: board private structure
  1766. * @ring: tx ring to configure
  1767. *
  1768. * Configure a transmit ring after a reset.
  1769. **/
  1770. void igb_configure_tx_ring(struct igb_adapter *adapter,
  1771. struct igb_ring *ring)
  1772. {
  1773. struct e1000_hw *hw = &adapter->hw;
  1774. u32 txdctl;
  1775. u64 tdba = ring->dma;
  1776. int reg_idx = ring->reg_idx;
  1777. /* disable the queue */
  1778. txdctl = rd32(E1000_TXDCTL(reg_idx));
  1779. wr32(E1000_TXDCTL(reg_idx),
  1780. txdctl & ~E1000_TXDCTL_QUEUE_ENABLE);
  1781. wrfl();
  1782. mdelay(10);
  1783. wr32(E1000_TDLEN(reg_idx),
  1784. ring->count * sizeof(union e1000_adv_tx_desc));
  1785. wr32(E1000_TDBAL(reg_idx),
  1786. tdba & 0x00000000ffffffffULL);
  1787. wr32(E1000_TDBAH(reg_idx), tdba >> 32);
  1788. ring->head = hw->hw_addr + E1000_TDH(reg_idx);
  1789. ring->tail = hw->hw_addr + E1000_TDT(reg_idx);
  1790. writel(0, ring->head);
  1791. writel(0, ring->tail);
  1792. txdctl |= IGB_TX_PTHRESH;
  1793. txdctl |= IGB_TX_HTHRESH << 8;
  1794. txdctl |= IGB_TX_WTHRESH << 16;
  1795. txdctl |= E1000_TXDCTL_QUEUE_ENABLE;
  1796. wr32(E1000_TXDCTL(reg_idx), txdctl);
  1797. }
  1798. /**
  1799. * igb_configure_tx - Configure transmit Unit after Reset
  1800. * @adapter: board private structure
  1801. *
  1802. * Configure the Tx unit of the MAC after a reset.
  1803. **/
  1804. static void igb_configure_tx(struct igb_adapter *adapter)
  1805. {
  1806. int i;
  1807. for (i = 0; i < adapter->num_tx_queues; i++)
  1808. igb_configure_tx_ring(adapter, &adapter->tx_ring[i]);
  1809. }
  1810. /**
  1811. * igb_setup_rx_resources - allocate Rx resources (Descriptors)
  1812. * @rx_ring: rx descriptor ring (for a specific queue) to setup
  1813. *
  1814. * Returns 0 on success, negative on failure
  1815. **/
  1816. int igb_setup_rx_resources(struct igb_ring *rx_ring)
  1817. {
  1818. struct pci_dev *pdev = rx_ring->pdev;
  1819. int size, desc_len;
  1820. size = sizeof(struct igb_buffer) * rx_ring->count;
  1821. rx_ring->buffer_info = vmalloc(size);
  1822. if (!rx_ring->buffer_info)
  1823. goto err;
  1824. memset(rx_ring->buffer_info, 0, size);
  1825. desc_len = sizeof(union e1000_adv_rx_desc);
  1826. /* Round up to nearest 4K */
  1827. rx_ring->size = rx_ring->count * desc_len;
  1828. rx_ring->size = ALIGN(rx_ring->size, 4096);
  1829. rx_ring->desc = pci_alloc_consistent(pdev, rx_ring->size,
  1830. &rx_ring->dma);
  1831. if (!rx_ring->desc)
  1832. goto err;
  1833. rx_ring->next_to_clean = 0;
  1834. rx_ring->next_to_use = 0;
  1835. return 0;
  1836. err:
  1837. vfree(rx_ring->buffer_info);
  1838. dev_err(&pdev->dev, "Unable to allocate memory for "
  1839. "the receive descriptor ring\n");
  1840. return -ENOMEM;
  1841. }
  1842. /**
  1843. * igb_setup_all_rx_resources - wrapper to allocate Rx resources
  1844. * (Descriptors) for all queues
  1845. * @adapter: board private structure
  1846. *
  1847. * Return 0 on success, negative on failure
  1848. **/
  1849. static int igb_setup_all_rx_resources(struct igb_adapter *adapter)
  1850. {
  1851. int i, err = 0;
  1852. for (i = 0; i < adapter->num_rx_queues; i++) {
  1853. err = igb_setup_rx_resources(&adapter->rx_ring[i]);
  1854. if (err) {
  1855. dev_err(&adapter->pdev->dev,
  1856. "Allocation for Rx Queue %u failed\n", i);
  1857. for (i--; i >= 0; i--)
  1858. igb_free_rx_resources(&adapter->rx_ring[i]);
  1859. break;
  1860. }
  1861. }
  1862. return err;
  1863. }
  1864. /**
  1865. * igb_setup_mrqc - configure the multiple receive queue control registers
  1866. * @adapter: Board private structure
  1867. **/
  1868. static void igb_setup_mrqc(struct igb_adapter *adapter)
  1869. {
  1870. struct e1000_hw *hw = &adapter->hw;
  1871. u32 mrqc, rxcsum;
  1872. u32 j, num_rx_queues, shift = 0, shift2 = 0;
  1873. union e1000_reta {
  1874. u32 dword;
  1875. u8 bytes[4];
  1876. } reta;
  1877. static const u8 rsshash[40] = {
  1878. 0x6d, 0x5a, 0x56, 0xda, 0x25, 0x5b, 0x0e, 0xc2, 0x41, 0x67,
  1879. 0x25, 0x3d, 0x43, 0xa3, 0x8f, 0xb0, 0xd0, 0xca, 0x2b, 0xcb,
  1880. 0xae, 0x7b, 0x30, 0xb4, 0x77, 0xcb, 0x2d, 0xa3, 0x80, 0x30,
  1881. 0xf2, 0x0c, 0x6a, 0x42, 0xb7, 0x3b, 0xbe, 0xac, 0x01, 0xfa };
  1882. /* Fill out hash function seeds */
  1883. for (j = 0; j < 10; j++) {
  1884. u32 rsskey = rsshash[(j * 4)];
  1885. rsskey |= rsshash[(j * 4) + 1] << 8;
  1886. rsskey |= rsshash[(j * 4) + 2] << 16;
  1887. rsskey |= rsshash[(j * 4) + 3] << 24;
  1888. array_wr32(E1000_RSSRK(0), j, rsskey);
  1889. }
  1890. num_rx_queues = adapter->num_rx_queues;
  1891. if (adapter->vfs_allocated_count) {
  1892. /* 82575 and 82576 supports 2 RSS queues for VMDq */
  1893. switch (hw->mac.type) {
  1894. case e1000_82576:
  1895. shift = 3;
  1896. num_rx_queues = 2;
  1897. break;
  1898. case e1000_82575:
  1899. shift = 2;
  1900. shift2 = 6;
  1901. default:
  1902. break;
  1903. }
  1904. } else {
  1905. if (hw->mac.type == e1000_82575)
  1906. shift = 6;
  1907. }
  1908. for (j = 0; j < (32 * 4); j++) {
  1909. reta.bytes[j & 3] = (j % num_rx_queues) << shift;
  1910. if (shift2)
  1911. reta.bytes[j & 3] |= num_rx_queues << shift2;
  1912. if ((j & 3) == 3)
  1913. wr32(E1000_RETA(j >> 2), reta.dword);
  1914. }
  1915. /*
  1916. * Disable raw packet checksumming so that RSS hash is placed in
  1917. * descriptor on writeback. No need to enable TCP/UDP/IP checksum
  1918. * offloads as they are enabled by default
  1919. */
  1920. rxcsum = rd32(E1000_RXCSUM);
  1921. rxcsum |= E1000_RXCSUM_PCSD;
  1922. if (adapter->hw.mac.type >= e1000_82576)
  1923. /* Enable Receive Checksum Offload for SCTP */
  1924. rxcsum |= E1000_RXCSUM_CRCOFL;
  1925. /* Don't need to set TUOFL or IPOFL, they default to 1 */
  1926. wr32(E1000_RXCSUM, rxcsum);
  1927. /* If VMDq is enabled then we set the appropriate mode for that, else
  1928. * we default to RSS so that an RSS hash is calculated per packet even
  1929. * if we are only using one queue */
  1930. if (adapter->vfs_allocated_count) {
  1931. if (hw->mac.type > e1000_82575) {
  1932. /* Set the default pool for the PF's first queue */
  1933. u32 vtctl = rd32(E1000_VT_CTL);
  1934. vtctl &= ~(E1000_VT_CTL_DEFAULT_POOL_MASK |
  1935. E1000_VT_CTL_DISABLE_DEF_POOL);
  1936. vtctl |= adapter->vfs_allocated_count <<
  1937. E1000_VT_CTL_DEFAULT_POOL_SHIFT;
  1938. wr32(E1000_VT_CTL, vtctl);
  1939. }
  1940. if (adapter->num_rx_queues > 1)
  1941. mrqc = E1000_MRQC_ENABLE_VMDQ_RSS_2Q;
  1942. else
  1943. mrqc = E1000_MRQC_ENABLE_VMDQ;
  1944. } else {
  1945. mrqc = E1000_MRQC_ENABLE_RSS_4Q;
  1946. }
  1947. igb_vmm_control(adapter);
  1948. mrqc |= (E1000_MRQC_RSS_FIELD_IPV4 |
  1949. E1000_MRQC_RSS_FIELD_IPV4_TCP);
  1950. mrqc |= (E1000_MRQC_RSS_FIELD_IPV6 |
  1951. E1000_MRQC_RSS_FIELD_IPV6_TCP);
  1952. mrqc |= (E1000_MRQC_RSS_FIELD_IPV4_UDP |
  1953. E1000_MRQC_RSS_FIELD_IPV6_UDP);
  1954. mrqc |= (E1000_MRQC_RSS_FIELD_IPV6_UDP_EX |
  1955. E1000_MRQC_RSS_FIELD_IPV6_TCP_EX);
  1956. wr32(E1000_MRQC, mrqc);
  1957. }
  1958. /**
  1959. * igb_setup_rctl - configure the receive control registers
  1960. * @adapter: Board private structure
  1961. **/
  1962. void igb_setup_rctl(struct igb_adapter *adapter)
  1963. {
  1964. struct e1000_hw *hw = &adapter->hw;
  1965. u32 rctl;
  1966. rctl = rd32(E1000_RCTL);
  1967. rctl &= ~(3 << E1000_RCTL_MO_SHIFT);
  1968. rctl &= ~(E1000_RCTL_LBM_TCVR | E1000_RCTL_LBM_MAC);
  1969. rctl |= E1000_RCTL_EN | E1000_RCTL_BAM | E1000_RCTL_RDMTS_HALF |
  1970. (hw->mac.mc_filter_type << E1000_RCTL_MO_SHIFT);
  1971. /*
  1972. * enable stripping of CRC. It's unlikely this will break BMC
  1973. * redirection as it did with e1000. Newer features require
  1974. * that the HW strips the CRC.
  1975. */
  1976. rctl |= E1000_RCTL_SECRC;
  1977. /*
  1978. * disable store bad packets and clear size bits.
  1979. */
  1980. rctl &= ~(E1000_RCTL_SBP | E1000_RCTL_SZ_256);
  1981. /* enable LPE to prevent packets larger than max_frame_size */
  1982. rctl |= E1000_RCTL_LPE;
  1983. /* disable queue 0 to prevent tail write w/o re-config */
  1984. wr32(E1000_RXDCTL(0), 0);
  1985. /* Attention!!! For SR-IOV PF driver operations you must enable
  1986. * queue drop for all VF and PF queues to prevent head of line blocking
  1987. * if an un-trusted VF does not provide descriptors to hardware.
  1988. */
  1989. if (adapter->vfs_allocated_count) {
  1990. u32 vmolr;
  1991. /* set all queue drop enable bits */
  1992. wr32(E1000_QDE, ALL_QUEUES);
  1993. vmolr = rd32(E1000_VMOLR(adapter->vfs_allocated_count));
  1994. if (rctl & E1000_RCTL_LPE)
  1995. vmolr |= E1000_VMOLR_LPE;
  1996. if (adapter->num_rx_queues > 1)
  1997. vmolr |= E1000_VMOLR_RSSE;
  1998. wr32(E1000_VMOLR(adapter->vfs_allocated_count), vmolr);
  1999. }
  2000. wr32(E1000_RCTL, rctl);
  2001. }
  2002. /**
  2003. * igb_rlpml_set - set maximum receive packet size
  2004. * @adapter: board private structure
  2005. *
  2006. * Configure maximum receivable packet size.
  2007. **/
  2008. static void igb_rlpml_set(struct igb_adapter *adapter)
  2009. {
  2010. u32 max_frame_size = adapter->max_frame_size;
  2011. struct e1000_hw *hw = &adapter->hw;
  2012. u16 pf_id = adapter->vfs_allocated_count;
  2013. if (adapter->vlgrp)
  2014. max_frame_size += VLAN_TAG_SIZE;
  2015. /* if vfs are enabled we set RLPML to the largest possible request
  2016. * size and set the VMOLR RLPML to the size we need */
  2017. if (pf_id) {
  2018. igb_set_vf_rlpml(adapter, max_frame_size, pf_id);
  2019. max_frame_size = MAX_STD_JUMBO_FRAME_SIZE + VLAN_TAG_SIZE;
  2020. }
  2021. wr32(E1000_RLPML, max_frame_size);
  2022. }
  2023. /**
  2024. * igb_configure_rx_ring - Configure a receive ring after Reset
  2025. * @adapter: board private structure
  2026. * @ring: receive ring to be configured
  2027. *
  2028. * Configure the Rx unit of the MAC after a reset.
  2029. **/
  2030. void igb_configure_rx_ring(struct igb_adapter *adapter,
  2031. struct igb_ring *ring)
  2032. {
  2033. struct e1000_hw *hw = &adapter->hw;
  2034. u64 rdba = ring->dma;
  2035. int reg_idx = ring->reg_idx;
  2036. u32 srrctl, rxdctl;
  2037. /* disable the queue */
  2038. rxdctl = rd32(E1000_RXDCTL(reg_idx));
  2039. wr32(E1000_RXDCTL(reg_idx),
  2040. rxdctl & ~E1000_RXDCTL_QUEUE_ENABLE);
  2041. /* Set DMA base address registers */
  2042. wr32(E1000_RDBAL(reg_idx),
  2043. rdba & 0x00000000ffffffffULL);
  2044. wr32(E1000_RDBAH(reg_idx), rdba >> 32);
  2045. wr32(E1000_RDLEN(reg_idx),
  2046. ring->count * sizeof(union e1000_adv_rx_desc));
  2047. /* initialize head and tail */
  2048. ring->head = hw->hw_addr + E1000_RDH(reg_idx);
  2049. ring->tail = hw->hw_addr + E1000_RDT(reg_idx);
  2050. writel(0, ring->head);
  2051. writel(0, ring->tail);
  2052. /* set descriptor configuration */
  2053. if (ring->rx_buffer_len < IGB_RXBUFFER_1024) {
  2054. srrctl = ALIGN(ring->rx_buffer_len, 64) <<
  2055. E1000_SRRCTL_BSIZEHDRSIZE_SHIFT;
  2056. #if (PAGE_SIZE / 2) > IGB_RXBUFFER_16384
  2057. srrctl |= IGB_RXBUFFER_16384 >>
  2058. E1000_SRRCTL_BSIZEPKT_SHIFT;
  2059. #else
  2060. srrctl |= (PAGE_SIZE / 2) >>
  2061. E1000_SRRCTL_BSIZEPKT_SHIFT;
  2062. #endif
  2063. srrctl |= E1000_SRRCTL_DESCTYPE_HDR_SPLIT_ALWAYS;
  2064. } else {
  2065. srrctl = ALIGN(ring->rx_buffer_len, 1024) >>
  2066. E1000_SRRCTL_BSIZEPKT_SHIFT;
  2067. srrctl |= E1000_SRRCTL_DESCTYPE_ADV_ONEBUF;
  2068. }
  2069. wr32(E1000_SRRCTL(reg_idx), srrctl);
  2070. /* enable receive descriptor fetching */
  2071. rxdctl = rd32(E1000_RXDCTL(reg_idx));
  2072. rxdctl |= E1000_RXDCTL_QUEUE_ENABLE;
  2073. rxdctl &= 0xFFF00000;
  2074. rxdctl |= IGB_RX_PTHRESH;
  2075. rxdctl |= IGB_RX_HTHRESH << 8;
  2076. rxdctl |= IGB_RX_WTHRESH << 16;
  2077. wr32(E1000_RXDCTL(reg_idx), rxdctl);
  2078. }
  2079. /**
  2080. * igb_configure_rx - Configure receive Unit after Reset
  2081. * @adapter: board private structure
  2082. *
  2083. * Configure the Rx unit of the MAC after a reset.
  2084. **/
  2085. static void igb_configure_rx(struct igb_adapter *adapter)
  2086. {
  2087. int i;
  2088. /* set UTA to appropriate mode */
  2089. igb_set_uta(adapter);
  2090. /* set the correct pool for the PF default MAC address in entry 0 */
  2091. igb_rar_set_qsel(adapter, adapter->hw.mac.addr, 0,
  2092. adapter->vfs_allocated_count);
  2093. /* Setup the HW Rx Head and Tail Descriptor Pointers and
  2094. * the Base and Length of the Rx Descriptor Ring */
  2095. for (i = 0; i < adapter->num_rx_queues; i++)
  2096. igb_configure_rx_ring(adapter, &adapter->rx_ring[i]);
  2097. }
  2098. /**
  2099. * igb_free_tx_resources - Free Tx Resources per Queue
  2100. * @tx_ring: Tx descriptor ring for a specific queue
  2101. *
  2102. * Free all transmit software resources
  2103. **/
  2104. void igb_free_tx_resources(struct igb_ring *tx_ring)
  2105. {
  2106. igb_clean_tx_ring(tx_ring);
  2107. vfree(tx_ring->buffer_info);
  2108. tx_ring->buffer_info = NULL;
  2109. pci_free_consistent(tx_ring->pdev, tx_ring->size,
  2110. tx_ring->desc, tx_ring->dma);
  2111. tx_ring->desc = NULL;
  2112. }
  2113. /**
  2114. * igb_free_all_tx_resources - Free Tx Resources for All Queues
  2115. * @adapter: board private structure
  2116. *
  2117. * Free all transmit software resources
  2118. **/
  2119. static void igb_free_all_tx_resources(struct igb_adapter *adapter)
  2120. {
  2121. int i;
  2122. for (i = 0; i < adapter->num_tx_queues; i++)
  2123. igb_free_tx_resources(&adapter->tx_ring[i]);
  2124. }
  2125. void igb_unmap_and_free_tx_resource(struct igb_ring *tx_ring,
  2126. struct igb_buffer *buffer_info)
  2127. {
  2128. buffer_info->dma = 0;
  2129. if (buffer_info->skb) {
  2130. skb_dma_unmap(&tx_ring->pdev->dev,
  2131. buffer_info->skb,
  2132. DMA_TO_DEVICE);
  2133. dev_kfree_skb_any(buffer_info->skb);
  2134. buffer_info->skb = NULL;
  2135. }
  2136. buffer_info->time_stamp = 0;
  2137. /* buffer_info must be completely set up in the transmit path */
  2138. }
  2139. /**
  2140. * igb_clean_tx_ring - Free Tx Buffers
  2141. * @tx_ring: ring to be cleaned
  2142. **/
  2143. static void igb_clean_tx_ring(struct igb_ring *tx_ring)
  2144. {
  2145. struct igb_buffer *buffer_info;
  2146. unsigned long size;
  2147. unsigned int i;
  2148. if (!tx_ring->buffer_info)
  2149. return;
  2150. /* Free all the Tx ring sk_buffs */
  2151. for (i = 0; i < tx_ring->count; i++) {
  2152. buffer_info = &tx_ring->buffer_info[i];
  2153. igb_unmap_and_free_tx_resource(tx_ring, buffer_info);
  2154. }
  2155. size = sizeof(struct igb_buffer) * tx_ring->count;
  2156. memset(tx_ring->buffer_info, 0, size);
  2157. /* Zero out the descriptor ring */
  2158. memset(tx_ring->desc, 0, tx_ring->size);
  2159. tx_ring->next_to_use = 0;
  2160. tx_ring->next_to_clean = 0;
  2161. writel(0, tx_ring->head);
  2162. writel(0, tx_ring->tail);
  2163. }
  2164. /**
  2165. * igb_clean_all_tx_rings - Free Tx Buffers for all queues
  2166. * @adapter: board private structure
  2167. **/
  2168. static void igb_clean_all_tx_rings(struct igb_adapter *adapter)
  2169. {
  2170. int i;
  2171. for (i = 0; i < adapter->num_tx_queues; i++)
  2172. igb_clean_tx_ring(&adapter->tx_ring[i]);
  2173. }
  2174. /**
  2175. * igb_free_rx_resources - Free Rx Resources
  2176. * @rx_ring: ring to clean the resources from
  2177. *
  2178. * Free all receive software resources
  2179. **/
  2180. void igb_free_rx_resources(struct igb_ring *rx_ring)
  2181. {
  2182. igb_clean_rx_ring(rx_ring);
  2183. vfree(rx_ring->buffer_info);
  2184. rx_ring->buffer_info = NULL;
  2185. pci_free_consistent(rx_ring->pdev, rx_ring->size,
  2186. rx_ring->desc, rx_ring->dma);
  2187. rx_ring->desc = NULL;
  2188. }
  2189. /**
  2190. * igb_free_all_rx_resources - Free Rx Resources for All Queues
  2191. * @adapter: board private structure
  2192. *
  2193. * Free all receive software resources
  2194. **/
  2195. static void igb_free_all_rx_resources(struct igb_adapter *adapter)
  2196. {
  2197. int i;
  2198. for (i = 0; i < adapter->num_rx_queues; i++)
  2199. igb_free_rx_resources(&adapter->rx_ring[i]);
  2200. }
  2201. /**
  2202. * igb_clean_rx_ring - Free Rx Buffers per Queue
  2203. * @rx_ring: ring to free buffers from
  2204. **/
  2205. static void igb_clean_rx_ring(struct igb_ring *rx_ring)
  2206. {
  2207. struct igb_buffer *buffer_info;
  2208. unsigned long size;
  2209. unsigned int i;
  2210. if (!rx_ring->buffer_info)
  2211. return;
  2212. /* Free all the Rx ring sk_buffs */
  2213. for (i = 0; i < rx_ring->count; i++) {
  2214. buffer_info = &rx_ring->buffer_info[i];
  2215. if (buffer_info->dma) {
  2216. pci_unmap_single(rx_ring->pdev,
  2217. buffer_info->dma,
  2218. rx_ring->rx_buffer_len,
  2219. PCI_DMA_FROMDEVICE);
  2220. buffer_info->dma = 0;
  2221. }
  2222. if (buffer_info->skb) {
  2223. dev_kfree_skb(buffer_info->skb);
  2224. buffer_info->skb = NULL;
  2225. }
  2226. if (buffer_info->page_dma) {
  2227. pci_unmap_page(rx_ring->pdev,
  2228. buffer_info->page_dma,
  2229. PAGE_SIZE / 2,
  2230. PCI_DMA_FROMDEVICE);
  2231. buffer_info->page_dma = 0;
  2232. }
  2233. if (buffer_info->page) {
  2234. put_page(buffer_info->page);
  2235. buffer_info->page = NULL;
  2236. buffer_info->page_offset = 0;
  2237. }
  2238. }
  2239. size = sizeof(struct igb_buffer) * rx_ring->count;
  2240. memset(rx_ring->buffer_info, 0, size);
  2241. /* Zero out the descriptor ring */
  2242. memset(rx_ring->desc, 0, rx_ring->size);
  2243. rx_ring->next_to_clean = 0;
  2244. rx_ring->next_to_use = 0;
  2245. writel(0, rx_ring->head);
  2246. writel(0, rx_ring->tail);
  2247. }
  2248. /**
  2249. * igb_clean_all_rx_rings - Free Rx Buffers for all queues
  2250. * @adapter: board private structure
  2251. **/
  2252. static void igb_clean_all_rx_rings(struct igb_adapter *adapter)
  2253. {
  2254. int i;
  2255. for (i = 0; i < adapter->num_rx_queues; i++)
  2256. igb_clean_rx_ring(&adapter->rx_ring[i]);
  2257. }
  2258. /**
  2259. * igb_set_mac - Change the Ethernet Address of the NIC
  2260. * @netdev: network interface device structure
  2261. * @p: pointer to an address structure
  2262. *
  2263. * Returns 0 on success, negative on failure
  2264. **/
  2265. static int igb_set_mac(struct net_device *netdev, void *p)
  2266. {
  2267. struct igb_adapter *adapter = netdev_priv(netdev);
  2268. struct e1000_hw *hw = &adapter->hw;
  2269. struct sockaddr *addr = p;
  2270. if (!is_valid_ether_addr(addr->sa_data))
  2271. return -EADDRNOTAVAIL;
  2272. memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
  2273. memcpy(hw->mac.addr, addr->sa_data, netdev->addr_len);
  2274. /* set the correct pool for the new PF MAC address in entry 0 */
  2275. igb_rar_set_qsel(adapter, hw->mac.addr, 0,
  2276. adapter->vfs_allocated_count);
  2277. return 0;
  2278. }
  2279. /**
  2280. * igb_write_mc_addr_list - write multicast addresses to MTA
  2281. * @netdev: network interface device structure
  2282. *
  2283. * Writes multicast address list to the MTA hash table.
  2284. * Returns: -ENOMEM on failure
  2285. * 0 on no addresses written
  2286. * X on writing X addresses to MTA
  2287. **/
  2288. static int igb_write_mc_addr_list(struct net_device *netdev)
  2289. {
  2290. struct igb_adapter *adapter = netdev_priv(netdev);
  2291. struct e1000_hw *hw = &adapter->hw;
  2292. struct dev_mc_list *mc_ptr = netdev->mc_list;
  2293. u8 *mta_list;
  2294. u32 vmolr = 0;
  2295. int i;
  2296. if (!netdev->mc_count) {
  2297. /* nothing to program, so clear mc list */
  2298. igb_update_mc_addr_list(hw, NULL, 0);
  2299. igb_restore_vf_multicasts(adapter);
  2300. return 0;
  2301. }
  2302. mta_list = kzalloc(netdev->mc_count * 6, GFP_ATOMIC);
  2303. if (!mta_list)
  2304. return -ENOMEM;
  2305. /* set vmolr receive overflow multicast bit */
  2306. vmolr |= E1000_VMOLR_ROMPE;
  2307. /* The shared function expects a packed array of only addresses. */
  2308. mc_ptr = netdev->mc_list;
  2309. for (i = 0; i < netdev->mc_count; i++) {
  2310. if (!mc_ptr)
  2311. break;
  2312. memcpy(mta_list + (i*ETH_ALEN), mc_ptr->dmi_addr, ETH_ALEN);
  2313. mc_ptr = mc_ptr->next;
  2314. }
  2315. igb_update_mc_addr_list(hw, mta_list, i);
  2316. kfree(mta_list);
  2317. return netdev->mc_count;
  2318. }
  2319. /**
  2320. * igb_write_uc_addr_list - write unicast addresses to RAR table
  2321. * @netdev: network interface device structure
  2322. *
  2323. * Writes unicast address list to the RAR table.
  2324. * Returns: -ENOMEM on failure/insufficient address space
  2325. * 0 on no addresses written
  2326. * X on writing X addresses to the RAR table
  2327. **/
  2328. static int igb_write_uc_addr_list(struct net_device *netdev)
  2329. {
  2330. struct igb_adapter *adapter = netdev_priv(netdev);
  2331. struct e1000_hw *hw = &adapter->hw;
  2332. unsigned int vfn = adapter->vfs_allocated_count;
  2333. unsigned int rar_entries = hw->mac.rar_entry_count - (vfn + 1);
  2334. int count = 0;
  2335. /* return ENOMEM indicating insufficient memory for addresses */
  2336. if (netdev->uc.count > rar_entries)
  2337. return -ENOMEM;
  2338. if (netdev->uc.count && rar_entries) {
  2339. struct netdev_hw_addr *ha;
  2340. list_for_each_entry(ha, &netdev->uc.list, list) {
  2341. if (!rar_entries)
  2342. break;
  2343. igb_rar_set_qsel(adapter, ha->addr,
  2344. rar_entries--,
  2345. vfn);
  2346. count++;
  2347. }
  2348. }
  2349. /* write the addresses in reverse order to avoid write combining */
  2350. for (; rar_entries > 0 ; rar_entries--) {
  2351. wr32(E1000_RAH(rar_entries), 0);
  2352. wr32(E1000_RAL(rar_entries), 0);
  2353. }
  2354. wrfl();
  2355. return count;
  2356. }
  2357. /**
  2358. * igb_set_rx_mode - Secondary Unicast, Multicast and Promiscuous mode set
  2359. * @netdev: network interface device structure
  2360. *
  2361. * The set_rx_mode entry point is called whenever the unicast or multicast
  2362. * address lists or the network interface flags are updated. This routine is
  2363. * responsible for configuring the hardware for proper unicast, multicast,
  2364. * promiscuous mode, and all-multi behavior.
  2365. **/
  2366. static void igb_set_rx_mode(struct net_device *netdev)
  2367. {
  2368. struct igb_adapter *adapter = netdev_priv(netdev);
  2369. struct e1000_hw *hw = &adapter->hw;
  2370. unsigned int vfn = adapter->vfs_allocated_count;
  2371. u32 rctl, vmolr = 0;
  2372. int count;
  2373. /* Check for Promiscuous and All Multicast modes */
  2374. rctl = rd32(E1000_RCTL);
  2375. /* clear the effected bits */
  2376. rctl &= ~(E1000_RCTL_UPE | E1000_RCTL_MPE | E1000_RCTL_VFE);
  2377. if (netdev->flags & IFF_PROMISC) {
  2378. rctl |= (E1000_RCTL_UPE | E1000_RCTL_MPE);
  2379. vmolr |= (E1000_VMOLR_ROPE | E1000_VMOLR_MPME);
  2380. } else {
  2381. if (netdev->flags & IFF_ALLMULTI) {
  2382. rctl |= E1000_RCTL_MPE;
  2383. vmolr |= E1000_VMOLR_MPME;
  2384. } else {
  2385. /*
  2386. * Write addresses to the MTA, if the attempt fails
  2387. * then we should just turn on promiscous mode so
  2388. * that we can at least receive multicast traffic
  2389. */
  2390. count = igb_write_mc_addr_list(netdev);
  2391. if (count < 0) {
  2392. rctl |= E1000_RCTL_MPE;
  2393. vmolr |= E1000_VMOLR_MPME;
  2394. } else if (count) {
  2395. vmolr |= E1000_VMOLR_ROMPE;
  2396. }
  2397. }
  2398. /*
  2399. * Write addresses to available RAR registers, if there is not
  2400. * sufficient space to store all the addresses then enable
  2401. * unicast promiscous mode
  2402. */
  2403. count = igb_write_uc_addr_list(netdev);
  2404. if (count < 0) {
  2405. rctl |= E1000_RCTL_UPE;
  2406. vmolr |= E1000_VMOLR_ROPE;
  2407. }
  2408. rctl |= E1000_RCTL_VFE;
  2409. }
  2410. wr32(E1000_RCTL, rctl);
  2411. /*
  2412. * In order to support SR-IOV and eventually VMDq it is necessary to set
  2413. * the VMOLR to enable the appropriate modes. Without this workaround
  2414. * we will have issues with VLAN tag stripping not being done for frames
  2415. * that are only arriving because we are the default pool
  2416. */
  2417. if (hw->mac.type < e1000_82576)
  2418. return;
  2419. vmolr |= rd32(E1000_VMOLR(vfn)) &
  2420. ~(E1000_VMOLR_ROPE | E1000_VMOLR_MPME | E1000_VMOLR_ROMPE);
  2421. wr32(E1000_VMOLR(vfn), vmolr);
  2422. igb_restore_vf_multicasts(adapter);
  2423. }
  2424. /* Need to wait a few seconds after link up to get diagnostic information from
  2425. * the phy */
  2426. static void igb_update_phy_info(unsigned long data)
  2427. {
  2428. struct igb_adapter *adapter = (struct igb_adapter *) data;
  2429. igb_get_phy_info(&adapter->hw);
  2430. }
  2431. /**
  2432. * igb_has_link - check shared code for link and determine up/down
  2433. * @adapter: pointer to driver private info
  2434. **/
  2435. static bool igb_has_link(struct igb_adapter *adapter)
  2436. {
  2437. struct e1000_hw *hw = &adapter->hw;
  2438. bool link_active = false;
  2439. s32 ret_val = 0;
  2440. /* get_link_status is set on LSC (link status) interrupt or
  2441. * rx sequence error interrupt. get_link_status will stay
  2442. * false until the e1000_check_for_link establishes link
  2443. * for copper adapters ONLY
  2444. */
  2445. switch (hw->phy.media_type) {
  2446. case e1000_media_type_copper:
  2447. if (hw->mac.get_link_status) {
  2448. ret_val = hw->mac.ops.check_for_link(hw);
  2449. link_active = !hw->mac.get_link_status;
  2450. } else {
  2451. link_active = true;
  2452. }
  2453. break;
  2454. case e1000_media_type_internal_serdes:
  2455. ret_val = hw->mac.ops.check_for_link(hw);
  2456. link_active = hw->mac.serdes_has_link;
  2457. break;
  2458. default:
  2459. case e1000_media_type_unknown:
  2460. break;
  2461. }
  2462. return link_active;
  2463. }
  2464. /**
  2465. * igb_watchdog - Timer Call-back
  2466. * @data: pointer to adapter cast into an unsigned long
  2467. **/
  2468. static void igb_watchdog(unsigned long data)
  2469. {
  2470. struct igb_adapter *adapter = (struct igb_adapter *)data;
  2471. /* Do the rest outside of interrupt context */
  2472. schedule_work(&adapter->watchdog_task);
  2473. }
  2474. static void igb_watchdog_task(struct work_struct *work)
  2475. {
  2476. struct igb_adapter *adapter = container_of(work,
  2477. struct igb_adapter, watchdog_task);
  2478. struct e1000_hw *hw = &adapter->hw;
  2479. struct net_device *netdev = adapter->netdev;
  2480. struct igb_ring *tx_ring = adapter->tx_ring;
  2481. u32 link;
  2482. int i;
  2483. link = igb_has_link(adapter);
  2484. if ((netif_carrier_ok(netdev)) && link)
  2485. goto link_up;
  2486. if (link) {
  2487. if (!netif_carrier_ok(netdev)) {
  2488. u32 ctrl;
  2489. hw->mac.ops.get_speed_and_duplex(&adapter->hw,
  2490. &adapter->link_speed,
  2491. &adapter->link_duplex);
  2492. ctrl = rd32(E1000_CTRL);
  2493. /* Links status message must follow this format */
  2494. printk(KERN_INFO "igb: %s NIC Link is Up %d Mbps %s, "
  2495. "Flow Control: %s\n",
  2496. netdev->name,
  2497. adapter->link_speed,
  2498. adapter->link_duplex == FULL_DUPLEX ?
  2499. "Full Duplex" : "Half Duplex",
  2500. ((ctrl & E1000_CTRL_TFCE) && (ctrl &
  2501. E1000_CTRL_RFCE)) ? "RX/TX" : ((ctrl &
  2502. E1000_CTRL_RFCE) ? "RX" : ((ctrl &
  2503. E1000_CTRL_TFCE) ? "TX" : "None")));
  2504. /* tweak tx_queue_len according to speed/duplex and
  2505. * adjust the timeout factor */
  2506. netdev->tx_queue_len = adapter->tx_queue_len;
  2507. adapter->tx_timeout_factor = 1;
  2508. switch (adapter->link_speed) {
  2509. case SPEED_10:
  2510. netdev->tx_queue_len = 10;
  2511. adapter->tx_timeout_factor = 14;
  2512. break;
  2513. case SPEED_100:
  2514. netdev->tx_queue_len = 100;
  2515. /* maybe add some timeout factor ? */
  2516. break;
  2517. }
  2518. netif_carrier_on(netdev);
  2519. igb_ping_all_vfs(adapter);
  2520. /* link state has changed, schedule phy info update */
  2521. if (!test_bit(__IGB_DOWN, &adapter->state))
  2522. mod_timer(&adapter->phy_info_timer,
  2523. round_jiffies(jiffies + 2 * HZ));
  2524. }
  2525. } else {
  2526. if (netif_carrier_ok(netdev)) {
  2527. adapter->link_speed = 0;
  2528. adapter->link_duplex = 0;
  2529. /* Links status message must follow this format */
  2530. printk(KERN_INFO "igb: %s NIC Link is Down\n",
  2531. netdev->name);
  2532. netif_carrier_off(netdev);
  2533. igb_ping_all_vfs(adapter);
  2534. /* link state has changed, schedule phy info update */
  2535. if (!test_bit(__IGB_DOWN, &adapter->state))
  2536. mod_timer(&adapter->phy_info_timer,
  2537. round_jiffies(jiffies + 2 * HZ));
  2538. }
  2539. }
  2540. link_up:
  2541. igb_update_stats(adapter);
  2542. hw->mac.tx_packet_delta = adapter->stats.tpt - adapter->tpt_old;
  2543. adapter->tpt_old = adapter->stats.tpt;
  2544. hw->mac.collision_delta = adapter->stats.colc - adapter->colc_old;
  2545. adapter->colc_old = adapter->stats.colc;
  2546. adapter->gorc = adapter->stats.gorc - adapter->gorc_old;
  2547. adapter->gorc_old = adapter->stats.gorc;
  2548. adapter->gotc = adapter->stats.gotc - adapter->gotc_old;
  2549. adapter->gotc_old = adapter->stats.gotc;
  2550. igb_update_adaptive(&adapter->hw);
  2551. if (!netif_carrier_ok(netdev)) {
  2552. if (igb_desc_unused(tx_ring) + 1 < tx_ring->count) {
  2553. /* We've lost link, so the controller stops DMA,
  2554. * but we've got queued Tx work that's never going
  2555. * to get done, so reset controller to flush Tx.
  2556. * (Do the reset outside of interrupt context). */
  2557. adapter->tx_timeout_count++;
  2558. schedule_work(&adapter->reset_task);
  2559. /* return immediately since reset is imminent */
  2560. return;
  2561. }
  2562. }
  2563. /* Cause software interrupt to ensure rx ring is cleaned */
  2564. if (adapter->msix_entries) {
  2565. u32 eics = 0;
  2566. for (i = 0; i < adapter->num_q_vectors; i++) {
  2567. struct igb_q_vector *q_vector = adapter->q_vector[i];
  2568. eics |= q_vector->eims_value;
  2569. }
  2570. wr32(E1000_EICS, eics);
  2571. } else {
  2572. wr32(E1000_ICS, E1000_ICS_RXDMT0);
  2573. }
  2574. /* Force detection of hung controller every watchdog period */
  2575. tx_ring->detect_tx_hung = true;
  2576. /* Reset the timer */
  2577. if (!test_bit(__IGB_DOWN, &adapter->state))
  2578. mod_timer(&adapter->watchdog_timer,
  2579. round_jiffies(jiffies + 2 * HZ));
  2580. }
  2581. enum latency_range {
  2582. lowest_latency = 0,
  2583. low_latency = 1,
  2584. bulk_latency = 2,
  2585. latency_invalid = 255
  2586. };
  2587. /**
  2588. * igb_update_ring_itr - update the dynamic ITR value based on packet size
  2589. *
  2590. * Stores a new ITR value based on strictly on packet size. This
  2591. * algorithm is less sophisticated than that used in igb_update_itr,
  2592. * due to the difficulty of synchronizing statistics across multiple
  2593. * receive rings. The divisors and thresholds used by this fuction
  2594. * were determined based on theoretical maximum wire speed and testing
  2595. * data, in order to minimize response time while increasing bulk
  2596. * throughput.
  2597. * This functionality is controlled by the InterruptThrottleRate module
  2598. * parameter (see igb_param.c)
  2599. * NOTE: This function is called only when operating in a multiqueue
  2600. * receive environment.
  2601. * @q_vector: pointer to q_vector
  2602. **/
  2603. static void igb_update_ring_itr(struct igb_q_vector *q_vector)
  2604. {
  2605. int new_val = q_vector->itr_val;
  2606. int avg_wire_size = 0;
  2607. struct igb_adapter *adapter = q_vector->adapter;
  2608. /* For non-gigabit speeds, just fix the interrupt rate at 4000
  2609. * ints/sec - ITR timer value of 120 ticks.
  2610. */
  2611. if (adapter->link_speed != SPEED_1000) {
  2612. new_val = 976;
  2613. goto set_itr_val;
  2614. }
  2615. if (q_vector->rx_ring && q_vector->rx_ring->total_packets) {
  2616. struct igb_ring *ring = q_vector->rx_ring;
  2617. avg_wire_size = ring->total_bytes / ring->total_packets;
  2618. }
  2619. if (q_vector->tx_ring && q_vector->tx_ring->total_packets) {
  2620. struct igb_ring *ring = q_vector->tx_ring;
  2621. avg_wire_size = max_t(u32, avg_wire_size,
  2622. (ring->total_bytes /
  2623. ring->total_packets));
  2624. }
  2625. /* if avg_wire_size isn't set no work was done */
  2626. if (!avg_wire_size)
  2627. goto clear_counts;
  2628. /* Add 24 bytes to size to account for CRC, preamble, and gap */
  2629. avg_wire_size += 24;
  2630. /* Don't starve jumbo frames */
  2631. avg_wire_size = min(avg_wire_size, 3000);
  2632. /* Give a little boost to mid-size frames */
  2633. if ((avg_wire_size > 300) && (avg_wire_size < 1200))
  2634. new_val = avg_wire_size / 3;
  2635. else
  2636. new_val = avg_wire_size / 2;
  2637. set_itr_val:
  2638. if (new_val != q_vector->itr_val) {
  2639. q_vector->itr_val = new_val;
  2640. q_vector->set_itr = 1;
  2641. }
  2642. clear_counts:
  2643. if (q_vector->rx_ring) {
  2644. q_vector->rx_ring->total_bytes = 0;
  2645. q_vector->rx_ring->total_packets = 0;
  2646. }
  2647. if (q_vector->tx_ring) {
  2648. q_vector->tx_ring->total_bytes = 0;
  2649. q_vector->tx_ring->total_packets = 0;
  2650. }
  2651. }
  2652. /**
  2653. * igb_update_itr - update the dynamic ITR value based on statistics
  2654. * Stores a new ITR value based on packets and byte
  2655. * counts during the last interrupt. The advantage of per interrupt
  2656. * computation is faster updates and more accurate ITR for the current
  2657. * traffic pattern. Constants in this function were computed
  2658. * based on theoretical maximum wire speed and thresholds were set based
  2659. * on testing data as well as attempting to minimize response time
  2660. * while increasing bulk throughput.
  2661. * this functionality is controlled by the InterruptThrottleRate module
  2662. * parameter (see igb_param.c)
  2663. * NOTE: These calculations are only valid when operating in a single-
  2664. * queue environment.
  2665. * @adapter: pointer to adapter
  2666. * @itr_setting: current q_vector->itr_val
  2667. * @packets: the number of packets during this measurement interval
  2668. * @bytes: the number of bytes during this measurement interval
  2669. **/
  2670. static unsigned int igb_update_itr(struct igb_adapter *adapter, u16 itr_setting,
  2671. int packets, int bytes)
  2672. {
  2673. unsigned int retval = itr_setting;
  2674. if (packets == 0)
  2675. goto update_itr_done;
  2676. switch (itr_setting) {
  2677. case lowest_latency:
  2678. /* handle TSO and jumbo frames */
  2679. if (bytes/packets > 8000)
  2680. retval = bulk_latency;
  2681. else if ((packets < 5) && (bytes > 512))
  2682. retval = low_latency;
  2683. break;
  2684. case low_latency: /* 50 usec aka 20000 ints/s */
  2685. if (bytes > 10000) {
  2686. /* this if handles the TSO accounting */
  2687. if (bytes/packets > 8000) {
  2688. retval = bulk_latency;
  2689. } else if ((packets < 10) || ((bytes/packets) > 1200)) {
  2690. retval = bulk_latency;
  2691. } else if ((packets > 35)) {
  2692. retval = lowest_latency;
  2693. }
  2694. } else if (bytes/packets > 2000) {
  2695. retval = bulk_latency;
  2696. } else if (packets <= 2 && bytes < 512) {
  2697. retval = lowest_latency;
  2698. }
  2699. break;
  2700. case bulk_latency: /* 250 usec aka 4000 ints/s */
  2701. if (bytes > 25000) {
  2702. if (packets > 35)
  2703. retval = low_latency;
  2704. } else if (bytes < 1500) {
  2705. retval = low_latency;
  2706. }
  2707. break;
  2708. }
  2709. update_itr_done:
  2710. return retval;
  2711. }
  2712. static void igb_set_itr(struct igb_adapter *adapter)
  2713. {
  2714. struct igb_q_vector *q_vector = adapter->q_vector[0];
  2715. u16 current_itr;
  2716. u32 new_itr = q_vector->itr_val;
  2717. /* for non-gigabit speeds, just fix the interrupt rate at 4000 */
  2718. if (adapter->link_speed != SPEED_1000) {
  2719. current_itr = 0;
  2720. new_itr = 4000;
  2721. goto set_itr_now;
  2722. }
  2723. adapter->rx_itr = igb_update_itr(adapter,
  2724. adapter->rx_itr,
  2725. adapter->rx_ring->total_packets,
  2726. adapter->rx_ring->total_bytes);
  2727. adapter->tx_itr = igb_update_itr(adapter,
  2728. adapter->tx_itr,
  2729. adapter->tx_ring->total_packets,
  2730. adapter->tx_ring->total_bytes);
  2731. current_itr = max(adapter->rx_itr, adapter->tx_itr);
  2732. /* conservative mode (itr 3) eliminates the lowest_latency setting */
  2733. if (adapter->rx_itr_setting == 3 && current_itr == lowest_latency)
  2734. current_itr = low_latency;
  2735. switch (current_itr) {
  2736. /* counts and packets in update_itr are dependent on these numbers */
  2737. case lowest_latency:
  2738. new_itr = 56; /* aka 70,000 ints/sec */
  2739. break;
  2740. case low_latency:
  2741. new_itr = 196; /* aka 20,000 ints/sec */
  2742. break;
  2743. case bulk_latency:
  2744. new_itr = 980; /* aka 4,000 ints/sec */
  2745. break;
  2746. default:
  2747. break;
  2748. }
  2749. set_itr_now:
  2750. adapter->rx_ring->total_bytes = 0;
  2751. adapter->rx_ring->total_packets = 0;
  2752. adapter->tx_ring->total_bytes = 0;
  2753. adapter->tx_ring->total_packets = 0;
  2754. if (new_itr != q_vector->itr_val) {
  2755. /* this attempts to bias the interrupt rate towards Bulk
  2756. * by adding intermediate steps when interrupt rate is
  2757. * increasing */
  2758. new_itr = new_itr > q_vector->itr_val ?
  2759. max((new_itr * q_vector->itr_val) /
  2760. (new_itr + (q_vector->itr_val >> 2)),
  2761. new_itr) :
  2762. new_itr;
  2763. /* Don't write the value here; it resets the adapter's
  2764. * internal timer, and causes us to delay far longer than
  2765. * we should between interrupts. Instead, we write the ITR
  2766. * value at the beginning of the next interrupt so the timing
  2767. * ends up being correct.
  2768. */
  2769. q_vector->itr_val = new_itr;
  2770. q_vector->set_itr = 1;
  2771. }
  2772. return;
  2773. }
  2774. #define IGB_TX_FLAGS_CSUM 0x00000001
  2775. #define IGB_TX_FLAGS_VLAN 0x00000002
  2776. #define IGB_TX_FLAGS_TSO 0x00000004
  2777. #define IGB_TX_FLAGS_IPV4 0x00000008
  2778. #define IGB_TX_FLAGS_TSTAMP 0x00000010
  2779. #define IGB_TX_FLAGS_VLAN_MASK 0xffff0000
  2780. #define IGB_TX_FLAGS_VLAN_SHIFT 16
  2781. static inline int igb_tso_adv(struct igb_ring *tx_ring,
  2782. struct sk_buff *skb, u32 tx_flags, u8 *hdr_len)
  2783. {
  2784. struct e1000_adv_tx_context_desc *context_desc;
  2785. unsigned int i;
  2786. int err;
  2787. struct igb_buffer *buffer_info;
  2788. u32 info = 0, tu_cmd = 0;
  2789. u32 mss_l4len_idx, l4len;
  2790. *hdr_len = 0;
  2791. if (skb_header_cloned(skb)) {
  2792. err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
  2793. if (err)
  2794. return err;
  2795. }
  2796. l4len = tcp_hdrlen(skb);
  2797. *hdr_len += l4len;
  2798. if (skb->protocol == htons(ETH_P_IP)) {
  2799. struct iphdr *iph = ip_hdr(skb);
  2800. iph->tot_len = 0;
  2801. iph->check = 0;
  2802. tcp_hdr(skb)->check = ~csum_tcpudp_magic(iph->saddr,
  2803. iph->daddr, 0,
  2804. IPPROTO_TCP,
  2805. 0);
  2806. } else if (skb_shinfo(skb)->gso_type == SKB_GSO_TCPV6) {
  2807. ipv6_hdr(skb)->payload_len = 0;
  2808. tcp_hdr(skb)->check = ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
  2809. &ipv6_hdr(skb)->daddr,
  2810. 0, IPPROTO_TCP, 0);
  2811. }
  2812. i = tx_ring->next_to_use;
  2813. buffer_info = &tx_ring->buffer_info[i];
  2814. context_desc = E1000_TX_CTXTDESC_ADV(*tx_ring, i);
  2815. /* VLAN MACLEN IPLEN */
  2816. if (tx_flags & IGB_TX_FLAGS_VLAN)
  2817. info |= (tx_flags & IGB_TX_FLAGS_VLAN_MASK);
  2818. info |= (skb_network_offset(skb) << E1000_ADVTXD_MACLEN_SHIFT);
  2819. *hdr_len += skb_network_offset(skb);
  2820. info |= skb_network_header_len(skb);
  2821. *hdr_len += skb_network_header_len(skb);
  2822. context_desc->vlan_macip_lens = cpu_to_le32(info);
  2823. /* ADV DTYP TUCMD MKRLOC/ISCSIHEDLEN */
  2824. tu_cmd |= (E1000_TXD_CMD_DEXT | E1000_ADVTXD_DTYP_CTXT);
  2825. if (skb->protocol == htons(ETH_P_IP))
  2826. tu_cmd |= E1000_ADVTXD_TUCMD_IPV4;
  2827. tu_cmd |= E1000_ADVTXD_TUCMD_L4T_TCP;
  2828. context_desc->type_tucmd_mlhl = cpu_to_le32(tu_cmd);
  2829. /* MSS L4LEN IDX */
  2830. mss_l4len_idx = (skb_shinfo(skb)->gso_size << E1000_ADVTXD_MSS_SHIFT);
  2831. mss_l4len_idx |= (l4len << E1000_ADVTXD_L4LEN_SHIFT);
  2832. /* For 82575, context index must be unique per ring. */
  2833. if (tx_ring->flags & IGB_RING_FLAG_TX_CTX_IDX)
  2834. mss_l4len_idx |= tx_ring->reg_idx << 4;
  2835. context_desc->mss_l4len_idx = cpu_to_le32(mss_l4len_idx);
  2836. context_desc->seqnum_seed = 0;
  2837. buffer_info->time_stamp = jiffies;
  2838. buffer_info->next_to_watch = i;
  2839. buffer_info->dma = 0;
  2840. i++;
  2841. if (i == tx_ring->count)
  2842. i = 0;
  2843. tx_ring->next_to_use = i;
  2844. return true;
  2845. }
  2846. static inline bool igb_tx_csum_adv(struct igb_ring *tx_ring,
  2847. struct sk_buff *skb, u32 tx_flags)
  2848. {
  2849. struct e1000_adv_tx_context_desc *context_desc;
  2850. struct pci_dev *pdev = tx_ring->pdev;
  2851. struct igb_buffer *buffer_info;
  2852. u32 info = 0, tu_cmd = 0;
  2853. unsigned int i;
  2854. if ((skb->ip_summed == CHECKSUM_PARTIAL) ||
  2855. (tx_flags & IGB_TX_FLAGS_VLAN)) {
  2856. i = tx_ring->next_to_use;
  2857. buffer_info = &tx_ring->buffer_info[i];
  2858. context_desc = E1000_TX_CTXTDESC_ADV(*tx_ring, i);
  2859. if (tx_flags & IGB_TX_FLAGS_VLAN)
  2860. info |= (tx_flags & IGB_TX_FLAGS_VLAN_MASK);
  2861. info |= (skb_network_offset(skb) << E1000_ADVTXD_MACLEN_SHIFT);
  2862. if (skb->ip_summed == CHECKSUM_PARTIAL)
  2863. info |= skb_network_header_len(skb);
  2864. context_desc->vlan_macip_lens = cpu_to_le32(info);
  2865. tu_cmd |= (E1000_TXD_CMD_DEXT | E1000_ADVTXD_DTYP_CTXT);
  2866. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  2867. __be16 protocol;
  2868. if (skb->protocol == cpu_to_be16(ETH_P_8021Q)) {
  2869. const struct vlan_ethhdr *vhdr =
  2870. (const struct vlan_ethhdr*)skb->data;
  2871. protocol = vhdr->h_vlan_encapsulated_proto;
  2872. } else {
  2873. protocol = skb->protocol;
  2874. }
  2875. switch (protocol) {
  2876. case cpu_to_be16(ETH_P_IP):
  2877. tu_cmd |= E1000_ADVTXD_TUCMD_IPV4;
  2878. if (ip_hdr(skb)->protocol == IPPROTO_TCP)
  2879. tu_cmd |= E1000_ADVTXD_TUCMD_L4T_TCP;
  2880. else if (ip_hdr(skb)->protocol == IPPROTO_SCTP)
  2881. tu_cmd |= E1000_ADVTXD_TUCMD_L4T_SCTP;
  2882. break;
  2883. case cpu_to_be16(ETH_P_IPV6):
  2884. /* XXX what about other V6 headers?? */
  2885. if (ipv6_hdr(skb)->nexthdr == IPPROTO_TCP)
  2886. tu_cmd |= E1000_ADVTXD_TUCMD_L4T_TCP;
  2887. else if (ipv6_hdr(skb)->nexthdr == IPPROTO_SCTP)
  2888. tu_cmd |= E1000_ADVTXD_TUCMD_L4T_SCTP;
  2889. break;
  2890. default:
  2891. if (unlikely(net_ratelimit()))
  2892. dev_warn(&pdev->dev,
  2893. "partial checksum but proto=%x!\n",
  2894. skb->protocol);
  2895. break;
  2896. }
  2897. }
  2898. context_desc->type_tucmd_mlhl = cpu_to_le32(tu_cmd);
  2899. context_desc->seqnum_seed = 0;
  2900. if (tx_ring->flags & IGB_RING_FLAG_TX_CTX_IDX)
  2901. context_desc->mss_l4len_idx =
  2902. cpu_to_le32(tx_ring->reg_idx << 4);
  2903. buffer_info->time_stamp = jiffies;
  2904. buffer_info->next_to_watch = i;
  2905. buffer_info->dma = 0;
  2906. i++;
  2907. if (i == tx_ring->count)
  2908. i = 0;
  2909. tx_ring->next_to_use = i;
  2910. return true;
  2911. }
  2912. return false;
  2913. }
  2914. #define IGB_MAX_TXD_PWR 16
  2915. #define IGB_MAX_DATA_PER_TXD (1<<IGB_MAX_TXD_PWR)
  2916. static inline int igb_tx_map_adv(struct igb_ring *tx_ring, struct sk_buff *skb,
  2917. unsigned int first)
  2918. {
  2919. struct igb_buffer *buffer_info;
  2920. struct pci_dev *pdev = tx_ring->pdev;
  2921. unsigned int len = skb_headlen(skb);
  2922. unsigned int count = 0, i;
  2923. unsigned int f;
  2924. dma_addr_t *map;
  2925. i = tx_ring->next_to_use;
  2926. if (skb_dma_map(&pdev->dev, skb, DMA_TO_DEVICE)) {
  2927. dev_err(&pdev->dev, "TX DMA map failed\n");
  2928. return 0;
  2929. }
  2930. map = skb_shinfo(skb)->dma_maps;
  2931. buffer_info = &tx_ring->buffer_info[i];
  2932. BUG_ON(len >= IGB_MAX_DATA_PER_TXD);
  2933. buffer_info->length = len;
  2934. /* set time_stamp *before* dma to help avoid a possible race */
  2935. buffer_info->time_stamp = jiffies;
  2936. buffer_info->next_to_watch = i;
  2937. buffer_info->dma = skb_shinfo(skb)->dma_head;
  2938. for (f = 0; f < skb_shinfo(skb)->nr_frags; f++) {
  2939. struct skb_frag_struct *frag;
  2940. i++;
  2941. if (i == tx_ring->count)
  2942. i = 0;
  2943. frag = &skb_shinfo(skb)->frags[f];
  2944. len = frag->size;
  2945. buffer_info = &tx_ring->buffer_info[i];
  2946. BUG_ON(len >= IGB_MAX_DATA_PER_TXD);
  2947. buffer_info->length = len;
  2948. buffer_info->time_stamp = jiffies;
  2949. buffer_info->next_to_watch = i;
  2950. buffer_info->dma = map[count];
  2951. count++;
  2952. }
  2953. tx_ring->buffer_info[i].skb = skb;
  2954. tx_ring->buffer_info[first].next_to_watch = i;
  2955. return count + 1;
  2956. }
  2957. static inline void igb_tx_queue_adv(struct igb_ring *tx_ring,
  2958. int tx_flags, int count, u32 paylen,
  2959. u8 hdr_len)
  2960. {
  2961. union e1000_adv_tx_desc *tx_desc = NULL;
  2962. struct igb_buffer *buffer_info;
  2963. u32 olinfo_status = 0, cmd_type_len;
  2964. unsigned int i;
  2965. cmd_type_len = (E1000_ADVTXD_DTYP_DATA | E1000_ADVTXD_DCMD_IFCS |
  2966. E1000_ADVTXD_DCMD_DEXT);
  2967. if (tx_flags & IGB_TX_FLAGS_VLAN)
  2968. cmd_type_len |= E1000_ADVTXD_DCMD_VLE;
  2969. if (tx_flags & IGB_TX_FLAGS_TSTAMP)
  2970. cmd_type_len |= E1000_ADVTXD_MAC_TSTAMP;
  2971. if (tx_flags & IGB_TX_FLAGS_TSO) {
  2972. cmd_type_len |= E1000_ADVTXD_DCMD_TSE;
  2973. /* insert tcp checksum */
  2974. olinfo_status |= E1000_TXD_POPTS_TXSM << 8;
  2975. /* insert ip checksum */
  2976. if (tx_flags & IGB_TX_FLAGS_IPV4)
  2977. olinfo_status |= E1000_TXD_POPTS_IXSM << 8;
  2978. } else if (tx_flags & IGB_TX_FLAGS_CSUM) {
  2979. olinfo_status |= E1000_TXD_POPTS_TXSM << 8;
  2980. }
  2981. if ((tx_ring->flags & IGB_RING_FLAG_TX_CTX_IDX) &&
  2982. (tx_flags & (IGB_TX_FLAGS_CSUM |
  2983. IGB_TX_FLAGS_TSO |
  2984. IGB_TX_FLAGS_VLAN)))
  2985. olinfo_status |= tx_ring->reg_idx << 4;
  2986. olinfo_status |= ((paylen - hdr_len) << E1000_ADVTXD_PAYLEN_SHIFT);
  2987. i = tx_ring->next_to_use;
  2988. while (count--) {
  2989. buffer_info = &tx_ring->buffer_info[i];
  2990. tx_desc = E1000_TX_DESC_ADV(*tx_ring, i);
  2991. tx_desc->read.buffer_addr = cpu_to_le64(buffer_info->dma);
  2992. tx_desc->read.cmd_type_len =
  2993. cpu_to_le32(cmd_type_len | buffer_info->length);
  2994. tx_desc->read.olinfo_status = cpu_to_le32(olinfo_status);
  2995. i++;
  2996. if (i == tx_ring->count)
  2997. i = 0;
  2998. }
  2999. tx_desc->read.cmd_type_len |= cpu_to_le32(IGB_ADVTXD_DCMD);
  3000. /* Force memory writes to complete before letting h/w
  3001. * know there are new descriptors to fetch. (Only
  3002. * applicable for weak-ordered memory model archs,
  3003. * such as IA-64). */
  3004. wmb();
  3005. tx_ring->next_to_use = i;
  3006. writel(i, tx_ring->tail);
  3007. /* we need this if more than one processor can write to our tail
  3008. * at a time, it syncronizes IO on IA64/Altix systems */
  3009. mmiowb();
  3010. }
  3011. static int __igb_maybe_stop_tx(struct igb_ring *tx_ring, int size)
  3012. {
  3013. struct net_device *netdev = tx_ring->netdev;
  3014. netif_stop_subqueue(netdev, tx_ring->queue_index);
  3015. /* Herbert's original patch had:
  3016. * smp_mb__after_netif_stop_queue();
  3017. * but since that doesn't exist yet, just open code it. */
  3018. smp_mb();
  3019. /* We need to check again in a case another CPU has just
  3020. * made room available. */
  3021. if (igb_desc_unused(tx_ring) < size)
  3022. return -EBUSY;
  3023. /* A reprieve! */
  3024. netif_wake_subqueue(netdev, tx_ring->queue_index);
  3025. tx_ring->tx_stats.restart_queue++;
  3026. return 0;
  3027. }
  3028. static int igb_maybe_stop_tx(struct igb_ring *tx_ring, int size)
  3029. {
  3030. if (igb_desc_unused(tx_ring) >= size)
  3031. return 0;
  3032. return __igb_maybe_stop_tx(tx_ring, size);
  3033. }
  3034. netdev_tx_t igb_xmit_frame_ring_adv(struct sk_buff *skb,
  3035. struct igb_ring *tx_ring)
  3036. {
  3037. struct igb_adapter *adapter = netdev_priv(tx_ring->netdev);
  3038. unsigned int first;
  3039. unsigned int tx_flags = 0;
  3040. u8 hdr_len = 0;
  3041. int count = 0;
  3042. int tso = 0;
  3043. union skb_shared_tx *shtx = skb_tx(skb);
  3044. /* need: 1 descriptor per page,
  3045. * + 2 desc gap to keep tail from touching head,
  3046. * + 1 desc for skb->data,
  3047. * + 1 desc for context descriptor,
  3048. * otherwise try next time */
  3049. if (igb_maybe_stop_tx(tx_ring, skb_shinfo(skb)->nr_frags + 4)) {
  3050. /* this is a hard error */
  3051. return NETDEV_TX_BUSY;
  3052. }
  3053. if (unlikely(shtx->hardware)) {
  3054. shtx->in_progress = 1;
  3055. tx_flags |= IGB_TX_FLAGS_TSTAMP;
  3056. }
  3057. if (adapter->vlgrp && vlan_tx_tag_present(skb)) {
  3058. tx_flags |= IGB_TX_FLAGS_VLAN;
  3059. tx_flags |= (vlan_tx_tag_get(skb) << IGB_TX_FLAGS_VLAN_SHIFT);
  3060. }
  3061. if (skb->protocol == htons(ETH_P_IP))
  3062. tx_flags |= IGB_TX_FLAGS_IPV4;
  3063. first = tx_ring->next_to_use;
  3064. if (skb_is_gso(skb)) {
  3065. tso = igb_tso_adv(tx_ring, skb, tx_flags, &hdr_len);
  3066. if (tso < 0) {
  3067. dev_kfree_skb_any(skb);
  3068. return NETDEV_TX_OK;
  3069. }
  3070. }
  3071. if (tso)
  3072. tx_flags |= IGB_TX_FLAGS_TSO;
  3073. else if (igb_tx_csum_adv(tx_ring, skb, tx_flags) &&
  3074. (skb->ip_summed == CHECKSUM_PARTIAL))
  3075. tx_flags |= IGB_TX_FLAGS_CSUM;
  3076. /*
  3077. * count reflects descriptors mapped, if 0 then mapping error
  3078. * has occured and we need to rewind the descriptor queue
  3079. */
  3080. count = igb_tx_map_adv(tx_ring, skb, first);
  3081. if (!count) {
  3082. dev_kfree_skb_any(skb);
  3083. tx_ring->buffer_info[first].time_stamp = 0;
  3084. tx_ring->next_to_use = first;
  3085. return NETDEV_TX_OK;
  3086. }
  3087. igb_tx_queue_adv(tx_ring, tx_flags, count, skb->len, hdr_len);
  3088. /* Make sure there is space in the ring for the next send. */
  3089. igb_maybe_stop_tx(tx_ring, MAX_SKB_FRAGS + 4);
  3090. return NETDEV_TX_OK;
  3091. }
  3092. static netdev_tx_t igb_xmit_frame_adv(struct sk_buff *skb,
  3093. struct net_device *netdev)
  3094. {
  3095. struct igb_adapter *adapter = netdev_priv(netdev);
  3096. struct igb_ring *tx_ring;
  3097. int r_idx = 0;
  3098. if (test_bit(__IGB_DOWN, &adapter->state)) {
  3099. dev_kfree_skb_any(skb);
  3100. return NETDEV_TX_OK;
  3101. }
  3102. if (skb->len <= 0) {
  3103. dev_kfree_skb_any(skb);
  3104. return NETDEV_TX_OK;
  3105. }
  3106. r_idx = skb->queue_mapping & (IGB_ABS_MAX_TX_QUEUES - 1);
  3107. tx_ring = adapter->multi_tx_table[r_idx];
  3108. /* This goes back to the question of how to logically map a tx queue
  3109. * to a flow. Right now, performance is impacted slightly negatively
  3110. * if using multiple tx queues. If the stack breaks away from a
  3111. * single qdisc implementation, we can look at this again. */
  3112. return igb_xmit_frame_ring_adv(skb, tx_ring);
  3113. }
  3114. /**
  3115. * igb_tx_timeout - Respond to a Tx Hang
  3116. * @netdev: network interface device structure
  3117. **/
  3118. static void igb_tx_timeout(struct net_device *netdev)
  3119. {
  3120. struct igb_adapter *adapter = netdev_priv(netdev);
  3121. struct e1000_hw *hw = &adapter->hw;
  3122. /* Do the reset outside of interrupt context */
  3123. adapter->tx_timeout_count++;
  3124. schedule_work(&adapter->reset_task);
  3125. wr32(E1000_EICS,
  3126. (adapter->eims_enable_mask & ~adapter->eims_other));
  3127. }
  3128. static void igb_reset_task(struct work_struct *work)
  3129. {
  3130. struct igb_adapter *adapter;
  3131. adapter = container_of(work, struct igb_adapter, reset_task);
  3132. igb_reinit_locked(adapter);
  3133. }
  3134. /**
  3135. * igb_get_stats - Get System Network Statistics
  3136. * @netdev: network interface device structure
  3137. *
  3138. * Returns the address of the device statistics structure.
  3139. * The statistics are actually updated from the timer callback.
  3140. **/
  3141. static struct net_device_stats *igb_get_stats(struct net_device *netdev)
  3142. {
  3143. /* only return the current stats */
  3144. return &netdev->stats;
  3145. }
  3146. /**
  3147. * igb_change_mtu - Change the Maximum Transfer Unit
  3148. * @netdev: network interface device structure
  3149. * @new_mtu: new value for maximum frame size
  3150. *
  3151. * Returns 0 on success, negative on failure
  3152. **/
  3153. static int igb_change_mtu(struct net_device *netdev, int new_mtu)
  3154. {
  3155. struct igb_adapter *adapter = netdev_priv(netdev);
  3156. int max_frame = new_mtu + ETH_HLEN + ETH_FCS_LEN;
  3157. u32 rx_buffer_len, i;
  3158. if ((max_frame < ETH_ZLEN + ETH_FCS_LEN) ||
  3159. (max_frame > MAX_JUMBO_FRAME_SIZE)) {
  3160. dev_err(&adapter->pdev->dev, "Invalid MTU setting\n");
  3161. return -EINVAL;
  3162. }
  3163. if (max_frame > MAX_STD_JUMBO_FRAME_SIZE) {
  3164. dev_err(&adapter->pdev->dev, "MTU > 9216 not supported.\n");
  3165. return -EINVAL;
  3166. }
  3167. while (test_and_set_bit(__IGB_RESETTING, &adapter->state))
  3168. msleep(1);
  3169. /* igb_down has a dependency on max_frame_size */
  3170. adapter->max_frame_size = max_frame;
  3171. /* NOTE: netdev_alloc_skb reserves 16 bytes, and typically NET_IP_ALIGN
  3172. * means we reserve 2 more, this pushes us to allocate from the next
  3173. * larger slab size.
  3174. * i.e. RXBUFFER_2048 --> size-4096 slab
  3175. */
  3176. if (max_frame <= IGB_RXBUFFER_1024)
  3177. rx_buffer_len = IGB_RXBUFFER_1024;
  3178. else if (max_frame <= MAXIMUM_ETHERNET_VLAN_SIZE)
  3179. rx_buffer_len = MAXIMUM_ETHERNET_VLAN_SIZE;
  3180. else
  3181. rx_buffer_len = IGB_RXBUFFER_128;
  3182. if (netif_running(netdev))
  3183. igb_down(adapter);
  3184. dev_info(&adapter->pdev->dev, "changing MTU from %d to %d\n",
  3185. netdev->mtu, new_mtu);
  3186. netdev->mtu = new_mtu;
  3187. for (i = 0; i < adapter->num_rx_queues; i++)
  3188. adapter->rx_ring[i].rx_buffer_len = rx_buffer_len;
  3189. if (netif_running(netdev))
  3190. igb_up(adapter);
  3191. else
  3192. igb_reset(adapter);
  3193. clear_bit(__IGB_RESETTING, &adapter->state);
  3194. return 0;
  3195. }
  3196. /**
  3197. * igb_update_stats - Update the board statistics counters
  3198. * @adapter: board private structure
  3199. **/
  3200. void igb_update_stats(struct igb_adapter *adapter)
  3201. {
  3202. struct net_device *netdev = adapter->netdev;
  3203. struct e1000_hw *hw = &adapter->hw;
  3204. struct pci_dev *pdev = adapter->pdev;
  3205. u16 phy_tmp;
  3206. #define PHY_IDLE_ERROR_COUNT_MASK 0x00FF
  3207. /*
  3208. * Prevent stats update while adapter is being reset, or if the pci
  3209. * connection is down.
  3210. */
  3211. if (adapter->link_speed == 0)
  3212. return;
  3213. if (pci_channel_offline(pdev))
  3214. return;
  3215. adapter->stats.crcerrs += rd32(E1000_CRCERRS);
  3216. adapter->stats.gprc += rd32(E1000_GPRC);
  3217. adapter->stats.gorc += rd32(E1000_GORCL);
  3218. rd32(E1000_GORCH); /* clear GORCL */
  3219. adapter->stats.bprc += rd32(E1000_BPRC);
  3220. adapter->stats.mprc += rd32(E1000_MPRC);
  3221. adapter->stats.roc += rd32(E1000_ROC);
  3222. adapter->stats.prc64 += rd32(E1000_PRC64);
  3223. adapter->stats.prc127 += rd32(E1000_PRC127);
  3224. adapter->stats.prc255 += rd32(E1000_PRC255);
  3225. adapter->stats.prc511 += rd32(E1000_PRC511);
  3226. adapter->stats.prc1023 += rd32(E1000_PRC1023);
  3227. adapter->stats.prc1522 += rd32(E1000_PRC1522);
  3228. adapter->stats.symerrs += rd32(E1000_SYMERRS);
  3229. adapter->stats.sec += rd32(E1000_SEC);
  3230. adapter->stats.mpc += rd32(E1000_MPC);
  3231. adapter->stats.scc += rd32(E1000_SCC);
  3232. adapter->stats.ecol += rd32(E1000_ECOL);
  3233. adapter->stats.mcc += rd32(E1000_MCC);
  3234. adapter->stats.latecol += rd32(E1000_LATECOL);
  3235. adapter->stats.dc += rd32(E1000_DC);
  3236. adapter->stats.rlec += rd32(E1000_RLEC);
  3237. adapter->stats.xonrxc += rd32(E1000_XONRXC);
  3238. adapter->stats.xontxc += rd32(E1000_XONTXC);
  3239. adapter->stats.xoffrxc += rd32(E1000_XOFFRXC);
  3240. adapter->stats.xofftxc += rd32(E1000_XOFFTXC);
  3241. adapter->stats.fcruc += rd32(E1000_FCRUC);
  3242. adapter->stats.gptc += rd32(E1000_GPTC);
  3243. adapter->stats.gotc += rd32(E1000_GOTCL);
  3244. rd32(E1000_GOTCH); /* clear GOTCL */
  3245. adapter->stats.rnbc += rd32(E1000_RNBC);
  3246. adapter->stats.ruc += rd32(E1000_RUC);
  3247. adapter->stats.rfc += rd32(E1000_RFC);
  3248. adapter->stats.rjc += rd32(E1000_RJC);
  3249. adapter->stats.tor += rd32(E1000_TORH);
  3250. adapter->stats.tot += rd32(E1000_TOTH);
  3251. adapter->stats.tpr += rd32(E1000_TPR);
  3252. adapter->stats.ptc64 += rd32(E1000_PTC64);
  3253. adapter->stats.ptc127 += rd32(E1000_PTC127);
  3254. adapter->stats.ptc255 += rd32(E1000_PTC255);
  3255. adapter->stats.ptc511 += rd32(E1000_PTC511);
  3256. adapter->stats.ptc1023 += rd32(E1000_PTC1023);
  3257. adapter->stats.ptc1522 += rd32(E1000_PTC1522);
  3258. adapter->stats.mptc += rd32(E1000_MPTC);
  3259. adapter->stats.bptc += rd32(E1000_BPTC);
  3260. /* used for adaptive IFS */
  3261. hw->mac.tx_packet_delta = rd32(E1000_TPT);
  3262. adapter->stats.tpt += hw->mac.tx_packet_delta;
  3263. hw->mac.collision_delta = rd32(E1000_COLC);
  3264. adapter->stats.colc += hw->mac.collision_delta;
  3265. adapter->stats.algnerrc += rd32(E1000_ALGNERRC);
  3266. adapter->stats.rxerrc += rd32(E1000_RXERRC);
  3267. adapter->stats.tncrs += rd32(E1000_TNCRS);
  3268. adapter->stats.tsctc += rd32(E1000_TSCTC);
  3269. adapter->stats.tsctfc += rd32(E1000_TSCTFC);
  3270. adapter->stats.iac += rd32(E1000_IAC);
  3271. adapter->stats.icrxoc += rd32(E1000_ICRXOC);
  3272. adapter->stats.icrxptc += rd32(E1000_ICRXPTC);
  3273. adapter->stats.icrxatc += rd32(E1000_ICRXATC);
  3274. adapter->stats.ictxptc += rd32(E1000_ICTXPTC);
  3275. adapter->stats.ictxatc += rd32(E1000_ICTXATC);
  3276. adapter->stats.ictxqec += rd32(E1000_ICTXQEC);
  3277. adapter->stats.ictxqmtc += rd32(E1000_ICTXQMTC);
  3278. adapter->stats.icrxdmtc += rd32(E1000_ICRXDMTC);
  3279. /* Fill out the OS statistics structure */
  3280. netdev->stats.multicast = adapter->stats.mprc;
  3281. netdev->stats.collisions = adapter->stats.colc;
  3282. /* Rx Errors */
  3283. if (hw->mac.type != e1000_82575) {
  3284. u32 rqdpc_tmp;
  3285. u64 rqdpc_total = 0;
  3286. int i;
  3287. /* Read out drops stats per RX queue. Notice RQDPC (Receive
  3288. * Queue Drop Packet Count) stats only gets incremented, if
  3289. * the DROP_EN but it set (in the SRRCTL register for that
  3290. * queue). If DROP_EN bit is NOT set, then the some what
  3291. * equivalent count is stored in RNBC (not per queue basis).
  3292. * Also note the drop count is due to lack of available
  3293. * descriptors.
  3294. */
  3295. for (i = 0; i < adapter->num_rx_queues; i++) {
  3296. rqdpc_tmp = rd32(E1000_RQDPC(i)) & 0xFFF;
  3297. adapter->rx_ring[i].rx_stats.drops += rqdpc_tmp;
  3298. rqdpc_total += adapter->rx_ring[i].rx_stats.drops;
  3299. }
  3300. netdev->stats.rx_fifo_errors = rqdpc_total;
  3301. }
  3302. /* Note RNBC (Receive No Buffers Count) is an not an exact
  3303. * drop count as the hardware FIFO might save the day. Thats
  3304. * one of the reason for saving it in rx_fifo_errors, as its
  3305. * potentially not a true drop.
  3306. */
  3307. netdev->stats.rx_fifo_errors += adapter->stats.rnbc;
  3308. /* RLEC on some newer hardware can be incorrect so build
  3309. * our own version based on RUC and ROC */
  3310. netdev->stats.rx_errors = adapter->stats.rxerrc +
  3311. adapter->stats.crcerrs + adapter->stats.algnerrc +
  3312. adapter->stats.ruc + adapter->stats.roc +
  3313. adapter->stats.cexterr;
  3314. netdev->stats.rx_length_errors = adapter->stats.ruc +
  3315. adapter->stats.roc;
  3316. netdev->stats.rx_crc_errors = adapter->stats.crcerrs;
  3317. netdev->stats.rx_frame_errors = adapter->stats.algnerrc;
  3318. netdev->stats.rx_missed_errors = adapter->stats.mpc;
  3319. /* Tx Errors */
  3320. netdev->stats.tx_errors = adapter->stats.ecol +
  3321. adapter->stats.latecol;
  3322. netdev->stats.tx_aborted_errors = adapter->stats.ecol;
  3323. netdev->stats.tx_window_errors = adapter->stats.latecol;
  3324. netdev->stats.tx_carrier_errors = adapter->stats.tncrs;
  3325. /* Tx Dropped needs to be maintained elsewhere */
  3326. /* Phy Stats */
  3327. if (hw->phy.media_type == e1000_media_type_copper) {
  3328. if ((adapter->link_speed == SPEED_1000) &&
  3329. (!igb_read_phy_reg(hw, PHY_1000T_STATUS, &phy_tmp))) {
  3330. phy_tmp &= PHY_IDLE_ERROR_COUNT_MASK;
  3331. adapter->phy_stats.idle_errors += phy_tmp;
  3332. }
  3333. }
  3334. /* Management Stats */
  3335. adapter->stats.mgptc += rd32(E1000_MGTPTC);
  3336. adapter->stats.mgprc += rd32(E1000_MGTPRC);
  3337. adapter->stats.mgpdc += rd32(E1000_MGTPDC);
  3338. }
  3339. static irqreturn_t igb_msix_other(int irq, void *data)
  3340. {
  3341. struct igb_adapter *adapter = data;
  3342. struct e1000_hw *hw = &adapter->hw;
  3343. u32 icr = rd32(E1000_ICR);
  3344. /* reading ICR causes bit 31 of EICR to be cleared */
  3345. if (icr & E1000_ICR_DOUTSYNC) {
  3346. /* HW is reporting DMA is out of sync */
  3347. adapter->stats.doosync++;
  3348. }
  3349. /* Check for a mailbox event */
  3350. if (icr & E1000_ICR_VMMB)
  3351. igb_msg_task(adapter);
  3352. if (icr & E1000_ICR_LSC) {
  3353. hw->mac.get_link_status = 1;
  3354. /* guard against interrupt when we're going down */
  3355. if (!test_bit(__IGB_DOWN, &adapter->state))
  3356. mod_timer(&adapter->watchdog_timer, jiffies + 1);
  3357. }
  3358. wr32(E1000_IMS, E1000_IMS_LSC | E1000_IMS_DOUTSYNC | E1000_IMS_VMMB);
  3359. wr32(E1000_EIMS, adapter->eims_other);
  3360. return IRQ_HANDLED;
  3361. }
  3362. static void igb_write_itr(struct igb_q_vector *q_vector)
  3363. {
  3364. u32 itr_val = q_vector->itr_val & 0x7FFC;
  3365. if (!q_vector->set_itr)
  3366. return;
  3367. if (!itr_val)
  3368. itr_val = 0x4;
  3369. if (q_vector->itr_shift)
  3370. itr_val |= itr_val << q_vector->itr_shift;
  3371. else
  3372. itr_val |= 0x8000000;
  3373. writel(itr_val, q_vector->itr_register);
  3374. q_vector->set_itr = 0;
  3375. }
  3376. static irqreturn_t igb_msix_ring(int irq, void *data)
  3377. {
  3378. struct igb_q_vector *q_vector = data;
  3379. /* Write the ITR value calculated from the previous interrupt. */
  3380. igb_write_itr(q_vector);
  3381. napi_schedule(&q_vector->napi);
  3382. return IRQ_HANDLED;
  3383. }
  3384. #ifdef CONFIG_IGB_DCA
  3385. static void igb_update_dca(struct igb_q_vector *q_vector)
  3386. {
  3387. struct igb_adapter *adapter = q_vector->adapter;
  3388. struct e1000_hw *hw = &adapter->hw;
  3389. int cpu = get_cpu();
  3390. if (q_vector->cpu == cpu)
  3391. goto out_no_update;
  3392. if (q_vector->tx_ring) {
  3393. int q = q_vector->tx_ring->reg_idx;
  3394. u32 dca_txctrl = rd32(E1000_DCA_TXCTRL(q));
  3395. if (hw->mac.type == e1000_82575) {
  3396. dca_txctrl &= ~E1000_DCA_TXCTRL_CPUID_MASK;
  3397. dca_txctrl |= dca3_get_tag(&adapter->pdev->dev, cpu);
  3398. } else {
  3399. dca_txctrl &= ~E1000_DCA_TXCTRL_CPUID_MASK_82576;
  3400. dca_txctrl |= dca3_get_tag(&adapter->pdev->dev, cpu) <<
  3401. E1000_DCA_TXCTRL_CPUID_SHIFT;
  3402. }
  3403. dca_txctrl |= E1000_DCA_TXCTRL_DESC_DCA_EN;
  3404. wr32(E1000_DCA_TXCTRL(q), dca_txctrl);
  3405. }
  3406. if (q_vector->rx_ring) {
  3407. int q = q_vector->rx_ring->reg_idx;
  3408. u32 dca_rxctrl = rd32(E1000_DCA_RXCTRL(q));
  3409. if (hw->mac.type == e1000_82575) {
  3410. dca_rxctrl &= ~E1000_DCA_RXCTRL_CPUID_MASK;
  3411. dca_rxctrl |= dca3_get_tag(&adapter->pdev->dev, cpu);
  3412. } else {
  3413. dca_rxctrl &= ~E1000_DCA_RXCTRL_CPUID_MASK_82576;
  3414. dca_rxctrl |= dca3_get_tag(&adapter->pdev->dev, cpu) <<
  3415. E1000_DCA_RXCTRL_CPUID_SHIFT;
  3416. }
  3417. dca_rxctrl |= E1000_DCA_RXCTRL_DESC_DCA_EN;
  3418. dca_rxctrl |= E1000_DCA_RXCTRL_HEAD_DCA_EN;
  3419. dca_rxctrl |= E1000_DCA_RXCTRL_DATA_DCA_EN;
  3420. wr32(E1000_DCA_RXCTRL(q), dca_rxctrl);
  3421. }
  3422. q_vector->cpu = cpu;
  3423. out_no_update:
  3424. put_cpu();
  3425. }
  3426. static void igb_setup_dca(struct igb_adapter *adapter)
  3427. {
  3428. struct e1000_hw *hw = &adapter->hw;
  3429. int i;
  3430. if (!(adapter->flags & IGB_FLAG_DCA_ENABLED))
  3431. return;
  3432. /* Always use CB2 mode, difference is masked in the CB driver. */
  3433. wr32(E1000_DCA_CTRL, E1000_DCA_CTRL_DCA_MODE_CB2);
  3434. for (i = 0; i < adapter->num_q_vectors; i++) {
  3435. struct igb_q_vector *q_vector = adapter->q_vector[i];
  3436. q_vector->cpu = -1;
  3437. igb_update_dca(q_vector);
  3438. }
  3439. }
  3440. static int __igb_notify_dca(struct device *dev, void *data)
  3441. {
  3442. struct net_device *netdev = dev_get_drvdata(dev);
  3443. struct igb_adapter *adapter = netdev_priv(netdev);
  3444. struct e1000_hw *hw = &adapter->hw;
  3445. unsigned long event = *(unsigned long *)data;
  3446. switch (event) {
  3447. case DCA_PROVIDER_ADD:
  3448. /* if already enabled, don't do it again */
  3449. if (adapter->flags & IGB_FLAG_DCA_ENABLED)
  3450. break;
  3451. /* Always use CB2 mode, difference is masked
  3452. * in the CB driver. */
  3453. wr32(E1000_DCA_CTRL, E1000_DCA_CTRL_DCA_MODE_CB2);
  3454. if (dca_add_requester(dev) == 0) {
  3455. adapter->flags |= IGB_FLAG_DCA_ENABLED;
  3456. dev_info(&adapter->pdev->dev, "DCA enabled\n");
  3457. igb_setup_dca(adapter);
  3458. break;
  3459. }
  3460. /* Fall Through since DCA is disabled. */
  3461. case DCA_PROVIDER_REMOVE:
  3462. if (adapter->flags & IGB_FLAG_DCA_ENABLED) {
  3463. /* without this a class_device is left
  3464. * hanging around in the sysfs model */
  3465. dca_remove_requester(dev);
  3466. dev_info(&adapter->pdev->dev, "DCA disabled\n");
  3467. adapter->flags &= ~IGB_FLAG_DCA_ENABLED;
  3468. wr32(E1000_DCA_CTRL, E1000_DCA_CTRL_DCA_MODE_DISABLE);
  3469. }
  3470. break;
  3471. }
  3472. return 0;
  3473. }
  3474. static int igb_notify_dca(struct notifier_block *nb, unsigned long event,
  3475. void *p)
  3476. {
  3477. int ret_val;
  3478. ret_val = driver_for_each_device(&igb_driver.driver, NULL, &event,
  3479. __igb_notify_dca);
  3480. return ret_val ? NOTIFY_BAD : NOTIFY_DONE;
  3481. }
  3482. #endif /* CONFIG_IGB_DCA */
  3483. static void igb_ping_all_vfs(struct igb_adapter *adapter)
  3484. {
  3485. struct e1000_hw *hw = &adapter->hw;
  3486. u32 ping;
  3487. int i;
  3488. for (i = 0 ; i < adapter->vfs_allocated_count; i++) {
  3489. ping = E1000_PF_CONTROL_MSG;
  3490. if (adapter->vf_data[i].clear_to_send)
  3491. ping |= E1000_VT_MSGTYPE_CTS;
  3492. igb_write_mbx(hw, &ping, 1, i);
  3493. }
  3494. }
  3495. static int igb_set_vf_multicasts(struct igb_adapter *adapter,
  3496. u32 *msgbuf, u32 vf)
  3497. {
  3498. int n = (msgbuf[0] & E1000_VT_MSGINFO_MASK) >> E1000_VT_MSGINFO_SHIFT;
  3499. u16 *hash_list = (u16 *)&msgbuf[1];
  3500. struct vf_data_storage *vf_data = &adapter->vf_data[vf];
  3501. int i;
  3502. /* only up to 30 hash values supported */
  3503. if (n > 30)
  3504. n = 30;
  3505. /* salt away the number of multi cast addresses assigned
  3506. * to this VF for later use to restore when the PF multi cast
  3507. * list changes
  3508. */
  3509. vf_data->num_vf_mc_hashes = n;
  3510. /* VFs are limited to using the MTA hash table for their multicast
  3511. * addresses */
  3512. for (i = 0; i < n; i++)
  3513. vf_data->vf_mc_hashes[i] = hash_list[i];
  3514. /* Flush and reset the mta with the new values */
  3515. igb_set_rx_mode(adapter->netdev);
  3516. return 0;
  3517. }
  3518. static void igb_restore_vf_multicasts(struct igb_adapter *adapter)
  3519. {
  3520. struct e1000_hw *hw = &adapter->hw;
  3521. struct vf_data_storage *vf_data;
  3522. int i, j;
  3523. for (i = 0; i < adapter->vfs_allocated_count; i++) {
  3524. vf_data = &adapter->vf_data[i];
  3525. for (j = 0; j < vf_data->num_vf_mc_hashes; j++)
  3526. igb_mta_set(hw, vf_data->vf_mc_hashes[j]);
  3527. }
  3528. }
  3529. static void igb_clear_vf_vfta(struct igb_adapter *adapter, u32 vf)
  3530. {
  3531. struct e1000_hw *hw = &adapter->hw;
  3532. u32 pool_mask, reg, vid;
  3533. int i;
  3534. pool_mask = 1 << (E1000_VLVF_POOLSEL_SHIFT + vf);
  3535. /* Find the vlan filter for this id */
  3536. for (i = 0; i < E1000_VLVF_ARRAY_SIZE; i++) {
  3537. reg = rd32(E1000_VLVF(i));
  3538. /* remove the vf from the pool */
  3539. reg &= ~pool_mask;
  3540. /* if pool is empty then remove entry from vfta */
  3541. if (!(reg & E1000_VLVF_POOLSEL_MASK) &&
  3542. (reg & E1000_VLVF_VLANID_ENABLE)) {
  3543. reg = 0;
  3544. vid = reg & E1000_VLVF_VLANID_MASK;
  3545. igb_vfta_set(hw, vid, false);
  3546. }
  3547. wr32(E1000_VLVF(i), reg);
  3548. }
  3549. adapter->vf_data[vf].vlans_enabled = 0;
  3550. }
  3551. static s32 igb_vlvf_set(struct igb_adapter *adapter, u32 vid, bool add, u32 vf)
  3552. {
  3553. struct e1000_hw *hw = &adapter->hw;
  3554. u32 reg, i;
  3555. /* It is an error to call this function when VFs are not enabled */
  3556. if (!adapter->vfs_allocated_count)
  3557. return -1;
  3558. /* Find the vlan filter for this id */
  3559. for (i = 0; i < E1000_VLVF_ARRAY_SIZE; i++) {
  3560. reg = rd32(E1000_VLVF(i));
  3561. if ((reg & E1000_VLVF_VLANID_ENABLE) &&
  3562. vid == (reg & E1000_VLVF_VLANID_MASK))
  3563. break;
  3564. }
  3565. if (add) {
  3566. if (i == E1000_VLVF_ARRAY_SIZE) {
  3567. /* Did not find a matching VLAN ID entry that was
  3568. * enabled. Search for a free filter entry, i.e.
  3569. * one without the enable bit set
  3570. */
  3571. for (i = 0; i < E1000_VLVF_ARRAY_SIZE; i++) {
  3572. reg = rd32(E1000_VLVF(i));
  3573. if (!(reg & E1000_VLVF_VLANID_ENABLE))
  3574. break;
  3575. }
  3576. }
  3577. if (i < E1000_VLVF_ARRAY_SIZE) {
  3578. /* Found an enabled/available entry */
  3579. reg |= 1 << (E1000_VLVF_POOLSEL_SHIFT + vf);
  3580. /* if !enabled we need to set this up in vfta */
  3581. if (!(reg & E1000_VLVF_VLANID_ENABLE)) {
  3582. /* add VID to filter table, if bit already set
  3583. * PF must have added it outside of table */
  3584. if (igb_vfta_set(hw, vid, true))
  3585. reg |= 1 << (E1000_VLVF_POOLSEL_SHIFT +
  3586. adapter->vfs_allocated_count);
  3587. reg |= E1000_VLVF_VLANID_ENABLE;
  3588. }
  3589. reg &= ~E1000_VLVF_VLANID_MASK;
  3590. reg |= vid;
  3591. wr32(E1000_VLVF(i), reg);
  3592. /* do not modify RLPML for PF devices */
  3593. if (vf >= adapter->vfs_allocated_count)
  3594. return 0;
  3595. if (!adapter->vf_data[vf].vlans_enabled) {
  3596. u32 size;
  3597. reg = rd32(E1000_VMOLR(vf));
  3598. size = reg & E1000_VMOLR_RLPML_MASK;
  3599. size += 4;
  3600. reg &= ~E1000_VMOLR_RLPML_MASK;
  3601. reg |= size;
  3602. wr32(E1000_VMOLR(vf), reg);
  3603. }
  3604. adapter->vf_data[vf].vlans_enabled++;
  3605. return 0;
  3606. }
  3607. } else {
  3608. if (i < E1000_VLVF_ARRAY_SIZE) {
  3609. /* remove vf from the pool */
  3610. reg &= ~(1 << (E1000_VLVF_POOLSEL_SHIFT + vf));
  3611. /* if pool is empty then remove entry from vfta */
  3612. if (!(reg & E1000_VLVF_POOLSEL_MASK)) {
  3613. reg = 0;
  3614. igb_vfta_set(hw, vid, false);
  3615. }
  3616. wr32(E1000_VLVF(i), reg);
  3617. /* do not modify RLPML for PF devices */
  3618. if (vf >= adapter->vfs_allocated_count)
  3619. return 0;
  3620. adapter->vf_data[vf].vlans_enabled--;
  3621. if (!adapter->vf_data[vf].vlans_enabled) {
  3622. u32 size;
  3623. reg = rd32(E1000_VMOLR(vf));
  3624. size = reg & E1000_VMOLR_RLPML_MASK;
  3625. size -= 4;
  3626. reg &= ~E1000_VMOLR_RLPML_MASK;
  3627. reg |= size;
  3628. wr32(E1000_VMOLR(vf), reg);
  3629. }
  3630. return 0;
  3631. }
  3632. }
  3633. return -1;
  3634. }
  3635. static int igb_set_vf_vlan(struct igb_adapter *adapter, u32 *msgbuf, u32 vf)
  3636. {
  3637. int add = (msgbuf[0] & E1000_VT_MSGINFO_MASK) >> E1000_VT_MSGINFO_SHIFT;
  3638. int vid = (msgbuf[1] & E1000_VLVF_VLANID_MASK);
  3639. return igb_vlvf_set(adapter, vid, add, vf);
  3640. }
  3641. static inline void igb_vf_reset_event(struct igb_adapter *adapter, u32 vf)
  3642. {
  3643. struct e1000_hw *hw = &adapter->hw;
  3644. /* disable mailbox functionality for vf */
  3645. adapter->vf_data[vf].clear_to_send = false;
  3646. /* reset offloads to defaults */
  3647. igb_set_vmolr(hw, vf);
  3648. /* reset vlans for device */
  3649. igb_clear_vf_vfta(adapter, vf);
  3650. /* reset multicast table array for vf */
  3651. adapter->vf_data[vf].num_vf_mc_hashes = 0;
  3652. /* Flush and reset the mta with the new values */
  3653. igb_set_rx_mode(adapter->netdev);
  3654. }
  3655. static inline void igb_vf_reset_msg(struct igb_adapter *adapter, u32 vf)
  3656. {
  3657. struct e1000_hw *hw = &adapter->hw;
  3658. unsigned char *vf_mac = adapter->vf_data[vf].vf_mac_addresses;
  3659. int rar_entry = hw->mac.rar_entry_count - (vf + 1);
  3660. u32 reg, msgbuf[3];
  3661. u8 *addr = (u8 *)(&msgbuf[1]);
  3662. /* process all the same items cleared in a function level reset */
  3663. igb_vf_reset_event(adapter, vf);
  3664. /* set vf mac address */
  3665. igb_rar_set_qsel(adapter, vf_mac, rar_entry, vf);
  3666. /* enable transmit and receive for vf */
  3667. reg = rd32(E1000_VFTE);
  3668. wr32(E1000_VFTE, reg | (1 << vf));
  3669. reg = rd32(E1000_VFRE);
  3670. wr32(E1000_VFRE, reg | (1 << vf));
  3671. /* enable mailbox functionality for vf */
  3672. adapter->vf_data[vf].clear_to_send = true;
  3673. /* reply to reset with ack and vf mac address */
  3674. msgbuf[0] = E1000_VF_RESET | E1000_VT_MSGTYPE_ACK;
  3675. memcpy(addr, vf_mac, 6);
  3676. igb_write_mbx(hw, msgbuf, 3, vf);
  3677. }
  3678. static int igb_set_vf_mac_addr(struct igb_adapter *adapter, u32 *msg, int vf)
  3679. {
  3680. unsigned char *addr = (char *)&msg[1];
  3681. int err = -1;
  3682. if (is_valid_ether_addr(addr))
  3683. err = igb_set_vf_mac(adapter, vf, addr);
  3684. return err;
  3685. }
  3686. static void igb_rcv_ack_from_vf(struct igb_adapter *adapter, u32 vf)
  3687. {
  3688. struct e1000_hw *hw = &adapter->hw;
  3689. u32 msg = E1000_VT_MSGTYPE_NACK;
  3690. /* if device isn't clear to send it shouldn't be reading either */
  3691. if (!adapter->vf_data[vf].clear_to_send)
  3692. igb_write_mbx(hw, &msg, 1, vf);
  3693. }
  3694. static void igb_msg_task(struct igb_adapter *adapter)
  3695. {
  3696. struct e1000_hw *hw = &adapter->hw;
  3697. u32 vf;
  3698. for (vf = 0; vf < adapter->vfs_allocated_count; vf++) {
  3699. /* process any reset requests */
  3700. if (!igb_check_for_rst(hw, vf)) {
  3701. adapter->vf_data[vf].clear_to_send = false;
  3702. igb_vf_reset_event(adapter, vf);
  3703. }
  3704. /* process any messages pending */
  3705. if (!igb_check_for_msg(hw, vf))
  3706. igb_rcv_msg_from_vf(adapter, vf);
  3707. /* process any acks */
  3708. if (!igb_check_for_ack(hw, vf))
  3709. igb_rcv_ack_from_vf(adapter, vf);
  3710. }
  3711. }
  3712. static int igb_rcv_msg_from_vf(struct igb_adapter *adapter, u32 vf)
  3713. {
  3714. u32 mbx_size = E1000_VFMAILBOX_SIZE;
  3715. u32 msgbuf[mbx_size];
  3716. struct e1000_hw *hw = &adapter->hw;
  3717. s32 retval;
  3718. retval = igb_read_mbx(hw, msgbuf, mbx_size, vf);
  3719. if (retval)
  3720. dev_err(&adapter->pdev->dev,
  3721. "Error receiving message from VF\n");
  3722. /* this is a message we already processed, do nothing */
  3723. if (msgbuf[0] & (E1000_VT_MSGTYPE_ACK | E1000_VT_MSGTYPE_NACK))
  3724. return retval;
  3725. /*
  3726. * until the vf completes a reset it should not be
  3727. * allowed to start any configuration.
  3728. */
  3729. if (msgbuf[0] == E1000_VF_RESET) {
  3730. igb_vf_reset_msg(adapter, vf);
  3731. return retval;
  3732. }
  3733. if (!adapter->vf_data[vf].clear_to_send) {
  3734. msgbuf[0] |= E1000_VT_MSGTYPE_NACK;
  3735. igb_write_mbx(hw, msgbuf, 1, vf);
  3736. return retval;
  3737. }
  3738. switch ((msgbuf[0] & 0xFFFF)) {
  3739. case E1000_VF_SET_MAC_ADDR:
  3740. retval = igb_set_vf_mac_addr(adapter, msgbuf, vf);
  3741. break;
  3742. case E1000_VF_SET_MULTICAST:
  3743. retval = igb_set_vf_multicasts(adapter, msgbuf, vf);
  3744. break;
  3745. case E1000_VF_SET_LPE:
  3746. retval = igb_set_vf_rlpml(adapter, msgbuf[1], vf);
  3747. break;
  3748. case E1000_VF_SET_VLAN:
  3749. retval = igb_set_vf_vlan(adapter, msgbuf, vf);
  3750. break;
  3751. default:
  3752. dev_err(&adapter->pdev->dev, "Unhandled Msg %08x\n", msgbuf[0]);
  3753. retval = -1;
  3754. break;
  3755. }
  3756. /* notify the VF of the results of what it sent us */
  3757. if (retval)
  3758. msgbuf[0] |= E1000_VT_MSGTYPE_NACK;
  3759. else
  3760. msgbuf[0] |= E1000_VT_MSGTYPE_ACK;
  3761. msgbuf[0] |= E1000_VT_MSGTYPE_CTS;
  3762. igb_write_mbx(hw, msgbuf, 1, vf);
  3763. return retval;
  3764. }
  3765. /**
  3766. * igb_set_uta - Set unicast filter table address
  3767. * @adapter: board private structure
  3768. *
  3769. * The unicast table address is a register array of 32-bit registers.
  3770. * The table is meant to be used in a way similar to how the MTA is used
  3771. * however due to certain limitations in the hardware it is necessary to
  3772. * set all the hash bits to 1 and use the VMOLR ROPE bit as a promiscous
  3773. * enable bit to allow vlan tag stripping when promiscous mode is enabled
  3774. **/
  3775. static void igb_set_uta(struct igb_adapter *adapter)
  3776. {
  3777. struct e1000_hw *hw = &adapter->hw;
  3778. int i;
  3779. /* The UTA table only exists on 82576 hardware and newer */
  3780. if (hw->mac.type < e1000_82576)
  3781. return;
  3782. /* we only need to do this if VMDq is enabled */
  3783. if (!adapter->vfs_allocated_count)
  3784. return;
  3785. for (i = 0; i < hw->mac.uta_reg_count; i++)
  3786. array_wr32(E1000_UTA, i, ~0);
  3787. }
  3788. /**
  3789. * igb_intr_msi - Interrupt Handler
  3790. * @irq: interrupt number
  3791. * @data: pointer to a network interface device structure
  3792. **/
  3793. static irqreturn_t igb_intr_msi(int irq, void *data)
  3794. {
  3795. struct igb_adapter *adapter = data;
  3796. struct igb_q_vector *q_vector = adapter->q_vector[0];
  3797. struct e1000_hw *hw = &adapter->hw;
  3798. /* read ICR disables interrupts using IAM */
  3799. u32 icr = rd32(E1000_ICR);
  3800. igb_write_itr(q_vector);
  3801. if (icr & E1000_ICR_DOUTSYNC) {
  3802. /* HW is reporting DMA is out of sync */
  3803. adapter->stats.doosync++;
  3804. }
  3805. if (icr & (E1000_ICR_RXSEQ | E1000_ICR_LSC)) {
  3806. hw->mac.get_link_status = 1;
  3807. if (!test_bit(__IGB_DOWN, &adapter->state))
  3808. mod_timer(&adapter->watchdog_timer, jiffies + 1);
  3809. }
  3810. napi_schedule(&q_vector->napi);
  3811. return IRQ_HANDLED;
  3812. }
  3813. /**
  3814. * igb_intr - Legacy Interrupt Handler
  3815. * @irq: interrupt number
  3816. * @data: pointer to a network interface device structure
  3817. **/
  3818. static irqreturn_t igb_intr(int irq, void *data)
  3819. {
  3820. struct igb_adapter *adapter = data;
  3821. struct igb_q_vector *q_vector = adapter->q_vector[0];
  3822. struct e1000_hw *hw = &adapter->hw;
  3823. /* Interrupt Auto-Mask...upon reading ICR, interrupts are masked. No
  3824. * need for the IMC write */
  3825. u32 icr = rd32(E1000_ICR);
  3826. if (!icr)
  3827. return IRQ_NONE; /* Not our interrupt */
  3828. igb_write_itr(q_vector);
  3829. /* IMS will not auto-mask if INT_ASSERTED is not set, and if it is
  3830. * not set, then the adapter didn't send an interrupt */
  3831. if (!(icr & E1000_ICR_INT_ASSERTED))
  3832. return IRQ_NONE;
  3833. if (icr & E1000_ICR_DOUTSYNC) {
  3834. /* HW is reporting DMA is out of sync */
  3835. adapter->stats.doosync++;
  3836. }
  3837. if (icr & (E1000_ICR_RXSEQ | E1000_ICR_LSC)) {
  3838. hw->mac.get_link_status = 1;
  3839. /* guard against interrupt when we're going down */
  3840. if (!test_bit(__IGB_DOWN, &adapter->state))
  3841. mod_timer(&adapter->watchdog_timer, jiffies + 1);
  3842. }
  3843. napi_schedule(&q_vector->napi);
  3844. return IRQ_HANDLED;
  3845. }
  3846. static inline void igb_ring_irq_enable(struct igb_q_vector *q_vector)
  3847. {
  3848. struct igb_adapter *adapter = q_vector->adapter;
  3849. struct e1000_hw *hw = &adapter->hw;
  3850. if ((q_vector->rx_ring && (adapter->rx_itr_setting & 3)) ||
  3851. (!q_vector->rx_ring && (adapter->tx_itr_setting & 3))) {
  3852. if (!adapter->msix_entries)
  3853. igb_set_itr(adapter);
  3854. else
  3855. igb_update_ring_itr(q_vector);
  3856. }
  3857. if (!test_bit(__IGB_DOWN, &adapter->state)) {
  3858. if (adapter->msix_entries)
  3859. wr32(E1000_EIMS, q_vector->eims_value);
  3860. else
  3861. igb_irq_enable(adapter);
  3862. }
  3863. }
  3864. /**
  3865. * igb_poll - NAPI Rx polling callback
  3866. * @napi: napi polling structure
  3867. * @budget: count of how many packets we should handle
  3868. **/
  3869. static int igb_poll(struct napi_struct *napi, int budget)
  3870. {
  3871. struct igb_q_vector *q_vector = container_of(napi,
  3872. struct igb_q_vector,
  3873. napi);
  3874. int tx_clean_complete = 1, work_done = 0;
  3875. #ifdef CONFIG_IGB_DCA
  3876. if (q_vector->adapter->flags & IGB_FLAG_DCA_ENABLED)
  3877. igb_update_dca(q_vector);
  3878. #endif
  3879. if (q_vector->tx_ring)
  3880. tx_clean_complete = igb_clean_tx_irq(q_vector);
  3881. if (q_vector->rx_ring)
  3882. igb_clean_rx_irq_adv(q_vector, &work_done, budget);
  3883. if (!tx_clean_complete)
  3884. work_done = budget;
  3885. /* If not enough Rx work done, exit the polling mode */
  3886. if (work_done < budget) {
  3887. napi_complete(napi);
  3888. igb_ring_irq_enable(q_vector);
  3889. }
  3890. return work_done;
  3891. }
  3892. /**
  3893. * igb_systim_to_hwtstamp - convert system time value to hw timestamp
  3894. * @adapter: board private structure
  3895. * @shhwtstamps: timestamp structure to update
  3896. * @regval: unsigned 64bit system time value.
  3897. *
  3898. * We need to convert the system time value stored in the RX/TXSTMP registers
  3899. * into a hwtstamp which can be used by the upper level timestamping functions
  3900. */
  3901. static void igb_systim_to_hwtstamp(struct igb_adapter *adapter,
  3902. struct skb_shared_hwtstamps *shhwtstamps,
  3903. u64 regval)
  3904. {
  3905. u64 ns;
  3906. ns = timecounter_cyc2time(&adapter->clock, regval);
  3907. timecompare_update(&adapter->compare, ns);
  3908. memset(shhwtstamps, 0, sizeof(struct skb_shared_hwtstamps));
  3909. shhwtstamps->hwtstamp = ns_to_ktime(ns);
  3910. shhwtstamps->syststamp = timecompare_transform(&adapter->compare, ns);
  3911. }
  3912. /**
  3913. * igb_tx_hwtstamp - utility function which checks for TX time stamp
  3914. * @q_vector: pointer to q_vector containing needed info
  3915. * @skb: packet that was just sent
  3916. *
  3917. * If we were asked to do hardware stamping and such a time stamp is
  3918. * available, then it must have been for this skb here because we only
  3919. * allow only one such packet into the queue.
  3920. */
  3921. static void igb_tx_hwtstamp(struct igb_q_vector *q_vector, struct sk_buff *skb)
  3922. {
  3923. struct igb_adapter *adapter = q_vector->adapter;
  3924. union skb_shared_tx *shtx = skb_tx(skb);
  3925. struct e1000_hw *hw = &adapter->hw;
  3926. struct skb_shared_hwtstamps shhwtstamps;
  3927. u64 regval;
  3928. /* if skb does not support hw timestamp or TX stamp not valid exit */
  3929. if (likely(!shtx->hardware) ||
  3930. !(rd32(E1000_TSYNCTXCTL) & E1000_TSYNCTXCTL_VALID))
  3931. return;
  3932. regval = rd32(E1000_TXSTMPL);
  3933. regval |= (u64)rd32(E1000_TXSTMPH) << 32;
  3934. igb_systim_to_hwtstamp(adapter, &shhwtstamps, regval);
  3935. skb_tstamp_tx(skb, &shhwtstamps);
  3936. }
  3937. /**
  3938. * igb_clean_tx_irq - Reclaim resources after transmit completes
  3939. * @q_vector: pointer to q_vector containing needed info
  3940. * returns true if ring is completely cleaned
  3941. **/
  3942. static bool igb_clean_tx_irq(struct igb_q_vector *q_vector)
  3943. {
  3944. struct igb_adapter *adapter = q_vector->adapter;
  3945. struct igb_ring *tx_ring = q_vector->tx_ring;
  3946. struct net_device *netdev = tx_ring->netdev;
  3947. struct e1000_hw *hw = &adapter->hw;
  3948. struct igb_buffer *buffer_info;
  3949. struct sk_buff *skb;
  3950. union e1000_adv_tx_desc *tx_desc, *eop_desc;
  3951. unsigned int total_bytes = 0, total_packets = 0;
  3952. unsigned int i, eop, count = 0;
  3953. bool cleaned = false;
  3954. i = tx_ring->next_to_clean;
  3955. eop = tx_ring->buffer_info[i].next_to_watch;
  3956. eop_desc = E1000_TX_DESC_ADV(*tx_ring, eop);
  3957. while ((eop_desc->wb.status & cpu_to_le32(E1000_TXD_STAT_DD)) &&
  3958. (count < tx_ring->count)) {
  3959. for (cleaned = false; !cleaned; count++) {
  3960. tx_desc = E1000_TX_DESC_ADV(*tx_ring, i);
  3961. buffer_info = &tx_ring->buffer_info[i];
  3962. cleaned = (i == eop);
  3963. skb = buffer_info->skb;
  3964. if (skb) {
  3965. unsigned int segs, bytecount;
  3966. /* gso_segs is currently only valid for tcp */
  3967. segs = skb_shinfo(skb)->gso_segs ?: 1;
  3968. /* multiply data chunks by size of headers */
  3969. bytecount = ((segs - 1) * skb_headlen(skb)) +
  3970. skb->len;
  3971. total_packets += segs;
  3972. total_bytes += bytecount;
  3973. igb_tx_hwtstamp(q_vector, skb);
  3974. }
  3975. igb_unmap_and_free_tx_resource(tx_ring, buffer_info);
  3976. tx_desc->wb.status = 0;
  3977. i++;
  3978. if (i == tx_ring->count)
  3979. i = 0;
  3980. }
  3981. eop = tx_ring->buffer_info[i].next_to_watch;
  3982. eop_desc = E1000_TX_DESC_ADV(*tx_ring, eop);
  3983. }
  3984. tx_ring->next_to_clean = i;
  3985. if (unlikely(count &&
  3986. netif_carrier_ok(netdev) &&
  3987. igb_desc_unused(tx_ring) >= IGB_TX_QUEUE_WAKE)) {
  3988. /* Make sure that anybody stopping the queue after this
  3989. * sees the new next_to_clean.
  3990. */
  3991. smp_mb();
  3992. if (__netif_subqueue_stopped(netdev, tx_ring->queue_index) &&
  3993. !(test_bit(__IGB_DOWN, &adapter->state))) {
  3994. netif_wake_subqueue(netdev, tx_ring->queue_index);
  3995. tx_ring->tx_stats.restart_queue++;
  3996. }
  3997. }
  3998. if (tx_ring->detect_tx_hung) {
  3999. /* Detect a transmit hang in hardware, this serializes the
  4000. * check with the clearing of time_stamp and movement of i */
  4001. tx_ring->detect_tx_hung = false;
  4002. if (tx_ring->buffer_info[i].time_stamp &&
  4003. time_after(jiffies, tx_ring->buffer_info[i].time_stamp +
  4004. (adapter->tx_timeout_factor * HZ))
  4005. && !(rd32(E1000_STATUS) &
  4006. E1000_STATUS_TXOFF)) {
  4007. /* detected Tx unit hang */
  4008. dev_err(&tx_ring->pdev->dev,
  4009. "Detected Tx Unit Hang\n"
  4010. " Tx Queue <%d>\n"
  4011. " TDH <%x>\n"
  4012. " TDT <%x>\n"
  4013. " next_to_use <%x>\n"
  4014. " next_to_clean <%x>\n"
  4015. "buffer_info[next_to_clean]\n"
  4016. " time_stamp <%lx>\n"
  4017. " next_to_watch <%x>\n"
  4018. " jiffies <%lx>\n"
  4019. " desc.status <%x>\n",
  4020. tx_ring->queue_index,
  4021. readl(tx_ring->head),
  4022. readl(tx_ring->tail),
  4023. tx_ring->next_to_use,
  4024. tx_ring->next_to_clean,
  4025. tx_ring->buffer_info[i].time_stamp,
  4026. eop,
  4027. jiffies,
  4028. eop_desc->wb.status);
  4029. netif_stop_subqueue(netdev, tx_ring->queue_index);
  4030. }
  4031. }
  4032. tx_ring->total_bytes += total_bytes;
  4033. tx_ring->total_packets += total_packets;
  4034. tx_ring->tx_stats.bytes += total_bytes;
  4035. tx_ring->tx_stats.packets += total_packets;
  4036. netdev->stats.tx_bytes += total_bytes;
  4037. netdev->stats.tx_packets += total_packets;
  4038. return (count < tx_ring->count);
  4039. }
  4040. /**
  4041. * igb_receive_skb - helper function to handle rx indications
  4042. * @q_vector: structure containing interrupt and ring information
  4043. * @skb: packet to send up
  4044. * @vlan_tag: vlan tag for packet
  4045. **/
  4046. static void igb_receive_skb(struct igb_q_vector *q_vector,
  4047. struct sk_buff *skb,
  4048. u16 vlan_tag)
  4049. {
  4050. struct igb_adapter *adapter = q_vector->adapter;
  4051. if (vlan_tag)
  4052. vlan_gro_receive(&q_vector->napi, adapter->vlgrp,
  4053. vlan_tag, skb);
  4054. else
  4055. napi_gro_receive(&q_vector->napi, skb);
  4056. }
  4057. static inline void igb_rx_checksum_adv(struct igb_ring *ring,
  4058. u32 status_err, struct sk_buff *skb)
  4059. {
  4060. skb->ip_summed = CHECKSUM_NONE;
  4061. /* Ignore Checksum bit is set or checksum is disabled through ethtool */
  4062. if (!(ring->flags & IGB_RING_FLAG_RX_CSUM) ||
  4063. (status_err & E1000_RXD_STAT_IXSM))
  4064. return;
  4065. /* TCP/UDP checksum error bit is set */
  4066. if (status_err &
  4067. (E1000_RXDEXT_STATERR_TCPE | E1000_RXDEXT_STATERR_IPE)) {
  4068. /*
  4069. * work around errata with sctp packets where the TCPE aka
  4070. * L4E bit is set incorrectly on 64 byte (60 byte w/o crc)
  4071. * packets, (aka let the stack check the crc32c)
  4072. */
  4073. if ((skb->len == 60) &&
  4074. (ring->flags & IGB_RING_FLAG_RX_SCTP_CSUM))
  4075. ring->rx_stats.csum_err++;
  4076. /* let the stack verify checksum errors */
  4077. return;
  4078. }
  4079. /* It must be a TCP or UDP packet with a valid checksum */
  4080. if (status_err & (E1000_RXD_STAT_TCPCS | E1000_RXD_STAT_UDPCS))
  4081. skb->ip_summed = CHECKSUM_UNNECESSARY;
  4082. dev_dbg(&ring->pdev->dev, "cksum success: bits %08X\n", status_err);
  4083. }
  4084. static inline void igb_rx_hwtstamp(struct igb_q_vector *q_vector, u32 staterr,
  4085. struct sk_buff *skb)
  4086. {
  4087. struct igb_adapter *adapter = q_vector->adapter;
  4088. struct e1000_hw *hw = &adapter->hw;
  4089. u64 regval;
  4090. /*
  4091. * If this bit is set, then the RX registers contain the time stamp. No
  4092. * other packet will be time stamped until we read these registers, so
  4093. * read the registers to make them available again. Because only one
  4094. * packet can be time stamped at a time, we know that the register
  4095. * values must belong to this one here and therefore we don't need to
  4096. * compare any of the additional attributes stored for it.
  4097. *
  4098. * If nothing went wrong, then it should have a skb_shared_tx that we
  4099. * can turn into a skb_shared_hwtstamps.
  4100. */
  4101. if (likely(!(staterr & E1000_RXDADV_STAT_TS)))
  4102. return;
  4103. if (!(rd32(E1000_TSYNCRXCTL) & E1000_TSYNCRXCTL_VALID))
  4104. return;
  4105. regval = rd32(E1000_RXSTMPL);
  4106. regval |= (u64)rd32(E1000_RXSTMPH) << 32;
  4107. igb_systim_to_hwtstamp(adapter, skb_hwtstamps(skb), regval);
  4108. }
  4109. static inline u16 igb_get_hlen(struct igb_ring *rx_ring,
  4110. union e1000_adv_rx_desc *rx_desc)
  4111. {
  4112. /* HW will not DMA in data larger than the given buffer, even if it
  4113. * parses the (NFS, of course) header to be larger. In that case, it
  4114. * fills the header buffer and spills the rest into the page.
  4115. */
  4116. u16 hlen = (le16_to_cpu(rx_desc->wb.lower.lo_dword.hdr_info) &
  4117. E1000_RXDADV_HDRBUFLEN_MASK) >> E1000_RXDADV_HDRBUFLEN_SHIFT;
  4118. if (hlen > rx_ring->rx_buffer_len)
  4119. hlen = rx_ring->rx_buffer_len;
  4120. return hlen;
  4121. }
  4122. static bool igb_clean_rx_irq_adv(struct igb_q_vector *q_vector,
  4123. int *work_done, int budget)
  4124. {
  4125. struct igb_ring *rx_ring = q_vector->rx_ring;
  4126. struct net_device *netdev = rx_ring->netdev;
  4127. struct pci_dev *pdev = rx_ring->pdev;
  4128. union e1000_adv_rx_desc *rx_desc , *next_rxd;
  4129. struct igb_buffer *buffer_info , *next_buffer;
  4130. struct sk_buff *skb;
  4131. bool cleaned = false;
  4132. int cleaned_count = 0;
  4133. unsigned int total_bytes = 0, total_packets = 0;
  4134. unsigned int i;
  4135. u32 staterr;
  4136. u16 length;
  4137. u16 vlan_tag;
  4138. i = rx_ring->next_to_clean;
  4139. buffer_info = &rx_ring->buffer_info[i];
  4140. rx_desc = E1000_RX_DESC_ADV(*rx_ring, i);
  4141. staterr = le32_to_cpu(rx_desc->wb.upper.status_error);
  4142. while (staterr & E1000_RXD_STAT_DD) {
  4143. if (*work_done >= budget)
  4144. break;
  4145. (*work_done)++;
  4146. skb = buffer_info->skb;
  4147. prefetch(skb->data - NET_IP_ALIGN);
  4148. buffer_info->skb = NULL;
  4149. i++;
  4150. if (i == rx_ring->count)
  4151. i = 0;
  4152. next_rxd = E1000_RX_DESC_ADV(*rx_ring, i);
  4153. prefetch(next_rxd);
  4154. next_buffer = &rx_ring->buffer_info[i];
  4155. length = le16_to_cpu(rx_desc->wb.upper.length);
  4156. cleaned = true;
  4157. cleaned_count++;
  4158. if (buffer_info->dma) {
  4159. pci_unmap_single(pdev, buffer_info->dma,
  4160. rx_ring->rx_buffer_len,
  4161. PCI_DMA_FROMDEVICE);
  4162. buffer_info->dma = 0;
  4163. if (rx_ring->rx_buffer_len >= IGB_RXBUFFER_1024) {
  4164. skb_put(skb, length);
  4165. goto send_up;
  4166. }
  4167. skb_put(skb, igb_get_hlen(rx_ring, rx_desc));
  4168. }
  4169. if (length) {
  4170. pci_unmap_page(pdev, buffer_info->page_dma,
  4171. PAGE_SIZE / 2, PCI_DMA_FROMDEVICE);
  4172. buffer_info->page_dma = 0;
  4173. skb_fill_page_desc(skb, skb_shinfo(skb)->nr_frags++,
  4174. buffer_info->page,
  4175. buffer_info->page_offset,
  4176. length);
  4177. if (page_count(buffer_info->page) != 1)
  4178. buffer_info->page = NULL;
  4179. else
  4180. get_page(buffer_info->page);
  4181. skb->len += length;
  4182. skb->data_len += length;
  4183. skb->truesize += length;
  4184. }
  4185. if (!(staterr & E1000_RXD_STAT_EOP)) {
  4186. buffer_info->skb = next_buffer->skb;
  4187. buffer_info->dma = next_buffer->dma;
  4188. next_buffer->skb = skb;
  4189. next_buffer->dma = 0;
  4190. goto next_desc;
  4191. }
  4192. send_up:
  4193. if (staterr & E1000_RXDEXT_ERR_FRAME_ERR_MASK) {
  4194. dev_kfree_skb_irq(skb);
  4195. goto next_desc;
  4196. }
  4197. igb_rx_hwtstamp(q_vector, staterr, skb);
  4198. total_bytes += skb->len;
  4199. total_packets++;
  4200. igb_rx_checksum_adv(rx_ring, staterr, skb);
  4201. skb->protocol = eth_type_trans(skb, netdev);
  4202. skb_record_rx_queue(skb, rx_ring->queue_index);
  4203. vlan_tag = ((staterr & E1000_RXD_STAT_VP) ?
  4204. le16_to_cpu(rx_desc->wb.upper.vlan) : 0);
  4205. igb_receive_skb(q_vector, skb, vlan_tag);
  4206. next_desc:
  4207. rx_desc->wb.upper.status_error = 0;
  4208. /* return some buffers to hardware, one at a time is too slow */
  4209. if (cleaned_count >= IGB_RX_BUFFER_WRITE) {
  4210. igb_alloc_rx_buffers_adv(rx_ring, cleaned_count);
  4211. cleaned_count = 0;
  4212. }
  4213. /* use prefetched values */
  4214. rx_desc = next_rxd;
  4215. buffer_info = next_buffer;
  4216. staterr = le32_to_cpu(rx_desc->wb.upper.status_error);
  4217. }
  4218. rx_ring->next_to_clean = i;
  4219. cleaned_count = igb_desc_unused(rx_ring);
  4220. if (cleaned_count)
  4221. igb_alloc_rx_buffers_adv(rx_ring, cleaned_count);
  4222. rx_ring->total_packets += total_packets;
  4223. rx_ring->total_bytes += total_bytes;
  4224. rx_ring->rx_stats.packets += total_packets;
  4225. rx_ring->rx_stats.bytes += total_bytes;
  4226. netdev->stats.rx_bytes += total_bytes;
  4227. netdev->stats.rx_packets += total_packets;
  4228. return cleaned;
  4229. }
  4230. /**
  4231. * igb_alloc_rx_buffers_adv - Replace used receive buffers; packet split
  4232. * @adapter: address of board private structure
  4233. **/
  4234. void igb_alloc_rx_buffers_adv(struct igb_ring *rx_ring, int cleaned_count)
  4235. {
  4236. struct net_device *netdev = rx_ring->netdev;
  4237. union e1000_adv_rx_desc *rx_desc;
  4238. struct igb_buffer *buffer_info;
  4239. struct sk_buff *skb;
  4240. unsigned int i;
  4241. int bufsz;
  4242. i = rx_ring->next_to_use;
  4243. buffer_info = &rx_ring->buffer_info[i];
  4244. bufsz = rx_ring->rx_buffer_len;
  4245. while (cleaned_count--) {
  4246. rx_desc = E1000_RX_DESC_ADV(*rx_ring, i);
  4247. if ((bufsz < IGB_RXBUFFER_1024) && !buffer_info->page_dma) {
  4248. if (!buffer_info->page) {
  4249. buffer_info->page = alloc_page(GFP_ATOMIC);
  4250. if (!buffer_info->page) {
  4251. rx_ring->rx_stats.alloc_failed++;
  4252. goto no_buffers;
  4253. }
  4254. buffer_info->page_offset = 0;
  4255. } else {
  4256. buffer_info->page_offset ^= PAGE_SIZE / 2;
  4257. }
  4258. buffer_info->page_dma =
  4259. pci_map_page(rx_ring->pdev, buffer_info->page,
  4260. buffer_info->page_offset,
  4261. PAGE_SIZE / 2,
  4262. PCI_DMA_FROMDEVICE);
  4263. }
  4264. if (!buffer_info->skb) {
  4265. skb = netdev_alloc_skb_ip_align(netdev, bufsz);
  4266. if (!skb) {
  4267. rx_ring->rx_stats.alloc_failed++;
  4268. goto no_buffers;
  4269. }
  4270. buffer_info->skb = skb;
  4271. buffer_info->dma = pci_map_single(rx_ring->pdev,
  4272. skb->data,
  4273. bufsz,
  4274. PCI_DMA_FROMDEVICE);
  4275. }
  4276. /* Refresh the desc even if buffer_addrs didn't change because
  4277. * each write-back erases this info. */
  4278. if (bufsz < IGB_RXBUFFER_1024) {
  4279. rx_desc->read.pkt_addr =
  4280. cpu_to_le64(buffer_info->page_dma);
  4281. rx_desc->read.hdr_addr = cpu_to_le64(buffer_info->dma);
  4282. } else {
  4283. rx_desc->read.pkt_addr =
  4284. cpu_to_le64(buffer_info->dma);
  4285. rx_desc->read.hdr_addr = 0;
  4286. }
  4287. i++;
  4288. if (i == rx_ring->count)
  4289. i = 0;
  4290. buffer_info = &rx_ring->buffer_info[i];
  4291. }
  4292. no_buffers:
  4293. if (rx_ring->next_to_use != i) {
  4294. rx_ring->next_to_use = i;
  4295. if (i == 0)
  4296. i = (rx_ring->count - 1);
  4297. else
  4298. i--;
  4299. /* Force memory writes to complete before letting h/w
  4300. * know there are new descriptors to fetch. (Only
  4301. * applicable for weak-ordered memory model archs,
  4302. * such as IA-64). */
  4303. wmb();
  4304. writel(i, rx_ring->tail);
  4305. }
  4306. }
  4307. /**
  4308. * igb_mii_ioctl -
  4309. * @netdev:
  4310. * @ifreq:
  4311. * @cmd:
  4312. **/
  4313. static int igb_mii_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
  4314. {
  4315. struct igb_adapter *adapter = netdev_priv(netdev);
  4316. struct mii_ioctl_data *data = if_mii(ifr);
  4317. if (adapter->hw.phy.media_type != e1000_media_type_copper)
  4318. return -EOPNOTSUPP;
  4319. switch (cmd) {
  4320. case SIOCGMIIPHY:
  4321. data->phy_id = adapter->hw.phy.addr;
  4322. break;
  4323. case SIOCGMIIREG:
  4324. if (igb_read_phy_reg(&adapter->hw, data->reg_num & 0x1F,
  4325. &data->val_out))
  4326. return -EIO;
  4327. break;
  4328. case SIOCSMIIREG:
  4329. default:
  4330. return -EOPNOTSUPP;
  4331. }
  4332. return 0;
  4333. }
  4334. /**
  4335. * igb_hwtstamp_ioctl - control hardware time stamping
  4336. * @netdev:
  4337. * @ifreq:
  4338. * @cmd:
  4339. *
  4340. * Outgoing time stamping can be enabled and disabled. Play nice and
  4341. * disable it when requested, although it shouldn't case any overhead
  4342. * when no packet needs it. At most one packet in the queue may be
  4343. * marked for time stamping, otherwise it would be impossible to tell
  4344. * for sure to which packet the hardware time stamp belongs.
  4345. *
  4346. * Incoming time stamping has to be configured via the hardware
  4347. * filters. Not all combinations are supported, in particular event
  4348. * type has to be specified. Matching the kind of event packet is
  4349. * not supported, with the exception of "all V2 events regardless of
  4350. * level 2 or 4".
  4351. *
  4352. **/
  4353. static int igb_hwtstamp_ioctl(struct net_device *netdev,
  4354. struct ifreq *ifr, int cmd)
  4355. {
  4356. struct igb_adapter *adapter = netdev_priv(netdev);
  4357. struct e1000_hw *hw = &adapter->hw;
  4358. struct hwtstamp_config config;
  4359. u32 tsync_tx_ctl = E1000_TSYNCTXCTL_ENABLED;
  4360. u32 tsync_rx_ctl = E1000_TSYNCRXCTL_ENABLED;
  4361. u32 tsync_rx_cfg = 0;
  4362. bool is_l4 = false;
  4363. bool is_l2 = false;
  4364. u32 regval;
  4365. if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
  4366. return -EFAULT;
  4367. /* reserved for future extensions */
  4368. if (config.flags)
  4369. return -EINVAL;
  4370. switch (config.tx_type) {
  4371. case HWTSTAMP_TX_OFF:
  4372. tsync_tx_ctl = 0;
  4373. case HWTSTAMP_TX_ON:
  4374. break;
  4375. default:
  4376. return -ERANGE;
  4377. }
  4378. switch (config.rx_filter) {
  4379. case HWTSTAMP_FILTER_NONE:
  4380. tsync_rx_ctl = 0;
  4381. break;
  4382. case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
  4383. case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
  4384. case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
  4385. case HWTSTAMP_FILTER_ALL:
  4386. /*
  4387. * register TSYNCRXCFG must be set, therefore it is not
  4388. * possible to time stamp both Sync and Delay_Req messages
  4389. * => fall back to time stamping all packets
  4390. */
  4391. tsync_rx_ctl |= E1000_TSYNCRXCTL_TYPE_ALL;
  4392. config.rx_filter = HWTSTAMP_FILTER_ALL;
  4393. break;
  4394. case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
  4395. tsync_rx_ctl |= E1000_TSYNCRXCTL_TYPE_L4_V1;
  4396. tsync_rx_cfg = E1000_TSYNCRXCFG_PTP_V1_SYNC_MESSAGE;
  4397. is_l4 = true;
  4398. break;
  4399. case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
  4400. tsync_rx_ctl |= E1000_TSYNCRXCTL_TYPE_L4_V1;
  4401. tsync_rx_cfg = E1000_TSYNCRXCFG_PTP_V1_DELAY_REQ_MESSAGE;
  4402. is_l4 = true;
  4403. break;
  4404. case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
  4405. case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
  4406. tsync_rx_ctl |= E1000_TSYNCRXCTL_TYPE_L2_L4_V2;
  4407. tsync_rx_cfg = E1000_TSYNCRXCFG_PTP_V2_SYNC_MESSAGE;
  4408. is_l2 = true;
  4409. is_l4 = true;
  4410. config.rx_filter = HWTSTAMP_FILTER_SOME;
  4411. break;
  4412. case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
  4413. case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
  4414. tsync_rx_ctl |= E1000_TSYNCRXCTL_TYPE_L2_L4_V2;
  4415. tsync_rx_cfg = E1000_TSYNCRXCFG_PTP_V2_DELAY_REQ_MESSAGE;
  4416. is_l2 = true;
  4417. is_l4 = true;
  4418. config.rx_filter = HWTSTAMP_FILTER_SOME;
  4419. break;
  4420. case HWTSTAMP_FILTER_PTP_V2_EVENT:
  4421. case HWTSTAMP_FILTER_PTP_V2_SYNC:
  4422. case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
  4423. tsync_rx_ctl |= E1000_TSYNCRXCTL_TYPE_EVENT_V2;
  4424. config.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
  4425. is_l2 = true;
  4426. break;
  4427. default:
  4428. return -ERANGE;
  4429. }
  4430. if (hw->mac.type == e1000_82575) {
  4431. if (tsync_rx_ctl | tsync_tx_ctl)
  4432. return -EINVAL;
  4433. return 0;
  4434. }
  4435. /* enable/disable TX */
  4436. regval = rd32(E1000_TSYNCTXCTL);
  4437. regval &= ~E1000_TSYNCTXCTL_ENABLED;
  4438. regval |= tsync_tx_ctl;
  4439. wr32(E1000_TSYNCTXCTL, regval);
  4440. /* enable/disable RX */
  4441. regval = rd32(E1000_TSYNCRXCTL);
  4442. regval &= ~(E1000_TSYNCRXCTL_ENABLED | E1000_TSYNCRXCTL_TYPE_MASK);
  4443. regval |= tsync_rx_ctl;
  4444. wr32(E1000_TSYNCRXCTL, regval);
  4445. /* define which PTP packets are time stamped */
  4446. wr32(E1000_TSYNCRXCFG, tsync_rx_cfg);
  4447. /* define ethertype filter for timestamped packets */
  4448. if (is_l2)
  4449. wr32(E1000_ETQF(3),
  4450. (E1000_ETQF_FILTER_ENABLE | /* enable filter */
  4451. E1000_ETQF_1588 | /* enable timestamping */
  4452. ETH_P_1588)); /* 1588 eth protocol type */
  4453. else
  4454. wr32(E1000_ETQF(3), 0);
  4455. #define PTP_PORT 319
  4456. /* L4 Queue Filter[3]: filter by destination port and protocol */
  4457. if (is_l4) {
  4458. u32 ftqf = (IPPROTO_UDP /* UDP */
  4459. | E1000_FTQF_VF_BP /* VF not compared */
  4460. | E1000_FTQF_1588_TIME_STAMP /* Enable Timestamping */
  4461. | E1000_FTQF_MASK); /* mask all inputs */
  4462. ftqf &= ~E1000_FTQF_MASK_PROTO_BP; /* enable protocol check */
  4463. wr32(E1000_IMIR(3), htons(PTP_PORT));
  4464. wr32(E1000_IMIREXT(3),
  4465. (E1000_IMIREXT_SIZE_BP | E1000_IMIREXT_CTRL_BP));
  4466. if (hw->mac.type == e1000_82576) {
  4467. /* enable source port check */
  4468. wr32(E1000_SPQF(3), htons(PTP_PORT));
  4469. ftqf &= ~E1000_FTQF_MASK_SOURCE_PORT_BP;
  4470. }
  4471. wr32(E1000_FTQF(3), ftqf);
  4472. } else {
  4473. wr32(E1000_FTQF(3), E1000_FTQF_MASK);
  4474. }
  4475. wrfl();
  4476. adapter->hwtstamp_config = config;
  4477. /* clear TX/RX time stamp registers, just to be sure */
  4478. regval = rd32(E1000_TXSTMPH);
  4479. regval = rd32(E1000_RXSTMPH);
  4480. return copy_to_user(ifr->ifr_data, &config, sizeof(config)) ?
  4481. -EFAULT : 0;
  4482. }
  4483. /**
  4484. * igb_ioctl -
  4485. * @netdev:
  4486. * @ifreq:
  4487. * @cmd:
  4488. **/
  4489. static int igb_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
  4490. {
  4491. switch (cmd) {
  4492. case SIOCGMIIPHY:
  4493. case SIOCGMIIREG:
  4494. case SIOCSMIIREG:
  4495. return igb_mii_ioctl(netdev, ifr, cmd);
  4496. case SIOCSHWTSTAMP:
  4497. return igb_hwtstamp_ioctl(netdev, ifr, cmd);
  4498. default:
  4499. return -EOPNOTSUPP;
  4500. }
  4501. }
  4502. s32 igb_read_pcie_cap_reg(struct e1000_hw *hw, u32 reg, u16 *value)
  4503. {
  4504. struct igb_adapter *adapter = hw->back;
  4505. u16 cap_offset;
  4506. cap_offset = pci_find_capability(adapter->pdev, PCI_CAP_ID_EXP);
  4507. if (!cap_offset)
  4508. return -E1000_ERR_CONFIG;
  4509. pci_read_config_word(adapter->pdev, cap_offset + reg, value);
  4510. return 0;
  4511. }
  4512. s32 igb_write_pcie_cap_reg(struct e1000_hw *hw, u32 reg, u16 *value)
  4513. {
  4514. struct igb_adapter *adapter = hw->back;
  4515. u16 cap_offset;
  4516. cap_offset = pci_find_capability(adapter->pdev, PCI_CAP_ID_EXP);
  4517. if (!cap_offset)
  4518. return -E1000_ERR_CONFIG;
  4519. pci_write_config_word(adapter->pdev, cap_offset + reg, *value);
  4520. return 0;
  4521. }
  4522. static void igb_vlan_rx_register(struct net_device *netdev,
  4523. struct vlan_group *grp)
  4524. {
  4525. struct igb_adapter *adapter = netdev_priv(netdev);
  4526. struct e1000_hw *hw = &adapter->hw;
  4527. u32 ctrl, rctl;
  4528. igb_irq_disable(adapter);
  4529. adapter->vlgrp = grp;
  4530. if (grp) {
  4531. /* enable VLAN tag insert/strip */
  4532. ctrl = rd32(E1000_CTRL);
  4533. ctrl |= E1000_CTRL_VME;
  4534. wr32(E1000_CTRL, ctrl);
  4535. /* enable VLAN receive filtering */
  4536. rctl = rd32(E1000_RCTL);
  4537. rctl &= ~E1000_RCTL_CFIEN;
  4538. wr32(E1000_RCTL, rctl);
  4539. igb_update_mng_vlan(adapter);
  4540. } else {
  4541. /* disable VLAN tag insert/strip */
  4542. ctrl = rd32(E1000_CTRL);
  4543. ctrl &= ~E1000_CTRL_VME;
  4544. wr32(E1000_CTRL, ctrl);
  4545. if (adapter->mng_vlan_id != (u16)IGB_MNG_VLAN_NONE) {
  4546. igb_vlan_rx_kill_vid(netdev, adapter->mng_vlan_id);
  4547. adapter->mng_vlan_id = IGB_MNG_VLAN_NONE;
  4548. }
  4549. }
  4550. igb_rlpml_set(adapter);
  4551. if (!test_bit(__IGB_DOWN, &adapter->state))
  4552. igb_irq_enable(adapter);
  4553. }
  4554. static void igb_vlan_rx_add_vid(struct net_device *netdev, u16 vid)
  4555. {
  4556. struct igb_adapter *adapter = netdev_priv(netdev);
  4557. struct e1000_hw *hw = &adapter->hw;
  4558. int pf_id = adapter->vfs_allocated_count;
  4559. if ((hw->mng_cookie.status &
  4560. E1000_MNG_DHCP_COOKIE_STATUS_VLAN) &&
  4561. (vid == adapter->mng_vlan_id))
  4562. return;
  4563. /* add vid to vlvf if sr-iov is enabled,
  4564. * if that fails add directly to filter table */
  4565. if (igb_vlvf_set(adapter, vid, true, pf_id))
  4566. igb_vfta_set(hw, vid, true);
  4567. }
  4568. static void igb_vlan_rx_kill_vid(struct net_device *netdev, u16 vid)
  4569. {
  4570. struct igb_adapter *adapter = netdev_priv(netdev);
  4571. struct e1000_hw *hw = &adapter->hw;
  4572. int pf_id = adapter->vfs_allocated_count;
  4573. igb_irq_disable(adapter);
  4574. vlan_group_set_device(adapter->vlgrp, vid, NULL);
  4575. if (!test_bit(__IGB_DOWN, &adapter->state))
  4576. igb_irq_enable(adapter);
  4577. if ((adapter->hw.mng_cookie.status &
  4578. E1000_MNG_DHCP_COOKIE_STATUS_VLAN) &&
  4579. (vid == adapter->mng_vlan_id)) {
  4580. /* release control to f/w */
  4581. igb_release_hw_control(adapter);
  4582. return;
  4583. }
  4584. /* remove vid from vlvf if sr-iov is enabled,
  4585. * if not in vlvf remove from vfta */
  4586. if (igb_vlvf_set(adapter, vid, false, pf_id))
  4587. igb_vfta_set(hw, vid, false);
  4588. }
  4589. static void igb_restore_vlan(struct igb_adapter *adapter)
  4590. {
  4591. igb_vlan_rx_register(adapter->netdev, adapter->vlgrp);
  4592. if (adapter->vlgrp) {
  4593. u16 vid;
  4594. for (vid = 0; vid < VLAN_GROUP_ARRAY_LEN; vid++) {
  4595. if (!vlan_group_get_device(adapter->vlgrp, vid))
  4596. continue;
  4597. igb_vlan_rx_add_vid(adapter->netdev, vid);
  4598. }
  4599. }
  4600. }
  4601. int igb_set_spd_dplx(struct igb_adapter *adapter, u16 spddplx)
  4602. {
  4603. struct e1000_mac_info *mac = &adapter->hw.mac;
  4604. mac->autoneg = 0;
  4605. switch (spddplx) {
  4606. case SPEED_10 + DUPLEX_HALF:
  4607. mac->forced_speed_duplex = ADVERTISE_10_HALF;
  4608. break;
  4609. case SPEED_10 + DUPLEX_FULL:
  4610. mac->forced_speed_duplex = ADVERTISE_10_FULL;
  4611. break;
  4612. case SPEED_100 + DUPLEX_HALF:
  4613. mac->forced_speed_duplex = ADVERTISE_100_HALF;
  4614. break;
  4615. case SPEED_100 + DUPLEX_FULL:
  4616. mac->forced_speed_duplex = ADVERTISE_100_FULL;
  4617. break;
  4618. case SPEED_1000 + DUPLEX_FULL:
  4619. mac->autoneg = 1;
  4620. adapter->hw.phy.autoneg_advertised = ADVERTISE_1000_FULL;
  4621. break;
  4622. case SPEED_1000 + DUPLEX_HALF: /* not supported */
  4623. default:
  4624. dev_err(&adapter->pdev->dev,
  4625. "Unsupported Speed/Duplex configuration\n");
  4626. return -EINVAL;
  4627. }
  4628. return 0;
  4629. }
  4630. static int __igb_shutdown(struct pci_dev *pdev, bool *enable_wake)
  4631. {
  4632. struct net_device *netdev = pci_get_drvdata(pdev);
  4633. struct igb_adapter *adapter = netdev_priv(netdev);
  4634. struct e1000_hw *hw = &adapter->hw;
  4635. u32 ctrl, rctl, status;
  4636. u32 wufc = adapter->wol;
  4637. #ifdef CONFIG_PM
  4638. int retval = 0;
  4639. #endif
  4640. netif_device_detach(netdev);
  4641. if (netif_running(netdev))
  4642. igb_close(netdev);
  4643. igb_clear_interrupt_scheme(adapter);
  4644. #ifdef CONFIG_PM
  4645. retval = pci_save_state(pdev);
  4646. if (retval)
  4647. return retval;
  4648. #endif
  4649. status = rd32(E1000_STATUS);
  4650. if (status & E1000_STATUS_LU)
  4651. wufc &= ~E1000_WUFC_LNKC;
  4652. if (wufc) {
  4653. igb_setup_rctl(adapter);
  4654. igb_set_rx_mode(netdev);
  4655. /* turn on all-multi mode if wake on multicast is enabled */
  4656. if (wufc & E1000_WUFC_MC) {
  4657. rctl = rd32(E1000_RCTL);
  4658. rctl |= E1000_RCTL_MPE;
  4659. wr32(E1000_RCTL, rctl);
  4660. }
  4661. ctrl = rd32(E1000_CTRL);
  4662. /* advertise wake from D3Cold */
  4663. #define E1000_CTRL_ADVD3WUC 0x00100000
  4664. /* phy power management enable */
  4665. #define E1000_CTRL_EN_PHY_PWR_MGMT 0x00200000
  4666. ctrl |= E1000_CTRL_ADVD3WUC;
  4667. wr32(E1000_CTRL, ctrl);
  4668. /* Allow time for pending master requests to run */
  4669. igb_disable_pcie_master(&adapter->hw);
  4670. wr32(E1000_WUC, E1000_WUC_PME_EN);
  4671. wr32(E1000_WUFC, wufc);
  4672. } else {
  4673. wr32(E1000_WUC, 0);
  4674. wr32(E1000_WUFC, 0);
  4675. }
  4676. *enable_wake = wufc || adapter->en_mng_pt;
  4677. if (!*enable_wake)
  4678. igb_shutdown_serdes_link_82575(hw);
  4679. /* Release control of h/w to f/w. If f/w is AMT enabled, this
  4680. * would have already happened in close and is redundant. */
  4681. igb_release_hw_control(adapter);
  4682. pci_disable_device(pdev);
  4683. return 0;
  4684. }
  4685. #ifdef CONFIG_PM
  4686. static int igb_suspend(struct pci_dev *pdev, pm_message_t state)
  4687. {
  4688. int retval;
  4689. bool wake;
  4690. retval = __igb_shutdown(pdev, &wake);
  4691. if (retval)
  4692. return retval;
  4693. if (wake) {
  4694. pci_prepare_to_sleep(pdev);
  4695. } else {
  4696. pci_wake_from_d3(pdev, false);
  4697. pci_set_power_state(pdev, PCI_D3hot);
  4698. }
  4699. return 0;
  4700. }
  4701. static int igb_resume(struct pci_dev *pdev)
  4702. {
  4703. struct net_device *netdev = pci_get_drvdata(pdev);
  4704. struct igb_adapter *adapter = netdev_priv(netdev);
  4705. struct e1000_hw *hw = &adapter->hw;
  4706. u32 err;
  4707. pci_set_power_state(pdev, PCI_D0);
  4708. pci_restore_state(pdev);
  4709. err = pci_enable_device_mem(pdev);
  4710. if (err) {
  4711. dev_err(&pdev->dev,
  4712. "igb: Cannot enable PCI device from suspend\n");
  4713. return err;
  4714. }
  4715. pci_set_master(pdev);
  4716. pci_enable_wake(pdev, PCI_D3hot, 0);
  4717. pci_enable_wake(pdev, PCI_D3cold, 0);
  4718. if (igb_init_interrupt_scheme(adapter)) {
  4719. dev_err(&pdev->dev, "Unable to allocate memory for queues\n");
  4720. return -ENOMEM;
  4721. }
  4722. /* e1000_power_up_phy(adapter); */
  4723. igb_reset(adapter);
  4724. /* let the f/w know that the h/w is now under the control of the
  4725. * driver. */
  4726. igb_get_hw_control(adapter);
  4727. wr32(E1000_WUS, ~0);
  4728. if (netif_running(netdev)) {
  4729. err = igb_open(netdev);
  4730. if (err)
  4731. return err;
  4732. }
  4733. netif_device_attach(netdev);
  4734. return 0;
  4735. }
  4736. #endif
  4737. static void igb_shutdown(struct pci_dev *pdev)
  4738. {
  4739. bool wake;
  4740. __igb_shutdown(pdev, &wake);
  4741. if (system_state == SYSTEM_POWER_OFF) {
  4742. pci_wake_from_d3(pdev, wake);
  4743. pci_set_power_state(pdev, PCI_D3hot);
  4744. }
  4745. }
  4746. #ifdef CONFIG_NET_POLL_CONTROLLER
  4747. /*
  4748. * Polling 'interrupt' - used by things like netconsole to send skbs
  4749. * without having to re-enable interrupts. It's not called while
  4750. * the interrupt routine is executing.
  4751. */
  4752. static void igb_netpoll(struct net_device *netdev)
  4753. {
  4754. struct igb_adapter *adapter = netdev_priv(netdev);
  4755. struct e1000_hw *hw = &adapter->hw;
  4756. int i;
  4757. if (!adapter->msix_entries) {
  4758. struct igb_q_vector *q_vector = adapter->q_vector[0];
  4759. igb_irq_disable(adapter);
  4760. napi_schedule(&q_vector->napi);
  4761. return;
  4762. }
  4763. for (i = 0; i < adapter->num_q_vectors; i++) {
  4764. struct igb_q_vector *q_vector = adapter->q_vector[i];
  4765. wr32(E1000_EIMC, q_vector->eims_value);
  4766. napi_schedule(&q_vector->napi);
  4767. }
  4768. }
  4769. #endif /* CONFIG_NET_POLL_CONTROLLER */
  4770. /**
  4771. * igb_io_error_detected - called when PCI error is detected
  4772. * @pdev: Pointer to PCI device
  4773. * @state: The current pci connection state
  4774. *
  4775. * This function is called after a PCI bus error affecting
  4776. * this device has been detected.
  4777. */
  4778. static pci_ers_result_t igb_io_error_detected(struct pci_dev *pdev,
  4779. pci_channel_state_t state)
  4780. {
  4781. struct net_device *netdev = pci_get_drvdata(pdev);
  4782. struct igb_adapter *adapter = netdev_priv(netdev);
  4783. netif_device_detach(netdev);
  4784. if (state == pci_channel_io_perm_failure)
  4785. return PCI_ERS_RESULT_DISCONNECT;
  4786. if (netif_running(netdev))
  4787. igb_down(adapter);
  4788. pci_disable_device(pdev);
  4789. /* Request a slot slot reset. */
  4790. return PCI_ERS_RESULT_NEED_RESET;
  4791. }
  4792. /**
  4793. * igb_io_slot_reset - called after the pci bus has been reset.
  4794. * @pdev: Pointer to PCI device
  4795. *
  4796. * Restart the card from scratch, as if from a cold-boot. Implementation
  4797. * resembles the first-half of the igb_resume routine.
  4798. */
  4799. static pci_ers_result_t igb_io_slot_reset(struct pci_dev *pdev)
  4800. {
  4801. struct net_device *netdev = pci_get_drvdata(pdev);
  4802. struct igb_adapter *adapter = netdev_priv(netdev);
  4803. struct e1000_hw *hw = &adapter->hw;
  4804. pci_ers_result_t result;
  4805. int err;
  4806. if (pci_enable_device_mem(pdev)) {
  4807. dev_err(&pdev->dev,
  4808. "Cannot re-enable PCI device after reset.\n");
  4809. result = PCI_ERS_RESULT_DISCONNECT;
  4810. } else {
  4811. pci_set_master(pdev);
  4812. pci_restore_state(pdev);
  4813. pci_enable_wake(pdev, PCI_D3hot, 0);
  4814. pci_enable_wake(pdev, PCI_D3cold, 0);
  4815. igb_reset(adapter);
  4816. wr32(E1000_WUS, ~0);
  4817. result = PCI_ERS_RESULT_RECOVERED;
  4818. }
  4819. err = pci_cleanup_aer_uncorrect_error_status(pdev);
  4820. if (err) {
  4821. dev_err(&pdev->dev, "pci_cleanup_aer_uncorrect_error_status "
  4822. "failed 0x%0x\n", err);
  4823. /* non-fatal, continue */
  4824. }
  4825. return result;
  4826. }
  4827. /**
  4828. * igb_io_resume - called when traffic can start flowing again.
  4829. * @pdev: Pointer to PCI device
  4830. *
  4831. * This callback is called when the error recovery driver tells us that
  4832. * its OK to resume normal operation. Implementation resembles the
  4833. * second-half of the igb_resume routine.
  4834. */
  4835. static void igb_io_resume(struct pci_dev *pdev)
  4836. {
  4837. struct net_device *netdev = pci_get_drvdata(pdev);
  4838. struct igb_adapter *adapter = netdev_priv(netdev);
  4839. if (netif_running(netdev)) {
  4840. if (igb_up(adapter)) {
  4841. dev_err(&pdev->dev, "igb_up failed after reset\n");
  4842. return;
  4843. }
  4844. }
  4845. netif_device_attach(netdev);
  4846. /* let the f/w know that the h/w is now under the control of the
  4847. * driver. */
  4848. igb_get_hw_control(adapter);
  4849. }
  4850. static void igb_rar_set_qsel(struct igb_adapter *adapter, u8 *addr, u32 index,
  4851. u8 qsel)
  4852. {
  4853. u32 rar_low, rar_high;
  4854. struct e1000_hw *hw = &adapter->hw;
  4855. /* HW expects these in little endian so we reverse the byte order
  4856. * from network order (big endian) to little endian
  4857. */
  4858. rar_low = ((u32) addr[0] | ((u32) addr[1] << 8) |
  4859. ((u32) addr[2] << 16) | ((u32) addr[3] << 24));
  4860. rar_high = ((u32) addr[4] | ((u32) addr[5] << 8));
  4861. /* Indicate to hardware the Address is Valid. */
  4862. rar_high |= E1000_RAH_AV;
  4863. if (hw->mac.type == e1000_82575)
  4864. rar_high |= E1000_RAH_POOL_1 * qsel;
  4865. else
  4866. rar_high |= E1000_RAH_POOL_1 << qsel;
  4867. wr32(E1000_RAL(index), rar_low);
  4868. wrfl();
  4869. wr32(E1000_RAH(index), rar_high);
  4870. wrfl();
  4871. }
  4872. static int igb_set_vf_mac(struct igb_adapter *adapter,
  4873. int vf, unsigned char *mac_addr)
  4874. {
  4875. struct e1000_hw *hw = &adapter->hw;
  4876. /* VF MAC addresses start at end of receive addresses and moves
  4877. * torwards the first, as a result a collision should not be possible */
  4878. int rar_entry = hw->mac.rar_entry_count - (vf + 1);
  4879. memcpy(adapter->vf_data[vf].vf_mac_addresses, mac_addr, ETH_ALEN);
  4880. igb_rar_set_qsel(adapter, mac_addr, rar_entry, vf);
  4881. return 0;
  4882. }
  4883. static void igb_vmm_control(struct igb_adapter *adapter)
  4884. {
  4885. struct e1000_hw *hw = &adapter->hw;
  4886. u32 reg;
  4887. /* replication is not supported for 82575 */
  4888. if (hw->mac.type == e1000_82575)
  4889. return;
  4890. /* enable replication vlan tag stripping */
  4891. reg = rd32(E1000_RPLOLR);
  4892. reg |= E1000_RPLOLR_STRVLAN;
  4893. wr32(E1000_RPLOLR, reg);
  4894. /* notify HW that the MAC is adding vlan tags */
  4895. reg = rd32(E1000_DTXCTL);
  4896. reg |= E1000_DTXCTL_VLAN_ADDED;
  4897. wr32(E1000_DTXCTL, reg);
  4898. if (adapter->vfs_allocated_count) {
  4899. igb_vmdq_set_loopback_pf(hw, true);
  4900. igb_vmdq_set_replication_pf(hw, true);
  4901. } else {
  4902. igb_vmdq_set_loopback_pf(hw, false);
  4903. igb_vmdq_set_replication_pf(hw, false);
  4904. }
  4905. }
  4906. /* igb_main.c */