xgbe-dev.c 79 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894
  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. netif_dbg(pdata, drv, pdata->netdev, "%s promiscuous mode\n",
  571. enable ? "entering" : "leaving");
  572. XGMAC_IOWRITE_BITS(pdata, MAC_PFR, PR, val);
  573. return 0;
  574. }
  575. static int xgbe_set_all_multicast_mode(struct xgbe_prv_data *pdata,
  576. unsigned int enable)
  577. {
  578. unsigned int val = enable ? 1 : 0;
  579. if (XGMAC_IOREAD_BITS(pdata, MAC_PFR, PM) == val)
  580. return 0;
  581. netif_dbg(pdata, drv, pdata->netdev, "%s allmulti mode\n",
  582. enable ? "entering" : "leaving");
  583. XGMAC_IOWRITE_BITS(pdata, MAC_PFR, PM, val);
  584. return 0;
  585. }
  586. static void xgbe_set_mac_reg(struct xgbe_prv_data *pdata,
  587. struct netdev_hw_addr *ha, unsigned int *mac_reg)
  588. {
  589. unsigned int mac_addr_hi, mac_addr_lo;
  590. u8 *mac_addr;
  591. mac_addr_lo = 0;
  592. mac_addr_hi = 0;
  593. if (ha) {
  594. mac_addr = (u8 *)&mac_addr_lo;
  595. mac_addr[0] = ha->addr[0];
  596. mac_addr[1] = ha->addr[1];
  597. mac_addr[2] = ha->addr[2];
  598. mac_addr[3] = ha->addr[3];
  599. mac_addr = (u8 *)&mac_addr_hi;
  600. mac_addr[0] = ha->addr[4];
  601. mac_addr[1] = ha->addr[5];
  602. netif_dbg(pdata, drv, pdata->netdev,
  603. "adding mac address %pM at %#x\n",
  604. ha->addr, *mac_reg);
  605. XGMAC_SET_BITS(mac_addr_hi, MAC_MACA1HR, AE, 1);
  606. }
  607. XGMAC_IOWRITE(pdata, *mac_reg, mac_addr_hi);
  608. *mac_reg += MAC_MACA_INC;
  609. XGMAC_IOWRITE(pdata, *mac_reg, mac_addr_lo);
  610. *mac_reg += MAC_MACA_INC;
  611. }
  612. static void xgbe_set_mac_addn_addrs(struct xgbe_prv_data *pdata)
  613. {
  614. struct net_device *netdev = pdata->netdev;
  615. struct netdev_hw_addr *ha;
  616. unsigned int mac_reg;
  617. unsigned int addn_macs;
  618. mac_reg = MAC_MACA1HR;
  619. addn_macs = pdata->hw_feat.addn_mac;
  620. if (netdev_uc_count(netdev) > addn_macs) {
  621. xgbe_set_promiscuous_mode(pdata, 1);
  622. } else {
  623. netdev_for_each_uc_addr(ha, netdev) {
  624. xgbe_set_mac_reg(pdata, ha, &mac_reg);
  625. addn_macs--;
  626. }
  627. if (netdev_mc_count(netdev) > addn_macs) {
  628. xgbe_set_all_multicast_mode(pdata, 1);
  629. } else {
  630. netdev_for_each_mc_addr(ha, netdev) {
  631. xgbe_set_mac_reg(pdata, ha, &mac_reg);
  632. addn_macs--;
  633. }
  634. }
  635. }
  636. /* Clear remaining additional MAC address entries */
  637. while (addn_macs--)
  638. xgbe_set_mac_reg(pdata, NULL, &mac_reg);
  639. }
  640. static void xgbe_set_mac_hash_table(struct xgbe_prv_data *pdata)
  641. {
  642. struct net_device *netdev = pdata->netdev;
  643. struct netdev_hw_addr *ha;
  644. unsigned int hash_reg;
  645. unsigned int hash_table_shift, hash_table_count;
  646. u32 hash_table[XGBE_MAC_HASH_TABLE_SIZE];
  647. u32 crc;
  648. unsigned int i;
  649. hash_table_shift = 26 - (pdata->hw_feat.hash_table_size >> 7);
  650. hash_table_count = pdata->hw_feat.hash_table_size / 32;
  651. memset(hash_table, 0, sizeof(hash_table));
  652. /* Build the MAC Hash Table register values */
  653. netdev_for_each_uc_addr(ha, netdev) {
  654. crc = bitrev32(~crc32_le(~0, ha->addr, ETH_ALEN));
  655. crc >>= hash_table_shift;
  656. hash_table[crc >> 5] |= (1 << (crc & 0x1f));
  657. }
  658. netdev_for_each_mc_addr(ha, netdev) {
  659. crc = bitrev32(~crc32_le(~0, ha->addr, ETH_ALEN));
  660. crc >>= hash_table_shift;
  661. hash_table[crc >> 5] |= (1 << (crc & 0x1f));
  662. }
  663. /* Set the MAC Hash Table registers */
  664. hash_reg = MAC_HTR0;
  665. for (i = 0; i < hash_table_count; i++) {
  666. XGMAC_IOWRITE(pdata, hash_reg, hash_table[i]);
  667. hash_reg += MAC_HTR_INC;
  668. }
  669. }
  670. static int xgbe_add_mac_addresses(struct xgbe_prv_data *pdata)
  671. {
  672. if (pdata->hw_feat.hash_table_size)
  673. xgbe_set_mac_hash_table(pdata);
  674. else
  675. xgbe_set_mac_addn_addrs(pdata);
  676. return 0;
  677. }
  678. static int xgbe_set_mac_address(struct xgbe_prv_data *pdata, u8 *addr)
  679. {
  680. unsigned int mac_addr_hi, mac_addr_lo;
  681. mac_addr_hi = (addr[5] << 8) | (addr[4] << 0);
  682. mac_addr_lo = (addr[3] << 24) | (addr[2] << 16) |
  683. (addr[1] << 8) | (addr[0] << 0);
  684. XGMAC_IOWRITE(pdata, MAC_MACA0HR, mac_addr_hi);
  685. XGMAC_IOWRITE(pdata, MAC_MACA0LR, mac_addr_lo);
  686. return 0;
  687. }
  688. static int xgbe_config_rx_mode(struct xgbe_prv_data *pdata)
  689. {
  690. struct net_device *netdev = pdata->netdev;
  691. unsigned int pr_mode, am_mode;
  692. pr_mode = ((netdev->flags & IFF_PROMISC) != 0);
  693. am_mode = ((netdev->flags & IFF_ALLMULTI) != 0);
  694. xgbe_set_promiscuous_mode(pdata, pr_mode);
  695. xgbe_set_all_multicast_mode(pdata, am_mode);
  696. xgbe_add_mac_addresses(pdata);
  697. return 0;
  698. }
  699. static int xgbe_read_mmd_regs(struct xgbe_prv_data *pdata, int prtad,
  700. int mmd_reg)
  701. {
  702. unsigned int mmd_address;
  703. int mmd_data;
  704. if (mmd_reg & MII_ADDR_C45)
  705. mmd_address = mmd_reg & ~MII_ADDR_C45;
  706. else
  707. mmd_address = (pdata->mdio_mmd << 16) | (mmd_reg & 0xffff);
  708. /* The PCS registers are accessed using mmio. The underlying APB3
  709. * management interface uses indirect addressing to access the MMD
  710. * register sets. This requires accessing of the PCS register in two
  711. * phases, an address phase and a data phase.
  712. *
  713. * The mmio interface is based on 32-bit offsets and values. All
  714. * register offsets must therefore be adjusted by left shifting the
  715. * offset 2 bits and reading 32 bits of data.
  716. */
  717. mutex_lock(&pdata->xpcs_mutex);
  718. XPCS_IOWRITE(pdata, PCS_MMD_SELECT << 2, mmd_address >> 8);
  719. mmd_data = XPCS_IOREAD(pdata, (mmd_address & 0xff) << 2);
  720. mutex_unlock(&pdata->xpcs_mutex);
  721. return mmd_data;
  722. }
  723. static void xgbe_write_mmd_regs(struct xgbe_prv_data *pdata, int prtad,
  724. int mmd_reg, int mmd_data)
  725. {
  726. unsigned int mmd_address;
  727. if (mmd_reg & MII_ADDR_C45)
  728. mmd_address = mmd_reg & ~MII_ADDR_C45;
  729. else
  730. mmd_address = (pdata->mdio_mmd << 16) | (mmd_reg & 0xffff);
  731. /* The PCS registers are accessed using mmio. The underlying APB3
  732. * management interface uses indirect addressing to access the MMD
  733. * register sets. This requires accessing of the PCS register in two
  734. * phases, an address phase and a data phase.
  735. *
  736. * The mmio interface is based on 32-bit offsets and values. All
  737. * register offsets must therefore be adjusted by left shifting the
  738. * offset 2 bits and reading 32 bits of data.
  739. */
  740. mutex_lock(&pdata->xpcs_mutex);
  741. XPCS_IOWRITE(pdata, PCS_MMD_SELECT << 2, mmd_address >> 8);
  742. XPCS_IOWRITE(pdata, (mmd_address & 0xff) << 2, mmd_data);
  743. mutex_unlock(&pdata->xpcs_mutex);
  744. }
  745. static int xgbe_tx_complete(struct xgbe_ring_desc *rdesc)
  746. {
  747. return !XGMAC_GET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, OWN);
  748. }
  749. static int xgbe_disable_rx_csum(struct xgbe_prv_data *pdata)
  750. {
  751. XGMAC_IOWRITE_BITS(pdata, MAC_RCR, IPC, 0);
  752. return 0;
  753. }
  754. static int xgbe_enable_rx_csum(struct xgbe_prv_data *pdata)
  755. {
  756. XGMAC_IOWRITE_BITS(pdata, MAC_RCR, IPC, 1);
  757. return 0;
  758. }
  759. static int xgbe_enable_rx_vlan_stripping(struct xgbe_prv_data *pdata)
  760. {
  761. /* Put the VLAN tag in the Rx descriptor */
  762. XGMAC_IOWRITE_BITS(pdata, MAC_VLANTR, EVLRXS, 1);
  763. /* Don't check the VLAN type */
  764. XGMAC_IOWRITE_BITS(pdata, MAC_VLANTR, DOVLTC, 1);
  765. /* Check only C-TAG (0x8100) packets */
  766. XGMAC_IOWRITE_BITS(pdata, MAC_VLANTR, ERSVLM, 0);
  767. /* Don't consider an S-TAG (0x88A8) packet as a VLAN packet */
  768. XGMAC_IOWRITE_BITS(pdata, MAC_VLANTR, ESVL, 0);
  769. /* Enable VLAN tag stripping */
  770. XGMAC_IOWRITE_BITS(pdata, MAC_VLANTR, EVLS, 0x3);
  771. return 0;
  772. }
  773. static int xgbe_disable_rx_vlan_stripping(struct xgbe_prv_data *pdata)
  774. {
  775. XGMAC_IOWRITE_BITS(pdata, MAC_VLANTR, EVLS, 0);
  776. return 0;
  777. }
  778. static int xgbe_enable_rx_vlan_filtering(struct xgbe_prv_data *pdata)
  779. {
  780. /* Enable VLAN filtering */
  781. XGMAC_IOWRITE_BITS(pdata, MAC_PFR, VTFE, 1);
  782. /* Enable VLAN Hash Table filtering */
  783. XGMAC_IOWRITE_BITS(pdata, MAC_VLANTR, VTHM, 1);
  784. /* Disable VLAN tag inverse matching */
  785. XGMAC_IOWRITE_BITS(pdata, MAC_VLANTR, VTIM, 0);
  786. /* Only filter on the lower 12-bits of the VLAN tag */
  787. XGMAC_IOWRITE_BITS(pdata, MAC_VLANTR, ETV, 1);
  788. /* In order for the VLAN Hash Table filtering to be effective,
  789. * the VLAN tag identifier in the VLAN Tag Register must not
  790. * be zero. Set the VLAN tag identifier to "1" to enable the
  791. * VLAN Hash Table filtering. This implies that a VLAN tag of
  792. * 1 will always pass filtering.
  793. */
  794. XGMAC_IOWRITE_BITS(pdata, MAC_VLANTR, VL, 1);
  795. return 0;
  796. }
  797. static int xgbe_disable_rx_vlan_filtering(struct xgbe_prv_data *pdata)
  798. {
  799. /* Disable VLAN filtering */
  800. XGMAC_IOWRITE_BITS(pdata, MAC_PFR, VTFE, 0);
  801. return 0;
  802. }
  803. #ifndef CRCPOLY_LE
  804. #define CRCPOLY_LE 0xedb88320
  805. #endif
  806. static u32 xgbe_vid_crc32_le(__le16 vid_le)
  807. {
  808. u32 poly = CRCPOLY_LE;
  809. u32 crc = ~0;
  810. u32 temp = 0;
  811. unsigned char *data = (unsigned char *)&vid_le;
  812. unsigned char data_byte = 0;
  813. int i, bits;
  814. bits = get_bitmask_order(VLAN_VID_MASK);
  815. for (i = 0; i < bits; i++) {
  816. if ((i % 8) == 0)
  817. data_byte = data[i / 8];
  818. temp = ((crc & 1) ^ data_byte) & 1;
  819. crc >>= 1;
  820. data_byte >>= 1;
  821. if (temp)
  822. crc ^= poly;
  823. }
  824. return crc;
  825. }
  826. static int xgbe_update_vlan_hash_table(struct xgbe_prv_data *pdata)
  827. {
  828. u32 crc;
  829. u16 vid;
  830. __le16 vid_le;
  831. u16 vlan_hash_table = 0;
  832. /* Generate the VLAN Hash Table value */
  833. for_each_set_bit(vid, pdata->active_vlans, VLAN_N_VID) {
  834. /* Get the CRC32 value of the VLAN ID */
  835. vid_le = cpu_to_le16(vid);
  836. crc = bitrev32(~xgbe_vid_crc32_le(vid_le)) >> 28;
  837. vlan_hash_table |= (1 << crc);
  838. }
  839. /* Set the VLAN Hash Table filtering register */
  840. XGMAC_IOWRITE_BITS(pdata, MAC_VLANHTR, VLHT, vlan_hash_table);
  841. return 0;
  842. }
  843. static void xgbe_tx_desc_reset(struct xgbe_ring_data *rdata)
  844. {
  845. struct xgbe_ring_desc *rdesc = rdata->rdesc;
  846. /* Reset the Tx descriptor
  847. * Set buffer 1 (lo) address to zero
  848. * Set buffer 1 (hi) address to zero
  849. * Reset all other control bits (IC, TTSE, B2L & B1L)
  850. * Reset all other control bits (OWN, CTXT, FD, LD, CPC, CIC, etc)
  851. */
  852. rdesc->desc0 = 0;
  853. rdesc->desc1 = 0;
  854. rdesc->desc2 = 0;
  855. rdesc->desc3 = 0;
  856. /* Make sure ownership is written to the descriptor */
  857. dma_wmb();
  858. }
  859. static void xgbe_tx_desc_init(struct xgbe_channel *channel)
  860. {
  861. struct xgbe_ring *ring = channel->tx_ring;
  862. struct xgbe_ring_data *rdata;
  863. int i;
  864. int start_index = ring->cur;
  865. DBGPR("-->tx_desc_init\n");
  866. /* Initialze all descriptors */
  867. for (i = 0; i < ring->rdesc_count; i++) {
  868. rdata = XGBE_GET_DESC_DATA(ring, i);
  869. /* Initialize Tx descriptor */
  870. xgbe_tx_desc_reset(rdata);
  871. }
  872. /* Update the total number of Tx descriptors */
  873. XGMAC_DMA_IOWRITE(channel, DMA_CH_TDRLR, ring->rdesc_count - 1);
  874. /* Update the starting address of descriptor ring */
  875. rdata = XGBE_GET_DESC_DATA(ring, start_index);
  876. XGMAC_DMA_IOWRITE(channel, DMA_CH_TDLR_HI,
  877. upper_32_bits(rdata->rdesc_dma));
  878. XGMAC_DMA_IOWRITE(channel, DMA_CH_TDLR_LO,
  879. lower_32_bits(rdata->rdesc_dma));
  880. DBGPR("<--tx_desc_init\n");
  881. }
  882. static void xgbe_rx_desc_reset(struct xgbe_prv_data *pdata,
  883. struct xgbe_ring_data *rdata, unsigned int index)
  884. {
  885. struct xgbe_ring_desc *rdesc = rdata->rdesc;
  886. unsigned int rx_usecs = pdata->rx_usecs;
  887. unsigned int rx_frames = pdata->rx_frames;
  888. unsigned int inte;
  889. dma_addr_t hdr_dma, buf_dma;
  890. if (!rx_usecs && !rx_frames) {
  891. /* No coalescing, interrupt for every descriptor */
  892. inte = 1;
  893. } else {
  894. /* Set interrupt based on Rx frame coalescing setting */
  895. if (rx_frames && !((index + 1) % rx_frames))
  896. inte = 1;
  897. else
  898. inte = 0;
  899. }
  900. /* Reset the Rx descriptor
  901. * Set buffer 1 (lo) address to header dma address (lo)
  902. * Set buffer 1 (hi) address to header dma address (hi)
  903. * Set buffer 2 (lo) address to buffer dma address (lo)
  904. * Set buffer 2 (hi) address to buffer dma address (hi) and
  905. * set control bits OWN and INTE
  906. */
  907. hdr_dma = rdata->rx.hdr.dma_base + rdata->rx.hdr.dma_off;
  908. buf_dma = rdata->rx.buf.dma_base + rdata->rx.buf.dma_off;
  909. rdesc->desc0 = cpu_to_le32(lower_32_bits(hdr_dma));
  910. rdesc->desc1 = cpu_to_le32(upper_32_bits(hdr_dma));
  911. rdesc->desc2 = cpu_to_le32(lower_32_bits(buf_dma));
  912. rdesc->desc3 = cpu_to_le32(upper_32_bits(buf_dma));
  913. XGMAC_SET_BITS_LE(rdesc->desc3, RX_NORMAL_DESC3, INTE, inte);
  914. /* Since the Rx DMA engine is likely running, make sure everything
  915. * is written to the descriptor(s) before setting the OWN bit
  916. * for the descriptor
  917. */
  918. dma_wmb();
  919. XGMAC_SET_BITS_LE(rdesc->desc3, RX_NORMAL_DESC3, OWN, 1);
  920. /* Make sure ownership is written to the descriptor */
  921. dma_wmb();
  922. }
  923. static void xgbe_rx_desc_init(struct xgbe_channel *channel)
  924. {
  925. struct xgbe_prv_data *pdata = channel->pdata;
  926. struct xgbe_ring *ring = channel->rx_ring;
  927. struct xgbe_ring_data *rdata;
  928. unsigned int start_index = ring->cur;
  929. unsigned int i;
  930. DBGPR("-->rx_desc_init\n");
  931. /* Initialize all descriptors */
  932. for (i = 0; i < ring->rdesc_count; i++) {
  933. rdata = XGBE_GET_DESC_DATA(ring, i);
  934. /* Initialize Rx descriptor */
  935. xgbe_rx_desc_reset(pdata, rdata, i);
  936. }
  937. /* Update the total number of Rx descriptors */
  938. XGMAC_DMA_IOWRITE(channel, DMA_CH_RDRLR, ring->rdesc_count - 1);
  939. /* Update the starting address of descriptor ring */
  940. rdata = XGBE_GET_DESC_DATA(ring, start_index);
  941. XGMAC_DMA_IOWRITE(channel, DMA_CH_RDLR_HI,
  942. upper_32_bits(rdata->rdesc_dma));
  943. XGMAC_DMA_IOWRITE(channel, DMA_CH_RDLR_LO,
  944. lower_32_bits(rdata->rdesc_dma));
  945. /* Update the Rx Descriptor Tail Pointer */
  946. rdata = XGBE_GET_DESC_DATA(ring, start_index + ring->rdesc_count - 1);
  947. XGMAC_DMA_IOWRITE(channel, DMA_CH_RDTR_LO,
  948. lower_32_bits(rdata->rdesc_dma));
  949. DBGPR("<--rx_desc_init\n");
  950. }
  951. static void xgbe_update_tstamp_addend(struct xgbe_prv_data *pdata,
  952. unsigned int addend)
  953. {
  954. /* Set the addend register value and tell the device */
  955. XGMAC_IOWRITE(pdata, MAC_TSAR, addend);
  956. XGMAC_IOWRITE_BITS(pdata, MAC_TSCR, TSADDREG, 1);
  957. /* Wait for addend update to complete */
  958. while (XGMAC_IOREAD_BITS(pdata, MAC_TSCR, TSADDREG))
  959. udelay(5);
  960. }
  961. static void xgbe_set_tstamp_time(struct xgbe_prv_data *pdata, unsigned int sec,
  962. unsigned int nsec)
  963. {
  964. /* Set the time values and tell the device */
  965. XGMAC_IOWRITE(pdata, MAC_STSUR, sec);
  966. XGMAC_IOWRITE(pdata, MAC_STNUR, nsec);
  967. XGMAC_IOWRITE_BITS(pdata, MAC_TSCR, TSINIT, 1);
  968. /* Wait for time update to complete */
  969. while (XGMAC_IOREAD_BITS(pdata, MAC_TSCR, TSINIT))
  970. udelay(5);
  971. }
  972. static u64 xgbe_get_tstamp_time(struct xgbe_prv_data *pdata)
  973. {
  974. u64 nsec;
  975. nsec = XGMAC_IOREAD(pdata, MAC_STSR);
  976. nsec *= NSEC_PER_SEC;
  977. nsec += XGMAC_IOREAD(pdata, MAC_STNR);
  978. return nsec;
  979. }
  980. static u64 xgbe_get_tx_tstamp(struct xgbe_prv_data *pdata)
  981. {
  982. unsigned int tx_snr;
  983. u64 nsec;
  984. tx_snr = XGMAC_IOREAD(pdata, MAC_TXSNR);
  985. if (XGMAC_GET_BITS(tx_snr, MAC_TXSNR, TXTSSTSMIS))
  986. return 0;
  987. nsec = XGMAC_IOREAD(pdata, MAC_TXSSR);
  988. nsec *= NSEC_PER_SEC;
  989. nsec += tx_snr;
  990. return nsec;
  991. }
  992. static void xgbe_get_rx_tstamp(struct xgbe_packet_data *packet,
  993. struct xgbe_ring_desc *rdesc)
  994. {
  995. u64 nsec;
  996. if (XGMAC_GET_BITS_LE(rdesc->desc3, RX_CONTEXT_DESC3, TSA) &&
  997. !XGMAC_GET_BITS_LE(rdesc->desc3, RX_CONTEXT_DESC3, TSD)) {
  998. nsec = le32_to_cpu(rdesc->desc1);
  999. nsec <<= 32;
  1000. nsec |= le32_to_cpu(rdesc->desc0);
  1001. if (nsec != 0xffffffffffffffffULL) {
  1002. packet->rx_tstamp = nsec;
  1003. XGMAC_SET_BITS(packet->attributes, RX_PACKET_ATTRIBUTES,
  1004. RX_TSTAMP, 1);
  1005. }
  1006. }
  1007. }
  1008. static int xgbe_config_tstamp(struct xgbe_prv_data *pdata,
  1009. unsigned int mac_tscr)
  1010. {
  1011. /* Set one nano-second accuracy */
  1012. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSCTRLSSR, 1);
  1013. /* Set fine timestamp update */
  1014. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSCFUPDT, 1);
  1015. /* Overwrite earlier timestamps */
  1016. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TXTSSTSM, 1);
  1017. XGMAC_IOWRITE(pdata, MAC_TSCR, mac_tscr);
  1018. /* Exit if timestamping is not enabled */
  1019. if (!XGMAC_GET_BITS(mac_tscr, MAC_TSCR, TSENA))
  1020. return 0;
  1021. /* Initialize time registers */
  1022. XGMAC_IOWRITE_BITS(pdata, MAC_SSIR, SSINC, XGBE_TSTAMP_SSINC);
  1023. XGMAC_IOWRITE_BITS(pdata, MAC_SSIR, SNSINC, XGBE_TSTAMP_SNSINC);
  1024. xgbe_update_tstamp_addend(pdata, pdata->tstamp_addend);
  1025. xgbe_set_tstamp_time(pdata, 0, 0);
  1026. /* Initialize the timecounter */
  1027. timecounter_init(&pdata->tstamp_tc, &pdata->tstamp_cc,
  1028. ktime_to_ns(ktime_get_real()));
  1029. return 0;
  1030. }
  1031. static void xgbe_config_dcb_tc(struct xgbe_prv_data *pdata)
  1032. {
  1033. struct ieee_ets *ets = pdata->ets;
  1034. unsigned int total_weight, min_weight, weight;
  1035. unsigned int i;
  1036. if (!ets)
  1037. return;
  1038. /* Set Tx to deficit weighted round robin scheduling algorithm (when
  1039. * traffic class is using ETS algorithm)
  1040. */
  1041. XGMAC_IOWRITE_BITS(pdata, MTL_OMR, ETSALG, MTL_ETSALG_DWRR);
  1042. /* Set Traffic Class algorithms */
  1043. total_weight = pdata->netdev->mtu * pdata->hw_feat.tc_cnt;
  1044. min_weight = total_weight / 100;
  1045. if (!min_weight)
  1046. min_weight = 1;
  1047. for (i = 0; i < pdata->hw_feat.tc_cnt; i++) {
  1048. switch (ets->tc_tsa[i]) {
  1049. case IEEE_8021QAZ_TSA_STRICT:
  1050. netif_dbg(pdata, drv, pdata->netdev,
  1051. "TC%u using SP\n", i);
  1052. XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_TC_ETSCR, TSA,
  1053. MTL_TSA_SP);
  1054. break;
  1055. case IEEE_8021QAZ_TSA_ETS:
  1056. weight = total_weight * ets->tc_tx_bw[i] / 100;
  1057. weight = clamp(weight, min_weight, total_weight);
  1058. netif_dbg(pdata, drv, pdata->netdev,
  1059. "TC%u using DWRR (weight %u)\n", i, weight);
  1060. XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_TC_ETSCR, TSA,
  1061. MTL_TSA_ETS);
  1062. XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_TC_QWR, QW,
  1063. weight);
  1064. break;
  1065. }
  1066. }
  1067. }
  1068. static void xgbe_config_dcb_pfc(struct xgbe_prv_data *pdata)
  1069. {
  1070. struct ieee_pfc *pfc = pdata->pfc;
  1071. struct ieee_ets *ets = pdata->ets;
  1072. unsigned int mask, reg, reg_val;
  1073. unsigned int tc, prio;
  1074. if (!pfc || !ets)
  1075. return;
  1076. for (tc = 0; tc < pdata->hw_feat.tc_cnt; tc++) {
  1077. mask = 0;
  1078. for (prio = 0; prio < IEEE_8021QAZ_MAX_TCS; prio++) {
  1079. if ((pfc->pfc_en & (1 << prio)) &&
  1080. (ets->prio_tc[prio] == tc))
  1081. mask |= (1 << prio);
  1082. }
  1083. mask &= 0xff;
  1084. netif_dbg(pdata, drv, pdata->netdev, "TC%u PFC mask=%#x\n",
  1085. tc, mask);
  1086. reg = MTL_TCPM0R + (MTL_TCPM_INC * (tc / MTL_TCPM_TC_PER_REG));
  1087. reg_val = XGMAC_IOREAD(pdata, reg);
  1088. reg_val &= ~(0xff << ((tc % MTL_TCPM_TC_PER_REG) << 3));
  1089. reg_val |= (mask << ((tc % MTL_TCPM_TC_PER_REG) << 3));
  1090. XGMAC_IOWRITE(pdata, reg, reg_val);
  1091. }
  1092. xgbe_config_flow_control(pdata);
  1093. }
  1094. static void xgbe_tx_start_xmit(struct xgbe_channel *channel,
  1095. struct xgbe_ring *ring)
  1096. {
  1097. struct xgbe_prv_data *pdata = channel->pdata;
  1098. struct xgbe_ring_data *rdata;
  1099. /* Make sure everything is written before the register write */
  1100. wmb();
  1101. /* Issue a poll command to Tx DMA by writing address
  1102. * of next immediate free descriptor */
  1103. rdata = XGBE_GET_DESC_DATA(ring, ring->cur);
  1104. XGMAC_DMA_IOWRITE(channel, DMA_CH_TDTR_LO,
  1105. lower_32_bits(rdata->rdesc_dma));
  1106. /* Start the Tx timer */
  1107. if (pdata->tx_usecs && !channel->tx_timer_active) {
  1108. channel->tx_timer_active = 1;
  1109. mod_timer(&channel->tx_timer,
  1110. jiffies + usecs_to_jiffies(pdata->tx_usecs));
  1111. }
  1112. ring->tx.xmit_more = 0;
  1113. }
  1114. static void xgbe_dev_xmit(struct xgbe_channel *channel)
  1115. {
  1116. struct xgbe_prv_data *pdata = channel->pdata;
  1117. struct xgbe_ring *ring = channel->tx_ring;
  1118. struct xgbe_ring_data *rdata;
  1119. struct xgbe_ring_desc *rdesc;
  1120. struct xgbe_packet_data *packet = &ring->packet_data;
  1121. unsigned int csum, tso, vlan;
  1122. unsigned int tso_context, vlan_context;
  1123. unsigned int tx_set_ic;
  1124. int start_index = ring->cur;
  1125. int cur_index = ring->cur;
  1126. int i;
  1127. DBGPR("-->xgbe_dev_xmit\n");
  1128. csum = XGMAC_GET_BITS(packet->attributes, TX_PACKET_ATTRIBUTES,
  1129. CSUM_ENABLE);
  1130. tso = XGMAC_GET_BITS(packet->attributes, TX_PACKET_ATTRIBUTES,
  1131. TSO_ENABLE);
  1132. vlan = XGMAC_GET_BITS(packet->attributes, TX_PACKET_ATTRIBUTES,
  1133. VLAN_CTAG);
  1134. if (tso && (packet->mss != ring->tx.cur_mss))
  1135. tso_context = 1;
  1136. else
  1137. tso_context = 0;
  1138. if (vlan && (packet->vlan_ctag != ring->tx.cur_vlan_ctag))
  1139. vlan_context = 1;
  1140. else
  1141. vlan_context = 0;
  1142. /* Determine if an interrupt should be generated for this Tx:
  1143. * Interrupt:
  1144. * - Tx frame count exceeds the frame count setting
  1145. * - Addition of Tx frame count to the frame count since the
  1146. * last interrupt was set exceeds the frame count setting
  1147. * No interrupt:
  1148. * - No frame count setting specified (ethtool -C ethX tx-frames 0)
  1149. * - Addition of Tx frame count to the frame count since the
  1150. * last interrupt was set does not exceed the frame count setting
  1151. */
  1152. ring->coalesce_count += packet->tx_packets;
  1153. if (!pdata->tx_frames)
  1154. tx_set_ic = 0;
  1155. else if (packet->tx_packets > pdata->tx_frames)
  1156. tx_set_ic = 1;
  1157. else if ((ring->coalesce_count % pdata->tx_frames) <
  1158. packet->tx_packets)
  1159. tx_set_ic = 1;
  1160. else
  1161. tx_set_ic = 0;
  1162. rdata = XGBE_GET_DESC_DATA(ring, cur_index);
  1163. rdesc = rdata->rdesc;
  1164. /* Create a context descriptor if this is a TSO packet */
  1165. if (tso_context || vlan_context) {
  1166. if (tso_context) {
  1167. netif_dbg(pdata, tx_queued, pdata->netdev,
  1168. "TSO context descriptor, mss=%u\n",
  1169. packet->mss);
  1170. /* Set the MSS size */
  1171. XGMAC_SET_BITS_LE(rdesc->desc2, TX_CONTEXT_DESC2,
  1172. MSS, packet->mss);
  1173. /* Mark it as a CONTEXT descriptor */
  1174. XGMAC_SET_BITS_LE(rdesc->desc3, TX_CONTEXT_DESC3,
  1175. CTXT, 1);
  1176. /* Indicate this descriptor contains the MSS */
  1177. XGMAC_SET_BITS_LE(rdesc->desc3, TX_CONTEXT_DESC3,
  1178. TCMSSV, 1);
  1179. ring->tx.cur_mss = packet->mss;
  1180. }
  1181. if (vlan_context) {
  1182. netif_dbg(pdata, tx_queued, pdata->netdev,
  1183. "VLAN context descriptor, ctag=%u\n",
  1184. packet->vlan_ctag);
  1185. /* Mark it as a CONTEXT descriptor */
  1186. XGMAC_SET_BITS_LE(rdesc->desc3, TX_CONTEXT_DESC3,
  1187. CTXT, 1);
  1188. /* Set the VLAN tag */
  1189. XGMAC_SET_BITS_LE(rdesc->desc3, TX_CONTEXT_DESC3,
  1190. VT, packet->vlan_ctag);
  1191. /* Indicate this descriptor contains the VLAN tag */
  1192. XGMAC_SET_BITS_LE(rdesc->desc3, TX_CONTEXT_DESC3,
  1193. VLTV, 1);
  1194. ring->tx.cur_vlan_ctag = packet->vlan_ctag;
  1195. }
  1196. cur_index++;
  1197. rdata = XGBE_GET_DESC_DATA(ring, cur_index);
  1198. rdesc = rdata->rdesc;
  1199. }
  1200. /* Update buffer address (for TSO this is the header) */
  1201. rdesc->desc0 = cpu_to_le32(lower_32_bits(rdata->skb_dma));
  1202. rdesc->desc1 = cpu_to_le32(upper_32_bits(rdata->skb_dma));
  1203. /* Update the buffer length */
  1204. XGMAC_SET_BITS_LE(rdesc->desc2, TX_NORMAL_DESC2, HL_B1L,
  1205. rdata->skb_dma_len);
  1206. /* VLAN tag insertion check */
  1207. if (vlan)
  1208. XGMAC_SET_BITS_LE(rdesc->desc2, TX_NORMAL_DESC2, VTIR,
  1209. TX_NORMAL_DESC2_VLAN_INSERT);
  1210. /* Timestamp enablement check */
  1211. if (XGMAC_GET_BITS(packet->attributes, TX_PACKET_ATTRIBUTES, PTP))
  1212. XGMAC_SET_BITS_LE(rdesc->desc2, TX_NORMAL_DESC2, TTSE, 1);
  1213. /* Mark it as First Descriptor */
  1214. XGMAC_SET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, FD, 1);
  1215. /* Mark it as a NORMAL descriptor */
  1216. XGMAC_SET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, CTXT, 0);
  1217. /* Set OWN bit if not the first descriptor */
  1218. if (cur_index != start_index)
  1219. XGMAC_SET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, OWN, 1);
  1220. if (tso) {
  1221. /* Enable TSO */
  1222. XGMAC_SET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, TSE, 1);
  1223. XGMAC_SET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, TCPPL,
  1224. packet->tcp_payload_len);
  1225. XGMAC_SET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, TCPHDRLEN,
  1226. packet->tcp_header_len / 4);
  1227. pdata->ext_stats.tx_tso_packets++;
  1228. } else {
  1229. /* Enable CRC and Pad Insertion */
  1230. XGMAC_SET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, CPC, 0);
  1231. /* Enable HW CSUM */
  1232. if (csum)
  1233. XGMAC_SET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3,
  1234. CIC, 0x3);
  1235. /* Set the total length to be transmitted */
  1236. XGMAC_SET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, FL,
  1237. packet->length);
  1238. }
  1239. for (i = cur_index - start_index + 1; i < packet->rdesc_count; i++) {
  1240. cur_index++;
  1241. rdata = XGBE_GET_DESC_DATA(ring, cur_index);
  1242. rdesc = rdata->rdesc;
  1243. /* Update buffer address */
  1244. rdesc->desc0 = cpu_to_le32(lower_32_bits(rdata->skb_dma));
  1245. rdesc->desc1 = cpu_to_le32(upper_32_bits(rdata->skb_dma));
  1246. /* Update the buffer length */
  1247. XGMAC_SET_BITS_LE(rdesc->desc2, TX_NORMAL_DESC2, HL_B1L,
  1248. rdata->skb_dma_len);
  1249. /* Set OWN bit */
  1250. XGMAC_SET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, OWN, 1);
  1251. /* Mark it as NORMAL descriptor */
  1252. XGMAC_SET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, CTXT, 0);
  1253. /* Enable HW CSUM */
  1254. if (csum)
  1255. XGMAC_SET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3,
  1256. CIC, 0x3);
  1257. }
  1258. /* Set LAST bit for the last descriptor */
  1259. XGMAC_SET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, LD, 1);
  1260. /* Set IC bit based on Tx coalescing settings */
  1261. if (tx_set_ic)
  1262. XGMAC_SET_BITS_LE(rdesc->desc2, TX_NORMAL_DESC2, IC, 1);
  1263. /* Save the Tx info to report back during cleanup */
  1264. rdata->tx.packets = packet->tx_packets;
  1265. rdata->tx.bytes = packet->tx_bytes;
  1266. /* In case the Tx DMA engine is running, make sure everything
  1267. * is written to the descriptor(s) before setting the OWN bit
  1268. * for the first descriptor
  1269. */
  1270. dma_wmb();
  1271. /* Set OWN bit for the first descriptor */
  1272. rdata = XGBE_GET_DESC_DATA(ring, start_index);
  1273. rdesc = rdata->rdesc;
  1274. XGMAC_SET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, OWN, 1);
  1275. if (netif_msg_tx_queued(pdata))
  1276. xgbe_dump_tx_desc(pdata, ring, start_index,
  1277. packet->rdesc_count, 1);
  1278. /* Make sure ownership is written to the descriptor */
  1279. smp_wmb();
  1280. ring->cur = cur_index + 1;
  1281. if (!packet->skb->xmit_more ||
  1282. netif_xmit_stopped(netdev_get_tx_queue(pdata->netdev,
  1283. channel->queue_index)))
  1284. xgbe_tx_start_xmit(channel, ring);
  1285. else
  1286. ring->tx.xmit_more = 1;
  1287. DBGPR(" %s: descriptors %u to %u written\n",
  1288. channel->name, start_index & (ring->rdesc_count - 1),
  1289. (ring->cur - 1) & (ring->rdesc_count - 1));
  1290. DBGPR("<--xgbe_dev_xmit\n");
  1291. }
  1292. static int xgbe_dev_read(struct xgbe_channel *channel)
  1293. {
  1294. struct xgbe_prv_data *pdata = channel->pdata;
  1295. struct xgbe_ring *ring = channel->rx_ring;
  1296. struct xgbe_ring_data *rdata;
  1297. struct xgbe_ring_desc *rdesc;
  1298. struct xgbe_packet_data *packet = &ring->packet_data;
  1299. struct net_device *netdev = pdata->netdev;
  1300. unsigned int err, etlt, l34t;
  1301. DBGPR("-->xgbe_dev_read: cur = %d\n", ring->cur);
  1302. rdata = XGBE_GET_DESC_DATA(ring, ring->cur);
  1303. rdesc = rdata->rdesc;
  1304. /* Check for data availability */
  1305. if (XGMAC_GET_BITS_LE(rdesc->desc3, RX_NORMAL_DESC3, OWN))
  1306. return 1;
  1307. /* Make sure descriptor fields are read after reading the OWN bit */
  1308. dma_rmb();
  1309. if (netif_msg_rx_status(pdata))
  1310. xgbe_dump_rx_desc(pdata, ring, ring->cur);
  1311. if (XGMAC_GET_BITS_LE(rdesc->desc3, RX_NORMAL_DESC3, CTXT)) {
  1312. /* Timestamp Context Descriptor */
  1313. xgbe_get_rx_tstamp(packet, rdesc);
  1314. XGMAC_SET_BITS(packet->attributes, RX_PACKET_ATTRIBUTES,
  1315. CONTEXT, 1);
  1316. XGMAC_SET_BITS(packet->attributes, RX_PACKET_ATTRIBUTES,
  1317. CONTEXT_NEXT, 0);
  1318. return 0;
  1319. }
  1320. /* Normal Descriptor, be sure Context Descriptor bit is off */
  1321. XGMAC_SET_BITS(packet->attributes, RX_PACKET_ATTRIBUTES, CONTEXT, 0);
  1322. /* Indicate if a Context Descriptor is next */
  1323. if (XGMAC_GET_BITS_LE(rdesc->desc3, RX_NORMAL_DESC3, CDA))
  1324. XGMAC_SET_BITS(packet->attributes, RX_PACKET_ATTRIBUTES,
  1325. CONTEXT_NEXT, 1);
  1326. /* Get the header length */
  1327. if (XGMAC_GET_BITS_LE(rdesc->desc3, RX_NORMAL_DESC3, FD)) {
  1328. rdata->rx.hdr_len = XGMAC_GET_BITS_LE(rdesc->desc2,
  1329. RX_NORMAL_DESC2, HL);
  1330. if (rdata->rx.hdr_len)
  1331. pdata->ext_stats.rx_split_header_packets++;
  1332. }
  1333. /* Get the RSS hash */
  1334. if (XGMAC_GET_BITS_LE(rdesc->desc3, RX_NORMAL_DESC3, RSV)) {
  1335. XGMAC_SET_BITS(packet->attributes, RX_PACKET_ATTRIBUTES,
  1336. RSS_HASH, 1);
  1337. packet->rss_hash = le32_to_cpu(rdesc->desc1);
  1338. l34t = XGMAC_GET_BITS_LE(rdesc->desc3, RX_NORMAL_DESC3, L34T);
  1339. switch (l34t) {
  1340. case RX_DESC3_L34T_IPV4_TCP:
  1341. case RX_DESC3_L34T_IPV4_UDP:
  1342. case RX_DESC3_L34T_IPV6_TCP:
  1343. case RX_DESC3_L34T_IPV6_UDP:
  1344. packet->rss_hash_type = PKT_HASH_TYPE_L4;
  1345. break;
  1346. default:
  1347. packet->rss_hash_type = PKT_HASH_TYPE_L3;
  1348. }
  1349. }
  1350. /* Get the packet length */
  1351. rdata->rx.len = XGMAC_GET_BITS_LE(rdesc->desc3, RX_NORMAL_DESC3, PL);
  1352. if (!XGMAC_GET_BITS_LE(rdesc->desc3, RX_NORMAL_DESC3, LD)) {
  1353. /* Not all the data has been transferred for this packet */
  1354. XGMAC_SET_BITS(packet->attributes, RX_PACKET_ATTRIBUTES,
  1355. INCOMPLETE, 1);
  1356. return 0;
  1357. }
  1358. /* This is the last of the data for this packet */
  1359. XGMAC_SET_BITS(packet->attributes, RX_PACKET_ATTRIBUTES,
  1360. INCOMPLETE, 0);
  1361. /* Set checksum done indicator as appropriate */
  1362. if (netdev->features & NETIF_F_RXCSUM)
  1363. XGMAC_SET_BITS(packet->attributes, RX_PACKET_ATTRIBUTES,
  1364. CSUM_DONE, 1);
  1365. /* Check for errors (only valid in last descriptor) */
  1366. err = XGMAC_GET_BITS_LE(rdesc->desc3, RX_NORMAL_DESC3, ES);
  1367. etlt = XGMAC_GET_BITS_LE(rdesc->desc3, RX_NORMAL_DESC3, ETLT);
  1368. netif_dbg(pdata, rx_status, netdev, "err=%u, etlt=%#x\n", err, etlt);
  1369. if (!err || !etlt) {
  1370. /* No error if err is 0 or etlt is 0 */
  1371. if ((etlt == 0x09) &&
  1372. (netdev->features & NETIF_F_HW_VLAN_CTAG_RX)) {
  1373. XGMAC_SET_BITS(packet->attributes, RX_PACKET_ATTRIBUTES,
  1374. VLAN_CTAG, 1);
  1375. packet->vlan_ctag = XGMAC_GET_BITS_LE(rdesc->desc0,
  1376. RX_NORMAL_DESC0,
  1377. OVT);
  1378. netif_dbg(pdata, rx_status, netdev, "vlan-ctag=%#06x\n",
  1379. packet->vlan_ctag);
  1380. }
  1381. } else {
  1382. if ((etlt == 0x05) || (etlt == 0x06))
  1383. XGMAC_SET_BITS(packet->attributes, RX_PACKET_ATTRIBUTES,
  1384. CSUM_DONE, 0);
  1385. else
  1386. XGMAC_SET_BITS(packet->errors, RX_PACKET_ERRORS,
  1387. FRAME, 1);
  1388. }
  1389. DBGPR("<--xgbe_dev_read: %s - descriptor=%u (cur=%d)\n", channel->name,
  1390. ring->cur & (ring->rdesc_count - 1), ring->cur);
  1391. return 0;
  1392. }
  1393. static int xgbe_is_context_desc(struct xgbe_ring_desc *rdesc)
  1394. {
  1395. /* Rx and Tx share CTXT bit, so check TDES3.CTXT bit */
  1396. return XGMAC_GET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, CTXT);
  1397. }
  1398. static int xgbe_is_last_desc(struct xgbe_ring_desc *rdesc)
  1399. {
  1400. /* Rx and Tx share LD bit, so check TDES3.LD bit */
  1401. return XGMAC_GET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, LD);
  1402. }
  1403. static int xgbe_enable_int(struct xgbe_channel *channel,
  1404. enum xgbe_int int_id)
  1405. {
  1406. unsigned int dma_ch_ier;
  1407. dma_ch_ier = XGMAC_DMA_IOREAD(channel, DMA_CH_IER);
  1408. switch (int_id) {
  1409. case XGMAC_INT_DMA_CH_SR_TI:
  1410. XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, TIE, 1);
  1411. break;
  1412. case XGMAC_INT_DMA_CH_SR_TPS:
  1413. XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, TXSE, 1);
  1414. break;
  1415. case XGMAC_INT_DMA_CH_SR_TBU:
  1416. XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, TBUE, 1);
  1417. break;
  1418. case XGMAC_INT_DMA_CH_SR_RI:
  1419. XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, RIE, 1);
  1420. break;
  1421. case XGMAC_INT_DMA_CH_SR_RBU:
  1422. XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, RBUE, 1);
  1423. break;
  1424. case XGMAC_INT_DMA_CH_SR_RPS:
  1425. XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, RSE, 1);
  1426. break;
  1427. case XGMAC_INT_DMA_CH_SR_TI_RI:
  1428. XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, TIE, 1);
  1429. XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, RIE, 1);
  1430. break;
  1431. case XGMAC_INT_DMA_CH_SR_FBE:
  1432. XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, FBEE, 1);
  1433. break;
  1434. case XGMAC_INT_DMA_ALL:
  1435. dma_ch_ier |= channel->saved_ier;
  1436. break;
  1437. default:
  1438. return -1;
  1439. }
  1440. XGMAC_DMA_IOWRITE(channel, DMA_CH_IER, dma_ch_ier);
  1441. return 0;
  1442. }
  1443. static int xgbe_disable_int(struct xgbe_channel *channel,
  1444. enum xgbe_int int_id)
  1445. {
  1446. unsigned int dma_ch_ier;
  1447. dma_ch_ier = XGMAC_DMA_IOREAD(channel, DMA_CH_IER);
  1448. switch (int_id) {
  1449. case XGMAC_INT_DMA_CH_SR_TI:
  1450. XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, TIE, 0);
  1451. break;
  1452. case XGMAC_INT_DMA_CH_SR_TPS:
  1453. XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, TXSE, 0);
  1454. break;
  1455. case XGMAC_INT_DMA_CH_SR_TBU:
  1456. XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, TBUE, 0);
  1457. break;
  1458. case XGMAC_INT_DMA_CH_SR_RI:
  1459. XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, RIE, 0);
  1460. break;
  1461. case XGMAC_INT_DMA_CH_SR_RBU:
  1462. XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, RBUE, 0);
  1463. break;
  1464. case XGMAC_INT_DMA_CH_SR_RPS:
  1465. XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, RSE, 0);
  1466. break;
  1467. case XGMAC_INT_DMA_CH_SR_TI_RI:
  1468. XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, TIE, 0);
  1469. XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, RIE, 0);
  1470. break;
  1471. case XGMAC_INT_DMA_CH_SR_FBE:
  1472. XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, FBEE, 0);
  1473. break;
  1474. case XGMAC_INT_DMA_ALL:
  1475. channel->saved_ier = dma_ch_ier & XGBE_DMA_INTERRUPT_MASK;
  1476. dma_ch_ier &= ~XGBE_DMA_INTERRUPT_MASK;
  1477. break;
  1478. default:
  1479. return -1;
  1480. }
  1481. XGMAC_DMA_IOWRITE(channel, DMA_CH_IER, dma_ch_ier);
  1482. return 0;
  1483. }
  1484. static int xgbe_exit(struct xgbe_prv_data *pdata)
  1485. {
  1486. unsigned int count = 2000;
  1487. DBGPR("-->xgbe_exit\n");
  1488. /* Issue a software reset */
  1489. XGMAC_IOWRITE_BITS(pdata, DMA_MR, SWR, 1);
  1490. usleep_range(10, 15);
  1491. /* Poll Until Poll Condition */
  1492. while (--count && XGMAC_IOREAD_BITS(pdata, DMA_MR, SWR))
  1493. usleep_range(500, 600);
  1494. if (!count)
  1495. return -EBUSY;
  1496. DBGPR("<--xgbe_exit\n");
  1497. return 0;
  1498. }
  1499. static int xgbe_flush_tx_queues(struct xgbe_prv_data *pdata)
  1500. {
  1501. unsigned int i, count;
  1502. if (XGMAC_GET_BITS(pdata->hw_feat.version, MAC_VR, SNPSVER) < 0x21)
  1503. return 0;
  1504. for (i = 0; i < pdata->tx_q_count; i++)
  1505. XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_Q_TQOMR, FTQ, 1);
  1506. /* Poll Until Poll Condition */
  1507. for (i = 0; i < pdata->tx_q_count; i++) {
  1508. count = 2000;
  1509. while (--count && XGMAC_MTL_IOREAD_BITS(pdata, i,
  1510. MTL_Q_TQOMR, FTQ))
  1511. usleep_range(500, 600);
  1512. if (!count)
  1513. return -EBUSY;
  1514. }
  1515. return 0;
  1516. }
  1517. static void xgbe_config_dma_bus(struct xgbe_prv_data *pdata)
  1518. {
  1519. /* Set enhanced addressing mode */
  1520. XGMAC_IOWRITE_BITS(pdata, DMA_SBMR, EAME, 1);
  1521. /* Set the System Bus mode */
  1522. XGMAC_IOWRITE_BITS(pdata, DMA_SBMR, UNDEF, 1);
  1523. XGMAC_IOWRITE_BITS(pdata, DMA_SBMR, BLEN_256, 1);
  1524. }
  1525. static void xgbe_config_dma_cache(struct xgbe_prv_data *pdata)
  1526. {
  1527. unsigned int arcache, awcache;
  1528. arcache = 0;
  1529. XGMAC_SET_BITS(arcache, DMA_AXIARCR, DRC, pdata->arcache);
  1530. XGMAC_SET_BITS(arcache, DMA_AXIARCR, DRD, pdata->axdomain);
  1531. XGMAC_SET_BITS(arcache, DMA_AXIARCR, TEC, pdata->arcache);
  1532. XGMAC_SET_BITS(arcache, DMA_AXIARCR, TED, pdata->axdomain);
  1533. XGMAC_SET_BITS(arcache, DMA_AXIARCR, THC, pdata->arcache);
  1534. XGMAC_SET_BITS(arcache, DMA_AXIARCR, THD, pdata->axdomain);
  1535. XGMAC_IOWRITE(pdata, DMA_AXIARCR, arcache);
  1536. awcache = 0;
  1537. XGMAC_SET_BITS(awcache, DMA_AXIAWCR, DWC, pdata->awcache);
  1538. XGMAC_SET_BITS(awcache, DMA_AXIAWCR, DWD, pdata->axdomain);
  1539. XGMAC_SET_BITS(awcache, DMA_AXIAWCR, RPC, pdata->awcache);
  1540. XGMAC_SET_BITS(awcache, DMA_AXIAWCR, RPD, pdata->axdomain);
  1541. XGMAC_SET_BITS(awcache, DMA_AXIAWCR, RHC, pdata->awcache);
  1542. XGMAC_SET_BITS(awcache, DMA_AXIAWCR, RHD, pdata->axdomain);
  1543. XGMAC_SET_BITS(awcache, DMA_AXIAWCR, TDC, pdata->awcache);
  1544. XGMAC_SET_BITS(awcache, DMA_AXIAWCR, TDD, pdata->axdomain);
  1545. XGMAC_IOWRITE(pdata, DMA_AXIAWCR, awcache);
  1546. }
  1547. static void xgbe_config_mtl_mode(struct xgbe_prv_data *pdata)
  1548. {
  1549. unsigned int i;
  1550. /* Set Tx to weighted round robin scheduling algorithm */
  1551. XGMAC_IOWRITE_BITS(pdata, MTL_OMR, ETSALG, MTL_ETSALG_WRR);
  1552. /* Set Tx traffic classes to use WRR algorithm with equal weights */
  1553. for (i = 0; i < pdata->hw_feat.tc_cnt; i++) {
  1554. XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_TC_ETSCR, TSA,
  1555. MTL_TSA_ETS);
  1556. XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_TC_QWR, QW, 1);
  1557. }
  1558. /* Set Rx to strict priority algorithm */
  1559. XGMAC_IOWRITE_BITS(pdata, MTL_OMR, RAA, MTL_RAA_SP);
  1560. }
  1561. static unsigned int xgbe_calculate_per_queue_fifo(unsigned int fifo_size,
  1562. unsigned int queue_count)
  1563. {
  1564. unsigned int q_fifo_size;
  1565. unsigned int p_fifo;
  1566. /* Calculate the configured fifo size */
  1567. q_fifo_size = 1 << (fifo_size + 7);
  1568. /* The configured value may not be the actual amount of fifo RAM */
  1569. q_fifo_size = min_t(unsigned int, XGBE_FIFO_MAX, q_fifo_size);
  1570. q_fifo_size = q_fifo_size / queue_count;
  1571. /* Each increment in the queue fifo size represents 256 bytes of
  1572. * fifo, with 0 representing 256 bytes. Distribute the fifo equally
  1573. * between the queues.
  1574. */
  1575. p_fifo = q_fifo_size / 256;
  1576. if (p_fifo)
  1577. p_fifo--;
  1578. return p_fifo;
  1579. }
  1580. static void xgbe_config_tx_fifo_size(struct xgbe_prv_data *pdata)
  1581. {
  1582. unsigned int fifo_size;
  1583. unsigned int i;
  1584. fifo_size = xgbe_calculate_per_queue_fifo(pdata->hw_feat.tx_fifo_size,
  1585. pdata->tx_q_count);
  1586. for (i = 0; i < pdata->tx_q_count; i++)
  1587. XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_Q_TQOMR, TQS, fifo_size);
  1588. netif_info(pdata, drv, pdata->netdev,
  1589. "%d Tx hardware queues, %d byte fifo per queue\n",
  1590. pdata->tx_q_count, ((fifo_size + 1) * 256));
  1591. }
  1592. static void xgbe_config_rx_fifo_size(struct xgbe_prv_data *pdata)
  1593. {
  1594. unsigned int fifo_size;
  1595. unsigned int i;
  1596. fifo_size = xgbe_calculate_per_queue_fifo(pdata->hw_feat.rx_fifo_size,
  1597. pdata->rx_q_count);
  1598. for (i = 0; i < pdata->rx_q_count; i++)
  1599. XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_Q_RQOMR, RQS, fifo_size);
  1600. netif_info(pdata, drv, pdata->netdev,
  1601. "%d Rx hardware queues, %d byte fifo per queue\n",
  1602. pdata->rx_q_count, ((fifo_size + 1) * 256));
  1603. }
  1604. static void xgbe_config_queue_mapping(struct xgbe_prv_data *pdata)
  1605. {
  1606. unsigned int qptc, qptc_extra, queue;
  1607. unsigned int prio_queues;
  1608. unsigned int ppq, ppq_extra, prio;
  1609. unsigned int mask;
  1610. unsigned int i, j, reg, reg_val;
  1611. /* Map the MTL Tx Queues to Traffic Classes
  1612. * Note: Tx Queues >= Traffic Classes
  1613. */
  1614. qptc = pdata->tx_q_count / pdata->hw_feat.tc_cnt;
  1615. qptc_extra = pdata->tx_q_count % pdata->hw_feat.tc_cnt;
  1616. for (i = 0, queue = 0; i < pdata->hw_feat.tc_cnt; i++) {
  1617. for (j = 0; j < qptc; j++) {
  1618. netif_dbg(pdata, drv, pdata->netdev,
  1619. "TXq%u mapped to TC%u\n", queue, i);
  1620. XGMAC_MTL_IOWRITE_BITS(pdata, queue, MTL_Q_TQOMR,
  1621. Q2TCMAP, i);
  1622. pdata->q2tc_map[queue++] = i;
  1623. }
  1624. if (i < qptc_extra) {
  1625. netif_dbg(pdata, drv, pdata->netdev,
  1626. "TXq%u mapped to TC%u\n", queue, i);
  1627. XGMAC_MTL_IOWRITE_BITS(pdata, queue, MTL_Q_TQOMR,
  1628. Q2TCMAP, i);
  1629. pdata->q2tc_map[queue++] = i;
  1630. }
  1631. }
  1632. /* Map the 8 VLAN priority values to available MTL Rx queues */
  1633. prio_queues = min_t(unsigned int, IEEE_8021QAZ_MAX_TCS,
  1634. pdata->rx_q_count);
  1635. ppq = IEEE_8021QAZ_MAX_TCS / prio_queues;
  1636. ppq_extra = IEEE_8021QAZ_MAX_TCS % prio_queues;
  1637. reg = MAC_RQC2R;
  1638. reg_val = 0;
  1639. for (i = 0, prio = 0; i < prio_queues;) {
  1640. mask = 0;
  1641. for (j = 0; j < ppq; j++) {
  1642. netif_dbg(pdata, drv, pdata->netdev,
  1643. "PRIO%u mapped to RXq%u\n", prio, i);
  1644. mask |= (1 << prio);
  1645. pdata->prio2q_map[prio++] = i;
  1646. }
  1647. if (i < ppq_extra) {
  1648. netif_dbg(pdata, drv, pdata->netdev,
  1649. "PRIO%u mapped to RXq%u\n", prio, i);
  1650. mask |= (1 << prio);
  1651. pdata->prio2q_map[prio++] = i;
  1652. }
  1653. reg_val |= (mask << ((i++ % MAC_RQC2_Q_PER_REG) << 3));
  1654. if ((i % MAC_RQC2_Q_PER_REG) && (i != prio_queues))
  1655. continue;
  1656. XGMAC_IOWRITE(pdata, reg, reg_val);
  1657. reg += MAC_RQC2_INC;
  1658. reg_val = 0;
  1659. }
  1660. /* Select dynamic mapping of MTL Rx queue to DMA Rx channel */
  1661. reg = MTL_RQDCM0R;
  1662. reg_val = 0;
  1663. for (i = 0; i < pdata->rx_q_count;) {
  1664. reg_val |= (0x80 << ((i++ % MTL_RQDCM_Q_PER_REG) << 3));
  1665. if ((i % MTL_RQDCM_Q_PER_REG) && (i != pdata->rx_q_count))
  1666. continue;
  1667. XGMAC_IOWRITE(pdata, reg, reg_val);
  1668. reg += MTL_RQDCM_INC;
  1669. reg_val = 0;
  1670. }
  1671. }
  1672. static void xgbe_config_flow_control_threshold(struct xgbe_prv_data *pdata)
  1673. {
  1674. unsigned int i;
  1675. for (i = 0; i < pdata->rx_q_count; i++) {
  1676. /* Activate flow control when less than 4k left in fifo */
  1677. XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_Q_RQFCR, RFA, 2);
  1678. /* De-activate flow control when more than 6k left in fifo */
  1679. XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_Q_RQFCR, RFD, 4);
  1680. }
  1681. }
  1682. static void xgbe_config_mac_address(struct xgbe_prv_data *pdata)
  1683. {
  1684. xgbe_set_mac_address(pdata, pdata->netdev->dev_addr);
  1685. /* Filtering is done using perfect filtering and hash filtering */
  1686. if (pdata->hw_feat.hash_table_size) {
  1687. XGMAC_IOWRITE_BITS(pdata, MAC_PFR, HPF, 1);
  1688. XGMAC_IOWRITE_BITS(pdata, MAC_PFR, HUC, 1);
  1689. XGMAC_IOWRITE_BITS(pdata, MAC_PFR, HMC, 1);
  1690. }
  1691. }
  1692. static void xgbe_config_jumbo_enable(struct xgbe_prv_data *pdata)
  1693. {
  1694. unsigned int val;
  1695. val = (pdata->netdev->mtu > XGMAC_STD_PACKET_MTU) ? 1 : 0;
  1696. XGMAC_IOWRITE_BITS(pdata, MAC_RCR, JE, val);
  1697. }
  1698. static void xgbe_config_mac_speed(struct xgbe_prv_data *pdata)
  1699. {
  1700. switch (pdata->phy_speed) {
  1701. case SPEED_10000:
  1702. xgbe_set_xgmii_speed(pdata);
  1703. break;
  1704. case SPEED_2500:
  1705. xgbe_set_gmii_2500_speed(pdata);
  1706. break;
  1707. case SPEED_1000:
  1708. xgbe_set_gmii_speed(pdata);
  1709. break;
  1710. }
  1711. }
  1712. static void xgbe_config_checksum_offload(struct xgbe_prv_data *pdata)
  1713. {
  1714. if (pdata->netdev->features & NETIF_F_RXCSUM)
  1715. xgbe_enable_rx_csum(pdata);
  1716. else
  1717. xgbe_disable_rx_csum(pdata);
  1718. }
  1719. static void xgbe_config_vlan_support(struct xgbe_prv_data *pdata)
  1720. {
  1721. /* Indicate that VLAN Tx CTAGs come from context descriptors */
  1722. XGMAC_IOWRITE_BITS(pdata, MAC_VLANIR, CSVL, 0);
  1723. XGMAC_IOWRITE_BITS(pdata, MAC_VLANIR, VLTI, 1);
  1724. /* Set the current VLAN Hash Table register value */
  1725. xgbe_update_vlan_hash_table(pdata);
  1726. if (pdata->netdev->features & NETIF_F_HW_VLAN_CTAG_FILTER)
  1727. xgbe_enable_rx_vlan_filtering(pdata);
  1728. else
  1729. xgbe_disable_rx_vlan_filtering(pdata);
  1730. if (pdata->netdev->features & NETIF_F_HW_VLAN_CTAG_RX)
  1731. xgbe_enable_rx_vlan_stripping(pdata);
  1732. else
  1733. xgbe_disable_rx_vlan_stripping(pdata);
  1734. }
  1735. static u64 xgbe_mmc_read(struct xgbe_prv_data *pdata, unsigned int reg_lo)
  1736. {
  1737. bool read_hi;
  1738. u64 val;
  1739. switch (reg_lo) {
  1740. /* These registers are always 64 bit */
  1741. case MMC_TXOCTETCOUNT_GB_LO:
  1742. case MMC_TXOCTETCOUNT_G_LO:
  1743. case MMC_RXOCTETCOUNT_GB_LO:
  1744. case MMC_RXOCTETCOUNT_G_LO:
  1745. read_hi = true;
  1746. break;
  1747. default:
  1748. read_hi = false;
  1749. }
  1750. val = XGMAC_IOREAD(pdata, reg_lo);
  1751. if (read_hi)
  1752. val |= ((u64)XGMAC_IOREAD(pdata, reg_lo + 4) << 32);
  1753. return val;
  1754. }
  1755. static void xgbe_tx_mmc_int(struct xgbe_prv_data *pdata)
  1756. {
  1757. struct xgbe_mmc_stats *stats = &pdata->mmc_stats;
  1758. unsigned int mmc_isr = XGMAC_IOREAD(pdata, MMC_TISR);
  1759. if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TXOCTETCOUNT_GB))
  1760. stats->txoctetcount_gb +=
  1761. xgbe_mmc_read(pdata, MMC_TXOCTETCOUNT_GB_LO);
  1762. if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TXFRAMECOUNT_GB))
  1763. stats->txframecount_gb +=
  1764. xgbe_mmc_read(pdata, MMC_TXFRAMECOUNT_GB_LO);
  1765. if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TXBROADCASTFRAMES_G))
  1766. stats->txbroadcastframes_g +=
  1767. xgbe_mmc_read(pdata, MMC_TXBROADCASTFRAMES_G_LO);
  1768. if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TXMULTICASTFRAMES_G))
  1769. stats->txmulticastframes_g +=
  1770. xgbe_mmc_read(pdata, MMC_TXMULTICASTFRAMES_G_LO);
  1771. if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TX64OCTETS_GB))
  1772. stats->tx64octets_gb +=
  1773. xgbe_mmc_read(pdata, MMC_TX64OCTETS_GB_LO);
  1774. if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TX65TO127OCTETS_GB))
  1775. stats->tx65to127octets_gb +=
  1776. xgbe_mmc_read(pdata, MMC_TX65TO127OCTETS_GB_LO);
  1777. if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TX128TO255OCTETS_GB))
  1778. stats->tx128to255octets_gb +=
  1779. xgbe_mmc_read(pdata, MMC_TX128TO255OCTETS_GB_LO);
  1780. if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TX256TO511OCTETS_GB))
  1781. stats->tx256to511octets_gb +=
  1782. xgbe_mmc_read(pdata, MMC_TX256TO511OCTETS_GB_LO);
  1783. if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TX512TO1023OCTETS_GB))
  1784. stats->tx512to1023octets_gb +=
  1785. xgbe_mmc_read(pdata, MMC_TX512TO1023OCTETS_GB_LO);
  1786. if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TX1024TOMAXOCTETS_GB))
  1787. stats->tx1024tomaxoctets_gb +=
  1788. xgbe_mmc_read(pdata, MMC_TX1024TOMAXOCTETS_GB_LO);
  1789. if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TXUNICASTFRAMES_GB))
  1790. stats->txunicastframes_gb +=
  1791. xgbe_mmc_read(pdata, MMC_TXUNICASTFRAMES_GB_LO);
  1792. if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TXMULTICASTFRAMES_GB))
  1793. stats->txmulticastframes_gb +=
  1794. xgbe_mmc_read(pdata, MMC_TXMULTICASTFRAMES_GB_LO);
  1795. if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TXBROADCASTFRAMES_GB))
  1796. stats->txbroadcastframes_g +=
  1797. xgbe_mmc_read(pdata, MMC_TXBROADCASTFRAMES_GB_LO);
  1798. if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TXUNDERFLOWERROR))
  1799. stats->txunderflowerror +=
  1800. xgbe_mmc_read(pdata, MMC_TXUNDERFLOWERROR_LO);
  1801. if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TXOCTETCOUNT_G))
  1802. stats->txoctetcount_g +=
  1803. xgbe_mmc_read(pdata, MMC_TXOCTETCOUNT_G_LO);
  1804. if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TXFRAMECOUNT_G))
  1805. stats->txframecount_g +=
  1806. xgbe_mmc_read(pdata, MMC_TXFRAMECOUNT_G_LO);
  1807. if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TXPAUSEFRAMES))
  1808. stats->txpauseframes +=
  1809. xgbe_mmc_read(pdata, MMC_TXPAUSEFRAMES_LO);
  1810. if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TXVLANFRAMES_G))
  1811. stats->txvlanframes_g +=
  1812. xgbe_mmc_read(pdata, MMC_TXVLANFRAMES_G_LO);
  1813. }
  1814. static void xgbe_rx_mmc_int(struct xgbe_prv_data *pdata)
  1815. {
  1816. struct xgbe_mmc_stats *stats = &pdata->mmc_stats;
  1817. unsigned int mmc_isr = XGMAC_IOREAD(pdata, MMC_RISR);
  1818. if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXFRAMECOUNT_GB))
  1819. stats->rxframecount_gb +=
  1820. xgbe_mmc_read(pdata, MMC_RXFRAMECOUNT_GB_LO);
  1821. if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXOCTETCOUNT_GB))
  1822. stats->rxoctetcount_gb +=
  1823. xgbe_mmc_read(pdata, MMC_RXOCTETCOUNT_GB_LO);
  1824. if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXOCTETCOUNT_G))
  1825. stats->rxoctetcount_g +=
  1826. xgbe_mmc_read(pdata, MMC_RXOCTETCOUNT_G_LO);
  1827. if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXBROADCASTFRAMES_G))
  1828. stats->rxbroadcastframes_g +=
  1829. xgbe_mmc_read(pdata, MMC_RXBROADCASTFRAMES_G_LO);
  1830. if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXMULTICASTFRAMES_G))
  1831. stats->rxmulticastframes_g +=
  1832. xgbe_mmc_read(pdata, MMC_RXMULTICASTFRAMES_G_LO);
  1833. if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXCRCERROR))
  1834. stats->rxcrcerror +=
  1835. xgbe_mmc_read(pdata, MMC_RXCRCERROR_LO);
  1836. if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXRUNTERROR))
  1837. stats->rxrunterror +=
  1838. xgbe_mmc_read(pdata, MMC_RXRUNTERROR);
  1839. if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXJABBERERROR))
  1840. stats->rxjabbererror +=
  1841. xgbe_mmc_read(pdata, MMC_RXJABBERERROR);
  1842. if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXUNDERSIZE_G))
  1843. stats->rxundersize_g +=
  1844. xgbe_mmc_read(pdata, MMC_RXUNDERSIZE_G);
  1845. if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXOVERSIZE_G))
  1846. stats->rxoversize_g +=
  1847. xgbe_mmc_read(pdata, MMC_RXOVERSIZE_G);
  1848. if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RX64OCTETS_GB))
  1849. stats->rx64octets_gb +=
  1850. xgbe_mmc_read(pdata, MMC_RX64OCTETS_GB_LO);
  1851. if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RX65TO127OCTETS_GB))
  1852. stats->rx65to127octets_gb +=
  1853. xgbe_mmc_read(pdata, MMC_RX65TO127OCTETS_GB_LO);
  1854. if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RX128TO255OCTETS_GB))
  1855. stats->rx128to255octets_gb +=
  1856. xgbe_mmc_read(pdata, MMC_RX128TO255OCTETS_GB_LO);
  1857. if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RX256TO511OCTETS_GB))
  1858. stats->rx256to511octets_gb +=
  1859. xgbe_mmc_read(pdata, MMC_RX256TO511OCTETS_GB_LO);
  1860. if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RX512TO1023OCTETS_GB))
  1861. stats->rx512to1023octets_gb +=
  1862. xgbe_mmc_read(pdata, MMC_RX512TO1023OCTETS_GB_LO);
  1863. if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RX1024TOMAXOCTETS_GB))
  1864. stats->rx1024tomaxoctets_gb +=
  1865. xgbe_mmc_read(pdata, MMC_RX1024TOMAXOCTETS_GB_LO);
  1866. if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXUNICASTFRAMES_G))
  1867. stats->rxunicastframes_g +=
  1868. xgbe_mmc_read(pdata, MMC_RXUNICASTFRAMES_G_LO);
  1869. if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXLENGTHERROR))
  1870. stats->rxlengtherror +=
  1871. xgbe_mmc_read(pdata, MMC_RXLENGTHERROR_LO);
  1872. if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXOUTOFRANGETYPE))
  1873. stats->rxoutofrangetype +=
  1874. xgbe_mmc_read(pdata, MMC_RXOUTOFRANGETYPE_LO);
  1875. if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXPAUSEFRAMES))
  1876. stats->rxpauseframes +=
  1877. xgbe_mmc_read(pdata, MMC_RXPAUSEFRAMES_LO);
  1878. if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXFIFOOVERFLOW))
  1879. stats->rxfifooverflow +=
  1880. xgbe_mmc_read(pdata, MMC_RXFIFOOVERFLOW_LO);
  1881. if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXVLANFRAMES_GB))
  1882. stats->rxvlanframes_gb +=
  1883. xgbe_mmc_read(pdata, MMC_RXVLANFRAMES_GB_LO);
  1884. if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXWATCHDOGERROR))
  1885. stats->rxwatchdogerror +=
  1886. xgbe_mmc_read(pdata, MMC_RXWATCHDOGERROR);
  1887. }
  1888. static void xgbe_read_mmc_stats(struct xgbe_prv_data *pdata)
  1889. {
  1890. struct xgbe_mmc_stats *stats = &pdata->mmc_stats;
  1891. /* Freeze counters */
  1892. XGMAC_IOWRITE_BITS(pdata, MMC_CR, MCF, 1);
  1893. stats->txoctetcount_gb +=
  1894. xgbe_mmc_read(pdata, MMC_TXOCTETCOUNT_GB_LO);
  1895. stats->txframecount_gb +=
  1896. xgbe_mmc_read(pdata, MMC_TXFRAMECOUNT_GB_LO);
  1897. stats->txbroadcastframes_g +=
  1898. xgbe_mmc_read(pdata, MMC_TXBROADCASTFRAMES_G_LO);
  1899. stats->txmulticastframes_g +=
  1900. xgbe_mmc_read(pdata, MMC_TXMULTICASTFRAMES_G_LO);
  1901. stats->tx64octets_gb +=
  1902. xgbe_mmc_read(pdata, MMC_TX64OCTETS_GB_LO);
  1903. stats->tx65to127octets_gb +=
  1904. xgbe_mmc_read(pdata, MMC_TX65TO127OCTETS_GB_LO);
  1905. stats->tx128to255octets_gb +=
  1906. xgbe_mmc_read(pdata, MMC_TX128TO255OCTETS_GB_LO);
  1907. stats->tx256to511octets_gb +=
  1908. xgbe_mmc_read(pdata, MMC_TX256TO511OCTETS_GB_LO);
  1909. stats->tx512to1023octets_gb +=
  1910. xgbe_mmc_read(pdata, MMC_TX512TO1023OCTETS_GB_LO);
  1911. stats->tx1024tomaxoctets_gb +=
  1912. xgbe_mmc_read(pdata, MMC_TX1024TOMAXOCTETS_GB_LO);
  1913. stats->txunicastframes_gb +=
  1914. xgbe_mmc_read(pdata, MMC_TXUNICASTFRAMES_GB_LO);
  1915. stats->txmulticastframes_gb +=
  1916. xgbe_mmc_read(pdata, MMC_TXMULTICASTFRAMES_GB_LO);
  1917. stats->txbroadcastframes_g +=
  1918. xgbe_mmc_read(pdata, MMC_TXBROADCASTFRAMES_GB_LO);
  1919. stats->txunderflowerror +=
  1920. xgbe_mmc_read(pdata, MMC_TXUNDERFLOWERROR_LO);
  1921. stats->txoctetcount_g +=
  1922. xgbe_mmc_read(pdata, MMC_TXOCTETCOUNT_G_LO);
  1923. stats->txframecount_g +=
  1924. xgbe_mmc_read(pdata, MMC_TXFRAMECOUNT_G_LO);
  1925. stats->txpauseframes +=
  1926. xgbe_mmc_read(pdata, MMC_TXPAUSEFRAMES_LO);
  1927. stats->txvlanframes_g +=
  1928. xgbe_mmc_read(pdata, MMC_TXVLANFRAMES_G_LO);
  1929. stats->rxframecount_gb +=
  1930. xgbe_mmc_read(pdata, MMC_RXFRAMECOUNT_GB_LO);
  1931. stats->rxoctetcount_gb +=
  1932. xgbe_mmc_read(pdata, MMC_RXOCTETCOUNT_GB_LO);
  1933. stats->rxoctetcount_g +=
  1934. xgbe_mmc_read(pdata, MMC_RXOCTETCOUNT_G_LO);
  1935. stats->rxbroadcastframes_g +=
  1936. xgbe_mmc_read(pdata, MMC_RXBROADCASTFRAMES_G_LO);
  1937. stats->rxmulticastframes_g +=
  1938. xgbe_mmc_read(pdata, MMC_RXMULTICASTFRAMES_G_LO);
  1939. stats->rxcrcerror +=
  1940. xgbe_mmc_read(pdata, MMC_RXCRCERROR_LO);
  1941. stats->rxrunterror +=
  1942. xgbe_mmc_read(pdata, MMC_RXRUNTERROR);
  1943. stats->rxjabbererror +=
  1944. xgbe_mmc_read(pdata, MMC_RXJABBERERROR);
  1945. stats->rxundersize_g +=
  1946. xgbe_mmc_read(pdata, MMC_RXUNDERSIZE_G);
  1947. stats->rxoversize_g +=
  1948. xgbe_mmc_read(pdata, MMC_RXOVERSIZE_G);
  1949. stats->rx64octets_gb +=
  1950. xgbe_mmc_read(pdata, MMC_RX64OCTETS_GB_LO);
  1951. stats->rx65to127octets_gb +=
  1952. xgbe_mmc_read(pdata, MMC_RX65TO127OCTETS_GB_LO);
  1953. stats->rx128to255octets_gb +=
  1954. xgbe_mmc_read(pdata, MMC_RX128TO255OCTETS_GB_LO);
  1955. stats->rx256to511octets_gb +=
  1956. xgbe_mmc_read(pdata, MMC_RX256TO511OCTETS_GB_LO);
  1957. stats->rx512to1023octets_gb +=
  1958. xgbe_mmc_read(pdata, MMC_RX512TO1023OCTETS_GB_LO);
  1959. stats->rx1024tomaxoctets_gb +=
  1960. xgbe_mmc_read(pdata, MMC_RX1024TOMAXOCTETS_GB_LO);
  1961. stats->rxunicastframes_g +=
  1962. xgbe_mmc_read(pdata, MMC_RXUNICASTFRAMES_G_LO);
  1963. stats->rxlengtherror +=
  1964. xgbe_mmc_read(pdata, MMC_RXLENGTHERROR_LO);
  1965. stats->rxoutofrangetype +=
  1966. xgbe_mmc_read(pdata, MMC_RXOUTOFRANGETYPE_LO);
  1967. stats->rxpauseframes +=
  1968. xgbe_mmc_read(pdata, MMC_RXPAUSEFRAMES_LO);
  1969. stats->rxfifooverflow +=
  1970. xgbe_mmc_read(pdata, MMC_RXFIFOOVERFLOW_LO);
  1971. stats->rxvlanframes_gb +=
  1972. xgbe_mmc_read(pdata, MMC_RXVLANFRAMES_GB_LO);
  1973. stats->rxwatchdogerror +=
  1974. xgbe_mmc_read(pdata, MMC_RXWATCHDOGERROR);
  1975. /* Un-freeze counters */
  1976. XGMAC_IOWRITE_BITS(pdata, MMC_CR, MCF, 0);
  1977. }
  1978. static void xgbe_config_mmc(struct xgbe_prv_data *pdata)
  1979. {
  1980. /* Set counters to reset on read */
  1981. XGMAC_IOWRITE_BITS(pdata, MMC_CR, ROR, 1);
  1982. /* Reset the counters */
  1983. XGMAC_IOWRITE_BITS(pdata, MMC_CR, CR, 1);
  1984. }
  1985. static void xgbe_prepare_tx_stop(struct xgbe_prv_data *pdata,
  1986. struct xgbe_channel *channel)
  1987. {
  1988. unsigned int tx_dsr, tx_pos, tx_qidx;
  1989. unsigned int tx_status;
  1990. unsigned long tx_timeout;
  1991. /* Calculate the status register to read and the position within */
  1992. if (channel->queue_index < DMA_DSRX_FIRST_QUEUE) {
  1993. tx_dsr = DMA_DSR0;
  1994. tx_pos = (channel->queue_index * DMA_DSR_Q_WIDTH) +
  1995. DMA_DSR0_TPS_START;
  1996. } else {
  1997. tx_qidx = channel->queue_index - DMA_DSRX_FIRST_QUEUE;
  1998. tx_dsr = DMA_DSR1 + ((tx_qidx / DMA_DSRX_QPR) * DMA_DSRX_INC);
  1999. tx_pos = ((tx_qidx % DMA_DSRX_QPR) * DMA_DSR_Q_WIDTH) +
  2000. DMA_DSRX_TPS_START;
  2001. }
  2002. /* The Tx engine cannot be stopped if it is actively processing
  2003. * descriptors. Wait for the Tx engine to enter the stopped or
  2004. * suspended state. Don't wait forever though...
  2005. */
  2006. tx_timeout = jiffies + (XGBE_DMA_STOP_TIMEOUT * HZ);
  2007. while (time_before(jiffies, tx_timeout)) {
  2008. tx_status = XGMAC_IOREAD(pdata, tx_dsr);
  2009. tx_status = GET_BITS(tx_status, tx_pos, DMA_DSR_TPS_WIDTH);
  2010. if ((tx_status == DMA_TPS_STOPPED) ||
  2011. (tx_status == DMA_TPS_SUSPENDED))
  2012. break;
  2013. usleep_range(500, 1000);
  2014. }
  2015. if (!time_before(jiffies, tx_timeout))
  2016. netdev_info(pdata->netdev,
  2017. "timed out waiting for Tx DMA channel %u to stop\n",
  2018. channel->queue_index);
  2019. }
  2020. static void xgbe_enable_tx(struct xgbe_prv_data *pdata)
  2021. {
  2022. struct xgbe_channel *channel;
  2023. unsigned int i;
  2024. /* Enable each Tx DMA channel */
  2025. channel = pdata->channel;
  2026. for (i = 0; i < pdata->channel_count; i++, channel++) {
  2027. if (!channel->tx_ring)
  2028. break;
  2029. XGMAC_DMA_IOWRITE_BITS(channel, DMA_CH_TCR, ST, 1);
  2030. }
  2031. /* Enable each Tx queue */
  2032. for (i = 0; i < pdata->tx_q_count; i++)
  2033. XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_Q_TQOMR, TXQEN,
  2034. MTL_Q_ENABLED);
  2035. /* Enable MAC Tx */
  2036. XGMAC_IOWRITE_BITS(pdata, MAC_TCR, TE, 1);
  2037. }
  2038. static void xgbe_disable_tx(struct xgbe_prv_data *pdata)
  2039. {
  2040. struct xgbe_channel *channel;
  2041. unsigned int i;
  2042. /* Prepare for Tx DMA channel stop */
  2043. channel = pdata->channel;
  2044. for (i = 0; i < pdata->channel_count; i++, channel++) {
  2045. if (!channel->tx_ring)
  2046. break;
  2047. xgbe_prepare_tx_stop(pdata, channel);
  2048. }
  2049. /* Disable MAC Tx */
  2050. XGMAC_IOWRITE_BITS(pdata, MAC_TCR, TE, 0);
  2051. /* Disable each Tx queue */
  2052. for (i = 0; i < pdata->tx_q_count; i++)
  2053. XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_Q_TQOMR, TXQEN, 0);
  2054. /* Disable each Tx DMA channel */
  2055. channel = pdata->channel;
  2056. for (i = 0; i < pdata->channel_count; i++, channel++) {
  2057. if (!channel->tx_ring)
  2058. break;
  2059. XGMAC_DMA_IOWRITE_BITS(channel, DMA_CH_TCR, ST, 0);
  2060. }
  2061. }
  2062. static void xgbe_enable_rx(struct xgbe_prv_data *pdata)
  2063. {
  2064. struct xgbe_channel *channel;
  2065. unsigned int reg_val, i;
  2066. /* Enable each Rx DMA channel */
  2067. channel = pdata->channel;
  2068. for (i = 0; i < pdata->channel_count; i++, channel++) {
  2069. if (!channel->rx_ring)
  2070. break;
  2071. XGMAC_DMA_IOWRITE_BITS(channel, DMA_CH_RCR, SR, 1);
  2072. }
  2073. /* Enable each Rx queue */
  2074. reg_val = 0;
  2075. for (i = 0; i < pdata->rx_q_count; i++)
  2076. reg_val |= (0x02 << (i << 1));
  2077. XGMAC_IOWRITE(pdata, MAC_RQC0R, reg_val);
  2078. /* Enable MAC Rx */
  2079. XGMAC_IOWRITE_BITS(pdata, MAC_RCR, DCRCC, 1);
  2080. XGMAC_IOWRITE_BITS(pdata, MAC_RCR, CST, 1);
  2081. XGMAC_IOWRITE_BITS(pdata, MAC_RCR, ACS, 1);
  2082. XGMAC_IOWRITE_BITS(pdata, MAC_RCR, RE, 1);
  2083. }
  2084. static void xgbe_disable_rx(struct xgbe_prv_data *pdata)
  2085. {
  2086. struct xgbe_channel *channel;
  2087. unsigned int i;
  2088. /* Disable MAC Rx */
  2089. XGMAC_IOWRITE_BITS(pdata, MAC_RCR, DCRCC, 0);
  2090. XGMAC_IOWRITE_BITS(pdata, MAC_RCR, CST, 0);
  2091. XGMAC_IOWRITE_BITS(pdata, MAC_RCR, ACS, 0);
  2092. XGMAC_IOWRITE_BITS(pdata, MAC_RCR, RE, 0);
  2093. /* Disable each Rx queue */
  2094. XGMAC_IOWRITE(pdata, MAC_RQC0R, 0);
  2095. /* Disable each Rx DMA channel */
  2096. channel = pdata->channel;
  2097. for (i = 0; i < pdata->channel_count; i++, channel++) {
  2098. if (!channel->rx_ring)
  2099. break;
  2100. XGMAC_DMA_IOWRITE_BITS(channel, DMA_CH_RCR, SR, 0);
  2101. }
  2102. }
  2103. static void xgbe_powerup_tx(struct xgbe_prv_data *pdata)
  2104. {
  2105. struct xgbe_channel *channel;
  2106. unsigned int i;
  2107. /* Enable each Tx DMA channel */
  2108. channel = pdata->channel;
  2109. for (i = 0; i < pdata->channel_count; i++, channel++) {
  2110. if (!channel->tx_ring)
  2111. break;
  2112. XGMAC_DMA_IOWRITE_BITS(channel, DMA_CH_TCR, ST, 1);
  2113. }
  2114. /* Enable MAC Tx */
  2115. XGMAC_IOWRITE_BITS(pdata, MAC_TCR, TE, 1);
  2116. }
  2117. static void xgbe_powerdown_tx(struct xgbe_prv_data *pdata)
  2118. {
  2119. struct xgbe_channel *channel;
  2120. unsigned int i;
  2121. /* Prepare for Tx DMA channel stop */
  2122. channel = pdata->channel;
  2123. for (i = 0; i < pdata->channel_count; i++, channel++) {
  2124. if (!channel->tx_ring)
  2125. break;
  2126. xgbe_prepare_tx_stop(pdata, channel);
  2127. }
  2128. /* Disable MAC Tx */
  2129. XGMAC_IOWRITE_BITS(pdata, MAC_TCR, TE, 0);
  2130. /* Disable each Tx DMA channel */
  2131. channel = pdata->channel;
  2132. for (i = 0; i < pdata->channel_count; i++, channel++) {
  2133. if (!channel->tx_ring)
  2134. break;
  2135. XGMAC_DMA_IOWRITE_BITS(channel, DMA_CH_TCR, ST, 0);
  2136. }
  2137. }
  2138. static void xgbe_powerup_rx(struct xgbe_prv_data *pdata)
  2139. {
  2140. struct xgbe_channel *channel;
  2141. unsigned int i;
  2142. /* Enable each Rx DMA channel */
  2143. channel = pdata->channel;
  2144. for (i = 0; i < pdata->channel_count; i++, channel++) {
  2145. if (!channel->rx_ring)
  2146. break;
  2147. XGMAC_DMA_IOWRITE_BITS(channel, DMA_CH_RCR, SR, 1);
  2148. }
  2149. }
  2150. static void xgbe_powerdown_rx(struct xgbe_prv_data *pdata)
  2151. {
  2152. struct xgbe_channel *channel;
  2153. unsigned int i;
  2154. /* Disable each Rx DMA channel */
  2155. channel = pdata->channel;
  2156. for (i = 0; i < pdata->channel_count; i++, channel++) {
  2157. if (!channel->rx_ring)
  2158. break;
  2159. XGMAC_DMA_IOWRITE_BITS(channel, DMA_CH_RCR, SR, 0);
  2160. }
  2161. }
  2162. static int xgbe_init(struct xgbe_prv_data *pdata)
  2163. {
  2164. struct xgbe_desc_if *desc_if = &pdata->desc_if;
  2165. int ret;
  2166. DBGPR("-->xgbe_init\n");
  2167. /* Flush Tx queues */
  2168. ret = xgbe_flush_tx_queues(pdata);
  2169. if (ret)
  2170. return ret;
  2171. /*
  2172. * Initialize DMA related features
  2173. */
  2174. xgbe_config_dma_bus(pdata);
  2175. xgbe_config_dma_cache(pdata);
  2176. xgbe_config_osp_mode(pdata);
  2177. xgbe_config_pblx8(pdata);
  2178. xgbe_config_tx_pbl_val(pdata);
  2179. xgbe_config_rx_pbl_val(pdata);
  2180. xgbe_config_rx_coalesce(pdata);
  2181. xgbe_config_tx_coalesce(pdata);
  2182. xgbe_config_rx_buffer_size(pdata);
  2183. xgbe_config_tso_mode(pdata);
  2184. xgbe_config_sph_mode(pdata);
  2185. xgbe_config_rss(pdata);
  2186. desc_if->wrapper_tx_desc_init(pdata);
  2187. desc_if->wrapper_rx_desc_init(pdata);
  2188. xgbe_enable_dma_interrupts(pdata);
  2189. /*
  2190. * Initialize MTL related features
  2191. */
  2192. xgbe_config_mtl_mode(pdata);
  2193. xgbe_config_queue_mapping(pdata);
  2194. xgbe_config_tsf_mode(pdata, pdata->tx_sf_mode);
  2195. xgbe_config_rsf_mode(pdata, pdata->rx_sf_mode);
  2196. xgbe_config_tx_threshold(pdata, pdata->tx_threshold);
  2197. xgbe_config_rx_threshold(pdata, pdata->rx_threshold);
  2198. xgbe_config_tx_fifo_size(pdata);
  2199. xgbe_config_rx_fifo_size(pdata);
  2200. xgbe_config_flow_control_threshold(pdata);
  2201. /*TODO: Error Packet and undersized good Packet forwarding enable
  2202. (FEP and FUP)
  2203. */
  2204. xgbe_config_dcb_tc(pdata);
  2205. xgbe_config_dcb_pfc(pdata);
  2206. xgbe_enable_mtl_interrupts(pdata);
  2207. /*
  2208. * Initialize MAC related features
  2209. */
  2210. xgbe_config_mac_address(pdata);
  2211. xgbe_config_rx_mode(pdata);
  2212. xgbe_config_jumbo_enable(pdata);
  2213. xgbe_config_flow_control(pdata);
  2214. xgbe_config_mac_speed(pdata);
  2215. xgbe_config_checksum_offload(pdata);
  2216. xgbe_config_vlan_support(pdata);
  2217. xgbe_config_mmc(pdata);
  2218. xgbe_enable_mac_interrupts(pdata);
  2219. DBGPR("<--xgbe_init\n");
  2220. return 0;
  2221. }
  2222. void xgbe_init_function_ptrs_dev(struct xgbe_hw_if *hw_if)
  2223. {
  2224. DBGPR("-->xgbe_init_function_ptrs\n");
  2225. hw_if->tx_complete = xgbe_tx_complete;
  2226. hw_if->set_mac_address = xgbe_set_mac_address;
  2227. hw_if->config_rx_mode = xgbe_config_rx_mode;
  2228. hw_if->enable_rx_csum = xgbe_enable_rx_csum;
  2229. hw_if->disable_rx_csum = xgbe_disable_rx_csum;
  2230. hw_if->enable_rx_vlan_stripping = xgbe_enable_rx_vlan_stripping;
  2231. hw_if->disable_rx_vlan_stripping = xgbe_disable_rx_vlan_stripping;
  2232. hw_if->enable_rx_vlan_filtering = xgbe_enable_rx_vlan_filtering;
  2233. hw_if->disable_rx_vlan_filtering = xgbe_disable_rx_vlan_filtering;
  2234. hw_if->update_vlan_hash_table = xgbe_update_vlan_hash_table;
  2235. hw_if->read_mmd_regs = xgbe_read_mmd_regs;
  2236. hw_if->write_mmd_regs = xgbe_write_mmd_regs;
  2237. hw_if->set_gmii_speed = xgbe_set_gmii_speed;
  2238. hw_if->set_gmii_2500_speed = xgbe_set_gmii_2500_speed;
  2239. hw_if->set_xgmii_speed = xgbe_set_xgmii_speed;
  2240. hw_if->enable_tx = xgbe_enable_tx;
  2241. hw_if->disable_tx = xgbe_disable_tx;
  2242. hw_if->enable_rx = xgbe_enable_rx;
  2243. hw_if->disable_rx = xgbe_disable_rx;
  2244. hw_if->powerup_tx = xgbe_powerup_tx;
  2245. hw_if->powerdown_tx = xgbe_powerdown_tx;
  2246. hw_if->powerup_rx = xgbe_powerup_rx;
  2247. hw_if->powerdown_rx = xgbe_powerdown_rx;
  2248. hw_if->dev_xmit = xgbe_dev_xmit;
  2249. hw_if->dev_read = xgbe_dev_read;
  2250. hw_if->enable_int = xgbe_enable_int;
  2251. hw_if->disable_int = xgbe_disable_int;
  2252. hw_if->init = xgbe_init;
  2253. hw_if->exit = xgbe_exit;
  2254. /* Descriptor related Sequences have to be initialized here */
  2255. hw_if->tx_desc_init = xgbe_tx_desc_init;
  2256. hw_if->rx_desc_init = xgbe_rx_desc_init;
  2257. hw_if->tx_desc_reset = xgbe_tx_desc_reset;
  2258. hw_if->rx_desc_reset = xgbe_rx_desc_reset;
  2259. hw_if->is_last_desc = xgbe_is_last_desc;
  2260. hw_if->is_context_desc = xgbe_is_context_desc;
  2261. hw_if->tx_start_xmit = xgbe_tx_start_xmit;
  2262. /* For FLOW ctrl */
  2263. hw_if->config_tx_flow_control = xgbe_config_tx_flow_control;
  2264. hw_if->config_rx_flow_control = xgbe_config_rx_flow_control;
  2265. /* For RX coalescing */
  2266. hw_if->config_rx_coalesce = xgbe_config_rx_coalesce;
  2267. hw_if->config_tx_coalesce = xgbe_config_tx_coalesce;
  2268. hw_if->usec_to_riwt = xgbe_usec_to_riwt;
  2269. hw_if->riwt_to_usec = xgbe_riwt_to_usec;
  2270. /* For RX and TX threshold config */
  2271. hw_if->config_rx_threshold = xgbe_config_rx_threshold;
  2272. hw_if->config_tx_threshold = xgbe_config_tx_threshold;
  2273. /* For RX and TX Store and Forward Mode config */
  2274. hw_if->config_rsf_mode = xgbe_config_rsf_mode;
  2275. hw_if->config_tsf_mode = xgbe_config_tsf_mode;
  2276. /* For TX DMA Operating on Second Frame config */
  2277. hw_if->config_osp_mode = xgbe_config_osp_mode;
  2278. /* For RX and TX PBL config */
  2279. hw_if->config_rx_pbl_val = xgbe_config_rx_pbl_val;
  2280. hw_if->get_rx_pbl_val = xgbe_get_rx_pbl_val;
  2281. hw_if->config_tx_pbl_val = xgbe_config_tx_pbl_val;
  2282. hw_if->get_tx_pbl_val = xgbe_get_tx_pbl_val;
  2283. hw_if->config_pblx8 = xgbe_config_pblx8;
  2284. /* For MMC statistics support */
  2285. hw_if->tx_mmc_int = xgbe_tx_mmc_int;
  2286. hw_if->rx_mmc_int = xgbe_rx_mmc_int;
  2287. hw_if->read_mmc_stats = xgbe_read_mmc_stats;
  2288. /* For PTP config */
  2289. hw_if->config_tstamp = xgbe_config_tstamp;
  2290. hw_if->update_tstamp_addend = xgbe_update_tstamp_addend;
  2291. hw_if->set_tstamp_time = xgbe_set_tstamp_time;
  2292. hw_if->get_tstamp_time = xgbe_get_tstamp_time;
  2293. hw_if->get_tx_tstamp = xgbe_get_tx_tstamp;
  2294. /* For Data Center Bridging config */
  2295. hw_if->config_dcb_tc = xgbe_config_dcb_tc;
  2296. hw_if->config_dcb_pfc = xgbe_config_dcb_pfc;
  2297. /* For Receive Side Scaling */
  2298. hw_if->enable_rss = xgbe_enable_rss;
  2299. hw_if->disable_rss = xgbe_disable_rss;
  2300. hw_if->set_rss_hash_key = xgbe_set_rss_hash_key;
  2301. hw_if->set_rss_lookup_table = xgbe_set_rss_lookup_table;
  2302. DBGPR("<--xgbe_init_function_ptrs\n");
  2303. }