xgbe-dev.c 81 KB

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