myri10ge.c 114 KB

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