myri10ge.c 113 KB

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