myri10ge.c 85 KB

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