xgbe-dev.c 80 KB

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