myri10ge.c 113 KB

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