e1000_main.c 145 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright(c) 1999 - 2006 Intel Corporation. */
  3. #include "e1000.h"
  4. #include <net/ip6_checksum.h>
  5. #include <linux/io.h>
  6. #include <linux/prefetch.h>
  7. #include <linux/bitops.h>
  8. #include <linux/if_vlan.h>
  9. char e1000_driver_name[] = "e1000";
  10. static char e1000_driver_string[] = "Intel(R) PRO/1000 Network Driver";
  11. #define DRV_VERSION "7.3.21-k8-NAPI"
  12. const char e1000_driver_version[] = DRV_VERSION;
  13. static const char e1000_copyright[] = "Copyright (c) 1999-2006 Intel Corporation.";
  14. /* e1000_pci_tbl - PCI Device ID Table
  15. *
  16. * Last entry must be all 0s
  17. *
  18. * Macro expands to...
  19. * {PCI_DEVICE(PCI_VENDOR_ID_INTEL, device_id)}
  20. */
  21. static const struct pci_device_id e1000_pci_tbl[] = {
  22. INTEL_E1000_ETHERNET_DEVICE(0x1000),
  23. INTEL_E1000_ETHERNET_DEVICE(0x1001),
  24. INTEL_E1000_ETHERNET_DEVICE(0x1004),
  25. INTEL_E1000_ETHERNET_DEVICE(0x1008),
  26. INTEL_E1000_ETHERNET_DEVICE(0x1009),
  27. INTEL_E1000_ETHERNET_DEVICE(0x100C),
  28. INTEL_E1000_ETHERNET_DEVICE(0x100D),
  29. INTEL_E1000_ETHERNET_DEVICE(0x100E),
  30. INTEL_E1000_ETHERNET_DEVICE(0x100F),
  31. INTEL_E1000_ETHERNET_DEVICE(0x1010),
  32. INTEL_E1000_ETHERNET_DEVICE(0x1011),
  33. INTEL_E1000_ETHERNET_DEVICE(0x1012),
  34. INTEL_E1000_ETHERNET_DEVICE(0x1013),
  35. INTEL_E1000_ETHERNET_DEVICE(0x1014),
  36. INTEL_E1000_ETHERNET_DEVICE(0x1015),
  37. INTEL_E1000_ETHERNET_DEVICE(0x1016),
  38. INTEL_E1000_ETHERNET_DEVICE(0x1017),
  39. INTEL_E1000_ETHERNET_DEVICE(0x1018),
  40. INTEL_E1000_ETHERNET_DEVICE(0x1019),
  41. INTEL_E1000_ETHERNET_DEVICE(0x101A),
  42. INTEL_E1000_ETHERNET_DEVICE(0x101D),
  43. INTEL_E1000_ETHERNET_DEVICE(0x101E),
  44. INTEL_E1000_ETHERNET_DEVICE(0x1026),
  45. INTEL_E1000_ETHERNET_DEVICE(0x1027),
  46. INTEL_E1000_ETHERNET_DEVICE(0x1028),
  47. INTEL_E1000_ETHERNET_DEVICE(0x1075),
  48. INTEL_E1000_ETHERNET_DEVICE(0x1076),
  49. INTEL_E1000_ETHERNET_DEVICE(0x1077),
  50. INTEL_E1000_ETHERNET_DEVICE(0x1078),
  51. INTEL_E1000_ETHERNET_DEVICE(0x1079),
  52. INTEL_E1000_ETHERNET_DEVICE(0x107A),
  53. INTEL_E1000_ETHERNET_DEVICE(0x107B),
  54. INTEL_E1000_ETHERNET_DEVICE(0x107C),
  55. INTEL_E1000_ETHERNET_DEVICE(0x108A),
  56. INTEL_E1000_ETHERNET_DEVICE(0x1099),
  57. INTEL_E1000_ETHERNET_DEVICE(0x10B5),
  58. INTEL_E1000_ETHERNET_DEVICE(0x2E6E),
  59. /* required last entry */
  60. {0,}
  61. };
  62. MODULE_DEVICE_TABLE(pci, e1000_pci_tbl);
  63. int e1000_up(struct e1000_adapter *adapter);
  64. void e1000_down(struct e1000_adapter *adapter);
  65. void e1000_reinit_locked(struct e1000_adapter *adapter);
  66. void e1000_reset(struct e1000_adapter *adapter);
  67. int e1000_setup_all_tx_resources(struct e1000_adapter *adapter);
  68. int e1000_setup_all_rx_resources(struct e1000_adapter *adapter);
  69. void e1000_free_all_tx_resources(struct e1000_adapter *adapter);
  70. void e1000_free_all_rx_resources(struct e1000_adapter *adapter);
  71. static int e1000_setup_tx_resources(struct e1000_adapter *adapter,
  72. struct e1000_tx_ring *txdr);
  73. static int e1000_setup_rx_resources(struct e1000_adapter *adapter,
  74. struct e1000_rx_ring *rxdr);
  75. static void e1000_free_tx_resources(struct e1000_adapter *adapter,
  76. struct e1000_tx_ring *tx_ring);
  77. static void e1000_free_rx_resources(struct e1000_adapter *adapter,
  78. struct e1000_rx_ring *rx_ring);
  79. void e1000_update_stats(struct e1000_adapter *adapter);
  80. static int e1000_init_module(void);
  81. static void e1000_exit_module(void);
  82. static int e1000_probe(struct pci_dev *pdev, const struct pci_device_id *ent);
  83. static void e1000_remove(struct pci_dev *pdev);
  84. static int e1000_alloc_queues(struct e1000_adapter *adapter);
  85. static int e1000_sw_init(struct e1000_adapter *adapter);
  86. int e1000_open(struct net_device *netdev);
  87. int e1000_close(struct net_device *netdev);
  88. static void e1000_configure_tx(struct e1000_adapter *adapter);
  89. static void e1000_configure_rx(struct e1000_adapter *adapter);
  90. static void e1000_setup_rctl(struct e1000_adapter *adapter);
  91. static void e1000_clean_all_tx_rings(struct e1000_adapter *adapter);
  92. static void e1000_clean_all_rx_rings(struct e1000_adapter *adapter);
  93. static void e1000_clean_tx_ring(struct e1000_adapter *adapter,
  94. struct e1000_tx_ring *tx_ring);
  95. static void e1000_clean_rx_ring(struct e1000_adapter *adapter,
  96. struct e1000_rx_ring *rx_ring);
  97. static void e1000_set_rx_mode(struct net_device *netdev);
  98. static void e1000_update_phy_info_task(struct work_struct *work);
  99. static void e1000_watchdog(struct work_struct *work);
  100. static void e1000_82547_tx_fifo_stall_task(struct work_struct *work);
  101. static netdev_tx_t e1000_xmit_frame(struct sk_buff *skb,
  102. struct net_device *netdev);
  103. static int e1000_change_mtu(struct net_device *netdev, int new_mtu);
  104. static int e1000_set_mac(struct net_device *netdev, void *p);
  105. static irqreturn_t e1000_intr(int irq, void *data);
  106. static bool e1000_clean_tx_irq(struct e1000_adapter *adapter,
  107. struct e1000_tx_ring *tx_ring);
  108. static int e1000_clean(struct napi_struct *napi, int budget);
  109. static bool e1000_clean_rx_irq(struct e1000_adapter *adapter,
  110. struct e1000_rx_ring *rx_ring,
  111. int *work_done, int work_to_do);
  112. static bool e1000_clean_jumbo_rx_irq(struct e1000_adapter *adapter,
  113. struct e1000_rx_ring *rx_ring,
  114. int *work_done, int work_to_do);
  115. static void e1000_alloc_dummy_rx_buffers(struct e1000_adapter *adapter,
  116. struct e1000_rx_ring *rx_ring,
  117. int cleaned_count)
  118. {
  119. }
  120. static void e1000_alloc_rx_buffers(struct e1000_adapter *adapter,
  121. struct e1000_rx_ring *rx_ring,
  122. int cleaned_count);
  123. static void e1000_alloc_jumbo_rx_buffers(struct e1000_adapter *adapter,
  124. struct e1000_rx_ring *rx_ring,
  125. int cleaned_count);
  126. static int e1000_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd);
  127. static int e1000_mii_ioctl(struct net_device *netdev, struct ifreq *ifr,
  128. int cmd);
  129. static void e1000_enter_82542_rst(struct e1000_adapter *adapter);
  130. static void e1000_leave_82542_rst(struct e1000_adapter *adapter);
  131. static void e1000_tx_timeout(struct net_device *dev);
  132. static void e1000_reset_task(struct work_struct *work);
  133. static void e1000_smartspeed(struct e1000_adapter *adapter);
  134. static int e1000_82547_fifo_workaround(struct e1000_adapter *adapter,
  135. struct sk_buff *skb);
  136. static bool e1000_vlan_used(struct e1000_adapter *adapter);
  137. static void e1000_vlan_mode(struct net_device *netdev,
  138. netdev_features_t features);
  139. static void e1000_vlan_filter_on_off(struct e1000_adapter *adapter,
  140. bool filter_on);
  141. static int e1000_vlan_rx_add_vid(struct net_device *netdev,
  142. __be16 proto, u16 vid);
  143. static int e1000_vlan_rx_kill_vid(struct net_device *netdev,
  144. __be16 proto, u16 vid);
  145. static void e1000_restore_vlan(struct e1000_adapter *adapter);
  146. #ifdef CONFIG_PM
  147. static int e1000_suspend(struct pci_dev *pdev, pm_message_t state);
  148. static int e1000_resume(struct pci_dev *pdev);
  149. #endif
  150. static void e1000_shutdown(struct pci_dev *pdev);
  151. #ifdef CONFIG_NET_POLL_CONTROLLER
  152. /* for netdump / net console */
  153. static void e1000_netpoll (struct net_device *netdev);
  154. #endif
  155. #define COPYBREAK_DEFAULT 256
  156. static unsigned int copybreak __read_mostly = COPYBREAK_DEFAULT;
  157. module_param(copybreak, uint, 0644);
  158. MODULE_PARM_DESC(copybreak,
  159. "Maximum size of packet that is copied to a new buffer on receive");
  160. static pci_ers_result_t e1000_io_error_detected(struct pci_dev *pdev,
  161. pci_channel_state_t state);
  162. static pci_ers_result_t e1000_io_slot_reset(struct pci_dev *pdev);
  163. static void e1000_io_resume(struct pci_dev *pdev);
  164. static const struct pci_error_handlers e1000_err_handler = {
  165. .error_detected = e1000_io_error_detected,
  166. .slot_reset = e1000_io_slot_reset,
  167. .resume = e1000_io_resume,
  168. };
  169. static struct pci_driver e1000_driver = {
  170. .name = e1000_driver_name,
  171. .id_table = e1000_pci_tbl,
  172. .probe = e1000_probe,
  173. .remove = e1000_remove,
  174. #ifdef CONFIG_PM
  175. /* Power Management Hooks */
  176. .suspend = e1000_suspend,
  177. .resume = e1000_resume,
  178. #endif
  179. .shutdown = e1000_shutdown,
  180. .err_handler = &e1000_err_handler
  181. };
  182. MODULE_AUTHOR("Intel Corporation, <linux.nics@intel.com>");
  183. MODULE_DESCRIPTION("Intel(R) PRO/1000 Network Driver");
  184. MODULE_LICENSE("GPL");
  185. MODULE_VERSION(DRV_VERSION);
  186. #define DEFAULT_MSG_ENABLE (NETIF_MSG_DRV|NETIF_MSG_PROBE|NETIF_MSG_LINK)
  187. static int debug = -1;
  188. module_param(debug, int, 0);
  189. MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all)");
  190. /**
  191. * e1000_get_hw_dev - return device
  192. * used by hardware layer to print debugging information
  193. *
  194. **/
  195. struct net_device *e1000_get_hw_dev(struct e1000_hw *hw)
  196. {
  197. struct e1000_adapter *adapter = hw->back;
  198. return adapter->netdev;
  199. }
  200. /**
  201. * e1000_init_module - Driver Registration Routine
  202. *
  203. * e1000_init_module is the first routine called when the driver is
  204. * loaded. All it does is register with the PCI subsystem.
  205. **/
  206. static int __init e1000_init_module(void)
  207. {
  208. int ret;
  209. pr_info("%s - version %s\n", e1000_driver_string, e1000_driver_version);
  210. pr_info("%s\n", e1000_copyright);
  211. ret = pci_register_driver(&e1000_driver);
  212. if (copybreak != COPYBREAK_DEFAULT) {
  213. if (copybreak == 0)
  214. pr_info("copybreak disabled\n");
  215. else
  216. pr_info("copybreak enabled for "
  217. "packets <= %u bytes\n", copybreak);
  218. }
  219. return ret;
  220. }
  221. module_init(e1000_init_module);
  222. /**
  223. * e1000_exit_module - Driver Exit Cleanup Routine
  224. *
  225. * e1000_exit_module is called just before the driver is removed
  226. * from memory.
  227. **/
  228. static void __exit e1000_exit_module(void)
  229. {
  230. pci_unregister_driver(&e1000_driver);
  231. }
  232. module_exit(e1000_exit_module);
  233. static int e1000_request_irq(struct e1000_adapter *adapter)
  234. {
  235. struct net_device *netdev = adapter->netdev;
  236. irq_handler_t handler = e1000_intr;
  237. int irq_flags = IRQF_SHARED;
  238. int err;
  239. err = request_irq(adapter->pdev->irq, handler, irq_flags, netdev->name,
  240. netdev);
  241. if (err) {
  242. e_err(probe, "Unable to allocate interrupt Error: %d\n", err);
  243. }
  244. return err;
  245. }
  246. static void e1000_free_irq(struct e1000_adapter *adapter)
  247. {
  248. struct net_device *netdev = adapter->netdev;
  249. free_irq(adapter->pdev->irq, netdev);
  250. }
  251. /**
  252. * e1000_irq_disable - Mask off interrupt generation on the NIC
  253. * @adapter: board private structure
  254. **/
  255. static void e1000_irq_disable(struct e1000_adapter *adapter)
  256. {
  257. struct e1000_hw *hw = &adapter->hw;
  258. ew32(IMC, ~0);
  259. E1000_WRITE_FLUSH();
  260. synchronize_irq(adapter->pdev->irq);
  261. }
  262. /**
  263. * e1000_irq_enable - Enable default interrupt generation settings
  264. * @adapter: board private structure
  265. **/
  266. static void e1000_irq_enable(struct e1000_adapter *adapter)
  267. {
  268. struct e1000_hw *hw = &adapter->hw;
  269. ew32(IMS, IMS_ENABLE_MASK);
  270. E1000_WRITE_FLUSH();
  271. }
  272. static void e1000_update_mng_vlan(struct e1000_adapter *adapter)
  273. {
  274. struct e1000_hw *hw = &adapter->hw;
  275. struct net_device *netdev = adapter->netdev;
  276. u16 vid = hw->mng_cookie.vlan_id;
  277. u16 old_vid = adapter->mng_vlan_id;
  278. if (!e1000_vlan_used(adapter))
  279. return;
  280. if (!test_bit(vid, adapter->active_vlans)) {
  281. if (hw->mng_cookie.status &
  282. E1000_MNG_DHCP_COOKIE_STATUS_VLAN_SUPPORT) {
  283. e1000_vlan_rx_add_vid(netdev, htons(ETH_P_8021Q), vid);
  284. adapter->mng_vlan_id = vid;
  285. } else {
  286. adapter->mng_vlan_id = E1000_MNG_VLAN_NONE;
  287. }
  288. if ((old_vid != (u16)E1000_MNG_VLAN_NONE) &&
  289. (vid != old_vid) &&
  290. !test_bit(old_vid, adapter->active_vlans))
  291. e1000_vlan_rx_kill_vid(netdev, htons(ETH_P_8021Q),
  292. old_vid);
  293. } else {
  294. adapter->mng_vlan_id = vid;
  295. }
  296. }
  297. static void e1000_init_manageability(struct e1000_adapter *adapter)
  298. {
  299. struct e1000_hw *hw = &adapter->hw;
  300. if (adapter->en_mng_pt) {
  301. u32 manc = er32(MANC);
  302. /* disable hardware interception of ARP */
  303. manc &= ~(E1000_MANC_ARP_EN);
  304. ew32(MANC, manc);
  305. }
  306. }
  307. static void e1000_release_manageability(struct e1000_adapter *adapter)
  308. {
  309. struct e1000_hw *hw = &adapter->hw;
  310. if (adapter->en_mng_pt) {
  311. u32 manc = er32(MANC);
  312. /* re-enable hardware interception of ARP */
  313. manc |= E1000_MANC_ARP_EN;
  314. ew32(MANC, manc);
  315. }
  316. }
  317. /**
  318. * e1000_configure - configure the hardware for RX and TX
  319. * @adapter = private board structure
  320. **/
  321. static void e1000_configure(struct e1000_adapter *adapter)
  322. {
  323. struct net_device *netdev = adapter->netdev;
  324. int i;
  325. e1000_set_rx_mode(netdev);
  326. e1000_restore_vlan(adapter);
  327. e1000_init_manageability(adapter);
  328. e1000_configure_tx(adapter);
  329. e1000_setup_rctl(adapter);
  330. e1000_configure_rx(adapter);
  331. /* call E1000_DESC_UNUSED which always leaves
  332. * at least 1 descriptor unused to make sure
  333. * next_to_use != next_to_clean
  334. */
  335. for (i = 0; i < adapter->num_rx_queues; i++) {
  336. struct e1000_rx_ring *ring = &adapter->rx_ring[i];
  337. adapter->alloc_rx_buf(adapter, ring,
  338. E1000_DESC_UNUSED(ring));
  339. }
  340. }
  341. int e1000_up(struct e1000_adapter *adapter)
  342. {
  343. struct e1000_hw *hw = &adapter->hw;
  344. /* hardware has been reset, we need to reload some things */
  345. e1000_configure(adapter);
  346. clear_bit(__E1000_DOWN, &adapter->flags);
  347. napi_enable(&adapter->napi);
  348. e1000_irq_enable(adapter);
  349. netif_wake_queue(adapter->netdev);
  350. /* fire a link change interrupt to start the watchdog */
  351. ew32(ICS, E1000_ICS_LSC);
  352. return 0;
  353. }
  354. /**
  355. * e1000_power_up_phy - restore link in case the phy was powered down
  356. * @adapter: address of board private structure
  357. *
  358. * The phy may be powered down to save power and turn off link when the
  359. * driver is unloaded and wake on lan is not enabled (among others)
  360. * *** this routine MUST be followed by a call to e1000_reset ***
  361. **/
  362. void e1000_power_up_phy(struct e1000_adapter *adapter)
  363. {
  364. struct e1000_hw *hw = &adapter->hw;
  365. u16 mii_reg = 0;
  366. /* Just clear the power down bit to wake the phy back up */
  367. if (hw->media_type == e1000_media_type_copper) {
  368. /* according to the manual, the phy will retain its
  369. * settings across a power-down/up cycle
  370. */
  371. e1000_read_phy_reg(hw, PHY_CTRL, &mii_reg);
  372. mii_reg &= ~MII_CR_POWER_DOWN;
  373. e1000_write_phy_reg(hw, PHY_CTRL, mii_reg);
  374. }
  375. }
  376. static void e1000_power_down_phy(struct e1000_adapter *adapter)
  377. {
  378. struct e1000_hw *hw = &adapter->hw;
  379. /* Power down the PHY so no link is implied when interface is down *
  380. * The PHY cannot be powered down if any of the following is true *
  381. * (a) WoL is enabled
  382. * (b) AMT is active
  383. * (c) SoL/IDER session is active
  384. */
  385. if (!adapter->wol && hw->mac_type >= e1000_82540 &&
  386. hw->media_type == e1000_media_type_copper) {
  387. u16 mii_reg = 0;
  388. switch (hw->mac_type) {
  389. case e1000_82540:
  390. case e1000_82545:
  391. case e1000_82545_rev_3:
  392. case e1000_82546:
  393. case e1000_ce4100:
  394. case e1000_82546_rev_3:
  395. case e1000_82541:
  396. case e1000_82541_rev_2:
  397. case e1000_82547:
  398. case e1000_82547_rev_2:
  399. if (er32(MANC) & E1000_MANC_SMBUS_EN)
  400. goto out;
  401. break;
  402. default:
  403. goto out;
  404. }
  405. e1000_read_phy_reg(hw, PHY_CTRL, &mii_reg);
  406. mii_reg |= MII_CR_POWER_DOWN;
  407. e1000_write_phy_reg(hw, PHY_CTRL, mii_reg);
  408. msleep(1);
  409. }
  410. out:
  411. return;
  412. }
  413. static void e1000_down_and_stop(struct e1000_adapter *adapter)
  414. {
  415. set_bit(__E1000_DOWN, &adapter->flags);
  416. cancel_delayed_work_sync(&adapter->watchdog_task);
  417. /*
  418. * Since the watchdog task can reschedule other tasks, we should cancel
  419. * it first, otherwise we can run into the situation when a work is
  420. * still running after the adapter has been turned down.
  421. */
  422. cancel_delayed_work_sync(&adapter->phy_info_task);
  423. cancel_delayed_work_sync(&adapter->fifo_stall_task);
  424. /* Only kill reset task if adapter is not resetting */
  425. if (!test_bit(__E1000_RESETTING, &adapter->flags))
  426. cancel_work_sync(&adapter->reset_task);
  427. }
  428. void e1000_down(struct e1000_adapter *adapter)
  429. {
  430. struct e1000_hw *hw = &adapter->hw;
  431. struct net_device *netdev = adapter->netdev;
  432. u32 rctl, tctl;
  433. /* disable receives in the hardware */
  434. rctl = er32(RCTL);
  435. ew32(RCTL, rctl & ~E1000_RCTL_EN);
  436. /* flush and sleep below */
  437. netif_tx_disable(netdev);
  438. /* disable transmits in the hardware */
  439. tctl = er32(TCTL);
  440. tctl &= ~E1000_TCTL_EN;
  441. ew32(TCTL, tctl);
  442. /* flush both disables and wait for them to finish */
  443. E1000_WRITE_FLUSH();
  444. msleep(10);
  445. /* Set the carrier off after transmits have been disabled in the
  446. * hardware, to avoid race conditions with e1000_watchdog() (which
  447. * may be running concurrently to us, checking for the carrier
  448. * bit to decide whether it should enable transmits again). Such
  449. * a race condition would result into transmission being disabled
  450. * in the hardware until the next IFF_DOWN+IFF_UP cycle.
  451. */
  452. netif_carrier_off(netdev);
  453. napi_disable(&adapter->napi);
  454. e1000_irq_disable(adapter);
  455. /* Setting DOWN must be after irq_disable to prevent
  456. * a screaming interrupt. Setting DOWN also prevents
  457. * tasks from rescheduling.
  458. */
  459. e1000_down_and_stop(adapter);
  460. adapter->link_speed = 0;
  461. adapter->link_duplex = 0;
  462. e1000_reset(adapter);
  463. e1000_clean_all_tx_rings(adapter);
  464. e1000_clean_all_rx_rings(adapter);
  465. }
  466. void e1000_reinit_locked(struct e1000_adapter *adapter)
  467. {
  468. WARN_ON(in_interrupt());
  469. while (test_and_set_bit(__E1000_RESETTING, &adapter->flags))
  470. msleep(1);
  471. e1000_down(adapter);
  472. e1000_up(adapter);
  473. clear_bit(__E1000_RESETTING, &adapter->flags);
  474. }
  475. void e1000_reset(struct e1000_adapter *adapter)
  476. {
  477. struct e1000_hw *hw = &adapter->hw;
  478. u32 pba = 0, tx_space, min_tx_space, min_rx_space;
  479. bool legacy_pba_adjust = false;
  480. u16 hwm;
  481. /* Repartition Pba for greater than 9k mtu
  482. * To take effect CTRL.RST is required.
  483. */
  484. switch (hw->mac_type) {
  485. case e1000_82542_rev2_0:
  486. case e1000_82542_rev2_1:
  487. case e1000_82543:
  488. case e1000_82544:
  489. case e1000_82540:
  490. case e1000_82541:
  491. case e1000_82541_rev_2:
  492. legacy_pba_adjust = true;
  493. pba = E1000_PBA_48K;
  494. break;
  495. case e1000_82545:
  496. case e1000_82545_rev_3:
  497. case e1000_82546:
  498. case e1000_ce4100:
  499. case e1000_82546_rev_3:
  500. pba = E1000_PBA_48K;
  501. break;
  502. case e1000_82547:
  503. case e1000_82547_rev_2:
  504. legacy_pba_adjust = true;
  505. pba = E1000_PBA_30K;
  506. break;
  507. case e1000_undefined:
  508. case e1000_num_macs:
  509. break;
  510. }
  511. if (legacy_pba_adjust) {
  512. if (hw->max_frame_size > E1000_RXBUFFER_8192)
  513. pba -= 8; /* allocate more FIFO for Tx */
  514. if (hw->mac_type == e1000_82547) {
  515. adapter->tx_fifo_head = 0;
  516. adapter->tx_head_addr = pba << E1000_TX_HEAD_ADDR_SHIFT;
  517. adapter->tx_fifo_size =
  518. (E1000_PBA_40K - pba) << E1000_PBA_BYTES_SHIFT;
  519. atomic_set(&adapter->tx_fifo_stall, 0);
  520. }
  521. } else if (hw->max_frame_size > ETH_FRAME_LEN + ETH_FCS_LEN) {
  522. /* adjust PBA for jumbo frames */
  523. ew32(PBA, pba);
  524. /* To maintain wire speed transmits, the Tx FIFO should be
  525. * large enough to accommodate two full transmit packets,
  526. * rounded up to the next 1KB and expressed in KB. Likewise,
  527. * the Rx FIFO should be large enough to accommodate at least
  528. * one full receive packet and is similarly rounded up and
  529. * expressed in KB.
  530. */
  531. pba = er32(PBA);
  532. /* upper 16 bits has Tx packet buffer allocation size in KB */
  533. tx_space = pba >> 16;
  534. /* lower 16 bits has Rx packet buffer allocation size in KB */
  535. pba &= 0xffff;
  536. /* the Tx fifo also stores 16 bytes of information about the Tx
  537. * but don't include ethernet FCS because hardware appends it
  538. */
  539. min_tx_space = (hw->max_frame_size +
  540. sizeof(struct e1000_tx_desc) -
  541. ETH_FCS_LEN) * 2;
  542. min_tx_space = ALIGN(min_tx_space, 1024);
  543. min_tx_space >>= 10;
  544. /* software strips receive CRC, so leave room for it */
  545. min_rx_space = hw->max_frame_size;
  546. min_rx_space = ALIGN(min_rx_space, 1024);
  547. min_rx_space >>= 10;
  548. /* If current Tx allocation is less than the min Tx FIFO size,
  549. * and the min Tx FIFO size is less than the current Rx FIFO
  550. * allocation, take space away from current Rx allocation
  551. */
  552. if (tx_space < min_tx_space &&
  553. ((min_tx_space - tx_space) < pba)) {
  554. pba = pba - (min_tx_space - tx_space);
  555. /* PCI/PCIx hardware has PBA alignment constraints */
  556. switch (hw->mac_type) {
  557. case e1000_82545 ... e1000_82546_rev_3:
  558. pba &= ~(E1000_PBA_8K - 1);
  559. break;
  560. default:
  561. break;
  562. }
  563. /* if short on Rx space, Rx wins and must trump Tx
  564. * adjustment or use Early Receive if available
  565. */
  566. if (pba < min_rx_space)
  567. pba = min_rx_space;
  568. }
  569. }
  570. ew32(PBA, pba);
  571. /* flow control settings:
  572. * The high water mark must be low enough to fit one full frame
  573. * (or the size used for early receive) above it in the Rx FIFO.
  574. * Set it to the lower of:
  575. * - 90% of the Rx FIFO size, and
  576. * - the full Rx FIFO size minus the early receive size (for parts
  577. * with ERT support assuming ERT set to E1000_ERT_2048), or
  578. * - the full Rx FIFO size minus one full frame
  579. */
  580. hwm = min(((pba << 10) * 9 / 10),
  581. ((pba << 10) - hw->max_frame_size));
  582. hw->fc_high_water = hwm & 0xFFF8; /* 8-byte granularity */
  583. hw->fc_low_water = hw->fc_high_water - 8;
  584. hw->fc_pause_time = E1000_FC_PAUSE_TIME;
  585. hw->fc_send_xon = 1;
  586. hw->fc = hw->original_fc;
  587. /* Allow time for pending master requests to run */
  588. e1000_reset_hw(hw);
  589. if (hw->mac_type >= e1000_82544)
  590. ew32(WUC, 0);
  591. if (e1000_init_hw(hw))
  592. e_dev_err("Hardware Error\n");
  593. e1000_update_mng_vlan(adapter);
  594. /* if (adapter->hwflags & HWFLAGS_PHY_PWR_BIT) { */
  595. if (hw->mac_type >= e1000_82544 &&
  596. hw->autoneg == 1 &&
  597. hw->autoneg_advertised == ADVERTISE_1000_FULL) {
  598. u32 ctrl = er32(CTRL);
  599. /* clear phy power management bit if we are in gig only mode,
  600. * which if enabled will attempt negotiation to 100Mb, which
  601. * can cause a loss of link at power off or driver unload
  602. */
  603. ctrl &= ~E1000_CTRL_SWDPIN3;
  604. ew32(CTRL, ctrl);
  605. }
  606. /* Enable h/w to recognize an 802.1Q VLAN Ethernet packet */
  607. ew32(VET, ETHERNET_IEEE_VLAN_TYPE);
  608. e1000_reset_adaptive(hw);
  609. e1000_phy_get_info(hw, &adapter->phy_info);
  610. e1000_release_manageability(adapter);
  611. }
  612. /* Dump the eeprom for users having checksum issues */
  613. static void e1000_dump_eeprom(struct e1000_adapter *adapter)
  614. {
  615. struct net_device *netdev = adapter->netdev;
  616. struct ethtool_eeprom eeprom;
  617. const struct ethtool_ops *ops = netdev->ethtool_ops;
  618. u8 *data;
  619. int i;
  620. u16 csum_old, csum_new = 0;
  621. eeprom.len = ops->get_eeprom_len(netdev);
  622. eeprom.offset = 0;
  623. data = kmalloc(eeprom.len, GFP_KERNEL);
  624. if (!data)
  625. return;
  626. ops->get_eeprom(netdev, &eeprom, data);
  627. csum_old = (data[EEPROM_CHECKSUM_REG * 2]) +
  628. (data[EEPROM_CHECKSUM_REG * 2 + 1] << 8);
  629. for (i = 0; i < EEPROM_CHECKSUM_REG * 2; i += 2)
  630. csum_new += data[i] + (data[i + 1] << 8);
  631. csum_new = EEPROM_SUM - csum_new;
  632. pr_err("/*********************/\n");
  633. pr_err("Current EEPROM Checksum : 0x%04x\n", csum_old);
  634. pr_err("Calculated : 0x%04x\n", csum_new);
  635. pr_err("Offset Values\n");
  636. pr_err("======== ======\n");
  637. print_hex_dump(KERN_ERR, "", DUMP_PREFIX_OFFSET, 16, 1, data, 128, 0);
  638. pr_err("Include this output when contacting your support provider.\n");
  639. pr_err("This is not a software error! Something bad happened to\n");
  640. pr_err("your hardware or EEPROM image. Ignoring this problem could\n");
  641. pr_err("result in further problems, possibly loss of data,\n");
  642. pr_err("corruption or system hangs!\n");
  643. pr_err("The MAC Address will be reset to 00:00:00:00:00:00,\n");
  644. pr_err("which is invalid and requires you to set the proper MAC\n");
  645. pr_err("address manually before continuing to enable this network\n");
  646. pr_err("device. Please inspect the EEPROM dump and report the\n");
  647. pr_err("issue to your hardware vendor or Intel Customer Support.\n");
  648. pr_err("/*********************/\n");
  649. kfree(data);
  650. }
  651. /**
  652. * e1000_is_need_ioport - determine if an adapter needs ioport resources or not
  653. * @pdev: PCI device information struct
  654. *
  655. * Return true if an adapter needs ioport resources
  656. **/
  657. static int e1000_is_need_ioport(struct pci_dev *pdev)
  658. {
  659. switch (pdev->device) {
  660. case E1000_DEV_ID_82540EM:
  661. case E1000_DEV_ID_82540EM_LOM:
  662. case E1000_DEV_ID_82540EP:
  663. case E1000_DEV_ID_82540EP_LOM:
  664. case E1000_DEV_ID_82540EP_LP:
  665. case E1000_DEV_ID_82541EI:
  666. case E1000_DEV_ID_82541EI_MOBILE:
  667. case E1000_DEV_ID_82541ER:
  668. case E1000_DEV_ID_82541ER_LOM:
  669. case E1000_DEV_ID_82541GI:
  670. case E1000_DEV_ID_82541GI_LF:
  671. case E1000_DEV_ID_82541GI_MOBILE:
  672. case E1000_DEV_ID_82544EI_COPPER:
  673. case E1000_DEV_ID_82544EI_FIBER:
  674. case E1000_DEV_ID_82544GC_COPPER:
  675. case E1000_DEV_ID_82544GC_LOM:
  676. case E1000_DEV_ID_82545EM_COPPER:
  677. case E1000_DEV_ID_82545EM_FIBER:
  678. case E1000_DEV_ID_82546EB_COPPER:
  679. case E1000_DEV_ID_82546EB_FIBER:
  680. case E1000_DEV_ID_82546EB_QUAD_COPPER:
  681. return true;
  682. default:
  683. return false;
  684. }
  685. }
  686. static netdev_features_t e1000_fix_features(struct net_device *netdev,
  687. netdev_features_t features)
  688. {
  689. /* Since there is no support for separate Rx/Tx vlan accel
  690. * enable/disable make sure Tx flag is always in same state as Rx.
  691. */
  692. if (features & NETIF_F_HW_VLAN_CTAG_RX)
  693. features |= NETIF_F_HW_VLAN_CTAG_TX;
  694. else
  695. features &= ~NETIF_F_HW_VLAN_CTAG_TX;
  696. return features;
  697. }
  698. static int e1000_set_features(struct net_device *netdev,
  699. netdev_features_t features)
  700. {
  701. struct e1000_adapter *adapter = netdev_priv(netdev);
  702. netdev_features_t changed = features ^ netdev->features;
  703. if (changed & NETIF_F_HW_VLAN_CTAG_RX)
  704. e1000_vlan_mode(netdev, features);
  705. if (!(changed & (NETIF_F_RXCSUM | NETIF_F_RXALL)))
  706. return 0;
  707. netdev->features = features;
  708. adapter->rx_csum = !!(features & NETIF_F_RXCSUM);
  709. if (netif_running(netdev))
  710. e1000_reinit_locked(adapter);
  711. else
  712. e1000_reset(adapter);
  713. return 0;
  714. }
  715. static const struct net_device_ops e1000_netdev_ops = {
  716. .ndo_open = e1000_open,
  717. .ndo_stop = e1000_close,
  718. .ndo_start_xmit = e1000_xmit_frame,
  719. .ndo_set_rx_mode = e1000_set_rx_mode,
  720. .ndo_set_mac_address = e1000_set_mac,
  721. .ndo_tx_timeout = e1000_tx_timeout,
  722. .ndo_change_mtu = e1000_change_mtu,
  723. .ndo_do_ioctl = e1000_ioctl,
  724. .ndo_validate_addr = eth_validate_addr,
  725. .ndo_vlan_rx_add_vid = e1000_vlan_rx_add_vid,
  726. .ndo_vlan_rx_kill_vid = e1000_vlan_rx_kill_vid,
  727. #ifdef CONFIG_NET_POLL_CONTROLLER
  728. .ndo_poll_controller = e1000_netpoll,
  729. #endif
  730. .ndo_fix_features = e1000_fix_features,
  731. .ndo_set_features = e1000_set_features,
  732. };
  733. /**
  734. * e1000_init_hw_struct - initialize members of hw struct
  735. * @adapter: board private struct
  736. * @hw: structure used by e1000_hw.c
  737. *
  738. * Factors out initialization of the e1000_hw struct to its own function
  739. * that can be called very early at init (just after struct allocation).
  740. * Fields are initialized based on PCI device information and
  741. * OS network device settings (MTU size).
  742. * Returns negative error codes if MAC type setup fails.
  743. */
  744. static int e1000_init_hw_struct(struct e1000_adapter *adapter,
  745. struct e1000_hw *hw)
  746. {
  747. struct pci_dev *pdev = adapter->pdev;
  748. /* PCI config space info */
  749. hw->vendor_id = pdev->vendor;
  750. hw->device_id = pdev->device;
  751. hw->subsystem_vendor_id = pdev->subsystem_vendor;
  752. hw->subsystem_id = pdev->subsystem_device;
  753. hw->revision_id = pdev->revision;
  754. pci_read_config_word(pdev, PCI_COMMAND, &hw->pci_cmd_word);
  755. hw->max_frame_size = adapter->netdev->mtu +
  756. ENET_HEADER_SIZE + ETHERNET_FCS_SIZE;
  757. hw->min_frame_size = MINIMUM_ETHERNET_FRAME_SIZE;
  758. /* identify the MAC */
  759. if (e1000_set_mac_type(hw)) {
  760. e_err(probe, "Unknown MAC Type\n");
  761. return -EIO;
  762. }
  763. switch (hw->mac_type) {
  764. default:
  765. break;
  766. case e1000_82541:
  767. case e1000_82547:
  768. case e1000_82541_rev_2:
  769. case e1000_82547_rev_2:
  770. hw->phy_init_script = 1;
  771. break;
  772. }
  773. e1000_set_media_type(hw);
  774. e1000_get_bus_info(hw);
  775. hw->wait_autoneg_complete = false;
  776. hw->tbi_compatibility_en = true;
  777. hw->adaptive_ifs = true;
  778. /* Copper options */
  779. if (hw->media_type == e1000_media_type_copper) {
  780. hw->mdix = AUTO_ALL_MODES;
  781. hw->disable_polarity_correction = false;
  782. hw->master_slave = E1000_MASTER_SLAVE;
  783. }
  784. return 0;
  785. }
  786. /**
  787. * e1000_probe - Device Initialization Routine
  788. * @pdev: PCI device information struct
  789. * @ent: entry in e1000_pci_tbl
  790. *
  791. * Returns 0 on success, negative on failure
  792. *
  793. * e1000_probe initializes an adapter identified by a pci_dev structure.
  794. * The OS initialization, configuring of the adapter private structure,
  795. * and a hardware reset occur.
  796. **/
  797. static int e1000_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
  798. {
  799. struct net_device *netdev;
  800. struct e1000_adapter *adapter = NULL;
  801. struct e1000_hw *hw;
  802. static int cards_found;
  803. static int global_quad_port_a; /* global ksp3 port a indication */
  804. int i, err, pci_using_dac;
  805. u16 eeprom_data = 0;
  806. u16 tmp = 0;
  807. u16 eeprom_apme_mask = E1000_EEPROM_APME;
  808. int bars, need_ioport;
  809. bool disable_dev = false;
  810. /* do not allocate ioport bars when not needed */
  811. need_ioport = e1000_is_need_ioport(pdev);
  812. if (need_ioport) {
  813. bars = pci_select_bars(pdev, IORESOURCE_MEM | IORESOURCE_IO);
  814. err = pci_enable_device(pdev);
  815. } else {
  816. bars = pci_select_bars(pdev, IORESOURCE_MEM);
  817. err = pci_enable_device_mem(pdev);
  818. }
  819. if (err)
  820. return err;
  821. err = pci_request_selected_regions(pdev, bars, e1000_driver_name);
  822. if (err)
  823. goto err_pci_reg;
  824. pci_set_master(pdev);
  825. err = pci_save_state(pdev);
  826. if (err)
  827. goto err_alloc_etherdev;
  828. err = -ENOMEM;
  829. netdev = alloc_etherdev(sizeof(struct e1000_adapter));
  830. if (!netdev)
  831. goto err_alloc_etherdev;
  832. SET_NETDEV_DEV(netdev, &pdev->dev);
  833. pci_set_drvdata(pdev, netdev);
  834. adapter = netdev_priv(netdev);
  835. adapter->netdev = netdev;
  836. adapter->pdev = pdev;
  837. adapter->msg_enable = netif_msg_init(debug, DEFAULT_MSG_ENABLE);
  838. adapter->bars = bars;
  839. adapter->need_ioport = need_ioport;
  840. hw = &adapter->hw;
  841. hw->back = adapter;
  842. err = -EIO;
  843. hw->hw_addr = pci_ioremap_bar(pdev, BAR_0);
  844. if (!hw->hw_addr)
  845. goto err_ioremap;
  846. if (adapter->need_ioport) {
  847. for (i = BAR_1; i <= BAR_5; i++) {
  848. if (pci_resource_len(pdev, i) == 0)
  849. continue;
  850. if (pci_resource_flags(pdev, i) & IORESOURCE_IO) {
  851. hw->io_base = pci_resource_start(pdev, i);
  852. break;
  853. }
  854. }
  855. }
  856. /* make ready for any if (hw->...) below */
  857. err = e1000_init_hw_struct(adapter, hw);
  858. if (err)
  859. goto err_sw_init;
  860. /* there is a workaround being applied below that limits
  861. * 64-bit DMA addresses to 64-bit hardware. There are some
  862. * 32-bit adapters that Tx hang when given 64-bit DMA addresses
  863. */
  864. pci_using_dac = 0;
  865. if ((hw->bus_type == e1000_bus_type_pcix) &&
  866. !dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64))) {
  867. pci_using_dac = 1;
  868. } else {
  869. err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
  870. if (err) {
  871. pr_err("No usable DMA config, aborting\n");
  872. goto err_dma;
  873. }
  874. }
  875. netdev->netdev_ops = &e1000_netdev_ops;
  876. e1000_set_ethtool_ops(netdev);
  877. netdev->watchdog_timeo = 5 * HZ;
  878. netif_napi_add(netdev, &adapter->napi, e1000_clean, 64);
  879. strncpy(netdev->name, pci_name(pdev), sizeof(netdev->name) - 1);
  880. adapter->bd_number = cards_found;
  881. /* setup the private structure */
  882. err = e1000_sw_init(adapter);
  883. if (err)
  884. goto err_sw_init;
  885. err = -EIO;
  886. if (hw->mac_type == e1000_ce4100) {
  887. hw->ce4100_gbe_mdio_base_virt =
  888. ioremap(pci_resource_start(pdev, BAR_1),
  889. pci_resource_len(pdev, BAR_1));
  890. if (!hw->ce4100_gbe_mdio_base_virt)
  891. goto err_mdio_ioremap;
  892. }
  893. if (hw->mac_type >= e1000_82543) {
  894. netdev->hw_features = NETIF_F_SG |
  895. NETIF_F_HW_CSUM |
  896. NETIF_F_HW_VLAN_CTAG_RX;
  897. netdev->features = NETIF_F_HW_VLAN_CTAG_TX |
  898. NETIF_F_HW_VLAN_CTAG_FILTER;
  899. }
  900. if ((hw->mac_type >= e1000_82544) &&
  901. (hw->mac_type != e1000_82547))
  902. netdev->hw_features |= NETIF_F_TSO;
  903. netdev->priv_flags |= IFF_SUPP_NOFCS;
  904. netdev->features |= netdev->hw_features;
  905. netdev->hw_features |= (NETIF_F_RXCSUM |
  906. NETIF_F_RXALL |
  907. NETIF_F_RXFCS);
  908. if (pci_using_dac) {
  909. netdev->features |= NETIF_F_HIGHDMA;
  910. netdev->vlan_features |= NETIF_F_HIGHDMA;
  911. }
  912. netdev->vlan_features |= (NETIF_F_TSO |
  913. NETIF_F_HW_CSUM |
  914. NETIF_F_SG);
  915. /* Do not set IFF_UNICAST_FLT for VMWare's 82545EM */
  916. if (hw->device_id != E1000_DEV_ID_82545EM_COPPER ||
  917. hw->subsystem_vendor_id != PCI_VENDOR_ID_VMWARE)
  918. netdev->priv_flags |= IFF_UNICAST_FLT;
  919. /* MTU range: 46 - 16110 */
  920. netdev->min_mtu = ETH_ZLEN - ETH_HLEN;
  921. netdev->max_mtu = MAX_JUMBO_FRAME_SIZE - (ETH_HLEN + ETH_FCS_LEN);
  922. adapter->en_mng_pt = e1000_enable_mng_pass_thru(hw);
  923. /* initialize eeprom parameters */
  924. if (e1000_init_eeprom_params(hw)) {
  925. e_err(probe, "EEPROM initialization failed\n");
  926. goto err_eeprom;
  927. }
  928. /* before reading the EEPROM, reset the controller to
  929. * put the device in a known good starting state
  930. */
  931. e1000_reset_hw(hw);
  932. /* make sure the EEPROM is good */
  933. if (e1000_validate_eeprom_checksum(hw) < 0) {
  934. e_err(probe, "The EEPROM Checksum Is Not Valid\n");
  935. e1000_dump_eeprom(adapter);
  936. /* set MAC address to all zeroes to invalidate and temporary
  937. * disable this device for the user. This blocks regular
  938. * traffic while still permitting ethtool ioctls from reaching
  939. * the hardware as well as allowing the user to run the
  940. * interface after manually setting a hw addr using
  941. * `ip set address`
  942. */
  943. memset(hw->mac_addr, 0, netdev->addr_len);
  944. } else {
  945. /* copy the MAC address out of the EEPROM */
  946. if (e1000_read_mac_addr(hw))
  947. e_err(probe, "EEPROM Read Error\n");
  948. }
  949. /* don't block initialization here due to bad MAC address */
  950. memcpy(netdev->dev_addr, hw->mac_addr, netdev->addr_len);
  951. if (!is_valid_ether_addr(netdev->dev_addr))
  952. e_err(probe, "Invalid MAC Address\n");
  953. INIT_DELAYED_WORK(&adapter->watchdog_task, e1000_watchdog);
  954. INIT_DELAYED_WORK(&adapter->fifo_stall_task,
  955. e1000_82547_tx_fifo_stall_task);
  956. INIT_DELAYED_WORK(&adapter->phy_info_task, e1000_update_phy_info_task);
  957. INIT_WORK(&adapter->reset_task, e1000_reset_task);
  958. e1000_check_options(adapter);
  959. /* Initial Wake on LAN setting
  960. * If APM wake is enabled in the EEPROM,
  961. * enable the ACPI Magic Packet filter
  962. */
  963. switch (hw->mac_type) {
  964. case e1000_82542_rev2_0:
  965. case e1000_82542_rev2_1:
  966. case e1000_82543:
  967. break;
  968. case e1000_82544:
  969. e1000_read_eeprom(hw,
  970. EEPROM_INIT_CONTROL2_REG, 1, &eeprom_data);
  971. eeprom_apme_mask = E1000_EEPROM_82544_APM;
  972. break;
  973. case e1000_82546:
  974. case e1000_82546_rev_3:
  975. if (er32(STATUS) & E1000_STATUS_FUNC_1) {
  976. e1000_read_eeprom(hw,
  977. EEPROM_INIT_CONTROL3_PORT_B, 1, &eeprom_data);
  978. break;
  979. }
  980. /* Fall Through */
  981. default:
  982. e1000_read_eeprom(hw,
  983. EEPROM_INIT_CONTROL3_PORT_A, 1, &eeprom_data);
  984. break;
  985. }
  986. if (eeprom_data & eeprom_apme_mask)
  987. adapter->eeprom_wol |= E1000_WUFC_MAG;
  988. /* now that we have the eeprom settings, apply the special cases
  989. * where the eeprom may be wrong or the board simply won't support
  990. * wake on lan on a particular port
  991. */
  992. switch (pdev->device) {
  993. case E1000_DEV_ID_82546GB_PCIE:
  994. adapter->eeprom_wol = 0;
  995. break;
  996. case E1000_DEV_ID_82546EB_FIBER:
  997. case E1000_DEV_ID_82546GB_FIBER:
  998. /* Wake events only supported on port A for dual fiber
  999. * regardless of eeprom setting
  1000. */
  1001. if (er32(STATUS) & E1000_STATUS_FUNC_1)
  1002. adapter->eeprom_wol = 0;
  1003. break;
  1004. case E1000_DEV_ID_82546GB_QUAD_COPPER_KSP3:
  1005. /* if quad port adapter, disable WoL on all but port A */
  1006. if (global_quad_port_a != 0)
  1007. adapter->eeprom_wol = 0;
  1008. else
  1009. adapter->quad_port_a = true;
  1010. /* Reset for multiple quad port adapters */
  1011. if (++global_quad_port_a == 4)
  1012. global_quad_port_a = 0;
  1013. break;
  1014. }
  1015. /* initialize the wol settings based on the eeprom settings */
  1016. adapter->wol = adapter->eeprom_wol;
  1017. device_set_wakeup_enable(&adapter->pdev->dev, adapter->wol);
  1018. /* Auto detect PHY address */
  1019. if (hw->mac_type == e1000_ce4100) {
  1020. for (i = 0; i < 32; i++) {
  1021. hw->phy_addr = i;
  1022. e1000_read_phy_reg(hw, PHY_ID2, &tmp);
  1023. if (tmp != 0 && tmp != 0xFF)
  1024. break;
  1025. }
  1026. if (i >= 32)
  1027. goto err_eeprom;
  1028. }
  1029. /* reset the hardware with the new settings */
  1030. e1000_reset(adapter);
  1031. strcpy(netdev->name, "eth%d");
  1032. err = register_netdev(netdev);
  1033. if (err)
  1034. goto err_register;
  1035. e1000_vlan_filter_on_off(adapter, false);
  1036. /* print bus type/speed/width info */
  1037. e_info(probe, "(PCI%s:%dMHz:%d-bit) %pM\n",
  1038. ((hw->bus_type == e1000_bus_type_pcix) ? "-X" : ""),
  1039. ((hw->bus_speed == e1000_bus_speed_133) ? 133 :
  1040. (hw->bus_speed == e1000_bus_speed_120) ? 120 :
  1041. (hw->bus_speed == e1000_bus_speed_100) ? 100 :
  1042. (hw->bus_speed == e1000_bus_speed_66) ? 66 : 33),
  1043. ((hw->bus_width == e1000_bus_width_64) ? 64 : 32),
  1044. netdev->dev_addr);
  1045. /* carrier off reporting is important to ethtool even BEFORE open */
  1046. netif_carrier_off(netdev);
  1047. e_info(probe, "Intel(R) PRO/1000 Network Connection\n");
  1048. cards_found++;
  1049. return 0;
  1050. err_register:
  1051. err_eeprom:
  1052. e1000_phy_hw_reset(hw);
  1053. if (hw->flash_address)
  1054. iounmap(hw->flash_address);
  1055. kfree(adapter->tx_ring);
  1056. kfree(adapter->rx_ring);
  1057. err_dma:
  1058. err_sw_init:
  1059. err_mdio_ioremap:
  1060. iounmap(hw->ce4100_gbe_mdio_base_virt);
  1061. iounmap(hw->hw_addr);
  1062. err_ioremap:
  1063. disable_dev = !test_and_set_bit(__E1000_DISABLED, &adapter->flags);
  1064. free_netdev(netdev);
  1065. err_alloc_etherdev:
  1066. pci_release_selected_regions(pdev, bars);
  1067. err_pci_reg:
  1068. if (!adapter || disable_dev)
  1069. pci_disable_device(pdev);
  1070. return err;
  1071. }
  1072. /**
  1073. * e1000_remove - Device Removal Routine
  1074. * @pdev: PCI device information struct
  1075. *
  1076. * e1000_remove is called by the PCI subsystem to alert the driver
  1077. * that it should release a PCI device. That could be caused by a
  1078. * Hot-Plug event, or because the driver is going to be removed from
  1079. * memory.
  1080. **/
  1081. static void e1000_remove(struct pci_dev *pdev)
  1082. {
  1083. struct net_device *netdev = pci_get_drvdata(pdev);
  1084. struct e1000_adapter *adapter = netdev_priv(netdev);
  1085. struct e1000_hw *hw = &adapter->hw;
  1086. bool disable_dev;
  1087. e1000_down_and_stop(adapter);
  1088. e1000_release_manageability(adapter);
  1089. unregister_netdev(netdev);
  1090. e1000_phy_hw_reset(hw);
  1091. kfree(adapter->tx_ring);
  1092. kfree(adapter->rx_ring);
  1093. if (hw->mac_type == e1000_ce4100)
  1094. iounmap(hw->ce4100_gbe_mdio_base_virt);
  1095. iounmap(hw->hw_addr);
  1096. if (hw->flash_address)
  1097. iounmap(hw->flash_address);
  1098. pci_release_selected_regions(pdev, adapter->bars);
  1099. disable_dev = !test_and_set_bit(__E1000_DISABLED, &adapter->flags);
  1100. free_netdev(netdev);
  1101. if (disable_dev)
  1102. pci_disable_device(pdev);
  1103. }
  1104. /**
  1105. * e1000_sw_init - Initialize general software structures (struct e1000_adapter)
  1106. * @adapter: board private structure to initialize
  1107. *
  1108. * e1000_sw_init initializes the Adapter private data structure.
  1109. * e1000_init_hw_struct MUST be called before this function
  1110. **/
  1111. static int e1000_sw_init(struct e1000_adapter *adapter)
  1112. {
  1113. adapter->rx_buffer_len = MAXIMUM_ETHERNET_VLAN_SIZE;
  1114. adapter->num_tx_queues = 1;
  1115. adapter->num_rx_queues = 1;
  1116. if (e1000_alloc_queues(adapter)) {
  1117. e_err(probe, "Unable to allocate memory for queues\n");
  1118. return -ENOMEM;
  1119. }
  1120. /* Explicitly disable IRQ since the NIC can be in any state. */
  1121. e1000_irq_disable(adapter);
  1122. spin_lock_init(&adapter->stats_lock);
  1123. set_bit(__E1000_DOWN, &adapter->flags);
  1124. return 0;
  1125. }
  1126. /**
  1127. * e1000_alloc_queues - Allocate memory for all rings
  1128. * @adapter: board private structure to initialize
  1129. *
  1130. * We allocate one ring per queue at run-time since we don't know the
  1131. * number of queues at compile-time.
  1132. **/
  1133. static int e1000_alloc_queues(struct e1000_adapter *adapter)
  1134. {
  1135. adapter->tx_ring = kcalloc(adapter->num_tx_queues,
  1136. sizeof(struct e1000_tx_ring), GFP_KERNEL);
  1137. if (!adapter->tx_ring)
  1138. return -ENOMEM;
  1139. adapter->rx_ring = kcalloc(adapter->num_rx_queues,
  1140. sizeof(struct e1000_rx_ring), GFP_KERNEL);
  1141. if (!adapter->rx_ring) {
  1142. kfree(adapter->tx_ring);
  1143. return -ENOMEM;
  1144. }
  1145. return E1000_SUCCESS;
  1146. }
  1147. /**
  1148. * e1000_open - Called when a network interface is made active
  1149. * @netdev: network interface device structure
  1150. *
  1151. * Returns 0 on success, negative value on failure
  1152. *
  1153. * The open entry point is called when a network interface is made
  1154. * active by the system (IFF_UP). At this point all resources needed
  1155. * for transmit and receive operations are allocated, the interrupt
  1156. * handler is registered with the OS, the watchdog task is started,
  1157. * and the stack is notified that the interface is ready.
  1158. **/
  1159. int e1000_open(struct net_device *netdev)
  1160. {
  1161. struct e1000_adapter *adapter = netdev_priv(netdev);
  1162. struct e1000_hw *hw = &adapter->hw;
  1163. int err;
  1164. /* disallow open during test */
  1165. if (test_bit(__E1000_TESTING, &adapter->flags))
  1166. return -EBUSY;
  1167. netif_carrier_off(netdev);
  1168. /* allocate transmit descriptors */
  1169. err = e1000_setup_all_tx_resources(adapter);
  1170. if (err)
  1171. goto err_setup_tx;
  1172. /* allocate receive descriptors */
  1173. err = e1000_setup_all_rx_resources(adapter);
  1174. if (err)
  1175. goto err_setup_rx;
  1176. e1000_power_up_phy(adapter);
  1177. adapter->mng_vlan_id = E1000_MNG_VLAN_NONE;
  1178. if ((hw->mng_cookie.status &
  1179. E1000_MNG_DHCP_COOKIE_STATUS_VLAN_SUPPORT)) {
  1180. e1000_update_mng_vlan(adapter);
  1181. }
  1182. /* before we allocate an interrupt, we must be ready to handle it.
  1183. * Setting DEBUG_SHIRQ in the kernel makes it fire an interrupt
  1184. * as soon as we call pci_request_irq, so we have to setup our
  1185. * clean_rx handler before we do so.
  1186. */
  1187. e1000_configure(adapter);
  1188. err = e1000_request_irq(adapter);
  1189. if (err)
  1190. goto err_req_irq;
  1191. /* From here on the code is the same as e1000_up() */
  1192. clear_bit(__E1000_DOWN, &adapter->flags);
  1193. napi_enable(&adapter->napi);
  1194. e1000_irq_enable(adapter);
  1195. netif_start_queue(netdev);
  1196. /* fire a link status change interrupt to start the watchdog */
  1197. ew32(ICS, E1000_ICS_LSC);
  1198. return E1000_SUCCESS;
  1199. err_req_irq:
  1200. e1000_power_down_phy(adapter);
  1201. e1000_free_all_rx_resources(adapter);
  1202. err_setup_rx:
  1203. e1000_free_all_tx_resources(adapter);
  1204. err_setup_tx:
  1205. e1000_reset(adapter);
  1206. return err;
  1207. }
  1208. /**
  1209. * e1000_close - Disables a network interface
  1210. * @netdev: network interface device structure
  1211. *
  1212. * Returns 0, this is not allowed to fail
  1213. *
  1214. * The close entry point is called when an interface is de-activated
  1215. * by the OS. The hardware is still under the drivers control, but
  1216. * needs to be disabled. A global MAC reset is issued to stop the
  1217. * hardware, and all transmit and receive resources are freed.
  1218. **/
  1219. int e1000_close(struct net_device *netdev)
  1220. {
  1221. struct e1000_adapter *adapter = netdev_priv(netdev);
  1222. struct e1000_hw *hw = &adapter->hw;
  1223. int count = E1000_CHECK_RESET_COUNT;
  1224. while (test_bit(__E1000_RESETTING, &adapter->flags) && count--)
  1225. usleep_range(10000, 20000);
  1226. WARN_ON(test_bit(__E1000_RESETTING, &adapter->flags));
  1227. e1000_down(adapter);
  1228. e1000_power_down_phy(adapter);
  1229. e1000_free_irq(adapter);
  1230. e1000_free_all_tx_resources(adapter);
  1231. e1000_free_all_rx_resources(adapter);
  1232. /* kill manageability vlan ID if supported, but not if a vlan with
  1233. * the same ID is registered on the host OS (let 8021q kill it)
  1234. */
  1235. if ((hw->mng_cookie.status &
  1236. E1000_MNG_DHCP_COOKIE_STATUS_VLAN_SUPPORT) &&
  1237. !test_bit(adapter->mng_vlan_id, adapter->active_vlans)) {
  1238. e1000_vlan_rx_kill_vid(netdev, htons(ETH_P_8021Q),
  1239. adapter->mng_vlan_id);
  1240. }
  1241. return 0;
  1242. }
  1243. /**
  1244. * e1000_check_64k_bound - check that memory doesn't cross 64kB boundary
  1245. * @adapter: address of board private structure
  1246. * @start: address of beginning of memory
  1247. * @len: length of memory
  1248. **/
  1249. static bool e1000_check_64k_bound(struct e1000_adapter *adapter, void *start,
  1250. unsigned long len)
  1251. {
  1252. struct e1000_hw *hw = &adapter->hw;
  1253. unsigned long begin = (unsigned long)start;
  1254. unsigned long end = begin + len;
  1255. /* First rev 82545 and 82546 need to not allow any memory
  1256. * write location to cross 64k boundary due to errata 23
  1257. */
  1258. if (hw->mac_type == e1000_82545 ||
  1259. hw->mac_type == e1000_ce4100 ||
  1260. hw->mac_type == e1000_82546) {
  1261. return ((begin ^ (end - 1)) >> 16) != 0 ? false : true;
  1262. }
  1263. return true;
  1264. }
  1265. /**
  1266. * e1000_setup_tx_resources - allocate Tx resources (Descriptors)
  1267. * @adapter: board private structure
  1268. * @txdr: tx descriptor ring (for a specific queue) to setup
  1269. *
  1270. * Return 0 on success, negative on failure
  1271. **/
  1272. static int e1000_setup_tx_resources(struct e1000_adapter *adapter,
  1273. struct e1000_tx_ring *txdr)
  1274. {
  1275. struct pci_dev *pdev = adapter->pdev;
  1276. int size;
  1277. size = sizeof(struct e1000_tx_buffer) * txdr->count;
  1278. txdr->buffer_info = vzalloc(size);
  1279. if (!txdr->buffer_info)
  1280. return -ENOMEM;
  1281. /* round up to nearest 4K */
  1282. txdr->size = txdr->count * sizeof(struct e1000_tx_desc);
  1283. txdr->size = ALIGN(txdr->size, 4096);
  1284. txdr->desc = dma_alloc_coherent(&pdev->dev, txdr->size, &txdr->dma,
  1285. GFP_KERNEL);
  1286. if (!txdr->desc) {
  1287. setup_tx_desc_die:
  1288. vfree(txdr->buffer_info);
  1289. return -ENOMEM;
  1290. }
  1291. /* Fix for errata 23, can't cross 64kB boundary */
  1292. if (!e1000_check_64k_bound(adapter, txdr->desc, txdr->size)) {
  1293. void *olddesc = txdr->desc;
  1294. dma_addr_t olddma = txdr->dma;
  1295. e_err(tx_err, "txdr align check failed: %u bytes at %p\n",
  1296. txdr->size, txdr->desc);
  1297. /* Try again, without freeing the previous */
  1298. txdr->desc = dma_alloc_coherent(&pdev->dev, txdr->size,
  1299. &txdr->dma, GFP_KERNEL);
  1300. /* Failed allocation, critical failure */
  1301. if (!txdr->desc) {
  1302. dma_free_coherent(&pdev->dev, txdr->size, olddesc,
  1303. olddma);
  1304. goto setup_tx_desc_die;
  1305. }
  1306. if (!e1000_check_64k_bound(adapter, txdr->desc, txdr->size)) {
  1307. /* give up */
  1308. dma_free_coherent(&pdev->dev, txdr->size, txdr->desc,
  1309. txdr->dma);
  1310. dma_free_coherent(&pdev->dev, txdr->size, olddesc,
  1311. olddma);
  1312. e_err(probe, "Unable to allocate aligned memory "
  1313. "for the transmit descriptor ring\n");
  1314. vfree(txdr->buffer_info);
  1315. return -ENOMEM;
  1316. } else {
  1317. /* Free old allocation, new allocation was successful */
  1318. dma_free_coherent(&pdev->dev, txdr->size, olddesc,
  1319. olddma);
  1320. }
  1321. }
  1322. memset(txdr->desc, 0, txdr->size);
  1323. txdr->next_to_use = 0;
  1324. txdr->next_to_clean = 0;
  1325. return 0;
  1326. }
  1327. /**
  1328. * e1000_setup_all_tx_resources - wrapper to allocate Tx resources
  1329. * (Descriptors) for all queues
  1330. * @adapter: board private structure
  1331. *
  1332. * Return 0 on success, negative on failure
  1333. **/
  1334. int e1000_setup_all_tx_resources(struct e1000_adapter *adapter)
  1335. {
  1336. int i, err = 0;
  1337. for (i = 0; i < adapter->num_tx_queues; i++) {
  1338. err = e1000_setup_tx_resources(adapter, &adapter->tx_ring[i]);
  1339. if (err) {
  1340. e_err(probe, "Allocation for Tx Queue %u failed\n", i);
  1341. for (i-- ; i >= 0; i--)
  1342. e1000_free_tx_resources(adapter,
  1343. &adapter->tx_ring[i]);
  1344. break;
  1345. }
  1346. }
  1347. return err;
  1348. }
  1349. /**
  1350. * e1000_configure_tx - Configure 8254x Transmit Unit after Reset
  1351. * @adapter: board private structure
  1352. *
  1353. * Configure the Tx unit of the MAC after a reset.
  1354. **/
  1355. static void e1000_configure_tx(struct e1000_adapter *adapter)
  1356. {
  1357. u64 tdba;
  1358. struct e1000_hw *hw = &adapter->hw;
  1359. u32 tdlen, tctl, tipg;
  1360. u32 ipgr1, ipgr2;
  1361. /* Setup the HW Tx Head and Tail descriptor pointers */
  1362. switch (adapter->num_tx_queues) {
  1363. case 1:
  1364. default:
  1365. tdba = adapter->tx_ring[0].dma;
  1366. tdlen = adapter->tx_ring[0].count *
  1367. sizeof(struct e1000_tx_desc);
  1368. ew32(TDLEN, tdlen);
  1369. ew32(TDBAH, (tdba >> 32));
  1370. ew32(TDBAL, (tdba & 0x00000000ffffffffULL));
  1371. ew32(TDT, 0);
  1372. ew32(TDH, 0);
  1373. adapter->tx_ring[0].tdh = ((hw->mac_type >= e1000_82543) ?
  1374. E1000_TDH : E1000_82542_TDH);
  1375. adapter->tx_ring[0].tdt = ((hw->mac_type >= e1000_82543) ?
  1376. E1000_TDT : E1000_82542_TDT);
  1377. break;
  1378. }
  1379. /* Set the default values for the Tx Inter Packet Gap timer */
  1380. if ((hw->media_type == e1000_media_type_fiber ||
  1381. hw->media_type == e1000_media_type_internal_serdes))
  1382. tipg = DEFAULT_82543_TIPG_IPGT_FIBER;
  1383. else
  1384. tipg = DEFAULT_82543_TIPG_IPGT_COPPER;
  1385. switch (hw->mac_type) {
  1386. case e1000_82542_rev2_0:
  1387. case e1000_82542_rev2_1:
  1388. tipg = DEFAULT_82542_TIPG_IPGT;
  1389. ipgr1 = DEFAULT_82542_TIPG_IPGR1;
  1390. ipgr2 = DEFAULT_82542_TIPG_IPGR2;
  1391. break;
  1392. default:
  1393. ipgr1 = DEFAULT_82543_TIPG_IPGR1;
  1394. ipgr2 = DEFAULT_82543_TIPG_IPGR2;
  1395. break;
  1396. }
  1397. tipg |= ipgr1 << E1000_TIPG_IPGR1_SHIFT;
  1398. tipg |= ipgr2 << E1000_TIPG_IPGR2_SHIFT;
  1399. ew32(TIPG, tipg);
  1400. /* Set the Tx Interrupt Delay register */
  1401. ew32(TIDV, adapter->tx_int_delay);
  1402. if (hw->mac_type >= e1000_82540)
  1403. ew32(TADV, adapter->tx_abs_int_delay);
  1404. /* Program the Transmit Control Register */
  1405. tctl = er32(TCTL);
  1406. tctl &= ~E1000_TCTL_CT;
  1407. tctl |= E1000_TCTL_PSP | E1000_TCTL_RTLC |
  1408. (E1000_COLLISION_THRESHOLD << E1000_CT_SHIFT);
  1409. e1000_config_collision_dist(hw);
  1410. /* Setup Transmit Descriptor Settings for eop descriptor */
  1411. adapter->txd_cmd = E1000_TXD_CMD_EOP | E1000_TXD_CMD_IFCS;
  1412. /* only set IDE if we are delaying interrupts using the timers */
  1413. if (adapter->tx_int_delay)
  1414. adapter->txd_cmd |= E1000_TXD_CMD_IDE;
  1415. if (hw->mac_type < e1000_82543)
  1416. adapter->txd_cmd |= E1000_TXD_CMD_RPS;
  1417. else
  1418. adapter->txd_cmd |= E1000_TXD_CMD_RS;
  1419. /* Cache if we're 82544 running in PCI-X because we'll
  1420. * need this to apply a workaround later in the send path.
  1421. */
  1422. if (hw->mac_type == e1000_82544 &&
  1423. hw->bus_type == e1000_bus_type_pcix)
  1424. adapter->pcix_82544 = true;
  1425. ew32(TCTL, tctl);
  1426. }
  1427. /**
  1428. * e1000_setup_rx_resources - allocate Rx resources (Descriptors)
  1429. * @adapter: board private structure
  1430. * @rxdr: rx descriptor ring (for a specific queue) to setup
  1431. *
  1432. * Returns 0 on success, negative on failure
  1433. **/
  1434. static int e1000_setup_rx_resources(struct e1000_adapter *adapter,
  1435. struct e1000_rx_ring *rxdr)
  1436. {
  1437. struct pci_dev *pdev = adapter->pdev;
  1438. int size, desc_len;
  1439. size = sizeof(struct e1000_rx_buffer) * rxdr->count;
  1440. rxdr->buffer_info = vzalloc(size);
  1441. if (!rxdr->buffer_info)
  1442. return -ENOMEM;
  1443. desc_len = sizeof(struct e1000_rx_desc);
  1444. /* Round up to nearest 4K */
  1445. rxdr->size = rxdr->count * desc_len;
  1446. rxdr->size = ALIGN(rxdr->size, 4096);
  1447. rxdr->desc = dma_alloc_coherent(&pdev->dev, rxdr->size, &rxdr->dma,
  1448. GFP_KERNEL);
  1449. if (!rxdr->desc) {
  1450. setup_rx_desc_die:
  1451. vfree(rxdr->buffer_info);
  1452. return -ENOMEM;
  1453. }
  1454. /* Fix for errata 23, can't cross 64kB boundary */
  1455. if (!e1000_check_64k_bound(adapter, rxdr->desc, rxdr->size)) {
  1456. void *olddesc = rxdr->desc;
  1457. dma_addr_t olddma = rxdr->dma;
  1458. e_err(rx_err, "rxdr align check failed: %u bytes at %p\n",
  1459. rxdr->size, rxdr->desc);
  1460. /* Try again, without freeing the previous */
  1461. rxdr->desc = dma_alloc_coherent(&pdev->dev, rxdr->size,
  1462. &rxdr->dma, GFP_KERNEL);
  1463. /* Failed allocation, critical failure */
  1464. if (!rxdr->desc) {
  1465. dma_free_coherent(&pdev->dev, rxdr->size, olddesc,
  1466. olddma);
  1467. goto setup_rx_desc_die;
  1468. }
  1469. if (!e1000_check_64k_bound(adapter, rxdr->desc, rxdr->size)) {
  1470. /* give up */
  1471. dma_free_coherent(&pdev->dev, rxdr->size, rxdr->desc,
  1472. rxdr->dma);
  1473. dma_free_coherent(&pdev->dev, rxdr->size, olddesc,
  1474. olddma);
  1475. e_err(probe, "Unable to allocate aligned memory for "
  1476. "the Rx descriptor ring\n");
  1477. goto setup_rx_desc_die;
  1478. } else {
  1479. /* Free old allocation, new allocation was successful */
  1480. dma_free_coherent(&pdev->dev, rxdr->size, olddesc,
  1481. olddma);
  1482. }
  1483. }
  1484. memset(rxdr->desc, 0, rxdr->size);
  1485. rxdr->next_to_clean = 0;
  1486. rxdr->next_to_use = 0;
  1487. rxdr->rx_skb_top = NULL;
  1488. return 0;
  1489. }
  1490. /**
  1491. * e1000_setup_all_rx_resources - wrapper to allocate Rx resources
  1492. * (Descriptors) for all queues
  1493. * @adapter: board private structure
  1494. *
  1495. * Return 0 on success, negative on failure
  1496. **/
  1497. int e1000_setup_all_rx_resources(struct e1000_adapter *adapter)
  1498. {
  1499. int i, err = 0;
  1500. for (i = 0; i < adapter->num_rx_queues; i++) {
  1501. err = e1000_setup_rx_resources(adapter, &adapter->rx_ring[i]);
  1502. if (err) {
  1503. e_err(probe, "Allocation for Rx Queue %u failed\n", i);
  1504. for (i-- ; i >= 0; i--)
  1505. e1000_free_rx_resources(adapter,
  1506. &adapter->rx_ring[i]);
  1507. break;
  1508. }
  1509. }
  1510. return err;
  1511. }
  1512. /**
  1513. * e1000_setup_rctl - configure the receive control registers
  1514. * @adapter: Board private structure
  1515. **/
  1516. static void e1000_setup_rctl(struct e1000_adapter *adapter)
  1517. {
  1518. struct e1000_hw *hw = &adapter->hw;
  1519. u32 rctl;
  1520. rctl = er32(RCTL);
  1521. rctl &= ~(3 << E1000_RCTL_MO_SHIFT);
  1522. rctl |= E1000_RCTL_BAM | E1000_RCTL_LBM_NO |
  1523. E1000_RCTL_RDMTS_HALF |
  1524. (hw->mc_filter_type << E1000_RCTL_MO_SHIFT);
  1525. if (hw->tbi_compatibility_on == 1)
  1526. rctl |= E1000_RCTL_SBP;
  1527. else
  1528. rctl &= ~E1000_RCTL_SBP;
  1529. if (adapter->netdev->mtu <= ETH_DATA_LEN)
  1530. rctl &= ~E1000_RCTL_LPE;
  1531. else
  1532. rctl |= E1000_RCTL_LPE;
  1533. /* Setup buffer sizes */
  1534. rctl &= ~E1000_RCTL_SZ_4096;
  1535. rctl |= E1000_RCTL_BSEX;
  1536. switch (adapter->rx_buffer_len) {
  1537. case E1000_RXBUFFER_2048:
  1538. default:
  1539. rctl |= E1000_RCTL_SZ_2048;
  1540. rctl &= ~E1000_RCTL_BSEX;
  1541. break;
  1542. case E1000_RXBUFFER_4096:
  1543. rctl |= E1000_RCTL_SZ_4096;
  1544. break;
  1545. case E1000_RXBUFFER_8192:
  1546. rctl |= E1000_RCTL_SZ_8192;
  1547. break;
  1548. case E1000_RXBUFFER_16384:
  1549. rctl |= E1000_RCTL_SZ_16384;
  1550. break;
  1551. }
  1552. /* This is useful for sniffing bad packets. */
  1553. if (adapter->netdev->features & NETIF_F_RXALL) {
  1554. /* UPE and MPE will be handled by normal PROMISC logic
  1555. * in e1000e_set_rx_mode
  1556. */
  1557. rctl |= (E1000_RCTL_SBP | /* Receive bad packets */
  1558. E1000_RCTL_BAM | /* RX All Bcast Pkts */
  1559. E1000_RCTL_PMCF); /* RX All MAC Ctrl Pkts */
  1560. rctl &= ~(E1000_RCTL_VFE | /* Disable VLAN filter */
  1561. E1000_RCTL_DPF | /* Allow filtered pause */
  1562. E1000_RCTL_CFIEN); /* Dis VLAN CFIEN Filter */
  1563. /* Do not mess with E1000_CTRL_VME, it affects transmit as well,
  1564. * and that breaks VLANs.
  1565. */
  1566. }
  1567. ew32(RCTL, rctl);
  1568. }
  1569. /**
  1570. * e1000_configure_rx - Configure 8254x Receive Unit after Reset
  1571. * @adapter: board private structure
  1572. *
  1573. * Configure the Rx unit of the MAC after a reset.
  1574. **/
  1575. static void e1000_configure_rx(struct e1000_adapter *adapter)
  1576. {
  1577. u64 rdba;
  1578. struct e1000_hw *hw = &adapter->hw;
  1579. u32 rdlen, rctl, rxcsum;
  1580. if (adapter->netdev->mtu > ETH_DATA_LEN) {
  1581. rdlen = adapter->rx_ring[0].count *
  1582. sizeof(struct e1000_rx_desc);
  1583. adapter->clean_rx = e1000_clean_jumbo_rx_irq;
  1584. adapter->alloc_rx_buf = e1000_alloc_jumbo_rx_buffers;
  1585. } else {
  1586. rdlen = adapter->rx_ring[0].count *
  1587. sizeof(struct e1000_rx_desc);
  1588. adapter->clean_rx = e1000_clean_rx_irq;
  1589. adapter->alloc_rx_buf = e1000_alloc_rx_buffers;
  1590. }
  1591. /* disable receives while setting up the descriptors */
  1592. rctl = er32(RCTL);
  1593. ew32(RCTL, rctl & ~E1000_RCTL_EN);
  1594. /* set the Receive Delay Timer Register */
  1595. ew32(RDTR, adapter->rx_int_delay);
  1596. if (hw->mac_type >= e1000_82540) {
  1597. ew32(RADV, adapter->rx_abs_int_delay);
  1598. if (adapter->itr_setting != 0)
  1599. ew32(ITR, 1000000000 / (adapter->itr * 256));
  1600. }
  1601. /* Setup the HW Rx Head and Tail Descriptor Pointers and
  1602. * the Base and Length of the Rx Descriptor Ring
  1603. */
  1604. switch (adapter->num_rx_queues) {
  1605. case 1:
  1606. default:
  1607. rdba = adapter->rx_ring[0].dma;
  1608. ew32(RDLEN, rdlen);
  1609. ew32(RDBAH, (rdba >> 32));
  1610. ew32(RDBAL, (rdba & 0x00000000ffffffffULL));
  1611. ew32(RDT, 0);
  1612. ew32(RDH, 0);
  1613. adapter->rx_ring[0].rdh = ((hw->mac_type >= e1000_82543) ?
  1614. E1000_RDH : E1000_82542_RDH);
  1615. adapter->rx_ring[0].rdt = ((hw->mac_type >= e1000_82543) ?
  1616. E1000_RDT : E1000_82542_RDT);
  1617. break;
  1618. }
  1619. /* Enable 82543 Receive Checksum Offload for TCP and UDP */
  1620. if (hw->mac_type >= e1000_82543) {
  1621. rxcsum = er32(RXCSUM);
  1622. if (adapter->rx_csum)
  1623. rxcsum |= E1000_RXCSUM_TUOFL;
  1624. else
  1625. /* don't need to clear IPPCSE as it defaults to 0 */
  1626. rxcsum &= ~E1000_RXCSUM_TUOFL;
  1627. ew32(RXCSUM, rxcsum);
  1628. }
  1629. /* Enable Receives */
  1630. ew32(RCTL, rctl | E1000_RCTL_EN);
  1631. }
  1632. /**
  1633. * e1000_free_tx_resources - Free Tx Resources per Queue
  1634. * @adapter: board private structure
  1635. * @tx_ring: Tx descriptor ring for a specific queue
  1636. *
  1637. * Free all transmit software resources
  1638. **/
  1639. static void e1000_free_tx_resources(struct e1000_adapter *adapter,
  1640. struct e1000_tx_ring *tx_ring)
  1641. {
  1642. struct pci_dev *pdev = adapter->pdev;
  1643. e1000_clean_tx_ring(adapter, tx_ring);
  1644. vfree(tx_ring->buffer_info);
  1645. tx_ring->buffer_info = NULL;
  1646. dma_free_coherent(&pdev->dev, tx_ring->size, tx_ring->desc,
  1647. tx_ring->dma);
  1648. tx_ring->desc = NULL;
  1649. }
  1650. /**
  1651. * e1000_free_all_tx_resources - Free Tx Resources for All Queues
  1652. * @adapter: board private structure
  1653. *
  1654. * Free all transmit software resources
  1655. **/
  1656. void e1000_free_all_tx_resources(struct e1000_adapter *adapter)
  1657. {
  1658. int i;
  1659. for (i = 0; i < adapter->num_tx_queues; i++)
  1660. e1000_free_tx_resources(adapter, &adapter->tx_ring[i]);
  1661. }
  1662. static void
  1663. e1000_unmap_and_free_tx_resource(struct e1000_adapter *adapter,
  1664. struct e1000_tx_buffer *buffer_info)
  1665. {
  1666. if (buffer_info->dma) {
  1667. if (buffer_info->mapped_as_page)
  1668. dma_unmap_page(&adapter->pdev->dev, buffer_info->dma,
  1669. buffer_info->length, DMA_TO_DEVICE);
  1670. else
  1671. dma_unmap_single(&adapter->pdev->dev, buffer_info->dma,
  1672. buffer_info->length,
  1673. DMA_TO_DEVICE);
  1674. buffer_info->dma = 0;
  1675. }
  1676. if (buffer_info->skb) {
  1677. dev_kfree_skb_any(buffer_info->skb);
  1678. buffer_info->skb = NULL;
  1679. }
  1680. buffer_info->time_stamp = 0;
  1681. /* buffer_info must be completely set up in the transmit path */
  1682. }
  1683. /**
  1684. * e1000_clean_tx_ring - Free Tx Buffers
  1685. * @adapter: board private structure
  1686. * @tx_ring: ring to be cleaned
  1687. **/
  1688. static void e1000_clean_tx_ring(struct e1000_adapter *adapter,
  1689. struct e1000_tx_ring *tx_ring)
  1690. {
  1691. struct e1000_hw *hw = &adapter->hw;
  1692. struct e1000_tx_buffer *buffer_info;
  1693. unsigned long size;
  1694. unsigned int i;
  1695. /* Free all the Tx ring sk_buffs */
  1696. for (i = 0; i < tx_ring->count; i++) {
  1697. buffer_info = &tx_ring->buffer_info[i];
  1698. e1000_unmap_and_free_tx_resource(adapter, buffer_info);
  1699. }
  1700. netdev_reset_queue(adapter->netdev);
  1701. size = sizeof(struct e1000_tx_buffer) * tx_ring->count;
  1702. memset(tx_ring->buffer_info, 0, size);
  1703. /* Zero out the descriptor ring */
  1704. memset(tx_ring->desc, 0, tx_ring->size);
  1705. tx_ring->next_to_use = 0;
  1706. tx_ring->next_to_clean = 0;
  1707. tx_ring->last_tx_tso = false;
  1708. writel(0, hw->hw_addr + tx_ring->tdh);
  1709. writel(0, hw->hw_addr + tx_ring->tdt);
  1710. }
  1711. /**
  1712. * e1000_clean_all_tx_rings - Free Tx Buffers for all queues
  1713. * @adapter: board private structure
  1714. **/
  1715. static void e1000_clean_all_tx_rings(struct e1000_adapter *adapter)
  1716. {
  1717. int i;
  1718. for (i = 0; i < adapter->num_tx_queues; i++)
  1719. e1000_clean_tx_ring(adapter, &adapter->tx_ring[i]);
  1720. }
  1721. /**
  1722. * e1000_free_rx_resources - Free Rx Resources
  1723. * @adapter: board private structure
  1724. * @rx_ring: ring to clean the resources from
  1725. *
  1726. * Free all receive software resources
  1727. **/
  1728. static void e1000_free_rx_resources(struct e1000_adapter *adapter,
  1729. struct e1000_rx_ring *rx_ring)
  1730. {
  1731. struct pci_dev *pdev = adapter->pdev;
  1732. e1000_clean_rx_ring(adapter, rx_ring);
  1733. vfree(rx_ring->buffer_info);
  1734. rx_ring->buffer_info = NULL;
  1735. dma_free_coherent(&pdev->dev, rx_ring->size, rx_ring->desc,
  1736. rx_ring->dma);
  1737. rx_ring->desc = NULL;
  1738. }
  1739. /**
  1740. * e1000_free_all_rx_resources - Free Rx Resources for All Queues
  1741. * @adapter: board private structure
  1742. *
  1743. * Free all receive software resources
  1744. **/
  1745. void e1000_free_all_rx_resources(struct e1000_adapter *adapter)
  1746. {
  1747. int i;
  1748. for (i = 0; i < adapter->num_rx_queues; i++)
  1749. e1000_free_rx_resources(adapter, &adapter->rx_ring[i]);
  1750. }
  1751. #define E1000_HEADROOM (NET_SKB_PAD + NET_IP_ALIGN)
  1752. static unsigned int e1000_frag_len(const struct e1000_adapter *a)
  1753. {
  1754. return SKB_DATA_ALIGN(a->rx_buffer_len + E1000_HEADROOM) +
  1755. SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
  1756. }
  1757. static void *e1000_alloc_frag(const struct e1000_adapter *a)
  1758. {
  1759. unsigned int len = e1000_frag_len(a);
  1760. u8 *data = netdev_alloc_frag(len);
  1761. if (likely(data))
  1762. data += E1000_HEADROOM;
  1763. return data;
  1764. }
  1765. /**
  1766. * e1000_clean_rx_ring - Free Rx Buffers per Queue
  1767. * @adapter: board private structure
  1768. * @rx_ring: ring to free buffers from
  1769. **/
  1770. static void e1000_clean_rx_ring(struct e1000_adapter *adapter,
  1771. struct e1000_rx_ring *rx_ring)
  1772. {
  1773. struct e1000_hw *hw = &adapter->hw;
  1774. struct e1000_rx_buffer *buffer_info;
  1775. struct pci_dev *pdev = adapter->pdev;
  1776. unsigned long size;
  1777. unsigned int i;
  1778. /* Free all the Rx netfrags */
  1779. for (i = 0; i < rx_ring->count; i++) {
  1780. buffer_info = &rx_ring->buffer_info[i];
  1781. if (adapter->clean_rx == e1000_clean_rx_irq) {
  1782. if (buffer_info->dma)
  1783. dma_unmap_single(&pdev->dev, buffer_info->dma,
  1784. adapter->rx_buffer_len,
  1785. DMA_FROM_DEVICE);
  1786. if (buffer_info->rxbuf.data) {
  1787. skb_free_frag(buffer_info->rxbuf.data);
  1788. buffer_info->rxbuf.data = NULL;
  1789. }
  1790. } else if (adapter->clean_rx == e1000_clean_jumbo_rx_irq) {
  1791. if (buffer_info->dma)
  1792. dma_unmap_page(&pdev->dev, buffer_info->dma,
  1793. adapter->rx_buffer_len,
  1794. DMA_FROM_DEVICE);
  1795. if (buffer_info->rxbuf.page) {
  1796. put_page(buffer_info->rxbuf.page);
  1797. buffer_info->rxbuf.page = NULL;
  1798. }
  1799. }
  1800. buffer_info->dma = 0;
  1801. }
  1802. /* there also may be some cached data from a chained receive */
  1803. napi_free_frags(&adapter->napi);
  1804. rx_ring->rx_skb_top = NULL;
  1805. size = sizeof(struct e1000_rx_buffer) * rx_ring->count;
  1806. memset(rx_ring->buffer_info, 0, size);
  1807. /* Zero out the descriptor ring */
  1808. memset(rx_ring->desc, 0, rx_ring->size);
  1809. rx_ring->next_to_clean = 0;
  1810. rx_ring->next_to_use = 0;
  1811. writel(0, hw->hw_addr + rx_ring->rdh);
  1812. writel(0, hw->hw_addr + rx_ring->rdt);
  1813. }
  1814. /**
  1815. * e1000_clean_all_rx_rings - Free Rx Buffers for all queues
  1816. * @adapter: board private structure
  1817. **/
  1818. static void e1000_clean_all_rx_rings(struct e1000_adapter *adapter)
  1819. {
  1820. int i;
  1821. for (i = 0; i < adapter->num_rx_queues; i++)
  1822. e1000_clean_rx_ring(adapter, &adapter->rx_ring[i]);
  1823. }
  1824. /* The 82542 2.0 (revision 2) needs to have the receive unit in reset
  1825. * and memory write and invalidate disabled for certain operations
  1826. */
  1827. static void e1000_enter_82542_rst(struct e1000_adapter *adapter)
  1828. {
  1829. struct e1000_hw *hw = &adapter->hw;
  1830. struct net_device *netdev = adapter->netdev;
  1831. u32 rctl;
  1832. e1000_pci_clear_mwi(hw);
  1833. rctl = er32(RCTL);
  1834. rctl |= E1000_RCTL_RST;
  1835. ew32(RCTL, rctl);
  1836. E1000_WRITE_FLUSH();
  1837. mdelay(5);
  1838. if (netif_running(netdev))
  1839. e1000_clean_all_rx_rings(adapter);
  1840. }
  1841. static void e1000_leave_82542_rst(struct e1000_adapter *adapter)
  1842. {
  1843. struct e1000_hw *hw = &adapter->hw;
  1844. struct net_device *netdev = adapter->netdev;
  1845. u32 rctl;
  1846. rctl = er32(RCTL);
  1847. rctl &= ~E1000_RCTL_RST;
  1848. ew32(RCTL, rctl);
  1849. E1000_WRITE_FLUSH();
  1850. mdelay(5);
  1851. if (hw->pci_cmd_word & PCI_COMMAND_INVALIDATE)
  1852. e1000_pci_set_mwi(hw);
  1853. if (netif_running(netdev)) {
  1854. /* No need to loop, because 82542 supports only 1 queue */
  1855. struct e1000_rx_ring *ring = &adapter->rx_ring[0];
  1856. e1000_configure_rx(adapter);
  1857. adapter->alloc_rx_buf(adapter, ring, E1000_DESC_UNUSED(ring));
  1858. }
  1859. }
  1860. /**
  1861. * e1000_set_mac - Change the Ethernet Address of the NIC
  1862. * @netdev: network interface device structure
  1863. * @p: pointer to an address structure
  1864. *
  1865. * Returns 0 on success, negative on failure
  1866. **/
  1867. static int e1000_set_mac(struct net_device *netdev, void *p)
  1868. {
  1869. struct e1000_adapter *adapter = netdev_priv(netdev);
  1870. struct e1000_hw *hw = &adapter->hw;
  1871. struct sockaddr *addr = p;
  1872. if (!is_valid_ether_addr(addr->sa_data))
  1873. return -EADDRNOTAVAIL;
  1874. /* 82542 2.0 needs to be in reset to write receive address registers */
  1875. if (hw->mac_type == e1000_82542_rev2_0)
  1876. e1000_enter_82542_rst(adapter);
  1877. memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
  1878. memcpy(hw->mac_addr, addr->sa_data, netdev->addr_len);
  1879. e1000_rar_set(hw, hw->mac_addr, 0);
  1880. if (hw->mac_type == e1000_82542_rev2_0)
  1881. e1000_leave_82542_rst(adapter);
  1882. return 0;
  1883. }
  1884. /**
  1885. * e1000_set_rx_mode - Secondary Unicast, Multicast and Promiscuous mode set
  1886. * @netdev: network interface device structure
  1887. *
  1888. * The set_rx_mode entry point is called whenever the unicast or multicast
  1889. * address lists or the network interface flags are updated. This routine is
  1890. * responsible for configuring the hardware for proper unicast, multicast,
  1891. * promiscuous mode, and all-multi behavior.
  1892. **/
  1893. static void e1000_set_rx_mode(struct net_device *netdev)
  1894. {
  1895. struct e1000_adapter *adapter = netdev_priv(netdev);
  1896. struct e1000_hw *hw = &adapter->hw;
  1897. struct netdev_hw_addr *ha;
  1898. bool use_uc = false;
  1899. u32 rctl;
  1900. u32 hash_value;
  1901. int i, rar_entries = E1000_RAR_ENTRIES;
  1902. int mta_reg_count = E1000_NUM_MTA_REGISTERS;
  1903. u32 *mcarray = kcalloc(mta_reg_count, sizeof(u32), GFP_ATOMIC);
  1904. if (!mcarray)
  1905. return;
  1906. /* Check for Promiscuous and All Multicast modes */
  1907. rctl = er32(RCTL);
  1908. if (netdev->flags & IFF_PROMISC) {
  1909. rctl |= (E1000_RCTL_UPE | E1000_RCTL_MPE);
  1910. rctl &= ~E1000_RCTL_VFE;
  1911. } else {
  1912. if (netdev->flags & IFF_ALLMULTI)
  1913. rctl |= E1000_RCTL_MPE;
  1914. else
  1915. rctl &= ~E1000_RCTL_MPE;
  1916. /* Enable VLAN filter if there is a VLAN */
  1917. if (e1000_vlan_used(adapter))
  1918. rctl |= E1000_RCTL_VFE;
  1919. }
  1920. if (netdev_uc_count(netdev) > rar_entries - 1) {
  1921. rctl |= E1000_RCTL_UPE;
  1922. } else if (!(netdev->flags & IFF_PROMISC)) {
  1923. rctl &= ~E1000_RCTL_UPE;
  1924. use_uc = true;
  1925. }
  1926. ew32(RCTL, rctl);
  1927. /* 82542 2.0 needs to be in reset to write receive address registers */
  1928. if (hw->mac_type == e1000_82542_rev2_0)
  1929. e1000_enter_82542_rst(adapter);
  1930. /* load the first 14 addresses into the exact filters 1-14. Unicast
  1931. * addresses take precedence to avoid disabling unicast filtering
  1932. * when possible.
  1933. *
  1934. * RAR 0 is used for the station MAC address
  1935. * if there are not 14 addresses, go ahead and clear the filters
  1936. */
  1937. i = 1;
  1938. if (use_uc)
  1939. netdev_for_each_uc_addr(ha, netdev) {
  1940. if (i == rar_entries)
  1941. break;
  1942. e1000_rar_set(hw, ha->addr, i++);
  1943. }
  1944. netdev_for_each_mc_addr(ha, netdev) {
  1945. if (i == rar_entries) {
  1946. /* load any remaining addresses into the hash table */
  1947. u32 hash_reg, hash_bit, mta;
  1948. hash_value = e1000_hash_mc_addr(hw, ha->addr);
  1949. hash_reg = (hash_value >> 5) & 0x7F;
  1950. hash_bit = hash_value & 0x1F;
  1951. mta = (1 << hash_bit);
  1952. mcarray[hash_reg] |= mta;
  1953. } else {
  1954. e1000_rar_set(hw, ha->addr, i++);
  1955. }
  1956. }
  1957. for (; i < rar_entries; i++) {
  1958. E1000_WRITE_REG_ARRAY(hw, RA, i << 1, 0);
  1959. E1000_WRITE_FLUSH();
  1960. E1000_WRITE_REG_ARRAY(hw, RA, (i << 1) + 1, 0);
  1961. E1000_WRITE_FLUSH();
  1962. }
  1963. /* write the hash table completely, write from bottom to avoid
  1964. * both stupid write combining chipsets, and flushing each write
  1965. */
  1966. for (i = mta_reg_count - 1; i >= 0 ; i--) {
  1967. /* If we are on an 82544 has an errata where writing odd
  1968. * offsets overwrites the previous even offset, but writing
  1969. * backwards over the range solves the issue by always
  1970. * writing the odd offset first
  1971. */
  1972. E1000_WRITE_REG_ARRAY(hw, MTA, i, mcarray[i]);
  1973. }
  1974. E1000_WRITE_FLUSH();
  1975. if (hw->mac_type == e1000_82542_rev2_0)
  1976. e1000_leave_82542_rst(adapter);
  1977. kfree(mcarray);
  1978. }
  1979. /**
  1980. * e1000_update_phy_info_task - get phy info
  1981. * @work: work struct contained inside adapter struct
  1982. *
  1983. * Need to wait a few seconds after link up to get diagnostic information from
  1984. * the phy
  1985. */
  1986. static void e1000_update_phy_info_task(struct work_struct *work)
  1987. {
  1988. struct e1000_adapter *adapter = container_of(work,
  1989. struct e1000_adapter,
  1990. phy_info_task.work);
  1991. e1000_phy_get_info(&adapter->hw, &adapter->phy_info);
  1992. }
  1993. /**
  1994. * e1000_82547_tx_fifo_stall_task - task to complete work
  1995. * @work: work struct contained inside adapter struct
  1996. **/
  1997. static void e1000_82547_tx_fifo_stall_task(struct work_struct *work)
  1998. {
  1999. struct e1000_adapter *adapter = container_of(work,
  2000. struct e1000_adapter,
  2001. fifo_stall_task.work);
  2002. struct e1000_hw *hw = &adapter->hw;
  2003. struct net_device *netdev = adapter->netdev;
  2004. u32 tctl;
  2005. if (atomic_read(&adapter->tx_fifo_stall)) {
  2006. if ((er32(TDT) == er32(TDH)) &&
  2007. (er32(TDFT) == er32(TDFH)) &&
  2008. (er32(TDFTS) == er32(TDFHS))) {
  2009. tctl = er32(TCTL);
  2010. ew32(TCTL, tctl & ~E1000_TCTL_EN);
  2011. ew32(TDFT, adapter->tx_head_addr);
  2012. ew32(TDFH, adapter->tx_head_addr);
  2013. ew32(TDFTS, adapter->tx_head_addr);
  2014. ew32(TDFHS, adapter->tx_head_addr);
  2015. ew32(TCTL, tctl);
  2016. E1000_WRITE_FLUSH();
  2017. adapter->tx_fifo_head = 0;
  2018. atomic_set(&adapter->tx_fifo_stall, 0);
  2019. netif_wake_queue(netdev);
  2020. } else if (!test_bit(__E1000_DOWN, &adapter->flags)) {
  2021. schedule_delayed_work(&adapter->fifo_stall_task, 1);
  2022. }
  2023. }
  2024. }
  2025. bool e1000_has_link(struct e1000_adapter *adapter)
  2026. {
  2027. struct e1000_hw *hw = &adapter->hw;
  2028. bool link_active = false;
  2029. /* get_link_status is set on LSC (link status) interrupt or rx
  2030. * sequence error interrupt (except on intel ce4100).
  2031. * get_link_status will stay false until the
  2032. * e1000_check_for_link establishes link for copper adapters
  2033. * ONLY
  2034. */
  2035. switch (hw->media_type) {
  2036. case e1000_media_type_copper:
  2037. if (hw->mac_type == e1000_ce4100)
  2038. hw->get_link_status = 1;
  2039. if (hw->get_link_status) {
  2040. e1000_check_for_link(hw);
  2041. link_active = !hw->get_link_status;
  2042. } else {
  2043. link_active = true;
  2044. }
  2045. break;
  2046. case e1000_media_type_fiber:
  2047. e1000_check_for_link(hw);
  2048. link_active = !!(er32(STATUS) & E1000_STATUS_LU);
  2049. break;
  2050. case e1000_media_type_internal_serdes:
  2051. e1000_check_for_link(hw);
  2052. link_active = hw->serdes_has_link;
  2053. break;
  2054. default:
  2055. break;
  2056. }
  2057. return link_active;
  2058. }
  2059. /**
  2060. * e1000_watchdog - work function
  2061. * @work: work struct contained inside adapter struct
  2062. **/
  2063. static void e1000_watchdog(struct work_struct *work)
  2064. {
  2065. struct e1000_adapter *adapter = container_of(work,
  2066. struct e1000_adapter,
  2067. watchdog_task.work);
  2068. struct e1000_hw *hw = &adapter->hw;
  2069. struct net_device *netdev = adapter->netdev;
  2070. struct e1000_tx_ring *txdr = adapter->tx_ring;
  2071. u32 link, tctl;
  2072. link = e1000_has_link(adapter);
  2073. if ((netif_carrier_ok(netdev)) && link)
  2074. goto link_up;
  2075. if (link) {
  2076. if (!netif_carrier_ok(netdev)) {
  2077. u32 ctrl;
  2078. bool txb2b = true;
  2079. /* update snapshot of PHY registers on LSC */
  2080. e1000_get_speed_and_duplex(hw,
  2081. &adapter->link_speed,
  2082. &adapter->link_duplex);
  2083. ctrl = er32(CTRL);
  2084. pr_info("%s NIC Link is Up %d Mbps %s, "
  2085. "Flow Control: %s\n",
  2086. netdev->name,
  2087. adapter->link_speed,
  2088. adapter->link_duplex == FULL_DUPLEX ?
  2089. "Full Duplex" : "Half Duplex",
  2090. ((ctrl & E1000_CTRL_TFCE) && (ctrl &
  2091. E1000_CTRL_RFCE)) ? "RX/TX" : ((ctrl &
  2092. E1000_CTRL_RFCE) ? "RX" : ((ctrl &
  2093. E1000_CTRL_TFCE) ? "TX" : "None")));
  2094. /* adjust timeout factor according to speed/duplex */
  2095. adapter->tx_timeout_factor = 1;
  2096. switch (adapter->link_speed) {
  2097. case SPEED_10:
  2098. txb2b = false;
  2099. adapter->tx_timeout_factor = 16;
  2100. break;
  2101. case SPEED_100:
  2102. txb2b = false;
  2103. /* maybe add some timeout factor ? */
  2104. break;
  2105. }
  2106. /* enable transmits in the hardware */
  2107. tctl = er32(TCTL);
  2108. tctl |= E1000_TCTL_EN;
  2109. ew32(TCTL, tctl);
  2110. netif_carrier_on(netdev);
  2111. if (!test_bit(__E1000_DOWN, &adapter->flags))
  2112. schedule_delayed_work(&adapter->phy_info_task,
  2113. 2 * HZ);
  2114. adapter->smartspeed = 0;
  2115. }
  2116. } else {
  2117. if (netif_carrier_ok(netdev)) {
  2118. adapter->link_speed = 0;
  2119. adapter->link_duplex = 0;
  2120. pr_info("%s NIC Link is Down\n",
  2121. netdev->name);
  2122. netif_carrier_off(netdev);
  2123. if (!test_bit(__E1000_DOWN, &adapter->flags))
  2124. schedule_delayed_work(&adapter->phy_info_task,
  2125. 2 * HZ);
  2126. }
  2127. e1000_smartspeed(adapter);
  2128. }
  2129. link_up:
  2130. e1000_update_stats(adapter);
  2131. hw->tx_packet_delta = adapter->stats.tpt - adapter->tpt_old;
  2132. adapter->tpt_old = adapter->stats.tpt;
  2133. hw->collision_delta = adapter->stats.colc - adapter->colc_old;
  2134. adapter->colc_old = adapter->stats.colc;
  2135. adapter->gorcl = adapter->stats.gorcl - adapter->gorcl_old;
  2136. adapter->gorcl_old = adapter->stats.gorcl;
  2137. adapter->gotcl = adapter->stats.gotcl - adapter->gotcl_old;
  2138. adapter->gotcl_old = adapter->stats.gotcl;
  2139. e1000_update_adaptive(hw);
  2140. if (!netif_carrier_ok(netdev)) {
  2141. if (E1000_DESC_UNUSED(txdr) + 1 < txdr->count) {
  2142. /* We've lost link, so the controller stops DMA,
  2143. * but we've got queued Tx work that's never going
  2144. * to get done, so reset controller to flush Tx.
  2145. * (Do the reset outside of interrupt context).
  2146. */
  2147. adapter->tx_timeout_count++;
  2148. schedule_work(&adapter->reset_task);
  2149. /* exit immediately since reset is imminent */
  2150. return;
  2151. }
  2152. }
  2153. /* Simple mode for Interrupt Throttle Rate (ITR) */
  2154. if (hw->mac_type >= e1000_82540 && adapter->itr_setting == 4) {
  2155. /* Symmetric Tx/Rx gets a reduced ITR=2000;
  2156. * Total asymmetrical Tx or Rx gets ITR=8000;
  2157. * everyone else is between 2000-8000.
  2158. */
  2159. u32 goc = (adapter->gotcl + adapter->gorcl) / 10000;
  2160. u32 dif = (adapter->gotcl > adapter->gorcl ?
  2161. adapter->gotcl - adapter->gorcl :
  2162. adapter->gorcl - adapter->gotcl) / 10000;
  2163. u32 itr = goc > 0 ? (dif * 6000 / goc + 2000) : 8000;
  2164. ew32(ITR, 1000000000 / (itr * 256));
  2165. }
  2166. /* Cause software interrupt to ensure rx ring is cleaned */
  2167. ew32(ICS, E1000_ICS_RXDMT0);
  2168. /* Force detection of hung controller every watchdog period */
  2169. adapter->detect_tx_hung = true;
  2170. /* Reschedule the task */
  2171. if (!test_bit(__E1000_DOWN, &adapter->flags))
  2172. schedule_delayed_work(&adapter->watchdog_task, 2 * HZ);
  2173. }
  2174. enum latency_range {
  2175. lowest_latency = 0,
  2176. low_latency = 1,
  2177. bulk_latency = 2,
  2178. latency_invalid = 255
  2179. };
  2180. /**
  2181. * e1000_update_itr - update the dynamic ITR value based on statistics
  2182. * @adapter: pointer to adapter
  2183. * @itr_setting: current adapter->itr
  2184. * @packets: the number of packets during this measurement interval
  2185. * @bytes: the number of bytes during this measurement interval
  2186. *
  2187. * Stores a new ITR value based on packets and byte
  2188. * counts during the last interrupt. The advantage of per interrupt
  2189. * computation is faster updates and more accurate ITR for the current
  2190. * traffic pattern. Constants in this function were computed
  2191. * based on theoretical maximum wire speed and thresholds were set based
  2192. * on testing data as well as attempting to minimize response time
  2193. * while increasing bulk throughput.
  2194. * this functionality is controlled by the InterruptThrottleRate module
  2195. * parameter (see e1000_param.c)
  2196. **/
  2197. static unsigned int e1000_update_itr(struct e1000_adapter *adapter,
  2198. u16 itr_setting, int packets, int bytes)
  2199. {
  2200. unsigned int retval = itr_setting;
  2201. struct e1000_hw *hw = &adapter->hw;
  2202. if (unlikely(hw->mac_type < e1000_82540))
  2203. goto update_itr_done;
  2204. if (packets == 0)
  2205. goto update_itr_done;
  2206. switch (itr_setting) {
  2207. case lowest_latency:
  2208. /* jumbo frames get bulk treatment*/
  2209. if (bytes/packets > 8000)
  2210. retval = bulk_latency;
  2211. else if ((packets < 5) && (bytes > 512))
  2212. retval = low_latency;
  2213. break;
  2214. case low_latency: /* 50 usec aka 20000 ints/s */
  2215. if (bytes > 10000) {
  2216. /* jumbo frames need bulk latency setting */
  2217. if (bytes/packets > 8000)
  2218. retval = bulk_latency;
  2219. else if ((packets < 10) || ((bytes/packets) > 1200))
  2220. retval = bulk_latency;
  2221. else if ((packets > 35))
  2222. retval = lowest_latency;
  2223. } else if (bytes/packets > 2000)
  2224. retval = bulk_latency;
  2225. else if (packets <= 2 && bytes < 512)
  2226. retval = lowest_latency;
  2227. break;
  2228. case bulk_latency: /* 250 usec aka 4000 ints/s */
  2229. if (bytes > 25000) {
  2230. if (packets > 35)
  2231. retval = low_latency;
  2232. } else if (bytes < 6000) {
  2233. retval = low_latency;
  2234. }
  2235. break;
  2236. }
  2237. update_itr_done:
  2238. return retval;
  2239. }
  2240. static void e1000_set_itr(struct e1000_adapter *adapter)
  2241. {
  2242. struct e1000_hw *hw = &adapter->hw;
  2243. u16 current_itr;
  2244. u32 new_itr = adapter->itr;
  2245. if (unlikely(hw->mac_type < e1000_82540))
  2246. return;
  2247. /* for non-gigabit speeds, just fix the interrupt rate at 4000 */
  2248. if (unlikely(adapter->link_speed != SPEED_1000)) {
  2249. current_itr = 0;
  2250. new_itr = 4000;
  2251. goto set_itr_now;
  2252. }
  2253. adapter->tx_itr = e1000_update_itr(adapter, adapter->tx_itr,
  2254. adapter->total_tx_packets,
  2255. adapter->total_tx_bytes);
  2256. /* conservative mode (itr 3) eliminates the lowest_latency setting */
  2257. if (adapter->itr_setting == 3 && adapter->tx_itr == lowest_latency)
  2258. adapter->tx_itr = low_latency;
  2259. adapter->rx_itr = e1000_update_itr(adapter, adapter->rx_itr,
  2260. adapter->total_rx_packets,
  2261. adapter->total_rx_bytes);
  2262. /* conservative mode (itr 3) eliminates the lowest_latency setting */
  2263. if (adapter->itr_setting == 3 && adapter->rx_itr == lowest_latency)
  2264. adapter->rx_itr = low_latency;
  2265. current_itr = max(adapter->rx_itr, adapter->tx_itr);
  2266. switch (current_itr) {
  2267. /* counts and packets in update_itr are dependent on these numbers */
  2268. case lowest_latency:
  2269. new_itr = 70000;
  2270. break;
  2271. case low_latency:
  2272. new_itr = 20000; /* aka hwitr = ~200 */
  2273. break;
  2274. case bulk_latency:
  2275. new_itr = 4000;
  2276. break;
  2277. default:
  2278. break;
  2279. }
  2280. set_itr_now:
  2281. if (new_itr != adapter->itr) {
  2282. /* this attempts to bias the interrupt rate towards Bulk
  2283. * by adding intermediate steps when interrupt rate is
  2284. * increasing
  2285. */
  2286. new_itr = new_itr > adapter->itr ?
  2287. min(adapter->itr + (new_itr >> 2), new_itr) :
  2288. new_itr;
  2289. adapter->itr = new_itr;
  2290. ew32(ITR, 1000000000 / (new_itr * 256));
  2291. }
  2292. }
  2293. #define E1000_TX_FLAGS_CSUM 0x00000001
  2294. #define E1000_TX_FLAGS_VLAN 0x00000002
  2295. #define E1000_TX_FLAGS_TSO 0x00000004
  2296. #define E1000_TX_FLAGS_IPV4 0x00000008
  2297. #define E1000_TX_FLAGS_NO_FCS 0x00000010
  2298. #define E1000_TX_FLAGS_VLAN_MASK 0xffff0000
  2299. #define E1000_TX_FLAGS_VLAN_SHIFT 16
  2300. static int e1000_tso(struct e1000_adapter *adapter,
  2301. struct e1000_tx_ring *tx_ring, struct sk_buff *skb,
  2302. __be16 protocol)
  2303. {
  2304. struct e1000_context_desc *context_desc;
  2305. struct e1000_tx_buffer *buffer_info;
  2306. unsigned int i;
  2307. u32 cmd_length = 0;
  2308. u16 ipcse = 0, tucse, mss;
  2309. u8 ipcss, ipcso, tucss, tucso, hdr_len;
  2310. if (skb_is_gso(skb)) {
  2311. int err;
  2312. err = skb_cow_head(skb, 0);
  2313. if (err < 0)
  2314. return err;
  2315. hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
  2316. mss = skb_shinfo(skb)->gso_size;
  2317. if (protocol == htons(ETH_P_IP)) {
  2318. struct iphdr *iph = ip_hdr(skb);
  2319. iph->tot_len = 0;
  2320. iph->check = 0;
  2321. tcp_hdr(skb)->check = ~csum_tcpudp_magic(iph->saddr,
  2322. iph->daddr, 0,
  2323. IPPROTO_TCP,
  2324. 0);
  2325. cmd_length = E1000_TXD_CMD_IP;
  2326. ipcse = skb_transport_offset(skb) - 1;
  2327. } else if (skb_is_gso_v6(skb)) {
  2328. ipv6_hdr(skb)->payload_len = 0;
  2329. tcp_hdr(skb)->check =
  2330. ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
  2331. &ipv6_hdr(skb)->daddr,
  2332. 0, IPPROTO_TCP, 0);
  2333. ipcse = 0;
  2334. }
  2335. ipcss = skb_network_offset(skb);
  2336. ipcso = (void *)&(ip_hdr(skb)->check) - (void *)skb->data;
  2337. tucss = skb_transport_offset(skb);
  2338. tucso = (void *)&(tcp_hdr(skb)->check) - (void *)skb->data;
  2339. tucse = 0;
  2340. cmd_length |= (E1000_TXD_CMD_DEXT | E1000_TXD_CMD_TSE |
  2341. E1000_TXD_CMD_TCP | (skb->len - (hdr_len)));
  2342. i = tx_ring->next_to_use;
  2343. context_desc = E1000_CONTEXT_DESC(*tx_ring, i);
  2344. buffer_info = &tx_ring->buffer_info[i];
  2345. context_desc->lower_setup.ip_fields.ipcss = ipcss;
  2346. context_desc->lower_setup.ip_fields.ipcso = ipcso;
  2347. context_desc->lower_setup.ip_fields.ipcse = cpu_to_le16(ipcse);
  2348. context_desc->upper_setup.tcp_fields.tucss = tucss;
  2349. context_desc->upper_setup.tcp_fields.tucso = tucso;
  2350. context_desc->upper_setup.tcp_fields.tucse = cpu_to_le16(tucse);
  2351. context_desc->tcp_seg_setup.fields.mss = cpu_to_le16(mss);
  2352. context_desc->tcp_seg_setup.fields.hdr_len = hdr_len;
  2353. context_desc->cmd_and_length = cpu_to_le32(cmd_length);
  2354. buffer_info->time_stamp = jiffies;
  2355. buffer_info->next_to_watch = i;
  2356. if (++i == tx_ring->count)
  2357. i = 0;
  2358. tx_ring->next_to_use = i;
  2359. return true;
  2360. }
  2361. return false;
  2362. }
  2363. static bool e1000_tx_csum(struct e1000_adapter *adapter,
  2364. struct e1000_tx_ring *tx_ring, struct sk_buff *skb,
  2365. __be16 protocol)
  2366. {
  2367. struct e1000_context_desc *context_desc;
  2368. struct e1000_tx_buffer *buffer_info;
  2369. unsigned int i;
  2370. u8 css;
  2371. u32 cmd_len = E1000_TXD_CMD_DEXT;
  2372. if (skb->ip_summed != CHECKSUM_PARTIAL)
  2373. return false;
  2374. switch (protocol) {
  2375. case cpu_to_be16(ETH_P_IP):
  2376. if (ip_hdr(skb)->protocol == IPPROTO_TCP)
  2377. cmd_len |= E1000_TXD_CMD_TCP;
  2378. break;
  2379. case cpu_to_be16(ETH_P_IPV6):
  2380. /* XXX not handling all IPV6 headers */
  2381. if (ipv6_hdr(skb)->nexthdr == IPPROTO_TCP)
  2382. cmd_len |= E1000_TXD_CMD_TCP;
  2383. break;
  2384. default:
  2385. if (unlikely(net_ratelimit()))
  2386. e_warn(drv, "checksum_partial proto=%x!\n",
  2387. skb->protocol);
  2388. break;
  2389. }
  2390. css = skb_checksum_start_offset(skb);
  2391. i = tx_ring->next_to_use;
  2392. buffer_info = &tx_ring->buffer_info[i];
  2393. context_desc = E1000_CONTEXT_DESC(*tx_ring, i);
  2394. context_desc->lower_setup.ip_config = 0;
  2395. context_desc->upper_setup.tcp_fields.tucss = css;
  2396. context_desc->upper_setup.tcp_fields.tucso =
  2397. css + skb->csum_offset;
  2398. context_desc->upper_setup.tcp_fields.tucse = 0;
  2399. context_desc->tcp_seg_setup.data = 0;
  2400. context_desc->cmd_and_length = cpu_to_le32(cmd_len);
  2401. buffer_info->time_stamp = jiffies;
  2402. buffer_info->next_to_watch = i;
  2403. if (unlikely(++i == tx_ring->count))
  2404. i = 0;
  2405. tx_ring->next_to_use = i;
  2406. return true;
  2407. }
  2408. #define E1000_MAX_TXD_PWR 12
  2409. #define E1000_MAX_DATA_PER_TXD (1<<E1000_MAX_TXD_PWR)
  2410. static int e1000_tx_map(struct e1000_adapter *adapter,
  2411. struct e1000_tx_ring *tx_ring,
  2412. struct sk_buff *skb, unsigned int first,
  2413. unsigned int max_per_txd, unsigned int nr_frags,
  2414. unsigned int mss)
  2415. {
  2416. struct e1000_hw *hw = &adapter->hw;
  2417. struct pci_dev *pdev = adapter->pdev;
  2418. struct e1000_tx_buffer *buffer_info;
  2419. unsigned int len = skb_headlen(skb);
  2420. unsigned int offset = 0, size, count = 0, i;
  2421. unsigned int f, bytecount, segs;
  2422. i = tx_ring->next_to_use;
  2423. while (len) {
  2424. buffer_info = &tx_ring->buffer_info[i];
  2425. size = min(len, max_per_txd);
  2426. /* Workaround for Controller erratum --
  2427. * descriptor for non-tso packet in a linear SKB that follows a
  2428. * tso gets written back prematurely before the data is fully
  2429. * DMA'd to the controller
  2430. */
  2431. if (!skb->data_len && tx_ring->last_tx_tso &&
  2432. !skb_is_gso(skb)) {
  2433. tx_ring->last_tx_tso = false;
  2434. size -= 4;
  2435. }
  2436. /* Workaround for premature desc write-backs
  2437. * in TSO mode. Append 4-byte sentinel desc
  2438. */
  2439. if (unlikely(mss && !nr_frags && size == len && size > 8))
  2440. size -= 4;
  2441. /* work-around for errata 10 and it applies
  2442. * to all controllers in PCI-X mode
  2443. * The fix is to make sure that the first descriptor of a
  2444. * packet is smaller than 2048 - 16 - 16 (or 2016) bytes
  2445. */
  2446. if (unlikely((hw->bus_type == e1000_bus_type_pcix) &&
  2447. (size > 2015) && count == 0))
  2448. size = 2015;
  2449. /* Workaround for potential 82544 hang in PCI-X. Avoid
  2450. * terminating buffers within evenly-aligned dwords.
  2451. */
  2452. if (unlikely(adapter->pcix_82544 &&
  2453. !((unsigned long)(skb->data + offset + size - 1) & 4) &&
  2454. size > 4))
  2455. size -= 4;
  2456. buffer_info->length = size;
  2457. /* set time_stamp *before* dma to help avoid a possible race */
  2458. buffer_info->time_stamp = jiffies;
  2459. buffer_info->mapped_as_page = false;
  2460. buffer_info->dma = dma_map_single(&pdev->dev,
  2461. skb->data + offset,
  2462. size, DMA_TO_DEVICE);
  2463. if (dma_mapping_error(&pdev->dev, buffer_info->dma))
  2464. goto dma_error;
  2465. buffer_info->next_to_watch = i;
  2466. len -= size;
  2467. offset += size;
  2468. count++;
  2469. if (len) {
  2470. i++;
  2471. if (unlikely(i == tx_ring->count))
  2472. i = 0;
  2473. }
  2474. }
  2475. for (f = 0; f < nr_frags; f++) {
  2476. const struct skb_frag_struct *frag;
  2477. frag = &skb_shinfo(skb)->frags[f];
  2478. len = skb_frag_size(frag);
  2479. offset = 0;
  2480. while (len) {
  2481. unsigned long bufend;
  2482. i++;
  2483. if (unlikely(i == tx_ring->count))
  2484. i = 0;
  2485. buffer_info = &tx_ring->buffer_info[i];
  2486. size = min(len, max_per_txd);
  2487. /* Workaround for premature desc write-backs
  2488. * in TSO mode. Append 4-byte sentinel desc
  2489. */
  2490. if (unlikely(mss && f == (nr_frags-1) &&
  2491. size == len && size > 8))
  2492. size -= 4;
  2493. /* Workaround for potential 82544 hang in PCI-X.
  2494. * Avoid terminating buffers within evenly-aligned
  2495. * dwords.
  2496. */
  2497. bufend = (unsigned long)
  2498. page_to_phys(skb_frag_page(frag));
  2499. bufend += offset + size - 1;
  2500. if (unlikely(adapter->pcix_82544 &&
  2501. !(bufend & 4) &&
  2502. size > 4))
  2503. size -= 4;
  2504. buffer_info->length = size;
  2505. buffer_info->time_stamp = jiffies;
  2506. buffer_info->mapped_as_page = true;
  2507. buffer_info->dma = skb_frag_dma_map(&pdev->dev, frag,
  2508. offset, size, DMA_TO_DEVICE);
  2509. if (dma_mapping_error(&pdev->dev, buffer_info->dma))
  2510. goto dma_error;
  2511. buffer_info->next_to_watch = i;
  2512. len -= size;
  2513. offset += size;
  2514. count++;
  2515. }
  2516. }
  2517. segs = skb_shinfo(skb)->gso_segs ?: 1;
  2518. /* multiply data chunks by size of headers */
  2519. bytecount = ((segs - 1) * skb_headlen(skb)) + skb->len;
  2520. tx_ring->buffer_info[i].skb = skb;
  2521. tx_ring->buffer_info[i].segs = segs;
  2522. tx_ring->buffer_info[i].bytecount = bytecount;
  2523. tx_ring->buffer_info[first].next_to_watch = i;
  2524. return count;
  2525. dma_error:
  2526. dev_err(&pdev->dev, "TX DMA map failed\n");
  2527. buffer_info->dma = 0;
  2528. if (count)
  2529. count--;
  2530. while (count--) {
  2531. if (i == 0)
  2532. i += tx_ring->count;
  2533. i--;
  2534. buffer_info = &tx_ring->buffer_info[i];
  2535. e1000_unmap_and_free_tx_resource(adapter, buffer_info);
  2536. }
  2537. return 0;
  2538. }
  2539. static void e1000_tx_queue(struct e1000_adapter *adapter,
  2540. struct e1000_tx_ring *tx_ring, int tx_flags,
  2541. int count)
  2542. {
  2543. struct e1000_tx_desc *tx_desc = NULL;
  2544. struct e1000_tx_buffer *buffer_info;
  2545. u32 txd_upper = 0, txd_lower = E1000_TXD_CMD_IFCS;
  2546. unsigned int i;
  2547. if (likely(tx_flags & E1000_TX_FLAGS_TSO)) {
  2548. txd_lower |= E1000_TXD_CMD_DEXT | E1000_TXD_DTYP_D |
  2549. E1000_TXD_CMD_TSE;
  2550. txd_upper |= E1000_TXD_POPTS_TXSM << 8;
  2551. if (likely(tx_flags & E1000_TX_FLAGS_IPV4))
  2552. txd_upper |= E1000_TXD_POPTS_IXSM << 8;
  2553. }
  2554. if (likely(tx_flags & E1000_TX_FLAGS_CSUM)) {
  2555. txd_lower |= E1000_TXD_CMD_DEXT | E1000_TXD_DTYP_D;
  2556. txd_upper |= E1000_TXD_POPTS_TXSM << 8;
  2557. }
  2558. if (unlikely(tx_flags & E1000_TX_FLAGS_VLAN)) {
  2559. txd_lower |= E1000_TXD_CMD_VLE;
  2560. txd_upper |= (tx_flags & E1000_TX_FLAGS_VLAN_MASK);
  2561. }
  2562. if (unlikely(tx_flags & E1000_TX_FLAGS_NO_FCS))
  2563. txd_lower &= ~(E1000_TXD_CMD_IFCS);
  2564. i = tx_ring->next_to_use;
  2565. while (count--) {
  2566. buffer_info = &tx_ring->buffer_info[i];
  2567. tx_desc = E1000_TX_DESC(*tx_ring, i);
  2568. tx_desc->buffer_addr = cpu_to_le64(buffer_info->dma);
  2569. tx_desc->lower.data =
  2570. cpu_to_le32(txd_lower | buffer_info->length);
  2571. tx_desc->upper.data = cpu_to_le32(txd_upper);
  2572. if (unlikely(++i == tx_ring->count))
  2573. i = 0;
  2574. }
  2575. tx_desc->lower.data |= cpu_to_le32(adapter->txd_cmd);
  2576. /* txd_cmd re-enables FCS, so we'll re-disable it here as desired. */
  2577. if (unlikely(tx_flags & E1000_TX_FLAGS_NO_FCS))
  2578. tx_desc->lower.data &= ~(cpu_to_le32(E1000_TXD_CMD_IFCS));
  2579. /* Force memory writes to complete before letting h/w
  2580. * know there are new descriptors to fetch. (Only
  2581. * applicable for weak-ordered memory model archs,
  2582. * such as IA-64).
  2583. */
  2584. wmb();
  2585. tx_ring->next_to_use = i;
  2586. }
  2587. /* 82547 workaround to avoid controller hang in half-duplex environment.
  2588. * The workaround is to avoid queuing a large packet that would span
  2589. * the internal Tx FIFO ring boundary by notifying the stack to resend
  2590. * the packet at a later time. This gives the Tx FIFO an opportunity to
  2591. * flush all packets. When that occurs, we reset the Tx FIFO pointers
  2592. * to the beginning of the Tx FIFO.
  2593. */
  2594. #define E1000_FIFO_HDR 0x10
  2595. #define E1000_82547_PAD_LEN 0x3E0
  2596. static int e1000_82547_fifo_workaround(struct e1000_adapter *adapter,
  2597. struct sk_buff *skb)
  2598. {
  2599. u32 fifo_space = adapter->tx_fifo_size - adapter->tx_fifo_head;
  2600. u32 skb_fifo_len = skb->len + E1000_FIFO_HDR;
  2601. skb_fifo_len = ALIGN(skb_fifo_len, E1000_FIFO_HDR);
  2602. if (adapter->link_duplex != HALF_DUPLEX)
  2603. goto no_fifo_stall_required;
  2604. if (atomic_read(&adapter->tx_fifo_stall))
  2605. return 1;
  2606. if (skb_fifo_len >= (E1000_82547_PAD_LEN + fifo_space)) {
  2607. atomic_set(&adapter->tx_fifo_stall, 1);
  2608. return 1;
  2609. }
  2610. no_fifo_stall_required:
  2611. adapter->tx_fifo_head += skb_fifo_len;
  2612. if (adapter->tx_fifo_head >= adapter->tx_fifo_size)
  2613. adapter->tx_fifo_head -= adapter->tx_fifo_size;
  2614. return 0;
  2615. }
  2616. static int __e1000_maybe_stop_tx(struct net_device *netdev, int size)
  2617. {
  2618. struct e1000_adapter *adapter = netdev_priv(netdev);
  2619. struct e1000_tx_ring *tx_ring = adapter->tx_ring;
  2620. netif_stop_queue(netdev);
  2621. /* Herbert's original patch had:
  2622. * smp_mb__after_netif_stop_queue();
  2623. * but since that doesn't exist yet, just open code it.
  2624. */
  2625. smp_mb();
  2626. /* We need to check again in a case another CPU has just
  2627. * made room available.
  2628. */
  2629. if (likely(E1000_DESC_UNUSED(tx_ring) < size))
  2630. return -EBUSY;
  2631. /* A reprieve! */
  2632. netif_start_queue(netdev);
  2633. ++adapter->restart_queue;
  2634. return 0;
  2635. }
  2636. static int e1000_maybe_stop_tx(struct net_device *netdev,
  2637. struct e1000_tx_ring *tx_ring, int size)
  2638. {
  2639. if (likely(E1000_DESC_UNUSED(tx_ring) >= size))
  2640. return 0;
  2641. return __e1000_maybe_stop_tx(netdev, size);
  2642. }
  2643. #define TXD_USE_COUNT(S, X) (((S) + ((1 << (X)) - 1)) >> (X))
  2644. static netdev_tx_t e1000_xmit_frame(struct sk_buff *skb,
  2645. struct net_device *netdev)
  2646. {
  2647. struct e1000_adapter *adapter = netdev_priv(netdev);
  2648. struct e1000_hw *hw = &adapter->hw;
  2649. struct e1000_tx_ring *tx_ring;
  2650. unsigned int first, max_per_txd = E1000_MAX_DATA_PER_TXD;
  2651. unsigned int max_txd_pwr = E1000_MAX_TXD_PWR;
  2652. unsigned int tx_flags = 0;
  2653. unsigned int len = skb_headlen(skb);
  2654. unsigned int nr_frags;
  2655. unsigned int mss;
  2656. int count = 0;
  2657. int tso;
  2658. unsigned int f;
  2659. __be16 protocol = vlan_get_protocol(skb);
  2660. /* This goes back to the question of how to logically map a Tx queue
  2661. * to a flow. Right now, performance is impacted slightly negatively
  2662. * if using multiple Tx queues. If the stack breaks away from a
  2663. * single qdisc implementation, we can look at this again.
  2664. */
  2665. tx_ring = adapter->tx_ring;
  2666. /* On PCI/PCI-X HW, if packet size is less than ETH_ZLEN,
  2667. * packets may get corrupted during padding by HW.
  2668. * To WA this issue, pad all small packets manually.
  2669. */
  2670. if (eth_skb_pad(skb))
  2671. return NETDEV_TX_OK;
  2672. mss = skb_shinfo(skb)->gso_size;
  2673. /* The controller does a simple calculation to
  2674. * make sure there is enough room in the FIFO before
  2675. * initiating the DMA for each buffer. The calc is:
  2676. * 4 = ceil(buffer len/mss). To make sure we don't
  2677. * overrun the FIFO, adjust the max buffer len if mss
  2678. * drops.
  2679. */
  2680. if (mss) {
  2681. u8 hdr_len;
  2682. max_per_txd = min(mss << 2, max_per_txd);
  2683. max_txd_pwr = fls(max_per_txd) - 1;
  2684. hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
  2685. if (skb->data_len && hdr_len == len) {
  2686. switch (hw->mac_type) {
  2687. unsigned int pull_size;
  2688. case e1000_82544:
  2689. /* Make sure we have room to chop off 4 bytes,
  2690. * and that the end alignment will work out to
  2691. * this hardware's requirements
  2692. * NOTE: this is a TSO only workaround
  2693. * if end byte alignment not correct move us
  2694. * into the next dword
  2695. */
  2696. if ((unsigned long)(skb_tail_pointer(skb) - 1)
  2697. & 4)
  2698. break;
  2699. /* fall through */
  2700. pull_size = min((unsigned int)4, skb->data_len);
  2701. if (!__pskb_pull_tail(skb, pull_size)) {
  2702. e_err(drv, "__pskb_pull_tail "
  2703. "failed.\n");
  2704. dev_kfree_skb_any(skb);
  2705. return NETDEV_TX_OK;
  2706. }
  2707. len = skb_headlen(skb);
  2708. break;
  2709. default:
  2710. /* do nothing */
  2711. break;
  2712. }
  2713. }
  2714. }
  2715. /* reserve a descriptor for the offload context */
  2716. if ((mss) || (skb->ip_summed == CHECKSUM_PARTIAL))
  2717. count++;
  2718. count++;
  2719. /* Controller Erratum workaround */
  2720. if (!skb->data_len && tx_ring->last_tx_tso && !skb_is_gso(skb))
  2721. count++;
  2722. count += TXD_USE_COUNT(len, max_txd_pwr);
  2723. if (adapter->pcix_82544)
  2724. count++;
  2725. /* work-around for errata 10 and it applies to all controllers
  2726. * in PCI-X mode, so add one more descriptor to the count
  2727. */
  2728. if (unlikely((hw->bus_type == e1000_bus_type_pcix) &&
  2729. (len > 2015)))
  2730. count++;
  2731. nr_frags = skb_shinfo(skb)->nr_frags;
  2732. for (f = 0; f < nr_frags; f++)
  2733. count += TXD_USE_COUNT(skb_frag_size(&skb_shinfo(skb)->frags[f]),
  2734. max_txd_pwr);
  2735. if (adapter->pcix_82544)
  2736. count += nr_frags;
  2737. /* need: count + 2 desc gap to keep tail from touching
  2738. * head, otherwise try next time
  2739. */
  2740. if (unlikely(e1000_maybe_stop_tx(netdev, tx_ring, count + 2)))
  2741. return NETDEV_TX_BUSY;
  2742. if (unlikely((hw->mac_type == e1000_82547) &&
  2743. (e1000_82547_fifo_workaround(adapter, skb)))) {
  2744. netif_stop_queue(netdev);
  2745. if (!test_bit(__E1000_DOWN, &adapter->flags))
  2746. schedule_delayed_work(&adapter->fifo_stall_task, 1);
  2747. return NETDEV_TX_BUSY;
  2748. }
  2749. if (skb_vlan_tag_present(skb)) {
  2750. tx_flags |= E1000_TX_FLAGS_VLAN;
  2751. tx_flags |= (skb_vlan_tag_get(skb) <<
  2752. E1000_TX_FLAGS_VLAN_SHIFT);
  2753. }
  2754. first = tx_ring->next_to_use;
  2755. tso = e1000_tso(adapter, tx_ring, skb, protocol);
  2756. if (tso < 0) {
  2757. dev_kfree_skb_any(skb);
  2758. return NETDEV_TX_OK;
  2759. }
  2760. if (likely(tso)) {
  2761. if (likely(hw->mac_type != e1000_82544))
  2762. tx_ring->last_tx_tso = true;
  2763. tx_flags |= E1000_TX_FLAGS_TSO;
  2764. } else if (likely(e1000_tx_csum(adapter, tx_ring, skb, protocol)))
  2765. tx_flags |= E1000_TX_FLAGS_CSUM;
  2766. if (protocol == htons(ETH_P_IP))
  2767. tx_flags |= E1000_TX_FLAGS_IPV4;
  2768. if (unlikely(skb->no_fcs))
  2769. tx_flags |= E1000_TX_FLAGS_NO_FCS;
  2770. count = e1000_tx_map(adapter, tx_ring, skb, first, max_per_txd,
  2771. nr_frags, mss);
  2772. if (count) {
  2773. /* The descriptors needed is higher than other Intel drivers
  2774. * due to a number of workarounds. The breakdown is below:
  2775. * Data descriptors: MAX_SKB_FRAGS + 1
  2776. * Context Descriptor: 1
  2777. * Keep head from touching tail: 2
  2778. * Workarounds: 3
  2779. */
  2780. int desc_needed = MAX_SKB_FRAGS + 7;
  2781. netdev_sent_queue(netdev, skb->len);
  2782. skb_tx_timestamp(skb);
  2783. e1000_tx_queue(adapter, tx_ring, tx_flags, count);
  2784. /* 82544 potentially requires twice as many data descriptors
  2785. * in order to guarantee buffers don't end on evenly-aligned
  2786. * dwords
  2787. */
  2788. if (adapter->pcix_82544)
  2789. desc_needed += MAX_SKB_FRAGS + 1;
  2790. /* Make sure there is space in the ring for the next send. */
  2791. e1000_maybe_stop_tx(netdev, tx_ring, desc_needed);
  2792. if (!skb->xmit_more ||
  2793. netif_xmit_stopped(netdev_get_tx_queue(netdev, 0))) {
  2794. writel(tx_ring->next_to_use, hw->hw_addr + tx_ring->tdt);
  2795. /* we need this if more than one processor can write to
  2796. * our tail at a time, it synchronizes IO on IA64/Altix
  2797. * systems
  2798. */
  2799. mmiowb();
  2800. }
  2801. } else {
  2802. dev_kfree_skb_any(skb);
  2803. tx_ring->buffer_info[first].time_stamp = 0;
  2804. tx_ring->next_to_use = first;
  2805. }
  2806. return NETDEV_TX_OK;
  2807. }
  2808. #define NUM_REGS 38 /* 1 based count */
  2809. static void e1000_regdump(struct e1000_adapter *adapter)
  2810. {
  2811. struct e1000_hw *hw = &adapter->hw;
  2812. u32 regs[NUM_REGS];
  2813. u32 *regs_buff = regs;
  2814. int i = 0;
  2815. static const char * const reg_name[] = {
  2816. "CTRL", "STATUS",
  2817. "RCTL", "RDLEN", "RDH", "RDT", "RDTR",
  2818. "TCTL", "TDBAL", "TDBAH", "TDLEN", "TDH", "TDT",
  2819. "TIDV", "TXDCTL", "TADV", "TARC0",
  2820. "TDBAL1", "TDBAH1", "TDLEN1", "TDH1", "TDT1",
  2821. "TXDCTL1", "TARC1",
  2822. "CTRL_EXT", "ERT", "RDBAL", "RDBAH",
  2823. "TDFH", "TDFT", "TDFHS", "TDFTS", "TDFPC",
  2824. "RDFH", "RDFT", "RDFHS", "RDFTS", "RDFPC"
  2825. };
  2826. regs_buff[0] = er32(CTRL);
  2827. regs_buff[1] = er32(STATUS);
  2828. regs_buff[2] = er32(RCTL);
  2829. regs_buff[3] = er32(RDLEN);
  2830. regs_buff[4] = er32(RDH);
  2831. regs_buff[5] = er32(RDT);
  2832. regs_buff[6] = er32(RDTR);
  2833. regs_buff[7] = er32(TCTL);
  2834. regs_buff[8] = er32(TDBAL);
  2835. regs_buff[9] = er32(TDBAH);
  2836. regs_buff[10] = er32(TDLEN);
  2837. regs_buff[11] = er32(TDH);
  2838. regs_buff[12] = er32(TDT);
  2839. regs_buff[13] = er32(TIDV);
  2840. regs_buff[14] = er32(TXDCTL);
  2841. regs_buff[15] = er32(TADV);
  2842. regs_buff[16] = er32(TARC0);
  2843. regs_buff[17] = er32(TDBAL1);
  2844. regs_buff[18] = er32(TDBAH1);
  2845. regs_buff[19] = er32(TDLEN1);
  2846. regs_buff[20] = er32(TDH1);
  2847. regs_buff[21] = er32(TDT1);
  2848. regs_buff[22] = er32(TXDCTL1);
  2849. regs_buff[23] = er32(TARC1);
  2850. regs_buff[24] = er32(CTRL_EXT);
  2851. regs_buff[25] = er32(ERT);
  2852. regs_buff[26] = er32(RDBAL0);
  2853. regs_buff[27] = er32(RDBAH0);
  2854. regs_buff[28] = er32(TDFH);
  2855. regs_buff[29] = er32(TDFT);
  2856. regs_buff[30] = er32(TDFHS);
  2857. regs_buff[31] = er32(TDFTS);
  2858. regs_buff[32] = er32(TDFPC);
  2859. regs_buff[33] = er32(RDFH);
  2860. regs_buff[34] = er32(RDFT);
  2861. regs_buff[35] = er32(RDFHS);
  2862. regs_buff[36] = er32(RDFTS);
  2863. regs_buff[37] = er32(RDFPC);
  2864. pr_info("Register dump\n");
  2865. for (i = 0; i < NUM_REGS; i++)
  2866. pr_info("%-15s %08x\n", reg_name[i], regs_buff[i]);
  2867. }
  2868. /*
  2869. * e1000_dump: Print registers, tx ring and rx ring
  2870. */
  2871. static void e1000_dump(struct e1000_adapter *adapter)
  2872. {
  2873. /* this code doesn't handle multiple rings */
  2874. struct e1000_tx_ring *tx_ring = adapter->tx_ring;
  2875. struct e1000_rx_ring *rx_ring = adapter->rx_ring;
  2876. int i;
  2877. if (!netif_msg_hw(adapter))
  2878. return;
  2879. /* Print Registers */
  2880. e1000_regdump(adapter);
  2881. /* transmit dump */
  2882. pr_info("TX Desc ring0 dump\n");
  2883. /* Transmit Descriptor Formats - DEXT[29] is 0 (Legacy) or 1 (Extended)
  2884. *
  2885. * Legacy Transmit Descriptor
  2886. * +--------------------------------------------------------------+
  2887. * 0 | Buffer Address [63:0] (Reserved on Write Back) |
  2888. * +--------------------------------------------------------------+
  2889. * 8 | Special | CSS | Status | CMD | CSO | Length |
  2890. * +--------------------------------------------------------------+
  2891. * 63 48 47 36 35 32 31 24 23 16 15 0
  2892. *
  2893. * Extended Context Descriptor (DTYP=0x0) for TSO or checksum offload
  2894. * 63 48 47 40 39 32 31 16 15 8 7 0
  2895. * +----------------------------------------------------------------+
  2896. * 0 | TUCSE | TUCS0 | TUCSS | IPCSE | IPCS0 | IPCSS |
  2897. * +----------------------------------------------------------------+
  2898. * 8 | MSS | HDRLEN | RSV | STA | TUCMD | DTYP | PAYLEN |
  2899. * +----------------------------------------------------------------+
  2900. * 63 48 47 40 39 36 35 32 31 24 23 20 19 0
  2901. *
  2902. * Extended Data Descriptor (DTYP=0x1)
  2903. * +----------------------------------------------------------------+
  2904. * 0 | Buffer Address [63:0] |
  2905. * +----------------------------------------------------------------+
  2906. * 8 | VLAN tag | POPTS | Rsvd | Status | Command | DTYP | DTALEN |
  2907. * +----------------------------------------------------------------+
  2908. * 63 48 47 40 39 36 35 32 31 24 23 20 19 0
  2909. */
  2910. pr_info("Tc[desc] [Ce CoCsIpceCoS] [MssHlRSCm0Plen] [bi->dma ] leng ntw timestmp bi->skb\n");
  2911. pr_info("Td[desc] [address 63:0 ] [VlaPoRSCm1Dlen] [bi->dma ] leng ntw timestmp bi->skb\n");
  2912. if (!netif_msg_tx_done(adapter))
  2913. goto rx_ring_summary;
  2914. for (i = 0; tx_ring->desc && (i < tx_ring->count); i++) {
  2915. struct e1000_tx_desc *tx_desc = E1000_TX_DESC(*tx_ring, i);
  2916. struct e1000_tx_buffer *buffer_info = &tx_ring->buffer_info[i];
  2917. struct my_u { __le64 a; __le64 b; };
  2918. struct my_u *u = (struct my_u *)tx_desc;
  2919. const char *type;
  2920. if (i == tx_ring->next_to_use && i == tx_ring->next_to_clean)
  2921. type = "NTC/U";
  2922. else if (i == tx_ring->next_to_use)
  2923. type = "NTU";
  2924. else if (i == tx_ring->next_to_clean)
  2925. type = "NTC";
  2926. else
  2927. type = "";
  2928. pr_info("T%c[0x%03X] %016llX %016llX %016llX %04X %3X %016llX %p %s\n",
  2929. ((le64_to_cpu(u->b) & (1<<20)) ? 'd' : 'c'), i,
  2930. le64_to_cpu(u->a), le64_to_cpu(u->b),
  2931. (u64)buffer_info->dma, buffer_info->length,
  2932. buffer_info->next_to_watch,
  2933. (u64)buffer_info->time_stamp, buffer_info->skb, type);
  2934. }
  2935. rx_ring_summary:
  2936. /* receive dump */
  2937. pr_info("\nRX Desc ring dump\n");
  2938. /* Legacy Receive Descriptor Format
  2939. *
  2940. * +-----------------------------------------------------+
  2941. * | Buffer Address [63:0] |
  2942. * +-----------------------------------------------------+
  2943. * | VLAN Tag | Errors | Status 0 | Packet csum | Length |
  2944. * +-----------------------------------------------------+
  2945. * 63 48 47 40 39 32 31 16 15 0
  2946. */
  2947. pr_info("R[desc] [address 63:0 ] [vl er S cks ln] [bi->dma ] [bi->skb]\n");
  2948. if (!netif_msg_rx_status(adapter))
  2949. goto exit;
  2950. for (i = 0; rx_ring->desc && (i < rx_ring->count); i++) {
  2951. struct e1000_rx_desc *rx_desc = E1000_RX_DESC(*rx_ring, i);
  2952. struct e1000_rx_buffer *buffer_info = &rx_ring->buffer_info[i];
  2953. struct my_u { __le64 a; __le64 b; };
  2954. struct my_u *u = (struct my_u *)rx_desc;
  2955. const char *type;
  2956. if (i == rx_ring->next_to_use)
  2957. type = "NTU";
  2958. else if (i == rx_ring->next_to_clean)
  2959. type = "NTC";
  2960. else
  2961. type = "";
  2962. pr_info("R[0x%03X] %016llX %016llX %016llX %p %s\n",
  2963. i, le64_to_cpu(u->a), le64_to_cpu(u->b),
  2964. (u64)buffer_info->dma, buffer_info->rxbuf.data, type);
  2965. } /* for */
  2966. /* dump the descriptor caches */
  2967. /* rx */
  2968. pr_info("Rx descriptor cache in 64bit format\n");
  2969. for (i = 0x6000; i <= 0x63FF ; i += 0x10) {
  2970. pr_info("R%04X: %08X|%08X %08X|%08X\n",
  2971. i,
  2972. readl(adapter->hw.hw_addr + i+4),
  2973. readl(adapter->hw.hw_addr + i),
  2974. readl(adapter->hw.hw_addr + i+12),
  2975. readl(adapter->hw.hw_addr + i+8));
  2976. }
  2977. /* tx */
  2978. pr_info("Tx descriptor cache in 64bit format\n");
  2979. for (i = 0x7000; i <= 0x73FF ; i += 0x10) {
  2980. pr_info("T%04X: %08X|%08X %08X|%08X\n",
  2981. i,
  2982. readl(adapter->hw.hw_addr + i+4),
  2983. readl(adapter->hw.hw_addr + i),
  2984. readl(adapter->hw.hw_addr + i+12),
  2985. readl(adapter->hw.hw_addr + i+8));
  2986. }
  2987. exit:
  2988. return;
  2989. }
  2990. /**
  2991. * e1000_tx_timeout - Respond to a Tx Hang
  2992. * @netdev: network interface device structure
  2993. **/
  2994. static void e1000_tx_timeout(struct net_device *netdev)
  2995. {
  2996. struct e1000_adapter *adapter = netdev_priv(netdev);
  2997. /* Do the reset outside of interrupt context */
  2998. adapter->tx_timeout_count++;
  2999. schedule_work(&adapter->reset_task);
  3000. }
  3001. static void e1000_reset_task(struct work_struct *work)
  3002. {
  3003. struct e1000_adapter *adapter =
  3004. container_of(work, struct e1000_adapter, reset_task);
  3005. e_err(drv, "Reset adapter\n");
  3006. e1000_reinit_locked(adapter);
  3007. }
  3008. /**
  3009. * e1000_change_mtu - Change the Maximum Transfer Unit
  3010. * @netdev: network interface device structure
  3011. * @new_mtu: new value for maximum frame size
  3012. *
  3013. * Returns 0 on success, negative on failure
  3014. **/
  3015. static int e1000_change_mtu(struct net_device *netdev, int new_mtu)
  3016. {
  3017. struct e1000_adapter *adapter = netdev_priv(netdev);
  3018. struct e1000_hw *hw = &adapter->hw;
  3019. int max_frame = new_mtu + ETH_HLEN + ETH_FCS_LEN;
  3020. /* Adapter-specific max frame size limits. */
  3021. switch (hw->mac_type) {
  3022. case e1000_undefined ... e1000_82542_rev2_1:
  3023. if (max_frame > (ETH_FRAME_LEN + ETH_FCS_LEN)) {
  3024. e_err(probe, "Jumbo Frames not supported.\n");
  3025. return -EINVAL;
  3026. }
  3027. break;
  3028. default:
  3029. /* Capable of supporting up to MAX_JUMBO_FRAME_SIZE limit. */
  3030. break;
  3031. }
  3032. while (test_and_set_bit(__E1000_RESETTING, &adapter->flags))
  3033. msleep(1);
  3034. /* e1000_down has a dependency on max_frame_size */
  3035. hw->max_frame_size = max_frame;
  3036. if (netif_running(netdev)) {
  3037. /* prevent buffers from being reallocated */
  3038. adapter->alloc_rx_buf = e1000_alloc_dummy_rx_buffers;
  3039. e1000_down(adapter);
  3040. }
  3041. /* NOTE: netdev_alloc_skb reserves 16 bytes, and typically NET_IP_ALIGN
  3042. * means we reserve 2 more, this pushes us to allocate from the next
  3043. * larger slab size.
  3044. * i.e. RXBUFFER_2048 --> size-4096 slab
  3045. * however with the new *_jumbo_rx* routines, jumbo receives will use
  3046. * fragmented skbs
  3047. */
  3048. if (max_frame <= E1000_RXBUFFER_2048)
  3049. adapter->rx_buffer_len = E1000_RXBUFFER_2048;
  3050. else
  3051. #if (PAGE_SIZE >= E1000_RXBUFFER_16384)
  3052. adapter->rx_buffer_len = E1000_RXBUFFER_16384;
  3053. #elif (PAGE_SIZE >= E1000_RXBUFFER_4096)
  3054. adapter->rx_buffer_len = PAGE_SIZE;
  3055. #endif
  3056. /* adjust allocation if LPE protects us, and we aren't using SBP */
  3057. if (!hw->tbi_compatibility_on &&
  3058. ((max_frame == (ETH_FRAME_LEN + ETH_FCS_LEN)) ||
  3059. (max_frame == MAXIMUM_ETHERNET_VLAN_SIZE)))
  3060. adapter->rx_buffer_len = MAXIMUM_ETHERNET_VLAN_SIZE;
  3061. pr_info("%s changing MTU from %d to %d\n",
  3062. netdev->name, netdev->mtu, new_mtu);
  3063. netdev->mtu = new_mtu;
  3064. if (netif_running(netdev))
  3065. e1000_up(adapter);
  3066. else
  3067. e1000_reset(adapter);
  3068. clear_bit(__E1000_RESETTING, &adapter->flags);
  3069. return 0;
  3070. }
  3071. /**
  3072. * e1000_update_stats - Update the board statistics counters
  3073. * @adapter: board private structure
  3074. **/
  3075. void e1000_update_stats(struct e1000_adapter *adapter)
  3076. {
  3077. struct net_device *netdev = adapter->netdev;
  3078. struct e1000_hw *hw = &adapter->hw;
  3079. struct pci_dev *pdev = adapter->pdev;
  3080. unsigned long flags;
  3081. u16 phy_tmp;
  3082. #define PHY_IDLE_ERROR_COUNT_MASK 0x00FF
  3083. /* Prevent stats update while adapter is being reset, or if the pci
  3084. * connection is down.
  3085. */
  3086. if (adapter->link_speed == 0)
  3087. return;
  3088. if (pci_channel_offline(pdev))
  3089. return;
  3090. spin_lock_irqsave(&adapter->stats_lock, flags);
  3091. /* these counters are modified from e1000_tbi_adjust_stats,
  3092. * called from the interrupt context, so they must only
  3093. * be written while holding adapter->stats_lock
  3094. */
  3095. adapter->stats.crcerrs += er32(CRCERRS);
  3096. adapter->stats.gprc += er32(GPRC);
  3097. adapter->stats.gorcl += er32(GORCL);
  3098. adapter->stats.gorch += er32(GORCH);
  3099. adapter->stats.bprc += er32(BPRC);
  3100. adapter->stats.mprc += er32(MPRC);
  3101. adapter->stats.roc += er32(ROC);
  3102. adapter->stats.prc64 += er32(PRC64);
  3103. adapter->stats.prc127 += er32(PRC127);
  3104. adapter->stats.prc255 += er32(PRC255);
  3105. adapter->stats.prc511 += er32(PRC511);
  3106. adapter->stats.prc1023 += er32(PRC1023);
  3107. adapter->stats.prc1522 += er32(PRC1522);
  3108. adapter->stats.symerrs += er32(SYMERRS);
  3109. adapter->stats.mpc += er32(MPC);
  3110. adapter->stats.scc += er32(SCC);
  3111. adapter->stats.ecol += er32(ECOL);
  3112. adapter->stats.mcc += er32(MCC);
  3113. adapter->stats.latecol += er32(LATECOL);
  3114. adapter->stats.dc += er32(DC);
  3115. adapter->stats.sec += er32(SEC);
  3116. adapter->stats.rlec += er32(RLEC);
  3117. adapter->stats.xonrxc += er32(XONRXC);
  3118. adapter->stats.xontxc += er32(XONTXC);
  3119. adapter->stats.xoffrxc += er32(XOFFRXC);
  3120. adapter->stats.xofftxc += er32(XOFFTXC);
  3121. adapter->stats.fcruc += er32(FCRUC);
  3122. adapter->stats.gptc += er32(GPTC);
  3123. adapter->stats.gotcl += er32(GOTCL);
  3124. adapter->stats.gotch += er32(GOTCH);
  3125. adapter->stats.rnbc += er32(RNBC);
  3126. adapter->stats.ruc += er32(RUC);
  3127. adapter->stats.rfc += er32(RFC);
  3128. adapter->stats.rjc += er32(RJC);
  3129. adapter->stats.torl += er32(TORL);
  3130. adapter->stats.torh += er32(TORH);
  3131. adapter->stats.totl += er32(TOTL);
  3132. adapter->stats.toth += er32(TOTH);
  3133. adapter->stats.tpr += er32(TPR);
  3134. adapter->stats.ptc64 += er32(PTC64);
  3135. adapter->stats.ptc127 += er32(PTC127);
  3136. adapter->stats.ptc255 += er32(PTC255);
  3137. adapter->stats.ptc511 += er32(PTC511);
  3138. adapter->stats.ptc1023 += er32(PTC1023);
  3139. adapter->stats.ptc1522 += er32(PTC1522);
  3140. adapter->stats.mptc += er32(MPTC);
  3141. adapter->stats.bptc += er32(BPTC);
  3142. /* used for adaptive IFS */
  3143. hw->tx_packet_delta = er32(TPT);
  3144. adapter->stats.tpt += hw->tx_packet_delta;
  3145. hw->collision_delta = er32(COLC);
  3146. adapter->stats.colc += hw->collision_delta;
  3147. if (hw->mac_type >= e1000_82543) {
  3148. adapter->stats.algnerrc += er32(ALGNERRC);
  3149. adapter->stats.rxerrc += er32(RXERRC);
  3150. adapter->stats.tncrs += er32(TNCRS);
  3151. adapter->stats.cexterr += er32(CEXTERR);
  3152. adapter->stats.tsctc += er32(TSCTC);
  3153. adapter->stats.tsctfc += er32(TSCTFC);
  3154. }
  3155. /* Fill out the OS statistics structure */
  3156. netdev->stats.multicast = adapter->stats.mprc;
  3157. netdev->stats.collisions = adapter->stats.colc;
  3158. /* Rx Errors */
  3159. /* RLEC on some newer hardware can be incorrect so build
  3160. * our own version based on RUC and ROC
  3161. */
  3162. netdev->stats.rx_errors = adapter->stats.rxerrc +
  3163. adapter->stats.crcerrs + adapter->stats.algnerrc +
  3164. adapter->stats.ruc + adapter->stats.roc +
  3165. adapter->stats.cexterr;
  3166. adapter->stats.rlerrc = adapter->stats.ruc + adapter->stats.roc;
  3167. netdev->stats.rx_length_errors = adapter->stats.rlerrc;
  3168. netdev->stats.rx_crc_errors = adapter->stats.crcerrs;
  3169. netdev->stats.rx_frame_errors = adapter->stats.algnerrc;
  3170. netdev->stats.rx_missed_errors = adapter->stats.mpc;
  3171. /* Tx Errors */
  3172. adapter->stats.txerrc = adapter->stats.ecol + adapter->stats.latecol;
  3173. netdev->stats.tx_errors = adapter->stats.txerrc;
  3174. netdev->stats.tx_aborted_errors = adapter->stats.ecol;
  3175. netdev->stats.tx_window_errors = adapter->stats.latecol;
  3176. netdev->stats.tx_carrier_errors = adapter->stats.tncrs;
  3177. if (hw->bad_tx_carr_stats_fd &&
  3178. adapter->link_duplex == FULL_DUPLEX) {
  3179. netdev->stats.tx_carrier_errors = 0;
  3180. adapter->stats.tncrs = 0;
  3181. }
  3182. /* Tx Dropped needs to be maintained elsewhere */
  3183. /* Phy Stats */
  3184. if (hw->media_type == e1000_media_type_copper) {
  3185. if ((adapter->link_speed == SPEED_1000) &&
  3186. (!e1000_read_phy_reg(hw, PHY_1000T_STATUS, &phy_tmp))) {
  3187. phy_tmp &= PHY_IDLE_ERROR_COUNT_MASK;
  3188. adapter->phy_stats.idle_errors += phy_tmp;
  3189. }
  3190. if ((hw->mac_type <= e1000_82546) &&
  3191. (hw->phy_type == e1000_phy_m88) &&
  3192. !e1000_read_phy_reg(hw, M88E1000_RX_ERR_CNTR, &phy_tmp))
  3193. adapter->phy_stats.receive_errors += phy_tmp;
  3194. }
  3195. /* Management Stats */
  3196. if (hw->has_smbus) {
  3197. adapter->stats.mgptc += er32(MGTPTC);
  3198. adapter->stats.mgprc += er32(MGTPRC);
  3199. adapter->stats.mgpdc += er32(MGTPDC);
  3200. }
  3201. spin_unlock_irqrestore(&adapter->stats_lock, flags);
  3202. }
  3203. /**
  3204. * e1000_intr - Interrupt Handler
  3205. * @irq: interrupt number
  3206. * @data: pointer to a network interface device structure
  3207. **/
  3208. static irqreturn_t e1000_intr(int irq, void *data)
  3209. {
  3210. struct net_device *netdev = data;
  3211. struct e1000_adapter *adapter = netdev_priv(netdev);
  3212. struct e1000_hw *hw = &adapter->hw;
  3213. u32 icr = er32(ICR);
  3214. if (unlikely((!icr)))
  3215. return IRQ_NONE; /* Not our interrupt */
  3216. /* we might have caused the interrupt, but the above
  3217. * read cleared it, and just in case the driver is
  3218. * down there is nothing to do so return handled
  3219. */
  3220. if (unlikely(test_bit(__E1000_DOWN, &adapter->flags)))
  3221. return IRQ_HANDLED;
  3222. if (unlikely(icr & (E1000_ICR_RXSEQ | E1000_ICR_LSC))) {
  3223. hw->get_link_status = 1;
  3224. /* guard against interrupt when we're going down */
  3225. if (!test_bit(__E1000_DOWN, &adapter->flags))
  3226. schedule_delayed_work(&adapter->watchdog_task, 1);
  3227. }
  3228. /* disable interrupts, without the synchronize_irq bit */
  3229. ew32(IMC, ~0);
  3230. E1000_WRITE_FLUSH();
  3231. if (likely(napi_schedule_prep(&adapter->napi))) {
  3232. adapter->total_tx_bytes = 0;
  3233. adapter->total_tx_packets = 0;
  3234. adapter->total_rx_bytes = 0;
  3235. adapter->total_rx_packets = 0;
  3236. __napi_schedule(&adapter->napi);
  3237. } else {
  3238. /* this really should not happen! if it does it is basically a
  3239. * bug, but not a hard error, so enable ints and continue
  3240. */
  3241. if (!test_bit(__E1000_DOWN, &adapter->flags))
  3242. e1000_irq_enable(adapter);
  3243. }
  3244. return IRQ_HANDLED;
  3245. }
  3246. /**
  3247. * e1000_clean - NAPI Rx polling callback
  3248. * @adapter: board private structure
  3249. **/
  3250. static int e1000_clean(struct napi_struct *napi, int budget)
  3251. {
  3252. struct e1000_adapter *adapter = container_of(napi, struct e1000_adapter,
  3253. napi);
  3254. int tx_clean_complete = 0, work_done = 0;
  3255. tx_clean_complete = e1000_clean_tx_irq(adapter, &adapter->tx_ring[0]);
  3256. adapter->clean_rx(adapter, &adapter->rx_ring[0], &work_done, budget);
  3257. if (!tx_clean_complete)
  3258. work_done = budget;
  3259. /* If budget not fully consumed, exit the polling mode */
  3260. if (work_done < budget) {
  3261. if (likely(adapter->itr_setting & 3))
  3262. e1000_set_itr(adapter);
  3263. napi_complete_done(napi, work_done);
  3264. if (!test_bit(__E1000_DOWN, &adapter->flags))
  3265. e1000_irq_enable(adapter);
  3266. }
  3267. return work_done;
  3268. }
  3269. /**
  3270. * e1000_clean_tx_irq - Reclaim resources after transmit completes
  3271. * @adapter: board private structure
  3272. **/
  3273. static bool e1000_clean_tx_irq(struct e1000_adapter *adapter,
  3274. struct e1000_tx_ring *tx_ring)
  3275. {
  3276. struct e1000_hw *hw = &adapter->hw;
  3277. struct net_device *netdev = adapter->netdev;
  3278. struct e1000_tx_desc *tx_desc, *eop_desc;
  3279. struct e1000_tx_buffer *buffer_info;
  3280. unsigned int i, eop;
  3281. unsigned int count = 0;
  3282. unsigned int total_tx_bytes = 0, total_tx_packets = 0;
  3283. unsigned int bytes_compl = 0, pkts_compl = 0;
  3284. i = tx_ring->next_to_clean;
  3285. eop = tx_ring->buffer_info[i].next_to_watch;
  3286. eop_desc = E1000_TX_DESC(*tx_ring, eop);
  3287. while ((eop_desc->upper.data & cpu_to_le32(E1000_TXD_STAT_DD)) &&
  3288. (count < tx_ring->count)) {
  3289. bool cleaned = false;
  3290. dma_rmb(); /* read buffer_info after eop_desc */
  3291. for ( ; !cleaned; count++) {
  3292. tx_desc = E1000_TX_DESC(*tx_ring, i);
  3293. buffer_info = &tx_ring->buffer_info[i];
  3294. cleaned = (i == eop);
  3295. if (cleaned) {
  3296. total_tx_packets += buffer_info->segs;
  3297. total_tx_bytes += buffer_info->bytecount;
  3298. if (buffer_info->skb) {
  3299. bytes_compl += buffer_info->skb->len;
  3300. pkts_compl++;
  3301. }
  3302. }
  3303. e1000_unmap_and_free_tx_resource(adapter, buffer_info);
  3304. tx_desc->upper.data = 0;
  3305. if (unlikely(++i == tx_ring->count))
  3306. i = 0;
  3307. }
  3308. eop = tx_ring->buffer_info[i].next_to_watch;
  3309. eop_desc = E1000_TX_DESC(*tx_ring, eop);
  3310. }
  3311. /* Synchronize with E1000_DESC_UNUSED called from e1000_xmit_frame,
  3312. * which will reuse the cleaned buffers.
  3313. */
  3314. smp_store_release(&tx_ring->next_to_clean, i);
  3315. netdev_completed_queue(netdev, pkts_compl, bytes_compl);
  3316. #define TX_WAKE_THRESHOLD 32
  3317. if (unlikely(count && netif_carrier_ok(netdev) &&
  3318. E1000_DESC_UNUSED(tx_ring) >= TX_WAKE_THRESHOLD)) {
  3319. /* Make sure that anybody stopping the queue after this
  3320. * sees the new next_to_clean.
  3321. */
  3322. smp_mb();
  3323. if (netif_queue_stopped(netdev) &&
  3324. !(test_bit(__E1000_DOWN, &adapter->flags))) {
  3325. netif_wake_queue(netdev);
  3326. ++adapter->restart_queue;
  3327. }
  3328. }
  3329. if (adapter->detect_tx_hung) {
  3330. /* Detect a transmit hang in hardware, this serializes the
  3331. * check with the clearing of time_stamp and movement of i
  3332. */
  3333. adapter->detect_tx_hung = false;
  3334. if (tx_ring->buffer_info[eop].time_stamp &&
  3335. time_after(jiffies, tx_ring->buffer_info[eop].time_stamp +
  3336. (adapter->tx_timeout_factor * HZ)) &&
  3337. !(er32(STATUS) & E1000_STATUS_TXOFF)) {
  3338. /* detected Tx unit hang */
  3339. e_err(drv, "Detected Tx Unit Hang\n"
  3340. " Tx Queue <%lu>\n"
  3341. " TDH <%x>\n"
  3342. " TDT <%x>\n"
  3343. " next_to_use <%x>\n"
  3344. " next_to_clean <%x>\n"
  3345. "buffer_info[next_to_clean]\n"
  3346. " time_stamp <%lx>\n"
  3347. " next_to_watch <%x>\n"
  3348. " jiffies <%lx>\n"
  3349. " next_to_watch.status <%x>\n",
  3350. (unsigned long)(tx_ring - adapter->tx_ring),
  3351. readl(hw->hw_addr + tx_ring->tdh),
  3352. readl(hw->hw_addr + tx_ring->tdt),
  3353. tx_ring->next_to_use,
  3354. tx_ring->next_to_clean,
  3355. tx_ring->buffer_info[eop].time_stamp,
  3356. eop,
  3357. jiffies,
  3358. eop_desc->upper.fields.status);
  3359. e1000_dump(adapter);
  3360. netif_stop_queue(netdev);
  3361. }
  3362. }
  3363. adapter->total_tx_bytes += total_tx_bytes;
  3364. adapter->total_tx_packets += total_tx_packets;
  3365. netdev->stats.tx_bytes += total_tx_bytes;
  3366. netdev->stats.tx_packets += total_tx_packets;
  3367. return count < tx_ring->count;
  3368. }
  3369. /**
  3370. * e1000_rx_checksum - Receive Checksum Offload for 82543
  3371. * @adapter: board private structure
  3372. * @status_err: receive descriptor status and error fields
  3373. * @csum: receive descriptor csum field
  3374. * @sk_buff: socket buffer with received data
  3375. **/
  3376. static void e1000_rx_checksum(struct e1000_adapter *adapter, u32 status_err,
  3377. u32 csum, struct sk_buff *skb)
  3378. {
  3379. struct e1000_hw *hw = &adapter->hw;
  3380. u16 status = (u16)status_err;
  3381. u8 errors = (u8)(status_err >> 24);
  3382. skb_checksum_none_assert(skb);
  3383. /* 82543 or newer only */
  3384. if (unlikely(hw->mac_type < e1000_82543))
  3385. return;
  3386. /* Ignore Checksum bit is set */
  3387. if (unlikely(status & E1000_RXD_STAT_IXSM))
  3388. return;
  3389. /* TCP/UDP checksum error bit is set */
  3390. if (unlikely(errors & E1000_RXD_ERR_TCPE)) {
  3391. /* let the stack verify checksum errors */
  3392. adapter->hw_csum_err++;
  3393. return;
  3394. }
  3395. /* TCP/UDP Checksum has not been calculated */
  3396. if (!(status & E1000_RXD_STAT_TCPCS))
  3397. return;
  3398. /* It must be a TCP or UDP packet with a valid checksum */
  3399. if (likely(status & E1000_RXD_STAT_TCPCS)) {
  3400. /* TCP checksum is good */
  3401. skb->ip_summed = CHECKSUM_UNNECESSARY;
  3402. }
  3403. adapter->hw_csum_good++;
  3404. }
  3405. /**
  3406. * e1000_consume_page - helper function for jumbo Rx path
  3407. **/
  3408. static void e1000_consume_page(struct e1000_rx_buffer *bi, struct sk_buff *skb,
  3409. u16 length)
  3410. {
  3411. bi->rxbuf.page = NULL;
  3412. skb->len += length;
  3413. skb->data_len += length;
  3414. skb->truesize += PAGE_SIZE;
  3415. }
  3416. /**
  3417. * e1000_receive_skb - helper function to handle rx indications
  3418. * @adapter: board private structure
  3419. * @status: descriptor status field as written by hardware
  3420. * @vlan: descriptor vlan field as written by hardware (no le/be conversion)
  3421. * @skb: pointer to sk_buff to be indicated to stack
  3422. */
  3423. static void e1000_receive_skb(struct e1000_adapter *adapter, u8 status,
  3424. __le16 vlan, struct sk_buff *skb)
  3425. {
  3426. skb->protocol = eth_type_trans(skb, adapter->netdev);
  3427. if (status & E1000_RXD_STAT_VP) {
  3428. u16 vid = le16_to_cpu(vlan) & E1000_RXD_SPC_VLAN_MASK;
  3429. __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vid);
  3430. }
  3431. napi_gro_receive(&adapter->napi, skb);
  3432. }
  3433. /**
  3434. * e1000_tbi_adjust_stats
  3435. * @hw: Struct containing variables accessed by shared code
  3436. * @frame_len: The length of the frame in question
  3437. * @mac_addr: The Ethernet destination address of the frame in question
  3438. *
  3439. * Adjusts the statistic counters when a frame is accepted by TBI_ACCEPT
  3440. */
  3441. static void e1000_tbi_adjust_stats(struct e1000_hw *hw,
  3442. struct e1000_hw_stats *stats,
  3443. u32 frame_len, const u8 *mac_addr)
  3444. {
  3445. u64 carry_bit;
  3446. /* First adjust the frame length. */
  3447. frame_len--;
  3448. /* We need to adjust the statistics counters, since the hardware
  3449. * counters overcount this packet as a CRC error and undercount
  3450. * the packet as a good packet
  3451. */
  3452. /* This packet should not be counted as a CRC error. */
  3453. stats->crcerrs--;
  3454. /* This packet does count as a Good Packet Received. */
  3455. stats->gprc++;
  3456. /* Adjust the Good Octets received counters */
  3457. carry_bit = 0x80000000 & stats->gorcl;
  3458. stats->gorcl += frame_len;
  3459. /* If the high bit of Gorcl (the low 32 bits of the Good Octets
  3460. * Received Count) was one before the addition,
  3461. * AND it is zero after, then we lost the carry out,
  3462. * need to add one to Gorch (Good Octets Received Count High).
  3463. * This could be simplified if all environments supported
  3464. * 64-bit integers.
  3465. */
  3466. if (carry_bit && ((stats->gorcl & 0x80000000) == 0))
  3467. stats->gorch++;
  3468. /* Is this a broadcast or multicast? Check broadcast first,
  3469. * since the test for a multicast frame will test positive on
  3470. * a broadcast frame.
  3471. */
  3472. if (is_broadcast_ether_addr(mac_addr))
  3473. stats->bprc++;
  3474. else if (is_multicast_ether_addr(mac_addr))
  3475. stats->mprc++;
  3476. if (frame_len == hw->max_frame_size) {
  3477. /* In this case, the hardware has overcounted the number of
  3478. * oversize frames.
  3479. */
  3480. if (stats->roc > 0)
  3481. stats->roc--;
  3482. }
  3483. /* Adjust the bin counters when the extra byte put the frame in the
  3484. * wrong bin. Remember that the frame_len was adjusted above.
  3485. */
  3486. if (frame_len == 64) {
  3487. stats->prc64++;
  3488. stats->prc127--;
  3489. } else if (frame_len == 127) {
  3490. stats->prc127++;
  3491. stats->prc255--;
  3492. } else if (frame_len == 255) {
  3493. stats->prc255++;
  3494. stats->prc511--;
  3495. } else if (frame_len == 511) {
  3496. stats->prc511++;
  3497. stats->prc1023--;
  3498. } else if (frame_len == 1023) {
  3499. stats->prc1023++;
  3500. stats->prc1522--;
  3501. } else if (frame_len == 1522) {
  3502. stats->prc1522++;
  3503. }
  3504. }
  3505. static bool e1000_tbi_should_accept(struct e1000_adapter *adapter,
  3506. u8 status, u8 errors,
  3507. u32 length, const u8 *data)
  3508. {
  3509. struct e1000_hw *hw = &adapter->hw;
  3510. u8 last_byte = *(data + length - 1);
  3511. if (TBI_ACCEPT(hw, status, errors, length, last_byte)) {
  3512. unsigned long irq_flags;
  3513. spin_lock_irqsave(&adapter->stats_lock, irq_flags);
  3514. e1000_tbi_adjust_stats(hw, &adapter->stats, length, data);
  3515. spin_unlock_irqrestore(&adapter->stats_lock, irq_flags);
  3516. return true;
  3517. }
  3518. return false;
  3519. }
  3520. static struct sk_buff *e1000_alloc_rx_skb(struct e1000_adapter *adapter,
  3521. unsigned int bufsz)
  3522. {
  3523. struct sk_buff *skb = napi_alloc_skb(&adapter->napi, bufsz);
  3524. if (unlikely(!skb))
  3525. adapter->alloc_rx_buff_failed++;
  3526. return skb;
  3527. }
  3528. /**
  3529. * e1000_clean_jumbo_rx_irq - Send received data up the network stack; legacy
  3530. * @adapter: board private structure
  3531. * @rx_ring: ring to clean
  3532. * @work_done: amount of napi work completed this call
  3533. * @work_to_do: max amount of work allowed for this call to do
  3534. *
  3535. * the return value indicates whether actual cleaning was done, there
  3536. * is no guarantee that everything was cleaned
  3537. */
  3538. static bool e1000_clean_jumbo_rx_irq(struct e1000_adapter *adapter,
  3539. struct e1000_rx_ring *rx_ring,
  3540. int *work_done, int work_to_do)
  3541. {
  3542. struct net_device *netdev = adapter->netdev;
  3543. struct pci_dev *pdev = adapter->pdev;
  3544. struct e1000_rx_desc *rx_desc, *next_rxd;
  3545. struct e1000_rx_buffer *buffer_info, *next_buffer;
  3546. u32 length;
  3547. unsigned int i;
  3548. int cleaned_count = 0;
  3549. bool cleaned = false;
  3550. unsigned int total_rx_bytes = 0, total_rx_packets = 0;
  3551. i = rx_ring->next_to_clean;
  3552. rx_desc = E1000_RX_DESC(*rx_ring, i);
  3553. buffer_info = &rx_ring->buffer_info[i];
  3554. while (rx_desc->status & E1000_RXD_STAT_DD) {
  3555. struct sk_buff *skb;
  3556. u8 status;
  3557. if (*work_done >= work_to_do)
  3558. break;
  3559. (*work_done)++;
  3560. dma_rmb(); /* read descriptor and rx_buffer_info after status DD */
  3561. status = rx_desc->status;
  3562. if (++i == rx_ring->count)
  3563. i = 0;
  3564. next_rxd = E1000_RX_DESC(*rx_ring, i);
  3565. prefetch(next_rxd);
  3566. next_buffer = &rx_ring->buffer_info[i];
  3567. cleaned = true;
  3568. cleaned_count++;
  3569. dma_unmap_page(&pdev->dev, buffer_info->dma,
  3570. adapter->rx_buffer_len, DMA_FROM_DEVICE);
  3571. buffer_info->dma = 0;
  3572. length = le16_to_cpu(rx_desc->length);
  3573. /* errors is only valid for DD + EOP descriptors */
  3574. if (unlikely((status & E1000_RXD_STAT_EOP) &&
  3575. (rx_desc->errors & E1000_RXD_ERR_FRAME_ERR_MASK))) {
  3576. u8 *mapped = page_address(buffer_info->rxbuf.page);
  3577. if (e1000_tbi_should_accept(adapter, status,
  3578. rx_desc->errors,
  3579. length, mapped)) {
  3580. length--;
  3581. } else if (netdev->features & NETIF_F_RXALL) {
  3582. goto process_skb;
  3583. } else {
  3584. /* an error means any chain goes out the window
  3585. * too
  3586. */
  3587. if (rx_ring->rx_skb_top)
  3588. dev_kfree_skb(rx_ring->rx_skb_top);
  3589. rx_ring->rx_skb_top = NULL;
  3590. goto next_desc;
  3591. }
  3592. }
  3593. #define rxtop rx_ring->rx_skb_top
  3594. process_skb:
  3595. if (!(status & E1000_RXD_STAT_EOP)) {
  3596. /* this descriptor is only the beginning (or middle) */
  3597. if (!rxtop) {
  3598. /* this is the beginning of a chain */
  3599. rxtop = napi_get_frags(&adapter->napi);
  3600. if (!rxtop)
  3601. break;
  3602. skb_fill_page_desc(rxtop, 0,
  3603. buffer_info->rxbuf.page,
  3604. 0, length);
  3605. } else {
  3606. /* this is the middle of a chain */
  3607. skb_fill_page_desc(rxtop,
  3608. skb_shinfo(rxtop)->nr_frags,
  3609. buffer_info->rxbuf.page, 0, length);
  3610. }
  3611. e1000_consume_page(buffer_info, rxtop, length);
  3612. goto next_desc;
  3613. } else {
  3614. if (rxtop) {
  3615. /* end of the chain */
  3616. skb_fill_page_desc(rxtop,
  3617. skb_shinfo(rxtop)->nr_frags,
  3618. buffer_info->rxbuf.page, 0, length);
  3619. skb = rxtop;
  3620. rxtop = NULL;
  3621. e1000_consume_page(buffer_info, skb, length);
  3622. } else {
  3623. struct page *p;
  3624. /* no chain, got EOP, this buf is the packet
  3625. * copybreak to save the put_page/alloc_page
  3626. */
  3627. p = buffer_info->rxbuf.page;
  3628. if (length <= copybreak) {
  3629. u8 *vaddr;
  3630. if (likely(!(netdev->features & NETIF_F_RXFCS)))
  3631. length -= 4;
  3632. skb = e1000_alloc_rx_skb(adapter,
  3633. length);
  3634. if (!skb)
  3635. break;
  3636. vaddr = kmap_atomic(p);
  3637. memcpy(skb_tail_pointer(skb), vaddr,
  3638. length);
  3639. kunmap_atomic(vaddr);
  3640. /* re-use the page, so don't erase
  3641. * buffer_info->rxbuf.page
  3642. */
  3643. skb_put(skb, length);
  3644. e1000_rx_checksum(adapter,
  3645. status | rx_desc->errors << 24,
  3646. le16_to_cpu(rx_desc->csum), skb);
  3647. total_rx_bytes += skb->len;
  3648. total_rx_packets++;
  3649. e1000_receive_skb(adapter, status,
  3650. rx_desc->special, skb);
  3651. goto next_desc;
  3652. } else {
  3653. skb = napi_get_frags(&adapter->napi);
  3654. if (!skb) {
  3655. adapter->alloc_rx_buff_failed++;
  3656. break;
  3657. }
  3658. skb_fill_page_desc(skb, 0, p, 0,
  3659. length);
  3660. e1000_consume_page(buffer_info, skb,
  3661. length);
  3662. }
  3663. }
  3664. }
  3665. /* Receive Checksum Offload XXX recompute due to CRC strip? */
  3666. e1000_rx_checksum(adapter,
  3667. (u32)(status) |
  3668. ((u32)(rx_desc->errors) << 24),
  3669. le16_to_cpu(rx_desc->csum), skb);
  3670. total_rx_bytes += (skb->len - 4); /* don't count FCS */
  3671. if (likely(!(netdev->features & NETIF_F_RXFCS)))
  3672. pskb_trim(skb, skb->len - 4);
  3673. total_rx_packets++;
  3674. if (status & E1000_RXD_STAT_VP) {
  3675. __le16 vlan = rx_desc->special;
  3676. u16 vid = le16_to_cpu(vlan) & E1000_RXD_SPC_VLAN_MASK;
  3677. __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vid);
  3678. }
  3679. napi_gro_frags(&adapter->napi);
  3680. next_desc:
  3681. rx_desc->status = 0;
  3682. /* return some buffers to hardware, one at a time is too slow */
  3683. if (unlikely(cleaned_count >= E1000_RX_BUFFER_WRITE)) {
  3684. adapter->alloc_rx_buf(adapter, rx_ring, cleaned_count);
  3685. cleaned_count = 0;
  3686. }
  3687. /* use prefetched values */
  3688. rx_desc = next_rxd;
  3689. buffer_info = next_buffer;
  3690. }
  3691. rx_ring->next_to_clean = i;
  3692. cleaned_count = E1000_DESC_UNUSED(rx_ring);
  3693. if (cleaned_count)
  3694. adapter->alloc_rx_buf(adapter, rx_ring, cleaned_count);
  3695. adapter->total_rx_packets += total_rx_packets;
  3696. adapter->total_rx_bytes += total_rx_bytes;
  3697. netdev->stats.rx_bytes += total_rx_bytes;
  3698. netdev->stats.rx_packets += total_rx_packets;
  3699. return cleaned;
  3700. }
  3701. /* this should improve performance for small packets with large amounts
  3702. * of reassembly being done in the stack
  3703. */
  3704. static struct sk_buff *e1000_copybreak(struct e1000_adapter *adapter,
  3705. struct e1000_rx_buffer *buffer_info,
  3706. u32 length, const void *data)
  3707. {
  3708. struct sk_buff *skb;
  3709. if (length > copybreak)
  3710. return NULL;
  3711. skb = e1000_alloc_rx_skb(adapter, length);
  3712. if (!skb)
  3713. return NULL;
  3714. dma_sync_single_for_cpu(&adapter->pdev->dev, buffer_info->dma,
  3715. length, DMA_FROM_DEVICE);
  3716. skb_put_data(skb, data, length);
  3717. return skb;
  3718. }
  3719. /**
  3720. * e1000_clean_rx_irq - Send received data up the network stack; legacy
  3721. * @adapter: board private structure
  3722. * @rx_ring: ring to clean
  3723. * @work_done: amount of napi work completed this call
  3724. * @work_to_do: max amount of work allowed for this call to do
  3725. */
  3726. static bool e1000_clean_rx_irq(struct e1000_adapter *adapter,
  3727. struct e1000_rx_ring *rx_ring,
  3728. int *work_done, int work_to_do)
  3729. {
  3730. struct net_device *netdev = adapter->netdev;
  3731. struct pci_dev *pdev = adapter->pdev;
  3732. struct e1000_rx_desc *rx_desc, *next_rxd;
  3733. struct e1000_rx_buffer *buffer_info, *next_buffer;
  3734. u32 length;
  3735. unsigned int i;
  3736. int cleaned_count = 0;
  3737. bool cleaned = false;
  3738. unsigned int total_rx_bytes = 0, total_rx_packets = 0;
  3739. i = rx_ring->next_to_clean;
  3740. rx_desc = E1000_RX_DESC(*rx_ring, i);
  3741. buffer_info = &rx_ring->buffer_info[i];
  3742. while (rx_desc->status & E1000_RXD_STAT_DD) {
  3743. struct sk_buff *skb;
  3744. u8 *data;
  3745. u8 status;
  3746. if (*work_done >= work_to_do)
  3747. break;
  3748. (*work_done)++;
  3749. dma_rmb(); /* read descriptor and rx_buffer_info after status DD */
  3750. status = rx_desc->status;
  3751. length = le16_to_cpu(rx_desc->length);
  3752. data = buffer_info->rxbuf.data;
  3753. prefetch(data);
  3754. skb = e1000_copybreak(adapter, buffer_info, length, data);
  3755. if (!skb) {
  3756. unsigned int frag_len = e1000_frag_len(adapter);
  3757. skb = build_skb(data - E1000_HEADROOM, frag_len);
  3758. if (!skb) {
  3759. adapter->alloc_rx_buff_failed++;
  3760. break;
  3761. }
  3762. skb_reserve(skb, E1000_HEADROOM);
  3763. dma_unmap_single(&pdev->dev, buffer_info->dma,
  3764. adapter->rx_buffer_len,
  3765. DMA_FROM_DEVICE);
  3766. buffer_info->dma = 0;
  3767. buffer_info->rxbuf.data = NULL;
  3768. }
  3769. if (++i == rx_ring->count)
  3770. i = 0;
  3771. next_rxd = E1000_RX_DESC(*rx_ring, i);
  3772. prefetch(next_rxd);
  3773. next_buffer = &rx_ring->buffer_info[i];
  3774. cleaned = true;
  3775. cleaned_count++;
  3776. /* !EOP means multiple descriptors were used to store a single
  3777. * packet, if thats the case we need to toss it. In fact, we
  3778. * to toss every packet with the EOP bit clear and the next
  3779. * frame that _does_ have the EOP bit set, as it is by
  3780. * definition only a frame fragment
  3781. */
  3782. if (unlikely(!(status & E1000_RXD_STAT_EOP)))
  3783. adapter->discarding = true;
  3784. if (adapter->discarding) {
  3785. /* All receives must fit into a single buffer */
  3786. netdev_dbg(netdev, "Receive packet consumed multiple buffers\n");
  3787. dev_kfree_skb(skb);
  3788. if (status & E1000_RXD_STAT_EOP)
  3789. adapter->discarding = false;
  3790. goto next_desc;
  3791. }
  3792. if (unlikely(rx_desc->errors & E1000_RXD_ERR_FRAME_ERR_MASK)) {
  3793. if (e1000_tbi_should_accept(adapter, status,
  3794. rx_desc->errors,
  3795. length, data)) {
  3796. length--;
  3797. } else if (netdev->features & NETIF_F_RXALL) {
  3798. goto process_skb;
  3799. } else {
  3800. dev_kfree_skb(skb);
  3801. goto next_desc;
  3802. }
  3803. }
  3804. process_skb:
  3805. total_rx_bytes += (length - 4); /* don't count FCS */
  3806. total_rx_packets++;
  3807. if (likely(!(netdev->features & NETIF_F_RXFCS)))
  3808. /* adjust length to remove Ethernet CRC, this must be
  3809. * done after the TBI_ACCEPT workaround above
  3810. */
  3811. length -= 4;
  3812. if (buffer_info->rxbuf.data == NULL)
  3813. skb_put(skb, length);
  3814. else /* copybreak skb */
  3815. skb_trim(skb, length);
  3816. /* Receive Checksum Offload */
  3817. e1000_rx_checksum(adapter,
  3818. (u32)(status) |
  3819. ((u32)(rx_desc->errors) << 24),
  3820. le16_to_cpu(rx_desc->csum), skb);
  3821. e1000_receive_skb(adapter, status, rx_desc->special, skb);
  3822. next_desc:
  3823. rx_desc->status = 0;
  3824. /* return some buffers to hardware, one at a time is too slow */
  3825. if (unlikely(cleaned_count >= E1000_RX_BUFFER_WRITE)) {
  3826. adapter->alloc_rx_buf(adapter, rx_ring, cleaned_count);
  3827. cleaned_count = 0;
  3828. }
  3829. /* use prefetched values */
  3830. rx_desc = next_rxd;
  3831. buffer_info = next_buffer;
  3832. }
  3833. rx_ring->next_to_clean = i;
  3834. cleaned_count = E1000_DESC_UNUSED(rx_ring);
  3835. if (cleaned_count)
  3836. adapter->alloc_rx_buf(adapter, rx_ring, cleaned_count);
  3837. adapter->total_rx_packets += total_rx_packets;
  3838. adapter->total_rx_bytes += total_rx_bytes;
  3839. netdev->stats.rx_bytes += total_rx_bytes;
  3840. netdev->stats.rx_packets += total_rx_packets;
  3841. return cleaned;
  3842. }
  3843. /**
  3844. * e1000_alloc_jumbo_rx_buffers - Replace used jumbo receive buffers
  3845. * @adapter: address of board private structure
  3846. * @rx_ring: pointer to receive ring structure
  3847. * @cleaned_count: number of buffers to allocate this pass
  3848. **/
  3849. static void
  3850. e1000_alloc_jumbo_rx_buffers(struct e1000_adapter *adapter,
  3851. struct e1000_rx_ring *rx_ring, int cleaned_count)
  3852. {
  3853. struct pci_dev *pdev = adapter->pdev;
  3854. struct e1000_rx_desc *rx_desc;
  3855. struct e1000_rx_buffer *buffer_info;
  3856. unsigned int i;
  3857. i = rx_ring->next_to_use;
  3858. buffer_info = &rx_ring->buffer_info[i];
  3859. while (cleaned_count--) {
  3860. /* allocate a new page if necessary */
  3861. if (!buffer_info->rxbuf.page) {
  3862. buffer_info->rxbuf.page = alloc_page(GFP_ATOMIC);
  3863. if (unlikely(!buffer_info->rxbuf.page)) {
  3864. adapter->alloc_rx_buff_failed++;
  3865. break;
  3866. }
  3867. }
  3868. if (!buffer_info->dma) {
  3869. buffer_info->dma = dma_map_page(&pdev->dev,
  3870. buffer_info->rxbuf.page, 0,
  3871. adapter->rx_buffer_len,
  3872. DMA_FROM_DEVICE);
  3873. if (dma_mapping_error(&pdev->dev, buffer_info->dma)) {
  3874. put_page(buffer_info->rxbuf.page);
  3875. buffer_info->rxbuf.page = NULL;
  3876. buffer_info->dma = 0;
  3877. adapter->alloc_rx_buff_failed++;
  3878. break;
  3879. }
  3880. }
  3881. rx_desc = E1000_RX_DESC(*rx_ring, i);
  3882. rx_desc->buffer_addr = cpu_to_le64(buffer_info->dma);
  3883. if (unlikely(++i == rx_ring->count))
  3884. i = 0;
  3885. buffer_info = &rx_ring->buffer_info[i];
  3886. }
  3887. if (likely(rx_ring->next_to_use != i)) {
  3888. rx_ring->next_to_use = i;
  3889. if (unlikely(i-- == 0))
  3890. i = (rx_ring->count - 1);
  3891. /* Force memory writes to complete before letting h/w
  3892. * know there are new descriptors to fetch. (Only
  3893. * applicable for weak-ordered memory model archs,
  3894. * such as IA-64).
  3895. */
  3896. wmb();
  3897. writel(i, adapter->hw.hw_addr + rx_ring->rdt);
  3898. }
  3899. }
  3900. /**
  3901. * e1000_alloc_rx_buffers - Replace used receive buffers; legacy & extended
  3902. * @adapter: address of board private structure
  3903. **/
  3904. static void e1000_alloc_rx_buffers(struct e1000_adapter *adapter,
  3905. struct e1000_rx_ring *rx_ring,
  3906. int cleaned_count)
  3907. {
  3908. struct e1000_hw *hw = &adapter->hw;
  3909. struct pci_dev *pdev = adapter->pdev;
  3910. struct e1000_rx_desc *rx_desc;
  3911. struct e1000_rx_buffer *buffer_info;
  3912. unsigned int i;
  3913. unsigned int bufsz = adapter->rx_buffer_len;
  3914. i = rx_ring->next_to_use;
  3915. buffer_info = &rx_ring->buffer_info[i];
  3916. while (cleaned_count--) {
  3917. void *data;
  3918. if (buffer_info->rxbuf.data)
  3919. goto skip;
  3920. data = e1000_alloc_frag(adapter);
  3921. if (!data) {
  3922. /* Better luck next round */
  3923. adapter->alloc_rx_buff_failed++;
  3924. break;
  3925. }
  3926. /* Fix for errata 23, can't cross 64kB boundary */
  3927. if (!e1000_check_64k_bound(adapter, data, bufsz)) {
  3928. void *olddata = data;
  3929. e_err(rx_err, "skb align check failed: %u bytes at "
  3930. "%p\n", bufsz, data);
  3931. /* Try again, without freeing the previous */
  3932. data = e1000_alloc_frag(adapter);
  3933. /* Failed allocation, critical failure */
  3934. if (!data) {
  3935. skb_free_frag(olddata);
  3936. adapter->alloc_rx_buff_failed++;
  3937. break;
  3938. }
  3939. if (!e1000_check_64k_bound(adapter, data, bufsz)) {
  3940. /* give up */
  3941. skb_free_frag(data);
  3942. skb_free_frag(olddata);
  3943. adapter->alloc_rx_buff_failed++;
  3944. break;
  3945. }
  3946. /* Use new allocation */
  3947. skb_free_frag(olddata);
  3948. }
  3949. buffer_info->dma = dma_map_single(&pdev->dev,
  3950. data,
  3951. adapter->rx_buffer_len,
  3952. DMA_FROM_DEVICE);
  3953. if (dma_mapping_error(&pdev->dev, buffer_info->dma)) {
  3954. skb_free_frag(data);
  3955. buffer_info->dma = 0;
  3956. adapter->alloc_rx_buff_failed++;
  3957. break;
  3958. }
  3959. /* XXX if it was allocated cleanly it will never map to a
  3960. * boundary crossing
  3961. */
  3962. /* Fix for errata 23, can't cross 64kB boundary */
  3963. if (!e1000_check_64k_bound(adapter,
  3964. (void *)(unsigned long)buffer_info->dma,
  3965. adapter->rx_buffer_len)) {
  3966. e_err(rx_err, "dma align check failed: %u bytes at "
  3967. "%p\n", adapter->rx_buffer_len,
  3968. (void *)(unsigned long)buffer_info->dma);
  3969. dma_unmap_single(&pdev->dev, buffer_info->dma,
  3970. adapter->rx_buffer_len,
  3971. DMA_FROM_DEVICE);
  3972. skb_free_frag(data);
  3973. buffer_info->rxbuf.data = NULL;
  3974. buffer_info->dma = 0;
  3975. adapter->alloc_rx_buff_failed++;
  3976. break;
  3977. }
  3978. buffer_info->rxbuf.data = data;
  3979. skip:
  3980. rx_desc = E1000_RX_DESC(*rx_ring, i);
  3981. rx_desc->buffer_addr = cpu_to_le64(buffer_info->dma);
  3982. if (unlikely(++i == rx_ring->count))
  3983. i = 0;
  3984. buffer_info = &rx_ring->buffer_info[i];
  3985. }
  3986. if (likely(rx_ring->next_to_use != i)) {
  3987. rx_ring->next_to_use = i;
  3988. if (unlikely(i-- == 0))
  3989. i = (rx_ring->count - 1);
  3990. /* Force memory writes to complete before letting h/w
  3991. * know there are new descriptors to fetch. (Only
  3992. * applicable for weak-ordered memory model archs,
  3993. * such as IA-64).
  3994. */
  3995. wmb();
  3996. writel(i, hw->hw_addr + rx_ring->rdt);
  3997. }
  3998. }
  3999. /**
  4000. * e1000_smartspeed - Workaround for SmartSpeed on 82541 and 82547 controllers.
  4001. * @adapter:
  4002. **/
  4003. static void e1000_smartspeed(struct e1000_adapter *adapter)
  4004. {
  4005. struct e1000_hw *hw = &adapter->hw;
  4006. u16 phy_status;
  4007. u16 phy_ctrl;
  4008. if ((hw->phy_type != e1000_phy_igp) || !hw->autoneg ||
  4009. !(hw->autoneg_advertised & ADVERTISE_1000_FULL))
  4010. return;
  4011. if (adapter->smartspeed == 0) {
  4012. /* If Master/Slave config fault is asserted twice,
  4013. * we assume back-to-back
  4014. */
  4015. e1000_read_phy_reg(hw, PHY_1000T_STATUS, &phy_status);
  4016. if (!(phy_status & SR_1000T_MS_CONFIG_FAULT))
  4017. return;
  4018. e1000_read_phy_reg(hw, PHY_1000T_STATUS, &phy_status);
  4019. if (!(phy_status & SR_1000T_MS_CONFIG_FAULT))
  4020. return;
  4021. e1000_read_phy_reg(hw, PHY_1000T_CTRL, &phy_ctrl);
  4022. if (phy_ctrl & CR_1000T_MS_ENABLE) {
  4023. phy_ctrl &= ~CR_1000T_MS_ENABLE;
  4024. e1000_write_phy_reg(hw, PHY_1000T_CTRL,
  4025. phy_ctrl);
  4026. adapter->smartspeed++;
  4027. if (!e1000_phy_setup_autoneg(hw) &&
  4028. !e1000_read_phy_reg(hw, PHY_CTRL,
  4029. &phy_ctrl)) {
  4030. phy_ctrl |= (MII_CR_AUTO_NEG_EN |
  4031. MII_CR_RESTART_AUTO_NEG);
  4032. e1000_write_phy_reg(hw, PHY_CTRL,
  4033. phy_ctrl);
  4034. }
  4035. }
  4036. return;
  4037. } else if (adapter->smartspeed == E1000_SMARTSPEED_DOWNSHIFT) {
  4038. /* If still no link, perhaps using 2/3 pair cable */
  4039. e1000_read_phy_reg(hw, PHY_1000T_CTRL, &phy_ctrl);
  4040. phy_ctrl |= CR_1000T_MS_ENABLE;
  4041. e1000_write_phy_reg(hw, PHY_1000T_CTRL, phy_ctrl);
  4042. if (!e1000_phy_setup_autoneg(hw) &&
  4043. !e1000_read_phy_reg(hw, PHY_CTRL, &phy_ctrl)) {
  4044. phy_ctrl |= (MII_CR_AUTO_NEG_EN |
  4045. MII_CR_RESTART_AUTO_NEG);
  4046. e1000_write_phy_reg(hw, PHY_CTRL, phy_ctrl);
  4047. }
  4048. }
  4049. /* Restart process after E1000_SMARTSPEED_MAX iterations */
  4050. if (adapter->smartspeed++ == E1000_SMARTSPEED_MAX)
  4051. adapter->smartspeed = 0;
  4052. }
  4053. /**
  4054. * e1000_ioctl -
  4055. * @netdev:
  4056. * @ifreq:
  4057. * @cmd:
  4058. **/
  4059. static int e1000_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
  4060. {
  4061. switch (cmd) {
  4062. case SIOCGMIIPHY:
  4063. case SIOCGMIIREG:
  4064. case SIOCSMIIREG:
  4065. return e1000_mii_ioctl(netdev, ifr, cmd);
  4066. default:
  4067. return -EOPNOTSUPP;
  4068. }
  4069. }
  4070. /**
  4071. * e1000_mii_ioctl -
  4072. * @netdev:
  4073. * @ifreq:
  4074. * @cmd:
  4075. **/
  4076. static int e1000_mii_ioctl(struct net_device *netdev, struct ifreq *ifr,
  4077. int cmd)
  4078. {
  4079. struct e1000_adapter *adapter = netdev_priv(netdev);
  4080. struct e1000_hw *hw = &adapter->hw;
  4081. struct mii_ioctl_data *data = if_mii(ifr);
  4082. int retval;
  4083. u16 mii_reg;
  4084. unsigned long flags;
  4085. if (hw->media_type != e1000_media_type_copper)
  4086. return -EOPNOTSUPP;
  4087. switch (cmd) {
  4088. case SIOCGMIIPHY:
  4089. data->phy_id = hw->phy_addr;
  4090. break;
  4091. case SIOCGMIIREG:
  4092. spin_lock_irqsave(&adapter->stats_lock, flags);
  4093. if (e1000_read_phy_reg(hw, data->reg_num & 0x1F,
  4094. &data->val_out)) {
  4095. spin_unlock_irqrestore(&adapter->stats_lock, flags);
  4096. return -EIO;
  4097. }
  4098. spin_unlock_irqrestore(&adapter->stats_lock, flags);
  4099. break;
  4100. case SIOCSMIIREG:
  4101. if (data->reg_num & ~(0x1F))
  4102. return -EFAULT;
  4103. mii_reg = data->val_in;
  4104. spin_lock_irqsave(&adapter->stats_lock, flags);
  4105. if (e1000_write_phy_reg(hw, data->reg_num,
  4106. mii_reg)) {
  4107. spin_unlock_irqrestore(&adapter->stats_lock, flags);
  4108. return -EIO;
  4109. }
  4110. spin_unlock_irqrestore(&adapter->stats_lock, flags);
  4111. if (hw->media_type == e1000_media_type_copper) {
  4112. switch (data->reg_num) {
  4113. case PHY_CTRL:
  4114. if (mii_reg & MII_CR_POWER_DOWN)
  4115. break;
  4116. if (mii_reg & MII_CR_AUTO_NEG_EN) {
  4117. hw->autoneg = 1;
  4118. hw->autoneg_advertised = 0x2F;
  4119. } else {
  4120. u32 speed;
  4121. if (mii_reg & 0x40)
  4122. speed = SPEED_1000;
  4123. else if (mii_reg & 0x2000)
  4124. speed = SPEED_100;
  4125. else
  4126. speed = SPEED_10;
  4127. retval = e1000_set_spd_dplx(
  4128. adapter, speed,
  4129. ((mii_reg & 0x100)
  4130. ? DUPLEX_FULL :
  4131. DUPLEX_HALF));
  4132. if (retval)
  4133. return retval;
  4134. }
  4135. if (netif_running(adapter->netdev))
  4136. e1000_reinit_locked(adapter);
  4137. else
  4138. e1000_reset(adapter);
  4139. break;
  4140. case M88E1000_PHY_SPEC_CTRL:
  4141. case M88E1000_EXT_PHY_SPEC_CTRL:
  4142. if (e1000_phy_reset(hw))
  4143. return -EIO;
  4144. break;
  4145. }
  4146. } else {
  4147. switch (data->reg_num) {
  4148. case PHY_CTRL:
  4149. if (mii_reg & MII_CR_POWER_DOWN)
  4150. break;
  4151. if (netif_running(adapter->netdev))
  4152. e1000_reinit_locked(adapter);
  4153. else
  4154. e1000_reset(adapter);
  4155. break;
  4156. }
  4157. }
  4158. break;
  4159. default:
  4160. return -EOPNOTSUPP;
  4161. }
  4162. return E1000_SUCCESS;
  4163. }
  4164. void e1000_pci_set_mwi(struct e1000_hw *hw)
  4165. {
  4166. struct e1000_adapter *adapter = hw->back;
  4167. int ret_val = pci_set_mwi(adapter->pdev);
  4168. if (ret_val)
  4169. e_err(probe, "Error in setting MWI\n");
  4170. }
  4171. void e1000_pci_clear_mwi(struct e1000_hw *hw)
  4172. {
  4173. struct e1000_adapter *adapter = hw->back;
  4174. pci_clear_mwi(adapter->pdev);
  4175. }
  4176. int e1000_pcix_get_mmrbc(struct e1000_hw *hw)
  4177. {
  4178. struct e1000_adapter *adapter = hw->back;
  4179. return pcix_get_mmrbc(adapter->pdev);
  4180. }
  4181. void e1000_pcix_set_mmrbc(struct e1000_hw *hw, int mmrbc)
  4182. {
  4183. struct e1000_adapter *adapter = hw->back;
  4184. pcix_set_mmrbc(adapter->pdev, mmrbc);
  4185. }
  4186. void e1000_io_write(struct e1000_hw *hw, unsigned long port, u32 value)
  4187. {
  4188. outl(value, port);
  4189. }
  4190. static bool e1000_vlan_used(struct e1000_adapter *adapter)
  4191. {
  4192. u16 vid;
  4193. for_each_set_bit(vid, adapter->active_vlans, VLAN_N_VID)
  4194. return true;
  4195. return false;
  4196. }
  4197. static void __e1000_vlan_mode(struct e1000_adapter *adapter,
  4198. netdev_features_t features)
  4199. {
  4200. struct e1000_hw *hw = &adapter->hw;
  4201. u32 ctrl;
  4202. ctrl = er32(CTRL);
  4203. if (features & NETIF_F_HW_VLAN_CTAG_RX) {
  4204. /* enable VLAN tag insert/strip */
  4205. ctrl |= E1000_CTRL_VME;
  4206. } else {
  4207. /* disable VLAN tag insert/strip */
  4208. ctrl &= ~E1000_CTRL_VME;
  4209. }
  4210. ew32(CTRL, ctrl);
  4211. }
  4212. static void e1000_vlan_filter_on_off(struct e1000_adapter *adapter,
  4213. bool filter_on)
  4214. {
  4215. struct e1000_hw *hw = &adapter->hw;
  4216. u32 rctl;
  4217. if (!test_bit(__E1000_DOWN, &adapter->flags))
  4218. e1000_irq_disable(adapter);
  4219. __e1000_vlan_mode(adapter, adapter->netdev->features);
  4220. if (filter_on) {
  4221. /* enable VLAN receive filtering */
  4222. rctl = er32(RCTL);
  4223. rctl &= ~E1000_RCTL_CFIEN;
  4224. if (!(adapter->netdev->flags & IFF_PROMISC))
  4225. rctl |= E1000_RCTL_VFE;
  4226. ew32(RCTL, rctl);
  4227. e1000_update_mng_vlan(adapter);
  4228. } else {
  4229. /* disable VLAN receive filtering */
  4230. rctl = er32(RCTL);
  4231. rctl &= ~E1000_RCTL_VFE;
  4232. ew32(RCTL, rctl);
  4233. }
  4234. if (!test_bit(__E1000_DOWN, &adapter->flags))
  4235. e1000_irq_enable(adapter);
  4236. }
  4237. static void e1000_vlan_mode(struct net_device *netdev,
  4238. netdev_features_t features)
  4239. {
  4240. struct e1000_adapter *adapter = netdev_priv(netdev);
  4241. if (!test_bit(__E1000_DOWN, &adapter->flags))
  4242. e1000_irq_disable(adapter);
  4243. __e1000_vlan_mode(adapter, features);
  4244. if (!test_bit(__E1000_DOWN, &adapter->flags))
  4245. e1000_irq_enable(adapter);
  4246. }
  4247. static int e1000_vlan_rx_add_vid(struct net_device *netdev,
  4248. __be16 proto, u16 vid)
  4249. {
  4250. struct e1000_adapter *adapter = netdev_priv(netdev);
  4251. struct e1000_hw *hw = &adapter->hw;
  4252. u32 vfta, index;
  4253. if ((hw->mng_cookie.status &
  4254. E1000_MNG_DHCP_COOKIE_STATUS_VLAN_SUPPORT) &&
  4255. (vid == adapter->mng_vlan_id))
  4256. return 0;
  4257. if (!e1000_vlan_used(adapter))
  4258. e1000_vlan_filter_on_off(adapter, true);
  4259. /* add VID to filter table */
  4260. index = (vid >> 5) & 0x7F;
  4261. vfta = E1000_READ_REG_ARRAY(hw, VFTA, index);
  4262. vfta |= (1 << (vid & 0x1F));
  4263. e1000_write_vfta(hw, index, vfta);
  4264. set_bit(vid, adapter->active_vlans);
  4265. return 0;
  4266. }
  4267. static int e1000_vlan_rx_kill_vid(struct net_device *netdev,
  4268. __be16 proto, u16 vid)
  4269. {
  4270. struct e1000_adapter *adapter = netdev_priv(netdev);
  4271. struct e1000_hw *hw = &adapter->hw;
  4272. u32 vfta, index;
  4273. if (!test_bit(__E1000_DOWN, &adapter->flags))
  4274. e1000_irq_disable(adapter);
  4275. if (!test_bit(__E1000_DOWN, &adapter->flags))
  4276. e1000_irq_enable(adapter);
  4277. /* remove VID from filter table */
  4278. index = (vid >> 5) & 0x7F;
  4279. vfta = E1000_READ_REG_ARRAY(hw, VFTA, index);
  4280. vfta &= ~(1 << (vid & 0x1F));
  4281. e1000_write_vfta(hw, index, vfta);
  4282. clear_bit(vid, adapter->active_vlans);
  4283. if (!e1000_vlan_used(adapter))
  4284. e1000_vlan_filter_on_off(adapter, false);
  4285. return 0;
  4286. }
  4287. static void e1000_restore_vlan(struct e1000_adapter *adapter)
  4288. {
  4289. u16 vid;
  4290. if (!e1000_vlan_used(adapter))
  4291. return;
  4292. e1000_vlan_filter_on_off(adapter, true);
  4293. for_each_set_bit(vid, adapter->active_vlans, VLAN_N_VID)
  4294. e1000_vlan_rx_add_vid(adapter->netdev, htons(ETH_P_8021Q), vid);
  4295. }
  4296. int e1000_set_spd_dplx(struct e1000_adapter *adapter, u32 spd, u8 dplx)
  4297. {
  4298. struct e1000_hw *hw = &adapter->hw;
  4299. hw->autoneg = 0;
  4300. /* Make sure dplx is at most 1 bit and lsb of speed is not set
  4301. * for the switch() below to work
  4302. */
  4303. if ((spd & 1) || (dplx & ~1))
  4304. goto err_inval;
  4305. /* Fiber NICs only allow 1000 gbps Full duplex */
  4306. if ((hw->media_type == e1000_media_type_fiber) &&
  4307. spd != SPEED_1000 &&
  4308. dplx != DUPLEX_FULL)
  4309. goto err_inval;
  4310. switch (spd + dplx) {
  4311. case SPEED_10 + DUPLEX_HALF:
  4312. hw->forced_speed_duplex = e1000_10_half;
  4313. break;
  4314. case SPEED_10 + DUPLEX_FULL:
  4315. hw->forced_speed_duplex = e1000_10_full;
  4316. break;
  4317. case SPEED_100 + DUPLEX_HALF:
  4318. hw->forced_speed_duplex = e1000_100_half;
  4319. break;
  4320. case SPEED_100 + DUPLEX_FULL:
  4321. hw->forced_speed_duplex = e1000_100_full;
  4322. break;
  4323. case SPEED_1000 + DUPLEX_FULL:
  4324. hw->autoneg = 1;
  4325. hw->autoneg_advertised = ADVERTISE_1000_FULL;
  4326. break;
  4327. case SPEED_1000 + DUPLEX_HALF: /* not supported */
  4328. default:
  4329. goto err_inval;
  4330. }
  4331. /* clear MDI, MDI(-X) override is only allowed when autoneg enabled */
  4332. hw->mdix = AUTO_ALL_MODES;
  4333. return 0;
  4334. err_inval:
  4335. e_err(probe, "Unsupported Speed/Duplex configuration\n");
  4336. return -EINVAL;
  4337. }
  4338. static int __e1000_shutdown(struct pci_dev *pdev, bool *enable_wake)
  4339. {
  4340. struct net_device *netdev = pci_get_drvdata(pdev);
  4341. struct e1000_adapter *adapter = netdev_priv(netdev);
  4342. struct e1000_hw *hw = &adapter->hw;
  4343. u32 ctrl, ctrl_ext, rctl, status;
  4344. u32 wufc = adapter->wol;
  4345. #ifdef CONFIG_PM
  4346. int retval = 0;
  4347. #endif
  4348. netif_device_detach(netdev);
  4349. if (netif_running(netdev)) {
  4350. int count = E1000_CHECK_RESET_COUNT;
  4351. while (test_bit(__E1000_RESETTING, &adapter->flags) && count--)
  4352. usleep_range(10000, 20000);
  4353. WARN_ON(test_bit(__E1000_RESETTING, &adapter->flags));
  4354. e1000_down(adapter);
  4355. }
  4356. #ifdef CONFIG_PM
  4357. retval = pci_save_state(pdev);
  4358. if (retval)
  4359. return retval;
  4360. #endif
  4361. status = er32(STATUS);
  4362. if (status & E1000_STATUS_LU)
  4363. wufc &= ~E1000_WUFC_LNKC;
  4364. if (wufc) {
  4365. e1000_setup_rctl(adapter);
  4366. e1000_set_rx_mode(netdev);
  4367. rctl = er32(RCTL);
  4368. /* turn on all-multi mode if wake on multicast is enabled */
  4369. if (wufc & E1000_WUFC_MC)
  4370. rctl |= E1000_RCTL_MPE;
  4371. /* enable receives in the hardware */
  4372. ew32(RCTL, rctl | E1000_RCTL_EN);
  4373. if (hw->mac_type >= e1000_82540) {
  4374. ctrl = er32(CTRL);
  4375. /* advertise wake from D3Cold */
  4376. #define E1000_CTRL_ADVD3WUC 0x00100000
  4377. /* phy power management enable */
  4378. #define E1000_CTRL_EN_PHY_PWR_MGMT 0x00200000
  4379. ctrl |= E1000_CTRL_ADVD3WUC |
  4380. E1000_CTRL_EN_PHY_PWR_MGMT;
  4381. ew32(CTRL, ctrl);
  4382. }
  4383. if (hw->media_type == e1000_media_type_fiber ||
  4384. hw->media_type == e1000_media_type_internal_serdes) {
  4385. /* keep the laser running in D3 */
  4386. ctrl_ext = er32(CTRL_EXT);
  4387. ctrl_ext |= E1000_CTRL_EXT_SDP7_DATA;
  4388. ew32(CTRL_EXT, ctrl_ext);
  4389. }
  4390. ew32(WUC, E1000_WUC_PME_EN);
  4391. ew32(WUFC, wufc);
  4392. } else {
  4393. ew32(WUC, 0);
  4394. ew32(WUFC, 0);
  4395. }
  4396. e1000_release_manageability(adapter);
  4397. *enable_wake = !!wufc;
  4398. /* make sure adapter isn't asleep if manageability is enabled */
  4399. if (adapter->en_mng_pt)
  4400. *enable_wake = true;
  4401. if (netif_running(netdev))
  4402. e1000_free_irq(adapter);
  4403. if (!test_and_set_bit(__E1000_DISABLED, &adapter->flags))
  4404. pci_disable_device(pdev);
  4405. return 0;
  4406. }
  4407. #ifdef CONFIG_PM
  4408. static int e1000_suspend(struct pci_dev *pdev, pm_message_t state)
  4409. {
  4410. int retval;
  4411. bool wake;
  4412. retval = __e1000_shutdown(pdev, &wake);
  4413. if (retval)
  4414. return retval;
  4415. if (wake) {
  4416. pci_prepare_to_sleep(pdev);
  4417. } else {
  4418. pci_wake_from_d3(pdev, false);
  4419. pci_set_power_state(pdev, PCI_D3hot);
  4420. }
  4421. return 0;
  4422. }
  4423. static int e1000_resume(struct pci_dev *pdev)
  4424. {
  4425. struct net_device *netdev = pci_get_drvdata(pdev);
  4426. struct e1000_adapter *adapter = netdev_priv(netdev);
  4427. struct e1000_hw *hw = &adapter->hw;
  4428. u32 err;
  4429. pci_set_power_state(pdev, PCI_D0);
  4430. pci_restore_state(pdev);
  4431. pci_save_state(pdev);
  4432. if (adapter->need_ioport)
  4433. err = pci_enable_device(pdev);
  4434. else
  4435. err = pci_enable_device_mem(pdev);
  4436. if (err) {
  4437. pr_err("Cannot enable PCI device from suspend\n");
  4438. return err;
  4439. }
  4440. /* flush memory to make sure state is correct */
  4441. smp_mb__before_atomic();
  4442. clear_bit(__E1000_DISABLED, &adapter->flags);
  4443. pci_set_master(pdev);
  4444. pci_enable_wake(pdev, PCI_D3hot, 0);
  4445. pci_enable_wake(pdev, PCI_D3cold, 0);
  4446. if (netif_running(netdev)) {
  4447. err = e1000_request_irq(adapter);
  4448. if (err)
  4449. return err;
  4450. }
  4451. e1000_power_up_phy(adapter);
  4452. e1000_reset(adapter);
  4453. ew32(WUS, ~0);
  4454. e1000_init_manageability(adapter);
  4455. if (netif_running(netdev))
  4456. e1000_up(adapter);
  4457. netif_device_attach(netdev);
  4458. return 0;
  4459. }
  4460. #endif
  4461. static void e1000_shutdown(struct pci_dev *pdev)
  4462. {
  4463. bool wake;
  4464. __e1000_shutdown(pdev, &wake);
  4465. if (system_state == SYSTEM_POWER_OFF) {
  4466. pci_wake_from_d3(pdev, wake);
  4467. pci_set_power_state(pdev, PCI_D3hot);
  4468. }
  4469. }
  4470. #ifdef CONFIG_NET_POLL_CONTROLLER
  4471. /* Polling 'interrupt' - used by things like netconsole to send skbs
  4472. * without having to re-enable interrupts. It's not called while
  4473. * the interrupt routine is executing.
  4474. */
  4475. static void e1000_netpoll(struct net_device *netdev)
  4476. {
  4477. struct e1000_adapter *adapter = netdev_priv(netdev);
  4478. if (disable_hardirq(adapter->pdev->irq))
  4479. e1000_intr(adapter->pdev->irq, netdev);
  4480. enable_irq(adapter->pdev->irq);
  4481. }
  4482. #endif
  4483. /**
  4484. * e1000_io_error_detected - called when PCI error is detected
  4485. * @pdev: Pointer to PCI device
  4486. * @state: The current pci connection state
  4487. *
  4488. * This function is called after a PCI bus error affecting
  4489. * this device has been detected.
  4490. */
  4491. static pci_ers_result_t e1000_io_error_detected(struct pci_dev *pdev,
  4492. pci_channel_state_t state)
  4493. {
  4494. struct net_device *netdev = pci_get_drvdata(pdev);
  4495. struct e1000_adapter *adapter = netdev_priv(netdev);
  4496. netif_device_detach(netdev);
  4497. if (state == pci_channel_io_perm_failure)
  4498. return PCI_ERS_RESULT_DISCONNECT;
  4499. if (netif_running(netdev))
  4500. e1000_down(adapter);
  4501. if (!test_and_set_bit(__E1000_DISABLED, &adapter->flags))
  4502. pci_disable_device(pdev);
  4503. /* Request a slot slot reset. */
  4504. return PCI_ERS_RESULT_NEED_RESET;
  4505. }
  4506. /**
  4507. * e1000_io_slot_reset - called after the pci bus has been reset.
  4508. * @pdev: Pointer to PCI device
  4509. *
  4510. * Restart the card from scratch, as if from a cold-boot. Implementation
  4511. * resembles the first-half of the e1000_resume routine.
  4512. */
  4513. static pci_ers_result_t e1000_io_slot_reset(struct pci_dev *pdev)
  4514. {
  4515. struct net_device *netdev = pci_get_drvdata(pdev);
  4516. struct e1000_adapter *adapter = netdev_priv(netdev);
  4517. struct e1000_hw *hw = &adapter->hw;
  4518. int err;
  4519. if (adapter->need_ioport)
  4520. err = pci_enable_device(pdev);
  4521. else
  4522. err = pci_enable_device_mem(pdev);
  4523. if (err) {
  4524. pr_err("Cannot re-enable PCI device after reset.\n");
  4525. return PCI_ERS_RESULT_DISCONNECT;
  4526. }
  4527. /* flush memory to make sure state is correct */
  4528. smp_mb__before_atomic();
  4529. clear_bit(__E1000_DISABLED, &adapter->flags);
  4530. pci_set_master(pdev);
  4531. pci_enable_wake(pdev, PCI_D3hot, 0);
  4532. pci_enable_wake(pdev, PCI_D3cold, 0);
  4533. e1000_reset(adapter);
  4534. ew32(WUS, ~0);
  4535. return PCI_ERS_RESULT_RECOVERED;
  4536. }
  4537. /**
  4538. * e1000_io_resume - called when traffic can start flowing again.
  4539. * @pdev: Pointer to PCI device
  4540. *
  4541. * This callback is called when the error recovery driver tells us that
  4542. * its OK to resume normal operation. Implementation resembles the
  4543. * second-half of the e1000_resume routine.
  4544. */
  4545. static void e1000_io_resume(struct pci_dev *pdev)
  4546. {
  4547. struct net_device *netdev = pci_get_drvdata(pdev);
  4548. struct e1000_adapter *adapter = netdev_priv(netdev);
  4549. e1000_init_manageability(adapter);
  4550. if (netif_running(netdev)) {
  4551. if (e1000_up(adapter)) {
  4552. pr_info("can't bring device back up after reset\n");
  4553. return;
  4554. }
  4555. }
  4556. netif_device_attach(netdev);
  4557. }
  4558. /* e1000_main.c */