myri10ge.c 80 KB

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