myri10ge.c 87 KB

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