myri10ge.c 87 KB

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