myri10ge.c 112 KB

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