myri10ge.c 112 KB

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