myri10ge.c 87 KB

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