myri10ge.c 85 KB

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