igb_main.c 149 KB

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