myri10ge.c 113 KB

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