qlge_main.c 132 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932
  1. /*
  2. * QLogic qlge NIC HBA Driver
  3. * Copyright (c) 2003-2008 QLogic Corporation
  4. * See LICENSE.qlge for copyright and licensing details.
  5. * Author: Linux qlge network device driver by
  6. * Ron Mercer <ron.mercer@qlogic.com>
  7. */
  8. #include <linux/kernel.h>
  9. #include <linux/init.h>
  10. #include <linux/bitops.h>
  11. #include <linux/types.h>
  12. #include <linux/module.h>
  13. #include <linux/list.h>
  14. #include <linux/pci.h>
  15. #include <linux/dma-mapping.h>
  16. #include <linux/pagemap.h>
  17. #include <linux/sched.h>
  18. #include <linux/slab.h>
  19. #include <linux/dmapool.h>
  20. #include <linux/mempool.h>
  21. #include <linux/spinlock.h>
  22. #include <linux/kthread.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/errno.h>
  25. #include <linux/ioport.h>
  26. #include <linux/in.h>
  27. #include <linux/ip.h>
  28. #include <linux/ipv6.h>
  29. #include <net/ipv6.h>
  30. #include <linux/tcp.h>
  31. #include <linux/udp.h>
  32. #include <linux/if_arp.h>
  33. #include <linux/if_ether.h>
  34. #include <linux/netdevice.h>
  35. #include <linux/etherdevice.h>
  36. #include <linux/ethtool.h>
  37. #include <linux/if_vlan.h>
  38. #include <linux/skbuff.h>
  39. #include <linux/delay.h>
  40. #include <linux/mm.h>
  41. #include <linux/vmalloc.h>
  42. #include <linux/prefetch.h>
  43. #include <net/ip6_checksum.h>
  44. #include "qlge.h"
  45. char qlge_driver_name[] = DRV_NAME;
  46. const char qlge_driver_version[] = DRV_VERSION;
  47. MODULE_AUTHOR("Ron Mercer <ron.mercer@qlogic.com>");
  48. MODULE_DESCRIPTION(DRV_STRING " ");
  49. MODULE_LICENSE("GPL");
  50. MODULE_VERSION(DRV_VERSION);
  51. static const u32 default_msg =
  52. NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_LINK |
  53. /* NETIF_MSG_TIMER | */
  54. NETIF_MSG_IFDOWN |
  55. NETIF_MSG_IFUP |
  56. NETIF_MSG_RX_ERR |
  57. NETIF_MSG_TX_ERR |
  58. /* NETIF_MSG_TX_QUEUED | */
  59. /* NETIF_MSG_INTR | NETIF_MSG_TX_DONE | NETIF_MSG_RX_STATUS | */
  60. /* NETIF_MSG_PKTDATA | */
  61. NETIF_MSG_HW | NETIF_MSG_WOL | 0;
  62. static int debug = -1; /* defaults above */
  63. module_param(debug, int, 0664);
  64. MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all)");
  65. #define MSIX_IRQ 0
  66. #define MSI_IRQ 1
  67. #define LEG_IRQ 2
  68. static int qlge_irq_type = MSIX_IRQ;
  69. module_param(qlge_irq_type, int, 0664);
  70. MODULE_PARM_DESC(qlge_irq_type, "0 = MSI-X, 1 = MSI, 2 = Legacy.");
  71. static int qlge_mpi_coredump;
  72. module_param(qlge_mpi_coredump, int, 0);
  73. MODULE_PARM_DESC(qlge_mpi_coredump,
  74. "Option to enable MPI firmware dump. "
  75. "Default is OFF - Do Not allocate memory. ");
  76. static int qlge_force_coredump;
  77. module_param(qlge_force_coredump, int, 0);
  78. MODULE_PARM_DESC(qlge_force_coredump,
  79. "Option to allow force of firmware core dump. "
  80. "Default is OFF - Do not allow.");
  81. static DEFINE_PCI_DEVICE_TABLE(qlge_pci_tbl) = {
  82. {PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, QLGE_DEVICE_ID_8012)},
  83. {PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, QLGE_DEVICE_ID_8000)},
  84. /* required last entry */
  85. {0,}
  86. };
  87. MODULE_DEVICE_TABLE(pci, qlge_pci_tbl);
  88. static int ql_wol(struct ql_adapter *qdev);
  89. static void qlge_set_multicast_list(struct net_device *ndev);
  90. /* This hardware semaphore causes exclusive access to
  91. * resources shared between the NIC driver, MPI firmware,
  92. * FCOE firmware and the FC driver.
  93. */
  94. static int ql_sem_trylock(struct ql_adapter *qdev, u32 sem_mask)
  95. {
  96. u32 sem_bits = 0;
  97. switch (sem_mask) {
  98. case SEM_XGMAC0_MASK:
  99. sem_bits = SEM_SET << SEM_XGMAC0_SHIFT;
  100. break;
  101. case SEM_XGMAC1_MASK:
  102. sem_bits = SEM_SET << SEM_XGMAC1_SHIFT;
  103. break;
  104. case SEM_ICB_MASK:
  105. sem_bits = SEM_SET << SEM_ICB_SHIFT;
  106. break;
  107. case SEM_MAC_ADDR_MASK:
  108. sem_bits = SEM_SET << SEM_MAC_ADDR_SHIFT;
  109. break;
  110. case SEM_FLASH_MASK:
  111. sem_bits = SEM_SET << SEM_FLASH_SHIFT;
  112. break;
  113. case SEM_PROBE_MASK:
  114. sem_bits = SEM_SET << SEM_PROBE_SHIFT;
  115. break;
  116. case SEM_RT_IDX_MASK:
  117. sem_bits = SEM_SET << SEM_RT_IDX_SHIFT;
  118. break;
  119. case SEM_PROC_REG_MASK:
  120. sem_bits = SEM_SET << SEM_PROC_REG_SHIFT;
  121. break;
  122. default:
  123. netif_alert(qdev, probe, qdev->ndev, "bad Semaphore mask!.\n");
  124. return -EINVAL;
  125. }
  126. ql_write32(qdev, SEM, sem_bits | sem_mask);
  127. return !(ql_read32(qdev, SEM) & sem_bits);
  128. }
  129. int ql_sem_spinlock(struct ql_adapter *qdev, u32 sem_mask)
  130. {
  131. unsigned int wait_count = 30;
  132. do {
  133. if (!ql_sem_trylock(qdev, sem_mask))
  134. return 0;
  135. udelay(100);
  136. } while (--wait_count);
  137. return -ETIMEDOUT;
  138. }
  139. void ql_sem_unlock(struct ql_adapter *qdev, u32 sem_mask)
  140. {
  141. ql_write32(qdev, SEM, sem_mask);
  142. ql_read32(qdev, SEM); /* flush */
  143. }
  144. /* This function waits for a specific bit to come ready
  145. * in a given register. It is used mostly by the initialize
  146. * process, but is also used in kernel thread API such as
  147. * netdev->set_multi, netdev->set_mac_address, netdev->vlan_rx_add_vid.
  148. */
  149. int ql_wait_reg_rdy(struct ql_adapter *qdev, u32 reg, u32 bit, u32 err_bit)
  150. {
  151. u32 temp;
  152. int count = UDELAY_COUNT;
  153. while (count) {
  154. temp = ql_read32(qdev, reg);
  155. /* check for errors */
  156. if (temp & err_bit) {
  157. netif_alert(qdev, probe, qdev->ndev,
  158. "register 0x%.08x access error, value = 0x%.08x!.\n",
  159. reg, temp);
  160. return -EIO;
  161. } else if (temp & bit)
  162. return 0;
  163. udelay(UDELAY_DELAY);
  164. count--;
  165. }
  166. netif_alert(qdev, probe, qdev->ndev,
  167. "Timed out waiting for reg %x to come ready.\n", reg);
  168. return -ETIMEDOUT;
  169. }
  170. /* The CFG register is used to download TX and RX control blocks
  171. * to the chip. This function waits for an operation to complete.
  172. */
  173. static int ql_wait_cfg(struct ql_adapter *qdev, u32 bit)
  174. {
  175. int count = UDELAY_COUNT;
  176. u32 temp;
  177. while (count) {
  178. temp = ql_read32(qdev, CFG);
  179. if (temp & CFG_LE)
  180. return -EIO;
  181. if (!(temp & bit))
  182. return 0;
  183. udelay(UDELAY_DELAY);
  184. count--;
  185. }
  186. return -ETIMEDOUT;
  187. }
  188. /* Used to issue init control blocks to hw. Maps control block,
  189. * sets address, triggers download, waits for completion.
  190. */
  191. int ql_write_cfg(struct ql_adapter *qdev, void *ptr, int size, u32 bit,
  192. u16 q_id)
  193. {
  194. u64 map;
  195. int status = 0;
  196. int direction;
  197. u32 mask;
  198. u32 value;
  199. direction =
  200. (bit & (CFG_LRQ | CFG_LR | CFG_LCQ)) ? PCI_DMA_TODEVICE :
  201. PCI_DMA_FROMDEVICE;
  202. map = pci_map_single(qdev->pdev, ptr, size, direction);
  203. if (pci_dma_mapping_error(qdev->pdev, map)) {
  204. netif_err(qdev, ifup, qdev->ndev, "Couldn't map DMA area.\n");
  205. return -ENOMEM;
  206. }
  207. status = ql_sem_spinlock(qdev, SEM_ICB_MASK);
  208. if (status)
  209. return status;
  210. status = ql_wait_cfg(qdev, bit);
  211. if (status) {
  212. netif_err(qdev, ifup, qdev->ndev,
  213. "Timed out waiting for CFG to come ready.\n");
  214. goto exit;
  215. }
  216. ql_write32(qdev, ICB_L, (u32) map);
  217. ql_write32(qdev, ICB_H, (u32) (map >> 32));
  218. mask = CFG_Q_MASK | (bit << 16);
  219. value = bit | (q_id << CFG_Q_SHIFT);
  220. ql_write32(qdev, CFG, (mask | value));
  221. /*
  222. * Wait for the bit to clear after signaling hw.
  223. */
  224. status = ql_wait_cfg(qdev, bit);
  225. exit:
  226. ql_sem_unlock(qdev, SEM_ICB_MASK); /* does flush too */
  227. pci_unmap_single(qdev->pdev, map, size, direction);
  228. return status;
  229. }
  230. /* Get a specific MAC address from the CAM. Used for debug and reg dump. */
  231. int ql_get_mac_addr_reg(struct ql_adapter *qdev, u32 type, u16 index,
  232. u32 *value)
  233. {
  234. u32 offset = 0;
  235. int status;
  236. switch (type) {
  237. case MAC_ADDR_TYPE_MULTI_MAC:
  238. case MAC_ADDR_TYPE_CAM_MAC:
  239. {
  240. status =
  241. ql_wait_reg_rdy(qdev,
  242. MAC_ADDR_IDX, MAC_ADDR_MW, 0);
  243. if (status)
  244. goto exit;
  245. ql_write32(qdev, MAC_ADDR_IDX, (offset++) | /* offset */
  246. (index << MAC_ADDR_IDX_SHIFT) | /* index */
  247. MAC_ADDR_ADR | MAC_ADDR_RS | type); /* type */
  248. status =
  249. ql_wait_reg_rdy(qdev,
  250. MAC_ADDR_IDX, MAC_ADDR_MR, 0);
  251. if (status)
  252. goto exit;
  253. *value++ = ql_read32(qdev, MAC_ADDR_DATA);
  254. status =
  255. ql_wait_reg_rdy(qdev,
  256. MAC_ADDR_IDX, MAC_ADDR_MW, 0);
  257. if (status)
  258. goto exit;
  259. ql_write32(qdev, MAC_ADDR_IDX, (offset++) | /* offset */
  260. (index << MAC_ADDR_IDX_SHIFT) | /* index */
  261. MAC_ADDR_ADR | MAC_ADDR_RS | type); /* type */
  262. status =
  263. ql_wait_reg_rdy(qdev,
  264. MAC_ADDR_IDX, MAC_ADDR_MR, 0);
  265. if (status)
  266. goto exit;
  267. *value++ = ql_read32(qdev, MAC_ADDR_DATA);
  268. if (type == MAC_ADDR_TYPE_CAM_MAC) {
  269. status =
  270. ql_wait_reg_rdy(qdev,
  271. MAC_ADDR_IDX, MAC_ADDR_MW, 0);
  272. if (status)
  273. goto exit;
  274. ql_write32(qdev, MAC_ADDR_IDX, (offset++) | /* offset */
  275. (index << MAC_ADDR_IDX_SHIFT) | /* index */
  276. MAC_ADDR_ADR | MAC_ADDR_RS | type); /* type */
  277. status =
  278. ql_wait_reg_rdy(qdev, MAC_ADDR_IDX,
  279. MAC_ADDR_MR, 0);
  280. if (status)
  281. goto exit;
  282. *value++ = ql_read32(qdev, MAC_ADDR_DATA);
  283. }
  284. break;
  285. }
  286. case MAC_ADDR_TYPE_VLAN:
  287. case MAC_ADDR_TYPE_MULTI_FLTR:
  288. default:
  289. netif_crit(qdev, ifup, qdev->ndev,
  290. "Address type %d not yet supported.\n", type);
  291. status = -EPERM;
  292. }
  293. exit:
  294. return status;
  295. }
  296. /* Set up a MAC, multicast or VLAN address for the
  297. * inbound frame matching.
  298. */
  299. static int ql_set_mac_addr_reg(struct ql_adapter *qdev, u8 *addr, u32 type,
  300. u16 index)
  301. {
  302. u32 offset = 0;
  303. int status = 0;
  304. switch (type) {
  305. case MAC_ADDR_TYPE_MULTI_MAC:
  306. {
  307. u32 upper = (addr[0] << 8) | addr[1];
  308. u32 lower = (addr[2] << 24) | (addr[3] << 16) |
  309. (addr[4] << 8) | (addr[5]);
  310. status =
  311. ql_wait_reg_rdy(qdev,
  312. MAC_ADDR_IDX, MAC_ADDR_MW, 0);
  313. if (status)
  314. goto exit;
  315. ql_write32(qdev, MAC_ADDR_IDX, (offset++) |
  316. (index << MAC_ADDR_IDX_SHIFT) |
  317. type | MAC_ADDR_E);
  318. ql_write32(qdev, MAC_ADDR_DATA, lower);
  319. status =
  320. ql_wait_reg_rdy(qdev,
  321. MAC_ADDR_IDX, MAC_ADDR_MW, 0);
  322. if (status)
  323. goto exit;
  324. ql_write32(qdev, MAC_ADDR_IDX, (offset++) |
  325. (index << MAC_ADDR_IDX_SHIFT) |
  326. type | MAC_ADDR_E);
  327. ql_write32(qdev, MAC_ADDR_DATA, upper);
  328. status =
  329. ql_wait_reg_rdy(qdev,
  330. MAC_ADDR_IDX, MAC_ADDR_MW, 0);
  331. if (status)
  332. goto exit;
  333. break;
  334. }
  335. case MAC_ADDR_TYPE_CAM_MAC:
  336. {
  337. u32 cam_output;
  338. u32 upper = (addr[0] << 8) | addr[1];
  339. u32 lower =
  340. (addr[2] << 24) | (addr[3] << 16) | (addr[4] << 8) |
  341. (addr[5]);
  342. status =
  343. ql_wait_reg_rdy(qdev,
  344. MAC_ADDR_IDX, MAC_ADDR_MW, 0);
  345. if (status)
  346. goto exit;
  347. ql_write32(qdev, MAC_ADDR_IDX, (offset++) | /* offset */
  348. (index << MAC_ADDR_IDX_SHIFT) | /* index */
  349. type); /* type */
  350. ql_write32(qdev, MAC_ADDR_DATA, lower);
  351. status =
  352. ql_wait_reg_rdy(qdev,
  353. MAC_ADDR_IDX, MAC_ADDR_MW, 0);
  354. if (status)
  355. goto exit;
  356. ql_write32(qdev, MAC_ADDR_IDX, (offset++) | /* offset */
  357. (index << MAC_ADDR_IDX_SHIFT) | /* index */
  358. type); /* type */
  359. ql_write32(qdev, MAC_ADDR_DATA, upper);
  360. status =
  361. ql_wait_reg_rdy(qdev,
  362. MAC_ADDR_IDX, MAC_ADDR_MW, 0);
  363. if (status)
  364. goto exit;
  365. ql_write32(qdev, MAC_ADDR_IDX, (offset) | /* offset */
  366. (index << MAC_ADDR_IDX_SHIFT) | /* index */
  367. type); /* type */
  368. /* This field should also include the queue id
  369. and possibly the function id. Right now we hardcode
  370. the route field to NIC core.
  371. */
  372. cam_output = (CAM_OUT_ROUTE_NIC |
  373. (qdev->
  374. func << CAM_OUT_FUNC_SHIFT) |
  375. (0 << CAM_OUT_CQ_ID_SHIFT));
  376. if (qdev->ndev->features & NETIF_F_HW_VLAN_RX)
  377. cam_output |= CAM_OUT_RV;
  378. /* route to NIC core */
  379. ql_write32(qdev, MAC_ADDR_DATA, cam_output);
  380. break;
  381. }
  382. case MAC_ADDR_TYPE_VLAN:
  383. {
  384. u32 enable_bit = *((u32 *) &addr[0]);
  385. /* For VLAN, the addr actually holds a bit that
  386. * either enables or disables the vlan id we are
  387. * addressing. It's either MAC_ADDR_E on or off.
  388. * That's bit-27 we're talking about.
  389. */
  390. status =
  391. ql_wait_reg_rdy(qdev,
  392. MAC_ADDR_IDX, MAC_ADDR_MW, 0);
  393. if (status)
  394. goto exit;
  395. ql_write32(qdev, MAC_ADDR_IDX, offset | /* offset */
  396. (index << MAC_ADDR_IDX_SHIFT) | /* index */
  397. type | /* type */
  398. enable_bit); /* enable/disable */
  399. break;
  400. }
  401. case MAC_ADDR_TYPE_MULTI_FLTR:
  402. default:
  403. netif_crit(qdev, ifup, qdev->ndev,
  404. "Address type %d not yet supported.\n", type);
  405. status = -EPERM;
  406. }
  407. exit:
  408. return status;
  409. }
  410. /* Set or clear MAC address in hardware. We sometimes
  411. * have to clear it to prevent wrong frame routing
  412. * especially in a bonding environment.
  413. */
  414. static int ql_set_mac_addr(struct ql_adapter *qdev, int set)
  415. {
  416. int status;
  417. char zero_mac_addr[ETH_ALEN];
  418. char *addr;
  419. if (set) {
  420. addr = &qdev->current_mac_addr[0];
  421. netif_printk(qdev, ifup, KERN_DEBUG, qdev->ndev,
  422. "Set Mac addr %pM\n", addr);
  423. } else {
  424. memset(zero_mac_addr, 0, ETH_ALEN);
  425. addr = &zero_mac_addr[0];
  426. netif_printk(qdev, ifup, KERN_DEBUG, qdev->ndev,
  427. "Clearing MAC address\n");
  428. }
  429. status = ql_sem_spinlock(qdev, SEM_MAC_ADDR_MASK);
  430. if (status)
  431. return status;
  432. status = ql_set_mac_addr_reg(qdev, (u8 *) addr,
  433. MAC_ADDR_TYPE_CAM_MAC, qdev->func * MAX_CQ);
  434. ql_sem_unlock(qdev, SEM_MAC_ADDR_MASK);
  435. if (status)
  436. netif_err(qdev, ifup, qdev->ndev,
  437. "Failed to init mac address.\n");
  438. return status;
  439. }
  440. void ql_link_on(struct ql_adapter *qdev)
  441. {
  442. netif_err(qdev, link, qdev->ndev, "Link is up.\n");
  443. netif_carrier_on(qdev->ndev);
  444. ql_set_mac_addr(qdev, 1);
  445. }
  446. void ql_link_off(struct ql_adapter *qdev)
  447. {
  448. netif_err(qdev, link, qdev->ndev, "Link is down.\n");
  449. netif_carrier_off(qdev->ndev);
  450. ql_set_mac_addr(qdev, 0);
  451. }
  452. /* Get a specific frame routing value from the CAM.
  453. * Used for debug and reg dump.
  454. */
  455. int ql_get_routing_reg(struct ql_adapter *qdev, u32 index, u32 *value)
  456. {
  457. int status = 0;
  458. status = ql_wait_reg_rdy(qdev, RT_IDX, RT_IDX_MW, 0);
  459. if (status)
  460. goto exit;
  461. ql_write32(qdev, RT_IDX,
  462. RT_IDX_TYPE_NICQ | RT_IDX_RS | (index << RT_IDX_IDX_SHIFT));
  463. status = ql_wait_reg_rdy(qdev, RT_IDX, RT_IDX_MR, 0);
  464. if (status)
  465. goto exit;
  466. *value = ql_read32(qdev, RT_DATA);
  467. exit:
  468. return status;
  469. }
  470. /* The NIC function for this chip has 16 routing indexes. Each one can be used
  471. * to route different frame types to various inbound queues. We send broadcast/
  472. * multicast/error frames to the default queue for slow handling,
  473. * and CAM hit/RSS frames to the fast handling queues.
  474. */
  475. static int ql_set_routing_reg(struct ql_adapter *qdev, u32 index, u32 mask,
  476. int enable)
  477. {
  478. int status = -EINVAL; /* Return error if no mask match. */
  479. u32 value = 0;
  480. switch (mask) {
  481. case RT_IDX_CAM_HIT:
  482. {
  483. value = RT_IDX_DST_CAM_Q | /* dest */
  484. RT_IDX_TYPE_NICQ | /* type */
  485. (RT_IDX_CAM_HIT_SLOT << RT_IDX_IDX_SHIFT);/* index */
  486. break;
  487. }
  488. case RT_IDX_VALID: /* Promiscuous Mode frames. */
  489. {
  490. value = RT_IDX_DST_DFLT_Q | /* dest */
  491. RT_IDX_TYPE_NICQ | /* type */
  492. (RT_IDX_PROMISCUOUS_SLOT << RT_IDX_IDX_SHIFT);/* index */
  493. break;
  494. }
  495. case RT_IDX_ERR: /* Pass up MAC,IP,TCP/UDP error frames. */
  496. {
  497. value = RT_IDX_DST_DFLT_Q | /* dest */
  498. RT_IDX_TYPE_NICQ | /* type */
  499. (RT_IDX_ALL_ERR_SLOT << RT_IDX_IDX_SHIFT);/* index */
  500. break;
  501. }
  502. case RT_IDX_IP_CSUM_ERR: /* Pass up IP CSUM error frames. */
  503. {
  504. value = RT_IDX_DST_DFLT_Q | /* dest */
  505. RT_IDX_TYPE_NICQ | /* type */
  506. (RT_IDX_IP_CSUM_ERR_SLOT <<
  507. RT_IDX_IDX_SHIFT); /* index */
  508. break;
  509. }
  510. case RT_IDX_TU_CSUM_ERR: /* Pass up TCP/UDP CSUM error frames. */
  511. {
  512. value = RT_IDX_DST_DFLT_Q | /* dest */
  513. RT_IDX_TYPE_NICQ | /* type */
  514. (RT_IDX_TCP_UDP_CSUM_ERR_SLOT <<
  515. RT_IDX_IDX_SHIFT); /* index */
  516. break;
  517. }
  518. case RT_IDX_BCAST: /* Pass up Broadcast frames to default Q. */
  519. {
  520. value = RT_IDX_DST_DFLT_Q | /* dest */
  521. RT_IDX_TYPE_NICQ | /* type */
  522. (RT_IDX_BCAST_SLOT << RT_IDX_IDX_SHIFT);/* index */
  523. break;
  524. }
  525. case RT_IDX_MCAST: /* Pass up All Multicast frames. */
  526. {
  527. value = RT_IDX_DST_DFLT_Q | /* dest */
  528. RT_IDX_TYPE_NICQ | /* type */
  529. (RT_IDX_ALLMULTI_SLOT << RT_IDX_IDX_SHIFT);/* index */
  530. break;
  531. }
  532. case RT_IDX_MCAST_MATCH: /* Pass up matched Multicast frames. */
  533. {
  534. value = RT_IDX_DST_DFLT_Q | /* dest */
  535. RT_IDX_TYPE_NICQ | /* type */
  536. (RT_IDX_MCAST_MATCH_SLOT << RT_IDX_IDX_SHIFT);/* index */
  537. break;
  538. }
  539. case RT_IDX_RSS_MATCH: /* Pass up matched RSS frames. */
  540. {
  541. value = RT_IDX_DST_RSS | /* dest */
  542. RT_IDX_TYPE_NICQ | /* type */
  543. (RT_IDX_RSS_MATCH_SLOT << RT_IDX_IDX_SHIFT);/* index */
  544. break;
  545. }
  546. case 0: /* Clear the E-bit on an entry. */
  547. {
  548. value = RT_IDX_DST_DFLT_Q | /* dest */
  549. RT_IDX_TYPE_NICQ | /* type */
  550. (index << RT_IDX_IDX_SHIFT);/* index */
  551. break;
  552. }
  553. default:
  554. netif_err(qdev, ifup, qdev->ndev,
  555. "Mask type %d not yet supported.\n", mask);
  556. status = -EPERM;
  557. goto exit;
  558. }
  559. if (value) {
  560. status = ql_wait_reg_rdy(qdev, RT_IDX, RT_IDX_MW, 0);
  561. if (status)
  562. goto exit;
  563. value |= (enable ? RT_IDX_E : 0);
  564. ql_write32(qdev, RT_IDX, value);
  565. ql_write32(qdev, RT_DATA, enable ? mask : 0);
  566. }
  567. exit:
  568. return status;
  569. }
  570. static void ql_enable_interrupts(struct ql_adapter *qdev)
  571. {
  572. ql_write32(qdev, INTR_EN, (INTR_EN_EI << 16) | INTR_EN_EI);
  573. }
  574. static void ql_disable_interrupts(struct ql_adapter *qdev)
  575. {
  576. ql_write32(qdev, INTR_EN, (INTR_EN_EI << 16));
  577. }
  578. /* If we're running with multiple MSI-X vectors then we enable on the fly.
  579. * Otherwise, we may have multiple outstanding workers and don't want to
  580. * enable until the last one finishes. In this case, the irq_cnt gets
  581. * incremented every time we queue a worker and decremented every time
  582. * a worker finishes. Once it hits zero we enable the interrupt.
  583. */
  584. u32 ql_enable_completion_interrupt(struct ql_adapter *qdev, u32 intr)
  585. {
  586. u32 var = 0;
  587. unsigned long hw_flags = 0;
  588. struct intr_context *ctx = qdev->intr_context + intr;
  589. if (likely(test_bit(QL_MSIX_ENABLED, &qdev->flags) && intr)) {
  590. /* Always enable if we're MSIX multi interrupts and
  591. * it's not the default (zeroeth) interrupt.
  592. */
  593. ql_write32(qdev, INTR_EN,
  594. ctx->intr_en_mask);
  595. var = ql_read32(qdev, STS);
  596. return var;
  597. }
  598. spin_lock_irqsave(&qdev->hw_lock, hw_flags);
  599. if (atomic_dec_and_test(&ctx->irq_cnt)) {
  600. ql_write32(qdev, INTR_EN,
  601. ctx->intr_en_mask);
  602. var = ql_read32(qdev, STS);
  603. }
  604. spin_unlock_irqrestore(&qdev->hw_lock, hw_flags);
  605. return var;
  606. }
  607. static u32 ql_disable_completion_interrupt(struct ql_adapter *qdev, u32 intr)
  608. {
  609. u32 var = 0;
  610. struct intr_context *ctx;
  611. /* HW disables for us if we're MSIX multi interrupts and
  612. * it's not the default (zeroeth) interrupt.
  613. */
  614. if (likely(test_bit(QL_MSIX_ENABLED, &qdev->flags) && intr))
  615. return 0;
  616. ctx = qdev->intr_context + intr;
  617. spin_lock(&qdev->hw_lock);
  618. if (!atomic_read(&ctx->irq_cnt)) {
  619. ql_write32(qdev, INTR_EN,
  620. ctx->intr_dis_mask);
  621. var = ql_read32(qdev, STS);
  622. }
  623. atomic_inc(&ctx->irq_cnt);
  624. spin_unlock(&qdev->hw_lock);
  625. return var;
  626. }
  627. static void ql_enable_all_completion_interrupts(struct ql_adapter *qdev)
  628. {
  629. int i;
  630. for (i = 0; i < qdev->intr_count; i++) {
  631. /* The enable call does a atomic_dec_and_test
  632. * and enables only if the result is zero.
  633. * So we precharge it here.
  634. */
  635. if (unlikely(!test_bit(QL_MSIX_ENABLED, &qdev->flags) ||
  636. i == 0))
  637. atomic_set(&qdev->intr_context[i].irq_cnt, 1);
  638. ql_enable_completion_interrupt(qdev, i);
  639. }
  640. }
  641. static int ql_validate_flash(struct ql_adapter *qdev, u32 size, const char *str)
  642. {
  643. int status, i;
  644. u16 csum = 0;
  645. __le16 *flash = (__le16 *)&qdev->flash;
  646. status = strncmp((char *)&qdev->flash, str, 4);
  647. if (status) {
  648. netif_err(qdev, ifup, qdev->ndev, "Invalid flash signature.\n");
  649. return status;
  650. }
  651. for (i = 0; i < size; i++)
  652. csum += le16_to_cpu(*flash++);
  653. if (csum)
  654. netif_err(qdev, ifup, qdev->ndev,
  655. "Invalid flash checksum, csum = 0x%.04x.\n", csum);
  656. return csum;
  657. }
  658. static int ql_read_flash_word(struct ql_adapter *qdev, int offset, __le32 *data)
  659. {
  660. int status = 0;
  661. /* wait for reg to come ready */
  662. status = ql_wait_reg_rdy(qdev,
  663. FLASH_ADDR, FLASH_ADDR_RDY, FLASH_ADDR_ERR);
  664. if (status)
  665. goto exit;
  666. /* set up for reg read */
  667. ql_write32(qdev, FLASH_ADDR, FLASH_ADDR_R | offset);
  668. /* wait for reg to come ready */
  669. status = ql_wait_reg_rdy(qdev,
  670. FLASH_ADDR, FLASH_ADDR_RDY, FLASH_ADDR_ERR);
  671. if (status)
  672. goto exit;
  673. /* This data is stored on flash as an array of
  674. * __le32. Since ql_read32() returns cpu endian
  675. * we need to swap it back.
  676. */
  677. *data = cpu_to_le32(ql_read32(qdev, FLASH_DATA));
  678. exit:
  679. return status;
  680. }
  681. static int ql_get_8000_flash_params(struct ql_adapter *qdev)
  682. {
  683. u32 i, size;
  684. int status;
  685. __le32 *p = (__le32 *)&qdev->flash;
  686. u32 offset;
  687. u8 mac_addr[6];
  688. /* Get flash offset for function and adjust
  689. * for dword access.
  690. */
  691. if (!qdev->port)
  692. offset = FUNC0_FLASH_OFFSET / sizeof(u32);
  693. else
  694. offset = FUNC1_FLASH_OFFSET / sizeof(u32);
  695. if (ql_sem_spinlock(qdev, SEM_FLASH_MASK))
  696. return -ETIMEDOUT;
  697. size = sizeof(struct flash_params_8000) / sizeof(u32);
  698. for (i = 0; i < size; i++, p++) {
  699. status = ql_read_flash_word(qdev, i+offset, p);
  700. if (status) {
  701. netif_err(qdev, ifup, qdev->ndev,
  702. "Error reading flash.\n");
  703. goto exit;
  704. }
  705. }
  706. status = ql_validate_flash(qdev,
  707. sizeof(struct flash_params_8000) / sizeof(u16),
  708. "8000");
  709. if (status) {
  710. netif_err(qdev, ifup, qdev->ndev, "Invalid flash.\n");
  711. status = -EINVAL;
  712. goto exit;
  713. }
  714. /* Extract either manufacturer or BOFM modified
  715. * MAC address.
  716. */
  717. if (qdev->flash.flash_params_8000.data_type1 == 2)
  718. memcpy(mac_addr,
  719. qdev->flash.flash_params_8000.mac_addr1,
  720. qdev->ndev->addr_len);
  721. else
  722. memcpy(mac_addr,
  723. qdev->flash.flash_params_8000.mac_addr,
  724. qdev->ndev->addr_len);
  725. if (!is_valid_ether_addr(mac_addr)) {
  726. netif_err(qdev, ifup, qdev->ndev, "Invalid MAC address.\n");
  727. status = -EINVAL;
  728. goto exit;
  729. }
  730. memcpy(qdev->ndev->dev_addr,
  731. mac_addr,
  732. qdev->ndev->addr_len);
  733. exit:
  734. ql_sem_unlock(qdev, SEM_FLASH_MASK);
  735. return status;
  736. }
  737. static int ql_get_8012_flash_params(struct ql_adapter *qdev)
  738. {
  739. int i;
  740. int status;
  741. __le32 *p = (__le32 *)&qdev->flash;
  742. u32 offset = 0;
  743. u32 size = sizeof(struct flash_params_8012) / sizeof(u32);
  744. /* Second function's parameters follow the first
  745. * function's.
  746. */
  747. if (qdev->port)
  748. offset = size;
  749. if (ql_sem_spinlock(qdev, SEM_FLASH_MASK))
  750. return -ETIMEDOUT;
  751. for (i = 0; i < size; i++, p++) {
  752. status = ql_read_flash_word(qdev, i+offset, p);
  753. if (status) {
  754. netif_err(qdev, ifup, qdev->ndev,
  755. "Error reading flash.\n");
  756. goto exit;
  757. }
  758. }
  759. status = ql_validate_flash(qdev,
  760. sizeof(struct flash_params_8012) / sizeof(u16),
  761. "8012");
  762. if (status) {
  763. netif_err(qdev, ifup, qdev->ndev, "Invalid flash.\n");
  764. status = -EINVAL;
  765. goto exit;
  766. }
  767. if (!is_valid_ether_addr(qdev->flash.flash_params_8012.mac_addr)) {
  768. status = -EINVAL;
  769. goto exit;
  770. }
  771. memcpy(qdev->ndev->dev_addr,
  772. qdev->flash.flash_params_8012.mac_addr,
  773. qdev->ndev->addr_len);
  774. exit:
  775. ql_sem_unlock(qdev, SEM_FLASH_MASK);
  776. return status;
  777. }
  778. /* xgmac register are located behind the xgmac_addr and xgmac_data
  779. * register pair. Each read/write requires us to wait for the ready
  780. * bit before reading/writing the data.
  781. */
  782. static int ql_write_xgmac_reg(struct ql_adapter *qdev, u32 reg, u32 data)
  783. {
  784. int status;
  785. /* wait for reg to come ready */
  786. status = ql_wait_reg_rdy(qdev,
  787. XGMAC_ADDR, XGMAC_ADDR_RDY, XGMAC_ADDR_XME);
  788. if (status)
  789. return status;
  790. /* write the data to the data reg */
  791. ql_write32(qdev, XGMAC_DATA, data);
  792. /* trigger the write */
  793. ql_write32(qdev, XGMAC_ADDR, reg);
  794. return status;
  795. }
  796. /* xgmac register are located behind the xgmac_addr and xgmac_data
  797. * register pair. Each read/write requires us to wait for the ready
  798. * bit before reading/writing the data.
  799. */
  800. int ql_read_xgmac_reg(struct ql_adapter *qdev, u32 reg, u32 *data)
  801. {
  802. int status = 0;
  803. /* wait for reg to come ready */
  804. status = ql_wait_reg_rdy(qdev,
  805. XGMAC_ADDR, XGMAC_ADDR_RDY, XGMAC_ADDR_XME);
  806. if (status)
  807. goto exit;
  808. /* set up for reg read */
  809. ql_write32(qdev, XGMAC_ADDR, reg | XGMAC_ADDR_R);
  810. /* wait for reg to come ready */
  811. status = ql_wait_reg_rdy(qdev,
  812. XGMAC_ADDR, XGMAC_ADDR_RDY, XGMAC_ADDR_XME);
  813. if (status)
  814. goto exit;
  815. /* get the data */
  816. *data = ql_read32(qdev, XGMAC_DATA);
  817. exit:
  818. return status;
  819. }
  820. /* This is used for reading the 64-bit statistics regs. */
  821. int ql_read_xgmac_reg64(struct ql_adapter *qdev, u32 reg, u64 *data)
  822. {
  823. int status = 0;
  824. u32 hi = 0;
  825. u32 lo = 0;
  826. status = ql_read_xgmac_reg(qdev, reg, &lo);
  827. if (status)
  828. goto exit;
  829. status = ql_read_xgmac_reg(qdev, reg + 4, &hi);
  830. if (status)
  831. goto exit;
  832. *data = (u64) lo | ((u64) hi << 32);
  833. exit:
  834. return status;
  835. }
  836. static int ql_8000_port_initialize(struct ql_adapter *qdev)
  837. {
  838. int status;
  839. /*
  840. * Get MPI firmware version for driver banner
  841. * and ethool info.
  842. */
  843. status = ql_mb_about_fw(qdev);
  844. if (status)
  845. goto exit;
  846. status = ql_mb_get_fw_state(qdev);
  847. if (status)
  848. goto exit;
  849. /* Wake up a worker to get/set the TX/RX frame sizes. */
  850. queue_delayed_work(qdev->workqueue, &qdev->mpi_port_cfg_work, 0);
  851. exit:
  852. return status;
  853. }
  854. /* Take the MAC Core out of reset.
  855. * Enable statistics counting.
  856. * Take the transmitter/receiver out of reset.
  857. * This functionality may be done in the MPI firmware at a
  858. * later date.
  859. */
  860. static int ql_8012_port_initialize(struct ql_adapter *qdev)
  861. {
  862. int status = 0;
  863. u32 data;
  864. if (ql_sem_trylock(qdev, qdev->xg_sem_mask)) {
  865. /* Another function has the semaphore, so
  866. * wait for the port init bit to come ready.
  867. */
  868. netif_info(qdev, link, qdev->ndev,
  869. "Another function has the semaphore, so wait for the port init bit to come ready.\n");
  870. status = ql_wait_reg_rdy(qdev, STS, qdev->port_init, 0);
  871. if (status) {
  872. netif_crit(qdev, link, qdev->ndev,
  873. "Port initialize timed out.\n");
  874. }
  875. return status;
  876. }
  877. netif_info(qdev, link, qdev->ndev, "Got xgmac semaphore!.\n");
  878. /* Set the core reset. */
  879. status = ql_read_xgmac_reg(qdev, GLOBAL_CFG, &data);
  880. if (status)
  881. goto end;
  882. data |= GLOBAL_CFG_RESET;
  883. status = ql_write_xgmac_reg(qdev, GLOBAL_CFG, data);
  884. if (status)
  885. goto end;
  886. /* Clear the core reset and turn on jumbo for receiver. */
  887. data &= ~GLOBAL_CFG_RESET; /* Clear core reset. */
  888. data |= GLOBAL_CFG_JUMBO; /* Turn on jumbo. */
  889. data |= GLOBAL_CFG_TX_STAT_EN;
  890. data |= GLOBAL_CFG_RX_STAT_EN;
  891. status = ql_write_xgmac_reg(qdev, GLOBAL_CFG, data);
  892. if (status)
  893. goto end;
  894. /* Enable transmitter, and clear it's reset. */
  895. status = ql_read_xgmac_reg(qdev, TX_CFG, &data);
  896. if (status)
  897. goto end;
  898. data &= ~TX_CFG_RESET; /* Clear the TX MAC reset. */
  899. data |= TX_CFG_EN; /* Enable the transmitter. */
  900. status = ql_write_xgmac_reg(qdev, TX_CFG, data);
  901. if (status)
  902. goto end;
  903. /* Enable receiver and clear it's reset. */
  904. status = ql_read_xgmac_reg(qdev, RX_CFG, &data);
  905. if (status)
  906. goto end;
  907. data &= ~RX_CFG_RESET; /* Clear the RX MAC reset. */
  908. data |= RX_CFG_EN; /* Enable the receiver. */
  909. status = ql_write_xgmac_reg(qdev, RX_CFG, data);
  910. if (status)
  911. goto end;
  912. /* Turn on jumbo. */
  913. status =
  914. ql_write_xgmac_reg(qdev, MAC_TX_PARAMS, MAC_TX_PARAMS_JUMBO | (0x2580 << 16));
  915. if (status)
  916. goto end;
  917. status =
  918. ql_write_xgmac_reg(qdev, MAC_RX_PARAMS, 0x2580);
  919. if (status)
  920. goto end;
  921. /* Signal to the world that the port is enabled. */
  922. ql_write32(qdev, STS, ((qdev->port_init << 16) | qdev->port_init));
  923. end:
  924. ql_sem_unlock(qdev, qdev->xg_sem_mask);
  925. return status;
  926. }
  927. static inline unsigned int ql_lbq_block_size(struct ql_adapter *qdev)
  928. {
  929. return PAGE_SIZE << qdev->lbq_buf_order;
  930. }
  931. /* Get the next large buffer. */
  932. static struct bq_desc *ql_get_curr_lbuf(struct rx_ring *rx_ring)
  933. {
  934. struct bq_desc *lbq_desc = &rx_ring->lbq[rx_ring->lbq_curr_idx];
  935. rx_ring->lbq_curr_idx++;
  936. if (rx_ring->lbq_curr_idx == rx_ring->lbq_len)
  937. rx_ring->lbq_curr_idx = 0;
  938. rx_ring->lbq_free_cnt++;
  939. return lbq_desc;
  940. }
  941. static struct bq_desc *ql_get_curr_lchunk(struct ql_adapter *qdev,
  942. struct rx_ring *rx_ring)
  943. {
  944. struct bq_desc *lbq_desc = ql_get_curr_lbuf(rx_ring);
  945. pci_dma_sync_single_for_cpu(qdev->pdev,
  946. dma_unmap_addr(lbq_desc, mapaddr),
  947. rx_ring->lbq_buf_size,
  948. PCI_DMA_FROMDEVICE);
  949. /* If it's the last chunk of our master page then
  950. * we unmap it.
  951. */
  952. if ((lbq_desc->p.pg_chunk.offset + rx_ring->lbq_buf_size)
  953. == ql_lbq_block_size(qdev))
  954. pci_unmap_page(qdev->pdev,
  955. lbq_desc->p.pg_chunk.map,
  956. ql_lbq_block_size(qdev),
  957. PCI_DMA_FROMDEVICE);
  958. return lbq_desc;
  959. }
  960. /* Get the next small buffer. */
  961. static struct bq_desc *ql_get_curr_sbuf(struct rx_ring *rx_ring)
  962. {
  963. struct bq_desc *sbq_desc = &rx_ring->sbq[rx_ring->sbq_curr_idx];
  964. rx_ring->sbq_curr_idx++;
  965. if (rx_ring->sbq_curr_idx == rx_ring->sbq_len)
  966. rx_ring->sbq_curr_idx = 0;
  967. rx_ring->sbq_free_cnt++;
  968. return sbq_desc;
  969. }
  970. /* Update an rx ring index. */
  971. static void ql_update_cq(struct rx_ring *rx_ring)
  972. {
  973. rx_ring->cnsmr_idx++;
  974. rx_ring->curr_entry++;
  975. if (unlikely(rx_ring->cnsmr_idx == rx_ring->cq_len)) {
  976. rx_ring->cnsmr_idx = 0;
  977. rx_ring->curr_entry = rx_ring->cq_base;
  978. }
  979. }
  980. static void ql_write_cq_idx(struct rx_ring *rx_ring)
  981. {
  982. ql_write_db_reg(rx_ring->cnsmr_idx, rx_ring->cnsmr_idx_db_reg);
  983. }
  984. static int ql_get_next_chunk(struct ql_adapter *qdev, struct rx_ring *rx_ring,
  985. struct bq_desc *lbq_desc)
  986. {
  987. if (!rx_ring->pg_chunk.page) {
  988. u64 map;
  989. rx_ring->pg_chunk.page = alloc_pages(__GFP_COLD | __GFP_COMP |
  990. GFP_ATOMIC,
  991. qdev->lbq_buf_order);
  992. if (unlikely(!rx_ring->pg_chunk.page)) {
  993. netif_err(qdev, drv, qdev->ndev,
  994. "page allocation failed.\n");
  995. return -ENOMEM;
  996. }
  997. rx_ring->pg_chunk.offset = 0;
  998. map = pci_map_page(qdev->pdev, rx_ring->pg_chunk.page,
  999. 0, ql_lbq_block_size(qdev),
  1000. PCI_DMA_FROMDEVICE);
  1001. if (pci_dma_mapping_error(qdev->pdev, map)) {
  1002. __free_pages(rx_ring->pg_chunk.page,
  1003. qdev->lbq_buf_order);
  1004. netif_err(qdev, drv, qdev->ndev,
  1005. "PCI mapping failed.\n");
  1006. return -ENOMEM;
  1007. }
  1008. rx_ring->pg_chunk.map = map;
  1009. rx_ring->pg_chunk.va = page_address(rx_ring->pg_chunk.page);
  1010. }
  1011. /* Copy the current master pg_chunk info
  1012. * to the current descriptor.
  1013. */
  1014. lbq_desc->p.pg_chunk = rx_ring->pg_chunk;
  1015. /* Adjust the master page chunk for next
  1016. * buffer get.
  1017. */
  1018. rx_ring->pg_chunk.offset += rx_ring->lbq_buf_size;
  1019. if (rx_ring->pg_chunk.offset == ql_lbq_block_size(qdev)) {
  1020. rx_ring->pg_chunk.page = NULL;
  1021. lbq_desc->p.pg_chunk.last_flag = 1;
  1022. } else {
  1023. rx_ring->pg_chunk.va += rx_ring->lbq_buf_size;
  1024. get_page(rx_ring->pg_chunk.page);
  1025. lbq_desc->p.pg_chunk.last_flag = 0;
  1026. }
  1027. return 0;
  1028. }
  1029. /* Process (refill) a large buffer queue. */
  1030. static void ql_update_lbq(struct ql_adapter *qdev, struct rx_ring *rx_ring)
  1031. {
  1032. u32 clean_idx = rx_ring->lbq_clean_idx;
  1033. u32 start_idx = clean_idx;
  1034. struct bq_desc *lbq_desc;
  1035. u64 map;
  1036. int i;
  1037. while (rx_ring->lbq_free_cnt > 32) {
  1038. for (i = (rx_ring->lbq_clean_idx % 16); i < 16; i++) {
  1039. netif_printk(qdev, rx_status, KERN_DEBUG, qdev->ndev,
  1040. "lbq: try cleaning clean_idx = %d.\n",
  1041. clean_idx);
  1042. lbq_desc = &rx_ring->lbq[clean_idx];
  1043. if (ql_get_next_chunk(qdev, rx_ring, lbq_desc)) {
  1044. rx_ring->lbq_clean_idx = clean_idx;
  1045. netif_err(qdev, ifup, qdev->ndev,
  1046. "Could not get a page chunk, i=%d, clean_idx =%d .\n",
  1047. i, clean_idx);
  1048. return;
  1049. }
  1050. map = lbq_desc->p.pg_chunk.map +
  1051. lbq_desc->p.pg_chunk.offset;
  1052. dma_unmap_addr_set(lbq_desc, mapaddr, map);
  1053. dma_unmap_len_set(lbq_desc, maplen,
  1054. rx_ring->lbq_buf_size);
  1055. *lbq_desc->addr = cpu_to_le64(map);
  1056. pci_dma_sync_single_for_device(qdev->pdev, map,
  1057. rx_ring->lbq_buf_size,
  1058. PCI_DMA_FROMDEVICE);
  1059. clean_idx++;
  1060. if (clean_idx == rx_ring->lbq_len)
  1061. clean_idx = 0;
  1062. }
  1063. rx_ring->lbq_clean_idx = clean_idx;
  1064. rx_ring->lbq_prod_idx += 16;
  1065. if (rx_ring->lbq_prod_idx == rx_ring->lbq_len)
  1066. rx_ring->lbq_prod_idx = 0;
  1067. rx_ring->lbq_free_cnt -= 16;
  1068. }
  1069. if (start_idx != clean_idx) {
  1070. netif_printk(qdev, rx_status, KERN_DEBUG, qdev->ndev,
  1071. "lbq: updating prod idx = %d.\n",
  1072. rx_ring->lbq_prod_idx);
  1073. ql_write_db_reg(rx_ring->lbq_prod_idx,
  1074. rx_ring->lbq_prod_idx_db_reg);
  1075. }
  1076. }
  1077. /* Process (refill) a small buffer queue. */
  1078. static void ql_update_sbq(struct ql_adapter *qdev, struct rx_ring *rx_ring)
  1079. {
  1080. u32 clean_idx = rx_ring->sbq_clean_idx;
  1081. u32 start_idx = clean_idx;
  1082. struct bq_desc *sbq_desc;
  1083. u64 map;
  1084. int i;
  1085. while (rx_ring->sbq_free_cnt > 16) {
  1086. for (i = (rx_ring->sbq_clean_idx % 16); i < 16; i++) {
  1087. sbq_desc = &rx_ring->sbq[clean_idx];
  1088. netif_printk(qdev, rx_status, KERN_DEBUG, qdev->ndev,
  1089. "sbq: try cleaning clean_idx = %d.\n",
  1090. clean_idx);
  1091. if (sbq_desc->p.skb == NULL) {
  1092. netif_printk(qdev, rx_status, KERN_DEBUG,
  1093. qdev->ndev,
  1094. "sbq: getting new skb for index %d.\n",
  1095. sbq_desc->index);
  1096. sbq_desc->p.skb =
  1097. netdev_alloc_skb(qdev->ndev,
  1098. SMALL_BUFFER_SIZE);
  1099. if (sbq_desc->p.skb == NULL) {
  1100. netif_err(qdev, probe, qdev->ndev,
  1101. "Couldn't get an skb.\n");
  1102. rx_ring->sbq_clean_idx = clean_idx;
  1103. return;
  1104. }
  1105. skb_reserve(sbq_desc->p.skb, QLGE_SB_PAD);
  1106. map = pci_map_single(qdev->pdev,
  1107. sbq_desc->p.skb->data,
  1108. rx_ring->sbq_buf_size,
  1109. PCI_DMA_FROMDEVICE);
  1110. if (pci_dma_mapping_error(qdev->pdev, map)) {
  1111. netif_err(qdev, ifup, qdev->ndev,
  1112. "PCI mapping failed.\n");
  1113. rx_ring->sbq_clean_idx = clean_idx;
  1114. dev_kfree_skb_any(sbq_desc->p.skb);
  1115. sbq_desc->p.skb = NULL;
  1116. return;
  1117. }
  1118. dma_unmap_addr_set(sbq_desc, mapaddr, map);
  1119. dma_unmap_len_set(sbq_desc, maplen,
  1120. rx_ring->sbq_buf_size);
  1121. *sbq_desc->addr = cpu_to_le64(map);
  1122. }
  1123. clean_idx++;
  1124. if (clean_idx == rx_ring->sbq_len)
  1125. clean_idx = 0;
  1126. }
  1127. rx_ring->sbq_clean_idx = clean_idx;
  1128. rx_ring->sbq_prod_idx += 16;
  1129. if (rx_ring->sbq_prod_idx == rx_ring->sbq_len)
  1130. rx_ring->sbq_prod_idx = 0;
  1131. rx_ring->sbq_free_cnt -= 16;
  1132. }
  1133. if (start_idx != clean_idx) {
  1134. netif_printk(qdev, rx_status, KERN_DEBUG, qdev->ndev,
  1135. "sbq: updating prod idx = %d.\n",
  1136. rx_ring->sbq_prod_idx);
  1137. ql_write_db_reg(rx_ring->sbq_prod_idx,
  1138. rx_ring->sbq_prod_idx_db_reg);
  1139. }
  1140. }
  1141. static void ql_update_buffer_queues(struct ql_adapter *qdev,
  1142. struct rx_ring *rx_ring)
  1143. {
  1144. ql_update_sbq(qdev, rx_ring);
  1145. ql_update_lbq(qdev, rx_ring);
  1146. }
  1147. /* Unmaps tx buffers. Can be called from send() if a pci mapping
  1148. * fails at some stage, or from the interrupt when a tx completes.
  1149. */
  1150. static void ql_unmap_send(struct ql_adapter *qdev,
  1151. struct tx_ring_desc *tx_ring_desc, int mapped)
  1152. {
  1153. int i;
  1154. for (i = 0; i < mapped; i++) {
  1155. if (i == 0 || (i == 7 && mapped > 7)) {
  1156. /*
  1157. * Unmap the skb->data area, or the
  1158. * external sglist (AKA the Outbound
  1159. * Address List (OAL)).
  1160. * If its the zeroeth element, then it's
  1161. * the skb->data area. If it's the 7th
  1162. * element and there is more than 6 frags,
  1163. * then its an OAL.
  1164. */
  1165. if (i == 7) {
  1166. netif_printk(qdev, tx_done, KERN_DEBUG,
  1167. qdev->ndev,
  1168. "unmapping OAL area.\n");
  1169. }
  1170. pci_unmap_single(qdev->pdev,
  1171. dma_unmap_addr(&tx_ring_desc->map[i],
  1172. mapaddr),
  1173. dma_unmap_len(&tx_ring_desc->map[i],
  1174. maplen),
  1175. PCI_DMA_TODEVICE);
  1176. } else {
  1177. netif_printk(qdev, tx_done, KERN_DEBUG, qdev->ndev,
  1178. "unmapping frag %d.\n", i);
  1179. pci_unmap_page(qdev->pdev,
  1180. dma_unmap_addr(&tx_ring_desc->map[i],
  1181. mapaddr),
  1182. dma_unmap_len(&tx_ring_desc->map[i],
  1183. maplen), PCI_DMA_TODEVICE);
  1184. }
  1185. }
  1186. }
  1187. /* Map the buffers for this transmit. This will return
  1188. * NETDEV_TX_BUSY or NETDEV_TX_OK based on success.
  1189. */
  1190. static int ql_map_send(struct ql_adapter *qdev,
  1191. struct ob_mac_iocb_req *mac_iocb_ptr,
  1192. struct sk_buff *skb, struct tx_ring_desc *tx_ring_desc)
  1193. {
  1194. int len = skb_headlen(skb);
  1195. dma_addr_t map;
  1196. int frag_idx, err, map_idx = 0;
  1197. struct tx_buf_desc *tbd = mac_iocb_ptr->tbd;
  1198. int frag_cnt = skb_shinfo(skb)->nr_frags;
  1199. if (frag_cnt) {
  1200. netif_printk(qdev, tx_queued, KERN_DEBUG, qdev->ndev,
  1201. "frag_cnt = %d.\n", frag_cnt);
  1202. }
  1203. /*
  1204. * Map the skb buffer first.
  1205. */
  1206. map = pci_map_single(qdev->pdev, skb->data, len, PCI_DMA_TODEVICE);
  1207. err = pci_dma_mapping_error(qdev->pdev, map);
  1208. if (err) {
  1209. netif_err(qdev, tx_queued, qdev->ndev,
  1210. "PCI mapping failed with error: %d\n", err);
  1211. return NETDEV_TX_BUSY;
  1212. }
  1213. tbd->len = cpu_to_le32(len);
  1214. tbd->addr = cpu_to_le64(map);
  1215. dma_unmap_addr_set(&tx_ring_desc->map[map_idx], mapaddr, map);
  1216. dma_unmap_len_set(&tx_ring_desc->map[map_idx], maplen, len);
  1217. map_idx++;
  1218. /*
  1219. * This loop fills the remainder of the 8 address descriptors
  1220. * in the IOCB. If there are more than 7 fragments, then the
  1221. * eighth address desc will point to an external list (OAL).
  1222. * When this happens, the remainder of the frags will be stored
  1223. * in this list.
  1224. */
  1225. for (frag_idx = 0; frag_idx < frag_cnt; frag_idx++, map_idx++) {
  1226. skb_frag_t *frag = &skb_shinfo(skb)->frags[frag_idx];
  1227. tbd++;
  1228. if (frag_idx == 6 && frag_cnt > 7) {
  1229. /* Let's tack on an sglist.
  1230. * Our control block will now
  1231. * look like this:
  1232. * iocb->seg[0] = skb->data
  1233. * iocb->seg[1] = frag[0]
  1234. * iocb->seg[2] = frag[1]
  1235. * iocb->seg[3] = frag[2]
  1236. * iocb->seg[4] = frag[3]
  1237. * iocb->seg[5] = frag[4]
  1238. * iocb->seg[6] = frag[5]
  1239. * iocb->seg[7] = ptr to OAL (external sglist)
  1240. * oal->seg[0] = frag[6]
  1241. * oal->seg[1] = frag[7]
  1242. * oal->seg[2] = frag[8]
  1243. * oal->seg[3] = frag[9]
  1244. * oal->seg[4] = frag[10]
  1245. * etc...
  1246. */
  1247. /* Tack on the OAL in the eighth segment of IOCB. */
  1248. map = pci_map_single(qdev->pdev, &tx_ring_desc->oal,
  1249. sizeof(struct oal),
  1250. PCI_DMA_TODEVICE);
  1251. err = pci_dma_mapping_error(qdev->pdev, map);
  1252. if (err) {
  1253. netif_err(qdev, tx_queued, qdev->ndev,
  1254. "PCI mapping outbound address list with error: %d\n",
  1255. err);
  1256. goto map_error;
  1257. }
  1258. tbd->addr = cpu_to_le64(map);
  1259. /*
  1260. * The length is the number of fragments
  1261. * that remain to be mapped times the length
  1262. * of our sglist (OAL).
  1263. */
  1264. tbd->len =
  1265. cpu_to_le32((sizeof(struct tx_buf_desc) *
  1266. (frag_cnt - frag_idx)) | TX_DESC_C);
  1267. dma_unmap_addr_set(&tx_ring_desc->map[map_idx], mapaddr,
  1268. map);
  1269. dma_unmap_len_set(&tx_ring_desc->map[map_idx], maplen,
  1270. sizeof(struct oal));
  1271. tbd = (struct tx_buf_desc *)&tx_ring_desc->oal;
  1272. map_idx++;
  1273. }
  1274. map = skb_frag_dma_map(&qdev->pdev->dev, frag, 0, skb_frag_size(frag),
  1275. DMA_TO_DEVICE);
  1276. err = dma_mapping_error(&qdev->pdev->dev, map);
  1277. if (err) {
  1278. netif_err(qdev, tx_queued, qdev->ndev,
  1279. "PCI mapping frags failed with error: %d.\n",
  1280. err);
  1281. goto map_error;
  1282. }
  1283. tbd->addr = cpu_to_le64(map);
  1284. tbd->len = cpu_to_le32(skb_frag_size(frag));
  1285. dma_unmap_addr_set(&tx_ring_desc->map[map_idx], mapaddr, map);
  1286. dma_unmap_len_set(&tx_ring_desc->map[map_idx], maplen,
  1287. skb_frag_size(frag));
  1288. }
  1289. /* Save the number of segments we've mapped. */
  1290. tx_ring_desc->map_cnt = map_idx;
  1291. /* Terminate the last segment. */
  1292. tbd->len = cpu_to_le32(le32_to_cpu(tbd->len) | TX_DESC_E);
  1293. return NETDEV_TX_OK;
  1294. map_error:
  1295. /*
  1296. * If the first frag mapping failed, then i will be zero.
  1297. * This causes the unmap of the skb->data area. Otherwise
  1298. * we pass in the number of frags that mapped successfully
  1299. * so they can be umapped.
  1300. */
  1301. ql_unmap_send(qdev, tx_ring_desc, map_idx);
  1302. return NETDEV_TX_BUSY;
  1303. }
  1304. /* Process an inbound completion from an rx ring. */
  1305. static void ql_process_mac_rx_gro_page(struct ql_adapter *qdev,
  1306. struct rx_ring *rx_ring,
  1307. struct ib_mac_iocb_rsp *ib_mac_rsp,
  1308. u32 length,
  1309. u16 vlan_id)
  1310. {
  1311. struct sk_buff *skb;
  1312. struct bq_desc *lbq_desc = ql_get_curr_lchunk(qdev, rx_ring);
  1313. struct napi_struct *napi = &rx_ring->napi;
  1314. napi->dev = qdev->ndev;
  1315. skb = napi_get_frags(napi);
  1316. if (!skb) {
  1317. netif_err(qdev, drv, qdev->ndev,
  1318. "Couldn't get an skb, exiting.\n");
  1319. rx_ring->rx_dropped++;
  1320. put_page(lbq_desc->p.pg_chunk.page);
  1321. return;
  1322. }
  1323. prefetch(lbq_desc->p.pg_chunk.va);
  1324. __skb_fill_page_desc(skb, skb_shinfo(skb)->nr_frags,
  1325. lbq_desc->p.pg_chunk.page,
  1326. lbq_desc->p.pg_chunk.offset,
  1327. length);
  1328. skb->len += length;
  1329. skb->data_len += length;
  1330. skb->truesize += length;
  1331. skb_shinfo(skb)->nr_frags++;
  1332. rx_ring->rx_packets++;
  1333. rx_ring->rx_bytes += length;
  1334. skb->ip_summed = CHECKSUM_UNNECESSARY;
  1335. skb_record_rx_queue(skb, rx_ring->cq_id);
  1336. if (vlan_id != 0xffff)
  1337. __vlan_hwaccel_put_tag(skb, vlan_id);
  1338. napi_gro_frags(napi);
  1339. }
  1340. /* Process an inbound completion from an rx ring. */
  1341. static void ql_process_mac_rx_page(struct ql_adapter *qdev,
  1342. struct rx_ring *rx_ring,
  1343. struct ib_mac_iocb_rsp *ib_mac_rsp,
  1344. u32 length,
  1345. u16 vlan_id)
  1346. {
  1347. struct net_device *ndev = qdev->ndev;
  1348. struct sk_buff *skb = NULL;
  1349. void *addr;
  1350. struct bq_desc *lbq_desc = ql_get_curr_lchunk(qdev, rx_ring);
  1351. struct napi_struct *napi = &rx_ring->napi;
  1352. skb = netdev_alloc_skb(ndev, length);
  1353. if (!skb) {
  1354. netif_err(qdev, drv, qdev->ndev,
  1355. "Couldn't get an skb, need to unwind!.\n");
  1356. rx_ring->rx_dropped++;
  1357. put_page(lbq_desc->p.pg_chunk.page);
  1358. return;
  1359. }
  1360. addr = lbq_desc->p.pg_chunk.va;
  1361. prefetch(addr);
  1362. /* Frame error, so drop the packet. */
  1363. if (ib_mac_rsp->flags2 & IB_MAC_IOCB_RSP_ERR_MASK) {
  1364. netif_info(qdev, drv, qdev->ndev,
  1365. "Receive error, flags2 = 0x%x\n", ib_mac_rsp->flags2);
  1366. rx_ring->rx_errors++;
  1367. goto err_out;
  1368. }
  1369. /* The max framesize filter on this chip is set higher than
  1370. * MTU since FCoE uses 2k frames.
  1371. */
  1372. if (skb->len > ndev->mtu + ETH_HLEN) {
  1373. netif_err(qdev, drv, qdev->ndev,
  1374. "Segment too small, dropping.\n");
  1375. rx_ring->rx_dropped++;
  1376. goto err_out;
  1377. }
  1378. memcpy(skb_put(skb, ETH_HLEN), addr, ETH_HLEN);
  1379. netif_printk(qdev, rx_status, KERN_DEBUG, qdev->ndev,
  1380. "%d bytes of headers and data in large. Chain page to new skb and pull tail.\n",
  1381. length);
  1382. skb_fill_page_desc(skb, 0, lbq_desc->p.pg_chunk.page,
  1383. lbq_desc->p.pg_chunk.offset+ETH_HLEN,
  1384. length-ETH_HLEN);
  1385. skb->len += length-ETH_HLEN;
  1386. skb->data_len += length-ETH_HLEN;
  1387. skb->truesize += length-ETH_HLEN;
  1388. rx_ring->rx_packets++;
  1389. rx_ring->rx_bytes += skb->len;
  1390. skb->protocol = eth_type_trans(skb, ndev);
  1391. skb_checksum_none_assert(skb);
  1392. if ((ndev->features & NETIF_F_RXCSUM) &&
  1393. !(ib_mac_rsp->flags1 & IB_MAC_CSUM_ERR_MASK)) {
  1394. /* TCP frame. */
  1395. if (ib_mac_rsp->flags2 & IB_MAC_IOCB_RSP_T) {
  1396. netif_printk(qdev, rx_status, KERN_DEBUG, qdev->ndev,
  1397. "TCP checksum done!\n");
  1398. skb->ip_summed = CHECKSUM_UNNECESSARY;
  1399. } else if ((ib_mac_rsp->flags2 & IB_MAC_IOCB_RSP_U) &&
  1400. (ib_mac_rsp->flags3 & IB_MAC_IOCB_RSP_V4)) {
  1401. /* Unfragmented ipv4 UDP frame. */
  1402. struct iphdr *iph =
  1403. (struct iphdr *) ((u8 *)addr + ETH_HLEN);
  1404. if (!(iph->frag_off &
  1405. cpu_to_be16(IP_MF|IP_OFFSET))) {
  1406. skb->ip_summed = CHECKSUM_UNNECESSARY;
  1407. netif_printk(qdev, rx_status, KERN_DEBUG,
  1408. qdev->ndev,
  1409. "UDP checksum done!\n");
  1410. }
  1411. }
  1412. }
  1413. skb_record_rx_queue(skb, rx_ring->cq_id);
  1414. if (vlan_id != 0xffff)
  1415. __vlan_hwaccel_put_tag(skb, vlan_id);
  1416. if (skb->ip_summed == CHECKSUM_UNNECESSARY)
  1417. napi_gro_receive(napi, skb);
  1418. else
  1419. netif_receive_skb(skb);
  1420. return;
  1421. err_out:
  1422. dev_kfree_skb_any(skb);
  1423. put_page(lbq_desc->p.pg_chunk.page);
  1424. }
  1425. /* Process an inbound completion from an rx ring. */
  1426. static void ql_process_mac_rx_skb(struct ql_adapter *qdev,
  1427. struct rx_ring *rx_ring,
  1428. struct ib_mac_iocb_rsp *ib_mac_rsp,
  1429. u32 length,
  1430. u16 vlan_id)
  1431. {
  1432. struct net_device *ndev = qdev->ndev;
  1433. struct sk_buff *skb = NULL;
  1434. struct sk_buff *new_skb = NULL;
  1435. struct bq_desc *sbq_desc = ql_get_curr_sbuf(rx_ring);
  1436. skb = sbq_desc->p.skb;
  1437. /* Allocate new_skb and copy */
  1438. new_skb = netdev_alloc_skb(qdev->ndev, length + NET_IP_ALIGN);
  1439. if (new_skb == NULL) {
  1440. netif_err(qdev, probe, qdev->ndev,
  1441. "No skb available, drop the packet.\n");
  1442. rx_ring->rx_dropped++;
  1443. return;
  1444. }
  1445. skb_reserve(new_skb, NET_IP_ALIGN);
  1446. memcpy(skb_put(new_skb, length), skb->data, length);
  1447. skb = new_skb;
  1448. /* Frame error, so drop the packet. */
  1449. if (ib_mac_rsp->flags2 & IB_MAC_IOCB_RSP_ERR_MASK) {
  1450. netif_info(qdev, drv, qdev->ndev,
  1451. "Receive error, flags2 = 0x%x\n", ib_mac_rsp->flags2);
  1452. dev_kfree_skb_any(skb);
  1453. rx_ring->rx_errors++;
  1454. return;
  1455. }
  1456. /* loopback self test for ethtool */
  1457. if (test_bit(QL_SELFTEST, &qdev->flags)) {
  1458. ql_check_lb_frame(qdev, skb);
  1459. dev_kfree_skb_any(skb);
  1460. return;
  1461. }
  1462. /* The max framesize filter on this chip is set higher than
  1463. * MTU since FCoE uses 2k frames.
  1464. */
  1465. if (skb->len > ndev->mtu + ETH_HLEN) {
  1466. dev_kfree_skb_any(skb);
  1467. rx_ring->rx_dropped++;
  1468. return;
  1469. }
  1470. prefetch(skb->data);
  1471. if (ib_mac_rsp->flags1 & IB_MAC_IOCB_RSP_M_MASK) {
  1472. netif_printk(qdev, rx_status, KERN_DEBUG, qdev->ndev,
  1473. "%s Multicast.\n",
  1474. (ib_mac_rsp->flags1 & IB_MAC_IOCB_RSP_M_MASK) ==
  1475. IB_MAC_IOCB_RSP_M_HASH ? "Hash" :
  1476. (ib_mac_rsp->flags1 & IB_MAC_IOCB_RSP_M_MASK) ==
  1477. IB_MAC_IOCB_RSP_M_REG ? "Registered" :
  1478. (ib_mac_rsp->flags1 & IB_MAC_IOCB_RSP_M_MASK) ==
  1479. IB_MAC_IOCB_RSP_M_PROM ? "Promiscuous" : "");
  1480. }
  1481. if (ib_mac_rsp->flags2 & IB_MAC_IOCB_RSP_P)
  1482. netif_printk(qdev, rx_status, KERN_DEBUG, qdev->ndev,
  1483. "Promiscuous Packet.\n");
  1484. rx_ring->rx_packets++;
  1485. rx_ring->rx_bytes += skb->len;
  1486. skb->protocol = eth_type_trans(skb, ndev);
  1487. skb_checksum_none_assert(skb);
  1488. /* If rx checksum is on, and there are no
  1489. * csum or frame errors.
  1490. */
  1491. if ((ndev->features & NETIF_F_RXCSUM) &&
  1492. !(ib_mac_rsp->flags1 & IB_MAC_CSUM_ERR_MASK)) {
  1493. /* TCP frame. */
  1494. if (ib_mac_rsp->flags2 & IB_MAC_IOCB_RSP_T) {
  1495. netif_printk(qdev, rx_status, KERN_DEBUG, qdev->ndev,
  1496. "TCP checksum done!\n");
  1497. skb->ip_summed = CHECKSUM_UNNECESSARY;
  1498. } else if ((ib_mac_rsp->flags2 & IB_MAC_IOCB_RSP_U) &&
  1499. (ib_mac_rsp->flags3 & IB_MAC_IOCB_RSP_V4)) {
  1500. /* Unfragmented ipv4 UDP frame. */
  1501. struct iphdr *iph = (struct iphdr *) skb->data;
  1502. if (!(iph->frag_off &
  1503. ntohs(IP_MF|IP_OFFSET))) {
  1504. skb->ip_summed = CHECKSUM_UNNECESSARY;
  1505. netif_printk(qdev, rx_status, KERN_DEBUG,
  1506. qdev->ndev,
  1507. "UDP checksum done!\n");
  1508. }
  1509. }
  1510. }
  1511. skb_record_rx_queue(skb, rx_ring->cq_id);
  1512. if (vlan_id != 0xffff)
  1513. __vlan_hwaccel_put_tag(skb, vlan_id);
  1514. if (skb->ip_summed == CHECKSUM_UNNECESSARY)
  1515. napi_gro_receive(&rx_ring->napi, skb);
  1516. else
  1517. netif_receive_skb(skb);
  1518. }
  1519. static void ql_realign_skb(struct sk_buff *skb, int len)
  1520. {
  1521. void *temp_addr = skb->data;
  1522. /* Undo the skb_reserve(skb,32) we did before
  1523. * giving to hardware, and realign data on
  1524. * a 2-byte boundary.
  1525. */
  1526. skb->data -= QLGE_SB_PAD - NET_IP_ALIGN;
  1527. skb->tail -= QLGE_SB_PAD - NET_IP_ALIGN;
  1528. skb_copy_to_linear_data(skb, temp_addr,
  1529. (unsigned int)len);
  1530. }
  1531. /*
  1532. * This function builds an skb for the given inbound
  1533. * completion. It will be rewritten for readability in the near
  1534. * future, but for not it works well.
  1535. */
  1536. static struct sk_buff *ql_build_rx_skb(struct ql_adapter *qdev,
  1537. struct rx_ring *rx_ring,
  1538. struct ib_mac_iocb_rsp *ib_mac_rsp)
  1539. {
  1540. struct bq_desc *lbq_desc;
  1541. struct bq_desc *sbq_desc;
  1542. struct sk_buff *skb = NULL;
  1543. u32 length = le32_to_cpu(ib_mac_rsp->data_len);
  1544. u32 hdr_len = le32_to_cpu(ib_mac_rsp->hdr_len);
  1545. /*
  1546. * Handle the header buffer if present.
  1547. */
  1548. if (ib_mac_rsp->flags4 & IB_MAC_IOCB_RSP_HV &&
  1549. ib_mac_rsp->flags4 & IB_MAC_IOCB_RSP_HS) {
  1550. netif_printk(qdev, rx_status, KERN_DEBUG, qdev->ndev,
  1551. "Header of %d bytes in small buffer.\n", hdr_len);
  1552. /*
  1553. * Headers fit nicely into a small buffer.
  1554. */
  1555. sbq_desc = ql_get_curr_sbuf(rx_ring);
  1556. pci_unmap_single(qdev->pdev,
  1557. dma_unmap_addr(sbq_desc, mapaddr),
  1558. dma_unmap_len(sbq_desc, maplen),
  1559. PCI_DMA_FROMDEVICE);
  1560. skb = sbq_desc->p.skb;
  1561. ql_realign_skb(skb, hdr_len);
  1562. skb_put(skb, hdr_len);
  1563. sbq_desc->p.skb = NULL;
  1564. }
  1565. /*
  1566. * Handle the data buffer(s).
  1567. */
  1568. if (unlikely(!length)) { /* Is there data too? */
  1569. netif_printk(qdev, rx_status, KERN_DEBUG, qdev->ndev,
  1570. "No Data buffer in this packet.\n");
  1571. return skb;
  1572. }
  1573. if (ib_mac_rsp->flags3 & IB_MAC_IOCB_RSP_DS) {
  1574. if (ib_mac_rsp->flags4 & IB_MAC_IOCB_RSP_HS) {
  1575. netif_printk(qdev, rx_status, KERN_DEBUG, qdev->ndev,
  1576. "Headers in small, data of %d bytes in small, combine them.\n",
  1577. length);
  1578. /*
  1579. * Data is less than small buffer size so it's
  1580. * stuffed in a small buffer.
  1581. * For this case we append the data
  1582. * from the "data" small buffer to the "header" small
  1583. * buffer.
  1584. */
  1585. sbq_desc = ql_get_curr_sbuf(rx_ring);
  1586. pci_dma_sync_single_for_cpu(qdev->pdev,
  1587. dma_unmap_addr
  1588. (sbq_desc, mapaddr),
  1589. dma_unmap_len
  1590. (sbq_desc, maplen),
  1591. PCI_DMA_FROMDEVICE);
  1592. memcpy(skb_put(skb, length),
  1593. sbq_desc->p.skb->data, length);
  1594. pci_dma_sync_single_for_device(qdev->pdev,
  1595. dma_unmap_addr
  1596. (sbq_desc,
  1597. mapaddr),
  1598. dma_unmap_len
  1599. (sbq_desc,
  1600. maplen),
  1601. PCI_DMA_FROMDEVICE);
  1602. } else {
  1603. netif_printk(qdev, rx_status, KERN_DEBUG, qdev->ndev,
  1604. "%d bytes in a single small buffer.\n",
  1605. length);
  1606. sbq_desc = ql_get_curr_sbuf(rx_ring);
  1607. skb = sbq_desc->p.skb;
  1608. ql_realign_skb(skb, length);
  1609. skb_put(skb, length);
  1610. pci_unmap_single(qdev->pdev,
  1611. dma_unmap_addr(sbq_desc,
  1612. mapaddr),
  1613. dma_unmap_len(sbq_desc,
  1614. maplen),
  1615. PCI_DMA_FROMDEVICE);
  1616. sbq_desc->p.skb = NULL;
  1617. }
  1618. } else if (ib_mac_rsp->flags3 & IB_MAC_IOCB_RSP_DL) {
  1619. if (ib_mac_rsp->flags4 & IB_MAC_IOCB_RSP_HS) {
  1620. netif_printk(qdev, rx_status, KERN_DEBUG, qdev->ndev,
  1621. "Header in small, %d bytes in large. Chain large to small!\n",
  1622. length);
  1623. /*
  1624. * The data is in a single large buffer. We
  1625. * chain it to the header buffer's skb and let
  1626. * it rip.
  1627. */
  1628. lbq_desc = ql_get_curr_lchunk(qdev, rx_ring);
  1629. netif_printk(qdev, rx_status, KERN_DEBUG, qdev->ndev,
  1630. "Chaining page at offset = %d, for %d bytes to skb.\n",
  1631. lbq_desc->p.pg_chunk.offset, length);
  1632. skb_fill_page_desc(skb, 0, lbq_desc->p.pg_chunk.page,
  1633. lbq_desc->p.pg_chunk.offset,
  1634. length);
  1635. skb->len += length;
  1636. skb->data_len += length;
  1637. skb->truesize += length;
  1638. } else {
  1639. /*
  1640. * The headers and data are in a single large buffer. We
  1641. * copy it to a new skb and let it go. This can happen with
  1642. * jumbo mtu on a non-TCP/UDP frame.
  1643. */
  1644. lbq_desc = ql_get_curr_lchunk(qdev, rx_ring);
  1645. skb = netdev_alloc_skb(qdev->ndev, length);
  1646. if (skb == NULL) {
  1647. netif_printk(qdev, probe, KERN_DEBUG, qdev->ndev,
  1648. "No skb available, drop the packet.\n");
  1649. return NULL;
  1650. }
  1651. pci_unmap_page(qdev->pdev,
  1652. dma_unmap_addr(lbq_desc,
  1653. mapaddr),
  1654. dma_unmap_len(lbq_desc, maplen),
  1655. PCI_DMA_FROMDEVICE);
  1656. skb_reserve(skb, NET_IP_ALIGN);
  1657. netif_printk(qdev, rx_status, KERN_DEBUG, qdev->ndev,
  1658. "%d bytes of headers and data in large. Chain page to new skb and pull tail.\n",
  1659. length);
  1660. skb_fill_page_desc(skb, 0,
  1661. lbq_desc->p.pg_chunk.page,
  1662. lbq_desc->p.pg_chunk.offset,
  1663. length);
  1664. skb->len += length;
  1665. skb->data_len += length;
  1666. skb->truesize += length;
  1667. length -= length;
  1668. __pskb_pull_tail(skb,
  1669. (ib_mac_rsp->flags2 & IB_MAC_IOCB_RSP_V) ?
  1670. VLAN_ETH_HLEN : ETH_HLEN);
  1671. }
  1672. } else {
  1673. /*
  1674. * The data is in a chain of large buffers
  1675. * pointed to by a small buffer. We loop
  1676. * thru and chain them to the our small header
  1677. * buffer's skb.
  1678. * frags: There are 18 max frags and our small
  1679. * buffer will hold 32 of them. The thing is,
  1680. * we'll use 3 max for our 9000 byte jumbo
  1681. * frames. If the MTU goes up we could
  1682. * eventually be in trouble.
  1683. */
  1684. int size, i = 0;
  1685. sbq_desc = ql_get_curr_sbuf(rx_ring);
  1686. pci_unmap_single(qdev->pdev,
  1687. dma_unmap_addr(sbq_desc, mapaddr),
  1688. dma_unmap_len(sbq_desc, maplen),
  1689. PCI_DMA_FROMDEVICE);
  1690. if (!(ib_mac_rsp->flags4 & IB_MAC_IOCB_RSP_HS)) {
  1691. /*
  1692. * This is an non TCP/UDP IP frame, so
  1693. * the headers aren't split into a small
  1694. * buffer. We have to use the small buffer
  1695. * that contains our sg list as our skb to
  1696. * send upstairs. Copy the sg list here to
  1697. * a local buffer and use it to find the
  1698. * pages to chain.
  1699. */
  1700. netif_printk(qdev, rx_status, KERN_DEBUG, qdev->ndev,
  1701. "%d bytes of headers & data in chain of large.\n",
  1702. length);
  1703. skb = sbq_desc->p.skb;
  1704. sbq_desc->p.skb = NULL;
  1705. skb_reserve(skb, NET_IP_ALIGN);
  1706. }
  1707. while (length > 0) {
  1708. lbq_desc = ql_get_curr_lchunk(qdev, rx_ring);
  1709. size = (length < rx_ring->lbq_buf_size) ? length :
  1710. rx_ring->lbq_buf_size;
  1711. netif_printk(qdev, rx_status, KERN_DEBUG, qdev->ndev,
  1712. "Adding page %d to skb for %d bytes.\n",
  1713. i, size);
  1714. skb_fill_page_desc(skb, i,
  1715. lbq_desc->p.pg_chunk.page,
  1716. lbq_desc->p.pg_chunk.offset,
  1717. size);
  1718. skb->len += size;
  1719. skb->data_len += size;
  1720. skb->truesize += size;
  1721. length -= size;
  1722. i++;
  1723. }
  1724. __pskb_pull_tail(skb, (ib_mac_rsp->flags2 & IB_MAC_IOCB_RSP_V) ?
  1725. VLAN_ETH_HLEN : ETH_HLEN);
  1726. }
  1727. return skb;
  1728. }
  1729. /* Process an inbound completion from an rx ring. */
  1730. static void ql_process_mac_split_rx_intr(struct ql_adapter *qdev,
  1731. struct rx_ring *rx_ring,
  1732. struct ib_mac_iocb_rsp *ib_mac_rsp,
  1733. u16 vlan_id)
  1734. {
  1735. struct net_device *ndev = qdev->ndev;
  1736. struct sk_buff *skb = NULL;
  1737. QL_DUMP_IB_MAC_RSP(ib_mac_rsp);
  1738. skb = ql_build_rx_skb(qdev, rx_ring, ib_mac_rsp);
  1739. if (unlikely(!skb)) {
  1740. netif_printk(qdev, rx_status, KERN_DEBUG, qdev->ndev,
  1741. "No skb available, drop packet.\n");
  1742. rx_ring->rx_dropped++;
  1743. return;
  1744. }
  1745. /* Frame error, so drop the packet. */
  1746. if (ib_mac_rsp->flags2 & IB_MAC_IOCB_RSP_ERR_MASK) {
  1747. netif_info(qdev, drv, qdev->ndev,
  1748. "Receive error, flags2 = 0x%x\n", ib_mac_rsp->flags2);
  1749. dev_kfree_skb_any(skb);
  1750. rx_ring->rx_errors++;
  1751. return;
  1752. }
  1753. /* The max framesize filter on this chip is set higher than
  1754. * MTU since FCoE uses 2k frames.
  1755. */
  1756. if (skb->len > ndev->mtu + ETH_HLEN) {
  1757. dev_kfree_skb_any(skb);
  1758. rx_ring->rx_dropped++;
  1759. return;
  1760. }
  1761. /* loopback self test for ethtool */
  1762. if (test_bit(QL_SELFTEST, &qdev->flags)) {
  1763. ql_check_lb_frame(qdev, skb);
  1764. dev_kfree_skb_any(skb);
  1765. return;
  1766. }
  1767. prefetch(skb->data);
  1768. if (ib_mac_rsp->flags1 & IB_MAC_IOCB_RSP_M_MASK) {
  1769. netif_printk(qdev, rx_status, KERN_DEBUG, qdev->ndev, "%s Multicast.\n",
  1770. (ib_mac_rsp->flags1 & IB_MAC_IOCB_RSP_M_MASK) ==
  1771. IB_MAC_IOCB_RSP_M_HASH ? "Hash" :
  1772. (ib_mac_rsp->flags1 & IB_MAC_IOCB_RSP_M_MASK) ==
  1773. IB_MAC_IOCB_RSP_M_REG ? "Registered" :
  1774. (ib_mac_rsp->flags1 & IB_MAC_IOCB_RSP_M_MASK) ==
  1775. IB_MAC_IOCB_RSP_M_PROM ? "Promiscuous" : "");
  1776. rx_ring->rx_multicast++;
  1777. }
  1778. if (ib_mac_rsp->flags2 & IB_MAC_IOCB_RSP_P) {
  1779. netif_printk(qdev, rx_status, KERN_DEBUG, qdev->ndev,
  1780. "Promiscuous Packet.\n");
  1781. }
  1782. skb->protocol = eth_type_trans(skb, ndev);
  1783. skb_checksum_none_assert(skb);
  1784. /* If rx checksum is on, and there are no
  1785. * csum or frame errors.
  1786. */
  1787. if ((ndev->features & NETIF_F_RXCSUM) &&
  1788. !(ib_mac_rsp->flags1 & IB_MAC_CSUM_ERR_MASK)) {
  1789. /* TCP frame. */
  1790. if (ib_mac_rsp->flags2 & IB_MAC_IOCB_RSP_T) {
  1791. netif_printk(qdev, rx_status, KERN_DEBUG, qdev->ndev,
  1792. "TCP checksum done!\n");
  1793. skb->ip_summed = CHECKSUM_UNNECESSARY;
  1794. } else if ((ib_mac_rsp->flags2 & IB_MAC_IOCB_RSP_U) &&
  1795. (ib_mac_rsp->flags3 & IB_MAC_IOCB_RSP_V4)) {
  1796. /* Unfragmented ipv4 UDP frame. */
  1797. struct iphdr *iph = (struct iphdr *) skb->data;
  1798. if (!(iph->frag_off &
  1799. ntohs(IP_MF|IP_OFFSET))) {
  1800. skb->ip_summed = CHECKSUM_UNNECESSARY;
  1801. netif_printk(qdev, rx_status, KERN_DEBUG, qdev->ndev,
  1802. "TCP checksum done!\n");
  1803. }
  1804. }
  1805. }
  1806. rx_ring->rx_packets++;
  1807. rx_ring->rx_bytes += skb->len;
  1808. skb_record_rx_queue(skb, rx_ring->cq_id);
  1809. if ((ib_mac_rsp->flags2 & IB_MAC_IOCB_RSP_V) && (vlan_id != 0))
  1810. __vlan_hwaccel_put_tag(skb, vlan_id);
  1811. if (skb->ip_summed == CHECKSUM_UNNECESSARY)
  1812. napi_gro_receive(&rx_ring->napi, skb);
  1813. else
  1814. netif_receive_skb(skb);
  1815. }
  1816. /* Process an inbound completion from an rx ring. */
  1817. static unsigned long ql_process_mac_rx_intr(struct ql_adapter *qdev,
  1818. struct rx_ring *rx_ring,
  1819. struct ib_mac_iocb_rsp *ib_mac_rsp)
  1820. {
  1821. u32 length = le32_to_cpu(ib_mac_rsp->data_len);
  1822. u16 vlan_id = (ib_mac_rsp->flags2 & IB_MAC_IOCB_RSP_V) ?
  1823. ((le16_to_cpu(ib_mac_rsp->vlan_id) &
  1824. IB_MAC_IOCB_RSP_VLAN_MASK)) : 0xffff;
  1825. QL_DUMP_IB_MAC_RSP(ib_mac_rsp);
  1826. if (ib_mac_rsp->flags4 & IB_MAC_IOCB_RSP_HV) {
  1827. /* The data and headers are split into
  1828. * separate buffers.
  1829. */
  1830. ql_process_mac_split_rx_intr(qdev, rx_ring, ib_mac_rsp,
  1831. vlan_id);
  1832. } else if (ib_mac_rsp->flags3 & IB_MAC_IOCB_RSP_DS) {
  1833. /* The data fit in a single small buffer.
  1834. * Allocate a new skb, copy the data and
  1835. * return the buffer to the free pool.
  1836. */
  1837. ql_process_mac_rx_skb(qdev, rx_ring, ib_mac_rsp,
  1838. length, vlan_id);
  1839. } else if ((ib_mac_rsp->flags3 & IB_MAC_IOCB_RSP_DL) &&
  1840. !(ib_mac_rsp->flags1 & IB_MAC_CSUM_ERR_MASK) &&
  1841. (ib_mac_rsp->flags2 & IB_MAC_IOCB_RSP_T)) {
  1842. /* TCP packet in a page chunk that's been checksummed.
  1843. * Tack it on to our GRO skb and let it go.
  1844. */
  1845. ql_process_mac_rx_gro_page(qdev, rx_ring, ib_mac_rsp,
  1846. length, vlan_id);
  1847. } else if (ib_mac_rsp->flags3 & IB_MAC_IOCB_RSP_DL) {
  1848. /* Non-TCP packet in a page chunk. Allocate an
  1849. * skb, tack it on frags, and send it up.
  1850. */
  1851. ql_process_mac_rx_page(qdev, rx_ring, ib_mac_rsp,
  1852. length, vlan_id);
  1853. } else {
  1854. /* Non-TCP/UDP large frames that span multiple buffers
  1855. * can be processed corrrectly by the split frame logic.
  1856. */
  1857. ql_process_mac_split_rx_intr(qdev, rx_ring, ib_mac_rsp,
  1858. vlan_id);
  1859. }
  1860. return (unsigned long)length;
  1861. }
  1862. /* Process an outbound completion from an rx ring. */
  1863. static void ql_process_mac_tx_intr(struct ql_adapter *qdev,
  1864. struct ob_mac_iocb_rsp *mac_rsp)
  1865. {
  1866. struct tx_ring *tx_ring;
  1867. struct tx_ring_desc *tx_ring_desc;
  1868. QL_DUMP_OB_MAC_RSP(mac_rsp);
  1869. tx_ring = &qdev->tx_ring[mac_rsp->txq_idx];
  1870. tx_ring_desc = &tx_ring->q[mac_rsp->tid];
  1871. ql_unmap_send(qdev, tx_ring_desc, tx_ring_desc->map_cnt);
  1872. tx_ring->tx_bytes += (tx_ring_desc->skb)->len;
  1873. tx_ring->tx_packets++;
  1874. dev_kfree_skb(tx_ring_desc->skb);
  1875. tx_ring_desc->skb = NULL;
  1876. if (unlikely(mac_rsp->flags1 & (OB_MAC_IOCB_RSP_E |
  1877. OB_MAC_IOCB_RSP_S |
  1878. OB_MAC_IOCB_RSP_L |
  1879. OB_MAC_IOCB_RSP_P | OB_MAC_IOCB_RSP_B))) {
  1880. if (mac_rsp->flags1 & OB_MAC_IOCB_RSP_E) {
  1881. netif_warn(qdev, tx_done, qdev->ndev,
  1882. "Total descriptor length did not match transfer length.\n");
  1883. }
  1884. if (mac_rsp->flags1 & OB_MAC_IOCB_RSP_S) {
  1885. netif_warn(qdev, tx_done, qdev->ndev,
  1886. "Frame too short to be valid, not sent.\n");
  1887. }
  1888. if (mac_rsp->flags1 & OB_MAC_IOCB_RSP_L) {
  1889. netif_warn(qdev, tx_done, qdev->ndev,
  1890. "Frame too long, but sent anyway.\n");
  1891. }
  1892. if (mac_rsp->flags1 & OB_MAC_IOCB_RSP_B) {
  1893. netif_warn(qdev, tx_done, qdev->ndev,
  1894. "PCI backplane error. Frame not sent.\n");
  1895. }
  1896. }
  1897. atomic_inc(&tx_ring->tx_count);
  1898. }
  1899. /* Fire up a handler to reset the MPI processor. */
  1900. void ql_queue_fw_error(struct ql_adapter *qdev)
  1901. {
  1902. ql_link_off(qdev);
  1903. queue_delayed_work(qdev->workqueue, &qdev->mpi_reset_work, 0);
  1904. }
  1905. void ql_queue_asic_error(struct ql_adapter *qdev)
  1906. {
  1907. ql_link_off(qdev);
  1908. ql_disable_interrupts(qdev);
  1909. /* Clear adapter up bit to signal the recovery
  1910. * process that it shouldn't kill the reset worker
  1911. * thread
  1912. */
  1913. clear_bit(QL_ADAPTER_UP, &qdev->flags);
  1914. /* Set asic recovery bit to indicate reset process that we are
  1915. * in fatal error recovery process rather than normal close
  1916. */
  1917. set_bit(QL_ASIC_RECOVERY, &qdev->flags);
  1918. queue_delayed_work(qdev->workqueue, &qdev->asic_reset_work, 0);
  1919. }
  1920. static void ql_process_chip_ae_intr(struct ql_adapter *qdev,
  1921. struct ib_ae_iocb_rsp *ib_ae_rsp)
  1922. {
  1923. switch (ib_ae_rsp->event) {
  1924. case MGMT_ERR_EVENT:
  1925. netif_err(qdev, rx_err, qdev->ndev,
  1926. "Management Processor Fatal Error.\n");
  1927. ql_queue_fw_error(qdev);
  1928. return;
  1929. case CAM_LOOKUP_ERR_EVENT:
  1930. netdev_err(qdev->ndev, "Multiple CAM hits lookup occurred.\n");
  1931. netdev_err(qdev->ndev, "This event shouldn't occur.\n");
  1932. ql_queue_asic_error(qdev);
  1933. return;
  1934. case SOFT_ECC_ERROR_EVENT:
  1935. netdev_err(qdev->ndev, "Soft ECC error detected.\n");
  1936. ql_queue_asic_error(qdev);
  1937. break;
  1938. case PCI_ERR_ANON_BUF_RD:
  1939. netdev_err(qdev->ndev, "PCI error occurred when reading "
  1940. "anonymous buffers from rx_ring %d.\n",
  1941. ib_ae_rsp->q_id);
  1942. ql_queue_asic_error(qdev);
  1943. break;
  1944. default:
  1945. netif_err(qdev, drv, qdev->ndev, "Unexpected event %d.\n",
  1946. ib_ae_rsp->event);
  1947. ql_queue_asic_error(qdev);
  1948. break;
  1949. }
  1950. }
  1951. static int ql_clean_outbound_rx_ring(struct rx_ring *rx_ring)
  1952. {
  1953. struct ql_adapter *qdev = rx_ring->qdev;
  1954. u32 prod = ql_read_sh_reg(rx_ring->prod_idx_sh_reg);
  1955. struct ob_mac_iocb_rsp *net_rsp = NULL;
  1956. int count = 0;
  1957. struct tx_ring *tx_ring;
  1958. /* While there are entries in the completion queue. */
  1959. while (prod != rx_ring->cnsmr_idx) {
  1960. netif_printk(qdev, rx_status, KERN_DEBUG, qdev->ndev,
  1961. "cq_id = %d, prod = %d, cnsmr = %d.\n.",
  1962. rx_ring->cq_id, prod, rx_ring->cnsmr_idx);
  1963. net_rsp = (struct ob_mac_iocb_rsp *)rx_ring->curr_entry;
  1964. rmb();
  1965. switch (net_rsp->opcode) {
  1966. case OPCODE_OB_MAC_TSO_IOCB:
  1967. case OPCODE_OB_MAC_IOCB:
  1968. ql_process_mac_tx_intr(qdev, net_rsp);
  1969. break;
  1970. default:
  1971. netif_printk(qdev, rx_status, KERN_DEBUG, qdev->ndev,
  1972. "Hit default case, not handled! dropping the packet, opcode = %x.\n",
  1973. net_rsp->opcode);
  1974. }
  1975. count++;
  1976. ql_update_cq(rx_ring);
  1977. prod = ql_read_sh_reg(rx_ring->prod_idx_sh_reg);
  1978. }
  1979. if (!net_rsp)
  1980. return 0;
  1981. ql_write_cq_idx(rx_ring);
  1982. tx_ring = &qdev->tx_ring[net_rsp->txq_idx];
  1983. if (__netif_subqueue_stopped(qdev->ndev, tx_ring->wq_id)) {
  1984. if ((atomic_read(&tx_ring->tx_count) > (tx_ring->wq_len / 4)))
  1985. /*
  1986. * The queue got stopped because the tx_ring was full.
  1987. * Wake it up, because it's now at least 25% empty.
  1988. */
  1989. netif_wake_subqueue(qdev->ndev, tx_ring->wq_id);
  1990. }
  1991. return count;
  1992. }
  1993. static int ql_clean_inbound_rx_ring(struct rx_ring *rx_ring, int budget)
  1994. {
  1995. struct ql_adapter *qdev = rx_ring->qdev;
  1996. u32 prod = ql_read_sh_reg(rx_ring->prod_idx_sh_reg);
  1997. struct ql_net_rsp_iocb *net_rsp;
  1998. int count = 0;
  1999. /* While there are entries in the completion queue. */
  2000. while (prod != rx_ring->cnsmr_idx) {
  2001. netif_printk(qdev, rx_status, KERN_DEBUG, qdev->ndev,
  2002. "cq_id = %d, prod = %d, cnsmr = %d.\n.",
  2003. rx_ring->cq_id, prod, rx_ring->cnsmr_idx);
  2004. net_rsp = rx_ring->curr_entry;
  2005. rmb();
  2006. switch (net_rsp->opcode) {
  2007. case OPCODE_IB_MAC_IOCB:
  2008. ql_process_mac_rx_intr(qdev, rx_ring,
  2009. (struct ib_mac_iocb_rsp *)
  2010. net_rsp);
  2011. break;
  2012. case OPCODE_IB_AE_IOCB:
  2013. ql_process_chip_ae_intr(qdev, (struct ib_ae_iocb_rsp *)
  2014. net_rsp);
  2015. break;
  2016. default:
  2017. netif_printk(qdev, rx_status, KERN_DEBUG, qdev->ndev,
  2018. "Hit default case, not handled! dropping the packet, opcode = %x.\n",
  2019. net_rsp->opcode);
  2020. break;
  2021. }
  2022. count++;
  2023. ql_update_cq(rx_ring);
  2024. prod = ql_read_sh_reg(rx_ring->prod_idx_sh_reg);
  2025. if (count == budget)
  2026. break;
  2027. }
  2028. ql_update_buffer_queues(qdev, rx_ring);
  2029. ql_write_cq_idx(rx_ring);
  2030. return count;
  2031. }
  2032. static int ql_napi_poll_msix(struct napi_struct *napi, int budget)
  2033. {
  2034. struct rx_ring *rx_ring = container_of(napi, struct rx_ring, napi);
  2035. struct ql_adapter *qdev = rx_ring->qdev;
  2036. struct rx_ring *trx_ring;
  2037. int i, work_done = 0;
  2038. struct intr_context *ctx = &qdev->intr_context[rx_ring->cq_id];
  2039. netif_printk(qdev, rx_status, KERN_DEBUG, qdev->ndev,
  2040. "Enter, NAPI POLL cq_id = %d.\n", rx_ring->cq_id);
  2041. /* Service the TX rings first. They start
  2042. * right after the RSS rings. */
  2043. for (i = qdev->rss_ring_count; i < qdev->rx_ring_count; i++) {
  2044. trx_ring = &qdev->rx_ring[i];
  2045. /* If this TX completion ring belongs to this vector and
  2046. * it's not empty then service it.
  2047. */
  2048. if ((ctx->irq_mask & (1 << trx_ring->cq_id)) &&
  2049. (ql_read_sh_reg(trx_ring->prod_idx_sh_reg) !=
  2050. trx_ring->cnsmr_idx)) {
  2051. netif_printk(qdev, intr, KERN_DEBUG, qdev->ndev,
  2052. "%s: Servicing TX completion ring %d.\n",
  2053. __func__, trx_ring->cq_id);
  2054. ql_clean_outbound_rx_ring(trx_ring);
  2055. }
  2056. }
  2057. /*
  2058. * Now service the RSS ring if it's active.
  2059. */
  2060. if (ql_read_sh_reg(rx_ring->prod_idx_sh_reg) !=
  2061. rx_ring->cnsmr_idx) {
  2062. netif_printk(qdev, intr, KERN_DEBUG, qdev->ndev,
  2063. "%s: Servicing RX completion ring %d.\n",
  2064. __func__, rx_ring->cq_id);
  2065. work_done = ql_clean_inbound_rx_ring(rx_ring, budget);
  2066. }
  2067. if (work_done < budget) {
  2068. napi_complete(napi);
  2069. ql_enable_completion_interrupt(qdev, rx_ring->irq);
  2070. }
  2071. return work_done;
  2072. }
  2073. static void qlge_vlan_mode(struct net_device *ndev, netdev_features_t features)
  2074. {
  2075. struct ql_adapter *qdev = netdev_priv(ndev);
  2076. if (features & NETIF_F_HW_VLAN_RX) {
  2077. ql_write32(qdev, NIC_RCV_CFG, NIC_RCV_CFG_VLAN_MASK |
  2078. NIC_RCV_CFG_VLAN_MATCH_AND_NON);
  2079. } else {
  2080. ql_write32(qdev, NIC_RCV_CFG, NIC_RCV_CFG_VLAN_MASK);
  2081. }
  2082. }
  2083. static netdev_features_t qlge_fix_features(struct net_device *ndev,
  2084. netdev_features_t features)
  2085. {
  2086. /*
  2087. * Since there is no support for separate rx/tx vlan accel
  2088. * enable/disable make sure tx flag is always in same state as rx.
  2089. */
  2090. if (features & NETIF_F_HW_VLAN_RX)
  2091. features |= NETIF_F_HW_VLAN_TX;
  2092. else
  2093. features &= ~NETIF_F_HW_VLAN_TX;
  2094. return features;
  2095. }
  2096. static int qlge_set_features(struct net_device *ndev,
  2097. netdev_features_t features)
  2098. {
  2099. netdev_features_t changed = ndev->features ^ features;
  2100. if (changed & NETIF_F_HW_VLAN_RX)
  2101. qlge_vlan_mode(ndev, features);
  2102. return 0;
  2103. }
  2104. static int __qlge_vlan_rx_add_vid(struct ql_adapter *qdev, u16 vid)
  2105. {
  2106. u32 enable_bit = MAC_ADDR_E;
  2107. int err;
  2108. err = ql_set_mac_addr_reg(qdev, (u8 *) &enable_bit,
  2109. MAC_ADDR_TYPE_VLAN, vid);
  2110. if (err)
  2111. netif_err(qdev, ifup, qdev->ndev,
  2112. "Failed to init vlan address.\n");
  2113. return err;
  2114. }
  2115. static int qlge_vlan_rx_add_vid(struct net_device *ndev, u16 vid)
  2116. {
  2117. struct ql_adapter *qdev = netdev_priv(ndev);
  2118. int status;
  2119. int err;
  2120. status = ql_sem_spinlock(qdev, SEM_MAC_ADDR_MASK);
  2121. if (status)
  2122. return status;
  2123. err = __qlge_vlan_rx_add_vid(qdev, vid);
  2124. set_bit(vid, qdev->active_vlans);
  2125. ql_sem_unlock(qdev, SEM_MAC_ADDR_MASK);
  2126. return err;
  2127. }
  2128. static int __qlge_vlan_rx_kill_vid(struct ql_adapter *qdev, u16 vid)
  2129. {
  2130. u32 enable_bit = 0;
  2131. int err;
  2132. err = ql_set_mac_addr_reg(qdev, (u8 *) &enable_bit,
  2133. MAC_ADDR_TYPE_VLAN, vid);
  2134. if (err)
  2135. netif_err(qdev, ifup, qdev->ndev,
  2136. "Failed to clear vlan address.\n");
  2137. return err;
  2138. }
  2139. static int qlge_vlan_rx_kill_vid(struct net_device *ndev, u16 vid)
  2140. {
  2141. struct ql_adapter *qdev = netdev_priv(ndev);
  2142. int status;
  2143. int err;
  2144. status = ql_sem_spinlock(qdev, SEM_MAC_ADDR_MASK);
  2145. if (status)
  2146. return status;
  2147. err = __qlge_vlan_rx_kill_vid(qdev, vid);
  2148. clear_bit(vid, qdev->active_vlans);
  2149. ql_sem_unlock(qdev, SEM_MAC_ADDR_MASK);
  2150. return err;
  2151. }
  2152. static void qlge_restore_vlan(struct ql_adapter *qdev)
  2153. {
  2154. int status;
  2155. u16 vid;
  2156. status = ql_sem_spinlock(qdev, SEM_MAC_ADDR_MASK);
  2157. if (status)
  2158. return;
  2159. for_each_set_bit(vid, qdev->active_vlans, VLAN_N_VID)
  2160. __qlge_vlan_rx_add_vid(qdev, vid);
  2161. ql_sem_unlock(qdev, SEM_MAC_ADDR_MASK);
  2162. }
  2163. /* MSI-X Multiple Vector Interrupt Handler for inbound completions. */
  2164. static irqreturn_t qlge_msix_rx_isr(int irq, void *dev_id)
  2165. {
  2166. struct rx_ring *rx_ring = dev_id;
  2167. napi_schedule(&rx_ring->napi);
  2168. return IRQ_HANDLED;
  2169. }
  2170. /* This handles a fatal error, MPI activity, and the default
  2171. * rx_ring in an MSI-X multiple vector environment.
  2172. * In MSI/Legacy environment it also process the rest of
  2173. * the rx_rings.
  2174. */
  2175. static irqreturn_t qlge_isr(int irq, void *dev_id)
  2176. {
  2177. struct rx_ring *rx_ring = dev_id;
  2178. struct ql_adapter *qdev = rx_ring->qdev;
  2179. struct intr_context *intr_context = &qdev->intr_context[0];
  2180. u32 var;
  2181. int work_done = 0;
  2182. spin_lock(&qdev->hw_lock);
  2183. if (atomic_read(&qdev->intr_context[0].irq_cnt)) {
  2184. netif_printk(qdev, intr, KERN_DEBUG, qdev->ndev,
  2185. "Shared Interrupt, Not ours!\n");
  2186. spin_unlock(&qdev->hw_lock);
  2187. return IRQ_NONE;
  2188. }
  2189. spin_unlock(&qdev->hw_lock);
  2190. var = ql_disable_completion_interrupt(qdev, intr_context->intr);
  2191. /*
  2192. * Check for fatal error.
  2193. */
  2194. if (var & STS_FE) {
  2195. ql_queue_asic_error(qdev);
  2196. netdev_err(qdev->ndev, "Got fatal error, STS = %x.\n", var);
  2197. var = ql_read32(qdev, ERR_STS);
  2198. netdev_err(qdev->ndev, "Resetting chip. "
  2199. "Error Status Register = 0x%x\n", var);
  2200. return IRQ_HANDLED;
  2201. }
  2202. /*
  2203. * Check MPI processor activity.
  2204. */
  2205. if ((var & STS_PI) &&
  2206. (ql_read32(qdev, INTR_MASK) & INTR_MASK_PI)) {
  2207. /*
  2208. * We've got an async event or mailbox completion.
  2209. * Handle it and clear the source of the interrupt.
  2210. */
  2211. netif_err(qdev, intr, qdev->ndev,
  2212. "Got MPI processor interrupt.\n");
  2213. ql_disable_completion_interrupt(qdev, intr_context->intr);
  2214. ql_write32(qdev, INTR_MASK, (INTR_MASK_PI << 16));
  2215. queue_delayed_work_on(smp_processor_id(),
  2216. qdev->workqueue, &qdev->mpi_work, 0);
  2217. work_done++;
  2218. }
  2219. /*
  2220. * Get the bit-mask that shows the active queues for this
  2221. * pass. Compare it to the queues that this irq services
  2222. * and call napi if there's a match.
  2223. */
  2224. var = ql_read32(qdev, ISR1);
  2225. if (var & intr_context->irq_mask) {
  2226. netif_info(qdev, intr, qdev->ndev,
  2227. "Waking handler for rx_ring[0].\n");
  2228. ql_disable_completion_interrupt(qdev, intr_context->intr);
  2229. napi_schedule(&rx_ring->napi);
  2230. work_done++;
  2231. }
  2232. ql_enable_completion_interrupt(qdev, intr_context->intr);
  2233. return work_done ? IRQ_HANDLED : IRQ_NONE;
  2234. }
  2235. static int ql_tso(struct sk_buff *skb, struct ob_mac_tso_iocb_req *mac_iocb_ptr)
  2236. {
  2237. if (skb_is_gso(skb)) {
  2238. int err;
  2239. if (skb_header_cloned(skb)) {
  2240. err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
  2241. if (err)
  2242. return err;
  2243. }
  2244. mac_iocb_ptr->opcode = OPCODE_OB_MAC_TSO_IOCB;
  2245. mac_iocb_ptr->flags3 |= OB_MAC_TSO_IOCB_IC;
  2246. mac_iocb_ptr->frame_len = cpu_to_le32((u32) skb->len);
  2247. mac_iocb_ptr->total_hdrs_len =
  2248. cpu_to_le16(skb_transport_offset(skb) + tcp_hdrlen(skb));
  2249. mac_iocb_ptr->net_trans_offset =
  2250. cpu_to_le16(skb_network_offset(skb) |
  2251. skb_transport_offset(skb)
  2252. << OB_MAC_TRANSPORT_HDR_SHIFT);
  2253. mac_iocb_ptr->mss = cpu_to_le16(skb_shinfo(skb)->gso_size);
  2254. mac_iocb_ptr->flags2 |= OB_MAC_TSO_IOCB_LSO;
  2255. if (likely(skb->protocol == htons(ETH_P_IP))) {
  2256. struct iphdr *iph = ip_hdr(skb);
  2257. iph->check = 0;
  2258. mac_iocb_ptr->flags1 |= OB_MAC_TSO_IOCB_IP4;
  2259. tcp_hdr(skb)->check = ~csum_tcpudp_magic(iph->saddr,
  2260. iph->daddr, 0,
  2261. IPPROTO_TCP,
  2262. 0);
  2263. } else if (skb->protocol == htons(ETH_P_IPV6)) {
  2264. mac_iocb_ptr->flags1 |= OB_MAC_TSO_IOCB_IP6;
  2265. tcp_hdr(skb)->check =
  2266. ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
  2267. &ipv6_hdr(skb)->daddr,
  2268. 0, IPPROTO_TCP, 0);
  2269. }
  2270. return 1;
  2271. }
  2272. return 0;
  2273. }
  2274. static void ql_hw_csum_setup(struct sk_buff *skb,
  2275. struct ob_mac_tso_iocb_req *mac_iocb_ptr)
  2276. {
  2277. int len;
  2278. struct iphdr *iph = ip_hdr(skb);
  2279. __sum16 *check;
  2280. mac_iocb_ptr->opcode = OPCODE_OB_MAC_TSO_IOCB;
  2281. mac_iocb_ptr->frame_len = cpu_to_le32((u32) skb->len);
  2282. mac_iocb_ptr->net_trans_offset =
  2283. cpu_to_le16(skb_network_offset(skb) |
  2284. skb_transport_offset(skb) << OB_MAC_TRANSPORT_HDR_SHIFT);
  2285. mac_iocb_ptr->flags1 |= OB_MAC_TSO_IOCB_IP4;
  2286. len = (ntohs(iph->tot_len) - (iph->ihl << 2));
  2287. if (likely(iph->protocol == IPPROTO_TCP)) {
  2288. check = &(tcp_hdr(skb)->check);
  2289. mac_iocb_ptr->flags2 |= OB_MAC_TSO_IOCB_TC;
  2290. mac_iocb_ptr->total_hdrs_len =
  2291. cpu_to_le16(skb_transport_offset(skb) +
  2292. (tcp_hdr(skb)->doff << 2));
  2293. } else {
  2294. check = &(udp_hdr(skb)->check);
  2295. mac_iocb_ptr->flags2 |= OB_MAC_TSO_IOCB_UC;
  2296. mac_iocb_ptr->total_hdrs_len =
  2297. cpu_to_le16(skb_transport_offset(skb) +
  2298. sizeof(struct udphdr));
  2299. }
  2300. *check = ~csum_tcpudp_magic(iph->saddr,
  2301. iph->daddr, len, iph->protocol, 0);
  2302. }
  2303. static netdev_tx_t qlge_send(struct sk_buff *skb, struct net_device *ndev)
  2304. {
  2305. struct tx_ring_desc *tx_ring_desc;
  2306. struct ob_mac_iocb_req *mac_iocb_ptr;
  2307. struct ql_adapter *qdev = netdev_priv(ndev);
  2308. int tso;
  2309. struct tx_ring *tx_ring;
  2310. u32 tx_ring_idx = (u32) skb->queue_mapping;
  2311. tx_ring = &qdev->tx_ring[tx_ring_idx];
  2312. if (skb_padto(skb, ETH_ZLEN))
  2313. return NETDEV_TX_OK;
  2314. if (unlikely(atomic_read(&tx_ring->tx_count) < 2)) {
  2315. netif_info(qdev, tx_queued, qdev->ndev,
  2316. "%s: BUG! shutting down tx queue %d due to lack of resources.\n",
  2317. __func__, tx_ring_idx);
  2318. netif_stop_subqueue(ndev, tx_ring->wq_id);
  2319. tx_ring->tx_errors++;
  2320. return NETDEV_TX_BUSY;
  2321. }
  2322. tx_ring_desc = &tx_ring->q[tx_ring->prod_idx];
  2323. mac_iocb_ptr = tx_ring_desc->queue_entry;
  2324. memset((void *)mac_iocb_ptr, 0, sizeof(*mac_iocb_ptr));
  2325. mac_iocb_ptr->opcode = OPCODE_OB_MAC_IOCB;
  2326. mac_iocb_ptr->tid = tx_ring_desc->index;
  2327. /* We use the upper 32-bits to store the tx queue for this IO.
  2328. * When we get the completion we can use it to establish the context.
  2329. */
  2330. mac_iocb_ptr->txq_idx = tx_ring_idx;
  2331. tx_ring_desc->skb = skb;
  2332. mac_iocb_ptr->frame_len = cpu_to_le16((u16) skb->len);
  2333. if (vlan_tx_tag_present(skb)) {
  2334. netif_printk(qdev, tx_queued, KERN_DEBUG, qdev->ndev,
  2335. "Adding a vlan tag %d.\n", vlan_tx_tag_get(skb));
  2336. mac_iocb_ptr->flags3 |= OB_MAC_IOCB_V;
  2337. mac_iocb_ptr->vlan_tci = cpu_to_le16(vlan_tx_tag_get(skb));
  2338. }
  2339. tso = ql_tso(skb, (struct ob_mac_tso_iocb_req *)mac_iocb_ptr);
  2340. if (tso < 0) {
  2341. dev_kfree_skb_any(skb);
  2342. return NETDEV_TX_OK;
  2343. } else if (unlikely(!tso) && (skb->ip_summed == CHECKSUM_PARTIAL)) {
  2344. ql_hw_csum_setup(skb,
  2345. (struct ob_mac_tso_iocb_req *)mac_iocb_ptr);
  2346. }
  2347. if (ql_map_send(qdev, mac_iocb_ptr, skb, tx_ring_desc) !=
  2348. NETDEV_TX_OK) {
  2349. netif_err(qdev, tx_queued, qdev->ndev,
  2350. "Could not map the segments.\n");
  2351. tx_ring->tx_errors++;
  2352. return NETDEV_TX_BUSY;
  2353. }
  2354. QL_DUMP_OB_MAC_IOCB(mac_iocb_ptr);
  2355. tx_ring->prod_idx++;
  2356. if (tx_ring->prod_idx == tx_ring->wq_len)
  2357. tx_ring->prod_idx = 0;
  2358. wmb();
  2359. ql_write_db_reg(tx_ring->prod_idx, tx_ring->prod_idx_db_reg);
  2360. netif_printk(qdev, tx_queued, KERN_DEBUG, qdev->ndev,
  2361. "tx queued, slot %d, len %d\n",
  2362. tx_ring->prod_idx, skb->len);
  2363. atomic_dec(&tx_ring->tx_count);
  2364. if (unlikely(atomic_read(&tx_ring->tx_count) < 2)) {
  2365. netif_stop_subqueue(ndev, tx_ring->wq_id);
  2366. if ((atomic_read(&tx_ring->tx_count) > (tx_ring->wq_len / 4)))
  2367. /*
  2368. * The queue got stopped because the tx_ring was full.
  2369. * Wake it up, because it's now at least 25% empty.
  2370. */
  2371. netif_wake_subqueue(qdev->ndev, tx_ring->wq_id);
  2372. }
  2373. return NETDEV_TX_OK;
  2374. }
  2375. static void ql_free_shadow_space(struct ql_adapter *qdev)
  2376. {
  2377. if (qdev->rx_ring_shadow_reg_area) {
  2378. pci_free_consistent(qdev->pdev,
  2379. PAGE_SIZE,
  2380. qdev->rx_ring_shadow_reg_area,
  2381. qdev->rx_ring_shadow_reg_dma);
  2382. qdev->rx_ring_shadow_reg_area = NULL;
  2383. }
  2384. if (qdev->tx_ring_shadow_reg_area) {
  2385. pci_free_consistent(qdev->pdev,
  2386. PAGE_SIZE,
  2387. qdev->tx_ring_shadow_reg_area,
  2388. qdev->tx_ring_shadow_reg_dma);
  2389. qdev->tx_ring_shadow_reg_area = NULL;
  2390. }
  2391. }
  2392. static int ql_alloc_shadow_space(struct ql_adapter *qdev)
  2393. {
  2394. qdev->rx_ring_shadow_reg_area =
  2395. pci_alloc_consistent(qdev->pdev,
  2396. PAGE_SIZE, &qdev->rx_ring_shadow_reg_dma);
  2397. if (qdev->rx_ring_shadow_reg_area == NULL) {
  2398. netif_err(qdev, ifup, qdev->ndev,
  2399. "Allocation of RX shadow space failed.\n");
  2400. return -ENOMEM;
  2401. }
  2402. memset(qdev->rx_ring_shadow_reg_area, 0, PAGE_SIZE);
  2403. qdev->tx_ring_shadow_reg_area =
  2404. pci_alloc_consistent(qdev->pdev, PAGE_SIZE,
  2405. &qdev->tx_ring_shadow_reg_dma);
  2406. if (qdev->tx_ring_shadow_reg_area == NULL) {
  2407. netif_err(qdev, ifup, qdev->ndev,
  2408. "Allocation of TX shadow space failed.\n");
  2409. goto err_wqp_sh_area;
  2410. }
  2411. memset(qdev->tx_ring_shadow_reg_area, 0, PAGE_SIZE);
  2412. return 0;
  2413. err_wqp_sh_area:
  2414. pci_free_consistent(qdev->pdev,
  2415. PAGE_SIZE,
  2416. qdev->rx_ring_shadow_reg_area,
  2417. qdev->rx_ring_shadow_reg_dma);
  2418. return -ENOMEM;
  2419. }
  2420. static void ql_init_tx_ring(struct ql_adapter *qdev, struct tx_ring *tx_ring)
  2421. {
  2422. struct tx_ring_desc *tx_ring_desc;
  2423. int i;
  2424. struct ob_mac_iocb_req *mac_iocb_ptr;
  2425. mac_iocb_ptr = tx_ring->wq_base;
  2426. tx_ring_desc = tx_ring->q;
  2427. for (i = 0; i < tx_ring->wq_len; i++) {
  2428. tx_ring_desc->index = i;
  2429. tx_ring_desc->skb = NULL;
  2430. tx_ring_desc->queue_entry = mac_iocb_ptr;
  2431. mac_iocb_ptr++;
  2432. tx_ring_desc++;
  2433. }
  2434. atomic_set(&tx_ring->tx_count, tx_ring->wq_len);
  2435. }
  2436. static void ql_free_tx_resources(struct ql_adapter *qdev,
  2437. struct tx_ring *tx_ring)
  2438. {
  2439. if (tx_ring->wq_base) {
  2440. pci_free_consistent(qdev->pdev, tx_ring->wq_size,
  2441. tx_ring->wq_base, tx_ring->wq_base_dma);
  2442. tx_ring->wq_base = NULL;
  2443. }
  2444. kfree(tx_ring->q);
  2445. tx_ring->q = NULL;
  2446. }
  2447. static int ql_alloc_tx_resources(struct ql_adapter *qdev,
  2448. struct tx_ring *tx_ring)
  2449. {
  2450. tx_ring->wq_base =
  2451. pci_alloc_consistent(qdev->pdev, tx_ring->wq_size,
  2452. &tx_ring->wq_base_dma);
  2453. if ((tx_ring->wq_base == NULL) ||
  2454. tx_ring->wq_base_dma & WQ_ADDR_ALIGN) {
  2455. netif_err(qdev, ifup, qdev->ndev, "tx_ring alloc failed.\n");
  2456. return -ENOMEM;
  2457. }
  2458. tx_ring->q =
  2459. kmalloc(tx_ring->wq_len * sizeof(struct tx_ring_desc), GFP_KERNEL);
  2460. if (tx_ring->q == NULL)
  2461. goto err;
  2462. return 0;
  2463. err:
  2464. pci_free_consistent(qdev->pdev, tx_ring->wq_size,
  2465. tx_ring->wq_base, tx_ring->wq_base_dma);
  2466. return -ENOMEM;
  2467. }
  2468. static void ql_free_lbq_buffers(struct ql_adapter *qdev, struct rx_ring *rx_ring)
  2469. {
  2470. struct bq_desc *lbq_desc;
  2471. uint32_t curr_idx, clean_idx;
  2472. curr_idx = rx_ring->lbq_curr_idx;
  2473. clean_idx = rx_ring->lbq_clean_idx;
  2474. while (curr_idx != clean_idx) {
  2475. lbq_desc = &rx_ring->lbq[curr_idx];
  2476. if (lbq_desc->p.pg_chunk.last_flag) {
  2477. pci_unmap_page(qdev->pdev,
  2478. lbq_desc->p.pg_chunk.map,
  2479. ql_lbq_block_size(qdev),
  2480. PCI_DMA_FROMDEVICE);
  2481. lbq_desc->p.pg_chunk.last_flag = 0;
  2482. }
  2483. put_page(lbq_desc->p.pg_chunk.page);
  2484. lbq_desc->p.pg_chunk.page = NULL;
  2485. if (++curr_idx == rx_ring->lbq_len)
  2486. curr_idx = 0;
  2487. }
  2488. }
  2489. static void ql_free_sbq_buffers(struct ql_adapter *qdev, struct rx_ring *rx_ring)
  2490. {
  2491. int i;
  2492. struct bq_desc *sbq_desc;
  2493. for (i = 0; i < rx_ring->sbq_len; i++) {
  2494. sbq_desc = &rx_ring->sbq[i];
  2495. if (sbq_desc == NULL) {
  2496. netif_err(qdev, ifup, qdev->ndev,
  2497. "sbq_desc %d is NULL.\n", i);
  2498. return;
  2499. }
  2500. if (sbq_desc->p.skb) {
  2501. pci_unmap_single(qdev->pdev,
  2502. dma_unmap_addr(sbq_desc, mapaddr),
  2503. dma_unmap_len(sbq_desc, maplen),
  2504. PCI_DMA_FROMDEVICE);
  2505. dev_kfree_skb(sbq_desc->p.skb);
  2506. sbq_desc->p.skb = NULL;
  2507. }
  2508. }
  2509. }
  2510. /* Free all large and small rx buffers associated
  2511. * with the completion queues for this device.
  2512. */
  2513. static void ql_free_rx_buffers(struct ql_adapter *qdev)
  2514. {
  2515. int i;
  2516. struct rx_ring *rx_ring;
  2517. for (i = 0; i < qdev->rx_ring_count; i++) {
  2518. rx_ring = &qdev->rx_ring[i];
  2519. if (rx_ring->lbq)
  2520. ql_free_lbq_buffers(qdev, rx_ring);
  2521. if (rx_ring->sbq)
  2522. ql_free_sbq_buffers(qdev, rx_ring);
  2523. }
  2524. }
  2525. static void ql_alloc_rx_buffers(struct ql_adapter *qdev)
  2526. {
  2527. struct rx_ring *rx_ring;
  2528. int i;
  2529. for (i = 0; i < qdev->rx_ring_count; i++) {
  2530. rx_ring = &qdev->rx_ring[i];
  2531. if (rx_ring->type != TX_Q)
  2532. ql_update_buffer_queues(qdev, rx_ring);
  2533. }
  2534. }
  2535. static void ql_init_lbq_ring(struct ql_adapter *qdev,
  2536. struct rx_ring *rx_ring)
  2537. {
  2538. int i;
  2539. struct bq_desc *lbq_desc;
  2540. __le64 *bq = rx_ring->lbq_base;
  2541. memset(rx_ring->lbq, 0, rx_ring->lbq_len * sizeof(struct bq_desc));
  2542. for (i = 0; i < rx_ring->lbq_len; i++) {
  2543. lbq_desc = &rx_ring->lbq[i];
  2544. memset(lbq_desc, 0, sizeof(*lbq_desc));
  2545. lbq_desc->index = i;
  2546. lbq_desc->addr = bq;
  2547. bq++;
  2548. }
  2549. }
  2550. static void ql_init_sbq_ring(struct ql_adapter *qdev,
  2551. struct rx_ring *rx_ring)
  2552. {
  2553. int i;
  2554. struct bq_desc *sbq_desc;
  2555. __le64 *bq = rx_ring->sbq_base;
  2556. memset(rx_ring->sbq, 0, rx_ring->sbq_len * sizeof(struct bq_desc));
  2557. for (i = 0; i < rx_ring->sbq_len; i++) {
  2558. sbq_desc = &rx_ring->sbq[i];
  2559. memset(sbq_desc, 0, sizeof(*sbq_desc));
  2560. sbq_desc->index = i;
  2561. sbq_desc->addr = bq;
  2562. bq++;
  2563. }
  2564. }
  2565. static void ql_free_rx_resources(struct ql_adapter *qdev,
  2566. struct rx_ring *rx_ring)
  2567. {
  2568. /* Free the small buffer queue. */
  2569. if (rx_ring->sbq_base) {
  2570. pci_free_consistent(qdev->pdev,
  2571. rx_ring->sbq_size,
  2572. rx_ring->sbq_base, rx_ring->sbq_base_dma);
  2573. rx_ring->sbq_base = NULL;
  2574. }
  2575. /* Free the small buffer queue control blocks. */
  2576. kfree(rx_ring->sbq);
  2577. rx_ring->sbq = NULL;
  2578. /* Free the large buffer queue. */
  2579. if (rx_ring->lbq_base) {
  2580. pci_free_consistent(qdev->pdev,
  2581. rx_ring->lbq_size,
  2582. rx_ring->lbq_base, rx_ring->lbq_base_dma);
  2583. rx_ring->lbq_base = NULL;
  2584. }
  2585. /* Free the large buffer queue control blocks. */
  2586. kfree(rx_ring->lbq);
  2587. rx_ring->lbq = NULL;
  2588. /* Free the rx queue. */
  2589. if (rx_ring->cq_base) {
  2590. pci_free_consistent(qdev->pdev,
  2591. rx_ring->cq_size,
  2592. rx_ring->cq_base, rx_ring->cq_base_dma);
  2593. rx_ring->cq_base = NULL;
  2594. }
  2595. }
  2596. /* Allocate queues and buffers for this completions queue based
  2597. * on the values in the parameter structure. */
  2598. static int ql_alloc_rx_resources(struct ql_adapter *qdev,
  2599. struct rx_ring *rx_ring)
  2600. {
  2601. /*
  2602. * Allocate the completion queue for this rx_ring.
  2603. */
  2604. rx_ring->cq_base =
  2605. pci_alloc_consistent(qdev->pdev, rx_ring->cq_size,
  2606. &rx_ring->cq_base_dma);
  2607. if (rx_ring->cq_base == NULL) {
  2608. netif_err(qdev, ifup, qdev->ndev, "rx_ring alloc failed.\n");
  2609. return -ENOMEM;
  2610. }
  2611. if (rx_ring->sbq_len) {
  2612. /*
  2613. * Allocate small buffer queue.
  2614. */
  2615. rx_ring->sbq_base =
  2616. pci_alloc_consistent(qdev->pdev, rx_ring->sbq_size,
  2617. &rx_ring->sbq_base_dma);
  2618. if (rx_ring->sbq_base == NULL) {
  2619. netif_err(qdev, ifup, qdev->ndev,
  2620. "Small buffer queue allocation failed.\n");
  2621. goto err_mem;
  2622. }
  2623. /*
  2624. * Allocate small buffer queue control blocks.
  2625. */
  2626. rx_ring->sbq =
  2627. kmalloc(rx_ring->sbq_len * sizeof(struct bq_desc),
  2628. GFP_KERNEL);
  2629. if (rx_ring->sbq == NULL) {
  2630. netif_err(qdev, ifup, qdev->ndev,
  2631. "Small buffer queue control block allocation failed.\n");
  2632. goto err_mem;
  2633. }
  2634. ql_init_sbq_ring(qdev, rx_ring);
  2635. }
  2636. if (rx_ring->lbq_len) {
  2637. /*
  2638. * Allocate large buffer queue.
  2639. */
  2640. rx_ring->lbq_base =
  2641. pci_alloc_consistent(qdev->pdev, rx_ring->lbq_size,
  2642. &rx_ring->lbq_base_dma);
  2643. if (rx_ring->lbq_base == NULL) {
  2644. netif_err(qdev, ifup, qdev->ndev,
  2645. "Large buffer queue allocation failed.\n");
  2646. goto err_mem;
  2647. }
  2648. /*
  2649. * Allocate large buffer queue control blocks.
  2650. */
  2651. rx_ring->lbq =
  2652. kmalloc(rx_ring->lbq_len * sizeof(struct bq_desc),
  2653. GFP_KERNEL);
  2654. if (rx_ring->lbq == NULL) {
  2655. netif_err(qdev, ifup, qdev->ndev,
  2656. "Large buffer queue control block allocation failed.\n");
  2657. goto err_mem;
  2658. }
  2659. ql_init_lbq_ring(qdev, rx_ring);
  2660. }
  2661. return 0;
  2662. err_mem:
  2663. ql_free_rx_resources(qdev, rx_ring);
  2664. return -ENOMEM;
  2665. }
  2666. static void ql_tx_ring_clean(struct ql_adapter *qdev)
  2667. {
  2668. struct tx_ring *tx_ring;
  2669. struct tx_ring_desc *tx_ring_desc;
  2670. int i, j;
  2671. /*
  2672. * Loop through all queues and free
  2673. * any resources.
  2674. */
  2675. for (j = 0; j < qdev->tx_ring_count; j++) {
  2676. tx_ring = &qdev->tx_ring[j];
  2677. for (i = 0; i < tx_ring->wq_len; i++) {
  2678. tx_ring_desc = &tx_ring->q[i];
  2679. if (tx_ring_desc && tx_ring_desc->skb) {
  2680. netif_err(qdev, ifdown, qdev->ndev,
  2681. "Freeing lost SKB %p, from queue %d, index %d.\n",
  2682. tx_ring_desc->skb, j,
  2683. tx_ring_desc->index);
  2684. ql_unmap_send(qdev, tx_ring_desc,
  2685. tx_ring_desc->map_cnt);
  2686. dev_kfree_skb(tx_ring_desc->skb);
  2687. tx_ring_desc->skb = NULL;
  2688. }
  2689. }
  2690. }
  2691. }
  2692. static void ql_free_mem_resources(struct ql_adapter *qdev)
  2693. {
  2694. int i;
  2695. for (i = 0; i < qdev->tx_ring_count; i++)
  2696. ql_free_tx_resources(qdev, &qdev->tx_ring[i]);
  2697. for (i = 0; i < qdev->rx_ring_count; i++)
  2698. ql_free_rx_resources(qdev, &qdev->rx_ring[i]);
  2699. ql_free_shadow_space(qdev);
  2700. }
  2701. static int ql_alloc_mem_resources(struct ql_adapter *qdev)
  2702. {
  2703. int i;
  2704. /* Allocate space for our shadow registers and such. */
  2705. if (ql_alloc_shadow_space(qdev))
  2706. return -ENOMEM;
  2707. for (i = 0; i < qdev->rx_ring_count; i++) {
  2708. if (ql_alloc_rx_resources(qdev, &qdev->rx_ring[i]) != 0) {
  2709. netif_err(qdev, ifup, qdev->ndev,
  2710. "RX resource allocation failed.\n");
  2711. goto err_mem;
  2712. }
  2713. }
  2714. /* Allocate tx queue resources */
  2715. for (i = 0; i < qdev->tx_ring_count; i++) {
  2716. if (ql_alloc_tx_resources(qdev, &qdev->tx_ring[i]) != 0) {
  2717. netif_err(qdev, ifup, qdev->ndev,
  2718. "TX resource allocation failed.\n");
  2719. goto err_mem;
  2720. }
  2721. }
  2722. return 0;
  2723. err_mem:
  2724. ql_free_mem_resources(qdev);
  2725. return -ENOMEM;
  2726. }
  2727. /* Set up the rx ring control block and pass it to the chip.
  2728. * The control block is defined as
  2729. * "Completion Queue Initialization Control Block", or cqicb.
  2730. */
  2731. static int ql_start_rx_ring(struct ql_adapter *qdev, struct rx_ring *rx_ring)
  2732. {
  2733. struct cqicb *cqicb = &rx_ring->cqicb;
  2734. void *shadow_reg = qdev->rx_ring_shadow_reg_area +
  2735. (rx_ring->cq_id * RX_RING_SHADOW_SPACE);
  2736. u64 shadow_reg_dma = qdev->rx_ring_shadow_reg_dma +
  2737. (rx_ring->cq_id * RX_RING_SHADOW_SPACE);
  2738. void __iomem *doorbell_area =
  2739. qdev->doorbell_area + (DB_PAGE_SIZE * (128 + rx_ring->cq_id));
  2740. int err = 0;
  2741. u16 bq_len;
  2742. u64 tmp;
  2743. __le64 *base_indirect_ptr;
  2744. int page_entries;
  2745. /* Set up the shadow registers for this ring. */
  2746. rx_ring->prod_idx_sh_reg = shadow_reg;
  2747. rx_ring->prod_idx_sh_reg_dma = shadow_reg_dma;
  2748. *rx_ring->prod_idx_sh_reg = 0;
  2749. shadow_reg += sizeof(u64);
  2750. shadow_reg_dma += sizeof(u64);
  2751. rx_ring->lbq_base_indirect = shadow_reg;
  2752. rx_ring->lbq_base_indirect_dma = shadow_reg_dma;
  2753. shadow_reg += (sizeof(u64) * MAX_DB_PAGES_PER_BQ(rx_ring->lbq_len));
  2754. shadow_reg_dma += (sizeof(u64) * MAX_DB_PAGES_PER_BQ(rx_ring->lbq_len));
  2755. rx_ring->sbq_base_indirect = shadow_reg;
  2756. rx_ring->sbq_base_indirect_dma = shadow_reg_dma;
  2757. /* PCI doorbell mem area + 0x00 for consumer index register */
  2758. rx_ring->cnsmr_idx_db_reg = (u32 __iomem *) doorbell_area;
  2759. rx_ring->cnsmr_idx = 0;
  2760. rx_ring->curr_entry = rx_ring->cq_base;
  2761. /* PCI doorbell mem area + 0x04 for valid register */
  2762. rx_ring->valid_db_reg = doorbell_area + 0x04;
  2763. /* PCI doorbell mem area + 0x18 for large buffer consumer */
  2764. rx_ring->lbq_prod_idx_db_reg = (u32 __iomem *) (doorbell_area + 0x18);
  2765. /* PCI doorbell mem area + 0x1c */
  2766. rx_ring->sbq_prod_idx_db_reg = (u32 __iomem *) (doorbell_area + 0x1c);
  2767. memset((void *)cqicb, 0, sizeof(struct cqicb));
  2768. cqicb->msix_vect = rx_ring->irq;
  2769. bq_len = (rx_ring->cq_len == 65536) ? 0 : (u16) rx_ring->cq_len;
  2770. cqicb->len = cpu_to_le16(bq_len | LEN_V | LEN_CPP_CONT);
  2771. cqicb->addr = cpu_to_le64(rx_ring->cq_base_dma);
  2772. cqicb->prod_idx_addr = cpu_to_le64(rx_ring->prod_idx_sh_reg_dma);
  2773. /*
  2774. * Set up the control block load flags.
  2775. */
  2776. cqicb->flags = FLAGS_LC | /* Load queue base address */
  2777. FLAGS_LV | /* Load MSI-X vector */
  2778. FLAGS_LI; /* Load irq delay values */
  2779. if (rx_ring->lbq_len) {
  2780. cqicb->flags |= FLAGS_LL; /* Load lbq values */
  2781. tmp = (u64)rx_ring->lbq_base_dma;
  2782. base_indirect_ptr = rx_ring->lbq_base_indirect;
  2783. page_entries = 0;
  2784. do {
  2785. *base_indirect_ptr = cpu_to_le64(tmp);
  2786. tmp += DB_PAGE_SIZE;
  2787. base_indirect_ptr++;
  2788. page_entries++;
  2789. } while (page_entries < MAX_DB_PAGES_PER_BQ(rx_ring->lbq_len));
  2790. cqicb->lbq_addr =
  2791. cpu_to_le64(rx_ring->lbq_base_indirect_dma);
  2792. bq_len = (rx_ring->lbq_buf_size == 65536) ? 0 :
  2793. (u16) rx_ring->lbq_buf_size;
  2794. cqicb->lbq_buf_size = cpu_to_le16(bq_len);
  2795. bq_len = (rx_ring->lbq_len == 65536) ? 0 :
  2796. (u16) rx_ring->lbq_len;
  2797. cqicb->lbq_len = cpu_to_le16(bq_len);
  2798. rx_ring->lbq_prod_idx = 0;
  2799. rx_ring->lbq_curr_idx = 0;
  2800. rx_ring->lbq_clean_idx = 0;
  2801. rx_ring->lbq_free_cnt = rx_ring->lbq_len;
  2802. }
  2803. if (rx_ring->sbq_len) {
  2804. cqicb->flags |= FLAGS_LS; /* Load sbq values */
  2805. tmp = (u64)rx_ring->sbq_base_dma;
  2806. base_indirect_ptr = rx_ring->sbq_base_indirect;
  2807. page_entries = 0;
  2808. do {
  2809. *base_indirect_ptr = cpu_to_le64(tmp);
  2810. tmp += DB_PAGE_SIZE;
  2811. base_indirect_ptr++;
  2812. page_entries++;
  2813. } while (page_entries < MAX_DB_PAGES_PER_BQ(rx_ring->sbq_len));
  2814. cqicb->sbq_addr =
  2815. cpu_to_le64(rx_ring->sbq_base_indirect_dma);
  2816. cqicb->sbq_buf_size =
  2817. cpu_to_le16((u16)(rx_ring->sbq_buf_size));
  2818. bq_len = (rx_ring->sbq_len == 65536) ? 0 :
  2819. (u16) rx_ring->sbq_len;
  2820. cqicb->sbq_len = cpu_to_le16(bq_len);
  2821. rx_ring->sbq_prod_idx = 0;
  2822. rx_ring->sbq_curr_idx = 0;
  2823. rx_ring->sbq_clean_idx = 0;
  2824. rx_ring->sbq_free_cnt = rx_ring->sbq_len;
  2825. }
  2826. switch (rx_ring->type) {
  2827. case TX_Q:
  2828. cqicb->irq_delay = cpu_to_le16(qdev->tx_coalesce_usecs);
  2829. cqicb->pkt_delay = cpu_to_le16(qdev->tx_max_coalesced_frames);
  2830. break;
  2831. case RX_Q:
  2832. /* Inbound completion handling rx_rings run in
  2833. * separate NAPI contexts.
  2834. */
  2835. netif_napi_add(qdev->ndev, &rx_ring->napi, ql_napi_poll_msix,
  2836. 64);
  2837. cqicb->irq_delay = cpu_to_le16(qdev->rx_coalesce_usecs);
  2838. cqicb->pkt_delay = cpu_to_le16(qdev->rx_max_coalesced_frames);
  2839. break;
  2840. default:
  2841. netif_printk(qdev, ifup, KERN_DEBUG, qdev->ndev,
  2842. "Invalid rx_ring->type = %d.\n", rx_ring->type);
  2843. }
  2844. err = ql_write_cfg(qdev, cqicb, sizeof(struct cqicb),
  2845. CFG_LCQ, rx_ring->cq_id);
  2846. if (err) {
  2847. netif_err(qdev, ifup, qdev->ndev, "Failed to load CQICB.\n");
  2848. return err;
  2849. }
  2850. return err;
  2851. }
  2852. static int ql_start_tx_ring(struct ql_adapter *qdev, struct tx_ring *tx_ring)
  2853. {
  2854. struct wqicb *wqicb = (struct wqicb *)tx_ring;
  2855. void __iomem *doorbell_area =
  2856. qdev->doorbell_area + (DB_PAGE_SIZE * tx_ring->wq_id);
  2857. void *shadow_reg = qdev->tx_ring_shadow_reg_area +
  2858. (tx_ring->wq_id * sizeof(u64));
  2859. u64 shadow_reg_dma = qdev->tx_ring_shadow_reg_dma +
  2860. (tx_ring->wq_id * sizeof(u64));
  2861. int err = 0;
  2862. /*
  2863. * Assign doorbell registers for this tx_ring.
  2864. */
  2865. /* TX PCI doorbell mem area for tx producer index */
  2866. tx_ring->prod_idx_db_reg = (u32 __iomem *) doorbell_area;
  2867. tx_ring->prod_idx = 0;
  2868. /* TX PCI doorbell mem area + 0x04 */
  2869. tx_ring->valid_db_reg = doorbell_area + 0x04;
  2870. /*
  2871. * Assign shadow registers for this tx_ring.
  2872. */
  2873. tx_ring->cnsmr_idx_sh_reg = shadow_reg;
  2874. tx_ring->cnsmr_idx_sh_reg_dma = shadow_reg_dma;
  2875. wqicb->len = cpu_to_le16(tx_ring->wq_len | Q_LEN_V | Q_LEN_CPP_CONT);
  2876. wqicb->flags = cpu_to_le16(Q_FLAGS_LC |
  2877. Q_FLAGS_LB | Q_FLAGS_LI | Q_FLAGS_LO);
  2878. wqicb->cq_id_rss = cpu_to_le16(tx_ring->cq_id);
  2879. wqicb->rid = 0;
  2880. wqicb->addr = cpu_to_le64(tx_ring->wq_base_dma);
  2881. wqicb->cnsmr_idx_addr = cpu_to_le64(tx_ring->cnsmr_idx_sh_reg_dma);
  2882. ql_init_tx_ring(qdev, tx_ring);
  2883. err = ql_write_cfg(qdev, wqicb, sizeof(*wqicb), CFG_LRQ,
  2884. (u16) tx_ring->wq_id);
  2885. if (err) {
  2886. netif_err(qdev, ifup, qdev->ndev, "Failed to load tx_ring.\n");
  2887. return err;
  2888. }
  2889. return err;
  2890. }
  2891. static void ql_disable_msix(struct ql_adapter *qdev)
  2892. {
  2893. if (test_bit(QL_MSIX_ENABLED, &qdev->flags)) {
  2894. pci_disable_msix(qdev->pdev);
  2895. clear_bit(QL_MSIX_ENABLED, &qdev->flags);
  2896. kfree(qdev->msi_x_entry);
  2897. qdev->msi_x_entry = NULL;
  2898. } else if (test_bit(QL_MSI_ENABLED, &qdev->flags)) {
  2899. pci_disable_msi(qdev->pdev);
  2900. clear_bit(QL_MSI_ENABLED, &qdev->flags);
  2901. }
  2902. }
  2903. /* We start by trying to get the number of vectors
  2904. * stored in qdev->intr_count. If we don't get that
  2905. * many then we reduce the count and try again.
  2906. */
  2907. static void ql_enable_msix(struct ql_adapter *qdev)
  2908. {
  2909. int i, err;
  2910. /* Get the MSIX vectors. */
  2911. if (qlge_irq_type == MSIX_IRQ) {
  2912. /* Try to alloc space for the msix struct,
  2913. * if it fails then go to MSI/legacy.
  2914. */
  2915. qdev->msi_x_entry = kcalloc(qdev->intr_count,
  2916. sizeof(struct msix_entry),
  2917. GFP_KERNEL);
  2918. if (!qdev->msi_x_entry) {
  2919. qlge_irq_type = MSI_IRQ;
  2920. goto msi;
  2921. }
  2922. for (i = 0; i < qdev->intr_count; i++)
  2923. qdev->msi_x_entry[i].entry = i;
  2924. /* Loop to get our vectors. We start with
  2925. * what we want and settle for what we get.
  2926. */
  2927. do {
  2928. err = pci_enable_msix(qdev->pdev,
  2929. qdev->msi_x_entry, qdev->intr_count);
  2930. if (err > 0)
  2931. qdev->intr_count = err;
  2932. } while (err > 0);
  2933. if (err < 0) {
  2934. kfree(qdev->msi_x_entry);
  2935. qdev->msi_x_entry = NULL;
  2936. netif_warn(qdev, ifup, qdev->ndev,
  2937. "MSI-X Enable failed, trying MSI.\n");
  2938. qdev->intr_count = 1;
  2939. qlge_irq_type = MSI_IRQ;
  2940. } else if (err == 0) {
  2941. set_bit(QL_MSIX_ENABLED, &qdev->flags);
  2942. netif_info(qdev, ifup, qdev->ndev,
  2943. "MSI-X Enabled, got %d vectors.\n",
  2944. qdev->intr_count);
  2945. return;
  2946. }
  2947. }
  2948. msi:
  2949. qdev->intr_count = 1;
  2950. if (qlge_irq_type == MSI_IRQ) {
  2951. if (!pci_enable_msi(qdev->pdev)) {
  2952. set_bit(QL_MSI_ENABLED, &qdev->flags);
  2953. netif_info(qdev, ifup, qdev->ndev,
  2954. "Running with MSI interrupts.\n");
  2955. return;
  2956. }
  2957. }
  2958. qlge_irq_type = LEG_IRQ;
  2959. netif_printk(qdev, ifup, KERN_DEBUG, qdev->ndev,
  2960. "Running with legacy interrupts.\n");
  2961. }
  2962. /* Each vector services 1 RSS ring and and 1 or more
  2963. * TX completion rings. This function loops through
  2964. * the TX completion rings and assigns the vector that
  2965. * will service it. An example would be if there are
  2966. * 2 vectors (so 2 RSS rings) and 8 TX completion rings.
  2967. * This would mean that vector 0 would service RSS ring 0
  2968. * and TX completion rings 0,1,2 and 3. Vector 1 would
  2969. * service RSS ring 1 and TX completion rings 4,5,6 and 7.
  2970. */
  2971. static void ql_set_tx_vect(struct ql_adapter *qdev)
  2972. {
  2973. int i, j, vect;
  2974. u32 tx_rings_per_vector = qdev->tx_ring_count / qdev->intr_count;
  2975. if (likely(test_bit(QL_MSIX_ENABLED, &qdev->flags))) {
  2976. /* Assign irq vectors to TX rx_rings.*/
  2977. for (vect = 0, j = 0, i = qdev->rss_ring_count;
  2978. i < qdev->rx_ring_count; i++) {
  2979. if (j == tx_rings_per_vector) {
  2980. vect++;
  2981. j = 0;
  2982. }
  2983. qdev->rx_ring[i].irq = vect;
  2984. j++;
  2985. }
  2986. } else {
  2987. /* For single vector all rings have an irq
  2988. * of zero.
  2989. */
  2990. for (i = 0; i < qdev->rx_ring_count; i++)
  2991. qdev->rx_ring[i].irq = 0;
  2992. }
  2993. }
  2994. /* Set the interrupt mask for this vector. Each vector
  2995. * will service 1 RSS ring and 1 or more TX completion
  2996. * rings. This function sets up a bit mask per vector
  2997. * that indicates which rings it services.
  2998. */
  2999. static void ql_set_irq_mask(struct ql_adapter *qdev, struct intr_context *ctx)
  3000. {
  3001. int j, vect = ctx->intr;
  3002. u32 tx_rings_per_vector = qdev->tx_ring_count / qdev->intr_count;
  3003. if (likely(test_bit(QL_MSIX_ENABLED, &qdev->flags))) {
  3004. /* Add the RSS ring serviced by this vector
  3005. * to the mask.
  3006. */
  3007. ctx->irq_mask = (1 << qdev->rx_ring[vect].cq_id);
  3008. /* Add the TX ring(s) serviced by this vector
  3009. * to the mask. */
  3010. for (j = 0; j < tx_rings_per_vector; j++) {
  3011. ctx->irq_mask |=
  3012. (1 << qdev->rx_ring[qdev->rss_ring_count +
  3013. (vect * tx_rings_per_vector) + j].cq_id);
  3014. }
  3015. } else {
  3016. /* For single vector we just shift each queue's
  3017. * ID into the mask.
  3018. */
  3019. for (j = 0; j < qdev->rx_ring_count; j++)
  3020. ctx->irq_mask |= (1 << qdev->rx_ring[j].cq_id);
  3021. }
  3022. }
  3023. /*
  3024. * Here we build the intr_context structures based on
  3025. * our rx_ring count and intr vector count.
  3026. * The intr_context structure is used to hook each vector
  3027. * to possibly different handlers.
  3028. */
  3029. static void ql_resolve_queues_to_irqs(struct ql_adapter *qdev)
  3030. {
  3031. int i = 0;
  3032. struct intr_context *intr_context = &qdev->intr_context[0];
  3033. if (likely(test_bit(QL_MSIX_ENABLED, &qdev->flags))) {
  3034. /* Each rx_ring has it's
  3035. * own intr_context since we have separate
  3036. * vectors for each queue.
  3037. */
  3038. for (i = 0; i < qdev->intr_count; i++, intr_context++) {
  3039. qdev->rx_ring[i].irq = i;
  3040. intr_context->intr = i;
  3041. intr_context->qdev = qdev;
  3042. /* Set up this vector's bit-mask that indicates
  3043. * which queues it services.
  3044. */
  3045. ql_set_irq_mask(qdev, intr_context);
  3046. /*
  3047. * We set up each vectors enable/disable/read bits so
  3048. * there's no bit/mask calculations in the critical path.
  3049. */
  3050. intr_context->intr_en_mask =
  3051. INTR_EN_TYPE_MASK | INTR_EN_INTR_MASK |
  3052. INTR_EN_TYPE_ENABLE | INTR_EN_IHD_MASK | INTR_EN_IHD
  3053. | i;
  3054. intr_context->intr_dis_mask =
  3055. INTR_EN_TYPE_MASK | INTR_EN_INTR_MASK |
  3056. INTR_EN_TYPE_DISABLE | INTR_EN_IHD_MASK |
  3057. INTR_EN_IHD | i;
  3058. intr_context->intr_read_mask =
  3059. INTR_EN_TYPE_MASK | INTR_EN_INTR_MASK |
  3060. INTR_EN_TYPE_READ | INTR_EN_IHD_MASK | INTR_EN_IHD |
  3061. i;
  3062. if (i == 0) {
  3063. /* The first vector/queue handles
  3064. * broadcast/multicast, fatal errors,
  3065. * and firmware events. This in addition
  3066. * to normal inbound NAPI processing.
  3067. */
  3068. intr_context->handler = qlge_isr;
  3069. sprintf(intr_context->name, "%s-rx-%d",
  3070. qdev->ndev->name, i);
  3071. } else {
  3072. /*
  3073. * Inbound queues handle unicast frames only.
  3074. */
  3075. intr_context->handler = qlge_msix_rx_isr;
  3076. sprintf(intr_context->name, "%s-rx-%d",
  3077. qdev->ndev->name, i);
  3078. }
  3079. }
  3080. } else {
  3081. /*
  3082. * All rx_rings use the same intr_context since
  3083. * there is only one vector.
  3084. */
  3085. intr_context->intr = 0;
  3086. intr_context->qdev = qdev;
  3087. /*
  3088. * We set up each vectors enable/disable/read bits so
  3089. * there's no bit/mask calculations in the critical path.
  3090. */
  3091. intr_context->intr_en_mask =
  3092. INTR_EN_TYPE_MASK | INTR_EN_INTR_MASK | INTR_EN_TYPE_ENABLE;
  3093. intr_context->intr_dis_mask =
  3094. INTR_EN_TYPE_MASK | INTR_EN_INTR_MASK |
  3095. INTR_EN_TYPE_DISABLE;
  3096. intr_context->intr_read_mask =
  3097. INTR_EN_TYPE_MASK | INTR_EN_INTR_MASK | INTR_EN_TYPE_READ;
  3098. /*
  3099. * Single interrupt means one handler for all rings.
  3100. */
  3101. intr_context->handler = qlge_isr;
  3102. sprintf(intr_context->name, "%s-single_irq", qdev->ndev->name);
  3103. /* Set up this vector's bit-mask that indicates
  3104. * which queues it services. In this case there is
  3105. * a single vector so it will service all RSS and
  3106. * TX completion rings.
  3107. */
  3108. ql_set_irq_mask(qdev, intr_context);
  3109. }
  3110. /* Tell the TX completion rings which MSIx vector
  3111. * they will be using.
  3112. */
  3113. ql_set_tx_vect(qdev);
  3114. }
  3115. static void ql_free_irq(struct ql_adapter *qdev)
  3116. {
  3117. int i;
  3118. struct intr_context *intr_context = &qdev->intr_context[0];
  3119. for (i = 0; i < qdev->intr_count; i++, intr_context++) {
  3120. if (intr_context->hooked) {
  3121. if (test_bit(QL_MSIX_ENABLED, &qdev->flags)) {
  3122. free_irq(qdev->msi_x_entry[i].vector,
  3123. &qdev->rx_ring[i]);
  3124. } else {
  3125. free_irq(qdev->pdev->irq, &qdev->rx_ring[0]);
  3126. }
  3127. }
  3128. }
  3129. ql_disable_msix(qdev);
  3130. }
  3131. static int ql_request_irq(struct ql_adapter *qdev)
  3132. {
  3133. int i;
  3134. int status = 0;
  3135. struct pci_dev *pdev = qdev->pdev;
  3136. struct intr_context *intr_context = &qdev->intr_context[0];
  3137. ql_resolve_queues_to_irqs(qdev);
  3138. for (i = 0; i < qdev->intr_count; i++, intr_context++) {
  3139. atomic_set(&intr_context->irq_cnt, 0);
  3140. if (test_bit(QL_MSIX_ENABLED, &qdev->flags)) {
  3141. status = request_irq(qdev->msi_x_entry[i].vector,
  3142. intr_context->handler,
  3143. 0,
  3144. intr_context->name,
  3145. &qdev->rx_ring[i]);
  3146. if (status) {
  3147. netif_err(qdev, ifup, qdev->ndev,
  3148. "Failed request for MSIX interrupt %d.\n",
  3149. i);
  3150. goto err_irq;
  3151. }
  3152. } else {
  3153. netif_printk(qdev, ifup, KERN_DEBUG, qdev->ndev,
  3154. "trying msi or legacy interrupts.\n");
  3155. netif_printk(qdev, ifup, KERN_DEBUG, qdev->ndev,
  3156. "%s: irq = %d.\n", __func__, pdev->irq);
  3157. netif_printk(qdev, ifup, KERN_DEBUG, qdev->ndev,
  3158. "%s: context->name = %s.\n", __func__,
  3159. intr_context->name);
  3160. netif_printk(qdev, ifup, KERN_DEBUG, qdev->ndev,
  3161. "%s: dev_id = 0x%p.\n", __func__,
  3162. &qdev->rx_ring[0]);
  3163. status =
  3164. request_irq(pdev->irq, qlge_isr,
  3165. test_bit(QL_MSI_ENABLED,
  3166. &qdev->
  3167. flags) ? 0 : IRQF_SHARED,
  3168. intr_context->name, &qdev->rx_ring[0]);
  3169. if (status)
  3170. goto err_irq;
  3171. netif_err(qdev, ifup, qdev->ndev,
  3172. "Hooked intr %d, queue type %s, with name %s.\n",
  3173. i,
  3174. qdev->rx_ring[0].type == DEFAULT_Q ?
  3175. "DEFAULT_Q" :
  3176. qdev->rx_ring[0].type == TX_Q ? "TX_Q" :
  3177. qdev->rx_ring[0].type == RX_Q ? "RX_Q" : "",
  3178. intr_context->name);
  3179. }
  3180. intr_context->hooked = 1;
  3181. }
  3182. return status;
  3183. err_irq:
  3184. netif_err(qdev, ifup, qdev->ndev, "Failed to get the interrupts!!!/n");
  3185. ql_free_irq(qdev);
  3186. return status;
  3187. }
  3188. static int ql_start_rss(struct ql_adapter *qdev)
  3189. {
  3190. static const u8 init_hash_seed[] = {
  3191. 0x6d, 0x5a, 0x56, 0xda, 0x25, 0x5b, 0x0e, 0xc2,
  3192. 0x41, 0x67, 0x25, 0x3d, 0x43, 0xa3, 0x8f, 0xb0,
  3193. 0xd0, 0xca, 0x2b, 0xcb, 0xae, 0x7b, 0x30, 0xb4,
  3194. 0x77, 0xcb, 0x2d, 0xa3, 0x80, 0x30, 0xf2, 0x0c,
  3195. 0x6a, 0x42, 0xb7, 0x3b, 0xbe, 0xac, 0x01, 0xfa
  3196. };
  3197. struct ricb *ricb = &qdev->ricb;
  3198. int status = 0;
  3199. int i;
  3200. u8 *hash_id = (u8 *) ricb->hash_cq_id;
  3201. memset((void *)ricb, 0, sizeof(*ricb));
  3202. ricb->base_cq = RSS_L4K;
  3203. ricb->flags =
  3204. (RSS_L6K | RSS_LI | RSS_LB | RSS_LM | RSS_RT4 | RSS_RT6);
  3205. ricb->mask = cpu_to_le16((u16)(0x3ff));
  3206. /*
  3207. * Fill out the Indirection Table.
  3208. */
  3209. for (i = 0; i < 1024; i++)
  3210. hash_id[i] = (i & (qdev->rss_ring_count - 1));
  3211. memcpy((void *)&ricb->ipv6_hash_key[0], init_hash_seed, 40);
  3212. memcpy((void *)&ricb->ipv4_hash_key[0], init_hash_seed, 16);
  3213. status = ql_write_cfg(qdev, ricb, sizeof(*ricb), CFG_LR, 0);
  3214. if (status) {
  3215. netif_err(qdev, ifup, qdev->ndev, "Failed to load RICB.\n");
  3216. return status;
  3217. }
  3218. return status;
  3219. }
  3220. static int ql_clear_routing_entries(struct ql_adapter *qdev)
  3221. {
  3222. int i, status = 0;
  3223. status = ql_sem_spinlock(qdev, SEM_RT_IDX_MASK);
  3224. if (status)
  3225. return status;
  3226. /* Clear all the entries in the routing table. */
  3227. for (i = 0; i < 16; i++) {
  3228. status = ql_set_routing_reg(qdev, i, 0, 0);
  3229. if (status) {
  3230. netif_err(qdev, ifup, qdev->ndev,
  3231. "Failed to init routing register for CAM packets.\n");
  3232. break;
  3233. }
  3234. }
  3235. ql_sem_unlock(qdev, SEM_RT_IDX_MASK);
  3236. return status;
  3237. }
  3238. /* Initialize the frame-to-queue routing. */
  3239. static int ql_route_initialize(struct ql_adapter *qdev)
  3240. {
  3241. int status = 0;
  3242. /* Clear all the entries in the routing table. */
  3243. status = ql_clear_routing_entries(qdev);
  3244. if (status)
  3245. return status;
  3246. status = ql_sem_spinlock(qdev, SEM_RT_IDX_MASK);
  3247. if (status)
  3248. return status;
  3249. status = ql_set_routing_reg(qdev, RT_IDX_IP_CSUM_ERR_SLOT,
  3250. RT_IDX_IP_CSUM_ERR, 1);
  3251. if (status) {
  3252. netif_err(qdev, ifup, qdev->ndev,
  3253. "Failed to init routing register "
  3254. "for IP CSUM error packets.\n");
  3255. goto exit;
  3256. }
  3257. status = ql_set_routing_reg(qdev, RT_IDX_TCP_UDP_CSUM_ERR_SLOT,
  3258. RT_IDX_TU_CSUM_ERR, 1);
  3259. if (status) {
  3260. netif_err(qdev, ifup, qdev->ndev,
  3261. "Failed to init routing register "
  3262. "for TCP/UDP CSUM error packets.\n");
  3263. goto exit;
  3264. }
  3265. status = ql_set_routing_reg(qdev, RT_IDX_BCAST_SLOT, RT_IDX_BCAST, 1);
  3266. if (status) {
  3267. netif_err(qdev, ifup, qdev->ndev,
  3268. "Failed to init routing register for broadcast packets.\n");
  3269. goto exit;
  3270. }
  3271. /* If we have more than one inbound queue, then turn on RSS in the
  3272. * routing block.
  3273. */
  3274. if (qdev->rss_ring_count > 1) {
  3275. status = ql_set_routing_reg(qdev, RT_IDX_RSS_MATCH_SLOT,
  3276. RT_IDX_RSS_MATCH, 1);
  3277. if (status) {
  3278. netif_err(qdev, ifup, qdev->ndev,
  3279. "Failed to init routing register for MATCH RSS packets.\n");
  3280. goto exit;
  3281. }
  3282. }
  3283. status = ql_set_routing_reg(qdev, RT_IDX_CAM_HIT_SLOT,
  3284. RT_IDX_CAM_HIT, 1);
  3285. if (status)
  3286. netif_err(qdev, ifup, qdev->ndev,
  3287. "Failed to init routing register for CAM packets.\n");
  3288. exit:
  3289. ql_sem_unlock(qdev, SEM_RT_IDX_MASK);
  3290. return status;
  3291. }
  3292. int ql_cam_route_initialize(struct ql_adapter *qdev)
  3293. {
  3294. int status, set;
  3295. /* If check if the link is up and use to
  3296. * determine if we are setting or clearing
  3297. * the MAC address in the CAM.
  3298. */
  3299. set = ql_read32(qdev, STS);
  3300. set &= qdev->port_link_up;
  3301. status = ql_set_mac_addr(qdev, set);
  3302. if (status) {
  3303. netif_err(qdev, ifup, qdev->ndev, "Failed to init mac address.\n");
  3304. return status;
  3305. }
  3306. status = ql_route_initialize(qdev);
  3307. if (status)
  3308. netif_err(qdev, ifup, qdev->ndev, "Failed to init routing table.\n");
  3309. return status;
  3310. }
  3311. static int ql_adapter_initialize(struct ql_adapter *qdev)
  3312. {
  3313. u32 value, mask;
  3314. int i;
  3315. int status = 0;
  3316. /*
  3317. * Set up the System register to halt on errors.
  3318. */
  3319. value = SYS_EFE | SYS_FAE;
  3320. mask = value << 16;
  3321. ql_write32(qdev, SYS, mask | value);
  3322. /* Set the default queue, and VLAN behavior. */
  3323. value = NIC_RCV_CFG_DFQ | NIC_RCV_CFG_RV;
  3324. mask = NIC_RCV_CFG_DFQ_MASK | (NIC_RCV_CFG_RV << 16);
  3325. ql_write32(qdev, NIC_RCV_CFG, (mask | value));
  3326. /* Set the MPI interrupt to enabled. */
  3327. ql_write32(qdev, INTR_MASK, (INTR_MASK_PI << 16) | INTR_MASK_PI);
  3328. /* Enable the function, set pagesize, enable error checking. */
  3329. value = FSC_FE | FSC_EPC_INBOUND | FSC_EPC_OUTBOUND |
  3330. FSC_EC | FSC_VM_PAGE_4K;
  3331. value |= SPLT_SETTING;
  3332. /* Set/clear header splitting. */
  3333. mask = FSC_VM_PAGESIZE_MASK |
  3334. FSC_DBL_MASK | FSC_DBRST_MASK | (value << 16);
  3335. ql_write32(qdev, FSC, mask | value);
  3336. ql_write32(qdev, SPLT_HDR, SPLT_LEN);
  3337. /* Set RX packet routing to use port/pci function on which the
  3338. * packet arrived on in addition to usual frame routing.
  3339. * This is helpful on bonding where both interfaces can have
  3340. * the same MAC address.
  3341. */
  3342. ql_write32(qdev, RST_FO, RST_FO_RR_MASK | RST_FO_RR_RCV_FUNC_CQ);
  3343. /* Reroute all packets to our Interface.
  3344. * They may have been routed to MPI firmware
  3345. * due to WOL.
  3346. */
  3347. value = ql_read32(qdev, MGMT_RCV_CFG);
  3348. value &= ~MGMT_RCV_CFG_RM;
  3349. mask = 0xffff0000;
  3350. /* Sticky reg needs clearing due to WOL. */
  3351. ql_write32(qdev, MGMT_RCV_CFG, mask);
  3352. ql_write32(qdev, MGMT_RCV_CFG, mask | value);
  3353. /* Default WOL is enable on Mezz cards */
  3354. if (qdev->pdev->subsystem_device == 0x0068 ||
  3355. qdev->pdev->subsystem_device == 0x0180)
  3356. qdev->wol = WAKE_MAGIC;
  3357. /* Start up the rx queues. */
  3358. for (i = 0; i < qdev->rx_ring_count; i++) {
  3359. status = ql_start_rx_ring(qdev, &qdev->rx_ring[i]);
  3360. if (status) {
  3361. netif_err(qdev, ifup, qdev->ndev,
  3362. "Failed to start rx ring[%d].\n", i);
  3363. return status;
  3364. }
  3365. }
  3366. /* If there is more than one inbound completion queue
  3367. * then download a RICB to configure RSS.
  3368. */
  3369. if (qdev->rss_ring_count > 1) {
  3370. status = ql_start_rss(qdev);
  3371. if (status) {
  3372. netif_err(qdev, ifup, qdev->ndev, "Failed to start RSS.\n");
  3373. return status;
  3374. }
  3375. }
  3376. /* Start up the tx queues. */
  3377. for (i = 0; i < qdev->tx_ring_count; i++) {
  3378. status = ql_start_tx_ring(qdev, &qdev->tx_ring[i]);
  3379. if (status) {
  3380. netif_err(qdev, ifup, qdev->ndev,
  3381. "Failed to start tx ring[%d].\n", i);
  3382. return status;
  3383. }
  3384. }
  3385. /* Initialize the port and set the max framesize. */
  3386. status = qdev->nic_ops->port_initialize(qdev);
  3387. if (status)
  3388. netif_err(qdev, ifup, qdev->ndev, "Failed to start port.\n");
  3389. /* Set up the MAC address and frame routing filter. */
  3390. status = ql_cam_route_initialize(qdev);
  3391. if (status) {
  3392. netif_err(qdev, ifup, qdev->ndev,
  3393. "Failed to init CAM/Routing tables.\n");
  3394. return status;
  3395. }
  3396. /* Start NAPI for the RSS queues. */
  3397. for (i = 0; i < qdev->rss_ring_count; i++)
  3398. napi_enable(&qdev->rx_ring[i].napi);
  3399. return status;
  3400. }
  3401. /* Issue soft reset to chip. */
  3402. static int ql_adapter_reset(struct ql_adapter *qdev)
  3403. {
  3404. u32 value;
  3405. int status = 0;
  3406. unsigned long end_jiffies;
  3407. /* Clear all the entries in the routing table. */
  3408. status = ql_clear_routing_entries(qdev);
  3409. if (status) {
  3410. netif_err(qdev, ifup, qdev->ndev, "Failed to clear routing bits.\n");
  3411. return status;
  3412. }
  3413. end_jiffies = jiffies +
  3414. max((unsigned long)1, usecs_to_jiffies(30));
  3415. /* Check if bit is set then skip the mailbox command and
  3416. * clear the bit, else we are in normal reset process.
  3417. */
  3418. if (!test_bit(QL_ASIC_RECOVERY, &qdev->flags)) {
  3419. /* Stop management traffic. */
  3420. ql_mb_set_mgmnt_traffic_ctl(qdev, MB_SET_MPI_TFK_STOP);
  3421. /* Wait for the NIC and MGMNT FIFOs to empty. */
  3422. ql_wait_fifo_empty(qdev);
  3423. } else
  3424. clear_bit(QL_ASIC_RECOVERY, &qdev->flags);
  3425. ql_write32(qdev, RST_FO, (RST_FO_FR << 16) | RST_FO_FR);
  3426. do {
  3427. value = ql_read32(qdev, RST_FO);
  3428. if ((value & RST_FO_FR) == 0)
  3429. break;
  3430. cpu_relax();
  3431. } while (time_before(jiffies, end_jiffies));
  3432. if (value & RST_FO_FR) {
  3433. netif_err(qdev, ifdown, qdev->ndev,
  3434. "ETIMEDOUT!!! errored out of resetting the chip!\n");
  3435. status = -ETIMEDOUT;
  3436. }
  3437. /* Resume management traffic. */
  3438. ql_mb_set_mgmnt_traffic_ctl(qdev, MB_SET_MPI_TFK_RESUME);
  3439. return status;
  3440. }
  3441. static void ql_display_dev_info(struct net_device *ndev)
  3442. {
  3443. struct ql_adapter *qdev = netdev_priv(ndev);
  3444. netif_info(qdev, probe, qdev->ndev,
  3445. "Function #%d, Port %d, NIC Roll %d, NIC Rev = %d, "
  3446. "XG Roll = %d, XG Rev = %d.\n",
  3447. qdev->func,
  3448. qdev->port,
  3449. qdev->chip_rev_id & 0x0000000f,
  3450. qdev->chip_rev_id >> 4 & 0x0000000f,
  3451. qdev->chip_rev_id >> 8 & 0x0000000f,
  3452. qdev->chip_rev_id >> 12 & 0x0000000f);
  3453. netif_info(qdev, probe, qdev->ndev,
  3454. "MAC address %pM\n", ndev->dev_addr);
  3455. }
  3456. static int ql_wol(struct ql_adapter *qdev)
  3457. {
  3458. int status = 0;
  3459. u32 wol = MB_WOL_DISABLE;
  3460. /* The CAM is still intact after a reset, but if we
  3461. * are doing WOL, then we may need to program the
  3462. * routing regs. We would also need to issue the mailbox
  3463. * commands to instruct the MPI what to do per the ethtool
  3464. * settings.
  3465. */
  3466. if (qdev->wol & (WAKE_ARP | WAKE_MAGICSECURE | WAKE_PHY | WAKE_UCAST |
  3467. WAKE_MCAST | WAKE_BCAST)) {
  3468. netif_err(qdev, ifdown, qdev->ndev,
  3469. "Unsupported WOL parameter. qdev->wol = 0x%x.\n",
  3470. qdev->wol);
  3471. return -EINVAL;
  3472. }
  3473. if (qdev->wol & WAKE_MAGIC) {
  3474. status = ql_mb_wol_set_magic(qdev, 1);
  3475. if (status) {
  3476. netif_err(qdev, ifdown, qdev->ndev,
  3477. "Failed to set magic packet on %s.\n",
  3478. qdev->ndev->name);
  3479. return status;
  3480. } else
  3481. netif_info(qdev, drv, qdev->ndev,
  3482. "Enabled magic packet successfully on %s.\n",
  3483. qdev->ndev->name);
  3484. wol |= MB_WOL_MAGIC_PKT;
  3485. }
  3486. if (qdev->wol) {
  3487. wol |= MB_WOL_MODE_ON;
  3488. status = ql_mb_wol_mode(qdev, wol);
  3489. netif_err(qdev, drv, qdev->ndev,
  3490. "WOL %s (wol code 0x%x) on %s\n",
  3491. (status == 0) ? "Successfully set" : "Failed",
  3492. wol, qdev->ndev->name);
  3493. }
  3494. return status;
  3495. }
  3496. static void ql_cancel_all_work_sync(struct ql_adapter *qdev)
  3497. {
  3498. /* Don't kill the reset worker thread if we
  3499. * are in the process of recovery.
  3500. */
  3501. if (test_bit(QL_ADAPTER_UP, &qdev->flags))
  3502. cancel_delayed_work_sync(&qdev->asic_reset_work);
  3503. cancel_delayed_work_sync(&qdev->mpi_reset_work);
  3504. cancel_delayed_work_sync(&qdev->mpi_work);
  3505. cancel_delayed_work_sync(&qdev->mpi_idc_work);
  3506. cancel_delayed_work_sync(&qdev->mpi_core_to_log);
  3507. cancel_delayed_work_sync(&qdev->mpi_port_cfg_work);
  3508. }
  3509. static int ql_adapter_down(struct ql_adapter *qdev)
  3510. {
  3511. int i, status = 0;
  3512. ql_link_off(qdev);
  3513. ql_cancel_all_work_sync(qdev);
  3514. for (i = 0; i < qdev->rss_ring_count; i++)
  3515. napi_disable(&qdev->rx_ring[i].napi);
  3516. clear_bit(QL_ADAPTER_UP, &qdev->flags);
  3517. ql_disable_interrupts(qdev);
  3518. ql_tx_ring_clean(qdev);
  3519. /* Call netif_napi_del() from common point.
  3520. */
  3521. for (i = 0; i < qdev->rss_ring_count; i++)
  3522. netif_napi_del(&qdev->rx_ring[i].napi);
  3523. status = ql_adapter_reset(qdev);
  3524. if (status)
  3525. netif_err(qdev, ifdown, qdev->ndev, "reset(func #%d) FAILED!\n",
  3526. qdev->func);
  3527. ql_free_rx_buffers(qdev);
  3528. return status;
  3529. }
  3530. static int ql_adapter_up(struct ql_adapter *qdev)
  3531. {
  3532. int err = 0;
  3533. err = ql_adapter_initialize(qdev);
  3534. if (err) {
  3535. netif_info(qdev, ifup, qdev->ndev, "Unable to initialize adapter.\n");
  3536. goto err_init;
  3537. }
  3538. set_bit(QL_ADAPTER_UP, &qdev->flags);
  3539. ql_alloc_rx_buffers(qdev);
  3540. /* If the port is initialized and the
  3541. * link is up the turn on the carrier.
  3542. */
  3543. if ((ql_read32(qdev, STS) & qdev->port_init) &&
  3544. (ql_read32(qdev, STS) & qdev->port_link_up))
  3545. ql_link_on(qdev);
  3546. /* Restore rx mode. */
  3547. clear_bit(QL_ALLMULTI, &qdev->flags);
  3548. clear_bit(QL_PROMISCUOUS, &qdev->flags);
  3549. qlge_set_multicast_list(qdev->ndev);
  3550. /* Restore vlan setting. */
  3551. qlge_restore_vlan(qdev);
  3552. ql_enable_interrupts(qdev);
  3553. ql_enable_all_completion_interrupts(qdev);
  3554. netif_tx_start_all_queues(qdev->ndev);
  3555. return 0;
  3556. err_init:
  3557. ql_adapter_reset(qdev);
  3558. return err;
  3559. }
  3560. static void ql_release_adapter_resources(struct ql_adapter *qdev)
  3561. {
  3562. ql_free_mem_resources(qdev);
  3563. ql_free_irq(qdev);
  3564. }
  3565. static int ql_get_adapter_resources(struct ql_adapter *qdev)
  3566. {
  3567. int status = 0;
  3568. if (ql_alloc_mem_resources(qdev)) {
  3569. netif_err(qdev, ifup, qdev->ndev, "Unable to allocate memory.\n");
  3570. return -ENOMEM;
  3571. }
  3572. status = ql_request_irq(qdev);
  3573. return status;
  3574. }
  3575. static int qlge_close(struct net_device *ndev)
  3576. {
  3577. struct ql_adapter *qdev = netdev_priv(ndev);
  3578. /* If we hit pci_channel_io_perm_failure
  3579. * failure condition, then we already
  3580. * brought the adapter down.
  3581. */
  3582. if (test_bit(QL_EEH_FATAL, &qdev->flags)) {
  3583. netif_err(qdev, drv, qdev->ndev, "EEH fatal did unload.\n");
  3584. clear_bit(QL_EEH_FATAL, &qdev->flags);
  3585. return 0;
  3586. }
  3587. /*
  3588. * Wait for device to recover from a reset.
  3589. * (Rarely happens, but possible.)
  3590. */
  3591. while (!test_bit(QL_ADAPTER_UP, &qdev->flags))
  3592. msleep(1);
  3593. ql_adapter_down(qdev);
  3594. ql_release_adapter_resources(qdev);
  3595. return 0;
  3596. }
  3597. static int ql_configure_rings(struct ql_adapter *qdev)
  3598. {
  3599. int i;
  3600. struct rx_ring *rx_ring;
  3601. struct tx_ring *tx_ring;
  3602. int cpu_cnt = min(MAX_CPUS, (int)num_online_cpus());
  3603. unsigned int lbq_buf_len = (qdev->ndev->mtu > 1500) ?
  3604. LARGE_BUFFER_MAX_SIZE : LARGE_BUFFER_MIN_SIZE;
  3605. qdev->lbq_buf_order = get_order(lbq_buf_len);
  3606. /* In a perfect world we have one RSS ring for each CPU
  3607. * and each has it's own vector. To do that we ask for
  3608. * cpu_cnt vectors. ql_enable_msix() will adjust the
  3609. * vector count to what we actually get. We then
  3610. * allocate an RSS ring for each.
  3611. * Essentially, we are doing min(cpu_count, msix_vector_count).
  3612. */
  3613. qdev->intr_count = cpu_cnt;
  3614. ql_enable_msix(qdev);
  3615. /* Adjust the RSS ring count to the actual vector count. */
  3616. qdev->rss_ring_count = qdev->intr_count;
  3617. qdev->tx_ring_count = cpu_cnt;
  3618. qdev->rx_ring_count = qdev->tx_ring_count + qdev->rss_ring_count;
  3619. for (i = 0; i < qdev->tx_ring_count; i++) {
  3620. tx_ring = &qdev->tx_ring[i];
  3621. memset((void *)tx_ring, 0, sizeof(*tx_ring));
  3622. tx_ring->qdev = qdev;
  3623. tx_ring->wq_id = i;
  3624. tx_ring->wq_len = qdev->tx_ring_size;
  3625. tx_ring->wq_size =
  3626. tx_ring->wq_len * sizeof(struct ob_mac_iocb_req);
  3627. /*
  3628. * The completion queue ID for the tx rings start
  3629. * immediately after the rss rings.
  3630. */
  3631. tx_ring->cq_id = qdev->rss_ring_count + i;
  3632. }
  3633. for (i = 0; i < qdev->rx_ring_count; i++) {
  3634. rx_ring = &qdev->rx_ring[i];
  3635. memset((void *)rx_ring, 0, sizeof(*rx_ring));
  3636. rx_ring->qdev = qdev;
  3637. rx_ring->cq_id = i;
  3638. rx_ring->cpu = i % cpu_cnt; /* CPU to run handler on. */
  3639. if (i < qdev->rss_ring_count) {
  3640. /*
  3641. * Inbound (RSS) queues.
  3642. */
  3643. rx_ring->cq_len = qdev->rx_ring_size;
  3644. rx_ring->cq_size =
  3645. rx_ring->cq_len * sizeof(struct ql_net_rsp_iocb);
  3646. rx_ring->lbq_len = NUM_LARGE_BUFFERS;
  3647. rx_ring->lbq_size =
  3648. rx_ring->lbq_len * sizeof(__le64);
  3649. rx_ring->lbq_buf_size = (u16)lbq_buf_len;
  3650. rx_ring->sbq_len = NUM_SMALL_BUFFERS;
  3651. rx_ring->sbq_size =
  3652. rx_ring->sbq_len * sizeof(__le64);
  3653. rx_ring->sbq_buf_size = SMALL_BUF_MAP_SIZE;
  3654. rx_ring->type = RX_Q;
  3655. } else {
  3656. /*
  3657. * Outbound queue handles outbound completions only.
  3658. */
  3659. /* outbound cq is same size as tx_ring it services. */
  3660. rx_ring->cq_len = qdev->tx_ring_size;
  3661. rx_ring->cq_size =
  3662. rx_ring->cq_len * sizeof(struct ql_net_rsp_iocb);
  3663. rx_ring->lbq_len = 0;
  3664. rx_ring->lbq_size = 0;
  3665. rx_ring->lbq_buf_size = 0;
  3666. rx_ring->sbq_len = 0;
  3667. rx_ring->sbq_size = 0;
  3668. rx_ring->sbq_buf_size = 0;
  3669. rx_ring->type = TX_Q;
  3670. }
  3671. }
  3672. return 0;
  3673. }
  3674. static int qlge_open(struct net_device *ndev)
  3675. {
  3676. int err = 0;
  3677. struct ql_adapter *qdev = netdev_priv(ndev);
  3678. err = ql_adapter_reset(qdev);
  3679. if (err)
  3680. return err;
  3681. err = ql_configure_rings(qdev);
  3682. if (err)
  3683. return err;
  3684. err = ql_get_adapter_resources(qdev);
  3685. if (err)
  3686. goto error_up;
  3687. err = ql_adapter_up(qdev);
  3688. if (err)
  3689. goto error_up;
  3690. return err;
  3691. error_up:
  3692. ql_release_adapter_resources(qdev);
  3693. return err;
  3694. }
  3695. static int ql_change_rx_buffers(struct ql_adapter *qdev)
  3696. {
  3697. struct rx_ring *rx_ring;
  3698. int i, status;
  3699. u32 lbq_buf_len;
  3700. /* Wait for an outstanding reset to complete. */
  3701. if (!test_bit(QL_ADAPTER_UP, &qdev->flags)) {
  3702. int i = 3;
  3703. while (i-- && !test_bit(QL_ADAPTER_UP, &qdev->flags)) {
  3704. netif_err(qdev, ifup, qdev->ndev,
  3705. "Waiting for adapter UP...\n");
  3706. ssleep(1);
  3707. }
  3708. if (!i) {
  3709. netif_err(qdev, ifup, qdev->ndev,
  3710. "Timed out waiting for adapter UP\n");
  3711. return -ETIMEDOUT;
  3712. }
  3713. }
  3714. status = ql_adapter_down(qdev);
  3715. if (status)
  3716. goto error;
  3717. /* Get the new rx buffer size. */
  3718. lbq_buf_len = (qdev->ndev->mtu > 1500) ?
  3719. LARGE_BUFFER_MAX_SIZE : LARGE_BUFFER_MIN_SIZE;
  3720. qdev->lbq_buf_order = get_order(lbq_buf_len);
  3721. for (i = 0; i < qdev->rss_ring_count; i++) {
  3722. rx_ring = &qdev->rx_ring[i];
  3723. /* Set the new size. */
  3724. rx_ring->lbq_buf_size = lbq_buf_len;
  3725. }
  3726. status = ql_adapter_up(qdev);
  3727. if (status)
  3728. goto error;
  3729. return status;
  3730. error:
  3731. netif_alert(qdev, ifup, qdev->ndev,
  3732. "Driver up/down cycle failed, closing device.\n");
  3733. set_bit(QL_ADAPTER_UP, &qdev->flags);
  3734. dev_close(qdev->ndev);
  3735. return status;
  3736. }
  3737. static int qlge_change_mtu(struct net_device *ndev, int new_mtu)
  3738. {
  3739. struct ql_adapter *qdev = netdev_priv(ndev);
  3740. int status;
  3741. if (ndev->mtu == 1500 && new_mtu == 9000) {
  3742. netif_err(qdev, ifup, qdev->ndev, "Changing to jumbo MTU.\n");
  3743. } else if (ndev->mtu == 9000 && new_mtu == 1500) {
  3744. netif_err(qdev, ifup, qdev->ndev, "Changing to normal MTU.\n");
  3745. } else
  3746. return -EINVAL;
  3747. queue_delayed_work(qdev->workqueue,
  3748. &qdev->mpi_port_cfg_work, 3*HZ);
  3749. ndev->mtu = new_mtu;
  3750. if (!netif_running(qdev->ndev)) {
  3751. return 0;
  3752. }
  3753. status = ql_change_rx_buffers(qdev);
  3754. if (status) {
  3755. netif_err(qdev, ifup, qdev->ndev,
  3756. "Changing MTU failed.\n");
  3757. }
  3758. return status;
  3759. }
  3760. static struct net_device_stats *qlge_get_stats(struct net_device
  3761. *ndev)
  3762. {
  3763. struct ql_adapter *qdev = netdev_priv(ndev);
  3764. struct rx_ring *rx_ring = &qdev->rx_ring[0];
  3765. struct tx_ring *tx_ring = &qdev->tx_ring[0];
  3766. unsigned long pkts, mcast, dropped, errors, bytes;
  3767. int i;
  3768. /* Get RX stats. */
  3769. pkts = mcast = dropped = errors = bytes = 0;
  3770. for (i = 0; i < qdev->rss_ring_count; i++, rx_ring++) {
  3771. pkts += rx_ring->rx_packets;
  3772. bytes += rx_ring->rx_bytes;
  3773. dropped += rx_ring->rx_dropped;
  3774. errors += rx_ring->rx_errors;
  3775. mcast += rx_ring->rx_multicast;
  3776. }
  3777. ndev->stats.rx_packets = pkts;
  3778. ndev->stats.rx_bytes = bytes;
  3779. ndev->stats.rx_dropped = dropped;
  3780. ndev->stats.rx_errors = errors;
  3781. ndev->stats.multicast = mcast;
  3782. /* Get TX stats. */
  3783. pkts = errors = bytes = 0;
  3784. for (i = 0; i < qdev->tx_ring_count; i++, tx_ring++) {
  3785. pkts += tx_ring->tx_packets;
  3786. bytes += tx_ring->tx_bytes;
  3787. errors += tx_ring->tx_errors;
  3788. }
  3789. ndev->stats.tx_packets = pkts;
  3790. ndev->stats.tx_bytes = bytes;
  3791. ndev->stats.tx_errors = errors;
  3792. return &ndev->stats;
  3793. }
  3794. static void qlge_set_multicast_list(struct net_device *ndev)
  3795. {
  3796. struct ql_adapter *qdev = netdev_priv(ndev);
  3797. struct netdev_hw_addr *ha;
  3798. int i, status;
  3799. status = ql_sem_spinlock(qdev, SEM_RT_IDX_MASK);
  3800. if (status)
  3801. return;
  3802. /*
  3803. * Set or clear promiscuous mode if a
  3804. * transition is taking place.
  3805. */
  3806. if (ndev->flags & IFF_PROMISC) {
  3807. if (!test_bit(QL_PROMISCUOUS, &qdev->flags)) {
  3808. if (ql_set_routing_reg
  3809. (qdev, RT_IDX_PROMISCUOUS_SLOT, RT_IDX_VALID, 1)) {
  3810. netif_err(qdev, hw, qdev->ndev,
  3811. "Failed to set promiscuous mode.\n");
  3812. } else {
  3813. set_bit(QL_PROMISCUOUS, &qdev->flags);
  3814. }
  3815. }
  3816. } else {
  3817. if (test_bit(QL_PROMISCUOUS, &qdev->flags)) {
  3818. if (ql_set_routing_reg
  3819. (qdev, RT_IDX_PROMISCUOUS_SLOT, RT_IDX_VALID, 0)) {
  3820. netif_err(qdev, hw, qdev->ndev,
  3821. "Failed to clear promiscuous mode.\n");
  3822. } else {
  3823. clear_bit(QL_PROMISCUOUS, &qdev->flags);
  3824. }
  3825. }
  3826. }
  3827. /*
  3828. * Set or clear all multicast mode if a
  3829. * transition is taking place.
  3830. */
  3831. if ((ndev->flags & IFF_ALLMULTI) ||
  3832. (netdev_mc_count(ndev) > MAX_MULTICAST_ENTRIES)) {
  3833. if (!test_bit(QL_ALLMULTI, &qdev->flags)) {
  3834. if (ql_set_routing_reg
  3835. (qdev, RT_IDX_ALLMULTI_SLOT, RT_IDX_MCAST, 1)) {
  3836. netif_err(qdev, hw, qdev->ndev,
  3837. "Failed to set all-multi mode.\n");
  3838. } else {
  3839. set_bit(QL_ALLMULTI, &qdev->flags);
  3840. }
  3841. }
  3842. } else {
  3843. if (test_bit(QL_ALLMULTI, &qdev->flags)) {
  3844. if (ql_set_routing_reg
  3845. (qdev, RT_IDX_ALLMULTI_SLOT, RT_IDX_MCAST, 0)) {
  3846. netif_err(qdev, hw, qdev->ndev,
  3847. "Failed to clear all-multi mode.\n");
  3848. } else {
  3849. clear_bit(QL_ALLMULTI, &qdev->flags);
  3850. }
  3851. }
  3852. }
  3853. if (!netdev_mc_empty(ndev)) {
  3854. status = ql_sem_spinlock(qdev, SEM_MAC_ADDR_MASK);
  3855. if (status)
  3856. goto exit;
  3857. i = 0;
  3858. netdev_for_each_mc_addr(ha, ndev) {
  3859. if (ql_set_mac_addr_reg(qdev, (u8 *) ha->addr,
  3860. MAC_ADDR_TYPE_MULTI_MAC, i)) {
  3861. netif_err(qdev, hw, qdev->ndev,
  3862. "Failed to loadmulticast address.\n");
  3863. ql_sem_unlock(qdev, SEM_MAC_ADDR_MASK);
  3864. goto exit;
  3865. }
  3866. i++;
  3867. }
  3868. ql_sem_unlock(qdev, SEM_MAC_ADDR_MASK);
  3869. if (ql_set_routing_reg
  3870. (qdev, RT_IDX_MCAST_MATCH_SLOT, RT_IDX_MCAST_MATCH, 1)) {
  3871. netif_err(qdev, hw, qdev->ndev,
  3872. "Failed to set multicast match mode.\n");
  3873. } else {
  3874. set_bit(QL_ALLMULTI, &qdev->flags);
  3875. }
  3876. }
  3877. exit:
  3878. ql_sem_unlock(qdev, SEM_RT_IDX_MASK);
  3879. }
  3880. static int qlge_set_mac_address(struct net_device *ndev, void *p)
  3881. {
  3882. struct ql_adapter *qdev = netdev_priv(ndev);
  3883. struct sockaddr *addr = p;
  3884. int status;
  3885. if (!is_valid_ether_addr(addr->sa_data))
  3886. return -EADDRNOTAVAIL;
  3887. memcpy(ndev->dev_addr, addr->sa_data, ndev->addr_len);
  3888. /* Update local copy of current mac address. */
  3889. memcpy(qdev->current_mac_addr, ndev->dev_addr, ndev->addr_len);
  3890. status = ql_sem_spinlock(qdev, SEM_MAC_ADDR_MASK);
  3891. if (status)
  3892. return status;
  3893. status = ql_set_mac_addr_reg(qdev, (u8 *) ndev->dev_addr,
  3894. MAC_ADDR_TYPE_CAM_MAC, qdev->func * MAX_CQ);
  3895. if (status)
  3896. netif_err(qdev, hw, qdev->ndev, "Failed to load MAC address.\n");
  3897. ql_sem_unlock(qdev, SEM_MAC_ADDR_MASK);
  3898. return status;
  3899. }
  3900. static void qlge_tx_timeout(struct net_device *ndev)
  3901. {
  3902. struct ql_adapter *qdev = netdev_priv(ndev);
  3903. ql_queue_asic_error(qdev);
  3904. }
  3905. static void ql_asic_reset_work(struct work_struct *work)
  3906. {
  3907. struct ql_adapter *qdev =
  3908. container_of(work, struct ql_adapter, asic_reset_work.work);
  3909. int status;
  3910. rtnl_lock();
  3911. status = ql_adapter_down(qdev);
  3912. if (status)
  3913. goto error;
  3914. status = ql_adapter_up(qdev);
  3915. if (status)
  3916. goto error;
  3917. /* Restore rx mode. */
  3918. clear_bit(QL_ALLMULTI, &qdev->flags);
  3919. clear_bit(QL_PROMISCUOUS, &qdev->flags);
  3920. qlge_set_multicast_list(qdev->ndev);
  3921. rtnl_unlock();
  3922. return;
  3923. error:
  3924. netif_alert(qdev, ifup, qdev->ndev,
  3925. "Driver up/down cycle failed, closing device\n");
  3926. set_bit(QL_ADAPTER_UP, &qdev->flags);
  3927. dev_close(qdev->ndev);
  3928. rtnl_unlock();
  3929. }
  3930. static const struct nic_operations qla8012_nic_ops = {
  3931. .get_flash = ql_get_8012_flash_params,
  3932. .port_initialize = ql_8012_port_initialize,
  3933. };
  3934. static const struct nic_operations qla8000_nic_ops = {
  3935. .get_flash = ql_get_8000_flash_params,
  3936. .port_initialize = ql_8000_port_initialize,
  3937. };
  3938. /* Find the pcie function number for the other NIC
  3939. * on this chip. Since both NIC functions share a
  3940. * common firmware we have the lowest enabled function
  3941. * do any common work. Examples would be resetting
  3942. * after a fatal firmware error, or doing a firmware
  3943. * coredump.
  3944. */
  3945. static int ql_get_alt_pcie_func(struct ql_adapter *qdev)
  3946. {
  3947. int status = 0;
  3948. u32 temp;
  3949. u32 nic_func1, nic_func2;
  3950. status = ql_read_mpi_reg(qdev, MPI_TEST_FUNC_PORT_CFG,
  3951. &temp);
  3952. if (status)
  3953. return status;
  3954. nic_func1 = ((temp >> MPI_TEST_NIC1_FUNC_SHIFT) &
  3955. MPI_TEST_NIC_FUNC_MASK);
  3956. nic_func2 = ((temp >> MPI_TEST_NIC2_FUNC_SHIFT) &
  3957. MPI_TEST_NIC_FUNC_MASK);
  3958. if (qdev->func == nic_func1)
  3959. qdev->alt_func = nic_func2;
  3960. else if (qdev->func == nic_func2)
  3961. qdev->alt_func = nic_func1;
  3962. else
  3963. status = -EIO;
  3964. return status;
  3965. }
  3966. static int ql_get_board_info(struct ql_adapter *qdev)
  3967. {
  3968. int status;
  3969. qdev->func =
  3970. (ql_read32(qdev, STS) & STS_FUNC_ID_MASK) >> STS_FUNC_ID_SHIFT;
  3971. if (qdev->func > 3)
  3972. return -EIO;
  3973. status = ql_get_alt_pcie_func(qdev);
  3974. if (status)
  3975. return status;
  3976. qdev->port = (qdev->func < qdev->alt_func) ? 0 : 1;
  3977. if (qdev->port) {
  3978. qdev->xg_sem_mask = SEM_XGMAC1_MASK;
  3979. qdev->port_link_up = STS_PL1;
  3980. qdev->port_init = STS_PI1;
  3981. qdev->mailbox_in = PROC_ADDR_MPI_RISC | PROC_ADDR_FUNC2_MBI;
  3982. qdev->mailbox_out = PROC_ADDR_MPI_RISC | PROC_ADDR_FUNC2_MBO;
  3983. } else {
  3984. qdev->xg_sem_mask = SEM_XGMAC0_MASK;
  3985. qdev->port_link_up = STS_PL0;
  3986. qdev->port_init = STS_PI0;
  3987. qdev->mailbox_in = PROC_ADDR_MPI_RISC | PROC_ADDR_FUNC0_MBI;
  3988. qdev->mailbox_out = PROC_ADDR_MPI_RISC | PROC_ADDR_FUNC0_MBO;
  3989. }
  3990. qdev->chip_rev_id = ql_read32(qdev, REV_ID);
  3991. qdev->device_id = qdev->pdev->device;
  3992. if (qdev->device_id == QLGE_DEVICE_ID_8012)
  3993. qdev->nic_ops = &qla8012_nic_ops;
  3994. else if (qdev->device_id == QLGE_DEVICE_ID_8000)
  3995. qdev->nic_ops = &qla8000_nic_ops;
  3996. return status;
  3997. }
  3998. static void ql_release_all(struct pci_dev *pdev)
  3999. {
  4000. struct net_device *ndev = pci_get_drvdata(pdev);
  4001. struct ql_adapter *qdev = netdev_priv(ndev);
  4002. if (qdev->workqueue) {
  4003. destroy_workqueue(qdev->workqueue);
  4004. qdev->workqueue = NULL;
  4005. }
  4006. if (qdev->reg_base)
  4007. iounmap(qdev->reg_base);
  4008. if (qdev->doorbell_area)
  4009. iounmap(qdev->doorbell_area);
  4010. vfree(qdev->mpi_coredump);
  4011. pci_release_regions(pdev);
  4012. pci_set_drvdata(pdev, NULL);
  4013. }
  4014. static int __devinit ql_init_device(struct pci_dev *pdev,
  4015. struct net_device *ndev, int cards_found)
  4016. {
  4017. struct ql_adapter *qdev = netdev_priv(ndev);
  4018. int err = 0;
  4019. memset((void *)qdev, 0, sizeof(*qdev));
  4020. err = pci_enable_device(pdev);
  4021. if (err) {
  4022. dev_err(&pdev->dev, "PCI device enable failed.\n");
  4023. return err;
  4024. }
  4025. qdev->ndev = ndev;
  4026. qdev->pdev = pdev;
  4027. pci_set_drvdata(pdev, ndev);
  4028. /* Set PCIe read request size */
  4029. err = pcie_set_readrq(pdev, 4096);
  4030. if (err) {
  4031. dev_err(&pdev->dev, "Set readrq failed.\n");
  4032. goto err_out1;
  4033. }
  4034. err = pci_request_regions(pdev, DRV_NAME);
  4035. if (err) {
  4036. dev_err(&pdev->dev, "PCI region request failed.\n");
  4037. return err;
  4038. }
  4039. pci_set_master(pdev);
  4040. if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(64))) {
  4041. set_bit(QL_DMA64, &qdev->flags);
  4042. err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
  4043. } else {
  4044. err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  4045. if (!err)
  4046. err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
  4047. }
  4048. if (err) {
  4049. dev_err(&pdev->dev, "No usable DMA configuration.\n");
  4050. goto err_out2;
  4051. }
  4052. /* Set PCIe reset type for EEH to fundamental. */
  4053. pdev->needs_freset = 1;
  4054. pci_save_state(pdev);
  4055. qdev->reg_base =
  4056. ioremap_nocache(pci_resource_start(pdev, 1),
  4057. pci_resource_len(pdev, 1));
  4058. if (!qdev->reg_base) {
  4059. dev_err(&pdev->dev, "Register mapping failed.\n");
  4060. err = -ENOMEM;
  4061. goto err_out2;
  4062. }
  4063. qdev->doorbell_area_size = pci_resource_len(pdev, 3);
  4064. qdev->doorbell_area =
  4065. ioremap_nocache(pci_resource_start(pdev, 3),
  4066. pci_resource_len(pdev, 3));
  4067. if (!qdev->doorbell_area) {
  4068. dev_err(&pdev->dev, "Doorbell register mapping failed.\n");
  4069. err = -ENOMEM;
  4070. goto err_out2;
  4071. }
  4072. err = ql_get_board_info(qdev);
  4073. if (err) {
  4074. dev_err(&pdev->dev, "Register access failed.\n");
  4075. err = -EIO;
  4076. goto err_out2;
  4077. }
  4078. qdev->msg_enable = netif_msg_init(debug, default_msg);
  4079. spin_lock_init(&qdev->hw_lock);
  4080. spin_lock_init(&qdev->stats_lock);
  4081. if (qlge_mpi_coredump) {
  4082. qdev->mpi_coredump =
  4083. vmalloc(sizeof(struct ql_mpi_coredump));
  4084. if (qdev->mpi_coredump == NULL) {
  4085. dev_err(&pdev->dev, "Coredump alloc failed.\n");
  4086. err = -ENOMEM;
  4087. goto err_out2;
  4088. }
  4089. if (qlge_force_coredump)
  4090. set_bit(QL_FRC_COREDUMP, &qdev->flags);
  4091. }
  4092. /* make sure the EEPROM is good */
  4093. err = qdev->nic_ops->get_flash(qdev);
  4094. if (err) {
  4095. dev_err(&pdev->dev, "Invalid FLASH.\n");
  4096. goto err_out2;
  4097. }
  4098. memcpy(ndev->perm_addr, ndev->dev_addr, ndev->addr_len);
  4099. /* Keep local copy of current mac address. */
  4100. memcpy(qdev->current_mac_addr, ndev->dev_addr, ndev->addr_len);
  4101. /* Set up the default ring sizes. */
  4102. qdev->tx_ring_size = NUM_TX_RING_ENTRIES;
  4103. qdev->rx_ring_size = NUM_RX_RING_ENTRIES;
  4104. /* Set up the coalescing parameters. */
  4105. qdev->rx_coalesce_usecs = DFLT_COALESCE_WAIT;
  4106. qdev->tx_coalesce_usecs = DFLT_COALESCE_WAIT;
  4107. qdev->rx_max_coalesced_frames = DFLT_INTER_FRAME_WAIT;
  4108. qdev->tx_max_coalesced_frames = DFLT_INTER_FRAME_WAIT;
  4109. /*
  4110. * Set up the operating parameters.
  4111. */
  4112. qdev->workqueue = create_singlethread_workqueue(ndev->name);
  4113. INIT_DELAYED_WORK(&qdev->asic_reset_work, ql_asic_reset_work);
  4114. INIT_DELAYED_WORK(&qdev->mpi_reset_work, ql_mpi_reset_work);
  4115. INIT_DELAYED_WORK(&qdev->mpi_work, ql_mpi_work);
  4116. INIT_DELAYED_WORK(&qdev->mpi_port_cfg_work, ql_mpi_port_cfg_work);
  4117. INIT_DELAYED_WORK(&qdev->mpi_idc_work, ql_mpi_idc_work);
  4118. INIT_DELAYED_WORK(&qdev->mpi_core_to_log, ql_mpi_core_to_log);
  4119. init_completion(&qdev->ide_completion);
  4120. mutex_init(&qdev->mpi_mutex);
  4121. if (!cards_found) {
  4122. dev_info(&pdev->dev, "%s\n", DRV_STRING);
  4123. dev_info(&pdev->dev, "Driver name: %s, Version: %s.\n",
  4124. DRV_NAME, DRV_VERSION);
  4125. }
  4126. return 0;
  4127. err_out2:
  4128. ql_release_all(pdev);
  4129. err_out1:
  4130. pci_disable_device(pdev);
  4131. return err;
  4132. }
  4133. static const struct net_device_ops qlge_netdev_ops = {
  4134. .ndo_open = qlge_open,
  4135. .ndo_stop = qlge_close,
  4136. .ndo_start_xmit = qlge_send,
  4137. .ndo_change_mtu = qlge_change_mtu,
  4138. .ndo_get_stats = qlge_get_stats,
  4139. .ndo_set_rx_mode = qlge_set_multicast_list,
  4140. .ndo_set_mac_address = qlge_set_mac_address,
  4141. .ndo_validate_addr = eth_validate_addr,
  4142. .ndo_tx_timeout = qlge_tx_timeout,
  4143. .ndo_fix_features = qlge_fix_features,
  4144. .ndo_set_features = qlge_set_features,
  4145. .ndo_vlan_rx_add_vid = qlge_vlan_rx_add_vid,
  4146. .ndo_vlan_rx_kill_vid = qlge_vlan_rx_kill_vid,
  4147. };
  4148. static void ql_timer(unsigned long data)
  4149. {
  4150. struct ql_adapter *qdev = (struct ql_adapter *)data;
  4151. u32 var = 0;
  4152. var = ql_read32(qdev, STS);
  4153. if (pci_channel_offline(qdev->pdev)) {
  4154. netif_err(qdev, ifup, qdev->ndev, "EEH STS = 0x%.08x.\n", var);
  4155. return;
  4156. }
  4157. mod_timer(&qdev->timer, jiffies + (5*HZ));
  4158. }
  4159. static int __devinit qlge_probe(struct pci_dev *pdev,
  4160. const struct pci_device_id *pci_entry)
  4161. {
  4162. struct net_device *ndev = NULL;
  4163. struct ql_adapter *qdev = NULL;
  4164. static int cards_found = 0;
  4165. int err = 0;
  4166. ndev = alloc_etherdev_mq(sizeof(struct ql_adapter),
  4167. min(MAX_CPUS, netif_get_num_default_rss_queues()));
  4168. if (!ndev)
  4169. return -ENOMEM;
  4170. err = ql_init_device(pdev, ndev, cards_found);
  4171. if (err < 0) {
  4172. free_netdev(ndev);
  4173. return err;
  4174. }
  4175. qdev = netdev_priv(ndev);
  4176. SET_NETDEV_DEV(ndev, &pdev->dev);
  4177. ndev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM |
  4178. NETIF_F_TSO | NETIF_F_TSO6 | NETIF_F_TSO_ECN |
  4179. NETIF_F_HW_VLAN_TX | NETIF_F_RXCSUM;
  4180. ndev->features = ndev->hw_features |
  4181. NETIF_F_HW_VLAN_RX | NETIF_F_HW_VLAN_FILTER;
  4182. if (test_bit(QL_DMA64, &qdev->flags))
  4183. ndev->features |= NETIF_F_HIGHDMA;
  4184. /*
  4185. * Set up net_device structure.
  4186. */
  4187. ndev->tx_queue_len = qdev->tx_ring_size;
  4188. ndev->irq = pdev->irq;
  4189. ndev->netdev_ops = &qlge_netdev_ops;
  4190. SET_ETHTOOL_OPS(ndev, &qlge_ethtool_ops);
  4191. ndev->watchdog_timeo = 10 * HZ;
  4192. err = register_netdev(ndev);
  4193. if (err) {
  4194. dev_err(&pdev->dev, "net device registration failed.\n");
  4195. ql_release_all(pdev);
  4196. pci_disable_device(pdev);
  4197. return err;
  4198. }
  4199. /* Start up the timer to trigger EEH if
  4200. * the bus goes dead
  4201. */
  4202. init_timer_deferrable(&qdev->timer);
  4203. qdev->timer.data = (unsigned long)qdev;
  4204. qdev->timer.function = ql_timer;
  4205. qdev->timer.expires = jiffies + (5*HZ);
  4206. add_timer(&qdev->timer);
  4207. ql_link_off(qdev);
  4208. ql_display_dev_info(ndev);
  4209. atomic_set(&qdev->lb_count, 0);
  4210. cards_found++;
  4211. return 0;
  4212. }
  4213. netdev_tx_t ql_lb_send(struct sk_buff *skb, struct net_device *ndev)
  4214. {
  4215. return qlge_send(skb, ndev);
  4216. }
  4217. int ql_clean_lb_rx_ring(struct rx_ring *rx_ring, int budget)
  4218. {
  4219. return ql_clean_inbound_rx_ring(rx_ring, budget);
  4220. }
  4221. static void __devexit qlge_remove(struct pci_dev *pdev)
  4222. {
  4223. struct net_device *ndev = pci_get_drvdata(pdev);
  4224. struct ql_adapter *qdev = netdev_priv(ndev);
  4225. del_timer_sync(&qdev->timer);
  4226. ql_cancel_all_work_sync(qdev);
  4227. unregister_netdev(ndev);
  4228. ql_release_all(pdev);
  4229. pci_disable_device(pdev);
  4230. free_netdev(ndev);
  4231. }
  4232. /* Clean up resources without touching hardware. */
  4233. static void ql_eeh_close(struct net_device *ndev)
  4234. {
  4235. int i;
  4236. struct ql_adapter *qdev = netdev_priv(ndev);
  4237. if (netif_carrier_ok(ndev)) {
  4238. netif_carrier_off(ndev);
  4239. netif_stop_queue(ndev);
  4240. }
  4241. /* Disabling the timer */
  4242. del_timer_sync(&qdev->timer);
  4243. ql_cancel_all_work_sync(qdev);
  4244. for (i = 0; i < qdev->rss_ring_count; i++)
  4245. netif_napi_del(&qdev->rx_ring[i].napi);
  4246. clear_bit(QL_ADAPTER_UP, &qdev->flags);
  4247. ql_tx_ring_clean(qdev);
  4248. ql_free_rx_buffers(qdev);
  4249. ql_release_adapter_resources(qdev);
  4250. }
  4251. /*
  4252. * This callback is called by the PCI subsystem whenever
  4253. * a PCI bus error is detected.
  4254. */
  4255. static pci_ers_result_t qlge_io_error_detected(struct pci_dev *pdev,
  4256. enum pci_channel_state state)
  4257. {
  4258. struct net_device *ndev = pci_get_drvdata(pdev);
  4259. struct ql_adapter *qdev = netdev_priv(ndev);
  4260. switch (state) {
  4261. case pci_channel_io_normal:
  4262. return PCI_ERS_RESULT_CAN_RECOVER;
  4263. case pci_channel_io_frozen:
  4264. netif_device_detach(ndev);
  4265. if (netif_running(ndev))
  4266. ql_eeh_close(ndev);
  4267. pci_disable_device(pdev);
  4268. return PCI_ERS_RESULT_NEED_RESET;
  4269. case pci_channel_io_perm_failure:
  4270. dev_err(&pdev->dev,
  4271. "%s: pci_channel_io_perm_failure.\n", __func__);
  4272. ql_eeh_close(ndev);
  4273. set_bit(QL_EEH_FATAL, &qdev->flags);
  4274. return PCI_ERS_RESULT_DISCONNECT;
  4275. }
  4276. /* Request a slot reset. */
  4277. return PCI_ERS_RESULT_NEED_RESET;
  4278. }
  4279. /*
  4280. * This callback is called after the PCI buss has been reset.
  4281. * Basically, this tries to restart the card from scratch.
  4282. * This is a shortened version of the device probe/discovery code,
  4283. * it resembles the first-half of the () routine.
  4284. */
  4285. static pci_ers_result_t qlge_io_slot_reset(struct pci_dev *pdev)
  4286. {
  4287. struct net_device *ndev = pci_get_drvdata(pdev);
  4288. struct ql_adapter *qdev = netdev_priv(ndev);
  4289. pdev->error_state = pci_channel_io_normal;
  4290. pci_restore_state(pdev);
  4291. if (pci_enable_device(pdev)) {
  4292. netif_err(qdev, ifup, qdev->ndev,
  4293. "Cannot re-enable PCI device after reset.\n");
  4294. return PCI_ERS_RESULT_DISCONNECT;
  4295. }
  4296. pci_set_master(pdev);
  4297. if (ql_adapter_reset(qdev)) {
  4298. netif_err(qdev, drv, qdev->ndev, "reset FAILED!\n");
  4299. set_bit(QL_EEH_FATAL, &qdev->flags);
  4300. return PCI_ERS_RESULT_DISCONNECT;
  4301. }
  4302. return PCI_ERS_RESULT_RECOVERED;
  4303. }
  4304. static void qlge_io_resume(struct pci_dev *pdev)
  4305. {
  4306. struct net_device *ndev = pci_get_drvdata(pdev);
  4307. struct ql_adapter *qdev = netdev_priv(ndev);
  4308. int err = 0;
  4309. if (netif_running(ndev)) {
  4310. err = qlge_open(ndev);
  4311. if (err) {
  4312. netif_err(qdev, ifup, qdev->ndev,
  4313. "Device initialization failed after reset.\n");
  4314. return;
  4315. }
  4316. } else {
  4317. netif_err(qdev, ifup, qdev->ndev,
  4318. "Device was not running prior to EEH.\n");
  4319. }
  4320. mod_timer(&qdev->timer, jiffies + (5*HZ));
  4321. netif_device_attach(ndev);
  4322. }
  4323. static struct pci_error_handlers qlge_err_handler = {
  4324. .error_detected = qlge_io_error_detected,
  4325. .slot_reset = qlge_io_slot_reset,
  4326. .resume = qlge_io_resume,
  4327. };
  4328. static int qlge_suspend(struct pci_dev *pdev, pm_message_t state)
  4329. {
  4330. struct net_device *ndev = pci_get_drvdata(pdev);
  4331. struct ql_adapter *qdev = netdev_priv(ndev);
  4332. int err;
  4333. netif_device_detach(ndev);
  4334. del_timer_sync(&qdev->timer);
  4335. if (netif_running(ndev)) {
  4336. err = ql_adapter_down(qdev);
  4337. if (!err)
  4338. return err;
  4339. }
  4340. ql_wol(qdev);
  4341. err = pci_save_state(pdev);
  4342. if (err)
  4343. return err;
  4344. pci_disable_device(pdev);
  4345. pci_set_power_state(pdev, pci_choose_state(pdev, state));
  4346. return 0;
  4347. }
  4348. #ifdef CONFIG_PM
  4349. static int qlge_resume(struct pci_dev *pdev)
  4350. {
  4351. struct net_device *ndev = pci_get_drvdata(pdev);
  4352. struct ql_adapter *qdev = netdev_priv(ndev);
  4353. int err;
  4354. pci_set_power_state(pdev, PCI_D0);
  4355. pci_restore_state(pdev);
  4356. err = pci_enable_device(pdev);
  4357. if (err) {
  4358. netif_err(qdev, ifup, qdev->ndev, "Cannot enable PCI device from suspend\n");
  4359. return err;
  4360. }
  4361. pci_set_master(pdev);
  4362. pci_enable_wake(pdev, PCI_D3hot, 0);
  4363. pci_enable_wake(pdev, PCI_D3cold, 0);
  4364. if (netif_running(ndev)) {
  4365. err = ql_adapter_up(qdev);
  4366. if (err)
  4367. return err;
  4368. }
  4369. mod_timer(&qdev->timer, jiffies + (5*HZ));
  4370. netif_device_attach(ndev);
  4371. return 0;
  4372. }
  4373. #endif /* CONFIG_PM */
  4374. static void qlge_shutdown(struct pci_dev *pdev)
  4375. {
  4376. qlge_suspend(pdev, PMSG_SUSPEND);
  4377. }
  4378. static struct pci_driver qlge_driver = {
  4379. .name = DRV_NAME,
  4380. .id_table = qlge_pci_tbl,
  4381. .probe = qlge_probe,
  4382. .remove = __devexit_p(qlge_remove),
  4383. #ifdef CONFIG_PM
  4384. .suspend = qlge_suspend,
  4385. .resume = qlge_resume,
  4386. #endif
  4387. .shutdown = qlge_shutdown,
  4388. .err_handler = &qlge_err_handler
  4389. };
  4390. static int __init qlge_init_module(void)
  4391. {
  4392. return pci_register_driver(&qlge_driver);
  4393. }
  4394. static void __exit qlge_exit(void)
  4395. {
  4396. pci_unregister_driver(&qlge_driver);
  4397. }
  4398. module_init(qlge_init_module);
  4399. module_exit(qlge_exit);