myri10ge.c 90 KB

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