e1000_main.c 146 KB

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