myri10ge.c 113 KB

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