myri10ge.c 111 KB

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