xgbe-dev.c 80 KB

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