igb_main.c 150 KB

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