myri10ge.c 108 KB

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