xgbe-dev.c 80 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924
  1. /*
  2. * AMD 10Gb Ethernet driver
  3. *
  4. * This file is available to you under your choice of the following two
  5. * licenses:
  6. *
  7. * License 1: GPLv2
  8. *
  9. * Copyright (c) 2014 Advanced Micro Devices, Inc.
  10. *
  11. * This file is free software; you may copy, redistribute and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation, either version 2 of the License, or (at
  14. * your option) any later version.
  15. *
  16. * This file is distributed in the hope that it will be useful, but
  17. * WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. * General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. *
  24. * This file incorporates work covered by the following copyright and
  25. * permission notice:
  26. * The Synopsys DWC ETHER XGMAC Software Driver and documentation
  27. * (hereinafter "Software") is an unsupported proprietary work of Synopsys,
  28. * Inc. unless otherwise expressly agreed to in writing between Synopsys
  29. * and you.
  30. *
  31. * The Software IS NOT an item of Licensed Software or Licensed Product
  32. * under any End User Software License Agreement or Agreement for Licensed
  33. * Product with Synopsys or any supplement thereto. Permission is hereby
  34. * granted, free of charge, to any person obtaining a copy of this software
  35. * annotated with this license and the Software, to deal in the Software
  36. * without restriction, including without limitation the rights to use,
  37. * copy, modify, merge, publish, distribute, sublicense, and/or sell copies
  38. * of the Software, and to permit persons to whom the Software is furnished
  39. * to do so, subject to the following conditions:
  40. *
  41. * The above copyright notice and this permission notice shall be included
  42. * in all copies or substantial portions of the Software.
  43. *
  44. * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"
  45. * BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  46. * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
  47. * PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS
  48. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  49. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  50. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  51. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  52. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  53. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  54. * THE POSSIBILITY OF SUCH DAMAGE.
  55. *
  56. *
  57. * License 2: Modified BSD
  58. *
  59. * Copyright (c) 2014 Advanced Micro Devices, Inc.
  60. * All rights reserved.
  61. *
  62. * Redistribution and use in source and binary forms, with or without
  63. * modification, are permitted provided that the following conditions are met:
  64. * * Redistributions of source code must retain the above copyright
  65. * notice, this list of conditions and the following disclaimer.
  66. * * Redistributions in binary form must reproduce the above copyright
  67. * notice, this list of conditions and the following disclaimer in the
  68. * documentation and/or other materials provided with the distribution.
  69. * * Neither the name of Advanced Micro Devices, Inc. nor the
  70. * names of its contributors may be used to endorse or promote products
  71. * derived from this software without specific prior written permission.
  72. *
  73. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  74. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  75. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  76. * ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
  77. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  78. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  79. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  80. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  81. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  82. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  83. *
  84. * This file incorporates work covered by the following copyright and
  85. * permission notice:
  86. * The Synopsys DWC ETHER XGMAC Software Driver and documentation
  87. * (hereinafter "Software") is an unsupported proprietary work of Synopsys,
  88. * Inc. unless otherwise expressly agreed to in writing between Synopsys
  89. * and you.
  90. *
  91. * The Software IS NOT an item of Licensed Software or Licensed Product
  92. * under any End User Software License Agreement or Agreement for Licensed
  93. * Product with Synopsys or any supplement thereto. Permission is hereby
  94. * granted, free of charge, to any person obtaining a copy of this software
  95. * annotated with this license and the Software, to deal in the Software
  96. * without restriction, including without limitation the rights to use,
  97. * copy, modify, merge, publish, distribute, sublicense, and/or sell copies
  98. * of the Software, and to permit persons to whom the Software is furnished
  99. * to do so, subject to the following conditions:
  100. *
  101. * The above copyright notice and this permission notice shall be included
  102. * in all copies or substantial portions of the Software.
  103. *
  104. * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"
  105. * BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  106. * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
  107. * PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS
  108. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  109. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  110. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  111. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  112. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  113. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  114. * THE POSSIBILITY OF SUCH DAMAGE.
  115. */
  116. #include <linux/phy.h>
  117. #include <linux/mdio.h>
  118. #include <linux/clk.h>
  119. #include <linux/bitrev.h>
  120. #include <linux/crc32.h>
  121. #include "xgbe.h"
  122. #include "xgbe-common.h"
  123. static unsigned int xgbe_usec_to_riwt(struct xgbe_prv_data *pdata,
  124. unsigned int usec)
  125. {
  126. unsigned long rate;
  127. unsigned int ret;
  128. DBGPR("-->xgbe_usec_to_riwt\n");
  129. rate = pdata->sysclk_rate;
  130. /*
  131. * Convert the input usec value to the watchdog timer value. Each
  132. * watchdog timer value is equivalent to 256 clock cycles.
  133. * Calculate the required value as:
  134. * ( usec * ( system_clock_mhz / 10^6 ) / 256
  135. */
  136. ret = (usec * (rate / 1000000)) / 256;
  137. DBGPR("<--xgbe_usec_to_riwt\n");
  138. return ret;
  139. }
  140. static unsigned int xgbe_riwt_to_usec(struct xgbe_prv_data *pdata,
  141. unsigned int riwt)
  142. {
  143. unsigned long rate;
  144. unsigned int ret;
  145. DBGPR("-->xgbe_riwt_to_usec\n");
  146. rate = pdata->sysclk_rate;
  147. /*
  148. * Convert the input watchdog timer value to the usec value. Each
  149. * watchdog timer value is equivalent to 256 clock cycles.
  150. * Calculate the required value as:
  151. * ( riwt * 256 ) / ( system_clock_mhz / 10^6 )
  152. */
  153. ret = (riwt * 256) / (rate / 1000000);
  154. DBGPR("<--xgbe_riwt_to_usec\n");
  155. return ret;
  156. }
  157. static int xgbe_config_pblx8(struct xgbe_prv_data *pdata)
  158. {
  159. struct xgbe_channel *channel;
  160. unsigned int i;
  161. channel = pdata->channel;
  162. for (i = 0; i < pdata->channel_count; i++, channel++)
  163. XGMAC_DMA_IOWRITE_BITS(channel, DMA_CH_CR, PBLX8,
  164. pdata->pblx8);
  165. return 0;
  166. }
  167. static int xgbe_get_tx_pbl_val(struct xgbe_prv_data *pdata)
  168. {
  169. return XGMAC_DMA_IOREAD_BITS(pdata->channel, DMA_CH_TCR, PBL);
  170. }
  171. static int xgbe_config_tx_pbl_val(struct xgbe_prv_data *pdata)
  172. {
  173. struct xgbe_channel *channel;
  174. unsigned int i;
  175. channel = pdata->channel;
  176. for (i = 0; i < pdata->channel_count; i++, channel++) {
  177. if (!channel->tx_ring)
  178. break;
  179. XGMAC_DMA_IOWRITE_BITS(channel, DMA_CH_TCR, PBL,
  180. pdata->tx_pbl);
  181. }
  182. return 0;
  183. }
  184. static int xgbe_get_rx_pbl_val(struct xgbe_prv_data *pdata)
  185. {
  186. return XGMAC_DMA_IOREAD_BITS(pdata->channel, DMA_CH_RCR, PBL);
  187. }
  188. static int xgbe_config_rx_pbl_val(struct xgbe_prv_data *pdata)
  189. {
  190. struct xgbe_channel *channel;
  191. unsigned int i;
  192. channel = pdata->channel;
  193. for (i = 0; i < pdata->channel_count; i++, channel++) {
  194. if (!channel->rx_ring)
  195. break;
  196. XGMAC_DMA_IOWRITE_BITS(channel, DMA_CH_RCR, PBL,
  197. pdata->rx_pbl);
  198. }
  199. return 0;
  200. }
  201. static int xgbe_config_osp_mode(struct xgbe_prv_data *pdata)
  202. {
  203. struct xgbe_channel *channel;
  204. unsigned int i;
  205. channel = pdata->channel;
  206. for (i = 0; i < pdata->channel_count; i++, channel++) {
  207. if (!channel->tx_ring)
  208. break;
  209. XGMAC_DMA_IOWRITE_BITS(channel, DMA_CH_TCR, OSP,
  210. pdata->tx_osp_mode);
  211. }
  212. return 0;
  213. }
  214. static int xgbe_config_rsf_mode(struct xgbe_prv_data *pdata, unsigned int val)
  215. {
  216. unsigned int i;
  217. for (i = 0; i < pdata->rx_q_count; i++)
  218. XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_Q_RQOMR, RSF, val);
  219. return 0;
  220. }
  221. static int xgbe_config_tsf_mode(struct xgbe_prv_data *pdata, unsigned int val)
  222. {
  223. unsigned int i;
  224. for (i = 0; i < pdata->tx_q_count; i++)
  225. XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_Q_TQOMR, TSF, val);
  226. return 0;
  227. }
  228. static int xgbe_config_rx_threshold(struct xgbe_prv_data *pdata,
  229. unsigned int val)
  230. {
  231. unsigned int i;
  232. for (i = 0; i < pdata->rx_q_count; i++)
  233. XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_Q_RQOMR, RTC, val);
  234. return 0;
  235. }
  236. static int xgbe_config_tx_threshold(struct xgbe_prv_data *pdata,
  237. unsigned int val)
  238. {
  239. unsigned int i;
  240. for (i = 0; i < pdata->tx_q_count; i++)
  241. XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_Q_TQOMR, TTC, val);
  242. return 0;
  243. }
  244. static int xgbe_config_rx_coalesce(struct xgbe_prv_data *pdata)
  245. {
  246. struct xgbe_channel *channel;
  247. unsigned int i;
  248. channel = pdata->channel;
  249. for (i = 0; i < pdata->channel_count; i++, channel++) {
  250. if (!channel->rx_ring)
  251. break;
  252. XGMAC_DMA_IOWRITE_BITS(channel, DMA_CH_RIWT, RWT,
  253. pdata->rx_riwt);
  254. }
  255. return 0;
  256. }
  257. static int xgbe_config_tx_coalesce(struct xgbe_prv_data *pdata)
  258. {
  259. return 0;
  260. }
  261. static void xgbe_config_rx_buffer_size(struct xgbe_prv_data *pdata)
  262. {
  263. struct xgbe_channel *channel;
  264. unsigned int i;
  265. channel = pdata->channel;
  266. for (i = 0; i < pdata->channel_count; i++, channel++) {
  267. if (!channel->rx_ring)
  268. break;
  269. XGMAC_DMA_IOWRITE_BITS(channel, DMA_CH_RCR, RBSZ,
  270. pdata->rx_buf_size);
  271. }
  272. }
  273. static void xgbe_config_tso_mode(struct xgbe_prv_data *pdata)
  274. {
  275. struct xgbe_channel *channel;
  276. unsigned int i;
  277. channel = pdata->channel;
  278. for (i = 0; i < pdata->channel_count; i++, channel++) {
  279. if (!channel->tx_ring)
  280. break;
  281. XGMAC_DMA_IOWRITE_BITS(channel, DMA_CH_TCR, TSE, 1);
  282. }
  283. }
  284. static void xgbe_config_sph_mode(struct xgbe_prv_data *pdata)
  285. {
  286. struct xgbe_channel *channel;
  287. unsigned int i;
  288. channel = pdata->channel;
  289. for (i = 0; i < pdata->channel_count; i++, channel++) {
  290. if (!channel->rx_ring)
  291. break;
  292. XGMAC_DMA_IOWRITE_BITS(channel, DMA_CH_CR, SPH, 1);
  293. }
  294. XGMAC_IOWRITE_BITS(pdata, MAC_RCR, HDSMS, XGBE_SPH_HDSMS_SIZE);
  295. }
  296. static int xgbe_write_rss_reg(struct xgbe_prv_data *pdata, unsigned int type,
  297. unsigned int index, unsigned int val)
  298. {
  299. unsigned int wait;
  300. int ret = 0;
  301. mutex_lock(&pdata->rss_mutex);
  302. if (XGMAC_IOREAD_BITS(pdata, MAC_RSSAR, OB)) {
  303. ret = -EBUSY;
  304. goto unlock;
  305. }
  306. XGMAC_IOWRITE(pdata, MAC_RSSDR, val);
  307. XGMAC_IOWRITE_BITS(pdata, MAC_RSSAR, RSSIA, index);
  308. XGMAC_IOWRITE_BITS(pdata, MAC_RSSAR, ADDRT, type);
  309. XGMAC_IOWRITE_BITS(pdata, MAC_RSSAR, CT, 0);
  310. XGMAC_IOWRITE_BITS(pdata, MAC_RSSAR, OB, 1);
  311. wait = 1000;
  312. while (wait--) {
  313. if (!XGMAC_IOREAD_BITS(pdata, MAC_RSSAR, OB))
  314. goto unlock;
  315. usleep_range(1000, 1500);
  316. }
  317. ret = -EBUSY;
  318. unlock:
  319. mutex_unlock(&pdata->rss_mutex);
  320. return ret;
  321. }
  322. static int xgbe_write_rss_hash_key(struct xgbe_prv_data *pdata)
  323. {
  324. unsigned int key_regs = sizeof(pdata->rss_key) / sizeof(u32);
  325. unsigned int *key = (unsigned int *)&pdata->rss_key;
  326. int ret;
  327. while (key_regs--) {
  328. ret = xgbe_write_rss_reg(pdata, XGBE_RSS_HASH_KEY_TYPE,
  329. key_regs, *key++);
  330. if (ret)
  331. return ret;
  332. }
  333. return 0;
  334. }
  335. static int xgbe_write_rss_lookup_table(struct xgbe_prv_data *pdata)
  336. {
  337. unsigned int i;
  338. int ret;
  339. for (i = 0; i < ARRAY_SIZE(pdata->rss_table); i++) {
  340. ret = xgbe_write_rss_reg(pdata,
  341. XGBE_RSS_LOOKUP_TABLE_TYPE, i,
  342. pdata->rss_table[i]);
  343. if (ret)
  344. return ret;
  345. }
  346. return 0;
  347. }
  348. static int xgbe_set_rss_hash_key(struct xgbe_prv_data *pdata, const u8 *key)
  349. {
  350. memcpy(pdata->rss_key, key, sizeof(pdata->rss_key));
  351. return xgbe_write_rss_hash_key(pdata);
  352. }
  353. static int xgbe_set_rss_lookup_table(struct xgbe_prv_data *pdata,
  354. const u32 *table)
  355. {
  356. unsigned int i;
  357. for (i = 0; i < ARRAY_SIZE(pdata->rss_table); i++)
  358. XGMAC_SET_BITS(pdata->rss_table[i], MAC_RSSDR, DMCH, table[i]);
  359. return xgbe_write_rss_lookup_table(pdata);
  360. }
  361. static int xgbe_enable_rss(struct xgbe_prv_data *pdata)
  362. {
  363. int ret;
  364. if (!pdata->hw_feat.rss)
  365. return -EOPNOTSUPP;
  366. /* Program the hash key */
  367. ret = xgbe_write_rss_hash_key(pdata);
  368. if (ret)
  369. return ret;
  370. /* Program the lookup table */
  371. ret = xgbe_write_rss_lookup_table(pdata);
  372. if (ret)
  373. return ret;
  374. /* Set the RSS options */
  375. XGMAC_IOWRITE(pdata, MAC_RSSCR, pdata->rss_options);
  376. /* Enable RSS */
  377. XGMAC_IOWRITE_BITS(pdata, MAC_RSSCR, RSSE, 1);
  378. return 0;
  379. }
  380. static int xgbe_disable_rss(struct xgbe_prv_data *pdata)
  381. {
  382. if (!pdata->hw_feat.rss)
  383. return -EOPNOTSUPP;
  384. XGMAC_IOWRITE_BITS(pdata, MAC_RSSCR, RSSE, 0);
  385. return 0;
  386. }
  387. static void xgbe_config_rss(struct xgbe_prv_data *pdata)
  388. {
  389. int ret;
  390. if (!pdata->hw_feat.rss)
  391. return;
  392. if (pdata->netdev->features & NETIF_F_RXHASH)
  393. ret = xgbe_enable_rss(pdata);
  394. else
  395. ret = xgbe_disable_rss(pdata);
  396. if (ret)
  397. netdev_err(pdata->netdev,
  398. "error configuring RSS, RSS disabled\n");
  399. }
  400. static int xgbe_disable_tx_flow_control(struct xgbe_prv_data *pdata)
  401. {
  402. unsigned int max_q_count, q_count;
  403. unsigned int reg, reg_val;
  404. unsigned int i;
  405. /* Clear MTL flow control */
  406. for (i = 0; i < pdata->rx_q_count; i++)
  407. XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_Q_RQOMR, EHFC, 0);
  408. /* Clear MAC flow control */
  409. max_q_count = XGMAC_MAX_FLOW_CONTROL_QUEUES;
  410. q_count = min_t(unsigned int, pdata->tx_q_count, max_q_count);
  411. reg = MAC_Q0TFCR;
  412. for (i = 0; i < q_count; i++) {
  413. reg_val = XGMAC_IOREAD(pdata, reg);
  414. XGMAC_SET_BITS(reg_val, MAC_Q0TFCR, TFE, 0);
  415. XGMAC_IOWRITE(pdata, reg, reg_val);
  416. reg += MAC_QTFCR_INC;
  417. }
  418. return 0;
  419. }
  420. static int xgbe_enable_tx_flow_control(struct xgbe_prv_data *pdata)
  421. {
  422. unsigned int max_q_count, q_count;
  423. unsigned int reg, reg_val;
  424. unsigned int i;
  425. /* Set MTL flow control */
  426. for (i = 0; i < pdata->rx_q_count; i++)
  427. XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_Q_RQOMR, EHFC, 1);
  428. /* Set MAC flow control */
  429. max_q_count = XGMAC_MAX_FLOW_CONTROL_QUEUES;
  430. q_count = min_t(unsigned int, pdata->tx_q_count, max_q_count);
  431. reg = MAC_Q0TFCR;
  432. for (i = 0; i < q_count; i++) {
  433. reg_val = XGMAC_IOREAD(pdata, reg);
  434. /* Enable transmit flow control */
  435. XGMAC_SET_BITS(reg_val, MAC_Q0TFCR, TFE, 1);
  436. /* Set pause time */
  437. XGMAC_SET_BITS(reg_val, MAC_Q0TFCR, PT, 0xffff);
  438. XGMAC_IOWRITE(pdata, reg, reg_val);
  439. reg += MAC_QTFCR_INC;
  440. }
  441. return 0;
  442. }
  443. static int xgbe_disable_rx_flow_control(struct xgbe_prv_data *pdata)
  444. {
  445. XGMAC_IOWRITE_BITS(pdata, MAC_RFCR, RFE, 0);
  446. return 0;
  447. }
  448. static int xgbe_enable_rx_flow_control(struct xgbe_prv_data *pdata)
  449. {
  450. XGMAC_IOWRITE_BITS(pdata, MAC_RFCR, RFE, 1);
  451. return 0;
  452. }
  453. static int xgbe_config_tx_flow_control(struct xgbe_prv_data *pdata)
  454. {
  455. struct ieee_pfc *pfc = pdata->pfc;
  456. if (pdata->tx_pause || (pfc && pfc->pfc_en))
  457. xgbe_enable_tx_flow_control(pdata);
  458. else
  459. xgbe_disable_tx_flow_control(pdata);
  460. return 0;
  461. }
  462. static int xgbe_config_rx_flow_control(struct xgbe_prv_data *pdata)
  463. {
  464. struct ieee_pfc *pfc = pdata->pfc;
  465. if (pdata->rx_pause || (pfc && pfc->pfc_en))
  466. xgbe_enable_rx_flow_control(pdata);
  467. else
  468. xgbe_disable_rx_flow_control(pdata);
  469. return 0;
  470. }
  471. static void xgbe_config_flow_control(struct xgbe_prv_data *pdata)
  472. {
  473. struct ieee_pfc *pfc = pdata->pfc;
  474. xgbe_config_tx_flow_control(pdata);
  475. xgbe_config_rx_flow_control(pdata);
  476. XGMAC_IOWRITE_BITS(pdata, MAC_RFCR, PFCE,
  477. (pfc && pfc->pfc_en) ? 1 : 0);
  478. }
  479. static void xgbe_enable_dma_interrupts(struct xgbe_prv_data *pdata)
  480. {
  481. struct xgbe_channel *channel;
  482. unsigned int dma_ch_isr, dma_ch_ier;
  483. unsigned int i;
  484. channel = pdata->channel;
  485. for (i = 0; i < pdata->channel_count; i++, channel++) {
  486. /* Clear all the interrupts which are set */
  487. dma_ch_isr = XGMAC_DMA_IOREAD(channel, DMA_CH_SR);
  488. XGMAC_DMA_IOWRITE(channel, DMA_CH_SR, dma_ch_isr);
  489. /* Clear all interrupt enable bits */
  490. dma_ch_ier = 0;
  491. /* Enable following interrupts
  492. * NIE - Normal Interrupt Summary Enable
  493. * AIE - Abnormal Interrupt Summary Enable
  494. * FBEE - Fatal Bus Error Enable
  495. */
  496. XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, NIE, 1);
  497. XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, AIE, 1);
  498. XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, FBEE, 1);
  499. if (channel->tx_ring) {
  500. /* Enable the following Tx interrupts
  501. * TIE - Transmit Interrupt Enable (unless using
  502. * per channel interrupts)
  503. */
  504. if (!pdata->per_channel_irq)
  505. XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, TIE, 1);
  506. }
  507. if (channel->rx_ring) {
  508. /* Enable following Rx interrupts
  509. * RBUE - Receive Buffer Unavailable Enable
  510. * RIE - Receive Interrupt Enable (unless using
  511. * per channel interrupts)
  512. */
  513. XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, RBUE, 1);
  514. if (!pdata->per_channel_irq)
  515. XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, RIE, 1);
  516. }
  517. XGMAC_DMA_IOWRITE(channel, DMA_CH_IER, dma_ch_ier);
  518. }
  519. }
  520. static void xgbe_enable_mtl_interrupts(struct xgbe_prv_data *pdata)
  521. {
  522. unsigned int mtl_q_isr;
  523. unsigned int q_count, i;
  524. q_count = max(pdata->hw_feat.tx_q_cnt, pdata->hw_feat.rx_q_cnt);
  525. for (i = 0; i < q_count; i++) {
  526. /* Clear all the interrupts which are set */
  527. mtl_q_isr = XGMAC_MTL_IOREAD(pdata, i, MTL_Q_ISR);
  528. XGMAC_MTL_IOWRITE(pdata, i, MTL_Q_ISR, mtl_q_isr);
  529. /* No MTL interrupts to be enabled */
  530. XGMAC_MTL_IOWRITE(pdata, i, MTL_Q_IER, 0);
  531. }
  532. }
  533. static void xgbe_enable_mac_interrupts(struct xgbe_prv_data *pdata)
  534. {
  535. unsigned int mac_ier = 0;
  536. /* Enable Timestamp interrupt */
  537. XGMAC_SET_BITS(mac_ier, MAC_IER, TSIE, 1);
  538. XGMAC_IOWRITE(pdata, MAC_IER, mac_ier);
  539. /* Enable all counter interrupts */
  540. XGMAC_IOWRITE_BITS(pdata, MMC_RIER, ALL_INTERRUPTS, 0xffffffff);
  541. XGMAC_IOWRITE_BITS(pdata, MMC_TIER, ALL_INTERRUPTS, 0xffffffff);
  542. }
  543. static int xgbe_set_gmii_speed(struct xgbe_prv_data *pdata)
  544. {
  545. if (XGMAC_IOREAD_BITS(pdata, MAC_TCR, SS) == 0x3)
  546. return 0;
  547. XGMAC_IOWRITE_BITS(pdata, MAC_TCR, SS, 0x3);
  548. return 0;
  549. }
  550. static int xgbe_set_gmii_2500_speed(struct xgbe_prv_data *pdata)
  551. {
  552. if (XGMAC_IOREAD_BITS(pdata, MAC_TCR, SS) == 0x2)
  553. return 0;
  554. XGMAC_IOWRITE_BITS(pdata, MAC_TCR, SS, 0x2);
  555. return 0;
  556. }
  557. static int xgbe_set_xgmii_speed(struct xgbe_prv_data *pdata)
  558. {
  559. if (XGMAC_IOREAD_BITS(pdata, MAC_TCR, SS) == 0)
  560. return 0;
  561. XGMAC_IOWRITE_BITS(pdata, MAC_TCR, SS, 0);
  562. return 0;
  563. }
  564. static int xgbe_set_promiscuous_mode(struct xgbe_prv_data *pdata,
  565. unsigned int enable)
  566. {
  567. unsigned int val = enable ? 1 : 0;
  568. if (XGMAC_IOREAD_BITS(pdata, MAC_PFR, PR) == val)
  569. return 0;
  570. DBGPR(" %s promiscuous mode\n", enable ? "entering" : "leaving");
  571. XGMAC_IOWRITE_BITS(pdata, MAC_PFR, PR, val);
  572. return 0;
  573. }
  574. static int xgbe_set_all_multicast_mode(struct xgbe_prv_data *pdata,
  575. unsigned int enable)
  576. {
  577. unsigned int val = enable ? 1 : 0;
  578. if (XGMAC_IOREAD_BITS(pdata, MAC_PFR, PM) == val)
  579. return 0;
  580. DBGPR(" %s allmulti mode\n", enable ? "entering" : "leaving");
  581. XGMAC_IOWRITE_BITS(pdata, MAC_PFR, PM, val);
  582. return 0;
  583. }
  584. static void xgbe_set_mac_reg(struct xgbe_prv_data *pdata,
  585. struct netdev_hw_addr *ha, unsigned int *mac_reg)
  586. {
  587. unsigned int mac_addr_hi, mac_addr_lo;
  588. u8 *mac_addr;
  589. mac_addr_lo = 0;
  590. mac_addr_hi = 0;
  591. if (ha) {
  592. mac_addr = (u8 *)&mac_addr_lo;
  593. mac_addr[0] = ha->addr[0];
  594. mac_addr[1] = ha->addr[1];
  595. mac_addr[2] = ha->addr[2];
  596. mac_addr[3] = ha->addr[3];
  597. mac_addr = (u8 *)&mac_addr_hi;
  598. mac_addr[0] = ha->addr[4];
  599. mac_addr[1] = ha->addr[5];
  600. DBGPR(" adding mac address %pM at 0x%04x\n", ha->addr,
  601. *mac_reg);
  602. XGMAC_SET_BITS(mac_addr_hi, MAC_MACA1HR, AE, 1);
  603. }
  604. XGMAC_IOWRITE(pdata, *mac_reg, mac_addr_hi);
  605. *mac_reg += MAC_MACA_INC;
  606. XGMAC_IOWRITE(pdata, *mac_reg, mac_addr_lo);
  607. *mac_reg += MAC_MACA_INC;
  608. }
  609. static void xgbe_set_mac_addn_addrs(struct xgbe_prv_data *pdata)
  610. {
  611. struct net_device *netdev = pdata->netdev;
  612. struct netdev_hw_addr *ha;
  613. unsigned int mac_reg;
  614. unsigned int addn_macs;
  615. mac_reg = MAC_MACA1HR;
  616. addn_macs = pdata->hw_feat.addn_mac;
  617. if (netdev_uc_count(netdev) > addn_macs) {
  618. xgbe_set_promiscuous_mode(pdata, 1);
  619. } else {
  620. netdev_for_each_uc_addr(ha, netdev) {
  621. xgbe_set_mac_reg(pdata, ha, &mac_reg);
  622. addn_macs--;
  623. }
  624. if (netdev_mc_count(netdev) > addn_macs) {
  625. xgbe_set_all_multicast_mode(pdata, 1);
  626. } else {
  627. netdev_for_each_mc_addr(ha, netdev) {
  628. xgbe_set_mac_reg(pdata, ha, &mac_reg);
  629. addn_macs--;
  630. }
  631. }
  632. }
  633. /* Clear remaining additional MAC address entries */
  634. while (addn_macs--)
  635. xgbe_set_mac_reg(pdata, NULL, &mac_reg);
  636. }
  637. static void xgbe_set_mac_hash_table(struct xgbe_prv_data *pdata)
  638. {
  639. struct net_device *netdev = pdata->netdev;
  640. struct netdev_hw_addr *ha;
  641. unsigned int hash_reg;
  642. unsigned int hash_table_shift, hash_table_count;
  643. u32 hash_table[XGBE_MAC_HASH_TABLE_SIZE];
  644. u32 crc;
  645. unsigned int i;
  646. hash_table_shift = 26 - (pdata->hw_feat.hash_table_size >> 7);
  647. hash_table_count = pdata->hw_feat.hash_table_size / 32;
  648. memset(hash_table, 0, sizeof(hash_table));
  649. /* Build the MAC Hash Table register values */
  650. netdev_for_each_uc_addr(ha, netdev) {
  651. crc = bitrev32(~crc32_le(~0, ha->addr, ETH_ALEN));
  652. crc >>= hash_table_shift;
  653. hash_table[crc >> 5] |= (1 << (crc & 0x1f));
  654. }
  655. netdev_for_each_mc_addr(ha, netdev) {
  656. crc = bitrev32(~crc32_le(~0, ha->addr, ETH_ALEN));
  657. crc >>= hash_table_shift;
  658. hash_table[crc >> 5] |= (1 << (crc & 0x1f));
  659. }
  660. /* Set the MAC Hash Table registers */
  661. hash_reg = MAC_HTR0;
  662. for (i = 0; i < hash_table_count; i++) {
  663. XGMAC_IOWRITE(pdata, hash_reg, hash_table[i]);
  664. hash_reg += MAC_HTR_INC;
  665. }
  666. }
  667. static int xgbe_add_mac_addresses(struct xgbe_prv_data *pdata)
  668. {
  669. if (pdata->hw_feat.hash_table_size)
  670. xgbe_set_mac_hash_table(pdata);
  671. else
  672. xgbe_set_mac_addn_addrs(pdata);
  673. return 0;
  674. }
  675. static int xgbe_set_mac_address(struct xgbe_prv_data *pdata, u8 *addr)
  676. {
  677. unsigned int mac_addr_hi, mac_addr_lo;
  678. mac_addr_hi = (addr[5] << 8) | (addr[4] << 0);
  679. mac_addr_lo = (addr[3] << 24) | (addr[2] << 16) |
  680. (addr[1] << 8) | (addr[0] << 0);
  681. XGMAC_IOWRITE(pdata, MAC_MACA0HR, mac_addr_hi);
  682. XGMAC_IOWRITE(pdata, MAC_MACA0LR, mac_addr_lo);
  683. return 0;
  684. }
  685. static int xgbe_read_mmd_regs(struct xgbe_prv_data *pdata, int prtad,
  686. int mmd_reg)
  687. {
  688. unsigned int mmd_address;
  689. int mmd_data;
  690. if (mmd_reg & MII_ADDR_C45)
  691. mmd_address = mmd_reg & ~MII_ADDR_C45;
  692. else
  693. mmd_address = (pdata->mdio_mmd << 16) | (mmd_reg & 0xffff);
  694. /* The PCS registers are accessed using mmio. The underlying APB3
  695. * management interface uses indirect addressing to access the MMD
  696. * register sets. This requires accessing of the PCS register in two
  697. * phases, an address phase and a data phase.
  698. *
  699. * The mmio interface is based on 32-bit offsets and values. All
  700. * register offsets must therefore be adjusted by left shifting the
  701. * offset 2 bits and reading 32 bits of data.
  702. */
  703. mutex_lock(&pdata->xpcs_mutex);
  704. XPCS_IOWRITE(pdata, PCS_MMD_SELECT << 2, mmd_address >> 8);
  705. mmd_data = XPCS_IOREAD(pdata, (mmd_address & 0xff) << 2);
  706. mutex_unlock(&pdata->xpcs_mutex);
  707. return mmd_data;
  708. }
  709. static void xgbe_write_mmd_regs(struct xgbe_prv_data *pdata, int prtad,
  710. int mmd_reg, int mmd_data)
  711. {
  712. unsigned int mmd_address;
  713. if (mmd_reg & MII_ADDR_C45)
  714. mmd_address = mmd_reg & ~MII_ADDR_C45;
  715. else
  716. mmd_address = (pdata->mdio_mmd << 16) | (mmd_reg & 0xffff);
  717. /* If the PCS is changing modes, match the MAC speed to it */
  718. if (((mmd_address >> 16) == MDIO_MMD_PCS) &&
  719. ((mmd_address & 0xffff) == MDIO_CTRL2)) {
  720. struct phy_device *phydev = pdata->phydev;
  721. if (mmd_data & MDIO_PCS_CTRL2_TYPE) {
  722. /* KX mode */
  723. if (phydev->supported & SUPPORTED_1000baseKX_Full)
  724. xgbe_set_gmii_speed(pdata);
  725. else
  726. xgbe_set_gmii_2500_speed(pdata);
  727. } else {
  728. /* KR mode */
  729. xgbe_set_xgmii_speed(pdata);
  730. }
  731. }
  732. /* The PCS registers are accessed using mmio. The underlying APB3
  733. * management interface uses indirect addressing to access the MMD
  734. * register sets. This requires accessing of the PCS register in two
  735. * phases, an address phase and a data phase.
  736. *
  737. * The mmio interface is based on 32-bit offsets and values. All
  738. * register offsets must therefore be adjusted by left shifting the
  739. * offset 2 bits and reading 32 bits of data.
  740. */
  741. mutex_lock(&pdata->xpcs_mutex);
  742. XPCS_IOWRITE(pdata, PCS_MMD_SELECT << 2, mmd_address >> 8);
  743. XPCS_IOWRITE(pdata, (mmd_address & 0xff) << 2, mmd_data);
  744. mutex_unlock(&pdata->xpcs_mutex);
  745. }
  746. static int xgbe_tx_complete(struct xgbe_ring_desc *rdesc)
  747. {
  748. return !XGMAC_GET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, OWN);
  749. }
  750. static int xgbe_disable_rx_csum(struct xgbe_prv_data *pdata)
  751. {
  752. XGMAC_IOWRITE_BITS(pdata, MAC_RCR, IPC, 0);
  753. return 0;
  754. }
  755. static int xgbe_enable_rx_csum(struct xgbe_prv_data *pdata)
  756. {
  757. XGMAC_IOWRITE_BITS(pdata, MAC_RCR, IPC, 1);
  758. return 0;
  759. }
  760. static int xgbe_enable_rx_vlan_stripping(struct xgbe_prv_data *pdata)
  761. {
  762. /* Put the VLAN tag in the Rx descriptor */
  763. XGMAC_IOWRITE_BITS(pdata, MAC_VLANTR, EVLRXS, 1);
  764. /* Don't check the VLAN type */
  765. XGMAC_IOWRITE_BITS(pdata, MAC_VLANTR, DOVLTC, 1);
  766. /* Check only C-TAG (0x8100) packets */
  767. XGMAC_IOWRITE_BITS(pdata, MAC_VLANTR, ERSVLM, 0);
  768. /* Don't consider an S-TAG (0x88A8) packet as a VLAN packet */
  769. XGMAC_IOWRITE_BITS(pdata, MAC_VLANTR, ESVL, 0);
  770. /* Enable VLAN tag stripping */
  771. XGMAC_IOWRITE_BITS(pdata, MAC_VLANTR, EVLS, 0x3);
  772. return 0;
  773. }
  774. static int xgbe_disable_rx_vlan_stripping(struct xgbe_prv_data *pdata)
  775. {
  776. XGMAC_IOWRITE_BITS(pdata, MAC_VLANTR, EVLS, 0);
  777. return 0;
  778. }
  779. static int xgbe_enable_rx_vlan_filtering(struct xgbe_prv_data *pdata)
  780. {
  781. /* Enable VLAN filtering */
  782. XGMAC_IOWRITE_BITS(pdata, MAC_PFR, VTFE, 1);
  783. /* Enable VLAN Hash Table filtering */
  784. XGMAC_IOWRITE_BITS(pdata, MAC_VLANTR, VTHM, 1);
  785. /* Disable VLAN tag inverse matching */
  786. XGMAC_IOWRITE_BITS(pdata, MAC_VLANTR, VTIM, 0);
  787. /* Only filter on the lower 12-bits of the VLAN tag */
  788. XGMAC_IOWRITE_BITS(pdata, MAC_VLANTR, ETV, 1);
  789. /* In order for the VLAN Hash Table filtering to be effective,
  790. * the VLAN tag identifier in the VLAN Tag Register must not
  791. * be zero. Set the VLAN tag identifier to "1" to enable the
  792. * VLAN Hash Table filtering. This implies that a VLAN tag of
  793. * 1 will always pass filtering.
  794. */
  795. XGMAC_IOWRITE_BITS(pdata, MAC_VLANTR, VL, 1);
  796. return 0;
  797. }
  798. static int xgbe_disable_rx_vlan_filtering(struct xgbe_prv_data *pdata)
  799. {
  800. /* Disable VLAN filtering */
  801. XGMAC_IOWRITE_BITS(pdata, MAC_PFR, VTFE, 0);
  802. return 0;
  803. }
  804. #ifndef CRCPOLY_LE
  805. #define CRCPOLY_LE 0xedb88320
  806. #endif
  807. static u32 xgbe_vid_crc32_le(__le16 vid_le)
  808. {
  809. u32 poly = CRCPOLY_LE;
  810. u32 crc = ~0;
  811. u32 temp = 0;
  812. unsigned char *data = (unsigned char *)&vid_le;
  813. unsigned char data_byte = 0;
  814. int i, bits;
  815. bits = get_bitmask_order(VLAN_VID_MASK);
  816. for (i = 0; i < bits; i++) {
  817. if ((i % 8) == 0)
  818. data_byte = data[i / 8];
  819. temp = ((crc & 1) ^ data_byte) & 1;
  820. crc >>= 1;
  821. data_byte >>= 1;
  822. if (temp)
  823. crc ^= poly;
  824. }
  825. return crc;
  826. }
  827. static int xgbe_update_vlan_hash_table(struct xgbe_prv_data *pdata)
  828. {
  829. u32 crc;
  830. u16 vid;
  831. __le16 vid_le;
  832. u16 vlan_hash_table = 0;
  833. /* Generate the VLAN Hash Table value */
  834. for_each_set_bit(vid, pdata->active_vlans, VLAN_N_VID) {
  835. /* Get the CRC32 value of the VLAN ID */
  836. vid_le = cpu_to_le16(vid);
  837. crc = bitrev32(~xgbe_vid_crc32_le(vid_le)) >> 28;
  838. vlan_hash_table |= (1 << crc);
  839. }
  840. /* Set the VLAN Hash Table filtering register */
  841. XGMAC_IOWRITE_BITS(pdata, MAC_VLANHTR, VLHT, vlan_hash_table);
  842. return 0;
  843. }
  844. static void xgbe_tx_desc_reset(struct xgbe_ring_data *rdata)
  845. {
  846. struct xgbe_ring_desc *rdesc = rdata->rdesc;
  847. /* Reset the Tx descriptor
  848. * Set buffer 1 (lo) address to zero
  849. * Set buffer 1 (hi) address to zero
  850. * Reset all other control bits (IC, TTSE, B2L & B1L)
  851. * Reset all other control bits (OWN, CTXT, FD, LD, CPC, CIC, etc)
  852. */
  853. rdesc->desc0 = 0;
  854. rdesc->desc1 = 0;
  855. rdesc->desc2 = 0;
  856. rdesc->desc3 = 0;
  857. /* Make sure ownership is written to the descriptor */
  858. dma_wmb();
  859. }
  860. static void xgbe_tx_desc_init(struct xgbe_channel *channel)
  861. {
  862. struct xgbe_ring *ring = channel->tx_ring;
  863. struct xgbe_ring_data *rdata;
  864. int i;
  865. int start_index = ring->cur;
  866. DBGPR("-->tx_desc_init\n");
  867. /* Initialze all descriptors */
  868. for (i = 0; i < ring->rdesc_count; i++) {
  869. rdata = XGBE_GET_DESC_DATA(ring, i);
  870. /* Initialize Tx descriptor */
  871. xgbe_tx_desc_reset(rdata);
  872. }
  873. /* Update the total number of Tx descriptors */
  874. XGMAC_DMA_IOWRITE(channel, DMA_CH_TDRLR, ring->rdesc_count - 1);
  875. /* Update the starting address of descriptor ring */
  876. rdata = XGBE_GET_DESC_DATA(ring, start_index);
  877. XGMAC_DMA_IOWRITE(channel, DMA_CH_TDLR_HI,
  878. upper_32_bits(rdata->rdesc_dma));
  879. XGMAC_DMA_IOWRITE(channel, DMA_CH_TDLR_LO,
  880. lower_32_bits(rdata->rdesc_dma));
  881. DBGPR("<--tx_desc_init\n");
  882. }
  883. static void xgbe_rx_desc_reset(struct xgbe_ring_data *rdata)
  884. {
  885. struct xgbe_ring_desc *rdesc = rdata->rdesc;
  886. /* Reset the Rx descriptor
  887. * Set buffer 1 (lo) address to header dma address (lo)
  888. * Set buffer 1 (hi) address to header dma address (hi)
  889. * Set buffer 2 (lo) address to buffer dma address (lo)
  890. * Set buffer 2 (hi) address to buffer dma address (hi) and
  891. * set control bits OWN and INTE
  892. */
  893. rdesc->desc0 = cpu_to_le32(lower_32_bits(rdata->rx.hdr.dma));
  894. rdesc->desc1 = cpu_to_le32(upper_32_bits(rdata->rx.hdr.dma));
  895. rdesc->desc2 = cpu_to_le32(lower_32_bits(rdata->rx.buf.dma));
  896. rdesc->desc3 = cpu_to_le32(upper_32_bits(rdata->rx.buf.dma));
  897. XGMAC_SET_BITS_LE(rdesc->desc3, RX_NORMAL_DESC3, INTE,
  898. rdata->interrupt ? 1 : 0);
  899. /* Since the Rx DMA engine is likely running, make sure everything
  900. * is written to the descriptor(s) before setting the OWN bit
  901. * for the descriptor
  902. */
  903. dma_wmb();
  904. XGMAC_SET_BITS_LE(rdesc->desc3, RX_NORMAL_DESC3, OWN, 1);
  905. /* Make sure ownership is written to the descriptor */
  906. dma_wmb();
  907. }
  908. static void xgbe_rx_desc_init(struct xgbe_channel *channel)
  909. {
  910. struct xgbe_prv_data *pdata = channel->pdata;
  911. struct xgbe_ring *ring = channel->rx_ring;
  912. struct xgbe_ring_data *rdata;
  913. unsigned int start_index = ring->cur;
  914. unsigned int rx_coalesce, rx_frames;
  915. unsigned int i;
  916. DBGPR("-->rx_desc_init\n");
  917. rx_coalesce = (pdata->rx_riwt || pdata->rx_frames) ? 1 : 0;
  918. rx_frames = pdata->rx_frames;
  919. /* Initialize all descriptors */
  920. for (i = 0; i < ring->rdesc_count; i++) {
  921. rdata = XGBE_GET_DESC_DATA(ring, i);
  922. /* Set interrupt on completion bit as appropriate */
  923. if (rx_coalesce && (!rx_frames || ((i + 1) % rx_frames)))
  924. rdata->interrupt = 0;
  925. else
  926. rdata->interrupt = 1;
  927. /* Initialize Rx descriptor */
  928. xgbe_rx_desc_reset(rdata);
  929. }
  930. /* Update the total number of Rx descriptors */
  931. XGMAC_DMA_IOWRITE(channel, DMA_CH_RDRLR, ring->rdesc_count - 1);
  932. /* Update the starting address of descriptor ring */
  933. rdata = XGBE_GET_DESC_DATA(ring, start_index);
  934. XGMAC_DMA_IOWRITE(channel, DMA_CH_RDLR_HI,
  935. upper_32_bits(rdata->rdesc_dma));
  936. XGMAC_DMA_IOWRITE(channel, DMA_CH_RDLR_LO,
  937. lower_32_bits(rdata->rdesc_dma));
  938. /* Update the Rx Descriptor Tail Pointer */
  939. rdata = XGBE_GET_DESC_DATA(ring, start_index + ring->rdesc_count - 1);
  940. XGMAC_DMA_IOWRITE(channel, DMA_CH_RDTR_LO,
  941. lower_32_bits(rdata->rdesc_dma));
  942. DBGPR("<--rx_desc_init\n");
  943. }
  944. static void xgbe_update_tstamp_addend(struct xgbe_prv_data *pdata,
  945. unsigned int addend)
  946. {
  947. /* Set the addend register value and tell the device */
  948. XGMAC_IOWRITE(pdata, MAC_TSAR, addend);
  949. XGMAC_IOWRITE_BITS(pdata, MAC_TSCR, TSADDREG, 1);
  950. /* Wait for addend update to complete */
  951. while (XGMAC_IOREAD_BITS(pdata, MAC_TSCR, TSADDREG))
  952. udelay(5);
  953. }
  954. static void xgbe_set_tstamp_time(struct xgbe_prv_data *pdata, unsigned int sec,
  955. unsigned int nsec)
  956. {
  957. /* Set the time values and tell the device */
  958. XGMAC_IOWRITE(pdata, MAC_STSUR, sec);
  959. XGMAC_IOWRITE(pdata, MAC_STNUR, nsec);
  960. XGMAC_IOWRITE_BITS(pdata, MAC_TSCR, TSINIT, 1);
  961. /* Wait for time update to complete */
  962. while (XGMAC_IOREAD_BITS(pdata, MAC_TSCR, TSINIT))
  963. udelay(5);
  964. }
  965. static u64 xgbe_get_tstamp_time(struct xgbe_prv_data *pdata)
  966. {
  967. u64 nsec;
  968. nsec = XGMAC_IOREAD(pdata, MAC_STSR);
  969. nsec *= NSEC_PER_SEC;
  970. nsec += XGMAC_IOREAD(pdata, MAC_STNR);
  971. return nsec;
  972. }
  973. static u64 xgbe_get_tx_tstamp(struct xgbe_prv_data *pdata)
  974. {
  975. unsigned int tx_snr;
  976. u64 nsec;
  977. tx_snr = XGMAC_IOREAD(pdata, MAC_TXSNR);
  978. if (XGMAC_GET_BITS(tx_snr, MAC_TXSNR, TXTSSTSMIS))
  979. return 0;
  980. nsec = XGMAC_IOREAD(pdata, MAC_TXSSR);
  981. nsec *= NSEC_PER_SEC;
  982. nsec += tx_snr;
  983. return nsec;
  984. }
  985. static void xgbe_get_rx_tstamp(struct xgbe_packet_data *packet,
  986. struct xgbe_ring_desc *rdesc)
  987. {
  988. u64 nsec;
  989. if (XGMAC_GET_BITS_LE(rdesc->desc3, RX_CONTEXT_DESC3, TSA) &&
  990. !XGMAC_GET_BITS_LE(rdesc->desc3, RX_CONTEXT_DESC3, TSD)) {
  991. nsec = le32_to_cpu(rdesc->desc1);
  992. nsec <<= 32;
  993. nsec |= le32_to_cpu(rdesc->desc0);
  994. if (nsec != 0xffffffffffffffffULL) {
  995. packet->rx_tstamp = nsec;
  996. XGMAC_SET_BITS(packet->attributes, RX_PACKET_ATTRIBUTES,
  997. RX_TSTAMP, 1);
  998. }
  999. }
  1000. }
  1001. static int xgbe_config_tstamp(struct xgbe_prv_data *pdata,
  1002. unsigned int mac_tscr)
  1003. {
  1004. /* Set one nano-second accuracy */
  1005. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSCTRLSSR, 1);
  1006. /* Set fine timestamp update */
  1007. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSCFUPDT, 1);
  1008. /* Overwrite earlier timestamps */
  1009. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TXTSSTSM, 1);
  1010. XGMAC_IOWRITE(pdata, MAC_TSCR, mac_tscr);
  1011. /* Exit if timestamping is not enabled */
  1012. if (!XGMAC_GET_BITS(mac_tscr, MAC_TSCR, TSENA))
  1013. return 0;
  1014. /* Initialize time registers */
  1015. XGMAC_IOWRITE_BITS(pdata, MAC_SSIR, SSINC, XGBE_TSTAMP_SSINC);
  1016. XGMAC_IOWRITE_BITS(pdata, MAC_SSIR, SNSINC, XGBE_TSTAMP_SNSINC);
  1017. xgbe_update_tstamp_addend(pdata, pdata->tstamp_addend);
  1018. xgbe_set_tstamp_time(pdata, 0, 0);
  1019. /* Initialize the timecounter */
  1020. timecounter_init(&pdata->tstamp_tc, &pdata->tstamp_cc,
  1021. ktime_to_ns(ktime_get_real()));
  1022. return 0;
  1023. }
  1024. static void xgbe_config_dcb_tc(struct xgbe_prv_data *pdata)
  1025. {
  1026. struct ieee_ets *ets = pdata->ets;
  1027. unsigned int total_weight, min_weight, weight;
  1028. unsigned int i;
  1029. if (!ets)
  1030. return;
  1031. /* Set Tx to deficit weighted round robin scheduling algorithm (when
  1032. * traffic class is using ETS algorithm)
  1033. */
  1034. XGMAC_IOWRITE_BITS(pdata, MTL_OMR, ETSALG, MTL_ETSALG_DWRR);
  1035. /* Set Traffic Class algorithms */
  1036. total_weight = pdata->netdev->mtu * pdata->hw_feat.tc_cnt;
  1037. min_weight = total_weight / 100;
  1038. if (!min_weight)
  1039. min_weight = 1;
  1040. for (i = 0; i < pdata->hw_feat.tc_cnt; i++) {
  1041. switch (ets->tc_tsa[i]) {
  1042. case IEEE_8021QAZ_TSA_STRICT:
  1043. DBGPR(" TC%u using SP\n", i);
  1044. XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_TC_ETSCR, TSA,
  1045. MTL_TSA_SP);
  1046. break;
  1047. case IEEE_8021QAZ_TSA_ETS:
  1048. weight = total_weight * ets->tc_tx_bw[i] / 100;
  1049. weight = clamp(weight, min_weight, total_weight);
  1050. DBGPR(" TC%u using DWRR (weight %u)\n", i, weight);
  1051. XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_TC_ETSCR, TSA,
  1052. MTL_TSA_ETS);
  1053. XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_TC_QWR, QW,
  1054. weight);
  1055. break;
  1056. }
  1057. }
  1058. }
  1059. static void xgbe_config_dcb_pfc(struct xgbe_prv_data *pdata)
  1060. {
  1061. struct ieee_pfc *pfc = pdata->pfc;
  1062. struct ieee_ets *ets = pdata->ets;
  1063. unsigned int mask, reg, reg_val;
  1064. unsigned int tc, prio;
  1065. if (!pfc || !ets)
  1066. return;
  1067. for (tc = 0; tc < pdata->hw_feat.tc_cnt; tc++) {
  1068. mask = 0;
  1069. for (prio = 0; prio < IEEE_8021QAZ_MAX_TCS; prio++) {
  1070. if ((pfc->pfc_en & (1 << prio)) &&
  1071. (ets->prio_tc[prio] == tc))
  1072. mask |= (1 << prio);
  1073. }
  1074. mask &= 0xff;
  1075. DBGPR(" TC%u PFC mask=%#x\n", tc, mask);
  1076. reg = MTL_TCPM0R + (MTL_TCPM_INC * (tc / MTL_TCPM_TC_PER_REG));
  1077. reg_val = XGMAC_IOREAD(pdata, reg);
  1078. reg_val &= ~(0xff << ((tc % MTL_TCPM_TC_PER_REG) << 3));
  1079. reg_val |= (mask << ((tc % MTL_TCPM_TC_PER_REG) << 3));
  1080. XGMAC_IOWRITE(pdata, reg, reg_val);
  1081. }
  1082. xgbe_config_flow_control(pdata);
  1083. }
  1084. static void xgbe_tx_start_xmit(struct xgbe_channel *channel,
  1085. struct xgbe_ring *ring)
  1086. {
  1087. struct xgbe_prv_data *pdata = channel->pdata;
  1088. struct xgbe_ring_data *rdata;
  1089. /* Make sure everything is written before the register write */
  1090. wmb();
  1091. /* Issue a poll command to Tx DMA by writing address
  1092. * of next immediate free descriptor */
  1093. rdata = XGBE_GET_DESC_DATA(ring, ring->cur);
  1094. XGMAC_DMA_IOWRITE(channel, DMA_CH_TDTR_LO,
  1095. lower_32_bits(rdata->rdesc_dma));
  1096. /* Start the Tx timer */
  1097. if (pdata->tx_usecs && !channel->tx_timer_active) {
  1098. channel->tx_timer_active = 1;
  1099. mod_timer(&channel->tx_timer,
  1100. jiffies + usecs_to_jiffies(pdata->tx_usecs));
  1101. }
  1102. ring->tx.xmit_more = 0;
  1103. }
  1104. static void xgbe_dev_xmit(struct xgbe_channel *channel)
  1105. {
  1106. struct xgbe_prv_data *pdata = channel->pdata;
  1107. struct xgbe_ring *ring = channel->tx_ring;
  1108. struct xgbe_ring_data *rdata;
  1109. struct xgbe_ring_desc *rdesc;
  1110. struct xgbe_packet_data *packet = &ring->packet_data;
  1111. unsigned int csum, tso, vlan;
  1112. unsigned int tso_context, vlan_context;
  1113. unsigned int tx_set_ic;
  1114. int start_index = ring->cur;
  1115. int cur_index = ring->cur;
  1116. int i;
  1117. DBGPR("-->xgbe_dev_xmit\n");
  1118. csum = XGMAC_GET_BITS(packet->attributes, TX_PACKET_ATTRIBUTES,
  1119. CSUM_ENABLE);
  1120. tso = XGMAC_GET_BITS(packet->attributes, TX_PACKET_ATTRIBUTES,
  1121. TSO_ENABLE);
  1122. vlan = XGMAC_GET_BITS(packet->attributes, TX_PACKET_ATTRIBUTES,
  1123. VLAN_CTAG);
  1124. if (tso && (packet->mss != ring->tx.cur_mss))
  1125. tso_context = 1;
  1126. else
  1127. tso_context = 0;
  1128. if (vlan && (packet->vlan_ctag != ring->tx.cur_vlan_ctag))
  1129. vlan_context = 1;
  1130. else
  1131. vlan_context = 0;
  1132. /* Determine if an interrupt should be generated for this Tx:
  1133. * Interrupt:
  1134. * - Tx frame count exceeds the frame count setting
  1135. * - Addition of Tx frame count to the frame count since the
  1136. * last interrupt was set exceeds the frame count setting
  1137. * No interrupt:
  1138. * - No frame count setting specified (ethtool -C ethX tx-frames 0)
  1139. * - Addition of Tx frame count to the frame count since the
  1140. * last interrupt was set does not exceed the frame count setting
  1141. */
  1142. ring->coalesce_count += packet->tx_packets;
  1143. if (!pdata->tx_frames)
  1144. tx_set_ic = 0;
  1145. else if (packet->tx_packets > pdata->tx_frames)
  1146. tx_set_ic = 1;
  1147. else if ((ring->coalesce_count % pdata->tx_frames) <
  1148. packet->tx_packets)
  1149. tx_set_ic = 1;
  1150. else
  1151. tx_set_ic = 0;
  1152. rdata = XGBE_GET_DESC_DATA(ring, cur_index);
  1153. rdesc = rdata->rdesc;
  1154. /* Create a context descriptor if this is a TSO packet */
  1155. if (tso_context || vlan_context) {
  1156. if (tso_context) {
  1157. DBGPR(" TSO context descriptor, mss=%u\n",
  1158. packet->mss);
  1159. /* Set the MSS size */
  1160. XGMAC_SET_BITS_LE(rdesc->desc2, TX_CONTEXT_DESC2,
  1161. MSS, packet->mss);
  1162. /* Mark it as a CONTEXT descriptor */
  1163. XGMAC_SET_BITS_LE(rdesc->desc3, TX_CONTEXT_DESC3,
  1164. CTXT, 1);
  1165. /* Indicate this descriptor contains the MSS */
  1166. XGMAC_SET_BITS_LE(rdesc->desc3, TX_CONTEXT_DESC3,
  1167. TCMSSV, 1);
  1168. ring->tx.cur_mss = packet->mss;
  1169. }
  1170. if (vlan_context) {
  1171. DBGPR(" VLAN context descriptor, ctag=%u\n",
  1172. packet->vlan_ctag);
  1173. /* Mark it as a CONTEXT descriptor */
  1174. XGMAC_SET_BITS_LE(rdesc->desc3, TX_CONTEXT_DESC3,
  1175. CTXT, 1);
  1176. /* Set the VLAN tag */
  1177. XGMAC_SET_BITS_LE(rdesc->desc3, TX_CONTEXT_DESC3,
  1178. VT, packet->vlan_ctag);
  1179. /* Indicate this descriptor contains the VLAN tag */
  1180. XGMAC_SET_BITS_LE(rdesc->desc3, TX_CONTEXT_DESC3,
  1181. VLTV, 1);
  1182. ring->tx.cur_vlan_ctag = packet->vlan_ctag;
  1183. }
  1184. cur_index++;
  1185. rdata = XGBE_GET_DESC_DATA(ring, cur_index);
  1186. rdesc = rdata->rdesc;
  1187. }
  1188. /* Update buffer address (for TSO this is the header) */
  1189. rdesc->desc0 = cpu_to_le32(lower_32_bits(rdata->skb_dma));
  1190. rdesc->desc1 = cpu_to_le32(upper_32_bits(rdata->skb_dma));
  1191. /* Update the buffer length */
  1192. XGMAC_SET_BITS_LE(rdesc->desc2, TX_NORMAL_DESC2, HL_B1L,
  1193. rdata->skb_dma_len);
  1194. /* VLAN tag insertion check */
  1195. if (vlan)
  1196. XGMAC_SET_BITS_LE(rdesc->desc2, TX_NORMAL_DESC2, VTIR,
  1197. TX_NORMAL_DESC2_VLAN_INSERT);
  1198. /* Timestamp enablement check */
  1199. if (XGMAC_GET_BITS(packet->attributes, TX_PACKET_ATTRIBUTES, PTP))
  1200. XGMAC_SET_BITS_LE(rdesc->desc2, TX_NORMAL_DESC2, TTSE, 1);
  1201. /* Mark it as First Descriptor */
  1202. XGMAC_SET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, FD, 1);
  1203. /* Mark it as a NORMAL descriptor */
  1204. XGMAC_SET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, CTXT, 0);
  1205. /* Set OWN bit if not the first descriptor */
  1206. if (cur_index != start_index)
  1207. XGMAC_SET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, OWN, 1);
  1208. if (tso) {
  1209. /* Enable TSO */
  1210. XGMAC_SET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, TSE, 1);
  1211. XGMAC_SET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, TCPPL,
  1212. packet->tcp_payload_len);
  1213. XGMAC_SET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, TCPHDRLEN,
  1214. packet->tcp_header_len / 4);
  1215. } else {
  1216. /* Enable CRC and Pad Insertion */
  1217. XGMAC_SET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, CPC, 0);
  1218. /* Enable HW CSUM */
  1219. if (csum)
  1220. XGMAC_SET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3,
  1221. CIC, 0x3);
  1222. /* Set the total length to be transmitted */
  1223. XGMAC_SET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, FL,
  1224. packet->length);
  1225. }
  1226. for (i = cur_index - start_index + 1; i < packet->rdesc_count; i++) {
  1227. cur_index++;
  1228. rdata = XGBE_GET_DESC_DATA(ring, cur_index);
  1229. rdesc = rdata->rdesc;
  1230. /* Update buffer address */
  1231. rdesc->desc0 = cpu_to_le32(lower_32_bits(rdata->skb_dma));
  1232. rdesc->desc1 = cpu_to_le32(upper_32_bits(rdata->skb_dma));
  1233. /* Update the buffer length */
  1234. XGMAC_SET_BITS_LE(rdesc->desc2, TX_NORMAL_DESC2, HL_B1L,
  1235. rdata->skb_dma_len);
  1236. /* Set OWN bit */
  1237. XGMAC_SET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, OWN, 1);
  1238. /* Mark it as NORMAL descriptor */
  1239. XGMAC_SET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, CTXT, 0);
  1240. /* Enable HW CSUM */
  1241. if (csum)
  1242. XGMAC_SET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3,
  1243. CIC, 0x3);
  1244. }
  1245. /* Set LAST bit for the last descriptor */
  1246. XGMAC_SET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, LD, 1);
  1247. /* Set IC bit based on Tx coalescing settings */
  1248. if (tx_set_ic)
  1249. XGMAC_SET_BITS_LE(rdesc->desc2, TX_NORMAL_DESC2, IC, 1);
  1250. /* Save the Tx info to report back during cleanup */
  1251. rdata->tx.packets = packet->tx_packets;
  1252. rdata->tx.bytes = packet->tx_bytes;
  1253. /* In case the Tx DMA engine is running, make sure everything
  1254. * is written to the descriptor(s) before setting the OWN bit
  1255. * for the first descriptor
  1256. */
  1257. dma_wmb();
  1258. /* Set OWN bit for the first descriptor */
  1259. rdata = XGBE_GET_DESC_DATA(ring, start_index);
  1260. rdesc = rdata->rdesc;
  1261. XGMAC_SET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, OWN, 1);
  1262. #ifdef XGMAC_ENABLE_TX_DESC_DUMP
  1263. xgbe_dump_tx_desc(ring, start_index, packet->rdesc_count, 1);
  1264. #endif
  1265. /* Make sure ownership is written to the descriptor */
  1266. dma_wmb();
  1267. ring->cur = cur_index + 1;
  1268. if (!packet->skb->xmit_more ||
  1269. netif_xmit_stopped(netdev_get_tx_queue(pdata->netdev,
  1270. channel->queue_index)))
  1271. xgbe_tx_start_xmit(channel, ring);
  1272. else
  1273. ring->tx.xmit_more = 1;
  1274. DBGPR(" %s: descriptors %u to %u written\n",
  1275. channel->name, start_index & (ring->rdesc_count - 1),
  1276. (ring->cur - 1) & (ring->rdesc_count - 1));
  1277. DBGPR("<--xgbe_dev_xmit\n");
  1278. }
  1279. static int xgbe_dev_read(struct xgbe_channel *channel)
  1280. {
  1281. struct xgbe_ring *ring = channel->rx_ring;
  1282. struct xgbe_ring_data *rdata;
  1283. struct xgbe_ring_desc *rdesc;
  1284. struct xgbe_packet_data *packet = &ring->packet_data;
  1285. struct net_device *netdev = channel->pdata->netdev;
  1286. unsigned int err, etlt, l34t;
  1287. DBGPR("-->xgbe_dev_read: cur = %d\n", ring->cur);
  1288. rdata = XGBE_GET_DESC_DATA(ring, ring->cur);
  1289. rdesc = rdata->rdesc;
  1290. /* Check for data availability */
  1291. if (XGMAC_GET_BITS_LE(rdesc->desc3, RX_NORMAL_DESC3, OWN))
  1292. return 1;
  1293. /* Make sure descriptor fields are read after reading the OWN bit */
  1294. dma_rmb();
  1295. #ifdef XGMAC_ENABLE_RX_DESC_DUMP
  1296. xgbe_dump_rx_desc(ring, rdesc, ring->cur);
  1297. #endif
  1298. if (XGMAC_GET_BITS_LE(rdesc->desc3, RX_NORMAL_DESC3, CTXT)) {
  1299. /* Timestamp Context Descriptor */
  1300. xgbe_get_rx_tstamp(packet, rdesc);
  1301. XGMAC_SET_BITS(packet->attributes, RX_PACKET_ATTRIBUTES,
  1302. CONTEXT, 1);
  1303. XGMAC_SET_BITS(packet->attributes, RX_PACKET_ATTRIBUTES,
  1304. CONTEXT_NEXT, 0);
  1305. return 0;
  1306. }
  1307. /* Normal Descriptor, be sure Context Descriptor bit is off */
  1308. XGMAC_SET_BITS(packet->attributes, RX_PACKET_ATTRIBUTES, CONTEXT, 0);
  1309. /* Indicate if a Context Descriptor is next */
  1310. if (XGMAC_GET_BITS_LE(rdesc->desc3, RX_NORMAL_DESC3, CDA))
  1311. XGMAC_SET_BITS(packet->attributes, RX_PACKET_ATTRIBUTES,
  1312. CONTEXT_NEXT, 1);
  1313. /* Get the header length */
  1314. if (XGMAC_GET_BITS_LE(rdesc->desc3, RX_NORMAL_DESC3, FD))
  1315. rdata->rx.hdr_len = XGMAC_GET_BITS_LE(rdesc->desc2,
  1316. RX_NORMAL_DESC2, HL);
  1317. /* Get the RSS hash */
  1318. if (XGMAC_GET_BITS_LE(rdesc->desc3, RX_NORMAL_DESC3, RSV)) {
  1319. XGMAC_SET_BITS(packet->attributes, RX_PACKET_ATTRIBUTES,
  1320. RSS_HASH, 1);
  1321. packet->rss_hash = le32_to_cpu(rdesc->desc1);
  1322. l34t = XGMAC_GET_BITS_LE(rdesc->desc3, RX_NORMAL_DESC3, L34T);
  1323. switch (l34t) {
  1324. case RX_DESC3_L34T_IPV4_TCP:
  1325. case RX_DESC3_L34T_IPV4_UDP:
  1326. case RX_DESC3_L34T_IPV6_TCP:
  1327. case RX_DESC3_L34T_IPV6_UDP:
  1328. packet->rss_hash_type = PKT_HASH_TYPE_L4;
  1329. break;
  1330. default:
  1331. packet->rss_hash_type = PKT_HASH_TYPE_L3;
  1332. }
  1333. }
  1334. /* Get the packet length */
  1335. rdata->rx.len = XGMAC_GET_BITS_LE(rdesc->desc3, RX_NORMAL_DESC3, PL);
  1336. if (!XGMAC_GET_BITS_LE(rdesc->desc3, RX_NORMAL_DESC3, LD)) {
  1337. /* Not all the data has been transferred for this packet */
  1338. XGMAC_SET_BITS(packet->attributes, RX_PACKET_ATTRIBUTES,
  1339. INCOMPLETE, 1);
  1340. return 0;
  1341. }
  1342. /* This is the last of the data for this packet */
  1343. XGMAC_SET_BITS(packet->attributes, RX_PACKET_ATTRIBUTES,
  1344. INCOMPLETE, 0);
  1345. /* Set checksum done indicator as appropriate */
  1346. if (channel->pdata->netdev->features & NETIF_F_RXCSUM)
  1347. XGMAC_SET_BITS(packet->attributes, RX_PACKET_ATTRIBUTES,
  1348. CSUM_DONE, 1);
  1349. /* Check for errors (only valid in last descriptor) */
  1350. err = XGMAC_GET_BITS_LE(rdesc->desc3, RX_NORMAL_DESC3, ES);
  1351. etlt = XGMAC_GET_BITS_LE(rdesc->desc3, RX_NORMAL_DESC3, ETLT);
  1352. DBGPR(" err=%u, etlt=%#x\n", err, etlt);
  1353. if (!err || !etlt) {
  1354. /* No error if err is 0 or etlt is 0 */
  1355. if ((etlt == 0x09) &&
  1356. (netdev->features & NETIF_F_HW_VLAN_CTAG_RX)) {
  1357. XGMAC_SET_BITS(packet->attributes, RX_PACKET_ATTRIBUTES,
  1358. VLAN_CTAG, 1);
  1359. packet->vlan_ctag = XGMAC_GET_BITS_LE(rdesc->desc0,
  1360. RX_NORMAL_DESC0,
  1361. OVT);
  1362. DBGPR(" vlan-ctag=0x%04x\n", packet->vlan_ctag);
  1363. }
  1364. } else {
  1365. if ((etlt == 0x05) || (etlt == 0x06))
  1366. XGMAC_SET_BITS(packet->attributes, RX_PACKET_ATTRIBUTES,
  1367. CSUM_DONE, 0);
  1368. else
  1369. XGMAC_SET_BITS(packet->errors, RX_PACKET_ERRORS,
  1370. FRAME, 1);
  1371. }
  1372. DBGPR("<--xgbe_dev_read: %s - descriptor=%u (cur=%d)\n", channel->name,
  1373. ring->cur & (ring->rdesc_count - 1), ring->cur);
  1374. return 0;
  1375. }
  1376. static int xgbe_is_context_desc(struct xgbe_ring_desc *rdesc)
  1377. {
  1378. /* Rx and Tx share CTXT bit, so check TDES3.CTXT bit */
  1379. return XGMAC_GET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, CTXT);
  1380. }
  1381. static int xgbe_is_last_desc(struct xgbe_ring_desc *rdesc)
  1382. {
  1383. /* Rx and Tx share LD bit, so check TDES3.LD bit */
  1384. return XGMAC_GET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, LD);
  1385. }
  1386. static int xgbe_enable_int(struct xgbe_channel *channel,
  1387. enum xgbe_int int_id)
  1388. {
  1389. unsigned int dma_ch_ier;
  1390. dma_ch_ier = XGMAC_DMA_IOREAD(channel, DMA_CH_IER);
  1391. switch (int_id) {
  1392. case XGMAC_INT_DMA_CH_SR_TI:
  1393. XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, TIE, 1);
  1394. break;
  1395. case XGMAC_INT_DMA_CH_SR_TPS:
  1396. XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, TXSE, 1);
  1397. break;
  1398. case XGMAC_INT_DMA_CH_SR_TBU:
  1399. XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, TBUE, 1);
  1400. break;
  1401. case XGMAC_INT_DMA_CH_SR_RI:
  1402. XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, RIE, 1);
  1403. break;
  1404. case XGMAC_INT_DMA_CH_SR_RBU:
  1405. XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, RBUE, 1);
  1406. break;
  1407. case XGMAC_INT_DMA_CH_SR_RPS:
  1408. XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, RSE, 1);
  1409. break;
  1410. case XGMAC_INT_DMA_CH_SR_TI_RI:
  1411. XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, TIE, 1);
  1412. XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, RIE, 1);
  1413. break;
  1414. case XGMAC_INT_DMA_CH_SR_FBE:
  1415. XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, FBEE, 1);
  1416. break;
  1417. case XGMAC_INT_DMA_ALL:
  1418. dma_ch_ier |= channel->saved_ier;
  1419. break;
  1420. default:
  1421. return -1;
  1422. }
  1423. XGMAC_DMA_IOWRITE(channel, DMA_CH_IER, dma_ch_ier);
  1424. return 0;
  1425. }
  1426. static int xgbe_disable_int(struct xgbe_channel *channel,
  1427. enum xgbe_int int_id)
  1428. {
  1429. unsigned int dma_ch_ier;
  1430. dma_ch_ier = XGMAC_DMA_IOREAD(channel, DMA_CH_IER);
  1431. switch (int_id) {
  1432. case XGMAC_INT_DMA_CH_SR_TI:
  1433. XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, TIE, 0);
  1434. break;
  1435. case XGMAC_INT_DMA_CH_SR_TPS:
  1436. XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, TXSE, 0);
  1437. break;
  1438. case XGMAC_INT_DMA_CH_SR_TBU:
  1439. XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, TBUE, 0);
  1440. break;
  1441. case XGMAC_INT_DMA_CH_SR_RI:
  1442. XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, RIE, 0);
  1443. break;
  1444. case XGMAC_INT_DMA_CH_SR_RBU:
  1445. XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, RBUE, 0);
  1446. break;
  1447. case XGMAC_INT_DMA_CH_SR_RPS:
  1448. XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, RSE, 0);
  1449. break;
  1450. case XGMAC_INT_DMA_CH_SR_TI_RI:
  1451. XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, TIE, 0);
  1452. XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, RIE, 0);
  1453. break;
  1454. case XGMAC_INT_DMA_CH_SR_FBE:
  1455. XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, FBEE, 0);
  1456. break;
  1457. case XGMAC_INT_DMA_ALL:
  1458. channel->saved_ier = dma_ch_ier & XGBE_DMA_INTERRUPT_MASK;
  1459. dma_ch_ier &= ~XGBE_DMA_INTERRUPT_MASK;
  1460. break;
  1461. default:
  1462. return -1;
  1463. }
  1464. XGMAC_DMA_IOWRITE(channel, DMA_CH_IER, dma_ch_ier);
  1465. return 0;
  1466. }
  1467. static int xgbe_exit(struct xgbe_prv_data *pdata)
  1468. {
  1469. unsigned int count = 2000;
  1470. DBGPR("-->xgbe_exit\n");
  1471. /* Issue a software reset */
  1472. XGMAC_IOWRITE_BITS(pdata, DMA_MR, SWR, 1);
  1473. usleep_range(10, 15);
  1474. /* Poll Until Poll Condition */
  1475. while (count-- && XGMAC_IOREAD_BITS(pdata, DMA_MR, SWR))
  1476. usleep_range(500, 600);
  1477. if (!count)
  1478. return -EBUSY;
  1479. DBGPR("<--xgbe_exit\n");
  1480. return 0;
  1481. }
  1482. static int xgbe_flush_tx_queues(struct xgbe_prv_data *pdata)
  1483. {
  1484. unsigned int i, count;
  1485. if (XGMAC_GET_BITS(pdata->hw_feat.version, MAC_VR, SNPSVER) < 0x21)
  1486. return 0;
  1487. for (i = 0; i < pdata->tx_q_count; i++)
  1488. XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_Q_TQOMR, FTQ, 1);
  1489. /* Poll Until Poll Condition */
  1490. for (i = 0; i < pdata->tx_q_count; i++) {
  1491. count = 2000;
  1492. while (count-- && XGMAC_MTL_IOREAD_BITS(pdata, i,
  1493. MTL_Q_TQOMR, FTQ))
  1494. usleep_range(500, 600);
  1495. if (!count)
  1496. return -EBUSY;
  1497. }
  1498. return 0;
  1499. }
  1500. static void xgbe_config_dma_bus(struct xgbe_prv_data *pdata)
  1501. {
  1502. /* Set enhanced addressing mode */
  1503. XGMAC_IOWRITE_BITS(pdata, DMA_SBMR, EAME, 1);
  1504. /* Set the System Bus mode */
  1505. XGMAC_IOWRITE_BITS(pdata, DMA_SBMR, UNDEF, 1);
  1506. XGMAC_IOWRITE_BITS(pdata, DMA_SBMR, BLEN_256, 1);
  1507. }
  1508. static void xgbe_config_dma_cache(struct xgbe_prv_data *pdata)
  1509. {
  1510. unsigned int arcache, awcache;
  1511. arcache = 0;
  1512. XGMAC_SET_BITS(arcache, DMA_AXIARCR, DRC, pdata->arcache);
  1513. XGMAC_SET_BITS(arcache, DMA_AXIARCR, DRD, pdata->axdomain);
  1514. XGMAC_SET_BITS(arcache, DMA_AXIARCR, TEC, pdata->arcache);
  1515. XGMAC_SET_BITS(arcache, DMA_AXIARCR, TED, pdata->axdomain);
  1516. XGMAC_SET_BITS(arcache, DMA_AXIARCR, THC, pdata->arcache);
  1517. XGMAC_SET_BITS(arcache, DMA_AXIARCR, THD, pdata->axdomain);
  1518. XGMAC_IOWRITE(pdata, DMA_AXIARCR, arcache);
  1519. awcache = 0;
  1520. XGMAC_SET_BITS(awcache, DMA_AXIAWCR, DWC, pdata->awcache);
  1521. XGMAC_SET_BITS(awcache, DMA_AXIAWCR, DWD, pdata->axdomain);
  1522. XGMAC_SET_BITS(awcache, DMA_AXIAWCR, RPC, pdata->awcache);
  1523. XGMAC_SET_BITS(awcache, DMA_AXIAWCR, RPD, pdata->axdomain);
  1524. XGMAC_SET_BITS(awcache, DMA_AXIAWCR, RHC, pdata->awcache);
  1525. XGMAC_SET_BITS(awcache, DMA_AXIAWCR, RHD, pdata->axdomain);
  1526. XGMAC_SET_BITS(awcache, DMA_AXIAWCR, TDC, pdata->awcache);
  1527. XGMAC_SET_BITS(awcache, DMA_AXIAWCR, TDD, pdata->axdomain);
  1528. XGMAC_IOWRITE(pdata, DMA_AXIAWCR, awcache);
  1529. }
  1530. static void xgbe_config_mtl_mode(struct xgbe_prv_data *pdata)
  1531. {
  1532. unsigned int i;
  1533. /* Set Tx to weighted round robin scheduling algorithm */
  1534. XGMAC_IOWRITE_BITS(pdata, MTL_OMR, ETSALG, MTL_ETSALG_WRR);
  1535. /* Set Tx traffic classes to use WRR algorithm with equal weights */
  1536. for (i = 0; i < pdata->hw_feat.tc_cnt; i++) {
  1537. XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_TC_ETSCR, TSA,
  1538. MTL_TSA_ETS);
  1539. XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_TC_QWR, QW, 1);
  1540. }
  1541. /* Set Rx to strict priority algorithm */
  1542. XGMAC_IOWRITE_BITS(pdata, MTL_OMR, RAA, MTL_RAA_SP);
  1543. }
  1544. static unsigned int xgbe_calculate_per_queue_fifo(unsigned int fifo_size,
  1545. unsigned int queue_count)
  1546. {
  1547. unsigned int q_fifo_size = 0;
  1548. enum xgbe_mtl_fifo_size p_fifo = XGMAC_MTL_FIFO_SIZE_256;
  1549. /* Calculate Tx/Rx fifo share per queue */
  1550. switch (fifo_size) {
  1551. case 0:
  1552. q_fifo_size = XGBE_FIFO_SIZE_B(128);
  1553. break;
  1554. case 1:
  1555. q_fifo_size = XGBE_FIFO_SIZE_B(256);
  1556. break;
  1557. case 2:
  1558. q_fifo_size = XGBE_FIFO_SIZE_B(512);
  1559. break;
  1560. case 3:
  1561. q_fifo_size = XGBE_FIFO_SIZE_KB(1);
  1562. break;
  1563. case 4:
  1564. q_fifo_size = XGBE_FIFO_SIZE_KB(2);
  1565. break;
  1566. case 5:
  1567. q_fifo_size = XGBE_FIFO_SIZE_KB(4);
  1568. break;
  1569. case 6:
  1570. q_fifo_size = XGBE_FIFO_SIZE_KB(8);
  1571. break;
  1572. case 7:
  1573. q_fifo_size = XGBE_FIFO_SIZE_KB(16);
  1574. break;
  1575. case 8:
  1576. q_fifo_size = XGBE_FIFO_SIZE_KB(32);
  1577. break;
  1578. case 9:
  1579. q_fifo_size = XGBE_FIFO_SIZE_KB(64);
  1580. break;
  1581. case 10:
  1582. q_fifo_size = XGBE_FIFO_SIZE_KB(128);
  1583. break;
  1584. case 11:
  1585. q_fifo_size = XGBE_FIFO_SIZE_KB(256);
  1586. break;
  1587. }
  1588. /* The configured value is not the actual amount of fifo RAM */
  1589. q_fifo_size = min_t(unsigned int, XGBE_FIFO_MAX, q_fifo_size);
  1590. q_fifo_size = q_fifo_size / queue_count;
  1591. /* Set the queue fifo size programmable value */
  1592. if (q_fifo_size >= XGBE_FIFO_SIZE_KB(256))
  1593. p_fifo = XGMAC_MTL_FIFO_SIZE_256K;
  1594. else if (q_fifo_size >= XGBE_FIFO_SIZE_KB(128))
  1595. p_fifo = XGMAC_MTL_FIFO_SIZE_128K;
  1596. else if (q_fifo_size >= XGBE_FIFO_SIZE_KB(64))
  1597. p_fifo = XGMAC_MTL_FIFO_SIZE_64K;
  1598. else if (q_fifo_size >= XGBE_FIFO_SIZE_KB(32))
  1599. p_fifo = XGMAC_MTL_FIFO_SIZE_32K;
  1600. else if (q_fifo_size >= XGBE_FIFO_SIZE_KB(16))
  1601. p_fifo = XGMAC_MTL_FIFO_SIZE_16K;
  1602. else if (q_fifo_size >= XGBE_FIFO_SIZE_KB(8))
  1603. p_fifo = XGMAC_MTL_FIFO_SIZE_8K;
  1604. else if (q_fifo_size >= XGBE_FIFO_SIZE_KB(4))
  1605. p_fifo = XGMAC_MTL_FIFO_SIZE_4K;
  1606. else if (q_fifo_size >= XGBE_FIFO_SIZE_KB(2))
  1607. p_fifo = XGMAC_MTL_FIFO_SIZE_2K;
  1608. else if (q_fifo_size >= XGBE_FIFO_SIZE_KB(1))
  1609. p_fifo = XGMAC_MTL_FIFO_SIZE_1K;
  1610. else if (q_fifo_size >= XGBE_FIFO_SIZE_B(512))
  1611. p_fifo = XGMAC_MTL_FIFO_SIZE_512;
  1612. else if (q_fifo_size >= XGBE_FIFO_SIZE_B(256))
  1613. p_fifo = XGMAC_MTL_FIFO_SIZE_256;
  1614. return p_fifo;
  1615. }
  1616. static void xgbe_config_tx_fifo_size(struct xgbe_prv_data *pdata)
  1617. {
  1618. enum xgbe_mtl_fifo_size fifo_size;
  1619. unsigned int i;
  1620. fifo_size = xgbe_calculate_per_queue_fifo(pdata->hw_feat.tx_fifo_size,
  1621. pdata->tx_q_count);
  1622. for (i = 0; i < pdata->tx_q_count; i++)
  1623. XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_Q_TQOMR, TQS, fifo_size);
  1624. netdev_notice(pdata->netdev,
  1625. "%d Tx hardware queues, %d byte fifo per queue\n",
  1626. pdata->tx_q_count, ((fifo_size + 1) * 256));
  1627. }
  1628. static void xgbe_config_rx_fifo_size(struct xgbe_prv_data *pdata)
  1629. {
  1630. enum xgbe_mtl_fifo_size fifo_size;
  1631. unsigned int i;
  1632. fifo_size = xgbe_calculate_per_queue_fifo(pdata->hw_feat.rx_fifo_size,
  1633. pdata->rx_q_count);
  1634. for (i = 0; i < pdata->rx_q_count; i++)
  1635. XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_Q_RQOMR, RQS, fifo_size);
  1636. netdev_notice(pdata->netdev,
  1637. "%d Rx hardware queues, %d byte fifo per queue\n",
  1638. pdata->rx_q_count, ((fifo_size + 1) * 256));
  1639. }
  1640. static void xgbe_config_queue_mapping(struct xgbe_prv_data *pdata)
  1641. {
  1642. unsigned int qptc, qptc_extra, queue;
  1643. unsigned int prio_queues;
  1644. unsigned int ppq, ppq_extra, prio;
  1645. unsigned int mask;
  1646. unsigned int i, j, reg, reg_val;
  1647. /* Map the MTL Tx Queues to Traffic Classes
  1648. * Note: Tx Queues >= Traffic Classes
  1649. */
  1650. qptc = pdata->tx_q_count / pdata->hw_feat.tc_cnt;
  1651. qptc_extra = pdata->tx_q_count % pdata->hw_feat.tc_cnt;
  1652. for (i = 0, queue = 0; i < pdata->hw_feat.tc_cnt; i++) {
  1653. for (j = 0; j < qptc; j++) {
  1654. DBGPR(" TXq%u mapped to TC%u\n", queue, i);
  1655. XGMAC_MTL_IOWRITE_BITS(pdata, queue, MTL_Q_TQOMR,
  1656. Q2TCMAP, i);
  1657. pdata->q2tc_map[queue++] = i;
  1658. }
  1659. if (i < qptc_extra) {
  1660. DBGPR(" TXq%u mapped to TC%u\n", queue, i);
  1661. XGMAC_MTL_IOWRITE_BITS(pdata, queue, MTL_Q_TQOMR,
  1662. Q2TCMAP, i);
  1663. pdata->q2tc_map[queue++] = i;
  1664. }
  1665. }
  1666. /* Map the 8 VLAN priority values to available MTL Rx queues */
  1667. prio_queues = min_t(unsigned int, IEEE_8021QAZ_MAX_TCS,
  1668. pdata->rx_q_count);
  1669. ppq = IEEE_8021QAZ_MAX_TCS / prio_queues;
  1670. ppq_extra = IEEE_8021QAZ_MAX_TCS % prio_queues;
  1671. reg = MAC_RQC2R;
  1672. reg_val = 0;
  1673. for (i = 0, prio = 0; i < prio_queues;) {
  1674. mask = 0;
  1675. for (j = 0; j < ppq; j++) {
  1676. DBGPR(" PRIO%u mapped to RXq%u\n", prio, i);
  1677. mask |= (1 << prio);
  1678. pdata->prio2q_map[prio++] = i;
  1679. }
  1680. if (i < ppq_extra) {
  1681. DBGPR(" PRIO%u mapped to RXq%u\n", prio, i);
  1682. mask |= (1 << prio);
  1683. pdata->prio2q_map[prio++] = i;
  1684. }
  1685. reg_val |= (mask << ((i++ % MAC_RQC2_Q_PER_REG) << 3));
  1686. if ((i % MAC_RQC2_Q_PER_REG) && (i != prio_queues))
  1687. continue;
  1688. XGMAC_IOWRITE(pdata, reg, reg_val);
  1689. reg += MAC_RQC2_INC;
  1690. reg_val = 0;
  1691. }
  1692. /* Select dynamic mapping of MTL Rx queue to DMA Rx channel */
  1693. reg = MTL_RQDCM0R;
  1694. reg_val = 0;
  1695. for (i = 0; i < pdata->rx_q_count;) {
  1696. reg_val |= (0x80 << ((i++ % MTL_RQDCM_Q_PER_REG) << 3));
  1697. if ((i % MTL_RQDCM_Q_PER_REG) && (i != pdata->rx_q_count))
  1698. continue;
  1699. XGMAC_IOWRITE(pdata, reg, reg_val);
  1700. reg += MTL_RQDCM_INC;
  1701. reg_val = 0;
  1702. }
  1703. }
  1704. static void xgbe_config_flow_control_threshold(struct xgbe_prv_data *pdata)
  1705. {
  1706. unsigned int i;
  1707. for (i = 0; i < pdata->rx_q_count; i++) {
  1708. /* Activate flow control when less than 4k left in fifo */
  1709. XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_Q_RQFCR, RFA, 2);
  1710. /* De-activate flow control when more than 6k left in fifo */
  1711. XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_Q_RQFCR, RFD, 4);
  1712. }
  1713. }
  1714. static void xgbe_config_mac_address(struct xgbe_prv_data *pdata)
  1715. {
  1716. xgbe_set_mac_address(pdata, pdata->netdev->dev_addr);
  1717. /* Filtering is done using perfect filtering and hash filtering */
  1718. if (pdata->hw_feat.hash_table_size) {
  1719. XGMAC_IOWRITE_BITS(pdata, MAC_PFR, HPF, 1);
  1720. XGMAC_IOWRITE_BITS(pdata, MAC_PFR, HUC, 1);
  1721. XGMAC_IOWRITE_BITS(pdata, MAC_PFR, HMC, 1);
  1722. }
  1723. }
  1724. static void xgbe_config_jumbo_enable(struct xgbe_prv_data *pdata)
  1725. {
  1726. unsigned int val;
  1727. val = (pdata->netdev->mtu > XGMAC_STD_PACKET_MTU) ? 1 : 0;
  1728. XGMAC_IOWRITE_BITS(pdata, MAC_RCR, JE, val);
  1729. }
  1730. static void xgbe_config_mac_speed(struct xgbe_prv_data *pdata)
  1731. {
  1732. switch (pdata->phy_speed) {
  1733. case SPEED_10000:
  1734. xgbe_set_xgmii_speed(pdata);
  1735. break;
  1736. case SPEED_2500:
  1737. xgbe_set_gmii_2500_speed(pdata);
  1738. break;
  1739. case SPEED_1000:
  1740. xgbe_set_gmii_speed(pdata);
  1741. break;
  1742. }
  1743. }
  1744. static void xgbe_config_checksum_offload(struct xgbe_prv_data *pdata)
  1745. {
  1746. if (pdata->netdev->features & NETIF_F_RXCSUM)
  1747. xgbe_enable_rx_csum(pdata);
  1748. else
  1749. xgbe_disable_rx_csum(pdata);
  1750. }
  1751. static void xgbe_config_vlan_support(struct xgbe_prv_data *pdata)
  1752. {
  1753. /* Indicate that VLAN Tx CTAGs come from context descriptors */
  1754. XGMAC_IOWRITE_BITS(pdata, MAC_VLANIR, CSVL, 0);
  1755. XGMAC_IOWRITE_BITS(pdata, MAC_VLANIR, VLTI, 1);
  1756. /* Set the current VLAN Hash Table register value */
  1757. xgbe_update_vlan_hash_table(pdata);
  1758. if (pdata->netdev->features & NETIF_F_HW_VLAN_CTAG_FILTER)
  1759. xgbe_enable_rx_vlan_filtering(pdata);
  1760. else
  1761. xgbe_disable_rx_vlan_filtering(pdata);
  1762. if (pdata->netdev->features & NETIF_F_HW_VLAN_CTAG_RX)
  1763. xgbe_enable_rx_vlan_stripping(pdata);
  1764. else
  1765. xgbe_disable_rx_vlan_stripping(pdata);
  1766. }
  1767. static u64 xgbe_mmc_read(struct xgbe_prv_data *pdata, unsigned int reg_lo)
  1768. {
  1769. bool read_hi;
  1770. u64 val;
  1771. switch (reg_lo) {
  1772. /* These registers are always 64 bit */
  1773. case MMC_TXOCTETCOUNT_GB_LO:
  1774. case MMC_TXOCTETCOUNT_G_LO:
  1775. case MMC_RXOCTETCOUNT_GB_LO:
  1776. case MMC_RXOCTETCOUNT_G_LO:
  1777. read_hi = true;
  1778. break;
  1779. default:
  1780. read_hi = false;
  1781. };
  1782. val = XGMAC_IOREAD(pdata, reg_lo);
  1783. if (read_hi)
  1784. val |= ((u64)XGMAC_IOREAD(pdata, reg_lo + 4) << 32);
  1785. return val;
  1786. }
  1787. static void xgbe_tx_mmc_int(struct xgbe_prv_data *pdata)
  1788. {
  1789. struct xgbe_mmc_stats *stats = &pdata->mmc_stats;
  1790. unsigned int mmc_isr = XGMAC_IOREAD(pdata, MMC_TISR);
  1791. if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TXOCTETCOUNT_GB))
  1792. stats->txoctetcount_gb +=
  1793. xgbe_mmc_read(pdata, MMC_TXOCTETCOUNT_GB_LO);
  1794. if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TXFRAMECOUNT_GB))
  1795. stats->txframecount_gb +=
  1796. xgbe_mmc_read(pdata, MMC_TXFRAMECOUNT_GB_LO);
  1797. if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TXBROADCASTFRAMES_G))
  1798. stats->txbroadcastframes_g +=
  1799. xgbe_mmc_read(pdata, MMC_TXBROADCASTFRAMES_G_LO);
  1800. if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TXMULTICASTFRAMES_G))
  1801. stats->txmulticastframes_g +=
  1802. xgbe_mmc_read(pdata, MMC_TXMULTICASTFRAMES_G_LO);
  1803. if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TX64OCTETS_GB))
  1804. stats->tx64octets_gb +=
  1805. xgbe_mmc_read(pdata, MMC_TX64OCTETS_GB_LO);
  1806. if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TX65TO127OCTETS_GB))
  1807. stats->tx65to127octets_gb +=
  1808. xgbe_mmc_read(pdata, MMC_TX65TO127OCTETS_GB_LO);
  1809. if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TX128TO255OCTETS_GB))
  1810. stats->tx128to255octets_gb +=
  1811. xgbe_mmc_read(pdata, MMC_TX128TO255OCTETS_GB_LO);
  1812. if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TX256TO511OCTETS_GB))
  1813. stats->tx256to511octets_gb +=
  1814. xgbe_mmc_read(pdata, MMC_TX256TO511OCTETS_GB_LO);
  1815. if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TX512TO1023OCTETS_GB))
  1816. stats->tx512to1023octets_gb +=
  1817. xgbe_mmc_read(pdata, MMC_TX512TO1023OCTETS_GB_LO);
  1818. if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TX1024TOMAXOCTETS_GB))
  1819. stats->tx1024tomaxoctets_gb +=
  1820. xgbe_mmc_read(pdata, MMC_TX1024TOMAXOCTETS_GB_LO);
  1821. if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TXUNICASTFRAMES_GB))
  1822. stats->txunicastframes_gb +=
  1823. xgbe_mmc_read(pdata, MMC_TXUNICASTFRAMES_GB_LO);
  1824. if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TXMULTICASTFRAMES_GB))
  1825. stats->txmulticastframes_gb +=
  1826. xgbe_mmc_read(pdata, MMC_TXMULTICASTFRAMES_GB_LO);
  1827. if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TXBROADCASTFRAMES_GB))
  1828. stats->txbroadcastframes_g +=
  1829. xgbe_mmc_read(pdata, MMC_TXBROADCASTFRAMES_GB_LO);
  1830. if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TXUNDERFLOWERROR))
  1831. stats->txunderflowerror +=
  1832. xgbe_mmc_read(pdata, MMC_TXUNDERFLOWERROR_LO);
  1833. if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TXOCTETCOUNT_G))
  1834. stats->txoctetcount_g +=
  1835. xgbe_mmc_read(pdata, MMC_TXOCTETCOUNT_G_LO);
  1836. if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TXFRAMECOUNT_G))
  1837. stats->txframecount_g +=
  1838. xgbe_mmc_read(pdata, MMC_TXFRAMECOUNT_G_LO);
  1839. if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TXPAUSEFRAMES))
  1840. stats->txpauseframes +=
  1841. xgbe_mmc_read(pdata, MMC_TXPAUSEFRAMES_LO);
  1842. if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TXVLANFRAMES_G))
  1843. stats->txvlanframes_g +=
  1844. xgbe_mmc_read(pdata, MMC_TXVLANFRAMES_G_LO);
  1845. }
  1846. static void xgbe_rx_mmc_int(struct xgbe_prv_data *pdata)
  1847. {
  1848. struct xgbe_mmc_stats *stats = &pdata->mmc_stats;
  1849. unsigned int mmc_isr = XGMAC_IOREAD(pdata, MMC_RISR);
  1850. if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXFRAMECOUNT_GB))
  1851. stats->rxframecount_gb +=
  1852. xgbe_mmc_read(pdata, MMC_RXFRAMECOUNT_GB_LO);
  1853. if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXOCTETCOUNT_GB))
  1854. stats->rxoctetcount_gb +=
  1855. xgbe_mmc_read(pdata, MMC_RXOCTETCOUNT_GB_LO);
  1856. if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXOCTETCOUNT_G))
  1857. stats->rxoctetcount_g +=
  1858. xgbe_mmc_read(pdata, MMC_RXOCTETCOUNT_G_LO);
  1859. if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXBROADCASTFRAMES_G))
  1860. stats->rxbroadcastframes_g +=
  1861. xgbe_mmc_read(pdata, MMC_RXBROADCASTFRAMES_G_LO);
  1862. if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXMULTICASTFRAMES_G))
  1863. stats->rxmulticastframes_g +=
  1864. xgbe_mmc_read(pdata, MMC_RXMULTICASTFRAMES_G_LO);
  1865. if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXCRCERROR))
  1866. stats->rxcrcerror +=
  1867. xgbe_mmc_read(pdata, MMC_RXCRCERROR_LO);
  1868. if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXRUNTERROR))
  1869. stats->rxrunterror +=
  1870. xgbe_mmc_read(pdata, MMC_RXRUNTERROR);
  1871. if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXJABBERERROR))
  1872. stats->rxjabbererror +=
  1873. xgbe_mmc_read(pdata, MMC_RXJABBERERROR);
  1874. if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXUNDERSIZE_G))
  1875. stats->rxundersize_g +=
  1876. xgbe_mmc_read(pdata, MMC_RXUNDERSIZE_G);
  1877. if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXOVERSIZE_G))
  1878. stats->rxoversize_g +=
  1879. xgbe_mmc_read(pdata, MMC_RXOVERSIZE_G);
  1880. if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RX64OCTETS_GB))
  1881. stats->rx64octets_gb +=
  1882. xgbe_mmc_read(pdata, MMC_RX64OCTETS_GB_LO);
  1883. if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RX65TO127OCTETS_GB))
  1884. stats->rx65to127octets_gb +=
  1885. xgbe_mmc_read(pdata, MMC_RX65TO127OCTETS_GB_LO);
  1886. if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RX128TO255OCTETS_GB))
  1887. stats->rx128to255octets_gb +=
  1888. xgbe_mmc_read(pdata, MMC_RX128TO255OCTETS_GB_LO);
  1889. if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RX256TO511OCTETS_GB))
  1890. stats->rx256to511octets_gb +=
  1891. xgbe_mmc_read(pdata, MMC_RX256TO511OCTETS_GB_LO);
  1892. if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RX512TO1023OCTETS_GB))
  1893. stats->rx512to1023octets_gb +=
  1894. xgbe_mmc_read(pdata, MMC_RX512TO1023OCTETS_GB_LO);
  1895. if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RX1024TOMAXOCTETS_GB))
  1896. stats->rx1024tomaxoctets_gb +=
  1897. xgbe_mmc_read(pdata, MMC_RX1024TOMAXOCTETS_GB_LO);
  1898. if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXUNICASTFRAMES_G))
  1899. stats->rxunicastframes_g +=
  1900. xgbe_mmc_read(pdata, MMC_RXUNICASTFRAMES_G_LO);
  1901. if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXLENGTHERROR))
  1902. stats->rxlengtherror +=
  1903. xgbe_mmc_read(pdata, MMC_RXLENGTHERROR_LO);
  1904. if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXOUTOFRANGETYPE))
  1905. stats->rxoutofrangetype +=
  1906. xgbe_mmc_read(pdata, MMC_RXOUTOFRANGETYPE_LO);
  1907. if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXPAUSEFRAMES))
  1908. stats->rxpauseframes +=
  1909. xgbe_mmc_read(pdata, MMC_RXPAUSEFRAMES_LO);
  1910. if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXFIFOOVERFLOW))
  1911. stats->rxfifooverflow +=
  1912. xgbe_mmc_read(pdata, MMC_RXFIFOOVERFLOW_LO);
  1913. if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXVLANFRAMES_GB))
  1914. stats->rxvlanframes_gb +=
  1915. xgbe_mmc_read(pdata, MMC_RXVLANFRAMES_GB_LO);
  1916. if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXWATCHDOGERROR))
  1917. stats->rxwatchdogerror +=
  1918. xgbe_mmc_read(pdata, MMC_RXWATCHDOGERROR);
  1919. }
  1920. static void xgbe_read_mmc_stats(struct xgbe_prv_data *pdata)
  1921. {
  1922. struct xgbe_mmc_stats *stats = &pdata->mmc_stats;
  1923. /* Freeze counters */
  1924. XGMAC_IOWRITE_BITS(pdata, MMC_CR, MCF, 1);
  1925. stats->txoctetcount_gb +=
  1926. xgbe_mmc_read(pdata, MMC_TXOCTETCOUNT_GB_LO);
  1927. stats->txframecount_gb +=
  1928. xgbe_mmc_read(pdata, MMC_TXFRAMECOUNT_GB_LO);
  1929. stats->txbroadcastframes_g +=
  1930. xgbe_mmc_read(pdata, MMC_TXBROADCASTFRAMES_G_LO);
  1931. stats->txmulticastframes_g +=
  1932. xgbe_mmc_read(pdata, MMC_TXMULTICASTFRAMES_G_LO);
  1933. stats->tx64octets_gb +=
  1934. xgbe_mmc_read(pdata, MMC_TX64OCTETS_GB_LO);
  1935. stats->tx65to127octets_gb +=
  1936. xgbe_mmc_read(pdata, MMC_TX65TO127OCTETS_GB_LO);
  1937. stats->tx128to255octets_gb +=
  1938. xgbe_mmc_read(pdata, MMC_TX128TO255OCTETS_GB_LO);
  1939. stats->tx256to511octets_gb +=
  1940. xgbe_mmc_read(pdata, MMC_TX256TO511OCTETS_GB_LO);
  1941. stats->tx512to1023octets_gb +=
  1942. xgbe_mmc_read(pdata, MMC_TX512TO1023OCTETS_GB_LO);
  1943. stats->tx1024tomaxoctets_gb +=
  1944. xgbe_mmc_read(pdata, MMC_TX1024TOMAXOCTETS_GB_LO);
  1945. stats->txunicastframes_gb +=
  1946. xgbe_mmc_read(pdata, MMC_TXUNICASTFRAMES_GB_LO);
  1947. stats->txmulticastframes_gb +=
  1948. xgbe_mmc_read(pdata, MMC_TXMULTICASTFRAMES_GB_LO);
  1949. stats->txbroadcastframes_g +=
  1950. xgbe_mmc_read(pdata, MMC_TXBROADCASTFRAMES_GB_LO);
  1951. stats->txunderflowerror +=
  1952. xgbe_mmc_read(pdata, MMC_TXUNDERFLOWERROR_LO);
  1953. stats->txoctetcount_g +=
  1954. xgbe_mmc_read(pdata, MMC_TXOCTETCOUNT_G_LO);
  1955. stats->txframecount_g +=
  1956. xgbe_mmc_read(pdata, MMC_TXFRAMECOUNT_G_LO);
  1957. stats->txpauseframes +=
  1958. xgbe_mmc_read(pdata, MMC_TXPAUSEFRAMES_LO);
  1959. stats->txvlanframes_g +=
  1960. xgbe_mmc_read(pdata, MMC_TXVLANFRAMES_G_LO);
  1961. stats->rxframecount_gb +=
  1962. xgbe_mmc_read(pdata, MMC_RXFRAMECOUNT_GB_LO);
  1963. stats->rxoctetcount_gb +=
  1964. xgbe_mmc_read(pdata, MMC_RXOCTETCOUNT_GB_LO);
  1965. stats->rxoctetcount_g +=
  1966. xgbe_mmc_read(pdata, MMC_RXOCTETCOUNT_G_LO);
  1967. stats->rxbroadcastframes_g +=
  1968. xgbe_mmc_read(pdata, MMC_RXBROADCASTFRAMES_G_LO);
  1969. stats->rxmulticastframes_g +=
  1970. xgbe_mmc_read(pdata, MMC_RXMULTICASTFRAMES_G_LO);
  1971. stats->rxcrcerror +=
  1972. xgbe_mmc_read(pdata, MMC_RXCRCERROR_LO);
  1973. stats->rxrunterror +=
  1974. xgbe_mmc_read(pdata, MMC_RXRUNTERROR);
  1975. stats->rxjabbererror +=
  1976. xgbe_mmc_read(pdata, MMC_RXJABBERERROR);
  1977. stats->rxundersize_g +=
  1978. xgbe_mmc_read(pdata, MMC_RXUNDERSIZE_G);
  1979. stats->rxoversize_g +=
  1980. xgbe_mmc_read(pdata, MMC_RXOVERSIZE_G);
  1981. stats->rx64octets_gb +=
  1982. xgbe_mmc_read(pdata, MMC_RX64OCTETS_GB_LO);
  1983. stats->rx65to127octets_gb +=
  1984. xgbe_mmc_read(pdata, MMC_RX65TO127OCTETS_GB_LO);
  1985. stats->rx128to255octets_gb +=
  1986. xgbe_mmc_read(pdata, MMC_RX128TO255OCTETS_GB_LO);
  1987. stats->rx256to511octets_gb +=
  1988. xgbe_mmc_read(pdata, MMC_RX256TO511OCTETS_GB_LO);
  1989. stats->rx512to1023octets_gb +=
  1990. xgbe_mmc_read(pdata, MMC_RX512TO1023OCTETS_GB_LO);
  1991. stats->rx1024tomaxoctets_gb +=
  1992. xgbe_mmc_read(pdata, MMC_RX1024TOMAXOCTETS_GB_LO);
  1993. stats->rxunicastframes_g +=
  1994. xgbe_mmc_read(pdata, MMC_RXUNICASTFRAMES_G_LO);
  1995. stats->rxlengtherror +=
  1996. xgbe_mmc_read(pdata, MMC_RXLENGTHERROR_LO);
  1997. stats->rxoutofrangetype +=
  1998. xgbe_mmc_read(pdata, MMC_RXOUTOFRANGETYPE_LO);
  1999. stats->rxpauseframes +=
  2000. xgbe_mmc_read(pdata, MMC_RXPAUSEFRAMES_LO);
  2001. stats->rxfifooverflow +=
  2002. xgbe_mmc_read(pdata, MMC_RXFIFOOVERFLOW_LO);
  2003. stats->rxvlanframes_gb +=
  2004. xgbe_mmc_read(pdata, MMC_RXVLANFRAMES_GB_LO);
  2005. stats->rxwatchdogerror +=
  2006. xgbe_mmc_read(pdata, MMC_RXWATCHDOGERROR);
  2007. /* Un-freeze counters */
  2008. XGMAC_IOWRITE_BITS(pdata, MMC_CR, MCF, 0);
  2009. }
  2010. static void xgbe_config_mmc(struct xgbe_prv_data *pdata)
  2011. {
  2012. /* Set counters to reset on read */
  2013. XGMAC_IOWRITE_BITS(pdata, MMC_CR, ROR, 1);
  2014. /* Reset the counters */
  2015. XGMAC_IOWRITE_BITS(pdata, MMC_CR, CR, 1);
  2016. }
  2017. static void xgbe_prepare_tx_stop(struct xgbe_prv_data *pdata,
  2018. struct xgbe_channel *channel)
  2019. {
  2020. unsigned int tx_dsr, tx_pos, tx_qidx;
  2021. unsigned int tx_status;
  2022. unsigned long tx_timeout;
  2023. /* Calculate the status register to read and the position within */
  2024. if (channel->queue_index < DMA_DSRX_FIRST_QUEUE) {
  2025. tx_dsr = DMA_DSR0;
  2026. tx_pos = (channel->queue_index * DMA_DSR_Q_WIDTH) +
  2027. DMA_DSR0_TPS_START;
  2028. } else {
  2029. tx_qidx = channel->queue_index - DMA_DSRX_FIRST_QUEUE;
  2030. tx_dsr = DMA_DSR1 + ((tx_qidx / DMA_DSRX_QPR) * DMA_DSRX_INC);
  2031. tx_pos = ((tx_qidx % DMA_DSRX_QPR) * DMA_DSR_Q_WIDTH) +
  2032. DMA_DSRX_TPS_START;
  2033. }
  2034. /* The Tx engine cannot be stopped if it is actively processing
  2035. * descriptors. Wait for the Tx engine to enter the stopped or
  2036. * suspended state. Don't wait forever though...
  2037. */
  2038. tx_timeout = jiffies + (XGBE_DMA_STOP_TIMEOUT * HZ);
  2039. while (time_before(jiffies, tx_timeout)) {
  2040. tx_status = XGMAC_IOREAD(pdata, tx_dsr);
  2041. tx_status = GET_BITS(tx_status, tx_pos, DMA_DSR_TPS_WIDTH);
  2042. if ((tx_status == DMA_TPS_STOPPED) ||
  2043. (tx_status == DMA_TPS_SUSPENDED))
  2044. break;
  2045. usleep_range(500, 1000);
  2046. }
  2047. if (!time_before(jiffies, tx_timeout))
  2048. netdev_info(pdata->netdev,
  2049. "timed out waiting for Tx DMA channel %u to stop\n",
  2050. channel->queue_index);
  2051. }
  2052. static void xgbe_enable_tx(struct xgbe_prv_data *pdata)
  2053. {
  2054. struct xgbe_channel *channel;
  2055. unsigned int i;
  2056. /* Enable each Tx DMA channel */
  2057. channel = pdata->channel;
  2058. for (i = 0; i < pdata->channel_count; i++, channel++) {
  2059. if (!channel->tx_ring)
  2060. break;
  2061. XGMAC_DMA_IOWRITE_BITS(channel, DMA_CH_TCR, ST, 1);
  2062. }
  2063. /* Enable each Tx queue */
  2064. for (i = 0; i < pdata->tx_q_count; i++)
  2065. XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_Q_TQOMR, TXQEN,
  2066. MTL_Q_ENABLED);
  2067. /* Enable MAC Tx */
  2068. XGMAC_IOWRITE_BITS(pdata, MAC_TCR, TE, 1);
  2069. }
  2070. static void xgbe_disable_tx(struct xgbe_prv_data *pdata)
  2071. {
  2072. struct xgbe_channel *channel;
  2073. unsigned int i;
  2074. /* Prepare for Tx DMA channel stop */
  2075. channel = pdata->channel;
  2076. for (i = 0; i < pdata->channel_count; i++, channel++) {
  2077. if (!channel->tx_ring)
  2078. break;
  2079. xgbe_prepare_tx_stop(pdata, channel);
  2080. }
  2081. /* Disable MAC Tx */
  2082. XGMAC_IOWRITE_BITS(pdata, MAC_TCR, TE, 0);
  2083. /* Disable each Tx queue */
  2084. for (i = 0; i < pdata->tx_q_count; i++)
  2085. XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_Q_TQOMR, TXQEN, 0);
  2086. /* Disable each Tx DMA channel */
  2087. channel = pdata->channel;
  2088. for (i = 0; i < pdata->channel_count; i++, channel++) {
  2089. if (!channel->tx_ring)
  2090. break;
  2091. XGMAC_DMA_IOWRITE_BITS(channel, DMA_CH_TCR, ST, 0);
  2092. }
  2093. }
  2094. static void xgbe_enable_rx(struct xgbe_prv_data *pdata)
  2095. {
  2096. struct xgbe_channel *channel;
  2097. unsigned int reg_val, i;
  2098. /* Enable each Rx DMA channel */
  2099. channel = pdata->channel;
  2100. for (i = 0; i < pdata->channel_count; i++, channel++) {
  2101. if (!channel->rx_ring)
  2102. break;
  2103. XGMAC_DMA_IOWRITE_BITS(channel, DMA_CH_RCR, SR, 1);
  2104. }
  2105. /* Enable each Rx queue */
  2106. reg_val = 0;
  2107. for (i = 0; i < pdata->rx_q_count; i++)
  2108. reg_val |= (0x02 << (i << 1));
  2109. XGMAC_IOWRITE(pdata, MAC_RQC0R, reg_val);
  2110. /* Enable MAC Rx */
  2111. XGMAC_IOWRITE_BITS(pdata, MAC_RCR, DCRCC, 1);
  2112. XGMAC_IOWRITE_BITS(pdata, MAC_RCR, CST, 1);
  2113. XGMAC_IOWRITE_BITS(pdata, MAC_RCR, ACS, 1);
  2114. XGMAC_IOWRITE_BITS(pdata, MAC_RCR, RE, 1);
  2115. }
  2116. static void xgbe_disable_rx(struct xgbe_prv_data *pdata)
  2117. {
  2118. struct xgbe_channel *channel;
  2119. unsigned int i;
  2120. /* Disable MAC Rx */
  2121. XGMAC_IOWRITE_BITS(pdata, MAC_RCR, DCRCC, 0);
  2122. XGMAC_IOWRITE_BITS(pdata, MAC_RCR, CST, 0);
  2123. XGMAC_IOWRITE_BITS(pdata, MAC_RCR, ACS, 0);
  2124. XGMAC_IOWRITE_BITS(pdata, MAC_RCR, RE, 0);
  2125. /* Disable each Rx queue */
  2126. XGMAC_IOWRITE(pdata, MAC_RQC0R, 0);
  2127. /* Disable each Rx DMA channel */
  2128. channel = pdata->channel;
  2129. for (i = 0; i < pdata->channel_count; i++, channel++) {
  2130. if (!channel->rx_ring)
  2131. break;
  2132. XGMAC_DMA_IOWRITE_BITS(channel, DMA_CH_RCR, SR, 0);
  2133. }
  2134. }
  2135. static void xgbe_powerup_tx(struct xgbe_prv_data *pdata)
  2136. {
  2137. struct xgbe_channel *channel;
  2138. unsigned int i;
  2139. /* Enable each Tx DMA channel */
  2140. channel = pdata->channel;
  2141. for (i = 0; i < pdata->channel_count; i++, channel++) {
  2142. if (!channel->tx_ring)
  2143. break;
  2144. XGMAC_DMA_IOWRITE_BITS(channel, DMA_CH_TCR, ST, 1);
  2145. }
  2146. /* Enable MAC Tx */
  2147. XGMAC_IOWRITE_BITS(pdata, MAC_TCR, TE, 1);
  2148. }
  2149. static void xgbe_powerdown_tx(struct xgbe_prv_data *pdata)
  2150. {
  2151. struct xgbe_channel *channel;
  2152. unsigned int i;
  2153. /* Prepare for Tx DMA channel stop */
  2154. channel = pdata->channel;
  2155. for (i = 0; i < pdata->channel_count; i++, channel++) {
  2156. if (!channel->tx_ring)
  2157. break;
  2158. xgbe_prepare_tx_stop(pdata, channel);
  2159. }
  2160. /* Disable MAC Tx */
  2161. XGMAC_IOWRITE_BITS(pdata, MAC_TCR, TE, 0);
  2162. /* Disable each Tx DMA channel */
  2163. channel = pdata->channel;
  2164. for (i = 0; i < pdata->channel_count; i++, channel++) {
  2165. if (!channel->tx_ring)
  2166. break;
  2167. XGMAC_DMA_IOWRITE_BITS(channel, DMA_CH_TCR, ST, 0);
  2168. }
  2169. }
  2170. static void xgbe_powerup_rx(struct xgbe_prv_data *pdata)
  2171. {
  2172. struct xgbe_channel *channel;
  2173. unsigned int i;
  2174. /* Enable each Rx DMA channel */
  2175. channel = pdata->channel;
  2176. for (i = 0; i < pdata->channel_count; i++, channel++) {
  2177. if (!channel->rx_ring)
  2178. break;
  2179. XGMAC_DMA_IOWRITE_BITS(channel, DMA_CH_RCR, SR, 1);
  2180. }
  2181. }
  2182. static void xgbe_powerdown_rx(struct xgbe_prv_data *pdata)
  2183. {
  2184. struct xgbe_channel *channel;
  2185. unsigned int i;
  2186. /* Disable each Rx DMA channel */
  2187. channel = pdata->channel;
  2188. for (i = 0; i < pdata->channel_count; i++, channel++) {
  2189. if (!channel->rx_ring)
  2190. break;
  2191. XGMAC_DMA_IOWRITE_BITS(channel, DMA_CH_RCR, SR, 0);
  2192. }
  2193. }
  2194. static int xgbe_init(struct xgbe_prv_data *pdata)
  2195. {
  2196. struct xgbe_desc_if *desc_if = &pdata->desc_if;
  2197. int ret;
  2198. DBGPR("-->xgbe_init\n");
  2199. /* Flush Tx queues */
  2200. ret = xgbe_flush_tx_queues(pdata);
  2201. if (ret)
  2202. return ret;
  2203. /*
  2204. * Initialize DMA related features
  2205. */
  2206. xgbe_config_dma_bus(pdata);
  2207. xgbe_config_dma_cache(pdata);
  2208. xgbe_config_osp_mode(pdata);
  2209. xgbe_config_pblx8(pdata);
  2210. xgbe_config_tx_pbl_val(pdata);
  2211. xgbe_config_rx_pbl_val(pdata);
  2212. xgbe_config_rx_coalesce(pdata);
  2213. xgbe_config_tx_coalesce(pdata);
  2214. xgbe_config_rx_buffer_size(pdata);
  2215. xgbe_config_tso_mode(pdata);
  2216. xgbe_config_sph_mode(pdata);
  2217. xgbe_config_rss(pdata);
  2218. desc_if->wrapper_tx_desc_init(pdata);
  2219. desc_if->wrapper_rx_desc_init(pdata);
  2220. xgbe_enable_dma_interrupts(pdata);
  2221. /*
  2222. * Initialize MTL related features
  2223. */
  2224. xgbe_config_mtl_mode(pdata);
  2225. xgbe_config_queue_mapping(pdata);
  2226. xgbe_config_tsf_mode(pdata, pdata->tx_sf_mode);
  2227. xgbe_config_rsf_mode(pdata, pdata->rx_sf_mode);
  2228. xgbe_config_tx_threshold(pdata, pdata->tx_threshold);
  2229. xgbe_config_rx_threshold(pdata, pdata->rx_threshold);
  2230. xgbe_config_tx_fifo_size(pdata);
  2231. xgbe_config_rx_fifo_size(pdata);
  2232. xgbe_config_flow_control_threshold(pdata);
  2233. /*TODO: Error Packet and undersized good Packet forwarding enable
  2234. (FEP and FUP)
  2235. */
  2236. xgbe_config_dcb_tc(pdata);
  2237. xgbe_config_dcb_pfc(pdata);
  2238. xgbe_enable_mtl_interrupts(pdata);
  2239. /*
  2240. * Initialize MAC related features
  2241. */
  2242. xgbe_config_mac_address(pdata);
  2243. xgbe_config_jumbo_enable(pdata);
  2244. xgbe_config_flow_control(pdata);
  2245. xgbe_config_mac_speed(pdata);
  2246. xgbe_config_checksum_offload(pdata);
  2247. xgbe_config_vlan_support(pdata);
  2248. xgbe_config_mmc(pdata);
  2249. xgbe_enable_mac_interrupts(pdata);
  2250. DBGPR("<--xgbe_init\n");
  2251. return 0;
  2252. }
  2253. void xgbe_init_function_ptrs_dev(struct xgbe_hw_if *hw_if)
  2254. {
  2255. DBGPR("-->xgbe_init_function_ptrs\n");
  2256. hw_if->tx_complete = xgbe_tx_complete;
  2257. hw_if->set_promiscuous_mode = xgbe_set_promiscuous_mode;
  2258. hw_if->set_all_multicast_mode = xgbe_set_all_multicast_mode;
  2259. hw_if->add_mac_addresses = xgbe_add_mac_addresses;
  2260. hw_if->set_mac_address = xgbe_set_mac_address;
  2261. hw_if->enable_rx_csum = xgbe_enable_rx_csum;
  2262. hw_if->disable_rx_csum = xgbe_disable_rx_csum;
  2263. hw_if->enable_rx_vlan_stripping = xgbe_enable_rx_vlan_stripping;
  2264. hw_if->disable_rx_vlan_stripping = xgbe_disable_rx_vlan_stripping;
  2265. hw_if->enable_rx_vlan_filtering = xgbe_enable_rx_vlan_filtering;
  2266. hw_if->disable_rx_vlan_filtering = xgbe_disable_rx_vlan_filtering;
  2267. hw_if->update_vlan_hash_table = xgbe_update_vlan_hash_table;
  2268. hw_if->read_mmd_regs = xgbe_read_mmd_regs;
  2269. hw_if->write_mmd_regs = xgbe_write_mmd_regs;
  2270. hw_if->set_gmii_speed = xgbe_set_gmii_speed;
  2271. hw_if->set_gmii_2500_speed = xgbe_set_gmii_2500_speed;
  2272. hw_if->set_xgmii_speed = xgbe_set_xgmii_speed;
  2273. hw_if->enable_tx = xgbe_enable_tx;
  2274. hw_if->disable_tx = xgbe_disable_tx;
  2275. hw_if->enable_rx = xgbe_enable_rx;
  2276. hw_if->disable_rx = xgbe_disable_rx;
  2277. hw_if->powerup_tx = xgbe_powerup_tx;
  2278. hw_if->powerdown_tx = xgbe_powerdown_tx;
  2279. hw_if->powerup_rx = xgbe_powerup_rx;
  2280. hw_if->powerdown_rx = xgbe_powerdown_rx;
  2281. hw_if->dev_xmit = xgbe_dev_xmit;
  2282. hw_if->dev_read = xgbe_dev_read;
  2283. hw_if->enable_int = xgbe_enable_int;
  2284. hw_if->disable_int = xgbe_disable_int;
  2285. hw_if->init = xgbe_init;
  2286. hw_if->exit = xgbe_exit;
  2287. /* Descriptor related Sequences have to be initialized here */
  2288. hw_if->tx_desc_init = xgbe_tx_desc_init;
  2289. hw_if->rx_desc_init = xgbe_rx_desc_init;
  2290. hw_if->tx_desc_reset = xgbe_tx_desc_reset;
  2291. hw_if->rx_desc_reset = xgbe_rx_desc_reset;
  2292. hw_if->is_last_desc = xgbe_is_last_desc;
  2293. hw_if->is_context_desc = xgbe_is_context_desc;
  2294. hw_if->tx_start_xmit = xgbe_tx_start_xmit;
  2295. /* For FLOW ctrl */
  2296. hw_if->config_tx_flow_control = xgbe_config_tx_flow_control;
  2297. hw_if->config_rx_flow_control = xgbe_config_rx_flow_control;
  2298. /* For RX coalescing */
  2299. hw_if->config_rx_coalesce = xgbe_config_rx_coalesce;
  2300. hw_if->config_tx_coalesce = xgbe_config_tx_coalesce;
  2301. hw_if->usec_to_riwt = xgbe_usec_to_riwt;
  2302. hw_if->riwt_to_usec = xgbe_riwt_to_usec;
  2303. /* For RX and TX threshold config */
  2304. hw_if->config_rx_threshold = xgbe_config_rx_threshold;
  2305. hw_if->config_tx_threshold = xgbe_config_tx_threshold;
  2306. /* For RX and TX Store and Forward Mode config */
  2307. hw_if->config_rsf_mode = xgbe_config_rsf_mode;
  2308. hw_if->config_tsf_mode = xgbe_config_tsf_mode;
  2309. /* For TX DMA Operating on Second Frame config */
  2310. hw_if->config_osp_mode = xgbe_config_osp_mode;
  2311. /* For RX and TX PBL config */
  2312. hw_if->config_rx_pbl_val = xgbe_config_rx_pbl_val;
  2313. hw_if->get_rx_pbl_val = xgbe_get_rx_pbl_val;
  2314. hw_if->config_tx_pbl_val = xgbe_config_tx_pbl_val;
  2315. hw_if->get_tx_pbl_val = xgbe_get_tx_pbl_val;
  2316. hw_if->config_pblx8 = xgbe_config_pblx8;
  2317. /* For MMC statistics support */
  2318. hw_if->tx_mmc_int = xgbe_tx_mmc_int;
  2319. hw_if->rx_mmc_int = xgbe_rx_mmc_int;
  2320. hw_if->read_mmc_stats = xgbe_read_mmc_stats;
  2321. /* For PTP config */
  2322. hw_if->config_tstamp = xgbe_config_tstamp;
  2323. hw_if->update_tstamp_addend = xgbe_update_tstamp_addend;
  2324. hw_if->set_tstamp_time = xgbe_set_tstamp_time;
  2325. hw_if->get_tstamp_time = xgbe_get_tstamp_time;
  2326. hw_if->get_tx_tstamp = xgbe_get_tx_tstamp;
  2327. /* For Data Center Bridging config */
  2328. hw_if->config_dcb_tc = xgbe_config_dcb_tc;
  2329. hw_if->config_dcb_pfc = xgbe_config_dcb_pfc;
  2330. /* For Receive Side Scaling */
  2331. hw_if->enable_rss = xgbe_enable_rss;
  2332. hw_if->disable_rss = xgbe_disable_rss;
  2333. hw_if->set_rss_hash_key = xgbe_set_rss_hash_key;
  2334. hw_if->set_rss_lookup_table = xgbe_set_rss_lookup_table;
  2335. DBGPR("<--xgbe_init_function_ptrs\n");
  2336. }