myri10ge.c 87 KB

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