myri10ge.c 111 KB

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