myri10ge.c 92 KB

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