myri10ge.c 90 KB

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