myri10ge.c 87 KB

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