xgbe-dev.c 80 KB

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