myri10ge.c 84 KB

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