myri10ge.c 111 KB

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