xgbe-dev.c 79 KB

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