myri10ge.c 115 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218
  1. /*************************************************************************
  2. * myri10ge.c: Myricom Myri-10G Ethernet driver.
  3. *
  4. * Copyright (C) 2005 - 2011 Myricom, Inc.
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. Neither the name of Myricom, Inc. nor the names of its contributors
  16. * may be used to endorse or promote products derived from this software
  17. * without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  20. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  22. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  23. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  24. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  25. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  26. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  27. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  28. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  29. * POSSIBILITY OF SUCH DAMAGE.
  30. *
  31. *
  32. * If the eeprom on your board is not recent enough, you will need to get a
  33. * newer firmware image at:
  34. * http://www.myri.com/scs/download-Myri10GE.html
  35. *
  36. * Contact Information:
  37. * <help@myri.com>
  38. * Myricom, Inc., 325N Santa Anita Avenue, Arcadia, CA 91006
  39. *************************************************************************/
  40. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  41. #include <linux/tcp.h>
  42. #include <linux/netdevice.h>
  43. #include <linux/skbuff.h>
  44. #include <linux/string.h>
  45. #include <linux/module.h>
  46. #include <linux/pci.h>
  47. #include <linux/dma-mapping.h>
  48. #include <linux/etherdevice.h>
  49. #include <linux/if_ether.h>
  50. #include <linux/if_vlan.h>
  51. #include <linux/inet_lro.h>
  52. #include <linux/dca.h>
  53. #include <linux/ip.h>
  54. #include <linux/inet.h>
  55. #include <linux/in.h>
  56. #include <linux/ethtool.h>
  57. #include <linux/firmware.h>
  58. #include <linux/delay.h>
  59. #include <linux/timer.h>
  60. #include <linux/vmalloc.h>
  61. #include <linux/crc32.h>
  62. #include <linux/moduleparam.h>
  63. #include <linux/io.h>
  64. #include <linux/log2.h>
  65. #include <linux/slab.h>
  66. #include <linux/prefetch.h>
  67. #include <net/checksum.h>
  68. #include <net/ip.h>
  69. #include <net/tcp.h>
  70. #include <asm/byteorder.h>
  71. #include <asm/io.h>
  72. #include <asm/processor.h>
  73. #ifdef CONFIG_MTRR
  74. #include <asm/mtrr.h>
  75. #endif
  76. #include "myri10ge_mcp.h"
  77. #include "myri10ge_mcp_gen_header.h"
  78. #define MYRI10GE_VERSION_STR "1.5.3-1.534"
  79. MODULE_DESCRIPTION("Myricom 10G driver (10GbE)");
  80. MODULE_AUTHOR("Maintainer: help@myri.com");
  81. MODULE_VERSION(MYRI10GE_VERSION_STR);
  82. MODULE_LICENSE("Dual BSD/GPL");
  83. #define MYRI10GE_MAX_ETHER_MTU 9014
  84. #define MYRI10GE_ETH_STOPPED 0
  85. #define MYRI10GE_ETH_STOPPING 1
  86. #define MYRI10GE_ETH_STARTING 2
  87. #define MYRI10GE_ETH_RUNNING 3
  88. #define MYRI10GE_ETH_OPEN_FAILED 4
  89. #define MYRI10GE_EEPROM_STRINGS_SIZE 256
  90. #define MYRI10GE_MAX_SEND_DESC_TSO ((65536 / 2048) * 2)
  91. #define MYRI10GE_MAX_LRO_DESCRIPTORS 8
  92. #define MYRI10GE_LRO_MAX_PKTS 64
  93. #define MYRI10GE_NO_CONFIRM_DATA htonl(0xffffffff)
  94. #define MYRI10GE_NO_RESPONSE_RESULT 0xffffffff
  95. #define MYRI10GE_ALLOC_ORDER 0
  96. #define MYRI10GE_ALLOC_SIZE ((1 << MYRI10GE_ALLOC_ORDER) * PAGE_SIZE)
  97. #define MYRI10GE_MAX_FRAGS_PER_FRAME (MYRI10GE_MAX_ETHER_MTU/MYRI10GE_ALLOC_SIZE + 1)
  98. #define MYRI10GE_MAX_SLICES 32
  99. struct myri10ge_rx_buffer_state {
  100. struct page *page;
  101. int page_offset;
  102. DEFINE_DMA_UNMAP_ADDR(bus);
  103. DEFINE_DMA_UNMAP_LEN(len);
  104. };
  105. struct myri10ge_tx_buffer_state {
  106. struct sk_buff *skb;
  107. int last;
  108. DEFINE_DMA_UNMAP_ADDR(bus);
  109. DEFINE_DMA_UNMAP_LEN(len);
  110. };
  111. struct myri10ge_cmd {
  112. u32 data0;
  113. u32 data1;
  114. u32 data2;
  115. };
  116. struct myri10ge_rx_buf {
  117. struct mcp_kreq_ether_recv __iomem *lanai; /* lanai ptr for recv ring */
  118. struct mcp_kreq_ether_recv *shadow; /* host shadow of recv ring */
  119. struct myri10ge_rx_buffer_state *info;
  120. struct page *page;
  121. dma_addr_t bus;
  122. int page_offset;
  123. int cnt;
  124. int fill_cnt;
  125. int alloc_fail;
  126. int mask; /* number of rx slots -1 */
  127. int watchdog_needed;
  128. };
  129. struct myri10ge_tx_buf {
  130. struct mcp_kreq_ether_send __iomem *lanai; /* lanai ptr for sendq */
  131. __be32 __iomem *send_go; /* "go" doorbell ptr */
  132. __be32 __iomem *send_stop; /* "stop" doorbell ptr */
  133. struct mcp_kreq_ether_send *req_list; /* host shadow of sendq */
  134. char *req_bytes;
  135. struct myri10ge_tx_buffer_state *info;
  136. int mask; /* number of transmit slots -1 */
  137. int req ____cacheline_aligned; /* transmit slots submitted */
  138. int pkt_start; /* packets started */
  139. int stop_queue;
  140. int linearized;
  141. int done ____cacheline_aligned; /* transmit slots completed */
  142. int pkt_done; /* packets completed */
  143. int wake_queue;
  144. int queue_active;
  145. };
  146. struct myri10ge_rx_done {
  147. struct mcp_slot *entry;
  148. dma_addr_t bus;
  149. int cnt;
  150. int idx;
  151. struct net_lro_mgr lro_mgr;
  152. struct net_lro_desc lro_desc[MYRI10GE_MAX_LRO_DESCRIPTORS];
  153. };
  154. struct myri10ge_slice_netstats {
  155. unsigned long rx_packets;
  156. unsigned long tx_packets;
  157. unsigned long rx_bytes;
  158. unsigned long tx_bytes;
  159. unsigned long rx_dropped;
  160. unsigned long tx_dropped;
  161. };
  162. struct myri10ge_slice_state {
  163. struct myri10ge_tx_buf tx; /* transmit ring */
  164. struct myri10ge_rx_buf rx_small;
  165. struct myri10ge_rx_buf rx_big;
  166. struct myri10ge_rx_done rx_done;
  167. struct net_device *dev;
  168. struct napi_struct napi;
  169. struct myri10ge_priv *mgp;
  170. struct myri10ge_slice_netstats stats;
  171. __be32 __iomem *irq_claim;
  172. struct mcp_irq_data *fw_stats;
  173. dma_addr_t fw_stats_bus;
  174. int watchdog_tx_done;
  175. int watchdog_tx_req;
  176. int watchdog_rx_done;
  177. int stuck;
  178. #ifdef CONFIG_MYRI10GE_DCA
  179. int cached_dca_tag;
  180. int cpu;
  181. __be32 __iomem *dca_tag;
  182. #endif
  183. char irq_desc[32];
  184. };
  185. struct myri10ge_priv {
  186. struct myri10ge_slice_state *ss;
  187. int tx_boundary; /* boundary transmits cannot cross */
  188. int num_slices;
  189. int running; /* running? */
  190. int small_bytes;
  191. int big_bytes;
  192. int max_intr_slots;
  193. struct net_device *dev;
  194. u8 __iomem *sram;
  195. int sram_size;
  196. unsigned long board_span;
  197. unsigned long iomem_base;
  198. __be32 __iomem *irq_deassert;
  199. char *mac_addr_string;
  200. struct mcp_cmd_response *cmd;
  201. dma_addr_t cmd_bus;
  202. struct pci_dev *pdev;
  203. int msi_enabled;
  204. int msix_enabled;
  205. struct msix_entry *msix_vectors;
  206. #ifdef CONFIG_MYRI10GE_DCA
  207. int dca_enabled;
  208. int relaxed_order;
  209. #endif
  210. u32 link_state;
  211. unsigned int rdma_tags_available;
  212. int intr_coal_delay;
  213. __be32 __iomem *intr_coal_delay_ptr;
  214. int mtrr;
  215. int wc_enabled;
  216. int down_cnt;
  217. wait_queue_head_t down_wq;
  218. struct work_struct watchdog_work;
  219. struct timer_list watchdog_timer;
  220. int watchdog_resets;
  221. int watchdog_pause;
  222. int pause;
  223. bool fw_name_allocated;
  224. char *fw_name;
  225. char eeprom_strings[MYRI10GE_EEPROM_STRINGS_SIZE];
  226. char *product_code_string;
  227. char fw_version[128];
  228. int fw_ver_major;
  229. int fw_ver_minor;
  230. int fw_ver_tiny;
  231. int adopted_rx_filter_bug;
  232. u8 mac_addr[6]; /* eeprom mac address */
  233. unsigned long serial_number;
  234. int vendor_specific_offset;
  235. int fw_multicast_support;
  236. u32 features;
  237. u32 max_tso6;
  238. u32 read_dma;
  239. u32 write_dma;
  240. u32 read_write_dma;
  241. u32 link_changes;
  242. u32 msg_enable;
  243. unsigned int board_number;
  244. int rebooted;
  245. };
  246. static char *myri10ge_fw_unaligned = "myri10ge_ethp_z8e.dat";
  247. static char *myri10ge_fw_aligned = "myri10ge_eth_z8e.dat";
  248. static char *myri10ge_fw_rss_unaligned = "myri10ge_rss_ethp_z8e.dat";
  249. static char *myri10ge_fw_rss_aligned = "myri10ge_rss_eth_z8e.dat";
  250. MODULE_FIRMWARE("myri10ge_ethp_z8e.dat");
  251. MODULE_FIRMWARE("myri10ge_eth_z8e.dat");
  252. MODULE_FIRMWARE("myri10ge_rss_ethp_z8e.dat");
  253. MODULE_FIRMWARE("myri10ge_rss_eth_z8e.dat");
  254. /* Careful: must be accessed under kparam_block_sysfs_write */
  255. static char *myri10ge_fw_name = NULL;
  256. module_param(myri10ge_fw_name, charp, S_IRUGO | S_IWUSR);
  257. MODULE_PARM_DESC(myri10ge_fw_name, "Firmware image name");
  258. #define MYRI10GE_MAX_BOARDS 8
  259. static char *myri10ge_fw_names[MYRI10GE_MAX_BOARDS] =
  260. {[0 ... (MYRI10GE_MAX_BOARDS - 1)] = NULL };
  261. module_param_array_named(myri10ge_fw_names, myri10ge_fw_names, charp, NULL,
  262. 0444);
  263. MODULE_PARM_DESC(myri10ge_fw_name, "Firmware image names per board");
  264. static int myri10ge_ecrc_enable = 1;
  265. module_param(myri10ge_ecrc_enable, int, S_IRUGO);
  266. MODULE_PARM_DESC(myri10ge_ecrc_enable, "Enable Extended CRC on PCI-E");
  267. static int myri10ge_small_bytes = -1; /* -1 == auto */
  268. module_param(myri10ge_small_bytes, int, S_IRUGO | S_IWUSR);
  269. MODULE_PARM_DESC(myri10ge_small_bytes, "Threshold of small packets");
  270. static int myri10ge_msi = 1; /* enable msi by default */
  271. module_param(myri10ge_msi, int, S_IRUGO | S_IWUSR);
  272. MODULE_PARM_DESC(myri10ge_msi, "Enable Message Signalled Interrupts");
  273. static int myri10ge_intr_coal_delay = 75;
  274. module_param(myri10ge_intr_coal_delay, int, S_IRUGO);
  275. MODULE_PARM_DESC(myri10ge_intr_coal_delay, "Interrupt coalescing delay");
  276. static int myri10ge_flow_control = 1;
  277. module_param(myri10ge_flow_control, int, S_IRUGO);
  278. MODULE_PARM_DESC(myri10ge_flow_control, "Pause parameter");
  279. static int myri10ge_deassert_wait = 1;
  280. module_param(myri10ge_deassert_wait, int, S_IRUGO | S_IWUSR);
  281. MODULE_PARM_DESC(myri10ge_deassert_wait,
  282. "Wait when deasserting legacy interrupts");
  283. static int myri10ge_force_firmware = 0;
  284. module_param(myri10ge_force_firmware, int, S_IRUGO);
  285. MODULE_PARM_DESC(myri10ge_force_firmware,
  286. "Force firmware to assume aligned completions");
  287. static int myri10ge_initial_mtu = MYRI10GE_MAX_ETHER_MTU - ETH_HLEN;
  288. module_param(myri10ge_initial_mtu, int, S_IRUGO);
  289. MODULE_PARM_DESC(myri10ge_initial_mtu, "Initial MTU");
  290. static int myri10ge_napi_weight = 64;
  291. module_param(myri10ge_napi_weight, int, S_IRUGO);
  292. MODULE_PARM_DESC(myri10ge_napi_weight, "Set NAPI weight");
  293. static int myri10ge_watchdog_timeout = 1;
  294. module_param(myri10ge_watchdog_timeout, int, S_IRUGO);
  295. MODULE_PARM_DESC(myri10ge_watchdog_timeout, "Set watchdog timeout");
  296. static int myri10ge_max_irq_loops = 1048576;
  297. module_param(myri10ge_max_irq_loops, int, S_IRUGO);
  298. MODULE_PARM_DESC(myri10ge_max_irq_loops,
  299. "Set stuck legacy IRQ detection threshold");
  300. #define MYRI10GE_MSG_DEFAULT NETIF_MSG_LINK
  301. static int myri10ge_debug = -1; /* defaults above */
  302. module_param(myri10ge_debug, int, 0);
  303. MODULE_PARM_DESC(myri10ge_debug, "Debug level (0=none,...,16=all)");
  304. static int myri10ge_lro_max_pkts = MYRI10GE_LRO_MAX_PKTS;
  305. module_param(myri10ge_lro_max_pkts, int, S_IRUGO);
  306. MODULE_PARM_DESC(myri10ge_lro_max_pkts,
  307. "Number of LRO packets to be aggregated");
  308. static int myri10ge_fill_thresh = 256;
  309. module_param(myri10ge_fill_thresh, int, S_IRUGO | S_IWUSR);
  310. MODULE_PARM_DESC(myri10ge_fill_thresh, "Number of empty rx slots allowed");
  311. static int myri10ge_reset_recover = 1;
  312. static int myri10ge_max_slices = 1;
  313. module_param(myri10ge_max_slices, int, S_IRUGO);
  314. MODULE_PARM_DESC(myri10ge_max_slices, "Max tx/rx queues");
  315. static int myri10ge_rss_hash = MXGEFW_RSS_HASH_TYPE_SRC_DST_PORT;
  316. module_param(myri10ge_rss_hash, int, S_IRUGO);
  317. MODULE_PARM_DESC(myri10ge_rss_hash, "Type of RSS hashing to do");
  318. static int myri10ge_dca = 1;
  319. module_param(myri10ge_dca, int, S_IRUGO);
  320. MODULE_PARM_DESC(myri10ge_dca, "Enable DCA if possible");
  321. #define MYRI10GE_FW_OFFSET 1024*1024
  322. #define MYRI10GE_HIGHPART_TO_U32(X) \
  323. (sizeof (X) == 8) ? ((u32)((u64)(X) >> 32)) : (0)
  324. #define MYRI10GE_LOWPART_TO_U32(X) ((u32)(X))
  325. #define myri10ge_pio_copy(to,from,size) __iowrite64_copy(to,from,size/8)
  326. static void myri10ge_set_multicast_list(struct net_device *dev);
  327. static netdev_tx_t myri10ge_sw_tso(struct sk_buff *skb,
  328. struct net_device *dev);
  329. static inline void put_be32(__be32 val, __be32 __iomem * p)
  330. {
  331. __raw_writel((__force __u32) val, (__force void __iomem *)p);
  332. }
  333. static struct rtnl_link_stats64 *myri10ge_get_stats(struct net_device *dev,
  334. struct rtnl_link_stats64 *stats);
  335. static void set_fw_name(struct myri10ge_priv *mgp, char *name, bool allocated)
  336. {
  337. if (mgp->fw_name_allocated)
  338. kfree(mgp->fw_name);
  339. mgp->fw_name = name;
  340. mgp->fw_name_allocated = allocated;
  341. }
  342. static int
  343. myri10ge_send_cmd(struct myri10ge_priv *mgp, u32 cmd,
  344. struct myri10ge_cmd *data, int atomic)
  345. {
  346. struct mcp_cmd *buf;
  347. char buf_bytes[sizeof(*buf) + 8];
  348. struct mcp_cmd_response *response = mgp->cmd;
  349. char __iomem *cmd_addr = mgp->sram + MXGEFW_ETH_CMD;
  350. u32 dma_low, dma_high, result, value;
  351. int sleep_total = 0;
  352. /* ensure buf is aligned to 8 bytes */
  353. buf = (struct mcp_cmd *)ALIGN((unsigned long)buf_bytes, 8);
  354. buf->data0 = htonl(data->data0);
  355. buf->data1 = htonl(data->data1);
  356. buf->data2 = htonl(data->data2);
  357. buf->cmd = htonl(cmd);
  358. dma_low = MYRI10GE_LOWPART_TO_U32(mgp->cmd_bus);
  359. dma_high = MYRI10GE_HIGHPART_TO_U32(mgp->cmd_bus);
  360. buf->response_addr.low = htonl(dma_low);
  361. buf->response_addr.high = htonl(dma_high);
  362. response->result = htonl(MYRI10GE_NO_RESPONSE_RESULT);
  363. mb();
  364. myri10ge_pio_copy(cmd_addr, buf, sizeof(*buf));
  365. /* wait up to 15ms. Longest command is the DMA benchmark,
  366. * which is capped at 5ms, but runs from a timeout handler
  367. * that runs every 7.8ms. So a 15ms timeout leaves us with
  368. * a 2.2ms margin
  369. */
  370. if (atomic) {
  371. /* if atomic is set, do not sleep,
  372. * and try to get the completion quickly
  373. * (1ms will be enough for those commands) */
  374. for (sleep_total = 0;
  375. sleep_total < 1000 &&
  376. response->result == htonl(MYRI10GE_NO_RESPONSE_RESULT);
  377. sleep_total += 10) {
  378. udelay(10);
  379. mb();
  380. }
  381. } else {
  382. /* use msleep for most command */
  383. for (sleep_total = 0;
  384. sleep_total < 15 &&
  385. response->result == htonl(MYRI10GE_NO_RESPONSE_RESULT);
  386. sleep_total++)
  387. msleep(1);
  388. }
  389. result = ntohl(response->result);
  390. value = ntohl(response->data);
  391. if (result != MYRI10GE_NO_RESPONSE_RESULT) {
  392. if (result == 0) {
  393. data->data0 = value;
  394. return 0;
  395. } else if (result == MXGEFW_CMD_UNKNOWN) {
  396. return -ENOSYS;
  397. } else if (result == MXGEFW_CMD_ERROR_UNALIGNED) {
  398. return -E2BIG;
  399. } else if (result == MXGEFW_CMD_ERROR_RANGE &&
  400. cmd == MXGEFW_CMD_ENABLE_RSS_QUEUES &&
  401. (data->
  402. data1 & MXGEFW_SLICE_ENABLE_MULTIPLE_TX_QUEUES) !=
  403. 0) {
  404. return -ERANGE;
  405. } else {
  406. dev_err(&mgp->pdev->dev,
  407. "command %d failed, result = %d\n",
  408. cmd, result);
  409. return -ENXIO;
  410. }
  411. }
  412. dev_err(&mgp->pdev->dev, "command %d timed out, result = %d\n",
  413. cmd, result);
  414. return -EAGAIN;
  415. }
  416. /*
  417. * The eeprom strings on the lanaiX have the format
  418. * SN=x\0
  419. * MAC=x:x:x:x:x:x\0
  420. * PT:ddd mmm xx xx:xx:xx xx\0
  421. * PV:ddd mmm xx xx:xx:xx xx\0
  422. */
  423. static int myri10ge_read_mac_addr(struct myri10ge_priv *mgp)
  424. {
  425. char *ptr, *limit;
  426. int i;
  427. ptr = mgp->eeprom_strings;
  428. limit = mgp->eeprom_strings + MYRI10GE_EEPROM_STRINGS_SIZE;
  429. while (*ptr != '\0' && ptr < limit) {
  430. if (memcmp(ptr, "MAC=", 4) == 0) {
  431. ptr += 4;
  432. mgp->mac_addr_string = ptr;
  433. for (i = 0; i < 6; i++) {
  434. if ((ptr + 2) > limit)
  435. goto abort;
  436. mgp->mac_addr[i] =
  437. simple_strtoul(ptr, &ptr, 16);
  438. ptr += 1;
  439. }
  440. }
  441. if (memcmp(ptr, "PC=", 3) == 0) {
  442. ptr += 3;
  443. mgp->product_code_string = ptr;
  444. }
  445. if (memcmp((const void *)ptr, "SN=", 3) == 0) {
  446. ptr += 3;
  447. mgp->serial_number = simple_strtoul(ptr, &ptr, 10);
  448. }
  449. while (ptr < limit && *ptr++) ;
  450. }
  451. return 0;
  452. abort:
  453. dev_err(&mgp->pdev->dev, "failed to parse eeprom_strings\n");
  454. return -ENXIO;
  455. }
  456. /*
  457. * Enable or disable periodic RDMAs from the host to make certain
  458. * chipsets resend dropped PCIe messages
  459. */
  460. static void myri10ge_dummy_rdma(struct myri10ge_priv *mgp, int enable)
  461. {
  462. char __iomem *submit;
  463. __be32 buf[16] __attribute__ ((__aligned__(8)));
  464. u32 dma_low, dma_high;
  465. int i;
  466. /* clear confirmation addr */
  467. mgp->cmd->data = 0;
  468. mb();
  469. /* send a rdma command to the PCIe engine, and wait for the
  470. * response in the confirmation address. The firmware should
  471. * write a -1 there to indicate it is alive and well
  472. */
  473. dma_low = MYRI10GE_LOWPART_TO_U32(mgp->cmd_bus);
  474. dma_high = MYRI10GE_HIGHPART_TO_U32(mgp->cmd_bus);
  475. buf[0] = htonl(dma_high); /* confirm addr MSW */
  476. buf[1] = htonl(dma_low); /* confirm addr LSW */
  477. buf[2] = MYRI10GE_NO_CONFIRM_DATA; /* confirm data */
  478. buf[3] = htonl(dma_high); /* dummy addr MSW */
  479. buf[4] = htonl(dma_low); /* dummy addr LSW */
  480. buf[5] = htonl(enable); /* enable? */
  481. submit = mgp->sram + MXGEFW_BOOT_DUMMY_RDMA;
  482. myri10ge_pio_copy(submit, &buf, sizeof(buf));
  483. for (i = 0; mgp->cmd->data != MYRI10GE_NO_CONFIRM_DATA && i < 20; i++)
  484. msleep(1);
  485. if (mgp->cmd->data != MYRI10GE_NO_CONFIRM_DATA)
  486. dev_err(&mgp->pdev->dev, "dummy rdma %s failed\n",
  487. (enable ? "enable" : "disable"));
  488. }
  489. static int
  490. myri10ge_validate_firmware(struct myri10ge_priv *mgp,
  491. struct mcp_gen_header *hdr)
  492. {
  493. struct device *dev = &mgp->pdev->dev;
  494. /* check firmware type */
  495. if (ntohl(hdr->mcp_type) != MCP_TYPE_ETH) {
  496. dev_err(dev, "Bad firmware type: 0x%x\n", ntohl(hdr->mcp_type));
  497. return -EINVAL;
  498. }
  499. /* save firmware version for ethtool */
  500. strncpy(mgp->fw_version, hdr->version, sizeof(mgp->fw_version));
  501. sscanf(mgp->fw_version, "%d.%d.%d", &mgp->fw_ver_major,
  502. &mgp->fw_ver_minor, &mgp->fw_ver_tiny);
  503. if (!(mgp->fw_ver_major == MXGEFW_VERSION_MAJOR &&
  504. mgp->fw_ver_minor == MXGEFW_VERSION_MINOR)) {
  505. dev_err(dev, "Found firmware version %s\n", mgp->fw_version);
  506. dev_err(dev, "Driver needs %d.%d\n", MXGEFW_VERSION_MAJOR,
  507. MXGEFW_VERSION_MINOR);
  508. return -EINVAL;
  509. }
  510. return 0;
  511. }
  512. static int myri10ge_load_hotplug_firmware(struct myri10ge_priv *mgp, u32 * size)
  513. {
  514. unsigned crc, reread_crc;
  515. const struct firmware *fw;
  516. struct device *dev = &mgp->pdev->dev;
  517. unsigned char *fw_readback;
  518. struct mcp_gen_header *hdr;
  519. size_t hdr_offset;
  520. int status;
  521. unsigned i;
  522. if ((status = request_firmware(&fw, mgp->fw_name, dev)) < 0) {
  523. dev_err(dev, "Unable to load %s firmware image via hotplug\n",
  524. mgp->fw_name);
  525. status = -EINVAL;
  526. goto abort_with_nothing;
  527. }
  528. /* check size */
  529. if (fw->size >= mgp->sram_size - MYRI10GE_FW_OFFSET ||
  530. fw->size < MCP_HEADER_PTR_OFFSET + 4) {
  531. dev_err(dev, "Firmware size invalid:%d\n", (int)fw->size);
  532. status = -EINVAL;
  533. goto abort_with_fw;
  534. }
  535. /* check id */
  536. hdr_offset = ntohl(*(__be32 *) (fw->data + MCP_HEADER_PTR_OFFSET));
  537. if ((hdr_offset & 3) || hdr_offset + sizeof(*hdr) > fw->size) {
  538. dev_err(dev, "Bad firmware file\n");
  539. status = -EINVAL;
  540. goto abort_with_fw;
  541. }
  542. hdr = (void *)(fw->data + hdr_offset);
  543. status = myri10ge_validate_firmware(mgp, hdr);
  544. if (status != 0)
  545. goto abort_with_fw;
  546. crc = crc32(~0, fw->data, fw->size);
  547. for (i = 0; i < fw->size; i += 256) {
  548. myri10ge_pio_copy(mgp->sram + MYRI10GE_FW_OFFSET + i,
  549. fw->data + i,
  550. min(256U, (unsigned)(fw->size - i)));
  551. mb();
  552. readb(mgp->sram);
  553. }
  554. fw_readback = vmalloc(fw->size);
  555. if (!fw_readback) {
  556. status = -ENOMEM;
  557. goto abort_with_fw;
  558. }
  559. /* corruption checking is good for parity recovery and buggy chipset */
  560. memcpy_fromio(fw_readback, mgp->sram + MYRI10GE_FW_OFFSET, fw->size);
  561. reread_crc = crc32(~0, fw_readback, fw->size);
  562. vfree(fw_readback);
  563. if (crc != reread_crc) {
  564. dev_err(dev, "CRC failed(fw-len=%u), got 0x%x (expect 0x%x)\n",
  565. (unsigned)fw->size, reread_crc, crc);
  566. status = -EIO;
  567. goto abort_with_fw;
  568. }
  569. *size = (u32) fw->size;
  570. abort_with_fw:
  571. release_firmware(fw);
  572. abort_with_nothing:
  573. return status;
  574. }
  575. static int myri10ge_adopt_running_firmware(struct myri10ge_priv *mgp)
  576. {
  577. struct mcp_gen_header *hdr;
  578. struct device *dev = &mgp->pdev->dev;
  579. const size_t bytes = sizeof(struct mcp_gen_header);
  580. size_t hdr_offset;
  581. int status;
  582. /* find running firmware header */
  583. hdr_offset = swab32(readl(mgp->sram + MCP_HEADER_PTR_OFFSET));
  584. if ((hdr_offset & 3) || hdr_offset + sizeof(*hdr) > mgp->sram_size) {
  585. dev_err(dev, "Running firmware has bad header offset (%d)\n",
  586. (int)hdr_offset);
  587. return -EIO;
  588. }
  589. /* copy header of running firmware from SRAM to host memory to
  590. * validate firmware */
  591. hdr = kmalloc(bytes, GFP_KERNEL);
  592. if (hdr == NULL) {
  593. dev_err(dev, "could not malloc firmware hdr\n");
  594. return -ENOMEM;
  595. }
  596. memcpy_fromio(hdr, mgp->sram + hdr_offset, bytes);
  597. status = myri10ge_validate_firmware(mgp, hdr);
  598. kfree(hdr);
  599. /* check to see if adopted firmware has bug where adopting
  600. * it will cause broadcasts to be filtered unless the NIC
  601. * is kept in ALLMULTI mode */
  602. if (mgp->fw_ver_major == 1 && mgp->fw_ver_minor == 4 &&
  603. mgp->fw_ver_tiny >= 4 && mgp->fw_ver_tiny <= 11) {
  604. mgp->adopted_rx_filter_bug = 1;
  605. dev_warn(dev, "Adopting fw %d.%d.%d: "
  606. "working around rx filter bug\n",
  607. mgp->fw_ver_major, mgp->fw_ver_minor,
  608. mgp->fw_ver_tiny);
  609. }
  610. return status;
  611. }
  612. static int myri10ge_get_firmware_capabilities(struct myri10ge_priv *mgp)
  613. {
  614. struct myri10ge_cmd cmd;
  615. int status;
  616. /* probe for IPv6 TSO support */
  617. mgp->features = NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_TSO;
  618. status = myri10ge_send_cmd(mgp, MXGEFW_CMD_GET_MAX_TSO6_HDR_SIZE,
  619. &cmd, 0);
  620. if (status == 0) {
  621. mgp->max_tso6 = cmd.data0;
  622. mgp->features |= NETIF_F_TSO6;
  623. }
  624. status = myri10ge_send_cmd(mgp, MXGEFW_CMD_GET_RX_RING_SIZE, &cmd, 0);
  625. if (status != 0) {
  626. dev_err(&mgp->pdev->dev,
  627. "failed MXGEFW_CMD_GET_RX_RING_SIZE\n");
  628. return -ENXIO;
  629. }
  630. mgp->max_intr_slots = 2 * (cmd.data0 / sizeof(struct mcp_dma_addr));
  631. return 0;
  632. }
  633. static int myri10ge_load_firmware(struct myri10ge_priv *mgp, int adopt)
  634. {
  635. char __iomem *submit;
  636. __be32 buf[16] __attribute__ ((__aligned__(8)));
  637. u32 dma_low, dma_high, size;
  638. int status, i;
  639. size = 0;
  640. status = myri10ge_load_hotplug_firmware(mgp, &size);
  641. if (status) {
  642. if (!adopt)
  643. return status;
  644. dev_warn(&mgp->pdev->dev, "hotplug firmware loading failed\n");
  645. /* Do not attempt to adopt firmware if there
  646. * was a bad crc */
  647. if (status == -EIO)
  648. return status;
  649. status = myri10ge_adopt_running_firmware(mgp);
  650. if (status != 0) {
  651. dev_err(&mgp->pdev->dev,
  652. "failed to adopt running firmware\n");
  653. return status;
  654. }
  655. dev_info(&mgp->pdev->dev,
  656. "Successfully adopted running firmware\n");
  657. if (mgp->tx_boundary == 4096) {
  658. dev_warn(&mgp->pdev->dev,
  659. "Using firmware currently running on NIC"
  660. ". For optimal\n");
  661. dev_warn(&mgp->pdev->dev,
  662. "performance consider loading optimized "
  663. "firmware\n");
  664. dev_warn(&mgp->pdev->dev, "via hotplug\n");
  665. }
  666. set_fw_name(mgp, "adopted", false);
  667. mgp->tx_boundary = 2048;
  668. myri10ge_dummy_rdma(mgp, 1);
  669. status = myri10ge_get_firmware_capabilities(mgp);
  670. return status;
  671. }
  672. /* clear confirmation addr */
  673. mgp->cmd->data = 0;
  674. mb();
  675. /* send a reload command to the bootstrap MCP, and wait for the
  676. * response in the confirmation address. The firmware should
  677. * write a -1 there to indicate it is alive and well
  678. */
  679. dma_low = MYRI10GE_LOWPART_TO_U32(mgp->cmd_bus);
  680. dma_high = MYRI10GE_HIGHPART_TO_U32(mgp->cmd_bus);
  681. buf[0] = htonl(dma_high); /* confirm addr MSW */
  682. buf[1] = htonl(dma_low); /* confirm addr LSW */
  683. buf[2] = MYRI10GE_NO_CONFIRM_DATA; /* confirm data */
  684. /* FIX: All newest firmware should un-protect the bottom of
  685. * the sram before handoff. However, the very first interfaces
  686. * do not. Therefore the handoff copy must skip the first 8 bytes
  687. */
  688. buf[3] = htonl(MYRI10GE_FW_OFFSET + 8); /* where the code starts */
  689. buf[4] = htonl(size - 8); /* length of code */
  690. buf[5] = htonl(8); /* where to copy to */
  691. buf[6] = htonl(0); /* where to jump to */
  692. submit = mgp->sram + MXGEFW_BOOT_HANDOFF;
  693. myri10ge_pio_copy(submit, &buf, sizeof(buf));
  694. mb();
  695. msleep(1);
  696. mb();
  697. i = 0;
  698. while (mgp->cmd->data != MYRI10GE_NO_CONFIRM_DATA && i < 9) {
  699. msleep(1 << i);
  700. i++;
  701. }
  702. if (mgp->cmd->data != MYRI10GE_NO_CONFIRM_DATA) {
  703. dev_err(&mgp->pdev->dev, "handoff failed\n");
  704. return -ENXIO;
  705. }
  706. myri10ge_dummy_rdma(mgp, 1);
  707. status = myri10ge_get_firmware_capabilities(mgp);
  708. return status;
  709. }
  710. static int myri10ge_update_mac_address(struct myri10ge_priv *mgp, u8 * addr)
  711. {
  712. struct myri10ge_cmd cmd;
  713. int status;
  714. cmd.data0 = ((addr[0] << 24) | (addr[1] << 16)
  715. | (addr[2] << 8) | addr[3]);
  716. cmd.data1 = ((addr[4] << 8) | (addr[5]));
  717. status = myri10ge_send_cmd(mgp, MXGEFW_SET_MAC_ADDRESS, &cmd, 0);
  718. return status;
  719. }
  720. static int myri10ge_change_pause(struct myri10ge_priv *mgp, int pause)
  721. {
  722. struct myri10ge_cmd cmd;
  723. int status, ctl;
  724. ctl = pause ? MXGEFW_ENABLE_FLOW_CONTROL : MXGEFW_DISABLE_FLOW_CONTROL;
  725. status = myri10ge_send_cmd(mgp, ctl, &cmd, 0);
  726. if (status) {
  727. netdev_err(mgp->dev, "Failed to set flow control mode\n");
  728. return status;
  729. }
  730. mgp->pause = pause;
  731. return 0;
  732. }
  733. static void
  734. myri10ge_change_promisc(struct myri10ge_priv *mgp, int promisc, int atomic)
  735. {
  736. struct myri10ge_cmd cmd;
  737. int status, ctl;
  738. ctl = promisc ? MXGEFW_ENABLE_PROMISC : MXGEFW_DISABLE_PROMISC;
  739. status = myri10ge_send_cmd(mgp, ctl, &cmd, atomic);
  740. if (status)
  741. netdev_err(mgp->dev, "Failed to set promisc mode\n");
  742. }
  743. static int myri10ge_dma_test(struct myri10ge_priv *mgp, int test_type)
  744. {
  745. struct myri10ge_cmd cmd;
  746. int status;
  747. u32 len;
  748. struct page *dmatest_page;
  749. dma_addr_t dmatest_bus;
  750. char *test = " ";
  751. dmatest_page = alloc_page(GFP_KERNEL);
  752. if (!dmatest_page)
  753. return -ENOMEM;
  754. dmatest_bus = pci_map_page(mgp->pdev, dmatest_page, 0, PAGE_SIZE,
  755. DMA_BIDIRECTIONAL);
  756. /* Run a small DMA test.
  757. * The magic multipliers to the length tell the firmware
  758. * to do DMA read, write, or read+write tests. The
  759. * results are returned in cmd.data0. The upper 16
  760. * bits or the return is the number of transfers completed.
  761. * The lower 16 bits is the time in 0.5us ticks that the
  762. * transfers took to complete.
  763. */
  764. len = mgp->tx_boundary;
  765. cmd.data0 = MYRI10GE_LOWPART_TO_U32(dmatest_bus);
  766. cmd.data1 = MYRI10GE_HIGHPART_TO_U32(dmatest_bus);
  767. cmd.data2 = len * 0x10000;
  768. status = myri10ge_send_cmd(mgp, test_type, &cmd, 0);
  769. if (status != 0) {
  770. test = "read";
  771. goto abort;
  772. }
  773. mgp->read_dma = ((cmd.data0 >> 16) * len * 2) / (cmd.data0 & 0xffff);
  774. cmd.data0 = MYRI10GE_LOWPART_TO_U32(dmatest_bus);
  775. cmd.data1 = MYRI10GE_HIGHPART_TO_U32(dmatest_bus);
  776. cmd.data2 = len * 0x1;
  777. status = myri10ge_send_cmd(mgp, test_type, &cmd, 0);
  778. if (status != 0) {
  779. test = "write";
  780. goto abort;
  781. }
  782. mgp->write_dma = ((cmd.data0 >> 16) * len * 2) / (cmd.data0 & 0xffff);
  783. cmd.data0 = MYRI10GE_LOWPART_TO_U32(dmatest_bus);
  784. cmd.data1 = MYRI10GE_HIGHPART_TO_U32(dmatest_bus);
  785. cmd.data2 = len * 0x10001;
  786. status = myri10ge_send_cmd(mgp, test_type, &cmd, 0);
  787. if (status != 0) {
  788. test = "read/write";
  789. goto abort;
  790. }
  791. mgp->read_write_dma = ((cmd.data0 >> 16) * len * 2 * 2) /
  792. (cmd.data0 & 0xffff);
  793. abort:
  794. pci_unmap_page(mgp->pdev, dmatest_bus, PAGE_SIZE, DMA_BIDIRECTIONAL);
  795. put_page(dmatest_page);
  796. if (status != 0 && test_type != MXGEFW_CMD_UNALIGNED_TEST)
  797. dev_warn(&mgp->pdev->dev, "DMA %s benchmark failed: %d\n",
  798. test, status);
  799. return status;
  800. }
  801. static int myri10ge_reset(struct myri10ge_priv *mgp)
  802. {
  803. struct myri10ge_cmd cmd;
  804. struct myri10ge_slice_state *ss;
  805. int i, status;
  806. size_t bytes;
  807. #ifdef CONFIG_MYRI10GE_DCA
  808. unsigned long dca_tag_off;
  809. #endif
  810. /* try to send a reset command to the card to see if it
  811. * is alive */
  812. memset(&cmd, 0, sizeof(cmd));
  813. status = myri10ge_send_cmd(mgp, MXGEFW_CMD_RESET, &cmd, 0);
  814. if (status != 0) {
  815. dev_err(&mgp->pdev->dev, "failed reset\n");
  816. return -ENXIO;
  817. }
  818. (void)myri10ge_dma_test(mgp, MXGEFW_DMA_TEST);
  819. /*
  820. * Use non-ndis mcp_slot (eg, 4 bytes total,
  821. * no toeplitz hash value returned. Older firmware will
  822. * not understand this command, but will use the correct
  823. * sized mcp_slot, so we ignore error returns
  824. */
  825. cmd.data0 = MXGEFW_RSS_MCP_SLOT_TYPE_MIN;
  826. (void)myri10ge_send_cmd(mgp, MXGEFW_CMD_SET_RSS_MCP_SLOT_TYPE, &cmd, 0);
  827. /* Now exchange information about interrupts */
  828. bytes = mgp->max_intr_slots * sizeof(*mgp->ss[0].rx_done.entry);
  829. cmd.data0 = (u32) bytes;
  830. status = myri10ge_send_cmd(mgp, MXGEFW_CMD_SET_INTRQ_SIZE, &cmd, 0);
  831. /*
  832. * Even though we already know how many slices are supported
  833. * via myri10ge_probe_slices() MXGEFW_CMD_GET_MAX_RSS_QUEUES
  834. * has magic side effects, and must be called after a reset.
  835. * It must be called prior to calling any RSS related cmds,
  836. * including assigning an interrupt queue for anything but
  837. * slice 0. It must also be called *after*
  838. * MXGEFW_CMD_SET_INTRQ_SIZE, since the intrq size is used by
  839. * the firmware to compute offsets.
  840. */
  841. if (mgp->num_slices > 1) {
  842. /* ask the maximum number of slices it supports */
  843. status = myri10ge_send_cmd(mgp, MXGEFW_CMD_GET_MAX_RSS_QUEUES,
  844. &cmd, 0);
  845. if (status != 0) {
  846. dev_err(&mgp->pdev->dev,
  847. "failed to get number of slices\n");
  848. }
  849. /*
  850. * MXGEFW_CMD_ENABLE_RSS_QUEUES must be called prior
  851. * to setting up the interrupt queue DMA
  852. */
  853. cmd.data0 = mgp->num_slices;
  854. cmd.data1 = MXGEFW_SLICE_INTR_MODE_ONE_PER_SLICE;
  855. if (mgp->dev->real_num_tx_queues > 1)
  856. cmd.data1 |= MXGEFW_SLICE_ENABLE_MULTIPLE_TX_QUEUES;
  857. status = myri10ge_send_cmd(mgp, MXGEFW_CMD_ENABLE_RSS_QUEUES,
  858. &cmd, 0);
  859. /* Firmware older than 1.4.32 only supports multiple
  860. * RX queues, so if we get an error, first retry using a
  861. * single TX queue before giving up */
  862. if (status != 0 && mgp->dev->real_num_tx_queues > 1) {
  863. netif_set_real_num_tx_queues(mgp->dev, 1);
  864. cmd.data0 = mgp->num_slices;
  865. cmd.data1 = MXGEFW_SLICE_INTR_MODE_ONE_PER_SLICE;
  866. status = myri10ge_send_cmd(mgp,
  867. MXGEFW_CMD_ENABLE_RSS_QUEUES,
  868. &cmd, 0);
  869. }
  870. if (status != 0) {
  871. dev_err(&mgp->pdev->dev,
  872. "failed to set number of slices\n");
  873. return status;
  874. }
  875. }
  876. for (i = 0; i < mgp->num_slices; i++) {
  877. ss = &mgp->ss[i];
  878. cmd.data0 = MYRI10GE_LOWPART_TO_U32(ss->rx_done.bus);
  879. cmd.data1 = MYRI10GE_HIGHPART_TO_U32(ss->rx_done.bus);
  880. cmd.data2 = i;
  881. status |= myri10ge_send_cmd(mgp, MXGEFW_CMD_SET_INTRQ_DMA,
  882. &cmd, 0);
  883. }
  884. status |=
  885. myri10ge_send_cmd(mgp, MXGEFW_CMD_GET_IRQ_ACK_OFFSET, &cmd, 0);
  886. for (i = 0; i < mgp->num_slices; i++) {
  887. ss = &mgp->ss[i];
  888. ss->irq_claim =
  889. (__iomem __be32 *) (mgp->sram + cmd.data0 + 8 * i);
  890. }
  891. status |= myri10ge_send_cmd(mgp, MXGEFW_CMD_GET_IRQ_DEASSERT_OFFSET,
  892. &cmd, 0);
  893. mgp->irq_deassert = (__iomem __be32 *) (mgp->sram + cmd.data0);
  894. status |= myri10ge_send_cmd
  895. (mgp, MXGEFW_CMD_GET_INTR_COAL_DELAY_OFFSET, &cmd, 0);
  896. mgp->intr_coal_delay_ptr = (__iomem __be32 *) (mgp->sram + cmd.data0);
  897. if (status != 0) {
  898. dev_err(&mgp->pdev->dev, "failed set interrupt parameters\n");
  899. return status;
  900. }
  901. put_be32(htonl(mgp->intr_coal_delay), mgp->intr_coal_delay_ptr);
  902. #ifdef CONFIG_MYRI10GE_DCA
  903. status = myri10ge_send_cmd(mgp, MXGEFW_CMD_GET_DCA_OFFSET, &cmd, 0);
  904. dca_tag_off = cmd.data0;
  905. for (i = 0; i < mgp->num_slices; i++) {
  906. ss = &mgp->ss[i];
  907. if (status == 0) {
  908. ss->dca_tag = (__iomem __be32 *)
  909. (mgp->sram + dca_tag_off + 4 * i);
  910. } else {
  911. ss->dca_tag = NULL;
  912. }
  913. }
  914. #endif /* CONFIG_MYRI10GE_DCA */
  915. /* reset mcp/driver shared state back to 0 */
  916. mgp->link_changes = 0;
  917. for (i = 0; i < mgp->num_slices; i++) {
  918. ss = &mgp->ss[i];
  919. memset(ss->rx_done.entry, 0, bytes);
  920. ss->tx.req = 0;
  921. ss->tx.done = 0;
  922. ss->tx.pkt_start = 0;
  923. ss->tx.pkt_done = 0;
  924. ss->rx_big.cnt = 0;
  925. ss->rx_small.cnt = 0;
  926. ss->rx_done.idx = 0;
  927. ss->rx_done.cnt = 0;
  928. ss->tx.wake_queue = 0;
  929. ss->tx.stop_queue = 0;
  930. }
  931. status = myri10ge_update_mac_address(mgp, mgp->dev->dev_addr);
  932. myri10ge_change_pause(mgp, mgp->pause);
  933. myri10ge_set_multicast_list(mgp->dev);
  934. return status;
  935. }
  936. #ifdef CONFIG_MYRI10GE_DCA
  937. static int myri10ge_toggle_relaxed(struct pci_dev *pdev, int on)
  938. {
  939. int ret;
  940. u16 ctl;
  941. pcie_capability_read_word(pdev, PCI_EXP_DEVCTL, &ctl);
  942. ret = (ctl & PCI_EXP_DEVCTL_RELAX_EN) >> 4;
  943. if (ret != on) {
  944. ctl &= ~PCI_EXP_DEVCTL_RELAX_EN;
  945. ctl |= (on << 4);
  946. pcie_capability_write_word(pdev, PCI_EXP_DEVCTL, ctl);
  947. }
  948. return ret;
  949. }
  950. static void
  951. myri10ge_write_dca(struct myri10ge_slice_state *ss, int cpu, int tag)
  952. {
  953. ss->cached_dca_tag = tag;
  954. put_be32(htonl(tag), ss->dca_tag);
  955. }
  956. static inline void myri10ge_update_dca(struct myri10ge_slice_state *ss)
  957. {
  958. int cpu = get_cpu();
  959. int tag;
  960. if (cpu != ss->cpu) {
  961. tag = dca3_get_tag(&ss->mgp->pdev->dev, cpu);
  962. if (ss->cached_dca_tag != tag)
  963. myri10ge_write_dca(ss, cpu, tag);
  964. ss->cpu = cpu;
  965. }
  966. put_cpu();
  967. }
  968. static void myri10ge_setup_dca(struct myri10ge_priv *mgp)
  969. {
  970. int err, i;
  971. struct pci_dev *pdev = mgp->pdev;
  972. if (mgp->ss[0].dca_tag == NULL || mgp->dca_enabled)
  973. return;
  974. if (!myri10ge_dca) {
  975. dev_err(&pdev->dev, "dca disabled by administrator\n");
  976. return;
  977. }
  978. err = dca_add_requester(&pdev->dev);
  979. if (err) {
  980. if (err != -ENODEV)
  981. dev_err(&pdev->dev,
  982. "dca_add_requester() failed, err=%d\n", err);
  983. return;
  984. }
  985. mgp->relaxed_order = myri10ge_toggle_relaxed(pdev, 0);
  986. mgp->dca_enabled = 1;
  987. for (i = 0; i < mgp->num_slices; i++) {
  988. mgp->ss[i].cpu = -1;
  989. mgp->ss[i].cached_dca_tag = -1;
  990. myri10ge_update_dca(&mgp->ss[i]);
  991. }
  992. }
  993. static void myri10ge_teardown_dca(struct myri10ge_priv *mgp)
  994. {
  995. struct pci_dev *pdev = mgp->pdev;
  996. if (!mgp->dca_enabled)
  997. return;
  998. mgp->dca_enabled = 0;
  999. if (mgp->relaxed_order)
  1000. myri10ge_toggle_relaxed(pdev, 1);
  1001. dca_remove_requester(&pdev->dev);
  1002. }
  1003. static int myri10ge_notify_dca_device(struct device *dev, void *data)
  1004. {
  1005. struct myri10ge_priv *mgp;
  1006. unsigned long event;
  1007. mgp = dev_get_drvdata(dev);
  1008. event = *(unsigned long *)data;
  1009. if (event == DCA_PROVIDER_ADD)
  1010. myri10ge_setup_dca(mgp);
  1011. else if (event == DCA_PROVIDER_REMOVE)
  1012. myri10ge_teardown_dca(mgp);
  1013. return 0;
  1014. }
  1015. #endif /* CONFIG_MYRI10GE_DCA */
  1016. static inline void
  1017. myri10ge_submit_8rx(struct mcp_kreq_ether_recv __iomem * dst,
  1018. struct mcp_kreq_ether_recv *src)
  1019. {
  1020. __be32 low;
  1021. low = src->addr_low;
  1022. src->addr_low = htonl(DMA_BIT_MASK(32));
  1023. myri10ge_pio_copy(dst, src, 4 * sizeof(*src));
  1024. mb();
  1025. myri10ge_pio_copy(dst + 4, src + 4, 4 * sizeof(*src));
  1026. mb();
  1027. src->addr_low = low;
  1028. put_be32(low, &dst->addr_low);
  1029. mb();
  1030. }
  1031. static inline void myri10ge_vlan_ip_csum(struct sk_buff *skb, __wsum hw_csum)
  1032. {
  1033. struct vlan_hdr *vh = (struct vlan_hdr *)(skb->data);
  1034. if ((skb->protocol == htons(ETH_P_8021Q)) &&
  1035. (vh->h_vlan_encapsulated_proto == htons(ETH_P_IP) ||
  1036. vh->h_vlan_encapsulated_proto == htons(ETH_P_IPV6))) {
  1037. skb->csum = hw_csum;
  1038. skb->ip_summed = CHECKSUM_COMPLETE;
  1039. }
  1040. }
  1041. static inline void
  1042. myri10ge_rx_skb_build(struct sk_buff *skb, u8 * va,
  1043. struct skb_frag_struct *rx_frags, int len, int hlen)
  1044. {
  1045. struct skb_frag_struct *skb_frags;
  1046. skb->len = skb->data_len = len;
  1047. /* attach the page(s) */
  1048. skb_frags = skb_shinfo(skb)->frags;
  1049. while (len > 0) {
  1050. memcpy(skb_frags, rx_frags, sizeof(*skb_frags));
  1051. len -= skb_frag_size(rx_frags);
  1052. skb_frags++;
  1053. rx_frags++;
  1054. skb_shinfo(skb)->nr_frags++;
  1055. }
  1056. /* pskb_may_pull is not available in irq context, but
  1057. * skb_pull() (for ether_pad and eth_type_trans()) requires
  1058. * the beginning of the packet in skb_headlen(), move it
  1059. * manually */
  1060. skb_copy_to_linear_data(skb, va, hlen);
  1061. skb_shinfo(skb)->frags[0].page_offset += hlen;
  1062. skb_frag_size_sub(&skb_shinfo(skb)->frags[0], hlen);
  1063. skb->data_len -= hlen;
  1064. skb->tail += hlen;
  1065. skb_pull(skb, MXGEFW_PAD);
  1066. }
  1067. static void
  1068. myri10ge_alloc_rx_pages(struct myri10ge_priv *mgp, struct myri10ge_rx_buf *rx,
  1069. int bytes, int watchdog)
  1070. {
  1071. struct page *page;
  1072. int idx;
  1073. #if MYRI10GE_ALLOC_SIZE > 4096
  1074. int end_offset;
  1075. #endif
  1076. if (unlikely(rx->watchdog_needed && !watchdog))
  1077. return;
  1078. /* try to refill entire ring */
  1079. while (rx->fill_cnt != (rx->cnt + rx->mask + 1)) {
  1080. idx = rx->fill_cnt & rx->mask;
  1081. if (rx->page_offset + bytes <= MYRI10GE_ALLOC_SIZE) {
  1082. /* we can use part of previous page */
  1083. get_page(rx->page);
  1084. } else {
  1085. /* we need a new page */
  1086. page =
  1087. alloc_pages(GFP_ATOMIC | __GFP_COMP,
  1088. MYRI10GE_ALLOC_ORDER);
  1089. if (unlikely(page == NULL)) {
  1090. if (rx->fill_cnt - rx->cnt < 16)
  1091. rx->watchdog_needed = 1;
  1092. return;
  1093. }
  1094. rx->page = page;
  1095. rx->page_offset = 0;
  1096. rx->bus = pci_map_page(mgp->pdev, page, 0,
  1097. MYRI10GE_ALLOC_SIZE,
  1098. PCI_DMA_FROMDEVICE);
  1099. }
  1100. rx->info[idx].page = rx->page;
  1101. rx->info[idx].page_offset = rx->page_offset;
  1102. /* note that this is the address of the start of the
  1103. * page */
  1104. dma_unmap_addr_set(&rx->info[idx], bus, rx->bus);
  1105. rx->shadow[idx].addr_low =
  1106. htonl(MYRI10GE_LOWPART_TO_U32(rx->bus) + rx->page_offset);
  1107. rx->shadow[idx].addr_high =
  1108. htonl(MYRI10GE_HIGHPART_TO_U32(rx->bus));
  1109. /* start next packet on a cacheline boundary */
  1110. rx->page_offset += SKB_DATA_ALIGN(bytes);
  1111. #if MYRI10GE_ALLOC_SIZE > 4096
  1112. /* don't cross a 4KB boundary */
  1113. end_offset = rx->page_offset + bytes - 1;
  1114. if ((unsigned)(rx->page_offset ^ end_offset) > 4095)
  1115. rx->page_offset = end_offset & ~4095;
  1116. #endif
  1117. rx->fill_cnt++;
  1118. /* copy 8 descriptors to the firmware at a time */
  1119. if ((idx & 7) == 7) {
  1120. myri10ge_submit_8rx(&rx->lanai[idx - 7],
  1121. &rx->shadow[idx - 7]);
  1122. }
  1123. }
  1124. }
  1125. static inline void
  1126. myri10ge_unmap_rx_page(struct pci_dev *pdev,
  1127. struct myri10ge_rx_buffer_state *info, int bytes)
  1128. {
  1129. /* unmap the recvd page if we're the only or last user of it */
  1130. if (bytes >= MYRI10GE_ALLOC_SIZE / 2 ||
  1131. (info->page_offset + 2 * bytes) > MYRI10GE_ALLOC_SIZE) {
  1132. pci_unmap_page(pdev, (dma_unmap_addr(info, bus)
  1133. & ~(MYRI10GE_ALLOC_SIZE - 1)),
  1134. MYRI10GE_ALLOC_SIZE, PCI_DMA_FROMDEVICE);
  1135. }
  1136. }
  1137. #define MYRI10GE_HLEN 64 /* The number of bytes to copy from a
  1138. * page into an skb */
  1139. static inline int
  1140. myri10ge_rx_done(struct myri10ge_slice_state *ss, int len, __wsum csum,
  1141. bool lro_enabled)
  1142. {
  1143. struct myri10ge_priv *mgp = ss->mgp;
  1144. struct sk_buff *skb;
  1145. struct skb_frag_struct rx_frags[MYRI10GE_MAX_FRAGS_PER_FRAME];
  1146. struct myri10ge_rx_buf *rx;
  1147. int i, idx, hlen, remainder, bytes;
  1148. struct pci_dev *pdev = mgp->pdev;
  1149. struct net_device *dev = mgp->dev;
  1150. u8 *va;
  1151. if (len <= mgp->small_bytes) {
  1152. rx = &ss->rx_small;
  1153. bytes = mgp->small_bytes;
  1154. } else {
  1155. rx = &ss->rx_big;
  1156. bytes = mgp->big_bytes;
  1157. }
  1158. len += MXGEFW_PAD;
  1159. idx = rx->cnt & rx->mask;
  1160. va = page_address(rx->info[idx].page) + rx->info[idx].page_offset;
  1161. prefetch(va);
  1162. /* Fill skb_frag_struct(s) with data from our receive */
  1163. for (i = 0, remainder = len; remainder > 0; i++) {
  1164. myri10ge_unmap_rx_page(pdev, &rx->info[idx], bytes);
  1165. __skb_frag_set_page(&rx_frags[i], rx->info[idx].page);
  1166. rx_frags[i].page_offset = rx->info[idx].page_offset;
  1167. if (remainder < MYRI10GE_ALLOC_SIZE)
  1168. skb_frag_size_set(&rx_frags[i], remainder);
  1169. else
  1170. skb_frag_size_set(&rx_frags[i], MYRI10GE_ALLOC_SIZE);
  1171. rx->cnt++;
  1172. idx = rx->cnt & rx->mask;
  1173. remainder -= MYRI10GE_ALLOC_SIZE;
  1174. }
  1175. if (lro_enabled) {
  1176. rx_frags[0].page_offset += MXGEFW_PAD;
  1177. skb_frag_size_sub(&rx_frags[0], MXGEFW_PAD);
  1178. len -= MXGEFW_PAD;
  1179. lro_receive_frags(&ss->rx_done.lro_mgr, rx_frags,
  1180. /* opaque, will come back in get_frag_header */
  1181. len, len,
  1182. (void *)(__force unsigned long)csum, csum);
  1183. return 1;
  1184. }
  1185. hlen = MYRI10GE_HLEN > len ? len : MYRI10GE_HLEN;
  1186. /* allocate an skb to attach the page(s) to. This is done
  1187. * after trying LRO, so as to avoid skb allocation overheads */
  1188. skb = netdev_alloc_skb(dev, MYRI10GE_HLEN + 16);
  1189. if (unlikely(skb == NULL)) {
  1190. ss->stats.rx_dropped++;
  1191. do {
  1192. i--;
  1193. __skb_frag_unref(&rx_frags[i]);
  1194. } while (i != 0);
  1195. return 0;
  1196. }
  1197. /* Attach the pages to the skb, and trim off any padding */
  1198. myri10ge_rx_skb_build(skb, va, rx_frags, len, hlen);
  1199. if (skb_frag_size(&skb_shinfo(skb)->frags[0]) <= 0) {
  1200. skb_frag_unref(skb, 0);
  1201. skb_shinfo(skb)->nr_frags = 0;
  1202. } else {
  1203. skb->truesize += bytes * skb_shinfo(skb)->nr_frags;
  1204. }
  1205. skb->protocol = eth_type_trans(skb, dev);
  1206. skb_record_rx_queue(skb, ss - &mgp->ss[0]);
  1207. if (dev->features & NETIF_F_RXCSUM) {
  1208. if ((skb->protocol == htons(ETH_P_IP)) ||
  1209. (skb->protocol == htons(ETH_P_IPV6))) {
  1210. skb->csum = csum;
  1211. skb->ip_summed = CHECKSUM_COMPLETE;
  1212. } else
  1213. myri10ge_vlan_ip_csum(skb, csum);
  1214. }
  1215. netif_receive_skb(skb);
  1216. return 1;
  1217. }
  1218. static inline void
  1219. myri10ge_tx_done(struct myri10ge_slice_state *ss, int mcp_index)
  1220. {
  1221. struct pci_dev *pdev = ss->mgp->pdev;
  1222. struct myri10ge_tx_buf *tx = &ss->tx;
  1223. struct netdev_queue *dev_queue;
  1224. struct sk_buff *skb;
  1225. int idx, len;
  1226. while (tx->pkt_done != mcp_index) {
  1227. idx = tx->done & tx->mask;
  1228. skb = tx->info[idx].skb;
  1229. /* Mark as free */
  1230. tx->info[idx].skb = NULL;
  1231. if (tx->info[idx].last) {
  1232. tx->pkt_done++;
  1233. tx->info[idx].last = 0;
  1234. }
  1235. tx->done++;
  1236. len = dma_unmap_len(&tx->info[idx], len);
  1237. dma_unmap_len_set(&tx->info[idx], len, 0);
  1238. if (skb) {
  1239. ss->stats.tx_bytes += skb->len;
  1240. ss->stats.tx_packets++;
  1241. dev_kfree_skb_irq(skb);
  1242. if (len)
  1243. pci_unmap_single(pdev,
  1244. dma_unmap_addr(&tx->info[idx],
  1245. bus), len,
  1246. PCI_DMA_TODEVICE);
  1247. } else {
  1248. if (len)
  1249. pci_unmap_page(pdev,
  1250. dma_unmap_addr(&tx->info[idx],
  1251. bus), len,
  1252. PCI_DMA_TODEVICE);
  1253. }
  1254. }
  1255. dev_queue = netdev_get_tx_queue(ss->dev, ss - ss->mgp->ss);
  1256. /*
  1257. * Make a minimal effort to prevent the NIC from polling an
  1258. * idle tx queue. If we can't get the lock we leave the queue
  1259. * active. In this case, either a thread was about to start
  1260. * using the queue anyway, or we lost a race and the NIC will
  1261. * waste some of its resources polling an inactive queue for a
  1262. * while.
  1263. */
  1264. if ((ss->mgp->dev->real_num_tx_queues > 1) &&
  1265. __netif_tx_trylock(dev_queue)) {
  1266. if (tx->req == tx->done) {
  1267. tx->queue_active = 0;
  1268. put_be32(htonl(1), tx->send_stop);
  1269. mb();
  1270. mmiowb();
  1271. }
  1272. __netif_tx_unlock(dev_queue);
  1273. }
  1274. /* start the queue if we've stopped it */
  1275. if (netif_tx_queue_stopped(dev_queue) &&
  1276. tx->req - tx->done < (tx->mask >> 1) &&
  1277. ss->mgp->running == MYRI10GE_ETH_RUNNING) {
  1278. tx->wake_queue++;
  1279. netif_tx_wake_queue(dev_queue);
  1280. }
  1281. }
  1282. static inline int
  1283. myri10ge_clean_rx_done(struct myri10ge_slice_state *ss, int budget)
  1284. {
  1285. struct myri10ge_rx_done *rx_done = &ss->rx_done;
  1286. struct myri10ge_priv *mgp = ss->mgp;
  1287. unsigned long rx_bytes = 0;
  1288. unsigned long rx_packets = 0;
  1289. unsigned long rx_ok;
  1290. int idx = rx_done->idx;
  1291. int cnt = rx_done->cnt;
  1292. int work_done = 0;
  1293. u16 length;
  1294. __wsum checksum;
  1295. /*
  1296. * Prevent compiler from generating more than one ->features memory
  1297. * access to avoid theoretical race condition with functions that
  1298. * change NETIF_F_LRO flag at runtime.
  1299. */
  1300. bool lro_enabled = !!(ACCESS_ONCE(mgp->dev->features) & NETIF_F_LRO);
  1301. while (rx_done->entry[idx].length != 0 && work_done < budget) {
  1302. length = ntohs(rx_done->entry[idx].length);
  1303. rx_done->entry[idx].length = 0;
  1304. checksum = csum_unfold(rx_done->entry[idx].checksum);
  1305. rx_ok = myri10ge_rx_done(ss, length, checksum, lro_enabled);
  1306. rx_packets += rx_ok;
  1307. rx_bytes += rx_ok * (unsigned long)length;
  1308. cnt++;
  1309. idx = cnt & (mgp->max_intr_slots - 1);
  1310. work_done++;
  1311. }
  1312. rx_done->idx = idx;
  1313. rx_done->cnt = cnt;
  1314. ss->stats.rx_packets += rx_packets;
  1315. ss->stats.rx_bytes += rx_bytes;
  1316. if (lro_enabled)
  1317. lro_flush_all(&rx_done->lro_mgr);
  1318. /* restock receive rings if needed */
  1319. if (ss->rx_small.fill_cnt - ss->rx_small.cnt < myri10ge_fill_thresh)
  1320. myri10ge_alloc_rx_pages(mgp, &ss->rx_small,
  1321. mgp->small_bytes + MXGEFW_PAD, 0);
  1322. if (ss->rx_big.fill_cnt - ss->rx_big.cnt < myri10ge_fill_thresh)
  1323. myri10ge_alloc_rx_pages(mgp, &ss->rx_big, mgp->big_bytes, 0);
  1324. return work_done;
  1325. }
  1326. static inline void myri10ge_check_statblock(struct myri10ge_priv *mgp)
  1327. {
  1328. struct mcp_irq_data *stats = mgp->ss[0].fw_stats;
  1329. if (unlikely(stats->stats_updated)) {
  1330. unsigned link_up = ntohl(stats->link_up);
  1331. if (mgp->link_state != link_up) {
  1332. mgp->link_state = link_up;
  1333. if (mgp->link_state == MXGEFW_LINK_UP) {
  1334. netif_info(mgp, link, mgp->dev, "link up\n");
  1335. netif_carrier_on(mgp->dev);
  1336. mgp->link_changes++;
  1337. } else {
  1338. netif_info(mgp, link, mgp->dev, "link %s\n",
  1339. (link_up == MXGEFW_LINK_MYRINET ?
  1340. "mismatch (Myrinet detected)" :
  1341. "down"));
  1342. netif_carrier_off(mgp->dev);
  1343. mgp->link_changes++;
  1344. }
  1345. }
  1346. if (mgp->rdma_tags_available !=
  1347. ntohl(stats->rdma_tags_available)) {
  1348. mgp->rdma_tags_available =
  1349. ntohl(stats->rdma_tags_available);
  1350. netdev_warn(mgp->dev, "RDMA timed out! %d tags left\n",
  1351. mgp->rdma_tags_available);
  1352. }
  1353. mgp->down_cnt += stats->link_down;
  1354. if (stats->link_down)
  1355. wake_up(&mgp->down_wq);
  1356. }
  1357. }
  1358. static int myri10ge_poll(struct napi_struct *napi, int budget)
  1359. {
  1360. struct myri10ge_slice_state *ss =
  1361. container_of(napi, struct myri10ge_slice_state, napi);
  1362. int work_done;
  1363. #ifdef CONFIG_MYRI10GE_DCA
  1364. if (ss->mgp->dca_enabled)
  1365. myri10ge_update_dca(ss);
  1366. #endif
  1367. /* process as many rx events as NAPI will allow */
  1368. work_done = myri10ge_clean_rx_done(ss, budget);
  1369. if (work_done < budget) {
  1370. napi_complete(napi);
  1371. put_be32(htonl(3), ss->irq_claim);
  1372. }
  1373. return work_done;
  1374. }
  1375. static irqreturn_t myri10ge_intr(int irq, void *arg)
  1376. {
  1377. struct myri10ge_slice_state *ss = arg;
  1378. struct myri10ge_priv *mgp = ss->mgp;
  1379. struct mcp_irq_data *stats = ss->fw_stats;
  1380. struct myri10ge_tx_buf *tx = &ss->tx;
  1381. u32 send_done_count;
  1382. int i;
  1383. /* an interrupt on a non-zero receive-only slice is implicitly
  1384. * valid since MSI-X irqs are not shared */
  1385. if ((mgp->dev->real_num_tx_queues == 1) && (ss != mgp->ss)) {
  1386. napi_schedule(&ss->napi);
  1387. return IRQ_HANDLED;
  1388. }
  1389. /* make sure it is our IRQ, and that the DMA has finished */
  1390. if (unlikely(!stats->valid))
  1391. return IRQ_NONE;
  1392. /* low bit indicates receives are present, so schedule
  1393. * napi poll handler */
  1394. if (stats->valid & 1)
  1395. napi_schedule(&ss->napi);
  1396. if (!mgp->msi_enabled && !mgp->msix_enabled) {
  1397. put_be32(0, mgp->irq_deassert);
  1398. if (!myri10ge_deassert_wait)
  1399. stats->valid = 0;
  1400. mb();
  1401. } else
  1402. stats->valid = 0;
  1403. /* Wait for IRQ line to go low, if using INTx */
  1404. i = 0;
  1405. while (1) {
  1406. i++;
  1407. /* check for transmit completes and receives */
  1408. send_done_count = ntohl(stats->send_done_count);
  1409. if (send_done_count != tx->pkt_done)
  1410. myri10ge_tx_done(ss, (int)send_done_count);
  1411. if (unlikely(i > myri10ge_max_irq_loops)) {
  1412. netdev_warn(mgp->dev, "irq stuck?\n");
  1413. stats->valid = 0;
  1414. schedule_work(&mgp->watchdog_work);
  1415. }
  1416. if (likely(stats->valid == 0))
  1417. break;
  1418. cpu_relax();
  1419. barrier();
  1420. }
  1421. /* Only slice 0 updates stats */
  1422. if (ss == mgp->ss)
  1423. myri10ge_check_statblock(mgp);
  1424. put_be32(htonl(3), ss->irq_claim + 1);
  1425. return IRQ_HANDLED;
  1426. }
  1427. static int
  1428. myri10ge_get_settings(struct net_device *netdev, struct ethtool_cmd *cmd)
  1429. {
  1430. struct myri10ge_priv *mgp = netdev_priv(netdev);
  1431. char *ptr;
  1432. int i;
  1433. cmd->autoneg = AUTONEG_DISABLE;
  1434. ethtool_cmd_speed_set(cmd, SPEED_10000);
  1435. cmd->duplex = DUPLEX_FULL;
  1436. /*
  1437. * parse the product code to deterimine the interface type
  1438. * (CX4, XFP, Quad Ribbon Fiber) by looking at the character
  1439. * after the 3rd dash in the driver's cached copy of the
  1440. * EEPROM's product code string.
  1441. */
  1442. ptr = mgp->product_code_string;
  1443. if (ptr == NULL) {
  1444. netdev_err(netdev, "Missing product code\n");
  1445. return 0;
  1446. }
  1447. for (i = 0; i < 3; i++, ptr++) {
  1448. ptr = strchr(ptr, '-');
  1449. if (ptr == NULL) {
  1450. netdev_err(netdev, "Invalid product code %s\n",
  1451. mgp->product_code_string);
  1452. return 0;
  1453. }
  1454. }
  1455. if (*ptr == '2')
  1456. ptr++;
  1457. if (*ptr == 'R' || *ptr == 'Q' || *ptr == 'S') {
  1458. /* We've found either an XFP, quad ribbon fiber, or SFP+ */
  1459. cmd->port = PORT_FIBRE;
  1460. cmd->supported |= SUPPORTED_FIBRE;
  1461. cmd->advertising |= ADVERTISED_FIBRE;
  1462. } else {
  1463. cmd->port = PORT_OTHER;
  1464. }
  1465. if (*ptr == 'R' || *ptr == 'S')
  1466. cmd->transceiver = XCVR_EXTERNAL;
  1467. else
  1468. cmd->transceiver = XCVR_INTERNAL;
  1469. return 0;
  1470. }
  1471. static void
  1472. myri10ge_get_drvinfo(struct net_device *netdev, struct ethtool_drvinfo *info)
  1473. {
  1474. struct myri10ge_priv *mgp = netdev_priv(netdev);
  1475. strlcpy(info->driver, "myri10ge", sizeof(info->driver));
  1476. strlcpy(info->version, MYRI10GE_VERSION_STR, sizeof(info->version));
  1477. strlcpy(info->fw_version, mgp->fw_version, sizeof(info->fw_version));
  1478. strlcpy(info->bus_info, pci_name(mgp->pdev), sizeof(info->bus_info));
  1479. }
  1480. static int
  1481. myri10ge_get_coalesce(struct net_device *netdev, struct ethtool_coalesce *coal)
  1482. {
  1483. struct myri10ge_priv *mgp = netdev_priv(netdev);
  1484. coal->rx_coalesce_usecs = mgp->intr_coal_delay;
  1485. return 0;
  1486. }
  1487. static int
  1488. myri10ge_set_coalesce(struct net_device *netdev, struct ethtool_coalesce *coal)
  1489. {
  1490. struct myri10ge_priv *mgp = netdev_priv(netdev);
  1491. mgp->intr_coal_delay = coal->rx_coalesce_usecs;
  1492. put_be32(htonl(mgp->intr_coal_delay), mgp->intr_coal_delay_ptr);
  1493. return 0;
  1494. }
  1495. static void
  1496. myri10ge_get_pauseparam(struct net_device *netdev,
  1497. struct ethtool_pauseparam *pause)
  1498. {
  1499. struct myri10ge_priv *mgp = netdev_priv(netdev);
  1500. pause->autoneg = 0;
  1501. pause->rx_pause = mgp->pause;
  1502. pause->tx_pause = mgp->pause;
  1503. }
  1504. static int
  1505. myri10ge_set_pauseparam(struct net_device *netdev,
  1506. struct ethtool_pauseparam *pause)
  1507. {
  1508. struct myri10ge_priv *mgp = netdev_priv(netdev);
  1509. if (pause->tx_pause != mgp->pause)
  1510. return myri10ge_change_pause(mgp, pause->tx_pause);
  1511. if (pause->rx_pause != mgp->pause)
  1512. return myri10ge_change_pause(mgp, pause->rx_pause);
  1513. if (pause->autoneg != 0)
  1514. return -EINVAL;
  1515. return 0;
  1516. }
  1517. static void
  1518. myri10ge_get_ringparam(struct net_device *netdev,
  1519. struct ethtool_ringparam *ring)
  1520. {
  1521. struct myri10ge_priv *mgp = netdev_priv(netdev);
  1522. ring->rx_mini_max_pending = mgp->ss[0].rx_small.mask + 1;
  1523. ring->rx_max_pending = mgp->ss[0].rx_big.mask + 1;
  1524. ring->rx_jumbo_max_pending = 0;
  1525. ring->tx_max_pending = mgp->ss[0].tx.mask + 1;
  1526. ring->rx_mini_pending = ring->rx_mini_max_pending;
  1527. ring->rx_pending = ring->rx_max_pending;
  1528. ring->rx_jumbo_pending = ring->rx_jumbo_max_pending;
  1529. ring->tx_pending = ring->tx_max_pending;
  1530. }
  1531. static const char myri10ge_gstrings_main_stats[][ETH_GSTRING_LEN] = {
  1532. "rx_packets", "tx_packets", "rx_bytes", "tx_bytes", "rx_errors",
  1533. "tx_errors", "rx_dropped", "tx_dropped", "multicast", "collisions",
  1534. "rx_length_errors", "rx_over_errors", "rx_crc_errors",
  1535. "rx_frame_errors", "rx_fifo_errors", "rx_missed_errors",
  1536. "tx_aborted_errors", "tx_carrier_errors", "tx_fifo_errors",
  1537. "tx_heartbeat_errors", "tx_window_errors",
  1538. /* device-specific stats */
  1539. "tx_boundary", "WC", "irq", "MSI", "MSIX",
  1540. "read_dma_bw_MBs", "write_dma_bw_MBs", "read_write_dma_bw_MBs",
  1541. "serial_number", "watchdog_resets",
  1542. #ifdef CONFIG_MYRI10GE_DCA
  1543. "dca_capable_firmware", "dca_device_present",
  1544. #endif
  1545. "link_changes", "link_up", "dropped_link_overflow",
  1546. "dropped_link_error_or_filtered",
  1547. "dropped_pause", "dropped_bad_phy", "dropped_bad_crc32",
  1548. "dropped_unicast_filtered", "dropped_multicast_filtered",
  1549. "dropped_runt", "dropped_overrun", "dropped_no_small_buffer",
  1550. "dropped_no_big_buffer"
  1551. };
  1552. static const char myri10ge_gstrings_slice_stats[][ETH_GSTRING_LEN] = {
  1553. "----------- slice ---------",
  1554. "tx_pkt_start", "tx_pkt_done", "tx_req", "tx_done",
  1555. "rx_small_cnt", "rx_big_cnt",
  1556. "wake_queue", "stop_queue", "tx_linearized",
  1557. "LRO aggregated", "LRO flushed", "LRO avg aggr", "LRO no_desc",
  1558. };
  1559. #define MYRI10GE_NET_STATS_LEN 21
  1560. #define MYRI10GE_MAIN_STATS_LEN ARRAY_SIZE(myri10ge_gstrings_main_stats)
  1561. #define MYRI10GE_SLICE_STATS_LEN ARRAY_SIZE(myri10ge_gstrings_slice_stats)
  1562. static void
  1563. myri10ge_get_strings(struct net_device *netdev, u32 stringset, u8 * data)
  1564. {
  1565. struct myri10ge_priv *mgp = netdev_priv(netdev);
  1566. int i;
  1567. switch (stringset) {
  1568. case ETH_SS_STATS:
  1569. memcpy(data, *myri10ge_gstrings_main_stats,
  1570. sizeof(myri10ge_gstrings_main_stats));
  1571. data += sizeof(myri10ge_gstrings_main_stats);
  1572. for (i = 0; i < mgp->num_slices; i++) {
  1573. memcpy(data, *myri10ge_gstrings_slice_stats,
  1574. sizeof(myri10ge_gstrings_slice_stats));
  1575. data += sizeof(myri10ge_gstrings_slice_stats);
  1576. }
  1577. break;
  1578. }
  1579. }
  1580. static int myri10ge_get_sset_count(struct net_device *netdev, int sset)
  1581. {
  1582. struct myri10ge_priv *mgp = netdev_priv(netdev);
  1583. switch (sset) {
  1584. case ETH_SS_STATS:
  1585. return MYRI10GE_MAIN_STATS_LEN +
  1586. mgp->num_slices * MYRI10GE_SLICE_STATS_LEN;
  1587. default:
  1588. return -EOPNOTSUPP;
  1589. }
  1590. }
  1591. static void
  1592. myri10ge_get_ethtool_stats(struct net_device *netdev,
  1593. struct ethtool_stats *stats, u64 * data)
  1594. {
  1595. struct myri10ge_priv *mgp = netdev_priv(netdev);
  1596. struct myri10ge_slice_state *ss;
  1597. struct rtnl_link_stats64 link_stats;
  1598. int slice;
  1599. int i;
  1600. /* force stats update */
  1601. memset(&link_stats, 0, sizeof(link_stats));
  1602. (void)myri10ge_get_stats(netdev, &link_stats);
  1603. for (i = 0; i < MYRI10GE_NET_STATS_LEN; i++)
  1604. data[i] = ((u64 *)&link_stats)[i];
  1605. data[i++] = (unsigned int)mgp->tx_boundary;
  1606. data[i++] = (unsigned int)mgp->wc_enabled;
  1607. data[i++] = (unsigned int)mgp->pdev->irq;
  1608. data[i++] = (unsigned int)mgp->msi_enabled;
  1609. data[i++] = (unsigned int)mgp->msix_enabled;
  1610. data[i++] = (unsigned int)mgp->read_dma;
  1611. data[i++] = (unsigned int)mgp->write_dma;
  1612. data[i++] = (unsigned int)mgp->read_write_dma;
  1613. data[i++] = (unsigned int)mgp->serial_number;
  1614. data[i++] = (unsigned int)mgp->watchdog_resets;
  1615. #ifdef CONFIG_MYRI10GE_DCA
  1616. data[i++] = (unsigned int)(mgp->ss[0].dca_tag != NULL);
  1617. data[i++] = (unsigned int)(mgp->dca_enabled);
  1618. #endif
  1619. data[i++] = (unsigned int)mgp->link_changes;
  1620. /* firmware stats are useful only in the first slice */
  1621. ss = &mgp->ss[0];
  1622. data[i++] = (unsigned int)ntohl(ss->fw_stats->link_up);
  1623. data[i++] = (unsigned int)ntohl(ss->fw_stats->dropped_link_overflow);
  1624. data[i++] =
  1625. (unsigned int)ntohl(ss->fw_stats->dropped_link_error_or_filtered);
  1626. data[i++] = (unsigned int)ntohl(ss->fw_stats->dropped_pause);
  1627. data[i++] = (unsigned int)ntohl(ss->fw_stats->dropped_bad_phy);
  1628. data[i++] = (unsigned int)ntohl(ss->fw_stats->dropped_bad_crc32);
  1629. data[i++] = (unsigned int)ntohl(ss->fw_stats->dropped_unicast_filtered);
  1630. data[i++] =
  1631. (unsigned int)ntohl(ss->fw_stats->dropped_multicast_filtered);
  1632. data[i++] = (unsigned int)ntohl(ss->fw_stats->dropped_runt);
  1633. data[i++] = (unsigned int)ntohl(ss->fw_stats->dropped_overrun);
  1634. data[i++] = (unsigned int)ntohl(ss->fw_stats->dropped_no_small_buffer);
  1635. data[i++] = (unsigned int)ntohl(ss->fw_stats->dropped_no_big_buffer);
  1636. for (slice = 0; slice < mgp->num_slices; slice++) {
  1637. ss = &mgp->ss[slice];
  1638. data[i++] = slice;
  1639. data[i++] = (unsigned int)ss->tx.pkt_start;
  1640. data[i++] = (unsigned int)ss->tx.pkt_done;
  1641. data[i++] = (unsigned int)ss->tx.req;
  1642. data[i++] = (unsigned int)ss->tx.done;
  1643. data[i++] = (unsigned int)ss->rx_small.cnt;
  1644. data[i++] = (unsigned int)ss->rx_big.cnt;
  1645. data[i++] = (unsigned int)ss->tx.wake_queue;
  1646. data[i++] = (unsigned int)ss->tx.stop_queue;
  1647. data[i++] = (unsigned int)ss->tx.linearized;
  1648. data[i++] = ss->rx_done.lro_mgr.stats.aggregated;
  1649. data[i++] = ss->rx_done.lro_mgr.stats.flushed;
  1650. if (ss->rx_done.lro_mgr.stats.flushed)
  1651. data[i++] = ss->rx_done.lro_mgr.stats.aggregated /
  1652. ss->rx_done.lro_mgr.stats.flushed;
  1653. else
  1654. data[i++] = 0;
  1655. data[i++] = ss->rx_done.lro_mgr.stats.no_desc;
  1656. }
  1657. }
  1658. static void myri10ge_set_msglevel(struct net_device *netdev, u32 value)
  1659. {
  1660. struct myri10ge_priv *mgp = netdev_priv(netdev);
  1661. mgp->msg_enable = value;
  1662. }
  1663. static u32 myri10ge_get_msglevel(struct net_device *netdev)
  1664. {
  1665. struct myri10ge_priv *mgp = netdev_priv(netdev);
  1666. return mgp->msg_enable;
  1667. }
  1668. /*
  1669. * Use a low-level command to change the LED behavior. Rather than
  1670. * blinking (which is the normal case), when identify is used, the
  1671. * yellow LED turns solid.
  1672. */
  1673. static int myri10ge_led(struct myri10ge_priv *mgp, int on)
  1674. {
  1675. struct mcp_gen_header *hdr;
  1676. struct device *dev = &mgp->pdev->dev;
  1677. size_t hdr_off, pattern_off, hdr_len;
  1678. u32 pattern = 0xfffffffe;
  1679. /* find running firmware header */
  1680. hdr_off = swab32(readl(mgp->sram + MCP_HEADER_PTR_OFFSET));
  1681. if ((hdr_off & 3) || hdr_off + sizeof(*hdr) > mgp->sram_size) {
  1682. dev_err(dev, "Running firmware has bad header offset (%d)\n",
  1683. (int)hdr_off);
  1684. return -EIO;
  1685. }
  1686. hdr_len = swab32(readl(mgp->sram + hdr_off +
  1687. offsetof(struct mcp_gen_header, header_length)));
  1688. pattern_off = hdr_off + offsetof(struct mcp_gen_header, led_pattern);
  1689. if (pattern_off >= (hdr_len + hdr_off)) {
  1690. dev_info(dev, "Firmware does not support LED identification\n");
  1691. return -EINVAL;
  1692. }
  1693. if (!on)
  1694. pattern = swab32(readl(mgp->sram + pattern_off + 4));
  1695. writel(htonl(pattern), mgp->sram + pattern_off);
  1696. return 0;
  1697. }
  1698. static int
  1699. myri10ge_phys_id(struct net_device *netdev, enum ethtool_phys_id_state state)
  1700. {
  1701. struct myri10ge_priv *mgp = netdev_priv(netdev);
  1702. int rc;
  1703. switch (state) {
  1704. case ETHTOOL_ID_ACTIVE:
  1705. rc = myri10ge_led(mgp, 1);
  1706. break;
  1707. case ETHTOOL_ID_INACTIVE:
  1708. rc = myri10ge_led(mgp, 0);
  1709. break;
  1710. default:
  1711. rc = -EINVAL;
  1712. }
  1713. return rc;
  1714. }
  1715. static const struct ethtool_ops myri10ge_ethtool_ops = {
  1716. .get_settings = myri10ge_get_settings,
  1717. .get_drvinfo = myri10ge_get_drvinfo,
  1718. .get_coalesce = myri10ge_get_coalesce,
  1719. .set_coalesce = myri10ge_set_coalesce,
  1720. .get_pauseparam = myri10ge_get_pauseparam,
  1721. .set_pauseparam = myri10ge_set_pauseparam,
  1722. .get_ringparam = myri10ge_get_ringparam,
  1723. .get_link = ethtool_op_get_link,
  1724. .get_strings = myri10ge_get_strings,
  1725. .get_sset_count = myri10ge_get_sset_count,
  1726. .get_ethtool_stats = myri10ge_get_ethtool_stats,
  1727. .set_msglevel = myri10ge_set_msglevel,
  1728. .get_msglevel = myri10ge_get_msglevel,
  1729. .set_phys_id = myri10ge_phys_id,
  1730. };
  1731. static int myri10ge_allocate_rings(struct myri10ge_slice_state *ss)
  1732. {
  1733. struct myri10ge_priv *mgp = ss->mgp;
  1734. struct myri10ge_cmd cmd;
  1735. struct net_device *dev = mgp->dev;
  1736. int tx_ring_size, rx_ring_size;
  1737. int tx_ring_entries, rx_ring_entries;
  1738. int i, slice, status;
  1739. size_t bytes;
  1740. /* get ring sizes */
  1741. slice = ss - mgp->ss;
  1742. cmd.data0 = slice;
  1743. status = myri10ge_send_cmd(mgp, MXGEFW_CMD_GET_SEND_RING_SIZE, &cmd, 0);
  1744. tx_ring_size = cmd.data0;
  1745. cmd.data0 = slice;
  1746. status |= myri10ge_send_cmd(mgp, MXGEFW_CMD_GET_RX_RING_SIZE, &cmd, 0);
  1747. if (status != 0)
  1748. return status;
  1749. rx_ring_size = cmd.data0;
  1750. tx_ring_entries = tx_ring_size / sizeof(struct mcp_kreq_ether_send);
  1751. rx_ring_entries = rx_ring_size / sizeof(struct mcp_dma_addr);
  1752. ss->tx.mask = tx_ring_entries - 1;
  1753. ss->rx_small.mask = ss->rx_big.mask = rx_ring_entries - 1;
  1754. status = -ENOMEM;
  1755. /* allocate the host shadow rings */
  1756. bytes = 8 + (MYRI10GE_MAX_SEND_DESC_TSO + 4)
  1757. * sizeof(*ss->tx.req_list);
  1758. ss->tx.req_bytes = kzalloc(bytes, GFP_KERNEL);
  1759. if (ss->tx.req_bytes == NULL)
  1760. goto abort_with_nothing;
  1761. /* ensure req_list entries are aligned to 8 bytes */
  1762. ss->tx.req_list = (struct mcp_kreq_ether_send *)
  1763. ALIGN((unsigned long)ss->tx.req_bytes, 8);
  1764. ss->tx.queue_active = 0;
  1765. bytes = rx_ring_entries * sizeof(*ss->rx_small.shadow);
  1766. ss->rx_small.shadow = kzalloc(bytes, GFP_KERNEL);
  1767. if (ss->rx_small.shadow == NULL)
  1768. goto abort_with_tx_req_bytes;
  1769. bytes = rx_ring_entries * sizeof(*ss->rx_big.shadow);
  1770. ss->rx_big.shadow = kzalloc(bytes, GFP_KERNEL);
  1771. if (ss->rx_big.shadow == NULL)
  1772. goto abort_with_rx_small_shadow;
  1773. /* allocate the host info rings */
  1774. bytes = tx_ring_entries * sizeof(*ss->tx.info);
  1775. ss->tx.info = kzalloc(bytes, GFP_KERNEL);
  1776. if (ss->tx.info == NULL)
  1777. goto abort_with_rx_big_shadow;
  1778. bytes = rx_ring_entries * sizeof(*ss->rx_small.info);
  1779. ss->rx_small.info = kzalloc(bytes, GFP_KERNEL);
  1780. if (ss->rx_small.info == NULL)
  1781. goto abort_with_tx_info;
  1782. bytes = rx_ring_entries * sizeof(*ss->rx_big.info);
  1783. ss->rx_big.info = kzalloc(bytes, GFP_KERNEL);
  1784. if (ss->rx_big.info == NULL)
  1785. goto abort_with_rx_small_info;
  1786. /* Fill the receive rings */
  1787. ss->rx_big.cnt = 0;
  1788. ss->rx_small.cnt = 0;
  1789. ss->rx_big.fill_cnt = 0;
  1790. ss->rx_small.fill_cnt = 0;
  1791. ss->rx_small.page_offset = MYRI10GE_ALLOC_SIZE;
  1792. ss->rx_big.page_offset = MYRI10GE_ALLOC_SIZE;
  1793. ss->rx_small.watchdog_needed = 0;
  1794. ss->rx_big.watchdog_needed = 0;
  1795. if (mgp->small_bytes == 0) {
  1796. ss->rx_small.fill_cnt = ss->rx_small.mask + 1;
  1797. } else {
  1798. myri10ge_alloc_rx_pages(mgp, &ss->rx_small,
  1799. mgp->small_bytes + MXGEFW_PAD, 0);
  1800. }
  1801. if (ss->rx_small.fill_cnt < ss->rx_small.mask + 1) {
  1802. netdev_err(dev, "slice-%d: alloced only %d small bufs\n",
  1803. slice, ss->rx_small.fill_cnt);
  1804. goto abort_with_rx_small_ring;
  1805. }
  1806. myri10ge_alloc_rx_pages(mgp, &ss->rx_big, mgp->big_bytes, 0);
  1807. if (ss->rx_big.fill_cnt < ss->rx_big.mask + 1) {
  1808. netdev_err(dev, "slice-%d: alloced only %d big bufs\n",
  1809. slice, ss->rx_big.fill_cnt);
  1810. goto abort_with_rx_big_ring;
  1811. }
  1812. return 0;
  1813. abort_with_rx_big_ring:
  1814. for (i = ss->rx_big.cnt; i < ss->rx_big.fill_cnt; i++) {
  1815. int idx = i & ss->rx_big.mask;
  1816. myri10ge_unmap_rx_page(mgp->pdev, &ss->rx_big.info[idx],
  1817. mgp->big_bytes);
  1818. put_page(ss->rx_big.info[idx].page);
  1819. }
  1820. abort_with_rx_small_ring:
  1821. if (mgp->small_bytes == 0)
  1822. ss->rx_small.fill_cnt = ss->rx_small.cnt;
  1823. for (i = ss->rx_small.cnt; i < ss->rx_small.fill_cnt; i++) {
  1824. int idx = i & ss->rx_small.mask;
  1825. myri10ge_unmap_rx_page(mgp->pdev, &ss->rx_small.info[idx],
  1826. mgp->small_bytes + MXGEFW_PAD);
  1827. put_page(ss->rx_small.info[idx].page);
  1828. }
  1829. kfree(ss->rx_big.info);
  1830. abort_with_rx_small_info:
  1831. kfree(ss->rx_small.info);
  1832. abort_with_tx_info:
  1833. kfree(ss->tx.info);
  1834. abort_with_rx_big_shadow:
  1835. kfree(ss->rx_big.shadow);
  1836. abort_with_rx_small_shadow:
  1837. kfree(ss->rx_small.shadow);
  1838. abort_with_tx_req_bytes:
  1839. kfree(ss->tx.req_bytes);
  1840. ss->tx.req_bytes = NULL;
  1841. ss->tx.req_list = NULL;
  1842. abort_with_nothing:
  1843. return status;
  1844. }
  1845. static void myri10ge_free_rings(struct myri10ge_slice_state *ss)
  1846. {
  1847. struct myri10ge_priv *mgp = ss->mgp;
  1848. struct sk_buff *skb;
  1849. struct myri10ge_tx_buf *tx;
  1850. int i, len, idx;
  1851. /* If not allocated, skip it */
  1852. if (ss->tx.req_list == NULL)
  1853. return;
  1854. for (i = ss->rx_big.cnt; i < ss->rx_big.fill_cnt; i++) {
  1855. idx = i & ss->rx_big.mask;
  1856. if (i == ss->rx_big.fill_cnt - 1)
  1857. ss->rx_big.info[idx].page_offset = MYRI10GE_ALLOC_SIZE;
  1858. myri10ge_unmap_rx_page(mgp->pdev, &ss->rx_big.info[idx],
  1859. mgp->big_bytes);
  1860. put_page(ss->rx_big.info[idx].page);
  1861. }
  1862. if (mgp->small_bytes == 0)
  1863. ss->rx_small.fill_cnt = ss->rx_small.cnt;
  1864. for (i = ss->rx_small.cnt; i < ss->rx_small.fill_cnt; i++) {
  1865. idx = i & ss->rx_small.mask;
  1866. if (i == ss->rx_small.fill_cnt - 1)
  1867. ss->rx_small.info[idx].page_offset =
  1868. MYRI10GE_ALLOC_SIZE;
  1869. myri10ge_unmap_rx_page(mgp->pdev, &ss->rx_small.info[idx],
  1870. mgp->small_bytes + MXGEFW_PAD);
  1871. put_page(ss->rx_small.info[idx].page);
  1872. }
  1873. tx = &ss->tx;
  1874. while (tx->done != tx->req) {
  1875. idx = tx->done & tx->mask;
  1876. skb = tx->info[idx].skb;
  1877. /* Mark as free */
  1878. tx->info[idx].skb = NULL;
  1879. tx->done++;
  1880. len = dma_unmap_len(&tx->info[idx], len);
  1881. dma_unmap_len_set(&tx->info[idx], len, 0);
  1882. if (skb) {
  1883. ss->stats.tx_dropped++;
  1884. dev_kfree_skb_any(skb);
  1885. if (len)
  1886. pci_unmap_single(mgp->pdev,
  1887. dma_unmap_addr(&tx->info[idx],
  1888. bus), len,
  1889. PCI_DMA_TODEVICE);
  1890. } else {
  1891. if (len)
  1892. pci_unmap_page(mgp->pdev,
  1893. dma_unmap_addr(&tx->info[idx],
  1894. bus), len,
  1895. PCI_DMA_TODEVICE);
  1896. }
  1897. }
  1898. kfree(ss->rx_big.info);
  1899. kfree(ss->rx_small.info);
  1900. kfree(ss->tx.info);
  1901. kfree(ss->rx_big.shadow);
  1902. kfree(ss->rx_small.shadow);
  1903. kfree(ss->tx.req_bytes);
  1904. ss->tx.req_bytes = NULL;
  1905. ss->tx.req_list = NULL;
  1906. }
  1907. static int myri10ge_request_irq(struct myri10ge_priv *mgp)
  1908. {
  1909. struct pci_dev *pdev = mgp->pdev;
  1910. struct myri10ge_slice_state *ss;
  1911. struct net_device *netdev = mgp->dev;
  1912. int i;
  1913. int status;
  1914. mgp->msi_enabled = 0;
  1915. mgp->msix_enabled = 0;
  1916. status = 0;
  1917. if (myri10ge_msi) {
  1918. if (mgp->num_slices > 1) {
  1919. status =
  1920. pci_enable_msix(pdev, mgp->msix_vectors,
  1921. mgp->num_slices);
  1922. if (status == 0) {
  1923. mgp->msix_enabled = 1;
  1924. } else {
  1925. dev_err(&pdev->dev,
  1926. "Error %d setting up MSI-X\n", status);
  1927. return status;
  1928. }
  1929. }
  1930. if (mgp->msix_enabled == 0) {
  1931. status = pci_enable_msi(pdev);
  1932. if (status != 0) {
  1933. dev_err(&pdev->dev,
  1934. "Error %d setting up MSI; falling back to xPIC\n",
  1935. status);
  1936. } else {
  1937. mgp->msi_enabled = 1;
  1938. }
  1939. }
  1940. }
  1941. if (mgp->msix_enabled) {
  1942. for (i = 0; i < mgp->num_slices; i++) {
  1943. ss = &mgp->ss[i];
  1944. snprintf(ss->irq_desc, sizeof(ss->irq_desc),
  1945. "%s:slice-%d", netdev->name, i);
  1946. status = request_irq(mgp->msix_vectors[i].vector,
  1947. myri10ge_intr, 0, ss->irq_desc,
  1948. ss);
  1949. if (status != 0) {
  1950. dev_err(&pdev->dev,
  1951. "slice %d failed to allocate IRQ\n", i);
  1952. i--;
  1953. while (i >= 0) {
  1954. free_irq(mgp->msix_vectors[i].vector,
  1955. &mgp->ss[i]);
  1956. i--;
  1957. }
  1958. pci_disable_msix(pdev);
  1959. return status;
  1960. }
  1961. }
  1962. } else {
  1963. status = request_irq(pdev->irq, myri10ge_intr, IRQF_SHARED,
  1964. mgp->dev->name, &mgp->ss[0]);
  1965. if (status != 0) {
  1966. dev_err(&pdev->dev, "failed to allocate IRQ\n");
  1967. if (mgp->msi_enabled)
  1968. pci_disable_msi(pdev);
  1969. }
  1970. }
  1971. return status;
  1972. }
  1973. static void myri10ge_free_irq(struct myri10ge_priv *mgp)
  1974. {
  1975. struct pci_dev *pdev = mgp->pdev;
  1976. int i;
  1977. if (mgp->msix_enabled) {
  1978. for (i = 0; i < mgp->num_slices; i++)
  1979. free_irq(mgp->msix_vectors[i].vector, &mgp->ss[i]);
  1980. } else {
  1981. free_irq(pdev->irq, &mgp->ss[0]);
  1982. }
  1983. if (mgp->msi_enabled)
  1984. pci_disable_msi(pdev);
  1985. if (mgp->msix_enabled)
  1986. pci_disable_msix(pdev);
  1987. }
  1988. static int
  1989. myri10ge_get_frag_header(struct skb_frag_struct *frag, void **mac_hdr,
  1990. void **ip_hdr, void **tcpudp_hdr,
  1991. u64 * hdr_flags, void *priv)
  1992. {
  1993. struct ethhdr *eh;
  1994. struct vlan_ethhdr *veh;
  1995. struct iphdr *iph;
  1996. u8 *va = skb_frag_address(frag);
  1997. unsigned long ll_hlen;
  1998. /* passed opaque through lro_receive_frags() */
  1999. __wsum csum = (__force __wsum) (unsigned long)priv;
  2000. /* find the mac header, aborting if not IPv4 */
  2001. eh = (struct ethhdr *)va;
  2002. *mac_hdr = eh;
  2003. ll_hlen = ETH_HLEN;
  2004. if (eh->h_proto != htons(ETH_P_IP)) {
  2005. if (eh->h_proto == htons(ETH_P_8021Q)) {
  2006. veh = (struct vlan_ethhdr *)va;
  2007. if (veh->h_vlan_encapsulated_proto != htons(ETH_P_IP))
  2008. return -1;
  2009. ll_hlen += VLAN_HLEN;
  2010. /*
  2011. * HW checksum starts ETH_HLEN bytes into
  2012. * frame, so we must subtract off the VLAN
  2013. * header's checksum before csum can be used
  2014. */
  2015. csum = csum_sub(csum, csum_partial(va + ETH_HLEN,
  2016. VLAN_HLEN, 0));
  2017. } else {
  2018. return -1;
  2019. }
  2020. }
  2021. *hdr_flags = LRO_IPV4;
  2022. iph = (struct iphdr *)(va + ll_hlen);
  2023. *ip_hdr = iph;
  2024. if (iph->protocol != IPPROTO_TCP)
  2025. return -1;
  2026. if (ip_is_fragment(iph))
  2027. return -1;
  2028. *hdr_flags |= LRO_TCP;
  2029. *tcpudp_hdr = (u8 *) (*ip_hdr) + (iph->ihl << 2);
  2030. /* verify the IP checksum */
  2031. if (unlikely(ip_fast_csum((u8 *) iph, iph->ihl)))
  2032. return -1;
  2033. /* verify the checksum */
  2034. if (unlikely(csum_tcpudp_magic(iph->saddr, iph->daddr,
  2035. ntohs(iph->tot_len) - (iph->ihl << 2),
  2036. IPPROTO_TCP, csum)))
  2037. return -1;
  2038. return 0;
  2039. }
  2040. static int myri10ge_get_txrx(struct myri10ge_priv *mgp, int slice)
  2041. {
  2042. struct myri10ge_cmd cmd;
  2043. struct myri10ge_slice_state *ss;
  2044. int status;
  2045. ss = &mgp->ss[slice];
  2046. status = 0;
  2047. if (slice == 0 || (mgp->dev->real_num_tx_queues > 1)) {
  2048. cmd.data0 = slice;
  2049. status = myri10ge_send_cmd(mgp, MXGEFW_CMD_GET_SEND_OFFSET,
  2050. &cmd, 0);
  2051. ss->tx.lanai = (struct mcp_kreq_ether_send __iomem *)
  2052. (mgp->sram + cmd.data0);
  2053. }
  2054. cmd.data0 = slice;
  2055. status |= myri10ge_send_cmd(mgp, MXGEFW_CMD_GET_SMALL_RX_OFFSET,
  2056. &cmd, 0);
  2057. ss->rx_small.lanai = (struct mcp_kreq_ether_recv __iomem *)
  2058. (mgp->sram + cmd.data0);
  2059. cmd.data0 = slice;
  2060. status |= myri10ge_send_cmd(mgp, MXGEFW_CMD_GET_BIG_RX_OFFSET, &cmd, 0);
  2061. ss->rx_big.lanai = (struct mcp_kreq_ether_recv __iomem *)
  2062. (mgp->sram + cmd.data0);
  2063. ss->tx.send_go = (__iomem __be32 *)
  2064. (mgp->sram + MXGEFW_ETH_SEND_GO + 64 * slice);
  2065. ss->tx.send_stop = (__iomem __be32 *)
  2066. (mgp->sram + MXGEFW_ETH_SEND_STOP + 64 * slice);
  2067. return status;
  2068. }
  2069. static int myri10ge_set_stats(struct myri10ge_priv *mgp, int slice)
  2070. {
  2071. struct myri10ge_cmd cmd;
  2072. struct myri10ge_slice_state *ss;
  2073. int status;
  2074. ss = &mgp->ss[slice];
  2075. cmd.data0 = MYRI10GE_LOWPART_TO_U32(ss->fw_stats_bus);
  2076. cmd.data1 = MYRI10GE_HIGHPART_TO_U32(ss->fw_stats_bus);
  2077. cmd.data2 = sizeof(struct mcp_irq_data) | (slice << 16);
  2078. status = myri10ge_send_cmd(mgp, MXGEFW_CMD_SET_STATS_DMA_V2, &cmd, 0);
  2079. if (status == -ENOSYS) {
  2080. dma_addr_t bus = ss->fw_stats_bus;
  2081. if (slice != 0)
  2082. return -EINVAL;
  2083. bus += offsetof(struct mcp_irq_data, send_done_count);
  2084. cmd.data0 = MYRI10GE_LOWPART_TO_U32(bus);
  2085. cmd.data1 = MYRI10GE_HIGHPART_TO_U32(bus);
  2086. status = myri10ge_send_cmd(mgp,
  2087. MXGEFW_CMD_SET_STATS_DMA_OBSOLETE,
  2088. &cmd, 0);
  2089. /* Firmware cannot support multicast without STATS_DMA_V2 */
  2090. mgp->fw_multicast_support = 0;
  2091. } else {
  2092. mgp->fw_multicast_support = 1;
  2093. }
  2094. return 0;
  2095. }
  2096. static int myri10ge_open(struct net_device *dev)
  2097. {
  2098. struct myri10ge_slice_state *ss;
  2099. struct myri10ge_priv *mgp = netdev_priv(dev);
  2100. struct myri10ge_cmd cmd;
  2101. int i, status, big_pow2, slice;
  2102. u8 *itable;
  2103. struct net_lro_mgr *lro_mgr;
  2104. if (mgp->running != MYRI10GE_ETH_STOPPED)
  2105. return -EBUSY;
  2106. mgp->running = MYRI10GE_ETH_STARTING;
  2107. status = myri10ge_reset(mgp);
  2108. if (status != 0) {
  2109. netdev_err(dev, "failed reset\n");
  2110. goto abort_with_nothing;
  2111. }
  2112. if (mgp->num_slices > 1) {
  2113. cmd.data0 = mgp->num_slices;
  2114. cmd.data1 = MXGEFW_SLICE_INTR_MODE_ONE_PER_SLICE;
  2115. if (mgp->dev->real_num_tx_queues > 1)
  2116. cmd.data1 |= MXGEFW_SLICE_ENABLE_MULTIPLE_TX_QUEUES;
  2117. status = myri10ge_send_cmd(mgp, MXGEFW_CMD_ENABLE_RSS_QUEUES,
  2118. &cmd, 0);
  2119. if (status != 0) {
  2120. netdev_err(dev, "failed to set number of slices\n");
  2121. goto abort_with_nothing;
  2122. }
  2123. /* setup the indirection table */
  2124. cmd.data0 = mgp->num_slices;
  2125. status = myri10ge_send_cmd(mgp, MXGEFW_CMD_SET_RSS_TABLE_SIZE,
  2126. &cmd, 0);
  2127. status |= myri10ge_send_cmd(mgp,
  2128. MXGEFW_CMD_GET_RSS_TABLE_OFFSET,
  2129. &cmd, 0);
  2130. if (status != 0) {
  2131. netdev_err(dev, "failed to setup rss tables\n");
  2132. goto abort_with_nothing;
  2133. }
  2134. /* just enable an identity mapping */
  2135. itable = mgp->sram + cmd.data0;
  2136. for (i = 0; i < mgp->num_slices; i++)
  2137. __raw_writeb(i, &itable[i]);
  2138. cmd.data0 = 1;
  2139. cmd.data1 = myri10ge_rss_hash;
  2140. status = myri10ge_send_cmd(mgp, MXGEFW_CMD_SET_RSS_ENABLE,
  2141. &cmd, 0);
  2142. if (status != 0) {
  2143. netdev_err(dev, "failed to enable slices\n");
  2144. goto abort_with_nothing;
  2145. }
  2146. }
  2147. status = myri10ge_request_irq(mgp);
  2148. if (status != 0)
  2149. goto abort_with_nothing;
  2150. /* decide what small buffer size to use. For good TCP rx
  2151. * performance, it is important to not receive 1514 byte
  2152. * frames into jumbo buffers, as it confuses the socket buffer
  2153. * accounting code, leading to drops and erratic performance.
  2154. */
  2155. if (dev->mtu <= ETH_DATA_LEN)
  2156. /* enough for a TCP header */
  2157. mgp->small_bytes = (128 > SMP_CACHE_BYTES)
  2158. ? (128 - MXGEFW_PAD)
  2159. : (SMP_CACHE_BYTES - MXGEFW_PAD);
  2160. else
  2161. /* enough for a vlan encapsulated ETH_DATA_LEN frame */
  2162. mgp->small_bytes = VLAN_ETH_FRAME_LEN;
  2163. /* Override the small buffer size? */
  2164. if (myri10ge_small_bytes >= 0)
  2165. mgp->small_bytes = myri10ge_small_bytes;
  2166. /* Firmware needs the big buff size as a power of 2. Lie and
  2167. * tell him the buffer is larger, because we only use 1
  2168. * buffer/pkt, and the mtu will prevent overruns.
  2169. */
  2170. big_pow2 = dev->mtu + ETH_HLEN + VLAN_HLEN + MXGEFW_PAD;
  2171. if (big_pow2 < MYRI10GE_ALLOC_SIZE / 2) {
  2172. while (!is_power_of_2(big_pow2))
  2173. big_pow2++;
  2174. mgp->big_bytes = dev->mtu + ETH_HLEN + VLAN_HLEN + MXGEFW_PAD;
  2175. } else {
  2176. big_pow2 = MYRI10GE_ALLOC_SIZE;
  2177. mgp->big_bytes = big_pow2;
  2178. }
  2179. /* setup the per-slice data structures */
  2180. for (slice = 0; slice < mgp->num_slices; slice++) {
  2181. ss = &mgp->ss[slice];
  2182. status = myri10ge_get_txrx(mgp, slice);
  2183. if (status != 0) {
  2184. netdev_err(dev, "failed to get ring sizes or locations\n");
  2185. goto abort_with_rings;
  2186. }
  2187. status = myri10ge_allocate_rings(ss);
  2188. if (status != 0)
  2189. goto abort_with_rings;
  2190. /* only firmware which supports multiple TX queues
  2191. * supports setting up the tx stats on non-zero
  2192. * slices */
  2193. if (slice == 0 || mgp->dev->real_num_tx_queues > 1)
  2194. status = myri10ge_set_stats(mgp, slice);
  2195. if (status) {
  2196. netdev_err(dev, "Couldn't set stats DMA\n");
  2197. goto abort_with_rings;
  2198. }
  2199. lro_mgr = &ss->rx_done.lro_mgr;
  2200. lro_mgr->dev = dev;
  2201. lro_mgr->features = LRO_F_NAPI;
  2202. lro_mgr->ip_summed = CHECKSUM_COMPLETE;
  2203. lro_mgr->ip_summed_aggr = CHECKSUM_UNNECESSARY;
  2204. lro_mgr->max_desc = MYRI10GE_MAX_LRO_DESCRIPTORS;
  2205. lro_mgr->lro_arr = ss->rx_done.lro_desc;
  2206. lro_mgr->get_frag_header = myri10ge_get_frag_header;
  2207. lro_mgr->max_aggr = myri10ge_lro_max_pkts;
  2208. lro_mgr->frag_align_pad = 2;
  2209. if (lro_mgr->max_aggr > MAX_SKB_FRAGS)
  2210. lro_mgr->max_aggr = MAX_SKB_FRAGS;
  2211. /* must happen prior to any irq */
  2212. napi_enable(&(ss)->napi);
  2213. }
  2214. /* now give firmware buffers sizes, and MTU */
  2215. cmd.data0 = dev->mtu + ETH_HLEN + VLAN_HLEN;
  2216. status = myri10ge_send_cmd(mgp, MXGEFW_CMD_SET_MTU, &cmd, 0);
  2217. cmd.data0 = mgp->small_bytes;
  2218. status |=
  2219. myri10ge_send_cmd(mgp, MXGEFW_CMD_SET_SMALL_BUFFER_SIZE, &cmd, 0);
  2220. cmd.data0 = big_pow2;
  2221. status |=
  2222. myri10ge_send_cmd(mgp, MXGEFW_CMD_SET_BIG_BUFFER_SIZE, &cmd, 0);
  2223. if (status) {
  2224. netdev_err(dev, "Couldn't set buffer sizes\n");
  2225. goto abort_with_rings;
  2226. }
  2227. /*
  2228. * Set Linux style TSO mode; this is needed only on newer
  2229. * firmware versions. Older versions default to Linux
  2230. * style TSO
  2231. */
  2232. cmd.data0 = 0;
  2233. status = myri10ge_send_cmd(mgp, MXGEFW_CMD_SET_TSO_MODE, &cmd, 0);
  2234. if (status && status != -ENOSYS) {
  2235. netdev_err(dev, "Couldn't set TSO mode\n");
  2236. goto abort_with_rings;
  2237. }
  2238. mgp->link_state = ~0U;
  2239. mgp->rdma_tags_available = 15;
  2240. status = myri10ge_send_cmd(mgp, MXGEFW_CMD_ETHERNET_UP, &cmd, 0);
  2241. if (status) {
  2242. netdev_err(dev, "Couldn't bring up link\n");
  2243. goto abort_with_rings;
  2244. }
  2245. mgp->running = MYRI10GE_ETH_RUNNING;
  2246. mgp->watchdog_timer.expires = jiffies + myri10ge_watchdog_timeout * HZ;
  2247. add_timer(&mgp->watchdog_timer);
  2248. netif_tx_wake_all_queues(dev);
  2249. return 0;
  2250. abort_with_rings:
  2251. while (slice) {
  2252. slice--;
  2253. napi_disable(&mgp->ss[slice].napi);
  2254. }
  2255. for (i = 0; i < mgp->num_slices; i++)
  2256. myri10ge_free_rings(&mgp->ss[i]);
  2257. myri10ge_free_irq(mgp);
  2258. abort_with_nothing:
  2259. mgp->running = MYRI10GE_ETH_STOPPED;
  2260. return -ENOMEM;
  2261. }
  2262. static int myri10ge_close(struct net_device *dev)
  2263. {
  2264. struct myri10ge_priv *mgp = netdev_priv(dev);
  2265. struct myri10ge_cmd cmd;
  2266. int status, old_down_cnt;
  2267. int i;
  2268. if (mgp->running != MYRI10GE_ETH_RUNNING)
  2269. return 0;
  2270. if (mgp->ss[0].tx.req_bytes == NULL)
  2271. return 0;
  2272. del_timer_sync(&mgp->watchdog_timer);
  2273. mgp->running = MYRI10GE_ETH_STOPPING;
  2274. for (i = 0; i < mgp->num_slices; i++) {
  2275. napi_disable(&mgp->ss[i].napi);
  2276. }
  2277. netif_carrier_off(dev);
  2278. netif_tx_stop_all_queues(dev);
  2279. if (mgp->rebooted == 0) {
  2280. old_down_cnt = mgp->down_cnt;
  2281. mb();
  2282. status =
  2283. myri10ge_send_cmd(mgp, MXGEFW_CMD_ETHERNET_DOWN, &cmd, 0);
  2284. if (status)
  2285. netdev_err(dev, "Couldn't bring down link\n");
  2286. wait_event_timeout(mgp->down_wq, old_down_cnt != mgp->down_cnt,
  2287. HZ);
  2288. if (old_down_cnt == mgp->down_cnt)
  2289. netdev_err(dev, "never got down irq\n");
  2290. }
  2291. netif_tx_disable(dev);
  2292. myri10ge_free_irq(mgp);
  2293. for (i = 0; i < mgp->num_slices; i++)
  2294. myri10ge_free_rings(&mgp->ss[i]);
  2295. mgp->running = MYRI10GE_ETH_STOPPED;
  2296. return 0;
  2297. }
  2298. /* copy an array of struct mcp_kreq_ether_send's to the mcp. Copy
  2299. * backwards one at a time and handle ring wraps */
  2300. static inline void
  2301. myri10ge_submit_req_backwards(struct myri10ge_tx_buf *tx,
  2302. struct mcp_kreq_ether_send *src, int cnt)
  2303. {
  2304. int idx, starting_slot;
  2305. starting_slot = tx->req;
  2306. while (cnt > 1) {
  2307. cnt--;
  2308. idx = (starting_slot + cnt) & tx->mask;
  2309. myri10ge_pio_copy(&tx->lanai[idx], &src[cnt], sizeof(*src));
  2310. mb();
  2311. }
  2312. }
  2313. /*
  2314. * copy an array of struct mcp_kreq_ether_send's to the mcp. Copy
  2315. * at most 32 bytes at a time, so as to avoid involving the software
  2316. * pio handler in the nic. We re-write the first segment's flags
  2317. * to mark them valid only after writing the entire chain.
  2318. */
  2319. static inline void
  2320. myri10ge_submit_req(struct myri10ge_tx_buf *tx, struct mcp_kreq_ether_send *src,
  2321. int cnt)
  2322. {
  2323. int idx, i;
  2324. struct mcp_kreq_ether_send __iomem *dstp, *dst;
  2325. struct mcp_kreq_ether_send *srcp;
  2326. u8 last_flags;
  2327. idx = tx->req & tx->mask;
  2328. last_flags = src->flags;
  2329. src->flags = 0;
  2330. mb();
  2331. dst = dstp = &tx->lanai[idx];
  2332. srcp = src;
  2333. if ((idx + cnt) < tx->mask) {
  2334. for (i = 0; i < (cnt - 1); i += 2) {
  2335. myri10ge_pio_copy(dstp, srcp, 2 * sizeof(*src));
  2336. mb(); /* force write every 32 bytes */
  2337. srcp += 2;
  2338. dstp += 2;
  2339. }
  2340. } else {
  2341. /* submit all but the first request, and ensure
  2342. * that it is submitted below */
  2343. myri10ge_submit_req_backwards(tx, src, cnt);
  2344. i = 0;
  2345. }
  2346. if (i < cnt) {
  2347. /* submit the first request */
  2348. myri10ge_pio_copy(dstp, srcp, sizeof(*src));
  2349. mb(); /* barrier before setting valid flag */
  2350. }
  2351. /* re-write the last 32-bits with the valid flags */
  2352. src->flags = last_flags;
  2353. put_be32(*((__be32 *) src + 3), (__be32 __iomem *) dst + 3);
  2354. tx->req += cnt;
  2355. mb();
  2356. }
  2357. /*
  2358. * Transmit a packet. We need to split the packet so that a single
  2359. * segment does not cross myri10ge->tx_boundary, so this makes segment
  2360. * counting tricky. So rather than try to count segments up front, we
  2361. * just give up if there are too few segments to hold a reasonably
  2362. * fragmented packet currently available. If we run
  2363. * out of segments while preparing a packet for DMA, we just linearize
  2364. * it and try again.
  2365. */
  2366. static netdev_tx_t myri10ge_xmit(struct sk_buff *skb,
  2367. struct net_device *dev)
  2368. {
  2369. struct myri10ge_priv *mgp = netdev_priv(dev);
  2370. struct myri10ge_slice_state *ss;
  2371. struct mcp_kreq_ether_send *req;
  2372. struct myri10ge_tx_buf *tx;
  2373. struct skb_frag_struct *frag;
  2374. struct netdev_queue *netdev_queue;
  2375. dma_addr_t bus;
  2376. u32 low;
  2377. __be32 high_swapped;
  2378. unsigned int len;
  2379. int idx, last_idx, avail, frag_cnt, frag_idx, count, mss, max_segments;
  2380. u16 pseudo_hdr_offset, cksum_offset, queue;
  2381. int cum_len, seglen, boundary, rdma_count;
  2382. u8 flags, odd_flag;
  2383. queue = skb_get_queue_mapping(skb);
  2384. ss = &mgp->ss[queue];
  2385. netdev_queue = netdev_get_tx_queue(mgp->dev, queue);
  2386. tx = &ss->tx;
  2387. again:
  2388. req = tx->req_list;
  2389. avail = tx->mask - 1 - (tx->req - tx->done);
  2390. mss = 0;
  2391. max_segments = MXGEFW_MAX_SEND_DESC;
  2392. if (skb_is_gso(skb)) {
  2393. mss = skb_shinfo(skb)->gso_size;
  2394. max_segments = MYRI10GE_MAX_SEND_DESC_TSO;
  2395. }
  2396. if ((unlikely(avail < max_segments))) {
  2397. /* we are out of transmit resources */
  2398. tx->stop_queue++;
  2399. netif_tx_stop_queue(netdev_queue);
  2400. return NETDEV_TX_BUSY;
  2401. }
  2402. /* Setup checksum offloading, if needed */
  2403. cksum_offset = 0;
  2404. pseudo_hdr_offset = 0;
  2405. odd_flag = 0;
  2406. flags = (MXGEFW_FLAGS_NO_TSO | MXGEFW_FLAGS_FIRST);
  2407. if (likely(skb->ip_summed == CHECKSUM_PARTIAL)) {
  2408. cksum_offset = skb_checksum_start_offset(skb);
  2409. pseudo_hdr_offset = cksum_offset + skb->csum_offset;
  2410. /* If the headers are excessively large, then we must
  2411. * fall back to a software checksum */
  2412. if (unlikely(!mss && (cksum_offset > 255 ||
  2413. pseudo_hdr_offset > 127))) {
  2414. if (skb_checksum_help(skb))
  2415. goto drop;
  2416. cksum_offset = 0;
  2417. pseudo_hdr_offset = 0;
  2418. } else {
  2419. odd_flag = MXGEFW_FLAGS_ALIGN_ODD;
  2420. flags |= MXGEFW_FLAGS_CKSUM;
  2421. }
  2422. }
  2423. cum_len = 0;
  2424. if (mss) { /* TSO */
  2425. /* this removes any CKSUM flag from before */
  2426. flags = (MXGEFW_FLAGS_TSO_HDR | MXGEFW_FLAGS_FIRST);
  2427. /* negative cum_len signifies to the
  2428. * send loop that we are still in the
  2429. * header portion of the TSO packet.
  2430. * TSO header can be at most 1KB long */
  2431. cum_len = -(skb_transport_offset(skb) + tcp_hdrlen(skb));
  2432. /* for IPv6 TSO, the checksum offset stores the
  2433. * TCP header length, to save the firmware from
  2434. * the need to parse the headers */
  2435. if (skb_is_gso_v6(skb)) {
  2436. cksum_offset = tcp_hdrlen(skb);
  2437. /* Can only handle headers <= max_tso6 long */
  2438. if (unlikely(-cum_len > mgp->max_tso6))
  2439. return myri10ge_sw_tso(skb, dev);
  2440. }
  2441. /* for TSO, pseudo_hdr_offset holds mss.
  2442. * The firmware figures out where to put
  2443. * the checksum by parsing the header. */
  2444. pseudo_hdr_offset = mss;
  2445. } else
  2446. /* Mark small packets, and pad out tiny packets */
  2447. if (skb->len <= MXGEFW_SEND_SMALL_SIZE) {
  2448. flags |= MXGEFW_FLAGS_SMALL;
  2449. /* pad frames to at least ETH_ZLEN bytes */
  2450. if (unlikely(skb->len < ETH_ZLEN)) {
  2451. if (skb_padto(skb, ETH_ZLEN)) {
  2452. /* The packet is gone, so we must
  2453. * return 0 */
  2454. ss->stats.tx_dropped += 1;
  2455. return NETDEV_TX_OK;
  2456. }
  2457. /* adjust the len to account for the zero pad
  2458. * so that the nic can know how long it is */
  2459. skb->len = ETH_ZLEN;
  2460. }
  2461. }
  2462. /* map the skb for DMA */
  2463. len = skb_headlen(skb);
  2464. idx = tx->req & tx->mask;
  2465. tx->info[idx].skb = skb;
  2466. bus = pci_map_single(mgp->pdev, skb->data, len, PCI_DMA_TODEVICE);
  2467. dma_unmap_addr_set(&tx->info[idx], bus, bus);
  2468. dma_unmap_len_set(&tx->info[idx], len, len);
  2469. frag_cnt = skb_shinfo(skb)->nr_frags;
  2470. frag_idx = 0;
  2471. count = 0;
  2472. rdma_count = 0;
  2473. /* "rdma_count" is the number of RDMAs belonging to the
  2474. * current packet BEFORE the current send request. For
  2475. * non-TSO packets, this is equal to "count".
  2476. * For TSO packets, rdma_count needs to be reset
  2477. * to 0 after a segment cut.
  2478. *
  2479. * The rdma_count field of the send request is
  2480. * the number of RDMAs of the packet starting at
  2481. * that request. For TSO send requests with one ore more cuts
  2482. * in the middle, this is the number of RDMAs starting
  2483. * after the last cut in the request. All previous
  2484. * segments before the last cut implicitly have 1 RDMA.
  2485. *
  2486. * Since the number of RDMAs is not known beforehand,
  2487. * it must be filled-in retroactively - after each
  2488. * segmentation cut or at the end of the entire packet.
  2489. */
  2490. while (1) {
  2491. /* Break the SKB or Fragment up into pieces which
  2492. * do not cross mgp->tx_boundary */
  2493. low = MYRI10GE_LOWPART_TO_U32(bus);
  2494. high_swapped = htonl(MYRI10GE_HIGHPART_TO_U32(bus));
  2495. while (len) {
  2496. u8 flags_next;
  2497. int cum_len_next;
  2498. if (unlikely(count == max_segments))
  2499. goto abort_linearize;
  2500. boundary =
  2501. (low + mgp->tx_boundary) & ~(mgp->tx_boundary - 1);
  2502. seglen = boundary - low;
  2503. if (seglen > len)
  2504. seglen = len;
  2505. flags_next = flags & ~MXGEFW_FLAGS_FIRST;
  2506. cum_len_next = cum_len + seglen;
  2507. if (mss) { /* TSO */
  2508. (req - rdma_count)->rdma_count = rdma_count + 1;
  2509. if (likely(cum_len >= 0)) { /* payload */
  2510. int next_is_first, chop;
  2511. chop = (cum_len_next > mss);
  2512. cum_len_next = cum_len_next % mss;
  2513. next_is_first = (cum_len_next == 0);
  2514. flags |= chop * MXGEFW_FLAGS_TSO_CHOP;
  2515. flags_next |= next_is_first *
  2516. MXGEFW_FLAGS_FIRST;
  2517. rdma_count |= -(chop | next_is_first);
  2518. rdma_count += chop & !next_is_first;
  2519. } else if (likely(cum_len_next >= 0)) { /* header ends */
  2520. int small;
  2521. rdma_count = -1;
  2522. cum_len_next = 0;
  2523. seglen = -cum_len;
  2524. small = (mss <= MXGEFW_SEND_SMALL_SIZE);
  2525. flags_next = MXGEFW_FLAGS_TSO_PLD |
  2526. MXGEFW_FLAGS_FIRST |
  2527. (small * MXGEFW_FLAGS_SMALL);
  2528. }
  2529. }
  2530. req->addr_high = high_swapped;
  2531. req->addr_low = htonl(low);
  2532. req->pseudo_hdr_offset = htons(pseudo_hdr_offset);
  2533. req->pad = 0; /* complete solid 16-byte block; does this matter? */
  2534. req->rdma_count = 1;
  2535. req->length = htons(seglen);
  2536. req->cksum_offset = cksum_offset;
  2537. req->flags = flags | ((cum_len & 1) * odd_flag);
  2538. low += seglen;
  2539. len -= seglen;
  2540. cum_len = cum_len_next;
  2541. flags = flags_next;
  2542. req++;
  2543. count++;
  2544. rdma_count++;
  2545. if (cksum_offset != 0 && !(mss && skb_is_gso_v6(skb))) {
  2546. if (unlikely(cksum_offset > seglen))
  2547. cksum_offset -= seglen;
  2548. else
  2549. cksum_offset = 0;
  2550. }
  2551. }
  2552. if (frag_idx == frag_cnt)
  2553. break;
  2554. /* map next fragment for DMA */
  2555. idx = (count + tx->req) & tx->mask;
  2556. frag = &skb_shinfo(skb)->frags[frag_idx];
  2557. frag_idx++;
  2558. len = skb_frag_size(frag);
  2559. bus = skb_frag_dma_map(&mgp->pdev->dev, frag, 0, len,
  2560. DMA_TO_DEVICE);
  2561. dma_unmap_addr_set(&tx->info[idx], bus, bus);
  2562. dma_unmap_len_set(&tx->info[idx], len, len);
  2563. }
  2564. (req - rdma_count)->rdma_count = rdma_count;
  2565. if (mss)
  2566. do {
  2567. req--;
  2568. req->flags |= MXGEFW_FLAGS_TSO_LAST;
  2569. } while (!(req->flags & (MXGEFW_FLAGS_TSO_CHOP |
  2570. MXGEFW_FLAGS_FIRST)));
  2571. idx = ((count - 1) + tx->req) & tx->mask;
  2572. tx->info[idx].last = 1;
  2573. myri10ge_submit_req(tx, tx->req_list, count);
  2574. /* if using multiple tx queues, make sure NIC polls the
  2575. * current slice */
  2576. if ((mgp->dev->real_num_tx_queues > 1) && tx->queue_active == 0) {
  2577. tx->queue_active = 1;
  2578. put_be32(htonl(1), tx->send_go);
  2579. mb();
  2580. mmiowb();
  2581. }
  2582. tx->pkt_start++;
  2583. if ((avail - count) < MXGEFW_MAX_SEND_DESC) {
  2584. tx->stop_queue++;
  2585. netif_tx_stop_queue(netdev_queue);
  2586. }
  2587. return NETDEV_TX_OK;
  2588. abort_linearize:
  2589. /* Free any DMA resources we've alloced and clear out the skb
  2590. * slot so as to not trip up assertions, and to avoid a
  2591. * double-free if linearizing fails */
  2592. last_idx = (idx + 1) & tx->mask;
  2593. idx = tx->req & tx->mask;
  2594. tx->info[idx].skb = NULL;
  2595. do {
  2596. len = dma_unmap_len(&tx->info[idx], len);
  2597. if (len) {
  2598. if (tx->info[idx].skb != NULL)
  2599. pci_unmap_single(mgp->pdev,
  2600. dma_unmap_addr(&tx->info[idx],
  2601. bus), len,
  2602. PCI_DMA_TODEVICE);
  2603. else
  2604. pci_unmap_page(mgp->pdev,
  2605. dma_unmap_addr(&tx->info[idx],
  2606. bus), len,
  2607. PCI_DMA_TODEVICE);
  2608. dma_unmap_len_set(&tx->info[idx], len, 0);
  2609. tx->info[idx].skb = NULL;
  2610. }
  2611. idx = (idx + 1) & tx->mask;
  2612. } while (idx != last_idx);
  2613. if (skb_is_gso(skb)) {
  2614. netdev_err(mgp->dev, "TSO but wanted to linearize?!?!?\n");
  2615. goto drop;
  2616. }
  2617. if (skb_linearize(skb))
  2618. goto drop;
  2619. tx->linearized++;
  2620. goto again;
  2621. drop:
  2622. dev_kfree_skb_any(skb);
  2623. ss->stats.tx_dropped += 1;
  2624. return NETDEV_TX_OK;
  2625. }
  2626. static netdev_tx_t myri10ge_sw_tso(struct sk_buff *skb,
  2627. struct net_device *dev)
  2628. {
  2629. struct sk_buff *segs, *curr;
  2630. struct myri10ge_priv *mgp = netdev_priv(dev);
  2631. struct myri10ge_slice_state *ss;
  2632. netdev_tx_t status;
  2633. segs = skb_gso_segment(skb, dev->features & ~NETIF_F_TSO6);
  2634. if (IS_ERR(segs))
  2635. goto drop;
  2636. while (segs) {
  2637. curr = segs;
  2638. segs = segs->next;
  2639. curr->next = NULL;
  2640. status = myri10ge_xmit(curr, dev);
  2641. if (status != 0) {
  2642. dev_kfree_skb_any(curr);
  2643. if (segs != NULL) {
  2644. curr = segs;
  2645. segs = segs->next;
  2646. curr->next = NULL;
  2647. dev_kfree_skb_any(segs);
  2648. }
  2649. goto drop;
  2650. }
  2651. }
  2652. dev_kfree_skb_any(skb);
  2653. return NETDEV_TX_OK;
  2654. drop:
  2655. ss = &mgp->ss[skb_get_queue_mapping(skb)];
  2656. dev_kfree_skb_any(skb);
  2657. ss->stats.tx_dropped += 1;
  2658. return NETDEV_TX_OK;
  2659. }
  2660. static struct rtnl_link_stats64 *myri10ge_get_stats(struct net_device *dev,
  2661. struct rtnl_link_stats64 *stats)
  2662. {
  2663. const struct myri10ge_priv *mgp = netdev_priv(dev);
  2664. const struct myri10ge_slice_netstats *slice_stats;
  2665. int i;
  2666. for (i = 0; i < mgp->num_slices; i++) {
  2667. slice_stats = &mgp->ss[i].stats;
  2668. stats->rx_packets += slice_stats->rx_packets;
  2669. stats->tx_packets += slice_stats->tx_packets;
  2670. stats->rx_bytes += slice_stats->rx_bytes;
  2671. stats->tx_bytes += slice_stats->tx_bytes;
  2672. stats->rx_dropped += slice_stats->rx_dropped;
  2673. stats->tx_dropped += slice_stats->tx_dropped;
  2674. }
  2675. return stats;
  2676. }
  2677. static void myri10ge_set_multicast_list(struct net_device *dev)
  2678. {
  2679. struct myri10ge_priv *mgp = netdev_priv(dev);
  2680. struct myri10ge_cmd cmd;
  2681. struct netdev_hw_addr *ha;
  2682. __be32 data[2] = { 0, 0 };
  2683. int err;
  2684. /* can be called from atomic contexts,
  2685. * pass 1 to force atomicity in myri10ge_send_cmd() */
  2686. myri10ge_change_promisc(mgp, dev->flags & IFF_PROMISC, 1);
  2687. /* This firmware is known to not support multicast */
  2688. if (!mgp->fw_multicast_support)
  2689. return;
  2690. /* Disable multicast filtering */
  2691. err = myri10ge_send_cmd(mgp, MXGEFW_ENABLE_ALLMULTI, &cmd, 1);
  2692. if (err != 0) {
  2693. netdev_err(dev, "Failed MXGEFW_ENABLE_ALLMULTI, error status: %d\n",
  2694. err);
  2695. goto abort;
  2696. }
  2697. if ((dev->flags & IFF_ALLMULTI) || mgp->adopted_rx_filter_bug) {
  2698. /* request to disable multicast filtering, so quit here */
  2699. return;
  2700. }
  2701. /* Flush the filters */
  2702. err = myri10ge_send_cmd(mgp, MXGEFW_LEAVE_ALL_MULTICAST_GROUPS,
  2703. &cmd, 1);
  2704. if (err != 0) {
  2705. netdev_err(dev, "Failed MXGEFW_LEAVE_ALL_MULTICAST_GROUPS, error status: %d\n",
  2706. err);
  2707. goto abort;
  2708. }
  2709. /* Walk the multicast list, and add each address */
  2710. netdev_for_each_mc_addr(ha, dev) {
  2711. memcpy(data, &ha->addr, 6);
  2712. cmd.data0 = ntohl(data[0]);
  2713. cmd.data1 = ntohl(data[1]);
  2714. err = myri10ge_send_cmd(mgp, MXGEFW_JOIN_MULTICAST_GROUP,
  2715. &cmd, 1);
  2716. if (err != 0) {
  2717. netdev_err(dev, "Failed MXGEFW_JOIN_MULTICAST_GROUP, error status:%d %pM\n",
  2718. err, ha->addr);
  2719. goto abort;
  2720. }
  2721. }
  2722. /* Enable multicast filtering */
  2723. err = myri10ge_send_cmd(mgp, MXGEFW_DISABLE_ALLMULTI, &cmd, 1);
  2724. if (err != 0) {
  2725. netdev_err(dev, "Failed MXGEFW_DISABLE_ALLMULTI, error status: %d\n",
  2726. err);
  2727. goto abort;
  2728. }
  2729. return;
  2730. abort:
  2731. return;
  2732. }
  2733. static int myri10ge_set_mac_address(struct net_device *dev, void *addr)
  2734. {
  2735. struct sockaddr *sa = addr;
  2736. struct myri10ge_priv *mgp = netdev_priv(dev);
  2737. int status;
  2738. if (!is_valid_ether_addr(sa->sa_data))
  2739. return -EADDRNOTAVAIL;
  2740. status = myri10ge_update_mac_address(mgp, sa->sa_data);
  2741. if (status != 0) {
  2742. netdev_err(dev, "changing mac address failed with %d\n",
  2743. status);
  2744. return status;
  2745. }
  2746. /* change the dev structure */
  2747. memcpy(dev->dev_addr, sa->sa_data, 6);
  2748. return 0;
  2749. }
  2750. static netdev_features_t myri10ge_fix_features(struct net_device *dev,
  2751. netdev_features_t features)
  2752. {
  2753. if (!(features & NETIF_F_RXCSUM))
  2754. features &= ~NETIF_F_LRO;
  2755. return features;
  2756. }
  2757. static int myri10ge_change_mtu(struct net_device *dev, int new_mtu)
  2758. {
  2759. struct myri10ge_priv *mgp = netdev_priv(dev);
  2760. int error = 0;
  2761. if ((new_mtu < 68) || (ETH_HLEN + new_mtu > MYRI10GE_MAX_ETHER_MTU)) {
  2762. netdev_err(dev, "new mtu (%d) is not valid\n", new_mtu);
  2763. return -EINVAL;
  2764. }
  2765. netdev_info(dev, "changing mtu from %d to %d\n", dev->mtu, new_mtu);
  2766. if (mgp->running) {
  2767. /* if we change the mtu on an active device, we must
  2768. * reset the device so the firmware sees the change */
  2769. myri10ge_close(dev);
  2770. dev->mtu = new_mtu;
  2771. myri10ge_open(dev);
  2772. } else
  2773. dev->mtu = new_mtu;
  2774. return error;
  2775. }
  2776. /*
  2777. * Enable ECRC to align PCI-E Completion packets on an 8-byte boundary.
  2778. * Only do it if the bridge is a root port since we don't want to disturb
  2779. * any other device, except if forced with myri10ge_ecrc_enable > 1.
  2780. */
  2781. static void myri10ge_enable_ecrc(struct myri10ge_priv *mgp)
  2782. {
  2783. struct pci_dev *bridge = mgp->pdev->bus->self;
  2784. struct device *dev = &mgp->pdev->dev;
  2785. int cap;
  2786. unsigned err_cap;
  2787. int ret;
  2788. if (!myri10ge_ecrc_enable || !bridge)
  2789. return;
  2790. /* check that the bridge is a root port */
  2791. if (pci_pcie_type(bridge) != PCI_EXP_TYPE_ROOT_PORT) {
  2792. if (myri10ge_ecrc_enable > 1) {
  2793. struct pci_dev *prev_bridge, *old_bridge = bridge;
  2794. /* Walk the hierarchy up to the root port
  2795. * where ECRC has to be enabled */
  2796. do {
  2797. prev_bridge = bridge;
  2798. bridge = bridge->bus->self;
  2799. if (!bridge || prev_bridge == bridge) {
  2800. dev_err(dev,
  2801. "Failed to find root port"
  2802. " to force ECRC\n");
  2803. return;
  2804. }
  2805. } while (pci_pcie_type(bridge) !=
  2806. PCI_EXP_TYPE_ROOT_PORT);
  2807. dev_info(dev,
  2808. "Forcing ECRC on non-root port %s"
  2809. " (enabling on root port %s)\n",
  2810. pci_name(old_bridge), pci_name(bridge));
  2811. } else {
  2812. dev_err(dev,
  2813. "Not enabling ECRC on non-root port %s\n",
  2814. pci_name(bridge));
  2815. return;
  2816. }
  2817. }
  2818. cap = pci_find_ext_capability(bridge, PCI_EXT_CAP_ID_ERR);
  2819. if (!cap)
  2820. return;
  2821. ret = pci_read_config_dword(bridge, cap + PCI_ERR_CAP, &err_cap);
  2822. if (ret) {
  2823. dev_err(dev, "failed reading ext-conf-space of %s\n",
  2824. pci_name(bridge));
  2825. dev_err(dev, "\t pci=nommconf in use? "
  2826. "or buggy/incomplete/absent ACPI MCFG attr?\n");
  2827. return;
  2828. }
  2829. if (!(err_cap & PCI_ERR_CAP_ECRC_GENC))
  2830. return;
  2831. err_cap |= PCI_ERR_CAP_ECRC_GENE;
  2832. pci_write_config_dword(bridge, cap + PCI_ERR_CAP, err_cap);
  2833. dev_info(dev, "Enabled ECRC on upstream bridge %s\n", pci_name(bridge));
  2834. }
  2835. /*
  2836. * The Lanai Z8E PCI-E interface achieves higher Read-DMA throughput
  2837. * when the PCI-E Completion packets are aligned on an 8-byte
  2838. * boundary. Some PCI-E chip sets always align Completion packets; on
  2839. * the ones that do not, the alignment can be enforced by enabling
  2840. * ECRC generation (if supported).
  2841. *
  2842. * When PCI-E Completion packets are not aligned, it is actually more
  2843. * efficient to limit Read-DMA transactions to 2KB, rather than 4KB.
  2844. *
  2845. * If the driver can neither enable ECRC nor verify that it has
  2846. * already been enabled, then it must use a firmware image which works
  2847. * around unaligned completion packets (myri10ge_rss_ethp_z8e.dat), and it
  2848. * should also ensure that it never gives the device a Read-DMA which is
  2849. * larger than 2KB by setting the tx_boundary to 2KB. If ECRC is
  2850. * enabled, then the driver should use the aligned (myri10ge_rss_eth_z8e.dat)
  2851. * firmware image, and set tx_boundary to 4KB.
  2852. */
  2853. static void myri10ge_firmware_probe(struct myri10ge_priv *mgp)
  2854. {
  2855. struct pci_dev *pdev = mgp->pdev;
  2856. struct device *dev = &pdev->dev;
  2857. int status;
  2858. mgp->tx_boundary = 4096;
  2859. /*
  2860. * Verify the max read request size was set to 4KB
  2861. * before trying the test with 4KB.
  2862. */
  2863. status = pcie_get_readrq(pdev);
  2864. if (status < 0) {
  2865. dev_err(dev, "Couldn't read max read req size: %d\n", status);
  2866. goto abort;
  2867. }
  2868. if (status != 4096) {
  2869. dev_warn(dev, "Max Read Request size != 4096 (%d)\n", status);
  2870. mgp->tx_boundary = 2048;
  2871. }
  2872. /*
  2873. * load the optimized firmware (which assumes aligned PCIe
  2874. * completions) in order to see if it works on this host.
  2875. */
  2876. set_fw_name(mgp, myri10ge_fw_aligned, false);
  2877. status = myri10ge_load_firmware(mgp, 1);
  2878. if (status != 0) {
  2879. goto abort;
  2880. }
  2881. /*
  2882. * Enable ECRC if possible
  2883. */
  2884. myri10ge_enable_ecrc(mgp);
  2885. /*
  2886. * Run a DMA test which watches for unaligned completions and
  2887. * aborts on the first one seen.
  2888. */
  2889. status = myri10ge_dma_test(mgp, MXGEFW_CMD_UNALIGNED_TEST);
  2890. if (status == 0)
  2891. return; /* keep the aligned firmware */
  2892. if (status != -E2BIG)
  2893. dev_warn(dev, "DMA test failed: %d\n", status);
  2894. if (status == -ENOSYS)
  2895. dev_warn(dev, "Falling back to ethp! "
  2896. "Please install up to date fw\n");
  2897. abort:
  2898. /* fall back to using the unaligned firmware */
  2899. mgp->tx_boundary = 2048;
  2900. set_fw_name(mgp, myri10ge_fw_unaligned, false);
  2901. }
  2902. static void myri10ge_select_firmware(struct myri10ge_priv *mgp)
  2903. {
  2904. int overridden = 0;
  2905. if (myri10ge_force_firmware == 0) {
  2906. int link_width;
  2907. u16 lnk;
  2908. pcie_capability_read_word(mgp->pdev, PCI_EXP_LNKSTA, &lnk);
  2909. link_width = (lnk >> 4) & 0x3f;
  2910. /* Check to see if Link is less than 8 or if the
  2911. * upstream bridge is known to provide aligned
  2912. * completions */
  2913. if (link_width < 8) {
  2914. dev_info(&mgp->pdev->dev, "PCIE x%d Link\n",
  2915. link_width);
  2916. mgp->tx_boundary = 4096;
  2917. set_fw_name(mgp, myri10ge_fw_aligned, false);
  2918. } else {
  2919. myri10ge_firmware_probe(mgp);
  2920. }
  2921. } else {
  2922. if (myri10ge_force_firmware == 1) {
  2923. dev_info(&mgp->pdev->dev,
  2924. "Assuming aligned completions (forced)\n");
  2925. mgp->tx_boundary = 4096;
  2926. set_fw_name(mgp, myri10ge_fw_aligned, false);
  2927. } else {
  2928. dev_info(&mgp->pdev->dev,
  2929. "Assuming unaligned completions (forced)\n");
  2930. mgp->tx_boundary = 2048;
  2931. set_fw_name(mgp, myri10ge_fw_unaligned, false);
  2932. }
  2933. }
  2934. kparam_block_sysfs_write(myri10ge_fw_name);
  2935. if (myri10ge_fw_name != NULL) {
  2936. char *fw_name = kstrdup(myri10ge_fw_name, GFP_KERNEL);
  2937. if (fw_name) {
  2938. overridden = 1;
  2939. set_fw_name(mgp, fw_name, true);
  2940. }
  2941. }
  2942. kparam_unblock_sysfs_write(myri10ge_fw_name);
  2943. if (mgp->board_number < MYRI10GE_MAX_BOARDS &&
  2944. myri10ge_fw_names[mgp->board_number] != NULL &&
  2945. strlen(myri10ge_fw_names[mgp->board_number])) {
  2946. set_fw_name(mgp, myri10ge_fw_names[mgp->board_number], false);
  2947. overridden = 1;
  2948. }
  2949. if (overridden)
  2950. dev_info(&mgp->pdev->dev, "overriding firmware to %s\n",
  2951. mgp->fw_name);
  2952. }
  2953. static void myri10ge_mask_surprise_down(struct pci_dev *pdev)
  2954. {
  2955. struct pci_dev *bridge = pdev->bus->self;
  2956. int cap;
  2957. u32 mask;
  2958. if (bridge == NULL)
  2959. return;
  2960. cap = pci_find_ext_capability(bridge, PCI_EXT_CAP_ID_ERR);
  2961. if (cap) {
  2962. /* a sram parity error can cause a surprise link
  2963. * down; since we expect and can recover from sram
  2964. * parity errors, mask surprise link down events */
  2965. pci_read_config_dword(bridge, cap + PCI_ERR_UNCOR_MASK, &mask);
  2966. mask |= 0x20;
  2967. pci_write_config_dword(bridge, cap + PCI_ERR_UNCOR_MASK, mask);
  2968. }
  2969. }
  2970. #ifdef CONFIG_PM
  2971. static int myri10ge_suspend(struct pci_dev *pdev, pm_message_t state)
  2972. {
  2973. struct myri10ge_priv *mgp;
  2974. struct net_device *netdev;
  2975. mgp = pci_get_drvdata(pdev);
  2976. if (mgp == NULL)
  2977. return -EINVAL;
  2978. netdev = mgp->dev;
  2979. netif_device_detach(netdev);
  2980. if (netif_running(netdev)) {
  2981. netdev_info(netdev, "closing\n");
  2982. rtnl_lock();
  2983. myri10ge_close(netdev);
  2984. rtnl_unlock();
  2985. }
  2986. myri10ge_dummy_rdma(mgp, 0);
  2987. pci_save_state(pdev);
  2988. pci_disable_device(pdev);
  2989. return pci_set_power_state(pdev, pci_choose_state(pdev, state));
  2990. }
  2991. static int myri10ge_resume(struct pci_dev *pdev)
  2992. {
  2993. struct myri10ge_priv *mgp;
  2994. struct net_device *netdev;
  2995. int status;
  2996. u16 vendor;
  2997. mgp = pci_get_drvdata(pdev);
  2998. if (mgp == NULL)
  2999. return -EINVAL;
  3000. netdev = mgp->dev;
  3001. pci_set_power_state(pdev, 0); /* zeros conf space as a side effect */
  3002. msleep(5); /* give card time to respond */
  3003. pci_read_config_word(mgp->pdev, PCI_VENDOR_ID, &vendor);
  3004. if (vendor == 0xffff) {
  3005. netdev_err(mgp->dev, "device disappeared!\n");
  3006. return -EIO;
  3007. }
  3008. pci_restore_state(pdev);
  3009. status = pci_enable_device(pdev);
  3010. if (status) {
  3011. dev_err(&pdev->dev, "failed to enable device\n");
  3012. return status;
  3013. }
  3014. pci_set_master(pdev);
  3015. myri10ge_reset(mgp);
  3016. myri10ge_dummy_rdma(mgp, 1);
  3017. /* Save configuration space to be restored if the
  3018. * nic resets due to a parity error */
  3019. pci_save_state(pdev);
  3020. if (netif_running(netdev)) {
  3021. rtnl_lock();
  3022. status = myri10ge_open(netdev);
  3023. rtnl_unlock();
  3024. if (status != 0)
  3025. goto abort_with_enabled;
  3026. }
  3027. netif_device_attach(netdev);
  3028. return 0;
  3029. abort_with_enabled:
  3030. pci_disable_device(pdev);
  3031. return -EIO;
  3032. }
  3033. #endif /* CONFIG_PM */
  3034. static u32 myri10ge_read_reboot(struct myri10ge_priv *mgp)
  3035. {
  3036. struct pci_dev *pdev = mgp->pdev;
  3037. int vs = mgp->vendor_specific_offset;
  3038. u32 reboot;
  3039. /*enter read32 mode */
  3040. pci_write_config_byte(pdev, vs + 0x10, 0x3);
  3041. /*read REBOOT_STATUS (0xfffffff0) */
  3042. pci_write_config_dword(pdev, vs + 0x18, 0xfffffff0);
  3043. pci_read_config_dword(pdev, vs + 0x14, &reboot);
  3044. return reboot;
  3045. }
  3046. static void
  3047. myri10ge_check_slice(struct myri10ge_slice_state *ss, int *reset_needed,
  3048. int *busy_slice_cnt, u32 rx_pause_cnt)
  3049. {
  3050. struct myri10ge_priv *mgp = ss->mgp;
  3051. int slice = ss - mgp->ss;
  3052. if (ss->tx.req != ss->tx.done &&
  3053. ss->tx.done == ss->watchdog_tx_done &&
  3054. ss->watchdog_tx_req != ss->watchdog_tx_done) {
  3055. /* nic seems like it might be stuck.. */
  3056. if (rx_pause_cnt != mgp->watchdog_pause) {
  3057. if (net_ratelimit())
  3058. netdev_warn(mgp->dev, "slice %d: TX paused, "
  3059. "check link partner\n", slice);
  3060. } else {
  3061. netdev_warn(mgp->dev,
  3062. "slice %d: TX stuck %d %d %d %d %d %d\n",
  3063. slice, ss->tx.queue_active, ss->tx.req,
  3064. ss->tx.done, ss->tx.pkt_start,
  3065. ss->tx.pkt_done,
  3066. (int)ntohl(mgp->ss[slice].fw_stats->
  3067. send_done_count));
  3068. *reset_needed = 1;
  3069. ss->stuck = 1;
  3070. }
  3071. }
  3072. if (ss->watchdog_tx_done != ss->tx.done ||
  3073. ss->watchdog_rx_done != ss->rx_done.cnt) {
  3074. *busy_slice_cnt += 1;
  3075. }
  3076. ss->watchdog_tx_done = ss->tx.done;
  3077. ss->watchdog_tx_req = ss->tx.req;
  3078. ss->watchdog_rx_done = ss->rx_done.cnt;
  3079. }
  3080. /*
  3081. * This watchdog is used to check whether the board has suffered
  3082. * from a parity error and needs to be recovered.
  3083. */
  3084. static void myri10ge_watchdog(struct work_struct *work)
  3085. {
  3086. struct myri10ge_priv *mgp =
  3087. container_of(work, struct myri10ge_priv, watchdog_work);
  3088. struct myri10ge_slice_state *ss;
  3089. u32 reboot, rx_pause_cnt;
  3090. int status, rebooted;
  3091. int i;
  3092. int reset_needed = 0;
  3093. int busy_slice_cnt = 0;
  3094. u16 cmd, vendor;
  3095. mgp->watchdog_resets++;
  3096. pci_read_config_word(mgp->pdev, PCI_COMMAND, &cmd);
  3097. rebooted = 0;
  3098. if ((cmd & PCI_COMMAND_MASTER) == 0) {
  3099. /* Bus master DMA disabled? Check to see
  3100. * if the card rebooted due to a parity error
  3101. * For now, just report it */
  3102. reboot = myri10ge_read_reboot(mgp);
  3103. netdev_err(mgp->dev, "NIC rebooted (0x%x),%s resetting\n",
  3104. reboot, myri10ge_reset_recover ? "" : " not");
  3105. if (myri10ge_reset_recover == 0)
  3106. return;
  3107. rtnl_lock();
  3108. mgp->rebooted = 1;
  3109. rebooted = 1;
  3110. myri10ge_close(mgp->dev);
  3111. myri10ge_reset_recover--;
  3112. mgp->rebooted = 0;
  3113. /*
  3114. * A rebooted nic will come back with config space as
  3115. * it was after power was applied to PCIe bus.
  3116. * Attempt to restore config space which was saved
  3117. * when the driver was loaded, or the last time the
  3118. * nic was resumed from power saving mode.
  3119. */
  3120. pci_restore_state(mgp->pdev);
  3121. /* save state again for accounting reasons */
  3122. pci_save_state(mgp->pdev);
  3123. } else {
  3124. /* if we get back -1's from our slot, perhaps somebody
  3125. * powered off our card. Don't try to reset it in
  3126. * this case */
  3127. if (cmd == 0xffff) {
  3128. pci_read_config_word(mgp->pdev, PCI_VENDOR_ID, &vendor);
  3129. if (vendor == 0xffff) {
  3130. netdev_err(mgp->dev, "device disappeared!\n");
  3131. return;
  3132. }
  3133. }
  3134. /* Perhaps it is a software error. See if stuck slice
  3135. * has recovered, reset if not */
  3136. rx_pause_cnt = ntohl(mgp->ss[0].fw_stats->dropped_pause);
  3137. for (i = 0; i < mgp->num_slices; i++) {
  3138. ss = mgp->ss;
  3139. if (ss->stuck) {
  3140. myri10ge_check_slice(ss, &reset_needed,
  3141. &busy_slice_cnt,
  3142. rx_pause_cnt);
  3143. ss->stuck = 0;
  3144. }
  3145. }
  3146. if (!reset_needed) {
  3147. netdev_dbg(mgp->dev, "not resetting\n");
  3148. return;
  3149. }
  3150. netdev_err(mgp->dev, "device timeout, resetting\n");
  3151. }
  3152. if (!rebooted) {
  3153. rtnl_lock();
  3154. myri10ge_close(mgp->dev);
  3155. }
  3156. status = myri10ge_load_firmware(mgp, 1);
  3157. if (status != 0)
  3158. netdev_err(mgp->dev, "failed to load firmware\n");
  3159. else
  3160. myri10ge_open(mgp->dev);
  3161. rtnl_unlock();
  3162. }
  3163. /*
  3164. * We use our own timer routine rather than relying upon
  3165. * netdev->tx_timeout because we have a very large hardware transmit
  3166. * queue. Due to the large queue, the netdev->tx_timeout function
  3167. * cannot detect a NIC with a parity error in a timely fashion if the
  3168. * NIC is lightly loaded.
  3169. */
  3170. static void myri10ge_watchdog_timer(unsigned long arg)
  3171. {
  3172. struct myri10ge_priv *mgp;
  3173. struct myri10ge_slice_state *ss;
  3174. int i, reset_needed, busy_slice_cnt;
  3175. u32 rx_pause_cnt;
  3176. u16 cmd;
  3177. mgp = (struct myri10ge_priv *)arg;
  3178. rx_pause_cnt = ntohl(mgp->ss[0].fw_stats->dropped_pause);
  3179. busy_slice_cnt = 0;
  3180. for (i = 0, reset_needed = 0;
  3181. i < mgp->num_slices && reset_needed == 0; ++i) {
  3182. ss = &mgp->ss[i];
  3183. if (ss->rx_small.watchdog_needed) {
  3184. myri10ge_alloc_rx_pages(mgp, &ss->rx_small,
  3185. mgp->small_bytes + MXGEFW_PAD,
  3186. 1);
  3187. if (ss->rx_small.fill_cnt - ss->rx_small.cnt >=
  3188. myri10ge_fill_thresh)
  3189. ss->rx_small.watchdog_needed = 0;
  3190. }
  3191. if (ss->rx_big.watchdog_needed) {
  3192. myri10ge_alloc_rx_pages(mgp, &ss->rx_big,
  3193. mgp->big_bytes, 1);
  3194. if (ss->rx_big.fill_cnt - ss->rx_big.cnt >=
  3195. myri10ge_fill_thresh)
  3196. ss->rx_big.watchdog_needed = 0;
  3197. }
  3198. myri10ge_check_slice(ss, &reset_needed, &busy_slice_cnt,
  3199. rx_pause_cnt);
  3200. }
  3201. /* if we've sent or received no traffic, poll the NIC to
  3202. * ensure it is still there. Otherwise, we risk not noticing
  3203. * an error in a timely fashion */
  3204. if (busy_slice_cnt == 0) {
  3205. pci_read_config_word(mgp->pdev, PCI_COMMAND, &cmd);
  3206. if ((cmd & PCI_COMMAND_MASTER) == 0) {
  3207. reset_needed = 1;
  3208. }
  3209. }
  3210. mgp->watchdog_pause = rx_pause_cnt;
  3211. if (reset_needed) {
  3212. schedule_work(&mgp->watchdog_work);
  3213. } else {
  3214. /* rearm timer */
  3215. mod_timer(&mgp->watchdog_timer,
  3216. jiffies + myri10ge_watchdog_timeout * HZ);
  3217. }
  3218. }
  3219. static void myri10ge_free_slices(struct myri10ge_priv *mgp)
  3220. {
  3221. struct myri10ge_slice_state *ss;
  3222. struct pci_dev *pdev = mgp->pdev;
  3223. size_t bytes;
  3224. int i;
  3225. if (mgp->ss == NULL)
  3226. return;
  3227. for (i = 0; i < mgp->num_slices; i++) {
  3228. ss = &mgp->ss[i];
  3229. if (ss->rx_done.entry != NULL) {
  3230. bytes = mgp->max_intr_slots *
  3231. sizeof(*ss->rx_done.entry);
  3232. dma_free_coherent(&pdev->dev, bytes,
  3233. ss->rx_done.entry, ss->rx_done.bus);
  3234. ss->rx_done.entry = NULL;
  3235. }
  3236. if (ss->fw_stats != NULL) {
  3237. bytes = sizeof(*ss->fw_stats);
  3238. dma_free_coherent(&pdev->dev, bytes,
  3239. ss->fw_stats, ss->fw_stats_bus);
  3240. ss->fw_stats = NULL;
  3241. }
  3242. netif_napi_del(&ss->napi);
  3243. }
  3244. kfree(mgp->ss);
  3245. mgp->ss = NULL;
  3246. }
  3247. static int myri10ge_alloc_slices(struct myri10ge_priv *mgp)
  3248. {
  3249. struct myri10ge_slice_state *ss;
  3250. struct pci_dev *pdev = mgp->pdev;
  3251. size_t bytes;
  3252. int i;
  3253. bytes = sizeof(*mgp->ss) * mgp->num_slices;
  3254. mgp->ss = kzalloc(bytes, GFP_KERNEL);
  3255. if (mgp->ss == NULL) {
  3256. return -ENOMEM;
  3257. }
  3258. for (i = 0; i < mgp->num_slices; i++) {
  3259. ss = &mgp->ss[i];
  3260. bytes = mgp->max_intr_slots * sizeof(*ss->rx_done.entry);
  3261. ss->rx_done.entry = dma_alloc_coherent(&pdev->dev, bytes,
  3262. &ss->rx_done.bus,
  3263. GFP_KERNEL);
  3264. if (ss->rx_done.entry == NULL)
  3265. goto abort;
  3266. memset(ss->rx_done.entry, 0, bytes);
  3267. bytes = sizeof(*ss->fw_stats);
  3268. ss->fw_stats = dma_alloc_coherent(&pdev->dev, bytes,
  3269. &ss->fw_stats_bus,
  3270. GFP_KERNEL);
  3271. if (ss->fw_stats == NULL)
  3272. goto abort;
  3273. ss->mgp = mgp;
  3274. ss->dev = mgp->dev;
  3275. netif_napi_add(ss->dev, &ss->napi, myri10ge_poll,
  3276. myri10ge_napi_weight);
  3277. }
  3278. return 0;
  3279. abort:
  3280. myri10ge_free_slices(mgp);
  3281. return -ENOMEM;
  3282. }
  3283. /*
  3284. * This function determines the number of slices supported.
  3285. * The number slices is the minimum of the number of CPUS,
  3286. * the number of MSI-X irqs supported, the number of slices
  3287. * supported by the firmware
  3288. */
  3289. static void myri10ge_probe_slices(struct myri10ge_priv *mgp)
  3290. {
  3291. struct myri10ge_cmd cmd;
  3292. struct pci_dev *pdev = mgp->pdev;
  3293. char *old_fw;
  3294. bool old_allocated;
  3295. int i, status, ncpus, msix_cap;
  3296. mgp->num_slices = 1;
  3297. msix_cap = pci_find_capability(pdev, PCI_CAP_ID_MSIX);
  3298. ncpus = netif_get_num_default_rss_queues();
  3299. if (myri10ge_max_slices == 1 || msix_cap == 0 ||
  3300. (myri10ge_max_slices == -1 && ncpus < 2))
  3301. return;
  3302. /* try to load the slice aware rss firmware */
  3303. old_fw = mgp->fw_name;
  3304. old_allocated = mgp->fw_name_allocated;
  3305. /* don't free old_fw if we override it. */
  3306. mgp->fw_name_allocated = false;
  3307. if (myri10ge_fw_name != NULL) {
  3308. dev_info(&mgp->pdev->dev, "overriding rss firmware to %s\n",
  3309. myri10ge_fw_name);
  3310. set_fw_name(mgp, myri10ge_fw_name, false);
  3311. } else if (old_fw == myri10ge_fw_aligned)
  3312. set_fw_name(mgp, myri10ge_fw_rss_aligned, false);
  3313. else
  3314. set_fw_name(mgp, myri10ge_fw_rss_unaligned, false);
  3315. status = myri10ge_load_firmware(mgp, 0);
  3316. if (status != 0) {
  3317. dev_info(&pdev->dev, "Rss firmware not found\n");
  3318. if (old_allocated)
  3319. kfree(old_fw);
  3320. return;
  3321. }
  3322. /* hit the board with a reset to ensure it is alive */
  3323. memset(&cmd, 0, sizeof(cmd));
  3324. status = myri10ge_send_cmd(mgp, MXGEFW_CMD_RESET, &cmd, 0);
  3325. if (status != 0) {
  3326. dev_err(&mgp->pdev->dev, "failed reset\n");
  3327. goto abort_with_fw;
  3328. }
  3329. mgp->max_intr_slots = cmd.data0 / sizeof(struct mcp_slot);
  3330. /* tell it the size of the interrupt queues */
  3331. cmd.data0 = mgp->max_intr_slots * sizeof(struct mcp_slot);
  3332. status = myri10ge_send_cmd(mgp, MXGEFW_CMD_SET_INTRQ_SIZE, &cmd, 0);
  3333. if (status != 0) {
  3334. dev_err(&mgp->pdev->dev, "failed MXGEFW_CMD_SET_INTRQ_SIZE\n");
  3335. goto abort_with_fw;
  3336. }
  3337. /* ask the maximum number of slices it supports */
  3338. status = myri10ge_send_cmd(mgp, MXGEFW_CMD_GET_MAX_RSS_QUEUES, &cmd, 0);
  3339. if (status != 0)
  3340. goto abort_with_fw;
  3341. else
  3342. mgp->num_slices = cmd.data0;
  3343. /* Only allow multiple slices if MSI-X is usable */
  3344. if (!myri10ge_msi) {
  3345. goto abort_with_fw;
  3346. }
  3347. /* if the admin did not specify a limit to how many
  3348. * slices we should use, cap it automatically to the
  3349. * number of CPUs currently online */
  3350. if (myri10ge_max_slices == -1)
  3351. myri10ge_max_slices = ncpus;
  3352. if (mgp->num_slices > myri10ge_max_slices)
  3353. mgp->num_slices = myri10ge_max_slices;
  3354. /* Now try to allocate as many MSI-X vectors as we have
  3355. * slices. We give up on MSI-X if we can only get a single
  3356. * vector. */
  3357. mgp->msix_vectors = kcalloc(mgp->num_slices, sizeof(*mgp->msix_vectors),
  3358. GFP_KERNEL);
  3359. if (mgp->msix_vectors == NULL)
  3360. goto disable_msix;
  3361. for (i = 0; i < mgp->num_slices; i++) {
  3362. mgp->msix_vectors[i].entry = i;
  3363. }
  3364. while (mgp->num_slices > 1) {
  3365. /* make sure it is a power of two */
  3366. while (!is_power_of_2(mgp->num_slices))
  3367. mgp->num_slices--;
  3368. if (mgp->num_slices == 1)
  3369. goto disable_msix;
  3370. status = pci_enable_msix(pdev, mgp->msix_vectors,
  3371. mgp->num_slices);
  3372. if (status == 0) {
  3373. pci_disable_msix(pdev);
  3374. if (old_allocated)
  3375. kfree(old_fw);
  3376. return;
  3377. }
  3378. if (status > 0)
  3379. mgp->num_slices = status;
  3380. else
  3381. goto disable_msix;
  3382. }
  3383. disable_msix:
  3384. if (mgp->msix_vectors != NULL) {
  3385. kfree(mgp->msix_vectors);
  3386. mgp->msix_vectors = NULL;
  3387. }
  3388. abort_with_fw:
  3389. mgp->num_slices = 1;
  3390. set_fw_name(mgp, old_fw, old_allocated);
  3391. myri10ge_load_firmware(mgp, 0);
  3392. }
  3393. static const struct net_device_ops myri10ge_netdev_ops = {
  3394. .ndo_open = myri10ge_open,
  3395. .ndo_stop = myri10ge_close,
  3396. .ndo_start_xmit = myri10ge_xmit,
  3397. .ndo_get_stats64 = myri10ge_get_stats,
  3398. .ndo_validate_addr = eth_validate_addr,
  3399. .ndo_change_mtu = myri10ge_change_mtu,
  3400. .ndo_fix_features = myri10ge_fix_features,
  3401. .ndo_set_rx_mode = myri10ge_set_multicast_list,
  3402. .ndo_set_mac_address = myri10ge_set_mac_address,
  3403. };
  3404. static int myri10ge_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
  3405. {
  3406. struct net_device *netdev;
  3407. struct myri10ge_priv *mgp;
  3408. struct device *dev = &pdev->dev;
  3409. int i;
  3410. int status = -ENXIO;
  3411. int dac_enabled;
  3412. unsigned hdr_offset, ss_offset;
  3413. static int board_number;
  3414. netdev = alloc_etherdev_mq(sizeof(*mgp), MYRI10GE_MAX_SLICES);
  3415. if (netdev == NULL)
  3416. return -ENOMEM;
  3417. SET_NETDEV_DEV(netdev, &pdev->dev);
  3418. mgp = netdev_priv(netdev);
  3419. mgp->dev = netdev;
  3420. mgp->pdev = pdev;
  3421. mgp->pause = myri10ge_flow_control;
  3422. mgp->intr_coal_delay = myri10ge_intr_coal_delay;
  3423. mgp->msg_enable = netif_msg_init(myri10ge_debug, MYRI10GE_MSG_DEFAULT);
  3424. mgp->board_number = board_number;
  3425. init_waitqueue_head(&mgp->down_wq);
  3426. if (pci_enable_device(pdev)) {
  3427. dev_err(&pdev->dev, "pci_enable_device call failed\n");
  3428. status = -ENODEV;
  3429. goto abort_with_netdev;
  3430. }
  3431. /* Find the vendor-specific cap so we can check
  3432. * the reboot register later on */
  3433. mgp->vendor_specific_offset
  3434. = pci_find_capability(pdev, PCI_CAP_ID_VNDR);
  3435. /* Set our max read request to 4KB */
  3436. status = pcie_set_readrq(pdev, 4096);
  3437. if (status != 0) {
  3438. dev_err(&pdev->dev, "Error %d writing PCI_EXP_DEVCTL\n",
  3439. status);
  3440. goto abort_with_enabled;
  3441. }
  3442. myri10ge_mask_surprise_down(pdev);
  3443. pci_set_master(pdev);
  3444. dac_enabled = 1;
  3445. status = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
  3446. if (status != 0) {
  3447. dac_enabled = 0;
  3448. dev_err(&pdev->dev,
  3449. "64-bit pci address mask was refused, "
  3450. "trying 32-bit\n");
  3451. status = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  3452. }
  3453. if (status != 0) {
  3454. dev_err(&pdev->dev, "Error %d setting DMA mask\n", status);
  3455. goto abort_with_enabled;
  3456. }
  3457. (void)pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
  3458. mgp->cmd = dma_alloc_coherent(&pdev->dev, sizeof(*mgp->cmd),
  3459. &mgp->cmd_bus, GFP_KERNEL);
  3460. if (mgp->cmd == NULL)
  3461. goto abort_with_enabled;
  3462. mgp->board_span = pci_resource_len(pdev, 0);
  3463. mgp->iomem_base = pci_resource_start(pdev, 0);
  3464. mgp->mtrr = -1;
  3465. mgp->wc_enabled = 0;
  3466. #ifdef CONFIG_MTRR
  3467. mgp->mtrr = mtrr_add(mgp->iomem_base, mgp->board_span,
  3468. MTRR_TYPE_WRCOMB, 1);
  3469. if (mgp->mtrr >= 0)
  3470. mgp->wc_enabled = 1;
  3471. #endif
  3472. mgp->sram = ioremap_wc(mgp->iomem_base, mgp->board_span);
  3473. if (mgp->sram == NULL) {
  3474. dev_err(&pdev->dev, "ioremap failed for %ld bytes at 0x%lx\n",
  3475. mgp->board_span, mgp->iomem_base);
  3476. status = -ENXIO;
  3477. goto abort_with_mtrr;
  3478. }
  3479. hdr_offset =
  3480. ntohl(__raw_readl(mgp->sram + MCP_HEADER_PTR_OFFSET)) & 0xffffc;
  3481. ss_offset = hdr_offset + offsetof(struct mcp_gen_header, string_specs);
  3482. mgp->sram_size = ntohl(__raw_readl(mgp->sram + ss_offset));
  3483. if (mgp->sram_size > mgp->board_span ||
  3484. mgp->sram_size <= MYRI10GE_FW_OFFSET) {
  3485. dev_err(&pdev->dev,
  3486. "invalid sram_size %dB or board span %ldB\n",
  3487. mgp->sram_size, mgp->board_span);
  3488. goto abort_with_ioremap;
  3489. }
  3490. memcpy_fromio(mgp->eeprom_strings,
  3491. mgp->sram + mgp->sram_size, MYRI10GE_EEPROM_STRINGS_SIZE);
  3492. memset(mgp->eeprom_strings + MYRI10GE_EEPROM_STRINGS_SIZE - 2, 0, 2);
  3493. status = myri10ge_read_mac_addr(mgp);
  3494. if (status)
  3495. goto abort_with_ioremap;
  3496. for (i = 0; i < ETH_ALEN; i++)
  3497. netdev->dev_addr[i] = mgp->mac_addr[i];
  3498. myri10ge_select_firmware(mgp);
  3499. status = myri10ge_load_firmware(mgp, 1);
  3500. if (status != 0) {
  3501. dev_err(&pdev->dev, "failed to load firmware\n");
  3502. goto abort_with_ioremap;
  3503. }
  3504. myri10ge_probe_slices(mgp);
  3505. status = myri10ge_alloc_slices(mgp);
  3506. if (status != 0) {
  3507. dev_err(&pdev->dev, "failed to alloc slice state\n");
  3508. goto abort_with_firmware;
  3509. }
  3510. netif_set_real_num_tx_queues(netdev, mgp->num_slices);
  3511. netif_set_real_num_rx_queues(netdev, mgp->num_slices);
  3512. status = myri10ge_reset(mgp);
  3513. if (status != 0) {
  3514. dev_err(&pdev->dev, "failed reset\n");
  3515. goto abort_with_slices;
  3516. }
  3517. #ifdef CONFIG_MYRI10GE_DCA
  3518. myri10ge_setup_dca(mgp);
  3519. #endif
  3520. pci_set_drvdata(pdev, mgp);
  3521. if ((myri10ge_initial_mtu + ETH_HLEN) > MYRI10GE_MAX_ETHER_MTU)
  3522. myri10ge_initial_mtu = MYRI10GE_MAX_ETHER_MTU - ETH_HLEN;
  3523. if ((myri10ge_initial_mtu + ETH_HLEN) < 68)
  3524. myri10ge_initial_mtu = 68;
  3525. netdev->netdev_ops = &myri10ge_netdev_ops;
  3526. netdev->mtu = myri10ge_initial_mtu;
  3527. netdev->hw_features = mgp->features | NETIF_F_LRO | NETIF_F_RXCSUM;
  3528. netdev->features = netdev->hw_features;
  3529. if (dac_enabled)
  3530. netdev->features |= NETIF_F_HIGHDMA;
  3531. netdev->vlan_features |= mgp->features;
  3532. if (mgp->fw_ver_tiny < 37)
  3533. netdev->vlan_features &= ~NETIF_F_TSO6;
  3534. if (mgp->fw_ver_tiny < 32)
  3535. netdev->vlan_features &= ~NETIF_F_TSO;
  3536. /* make sure we can get an irq, and that MSI can be
  3537. * setup (if available). */
  3538. status = myri10ge_request_irq(mgp);
  3539. if (status != 0)
  3540. goto abort_with_firmware;
  3541. myri10ge_free_irq(mgp);
  3542. /* Save configuration space to be restored if the
  3543. * nic resets due to a parity error */
  3544. pci_save_state(pdev);
  3545. /* Setup the watchdog timer */
  3546. setup_timer(&mgp->watchdog_timer, myri10ge_watchdog_timer,
  3547. (unsigned long)mgp);
  3548. SET_ETHTOOL_OPS(netdev, &myri10ge_ethtool_ops);
  3549. INIT_WORK(&mgp->watchdog_work, myri10ge_watchdog);
  3550. status = register_netdev(netdev);
  3551. if (status != 0) {
  3552. dev_err(&pdev->dev, "register_netdev failed: %d\n", status);
  3553. goto abort_with_state;
  3554. }
  3555. if (mgp->msix_enabled)
  3556. dev_info(dev, "%d MSI-X IRQs, tx bndry %d, fw %s, WC %s\n",
  3557. mgp->num_slices, mgp->tx_boundary, mgp->fw_name,
  3558. (mgp->wc_enabled ? "Enabled" : "Disabled"));
  3559. else
  3560. dev_info(dev, "%s IRQ %d, tx bndry %d, fw %s, WC %s\n",
  3561. mgp->msi_enabled ? "MSI" : "xPIC",
  3562. pdev->irq, mgp->tx_boundary, mgp->fw_name,
  3563. (mgp->wc_enabled ? "Enabled" : "Disabled"));
  3564. board_number++;
  3565. return 0;
  3566. abort_with_state:
  3567. pci_restore_state(pdev);
  3568. abort_with_slices:
  3569. myri10ge_free_slices(mgp);
  3570. abort_with_firmware:
  3571. myri10ge_dummy_rdma(mgp, 0);
  3572. abort_with_ioremap:
  3573. if (mgp->mac_addr_string != NULL)
  3574. dev_err(&pdev->dev,
  3575. "myri10ge_probe() failed: MAC=%s, SN=%ld\n",
  3576. mgp->mac_addr_string, mgp->serial_number);
  3577. iounmap(mgp->sram);
  3578. abort_with_mtrr:
  3579. #ifdef CONFIG_MTRR
  3580. if (mgp->mtrr >= 0)
  3581. mtrr_del(mgp->mtrr, mgp->iomem_base, mgp->board_span);
  3582. #endif
  3583. dma_free_coherent(&pdev->dev, sizeof(*mgp->cmd),
  3584. mgp->cmd, mgp->cmd_bus);
  3585. abort_with_enabled:
  3586. pci_disable_device(pdev);
  3587. abort_with_netdev:
  3588. set_fw_name(mgp, NULL, false);
  3589. free_netdev(netdev);
  3590. return status;
  3591. }
  3592. /*
  3593. * myri10ge_remove
  3594. *
  3595. * Does what is necessary to shutdown one Myrinet device. Called
  3596. * once for each Myrinet card by the kernel when a module is
  3597. * unloaded.
  3598. */
  3599. static void myri10ge_remove(struct pci_dev *pdev)
  3600. {
  3601. struct myri10ge_priv *mgp;
  3602. struct net_device *netdev;
  3603. mgp = pci_get_drvdata(pdev);
  3604. if (mgp == NULL)
  3605. return;
  3606. cancel_work_sync(&mgp->watchdog_work);
  3607. netdev = mgp->dev;
  3608. unregister_netdev(netdev);
  3609. #ifdef CONFIG_MYRI10GE_DCA
  3610. myri10ge_teardown_dca(mgp);
  3611. #endif
  3612. myri10ge_dummy_rdma(mgp, 0);
  3613. /* avoid a memory leak */
  3614. pci_restore_state(pdev);
  3615. iounmap(mgp->sram);
  3616. #ifdef CONFIG_MTRR
  3617. if (mgp->mtrr >= 0)
  3618. mtrr_del(mgp->mtrr, mgp->iomem_base, mgp->board_span);
  3619. #endif
  3620. myri10ge_free_slices(mgp);
  3621. if (mgp->msix_vectors != NULL)
  3622. kfree(mgp->msix_vectors);
  3623. dma_free_coherent(&pdev->dev, sizeof(*mgp->cmd),
  3624. mgp->cmd, mgp->cmd_bus);
  3625. set_fw_name(mgp, NULL, false);
  3626. free_netdev(netdev);
  3627. pci_disable_device(pdev);
  3628. pci_set_drvdata(pdev, NULL);
  3629. }
  3630. #define PCI_DEVICE_ID_MYRICOM_MYRI10GE_Z8E 0x0008
  3631. #define PCI_DEVICE_ID_MYRICOM_MYRI10GE_Z8E_9 0x0009
  3632. static DEFINE_PCI_DEVICE_TABLE(myri10ge_pci_tbl) = {
  3633. {PCI_DEVICE(PCI_VENDOR_ID_MYRICOM, PCI_DEVICE_ID_MYRICOM_MYRI10GE_Z8E)},
  3634. {PCI_DEVICE
  3635. (PCI_VENDOR_ID_MYRICOM, PCI_DEVICE_ID_MYRICOM_MYRI10GE_Z8E_9)},
  3636. {0},
  3637. };
  3638. MODULE_DEVICE_TABLE(pci, myri10ge_pci_tbl);
  3639. static struct pci_driver myri10ge_driver = {
  3640. .name = "myri10ge",
  3641. .probe = myri10ge_probe,
  3642. .remove = myri10ge_remove,
  3643. .id_table = myri10ge_pci_tbl,
  3644. #ifdef CONFIG_PM
  3645. .suspend = myri10ge_suspend,
  3646. .resume = myri10ge_resume,
  3647. #endif
  3648. };
  3649. #ifdef CONFIG_MYRI10GE_DCA
  3650. static int
  3651. myri10ge_notify_dca(struct notifier_block *nb, unsigned long event, void *p)
  3652. {
  3653. int err = driver_for_each_device(&myri10ge_driver.driver,
  3654. NULL, &event,
  3655. myri10ge_notify_dca_device);
  3656. if (err)
  3657. return NOTIFY_BAD;
  3658. return NOTIFY_DONE;
  3659. }
  3660. static struct notifier_block myri10ge_dca_notifier = {
  3661. .notifier_call = myri10ge_notify_dca,
  3662. .next = NULL,
  3663. .priority = 0,
  3664. };
  3665. #endif /* CONFIG_MYRI10GE_DCA */
  3666. static __init int myri10ge_init_module(void)
  3667. {
  3668. pr_info("Version %s\n", MYRI10GE_VERSION_STR);
  3669. if (myri10ge_rss_hash > MXGEFW_RSS_HASH_TYPE_MAX) {
  3670. pr_err("Illegal rssh hash type %d, defaulting to source port\n",
  3671. myri10ge_rss_hash);
  3672. myri10ge_rss_hash = MXGEFW_RSS_HASH_TYPE_SRC_PORT;
  3673. }
  3674. #ifdef CONFIG_MYRI10GE_DCA
  3675. dca_register_notify(&myri10ge_dca_notifier);
  3676. #endif
  3677. if (myri10ge_max_slices > MYRI10GE_MAX_SLICES)
  3678. myri10ge_max_slices = MYRI10GE_MAX_SLICES;
  3679. return pci_register_driver(&myri10ge_driver);
  3680. }
  3681. module_init(myri10ge_init_module);
  3682. static __exit void myri10ge_cleanup_module(void)
  3683. {
  3684. #ifdef CONFIG_MYRI10GE_DCA
  3685. dca_unregister_notify(&myri10ge_dca_notifier);
  3686. #endif
  3687. pci_unregister_driver(&myri10ge_driver);
  3688. }
  3689. module_exit(myri10ge_cleanup_module);