myri10ge.c 92 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300
  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/ip.h>
  52. #include <linux/inet.h>
  53. #include <linux/in.h>
  54. #include <linux/ethtool.h>
  55. #include <linux/firmware.h>
  56. #include <linux/delay.h>
  57. #include <linux/version.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.3.2-1.287"
  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. u8 __iomem *wc_fifo; /* w/c rx dma addr fifo address */
  115. struct mcp_kreq_ether_recv *shadow; /* host shadow of recv ring */
  116. struct myri10ge_rx_buffer_state *info;
  117. struct page *page;
  118. dma_addr_t bus;
  119. int page_offset;
  120. int cnt;
  121. int fill_cnt;
  122. int alloc_fail;
  123. int mask; /* number of rx slots -1 */
  124. int watchdog_needed;
  125. };
  126. struct myri10ge_tx_buf {
  127. struct mcp_kreq_ether_send __iomem *lanai; /* lanai ptr for sendq */
  128. u8 __iomem *wc_fifo; /* w/c send fifo address */
  129. struct mcp_kreq_ether_send *req_list; /* host shadow of sendq */
  130. char *req_bytes;
  131. struct myri10ge_tx_buffer_state *info;
  132. int mask; /* number of transmit slots -1 */
  133. int boundary; /* boundary transmits cannot cross */
  134. int req ____cacheline_aligned; /* transmit slots submitted */
  135. int pkt_start; /* packets started */
  136. int done ____cacheline_aligned; /* transmit slots completed */
  137. int pkt_done; /* packets completed */
  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_priv {
  148. int running; /* running? */
  149. int csum_flag; /* rx_csums? */
  150. struct myri10ge_tx_buf tx; /* transmit ring */
  151. struct myri10ge_rx_buf rx_small;
  152. struct myri10ge_rx_buf rx_big;
  153. struct myri10ge_rx_done rx_done;
  154. int small_bytes;
  155. int big_bytes;
  156. struct net_device *dev;
  157. struct napi_struct napi;
  158. struct net_device_stats stats;
  159. u8 __iomem *sram;
  160. int sram_size;
  161. unsigned long board_span;
  162. unsigned long iomem_base;
  163. __be32 __iomem *irq_claim;
  164. __be32 __iomem *irq_deassert;
  165. char *mac_addr_string;
  166. struct mcp_cmd_response *cmd;
  167. dma_addr_t cmd_bus;
  168. struct mcp_irq_data *fw_stats;
  169. dma_addr_t fw_stats_bus;
  170. struct pci_dev *pdev;
  171. int msi_enabled;
  172. __be32 link_state;
  173. unsigned int rdma_tags_available;
  174. int intr_coal_delay;
  175. __be32 __iomem *intr_coal_delay_ptr;
  176. int mtrr;
  177. int wc_enabled;
  178. int wake_queue;
  179. int stop_queue;
  180. int down_cnt;
  181. wait_queue_head_t down_wq;
  182. struct work_struct watchdog_work;
  183. struct timer_list watchdog_timer;
  184. int watchdog_tx_done;
  185. int watchdog_tx_req;
  186. int watchdog_pause;
  187. int watchdog_resets;
  188. int tx_linearized;
  189. int pause;
  190. char *fw_name;
  191. char eeprom_strings[MYRI10GE_EEPROM_STRINGS_SIZE];
  192. char fw_version[128];
  193. int fw_ver_major;
  194. int fw_ver_minor;
  195. int fw_ver_tiny;
  196. int adopted_rx_filter_bug;
  197. u8 mac_addr[6]; /* eeprom mac address */
  198. unsigned long serial_number;
  199. int vendor_specific_offset;
  200. int fw_multicast_support;
  201. unsigned long features;
  202. u32 max_tso6;
  203. u32 read_dma;
  204. u32 write_dma;
  205. u32 read_write_dma;
  206. u32 link_changes;
  207. u32 msg_enable;
  208. };
  209. static char *myri10ge_fw_unaligned = "myri10ge_ethp_z8e.dat";
  210. static char *myri10ge_fw_aligned = "myri10ge_eth_z8e.dat";
  211. static char *myri10ge_fw_name = NULL;
  212. module_param(myri10ge_fw_name, charp, S_IRUGO | S_IWUSR);
  213. MODULE_PARM_DESC(myri10ge_fw_name, "Firmware image name\n");
  214. static int myri10ge_ecrc_enable = 1;
  215. module_param(myri10ge_ecrc_enable, int, S_IRUGO);
  216. MODULE_PARM_DESC(myri10ge_ecrc_enable, "Enable Extended CRC on PCI-E\n");
  217. static int myri10ge_max_intr_slots = 1024;
  218. module_param(myri10ge_max_intr_slots, int, S_IRUGO);
  219. MODULE_PARM_DESC(myri10ge_max_intr_slots, "Interrupt queue slots\n");
  220. static int myri10ge_small_bytes = -1; /* -1 == auto */
  221. module_param(myri10ge_small_bytes, int, S_IRUGO | S_IWUSR);
  222. MODULE_PARM_DESC(myri10ge_small_bytes, "Threshold of small packets\n");
  223. static int myri10ge_msi = 1; /* enable msi by default */
  224. module_param(myri10ge_msi, int, S_IRUGO | S_IWUSR);
  225. MODULE_PARM_DESC(myri10ge_msi, "Enable Message Signalled Interrupts\n");
  226. static int myri10ge_intr_coal_delay = 75;
  227. module_param(myri10ge_intr_coal_delay, int, S_IRUGO);
  228. MODULE_PARM_DESC(myri10ge_intr_coal_delay, "Interrupt coalescing delay\n");
  229. static int myri10ge_flow_control = 1;
  230. module_param(myri10ge_flow_control, int, S_IRUGO);
  231. MODULE_PARM_DESC(myri10ge_flow_control, "Pause parameter\n");
  232. static int myri10ge_deassert_wait = 1;
  233. module_param(myri10ge_deassert_wait, int, S_IRUGO | S_IWUSR);
  234. MODULE_PARM_DESC(myri10ge_deassert_wait,
  235. "Wait when deasserting legacy interrupts\n");
  236. static int myri10ge_force_firmware = 0;
  237. module_param(myri10ge_force_firmware, int, S_IRUGO);
  238. MODULE_PARM_DESC(myri10ge_force_firmware,
  239. "Force firmware to assume aligned completions\n");
  240. static int myri10ge_initial_mtu = MYRI10GE_MAX_ETHER_MTU - ETH_HLEN;
  241. module_param(myri10ge_initial_mtu, int, S_IRUGO);
  242. MODULE_PARM_DESC(myri10ge_initial_mtu, "Initial MTU\n");
  243. static int myri10ge_napi_weight = 64;
  244. module_param(myri10ge_napi_weight, int, S_IRUGO);
  245. MODULE_PARM_DESC(myri10ge_napi_weight, "Set NAPI weight\n");
  246. static int myri10ge_watchdog_timeout = 1;
  247. module_param(myri10ge_watchdog_timeout, int, S_IRUGO);
  248. MODULE_PARM_DESC(myri10ge_watchdog_timeout, "Set watchdog timeout\n");
  249. static int myri10ge_max_irq_loops = 1048576;
  250. module_param(myri10ge_max_irq_loops, int, S_IRUGO);
  251. MODULE_PARM_DESC(myri10ge_max_irq_loops,
  252. "Set stuck legacy IRQ detection threshold\n");
  253. #define MYRI10GE_MSG_DEFAULT NETIF_MSG_LINK
  254. static int myri10ge_debug = -1; /* defaults above */
  255. module_param(myri10ge_debug, int, 0);
  256. MODULE_PARM_DESC(myri10ge_debug, "Debug level (0=none,...,16=all)");
  257. static int myri10ge_lro = 1;
  258. module_param(myri10ge_lro, int, S_IRUGO);
  259. MODULE_PARM_DESC(myri10ge_lro, "Enable large receive offload\n");
  260. static int myri10ge_lro_max_pkts = MYRI10GE_LRO_MAX_PKTS;
  261. module_param(myri10ge_lro_max_pkts, int, S_IRUGO);
  262. MODULE_PARM_DESC(myri10ge_lro, "Number of LRO packets to be aggregated\n");
  263. static int myri10ge_fill_thresh = 256;
  264. module_param(myri10ge_fill_thresh, int, S_IRUGO | S_IWUSR);
  265. MODULE_PARM_DESC(myri10ge_fill_thresh, "Number of empty rx slots allowed\n");
  266. static int myri10ge_reset_recover = 1;
  267. static int myri10ge_wcfifo = 0;
  268. module_param(myri10ge_wcfifo, int, S_IRUGO);
  269. MODULE_PARM_DESC(myri10ge_wcfifo, "Enable WC Fifo when WC is enabled\n");
  270. #define MYRI10GE_FW_OFFSET 1024*1024
  271. #define MYRI10GE_HIGHPART_TO_U32(X) \
  272. (sizeof (X) == 8) ? ((u32)((u64)(X) >> 32)) : (0)
  273. #define MYRI10GE_LOWPART_TO_U32(X) ((u32)(X))
  274. #define myri10ge_pio_copy(to,from,size) __iowrite64_copy(to,from,size/8)
  275. static void myri10ge_set_multicast_list(struct net_device *dev);
  276. static int myri10ge_sw_tso(struct sk_buff *skb, struct net_device *dev);
  277. static inline void put_be32(__be32 val, __be32 __iomem * p)
  278. {
  279. __raw_writel((__force __u32) val, (__force void __iomem *)p);
  280. }
  281. static int
  282. myri10ge_send_cmd(struct myri10ge_priv *mgp, u32 cmd,
  283. struct myri10ge_cmd *data, int atomic)
  284. {
  285. struct mcp_cmd *buf;
  286. char buf_bytes[sizeof(*buf) + 8];
  287. struct mcp_cmd_response *response = mgp->cmd;
  288. char __iomem *cmd_addr = mgp->sram + MXGEFW_ETH_CMD;
  289. u32 dma_low, dma_high, result, value;
  290. int sleep_total = 0;
  291. /* ensure buf is aligned to 8 bytes */
  292. buf = (struct mcp_cmd *)ALIGN((unsigned long)buf_bytes, 8);
  293. buf->data0 = htonl(data->data0);
  294. buf->data1 = htonl(data->data1);
  295. buf->data2 = htonl(data->data2);
  296. buf->cmd = htonl(cmd);
  297. dma_low = MYRI10GE_LOWPART_TO_U32(mgp->cmd_bus);
  298. dma_high = MYRI10GE_HIGHPART_TO_U32(mgp->cmd_bus);
  299. buf->response_addr.low = htonl(dma_low);
  300. buf->response_addr.high = htonl(dma_high);
  301. response->result = htonl(MYRI10GE_NO_RESPONSE_RESULT);
  302. mb();
  303. myri10ge_pio_copy(cmd_addr, buf, sizeof(*buf));
  304. /* wait up to 15ms. Longest command is the DMA benchmark,
  305. * which is capped at 5ms, but runs from a timeout handler
  306. * that runs every 7.8ms. So a 15ms timeout leaves us with
  307. * a 2.2ms margin
  308. */
  309. if (atomic) {
  310. /* if atomic is set, do not sleep,
  311. * and try to get the completion quickly
  312. * (1ms will be enough for those commands) */
  313. for (sleep_total = 0;
  314. sleep_total < 1000
  315. && response->result == htonl(MYRI10GE_NO_RESPONSE_RESULT);
  316. sleep_total += 10)
  317. udelay(10);
  318. } else {
  319. /* use msleep for most command */
  320. for (sleep_total = 0;
  321. sleep_total < 15
  322. && response->result == htonl(MYRI10GE_NO_RESPONSE_RESULT);
  323. sleep_total++)
  324. msleep(1);
  325. }
  326. result = ntohl(response->result);
  327. value = ntohl(response->data);
  328. if (result != MYRI10GE_NO_RESPONSE_RESULT) {
  329. if (result == 0) {
  330. data->data0 = value;
  331. return 0;
  332. } else if (result == MXGEFW_CMD_UNKNOWN) {
  333. return -ENOSYS;
  334. } else if (result == MXGEFW_CMD_ERROR_UNALIGNED) {
  335. return -E2BIG;
  336. } else {
  337. dev_err(&mgp->pdev->dev,
  338. "command %d failed, result = %d\n",
  339. cmd, result);
  340. return -ENXIO;
  341. }
  342. }
  343. dev_err(&mgp->pdev->dev, "command %d timed out, result = %d\n",
  344. cmd, result);
  345. return -EAGAIN;
  346. }
  347. /*
  348. * The eeprom strings on the lanaiX have the format
  349. * SN=x\0
  350. * MAC=x:x:x:x:x:x\0
  351. * PT:ddd mmm xx xx:xx:xx xx\0
  352. * PV:ddd mmm xx xx:xx:xx xx\0
  353. */
  354. static int myri10ge_read_mac_addr(struct myri10ge_priv *mgp)
  355. {
  356. char *ptr, *limit;
  357. int i;
  358. ptr = mgp->eeprom_strings;
  359. limit = mgp->eeprom_strings + MYRI10GE_EEPROM_STRINGS_SIZE;
  360. while (*ptr != '\0' && ptr < limit) {
  361. if (memcmp(ptr, "MAC=", 4) == 0) {
  362. ptr += 4;
  363. mgp->mac_addr_string = ptr;
  364. for (i = 0; i < 6; i++) {
  365. if ((ptr + 2) > limit)
  366. goto abort;
  367. mgp->mac_addr[i] =
  368. simple_strtoul(ptr, &ptr, 16);
  369. ptr += 1;
  370. }
  371. }
  372. if (memcmp((const void *)ptr, "SN=", 3) == 0) {
  373. ptr += 3;
  374. mgp->serial_number = simple_strtoul(ptr, &ptr, 10);
  375. }
  376. while (ptr < limit && *ptr++) ;
  377. }
  378. return 0;
  379. abort:
  380. dev_err(&mgp->pdev->dev, "failed to parse eeprom_strings\n");
  381. return -ENXIO;
  382. }
  383. /*
  384. * Enable or disable periodic RDMAs from the host to make certain
  385. * chipsets resend dropped PCIe messages
  386. */
  387. static void myri10ge_dummy_rdma(struct myri10ge_priv *mgp, int enable)
  388. {
  389. char __iomem *submit;
  390. __be32 buf[16];
  391. u32 dma_low, dma_high;
  392. int i;
  393. /* clear confirmation addr */
  394. mgp->cmd->data = 0;
  395. mb();
  396. /* send a rdma command to the PCIe engine, and wait for the
  397. * response in the confirmation address. The firmware should
  398. * write a -1 there to indicate it is alive and well
  399. */
  400. dma_low = MYRI10GE_LOWPART_TO_U32(mgp->cmd_bus);
  401. dma_high = MYRI10GE_HIGHPART_TO_U32(mgp->cmd_bus);
  402. buf[0] = htonl(dma_high); /* confirm addr MSW */
  403. buf[1] = htonl(dma_low); /* confirm addr LSW */
  404. buf[2] = MYRI10GE_NO_CONFIRM_DATA; /* confirm data */
  405. buf[3] = htonl(dma_high); /* dummy addr MSW */
  406. buf[4] = htonl(dma_low); /* dummy addr LSW */
  407. buf[5] = htonl(enable); /* enable? */
  408. submit = mgp->sram + MXGEFW_BOOT_DUMMY_RDMA;
  409. myri10ge_pio_copy(submit, &buf, sizeof(buf));
  410. for (i = 0; mgp->cmd->data != MYRI10GE_NO_CONFIRM_DATA && i < 20; i++)
  411. msleep(1);
  412. if (mgp->cmd->data != MYRI10GE_NO_CONFIRM_DATA)
  413. dev_err(&mgp->pdev->dev, "dummy rdma %s failed\n",
  414. (enable ? "enable" : "disable"));
  415. }
  416. static int
  417. myri10ge_validate_firmware(struct myri10ge_priv *mgp,
  418. struct mcp_gen_header *hdr)
  419. {
  420. struct device *dev = &mgp->pdev->dev;
  421. /* check firmware type */
  422. if (ntohl(hdr->mcp_type) != MCP_TYPE_ETH) {
  423. dev_err(dev, "Bad firmware type: 0x%x\n", ntohl(hdr->mcp_type));
  424. return -EINVAL;
  425. }
  426. /* save firmware version for ethtool */
  427. strncpy(mgp->fw_version, hdr->version, sizeof(mgp->fw_version));
  428. sscanf(mgp->fw_version, "%d.%d.%d", &mgp->fw_ver_major,
  429. &mgp->fw_ver_minor, &mgp->fw_ver_tiny);
  430. if (!(mgp->fw_ver_major == MXGEFW_VERSION_MAJOR
  431. && mgp->fw_ver_minor == MXGEFW_VERSION_MINOR)) {
  432. dev_err(dev, "Found firmware version %s\n", mgp->fw_version);
  433. dev_err(dev, "Driver needs %d.%d\n", MXGEFW_VERSION_MAJOR,
  434. MXGEFW_VERSION_MINOR);
  435. return -EINVAL;
  436. }
  437. return 0;
  438. }
  439. static int myri10ge_load_hotplug_firmware(struct myri10ge_priv *mgp, u32 * size)
  440. {
  441. unsigned crc, reread_crc;
  442. const struct firmware *fw;
  443. struct device *dev = &mgp->pdev->dev;
  444. struct mcp_gen_header *hdr;
  445. size_t hdr_offset;
  446. int status;
  447. unsigned i;
  448. if ((status = request_firmware(&fw, mgp->fw_name, dev)) < 0) {
  449. dev_err(dev, "Unable to load %s firmware image via hotplug\n",
  450. mgp->fw_name);
  451. status = -EINVAL;
  452. goto abort_with_nothing;
  453. }
  454. /* check size */
  455. if (fw->size >= mgp->sram_size - MYRI10GE_FW_OFFSET ||
  456. fw->size < MCP_HEADER_PTR_OFFSET + 4) {
  457. dev_err(dev, "Firmware size invalid:%d\n", (int)fw->size);
  458. status = -EINVAL;
  459. goto abort_with_fw;
  460. }
  461. /* check id */
  462. hdr_offset = ntohl(*(__be32 *) (fw->data + MCP_HEADER_PTR_OFFSET));
  463. if ((hdr_offset & 3) || hdr_offset + sizeof(*hdr) > fw->size) {
  464. dev_err(dev, "Bad firmware file\n");
  465. status = -EINVAL;
  466. goto abort_with_fw;
  467. }
  468. hdr = (void *)(fw->data + hdr_offset);
  469. status = myri10ge_validate_firmware(mgp, hdr);
  470. if (status != 0)
  471. goto abort_with_fw;
  472. crc = crc32(~0, fw->data, fw->size);
  473. for (i = 0; i < fw->size; i += 256) {
  474. myri10ge_pio_copy(mgp->sram + MYRI10GE_FW_OFFSET + i,
  475. fw->data + i,
  476. min(256U, (unsigned)(fw->size - i)));
  477. mb();
  478. readb(mgp->sram);
  479. }
  480. /* corruption checking is good for parity recovery and buggy chipset */
  481. memcpy_fromio(fw->data, mgp->sram + MYRI10GE_FW_OFFSET, fw->size);
  482. reread_crc = crc32(~0, fw->data, fw->size);
  483. if (crc != reread_crc) {
  484. dev_err(dev, "CRC failed(fw-len=%u), got 0x%x (expect 0x%x)\n",
  485. (unsigned)fw->size, reread_crc, crc);
  486. status = -EIO;
  487. goto abort_with_fw;
  488. }
  489. *size = (u32) fw->size;
  490. abort_with_fw:
  491. release_firmware(fw);
  492. abort_with_nothing:
  493. return status;
  494. }
  495. static int myri10ge_adopt_running_firmware(struct myri10ge_priv *mgp)
  496. {
  497. struct mcp_gen_header *hdr;
  498. struct device *dev = &mgp->pdev->dev;
  499. const size_t bytes = sizeof(struct mcp_gen_header);
  500. size_t hdr_offset;
  501. int status;
  502. /* find running firmware header */
  503. hdr_offset = ntohl(__raw_readl(mgp->sram + MCP_HEADER_PTR_OFFSET));
  504. if ((hdr_offset & 3) || hdr_offset + sizeof(*hdr) > mgp->sram_size) {
  505. dev_err(dev, "Running firmware has bad header offset (%d)\n",
  506. (int)hdr_offset);
  507. return -EIO;
  508. }
  509. /* copy header of running firmware from SRAM to host memory to
  510. * validate firmware */
  511. hdr = kmalloc(bytes, GFP_KERNEL);
  512. if (hdr == NULL) {
  513. dev_err(dev, "could not malloc firmware hdr\n");
  514. return -ENOMEM;
  515. }
  516. memcpy_fromio(hdr, mgp->sram + hdr_offset, bytes);
  517. status = myri10ge_validate_firmware(mgp, hdr);
  518. kfree(hdr);
  519. /* check to see if adopted firmware has bug where adopting
  520. * it will cause broadcasts to be filtered unless the NIC
  521. * is kept in ALLMULTI mode */
  522. if (mgp->fw_ver_major == 1 && mgp->fw_ver_minor == 4 &&
  523. mgp->fw_ver_tiny >= 4 && mgp->fw_ver_tiny <= 11) {
  524. mgp->adopted_rx_filter_bug = 1;
  525. dev_warn(dev, "Adopting fw %d.%d.%d: "
  526. "working around rx filter bug\n",
  527. mgp->fw_ver_major, mgp->fw_ver_minor,
  528. mgp->fw_ver_tiny);
  529. }
  530. return status;
  531. }
  532. static int myri10ge_load_firmware(struct myri10ge_priv *mgp)
  533. {
  534. char __iomem *submit;
  535. __be32 buf[16];
  536. u32 dma_low, dma_high, size;
  537. int status, i;
  538. struct myri10ge_cmd cmd;
  539. size = 0;
  540. status = myri10ge_load_hotplug_firmware(mgp, &size);
  541. if (status) {
  542. dev_warn(&mgp->pdev->dev, "hotplug firmware loading failed\n");
  543. /* Do not attempt to adopt firmware if there
  544. * was a bad crc */
  545. if (status == -EIO)
  546. return status;
  547. status = myri10ge_adopt_running_firmware(mgp);
  548. if (status != 0) {
  549. dev_err(&mgp->pdev->dev,
  550. "failed to adopt running firmware\n");
  551. return status;
  552. }
  553. dev_info(&mgp->pdev->dev,
  554. "Successfully adopted running firmware\n");
  555. if (mgp->tx.boundary == 4096) {
  556. dev_warn(&mgp->pdev->dev,
  557. "Using firmware currently running on NIC"
  558. ". For optimal\n");
  559. dev_warn(&mgp->pdev->dev,
  560. "performance consider loading optimized "
  561. "firmware\n");
  562. dev_warn(&mgp->pdev->dev, "via hotplug\n");
  563. }
  564. mgp->fw_name = "adopted";
  565. mgp->tx.boundary = 2048;
  566. return status;
  567. }
  568. /* clear confirmation addr */
  569. mgp->cmd->data = 0;
  570. mb();
  571. /* send a reload command to the bootstrap MCP, and wait for the
  572. * response in the confirmation address. The firmware should
  573. * write a -1 there to indicate it is alive and well
  574. */
  575. dma_low = MYRI10GE_LOWPART_TO_U32(mgp->cmd_bus);
  576. dma_high = MYRI10GE_HIGHPART_TO_U32(mgp->cmd_bus);
  577. buf[0] = htonl(dma_high); /* confirm addr MSW */
  578. buf[1] = htonl(dma_low); /* confirm addr LSW */
  579. buf[2] = MYRI10GE_NO_CONFIRM_DATA; /* confirm data */
  580. /* FIX: All newest firmware should un-protect the bottom of
  581. * the sram before handoff. However, the very first interfaces
  582. * do not. Therefore the handoff copy must skip the first 8 bytes
  583. */
  584. buf[3] = htonl(MYRI10GE_FW_OFFSET + 8); /* where the code starts */
  585. buf[4] = htonl(size - 8); /* length of code */
  586. buf[5] = htonl(8); /* where to copy to */
  587. buf[6] = htonl(0); /* where to jump to */
  588. submit = mgp->sram + MXGEFW_BOOT_HANDOFF;
  589. myri10ge_pio_copy(submit, &buf, sizeof(buf));
  590. mb();
  591. msleep(1);
  592. mb();
  593. i = 0;
  594. while (mgp->cmd->data != MYRI10GE_NO_CONFIRM_DATA && i < 20) {
  595. msleep(1);
  596. i++;
  597. }
  598. if (mgp->cmd->data != MYRI10GE_NO_CONFIRM_DATA) {
  599. dev_err(&mgp->pdev->dev, "handoff failed\n");
  600. return -ENXIO;
  601. }
  602. dev_info(&mgp->pdev->dev, "handoff confirmed\n");
  603. myri10ge_dummy_rdma(mgp, 1);
  604. /* probe for IPv6 TSO support */
  605. mgp->features = NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_TSO;
  606. status = myri10ge_send_cmd(mgp, MXGEFW_CMD_GET_MAX_TSO6_HDR_SIZE,
  607. &cmd, 0);
  608. if (status == 0) {
  609. mgp->max_tso6 = cmd.data0;
  610. mgp->features |= NETIF_F_TSO6;
  611. }
  612. return 0;
  613. }
  614. static int myri10ge_update_mac_address(struct myri10ge_priv *mgp, u8 * addr)
  615. {
  616. struct myri10ge_cmd cmd;
  617. int status;
  618. cmd.data0 = ((addr[0] << 24) | (addr[1] << 16)
  619. | (addr[2] << 8) | addr[3]);
  620. cmd.data1 = ((addr[4] << 8) | (addr[5]));
  621. status = myri10ge_send_cmd(mgp, MXGEFW_SET_MAC_ADDRESS, &cmd, 0);
  622. return status;
  623. }
  624. static int myri10ge_change_pause(struct myri10ge_priv *mgp, int pause)
  625. {
  626. struct myri10ge_cmd cmd;
  627. int status, ctl;
  628. ctl = pause ? MXGEFW_ENABLE_FLOW_CONTROL : MXGEFW_DISABLE_FLOW_CONTROL;
  629. status = myri10ge_send_cmd(mgp, ctl, &cmd, 0);
  630. if (status) {
  631. printk(KERN_ERR
  632. "myri10ge: %s: Failed to set flow control mode\n",
  633. mgp->dev->name);
  634. return status;
  635. }
  636. mgp->pause = pause;
  637. return 0;
  638. }
  639. static void
  640. myri10ge_change_promisc(struct myri10ge_priv *mgp, int promisc, int atomic)
  641. {
  642. struct myri10ge_cmd cmd;
  643. int status, ctl;
  644. ctl = promisc ? MXGEFW_ENABLE_PROMISC : MXGEFW_DISABLE_PROMISC;
  645. status = myri10ge_send_cmd(mgp, ctl, &cmd, atomic);
  646. if (status)
  647. printk(KERN_ERR "myri10ge: %s: Failed to set promisc mode\n",
  648. mgp->dev->name);
  649. }
  650. static int myri10ge_dma_test(struct myri10ge_priv *mgp, int test_type)
  651. {
  652. struct myri10ge_cmd cmd;
  653. int status;
  654. u32 len;
  655. struct page *dmatest_page;
  656. dma_addr_t dmatest_bus;
  657. char *test = " ";
  658. dmatest_page = alloc_page(GFP_KERNEL);
  659. if (!dmatest_page)
  660. return -ENOMEM;
  661. dmatest_bus = pci_map_page(mgp->pdev, dmatest_page, 0, PAGE_SIZE,
  662. DMA_BIDIRECTIONAL);
  663. /* Run a small DMA test.
  664. * The magic multipliers to the length tell the firmware
  665. * to do DMA read, write, or read+write tests. The
  666. * results are returned in cmd.data0. The upper 16
  667. * bits or the return is the number of transfers completed.
  668. * The lower 16 bits is the time in 0.5us ticks that the
  669. * transfers took to complete.
  670. */
  671. len = mgp->tx.boundary;
  672. cmd.data0 = MYRI10GE_LOWPART_TO_U32(dmatest_bus);
  673. cmd.data1 = MYRI10GE_HIGHPART_TO_U32(dmatest_bus);
  674. cmd.data2 = len * 0x10000;
  675. status = myri10ge_send_cmd(mgp, test_type, &cmd, 0);
  676. if (status != 0) {
  677. test = "read";
  678. goto abort;
  679. }
  680. mgp->read_dma = ((cmd.data0 >> 16) * len * 2) / (cmd.data0 & 0xffff);
  681. cmd.data0 = MYRI10GE_LOWPART_TO_U32(dmatest_bus);
  682. cmd.data1 = MYRI10GE_HIGHPART_TO_U32(dmatest_bus);
  683. cmd.data2 = len * 0x1;
  684. status = myri10ge_send_cmd(mgp, test_type, &cmd, 0);
  685. if (status != 0) {
  686. test = "write";
  687. goto abort;
  688. }
  689. mgp->write_dma = ((cmd.data0 >> 16) * len * 2) / (cmd.data0 & 0xffff);
  690. cmd.data0 = MYRI10GE_LOWPART_TO_U32(dmatest_bus);
  691. cmd.data1 = MYRI10GE_HIGHPART_TO_U32(dmatest_bus);
  692. cmd.data2 = len * 0x10001;
  693. status = myri10ge_send_cmd(mgp, test_type, &cmd, 0);
  694. if (status != 0) {
  695. test = "read/write";
  696. goto abort;
  697. }
  698. mgp->read_write_dma = ((cmd.data0 >> 16) * len * 2 * 2) /
  699. (cmd.data0 & 0xffff);
  700. abort:
  701. pci_unmap_page(mgp->pdev, dmatest_bus, PAGE_SIZE, DMA_BIDIRECTIONAL);
  702. put_page(dmatest_page);
  703. if (status != 0 && test_type != MXGEFW_CMD_UNALIGNED_TEST)
  704. dev_warn(&mgp->pdev->dev, "DMA %s benchmark failed: %d\n",
  705. test, status);
  706. return status;
  707. }
  708. static int myri10ge_reset(struct myri10ge_priv *mgp)
  709. {
  710. struct myri10ge_cmd cmd;
  711. int status;
  712. size_t bytes;
  713. /* try to send a reset command to the card to see if it
  714. * is alive */
  715. memset(&cmd, 0, sizeof(cmd));
  716. status = myri10ge_send_cmd(mgp, MXGEFW_CMD_RESET, &cmd, 0);
  717. if (status != 0) {
  718. dev_err(&mgp->pdev->dev, "failed reset\n");
  719. return -ENXIO;
  720. }
  721. (void)myri10ge_dma_test(mgp, MXGEFW_DMA_TEST);
  722. /* Now exchange information about interrupts */
  723. bytes = myri10ge_max_intr_slots * sizeof(*mgp->rx_done.entry);
  724. memset(mgp->rx_done.entry, 0, bytes);
  725. cmd.data0 = (u32) bytes;
  726. status = myri10ge_send_cmd(mgp, MXGEFW_CMD_SET_INTRQ_SIZE, &cmd, 0);
  727. cmd.data0 = MYRI10GE_LOWPART_TO_U32(mgp->rx_done.bus);
  728. cmd.data1 = MYRI10GE_HIGHPART_TO_U32(mgp->rx_done.bus);
  729. status |= myri10ge_send_cmd(mgp, MXGEFW_CMD_SET_INTRQ_DMA, &cmd, 0);
  730. status |=
  731. myri10ge_send_cmd(mgp, MXGEFW_CMD_GET_IRQ_ACK_OFFSET, &cmd, 0);
  732. mgp->irq_claim = (__iomem __be32 *) (mgp->sram + cmd.data0);
  733. status |= myri10ge_send_cmd(mgp, MXGEFW_CMD_GET_IRQ_DEASSERT_OFFSET,
  734. &cmd, 0);
  735. mgp->irq_deassert = (__iomem __be32 *) (mgp->sram + cmd.data0);
  736. status |= myri10ge_send_cmd
  737. (mgp, MXGEFW_CMD_GET_INTR_COAL_DELAY_OFFSET, &cmd, 0);
  738. mgp->intr_coal_delay_ptr = (__iomem __be32 *) (mgp->sram + cmd.data0);
  739. if (status != 0) {
  740. dev_err(&mgp->pdev->dev, "failed set interrupt parameters\n");
  741. return status;
  742. }
  743. put_be32(htonl(mgp->intr_coal_delay), mgp->intr_coal_delay_ptr);
  744. memset(mgp->rx_done.entry, 0, bytes);
  745. /* reset mcp/driver shared state back to 0 */
  746. mgp->tx.req = 0;
  747. mgp->tx.done = 0;
  748. mgp->tx.pkt_start = 0;
  749. mgp->tx.pkt_done = 0;
  750. mgp->rx_big.cnt = 0;
  751. mgp->rx_small.cnt = 0;
  752. mgp->rx_done.idx = 0;
  753. mgp->rx_done.cnt = 0;
  754. mgp->link_changes = 0;
  755. status = myri10ge_update_mac_address(mgp, mgp->dev->dev_addr);
  756. myri10ge_change_pause(mgp, mgp->pause);
  757. myri10ge_set_multicast_list(mgp->dev);
  758. return status;
  759. }
  760. static inline void
  761. myri10ge_submit_8rx(struct mcp_kreq_ether_recv __iomem * dst,
  762. struct mcp_kreq_ether_recv *src)
  763. {
  764. __be32 low;
  765. low = src->addr_low;
  766. src->addr_low = htonl(DMA_32BIT_MASK);
  767. myri10ge_pio_copy(dst, src, 4 * sizeof(*src));
  768. mb();
  769. myri10ge_pio_copy(dst + 4, src + 4, 4 * sizeof(*src));
  770. mb();
  771. src->addr_low = low;
  772. put_be32(low, &dst->addr_low);
  773. mb();
  774. }
  775. static inline void myri10ge_vlan_ip_csum(struct sk_buff *skb, __wsum hw_csum)
  776. {
  777. struct vlan_hdr *vh = (struct vlan_hdr *)(skb->data);
  778. if ((skb->protocol == htons(ETH_P_8021Q)) &&
  779. (vh->h_vlan_encapsulated_proto == htons(ETH_P_IP) ||
  780. vh->h_vlan_encapsulated_proto == htons(ETH_P_IPV6))) {
  781. skb->csum = hw_csum;
  782. skb->ip_summed = CHECKSUM_COMPLETE;
  783. }
  784. }
  785. static inline void
  786. myri10ge_rx_skb_build(struct sk_buff *skb, u8 * va,
  787. struct skb_frag_struct *rx_frags, int len, int hlen)
  788. {
  789. struct skb_frag_struct *skb_frags;
  790. skb->len = skb->data_len = len;
  791. skb->truesize = len + sizeof(struct sk_buff);
  792. /* attach the page(s) */
  793. skb_frags = skb_shinfo(skb)->frags;
  794. while (len > 0) {
  795. memcpy(skb_frags, rx_frags, sizeof(*skb_frags));
  796. len -= rx_frags->size;
  797. skb_frags++;
  798. rx_frags++;
  799. skb_shinfo(skb)->nr_frags++;
  800. }
  801. /* pskb_may_pull is not available in irq context, but
  802. * skb_pull() (for ether_pad and eth_type_trans()) requires
  803. * the beginning of the packet in skb_headlen(), move it
  804. * manually */
  805. skb_copy_to_linear_data(skb, va, hlen);
  806. skb_shinfo(skb)->frags[0].page_offset += hlen;
  807. skb_shinfo(skb)->frags[0].size -= hlen;
  808. skb->data_len -= hlen;
  809. skb->tail += hlen;
  810. skb_pull(skb, MXGEFW_PAD);
  811. }
  812. static void
  813. myri10ge_alloc_rx_pages(struct myri10ge_priv *mgp, struct myri10ge_rx_buf *rx,
  814. int bytes, int watchdog)
  815. {
  816. struct page *page;
  817. int idx;
  818. if (unlikely(rx->watchdog_needed && !watchdog))
  819. return;
  820. /* try to refill entire ring */
  821. while (rx->fill_cnt != (rx->cnt + rx->mask + 1)) {
  822. idx = rx->fill_cnt & rx->mask;
  823. if (rx->page_offset + bytes <= MYRI10GE_ALLOC_SIZE) {
  824. /* we can use part of previous page */
  825. get_page(rx->page);
  826. } else {
  827. /* we need a new page */
  828. page =
  829. alloc_pages(GFP_ATOMIC | __GFP_COMP,
  830. MYRI10GE_ALLOC_ORDER);
  831. if (unlikely(page == NULL)) {
  832. if (rx->fill_cnt - rx->cnt < 16)
  833. rx->watchdog_needed = 1;
  834. return;
  835. }
  836. rx->page = page;
  837. rx->page_offset = 0;
  838. rx->bus = pci_map_page(mgp->pdev, page, 0,
  839. MYRI10GE_ALLOC_SIZE,
  840. PCI_DMA_FROMDEVICE);
  841. }
  842. rx->info[idx].page = rx->page;
  843. rx->info[idx].page_offset = rx->page_offset;
  844. /* note that this is the address of the start of the
  845. * page */
  846. pci_unmap_addr_set(&rx->info[idx], bus, rx->bus);
  847. rx->shadow[idx].addr_low =
  848. htonl(MYRI10GE_LOWPART_TO_U32(rx->bus) + rx->page_offset);
  849. rx->shadow[idx].addr_high =
  850. htonl(MYRI10GE_HIGHPART_TO_U32(rx->bus));
  851. /* start next packet on a cacheline boundary */
  852. rx->page_offset += SKB_DATA_ALIGN(bytes);
  853. #if MYRI10GE_ALLOC_SIZE > 4096
  854. /* don't cross a 4KB boundary */
  855. if ((rx->page_offset >> 12) !=
  856. ((rx->page_offset + bytes - 1) >> 12))
  857. rx->page_offset = (rx->page_offset + 4096) & ~4095;
  858. #endif
  859. rx->fill_cnt++;
  860. /* copy 8 descriptors to the firmware at a time */
  861. if ((idx & 7) == 7) {
  862. if (rx->wc_fifo == NULL)
  863. myri10ge_submit_8rx(&rx->lanai[idx - 7],
  864. &rx->shadow[idx - 7]);
  865. else {
  866. mb();
  867. myri10ge_pio_copy(rx->wc_fifo,
  868. &rx->shadow[idx - 7], 64);
  869. }
  870. }
  871. }
  872. }
  873. static inline void
  874. myri10ge_unmap_rx_page(struct pci_dev *pdev,
  875. struct myri10ge_rx_buffer_state *info, int bytes)
  876. {
  877. /* unmap the recvd page if we're the only or last user of it */
  878. if (bytes >= MYRI10GE_ALLOC_SIZE / 2 ||
  879. (info->page_offset + 2 * bytes) > MYRI10GE_ALLOC_SIZE) {
  880. pci_unmap_page(pdev, (pci_unmap_addr(info, bus)
  881. & ~(MYRI10GE_ALLOC_SIZE - 1)),
  882. MYRI10GE_ALLOC_SIZE, PCI_DMA_FROMDEVICE);
  883. }
  884. }
  885. #define MYRI10GE_HLEN 64 /* The number of bytes to copy from a
  886. * page into an skb */
  887. static inline int
  888. myri10ge_rx_done(struct myri10ge_priv *mgp, struct myri10ge_rx_buf *rx,
  889. int bytes, int len, __wsum csum)
  890. {
  891. struct sk_buff *skb;
  892. struct skb_frag_struct rx_frags[MYRI10GE_MAX_FRAGS_PER_FRAME];
  893. int i, idx, hlen, remainder;
  894. struct pci_dev *pdev = mgp->pdev;
  895. struct net_device *dev = mgp->dev;
  896. u8 *va;
  897. len += MXGEFW_PAD;
  898. idx = rx->cnt & rx->mask;
  899. va = page_address(rx->info[idx].page) + rx->info[idx].page_offset;
  900. prefetch(va);
  901. /* Fill skb_frag_struct(s) with data from our receive */
  902. for (i = 0, remainder = len; remainder > 0; i++) {
  903. myri10ge_unmap_rx_page(pdev, &rx->info[idx], bytes);
  904. rx_frags[i].page = rx->info[idx].page;
  905. rx_frags[i].page_offset = rx->info[idx].page_offset;
  906. if (remainder < MYRI10GE_ALLOC_SIZE)
  907. rx_frags[i].size = remainder;
  908. else
  909. rx_frags[i].size = MYRI10GE_ALLOC_SIZE;
  910. rx->cnt++;
  911. idx = rx->cnt & rx->mask;
  912. remainder -= MYRI10GE_ALLOC_SIZE;
  913. }
  914. if (mgp->csum_flag && myri10ge_lro) {
  915. rx_frags[0].page_offset += MXGEFW_PAD;
  916. rx_frags[0].size -= MXGEFW_PAD;
  917. len -= MXGEFW_PAD;
  918. lro_receive_frags(&mgp->rx_done.lro_mgr, rx_frags,
  919. len, len, (void *)(unsigned long)csum, csum);
  920. return 1;
  921. }
  922. hlen = MYRI10GE_HLEN > len ? len : MYRI10GE_HLEN;
  923. /* allocate an skb to attach the page(s) to. This is done
  924. * after trying LRO, so as to avoid skb allocation overheads */
  925. skb = netdev_alloc_skb(dev, MYRI10GE_HLEN + 16);
  926. if (unlikely(skb == NULL)) {
  927. mgp->stats.rx_dropped++;
  928. do {
  929. i--;
  930. put_page(rx_frags[i].page);
  931. } while (i != 0);
  932. return 0;
  933. }
  934. /* Attach the pages to the skb, and trim off any padding */
  935. myri10ge_rx_skb_build(skb, va, rx_frags, len, hlen);
  936. if (skb_shinfo(skb)->frags[0].size <= 0) {
  937. put_page(skb_shinfo(skb)->frags[0].page);
  938. skb_shinfo(skb)->nr_frags = 0;
  939. }
  940. skb->protocol = eth_type_trans(skb, dev);
  941. if (mgp->csum_flag) {
  942. if ((skb->protocol == htons(ETH_P_IP)) ||
  943. (skb->protocol == htons(ETH_P_IPV6))) {
  944. skb->csum = csum;
  945. skb->ip_summed = CHECKSUM_COMPLETE;
  946. } else
  947. myri10ge_vlan_ip_csum(skb, csum);
  948. }
  949. netif_receive_skb(skb);
  950. dev->last_rx = jiffies;
  951. return 1;
  952. }
  953. static inline void myri10ge_tx_done(struct myri10ge_priv *mgp, int mcp_index)
  954. {
  955. struct pci_dev *pdev = mgp->pdev;
  956. struct myri10ge_tx_buf *tx = &mgp->tx;
  957. struct sk_buff *skb;
  958. int idx, len;
  959. while (tx->pkt_done != mcp_index) {
  960. idx = tx->done & tx->mask;
  961. skb = tx->info[idx].skb;
  962. /* Mark as free */
  963. tx->info[idx].skb = NULL;
  964. if (tx->info[idx].last) {
  965. tx->pkt_done++;
  966. tx->info[idx].last = 0;
  967. }
  968. tx->done++;
  969. len = pci_unmap_len(&tx->info[idx], len);
  970. pci_unmap_len_set(&tx->info[idx], len, 0);
  971. if (skb) {
  972. mgp->stats.tx_bytes += skb->len;
  973. mgp->stats.tx_packets++;
  974. dev_kfree_skb_irq(skb);
  975. if (len)
  976. pci_unmap_single(pdev,
  977. pci_unmap_addr(&tx->info[idx],
  978. bus), len,
  979. PCI_DMA_TODEVICE);
  980. } else {
  981. if (len)
  982. pci_unmap_page(pdev,
  983. pci_unmap_addr(&tx->info[idx],
  984. bus), len,
  985. PCI_DMA_TODEVICE);
  986. }
  987. }
  988. /* start the queue if we've stopped it */
  989. if (netif_queue_stopped(mgp->dev)
  990. && tx->req - tx->done < (tx->mask >> 1)) {
  991. mgp->wake_queue++;
  992. netif_wake_queue(mgp->dev);
  993. }
  994. }
  995. static inline int myri10ge_clean_rx_done(struct myri10ge_priv *mgp, int budget)
  996. {
  997. struct myri10ge_rx_done *rx_done = &mgp->rx_done;
  998. unsigned long rx_bytes = 0;
  999. unsigned long rx_packets = 0;
  1000. unsigned long rx_ok;
  1001. int idx = rx_done->idx;
  1002. int cnt = rx_done->cnt;
  1003. int work_done = 0;
  1004. u16 length;
  1005. __wsum checksum;
  1006. while (rx_done->entry[idx].length != 0 && work_done < budget) {
  1007. length = ntohs(rx_done->entry[idx].length);
  1008. rx_done->entry[idx].length = 0;
  1009. checksum = csum_unfold(rx_done->entry[idx].checksum);
  1010. if (length <= mgp->small_bytes)
  1011. rx_ok = myri10ge_rx_done(mgp, &mgp->rx_small,
  1012. mgp->small_bytes,
  1013. length, checksum);
  1014. else
  1015. rx_ok = myri10ge_rx_done(mgp, &mgp->rx_big,
  1016. mgp->big_bytes,
  1017. length, checksum);
  1018. rx_packets += rx_ok;
  1019. rx_bytes += rx_ok * (unsigned long)length;
  1020. cnt++;
  1021. idx = cnt & (myri10ge_max_intr_slots - 1);
  1022. work_done++;
  1023. }
  1024. rx_done->idx = idx;
  1025. rx_done->cnt = cnt;
  1026. mgp->stats.rx_packets += rx_packets;
  1027. mgp->stats.rx_bytes += rx_bytes;
  1028. if (myri10ge_lro)
  1029. lro_flush_all(&rx_done->lro_mgr);
  1030. /* restock receive rings if needed */
  1031. if (mgp->rx_small.fill_cnt - mgp->rx_small.cnt < myri10ge_fill_thresh)
  1032. myri10ge_alloc_rx_pages(mgp, &mgp->rx_small,
  1033. mgp->small_bytes + MXGEFW_PAD, 0);
  1034. if (mgp->rx_big.fill_cnt - mgp->rx_big.cnt < myri10ge_fill_thresh)
  1035. myri10ge_alloc_rx_pages(mgp, &mgp->rx_big, mgp->big_bytes, 0);
  1036. return work_done;
  1037. }
  1038. static inline void myri10ge_check_statblock(struct myri10ge_priv *mgp)
  1039. {
  1040. struct mcp_irq_data *stats = mgp->fw_stats;
  1041. if (unlikely(stats->stats_updated)) {
  1042. unsigned link_up = ntohl(stats->link_up);
  1043. if (mgp->link_state != link_up) {
  1044. mgp->link_state = link_up;
  1045. if (mgp->link_state == MXGEFW_LINK_UP) {
  1046. if (netif_msg_link(mgp))
  1047. printk(KERN_INFO
  1048. "myri10ge: %s: link up\n",
  1049. mgp->dev->name);
  1050. netif_carrier_on(mgp->dev);
  1051. mgp->link_changes++;
  1052. } else {
  1053. if (netif_msg_link(mgp))
  1054. printk(KERN_INFO
  1055. "myri10ge: %s: link %s\n",
  1056. mgp->dev->name,
  1057. (link_up == MXGEFW_LINK_MYRINET ?
  1058. "mismatch (Myrinet detected)" :
  1059. "down"));
  1060. netif_carrier_off(mgp->dev);
  1061. mgp->link_changes++;
  1062. }
  1063. }
  1064. if (mgp->rdma_tags_available !=
  1065. ntohl(mgp->fw_stats->rdma_tags_available)) {
  1066. mgp->rdma_tags_available =
  1067. ntohl(mgp->fw_stats->rdma_tags_available);
  1068. printk(KERN_WARNING "myri10ge: %s: RDMA timed out! "
  1069. "%d tags left\n", mgp->dev->name,
  1070. mgp->rdma_tags_available);
  1071. }
  1072. mgp->down_cnt += stats->link_down;
  1073. if (stats->link_down)
  1074. wake_up(&mgp->down_wq);
  1075. }
  1076. }
  1077. static int myri10ge_poll(struct napi_struct *napi, int budget)
  1078. {
  1079. struct myri10ge_priv *mgp =
  1080. container_of(napi, struct myri10ge_priv, napi);
  1081. struct net_device *netdev = mgp->dev;
  1082. int work_done;
  1083. /* process as many rx events as NAPI will allow */
  1084. work_done = myri10ge_clean_rx_done(mgp, budget);
  1085. if (work_done < budget) {
  1086. netif_rx_complete(netdev, napi);
  1087. put_be32(htonl(3), mgp->irq_claim);
  1088. }
  1089. return work_done;
  1090. }
  1091. static irqreturn_t myri10ge_intr(int irq, void *arg)
  1092. {
  1093. struct myri10ge_priv *mgp = arg;
  1094. struct mcp_irq_data *stats = mgp->fw_stats;
  1095. struct myri10ge_tx_buf *tx = &mgp->tx;
  1096. u32 send_done_count;
  1097. int i;
  1098. /* make sure it is our IRQ, and that the DMA has finished */
  1099. if (unlikely(!stats->valid))
  1100. return (IRQ_NONE);
  1101. /* low bit indicates receives are present, so schedule
  1102. * napi poll handler */
  1103. if (stats->valid & 1)
  1104. netif_rx_schedule(mgp->dev, &mgp->napi);
  1105. if (!mgp->msi_enabled) {
  1106. put_be32(0, mgp->irq_deassert);
  1107. if (!myri10ge_deassert_wait)
  1108. stats->valid = 0;
  1109. mb();
  1110. } else
  1111. stats->valid = 0;
  1112. /* Wait for IRQ line to go low, if using INTx */
  1113. i = 0;
  1114. while (1) {
  1115. i++;
  1116. /* check for transmit completes and receives */
  1117. send_done_count = ntohl(stats->send_done_count);
  1118. if (send_done_count != tx->pkt_done)
  1119. myri10ge_tx_done(mgp, (int)send_done_count);
  1120. if (unlikely(i > myri10ge_max_irq_loops)) {
  1121. printk(KERN_WARNING "myri10ge: %s: irq stuck?\n",
  1122. mgp->dev->name);
  1123. stats->valid = 0;
  1124. schedule_work(&mgp->watchdog_work);
  1125. }
  1126. if (likely(stats->valid == 0))
  1127. break;
  1128. cpu_relax();
  1129. barrier();
  1130. }
  1131. myri10ge_check_statblock(mgp);
  1132. put_be32(htonl(3), mgp->irq_claim + 1);
  1133. return (IRQ_HANDLED);
  1134. }
  1135. static int
  1136. myri10ge_get_settings(struct net_device *netdev, struct ethtool_cmd *cmd)
  1137. {
  1138. cmd->autoneg = AUTONEG_DISABLE;
  1139. cmd->speed = SPEED_10000;
  1140. cmd->duplex = DUPLEX_FULL;
  1141. return 0;
  1142. }
  1143. static void
  1144. myri10ge_get_drvinfo(struct net_device *netdev, struct ethtool_drvinfo *info)
  1145. {
  1146. struct myri10ge_priv *mgp = netdev_priv(netdev);
  1147. strlcpy(info->driver, "myri10ge", sizeof(info->driver));
  1148. strlcpy(info->version, MYRI10GE_VERSION_STR, sizeof(info->version));
  1149. strlcpy(info->fw_version, mgp->fw_version, sizeof(info->fw_version));
  1150. strlcpy(info->bus_info, pci_name(mgp->pdev), sizeof(info->bus_info));
  1151. }
  1152. static int
  1153. myri10ge_get_coalesce(struct net_device *netdev, struct ethtool_coalesce *coal)
  1154. {
  1155. struct myri10ge_priv *mgp = netdev_priv(netdev);
  1156. coal->rx_coalesce_usecs = mgp->intr_coal_delay;
  1157. return 0;
  1158. }
  1159. static int
  1160. myri10ge_set_coalesce(struct net_device *netdev, struct ethtool_coalesce *coal)
  1161. {
  1162. struct myri10ge_priv *mgp = netdev_priv(netdev);
  1163. mgp->intr_coal_delay = coal->rx_coalesce_usecs;
  1164. put_be32(htonl(mgp->intr_coal_delay), mgp->intr_coal_delay_ptr);
  1165. return 0;
  1166. }
  1167. static void
  1168. myri10ge_get_pauseparam(struct net_device *netdev,
  1169. struct ethtool_pauseparam *pause)
  1170. {
  1171. struct myri10ge_priv *mgp = netdev_priv(netdev);
  1172. pause->autoneg = 0;
  1173. pause->rx_pause = mgp->pause;
  1174. pause->tx_pause = mgp->pause;
  1175. }
  1176. static int
  1177. myri10ge_set_pauseparam(struct net_device *netdev,
  1178. struct ethtool_pauseparam *pause)
  1179. {
  1180. struct myri10ge_priv *mgp = netdev_priv(netdev);
  1181. if (pause->tx_pause != mgp->pause)
  1182. return myri10ge_change_pause(mgp, pause->tx_pause);
  1183. if (pause->rx_pause != mgp->pause)
  1184. return myri10ge_change_pause(mgp, pause->tx_pause);
  1185. if (pause->autoneg != 0)
  1186. return -EINVAL;
  1187. return 0;
  1188. }
  1189. static void
  1190. myri10ge_get_ringparam(struct net_device *netdev,
  1191. struct ethtool_ringparam *ring)
  1192. {
  1193. struct myri10ge_priv *mgp = netdev_priv(netdev);
  1194. ring->rx_mini_max_pending = mgp->rx_small.mask + 1;
  1195. ring->rx_max_pending = mgp->rx_big.mask + 1;
  1196. ring->rx_jumbo_max_pending = 0;
  1197. ring->tx_max_pending = mgp->rx_small.mask + 1;
  1198. ring->rx_mini_pending = ring->rx_mini_max_pending;
  1199. ring->rx_pending = ring->rx_max_pending;
  1200. ring->rx_jumbo_pending = ring->rx_jumbo_max_pending;
  1201. ring->tx_pending = ring->tx_max_pending;
  1202. }
  1203. static u32 myri10ge_get_rx_csum(struct net_device *netdev)
  1204. {
  1205. struct myri10ge_priv *mgp = netdev_priv(netdev);
  1206. if (mgp->csum_flag)
  1207. return 1;
  1208. else
  1209. return 0;
  1210. }
  1211. static int myri10ge_set_rx_csum(struct net_device *netdev, u32 csum_enabled)
  1212. {
  1213. struct myri10ge_priv *mgp = netdev_priv(netdev);
  1214. if (csum_enabled)
  1215. mgp->csum_flag = MXGEFW_FLAGS_CKSUM;
  1216. else
  1217. mgp->csum_flag = 0;
  1218. return 0;
  1219. }
  1220. static int myri10ge_set_tso(struct net_device *netdev, u32 tso_enabled)
  1221. {
  1222. struct myri10ge_priv *mgp = netdev_priv(netdev);
  1223. unsigned long flags = mgp->features & (NETIF_F_TSO6 | NETIF_F_TSO);
  1224. if (tso_enabled)
  1225. netdev->features |= flags;
  1226. else
  1227. netdev->features &= ~flags;
  1228. return 0;
  1229. }
  1230. static const char myri10ge_gstrings_stats[][ETH_GSTRING_LEN] = {
  1231. "rx_packets", "tx_packets", "rx_bytes", "tx_bytes", "rx_errors",
  1232. "tx_errors", "rx_dropped", "tx_dropped", "multicast", "collisions",
  1233. "rx_length_errors", "rx_over_errors", "rx_crc_errors",
  1234. "rx_frame_errors", "rx_fifo_errors", "rx_missed_errors",
  1235. "tx_aborted_errors", "tx_carrier_errors", "tx_fifo_errors",
  1236. "tx_heartbeat_errors", "tx_window_errors",
  1237. /* device-specific stats */
  1238. "tx_boundary", "WC", "irq", "MSI",
  1239. "read_dma_bw_MBs", "write_dma_bw_MBs", "read_write_dma_bw_MBs",
  1240. "serial_number", "tx_pkt_start", "tx_pkt_done",
  1241. "tx_req", "tx_done", "rx_small_cnt", "rx_big_cnt",
  1242. "wake_queue", "stop_queue", "watchdog_resets", "tx_linearized",
  1243. "link_changes", "link_up", "dropped_link_overflow",
  1244. "dropped_link_error_or_filtered",
  1245. "dropped_pause", "dropped_bad_phy", "dropped_bad_crc32",
  1246. "dropped_unicast_filtered", "dropped_multicast_filtered",
  1247. "dropped_runt", "dropped_overrun", "dropped_no_small_buffer",
  1248. "dropped_no_big_buffer", "LRO aggregated", "LRO flushed",
  1249. "LRO avg aggr", "LRO no_desc"
  1250. };
  1251. #define MYRI10GE_NET_STATS_LEN 21
  1252. #define MYRI10GE_STATS_LEN sizeof(myri10ge_gstrings_stats) / ETH_GSTRING_LEN
  1253. static void
  1254. myri10ge_get_strings(struct net_device *netdev, u32 stringset, u8 * data)
  1255. {
  1256. switch (stringset) {
  1257. case ETH_SS_STATS:
  1258. memcpy(data, *myri10ge_gstrings_stats,
  1259. sizeof(myri10ge_gstrings_stats));
  1260. break;
  1261. }
  1262. }
  1263. static int myri10ge_get_sset_count(struct net_device *netdev, int sset)
  1264. {
  1265. switch (sset) {
  1266. case ETH_SS_STATS:
  1267. return MYRI10GE_STATS_LEN;
  1268. default:
  1269. return -EOPNOTSUPP;
  1270. }
  1271. }
  1272. static void
  1273. myri10ge_get_ethtool_stats(struct net_device *netdev,
  1274. struct ethtool_stats *stats, u64 * data)
  1275. {
  1276. struct myri10ge_priv *mgp = netdev_priv(netdev);
  1277. int i;
  1278. for (i = 0; i < MYRI10GE_NET_STATS_LEN; i++)
  1279. data[i] = ((unsigned long *)&mgp->stats)[i];
  1280. data[i++] = (unsigned int)mgp->tx.boundary;
  1281. data[i++] = (unsigned int)mgp->wc_enabled;
  1282. data[i++] = (unsigned int)mgp->pdev->irq;
  1283. data[i++] = (unsigned int)mgp->msi_enabled;
  1284. data[i++] = (unsigned int)mgp->read_dma;
  1285. data[i++] = (unsigned int)mgp->write_dma;
  1286. data[i++] = (unsigned int)mgp->read_write_dma;
  1287. data[i++] = (unsigned int)mgp->serial_number;
  1288. data[i++] = (unsigned int)mgp->tx.pkt_start;
  1289. data[i++] = (unsigned int)mgp->tx.pkt_done;
  1290. data[i++] = (unsigned int)mgp->tx.req;
  1291. data[i++] = (unsigned int)mgp->tx.done;
  1292. data[i++] = (unsigned int)mgp->rx_small.cnt;
  1293. data[i++] = (unsigned int)mgp->rx_big.cnt;
  1294. data[i++] = (unsigned int)mgp->wake_queue;
  1295. data[i++] = (unsigned int)mgp->stop_queue;
  1296. data[i++] = (unsigned int)mgp->watchdog_resets;
  1297. data[i++] = (unsigned int)mgp->tx_linearized;
  1298. data[i++] = (unsigned int)mgp->link_changes;
  1299. data[i++] = (unsigned int)ntohl(mgp->fw_stats->link_up);
  1300. data[i++] = (unsigned int)ntohl(mgp->fw_stats->dropped_link_overflow);
  1301. data[i++] =
  1302. (unsigned int)ntohl(mgp->fw_stats->dropped_link_error_or_filtered);
  1303. data[i++] = (unsigned int)ntohl(mgp->fw_stats->dropped_pause);
  1304. data[i++] = (unsigned int)ntohl(mgp->fw_stats->dropped_bad_phy);
  1305. data[i++] = (unsigned int)ntohl(mgp->fw_stats->dropped_bad_crc32);
  1306. data[i++] =
  1307. (unsigned int)ntohl(mgp->fw_stats->dropped_unicast_filtered);
  1308. data[i++] =
  1309. (unsigned int)ntohl(mgp->fw_stats->dropped_multicast_filtered);
  1310. data[i++] = (unsigned int)ntohl(mgp->fw_stats->dropped_runt);
  1311. data[i++] = (unsigned int)ntohl(mgp->fw_stats->dropped_overrun);
  1312. data[i++] = (unsigned int)ntohl(mgp->fw_stats->dropped_no_small_buffer);
  1313. data[i++] = (unsigned int)ntohl(mgp->fw_stats->dropped_no_big_buffer);
  1314. data[i++] = mgp->rx_done.lro_mgr.stats.aggregated;
  1315. data[i++] = mgp->rx_done.lro_mgr.stats.flushed;
  1316. if (mgp->rx_done.lro_mgr.stats.flushed)
  1317. data[i++] = mgp->rx_done.lro_mgr.stats.aggregated /
  1318. mgp->rx_done.lro_mgr.stats.flushed;
  1319. else
  1320. data[i++] = 0;
  1321. data[i++] = mgp->rx_done.lro_mgr.stats.no_desc;
  1322. }
  1323. static void myri10ge_set_msglevel(struct net_device *netdev, u32 value)
  1324. {
  1325. struct myri10ge_priv *mgp = netdev_priv(netdev);
  1326. mgp->msg_enable = value;
  1327. }
  1328. static u32 myri10ge_get_msglevel(struct net_device *netdev)
  1329. {
  1330. struct myri10ge_priv *mgp = netdev_priv(netdev);
  1331. return mgp->msg_enable;
  1332. }
  1333. static const struct ethtool_ops myri10ge_ethtool_ops = {
  1334. .get_settings = myri10ge_get_settings,
  1335. .get_drvinfo = myri10ge_get_drvinfo,
  1336. .get_coalesce = myri10ge_get_coalesce,
  1337. .set_coalesce = myri10ge_set_coalesce,
  1338. .get_pauseparam = myri10ge_get_pauseparam,
  1339. .set_pauseparam = myri10ge_set_pauseparam,
  1340. .get_ringparam = myri10ge_get_ringparam,
  1341. .get_rx_csum = myri10ge_get_rx_csum,
  1342. .set_rx_csum = myri10ge_set_rx_csum,
  1343. .set_tx_csum = ethtool_op_set_tx_hw_csum,
  1344. .set_sg = ethtool_op_set_sg,
  1345. .set_tso = myri10ge_set_tso,
  1346. .get_link = ethtool_op_get_link,
  1347. .get_strings = myri10ge_get_strings,
  1348. .get_sset_count = myri10ge_get_sset_count,
  1349. .get_ethtool_stats = myri10ge_get_ethtool_stats,
  1350. .set_msglevel = myri10ge_set_msglevel,
  1351. .get_msglevel = myri10ge_get_msglevel
  1352. };
  1353. static int myri10ge_allocate_rings(struct net_device *dev)
  1354. {
  1355. struct myri10ge_priv *mgp;
  1356. struct myri10ge_cmd cmd;
  1357. int tx_ring_size, rx_ring_size;
  1358. int tx_ring_entries, rx_ring_entries;
  1359. int i, status;
  1360. size_t bytes;
  1361. mgp = netdev_priv(dev);
  1362. /* get ring sizes */
  1363. status = myri10ge_send_cmd(mgp, MXGEFW_CMD_GET_SEND_RING_SIZE, &cmd, 0);
  1364. tx_ring_size = cmd.data0;
  1365. status |= myri10ge_send_cmd(mgp, MXGEFW_CMD_GET_RX_RING_SIZE, &cmd, 0);
  1366. if (status != 0)
  1367. return status;
  1368. rx_ring_size = cmd.data0;
  1369. tx_ring_entries = tx_ring_size / sizeof(struct mcp_kreq_ether_send);
  1370. rx_ring_entries = rx_ring_size / sizeof(struct mcp_dma_addr);
  1371. mgp->tx.mask = tx_ring_entries - 1;
  1372. mgp->rx_small.mask = mgp->rx_big.mask = rx_ring_entries - 1;
  1373. status = -ENOMEM;
  1374. /* allocate the host shadow rings */
  1375. bytes = 8 + (MYRI10GE_MAX_SEND_DESC_TSO + 4)
  1376. * sizeof(*mgp->tx.req_list);
  1377. mgp->tx.req_bytes = kzalloc(bytes, GFP_KERNEL);
  1378. if (mgp->tx.req_bytes == NULL)
  1379. goto abort_with_nothing;
  1380. /* ensure req_list entries are aligned to 8 bytes */
  1381. mgp->tx.req_list = (struct mcp_kreq_ether_send *)
  1382. ALIGN((unsigned long)mgp->tx.req_bytes, 8);
  1383. bytes = rx_ring_entries * sizeof(*mgp->rx_small.shadow);
  1384. mgp->rx_small.shadow = kzalloc(bytes, GFP_KERNEL);
  1385. if (mgp->rx_small.shadow == NULL)
  1386. goto abort_with_tx_req_bytes;
  1387. bytes = rx_ring_entries * sizeof(*mgp->rx_big.shadow);
  1388. mgp->rx_big.shadow = kzalloc(bytes, GFP_KERNEL);
  1389. if (mgp->rx_big.shadow == NULL)
  1390. goto abort_with_rx_small_shadow;
  1391. /* allocate the host info rings */
  1392. bytes = tx_ring_entries * sizeof(*mgp->tx.info);
  1393. mgp->tx.info = kzalloc(bytes, GFP_KERNEL);
  1394. if (mgp->tx.info == NULL)
  1395. goto abort_with_rx_big_shadow;
  1396. bytes = rx_ring_entries * sizeof(*mgp->rx_small.info);
  1397. mgp->rx_small.info = kzalloc(bytes, GFP_KERNEL);
  1398. if (mgp->rx_small.info == NULL)
  1399. goto abort_with_tx_info;
  1400. bytes = rx_ring_entries * sizeof(*mgp->rx_big.info);
  1401. mgp->rx_big.info = kzalloc(bytes, GFP_KERNEL);
  1402. if (mgp->rx_big.info == NULL)
  1403. goto abort_with_rx_small_info;
  1404. /* Fill the receive rings */
  1405. mgp->rx_big.cnt = 0;
  1406. mgp->rx_small.cnt = 0;
  1407. mgp->rx_big.fill_cnt = 0;
  1408. mgp->rx_small.fill_cnt = 0;
  1409. mgp->rx_small.page_offset = MYRI10GE_ALLOC_SIZE;
  1410. mgp->rx_big.page_offset = MYRI10GE_ALLOC_SIZE;
  1411. mgp->rx_small.watchdog_needed = 0;
  1412. mgp->rx_big.watchdog_needed = 0;
  1413. myri10ge_alloc_rx_pages(mgp, &mgp->rx_small,
  1414. mgp->small_bytes + MXGEFW_PAD, 0);
  1415. if (mgp->rx_small.fill_cnt < mgp->rx_small.mask + 1) {
  1416. printk(KERN_ERR "myri10ge: %s: alloced only %d small bufs\n",
  1417. dev->name, mgp->rx_small.fill_cnt);
  1418. goto abort_with_rx_small_ring;
  1419. }
  1420. myri10ge_alloc_rx_pages(mgp, &mgp->rx_big, mgp->big_bytes, 0);
  1421. if (mgp->rx_big.fill_cnt < mgp->rx_big.mask + 1) {
  1422. printk(KERN_ERR "myri10ge: %s: alloced only %d big bufs\n",
  1423. dev->name, mgp->rx_big.fill_cnt);
  1424. goto abort_with_rx_big_ring;
  1425. }
  1426. return 0;
  1427. abort_with_rx_big_ring:
  1428. for (i = mgp->rx_big.cnt; i < mgp->rx_big.fill_cnt; i++) {
  1429. int idx = i & mgp->rx_big.mask;
  1430. myri10ge_unmap_rx_page(mgp->pdev, &mgp->rx_big.info[idx],
  1431. mgp->big_bytes);
  1432. put_page(mgp->rx_big.info[idx].page);
  1433. }
  1434. abort_with_rx_small_ring:
  1435. for (i = mgp->rx_small.cnt; i < mgp->rx_small.fill_cnt; i++) {
  1436. int idx = i & mgp->rx_small.mask;
  1437. myri10ge_unmap_rx_page(mgp->pdev, &mgp->rx_small.info[idx],
  1438. mgp->small_bytes + MXGEFW_PAD);
  1439. put_page(mgp->rx_small.info[idx].page);
  1440. }
  1441. kfree(mgp->rx_big.info);
  1442. abort_with_rx_small_info:
  1443. kfree(mgp->rx_small.info);
  1444. abort_with_tx_info:
  1445. kfree(mgp->tx.info);
  1446. abort_with_rx_big_shadow:
  1447. kfree(mgp->rx_big.shadow);
  1448. abort_with_rx_small_shadow:
  1449. kfree(mgp->rx_small.shadow);
  1450. abort_with_tx_req_bytes:
  1451. kfree(mgp->tx.req_bytes);
  1452. mgp->tx.req_bytes = NULL;
  1453. mgp->tx.req_list = NULL;
  1454. abort_with_nothing:
  1455. return status;
  1456. }
  1457. static void myri10ge_free_rings(struct net_device *dev)
  1458. {
  1459. struct myri10ge_priv *mgp;
  1460. struct sk_buff *skb;
  1461. struct myri10ge_tx_buf *tx;
  1462. int i, len, idx;
  1463. mgp = netdev_priv(dev);
  1464. for (i = mgp->rx_big.cnt; i < mgp->rx_big.fill_cnt; i++) {
  1465. idx = i & mgp->rx_big.mask;
  1466. if (i == mgp->rx_big.fill_cnt - 1)
  1467. mgp->rx_big.info[idx].page_offset = MYRI10GE_ALLOC_SIZE;
  1468. myri10ge_unmap_rx_page(mgp->pdev, &mgp->rx_big.info[idx],
  1469. mgp->big_bytes);
  1470. put_page(mgp->rx_big.info[idx].page);
  1471. }
  1472. for (i = mgp->rx_small.cnt; i < mgp->rx_small.fill_cnt; i++) {
  1473. idx = i & mgp->rx_small.mask;
  1474. if (i == mgp->rx_small.fill_cnt - 1)
  1475. mgp->rx_small.info[idx].page_offset =
  1476. MYRI10GE_ALLOC_SIZE;
  1477. myri10ge_unmap_rx_page(mgp->pdev, &mgp->rx_small.info[idx],
  1478. mgp->small_bytes + MXGEFW_PAD);
  1479. put_page(mgp->rx_small.info[idx].page);
  1480. }
  1481. tx = &mgp->tx;
  1482. while (tx->done != tx->req) {
  1483. idx = tx->done & tx->mask;
  1484. skb = tx->info[idx].skb;
  1485. /* Mark as free */
  1486. tx->info[idx].skb = NULL;
  1487. tx->done++;
  1488. len = pci_unmap_len(&tx->info[idx], len);
  1489. pci_unmap_len_set(&tx->info[idx], len, 0);
  1490. if (skb) {
  1491. mgp->stats.tx_dropped++;
  1492. dev_kfree_skb_any(skb);
  1493. if (len)
  1494. pci_unmap_single(mgp->pdev,
  1495. pci_unmap_addr(&tx->info[idx],
  1496. bus), len,
  1497. PCI_DMA_TODEVICE);
  1498. } else {
  1499. if (len)
  1500. pci_unmap_page(mgp->pdev,
  1501. pci_unmap_addr(&tx->info[idx],
  1502. bus), len,
  1503. PCI_DMA_TODEVICE);
  1504. }
  1505. }
  1506. kfree(mgp->rx_big.info);
  1507. kfree(mgp->rx_small.info);
  1508. kfree(mgp->tx.info);
  1509. kfree(mgp->rx_big.shadow);
  1510. kfree(mgp->rx_small.shadow);
  1511. kfree(mgp->tx.req_bytes);
  1512. mgp->tx.req_bytes = NULL;
  1513. mgp->tx.req_list = NULL;
  1514. }
  1515. static int myri10ge_request_irq(struct myri10ge_priv *mgp)
  1516. {
  1517. struct pci_dev *pdev = mgp->pdev;
  1518. int status;
  1519. if (myri10ge_msi) {
  1520. status = pci_enable_msi(pdev);
  1521. if (status != 0)
  1522. dev_err(&pdev->dev,
  1523. "Error %d setting up MSI; falling back to xPIC\n",
  1524. status);
  1525. else
  1526. mgp->msi_enabled = 1;
  1527. } else {
  1528. mgp->msi_enabled = 0;
  1529. }
  1530. status = request_irq(pdev->irq, myri10ge_intr, IRQF_SHARED,
  1531. mgp->dev->name, mgp);
  1532. if (status != 0) {
  1533. dev_err(&pdev->dev, "failed to allocate IRQ\n");
  1534. if (mgp->msi_enabled)
  1535. pci_disable_msi(pdev);
  1536. }
  1537. return status;
  1538. }
  1539. static void myri10ge_free_irq(struct myri10ge_priv *mgp)
  1540. {
  1541. struct pci_dev *pdev = mgp->pdev;
  1542. free_irq(pdev->irq, mgp);
  1543. if (mgp->msi_enabled)
  1544. pci_disable_msi(pdev);
  1545. }
  1546. static int
  1547. myri10ge_get_frag_header(struct skb_frag_struct *frag, void **mac_hdr,
  1548. void **ip_hdr, void **tcpudp_hdr,
  1549. u64 * hdr_flags, void *priv)
  1550. {
  1551. struct ethhdr *eh;
  1552. struct vlan_ethhdr *veh;
  1553. struct iphdr *iph;
  1554. u8 *va = page_address(frag->page) + frag->page_offset;
  1555. unsigned long ll_hlen;
  1556. __wsum csum = (__wsum) (unsigned long)priv;
  1557. /* find the mac header, aborting if not IPv4 */
  1558. eh = (struct ethhdr *)va;
  1559. *mac_hdr = eh;
  1560. ll_hlen = ETH_HLEN;
  1561. if (eh->h_proto != htons(ETH_P_IP)) {
  1562. if (eh->h_proto == htons(ETH_P_8021Q)) {
  1563. veh = (struct vlan_ethhdr *)va;
  1564. if (veh->h_vlan_encapsulated_proto != htons(ETH_P_IP))
  1565. return -1;
  1566. ll_hlen += VLAN_HLEN;
  1567. /*
  1568. * HW checksum starts ETH_HLEN bytes into
  1569. * frame, so we must subtract off the VLAN
  1570. * header's checksum before csum can be used
  1571. */
  1572. csum = csum_sub(csum, csum_partial(va + ETH_HLEN,
  1573. VLAN_HLEN, 0));
  1574. } else {
  1575. return -1;
  1576. }
  1577. }
  1578. *hdr_flags = LRO_IPV4;
  1579. iph = (struct iphdr *)(va + ll_hlen);
  1580. *ip_hdr = iph;
  1581. if (iph->protocol != IPPROTO_TCP)
  1582. return -1;
  1583. *hdr_flags |= LRO_TCP;
  1584. *tcpudp_hdr = (u8 *) (*ip_hdr) + (iph->ihl << 2);
  1585. /* verify the IP checksum */
  1586. if (unlikely(ip_fast_csum((u8 *) iph, iph->ihl)))
  1587. return -1;
  1588. /* verify the checksum */
  1589. if (unlikely(csum_tcpudp_magic(iph->saddr, iph->daddr,
  1590. ntohs(iph->tot_len) - (iph->ihl << 2),
  1591. IPPROTO_TCP, csum)))
  1592. return -1;
  1593. return 0;
  1594. }
  1595. static int myri10ge_open(struct net_device *dev)
  1596. {
  1597. struct myri10ge_priv *mgp;
  1598. struct myri10ge_cmd cmd;
  1599. struct net_lro_mgr *lro_mgr;
  1600. int status, big_pow2;
  1601. mgp = netdev_priv(dev);
  1602. if (mgp->running != MYRI10GE_ETH_STOPPED)
  1603. return -EBUSY;
  1604. mgp->running = MYRI10GE_ETH_STARTING;
  1605. status = myri10ge_reset(mgp);
  1606. if (status != 0) {
  1607. printk(KERN_ERR "myri10ge: %s: failed reset\n", dev->name);
  1608. goto abort_with_nothing;
  1609. }
  1610. status = myri10ge_request_irq(mgp);
  1611. if (status != 0)
  1612. goto abort_with_nothing;
  1613. /* decide what small buffer size to use. For good TCP rx
  1614. * performance, it is important to not receive 1514 byte
  1615. * frames into jumbo buffers, as it confuses the socket buffer
  1616. * accounting code, leading to drops and erratic performance.
  1617. */
  1618. if (dev->mtu <= ETH_DATA_LEN)
  1619. /* enough for a TCP header */
  1620. mgp->small_bytes = (128 > SMP_CACHE_BYTES)
  1621. ? (128 - MXGEFW_PAD)
  1622. : (SMP_CACHE_BYTES - MXGEFW_PAD);
  1623. else
  1624. /* enough for a vlan encapsulated ETH_DATA_LEN frame */
  1625. mgp->small_bytes = VLAN_ETH_FRAME_LEN;
  1626. /* Override the small buffer size? */
  1627. if (myri10ge_small_bytes > 0)
  1628. mgp->small_bytes = myri10ge_small_bytes;
  1629. /* get the lanai pointers to the send and receive rings */
  1630. status |= myri10ge_send_cmd(mgp, MXGEFW_CMD_GET_SEND_OFFSET, &cmd, 0);
  1631. mgp->tx.lanai =
  1632. (struct mcp_kreq_ether_send __iomem *)(mgp->sram + cmd.data0);
  1633. status |=
  1634. myri10ge_send_cmd(mgp, MXGEFW_CMD_GET_SMALL_RX_OFFSET, &cmd, 0);
  1635. mgp->rx_small.lanai =
  1636. (struct mcp_kreq_ether_recv __iomem *)(mgp->sram + cmd.data0);
  1637. status |= myri10ge_send_cmd(mgp, MXGEFW_CMD_GET_BIG_RX_OFFSET, &cmd, 0);
  1638. mgp->rx_big.lanai =
  1639. (struct mcp_kreq_ether_recv __iomem *)(mgp->sram + cmd.data0);
  1640. if (status != 0) {
  1641. printk(KERN_ERR
  1642. "myri10ge: %s: failed to get ring sizes or locations\n",
  1643. dev->name);
  1644. mgp->running = MYRI10GE_ETH_STOPPED;
  1645. goto abort_with_irq;
  1646. }
  1647. if (myri10ge_wcfifo && mgp->wc_enabled) {
  1648. mgp->tx.wc_fifo = (u8 __iomem *) mgp->sram + MXGEFW_ETH_SEND_4;
  1649. mgp->rx_small.wc_fifo =
  1650. (u8 __iomem *) mgp->sram + MXGEFW_ETH_RECV_SMALL;
  1651. mgp->rx_big.wc_fifo =
  1652. (u8 __iomem *) mgp->sram + MXGEFW_ETH_RECV_BIG;
  1653. } else {
  1654. mgp->tx.wc_fifo = NULL;
  1655. mgp->rx_small.wc_fifo = NULL;
  1656. mgp->rx_big.wc_fifo = NULL;
  1657. }
  1658. /* Firmware needs the big buff size as a power of 2. Lie and
  1659. * tell him the buffer is larger, because we only use 1
  1660. * buffer/pkt, and the mtu will prevent overruns.
  1661. */
  1662. big_pow2 = dev->mtu + ETH_HLEN + VLAN_HLEN + MXGEFW_PAD;
  1663. if (big_pow2 < MYRI10GE_ALLOC_SIZE / 2) {
  1664. while (!is_power_of_2(big_pow2))
  1665. big_pow2++;
  1666. mgp->big_bytes = dev->mtu + ETH_HLEN + VLAN_HLEN + MXGEFW_PAD;
  1667. } else {
  1668. big_pow2 = MYRI10GE_ALLOC_SIZE;
  1669. mgp->big_bytes = big_pow2;
  1670. }
  1671. status = myri10ge_allocate_rings(dev);
  1672. if (status != 0)
  1673. goto abort_with_irq;
  1674. /* now give firmware buffers sizes, and MTU */
  1675. cmd.data0 = dev->mtu + ETH_HLEN + VLAN_HLEN;
  1676. status = myri10ge_send_cmd(mgp, MXGEFW_CMD_SET_MTU, &cmd, 0);
  1677. cmd.data0 = mgp->small_bytes;
  1678. status |=
  1679. myri10ge_send_cmd(mgp, MXGEFW_CMD_SET_SMALL_BUFFER_SIZE, &cmd, 0);
  1680. cmd.data0 = big_pow2;
  1681. status |=
  1682. myri10ge_send_cmd(mgp, MXGEFW_CMD_SET_BIG_BUFFER_SIZE, &cmd, 0);
  1683. if (status) {
  1684. printk(KERN_ERR "myri10ge: %s: Couldn't set buffer sizes\n",
  1685. dev->name);
  1686. goto abort_with_rings;
  1687. }
  1688. cmd.data0 = MYRI10GE_LOWPART_TO_U32(mgp->fw_stats_bus);
  1689. cmd.data1 = MYRI10GE_HIGHPART_TO_U32(mgp->fw_stats_bus);
  1690. cmd.data2 = sizeof(struct mcp_irq_data);
  1691. status = myri10ge_send_cmd(mgp, MXGEFW_CMD_SET_STATS_DMA_V2, &cmd, 0);
  1692. if (status == -ENOSYS) {
  1693. dma_addr_t bus = mgp->fw_stats_bus;
  1694. bus += offsetof(struct mcp_irq_data, send_done_count);
  1695. cmd.data0 = MYRI10GE_LOWPART_TO_U32(bus);
  1696. cmd.data1 = MYRI10GE_HIGHPART_TO_U32(bus);
  1697. status = myri10ge_send_cmd(mgp,
  1698. MXGEFW_CMD_SET_STATS_DMA_OBSOLETE,
  1699. &cmd, 0);
  1700. /* Firmware cannot support multicast without STATS_DMA_V2 */
  1701. mgp->fw_multicast_support = 0;
  1702. } else {
  1703. mgp->fw_multicast_support = 1;
  1704. }
  1705. if (status) {
  1706. printk(KERN_ERR "myri10ge: %s: Couldn't set stats DMA\n",
  1707. dev->name);
  1708. goto abort_with_rings;
  1709. }
  1710. mgp->link_state = htonl(~0U);
  1711. mgp->rdma_tags_available = 15;
  1712. lro_mgr = &mgp->rx_done.lro_mgr;
  1713. lro_mgr->dev = dev;
  1714. lro_mgr->features = LRO_F_NAPI;
  1715. lro_mgr->ip_summed = CHECKSUM_COMPLETE;
  1716. lro_mgr->ip_summed_aggr = CHECKSUM_UNNECESSARY;
  1717. lro_mgr->max_desc = MYRI10GE_MAX_LRO_DESCRIPTORS;
  1718. lro_mgr->lro_arr = mgp->rx_done.lro_desc;
  1719. lro_mgr->get_frag_header = myri10ge_get_frag_header;
  1720. lro_mgr->max_aggr = myri10ge_lro_max_pkts;
  1721. lro_mgr->frag_align_pad = 2;
  1722. if (lro_mgr->max_aggr > MAX_SKB_FRAGS)
  1723. lro_mgr->max_aggr = MAX_SKB_FRAGS;
  1724. napi_enable(&mgp->napi); /* must happen prior to any irq */
  1725. status = myri10ge_send_cmd(mgp, MXGEFW_CMD_ETHERNET_UP, &cmd, 0);
  1726. if (status) {
  1727. printk(KERN_ERR "myri10ge: %s: Couldn't bring up link\n",
  1728. dev->name);
  1729. goto abort_with_rings;
  1730. }
  1731. mgp->wake_queue = 0;
  1732. mgp->stop_queue = 0;
  1733. mgp->running = MYRI10GE_ETH_RUNNING;
  1734. mgp->watchdog_timer.expires = jiffies + myri10ge_watchdog_timeout * HZ;
  1735. add_timer(&mgp->watchdog_timer);
  1736. netif_wake_queue(dev);
  1737. return 0;
  1738. abort_with_rings:
  1739. myri10ge_free_rings(dev);
  1740. abort_with_irq:
  1741. myri10ge_free_irq(mgp);
  1742. abort_with_nothing:
  1743. mgp->running = MYRI10GE_ETH_STOPPED;
  1744. return -ENOMEM;
  1745. }
  1746. static int myri10ge_close(struct net_device *dev)
  1747. {
  1748. struct myri10ge_priv *mgp;
  1749. struct myri10ge_cmd cmd;
  1750. int status, old_down_cnt;
  1751. mgp = netdev_priv(dev);
  1752. if (mgp->running != MYRI10GE_ETH_RUNNING)
  1753. return 0;
  1754. if (mgp->tx.req_bytes == NULL)
  1755. return 0;
  1756. del_timer_sync(&mgp->watchdog_timer);
  1757. mgp->running = MYRI10GE_ETH_STOPPING;
  1758. napi_disable(&mgp->napi);
  1759. netif_carrier_off(dev);
  1760. netif_stop_queue(dev);
  1761. old_down_cnt = mgp->down_cnt;
  1762. mb();
  1763. status = myri10ge_send_cmd(mgp, MXGEFW_CMD_ETHERNET_DOWN, &cmd, 0);
  1764. if (status)
  1765. printk(KERN_ERR "myri10ge: %s: Couldn't bring down link\n",
  1766. dev->name);
  1767. wait_event_timeout(mgp->down_wq, old_down_cnt != mgp->down_cnt, HZ);
  1768. if (old_down_cnt == mgp->down_cnt)
  1769. printk(KERN_ERR "myri10ge: %s never got down irq\n", dev->name);
  1770. netif_tx_disable(dev);
  1771. myri10ge_free_irq(mgp);
  1772. myri10ge_free_rings(dev);
  1773. mgp->running = MYRI10GE_ETH_STOPPED;
  1774. return 0;
  1775. }
  1776. /* copy an array of struct mcp_kreq_ether_send's to the mcp. Copy
  1777. * backwards one at a time and handle ring wraps */
  1778. static inline void
  1779. myri10ge_submit_req_backwards(struct myri10ge_tx_buf *tx,
  1780. struct mcp_kreq_ether_send *src, int cnt)
  1781. {
  1782. int idx, starting_slot;
  1783. starting_slot = tx->req;
  1784. while (cnt > 1) {
  1785. cnt--;
  1786. idx = (starting_slot + cnt) & tx->mask;
  1787. myri10ge_pio_copy(&tx->lanai[idx], &src[cnt], sizeof(*src));
  1788. mb();
  1789. }
  1790. }
  1791. /*
  1792. * copy an array of struct mcp_kreq_ether_send's to the mcp. Copy
  1793. * at most 32 bytes at a time, so as to avoid involving the software
  1794. * pio handler in the nic. We re-write the first segment's flags
  1795. * to mark them valid only after writing the entire chain.
  1796. */
  1797. static inline void
  1798. myri10ge_submit_req(struct myri10ge_tx_buf *tx, struct mcp_kreq_ether_send *src,
  1799. int cnt)
  1800. {
  1801. int idx, i;
  1802. struct mcp_kreq_ether_send __iomem *dstp, *dst;
  1803. struct mcp_kreq_ether_send *srcp;
  1804. u8 last_flags;
  1805. idx = tx->req & tx->mask;
  1806. last_flags = src->flags;
  1807. src->flags = 0;
  1808. mb();
  1809. dst = dstp = &tx->lanai[idx];
  1810. srcp = src;
  1811. if ((idx + cnt) < tx->mask) {
  1812. for (i = 0; i < (cnt - 1); i += 2) {
  1813. myri10ge_pio_copy(dstp, srcp, 2 * sizeof(*src));
  1814. mb(); /* force write every 32 bytes */
  1815. srcp += 2;
  1816. dstp += 2;
  1817. }
  1818. } else {
  1819. /* submit all but the first request, and ensure
  1820. * that it is submitted below */
  1821. myri10ge_submit_req_backwards(tx, src, cnt);
  1822. i = 0;
  1823. }
  1824. if (i < cnt) {
  1825. /* submit the first request */
  1826. myri10ge_pio_copy(dstp, srcp, sizeof(*src));
  1827. mb(); /* barrier before setting valid flag */
  1828. }
  1829. /* re-write the last 32-bits with the valid flags */
  1830. src->flags = last_flags;
  1831. put_be32(*((__be32 *) src + 3), (__be32 __iomem *) dst + 3);
  1832. tx->req += cnt;
  1833. mb();
  1834. }
  1835. static inline void
  1836. myri10ge_submit_req_wc(struct myri10ge_tx_buf *tx,
  1837. struct mcp_kreq_ether_send *src, int cnt)
  1838. {
  1839. tx->req += cnt;
  1840. mb();
  1841. while (cnt >= 4) {
  1842. myri10ge_pio_copy(tx->wc_fifo, src, 64);
  1843. mb();
  1844. src += 4;
  1845. cnt -= 4;
  1846. }
  1847. if (cnt > 0) {
  1848. /* pad it to 64 bytes. The src is 64 bytes bigger than it
  1849. * needs to be so that we don't overrun it */
  1850. myri10ge_pio_copy(tx->wc_fifo + MXGEFW_ETH_SEND_OFFSET(cnt),
  1851. src, 64);
  1852. mb();
  1853. }
  1854. }
  1855. /*
  1856. * Transmit a packet. We need to split the packet so that a single
  1857. * segment does not cross myri10ge->tx.boundary, so this makes segment
  1858. * counting tricky. So rather than try to count segments up front, we
  1859. * just give up if there are too few segments to hold a reasonably
  1860. * fragmented packet currently available. If we run
  1861. * out of segments while preparing a packet for DMA, we just linearize
  1862. * it and try again.
  1863. */
  1864. static int myri10ge_xmit(struct sk_buff *skb, struct net_device *dev)
  1865. {
  1866. struct myri10ge_priv *mgp = netdev_priv(dev);
  1867. struct mcp_kreq_ether_send *req;
  1868. struct myri10ge_tx_buf *tx = &mgp->tx;
  1869. struct skb_frag_struct *frag;
  1870. dma_addr_t bus;
  1871. u32 low;
  1872. __be32 high_swapped;
  1873. unsigned int len;
  1874. int idx, last_idx, avail, frag_cnt, frag_idx, count, mss, max_segments;
  1875. u16 pseudo_hdr_offset, cksum_offset;
  1876. int cum_len, seglen, boundary, rdma_count;
  1877. u8 flags, odd_flag;
  1878. again:
  1879. req = tx->req_list;
  1880. avail = tx->mask - 1 - (tx->req - tx->done);
  1881. mss = 0;
  1882. max_segments = MXGEFW_MAX_SEND_DESC;
  1883. if (skb_is_gso(skb)) {
  1884. mss = skb_shinfo(skb)->gso_size;
  1885. max_segments = MYRI10GE_MAX_SEND_DESC_TSO;
  1886. }
  1887. if ((unlikely(avail < max_segments))) {
  1888. /* we are out of transmit resources */
  1889. mgp->stop_queue++;
  1890. netif_stop_queue(dev);
  1891. return 1;
  1892. }
  1893. /* Setup checksum offloading, if needed */
  1894. cksum_offset = 0;
  1895. pseudo_hdr_offset = 0;
  1896. odd_flag = 0;
  1897. flags = (MXGEFW_FLAGS_NO_TSO | MXGEFW_FLAGS_FIRST);
  1898. if (likely(skb->ip_summed == CHECKSUM_PARTIAL)) {
  1899. cksum_offset = skb_transport_offset(skb);
  1900. pseudo_hdr_offset = cksum_offset + skb->csum_offset;
  1901. /* If the headers are excessively large, then we must
  1902. * fall back to a software checksum */
  1903. if (unlikely(!mss && (cksum_offset > 255 ||
  1904. pseudo_hdr_offset > 127))) {
  1905. if (skb_checksum_help(skb))
  1906. goto drop;
  1907. cksum_offset = 0;
  1908. pseudo_hdr_offset = 0;
  1909. } else {
  1910. odd_flag = MXGEFW_FLAGS_ALIGN_ODD;
  1911. flags |= MXGEFW_FLAGS_CKSUM;
  1912. }
  1913. }
  1914. cum_len = 0;
  1915. if (mss) { /* TSO */
  1916. /* this removes any CKSUM flag from before */
  1917. flags = (MXGEFW_FLAGS_TSO_HDR | MXGEFW_FLAGS_FIRST);
  1918. /* negative cum_len signifies to the
  1919. * send loop that we are still in the
  1920. * header portion of the TSO packet.
  1921. * TSO header can be at most 1KB long */
  1922. cum_len = -(skb_transport_offset(skb) + tcp_hdrlen(skb));
  1923. /* for IPv6 TSO, the checksum offset stores the
  1924. * TCP header length, to save the firmware from
  1925. * the need to parse the headers */
  1926. if (skb_is_gso_v6(skb)) {
  1927. cksum_offset = tcp_hdrlen(skb);
  1928. /* Can only handle headers <= max_tso6 long */
  1929. if (unlikely(-cum_len > mgp->max_tso6))
  1930. return myri10ge_sw_tso(skb, dev);
  1931. }
  1932. /* for TSO, pseudo_hdr_offset holds mss.
  1933. * The firmware figures out where to put
  1934. * the checksum by parsing the header. */
  1935. pseudo_hdr_offset = mss;
  1936. } else
  1937. /* Mark small packets, and pad out tiny packets */
  1938. if (skb->len <= MXGEFW_SEND_SMALL_SIZE) {
  1939. flags |= MXGEFW_FLAGS_SMALL;
  1940. /* pad frames to at least ETH_ZLEN bytes */
  1941. if (unlikely(skb->len < ETH_ZLEN)) {
  1942. if (skb_padto(skb, ETH_ZLEN)) {
  1943. /* The packet is gone, so we must
  1944. * return 0 */
  1945. mgp->stats.tx_dropped += 1;
  1946. return 0;
  1947. }
  1948. /* adjust the len to account for the zero pad
  1949. * so that the nic can know how long it is */
  1950. skb->len = ETH_ZLEN;
  1951. }
  1952. }
  1953. /* map the skb for DMA */
  1954. len = skb->len - skb->data_len;
  1955. idx = tx->req & tx->mask;
  1956. tx->info[idx].skb = skb;
  1957. bus = pci_map_single(mgp->pdev, skb->data, len, PCI_DMA_TODEVICE);
  1958. pci_unmap_addr_set(&tx->info[idx], bus, bus);
  1959. pci_unmap_len_set(&tx->info[idx], len, len);
  1960. frag_cnt = skb_shinfo(skb)->nr_frags;
  1961. frag_idx = 0;
  1962. count = 0;
  1963. rdma_count = 0;
  1964. /* "rdma_count" is the number of RDMAs belonging to the
  1965. * current packet BEFORE the current send request. For
  1966. * non-TSO packets, this is equal to "count".
  1967. * For TSO packets, rdma_count needs to be reset
  1968. * to 0 after a segment cut.
  1969. *
  1970. * The rdma_count field of the send request is
  1971. * the number of RDMAs of the packet starting at
  1972. * that request. For TSO send requests with one ore more cuts
  1973. * in the middle, this is the number of RDMAs starting
  1974. * after the last cut in the request. All previous
  1975. * segments before the last cut implicitly have 1 RDMA.
  1976. *
  1977. * Since the number of RDMAs is not known beforehand,
  1978. * it must be filled-in retroactively - after each
  1979. * segmentation cut or at the end of the entire packet.
  1980. */
  1981. while (1) {
  1982. /* Break the SKB or Fragment up into pieces which
  1983. * do not cross mgp->tx.boundary */
  1984. low = MYRI10GE_LOWPART_TO_U32(bus);
  1985. high_swapped = htonl(MYRI10GE_HIGHPART_TO_U32(bus));
  1986. while (len) {
  1987. u8 flags_next;
  1988. int cum_len_next;
  1989. if (unlikely(count == max_segments))
  1990. goto abort_linearize;
  1991. boundary = (low + tx->boundary) & ~(tx->boundary - 1);
  1992. seglen = boundary - low;
  1993. if (seglen > len)
  1994. seglen = len;
  1995. flags_next = flags & ~MXGEFW_FLAGS_FIRST;
  1996. cum_len_next = cum_len + seglen;
  1997. if (mss) { /* TSO */
  1998. (req - rdma_count)->rdma_count = rdma_count + 1;
  1999. if (likely(cum_len >= 0)) { /* payload */
  2000. int next_is_first, chop;
  2001. chop = (cum_len_next > mss);
  2002. cum_len_next = cum_len_next % mss;
  2003. next_is_first = (cum_len_next == 0);
  2004. flags |= chop * MXGEFW_FLAGS_TSO_CHOP;
  2005. flags_next |= next_is_first *
  2006. MXGEFW_FLAGS_FIRST;
  2007. rdma_count |= -(chop | next_is_first);
  2008. rdma_count += chop & !next_is_first;
  2009. } else if (likely(cum_len_next >= 0)) { /* header ends */
  2010. int small;
  2011. rdma_count = -1;
  2012. cum_len_next = 0;
  2013. seglen = -cum_len;
  2014. small = (mss <= MXGEFW_SEND_SMALL_SIZE);
  2015. flags_next = MXGEFW_FLAGS_TSO_PLD |
  2016. MXGEFW_FLAGS_FIRST |
  2017. (small * MXGEFW_FLAGS_SMALL);
  2018. }
  2019. }
  2020. req->addr_high = high_swapped;
  2021. req->addr_low = htonl(low);
  2022. req->pseudo_hdr_offset = htons(pseudo_hdr_offset);
  2023. req->pad = 0; /* complete solid 16-byte block; does this matter? */
  2024. req->rdma_count = 1;
  2025. req->length = htons(seglen);
  2026. req->cksum_offset = cksum_offset;
  2027. req->flags = flags | ((cum_len & 1) * odd_flag);
  2028. low += seglen;
  2029. len -= seglen;
  2030. cum_len = cum_len_next;
  2031. flags = flags_next;
  2032. req++;
  2033. count++;
  2034. rdma_count++;
  2035. if (cksum_offset != 0 && !(mss && skb_is_gso_v6(skb))) {
  2036. if (unlikely(cksum_offset > seglen))
  2037. cksum_offset -= seglen;
  2038. else
  2039. cksum_offset = 0;
  2040. }
  2041. }
  2042. if (frag_idx == frag_cnt)
  2043. break;
  2044. /* map next fragment for DMA */
  2045. idx = (count + tx->req) & tx->mask;
  2046. frag = &skb_shinfo(skb)->frags[frag_idx];
  2047. frag_idx++;
  2048. len = frag->size;
  2049. bus = pci_map_page(mgp->pdev, frag->page, frag->page_offset,
  2050. len, PCI_DMA_TODEVICE);
  2051. pci_unmap_addr_set(&tx->info[idx], bus, bus);
  2052. pci_unmap_len_set(&tx->info[idx], len, len);
  2053. }
  2054. (req - rdma_count)->rdma_count = rdma_count;
  2055. if (mss)
  2056. do {
  2057. req--;
  2058. req->flags |= MXGEFW_FLAGS_TSO_LAST;
  2059. } while (!(req->flags & (MXGEFW_FLAGS_TSO_CHOP |
  2060. MXGEFW_FLAGS_FIRST)));
  2061. idx = ((count - 1) + tx->req) & tx->mask;
  2062. tx->info[idx].last = 1;
  2063. if (tx->wc_fifo == NULL)
  2064. myri10ge_submit_req(tx, tx->req_list, count);
  2065. else
  2066. myri10ge_submit_req_wc(tx, tx->req_list, count);
  2067. tx->pkt_start++;
  2068. if ((avail - count) < MXGEFW_MAX_SEND_DESC) {
  2069. mgp->stop_queue++;
  2070. netif_stop_queue(dev);
  2071. }
  2072. dev->trans_start = jiffies;
  2073. return 0;
  2074. abort_linearize:
  2075. /* Free any DMA resources we've alloced and clear out the skb
  2076. * slot so as to not trip up assertions, and to avoid a
  2077. * double-free if linearizing fails */
  2078. last_idx = (idx + 1) & tx->mask;
  2079. idx = tx->req & tx->mask;
  2080. tx->info[idx].skb = NULL;
  2081. do {
  2082. len = pci_unmap_len(&tx->info[idx], len);
  2083. if (len) {
  2084. if (tx->info[idx].skb != NULL)
  2085. pci_unmap_single(mgp->pdev,
  2086. pci_unmap_addr(&tx->info[idx],
  2087. bus), len,
  2088. PCI_DMA_TODEVICE);
  2089. else
  2090. pci_unmap_page(mgp->pdev,
  2091. pci_unmap_addr(&tx->info[idx],
  2092. bus), len,
  2093. PCI_DMA_TODEVICE);
  2094. pci_unmap_len_set(&tx->info[idx], len, 0);
  2095. tx->info[idx].skb = NULL;
  2096. }
  2097. idx = (idx + 1) & tx->mask;
  2098. } while (idx != last_idx);
  2099. if (skb_is_gso(skb)) {
  2100. printk(KERN_ERR
  2101. "myri10ge: %s: TSO but wanted to linearize?!?!?\n",
  2102. mgp->dev->name);
  2103. goto drop;
  2104. }
  2105. if (skb_linearize(skb))
  2106. goto drop;
  2107. mgp->tx_linearized++;
  2108. goto again;
  2109. drop:
  2110. dev_kfree_skb_any(skb);
  2111. mgp->stats.tx_dropped += 1;
  2112. return 0;
  2113. }
  2114. static int myri10ge_sw_tso(struct sk_buff *skb, struct net_device *dev)
  2115. {
  2116. struct sk_buff *segs, *curr;
  2117. struct myri10ge_priv *mgp = dev->priv;
  2118. int status;
  2119. segs = skb_gso_segment(skb, dev->features & ~NETIF_F_TSO6);
  2120. if (unlikely(IS_ERR(segs)))
  2121. goto drop;
  2122. while (segs) {
  2123. curr = segs;
  2124. segs = segs->next;
  2125. curr->next = NULL;
  2126. status = myri10ge_xmit(curr, dev);
  2127. if (status != 0) {
  2128. dev_kfree_skb_any(curr);
  2129. if (segs != NULL) {
  2130. curr = segs;
  2131. segs = segs->next;
  2132. curr->next = NULL;
  2133. dev_kfree_skb_any(segs);
  2134. }
  2135. goto drop;
  2136. }
  2137. }
  2138. dev_kfree_skb_any(skb);
  2139. return 0;
  2140. drop:
  2141. dev_kfree_skb_any(skb);
  2142. mgp->stats.tx_dropped += 1;
  2143. return 0;
  2144. }
  2145. static struct net_device_stats *myri10ge_get_stats(struct net_device *dev)
  2146. {
  2147. struct myri10ge_priv *mgp = netdev_priv(dev);
  2148. return &mgp->stats;
  2149. }
  2150. static void myri10ge_set_multicast_list(struct net_device *dev)
  2151. {
  2152. struct myri10ge_cmd cmd;
  2153. struct myri10ge_priv *mgp;
  2154. struct dev_mc_list *mc_list;
  2155. __be32 data[2] = { 0, 0 };
  2156. int err;
  2157. DECLARE_MAC_BUF(mac);
  2158. mgp = netdev_priv(dev);
  2159. /* can be called from atomic contexts,
  2160. * pass 1 to force atomicity in myri10ge_send_cmd() */
  2161. myri10ge_change_promisc(mgp, dev->flags & IFF_PROMISC, 1);
  2162. /* This firmware is known to not support multicast */
  2163. if (!mgp->fw_multicast_support)
  2164. return;
  2165. /* Disable multicast filtering */
  2166. err = myri10ge_send_cmd(mgp, MXGEFW_ENABLE_ALLMULTI, &cmd, 1);
  2167. if (err != 0) {
  2168. printk(KERN_ERR "myri10ge: %s: Failed MXGEFW_ENABLE_ALLMULTI,"
  2169. " error status: %d\n", dev->name, err);
  2170. goto abort;
  2171. }
  2172. if ((dev->flags & IFF_ALLMULTI) || mgp->adopted_rx_filter_bug) {
  2173. /* request to disable multicast filtering, so quit here */
  2174. return;
  2175. }
  2176. /* Flush the filters */
  2177. err = myri10ge_send_cmd(mgp, MXGEFW_LEAVE_ALL_MULTICAST_GROUPS,
  2178. &cmd, 1);
  2179. if (err != 0) {
  2180. printk(KERN_ERR
  2181. "myri10ge: %s: Failed MXGEFW_LEAVE_ALL_MULTICAST_GROUPS"
  2182. ", error status: %d\n", dev->name, err);
  2183. goto abort;
  2184. }
  2185. /* Walk the multicast list, and add each address */
  2186. for (mc_list = dev->mc_list; mc_list != NULL; mc_list = mc_list->next) {
  2187. memcpy(data, &mc_list->dmi_addr, 6);
  2188. cmd.data0 = ntohl(data[0]);
  2189. cmd.data1 = ntohl(data[1]);
  2190. err = myri10ge_send_cmd(mgp, MXGEFW_JOIN_MULTICAST_GROUP,
  2191. &cmd, 1);
  2192. if (err != 0) {
  2193. printk(KERN_ERR "myri10ge: %s: Failed "
  2194. "MXGEFW_JOIN_MULTICAST_GROUP, error status:"
  2195. "%d\t", dev->name, err);
  2196. printk(KERN_ERR "MAC %s\n",
  2197. print_mac(mac, mc_list->dmi_addr));
  2198. goto abort;
  2199. }
  2200. }
  2201. /* Enable multicast filtering */
  2202. err = myri10ge_send_cmd(mgp, MXGEFW_DISABLE_ALLMULTI, &cmd, 1);
  2203. if (err != 0) {
  2204. printk(KERN_ERR "myri10ge: %s: Failed MXGEFW_DISABLE_ALLMULTI,"
  2205. "error status: %d\n", dev->name, err);
  2206. goto abort;
  2207. }
  2208. return;
  2209. abort:
  2210. return;
  2211. }
  2212. static int myri10ge_set_mac_address(struct net_device *dev, void *addr)
  2213. {
  2214. struct sockaddr *sa = addr;
  2215. struct myri10ge_priv *mgp = netdev_priv(dev);
  2216. int status;
  2217. if (!is_valid_ether_addr(sa->sa_data))
  2218. return -EADDRNOTAVAIL;
  2219. status = myri10ge_update_mac_address(mgp, sa->sa_data);
  2220. if (status != 0) {
  2221. printk(KERN_ERR
  2222. "myri10ge: %s: changing mac address failed with %d\n",
  2223. dev->name, status);
  2224. return status;
  2225. }
  2226. /* change the dev structure */
  2227. memcpy(dev->dev_addr, sa->sa_data, 6);
  2228. return 0;
  2229. }
  2230. static int myri10ge_change_mtu(struct net_device *dev, int new_mtu)
  2231. {
  2232. struct myri10ge_priv *mgp = netdev_priv(dev);
  2233. int error = 0;
  2234. if ((new_mtu < 68) || (ETH_HLEN + new_mtu > MYRI10GE_MAX_ETHER_MTU)) {
  2235. printk(KERN_ERR "myri10ge: %s: new mtu (%d) is not valid\n",
  2236. dev->name, new_mtu);
  2237. return -EINVAL;
  2238. }
  2239. printk(KERN_INFO "%s: changing mtu from %d to %d\n",
  2240. dev->name, dev->mtu, new_mtu);
  2241. if (mgp->running) {
  2242. /* if we change the mtu on an active device, we must
  2243. * reset the device so the firmware sees the change */
  2244. myri10ge_close(dev);
  2245. dev->mtu = new_mtu;
  2246. myri10ge_open(dev);
  2247. } else
  2248. dev->mtu = new_mtu;
  2249. return error;
  2250. }
  2251. /*
  2252. * Enable ECRC to align PCI-E Completion packets on an 8-byte boundary.
  2253. * Only do it if the bridge is a root port since we don't want to disturb
  2254. * any other device, except if forced with myri10ge_ecrc_enable > 1.
  2255. */
  2256. static void myri10ge_enable_ecrc(struct myri10ge_priv *mgp)
  2257. {
  2258. struct pci_dev *bridge = mgp->pdev->bus->self;
  2259. struct device *dev = &mgp->pdev->dev;
  2260. unsigned cap;
  2261. unsigned err_cap;
  2262. u16 val;
  2263. u8 ext_type;
  2264. int ret;
  2265. if (!myri10ge_ecrc_enable || !bridge)
  2266. return;
  2267. /* check that the bridge is a root port */
  2268. cap = pci_find_capability(bridge, PCI_CAP_ID_EXP);
  2269. pci_read_config_word(bridge, cap + PCI_CAP_FLAGS, &val);
  2270. ext_type = (val & PCI_EXP_FLAGS_TYPE) >> 4;
  2271. if (ext_type != PCI_EXP_TYPE_ROOT_PORT) {
  2272. if (myri10ge_ecrc_enable > 1) {
  2273. struct pci_dev *old_bridge = bridge;
  2274. /* Walk the hierarchy up to the root port
  2275. * where ECRC has to be enabled */
  2276. do {
  2277. bridge = bridge->bus->self;
  2278. if (!bridge) {
  2279. dev_err(dev,
  2280. "Failed to find root port"
  2281. " to force ECRC\n");
  2282. return;
  2283. }
  2284. cap =
  2285. pci_find_capability(bridge, PCI_CAP_ID_EXP);
  2286. pci_read_config_word(bridge,
  2287. cap + PCI_CAP_FLAGS, &val);
  2288. ext_type = (val & PCI_EXP_FLAGS_TYPE) >> 4;
  2289. } while (ext_type != PCI_EXP_TYPE_ROOT_PORT);
  2290. dev_info(dev,
  2291. "Forcing ECRC on non-root port %s"
  2292. " (enabling on root port %s)\n",
  2293. pci_name(old_bridge), pci_name(bridge));
  2294. } else {
  2295. dev_err(dev,
  2296. "Not enabling ECRC on non-root port %s\n",
  2297. pci_name(bridge));
  2298. return;
  2299. }
  2300. }
  2301. cap = pci_find_ext_capability(bridge, PCI_EXT_CAP_ID_ERR);
  2302. if (!cap)
  2303. return;
  2304. ret = pci_read_config_dword(bridge, cap + PCI_ERR_CAP, &err_cap);
  2305. if (ret) {
  2306. dev_err(dev, "failed reading ext-conf-space of %s\n",
  2307. pci_name(bridge));
  2308. dev_err(dev, "\t pci=nommconf in use? "
  2309. "or buggy/incomplete/absent ACPI MCFG attr?\n");
  2310. return;
  2311. }
  2312. if (!(err_cap & PCI_ERR_CAP_ECRC_GENC))
  2313. return;
  2314. err_cap |= PCI_ERR_CAP_ECRC_GENE;
  2315. pci_write_config_dword(bridge, cap + PCI_ERR_CAP, err_cap);
  2316. dev_info(dev, "Enabled ECRC on upstream bridge %s\n", pci_name(bridge));
  2317. }
  2318. /*
  2319. * The Lanai Z8E PCI-E interface achieves higher Read-DMA throughput
  2320. * when the PCI-E Completion packets are aligned on an 8-byte
  2321. * boundary. Some PCI-E chip sets always align Completion packets; on
  2322. * the ones that do not, the alignment can be enforced by enabling
  2323. * ECRC generation (if supported).
  2324. *
  2325. * When PCI-E Completion packets are not aligned, it is actually more
  2326. * efficient to limit Read-DMA transactions to 2KB, rather than 4KB.
  2327. *
  2328. * If the driver can neither enable ECRC nor verify that it has
  2329. * already been enabled, then it must use a firmware image which works
  2330. * around unaligned completion packets (myri10ge_ethp_z8e.dat), and it
  2331. * should also ensure that it never gives the device a Read-DMA which is
  2332. * larger than 2KB by setting the tx.boundary to 2KB. If ECRC is
  2333. * enabled, then the driver should use the aligned (myri10ge_eth_z8e.dat)
  2334. * firmware image, and set tx.boundary to 4KB.
  2335. */
  2336. static void myri10ge_firmware_probe(struct myri10ge_priv *mgp)
  2337. {
  2338. struct pci_dev *pdev = mgp->pdev;
  2339. struct device *dev = &pdev->dev;
  2340. int status;
  2341. mgp->tx.boundary = 4096;
  2342. /*
  2343. * Verify the max read request size was set to 4KB
  2344. * before trying the test with 4KB.
  2345. */
  2346. status = pcie_get_readrq(pdev);
  2347. if (status < 0) {
  2348. dev_err(dev, "Couldn't read max read req size: %d\n", status);
  2349. goto abort;
  2350. }
  2351. if (status != 4096) {
  2352. dev_warn(dev, "Max Read Request size != 4096 (%d)\n", status);
  2353. mgp->tx.boundary = 2048;
  2354. }
  2355. /*
  2356. * load the optimized firmware (which assumes aligned PCIe
  2357. * completions) in order to see if it works on this host.
  2358. */
  2359. mgp->fw_name = myri10ge_fw_aligned;
  2360. status = myri10ge_load_firmware(mgp);
  2361. if (status != 0) {
  2362. goto abort;
  2363. }
  2364. /*
  2365. * Enable ECRC if possible
  2366. */
  2367. myri10ge_enable_ecrc(mgp);
  2368. /*
  2369. * Run a DMA test which watches for unaligned completions and
  2370. * aborts on the first one seen.
  2371. */
  2372. status = myri10ge_dma_test(mgp, MXGEFW_CMD_UNALIGNED_TEST);
  2373. if (status == 0)
  2374. return; /* keep the aligned firmware */
  2375. if (status != -E2BIG)
  2376. dev_warn(dev, "DMA test failed: %d\n", status);
  2377. if (status == -ENOSYS)
  2378. dev_warn(dev, "Falling back to ethp! "
  2379. "Please install up to date fw\n");
  2380. abort:
  2381. /* fall back to using the unaligned firmware */
  2382. mgp->tx.boundary = 2048;
  2383. mgp->fw_name = myri10ge_fw_unaligned;
  2384. }
  2385. static void myri10ge_select_firmware(struct myri10ge_priv *mgp)
  2386. {
  2387. if (myri10ge_force_firmware == 0) {
  2388. int link_width, exp_cap;
  2389. u16 lnk;
  2390. exp_cap = pci_find_capability(mgp->pdev, PCI_CAP_ID_EXP);
  2391. pci_read_config_word(mgp->pdev, exp_cap + PCI_EXP_LNKSTA, &lnk);
  2392. link_width = (lnk >> 4) & 0x3f;
  2393. /* Check to see if Link is less than 8 or if the
  2394. * upstream bridge is known to provide aligned
  2395. * completions */
  2396. if (link_width < 8) {
  2397. dev_info(&mgp->pdev->dev, "PCIE x%d Link\n",
  2398. link_width);
  2399. mgp->tx.boundary = 4096;
  2400. mgp->fw_name = myri10ge_fw_aligned;
  2401. } else {
  2402. myri10ge_firmware_probe(mgp);
  2403. }
  2404. } else {
  2405. if (myri10ge_force_firmware == 1) {
  2406. dev_info(&mgp->pdev->dev,
  2407. "Assuming aligned completions (forced)\n");
  2408. mgp->tx.boundary = 4096;
  2409. mgp->fw_name = myri10ge_fw_aligned;
  2410. } else {
  2411. dev_info(&mgp->pdev->dev,
  2412. "Assuming unaligned completions (forced)\n");
  2413. mgp->tx.boundary = 2048;
  2414. mgp->fw_name = myri10ge_fw_unaligned;
  2415. }
  2416. }
  2417. if (myri10ge_fw_name != NULL) {
  2418. dev_info(&mgp->pdev->dev, "overriding firmware to %s\n",
  2419. myri10ge_fw_name);
  2420. mgp->fw_name = myri10ge_fw_name;
  2421. }
  2422. }
  2423. #ifdef CONFIG_PM
  2424. static int myri10ge_suspend(struct pci_dev *pdev, pm_message_t state)
  2425. {
  2426. struct myri10ge_priv *mgp;
  2427. struct net_device *netdev;
  2428. mgp = pci_get_drvdata(pdev);
  2429. if (mgp == NULL)
  2430. return -EINVAL;
  2431. netdev = mgp->dev;
  2432. netif_device_detach(netdev);
  2433. if (netif_running(netdev)) {
  2434. printk(KERN_INFO "myri10ge: closing %s\n", netdev->name);
  2435. rtnl_lock();
  2436. myri10ge_close(netdev);
  2437. rtnl_unlock();
  2438. }
  2439. myri10ge_dummy_rdma(mgp, 0);
  2440. pci_save_state(pdev);
  2441. pci_disable_device(pdev);
  2442. return pci_set_power_state(pdev, pci_choose_state(pdev, state));
  2443. }
  2444. static int myri10ge_resume(struct pci_dev *pdev)
  2445. {
  2446. struct myri10ge_priv *mgp;
  2447. struct net_device *netdev;
  2448. int status;
  2449. u16 vendor;
  2450. mgp = pci_get_drvdata(pdev);
  2451. if (mgp == NULL)
  2452. return -EINVAL;
  2453. netdev = mgp->dev;
  2454. pci_set_power_state(pdev, 0); /* zeros conf space as a side effect */
  2455. msleep(5); /* give card time to respond */
  2456. pci_read_config_word(mgp->pdev, PCI_VENDOR_ID, &vendor);
  2457. if (vendor == 0xffff) {
  2458. printk(KERN_ERR "myri10ge: %s: device disappeared!\n",
  2459. mgp->dev->name);
  2460. return -EIO;
  2461. }
  2462. status = pci_restore_state(pdev);
  2463. if (status)
  2464. return status;
  2465. status = pci_enable_device(pdev);
  2466. if (status) {
  2467. dev_err(&pdev->dev, "failed to enable device\n");
  2468. return status;
  2469. }
  2470. pci_set_master(pdev);
  2471. myri10ge_reset(mgp);
  2472. myri10ge_dummy_rdma(mgp, 1);
  2473. /* Save configuration space to be restored if the
  2474. * nic resets due to a parity error */
  2475. pci_save_state(pdev);
  2476. if (netif_running(netdev)) {
  2477. rtnl_lock();
  2478. status = myri10ge_open(netdev);
  2479. rtnl_unlock();
  2480. if (status != 0)
  2481. goto abort_with_enabled;
  2482. }
  2483. netif_device_attach(netdev);
  2484. return 0;
  2485. abort_with_enabled:
  2486. pci_disable_device(pdev);
  2487. return -EIO;
  2488. }
  2489. #endif /* CONFIG_PM */
  2490. static u32 myri10ge_read_reboot(struct myri10ge_priv *mgp)
  2491. {
  2492. struct pci_dev *pdev = mgp->pdev;
  2493. int vs = mgp->vendor_specific_offset;
  2494. u32 reboot;
  2495. /*enter read32 mode */
  2496. pci_write_config_byte(pdev, vs + 0x10, 0x3);
  2497. /*read REBOOT_STATUS (0xfffffff0) */
  2498. pci_write_config_dword(pdev, vs + 0x18, 0xfffffff0);
  2499. pci_read_config_dword(pdev, vs + 0x14, &reboot);
  2500. return reboot;
  2501. }
  2502. /*
  2503. * This watchdog is used to check whether the board has suffered
  2504. * from a parity error and needs to be recovered.
  2505. */
  2506. static void myri10ge_watchdog(struct work_struct *work)
  2507. {
  2508. struct myri10ge_priv *mgp =
  2509. container_of(work, struct myri10ge_priv, watchdog_work);
  2510. u32 reboot;
  2511. int status;
  2512. u16 cmd, vendor;
  2513. mgp->watchdog_resets++;
  2514. pci_read_config_word(mgp->pdev, PCI_COMMAND, &cmd);
  2515. if ((cmd & PCI_COMMAND_MASTER) == 0) {
  2516. /* Bus master DMA disabled? Check to see
  2517. * if the card rebooted due to a parity error
  2518. * For now, just report it */
  2519. reboot = myri10ge_read_reboot(mgp);
  2520. printk(KERN_ERR
  2521. "myri10ge: %s: NIC rebooted (0x%x),%s resetting\n",
  2522. mgp->dev->name, reboot,
  2523. myri10ge_reset_recover ? " " : " not");
  2524. if (myri10ge_reset_recover == 0)
  2525. return;
  2526. myri10ge_reset_recover--;
  2527. /*
  2528. * A rebooted nic will come back with config space as
  2529. * it was after power was applied to PCIe bus.
  2530. * Attempt to restore config space which was saved
  2531. * when the driver was loaded, or the last time the
  2532. * nic was resumed from power saving mode.
  2533. */
  2534. pci_restore_state(mgp->pdev);
  2535. /* save state again for accounting reasons */
  2536. pci_save_state(mgp->pdev);
  2537. } else {
  2538. /* if we get back -1's from our slot, perhaps somebody
  2539. * powered off our card. Don't try to reset it in
  2540. * this case */
  2541. if (cmd == 0xffff) {
  2542. pci_read_config_word(mgp->pdev, PCI_VENDOR_ID, &vendor);
  2543. if (vendor == 0xffff) {
  2544. printk(KERN_ERR
  2545. "myri10ge: %s: device disappeared!\n",
  2546. mgp->dev->name);
  2547. return;
  2548. }
  2549. }
  2550. /* Perhaps it is a software error. Try to reset */
  2551. printk(KERN_ERR "myri10ge: %s: device timeout, resetting\n",
  2552. mgp->dev->name);
  2553. printk(KERN_INFO "myri10ge: %s: %d %d %d %d %d\n",
  2554. mgp->dev->name, mgp->tx.req, mgp->tx.done,
  2555. mgp->tx.pkt_start, mgp->tx.pkt_done,
  2556. (int)ntohl(mgp->fw_stats->send_done_count));
  2557. msleep(2000);
  2558. printk(KERN_INFO "myri10ge: %s: %d %d %d %d %d\n",
  2559. mgp->dev->name, mgp->tx.req, mgp->tx.done,
  2560. mgp->tx.pkt_start, mgp->tx.pkt_done,
  2561. (int)ntohl(mgp->fw_stats->send_done_count));
  2562. }
  2563. rtnl_lock();
  2564. myri10ge_close(mgp->dev);
  2565. status = myri10ge_load_firmware(mgp);
  2566. if (status != 0)
  2567. printk(KERN_ERR "myri10ge: %s: failed to load firmware\n",
  2568. mgp->dev->name);
  2569. else
  2570. myri10ge_open(mgp->dev);
  2571. rtnl_unlock();
  2572. }
  2573. /*
  2574. * We use our own timer routine rather than relying upon
  2575. * netdev->tx_timeout because we have a very large hardware transmit
  2576. * queue. Due to the large queue, the netdev->tx_timeout function
  2577. * cannot detect a NIC with a parity error in a timely fashion if the
  2578. * NIC is lightly loaded.
  2579. */
  2580. static void myri10ge_watchdog_timer(unsigned long arg)
  2581. {
  2582. struct myri10ge_priv *mgp;
  2583. u32 rx_pause_cnt;
  2584. mgp = (struct myri10ge_priv *)arg;
  2585. if (mgp->rx_small.watchdog_needed) {
  2586. myri10ge_alloc_rx_pages(mgp, &mgp->rx_small,
  2587. mgp->small_bytes + MXGEFW_PAD, 1);
  2588. if (mgp->rx_small.fill_cnt - mgp->rx_small.cnt >=
  2589. myri10ge_fill_thresh)
  2590. mgp->rx_small.watchdog_needed = 0;
  2591. }
  2592. if (mgp->rx_big.watchdog_needed) {
  2593. myri10ge_alloc_rx_pages(mgp, &mgp->rx_big, mgp->big_bytes, 1);
  2594. if (mgp->rx_big.fill_cnt - mgp->rx_big.cnt >=
  2595. myri10ge_fill_thresh)
  2596. mgp->rx_big.watchdog_needed = 0;
  2597. }
  2598. rx_pause_cnt = ntohl(mgp->fw_stats->dropped_pause);
  2599. if (mgp->tx.req != mgp->tx.done &&
  2600. mgp->tx.done == mgp->watchdog_tx_done &&
  2601. mgp->watchdog_tx_req != mgp->watchdog_tx_done) {
  2602. /* nic seems like it might be stuck.. */
  2603. if (rx_pause_cnt != mgp->watchdog_pause) {
  2604. if (net_ratelimit())
  2605. printk(KERN_WARNING "myri10ge %s:"
  2606. "TX paused, check link partner\n",
  2607. mgp->dev->name);
  2608. } else {
  2609. schedule_work(&mgp->watchdog_work);
  2610. return;
  2611. }
  2612. }
  2613. /* rearm timer */
  2614. mod_timer(&mgp->watchdog_timer,
  2615. jiffies + myri10ge_watchdog_timeout * HZ);
  2616. mgp->watchdog_tx_done = mgp->tx.done;
  2617. mgp->watchdog_tx_req = mgp->tx.req;
  2618. mgp->watchdog_pause = rx_pause_cnt;
  2619. }
  2620. static int myri10ge_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
  2621. {
  2622. struct net_device *netdev;
  2623. struct myri10ge_priv *mgp;
  2624. struct device *dev = &pdev->dev;
  2625. size_t bytes;
  2626. int i;
  2627. int status = -ENXIO;
  2628. int dac_enabled;
  2629. netdev = alloc_etherdev(sizeof(*mgp));
  2630. if (netdev == NULL) {
  2631. dev_err(dev, "Could not allocate ethernet device\n");
  2632. return -ENOMEM;
  2633. }
  2634. SET_NETDEV_DEV(netdev, &pdev->dev);
  2635. mgp = netdev_priv(netdev);
  2636. mgp->dev = netdev;
  2637. netif_napi_add(netdev, &mgp->napi, myri10ge_poll, myri10ge_napi_weight);
  2638. mgp->pdev = pdev;
  2639. mgp->csum_flag = MXGEFW_FLAGS_CKSUM;
  2640. mgp->pause = myri10ge_flow_control;
  2641. mgp->intr_coal_delay = myri10ge_intr_coal_delay;
  2642. mgp->msg_enable = netif_msg_init(myri10ge_debug, MYRI10GE_MSG_DEFAULT);
  2643. init_waitqueue_head(&mgp->down_wq);
  2644. if (pci_enable_device(pdev)) {
  2645. dev_err(&pdev->dev, "pci_enable_device call failed\n");
  2646. status = -ENODEV;
  2647. goto abort_with_netdev;
  2648. }
  2649. /* Find the vendor-specific cap so we can check
  2650. * the reboot register later on */
  2651. mgp->vendor_specific_offset
  2652. = pci_find_capability(pdev, PCI_CAP_ID_VNDR);
  2653. /* Set our max read request to 4KB */
  2654. status = pcie_set_readrq(pdev, 4096);
  2655. if (status != 0) {
  2656. dev_err(&pdev->dev, "Error %d writing PCI_EXP_DEVCTL\n",
  2657. status);
  2658. goto abort_with_netdev;
  2659. }
  2660. pci_set_master(pdev);
  2661. dac_enabled = 1;
  2662. status = pci_set_dma_mask(pdev, DMA_64BIT_MASK);
  2663. if (status != 0) {
  2664. dac_enabled = 0;
  2665. dev_err(&pdev->dev,
  2666. "64-bit pci address mask was refused, "
  2667. "trying 32-bit\n");
  2668. status = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
  2669. }
  2670. if (status != 0) {
  2671. dev_err(&pdev->dev, "Error %d setting DMA mask\n", status);
  2672. goto abort_with_netdev;
  2673. }
  2674. mgp->cmd = dma_alloc_coherent(&pdev->dev, sizeof(*mgp->cmd),
  2675. &mgp->cmd_bus, GFP_KERNEL);
  2676. if (mgp->cmd == NULL)
  2677. goto abort_with_netdev;
  2678. mgp->fw_stats = dma_alloc_coherent(&pdev->dev, sizeof(*mgp->fw_stats),
  2679. &mgp->fw_stats_bus, GFP_KERNEL);
  2680. if (mgp->fw_stats == NULL)
  2681. goto abort_with_cmd;
  2682. mgp->board_span = pci_resource_len(pdev, 0);
  2683. mgp->iomem_base = pci_resource_start(pdev, 0);
  2684. mgp->mtrr = -1;
  2685. mgp->wc_enabled = 0;
  2686. #ifdef CONFIG_MTRR
  2687. mgp->mtrr = mtrr_add(mgp->iomem_base, mgp->board_span,
  2688. MTRR_TYPE_WRCOMB, 1);
  2689. if (mgp->mtrr >= 0)
  2690. mgp->wc_enabled = 1;
  2691. #endif
  2692. /* Hack. need to get rid of these magic numbers */
  2693. mgp->sram_size =
  2694. 2 * 1024 * 1024 - (2 * (48 * 1024) + (32 * 1024)) - 0x100;
  2695. if (mgp->sram_size > mgp->board_span) {
  2696. dev_err(&pdev->dev, "board span %ld bytes too small\n",
  2697. mgp->board_span);
  2698. goto abort_with_wc;
  2699. }
  2700. mgp->sram = ioremap(mgp->iomem_base, mgp->board_span);
  2701. if (mgp->sram == NULL) {
  2702. dev_err(&pdev->dev, "ioremap failed for %ld bytes at 0x%lx\n",
  2703. mgp->board_span, mgp->iomem_base);
  2704. status = -ENXIO;
  2705. goto abort_with_wc;
  2706. }
  2707. memcpy_fromio(mgp->eeprom_strings,
  2708. mgp->sram + mgp->sram_size - MYRI10GE_EEPROM_STRINGS_SIZE,
  2709. MYRI10GE_EEPROM_STRINGS_SIZE);
  2710. memset(mgp->eeprom_strings + MYRI10GE_EEPROM_STRINGS_SIZE - 2, 0, 2);
  2711. status = myri10ge_read_mac_addr(mgp);
  2712. if (status)
  2713. goto abort_with_ioremap;
  2714. for (i = 0; i < ETH_ALEN; i++)
  2715. netdev->dev_addr[i] = mgp->mac_addr[i];
  2716. /* allocate rx done ring */
  2717. bytes = myri10ge_max_intr_slots * sizeof(*mgp->rx_done.entry);
  2718. mgp->rx_done.entry = dma_alloc_coherent(&pdev->dev, bytes,
  2719. &mgp->rx_done.bus, GFP_KERNEL);
  2720. if (mgp->rx_done.entry == NULL)
  2721. goto abort_with_ioremap;
  2722. memset(mgp->rx_done.entry, 0, bytes);
  2723. myri10ge_select_firmware(mgp);
  2724. status = myri10ge_load_firmware(mgp);
  2725. if (status != 0) {
  2726. dev_err(&pdev->dev, "failed to load firmware\n");
  2727. goto abort_with_rx_done;
  2728. }
  2729. status = myri10ge_reset(mgp);
  2730. if (status != 0) {
  2731. dev_err(&pdev->dev, "failed reset\n");
  2732. goto abort_with_firmware;
  2733. }
  2734. pci_set_drvdata(pdev, mgp);
  2735. if ((myri10ge_initial_mtu + ETH_HLEN) > MYRI10GE_MAX_ETHER_MTU)
  2736. myri10ge_initial_mtu = MYRI10GE_MAX_ETHER_MTU - ETH_HLEN;
  2737. if ((myri10ge_initial_mtu + ETH_HLEN) < 68)
  2738. myri10ge_initial_mtu = 68;
  2739. netdev->mtu = myri10ge_initial_mtu;
  2740. netdev->open = myri10ge_open;
  2741. netdev->stop = myri10ge_close;
  2742. netdev->hard_start_xmit = myri10ge_xmit;
  2743. netdev->get_stats = myri10ge_get_stats;
  2744. netdev->base_addr = mgp->iomem_base;
  2745. netdev->change_mtu = myri10ge_change_mtu;
  2746. netdev->set_multicast_list = myri10ge_set_multicast_list;
  2747. netdev->set_mac_address = myri10ge_set_mac_address;
  2748. netdev->features = mgp->features;
  2749. if (dac_enabled)
  2750. netdev->features |= NETIF_F_HIGHDMA;
  2751. /* make sure we can get an irq, and that MSI can be
  2752. * setup (if available). Also ensure netdev->irq
  2753. * is set to correct value if MSI is enabled */
  2754. status = myri10ge_request_irq(mgp);
  2755. if (status != 0)
  2756. goto abort_with_firmware;
  2757. netdev->irq = pdev->irq;
  2758. myri10ge_free_irq(mgp);
  2759. /* Save configuration space to be restored if the
  2760. * nic resets due to a parity error */
  2761. pci_save_state(pdev);
  2762. /* Setup the watchdog timer */
  2763. setup_timer(&mgp->watchdog_timer, myri10ge_watchdog_timer,
  2764. (unsigned long)mgp);
  2765. SET_ETHTOOL_OPS(netdev, &myri10ge_ethtool_ops);
  2766. INIT_WORK(&mgp->watchdog_work, myri10ge_watchdog);
  2767. status = register_netdev(netdev);
  2768. if (status != 0) {
  2769. dev_err(&pdev->dev, "register_netdev failed: %d\n", status);
  2770. goto abort_with_state;
  2771. }
  2772. dev_info(dev, "%s IRQ %d, tx bndry %d, fw %s, WC %s\n",
  2773. (mgp->msi_enabled ? "MSI" : "xPIC"),
  2774. netdev->irq, mgp->tx.boundary, mgp->fw_name,
  2775. (mgp->wc_enabled ? "Enabled" : "Disabled"));
  2776. return 0;
  2777. abort_with_state:
  2778. pci_restore_state(pdev);
  2779. abort_with_firmware:
  2780. myri10ge_dummy_rdma(mgp, 0);
  2781. abort_with_rx_done:
  2782. bytes = myri10ge_max_intr_slots * sizeof(*mgp->rx_done.entry);
  2783. dma_free_coherent(&pdev->dev, bytes,
  2784. mgp->rx_done.entry, mgp->rx_done.bus);
  2785. abort_with_ioremap:
  2786. iounmap(mgp->sram);
  2787. abort_with_wc:
  2788. #ifdef CONFIG_MTRR
  2789. if (mgp->mtrr >= 0)
  2790. mtrr_del(mgp->mtrr, mgp->iomem_base, mgp->board_span);
  2791. #endif
  2792. dma_free_coherent(&pdev->dev, sizeof(*mgp->fw_stats),
  2793. mgp->fw_stats, mgp->fw_stats_bus);
  2794. abort_with_cmd:
  2795. dma_free_coherent(&pdev->dev, sizeof(*mgp->cmd),
  2796. mgp->cmd, mgp->cmd_bus);
  2797. abort_with_netdev:
  2798. free_netdev(netdev);
  2799. return status;
  2800. }
  2801. /*
  2802. * myri10ge_remove
  2803. *
  2804. * Does what is necessary to shutdown one Myrinet device. Called
  2805. * once for each Myrinet card by the kernel when a module is
  2806. * unloaded.
  2807. */
  2808. static void myri10ge_remove(struct pci_dev *pdev)
  2809. {
  2810. struct myri10ge_priv *mgp;
  2811. struct net_device *netdev;
  2812. size_t bytes;
  2813. mgp = pci_get_drvdata(pdev);
  2814. if (mgp == NULL)
  2815. return;
  2816. flush_scheduled_work();
  2817. netdev = mgp->dev;
  2818. unregister_netdev(netdev);
  2819. myri10ge_dummy_rdma(mgp, 0);
  2820. /* avoid a memory leak */
  2821. pci_restore_state(pdev);
  2822. bytes = myri10ge_max_intr_slots * sizeof(*mgp->rx_done.entry);
  2823. dma_free_coherent(&pdev->dev, bytes,
  2824. mgp->rx_done.entry, mgp->rx_done.bus);
  2825. iounmap(mgp->sram);
  2826. #ifdef CONFIG_MTRR
  2827. if (mgp->mtrr >= 0)
  2828. mtrr_del(mgp->mtrr, mgp->iomem_base, mgp->board_span);
  2829. #endif
  2830. dma_free_coherent(&pdev->dev, sizeof(*mgp->fw_stats),
  2831. mgp->fw_stats, mgp->fw_stats_bus);
  2832. dma_free_coherent(&pdev->dev, sizeof(*mgp->cmd),
  2833. mgp->cmd, mgp->cmd_bus);
  2834. free_netdev(netdev);
  2835. pci_set_drvdata(pdev, NULL);
  2836. }
  2837. #define PCI_DEVICE_ID_MYRICOM_MYRI10GE_Z8E 0x0008
  2838. #define PCI_DEVICE_ID_MYRICOM_MYRI10GE_Z8E_9 0x0009
  2839. static struct pci_device_id myri10ge_pci_tbl[] = {
  2840. {PCI_DEVICE(PCI_VENDOR_ID_MYRICOM, PCI_DEVICE_ID_MYRICOM_MYRI10GE_Z8E)},
  2841. {PCI_DEVICE
  2842. (PCI_VENDOR_ID_MYRICOM, PCI_DEVICE_ID_MYRICOM_MYRI10GE_Z8E_9)},
  2843. {0},
  2844. };
  2845. static struct pci_driver myri10ge_driver = {
  2846. .name = "myri10ge",
  2847. .probe = myri10ge_probe,
  2848. .remove = myri10ge_remove,
  2849. .id_table = myri10ge_pci_tbl,
  2850. #ifdef CONFIG_PM
  2851. .suspend = myri10ge_suspend,
  2852. .resume = myri10ge_resume,
  2853. #endif
  2854. };
  2855. static __init int myri10ge_init_module(void)
  2856. {
  2857. printk(KERN_INFO "%s: Version %s\n", myri10ge_driver.name,
  2858. MYRI10GE_VERSION_STR);
  2859. return pci_register_driver(&myri10ge_driver);
  2860. }
  2861. module_init(myri10ge_init_module);
  2862. static __exit void myri10ge_cleanup_module(void)
  2863. {
  2864. pci_unregister_driver(&myri10ge_driver);
  2865. }
  2866. module_exit(myri10ge_cleanup_module);