myri10ge.c 114 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169
  1. /*************************************************************************
  2. * myri10ge.c: Myricom Myri-10G Ethernet driver.
  3. *
  4. * Copyright (C) 2005 - 2009 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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  41. #include <linux/tcp.h>
  42. #include <linux/netdevice.h>
  43. #include <linux/skbuff.h>
  44. #include <linux/string.h>
  45. #include <linux/module.h>
  46. #include <linux/pci.h>
  47. #include <linux/dma-mapping.h>
  48. #include <linux/etherdevice.h>
  49. #include <linux/if_ether.h>
  50. #include <linux/if_vlan.h>
  51. #include <linux/inet_lro.h>
  52. #include <linux/dca.h>
  53. #include <linux/ip.h>
  54. #include <linux/inet.h>
  55. #include <linux/in.h>
  56. #include <linux/ethtool.h>
  57. #include <linux/firmware.h>
  58. #include <linux/delay.h>
  59. #include <linux/timer.h>
  60. #include <linux/vmalloc.h>
  61. #include <linux/crc32.h>
  62. #include <linux/moduleparam.h>
  63. #include <linux/io.h>
  64. #include <linux/log2.h>
  65. #include <linux/slab.h>
  66. #include <net/checksum.h>
  67. #include <net/ip.h>
  68. #include <net/tcp.h>
  69. #include <asm/byteorder.h>
  70. #include <asm/io.h>
  71. #include <asm/processor.h>
  72. #ifdef CONFIG_MTRR
  73. #include <asm/mtrr.h>
  74. #endif
  75. #include "myri10ge_mcp.h"
  76. #include "myri10ge_mcp_gen_header.h"
  77. #define MYRI10GE_VERSION_STR "1.5.2-1.459"
  78. MODULE_DESCRIPTION("Myricom 10G driver (10GbE)");
  79. MODULE_AUTHOR("Maintainer: help@myri.com");
  80. MODULE_VERSION(MYRI10GE_VERSION_STR);
  81. MODULE_LICENSE("Dual BSD/GPL");
  82. #define MYRI10GE_MAX_ETHER_MTU 9014
  83. #define MYRI10GE_ETH_STOPPED 0
  84. #define MYRI10GE_ETH_STOPPING 1
  85. #define MYRI10GE_ETH_STARTING 2
  86. #define MYRI10GE_ETH_RUNNING 3
  87. #define MYRI10GE_ETH_OPEN_FAILED 4
  88. #define MYRI10GE_EEPROM_STRINGS_SIZE 256
  89. #define MYRI10GE_MAX_SEND_DESC_TSO ((65536 / 2048) * 2)
  90. #define MYRI10GE_MAX_LRO_DESCRIPTORS 8
  91. #define MYRI10GE_LRO_MAX_PKTS 64
  92. #define MYRI10GE_NO_CONFIRM_DATA htonl(0xffffffff)
  93. #define MYRI10GE_NO_RESPONSE_RESULT 0xffffffff
  94. #define MYRI10GE_ALLOC_ORDER 0
  95. #define MYRI10GE_ALLOC_SIZE ((1 << MYRI10GE_ALLOC_ORDER) * PAGE_SIZE)
  96. #define MYRI10GE_MAX_FRAGS_PER_FRAME (MYRI10GE_MAX_ETHER_MTU/MYRI10GE_ALLOC_SIZE + 1)
  97. #define MYRI10GE_MAX_SLICES 32
  98. struct myri10ge_rx_buffer_state {
  99. struct page *page;
  100. int page_offset;
  101. DEFINE_DMA_UNMAP_ADDR(bus);
  102. DEFINE_DMA_UNMAP_LEN(len);
  103. };
  104. struct myri10ge_tx_buffer_state {
  105. struct sk_buff *skb;
  106. int last;
  107. DEFINE_DMA_UNMAP_ADDR(bus);
  108. DEFINE_DMA_UNMAP_LEN(len);
  109. };
  110. struct myri10ge_cmd {
  111. u32 data0;
  112. u32 data1;
  113. u32 data2;
  114. };
  115. struct myri10ge_rx_buf {
  116. struct mcp_kreq_ether_recv __iomem *lanai; /* lanai ptr for recv ring */
  117. struct mcp_kreq_ether_recv *shadow; /* host shadow of recv ring */
  118. struct myri10ge_rx_buffer_state *info;
  119. struct page *page;
  120. dma_addr_t bus;
  121. int page_offset;
  122. int cnt;
  123. int fill_cnt;
  124. int alloc_fail;
  125. int mask; /* number of rx slots -1 */
  126. int watchdog_needed;
  127. };
  128. struct myri10ge_tx_buf {
  129. struct mcp_kreq_ether_send __iomem *lanai; /* lanai ptr for sendq */
  130. __be32 __iomem *send_go; /* "go" doorbell ptr */
  131. __be32 __iomem *send_stop; /* "stop" doorbell ptr */
  132. struct mcp_kreq_ether_send *req_list; /* host shadow of sendq */
  133. char *req_bytes;
  134. struct myri10ge_tx_buffer_state *info;
  135. int mask; /* number of transmit slots -1 */
  136. int req ____cacheline_aligned; /* transmit slots submitted */
  137. int pkt_start; /* packets started */
  138. int stop_queue;
  139. int linearized;
  140. int done ____cacheline_aligned; /* transmit slots completed */
  141. int pkt_done; /* packets completed */
  142. int wake_queue;
  143. int queue_active;
  144. };
  145. struct myri10ge_rx_done {
  146. struct mcp_slot *entry;
  147. dma_addr_t bus;
  148. int cnt;
  149. int idx;
  150. struct net_lro_mgr lro_mgr;
  151. struct net_lro_desc lro_desc[MYRI10GE_MAX_LRO_DESCRIPTORS];
  152. };
  153. struct myri10ge_slice_netstats {
  154. unsigned long rx_packets;
  155. unsigned long tx_packets;
  156. unsigned long rx_bytes;
  157. unsigned long tx_bytes;
  158. unsigned long rx_dropped;
  159. unsigned long tx_dropped;
  160. };
  161. struct myri10ge_slice_state {
  162. struct myri10ge_tx_buf tx; /* transmit ring */
  163. struct myri10ge_rx_buf rx_small;
  164. struct myri10ge_rx_buf rx_big;
  165. struct myri10ge_rx_done rx_done;
  166. struct net_device *dev;
  167. struct napi_struct napi;
  168. struct myri10ge_priv *mgp;
  169. struct myri10ge_slice_netstats stats;
  170. __be32 __iomem *irq_claim;
  171. struct mcp_irq_data *fw_stats;
  172. dma_addr_t fw_stats_bus;
  173. int watchdog_tx_done;
  174. int watchdog_tx_req;
  175. int watchdog_rx_done;
  176. #ifdef CONFIG_MYRI10GE_DCA
  177. int cached_dca_tag;
  178. int cpu;
  179. __be32 __iomem *dca_tag;
  180. #endif
  181. char irq_desc[32];
  182. };
  183. struct myri10ge_priv {
  184. struct myri10ge_slice_state *ss;
  185. int tx_boundary; /* boundary transmits cannot cross */
  186. int num_slices;
  187. int running; /* running? */
  188. int csum_flag; /* rx_csums? */
  189. int small_bytes;
  190. int big_bytes;
  191. int max_intr_slots;
  192. struct net_device *dev;
  193. spinlock_t stats_lock;
  194. u8 __iomem *sram;
  195. int sram_size;
  196. unsigned long board_span;
  197. unsigned long iomem_base;
  198. __be32 __iomem *irq_deassert;
  199. char *mac_addr_string;
  200. struct mcp_cmd_response *cmd;
  201. dma_addr_t cmd_bus;
  202. struct pci_dev *pdev;
  203. int msi_enabled;
  204. int msix_enabled;
  205. struct msix_entry *msix_vectors;
  206. #ifdef CONFIG_MYRI10GE_DCA
  207. int dca_enabled;
  208. int relaxed_order;
  209. #endif
  210. u32 link_state;
  211. unsigned int rdma_tags_available;
  212. int intr_coal_delay;
  213. __be32 __iomem *intr_coal_delay_ptr;
  214. int mtrr;
  215. int wc_enabled;
  216. int down_cnt;
  217. wait_queue_head_t down_wq;
  218. struct work_struct watchdog_work;
  219. struct timer_list watchdog_timer;
  220. int watchdog_resets;
  221. int watchdog_pause;
  222. int pause;
  223. bool fw_name_allocated;
  224. char *fw_name;
  225. char eeprom_strings[MYRI10GE_EEPROM_STRINGS_SIZE];
  226. char *product_code_string;
  227. char fw_version[128];
  228. int fw_ver_major;
  229. int fw_ver_minor;
  230. int fw_ver_tiny;
  231. int adopted_rx_filter_bug;
  232. u8 mac_addr[6]; /* eeprom mac address */
  233. unsigned long serial_number;
  234. int vendor_specific_offset;
  235. int fw_multicast_support;
  236. u32 features;
  237. u32 max_tso6;
  238. u32 read_dma;
  239. u32 write_dma;
  240. u32 read_write_dma;
  241. u32 link_changes;
  242. u32 msg_enable;
  243. unsigned int board_number;
  244. int rebooted;
  245. };
  246. static char *myri10ge_fw_unaligned = "myri10ge_ethp_z8e.dat";
  247. static char *myri10ge_fw_aligned = "myri10ge_eth_z8e.dat";
  248. static char *myri10ge_fw_rss_unaligned = "myri10ge_rss_ethp_z8e.dat";
  249. static char *myri10ge_fw_rss_aligned = "myri10ge_rss_eth_z8e.dat";
  250. MODULE_FIRMWARE("myri10ge_ethp_z8e.dat");
  251. MODULE_FIRMWARE("myri10ge_eth_z8e.dat");
  252. MODULE_FIRMWARE("myri10ge_rss_ethp_z8e.dat");
  253. MODULE_FIRMWARE("myri10ge_rss_eth_z8e.dat");
  254. /* Careful: must be accessed under kparam_block_sysfs_write */
  255. static char *myri10ge_fw_name = NULL;
  256. module_param(myri10ge_fw_name, charp, S_IRUGO | S_IWUSR);
  257. MODULE_PARM_DESC(myri10ge_fw_name, "Firmware image name");
  258. #define MYRI10GE_MAX_BOARDS 8
  259. static char *myri10ge_fw_names[MYRI10GE_MAX_BOARDS] =
  260. {[0 ... (MYRI10GE_MAX_BOARDS - 1)] = NULL };
  261. module_param_array_named(myri10ge_fw_names, myri10ge_fw_names, charp, NULL,
  262. 0444);
  263. MODULE_PARM_DESC(myri10ge_fw_name, "Firmware image names per board");
  264. static int myri10ge_ecrc_enable = 1;
  265. module_param(myri10ge_ecrc_enable, int, S_IRUGO);
  266. MODULE_PARM_DESC(myri10ge_ecrc_enable, "Enable Extended CRC on PCI-E");
  267. static int myri10ge_small_bytes = -1; /* -1 == auto */
  268. module_param(myri10ge_small_bytes, int, S_IRUGO | S_IWUSR);
  269. MODULE_PARM_DESC(myri10ge_small_bytes, "Threshold of small packets");
  270. static int myri10ge_msi = 1; /* enable msi by default */
  271. module_param(myri10ge_msi, int, S_IRUGO | S_IWUSR);
  272. MODULE_PARM_DESC(myri10ge_msi, "Enable Message Signalled Interrupts");
  273. static int myri10ge_intr_coal_delay = 75;
  274. module_param(myri10ge_intr_coal_delay, int, S_IRUGO);
  275. MODULE_PARM_DESC(myri10ge_intr_coal_delay, "Interrupt coalescing delay");
  276. static int myri10ge_flow_control = 1;
  277. module_param(myri10ge_flow_control, int, S_IRUGO);
  278. MODULE_PARM_DESC(myri10ge_flow_control, "Pause parameter");
  279. static int myri10ge_deassert_wait = 1;
  280. module_param(myri10ge_deassert_wait, int, S_IRUGO | S_IWUSR);
  281. MODULE_PARM_DESC(myri10ge_deassert_wait,
  282. "Wait when deasserting legacy interrupts");
  283. static int myri10ge_force_firmware = 0;
  284. module_param(myri10ge_force_firmware, int, S_IRUGO);
  285. MODULE_PARM_DESC(myri10ge_force_firmware,
  286. "Force firmware to assume aligned completions");
  287. static int myri10ge_initial_mtu = MYRI10GE_MAX_ETHER_MTU - ETH_HLEN;
  288. module_param(myri10ge_initial_mtu, int, S_IRUGO);
  289. MODULE_PARM_DESC(myri10ge_initial_mtu, "Initial MTU");
  290. static int myri10ge_napi_weight = 64;
  291. module_param(myri10ge_napi_weight, int, S_IRUGO);
  292. MODULE_PARM_DESC(myri10ge_napi_weight, "Set NAPI weight");
  293. static int myri10ge_watchdog_timeout = 1;
  294. module_param(myri10ge_watchdog_timeout, int, S_IRUGO);
  295. MODULE_PARM_DESC(myri10ge_watchdog_timeout, "Set watchdog timeout");
  296. static int myri10ge_max_irq_loops = 1048576;
  297. module_param(myri10ge_max_irq_loops, int, S_IRUGO);
  298. MODULE_PARM_DESC(myri10ge_max_irq_loops,
  299. "Set stuck legacy IRQ detection threshold");
  300. #define MYRI10GE_MSG_DEFAULT NETIF_MSG_LINK
  301. static int myri10ge_debug = -1; /* defaults above */
  302. module_param(myri10ge_debug, int, 0);
  303. MODULE_PARM_DESC(myri10ge_debug, "Debug level (0=none,...,16=all)");
  304. static int myri10ge_lro_max_pkts = MYRI10GE_LRO_MAX_PKTS;
  305. module_param(myri10ge_lro_max_pkts, int, S_IRUGO);
  306. MODULE_PARM_DESC(myri10ge_lro_max_pkts,
  307. "Number of LRO packets to be aggregated");
  308. static int myri10ge_fill_thresh = 256;
  309. module_param(myri10ge_fill_thresh, int, S_IRUGO | S_IWUSR);
  310. MODULE_PARM_DESC(myri10ge_fill_thresh, "Number of empty rx slots allowed");
  311. static int myri10ge_reset_recover = 1;
  312. static int myri10ge_max_slices = 1;
  313. module_param(myri10ge_max_slices, int, S_IRUGO);
  314. MODULE_PARM_DESC(myri10ge_max_slices, "Max tx/rx queues");
  315. static int myri10ge_rss_hash = MXGEFW_RSS_HASH_TYPE_SRC_DST_PORT;
  316. module_param(myri10ge_rss_hash, int, S_IRUGO);
  317. MODULE_PARM_DESC(myri10ge_rss_hash, "Type of RSS hashing to do");
  318. static int myri10ge_dca = 1;
  319. module_param(myri10ge_dca, int, S_IRUGO);
  320. MODULE_PARM_DESC(myri10ge_dca, "Enable DCA if possible");
  321. #define MYRI10GE_FW_OFFSET 1024*1024
  322. #define MYRI10GE_HIGHPART_TO_U32(X) \
  323. (sizeof (X) == 8) ? ((u32)((u64)(X) >> 32)) : (0)
  324. #define MYRI10GE_LOWPART_TO_U32(X) ((u32)(X))
  325. #define myri10ge_pio_copy(to,from,size) __iowrite64_copy(to,from,size/8)
  326. static void myri10ge_set_multicast_list(struct net_device *dev);
  327. static netdev_tx_t myri10ge_sw_tso(struct sk_buff *skb,
  328. struct net_device *dev);
  329. static inline void put_be32(__be32 val, __be32 __iomem * p)
  330. {
  331. __raw_writel((__force __u32) val, (__force void __iomem *)p);
  332. }
  333. static struct net_device_stats *myri10ge_get_stats(struct net_device *dev);
  334. static void set_fw_name(struct myri10ge_priv *mgp, char *name, bool allocated)
  335. {
  336. if (mgp->fw_name_allocated)
  337. kfree(mgp->fw_name);
  338. mgp->fw_name = name;
  339. mgp->fw_name_allocated = allocated;
  340. }
  341. static int
  342. myri10ge_send_cmd(struct myri10ge_priv *mgp, u32 cmd,
  343. struct myri10ge_cmd *data, int atomic)
  344. {
  345. struct mcp_cmd *buf;
  346. char buf_bytes[sizeof(*buf) + 8];
  347. struct mcp_cmd_response *response = mgp->cmd;
  348. char __iomem *cmd_addr = mgp->sram + MXGEFW_ETH_CMD;
  349. u32 dma_low, dma_high, result, value;
  350. int sleep_total = 0;
  351. /* ensure buf is aligned to 8 bytes */
  352. buf = (struct mcp_cmd *)ALIGN((unsigned long)buf_bytes, 8);
  353. buf->data0 = htonl(data->data0);
  354. buf->data1 = htonl(data->data1);
  355. buf->data2 = htonl(data->data2);
  356. buf->cmd = htonl(cmd);
  357. dma_low = MYRI10GE_LOWPART_TO_U32(mgp->cmd_bus);
  358. dma_high = MYRI10GE_HIGHPART_TO_U32(mgp->cmd_bus);
  359. buf->response_addr.low = htonl(dma_low);
  360. buf->response_addr.high = htonl(dma_high);
  361. response->result = htonl(MYRI10GE_NO_RESPONSE_RESULT);
  362. mb();
  363. myri10ge_pio_copy(cmd_addr, buf, sizeof(*buf));
  364. /* wait up to 15ms. Longest command is the DMA benchmark,
  365. * which is capped at 5ms, but runs from a timeout handler
  366. * that runs every 7.8ms. So a 15ms timeout leaves us with
  367. * a 2.2ms margin
  368. */
  369. if (atomic) {
  370. /* if atomic is set, do not sleep,
  371. * and try to get the completion quickly
  372. * (1ms will be enough for those commands) */
  373. for (sleep_total = 0;
  374. sleep_total < 1000 &&
  375. response->result == htonl(MYRI10GE_NO_RESPONSE_RESULT);
  376. sleep_total += 10) {
  377. udelay(10);
  378. mb();
  379. }
  380. } else {
  381. /* use msleep for most command */
  382. for (sleep_total = 0;
  383. sleep_total < 15 &&
  384. response->result == htonl(MYRI10GE_NO_RESPONSE_RESULT);
  385. sleep_total++)
  386. msleep(1);
  387. }
  388. result = ntohl(response->result);
  389. value = ntohl(response->data);
  390. if (result != MYRI10GE_NO_RESPONSE_RESULT) {
  391. if (result == 0) {
  392. data->data0 = value;
  393. return 0;
  394. } else if (result == MXGEFW_CMD_UNKNOWN) {
  395. return -ENOSYS;
  396. } else if (result == MXGEFW_CMD_ERROR_UNALIGNED) {
  397. return -E2BIG;
  398. } else if (result == MXGEFW_CMD_ERROR_RANGE &&
  399. cmd == MXGEFW_CMD_ENABLE_RSS_QUEUES &&
  400. (data->
  401. data1 & MXGEFW_SLICE_ENABLE_MULTIPLE_TX_QUEUES) !=
  402. 0) {
  403. return -ERANGE;
  404. } else {
  405. dev_err(&mgp->pdev->dev,
  406. "command %d failed, result = %d\n",
  407. cmd, result);
  408. return -ENXIO;
  409. }
  410. }
  411. dev_err(&mgp->pdev->dev, "command %d timed out, result = %d\n",
  412. cmd, result);
  413. return -EAGAIN;
  414. }
  415. /*
  416. * The eeprom strings on the lanaiX have the format
  417. * SN=x\0
  418. * MAC=x:x:x:x:x:x\0
  419. * PT:ddd mmm xx xx:xx:xx xx\0
  420. * PV:ddd mmm xx xx:xx:xx xx\0
  421. */
  422. static int myri10ge_read_mac_addr(struct myri10ge_priv *mgp)
  423. {
  424. char *ptr, *limit;
  425. int i;
  426. ptr = mgp->eeprom_strings;
  427. limit = mgp->eeprom_strings + MYRI10GE_EEPROM_STRINGS_SIZE;
  428. while (*ptr != '\0' && ptr < limit) {
  429. if (memcmp(ptr, "MAC=", 4) == 0) {
  430. ptr += 4;
  431. mgp->mac_addr_string = ptr;
  432. for (i = 0; i < 6; i++) {
  433. if ((ptr + 2) > limit)
  434. goto abort;
  435. mgp->mac_addr[i] =
  436. simple_strtoul(ptr, &ptr, 16);
  437. ptr += 1;
  438. }
  439. }
  440. if (memcmp(ptr, "PC=", 3) == 0) {
  441. ptr += 3;
  442. mgp->product_code_string = ptr;
  443. }
  444. if (memcmp((const void *)ptr, "SN=", 3) == 0) {
  445. ptr += 3;
  446. mgp->serial_number = simple_strtoul(ptr, &ptr, 10);
  447. }
  448. while (ptr < limit && *ptr++) ;
  449. }
  450. return 0;
  451. abort:
  452. dev_err(&mgp->pdev->dev, "failed to parse eeprom_strings\n");
  453. return -ENXIO;
  454. }
  455. /*
  456. * Enable or disable periodic RDMAs from the host to make certain
  457. * chipsets resend dropped PCIe messages
  458. */
  459. static void myri10ge_dummy_rdma(struct myri10ge_priv *mgp, int enable)
  460. {
  461. char __iomem *submit;
  462. __be32 buf[16] __attribute__ ((__aligned__(8)));
  463. u32 dma_low, dma_high;
  464. int i;
  465. /* clear confirmation addr */
  466. mgp->cmd->data = 0;
  467. mb();
  468. /* send a rdma command to the PCIe engine, and wait for the
  469. * response in the confirmation address. The firmware should
  470. * write a -1 there to indicate it is alive and well
  471. */
  472. dma_low = MYRI10GE_LOWPART_TO_U32(mgp->cmd_bus);
  473. dma_high = MYRI10GE_HIGHPART_TO_U32(mgp->cmd_bus);
  474. buf[0] = htonl(dma_high); /* confirm addr MSW */
  475. buf[1] = htonl(dma_low); /* confirm addr LSW */
  476. buf[2] = MYRI10GE_NO_CONFIRM_DATA; /* confirm data */
  477. buf[3] = htonl(dma_high); /* dummy addr MSW */
  478. buf[4] = htonl(dma_low); /* dummy addr LSW */
  479. buf[5] = htonl(enable); /* enable? */
  480. submit = mgp->sram + MXGEFW_BOOT_DUMMY_RDMA;
  481. myri10ge_pio_copy(submit, &buf, sizeof(buf));
  482. for (i = 0; mgp->cmd->data != MYRI10GE_NO_CONFIRM_DATA && i < 20; i++)
  483. msleep(1);
  484. if (mgp->cmd->data != MYRI10GE_NO_CONFIRM_DATA)
  485. dev_err(&mgp->pdev->dev, "dummy rdma %s failed\n",
  486. (enable ? "enable" : "disable"));
  487. }
  488. static int
  489. myri10ge_validate_firmware(struct myri10ge_priv *mgp,
  490. struct mcp_gen_header *hdr)
  491. {
  492. struct device *dev = &mgp->pdev->dev;
  493. /* check firmware type */
  494. if (ntohl(hdr->mcp_type) != MCP_TYPE_ETH) {
  495. dev_err(dev, "Bad firmware type: 0x%x\n", ntohl(hdr->mcp_type));
  496. return -EINVAL;
  497. }
  498. /* save firmware version for ethtool */
  499. strncpy(mgp->fw_version, hdr->version, sizeof(mgp->fw_version));
  500. sscanf(mgp->fw_version, "%d.%d.%d", &mgp->fw_ver_major,
  501. &mgp->fw_ver_minor, &mgp->fw_ver_tiny);
  502. if (!(mgp->fw_ver_major == MXGEFW_VERSION_MAJOR &&
  503. mgp->fw_ver_minor == MXGEFW_VERSION_MINOR)) {
  504. dev_err(dev, "Found firmware version %s\n", mgp->fw_version);
  505. dev_err(dev, "Driver needs %d.%d\n", MXGEFW_VERSION_MAJOR,
  506. MXGEFW_VERSION_MINOR);
  507. return -EINVAL;
  508. }
  509. return 0;
  510. }
  511. static int myri10ge_load_hotplug_firmware(struct myri10ge_priv *mgp, u32 * size)
  512. {
  513. unsigned crc, reread_crc;
  514. const struct firmware *fw;
  515. struct device *dev = &mgp->pdev->dev;
  516. unsigned char *fw_readback;
  517. struct mcp_gen_header *hdr;
  518. size_t hdr_offset;
  519. int status;
  520. unsigned i;
  521. if ((status = request_firmware(&fw, mgp->fw_name, dev)) < 0) {
  522. dev_err(dev, "Unable to load %s firmware image via hotplug\n",
  523. mgp->fw_name);
  524. status = -EINVAL;
  525. goto abort_with_nothing;
  526. }
  527. /* check size */
  528. if (fw->size >= mgp->sram_size - MYRI10GE_FW_OFFSET ||
  529. fw->size < MCP_HEADER_PTR_OFFSET + 4) {
  530. dev_err(dev, "Firmware size invalid:%d\n", (int)fw->size);
  531. status = -EINVAL;
  532. goto abort_with_fw;
  533. }
  534. /* check id */
  535. hdr_offset = ntohl(*(__be32 *) (fw->data + MCP_HEADER_PTR_OFFSET));
  536. if ((hdr_offset & 3) || hdr_offset + sizeof(*hdr) > fw->size) {
  537. dev_err(dev, "Bad firmware file\n");
  538. status = -EINVAL;
  539. goto abort_with_fw;
  540. }
  541. hdr = (void *)(fw->data + hdr_offset);
  542. status = myri10ge_validate_firmware(mgp, hdr);
  543. if (status != 0)
  544. goto abort_with_fw;
  545. crc = crc32(~0, fw->data, fw->size);
  546. for (i = 0; i < fw->size; i += 256) {
  547. myri10ge_pio_copy(mgp->sram + MYRI10GE_FW_OFFSET + i,
  548. fw->data + i,
  549. min(256U, (unsigned)(fw->size - i)));
  550. mb();
  551. readb(mgp->sram);
  552. }
  553. fw_readback = vmalloc(fw->size);
  554. if (!fw_readback) {
  555. status = -ENOMEM;
  556. goto abort_with_fw;
  557. }
  558. /* corruption checking is good for parity recovery and buggy chipset */
  559. memcpy_fromio(fw_readback, mgp->sram + MYRI10GE_FW_OFFSET, fw->size);
  560. reread_crc = crc32(~0, fw_readback, fw->size);
  561. vfree(fw_readback);
  562. if (crc != reread_crc) {
  563. dev_err(dev, "CRC failed(fw-len=%u), got 0x%x (expect 0x%x)\n",
  564. (unsigned)fw->size, reread_crc, crc);
  565. status = -EIO;
  566. goto abort_with_fw;
  567. }
  568. *size = (u32) fw->size;
  569. abort_with_fw:
  570. release_firmware(fw);
  571. abort_with_nothing:
  572. return status;
  573. }
  574. static int myri10ge_adopt_running_firmware(struct myri10ge_priv *mgp)
  575. {
  576. struct mcp_gen_header *hdr;
  577. struct device *dev = &mgp->pdev->dev;
  578. const size_t bytes = sizeof(struct mcp_gen_header);
  579. size_t hdr_offset;
  580. int status;
  581. /* find running firmware header */
  582. hdr_offset = swab32(readl(mgp->sram + MCP_HEADER_PTR_OFFSET));
  583. if ((hdr_offset & 3) || hdr_offset + sizeof(*hdr) > mgp->sram_size) {
  584. dev_err(dev, "Running firmware has bad header offset (%d)\n",
  585. (int)hdr_offset);
  586. return -EIO;
  587. }
  588. /* copy header of running firmware from SRAM to host memory to
  589. * validate firmware */
  590. hdr = kmalloc(bytes, GFP_KERNEL);
  591. if (hdr == NULL) {
  592. dev_err(dev, "could not malloc firmware hdr\n");
  593. return -ENOMEM;
  594. }
  595. memcpy_fromio(hdr, mgp->sram + hdr_offset, bytes);
  596. status = myri10ge_validate_firmware(mgp, hdr);
  597. kfree(hdr);
  598. /* check to see if adopted firmware has bug where adopting
  599. * it will cause broadcasts to be filtered unless the NIC
  600. * is kept in ALLMULTI mode */
  601. if (mgp->fw_ver_major == 1 && mgp->fw_ver_minor == 4 &&
  602. mgp->fw_ver_tiny >= 4 && mgp->fw_ver_tiny <= 11) {
  603. mgp->adopted_rx_filter_bug = 1;
  604. dev_warn(dev, "Adopting fw %d.%d.%d: "
  605. "working around rx filter bug\n",
  606. mgp->fw_ver_major, mgp->fw_ver_minor,
  607. mgp->fw_ver_tiny);
  608. }
  609. return status;
  610. }
  611. static int myri10ge_get_firmware_capabilities(struct myri10ge_priv *mgp)
  612. {
  613. struct myri10ge_cmd cmd;
  614. int status;
  615. /* probe for IPv6 TSO support */
  616. mgp->features = NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_TSO;
  617. status = myri10ge_send_cmd(mgp, MXGEFW_CMD_GET_MAX_TSO6_HDR_SIZE,
  618. &cmd, 0);
  619. if (status == 0) {
  620. mgp->max_tso6 = cmd.data0;
  621. mgp->features |= NETIF_F_TSO6;
  622. }
  623. status = myri10ge_send_cmd(mgp, MXGEFW_CMD_GET_RX_RING_SIZE, &cmd, 0);
  624. if (status != 0) {
  625. dev_err(&mgp->pdev->dev,
  626. "failed MXGEFW_CMD_GET_RX_RING_SIZE\n");
  627. return -ENXIO;
  628. }
  629. mgp->max_intr_slots = 2 * (cmd.data0 / sizeof(struct mcp_dma_addr));
  630. return 0;
  631. }
  632. static int myri10ge_load_firmware(struct myri10ge_priv *mgp, int adopt)
  633. {
  634. char __iomem *submit;
  635. __be32 buf[16] __attribute__ ((__aligned__(8)));
  636. u32 dma_low, dma_high, size;
  637. int status, i;
  638. size = 0;
  639. status = myri10ge_load_hotplug_firmware(mgp, &size);
  640. if (status) {
  641. if (!adopt)
  642. return status;
  643. dev_warn(&mgp->pdev->dev, "hotplug firmware loading failed\n");
  644. /* Do not attempt to adopt firmware if there
  645. * was a bad crc */
  646. if (status == -EIO)
  647. return status;
  648. status = myri10ge_adopt_running_firmware(mgp);
  649. if (status != 0) {
  650. dev_err(&mgp->pdev->dev,
  651. "failed to adopt running firmware\n");
  652. return status;
  653. }
  654. dev_info(&mgp->pdev->dev,
  655. "Successfully adopted running firmware\n");
  656. if (mgp->tx_boundary == 4096) {
  657. dev_warn(&mgp->pdev->dev,
  658. "Using firmware currently running on NIC"
  659. ". For optimal\n");
  660. dev_warn(&mgp->pdev->dev,
  661. "performance consider loading optimized "
  662. "firmware\n");
  663. dev_warn(&mgp->pdev->dev, "via hotplug\n");
  664. }
  665. set_fw_name(mgp, "adopted", false);
  666. mgp->tx_boundary = 2048;
  667. myri10ge_dummy_rdma(mgp, 1);
  668. status = myri10ge_get_firmware_capabilities(mgp);
  669. return status;
  670. }
  671. /* clear confirmation addr */
  672. mgp->cmd->data = 0;
  673. mb();
  674. /* send a reload command to the bootstrap MCP, and wait for the
  675. * response in the confirmation address. The firmware should
  676. * write a -1 there to indicate it is alive and well
  677. */
  678. dma_low = MYRI10GE_LOWPART_TO_U32(mgp->cmd_bus);
  679. dma_high = MYRI10GE_HIGHPART_TO_U32(mgp->cmd_bus);
  680. buf[0] = htonl(dma_high); /* confirm addr MSW */
  681. buf[1] = htonl(dma_low); /* confirm addr LSW */
  682. buf[2] = MYRI10GE_NO_CONFIRM_DATA; /* confirm data */
  683. /* FIX: All newest firmware should un-protect the bottom of
  684. * the sram before handoff. However, the very first interfaces
  685. * do not. Therefore the handoff copy must skip the first 8 bytes
  686. */
  687. buf[3] = htonl(MYRI10GE_FW_OFFSET + 8); /* where the code starts */
  688. buf[4] = htonl(size - 8); /* length of code */
  689. buf[5] = htonl(8); /* where to copy to */
  690. buf[6] = htonl(0); /* where to jump to */
  691. submit = mgp->sram + MXGEFW_BOOT_HANDOFF;
  692. myri10ge_pio_copy(submit, &buf, sizeof(buf));
  693. mb();
  694. msleep(1);
  695. mb();
  696. i = 0;
  697. while (mgp->cmd->data != MYRI10GE_NO_CONFIRM_DATA && i < 9) {
  698. msleep(1 << i);
  699. i++;
  700. }
  701. if (mgp->cmd->data != MYRI10GE_NO_CONFIRM_DATA) {
  702. dev_err(&mgp->pdev->dev, "handoff failed\n");
  703. return -ENXIO;
  704. }
  705. myri10ge_dummy_rdma(mgp, 1);
  706. status = myri10ge_get_firmware_capabilities(mgp);
  707. return status;
  708. }
  709. static int myri10ge_update_mac_address(struct myri10ge_priv *mgp, u8 * addr)
  710. {
  711. struct myri10ge_cmd cmd;
  712. int status;
  713. cmd.data0 = ((addr[0] << 24) | (addr[1] << 16)
  714. | (addr[2] << 8) | addr[3]);
  715. cmd.data1 = ((addr[4] << 8) | (addr[5]));
  716. status = myri10ge_send_cmd(mgp, MXGEFW_SET_MAC_ADDRESS, &cmd, 0);
  717. return status;
  718. }
  719. static int myri10ge_change_pause(struct myri10ge_priv *mgp, int pause)
  720. {
  721. struct myri10ge_cmd cmd;
  722. int status, ctl;
  723. ctl = pause ? MXGEFW_ENABLE_FLOW_CONTROL : MXGEFW_DISABLE_FLOW_CONTROL;
  724. status = myri10ge_send_cmd(mgp, ctl, &cmd, 0);
  725. if (status) {
  726. netdev_err(mgp->dev, "Failed to set flow control mode\n");
  727. return status;
  728. }
  729. mgp->pause = pause;
  730. return 0;
  731. }
  732. static void
  733. myri10ge_change_promisc(struct myri10ge_priv *mgp, int promisc, int atomic)
  734. {
  735. struct myri10ge_cmd cmd;
  736. int status, ctl;
  737. ctl = promisc ? MXGEFW_ENABLE_PROMISC : MXGEFW_DISABLE_PROMISC;
  738. status = myri10ge_send_cmd(mgp, ctl, &cmd, atomic);
  739. if (status)
  740. netdev_err(mgp->dev, "Failed to set promisc mode\n");
  741. }
  742. static int myri10ge_dma_test(struct myri10ge_priv *mgp, int test_type)
  743. {
  744. struct myri10ge_cmd cmd;
  745. int status;
  746. u32 len;
  747. struct page *dmatest_page;
  748. dma_addr_t dmatest_bus;
  749. char *test = " ";
  750. dmatest_page = alloc_page(GFP_KERNEL);
  751. if (!dmatest_page)
  752. return -ENOMEM;
  753. dmatest_bus = pci_map_page(mgp->pdev, dmatest_page, 0, PAGE_SIZE,
  754. DMA_BIDIRECTIONAL);
  755. /* Run a small DMA test.
  756. * The magic multipliers to the length tell the firmware
  757. * to do DMA read, write, or read+write tests. The
  758. * results are returned in cmd.data0. The upper 16
  759. * bits or the return is the number of transfers completed.
  760. * The lower 16 bits is the time in 0.5us ticks that the
  761. * transfers took to complete.
  762. */
  763. len = mgp->tx_boundary;
  764. cmd.data0 = MYRI10GE_LOWPART_TO_U32(dmatest_bus);
  765. cmd.data1 = MYRI10GE_HIGHPART_TO_U32(dmatest_bus);
  766. cmd.data2 = len * 0x10000;
  767. status = myri10ge_send_cmd(mgp, test_type, &cmd, 0);
  768. if (status != 0) {
  769. test = "read";
  770. goto abort;
  771. }
  772. mgp->read_dma = ((cmd.data0 >> 16) * len * 2) / (cmd.data0 & 0xffff);
  773. cmd.data0 = MYRI10GE_LOWPART_TO_U32(dmatest_bus);
  774. cmd.data1 = MYRI10GE_HIGHPART_TO_U32(dmatest_bus);
  775. cmd.data2 = len * 0x1;
  776. status = myri10ge_send_cmd(mgp, test_type, &cmd, 0);
  777. if (status != 0) {
  778. test = "write";
  779. goto abort;
  780. }
  781. mgp->write_dma = ((cmd.data0 >> 16) * len * 2) / (cmd.data0 & 0xffff);
  782. cmd.data0 = MYRI10GE_LOWPART_TO_U32(dmatest_bus);
  783. cmd.data1 = MYRI10GE_HIGHPART_TO_U32(dmatest_bus);
  784. cmd.data2 = len * 0x10001;
  785. status = myri10ge_send_cmd(mgp, test_type, &cmd, 0);
  786. if (status != 0) {
  787. test = "read/write";
  788. goto abort;
  789. }
  790. mgp->read_write_dma = ((cmd.data0 >> 16) * len * 2 * 2) /
  791. (cmd.data0 & 0xffff);
  792. abort:
  793. pci_unmap_page(mgp->pdev, dmatest_bus, PAGE_SIZE, DMA_BIDIRECTIONAL);
  794. put_page(dmatest_page);
  795. if (status != 0 && test_type != MXGEFW_CMD_UNALIGNED_TEST)
  796. dev_warn(&mgp->pdev->dev, "DMA %s benchmark failed: %d\n",
  797. test, status);
  798. return status;
  799. }
  800. static int myri10ge_reset(struct myri10ge_priv *mgp)
  801. {
  802. struct myri10ge_cmd cmd;
  803. struct myri10ge_slice_state *ss;
  804. int i, status;
  805. size_t bytes;
  806. #ifdef CONFIG_MYRI10GE_DCA
  807. unsigned long dca_tag_off;
  808. #endif
  809. /* try to send a reset command to the card to see if it
  810. * is alive */
  811. memset(&cmd, 0, sizeof(cmd));
  812. status = myri10ge_send_cmd(mgp, MXGEFW_CMD_RESET, &cmd, 0);
  813. if (status != 0) {
  814. dev_err(&mgp->pdev->dev, "failed reset\n");
  815. return -ENXIO;
  816. }
  817. (void)myri10ge_dma_test(mgp, MXGEFW_DMA_TEST);
  818. /*
  819. * Use non-ndis mcp_slot (eg, 4 bytes total,
  820. * no toeplitz hash value returned. Older firmware will
  821. * not understand this command, but will use the correct
  822. * sized mcp_slot, so we ignore error returns
  823. */
  824. cmd.data0 = MXGEFW_RSS_MCP_SLOT_TYPE_MIN;
  825. (void)myri10ge_send_cmd(mgp, MXGEFW_CMD_SET_RSS_MCP_SLOT_TYPE, &cmd, 0);
  826. /* Now exchange information about interrupts */
  827. bytes = mgp->max_intr_slots * sizeof(*mgp->ss[0].rx_done.entry);
  828. cmd.data0 = (u32) bytes;
  829. status = myri10ge_send_cmd(mgp, MXGEFW_CMD_SET_INTRQ_SIZE, &cmd, 0);
  830. /*
  831. * Even though we already know how many slices are supported
  832. * via myri10ge_probe_slices() MXGEFW_CMD_GET_MAX_RSS_QUEUES
  833. * has magic side effects, and must be called after a reset.
  834. * It must be called prior to calling any RSS related cmds,
  835. * including assigning an interrupt queue for anything but
  836. * slice 0. It must also be called *after*
  837. * MXGEFW_CMD_SET_INTRQ_SIZE, since the intrq size is used by
  838. * the firmware to compute offsets.
  839. */
  840. if (mgp->num_slices > 1) {
  841. /* ask the maximum number of slices it supports */
  842. status = myri10ge_send_cmd(mgp, MXGEFW_CMD_GET_MAX_RSS_QUEUES,
  843. &cmd, 0);
  844. if (status != 0) {
  845. dev_err(&mgp->pdev->dev,
  846. "failed to get number of slices\n");
  847. }
  848. /*
  849. * MXGEFW_CMD_ENABLE_RSS_QUEUES must be called prior
  850. * to setting up the interrupt queue DMA
  851. */
  852. cmd.data0 = mgp->num_slices;
  853. cmd.data1 = MXGEFW_SLICE_INTR_MODE_ONE_PER_SLICE;
  854. if (mgp->dev->real_num_tx_queues > 1)
  855. cmd.data1 |= MXGEFW_SLICE_ENABLE_MULTIPLE_TX_QUEUES;
  856. status = myri10ge_send_cmd(mgp, MXGEFW_CMD_ENABLE_RSS_QUEUES,
  857. &cmd, 0);
  858. /* Firmware older than 1.4.32 only supports multiple
  859. * RX queues, so if we get an error, first retry using a
  860. * single TX queue before giving up */
  861. if (status != 0 && mgp->dev->real_num_tx_queues > 1) {
  862. netif_set_real_num_tx_queues(mgp->dev, 1);
  863. cmd.data0 = mgp->num_slices;
  864. cmd.data1 = MXGEFW_SLICE_INTR_MODE_ONE_PER_SLICE;
  865. status = myri10ge_send_cmd(mgp,
  866. MXGEFW_CMD_ENABLE_RSS_QUEUES,
  867. &cmd, 0);
  868. }
  869. if (status != 0) {
  870. dev_err(&mgp->pdev->dev,
  871. "failed to set number of slices\n");
  872. return status;
  873. }
  874. }
  875. for (i = 0; i < mgp->num_slices; i++) {
  876. ss = &mgp->ss[i];
  877. cmd.data0 = MYRI10GE_LOWPART_TO_U32(ss->rx_done.bus);
  878. cmd.data1 = MYRI10GE_HIGHPART_TO_U32(ss->rx_done.bus);
  879. cmd.data2 = i;
  880. status |= myri10ge_send_cmd(mgp, MXGEFW_CMD_SET_INTRQ_DMA,
  881. &cmd, 0);
  882. };
  883. status |=
  884. myri10ge_send_cmd(mgp, MXGEFW_CMD_GET_IRQ_ACK_OFFSET, &cmd, 0);
  885. for (i = 0; i < mgp->num_slices; i++) {
  886. ss = &mgp->ss[i];
  887. ss->irq_claim =
  888. (__iomem __be32 *) (mgp->sram + cmd.data0 + 8 * i);
  889. }
  890. status |= myri10ge_send_cmd(mgp, MXGEFW_CMD_GET_IRQ_DEASSERT_OFFSET,
  891. &cmd, 0);
  892. mgp->irq_deassert = (__iomem __be32 *) (mgp->sram + cmd.data0);
  893. status |= myri10ge_send_cmd
  894. (mgp, MXGEFW_CMD_GET_INTR_COAL_DELAY_OFFSET, &cmd, 0);
  895. mgp->intr_coal_delay_ptr = (__iomem __be32 *) (mgp->sram + cmd.data0);
  896. if (status != 0) {
  897. dev_err(&mgp->pdev->dev, "failed set interrupt parameters\n");
  898. return status;
  899. }
  900. put_be32(htonl(mgp->intr_coal_delay), mgp->intr_coal_delay_ptr);
  901. #ifdef CONFIG_MYRI10GE_DCA
  902. status = myri10ge_send_cmd(mgp, MXGEFW_CMD_GET_DCA_OFFSET, &cmd, 0);
  903. dca_tag_off = cmd.data0;
  904. for (i = 0; i < mgp->num_slices; i++) {
  905. ss = &mgp->ss[i];
  906. if (status == 0) {
  907. ss->dca_tag = (__iomem __be32 *)
  908. (mgp->sram + dca_tag_off + 4 * i);
  909. } else {
  910. ss->dca_tag = NULL;
  911. }
  912. }
  913. #endif /* CONFIG_MYRI10GE_DCA */
  914. /* reset mcp/driver shared state back to 0 */
  915. mgp->link_changes = 0;
  916. for (i = 0; i < mgp->num_slices; i++) {
  917. ss = &mgp->ss[i];
  918. memset(ss->rx_done.entry, 0, bytes);
  919. ss->tx.req = 0;
  920. ss->tx.done = 0;
  921. ss->tx.pkt_start = 0;
  922. ss->tx.pkt_done = 0;
  923. ss->rx_big.cnt = 0;
  924. ss->rx_small.cnt = 0;
  925. ss->rx_done.idx = 0;
  926. ss->rx_done.cnt = 0;
  927. ss->tx.wake_queue = 0;
  928. ss->tx.stop_queue = 0;
  929. }
  930. status = myri10ge_update_mac_address(mgp, mgp->dev->dev_addr);
  931. myri10ge_change_pause(mgp, mgp->pause);
  932. myri10ge_set_multicast_list(mgp->dev);
  933. return status;
  934. }
  935. #ifdef CONFIG_MYRI10GE_DCA
  936. static int myri10ge_toggle_relaxed(struct pci_dev *pdev, int on)
  937. {
  938. int ret, cap, err;
  939. u16 ctl;
  940. cap = pci_find_capability(pdev, PCI_CAP_ID_EXP);
  941. if (!cap)
  942. return 0;
  943. err = pci_read_config_word(pdev, cap + PCI_EXP_DEVCTL, &ctl);
  944. ret = (ctl & PCI_EXP_DEVCTL_RELAX_EN) >> 4;
  945. if (ret != on) {
  946. ctl &= ~PCI_EXP_DEVCTL_RELAX_EN;
  947. ctl |= (on << 4);
  948. pci_write_config_word(pdev, cap + PCI_EXP_DEVCTL, ctl);
  949. }
  950. return ret;
  951. }
  952. static void
  953. myri10ge_write_dca(struct myri10ge_slice_state *ss, int cpu, int tag)
  954. {
  955. ss->cached_dca_tag = tag;
  956. put_be32(htonl(tag), ss->dca_tag);
  957. }
  958. static inline void myri10ge_update_dca(struct myri10ge_slice_state *ss)
  959. {
  960. int cpu = get_cpu();
  961. int tag;
  962. if (cpu != ss->cpu) {
  963. tag = dca3_get_tag(&ss->mgp->pdev->dev, cpu);
  964. if (ss->cached_dca_tag != tag)
  965. myri10ge_write_dca(ss, cpu, tag);
  966. ss->cpu = cpu;
  967. }
  968. put_cpu();
  969. }
  970. static void myri10ge_setup_dca(struct myri10ge_priv *mgp)
  971. {
  972. int err, i;
  973. struct pci_dev *pdev = mgp->pdev;
  974. if (mgp->ss[0].dca_tag == NULL || mgp->dca_enabled)
  975. return;
  976. if (!myri10ge_dca) {
  977. dev_err(&pdev->dev, "dca disabled by administrator\n");
  978. return;
  979. }
  980. err = dca_add_requester(&pdev->dev);
  981. if (err) {
  982. if (err != -ENODEV)
  983. dev_err(&pdev->dev,
  984. "dca_add_requester() failed, err=%d\n", err);
  985. return;
  986. }
  987. mgp->relaxed_order = myri10ge_toggle_relaxed(pdev, 0);
  988. mgp->dca_enabled = 1;
  989. for (i = 0; i < mgp->num_slices; i++) {
  990. mgp->ss[i].cpu = -1;
  991. mgp->ss[i].cached_dca_tag = -1;
  992. myri10ge_update_dca(&mgp->ss[i]);
  993. }
  994. }
  995. static void myri10ge_teardown_dca(struct myri10ge_priv *mgp)
  996. {
  997. struct pci_dev *pdev = mgp->pdev;
  998. int err;
  999. if (!mgp->dca_enabled)
  1000. return;
  1001. mgp->dca_enabled = 0;
  1002. if (mgp->relaxed_order)
  1003. myri10ge_toggle_relaxed(pdev, 1);
  1004. err = dca_remove_requester(&pdev->dev);
  1005. }
  1006. static int myri10ge_notify_dca_device(struct device *dev, void *data)
  1007. {
  1008. struct myri10ge_priv *mgp;
  1009. unsigned long event;
  1010. mgp = dev_get_drvdata(dev);
  1011. event = *(unsigned long *)data;
  1012. if (event == DCA_PROVIDER_ADD)
  1013. myri10ge_setup_dca(mgp);
  1014. else if (event == DCA_PROVIDER_REMOVE)
  1015. myri10ge_teardown_dca(mgp);
  1016. return 0;
  1017. }
  1018. #endif /* CONFIG_MYRI10GE_DCA */
  1019. static inline void
  1020. myri10ge_submit_8rx(struct mcp_kreq_ether_recv __iomem * dst,
  1021. struct mcp_kreq_ether_recv *src)
  1022. {
  1023. __be32 low;
  1024. low = src->addr_low;
  1025. src->addr_low = htonl(DMA_BIT_MASK(32));
  1026. myri10ge_pio_copy(dst, src, 4 * sizeof(*src));
  1027. mb();
  1028. myri10ge_pio_copy(dst + 4, src + 4, 4 * sizeof(*src));
  1029. mb();
  1030. src->addr_low = low;
  1031. put_be32(low, &dst->addr_low);
  1032. mb();
  1033. }
  1034. static inline void myri10ge_vlan_ip_csum(struct sk_buff *skb, __wsum hw_csum)
  1035. {
  1036. struct vlan_hdr *vh = (struct vlan_hdr *)(skb->data);
  1037. if ((skb->protocol == htons(ETH_P_8021Q)) &&
  1038. (vh->h_vlan_encapsulated_proto == htons(ETH_P_IP) ||
  1039. vh->h_vlan_encapsulated_proto == htons(ETH_P_IPV6))) {
  1040. skb->csum = hw_csum;
  1041. skb->ip_summed = CHECKSUM_COMPLETE;
  1042. }
  1043. }
  1044. static inline void
  1045. myri10ge_rx_skb_build(struct sk_buff *skb, u8 * va,
  1046. struct skb_frag_struct *rx_frags, int len, int hlen)
  1047. {
  1048. struct skb_frag_struct *skb_frags;
  1049. skb->len = skb->data_len = len;
  1050. skb->truesize = len + sizeof(struct sk_buff);
  1051. /* attach the page(s) */
  1052. skb_frags = skb_shinfo(skb)->frags;
  1053. while (len > 0) {
  1054. memcpy(skb_frags, rx_frags, sizeof(*skb_frags));
  1055. len -= rx_frags->size;
  1056. skb_frags++;
  1057. rx_frags++;
  1058. skb_shinfo(skb)->nr_frags++;
  1059. }
  1060. /* pskb_may_pull is not available in irq context, but
  1061. * skb_pull() (for ether_pad and eth_type_trans()) requires
  1062. * the beginning of the packet in skb_headlen(), move it
  1063. * manually */
  1064. skb_copy_to_linear_data(skb, va, hlen);
  1065. skb_shinfo(skb)->frags[0].page_offset += hlen;
  1066. skb_shinfo(skb)->frags[0].size -= hlen;
  1067. skb->data_len -= hlen;
  1068. skb->tail += hlen;
  1069. skb_pull(skb, MXGEFW_PAD);
  1070. }
  1071. static void
  1072. myri10ge_alloc_rx_pages(struct myri10ge_priv *mgp, struct myri10ge_rx_buf *rx,
  1073. int bytes, int watchdog)
  1074. {
  1075. struct page *page;
  1076. int idx;
  1077. #if MYRI10GE_ALLOC_SIZE > 4096
  1078. int end_offset;
  1079. #endif
  1080. if (unlikely(rx->watchdog_needed && !watchdog))
  1081. return;
  1082. /* try to refill entire ring */
  1083. while (rx->fill_cnt != (rx->cnt + rx->mask + 1)) {
  1084. idx = rx->fill_cnt & rx->mask;
  1085. if (rx->page_offset + bytes <= MYRI10GE_ALLOC_SIZE) {
  1086. /* we can use part of previous page */
  1087. get_page(rx->page);
  1088. } else {
  1089. /* we need a new page */
  1090. page =
  1091. alloc_pages(GFP_ATOMIC | __GFP_COMP,
  1092. MYRI10GE_ALLOC_ORDER);
  1093. if (unlikely(page == NULL)) {
  1094. if (rx->fill_cnt - rx->cnt < 16)
  1095. rx->watchdog_needed = 1;
  1096. return;
  1097. }
  1098. rx->page = page;
  1099. rx->page_offset = 0;
  1100. rx->bus = pci_map_page(mgp->pdev, page, 0,
  1101. MYRI10GE_ALLOC_SIZE,
  1102. PCI_DMA_FROMDEVICE);
  1103. }
  1104. rx->info[idx].page = rx->page;
  1105. rx->info[idx].page_offset = rx->page_offset;
  1106. /* note that this is the address of the start of the
  1107. * page */
  1108. dma_unmap_addr_set(&rx->info[idx], bus, rx->bus);
  1109. rx->shadow[idx].addr_low =
  1110. htonl(MYRI10GE_LOWPART_TO_U32(rx->bus) + rx->page_offset);
  1111. rx->shadow[idx].addr_high =
  1112. htonl(MYRI10GE_HIGHPART_TO_U32(rx->bus));
  1113. /* start next packet on a cacheline boundary */
  1114. rx->page_offset += SKB_DATA_ALIGN(bytes);
  1115. #if MYRI10GE_ALLOC_SIZE > 4096
  1116. /* don't cross a 4KB boundary */
  1117. end_offset = rx->page_offset + bytes - 1;
  1118. if ((unsigned)(rx->page_offset ^ end_offset) > 4095)
  1119. rx->page_offset = end_offset & ~4095;
  1120. #endif
  1121. rx->fill_cnt++;
  1122. /* copy 8 descriptors to the firmware at a time */
  1123. if ((idx & 7) == 7) {
  1124. myri10ge_submit_8rx(&rx->lanai[idx - 7],
  1125. &rx->shadow[idx - 7]);
  1126. }
  1127. }
  1128. }
  1129. static inline void
  1130. myri10ge_unmap_rx_page(struct pci_dev *pdev,
  1131. struct myri10ge_rx_buffer_state *info, int bytes)
  1132. {
  1133. /* unmap the recvd page if we're the only or last user of it */
  1134. if (bytes >= MYRI10GE_ALLOC_SIZE / 2 ||
  1135. (info->page_offset + 2 * bytes) > MYRI10GE_ALLOC_SIZE) {
  1136. pci_unmap_page(pdev, (dma_unmap_addr(info, bus)
  1137. & ~(MYRI10GE_ALLOC_SIZE - 1)),
  1138. MYRI10GE_ALLOC_SIZE, PCI_DMA_FROMDEVICE);
  1139. }
  1140. }
  1141. #define MYRI10GE_HLEN 64 /* The number of bytes to copy from a
  1142. * page into an skb */
  1143. static inline int
  1144. myri10ge_rx_done(struct myri10ge_slice_state *ss, struct myri10ge_rx_buf *rx,
  1145. int bytes, int len, __wsum csum)
  1146. {
  1147. struct myri10ge_priv *mgp = ss->mgp;
  1148. struct sk_buff *skb;
  1149. struct skb_frag_struct rx_frags[MYRI10GE_MAX_FRAGS_PER_FRAME];
  1150. int i, idx, hlen, remainder;
  1151. struct pci_dev *pdev = mgp->pdev;
  1152. struct net_device *dev = mgp->dev;
  1153. u8 *va;
  1154. len += MXGEFW_PAD;
  1155. idx = rx->cnt & rx->mask;
  1156. va = page_address(rx->info[idx].page) + rx->info[idx].page_offset;
  1157. prefetch(va);
  1158. /* Fill skb_frag_struct(s) with data from our receive */
  1159. for (i = 0, remainder = len; remainder > 0; i++) {
  1160. myri10ge_unmap_rx_page(pdev, &rx->info[idx], bytes);
  1161. rx_frags[i].page = rx->info[idx].page;
  1162. rx_frags[i].page_offset = rx->info[idx].page_offset;
  1163. if (remainder < MYRI10GE_ALLOC_SIZE)
  1164. rx_frags[i].size = remainder;
  1165. else
  1166. rx_frags[i].size = MYRI10GE_ALLOC_SIZE;
  1167. rx->cnt++;
  1168. idx = rx->cnt & rx->mask;
  1169. remainder -= MYRI10GE_ALLOC_SIZE;
  1170. }
  1171. if (dev->features & NETIF_F_LRO) {
  1172. rx_frags[0].page_offset += MXGEFW_PAD;
  1173. rx_frags[0].size -= MXGEFW_PAD;
  1174. len -= MXGEFW_PAD;
  1175. lro_receive_frags(&ss->rx_done.lro_mgr, rx_frags,
  1176. /* opaque, will come back in get_frag_header */
  1177. len, len,
  1178. (void *)(__force unsigned long)csum, csum);
  1179. return 1;
  1180. }
  1181. hlen = MYRI10GE_HLEN > len ? len : MYRI10GE_HLEN;
  1182. /* allocate an skb to attach the page(s) to. This is done
  1183. * after trying LRO, so as to avoid skb allocation overheads */
  1184. skb = netdev_alloc_skb(dev, MYRI10GE_HLEN + 16);
  1185. if (unlikely(skb == NULL)) {
  1186. ss->stats.rx_dropped++;
  1187. do {
  1188. i--;
  1189. put_page(rx_frags[i].page);
  1190. } while (i != 0);
  1191. return 0;
  1192. }
  1193. /* Attach the pages to the skb, and trim off any padding */
  1194. myri10ge_rx_skb_build(skb, va, rx_frags, len, hlen);
  1195. if (skb_shinfo(skb)->frags[0].size <= 0) {
  1196. put_page(skb_shinfo(skb)->frags[0].page);
  1197. skb_shinfo(skb)->nr_frags = 0;
  1198. }
  1199. skb->protocol = eth_type_trans(skb, dev);
  1200. skb_record_rx_queue(skb, ss - &mgp->ss[0]);
  1201. if (mgp->csum_flag) {
  1202. if ((skb->protocol == htons(ETH_P_IP)) ||
  1203. (skb->protocol == htons(ETH_P_IPV6))) {
  1204. skb->csum = csum;
  1205. skb->ip_summed = CHECKSUM_COMPLETE;
  1206. } else
  1207. myri10ge_vlan_ip_csum(skb, csum);
  1208. }
  1209. netif_receive_skb(skb);
  1210. return 1;
  1211. }
  1212. static inline void
  1213. myri10ge_tx_done(struct myri10ge_slice_state *ss, int mcp_index)
  1214. {
  1215. struct pci_dev *pdev = ss->mgp->pdev;
  1216. struct myri10ge_tx_buf *tx = &ss->tx;
  1217. struct netdev_queue *dev_queue;
  1218. struct sk_buff *skb;
  1219. int idx, len;
  1220. while (tx->pkt_done != mcp_index) {
  1221. idx = tx->done & tx->mask;
  1222. skb = tx->info[idx].skb;
  1223. /* Mark as free */
  1224. tx->info[idx].skb = NULL;
  1225. if (tx->info[idx].last) {
  1226. tx->pkt_done++;
  1227. tx->info[idx].last = 0;
  1228. }
  1229. tx->done++;
  1230. len = dma_unmap_len(&tx->info[idx], len);
  1231. dma_unmap_len_set(&tx->info[idx], len, 0);
  1232. if (skb) {
  1233. ss->stats.tx_bytes += skb->len;
  1234. ss->stats.tx_packets++;
  1235. dev_kfree_skb_irq(skb);
  1236. if (len)
  1237. pci_unmap_single(pdev,
  1238. dma_unmap_addr(&tx->info[idx],
  1239. bus), len,
  1240. PCI_DMA_TODEVICE);
  1241. } else {
  1242. if (len)
  1243. pci_unmap_page(pdev,
  1244. dma_unmap_addr(&tx->info[idx],
  1245. bus), len,
  1246. PCI_DMA_TODEVICE);
  1247. }
  1248. }
  1249. dev_queue = netdev_get_tx_queue(ss->dev, ss - ss->mgp->ss);
  1250. /*
  1251. * Make a minimal effort to prevent the NIC from polling an
  1252. * idle tx queue. If we can't get the lock we leave the queue
  1253. * active. In this case, either a thread was about to start
  1254. * using the queue anyway, or we lost a race and the NIC will
  1255. * waste some of its resources polling an inactive queue for a
  1256. * while.
  1257. */
  1258. if ((ss->mgp->dev->real_num_tx_queues > 1) &&
  1259. __netif_tx_trylock(dev_queue)) {
  1260. if (tx->req == tx->done) {
  1261. tx->queue_active = 0;
  1262. put_be32(htonl(1), tx->send_stop);
  1263. mb();
  1264. mmiowb();
  1265. }
  1266. __netif_tx_unlock(dev_queue);
  1267. }
  1268. /* start the queue if we've stopped it */
  1269. if (netif_tx_queue_stopped(dev_queue) &&
  1270. tx->req - tx->done < (tx->mask >> 1)) {
  1271. tx->wake_queue++;
  1272. netif_tx_wake_queue(dev_queue);
  1273. }
  1274. }
  1275. static inline int
  1276. myri10ge_clean_rx_done(struct myri10ge_slice_state *ss, int budget)
  1277. {
  1278. struct myri10ge_rx_done *rx_done = &ss->rx_done;
  1279. struct myri10ge_priv *mgp = ss->mgp;
  1280. struct net_device *netdev = mgp->dev;
  1281. unsigned long rx_bytes = 0;
  1282. unsigned long rx_packets = 0;
  1283. unsigned long rx_ok;
  1284. int idx = rx_done->idx;
  1285. int cnt = rx_done->cnt;
  1286. int work_done = 0;
  1287. u16 length;
  1288. __wsum checksum;
  1289. while (rx_done->entry[idx].length != 0 && work_done < budget) {
  1290. length = ntohs(rx_done->entry[idx].length);
  1291. rx_done->entry[idx].length = 0;
  1292. checksum = csum_unfold(rx_done->entry[idx].checksum);
  1293. if (length <= mgp->small_bytes)
  1294. rx_ok = myri10ge_rx_done(ss, &ss->rx_small,
  1295. mgp->small_bytes,
  1296. length, checksum);
  1297. else
  1298. rx_ok = myri10ge_rx_done(ss, &ss->rx_big,
  1299. mgp->big_bytes,
  1300. length, checksum);
  1301. rx_packets += rx_ok;
  1302. rx_bytes += rx_ok * (unsigned long)length;
  1303. cnt++;
  1304. idx = cnt & (mgp->max_intr_slots - 1);
  1305. work_done++;
  1306. }
  1307. rx_done->idx = idx;
  1308. rx_done->cnt = cnt;
  1309. ss->stats.rx_packets += rx_packets;
  1310. ss->stats.rx_bytes += rx_bytes;
  1311. if (netdev->features & NETIF_F_LRO)
  1312. lro_flush_all(&rx_done->lro_mgr);
  1313. /* restock receive rings if needed */
  1314. if (ss->rx_small.fill_cnt - ss->rx_small.cnt < myri10ge_fill_thresh)
  1315. myri10ge_alloc_rx_pages(mgp, &ss->rx_small,
  1316. mgp->small_bytes + MXGEFW_PAD, 0);
  1317. if (ss->rx_big.fill_cnt - ss->rx_big.cnt < myri10ge_fill_thresh)
  1318. myri10ge_alloc_rx_pages(mgp, &ss->rx_big, mgp->big_bytes, 0);
  1319. return work_done;
  1320. }
  1321. static inline void myri10ge_check_statblock(struct myri10ge_priv *mgp)
  1322. {
  1323. struct mcp_irq_data *stats = mgp->ss[0].fw_stats;
  1324. if (unlikely(stats->stats_updated)) {
  1325. unsigned link_up = ntohl(stats->link_up);
  1326. if (mgp->link_state != link_up) {
  1327. mgp->link_state = link_up;
  1328. if (mgp->link_state == MXGEFW_LINK_UP) {
  1329. if (netif_msg_link(mgp))
  1330. netdev_info(mgp->dev, "link up\n");
  1331. netif_carrier_on(mgp->dev);
  1332. mgp->link_changes++;
  1333. } else {
  1334. if (netif_msg_link(mgp))
  1335. netdev_info(mgp->dev, "link %s\n",
  1336. link_up == MXGEFW_LINK_MYRINET ?
  1337. "mismatch (Myrinet detected)" :
  1338. "down");
  1339. netif_carrier_off(mgp->dev);
  1340. mgp->link_changes++;
  1341. }
  1342. }
  1343. if (mgp->rdma_tags_available !=
  1344. ntohl(stats->rdma_tags_available)) {
  1345. mgp->rdma_tags_available =
  1346. ntohl(stats->rdma_tags_available);
  1347. netdev_warn(mgp->dev, "RDMA timed out! %d tags left\n",
  1348. mgp->rdma_tags_available);
  1349. }
  1350. mgp->down_cnt += stats->link_down;
  1351. if (stats->link_down)
  1352. wake_up(&mgp->down_wq);
  1353. }
  1354. }
  1355. static int myri10ge_poll(struct napi_struct *napi, int budget)
  1356. {
  1357. struct myri10ge_slice_state *ss =
  1358. container_of(napi, struct myri10ge_slice_state, napi);
  1359. int work_done;
  1360. #ifdef CONFIG_MYRI10GE_DCA
  1361. if (ss->mgp->dca_enabled)
  1362. myri10ge_update_dca(ss);
  1363. #endif
  1364. /* process as many rx events as NAPI will allow */
  1365. work_done = myri10ge_clean_rx_done(ss, budget);
  1366. if (work_done < budget) {
  1367. napi_complete(napi);
  1368. put_be32(htonl(3), ss->irq_claim);
  1369. }
  1370. return work_done;
  1371. }
  1372. static irqreturn_t myri10ge_intr(int irq, void *arg)
  1373. {
  1374. struct myri10ge_slice_state *ss = arg;
  1375. struct myri10ge_priv *mgp = ss->mgp;
  1376. struct mcp_irq_data *stats = ss->fw_stats;
  1377. struct myri10ge_tx_buf *tx = &ss->tx;
  1378. u32 send_done_count;
  1379. int i;
  1380. /* an interrupt on a non-zero receive-only slice is implicitly
  1381. * valid since MSI-X irqs are not shared */
  1382. if ((mgp->dev->real_num_tx_queues == 1) && (ss != mgp->ss)) {
  1383. napi_schedule(&ss->napi);
  1384. return IRQ_HANDLED;
  1385. }
  1386. /* make sure it is our IRQ, and that the DMA has finished */
  1387. if (unlikely(!stats->valid))
  1388. return IRQ_NONE;
  1389. /* low bit indicates receives are present, so schedule
  1390. * napi poll handler */
  1391. if (stats->valid & 1)
  1392. napi_schedule(&ss->napi);
  1393. if (!mgp->msi_enabled && !mgp->msix_enabled) {
  1394. put_be32(0, mgp->irq_deassert);
  1395. if (!myri10ge_deassert_wait)
  1396. stats->valid = 0;
  1397. mb();
  1398. } else
  1399. stats->valid = 0;
  1400. /* Wait for IRQ line to go low, if using INTx */
  1401. i = 0;
  1402. while (1) {
  1403. i++;
  1404. /* check for transmit completes and receives */
  1405. send_done_count = ntohl(stats->send_done_count);
  1406. if (send_done_count != tx->pkt_done)
  1407. myri10ge_tx_done(ss, (int)send_done_count);
  1408. if (unlikely(i > myri10ge_max_irq_loops)) {
  1409. netdev_err(mgp->dev, "irq stuck?\n");
  1410. stats->valid = 0;
  1411. schedule_work(&mgp->watchdog_work);
  1412. }
  1413. if (likely(stats->valid == 0))
  1414. break;
  1415. cpu_relax();
  1416. barrier();
  1417. }
  1418. /* Only slice 0 updates stats */
  1419. if (ss == mgp->ss)
  1420. myri10ge_check_statblock(mgp);
  1421. put_be32(htonl(3), ss->irq_claim + 1);
  1422. return IRQ_HANDLED;
  1423. }
  1424. static int
  1425. myri10ge_get_settings(struct net_device *netdev, struct ethtool_cmd *cmd)
  1426. {
  1427. struct myri10ge_priv *mgp = netdev_priv(netdev);
  1428. char *ptr;
  1429. int i;
  1430. cmd->autoneg = AUTONEG_DISABLE;
  1431. cmd->speed = SPEED_10000;
  1432. cmd->duplex = DUPLEX_FULL;
  1433. /*
  1434. * parse the product code to deterimine the interface type
  1435. * (CX4, XFP, Quad Ribbon Fiber) by looking at the character
  1436. * after the 3rd dash in the driver's cached copy of the
  1437. * EEPROM's product code string.
  1438. */
  1439. ptr = mgp->product_code_string;
  1440. if (ptr == NULL) {
  1441. netdev_err(netdev, "Missing product code\n");
  1442. return 0;
  1443. }
  1444. for (i = 0; i < 3; i++, ptr++) {
  1445. ptr = strchr(ptr, '-');
  1446. if (ptr == NULL) {
  1447. netdev_err(netdev, "Invalid product code %s\n",
  1448. mgp->product_code_string);
  1449. return 0;
  1450. }
  1451. }
  1452. if (*ptr == '2')
  1453. ptr++;
  1454. if (*ptr == 'R' || *ptr == 'Q' || *ptr == 'S') {
  1455. /* We've found either an XFP, quad ribbon fiber, or SFP+ */
  1456. cmd->port = PORT_FIBRE;
  1457. cmd->supported |= SUPPORTED_FIBRE;
  1458. cmd->advertising |= ADVERTISED_FIBRE;
  1459. } else {
  1460. cmd->port = PORT_OTHER;
  1461. }
  1462. if (*ptr == 'R' || *ptr == 'S')
  1463. cmd->transceiver = XCVR_EXTERNAL;
  1464. else
  1465. cmd->transceiver = XCVR_INTERNAL;
  1466. return 0;
  1467. }
  1468. static void
  1469. myri10ge_get_drvinfo(struct net_device *netdev, struct ethtool_drvinfo *info)
  1470. {
  1471. struct myri10ge_priv *mgp = netdev_priv(netdev);
  1472. strlcpy(info->driver, "myri10ge", sizeof(info->driver));
  1473. strlcpy(info->version, MYRI10GE_VERSION_STR, sizeof(info->version));
  1474. strlcpy(info->fw_version, mgp->fw_version, sizeof(info->fw_version));
  1475. strlcpy(info->bus_info, pci_name(mgp->pdev), sizeof(info->bus_info));
  1476. }
  1477. static int
  1478. myri10ge_get_coalesce(struct net_device *netdev, struct ethtool_coalesce *coal)
  1479. {
  1480. struct myri10ge_priv *mgp = netdev_priv(netdev);
  1481. coal->rx_coalesce_usecs = mgp->intr_coal_delay;
  1482. return 0;
  1483. }
  1484. static int
  1485. myri10ge_set_coalesce(struct net_device *netdev, struct ethtool_coalesce *coal)
  1486. {
  1487. struct myri10ge_priv *mgp = netdev_priv(netdev);
  1488. mgp->intr_coal_delay = coal->rx_coalesce_usecs;
  1489. put_be32(htonl(mgp->intr_coal_delay), mgp->intr_coal_delay_ptr);
  1490. return 0;
  1491. }
  1492. static void
  1493. myri10ge_get_pauseparam(struct net_device *netdev,
  1494. struct ethtool_pauseparam *pause)
  1495. {
  1496. struct myri10ge_priv *mgp = netdev_priv(netdev);
  1497. pause->autoneg = 0;
  1498. pause->rx_pause = mgp->pause;
  1499. pause->tx_pause = mgp->pause;
  1500. }
  1501. static int
  1502. myri10ge_set_pauseparam(struct net_device *netdev,
  1503. struct ethtool_pauseparam *pause)
  1504. {
  1505. struct myri10ge_priv *mgp = netdev_priv(netdev);
  1506. if (pause->tx_pause != mgp->pause)
  1507. return myri10ge_change_pause(mgp, pause->tx_pause);
  1508. if (pause->rx_pause != mgp->pause)
  1509. return myri10ge_change_pause(mgp, pause->rx_pause);
  1510. if (pause->autoneg != 0)
  1511. return -EINVAL;
  1512. return 0;
  1513. }
  1514. static void
  1515. myri10ge_get_ringparam(struct net_device *netdev,
  1516. struct ethtool_ringparam *ring)
  1517. {
  1518. struct myri10ge_priv *mgp = netdev_priv(netdev);
  1519. ring->rx_mini_max_pending = mgp->ss[0].rx_small.mask + 1;
  1520. ring->rx_max_pending = mgp->ss[0].rx_big.mask + 1;
  1521. ring->rx_jumbo_max_pending = 0;
  1522. ring->tx_max_pending = mgp->ss[0].tx.mask + 1;
  1523. ring->rx_mini_pending = ring->rx_mini_max_pending;
  1524. ring->rx_pending = ring->rx_max_pending;
  1525. ring->rx_jumbo_pending = ring->rx_jumbo_max_pending;
  1526. ring->tx_pending = ring->tx_max_pending;
  1527. }
  1528. static u32 myri10ge_get_rx_csum(struct net_device *netdev)
  1529. {
  1530. struct myri10ge_priv *mgp = netdev_priv(netdev);
  1531. if (mgp->csum_flag)
  1532. return 1;
  1533. else
  1534. return 0;
  1535. }
  1536. static int myri10ge_set_rx_csum(struct net_device *netdev, u32 csum_enabled)
  1537. {
  1538. struct myri10ge_priv *mgp = netdev_priv(netdev);
  1539. int err = 0;
  1540. if (csum_enabled)
  1541. mgp->csum_flag = MXGEFW_FLAGS_CKSUM;
  1542. else {
  1543. netdev->features &= ~NETIF_F_LRO;
  1544. mgp->csum_flag = 0;
  1545. }
  1546. return err;
  1547. }
  1548. static int myri10ge_set_tso(struct net_device *netdev, u32 tso_enabled)
  1549. {
  1550. struct myri10ge_priv *mgp = netdev_priv(netdev);
  1551. u32 flags = mgp->features & (NETIF_F_TSO6 | NETIF_F_TSO);
  1552. if (tso_enabled)
  1553. netdev->features |= flags;
  1554. else
  1555. netdev->features &= ~flags;
  1556. return 0;
  1557. }
  1558. static const char myri10ge_gstrings_main_stats[][ETH_GSTRING_LEN] = {
  1559. "rx_packets", "tx_packets", "rx_bytes", "tx_bytes", "rx_errors",
  1560. "tx_errors", "rx_dropped", "tx_dropped", "multicast", "collisions",
  1561. "rx_length_errors", "rx_over_errors", "rx_crc_errors",
  1562. "rx_frame_errors", "rx_fifo_errors", "rx_missed_errors",
  1563. "tx_aborted_errors", "tx_carrier_errors", "tx_fifo_errors",
  1564. "tx_heartbeat_errors", "tx_window_errors",
  1565. /* device-specific stats */
  1566. "tx_boundary", "WC", "irq", "MSI", "MSIX",
  1567. "read_dma_bw_MBs", "write_dma_bw_MBs", "read_write_dma_bw_MBs",
  1568. "serial_number", "watchdog_resets",
  1569. #ifdef CONFIG_MYRI10GE_DCA
  1570. "dca_capable_firmware", "dca_device_present",
  1571. #endif
  1572. "link_changes", "link_up", "dropped_link_overflow",
  1573. "dropped_link_error_or_filtered",
  1574. "dropped_pause", "dropped_bad_phy", "dropped_bad_crc32",
  1575. "dropped_unicast_filtered", "dropped_multicast_filtered",
  1576. "dropped_runt", "dropped_overrun", "dropped_no_small_buffer",
  1577. "dropped_no_big_buffer"
  1578. };
  1579. static const char myri10ge_gstrings_slice_stats[][ETH_GSTRING_LEN] = {
  1580. "----------- slice ---------",
  1581. "tx_pkt_start", "tx_pkt_done", "tx_req", "tx_done",
  1582. "rx_small_cnt", "rx_big_cnt",
  1583. "wake_queue", "stop_queue", "tx_linearized", "LRO aggregated",
  1584. "LRO flushed",
  1585. "LRO avg aggr", "LRO no_desc"
  1586. };
  1587. #define MYRI10GE_NET_STATS_LEN 21
  1588. #define MYRI10GE_MAIN_STATS_LEN ARRAY_SIZE(myri10ge_gstrings_main_stats)
  1589. #define MYRI10GE_SLICE_STATS_LEN ARRAY_SIZE(myri10ge_gstrings_slice_stats)
  1590. static void
  1591. myri10ge_get_strings(struct net_device *netdev, u32 stringset, u8 * data)
  1592. {
  1593. struct myri10ge_priv *mgp = netdev_priv(netdev);
  1594. int i;
  1595. switch (stringset) {
  1596. case ETH_SS_STATS:
  1597. memcpy(data, *myri10ge_gstrings_main_stats,
  1598. sizeof(myri10ge_gstrings_main_stats));
  1599. data += sizeof(myri10ge_gstrings_main_stats);
  1600. for (i = 0; i < mgp->num_slices; i++) {
  1601. memcpy(data, *myri10ge_gstrings_slice_stats,
  1602. sizeof(myri10ge_gstrings_slice_stats));
  1603. data += sizeof(myri10ge_gstrings_slice_stats);
  1604. }
  1605. break;
  1606. }
  1607. }
  1608. static int myri10ge_get_sset_count(struct net_device *netdev, int sset)
  1609. {
  1610. struct myri10ge_priv *mgp = netdev_priv(netdev);
  1611. switch (sset) {
  1612. case ETH_SS_STATS:
  1613. return MYRI10GE_MAIN_STATS_LEN +
  1614. mgp->num_slices * MYRI10GE_SLICE_STATS_LEN;
  1615. default:
  1616. return -EOPNOTSUPP;
  1617. }
  1618. }
  1619. static void
  1620. myri10ge_get_ethtool_stats(struct net_device *netdev,
  1621. struct ethtool_stats *stats, u64 * data)
  1622. {
  1623. struct myri10ge_priv *mgp = netdev_priv(netdev);
  1624. struct myri10ge_slice_state *ss;
  1625. int slice;
  1626. int i;
  1627. /* force stats update */
  1628. (void)myri10ge_get_stats(netdev);
  1629. for (i = 0; i < MYRI10GE_NET_STATS_LEN; i++)
  1630. data[i] = ((unsigned long *)&netdev->stats)[i];
  1631. data[i++] = (unsigned int)mgp->tx_boundary;
  1632. data[i++] = (unsigned int)mgp->wc_enabled;
  1633. data[i++] = (unsigned int)mgp->pdev->irq;
  1634. data[i++] = (unsigned int)mgp->msi_enabled;
  1635. data[i++] = (unsigned int)mgp->msix_enabled;
  1636. data[i++] = (unsigned int)mgp->read_dma;
  1637. data[i++] = (unsigned int)mgp->write_dma;
  1638. data[i++] = (unsigned int)mgp->read_write_dma;
  1639. data[i++] = (unsigned int)mgp->serial_number;
  1640. data[i++] = (unsigned int)mgp->watchdog_resets;
  1641. #ifdef CONFIG_MYRI10GE_DCA
  1642. data[i++] = (unsigned int)(mgp->ss[0].dca_tag != NULL);
  1643. data[i++] = (unsigned int)(mgp->dca_enabled);
  1644. #endif
  1645. data[i++] = (unsigned int)mgp->link_changes;
  1646. /* firmware stats are useful only in the first slice */
  1647. ss = &mgp->ss[0];
  1648. data[i++] = (unsigned int)ntohl(ss->fw_stats->link_up);
  1649. data[i++] = (unsigned int)ntohl(ss->fw_stats->dropped_link_overflow);
  1650. data[i++] =
  1651. (unsigned int)ntohl(ss->fw_stats->dropped_link_error_or_filtered);
  1652. data[i++] = (unsigned int)ntohl(ss->fw_stats->dropped_pause);
  1653. data[i++] = (unsigned int)ntohl(ss->fw_stats->dropped_bad_phy);
  1654. data[i++] = (unsigned int)ntohl(ss->fw_stats->dropped_bad_crc32);
  1655. data[i++] = (unsigned int)ntohl(ss->fw_stats->dropped_unicast_filtered);
  1656. data[i++] =
  1657. (unsigned int)ntohl(ss->fw_stats->dropped_multicast_filtered);
  1658. data[i++] = (unsigned int)ntohl(ss->fw_stats->dropped_runt);
  1659. data[i++] = (unsigned int)ntohl(ss->fw_stats->dropped_overrun);
  1660. data[i++] = (unsigned int)ntohl(ss->fw_stats->dropped_no_small_buffer);
  1661. data[i++] = (unsigned int)ntohl(ss->fw_stats->dropped_no_big_buffer);
  1662. for (slice = 0; slice < mgp->num_slices; slice++) {
  1663. ss = &mgp->ss[slice];
  1664. data[i++] = slice;
  1665. data[i++] = (unsigned int)ss->tx.pkt_start;
  1666. data[i++] = (unsigned int)ss->tx.pkt_done;
  1667. data[i++] = (unsigned int)ss->tx.req;
  1668. data[i++] = (unsigned int)ss->tx.done;
  1669. data[i++] = (unsigned int)ss->rx_small.cnt;
  1670. data[i++] = (unsigned int)ss->rx_big.cnt;
  1671. data[i++] = (unsigned int)ss->tx.wake_queue;
  1672. data[i++] = (unsigned int)ss->tx.stop_queue;
  1673. data[i++] = (unsigned int)ss->tx.linearized;
  1674. data[i++] = ss->rx_done.lro_mgr.stats.aggregated;
  1675. data[i++] = ss->rx_done.lro_mgr.stats.flushed;
  1676. if (ss->rx_done.lro_mgr.stats.flushed)
  1677. data[i++] = ss->rx_done.lro_mgr.stats.aggregated /
  1678. ss->rx_done.lro_mgr.stats.flushed;
  1679. else
  1680. data[i++] = 0;
  1681. data[i++] = ss->rx_done.lro_mgr.stats.no_desc;
  1682. }
  1683. }
  1684. static void myri10ge_set_msglevel(struct net_device *netdev, u32 value)
  1685. {
  1686. struct myri10ge_priv *mgp = netdev_priv(netdev);
  1687. mgp->msg_enable = value;
  1688. }
  1689. static u32 myri10ge_get_msglevel(struct net_device *netdev)
  1690. {
  1691. struct myri10ge_priv *mgp = netdev_priv(netdev);
  1692. return mgp->msg_enable;
  1693. }
  1694. static int myri10ge_set_flags(struct net_device *netdev, u32 value)
  1695. {
  1696. return ethtool_op_set_flags(netdev, value, ETH_FLAG_LRO);
  1697. }
  1698. static const struct ethtool_ops myri10ge_ethtool_ops = {
  1699. .get_settings = myri10ge_get_settings,
  1700. .get_drvinfo = myri10ge_get_drvinfo,
  1701. .get_coalesce = myri10ge_get_coalesce,
  1702. .set_coalesce = myri10ge_set_coalesce,
  1703. .get_pauseparam = myri10ge_get_pauseparam,
  1704. .set_pauseparam = myri10ge_set_pauseparam,
  1705. .get_ringparam = myri10ge_get_ringparam,
  1706. .get_rx_csum = myri10ge_get_rx_csum,
  1707. .set_rx_csum = myri10ge_set_rx_csum,
  1708. .set_tx_csum = ethtool_op_set_tx_hw_csum,
  1709. .set_sg = ethtool_op_set_sg,
  1710. .set_tso = myri10ge_set_tso,
  1711. .get_link = ethtool_op_get_link,
  1712. .get_strings = myri10ge_get_strings,
  1713. .get_sset_count = myri10ge_get_sset_count,
  1714. .get_ethtool_stats = myri10ge_get_ethtool_stats,
  1715. .set_msglevel = myri10ge_set_msglevel,
  1716. .get_msglevel = myri10ge_get_msglevel,
  1717. .get_flags = ethtool_op_get_flags,
  1718. .set_flags = myri10ge_set_flags
  1719. };
  1720. static int myri10ge_allocate_rings(struct myri10ge_slice_state *ss)
  1721. {
  1722. struct myri10ge_priv *mgp = ss->mgp;
  1723. struct myri10ge_cmd cmd;
  1724. struct net_device *dev = mgp->dev;
  1725. int tx_ring_size, rx_ring_size;
  1726. int tx_ring_entries, rx_ring_entries;
  1727. int i, slice, status;
  1728. size_t bytes;
  1729. /* get ring sizes */
  1730. slice = ss - mgp->ss;
  1731. cmd.data0 = slice;
  1732. status = myri10ge_send_cmd(mgp, MXGEFW_CMD_GET_SEND_RING_SIZE, &cmd, 0);
  1733. tx_ring_size = cmd.data0;
  1734. cmd.data0 = slice;
  1735. status |= myri10ge_send_cmd(mgp, MXGEFW_CMD_GET_RX_RING_SIZE, &cmd, 0);
  1736. if (status != 0)
  1737. return status;
  1738. rx_ring_size = cmd.data0;
  1739. tx_ring_entries = tx_ring_size / sizeof(struct mcp_kreq_ether_send);
  1740. rx_ring_entries = rx_ring_size / sizeof(struct mcp_dma_addr);
  1741. ss->tx.mask = tx_ring_entries - 1;
  1742. ss->rx_small.mask = ss->rx_big.mask = rx_ring_entries - 1;
  1743. status = -ENOMEM;
  1744. /* allocate the host shadow rings */
  1745. bytes = 8 + (MYRI10GE_MAX_SEND_DESC_TSO + 4)
  1746. * sizeof(*ss->tx.req_list);
  1747. ss->tx.req_bytes = kzalloc(bytes, GFP_KERNEL);
  1748. if (ss->tx.req_bytes == NULL)
  1749. goto abort_with_nothing;
  1750. /* ensure req_list entries are aligned to 8 bytes */
  1751. ss->tx.req_list = (struct mcp_kreq_ether_send *)
  1752. ALIGN((unsigned long)ss->tx.req_bytes, 8);
  1753. ss->tx.queue_active = 0;
  1754. bytes = rx_ring_entries * sizeof(*ss->rx_small.shadow);
  1755. ss->rx_small.shadow = kzalloc(bytes, GFP_KERNEL);
  1756. if (ss->rx_small.shadow == NULL)
  1757. goto abort_with_tx_req_bytes;
  1758. bytes = rx_ring_entries * sizeof(*ss->rx_big.shadow);
  1759. ss->rx_big.shadow = kzalloc(bytes, GFP_KERNEL);
  1760. if (ss->rx_big.shadow == NULL)
  1761. goto abort_with_rx_small_shadow;
  1762. /* allocate the host info rings */
  1763. bytes = tx_ring_entries * sizeof(*ss->tx.info);
  1764. ss->tx.info = kzalloc(bytes, GFP_KERNEL);
  1765. if (ss->tx.info == NULL)
  1766. goto abort_with_rx_big_shadow;
  1767. bytes = rx_ring_entries * sizeof(*ss->rx_small.info);
  1768. ss->rx_small.info = kzalloc(bytes, GFP_KERNEL);
  1769. if (ss->rx_small.info == NULL)
  1770. goto abort_with_tx_info;
  1771. bytes = rx_ring_entries * sizeof(*ss->rx_big.info);
  1772. ss->rx_big.info = kzalloc(bytes, GFP_KERNEL);
  1773. if (ss->rx_big.info == NULL)
  1774. goto abort_with_rx_small_info;
  1775. /* Fill the receive rings */
  1776. ss->rx_big.cnt = 0;
  1777. ss->rx_small.cnt = 0;
  1778. ss->rx_big.fill_cnt = 0;
  1779. ss->rx_small.fill_cnt = 0;
  1780. ss->rx_small.page_offset = MYRI10GE_ALLOC_SIZE;
  1781. ss->rx_big.page_offset = MYRI10GE_ALLOC_SIZE;
  1782. ss->rx_small.watchdog_needed = 0;
  1783. ss->rx_big.watchdog_needed = 0;
  1784. myri10ge_alloc_rx_pages(mgp, &ss->rx_small,
  1785. mgp->small_bytes + MXGEFW_PAD, 0);
  1786. if (ss->rx_small.fill_cnt < ss->rx_small.mask + 1) {
  1787. netdev_err(dev, "slice-%d: alloced only %d small bufs\n",
  1788. slice, ss->rx_small.fill_cnt);
  1789. goto abort_with_rx_small_ring;
  1790. }
  1791. myri10ge_alloc_rx_pages(mgp, &ss->rx_big, mgp->big_bytes, 0);
  1792. if (ss->rx_big.fill_cnt < ss->rx_big.mask + 1) {
  1793. netdev_err(dev, "slice-%d: alloced only %d big bufs\n",
  1794. slice, ss->rx_big.fill_cnt);
  1795. goto abort_with_rx_big_ring;
  1796. }
  1797. return 0;
  1798. abort_with_rx_big_ring:
  1799. for (i = ss->rx_big.cnt; i < ss->rx_big.fill_cnt; i++) {
  1800. int idx = i & ss->rx_big.mask;
  1801. myri10ge_unmap_rx_page(mgp->pdev, &ss->rx_big.info[idx],
  1802. mgp->big_bytes);
  1803. put_page(ss->rx_big.info[idx].page);
  1804. }
  1805. abort_with_rx_small_ring:
  1806. for (i = ss->rx_small.cnt; i < ss->rx_small.fill_cnt; i++) {
  1807. int idx = i & ss->rx_small.mask;
  1808. myri10ge_unmap_rx_page(mgp->pdev, &ss->rx_small.info[idx],
  1809. mgp->small_bytes + MXGEFW_PAD);
  1810. put_page(ss->rx_small.info[idx].page);
  1811. }
  1812. kfree(ss->rx_big.info);
  1813. abort_with_rx_small_info:
  1814. kfree(ss->rx_small.info);
  1815. abort_with_tx_info:
  1816. kfree(ss->tx.info);
  1817. abort_with_rx_big_shadow:
  1818. kfree(ss->rx_big.shadow);
  1819. abort_with_rx_small_shadow:
  1820. kfree(ss->rx_small.shadow);
  1821. abort_with_tx_req_bytes:
  1822. kfree(ss->tx.req_bytes);
  1823. ss->tx.req_bytes = NULL;
  1824. ss->tx.req_list = NULL;
  1825. abort_with_nothing:
  1826. return status;
  1827. }
  1828. static void myri10ge_free_rings(struct myri10ge_slice_state *ss)
  1829. {
  1830. struct myri10ge_priv *mgp = ss->mgp;
  1831. struct sk_buff *skb;
  1832. struct myri10ge_tx_buf *tx;
  1833. int i, len, idx;
  1834. /* If not allocated, skip it */
  1835. if (ss->tx.req_list == NULL)
  1836. return;
  1837. for (i = ss->rx_big.cnt; i < ss->rx_big.fill_cnt; i++) {
  1838. idx = i & ss->rx_big.mask;
  1839. if (i == ss->rx_big.fill_cnt - 1)
  1840. ss->rx_big.info[idx].page_offset = MYRI10GE_ALLOC_SIZE;
  1841. myri10ge_unmap_rx_page(mgp->pdev, &ss->rx_big.info[idx],
  1842. mgp->big_bytes);
  1843. put_page(ss->rx_big.info[idx].page);
  1844. }
  1845. for (i = ss->rx_small.cnt; i < ss->rx_small.fill_cnt; i++) {
  1846. idx = i & ss->rx_small.mask;
  1847. if (i == ss->rx_small.fill_cnt - 1)
  1848. ss->rx_small.info[idx].page_offset =
  1849. MYRI10GE_ALLOC_SIZE;
  1850. myri10ge_unmap_rx_page(mgp->pdev, &ss->rx_small.info[idx],
  1851. mgp->small_bytes + MXGEFW_PAD);
  1852. put_page(ss->rx_small.info[idx].page);
  1853. }
  1854. tx = &ss->tx;
  1855. while (tx->done != tx->req) {
  1856. idx = tx->done & tx->mask;
  1857. skb = tx->info[idx].skb;
  1858. /* Mark as free */
  1859. tx->info[idx].skb = NULL;
  1860. tx->done++;
  1861. len = dma_unmap_len(&tx->info[idx], len);
  1862. dma_unmap_len_set(&tx->info[idx], len, 0);
  1863. if (skb) {
  1864. ss->stats.tx_dropped++;
  1865. dev_kfree_skb_any(skb);
  1866. if (len)
  1867. pci_unmap_single(mgp->pdev,
  1868. dma_unmap_addr(&tx->info[idx],
  1869. bus), len,
  1870. PCI_DMA_TODEVICE);
  1871. } else {
  1872. if (len)
  1873. pci_unmap_page(mgp->pdev,
  1874. dma_unmap_addr(&tx->info[idx],
  1875. bus), len,
  1876. PCI_DMA_TODEVICE);
  1877. }
  1878. }
  1879. kfree(ss->rx_big.info);
  1880. kfree(ss->rx_small.info);
  1881. kfree(ss->tx.info);
  1882. kfree(ss->rx_big.shadow);
  1883. kfree(ss->rx_small.shadow);
  1884. kfree(ss->tx.req_bytes);
  1885. ss->tx.req_bytes = NULL;
  1886. ss->tx.req_list = NULL;
  1887. }
  1888. static int myri10ge_request_irq(struct myri10ge_priv *mgp)
  1889. {
  1890. struct pci_dev *pdev = mgp->pdev;
  1891. struct myri10ge_slice_state *ss;
  1892. struct net_device *netdev = mgp->dev;
  1893. int i;
  1894. int status;
  1895. mgp->msi_enabled = 0;
  1896. mgp->msix_enabled = 0;
  1897. status = 0;
  1898. if (myri10ge_msi) {
  1899. if (mgp->num_slices > 1) {
  1900. status =
  1901. pci_enable_msix(pdev, mgp->msix_vectors,
  1902. mgp->num_slices);
  1903. if (status == 0) {
  1904. mgp->msix_enabled = 1;
  1905. } else {
  1906. dev_err(&pdev->dev,
  1907. "Error %d setting up MSI-X\n", status);
  1908. return status;
  1909. }
  1910. }
  1911. if (mgp->msix_enabled == 0) {
  1912. status = pci_enable_msi(pdev);
  1913. if (status != 0) {
  1914. dev_err(&pdev->dev,
  1915. "Error %d setting up MSI; falling back to xPIC\n",
  1916. status);
  1917. } else {
  1918. mgp->msi_enabled = 1;
  1919. }
  1920. }
  1921. }
  1922. if (mgp->msix_enabled) {
  1923. for (i = 0; i < mgp->num_slices; i++) {
  1924. ss = &mgp->ss[i];
  1925. snprintf(ss->irq_desc, sizeof(ss->irq_desc),
  1926. "%s:slice-%d", netdev->name, i);
  1927. status = request_irq(mgp->msix_vectors[i].vector,
  1928. myri10ge_intr, 0, ss->irq_desc,
  1929. ss);
  1930. if (status != 0) {
  1931. dev_err(&pdev->dev,
  1932. "slice %d failed to allocate IRQ\n", i);
  1933. i--;
  1934. while (i >= 0) {
  1935. free_irq(mgp->msix_vectors[i].vector,
  1936. &mgp->ss[i]);
  1937. i--;
  1938. }
  1939. pci_disable_msix(pdev);
  1940. return status;
  1941. }
  1942. }
  1943. } else {
  1944. status = request_irq(pdev->irq, myri10ge_intr, IRQF_SHARED,
  1945. mgp->dev->name, &mgp->ss[0]);
  1946. if (status != 0) {
  1947. dev_err(&pdev->dev, "failed to allocate IRQ\n");
  1948. if (mgp->msi_enabled)
  1949. pci_disable_msi(pdev);
  1950. }
  1951. }
  1952. return status;
  1953. }
  1954. static void myri10ge_free_irq(struct myri10ge_priv *mgp)
  1955. {
  1956. struct pci_dev *pdev = mgp->pdev;
  1957. int i;
  1958. if (mgp->msix_enabled) {
  1959. for (i = 0; i < mgp->num_slices; i++)
  1960. free_irq(mgp->msix_vectors[i].vector, &mgp->ss[i]);
  1961. } else {
  1962. free_irq(pdev->irq, &mgp->ss[0]);
  1963. }
  1964. if (mgp->msi_enabled)
  1965. pci_disable_msi(pdev);
  1966. if (mgp->msix_enabled)
  1967. pci_disable_msix(pdev);
  1968. }
  1969. static int
  1970. myri10ge_get_frag_header(struct skb_frag_struct *frag, void **mac_hdr,
  1971. void **ip_hdr, void **tcpudp_hdr,
  1972. u64 * hdr_flags, void *priv)
  1973. {
  1974. struct ethhdr *eh;
  1975. struct vlan_ethhdr *veh;
  1976. struct iphdr *iph;
  1977. u8 *va = page_address(frag->page) + frag->page_offset;
  1978. unsigned long ll_hlen;
  1979. /* passed opaque through lro_receive_frags() */
  1980. __wsum csum = (__force __wsum) (unsigned long)priv;
  1981. /* find the mac header, aborting if not IPv4 */
  1982. eh = (struct ethhdr *)va;
  1983. *mac_hdr = eh;
  1984. ll_hlen = ETH_HLEN;
  1985. if (eh->h_proto != htons(ETH_P_IP)) {
  1986. if (eh->h_proto == htons(ETH_P_8021Q)) {
  1987. veh = (struct vlan_ethhdr *)va;
  1988. if (veh->h_vlan_encapsulated_proto != htons(ETH_P_IP))
  1989. return -1;
  1990. ll_hlen += VLAN_HLEN;
  1991. /*
  1992. * HW checksum starts ETH_HLEN bytes into
  1993. * frame, so we must subtract off the VLAN
  1994. * header's checksum before csum can be used
  1995. */
  1996. csum = csum_sub(csum, csum_partial(va + ETH_HLEN,
  1997. VLAN_HLEN, 0));
  1998. } else {
  1999. return -1;
  2000. }
  2001. }
  2002. *hdr_flags = LRO_IPV4;
  2003. iph = (struct iphdr *)(va + ll_hlen);
  2004. *ip_hdr = iph;
  2005. if (iph->protocol != IPPROTO_TCP)
  2006. return -1;
  2007. if (iph->frag_off & htons(IP_MF | IP_OFFSET))
  2008. return -1;
  2009. *hdr_flags |= LRO_TCP;
  2010. *tcpudp_hdr = (u8 *) (*ip_hdr) + (iph->ihl << 2);
  2011. /* verify the IP checksum */
  2012. if (unlikely(ip_fast_csum((u8 *) iph, iph->ihl)))
  2013. return -1;
  2014. /* verify the checksum */
  2015. if (unlikely(csum_tcpudp_magic(iph->saddr, iph->daddr,
  2016. ntohs(iph->tot_len) - (iph->ihl << 2),
  2017. IPPROTO_TCP, csum)))
  2018. return -1;
  2019. return 0;
  2020. }
  2021. static int myri10ge_get_txrx(struct myri10ge_priv *mgp, int slice)
  2022. {
  2023. struct myri10ge_cmd cmd;
  2024. struct myri10ge_slice_state *ss;
  2025. int status;
  2026. ss = &mgp->ss[slice];
  2027. status = 0;
  2028. if (slice == 0 || (mgp->dev->real_num_tx_queues > 1)) {
  2029. cmd.data0 = slice;
  2030. status = myri10ge_send_cmd(mgp, MXGEFW_CMD_GET_SEND_OFFSET,
  2031. &cmd, 0);
  2032. ss->tx.lanai = (struct mcp_kreq_ether_send __iomem *)
  2033. (mgp->sram + cmd.data0);
  2034. }
  2035. cmd.data0 = slice;
  2036. status |= myri10ge_send_cmd(mgp, MXGEFW_CMD_GET_SMALL_RX_OFFSET,
  2037. &cmd, 0);
  2038. ss->rx_small.lanai = (struct mcp_kreq_ether_recv __iomem *)
  2039. (mgp->sram + cmd.data0);
  2040. cmd.data0 = slice;
  2041. status |= myri10ge_send_cmd(mgp, MXGEFW_CMD_GET_BIG_RX_OFFSET, &cmd, 0);
  2042. ss->rx_big.lanai = (struct mcp_kreq_ether_recv __iomem *)
  2043. (mgp->sram + cmd.data0);
  2044. ss->tx.send_go = (__iomem __be32 *)
  2045. (mgp->sram + MXGEFW_ETH_SEND_GO + 64 * slice);
  2046. ss->tx.send_stop = (__iomem __be32 *)
  2047. (mgp->sram + MXGEFW_ETH_SEND_STOP + 64 * slice);
  2048. return status;
  2049. }
  2050. static int myri10ge_set_stats(struct myri10ge_priv *mgp, int slice)
  2051. {
  2052. struct myri10ge_cmd cmd;
  2053. struct myri10ge_slice_state *ss;
  2054. int status;
  2055. ss = &mgp->ss[slice];
  2056. cmd.data0 = MYRI10GE_LOWPART_TO_U32(ss->fw_stats_bus);
  2057. cmd.data1 = MYRI10GE_HIGHPART_TO_U32(ss->fw_stats_bus);
  2058. cmd.data2 = sizeof(struct mcp_irq_data) | (slice << 16);
  2059. status = myri10ge_send_cmd(mgp, MXGEFW_CMD_SET_STATS_DMA_V2, &cmd, 0);
  2060. if (status == -ENOSYS) {
  2061. dma_addr_t bus = ss->fw_stats_bus;
  2062. if (slice != 0)
  2063. return -EINVAL;
  2064. bus += offsetof(struct mcp_irq_data, send_done_count);
  2065. cmd.data0 = MYRI10GE_LOWPART_TO_U32(bus);
  2066. cmd.data1 = MYRI10GE_HIGHPART_TO_U32(bus);
  2067. status = myri10ge_send_cmd(mgp,
  2068. MXGEFW_CMD_SET_STATS_DMA_OBSOLETE,
  2069. &cmd, 0);
  2070. /* Firmware cannot support multicast without STATS_DMA_V2 */
  2071. mgp->fw_multicast_support = 0;
  2072. } else {
  2073. mgp->fw_multicast_support = 1;
  2074. }
  2075. return 0;
  2076. }
  2077. static int myri10ge_open(struct net_device *dev)
  2078. {
  2079. struct myri10ge_slice_state *ss;
  2080. struct myri10ge_priv *mgp = netdev_priv(dev);
  2081. struct myri10ge_cmd cmd;
  2082. int i, status, big_pow2, slice;
  2083. u8 *itable;
  2084. struct net_lro_mgr *lro_mgr;
  2085. if (mgp->running != MYRI10GE_ETH_STOPPED)
  2086. return -EBUSY;
  2087. mgp->running = MYRI10GE_ETH_STARTING;
  2088. status = myri10ge_reset(mgp);
  2089. if (status != 0) {
  2090. netdev_err(dev, "failed reset\n");
  2091. goto abort_with_nothing;
  2092. }
  2093. if (mgp->num_slices > 1) {
  2094. cmd.data0 = mgp->num_slices;
  2095. cmd.data1 = MXGEFW_SLICE_INTR_MODE_ONE_PER_SLICE;
  2096. if (mgp->dev->real_num_tx_queues > 1)
  2097. cmd.data1 |= MXGEFW_SLICE_ENABLE_MULTIPLE_TX_QUEUES;
  2098. status = myri10ge_send_cmd(mgp, MXGEFW_CMD_ENABLE_RSS_QUEUES,
  2099. &cmd, 0);
  2100. if (status != 0) {
  2101. netdev_err(dev, "failed to set number of slices\n");
  2102. goto abort_with_nothing;
  2103. }
  2104. /* setup the indirection table */
  2105. cmd.data0 = mgp->num_slices;
  2106. status = myri10ge_send_cmd(mgp, MXGEFW_CMD_SET_RSS_TABLE_SIZE,
  2107. &cmd, 0);
  2108. status |= myri10ge_send_cmd(mgp,
  2109. MXGEFW_CMD_GET_RSS_TABLE_OFFSET,
  2110. &cmd, 0);
  2111. if (status != 0) {
  2112. netdev_err(dev, "failed to setup rss tables\n");
  2113. goto abort_with_nothing;
  2114. }
  2115. /* just enable an identity mapping */
  2116. itable = mgp->sram + cmd.data0;
  2117. for (i = 0; i < mgp->num_slices; i++)
  2118. __raw_writeb(i, &itable[i]);
  2119. cmd.data0 = 1;
  2120. cmd.data1 = myri10ge_rss_hash;
  2121. status = myri10ge_send_cmd(mgp, MXGEFW_CMD_SET_RSS_ENABLE,
  2122. &cmd, 0);
  2123. if (status != 0) {
  2124. netdev_err(dev, "failed to enable slices\n");
  2125. goto abort_with_nothing;
  2126. }
  2127. }
  2128. status = myri10ge_request_irq(mgp);
  2129. if (status != 0)
  2130. goto abort_with_nothing;
  2131. /* decide what small buffer size to use. For good TCP rx
  2132. * performance, it is important to not receive 1514 byte
  2133. * frames into jumbo buffers, as it confuses the socket buffer
  2134. * accounting code, leading to drops and erratic performance.
  2135. */
  2136. if (dev->mtu <= ETH_DATA_LEN)
  2137. /* enough for a TCP header */
  2138. mgp->small_bytes = (128 > SMP_CACHE_BYTES)
  2139. ? (128 - MXGEFW_PAD)
  2140. : (SMP_CACHE_BYTES - MXGEFW_PAD);
  2141. else
  2142. /* enough for a vlan encapsulated ETH_DATA_LEN frame */
  2143. mgp->small_bytes = VLAN_ETH_FRAME_LEN;
  2144. /* Override the small buffer size? */
  2145. if (myri10ge_small_bytes > 0)
  2146. mgp->small_bytes = myri10ge_small_bytes;
  2147. /* Firmware needs the big buff size as a power of 2. Lie and
  2148. * tell him the buffer is larger, because we only use 1
  2149. * buffer/pkt, and the mtu will prevent overruns.
  2150. */
  2151. big_pow2 = dev->mtu + ETH_HLEN + VLAN_HLEN + MXGEFW_PAD;
  2152. if (big_pow2 < MYRI10GE_ALLOC_SIZE / 2) {
  2153. while (!is_power_of_2(big_pow2))
  2154. big_pow2++;
  2155. mgp->big_bytes = dev->mtu + ETH_HLEN + VLAN_HLEN + MXGEFW_PAD;
  2156. } else {
  2157. big_pow2 = MYRI10GE_ALLOC_SIZE;
  2158. mgp->big_bytes = big_pow2;
  2159. }
  2160. /* setup the per-slice data structures */
  2161. for (slice = 0; slice < mgp->num_slices; slice++) {
  2162. ss = &mgp->ss[slice];
  2163. status = myri10ge_get_txrx(mgp, slice);
  2164. if (status != 0) {
  2165. netdev_err(dev, "failed to get ring sizes or locations\n");
  2166. goto abort_with_rings;
  2167. }
  2168. status = myri10ge_allocate_rings(ss);
  2169. if (status != 0)
  2170. goto abort_with_rings;
  2171. /* only firmware which supports multiple TX queues
  2172. * supports setting up the tx stats on non-zero
  2173. * slices */
  2174. if (slice == 0 || mgp->dev->real_num_tx_queues > 1)
  2175. status = myri10ge_set_stats(mgp, slice);
  2176. if (status) {
  2177. netdev_err(dev, "Couldn't set stats DMA\n");
  2178. goto abort_with_rings;
  2179. }
  2180. lro_mgr = &ss->rx_done.lro_mgr;
  2181. lro_mgr->dev = dev;
  2182. lro_mgr->features = LRO_F_NAPI;
  2183. lro_mgr->ip_summed = CHECKSUM_COMPLETE;
  2184. lro_mgr->ip_summed_aggr = CHECKSUM_UNNECESSARY;
  2185. lro_mgr->max_desc = MYRI10GE_MAX_LRO_DESCRIPTORS;
  2186. lro_mgr->lro_arr = ss->rx_done.lro_desc;
  2187. lro_mgr->get_frag_header = myri10ge_get_frag_header;
  2188. lro_mgr->max_aggr = myri10ge_lro_max_pkts;
  2189. lro_mgr->frag_align_pad = 2;
  2190. if (lro_mgr->max_aggr > MAX_SKB_FRAGS)
  2191. lro_mgr->max_aggr = MAX_SKB_FRAGS;
  2192. /* must happen prior to any irq */
  2193. napi_enable(&(ss)->napi);
  2194. }
  2195. /* now give firmware buffers sizes, and MTU */
  2196. cmd.data0 = dev->mtu + ETH_HLEN + VLAN_HLEN;
  2197. status = myri10ge_send_cmd(mgp, MXGEFW_CMD_SET_MTU, &cmd, 0);
  2198. cmd.data0 = mgp->small_bytes;
  2199. status |=
  2200. myri10ge_send_cmd(mgp, MXGEFW_CMD_SET_SMALL_BUFFER_SIZE, &cmd, 0);
  2201. cmd.data0 = big_pow2;
  2202. status |=
  2203. myri10ge_send_cmd(mgp, MXGEFW_CMD_SET_BIG_BUFFER_SIZE, &cmd, 0);
  2204. if (status) {
  2205. netdev_err(dev, "Couldn't set buffer sizes\n");
  2206. goto abort_with_rings;
  2207. }
  2208. /*
  2209. * Set Linux style TSO mode; this is needed only on newer
  2210. * firmware versions. Older versions default to Linux
  2211. * style TSO
  2212. */
  2213. cmd.data0 = 0;
  2214. status = myri10ge_send_cmd(mgp, MXGEFW_CMD_SET_TSO_MODE, &cmd, 0);
  2215. if (status && status != -ENOSYS) {
  2216. netdev_err(dev, "Couldn't set TSO mode\n");
  2217. goto abort_with_rings;
  2218. }
  2219. mgp->link_state = ~0U;
  2220. mgp->rdma_tags_available = 15;
  2221. status = myri10ge_send_cmd(mgp, MXGEFW_CMD_ETHERNET_UP, &cmd, 0);
  2222. if (status) {
  2223. netdev_err(dev, "Couldn't bring up link\n");
  2224. goto abort_with_rings;
  2225. }
  2226. mgp->running = MYRI10GE_ETH_RUNNING;
  2227. mgp->watchdog_timer.expires = jiffies + myri10ge_watchdog_timeout * HZ;
  2228. add_timer(&mgp->watchdog_timer);
  2229. netif_tx_wake_all_queues(dev);
  2230. return 0;
  2231. abort_with_rings:
  2232. while (slice) {
  2233. slice--;
  2234. napi_disable(&mgp->ss[slice].napi);
  2235. }
  2236. for (i = 0; i < mgp->num_slices; i++)
  2237. myri10ge_free_rings(&mgp->ss[i]);
  2238. myri10ge_free_irq(mgp);
  2239. abort_with_nothing:
  2240. mgp->running = MYRI10GE_ETH_STOPPED;
  2241. return -ENOMEM;
  2242. }
  2243. static int myri10ge_close(struct net_device *dev)
  2244. {
  2245. struct myri10ge_priv *mgp = netdev_priv(dev);
  2246. struct myri10ge_cmd cmd;
  2247. int status, old_down_cnt;
  2248. int i;
  2249. if (mgp->running != MYRI10GE_ETH_RUNNING)
  2250. return 0;
  2251. if (mgp->ss[0].tx.req_bytes == NULL)
  2252. return 0;
  2253. del_timer_sync(&mgp->watchdog_timer);
  2254. mgp->running = MYRI10GE_ETH_STOPPING;
  2255. for (i = 0; i < mgp->num_slices; i++) {
  2256. napi_disable(&mgp->ss[i].napi);
  2257. }
  2258. netif_carrier_off(dev);
  2259. netif_tx_stop_all_queues(dev);
  2260. if (mgp->rebooted == 0) {
  2261. old_down_cnt = mgp->down_cnt;
  2262. mb();
  2263. status =
  2264. myri10ge_send_cmd(mgp, MXGEFW_CMD_ETHERNET_DOWN, &cmd, 0);
  2265. if (status)
  2266. netdev_err(dev, "Couldn't bring down link\n");
  2267. wait_event_timeout(mgp->down_wq, old_down_cnt != mgp->down_cnt,
  2268. HZ);
  2269. if (old_down_cnt == mgp->down_cnt)
  2270. netdev_err(dev, "never got down irq\n");
  2271. }
  2272. netif_tx_disable(dev);
  2273. myri10ge_free_irq(mgp);
  2274. for (i = 0; i < mgp->num_slices; i++)
  2275. myri10ge_free_rings(&mgp->ss[i]);
  2276. mgp->running = MYRI10GE_ETH_STOPPED;
  2277. return 0;
  2278. }
  2279. /* copy an array of struct mcp_kreq_ether_send's to the mcp. Copy
  2280. * backwards one at a time and handle ring wraps */
  2281. static inline void
  2282. myri10ge_submit_req_backwards(struct myri10ge_tx_buf *tx,
  2283. struct mcp_kreq_ether_send *src, int cnt)
  2284. {
  2285. int idx, starting_slot;
  2286. starting_slot = tx->req;
  2287. while (cnt > 1) {
  2288. cnt--;
  2289. idx = (starting_slot + cnt) & tx->mask;
  2290. myri10ge_pio_copy(&tx->lanai[idx], &src[cnt], sizeof(*src));
  2291. mb();
  2292. }
  2293. }
  2294. /*
  2295. * copy an array of struct mcp_kreq_ether_send's to the mcp. Copy
  2296. * at most 32 bytes at a time, so as to avoid involving the software
  2297. * pio handler in the nic. We re-write the first segment's flags
  2298. * to mark them valid only after writing the entire chain.
  2299. */
  2300. static inline void
  2301. myri10ge_submit_req(struct myri10ge_tx_buf *tx, struct mcp_kreq_ether_send *src,
  2302. int cnt)
  2303. {
  2304. int idx, i;
  2305. struct mcp_kreq_ether_send __iomem *dstp, *dst;
  2306. struct mcp_kreq_ether_send *srcp;
  2307. u8 last_flags;
  2308. idx = tx->req & tx->mask;
  2309. last_flags = src->flags;
  2310. src->flags = 0;
  2311. mb();
  2312. dst = dstp = &tx->lanai[idx];
  2313. srcp = src;
  2314. if ((idx + cnt) < tx->mask) {
  2315. for (i = 0; i < (cnt - 1); i += 2) {
  2316. myri10ge_pio_copy(dstp, srcp, 2 * sizeof(*src));
  2317. mb(); /* force write every 32 bytes */
  2318. srcp += 2;
  2319. dstp += 2;
  2320. }
  2321. } else {
  2322. /* submit all but the first request, and ensure
  2323. * that it is submitted below */
  2324. myri10ge_submit_req_backwards(tx, src, cnt);
  2325. i = 0;
  2326. }
  2327. if (i < cnt) {
  2328. /* submit the first request */
  2329. myri10ge_pio_copy(dstp, srcp, sizeof(*src));
  2330. mb(); /* barrier before setting valid flag */
  2331. }
  2332. /* re-write the last 32-bits with the valid flags */
  2333. src->flags = last_flags;
  2334. put_be32(*((__be32 *) src + 3), (__be32 __iomem *) dst + 3);
  2335. tx->req += cnt;
  2336. mb();
  2337. }
  2338. /*
  2339. * Transmit a packet. We need to split the packet so that a single
  2340. * segment does not cross myri10ge->tx_boundary, so this makes segment
  2341. * counting tricky. So rather than try to count segments up front, we
  2342. * just give up if there are too few segments to hold a reasonably
  2343. * fragmented packet currently available. If we run
  2344. * out of segments while preparing a packet for DMA, we just linearize
  2345. * it and try again.
  2346. */
  2347. static netdev_tx_t myri10ge_xmit(struct sk_buff *skb,
  2348. struct net_device *dev)
  2349. {
  2350. struct myri10ge_priv *mgp = netdev_priv(dev);
  2351. struct myri10ge_slice_state *ss;
  2352. struct mcp_kreq_ether_send *req;
  2353. struct myri10ge_tx_buf *tx;
  2354. struct skb_frag_struct *frag;
  2355. struct netdev_queue *netdev_queue;
  2356. dma_addr_t bus;
  2357. u32 low;
  2358. __be32 high_swapped;
  2359. unsigned int len;
  2360. int idx, last_idx, avail, frag_cnt, frag_idx, count, mss, max_segments;
  2361. u16 pseudo_hdr_offset, cksum_offset, queue;
  2362. int cum_len, seglen, boundary, rdma_count;
  2363. u8 flags, odd_flag;
  2364. queue = skb_get_queue_mapping(skb);
  2365. ss = &mgp->ss[queue];
  2366. netdev_queue = netdev_get_tx_queue(mgp->dev, queue);
  2367. tx = &ss->tx;
  2368. again:
  2369. req = tx->req_list;
  2370. avail = tx->mask - 1 - (tx->req - tx->done);
  2371. mss = 0;
  2372. max_segments = MXGEFW_MAX_SEND_DESC;
  2373. if (skb_is_gso(skb)) {
  2374. mss = skb_shinfo(skb)->gso_size;
  2375. max_segments = MYRI10GE_MAX_SEND_DESC_TSO;
  2376. }
  2377. if ((unlikely(avail < max_segments))) {
  2378. /* we are out of transmit resources */
  2379. tx->stop_queue++;
  2380. netif_tx_stop_queue(netdev_queue);
  2381. return NETDEV_TX_BUSY;
  2382. }
  2383. /* Setup checksum offloading, if needed */
  2384. cksum_offset = 0;
  2385. pseudo_hdr_offset = 0;
  2386. odd_flag = 0;
  2387. flags = (MXGEFW_FLAGS_NO_TSO | MXGEFW_FLAGS_FIRST);
  2388. if (likely(skb->ip_summed == CHECKSUM_PARTIAL)) {
  2389. cksum_offset = skb_checksum_start_offset(skb);
  2390. pseudo_hdr_offset = cksum_offset + skb->csum_offset;
  2391. /* If the headers are excessively large, then we must
  2392. * fall back to a software checksum */
  2393. if (unlikely(!mss && (cksum_offset > 255 ||
  2394. pseudo_hdr_offset > 127))) {
  2395. if (skb_checksum_help(skb))
  2396. goto drop;
  2397. cksum_offset = 0;
  2398. pseudo_hdr_offset = 0;
  2399. } else {
  2400. odd_flag = MXGEFW_FLAGS_ALIGN_ODD;
  2401. flags |= MXGEFW_FLAGS_CKSUM;
  2402. }
  2403. }
  2404. cum_len = 0;
  2405. if (mss) { /* TSO */
  2406. /* this removes any CKSUM flag from before */
  2407. flags = (MXGEFW_FLAGS_TSO_HDR | MXGEFW_FLAGS_FIRST);
  2408. /* negative cum_len signifies to the
  2409. * send loop that we are still in the
  2410. * header portion of the TSO packet.
  2411. * TSO header can be at most 1KB long */
  2412. cum_len = -(skb_transport_offset(skb) + tcp_hdrlen(skb));
  2413. /* for IPv6 TSO, the checksum offset stores the
  2414. * TCP header length, to save the firmware from
  2415. * the need to parse the headers */
  2416. if (skb_is_gso_v6(skb)) {
  2417. cksum_offset = tcp_hdrlen(skb);
  2418. /* Can only handle headers <= max_tso6 long */
  2419. if (unlikely(-cum_len > mgp->max_tso6))
  2420. return myri10ge_sw_tso(skb, dev);
  2421. }
  2422. /* for TSO, pseudo_hdr_offset holds mss.
  2423. * The firmware figures out where to put
  2424. * the checksum by parsing the header. */
  2425. pseudo_hdr_offset = mss;
  2426. } else
  2427. /* Mark small packets, and pad out tiny packets */
  2428. if (skb->len <= MXGEFW_SEND_SMALL_SIZE) {
  2429. flags |= MXGEFW_FLAGS_SMALL;
  2430. /* pad frames to at least ETH_ZLEN bytes */
  2431. if (unlikely(skb->len < ETH_ZLEN)) {
  2432. if (skb_padto(skb, ETH_ZLEN)) {
  2433. /* The packet is gone, so we must
  2434. * return 0 */
  2435. ss->stats.tx_dropped += 1;
  2436. return NETDEV_TX_OK;
  2437. }
  2438. /* adjust the len to account for the zero pad
  2439. * so that the nic can know how long it is */
  2440. skb->len = ETH_ZLEN;
  2441. }
  2442. }
  2443. /* map the skb for DMA */
  2444. len = skb_headlen(skb);
  2445. idx = tx->req & tx->mask;
  2446. tx->info[idx].skb = skb;
  2447. bus = pci_map_single(mgp->pdev, skb->data, len, PCI_DMA_TODEVICE);
  2448. dma_unmap_addr_set(&tx->info[idx], bus, bus);
  2449. dma_unmap_len_set(&tx->info[idx], len, len);
  2450. frag_cnt = skb_shinfo(skb)->nr_frags;
  2451. frag_idx = 0;
  2452. count = 0;
  2453. rdma_count = 0;
  2454. /* "rdma_count" is the number of RDMAs belonging to the
  2455. * current packet BEFORE the current send request. For
  2456. * non-TSO packets, this is equal to "count".
  2457. * For TSO packets, rdma_count needs to be reset
  2458. * to 0 after a segment cut.
  2459. *
  2460. * The rdma_count field of the send request is
  2461. * the number of RDMAs of the packet starting at
  2462. * that request. For TSO send requests with one ore more cuts
  2463. * in the middle, this is the number of RDMAs starting
  2464. * after the last cut in the request. All previous
  2465. * segments before the last cut implicitly have 1 RDMA.
  2466. *
  2467. * Since the number of RDMAs is not known beforehand,
  2468. * it must be filled-in retroactively - after each
  2469. * segmentation cut or at the end of the entire packet.
  2470. */
  2471. while (1) {
  2472. /* Break the SKB or Fragment up into pieces which
  2473. * do not cross mgp->tx_boundary */
  2474. low = MYRI10GE_LOWPART_TO_U32(bus);
  2475. high_swapped = htonl(MYRI10GE_HIGHPART_TO_U32(bus));
  2476. while (len) {
  2477. u8 flags_next;
  2478. int cum_len_next;
  2479. if (unlikely(count == max_segments))
  2480. goto abort_linearize;
  2481. boundary =
  2482. (low + mgp->tx_boundary) & ~(mgp->tx_boundary - 1);
  2483. seglen = boundary - low;
  2484. if (seglen > len)
  2485. seglen = len;
  2486. flags_next = flags & ~MXGEFW_FLAGS_FIRST;
  2487. cum_len_next = cum_len + seglen;
  2488. if (mss) { /* TSO */
  2489. (req - rdma_count)->rdma_count = rdma_count + 1;
  2490. if (likely(cum_len >= 0)) { /* payload */
  2491. int next_is_first, chop;
  2492. chop = (cum_len_next > mss);
  2493. cum_len_next = cum_len_next % mss;
  2494. next_is_first = (cum_len_next == 0);
  2495. flags |= chop * MXGEFW_FLAGS_TSO_CHOP;
  2496. flags_next |= next_is_first *
  2497. MXGEFW_FLAGS_FIRST;
  2498. rdma_count |= -(chop | next_is_first);
  2499. rdma_count += chop & !next_is_first;
  2500. } else if (likely(cum_len_next >= 0)) { /* header ends */
  2501. int small;
  2502. rdma_count = -1;
  2503. cum_len_next = 0;
  2504. seglen = -cum_len;
  2505. small = (mss <= MXGEFW_SEND_SMALL_SIZE);
  2506. flags_next = MXGEFW_FLAGS_TSO_PLD |
  2507. MXGEFW_FLAGS_FIRST |
  2508. (small * MXGEFW_FLAGS_SMALL);
  2509. }
  2510. }
  2511. req->addr_high = high_swapped;
  2512. req->addr_low = htonl(low);
  2513. req->pseudo_hdr_offset = htons(pseudo_hdr_offset);
  2514. req->pad = 0; /* complete solid 16-byte block; does this matter? */
  2515. req->rdma_count = 1;
  2516. req->length = htons(seglen);
  2517. req->cksum_offset = cksum_offset;
  2518. req->flags = flags | ((cum_len & 1) * odd_flag);
  2519. low += seglen;
  2520. len -= seglen;
  2521. cum_len = cum_len_next;
  2522. flags = flags_next;
  2523. req++;
  2524. count++;
  2525. rdma_count++;
  2526. if (cksum_offset != 0 && !(mss && skb_is_gso_v6(skb))) {
  2527. if (unlikely(cksum_offset > seglen))
  2528. cksum_offset -= seglen;
  2529. else
  2530. cksum_offset = 0;
  2531. }
  2532. }
  2533. if (frag_idx == frag_cnt)
  2534. break;
  2535. /* map next fragment for DMA */
  2536. idx = (count + tx->req) & tx->mask;
  2537. frag = &skb_shinfo(skb)->frags[frag_idx];
  2538. frag_idx++;
  2539. len = frag->size;
  2540. bus = pci_map_page(mgp->pdev, frag->page, frag->page_offset,
  2541. len, PCI_DMA_TODEVICE);
  2542. dma_unmap_addr_set(&tx->info[idx], bus, bus);
  2543. dma_unmap_len_set(&tx->info[idx], len, len);
  2544. }
  2545. (req - rdma_count)->rdma_count = rdma_count;
  2546. if (mss)
  2547. do {
  2548. req--;
  2549. req->flags |= MXGEFW_FLAGS_TSO_LAST;
  2550. } while (!(req->flags & (MXGEFW_FLAGS_TSO_CHOP |
  2551. MXGEFW_FLAGS_FIRST)));
  2552. idx = ((count - 1) + tx->req) & tx->mask;
  2553. tx->info[idx].last = 1;
  2554. myri10ge_submit_req(tx, tx->req_list, count);
  2555. /* if using multiple tx queues, make sure NIC polls the
  2556. * current slice */
  2557. if ((mgp->dev->real_num_tx_queues > 1) && tx->queue_active == 0) {
  2558. tx->queue_active = 1;
  2559. put_be32(htonl(1), tx->send_go);
  2560. mb();
  2561. mmiowb();
  2562. }
  2563. tx->pkt_start++;
  2564. if ((avail - count) < MXGEFW_MAX_SEND_DESC) {
  2565. tx->stop_queue++;
  2566. netif_tx_stop_queue(netdev_queue);
  2567. }
  2568. return NETDEV_TX_OK;
  2569. abort_linearize:
  2570. /* Free any DMA resources we've alloced and clear out the skb
  2571. * slot so as to not trip up assertions, and to avoid a
  2572. * double-free if linearizing fails */
  2573. last_idx = (idx + 1) & tx->mask;
  2574. idx = tx->req & tx->mask;
  2575. tx->info[idx].skb = NULL;
  2576. do {
  2577. len = dma_unmap_len(&tx->info[idx], len);
  2578. if (len) {
  2579. if (tx->info[idx].skb != NULL)
  2580. pci_unmap_single(mgp->pdev,
  2581. dma_unmap_addr(&tx->info[idx],
  2582. bus), len,
  2583. PCI_DMA_TODEVICE);
  2584. else
  2585. pci_unmap_page(mgp->pdev,
  2586. dma_unmap_addr(&tx->info[idx],
  2587. bus), len,
  2588. PCI_DMA_TODEVICE);
  2589. dma_unmap_len_set(&tx->info[idx], len, 0);
  2590. tx->info[idx].skb = NULL;
  2591. }
  2592. idx = (idx + 1) & tx->mask;
  2593. } while (idx != last_idx);
  2594. if (skb_is_gso(skb)) {
  2595. netdev_err(mgp->dev, "TSO but wanted to linearize?!?!?\n");
  2596. goto drop;
  2597. }
  2598. if (skb_linearize(skb))
  2599. goto drop;
  2600. tx->linearized++;
  2601. goto again;
  2602. drop:
  2603. dev_kfree_skb_any(skb);
  2604. ss->stats.tx_dropped += 1;
  2605. return NETDEV_TX_OK;
  2606. }
  2607. static netdev_tx_t myri10ge_sw_tso(struct sk_buff *skb,
  2608. struct net_device *dev)
  2609. {
  2610. struct sk_buff *segs, *curr;
  2611. struct myri10ge_priv *mgp = netdev_priv(dev);
  2612. struct myri10ge_slice_state *ss;
  2613. netdev_tx_t status;
  2614. segs = skb_gso_segment(skb, dev->features & ~NETIF_F_TSO6);
  2615. if (IS_ERR(segs))
  2616. goto drop;
  2617. while (segs) {
  2618. curr = segs;
  2619. segs = segs->next;
  2620. curr->next = NULL;
  2621. status = myri10ge_xmit(curr, dev);
  2622. if (status != 0) {
  2623. dev_kfree_skb_any(curr);
  2624. if (segs != NULL) {
  2625. curr = segs;
  2626. segs = segs->next;
  2627. curr->next = NULL;
  2628. dev_kfree_skb_any(segs);
  2629. }
  2630. goto drop;
  2631. }
  2632. }
  2633. dev_kfree_skb_any(skb);
  2634. return NETDEV_TX_OK;
  2635. drop:
  2636. ss = &mgp->ss[skb_get_queue_mapping(skb)];
  2637. dev_kfree_skb_any(skb);
  2638. ss->stats.tx_dropped += 1;
  2639. return NETDEV_TX_OK;
  2640. }
  2641. static struct net_device_stats *myri10ge_get_stats(struct net_device *dev)
  2642. {
  2643. struct myri10ge_priv *mgp = netdev_priv(dev);
  2644. struct myri10ge_slice_netstats *slice_stats;
  2645. struct net_device_stats *stats = &dev->stats;
  2646. int i;
  2647. spin_lock(&mgp->stats_lock);
  2648. memset(stats, 0, sizeof(*stats));
  2649. for (i = 0; i < mgp->num_slices; i++) {
  2650. slice_stats = &mgp->ss[i].stats;
  2651. stats->rx_packets += slice_stats->rx_packets;
  2652. stats->tx_packets += slice_stats->tx_packets;
  2653. stats->rx_bytes += slice_stats->rx_bytes;
  2654. stats->tx_bytes += slice_stats->tx_bytes;
  2655. stats->rx_dropped += slice_stats->rx_dropped;
  2656. stats->tx_dropped += slice_stats->tx_dropped;
  2657. }
  2658. spin_unlock(&mgp->stats_lock);
  2659. return stats;
  2660. }
  2661. static void myri10ge_set_multicast_list(struct net_device *dev)
  2662. {
  2663. struct myri10ge_priv *mgp = netdev_priv(dev);
  2664. struct myri10ge_cmd cmd;
  2665. struct netdev_hw_addr *ha;
  2666. __be32 data[2] = { 0, 0 };
  2667. int err;
  2668. /* can be called from atomic contexts,
  2669. * pass 1 to force atomicity in myri10ge_send_cmd() */
  2670. myri10ge_change_promisc(mgp, dev->flags & IFF_PROMISC, 1);
  2671. /* This firmware is known to not support multicast */
  2672. if (!mgp->fw_multicast_support)
  2673. return;
  2674. /* Disable multicast filtering */
  2675. err = myri10ge_send_cmd(mgp, MXGEFW_ENABLE_ALLMULTI, &cmd, 1);
  2676. if (err != 0) {
  2677. netdev_err(dev, "Failed MXGEFW_ENABLE_ALLMULTI, error status: %d\n",
  2678. err);
  2679. goto abort;
  2680. }
  2681. if ((dev->flags & IFF_ALLMULTI) || mgp->adopted_rx_filter_bug) {
  2682. /* request to disable multicast filtering, so quit here */
  2683. return;
  2684. }
  2685. /* Flush the filters */
  2686. err = myri10ge_send_cmd(mgp, MXGEFW_LEAVE_ALL_MULTICAST_GROUPS,
  2687. &cmd, 1);
  2688. if (err != 0) {
  2689. netdev_err(dev, "Failed MXGEFW_LEAVE_ALL_MULTICAST_GROUPS, error status: %d\n",
  2690. err);
  2691. goto abort;
  2692. }
  2693. /* Walk the multicast list, and add each address */
  2694. netdev_for_each_mc_addr(ha, dev) {
  2695. memcpy(data, &ha->addr, 6);
  2696. cmd.data0 = ntohl(data[0]);
  2697. cmd.data1 = ntohl(data[1]);
  2698. err = myri10ge_send_cmd(mgp, MXGEFW_JOIN_MULTICAST_GROUP,
  2699. &cmd, 1);
  2700. if (err != 0) {
  2701. netdev_err(dev, "Failed MXGEFW_JOIN_MULTICAST_GROUP, error status:%d %pM\n",
  2702. err, ha->addr);
  2703. goto abort;
  2704. }
  2705. }
  2706. /* Enable multicast filtering */
  2707. err = myri10ge_send_cmd(mgp, MXGEFW_DISABLE_ALLMULTI, &cmd, 1);
  2708. if (err != 0) {
  2709. netdev_err(dev, "Failed MXGEFW_DISABLE_ALLMULTI, error status: %d\n",
  2710. err);
  2711. goto abort;
  2712. }
  2713. return;
  2714. abort:
  2715. return;
  2716. }
  2717. static int myri10ge_set_mac_address(struct net_device *dev, void *addr)
  2718. {
  2719. struct sockaddr *sa = addr;
  2720. struct myri10ge_priv *mgp = netdev_priv(dev);
  2721. int status;
  2722. if (!is_valid_ether_addr(sa->sa_data))
  2723. return -EADDRNOTAVAIL;
  2724. status = myri10ge_update_mac_address(mgp, sa->sa_data);
  2725. if (status != 0) {
  2726. netdev_err(dev, "changing mac address failed with %d\n",
  2727. status);
  2728. return status;
  2729. }
  2730. /* change the dev structure */
  2731. memcpy(dev->dev_addr, sa->sa_data, 6);
  2732. return 0;
  2733. }
  2734. static int myri10ge_change_mtu(struct net_device *dev, int new_mtu)
  2735. {
  2736. struct myri10ge_priv *mgp = netdev_priv(dev);
  2737. int error = 0;
  2738. if ((new_mtu < 68) || (ETH_HLEN + new_mtu > MYRI10GE_MAX_ETHER_MTU)) {
  2739. netdev_err(dev, "new mtu (%d) is not valid\n", new_mtu);
  2740. return -EINVAL;
  2741. }
  2742. netdev_info(dev, "changing mtu from %d to %d\n", dev->mtu, new_mtu);
  2743. if (mgp->running) {
  2744. /* if we change the mtu on an active device, we must
  2745. * reset the device so the firmware sees the change */
  2746. myri10ge_close(dev);
  2747. dev->mtu = new_mtu;
  2748. myri10ge_open(dev);
  2749. } else
  2750. dev->mtu = new_mtu;
  2751. return error;
  2752. }
  2753. /*
  2754. * Enable ECRC to align PCI-E Completion packets on an 8-byte boundary.
  2755. * Only do it if the bridge is a root port since we don't want to disturb
  2756. * any other device, except if forced with myri10ge_ecrc_enable > 1.
  2757. */
  2758. static void myri10ge_enable_ecrc(struct myri10ge_priv *mgp)
  2759. {
  2760. struct pci_dev *bridge = mgp->pdev->bus->self;
  2761. struct device *dev = &mgp->pdev->dev;
  2762. unsigned cap;
  2763. unsigned err_cap;
  2764. u16 val;
  2765. u8 ext_type;
  2766. int ret;
  2767. if (!myri10ge_ecrc_enable || !bridge)
  2768. return;
  2769. /* check that the bridge is a root port */
  2770. cap = pci_find_capability(bridge, PCI_CAP_ID_EXP);
  2771. pci_read_config_word(bridge, cap + PCI_CAP_FLAGS, &val);
  2772. ext_type = (val & PCI_EXP_FLAGS_TYPE) >> 4;
  2773. if (ext_type != PCI_EXP_TYPE_ROOT_PORT) {
  2774. if (myri10ge_ecrc_enable > 1) {
  2775. struct pci_dev *prev_bridge, *old_bridge = bridge;
  2776. /* Walk the hierarchy up to the root port
  2777. * where ECRC has to be enabled */
  2778. do {
  2779. prev_bridge = bridge;
  2780. bridge = bridge->bus->self;
  2781. if (!bridge || prev_bridge == bridge) {
  2782. dev_err(dev,
  2783. "Failed to find root port"
  2784. " to force ECRC\n");
  2785. return;
  2786. }
  2787. cap =
  2788. pci_find_capability(bridge, PCI_CAP_ID_EXP);
  2789. pci_read_config_word(bridge,
  2790. cap + PCI_CAP_FLAGS, &val);
  2791. ext_type = (val & PCI_EXP_FLAGS_TYPE) >> 4;
  2792. } while (ext_type != PCI_EXP_TYPE_ROOT_PORT);
  2793. dev_info(dev,
  2794. "Forcing ECRC on non-root port %s"
  2795. " (enabling on root port %s)\n",
  2796. pci_name(old_bridge), pci_name(bridge));
  2797. } else {
  2798. dev_err(dev,
  2799. "Not enabling ECRC on non-root port %s\n",
  2800. pci_name(bridge));
  2801. return;
  2802. }
  2803. }
  2804. cap = pci_find_ext_capability(bridge, PCI_EXT_CAP_ID_ERR);
  2805. if (!cap)
  2806. return;
  2807. ret = pci_read_config_dword(bridge, cap + PCI_ERR_CAP, &err_cap);
  2808. if (ret) {
  2809. dev_err(dev, "failed reading ext-conf-space of %s\n",
  2810. pci_name(bridge));
  2811. dev_err(dev, "\t pci=nommconf in use? "
  2812. "or buggy/incomplete/absent ACPI MCFG attr?\n");
  2813. return;
  2814. }
  2815. if (!(err_cap & PCI_ERR_CAP_ECRC_GENC))
  2816. return;
  2817. err_cap |= PCI_ERR_CAP_ECRC_GENE;
  2818. pci_write_config_dword(bridge, cap + PCI_ERR_CAP, err_cap);
  2819. dev_info(dev, "Enabled ECRC on upstream bridge %s\n", pci_name(bridge));
  2820. }
  2821. /*
  2822. * The Lanai Z8E PCI-E interface achieves higher Read-DMA throughput
  2823. * when the PCI-E Completion packets are aligned on an 8-byte
  2824. * boundary. Some PCI-E chip sets always align Completion packets; on
  2825. * the ones that do not, the alignment can be enforced by enabling
  2826. * ECRC generation (if supported).
  2827. *
  2828. * When PCI-E Completion packets are not aligned, it is actually more
  2829. * efficient to limit Read-DMA transactions to 2KB, rather than 4KB.
  2830. *
  2831. * If the driver can neither enable ECRC nor verify that it has
  2832. * already been enabled, then it must use a firmware image which works
  2833. * around unaligned completion packets (myri10ge_rss_ethp_z8e.dat), and it
  2834. * should also ensure that it never gives the device a Read-DMA which is
  2835. * larger than 2KB by setting the tx_boundary to 2KB. If ECRC is
  2836. * enabled, then the driver should use the aligned (myri10ge_rss_eth_z8e.dat)
  2837. * firmware image, and set tx_boundary to 4KB.
  2838. */
  2839. static void myri10ge_firmware_probe(struct myri10ge_priv *mgp)
  2840. {
  2841. struct pci_dev *pdev = mgp->pdev;
  2842. struct device *dev = &pdev->dev;
  2843. int status;
  2844. mgp->tx_boundary = 4096;
  2845. /*
  2846. * Verify the max read request size was set to 4KB
  2847. * before trying the test with 4KB.
  2848. */
  2849. status = pcie_get_readrq(pdev);
  2850. if (status < 0) {
  2851. dev_err(dev, "Couldn't read max read req size: %d\n", status);
  2852. goto abort;
  2853. }
  2854. if (status != 4096) {
  2855. dev_warn(dev, "Max Read Request size != 4096 (%d)\n", status);
  2856. mgp->tx_boundary = 2048;
  2857. }
  2858. /*
  2859. * load the optimized firmware (which assumes aligned PCIe
  2860. * completions) in order to see if it works on this host.
  2861. */
  2862. set_fw_name(mgp, myri10ge_fw_aligned, false);
  2863. status = myri10ge_load_firmware(mgp, 1);
  2864. if (status != 0) {
  2865. goto abort;
  2866. }
  2867. /*
  2868. * Enable ECRC if possible
  2869. */
  2870. myri10ge_enable_ecrc(mgp);
  2871. /*
  2872. * Run a DMA test which watches for unaligned completions and
  2873. * aborts on the first one seen.
  2874. */
  2875. status = myri10ge_dma_test(mgp, MXGEFW_CMD_UNALIGNED_TEST);
  2876. if (status == 0)
  2877. return; /* keep the aligned firmware */
  2878. if (status != -E2BIG)
  2879. dev_warn(dev, "DMA test failed: %d\n", status);
  2880. if (status == -ENOSYS)
  2881. dev_warn(dev, "Falling back to ethp! "
  2882. "Please install up to date fw\n");
  2883. abort:
  2884. /* fall back to using the unaligned firmware */
  2885. mgp->tx_boundary = 2048;
  2886. set_fw_name(mgp, myri10ge_fw_unaligned, false);
  2887. }
  2888. static void myri10ge_select_firmware(struct myri10ge_priv *mgp)
  2889. {
  2890. int overridden = 0;
  2891. if (myri10ge_force_firmware == 0) {
  2892. int link_width, exp_cap;
  2893. u16 lnk;
  2894. exp_cap = pci_find_capability(mgp->pdev, PCI_CAP_ID_EXP);
  2895. pci_read_config_word(mgp->pdev, exp_cap + PCI_EXP_LNKSTA, &lnk);
  2896. link_width = (lnk >> 4) & 0x3f;
  2897. /* Check to see if Link is less than 8 or if the
  2898. * upstream bridge is known to provide aligned
  2899. * completions */
  2900. if (link_width < 8) {
  2901. dev_info(&mgp->pdev->dev, "PCIE x%d Link\n",
  2902. link_width);
  2903. mgp->tx_boundary = 4096;
  2904. set_fw_name(mgp, myri10ge_fw_aligned, false);
  2905. } else {
  2906. myri10ge_firmware_probe(mgp);
  2907. }
  2908. } else {
  2909. if (myri10ge_force_firmware == 1) {
  2910. dev_info(&mgp->pdev->dev,
  2911. "Assuming aligned completions (forced)\n");
  2912. mgp->tx_boundary = 4096;
  2913. set_fw_name(mgp, myri10ge_fw_aligned, false);
  2914. } else {
  2915. dev_info(&mgp->pdev->dev,
  2916. "Assuming unaligned completions (forced)\n");
  2917. mgp->tx_boundary = 2048;
  2918. set_fw_name(mgp, myri10ge_fw_unaligned, false);
  2919. }
  2920. }
  2921. kparam_block_sysfs_write(myri10ge_fw_name);
  2922. if (myri10ge_fw_name != NULL) {
  2923. char *fw_name = kstrdup(myri10ge_fw_name, GFP_KERNEL);
  2924. if (fw_name) {
  2925. overridden = 1;
  2926. set_fw_name(mgp, fw_name, true);
  2927. }
  2928. }
  2929. kparam_unblock_sysfs_write(myri10ge_fw_name);
  2930. if (mgp->board_number < MYRI10GE_MAX_BOARDS &&
  2931. myri10ge_fw_names[mgp->board_number] != NULL &&
  2932. strlen(myri10ge_fw_names[mgp->board_number])) {
  2933. set_fw_name(mgp, myri10ge_fw_names[mgp->board_number], false);
  2934. overridden = 1;
  2935. }
  2936. if (overridden)
  2937. dev_info(&mgp->pdev->dev, "overriding firmware to %s\n",
  2938. mgp->fw_name);
  2939. }
  2940. #ifdef CONFIG_PM
  2941. static int myri10ge_suspend(struct pci_dev *pdev, pm_message_t state)
  2942. {
  2943. struct myri10ge_priv *mgp;
  2944. struct net_device *netdev;
  2945. mgp = pci_get_drvdata(pdev);
  2946. if (mgp == NULL)
  2947. return -EINVAL;
  2948. netdev = mgp->dev;
  2949. netif_device_detach(netdev);
  2950. if (netif_running(netdev)) {
  2951. netdev_info(netdev, "closing\n");
  2952. rtnl_lock();
  2953. myri10ge_close(netdev);
  2954. rtnl_unlock();
  2955. }
  2956. myri10ge_dummy_rdma(mgp, 0);
  2957. pci_save_state(pdev);
  2958. pci_disable_device(pdev);
  2959. return pci_set_power_state(pdev, pci_choose_state(pdev, state));
  2960. }
  2961. static int myri10ge_resume(struct pci_dev *pdev)
  2962. {
  2963. struct myri10ge_priv *mgp;
  2964. struct net_device *netdev;
  2965. int status;
  2966. u16 vendor;
  2967. mgp = pci_get_drvdata(pdev);
  2968. if (mgp == NULL)
  2969. return -EINVAL;
  2970. netdev = mgp->dev;
  2971. pci_set_power_state(pdev, 0); /* zeros conf space as a side effect */
  2972. msleep(5); /* give card time to respond */
  2973. pci_read_config_word(mgp->pdev, PCI_VENDOR_ID, &vendor);
  2974. if (vendor == 0xffff) {
  2975. netdev_err(mgp->dev, "device disappeared!\n");
  2976. return -EIO;
  2977. }
  2978. pci_restore_state(pdev);
  2979. status = pci_enable_device(pdev);
  2980. if (status) {
  2981. dev_err(&pdev->dev, "failed to enable device\n");
  2982. return status;
  2983. }
  2984. pci_set_master(pdev);
  2985. myri10ge_reset(mgp);
  2986. myri10ge_dummy_rdma(mgp, 1);
  2987. /* Save configuration space to be restored if the
  2988. * nic resets due to a parity error */
  2989. pci_save_state(pdev);
  2990. if (netif_running(netdev)) {
  2991. rtnl_lock();
  2992. status = myri10ge_open(netdev);
  2993. rtnl_unlock();
  2994. if (status != 0)
  2995. goto abort_with_enabled;
  2996. }
  2997. netif_device_attach(netdev);
  2998. return 0;
  2999. abort_with_enabled:
  3000. pci_disable_device(pdev);
  3001. return -EIO;
  3002. }
  3003. #endif /* CONFIG_PM */
  3004. static u32 myri10ge_read_reboot(struct myri10ge_priv *mgp)
  3005. {
  3006. struct pci_dev *pdev = mgp->pdev;
  3007. int vs = mgp->vendor_specific_offset;
  3008. u32 reboot;
  3009. /*enter read32 mode */
  3010. pci_write_config_byte(pdev, vs + 0x10, 0x3);
  3011. /*read REBOOT_STATUS (0xfffffff0) */
  3012. pci_write_config_dword(pdev, vs + 0x18, 0xfffffff0);
  3013. pci_read_config_dword(pdev, vs + 0x14, &reboot);
  3014. return reboot;
  3015. }
  3016. /*
  3017. * This watchdog is used to check whether the board has suffered
  3018. * from a parity error and needs to be recovered.
  3019. */
  3020. static void myri10ge_watchdog(struct work_struct *work)
  3021. {
  3022. struct myri10ge_priv *mgp =
  3023. container_of(work, struct myri10ge_priv, watchdog_work);
  3024. struct myri10ge_tx_buf *tx;
  3025. u32 reboot;
  3026. int status, rebooted;
  3027. int i;
  3028. u16 cmd, vendor;
  3029. mgp->watchdog_resets++;
  3030. pci_read_config_word(mgp->pdev, PCI_COMMAND, &cmd);
  3031. rebooted = 0;
  3032. if ((cmd & PCI_COMMAND_MASTER) == 0) {
  3033. /* Bus master DMA disabled? Check to see
  3034. * if the card rebooted due to a parity error
  3035. * For now, just report it */
  3036. reboot = myri10ge_read_reboot(mgp);
  3037. netdev_err(mgp->dev, "NIC rebooted (0x%x),%s resetting\n",
  3038. reboot,
  3039. myri10ge_reset_recover ? "" : " not");
  3040. if (myri10ge_reset_recover == 0)
  3041. return;
  3042. rtnl_lock();
  3043. mgp->rebooted = 1;
  3044. rebooted = 1;
  3045. myri10ge_close(mgp->dev);
  3046. myri10ge_reset_recover--;
  3047. mgp->rebooted = 0;
  3048. /*
  3049. * A rebooted nic will come back with config space as
  3050. * it was after power was applied to PCIe bus.
  3051. * Attempt to restore config space which was saved
  3052. * when the driver was loaded, or the last time the
  3053. * nic was resumed from power saving mode.
  3054. */
  3055. pci_restore_state(mgp->pdev);
  3056. /* save state again for accounting reasons */
  3057. pci_save_state(mgp->pdev);
  3058. } else {
  3059. /* if we get back -1's from our slot, perhaps somebody
  3060. * powered off our card. Don't try to reset it in
  3061. * this case */
  3062. if (cmd == 0xffff) {
  3063. pci_read_config_word(mgp->pdev, PCI_VENDOR_ID, &vendor);
  3064. if (vendor == 0xffff) {
  3065. netdev_err(mgp->dev, "device disappeared!\n");
  3066. return;
  3067. }
  3068. }
  3069. /* Perhaps it is a software error. Try to reset */
  3070. netdev_err(mgp->dev, "device timeout, resetting\n");
  3071. for (i = 0; i < mgp->num_slices; i++) {
  3072. tx = &mgp->ss[i].tx;
  3073. netdev_err(mgp->dev, "(%d): %d %d %d %d %d %d\n",
  3074. i, tx->queue_active, tx->req,
  3075. tx->done, tx->pkt_start, tx->pkt_done,
  3076. (int)ntohl(mgp->ss[i].fw_stats->
  3077. send_done_count));
  3078. msleep(2000);
  3079. netdev_info(mgp->dev, "(%d): %d %d %d %d %d %d\n",
  3080. i, tx->queue_active, tx->req,
  3081. tx->done, tx->pkt_start, tx->pkt_done,
  3082. (int)ntohl(mgp->ss[i].fw_stats->
  3083. send_done_count));
  3084. }
  3085. }
  3086. if (!rebooted) {
  3087. rtnl_lock();
  3088. myri10ge_close(mgp->dev);
  3089. }
  3090. status = myri10ge_load_firmware(mgp, 1);
  3091. if (status != 0)
  3092. netdev_err(mgp->dev, "failed to load firmware\n");
  3093. else
  3094. myri10ge_open(mgp->dev);
  3095. rtnl_unlock();
  3096. }
  3097. /*
  3098. * We use our own timer routine rather than relying upon
  3099. * netdev->tx_timeout because we have a very large hardware transmit
  3100. * queue. Due to the large queue, the netdev->tx_timeout function
  3101. * cannot detect a NIC with a parity error in a timely fashion if the
  3102. * NIC is lightly loaded.
  3103. */
  3104. static void myri10ge_watchdog_timer(unsigned long arg)
  3105. {
  3106. struct myri10ge_priv *mgp;
  3107. struct myri10ge_slice_state *ss;
  3108. int i, reset_needed, busy_slice_cnt;
  3109. u32 rx_pause_cnt;
  3110. u16 cmd;
  3111. mgp = (struct myri10ge_priv *)arg;
  3112. rx_pause_cnt = ntohl(mgp->ss[0].fw_stats->dropped_pause);
  3113. busy_slice_cnt = 0;
  3114. for (i = 0, reset_needed = 0;
  3115. i < mgp->num_slices && reset_needed == 0; ++i) {
  3116. ss = &mgp->ss[i];
  3117. if (ss->rx_small.watchdog_needed) {
  3118. myri10ge_alloc_rx_pages(mgp, &ss->rx_small,
  3119. mgp->small_bytes + MXGEFW_PAD,
  3120. 1);
  3121. if (ss->rx_small.fill_cnt - ss->rx_small.cnt >=
  3122. myri10ge_fill_thresh)
  3123. ss->rx_small.watchdog_needed = 0;
  3124. }
  3125. if (ss->rx_big.watchdog_needed) {
  3126. myri10ge_alloc_rx_pages(mgp, &ss->rx_big,
  3127. mgp->big_bytes, 1);
  3128. if (ss->rx_big.fill_cnt - ss->rx_big.cnt >=
  3129. myri10ge_fill_thresh)
  3130. ss->rx_big.watchdog_needed = 0;
  3131. }
  3132. if (ss->tx.req != ss->tx.done &&
  3133. ss->tx.done == ss->watchdog_tx_done &&
  3134. ss->watchdog_tx_req != ss->watchdog_tx_done) {
  3135. /* nic seems like it might be stuck.. */
  3136. if (rx_pause_cnt != mgp->watchdog_pause) {
  3137. if (net_ratelimit())
  3138. netdev_err(mgp->dev, "slice %d: TX paused, check link partner\n",
  3139. i);
  3140. } else {
  3141. netdev_warn(mgp->dev, "slice %d stuck:", i);
  3142. reset_needed = 1;
  3143. }
  3144. }
  3145. if (ss->watchdog_tx_done != ss->tx.done ||
  3146. ss->watchdog_rx_done != ss->rx_done.cnt) {
  3147. busy_slice_cnt++;
  3148. }
  3149. ss->watchdog_tx_done = ss->tx.done;
  3150. ss->watchdog_tx_req = ss->tx.req;
  3151. ss->watchdog_rx_done = ss->rx_done.cnt;
  3152. }
  3153. /* if we've sent or received no traffic, poll the NIC to
  3154. * ensure it is still there. Otherwise, we risk not noticing
  3155. * an error in a timely fashion */
  3156. if (busy_slice_cnt == 0) {
  3157. pci_read_config_word(mgp->pdev, PCI_COMMAND, &cmd);
  3158. if ((cmd & PCI_COMMAND_MASTER) == 0) {
  3159. reset_needed = 1;
  3160. }
  3161. }
  3162. mgp->watchdog_pause = rx_pause_cnt;
  3163. if (reset_needed) {
  3164. schedule_work(&mgp->watchdog_work);
  3165. } else {
  3166. /* rearm timer */
  3167. mod_timer(&mgp->watchdog_timer,
  3168. jiffies + myri10ge_watchdog_timeout * HZ);
  3169. }
  3170. }
  3171. static void myri10ge_free_slices(struct myri10ge_priv *mgp)
  3172. {
  3173. struct myri10ge_slice_state *ss;
  3174. struct pci_dev *pdev = mgp->pdev;
  3175. size_t bytes;
  3176. int i;
  3177. if (mgp->ss == NULL)
  3178. return;
  3179. for (i = 0; i < mgp->num_slices; i++) {
  3180. ss = &mgp->ss[i];
  3181. if (ss->rx_done.entry != NULL) {
  3182. bytes = mgp->max_intr_slots *
  3183. sizeof(*ss->rx_done.entry);
  3184. dma_free_coherent(&pdev->dev, bytes,
  3185. ss->rx_done.entry, ss->rx_done.bus);
  3186. ss->rx_done.entry = NULL;
  3187. }
  3188. if (ss->fw_stats != NULL) {
  3189. bytes = sizeof(*ss->fw_stats);
  3190. dma_free_coherent(&pdev->dev, bytes,
  3191. ss->fw_stats, ss->fw_stats_bus);
  3192. ss->fw_stats = NULL;
  3193. }
  3194. }
  3195. kfree(mgp->ss);
  3196. mgp->ss = NULL;
  3197. }
  3198. static int myri10ge_alloc_slices(struct myri10ge_priv *mgp)
  3199. {
  3200. struct myri10ge_slice_state *ss;
  3201. struct pci_dev *pdev = mgp->pdev;
  3202. size_t bytes;
  3203. int i;
  3204. bytes = sizeof(*mgp->ss) * mgp->num_slices;
  3205. mgp->ss = kzalloc(bytes, GFP_KERNEL);
  3206. if (mgp->ss == NULL) {
  3207. return -ENOMEM;
  3208. }
  3209. for (i = 0; i < mgp->num_slices; i++) {
  3210. ss = &mgp->ss[i];
  3211. bytes = mgp->max_intr_slots * sizeof(*ss->rx_done.entry);
  3212. ss->rx_done.entry = dma_alloc_coherent(&pdev->dev, bytes,
  3213. &ss->rx_done.bus,
  3214. GFP_KERNEL);
  3215. if (ss->rx_done.entry == NULL)
  3216. goto abort;
  3217. memset(ss->rx_done.entry, 0, bytes);
  3218. bytes = sizeof(*ss->fw_stats);
  3219. ss->fw_stats = dma_alloc_coherent(&pdev->dev, bytes,
  3220. &ss->fw_stats_bus,
  3221. GFP_KERNEL);
  3222. if (ss->fw_stats == NULL)
  3223. goto abort;
  3224. ss->mgp = mgp;
  3225. ss->dev = mgp->dev;
  3226. netif_napi_add(ss->dev, &ss->napi, myri10ge_poll,
  3227. myri10ge_napi_weight);
  3228. }
  3229. return 0;
  3230. abort:
  3231. myri10ge_free_slices(mgp);
  3232. return -ENOMEM;
  3233. }
  3234. /*
  3235. * This function determines the number of slices supported.
  3236. * The number slices is the minumum of the number of CPUS,
  3237. * the number of MSI-X irqs supported, the number of slices
  3238. * supported by the firmware
  3239. */
  3240. static void myri10ge_probe_slices(struct myri10ge_priv *mgp)
  3241. {
  3242. struct myri10ge_cmd cmd;
  3243. struct pci_dev *pdev = mgp->pdev;
  3244. char *old_fw;
  3245. bool old_allocated;
  3246. int i, status, ncpus, msix_cap;
  3247. mgp->num_slices = 1;
  3248. msix_cap = pci_find_capability(pdev, PCI_CAP_ID_MSIX);
  3249. ncpus = num_online_cpus();
  3250. if (myri10ge_max_slices == 1 || msix_cap == 0 ||
  3251. (myri10ge_max_slices == -1 && ncpus < 2))
  3252. return;
  3253. /* try to load the slice aware rss firmware */
  3254. old_fw = mgp->fw_name;
  3255. old_allocated = mgp->fw_name_allocated;
  3256. /* don't free old_fw if we override it. */
  3257. mgp->fw_name_allocated = false;
  3258. if (myri10ge_fw_name != NULL) {
  3259. dev_info(&mgp->pdev->dev, "overriding rss firmware to %s\n",
  3260. myri10ge_fw_name);
  3261. set_fw_name(mgp, myri10ge_fw_name, false);
  3262. } else if (old_fw == myri10ge_fw_aligned)
  3263. set_fw_name(mgp, myri10ge_fw_rss_aligned, false);
  3264. else
  3265. set_fw_name(mgp, myri10ge_fw_rss_unaligned, false);
  3266. status = myri10ge_load_firmware(mgp, 0);
  3267. if (status != 0) {
  3268. dev_info(&pdev->dev, "Rss firmware not found\n");
  3269. if (old_allocated)
  3270. kfree(old_fw);
  3271. return;
  3272. }
  3273. /* hit the board with a reset to ensure it is alive */
  3274. memset(&cmd, 0, sizeof(cmd));
  3275. status = myri10ge_send_cmd(mgp, MXGEFW_CMD_RESET, &cmd, 0);
  3276. if (status != 0) {
  3277. dev_err(&mgp->pdev->dev, "failed reset\n");
  3278. goto abort_with_fw;
  3279. }
  3280. mgp->max_intr_slots = cmd.data0 / sizeof(struct mcp_slot);
  3281. /* tell it the size of the interrupt queues */
  3282. cmd.data0 = mgp->max_intr_slots * sizeof(struct mcp_slot);
  3283. status = myri10ge_send_cmd(mgp, MXGEFW_CMD_SET_INTRQ_SIZE, &cmd, 0);
  3284. if (status != 0) {
  3285. dev_err(&mgp->pdev->dev, "failed MXGEFW_CMD_SET_INTRQ_SIZE\n");
  3286. goto abort_with_fw;
  3287. }
  3288. /* ask the maximum number of slices it supports */
  3289. status = myri10ge_send_cmd(mgp, MXGEFW_CMD_GET_MAX_RSS_QUEUES, &cmd, 0);
  3290. if (status != 0)
  3291. goto abort_with_fw;
  3292. else
  3293. mgp->num_slices = cmd.data0;
  3294. /* Only allow multiple slices if MSI-X is usable */
  3295. if (!myri10ge_msi) {
  3296. goto abort_with_fw;
  3297. }
  3298. /* if the admin did not specify a limit to how many
  3299. * slices we should use, cap it automatically to the
  3300. * number of CPUs currently online */
  3301. if (myri10ge_max_slices == -1)
  3302. myri10ge_max_slices = ncpus;
  3303. if (mgp->num_slices > myri10ge_max_slices)
  3304. mgp->num_slices = myri10ge_max_slices;
  3305. /* Now try to allocate as many MSI-X vectors as we have
  3306. * slices. We give up on MSI-X if we can only get a single
  3307. * vector. */
  3308. mgp->msix_vectors = kcalloc(mgp->num_slices, sizeof(*mgp->msix_vectors),
  3309. GFP_KERNEL);
  3310. if (mgp->msix_vectors == NULL)
  3311. goto disable_msix;
  3312. for (i = 0; i < mgp->num_slices; i++) {
  3313. mgp->msix_vectors[i].entry = i;
  3314. }
  3315. while (mgp->num_slices > 1) {
  3316. /* make sure it is a power of two */
  3317. while (!is_power_of_2(mgp->num_slices))
  3318. mgp->num_slices--;
  3319. if (mgp->num_slices == 1)
  3320. goto disable_msix;
  3321. status = pci_enable_msix(pdev, mgp->msix_vectors,
  3322. mgp->num_slices);
  3323. if (status == 0) {
  3324. pci_disable_msix(pdev);
  3325. if (old_allocated)
  3326. kfree(old_fw);
  3327. return;
  3328. }
  3329. if (status > 0)
  3330. mgp->num_slices = status;
  3331. else
  3332. goto disable_msix;
  3333. }
  3334. disable_msix:
  3335. if (mgp->msix_vectors != NULL) {
  3336. kfree(mgp->msix_vectors);
  3337. mgp->msix_vectors = NULL;
  3338. }
  3339. abort_with_fw:
  3340. mgp->num_slices = 1;
  3341. set_fw_name(mgp, old_fw, old_allocated);
  3342. myri10ge_load_firmware(mgp, 0);
  3343. }
  3344. static const struct net_device_ops myri10ge_netdev_ops = {
  3345. .ndo_open = myri10ge_open,
  3346. .ndo_stop = myri10ge_close,
  3347. .ndo_start_xmit = myri10ge_xmit,
  3348. .ndo_get_stats = myri10ge_get_stats,
  3349. .ndo_validate_addr = eth_validate_addr,
  3350. .ndo_change_mtu = myri10ge_change_mtu,
  3351. .ndo_set_multicast_list = myri10ge_set_multicast_list,
  3352. .ndo_set_mac_address = myri10ge_set_mac_address,
  3353. };
  3354. static int myri10ge_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
  3355. {
  3356. struct net_device *netdev;
  3357. struct myri10ge_priv *mgp;
  3358. struct device *dev = &pdev->dev;
  3359. int i;
  3360. int status = -ENXIO;
  3361. int dac_enabled;
  3362. unsigned hdr_offset, ss_offset;
  3363. static int board_number;
  3364. netdev = alloc_etherdev_mq(sizeof(*mgp), MYRI10GE_MAX_SLICES);
  3365. if (netdev == NULL) {
  3366. dev_err(dev, "Could not allocate ethernet device\n");
  3367. return -ENOMEM;
  3368. }
  3369. SET_NETDEV_DEV(netdev, &pdev->dev);
  3370. mgp = netdev_priv(netdev);
  3371. mgp->dev = netdev;
  3372. mgp->pdev = pdev;
  3373. mgp->csum_flag = MXGEFW_FLAGS_CKSUM;
  3374. mgp->pause = myri10ge_flow_control;
  3375. mgp->intr_coal_delay = myri10ge_intr_coal_delay;
  3376. mgp->msg_enable = netif_msg_init(myri10ge_debug, MYRI10GE_MSG_DEFAULT);
  3377. mgp->board_number = board_number;
  3378. init_waitqueue_head(&mgp->down_wq);
  3379. if (pci_enable_device(pdev)) {
  3380. dev_err(&pdev->dev, "pci_enable_device call failed\n");
  3381. status = -ENODEV;
  3382. goto abort_with_netdev;
  3383. }
  3384. /* Find the vendor-specific cap so we can check
  3385. * the reboot register later on */
  3386. mgp->vendor_specific_offset
  3387. = pci_find_capability(pdev, PCI_CAP_ID_VNDR);
  3388. /* Set our max read request to 4KB */
  3389. status = pcie_set_readrq(pdev, 4096);
  3390. if (status != 0) {
  3391. dev_err(&pdev->dev, "Error %d writing PCI_EXP_DEVCTL\n",
  3392. status);
  3393. goto abort_with_enabled;
  3394. }
  3395. pci_set_master(pdev);
  3396. dac_enabled = 1;
  3397. status = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
  3398. if (status != 0) {
  3399. dac_enabled = 0;
  3400. dev_err(&pdev->dev,
  3401. "64-bit pci address mask was refused, "
  3402. "trying 32-bit\n");
  3403. status = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  3404. }
  3405. if (status != 0) {
  3406. dev_err(&pdev->dev, "Error %d setting DMA mask\n", status);
  3407. goto abort_with_enabled;
  3408. }
  3409. (void)pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
  3410. mgp->cmd = dma_alloc_coherent(&pdev->dev, sizeof(*mgp->cmd),
  3411. &mgp->cmd_bus, GFP_KERNEL);
  3412. if (mgp->cmd == NULL)
  3413. goto abort_with_enabled;
  3414. mgp->board_span = pci_resource_len(pdev, 0);
  3415. mgp->iomem_base = pci_resource_start(pdev, 0);
  3416. mgp->mtrr = -1;
  3417. mgp->wc_enabled = 0;
  3418. #ifdef CONFIG_MTRR
  3419. mgp->mtrr = mtrr_add(mgp->iomem_base, mgp->board_span,
  3420. MTRR_TYPE_WRCOMB, 1);
  3421. if (mgp->mtrr >= 0)
  3422. mgp->wc_enabled = 1;
  3423. #endif
  3424. mgp->sram = ioremap_wc(mgp->iomem_base, mgp->board_span);
  3425. if (mgp->sram == NULL) {
  3426. dev_err(&pdev->dev, "ioremap failed for %ld bytes at 0x%lx\n",
  3427. mgp->board_span, mgp->iomem_base);
  3428. status = -ENXIO;
  3429. goto abort_with_mtrr;
  3430. }
  3431. hdr_offset =
  3432. ntohl(__raw_readl(mgp->sram + MCP_HEADER_PTR_OFFSET)) & 0xffffc;
  3433. ss_offset = hdr_offset + offsetof(struct mcp_gen_header, string_specs);
  3434. mgp->sram_size = ntohl(__raw_readl(mgp->sram + ss_offset));
  3435. if (mgp->sram_size > mgp->board_span ||
  3436. mgp->sram_size <= MYRI10GE_FW_OFFSET) {
  3437. dev_err(&pdev->dev,
  3438. "invalid sram_size %dB or board span %ldB\n",
  3439. mgp->sram_size, mgp->board_span);
  3440. goto abort_with_ioremap;
  3441. }
  3442. memcpy_fromio(mgp->eeprom_strings,
  3443. mgp->sram + mgp->sram_size, MYRI10GE_EEPROM_STRINGS_SIZE);
  3444. memset(mgp->eeprom_strings + MYRI10GE_EEPROM_STRINGS_SIZE - 2, 0, 2);
  3445. status = myri10ge_read_mac_addr(mgp);
  3446. if (status)
  3447. goto abort_with_ioremap;
  3448. for (i = 0; i < ETH_ALEN; i++)
  3449. netdev->dev_addr[i] = mgp->mac_addr[i];
  3450. myri10ge_select_firmware(mgp);
  3451. status = myri10ge_load_firmware(mgp, 1);
  3452. if (status != 0) {
  3453. dev_err(&pdev->dev, "failed to load firmware\n");
  3454. goto abort_with_ioremap;
  3455. }
  3456. myri10ge_probe_slices(mgp);
  3457. status = myri10ge_alloc_slices(mgp);
  3458. if (status != 0) {
  3459. dev_err(&pdev->dev, "failed to alloc slice state\n");
  3460. goto abort_with_firmware;
  3461. }
  3462. netif_set_real_num_tx_queues(netdev, mgp->num_slices);
  3463. netif_set_real_num_rx_queues(netdev, mgp->num_slices);
  3464. status = myri10ge_reset(mgp);
  3465. if (status != 0) {
  3466. dev_err(&pdev->dev, "failed reset\n");
  3467. goto abort_with_slices;
  3468. }
  3469. #ifdef CONFIG_MYRI10GE_DCA
  3470. myri10ge_setup_dca(mgp);
  3471. #endif
  3472. pci_set_drvdata(pdev, mgp);
  3473. if ((myri10ge_initial_mtu + ETH_HLEN) > MYRI10GE_MAX_ETHER_MTU)
  3474. myri10ge_initial_mtu = MYRI10GE_MAX_ETHER_MTU - ETH_HLEN;
  3475. if ((myri10ge_initial_mtu + ETH_HLEN) < 68)
  3476. myri10ge_initial_mtu = 68;
  3477. netdev->netdev_ops = &myri10ge_netdev_ops;
  3478. netdev->mtu = myri10ge_initial_mtu;
  3479. netdev->base_addr = mgp->iomem_base;
  3480. netdev->features = mgp->features;
  3481. if (dac_enabled)
  3482. netdev->features |= NETIF_F_HIGHDMA;
  3483. netdev->features |= NETIF_F_LRO;
  3484. netdev->vlan_features |= mgp->features;
  3485. if (mgp->fw_ver_tiny < 37)
  3486. netdev->vlan_features &= ~NETIF_F_TSO6;
  3487. if (mgp->fw_ver_tiny < 32)
  3488. netdev->vlan_features &= ~NETIF_F_TSO;
  3489. /* make sure we can get an irq, and that MSI can be
  3490. * setup (if available). Also ensure netdev->irq
  3491. * is set to correct value if MSI is enabled */
  3492. status = myri10ge_request_irq(mgp);
  3493. if (status != 0)
  3494. goto abort_with_firmware;
  3495. netdev->irq = pdev->irq;
  3496. myri10ge_free_irq(mgp);
  3497. /* Save configuration space to be restored if the
  3498. * nic resets due to a parity error */
  3499. pci_save_state(pdev);
  3500. /* Setup the watchdog timer */
  3501. setup_timer(&mgp->watchdog_timer, myri10ge_watchdog_timer,
  3502. (unsigned long)mgp);
  3503. spin_lock_init(&mgp->stats_lock);
  3504. SET_ETHTOOL_OPS(netdev, &myri10ge_ethtool_ops);
  3505. INIT_WORK(&mgp->watchdog_work, myri10ge_watchdog);
  3506. status = register_netdev(netdev);
  3507. if (status != 0) {
  3508. dev_err(&pdev->dev, "register_netdev failed: %d\n", status);
  3509. goto abort_with_state;
  3510. }
  3511. if (mgp->msix_enabled)
  3512. dev_info(dev, "%d MSI-X IRQs, tx bndry %d, fw %s, WC %s\n",
  3513. mgp->num_slices, mgp->tx_boundary, mgp->fw_name,
  3514. (mgp->wc_enabled ? "Enabled" : "Disabled"));
  3515. else
  3516. dev_info(dev, "%s IRQ %d, tx bndry %d, fw %s, WC %s\n",
  3517. mgp->msi_enabled ? "MSI" : "xPIC",
  3518. netdev->irq, mgp->tx_boundary, mgp->fw_name,
  3519. (mgp->wc_enabled ? "Enabled" : "Disabled"));
  3520. board_number++;
  3521. return 0;
  3522. abort_with_state:
  3523. pci_restore_state(pdev);
  3524. abort_with_slices:
  3525. myri10ge_free_slices(mgp);
  3526. abort_with_firmware:
  3527. myri10ge_dummy_rdma(mgp, 0);
  3528. abort_with_ioremap:
  3529. if (mgp->mac_addr_string != NULL)
  3530. dev_err(&pdev->dev,
  3531. "myri10ge_probe() failed: MAC=%s, SN=%ld\n",
  3532. mgp->mac_addr_string, mgp->serial_number);
  3533. iounmap(mgp->sram);
  3534. abort_with_mtrr:
  3535. #ifdef CONFIG_MTRR
  3536. if (mgp->mtrr >= 0)
  3537. mtrr_del(mgp->mtrr, mgp->iomem_base, mgp->board_span);
  3538. #endif
  3539. dma_free_coherent(&pdev->dev, sizeof(*mgp->cmd),
  3540. mgp->cmd, mgp->cmd_bus);
  3541. abort_with_enabled:
  3542. pci_disable_device(pdev);
  3543. abort_with_netdev:
  3544. set_fw_name(mgp, NULL, false);
  3545. free_netdev(netdev);
  3546. return status;
  3547. }
  3548. /*
  3549. * myri10ge_remove
  3550. *
  3551. * Does what is necessary to shutdown one Myrinet device. Called
  3552. * once for each Myrinet card by the kernel when a module is
  3553. * unloaded.
  3554. */
  3555. static void myri10ge_remove(struct pci_dev *pdev)
  3556. {
  3557. struct myri10ge_priv *mgp;
  3558. struct net_device *netdev;
  3559. mgp = pci_get_drvdata(pdev);
  3560. if (mgp == NULL)
  3561. return;
  3562. cancel_work_sync(&mgp->watchdog_work);
  3563. netdev = mgp->dev;
  3564. unregister_netdev(netdev);
  3565. #ifdef CONFIG_MYRI10GE_DCA
  3566. myri10ge_teardown_dca(mgp);
  3567. #endif
  3568. myri10ge_dummy_rdma(mgp, 0);
  3569. /* avoid a memory leak */
  3570. pci_restore_state(pdev);
  3571. iounmap(mgp->sram);
  3572. #ifdef CONFIG_MTRR
  3573. if (mgp->mtrr >= 0)
  3574. mtrr_del(mgp->mtrr, mgp->iomem_base, mgp->board_span);
  3575. #endif
  3576. myri10ge_free_slices(mgp);
  3577. if (mgp->msix_vectors != NULL)
  3578. kfree(mgp->msix_vectors);
  3579. dma_free_coherent(&pdev->dev, sizeof(*mgp->cmd),
  3580. mgp->cmd, mgp->cmd_bus);
  3581. set_fw_name(mgp, NULL, false);
  3582. free_netdev(netdev);
  3583. pci_disable_device(pdev);
  3584. pci_set_drvdata(pdev, NULL);
  3585. }
  3586. #define PCI_DEVICE_ID_MYRICOM_MYRI10GE_Z8E 0x0008
  3587. #define PCI_DEVICE_ID_MYRICOM_MYRI10GE_Z8E_9 0x0009
  3588. static DEFINE_PCI_DEVICE_TABLE(myri10ge_pci_tbl) = {
  3589. {PCI_DEVICE(PCI_VENDOR_ID_MYRICOM, PCI_DEVICE_ID_MYRICOM_MYRI10GE_Z8E)},
  3590. {PCI_DEVICE
  3591. (PCI_VENDOR_ID_MYRICOM, PCI_DEVICE_ID_MYRICOM_MYRI10GE_Z8E_9)},
  3592. {0},
  3593. };
  3594. MODULE_DEVICE_TABLE(pci, myri10ge_pci_tbl);
  3595. static struct pci_driver myri10ge_driver = {
  3596. .name = "myri10ge",
  3597. .probe = myri10ge_probe,
  3598. .remove = myri10ge_remove,
  3599. .id_table = myri10ge_pci_tbl,
  3600. #ifdef CONFIG_PM
  3601. .suspend = myri10ge_suspend,
  3602. .resume = myri10ge_resume,
  3603. #endif
  3604. };
  3605. #ifdef CONFIG_MYRI10GE_DCA
  3606. static int
  3607. myri10ge_notify_dca(struct notifier_block *nb, unsigned long event, void *p)
  3608. {
  3609. int err = driver_for_each_device(&myri10ge_driver.driver,
  3610. NULL, &event,
  3611. myri10ge_notify_dca_device);
  3612. if (err)
  3613. return NOTIFY_BAD;
  3614. return NOTIFY_DONE;
  3615. }
  3616. static struct notifier_block myri10ge_dca_notifier = {
  3617. .notifier_call = myri10ge_notify_dca,
  3618. .next = NULL,
  3619. .priority = 0,
  3620. };
  3621. #endif /* CONFIG_MYRI10GE_DCA */
  3622. static __init int myri10ge_init_module(void)
  3623. {
  3624. pr_info("Version %s\n", MYRI10GE_VERSION_STR);
  3625. if (myri10ge_rss_hash > MXGEFW_RSS_HASH_TYPE_MAX) {
  3626. pr_err("Illegal rssh hash type %d, defaulting to source port\n",
  3627. myri10ge_rss_hash);
  3628. myri10ge_rss_hash = MXGEFW_RSS_HASH_TYPE_SRC_PORT;
  3629. }
  3630. #ifdef CONFIG_MYRI10GE_DCA
  3631. dca_register_notify(&myri10ge_dca_notifier);
  3632. #endif
  3633. if (myri10ge_max_slices > MYRI10GE_MAX_SLICES)
  3634. myri10ge_max_slices = MYRI10GE_MAX_SLICES;
  3635. return pci_register_driver(&myri10ge_driver);
  3636. }
  3637. module_init(myri10ge_init_module);
  3638. static __exit void myri10ge_cleanup_module(void)
  3639. {
  3640. #ifdef CONFIG_MYRI10GE_DCA
  3641. dca_unregister_notify(&myri10ge_dca_notifier);
  3642. #endif
  3643. pci_unregister_driver(&myri10ge_driver);
  3644. }
  3645. module_exit(myri10ge_cleanup_module);