myri10ge.c 108 KB

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