myri10ge.c 114 KB

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