am65-cpsw-nuss.c 78 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Texas Instruments K3 AM65 Ethernet Switch SubSystem Driver
  3. *
  4. * Copyright (C) 2020 Texas Instruments Incorporated - http://www.ti.com/
  5. *
  6. */
  7. #include <linux/clk.h>
  8. #include <linux/etherdevice.h>
  9. #include <linux/if_vlan.h>
  10. #include <linux/interrupt.h>
  11. #include <linux/irqdomain.h>
  12. #include <linux/kernel.h>
  13. #include <linux/kmemleak.h>
  14. #include <linux/module.h>
  15. #include <linux/netdevice.h>
  16. #include <linux/net_tstamp.h>
  17. #include <linux/of.h>
  18. #include <linux/of_mdio.h>
  19. #include <linux/of_net.h>
  20. #include <linux/of_device.h>
  21. #include <linux/phylink.h>
  22. #include <linux/phy/phy.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/pm_runtime.h>
  25. #include <linux/regmap.h>
  26. #include <linux/mfd/syscon.h>
  27. #include <linux/sys_soc.h>
  28. #include <linux/dma/ti-cppi5.h>
  29. #include <linux/dma/k3-udma-glue.h>
  30. #include <net/switchdev.h>
  31. #include "cpsw_ale.h"
  32. #include "cpsw_sl.h"
  33. #include "am65-cpsw-nuss.h"
  34. #include "am65-cpsw-switchdev.h"
  35. #include "k3-cppi-desc-pool.h"
  36. #include "am65-cpts.h"
  37. #define AM65_CPSW_SS_BASE 0x0
  38. #define AM65_CPSW_SGMII_BASE 0x100
  39. #define AM65_CPSW_XGMII_BASE 0x2100
  40. #define AM65_CPSW_CPSW_NU_BASE 0x20000
  41. #define AM65_CPSW_NU_PORTS_BASE 0x1000
  42. #define AM65_CPSW_NU_FRAM_BASE 0x12000
  43. #define AM65_CPSW_NU_STATS_BASE 0x1a000
  44. #define AM65_CPSW_NU_ALE_BASE 0x1e000
  45. #define AM65_CPSW_NU_CPTS_BASE 0x1d000
  46. #define AM65_CPSW_NU_PORTS_OFFSET 0x1000
  47. #define AM65_CPSW_NU_STATS_PORT_OFFSET 0x200
  48. #define AM65_CPSW_NU_FRAM_PORT_OFFSET 0x200
  49. #define AM65_CPSW_MAX_PORTS 8
  50. #define AM65_CPSW_MIN_PACKET_SIZE VLAN_ETH_ZLEN
  51. #define AM65_CPSW_MAX_PACKET_SIZE (VLAN_ETH_FRAME_LEN + ETH_FCS_LEN)
  52. #define AM65_CPSW_REG_CTL 0x004
  53. #define AM65_CPSW_REG_STAT_PORT_EN 0x014
  54. #define AM65_CPSW_REG_PTYPE 0x018
  55. #define AM65_CPSW_P0_REG_CTL 0x004
  56. #define AM65_CPSW_PORT0_REG_FLOW_ID_OFFSET 0x008
  57. #define AM65_CPSW_PORT_REG_PRI_CTL 0x01c
  58. #define AM65_CPSW_PORT_REG_RX_PRI_MAP 0x020
  59. #define AM65_CPSW_PORT_REG_RX_MAXLEN 0x024
  60. #define AM65_CPSW_PORTN_REG_SA_L 0x308
  61. #define AM65_CPSW_PORTN_REG_SA_H 0x30c
  62. #define AM65_CPSW_PORTN_REG_TS_CTL 0x310
  63. #define AM65_CPSW_PORTN_REG_TS_SEQ_LTYPE_REG 0x314
  64. #define AM65_CPSW_PORTN_REG_TS_VLAN_LTYPE_REG 0x318
  65. #define AM65_CPSW_PORTN_REG_TS_CTL_LTYPE2 0x31C
  66. #define AM65_CPSW_SGMII_CONTROL_REG 0x010
  67. #define AM65_CPSW_SGMII_CONTROL_MR_AN_ENABLE BIT(0)
  68. #define AM65_CPSW_CTL_VLAN_AWARE BIT(1)
  69. #define AM65_CPSW_CTL_P0_ENABLE BIT(2)
  70. #define AM65_CPSW_CTL_P0_TX_CRC_REMOVE BIT(13)
  71. #define AM65_CPSW_CTL_P0_RX_PAD BIT(14)
  72. /* AM65_CPSW_P0_REG_CTL */
  73. #define AM65_CPSW_P0_REG_CTL_RX_CHECKSUM_EN BIT(0)
  74. /* AM65_CPSW_PORT_REG_PRI_CTL */
  75. #define AM65_CPSW_PORT_REG_PRI_CTL_RX_PTYPE_RROBIN BIT(8)
  76. /* AM65_CPSW_PN_TS_CTL register fields */
  77. #define AM65_CPSW_PN_TS_CTL_TX_ANX_F_EN BIT(4)
  78. #define AM65_CPSW_PN_TS_CTL_TX_VLAN_LT1_EN BIT(5)
  79. #define AM65_CPSW_PN_TS_CTL_TX_VLAN_LT2_EN BIT(6)
  80. #define AM65_CPSW_PN_TS_CTL_TX_ANX_D_EN BIT(7)
  81. #define AM65_CPSW_PN_TS_CTL_TX_ANX_E_EN BIT(10)
  82. #define AM65_CPSW_PN_TS_CTL_TX_HOST_TS_EN BIT(11)
  83. #define AM65_CPSW_PN_TS_CTL_MSG_TYPE_EN_SHIFT 16
  84. /* AM65_CPSW_PORTN_REG_TS_SEQ_LTYPE_REG register fields */
  85. #define AM65_CPSW_PN_TS_SEQ_ID_OFFSET_SHIFT 16
  86. /* AM65_CPSW_PORTN_REG_TS_CTL_LTYPE2 */
  87. #define AM65_CPSW_PN_TS_CTL_LTYPE2_TS_107 BIT(16)
  88. #define AM65_CPSW_PN_TS_CTL_LTYPE2_TS_129 BIT(17)
  89. #define AM65_CPSW_PN_TS_CTL_LTYPE2_TS_130 BIT(18)
  90. #define AM65_CPSW_PN_TS_CTL_LTYPE2_TS_131 BIT(19)
  91. #define AM65_CPSW_PN_TS_CTL_LTYPE2_TS_132 BIT(20)
  92. #define AM65_CPSW_PN_TS_CTL_LTYPE2_TS_319 BIT(21)
  93. #define AM65_CPSW_PN_TS_CTL_LTYPE2_TS_320 BIT(22)
  94. #define AM65_CPSW_PN_TS_CTL_LTYPE2_TS_TTL_NONZERO BIT(23)
  95. /* The PTP event messages - Sync, Delay_Req, Pdelay_Req, and Pdelay_Resp. */
  96. #define AM65_CPSW_TS_EVENT_MSG_TYPE_BITS (BIT(0) | BIT(1) | BIT(2) | BIT(3))
  97. #define AM65_CPSW_TS_SEQ_ID_OFFSET (0x1e)
  98. #define AM65_CPSW_TS_TX_ANX_ALL_EN \
  99. (AM65_CPSW_PN_TS_CTL_TX_ANX_D_EN | \
  100. AM65_CPSW_PN_TS_CTL_TX_ANX_E_EN | \
  101. AM65_CPSW_PN_TS_CTL_TX_ANX_F_EN)
  102. #define AM65_CPSW_ALE_AGEOUT_DEFAULT 30
  103. /* Number of TX/RX descriptors */
  104. #define AM65_CPSW_MAX_TX_DESC 500
  105. #define AM65_CPSW_MAX_RX_DESC 500
  106. #define AM65_CPSW_NAV_PS_DATA_SIZE 16
  107. #define AM65_CPSW_NAV_SW_DATA_SIZE 16
  108. #define AM65_CPSW_DEBUG (NETIF_MSG_HW | NETIF_MSG_DRV | NETIF_MSG_LINK | \
  109. NETIF_MSG_IFUP | NETIF_MSG_PROBE | NETIF_MSG_IFDOWN | \
  110. NETIF_MSG_RX_ERR | NETIF_MSG_TX_ERR)
  111. static void am65_cpsw_port_set_sl_mac(struct am65_cpsw_port *slave,
  112. const u8 *dev_addr)
  113. {
  114. u32 mac_hi = (dev_addr[0] << 0) | (dev_addr[1] << 8) |
  115. (dev_addr[2] << 16) | (dev_addr[3] << 24);
  116. u32 mac_lo = (dev_addr[4] << 0) | (dev_addr[5] << 8);
  117. writel(mac_hi, slave->port_base + AM65_CPSW_PORTN_REG_SA_H);
  118. writel(mac_lo, slave->port_base + AM65_CPSW_PORTN_REG_SA_L);
  119. }
  120. static void am65_cpsw_sl_ctl_reset(struct am65_cpsw_port *port)
  121. {
  122. cpsw_sl_reset(port->slave.mac_sl, 100);
  123. /* Max length register has to be restored after MAC SL reset */
  124. writel(AM65_CPSW_MAX_PACKET_SIZE,
  125. port->port_base + AM65_CPSW_PORT_REG_RX_MAXLEN);
  126. }
  127. static void am65_cpsw_nuss_get_ver(struct am65_cpsw_common *common)
  128. {
  129. common->nuss_ver = readl(common->ss_base);
  130. common->cpsw_ver = readl(common->cpsw_base);
  131. dev_info(common->dev,
  132. "initializing am65 cpsw nuss version 0x%08X, cpsw version 0x%08X Ports: %u quirks:%08x\n",
  133. common->nuss_ver,
  134. common->cpsw_ver,
  135. common->port_num + 1,
  136. common->pdata.quirks);
  137. }
  138. static int am65_cpsw_nuss_ndo_slave_add_vid(struct net_device *ndev,
  139. __be16 proto, u16 vid)
  140. {
  141. struct am65_cpsw_common *common = am65_ndev_to_common(ndev);
  142. struct am65_cpsw_port *port = am65_ndev_to_port(ndev);
  143. u32 port_mask, unreg_mcast = 0;
  144. int ret;
  145. if (!common->is_emac_mode)
  146. return 0;
  147. if (!netif_running(ndev) || !vid)
  148. return 0;
  149. ret = pm_runtime_resume_and_get(common->dev);
  150. if (ret < 0)
  151. return ret;
  152. port_mask = BIT(port->port_id) | ALE_PORT_HOST;
  153. if (!vid)
  154. unreg_mcast = port_mask;
  155. dev_info(common->dev, "Adding vlan %d to vlan filter\n", vid);
  156. ret = cpsw_ale_vlan_add_modify(common->ale, vid, port_mask,
  157. unreg_mcast, port_mask, 0);
  158. pm_runtime_put(common->dev);
  159. return ret;
  160. }
  161. static int am65_cpsw_nuss_ndo_slave_kill_vid(struct net_device *ndev,
  162. __be16 proto, u16 vid)
  163. {
  164. struct am65_cpsw_common *common = am65_ndev_to_common(ndev);
  165. struct am65_cpsw_port *port = am65_ndev_to_port(ndev);
  166. int ret;
  167. if (!common->is_emac_mode)
  168. return 0;
  169. if (!netif_running(ndev) || !vid)
  170. return 0;
  171. ret = pm_runtime_resume_and_get(common->dev);
  172. if (ret < 0)
  173. return ret;
  174. dev_info(common->dev, "Removing vlan %d from vlan filter\n", vid);
  175. ret = cpsw_ale_del_vlan(common->ale, vid,
  176. BIT(port->port_id) | ALE_PORT_HOST);
  177. pm_runtime_put(common->dev);
  178. return ret;
  179. }
  180. static void am65_cpsw_slave_set_promisc(struct am65_cpsw_port *port,
  181. bool promisc)
  182. {
  183. struct am65_cpsw_common *common = port->common;
  184. if (promisc && !common->is_emac_mode) {
  185. dev_dbg(common->dev, "promisc mode requested in switch mode");
  186. return;
  187. }
  188. if (promisc) {
  189. /* Enable promiscuous mode */
  190. cpsw_ale_control_set(common->ale, port->port_id,
  191. ALE_PORT_MACONLY_CAF, 1);
  192. dev_dbg(common->dev, "promisc enabled\n");
  193. } else {
  194. /* Disable promiscuous mode */
  195. cpsw_ale_control_set(common->ale, port->port_id,
  196. ALE_PORT_MACONLY_CAF, 0);
  197. dev_dbg(common->dev, "promisc disabled\n");
  198. }
  199. }
  200. static void am65_cpsw_nuss_ndo_slave_set_rx_mode(struct net_device *ndev)
  201. {
  202. struct am65_cpsw_common *common = am65_ndev_to_common(ndev);
  203. struct am65_cpsw_port *port = am65_ndev_to_port(ndev);
  204. u32 port_mask;
  205. bool promisc;
  206. promisc = !!(ndev->flags & IFF_PROMISC);
  207. am65_cpsw_slave_set_promisc(port, promisc);
  208. if (promisc)
  209. return;
  210. /* Restore allmulti on vlans if necessary */
  211. cpsw_ale_set_allmulti(common->ale,
  212. ndev->flags & IFF_ALLMULTI, port->port_id);
  213. port_mask = ALE_PORT_HOST;
  214. /* Clear all mcast from ALE */
  215. cpsw_ale_flush_multicast(common->ale, port_mask, -1);
  216. if (!netdev_mc_empty(ndev)) {
  217. struct netdev_hw_addr *ha;
  218. /* program multicast address list into ALE register */
  219. netdev_for_each_mc_addr(ha, ndev) {
  220. cpsw_ale_add_mcast(common->ale, ha->addr,
  221. port_mask, 0, 0, 0);
  222. }
  223. }
  224. }
  225. static void am65_cpsw_nuss_ndo_host_tx_timeout(struct net_device *ndev,
  226. unsigned int txqueue)
  227. {
  228. struct am65_cpsw_common *common = am65_ndev_to_common(ndev);
  229. struct am65_cpsw_tx_chn *tx_chn;
  230. struct netdev_queue *netif_txq;
  231. unsigned long trans_start;
  232. netif_txq = netdev_get_tx_queue(ndev, txqueue);
  233. tx_chn = &common->tx_chns[txqueue];
  234. trans_start = READ_ONCE(netif_txq->trans_start);
  235. netdev_err(ndev, "txq:%d DRV_XOFF:%d tmo:%u dql_avail:%d free_desc:%zu\n",
  236. txqueue,
  237. netif_tx_queue_stopped(netif_txq),
  238. jiffies_to_msecs(jiffies - trans_start),
  239. dql_avail(&netif_txq->dql),
  240. k3_cppi_desc_pool_avail(tx_chn->desc_pool));
  241. if (netif_tx_queue_stopped(netif_txq)) {
  242. /* try recover if stopped by us */
  243. txq_trans_update(netif_txq);
  244. netif_tx_wake_queue(netif_txq);
  245. }
  246. }
  247. static int am65_cpsw_nuss_rx_push(struct am65_cpsw_common *common,
  248. struct sk_buff *skb)
  249. {
  250. struct am65_cpsw_rx_chn *rx_chn = &common->rx_chns;
  251. struct cppi5_host_desc_t *desc_rx;
  252. struct device *dev = common->dev;
  253. u32 pkt_len = skb_tailroom(skb);
  254. dma_addr_t desc_dma;
  255. dma_addr_t buf_dma;
  256. void *swdata;
  257. desc_rx = k3_cppi_desc_pool_alloc(rx_chn->desc_pool);
  258. if (!desc_rx) {
  259. dev_err(dev, "Failed to allocate RXFDQ descriptor\n");
  260. return -ENOMEM;
  261. }
  262. desc_dma = k3_cppi_desc_pool_virt2dma(rx_chn->desc_pool, desc_rx);
  263. buf_dma = dma_map_single(rx_chn->dma_dev, skb->data, pkt_len,
  264. DMA_FROM_DEVICE);
  265. if (unlikely(dma_mapping_error(rx_chn->dma_dev, buf_dma))) {
  266. k3_cppi_desc_pool_free(rx_chn->desc_pool, desc_rx);
  267. dev_err(dev, "Failed to map rx skb buffer\n");
  268. return -EINVAL;
  269. }
  270. cppi5_hdesc_init(desc_rx, CPPI5_INFO0_HDESC_EPIB_PRESENT,
  271. AM65_CPSW_NAV_PS_DATA_SIZE);
  272. k3_udma_glue_rx_dma_to_cppi5_addr(rx_chn->rx_chn, &buf_dma);
  273. cppi5_hdesc_attach_buf(desc_rx, buf_dma, skb_tailroom(skb), buf_dma, skb_tailroom(skb));
  274. swdata = cppi5_hdesc_get_swdata(desc_rx);
  275. *((void **)swdata) = skb;
  276. return k3_udma_glue_push_rx_chn(rx_chn->rx_chn, 0, desc_rx, desc_dma);
  277. }
  278. void am65_cpsw_nuss_set_p0_ptype(struct am65_cpsw_common *common)
  279. {
  280. struct am65_cpsw_host *host_p = am65_common_get_host(common);
  281. u32 val, pri_map;
  282. /* P0 set Receive Priority Type */
  283. val = readl(host_p->port_base + AM65_CPSW_PORT_REG_PRI_CTL);
  284. if (common->pf_p0_rx_ptype_rrobin) {
  285. val |= AM65_CPSW_PORT_REG_PRI_CTL_RX_PTYPE_RROBIN;
  286. /* Enet Ports fifos works in fixed priority mode only, so
  287. * reset P0_Rx_Pri_Map so all packet will go in Enet fifo 0
  288. */
  289. pri_map = 0x0;
  290. } else {
  291. val &= ~AM65_CPSW_PORT_REG_PRI_CTL_RX_PTYPE_RROBIN;
  292. /* restore P0_Rx_Pri_Map */
  293. pri_map = 0x76543210;
  294. }
  295. writel(pri_map, host_p->port_base + AM65_CPSW_PORT_REG_RX_PRI_MAP);
  296. writel(val, host_p->port_base + AM65_CPSW_PORT_REG_PRI_CTL);
  297. }
  298. static void am65_cpsw_init_host_port_switch(struct am65_cpsw_common *common);
  299. static void am65_cpsw_init_host_port_emac(struct am65_cpsw_common *common);
  300. static void am65_cpsw_init_port_switch_ale(struct am65_cpsw_port *port);
  301. static void am65_cpsw_init_port_emac_ale(struct am65_cpsw_port *port);
  302. static int am65_cpsw_nuss_common_open(struct am65_cpsw_common *common)
  303. {
  304. struct am65_cpsw_host *host_p = am65_common_get_host(common);
  305. int port_idx, i, ret;
  306. struct sk_buff *skb;
  307. u32 val, port_mask;
  308. if (common->usage_count)
  309. return 0;
  310. /* Control register */
  311. writel(AM65_CPSW_CTL_P0_ENABLE | AM65_CPSW_CTL_P0_TX_CRC_REMOVE |
  312. AM65_CPSW_CTL_VLAN_AWARE | AM65_CPSW_CTL_P0_RX_PAD,
  313. common->cpsw_base + AM65_CPSW_REG_CTL);
  314. /* Max length register */
  315. writel(AM65_CPSW_MAX_PACKET_SIZE,
  316. host_p->port_base + AM65_CPSW_PORT_REG_RX_MAXLEN);
  317. /* set base flow_id */
  318. writel(common->rx_flow_id_base,
  319. host_p->port_base + AM65_CPSW_PORT0_REG_FLOW_ID_OFFSET);
  320. /* en tx crc offload */
  321. writel(AM65_CPSW_P0_REG_CTL_RX_CHECKSUM_EN, host_p->port_base + AM65_CPSW_P0_REG_CTL);
  322. am65_cpsw_nuss_set_p0_ptype(common);
  323. /* enable statistic */
  324. val = BIT(HOST_PORT_NUM);
  325. for (port_idx = 0; port_idx < common->port_num; port_idx++) {
  326. struct am65_cpsw_port *port = &common->ports[port_idx];
  327. if (!port->disabled)
  328. val |= BIT(port->port_id);
  329. }
  330. writel(val, common->cpsw_base + AM65_CPSW_REG_STAT_PORT_EN);
  331. /* disable priority elevation */
  332. writel(0, common->cpsw_base + AM65_CPSW_REG_PTYPE);
  333. cpsw_ale_start(common->ale);
  334. /* limit to one RX flow only */
  335. cpsw_ale_control_set(common->ale, HOST_PORT_NUM,
  336. ALE_DEFAULT_THREAD_ID, 0);
  337. cpsw_ale_control_set(common->ale, HOST_PORT_NUM,
  338. ALE_DEFAULT_THREAD_ENABLE, 1);
  339. /* switch to vlan unaware mode */
  340. cpsw_ale_control_set(common->ale, HOST_PORT_NUM, ALE_VLAN_AWARE, 1);
  341. cpsw_ale_control_set(common->ale, HOST_PORT_NUM,
  342. ALE_PORT_STATE, ALE_PORT_STATE_FORWARD);
  343. /* default vlan cfg: create mask based on enabled ports */
  344. port_mask = GENMASK(common->port_num, 0) &
  345. ~common->disabled_ports_mask;
  346. cpsw_ale_add_vlan(common->ale, 0, port_mask,
  347. port_mask, port_mask,
  348. port_mask & ~ALE_PORT_HOST);
  349. if (common->is_emac_mode)
  350. am65_cpsw_init_host_port_emac(common);
  351. else
  352. am65_cpsw_init_host_port_switch(common);
  353. for (i = 0; i < common->rx_chns.descs_num; i++) {
  354. skb = __netdev_alloc_skb_ip_align(NULL,
  355. AM65_CPSW_MAX_PACKET_SIZE,
  356. GFP_KERNEL);
  357. if (!skb) {
  358. dev_err(common->dev, "cannot allocate skb\n");
  359. return -ENOMEM;
  360. }
  361. ret = am65_cpsw_nuss_rx_push(common, skb);
  362. if (ret < 0) {
  363. dev_err(common->dev,
  364. "cannot submit skb to channel rx, error %d\n",
  365. ret);
  366. kfree_skb(skb);
  367. return ret;
  368. }
  369. kmemleak_not_leak(skb);
  370. }
  371. k3_udma_glue_enable_rx_chn(common->rx_chns.rx_chn);
  372. for (i = 0; i < common->tx_ch_num; i++) {
  373. ret = k3_udma_glue_enable_tx_chn(common->tx_chns[i].tx_chn);
  374. if (ret)
  375. return ret;
  376. napi_enable(&common->tx_chns[i].napi_tx);
  377. }
  378. napi_enable(&common->napi_rx);
  379. if (common->rx_irq_disabled) {
  380. common->rx_irq_disabled = false;
  381. enable_irq(common->rx_chns.irq);
  382. }
  383. dev_dbg(common->dev, "cpsw_nuss started\n");
  384. return 0;
  385. }
  386. static void am65_cpsw_nuss_tx_cleanup(void *data, dma_addr_t desc_dma);
  387. static void am65_cpsw_nuss_rx_cleanup(void *data, dma_addr_t desc_dma);
  388. static int am65_cpsw_nuss_common_stop(struct am65_cpsw_common *common)
  389. {
  390. int i;
  391. if (common->usage_count != 1)
  392. return 0;
  393. cpsw_ale_control_set(common->ale, HOST_PORT_NUM,
  394. ALE_PORT_STATE, ALE_PORT_STATE_DISABLE);
  395. /* shutdown tx channels */
  396. atomic_set(&common->tdown_cnt, common->tx_ch_num);
  397. /* ensure new tdown_cnt value is visible */
  398. smp_mb__after_atomic();
  399. reinit_completion(&common->tdown_complete);
  400. for (i = 0; i < common->tx_ch_num; i++)
  401. k3_udma_glue_tdown_tx_chn(common->tx_chns[i].tx_chn, false);
  402. i = wait_for_completion_timeout(&common->tdown_complete,
  403. msecs_to_jiffies(1000));
  404. if (!i)
  405. dev_err(common->dev, "tx timeout\n");
  406. for (i = 0; i < common->tx_ch_num; i++)
  407. napi_disable(&common->tx_chns[i].napi_tx);
  408. for (i = 0; i < common->tx_ch_num; i++) {
  409. k3_udma_glue_reset_tx_chn(common->tx_chns[i].tx_chn,
  410. &common->tx_chns[i],
  411. am65_cpsw_nuss_tx_cleanup);
  412. k3_udma_glue_disable_tx_chn(common->tx_chns[i].tx_chn);
  413. }
  414. reinit_completion(&common->tdown_complete);
  415. k3_udma_glue_tdown_rx_chn(common->rx_chns.rx_chn, true);
  416. if (common->pdata.quirks & AM64_CPSW_QUIRK_DMA_RX_TDOWN_IRQ) {
  417. i = wait_for_completion_timeout(&common->tdown_complete, msecs_to_jiffies(1000));
  418. if (!i)
  419. dev_err(common->dev, "rx teardown timeout\n");
  420. }
  421. napi_disable(&common->napi_rx);
  422. for (i = 0; i < AM65_CPSW_MAX_RX_FLOWS; i++)
  423. k3_udma_glue_reset_rx_chn(common->rx_chns.rx_chn, i,
  424. &common->rx_chns,
  425. am65_cpsw_nuss_rx_cleanup, !!i);
  426. k3_udma_glue_disable_rx_chn(common->rx_chns.rx_chn);
  427. cpsw_ale_stop(common->ale);
  428. writel(0, common->cpsw_base + AM65_CPSW_REG_CTL);
  429. writel(0, common->cpsw_base + AM65_CPSW_REG_STAT_PORT_EN);
  430. dev_dbg(common->dev, "cpsw_nuss stopped\n");
  431. return 0;
  432. }
  433. static int am65_cpsw_nuss_ndo_slave_stop(struct net_device *ndev)
  434. {
  435. struct am65_cpsw_common *common = am65_ndev_to_common(ndev);
  436. struct am65_cpsw_port *port = am65_ndev_to_port(ndev);
  437. int ret;
  438. phylink_stop(port->slave.phylink);
  439. netif_tx_stop_all_queues(ndev);
  440. phylink_disconnect_phy(port->slave.phylink);
  441. ret = am65_cpsw_nuss_common_stop(common);
  442. if (ret)
  443. return ret;
  444. common->usage_count--;
  445. pm_runtime_put(common->dev);
  446. return 0;
  447. }
  448. static int cpsw_restore_vlans(struct net_device *vdev, int vid, void *arg)
  449. {
  450. struct am65_cpsw_port *port = arg;
  451. if (!vdev)
  452. return 0;
  453. return am65_cpsw_nuss_ndo_slave_add_vid(port->ndev, 0, vid);
  454. }
  455. static int am65_cpsw_nuss_ndo_slave_open(struct net_device *ndev)
  456. {
  457. struct am65_cpsw_common *common = am65_ndev_to_common(ndev);
  458. struct am65_cpsw_port *port = am65_ndev_to_port(ndev);
  459. int ret, i;
  460. ret = pm_runtime_resume_and_get(common->dev);
  461. if (ret < 0)
  462. return ret;
  463. /* Notify the stack of the actual queue counts. */
  464. ret = netif_set_real_num_tx_queues(ndev, common->tx_ch_num);
  465. if (ret) {
  466. dev_err(common->dev, "cannot set real number of tx queues\n");
  467. goto runtime_put;
  468. }
  469. ret = netif_set_real_num_rx_queues(ndev, AM65_CPSW_MAX_RX_QUEUES);
  470. if (ret) {
  471. dev_err(common->dev, "cannot set real number of rx queues\n");
  472. goto runtime_put;
  473. }
  474. for (i = 0; i < common->tx_ch_num; i++)
  475. netdev_tx_reset_queue(netdev_get_tx_queue(ndev, i));
  476. ret = am65_cpsw_nuss_common_open(common);
  477. if (ret)
  478. goto runtime_put;
  479. common->usage_count++;
  480. am65_cpsw_port_set_sl_mac(port, ndev->dev_addr);
  481. if (common->is_emac_mode)
  482. am65_cpsw_init_port_emac_ale(port);
  483. else
  484. am65_cpsw_init_port_switch_ale(port);
  485. /* mac_sl should be configured via phy-link interface */
  486. am65_cpsw_sl_ctl_reset(port);
  487. ret = phylink_of_phy_connect(port->slave.phylink, port->slave.phy_node, 0);
  488. if (ret)
  489. goto error_cleanup;
  490. /* restore vlan configurations */
  491. vlan_for_each(ndev, cpsw_restore_vlans, port);
  492. phylink_start(port->slave.phylink);
  493. return 0;
  494. error_cleanup:
  495. am65_cpsw_nuss_ndo_slave_stop(ndev);
  496. return ret;
  497. runtime_put:
  498. pm_runtime_put(common->dev);
  499. return ret;
  500. }
  501. static void am65_cpsw_nuss_rx_cleanup(void *data, dma_addr_t desc_dma)
  502. {
  503. struct am65_cpsw_rx_chn *rx_chn = data;
  504. struct cppi5_host_desc_t *desc_rx;
  505. struct sk_buff *skb;
  506. dma_addr_t buf_dma;
  507. u32 buf_dma_len;
  508. void **swdata;
  509. desc_rx = k3_cppi_desc_pool_dma2virt(rx_chn->desc_pool, desc_dma);
  510. swdata = cppi5_hdesc_get_swdata(desc_rx);
  511. skb = *swdata;
  512. cppi5_hdesc_get_obuf(desc_rx, &buf_dma, &buf_dma_len);
  513. k3_udma_glue_rx_cppi5_to_dma_addr(rx_chn->rx_chn, &buf_dma);
  514. dma_unmap_single(rx_chn->dma_dev, buf_dma, buf_dma_len, DMA_FROM_DEVICE);
  515. k3_cppi_desc_pool_free(rx_chn->desc_pool, desc_rx);
  516. dev_kfree_skb_any(skb);
  517. }
  518. static void am65_cpsw_nuss_rx_ts(struct sk_buff *skb, u32 *psdata)
  519. {
  520. struct skb_shared_hwtstamps *ssh;
  521. u64 ns;
  522. ns = ((u64)psdata[1] << 32) | psdata[0];
  523. ssh = skb_hwtstamps(skb);
  524. memset(ssh, 0, sizeof(*ssh));
  525. ssh->hwtstamp = ns_to_ktime(ns);
  526. }
  527. /* RX psdata[2] word format - checksum information */
  528. #define AM65_CPSW_RX_PSD_CSUM_ADD GENMASK(15, 0)
  529. #define AM65_CPSW_RX_PSD_CSUM_ERR BIT(16)
  530. #define AM65_CPSW_RX_PSD_IS_FRAGMENT BIT(17)
  531. #define AM65_CPSW_RX_PSD_IS_TCP BIT(18)
  532. #define AM65_CPSW_RX_PSD_IPV6_VALID BIT(19)
  533. #define AM65_CPSW_RX_PSD_IPV4_VALID BIT(20)
  534. static void am65_cpsw_nuss_rx_csum(struct sk_buff *skb, u32 csum_info)
  535. {
  536. /* HW can verify IPv4/IPv6 TCP/UDP packets checksum
  537. * csum information provides in psdata[2] word:
  538. * AM65_CPSW_RX_PSD_CSUM_ERR bit - indicates csum error
  539. * AM65_CPSW_RX_PSD_IPV6_VALID and AM65_CPSW_RX_PSD_IPV4_VALID
  540. * bits - indicates IPv4/IPv6 packet
  541. * AM65_CPSW_RX_PSD_IS_FRAGMENT bit - indicates fragmented packet
  542. * AM65_CPSW_RX_PSD_CSUM_ADD has value 0xFFFF for non fragmented packets
  543. * or csum value for fragmented packets if !AM65_CPSW_RX_PSD_CSUM_ERR
  544. */
  545. skb_checksum_none_assert(skb);
  546. if (unlikely(!(skb->dev->features & NETIF_F_RXCSUM)))
  547. return;
  548. if ((csum_info & (AM65_CPSW_RX_PSD_IPV6_VALID |
  549. AM65_CPSW_RX_PSD_IPV4_VALID)) &&
  550. !(csum_info & AM65_CPSW_RX_PSD_CSUM_ERR)) {
  551. /* csum for fragmented packets is unsupported */
  552. if (!(csum_info & AM65_CPSW_RX_PSD_IS_FRAGMENT))
  553. skb->ip_summed = CHECKSUM_UNNECESSARY;
  554. }
  555. }
  556. static int am65_cpsw_nuss_rx_packets(struct am65_cpsw_common *common,
  557. u32 flow_idx)
  558. {
  559. struct am65_cpsw_rx_chn *rx_chn = &common->rx_chns;
  560. u32 buf_dma_len, pkt_len, port_id = 0, csum_info;
  561. struct am65_cpsw_ndev_priv *ndev_priv;
  562. struct am65_cpsw_ndev_stats *stats;
  563. struct cppi5_host_desc_t *desc_rx;
  564. struct device *dev = common->dev;
  565. struct sk_buff *skb, *new_skb;
  566. dma_addr_t desc_dma, buf_dma;
  567. struct am65_cpsw_port *port;
  568. struct net_device *ndev;
  569. void **swdata;
  570. u32 *psdata;
  571. int ret = 0;
  572. ret = k3_udma_glue_pop_rx_chn(rx_chn->rx_chn, flow_idx, &desc_dma);
  573. if (ret) {
  574. if (ret != -ENODATA)
  575. dev_err(dev, "RX: pop chn fail %d\n", ret);
  576. return ret;
  577. }
  578. if (cppi5_desc_is_tdcm(desc_dma)) {
  579. dev_dbg(dev, "%s RX tdown flow: %u\n", __func__, flow_idx);
  580. if (common->pdata.quirks & AM64_CPSW_QUIRK_DMA_RX_TDOWN_IRQ)
  581. complete(&common->tdown_complete);
  582. return 0;
  583. }
  584. desc_rx = k3_cppi_desc_pool_dma2virt(rx_chn->desc_pool, desc_dma);
  585. dev_dbg(dev, "%s flow_idx: %u desc %pad\n",
  586. __func__, flow_idx, &desc_dma);
  587. swdata = cppi5_hdesc_get_swdata(desc_rx);
  588. skb = *swdata;
  589. cppi5_hdesc_get_obuf(desc_rx, &buf_dma, &buf_dma_len);
  590. k3_udma_glue_rx_cppi5_to_dma_addr(rx_chn->rx_chn, &buf_dma);
  591. pkt_len = cppi5_hdesc_get_pktlen(desc_rx);
  592. cppi5_desc_get_tags_ids(&desc_rx->hdr, &port_id, NULL);
  593. dev_dbg(dev, "%s rx port_id:%d\n", __func__, port_id);
  594. port = am65_common_get_port(common, port_id);
  595. ndev = port->ndev;
  596. skb->dev = ndev;
  597. psdata = cppi5_hdesc_get_psdata(desc_rx);
  598. /* add RX timestamp */
  599. if (port->rx_ts_enabled)
  600. am65_cpsw_nuss_rx_ts(skb, psdata);
  601. csum_info = psdata[2];
  602. dev_dbg(dev, "%s rx csum_info:%#x\n", __func__, csum_info);
  603. dma_unmap_single(rx_chn->dma_dev, buf_dma, buf_dma_len, DMA_FROM_DEVICE);
  604. k3_cppi_desc_pool_free(rx_chn->desc_pool, desc_rx);
  605. new_skb = netdev_alloc_skb_ip_align(ndev, AM65_CPSW_MAX_PACKET_SIZE);
  606. if (new_skb) {
  607. ndev_priv = netdev_priv(ndev);
  608. am65_cpsw_nuss_set_offload_fwd_mark(skb, ndev_priv->offload_fwd_mark);
  609. skb_put(skb, pkt_len);
  610. skb->protocol = eth_type_trans(skb, ndev);
  611. am65_cpsw_nuss_rx_csum(skb, csum_info);
  612. napi_gro_receive(&common->napi_rx, skb);
  613. stats = this_cpu_ptr(ndev_priv->stats);
  614. u64_stats_update_begin(&stats->syncp);
  615. stats->rx_packets++;
  616. stats->rx_bytes += pkt_len;
  617. u64_stats_update_end(&stats->syncp);
  618. kmemleak_not_leak(new_skb);
  619. } else {
  620. ndev->stats.rx_dropped++;
  621. new_skb = skb;
  622. }
  623. if (netif_dormant(ndev)) {
  624. dev_kfree_skb_any(new_skb);
  625. ndev->stats.rx_dropped++;
  626. return 0;
  627. }
  628. ret = am65_cpsw_nuss_rx_push(common, new_skb);
  629. if (WARN_ON(ret < 0)) {
  630. dev_kfree_skb_any(new_skb);
  631. ndev->stats.rx_errors++;
  632. ndev->stats.rx_dropped++;
  633. }
  634. return ret;
  635. }
  636. static int am65_cpsw_nuss_rx_poll(struct napi_struct *napi_rx, int budget)
  637. {
  638. struct am65_cpsw_common *common = am65_cpsw_napi_to_common(napi_rx);
  639. int flow = AM65_CPSW_MAX_RX_FLOWS;
  640. int cur_budget, ret;
  641. int num_rx = 0;
  642. /* process every flow */
  643. while (flow--) {
  644. cur_budget = budget - num_rx;
  645. while (cur_budget--) {
  646. ret = am65_cpsw_nuss_rx_packets(common, flow);
  647. if (ret)
  648. break;
  649. num_rx++;
  650. }
  651. if (num_rx >= budget)
  652. break;
  653. }
  654. dev_dbg(common->dev, "%s num_rx:%d %d\n", __func__, num_rx, budget);
  655. if (num_rx < budget && napi_complete_done(napi_rx, num_rx)) {
  656. if (common->rx_irq_disabled) {
  657. common->rx_irq_disabled = false;
  658. enable_irq(common->rx_chns.irq);
  659. }
  660. }
  661. return num_rx;
  662. }
  663. static void am65_cpsw_nuss_xmit_free(struct am65_cpsw_tx_chn *tx_chn,
  664. struct cppi5_host_desc_t *desc)
  665. {
  666. struct cppi5_host_desc_t *first_desc, *next_desc;
  667. dma_addr_t buf_dma, next_desc_dma;
  668. u32 buf_dma_len;
  669. first_desc = desc;
  670. next_desc = first_desc;
  671. cppi5_hdesc_get_obuf(first_desc, &buf_dma, &buf_dma_len);
  672. k3_udma_glue_tx_cppi5_to_dma_addr(tx_chn->tx_chn, &buf_dma);
  673. dma_unmap_single(tx_chn->dma_dev, buf_dma, buf_dma_len, DMA_TO_DEVICE);
  674. next_desc_dma = cppi5_hdesc_get_next_hbdesc(first_desc);
  675. k3_udma_glue_tx_cppi5_to_dma_addr(tx_chn->tx_chn, &next_desc_dma);
  676. while (next_desc_dma) {
  677. next_desc = k3_cppi_desc_pool_dma2virt(tx_chn->desc_pool,
  678. next_desc_dma);
  679. cppi5_hdesc_get_obuf(next_desc, &buf_dma, &buf_dma_len);
  680. k3_udma_glue_tx_cppi5_to_dma_addr(tx_chn->tx_chn, &buf_dma);
  681. dma_unmap_page(tx_chn->dma_dev, buf_dma, buf_dma_len,
  682. DMA_TO_DEVICE);
  683. next_desc_dma = cppi5_hdesc_get_next_hbdesc(next_desc);
  684. k3_udma_glue_tx_cppi5_to_dma_addr(tx_chn->tx_chn, &next_desc_dma);
  685. k3_cppi_desc_pool_free(tx_chn->desc_pool, next_desc);
  686. }
  687. k3_cppi_desc_pool_free(tx_chn->desc_pool, first_desc);
  688. }
  689. static void am65_cpsw_nuss_tx_cleanup(void *data, dma_addr_t desc_dma)
  690. {
  691. struct am65_cpsw_tx_chn *tx_chn = data;
  692. struct cppi5_host_desc_t *desc_tx;
  693. struct sk_buff *skb;
  694. void **swdata;
  695. desc_tx = k3_cppi_desc_pool_dma2virt(tx_chn->desc_pool, desc_dma);
  696. swdata = cppi5_hdesc_get_swdata(desc_tx);
  697. skb = *(swdata);
  698. am65_cpsw_nuss_xmit_free(tx_chn, desc_tx);
  699. dev_kfree_skb_any(skb);
  700. }
  701. static struct sk_buff *
  702. am65_cpsw_nuss_tx_compl_packet(struct am65_cpsw_tx_chn *tx_chn,
  703. dma_addr_t desc_dma)
  704. {
  705. struct am65_cpsw_ndev_priv *ndev_priv;
  706. struct am65_cpsw_ndev_stats *stats;
  707. struct cppi5_host_desc_t *desc_tx;
  708. struct net_device *ndev;
  709. struct sk_buff *skb;
  710. void **swdata;
  711. desc_tx = k3_cppi_desc_pool_dma2virt(tx_chn->desc_pool,
  712. desc_dma);
  713. swdata = cppi5_hdesc_get_swdata(desc_tx);
  714. skb = *(swdata);
  715. am65_cpsw_nuss_xmit_free(tx_chn, desc_tx);
  716. ndev = skb->dev;
  717. am65_cpts_tx_timestamp(tx_chn->common->cpts, skb);
  718. ndev_priv = netdev_priv(ndev);
  719. stats = this_cpu_ptr(ndev_priv->stats);
  720. u64_stats_update_begin(&stats->syncp);
  721. stats->tx_packets++;
  722. stats->tx_bytes += skb->len;
  723. u64_stats_update_end(&stats->syncp);
  724. return skb;
  725. }
  726. static void am65_cpsw_nuss_tx_wake(struct am65_cpsw_tx_chn *tx_chn, struct net_device *ndev,
  727. struct netdev_queue *netif_txq)
  728. {
  729. if (netif_tx_queue_stopped(netif_txq)) {
  730. /* Check whether the queue is stopped due to stalled
  731. * tx dma, if the queue is stopped then wake the queue
  732. * as we have free desc for tx
  733. */
  734. __netif_tx_lock(netif_txq, smp_processor_id());
  735. if (netif_running(ndev) &&
  736. (k3_cppi_desc_pool_avail(tx_chn->desc_pool) >= MAX_SKB_FRAGS))
  737. netif_tx_wake_queue(netif_txq);
  738. __netif_tx_unlock(netif_txq);
  739. }
  740. }
  741. static int am65_cpsw_nuss_tx_compl_packets(struct am65_cpsw_common *common,
  742. int chn, unsigned int budget)
  743. {
  744. struct device *dev = common->dev;
  745. struct am65_cpsw_tx_chn *tx_chn;
  746. struct netdev_queue *netif_txq;
  747. unsigned int total_bytes = 0;
  748. struct net_device *ndev;
  749. struct sk_buff *skb;
  750. dma_addr_t desc_dma;
  751. int res, num_tx = 0;
  752. tx_chn = &common->tx_chns[chn];
  753. while (true) {
  754. spin_lock(&tx_chn->lock);
  755. res = k3_udma_glue_pop_tx_chn(tx_chn->tx_chn, &desc_dma);
  756. spin_unlock(&tx_chn->lock);
  757. if (res == -ENODATA)
  758. break;
  759. if (cppi5_desc_is_tdcm(desc_dma)) {
  760. if (atomic_dec_and_test(&common->tdown_cnt))
  761. complete(&common->tdown_complete);
  762. break;
  763. }
  764. skb = am65_cpsw_nuss_tx_compl_packet(tx_chn, desc_dma);
  765. total_bytes = skb->len;
  766. ndev = skb->dev;
  767. napi_consume_skb(skb, budget);
  768. num_tx++;
  769. netif_txq = netdev_get_tx_queue(ndev, chn);
  770. netdev_tx_completed_queue(netif_txq, num_tx, total_bytes);
  771. am65_cpsw_nuss_tx_wake(tx_chn, ndev, netif_txq);
  772. }
  773. dev_dbg(dev, "%s:%u pkt:%d\n", __func__, chn, num_tx);
  774. return num_tx;
  775. }
  776. static int am65_cpsw_nuss_tx_compl_packets_2g(struct am65_cpsw_common *common,
  777. int chn, unsigned int budget)
  778. {
  779. struct device *dev = common->dev;
  780. struct am65_cpsw_tx_chn *tx_chn;
  781. struct netdev_queue *netif_txq;
  782. unsigned int total_bytes = 0;
  783. struct net_device *ndev;
  784. struct sk_buff *skb;
  785. dma_addr_t desc_dma;
  786. int res, num_tx = 0;
  787. tx_chn = &common->tx_chns[chn];
  788. while (true) {
  789. res = k3_udma_glue_pop_tx_chn(tx_chn->tx_chn, &desc_dma);
  790. if (res == -ENODATA)
  791. break;
  792. if (cppi5_desc_is_tdcm(desc_dma)) {
  793. if (atomic_dec_and_test(&common->tdown_cnt))
  794. complete(&common->tdown_complete);
  795. break;
  796. }
  797. skb = am65_cpsw_nuss_tx_compl_packet(tx_chn, desc_dma);
  798. ndev = skb->dev;
  799. total_bytes += skb->len;
  800. napi_consume_skb(skb, budget);
  801. num_tx++;
  802. }
  803. if (!num_tx)
  804. return 0;
  805. netif_txq = netdev_get_tx_queue(ndev, chn);
  806. netdev_tx_completed_queue(netif_txq, num_tx, total_bytes);
  807. am65_cpsw_nuss_tx_wake(tx_chn, ndev, netif_txq);
  808. dev_dbg(dev, "%s:%u pkt:%d\n", __func__, chn, num_tx);
  809. return num_tx;
  810. }
  811. static int am65_cpsw_nuss_tx_poll(struct napi_struct *napi_tx, int budget)
  812. {
  813. struct am65_cpsw_tx_chn *tx_chn = am65_cpsw_napi_to_tx_chn(napi_tx);
  814. int num_tx;
  815. if (AM65_CPSW_IS_CPSW2G(tx_chn->common))
  816. num_tx = am65_cpsw_nuss_tx_compl_packets_2g(tx_chn->common, tx_chn->id, budget);
  817. else
  818. num_tx = am65_cpsw_nuss_tx_compl_packets(tx_chn->common, tx_chn->id, budget);
  819. if (num_tx >= budget)
  820. return budget;
  821. if (napi_complete_done(napi_tx, num_tx))
  822. enable_irq(tx_chn->irq);
  823. return 0;
  824. }
  825. static irqreturn_t am65_cpsw_nuss_rx_irq(int irq, void *dev_id)
  826. {
  827. struct am65_cpsw_common *common = dev_id;
  828. common->rx_irq_disabled = true;
  829. disable_irq_nosync(irq);
  830. napi_schedule(&common->napi_rx);
  831. return IRQ_HANDLED;
  832. }
  833. static irqreturn_t am65_cpsw_nuss_tx_irq(int irq, void *dev_id)
  834. {
  835. struct am65_cpsw_tx_chn *tx_chn = dev_id;
  836. disable_irq_nosync(irq);
  837. napi_schedule(&tx_chn->napi_tx);
  838. return IRQ_HANDLED;
  839. }
  840. static netdev_tx_t am65_cpsw_nuss_ndo_slave_xmit(struct sk_buff *skb,
  841. struct net_device *ndev)
  842. {
  843. struct am65_cpsw_common *common = am65_ndev_to_common(ndev);
  844. struct cppi5_host_desc_t *first_desc, *next_desc, *cur_desc;
  845. struct am65_cpsw_port *port = am65_ndev_to_port(ndev);
  846. struct device *dev = common->dev;
  847. struct am65_cpsw_tx_chn *tx_chn;
  848. struct netdev_queue *netif_txq;
  849. dma_addr_t desc_dma, buf_dma;
  850. int ret, q_idx, i;
  851. void **swdata;
  852. u32 *psdata;
  853. u32 pkt_len;
  854. /* padding enabled in hw */
  855. pkt_len = skb_headlen(skb);
  856. /* SKB TX timestamp */
  857. if (port->tx_ts_enabled)
  858. am65_cpts_prep_tx_timestamp(common->cpts, skb);
  859. q_idx = skb_get_queue_mapping(skb);
  860. dev_dbg(dev, "%s skb_queue:%d\n", __func__, q_idx);
  861. tx_chn = &common->tx_chns[q_idx];
  862. netif_txq = netdev_get_tx_queue(ndev, q_idx);
  863. /* Map the linear buffer */
  864. buf_dma = dma_map_single(tx_chn->dma_dev, skb->data, pkt_len,
  865. DMA_TO_DEVICE);
  866. if (unlikely(dma_mapping_error(tx_chn->dma_dev, buf_dma))) {
  867. dev_err(dev, "Failed to map tx skb buffer\n");
  868. ndev->stats.tx_errors++;
  869. goto err_free_skb;
  870. }
  871. first_desc = k3_cppi_desc_pool_alloc(tx_chn->desc_pool);
  872. if (!first_desc) {
  873. dev_dbg(dev, "Failed to allocate descriptor\n");
  874. dma_unmap_single(tx_chn->dma_dev, buf_dma, pkt_len,
  875. DMA_TO_DEVICE);
  876. goto busy_stop_q;
  877. }
  878. cppi5_hdesc_init(first_desc, CPPI5_INFO0_HDESC_EPIB_PRESENT,
  879. AM65_CPSW_NAV_PS_DATA_SIZE);
  880. cppi5_desc_set_pktids(&first_desc->hdr, 0, 0x3FFF);
  881. cppi5_hdesc_set_pkttype(first_desc, 0x7);
  882. cppi5_desc_set_tags_ids(&first_desc->hdr, 0, port->port_id);
  883. k3_udma_glue_tx_dma_to_cppi5_addr(tx_chn->tx_chn, &buf_dma);
  884. cppi5_hdesc_attach_buf(first_desc, buf_dma, pkt_len, buf_dma, pkt_len);
  885. swdata = cppi5_hdesc_get_swdata(first_desc);
  886. *(swdata) = skb;
  887. psdata = cppi5_hdesc_get_psdata(first_desc);
  888. /* HW csum offload if enabled */
  889. psdata[2] = 0;
  890. if (likely(skb->ip_summed == CHECKSUM_PARTIAL)) {
  891. unsigned int cs_start, cs_offset;
  892. cs_start = skb_transport_offset(skb);
  893. cs_offset = cs_start + skb->csum_offset;
  894. /* HW numerates bytes starting from 1 */
  895. psdata[2] = ((cs_offset + 1) << 24) |
  896. ((cs_start + 1) << 16) | (skb->len - cs_start);
  897. dev_dbg(dev, "%s tx psdata:%#x\n", __func__, psdata[2]);
  898. }
  899. if (!skb_is_nonlinear(skb))
  900. goto done_tx;
  901. dev_dbg(dev, "fragmented SKB\n");
  902. /* Handle the case where skb is fragmented in pages */
  903. cur_desc = first_desc;
  904. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  905. skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  906. u32 frag_size = skb_frag_size(frag);
  907. next_desc = k3_cppi_desc_pool_alloc(tx_chn->desc_pool);
  908. if (!next_desc) {
  909. dev_err(dev, "Failed to allocate descriptor\n");
  910. goto busy_free_descs;
  911. }
  912. buf_dma = skb_frag_dma_map(tx_chn->dma_dev, frag, 0, frag_size,
  913. DMA_TO_DEVICE);
  914. if (unlikely(dma_mapping_error(tx_chn->dma_dev, buf_dma))) {
  915. dev_err(dev, "Failed to map tx skb page\n");
  916. k3_cppi_desc_pool_free(tx_chn->desc_pool, next_desc);
  917. ndev->stats.tx_errors++;
  918. goto err_free_descs;
  919. }
  920. cppi5_hdesc_reset_hbdesc(next_desc);
  921. k3_udma_glue_tx_dma_to_cppi5_addr(tx_chn->tx_chn, &buf_dma);
  922. cppi5_hdesc_attach_buf(next_desc,
  923. buf_dma, frag_size, buf_dma, frag_size);
  924. desc_dma = k3_cppi_desc_pool_virt2dma(tx_chn->desc_pool,
  925. next_desc);
  926. k3_udma_glue_tx_dma_to_cppi5_addr(tx_chn->tx_chn, &desc_dma);
  927. cppi5_hdesc_link_hbdesc(cur_desc, desc_dma);
  928. pkt_len += frag_size;
  929. cur_desc = next_desc;
  930. }
  931. WARN_ON(pkt_len != skb->len);
  932. done_tx:
  933. skb_tx_timestamp(skb);
  934. /* report bql before sending packet */
  935. netdev_tx_sent_queue(netif_txq, pkt_len);
  936. cppi5_hdesc_set_pktlen(first_desc, pkt_len);
  937. desc_dma = k3_cppi_desc_pool_virt2dma(tx_chn->desc_pool, first_desc);
  938. if (AM65_CPSW_IS_CPSW2G(common)) {
  939. ret = k3_udma_glue_push_tx_chn(tx_chn->tx_chn, first_desc, desc_dma);
  940. } else {
  941. spin_lock_bh(&tx_chn->lock);
  942. ret = k3_udma_glue_push_tx_chn(tx_chn->tx_chn, first_desc, desc_dma);
  943. spin_unlock_bh(&tx_chn->lock);
  944. }
  945. if (ret) {
  946. dev_err(dev, "can't push desc %d\n", ret);
  947. /* inform bql */
  948. netdev_tx_completed_queue(netif_txq, 1, pkt_len);
  949. ndev->stats.tx_errors++;
  950. goto err_free_descs;
  951. }
  952. if (k3_cppi_desc_pool_avail(tx_chn->desc_pool) < MAX_SKB_FRAGS) {
  953. netif_tx_stop_queue(netif_txq);
  954. /* Barrier, so that stop_queue visible to other cpus */
  955. smp_mb__after_atomic();
  956. dev_dbg(dev, "netif_tx_stop_queue %d\n", q_idx);
  957. /* re-check for smp */
  958. if (k3_cppi_desc_pool_avail(tx_chn->desc_pool) >=
  959. MAX_SKB_FRAGS) {
  960. netif_tx_wake_queue(netif_txq);
  961. dev_dbg(dev, "netif_tx_wake_queue %d\n", q_idx);
  962. }
  963. }
  964. return NETDEV_TX_OK;
  965. err_free_descs:
  966. am65_cpsw_nuss_xmit_free(tx_chn, first_desc);
  967. err_free_skb:
  968. ndev->stats.tx_dropped++;
  969. dev_kfree_skb_any(skb);
  970. return NETDEV_TX_OK;
  971. busy_free_descs:
  972. am65_cpsw_nuss_xmit_free(tx_chn, first_desc);
  973. busy_stop_q:
  974. netif_tx_stop_queue(netif_txq);
  975. return NETDEV_TX_BUSY;
  976. }
  977. static int am65_cpsw_nuss_ndo_slave_set_mac_address(struct net_device *ndev,
  978. void *addr)
  979. {
  980. struct am65_cpsw_common *common = am65_ndev_to_common(ndev);
  981. struct am65_cpsw_port *port = am65_ndev_to_port(ndev);
  982. struct sockaddr *sockaddr = (struct sockaddr *)addr;
  983. int ret;
  984. ret = eth_prepare_mac_addr_change(ndev, addr);
  985. if (ret < 0)
  986. return ret;
  987. ret = pm_runtime_resume_and_get(common->dev);
  988. if (ret < 0)
  989. return ret;
  990. cpsw_ale_del_ucast(common->ale, ndev->dev_addr,
  991. HOST_PORT_NUM, 0, 0);
  992. cpsw_ale_add_ucast(common->ale, sockaddr->sa_data,
  993. HOST_PORT_NUM, ALE_SECURE, 0);
  994. am65_cpsw_port_set_sl_mac(port, addr);
  995. eth_commit_mac_addr_change(ndev, sockaddr);
  996. pm_runtime_put(common->dev);
  997. return 0;
  998. }
  999. static int am65_cpsw_nuss_hwtstamp_set(struct net_device *ndev,
  1000. struct ifreq *ifr)
  1001. {
  1002. struct am65_cpsw_common *common = am65_ndev_to_common(ndev);
  1003. struct am65_cpsw_port *port = am65_ndev_to_port(ndev);
  1004. u32 ts_ctrl, seq_id, ts_ctrl_ltype2, ts_vlan_ltype;
  1005. struct hwtstamp_config cfg;
  1006. if (!IS_ENABLED(CONFIG_TI_K3_AM65_CPTS))
  1007. return -EOPNOTSUPP;
  1008. if (copy_from_user(&cfg, ifr->ifr_data, sizeof(cfg)))
  1009. return -EFAULT;
  1010. /* TX HW timestamp */
  1011. switch (cfg.tx_type) {
  1012. case HWTSTAMP_TX_OFF:
  1013. case HWTSTAMP_TX_ON:
  1014. break;
  1015. default:
  1016. return -ERANGE;
  1017. }
  1018. switch (cfg.rx_filter) {
  1019. case HWTSTAMP_FILTER_NONE:
  1020. port->rx_ts_enabled = false;
  1021. break;
  1022. case HWTSTAMP_FILTER_ALL:
  1023. case HWTSTAMP_FILTER_SOME:
  1024. case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
  1025. case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
  1026. case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
  1027. case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
  1028. case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
  1029. case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
  1030. case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
  1031. case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
  1032. case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
  1033. case HWTSTAMP_FILTER_PTP_V2_EVENT:
  1034. case HWTSTAMP_FILTER_PTP_V2_SYNC:
  1035. case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
  1036. case HWTSTAMP_FILTER_NTP_ALL:
  1037. port->rx_ts_enabled = true;
  1038. cfg.rx_filter = HWTSTAMP_FILTER_ALL;
  1039. break;
  1040. default:
  1041. return -ERANGE;
  1042. }
  1043. port->tx_ts_enabled = (cfg.tx_type == HWTSTAMP_TX_ON);
  1044. /* cfg TX timestamp */
  1045. seq_id = (AM65_CPSW_TS_SEQ_ID_OFFSET <<
  1046. AM65_CPSW_PN_TS_SEQ_ID_OFFSET_SHIFT) | ETH_P_1588;
  1047. ts_vlan_ltype = ETH_P_8021Q;
  1048. ts_ctrl_ltype2 = ETH_P_1588 |
  1049. AM65_CPSW_PN_TS_CTL_LTYPE2_TS_107 |
  1050. AM65_CPSW_PN_TS_CTL_LTYPE2_TS_129 |
  1051. AM65_CPSW_PN_TS_CTL_LTYPE2_TS_130 |
  1052. AM65_CPSW_PN_TS_CTL_LTYPE2_TS_131 |
  1053. AM65_CPSW_PN_TS_CTL_LTYPE2_TS_132 |
  1054. AM65_CPSW_PN_TS_CTL_LTYPE2_TS_319 |
  1055. AM65_CPSW_PN_TS_CTL_LTYPE2_TS_320 |
  1056. AM65_CPSW_PN_TS_CTL_LTYPE2_TS_TTL_NONZERO;
  1057. ts_ctrl = AM65_CPSW_TS_EVENT_MSG_TYPE_BITS <<
  1058. AM65_CPSW_PN_TS_CTL_MSG_TYPE_EN_SHIFT;
  1059. if (port->tx_ts_enabled)
  1060. ts_ctrl |= AM65_CPSW_TS_TX_ANX_ALL_EN |
  1061. AM65_CPSW_PN_TS_CTL_TX_VLAN_LT1_EN;
  1062. writel(seq_id, port->port_base + AM65_CPSW_PORTN_REG_TS_SEQ_LTYPE_REG);
  1063. writel(ts_vlan_ltype, port->port_base +
  1064. AM65_CPSW_PORTN_REG_TS_VLAN_LTYPE_REG);
  1065. writel(ts_ctrl_ltype2, port->port_base +
  1066. AM65_CPSW_PORTN_REG_TS_CTL_LTYPE2);
  1067. writel(ts_ctrl, port->port_base + AM65_CPSW_PORTN_REG_TS_CTL);
  1068. /* en/dis RX timestamp */
  1069. am65_cpts_rx_enable(common->cpts, port->rx_ts_enabled);
  1070. return copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)) ? -EFAULT : 0;
  1071. }
  1072. static int am65_cpsw_nuss_hwtstamp_get(struct net_device *ndev,
  1073. struct ifreq *ifr)
  1074. {
  1075. struct am65_cpsw_port *port = am65_ndev_to_port(ndev);
  1076. struct hwtstamp_config cfg;
  1077. if (!IS_ENABLED(CONFIG_TI_K3_AM65_CPTS))
  1078. return -EOPNOTSUPP;
  1079. cfg.flags = 0;
  1080. cfg.tx_type = port->tx_ts_enabled ?
  1081. HWTSTAMP_TX_ON : HWTSTAMP_TX_OFF;
  1082. cfg.rx_filter = port->rx_ts_enabled ?
  1083. HWTSTAMP_FILTER_ALL : HWTSTAMP_FILTER_NONE;
  1084. return copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)) ? -EFAULT : 0;
  1085. }
  1086. static int am65_cpsw_nuss_ndo_slave_ioctl(struct net_device *ndev,
  1087. struct ifreq *req, int cmd)
  1088. {
  1089. struct am65_cpsw_port *port = am65_ndev_to_port(ndev);
  1090. if (!netif_running(ndev))
  1091. return -EINVAL;
  1092. switch (cmd) {
  1093. case SIOCSHWTSTAMP:
  1094. return am65_cpsw_nuss_hwtstamp_set(ndev, req);
  1095. case SIOCGHWTSTAMP:
  1096. return am65_cpsw_nuss_hwtstamp_get(ndev, req);
  1097. }
  1098. return phylink_mii_ioctl(port->slave.phylink, req, cmd);
  1099. }
  1100. static void am65_cpsw_nuss_ndo_get_stats(struct net_device *dev,
  1101. struct rtnl_link_stats64 *stats)
  1102. {
  1103. struct am65_cpsw_ndev_priv *ndev_priv = netdev_priv(dev);
  1104. unsigned int start;
  1105. int cpu;
  1106. for_each_possible_cpu(cpu) {
  1107. struct am65_cpsw_ndev_stats *cpu_stats;
  1108. u64 rx_packets;
  1109. u64 rx_bytes;
  1110. u64 tx_packets;
  1111. u64 tx_bytes;
  1112. cpu_stats = per_cpu_ptr(ndev_priv->stats, cpu);
  1113. do {
  1114. start = u64_stats_fetch_begin_irq(&cpu_stats->syncp);
  1115. rx_packets = cpu_stats->rx_packets;
  1116. rx_bytes = cpu_stats->rx_bytes;
  1117. tx_packets = cpu_stats->tx_packets;
  1118. tx_bytes = cpu_stats->tx_bytes;
  1119. } while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, start));
  1120. stats->rx_packets += rx_packets;
  1121. stats->rx_bytes += rx_bytes;
  1122. stats->tx_packets += tx_packets;
  1123. stats->tx_bytes += tx_bytes;
  1124. }
  1125. stats->rx_errors = dev->stats.rx_errors;
  1126. stats->rx_dropped = dev->stats.rx_dropped;
  1127. stats->tx_dropped = dev->stats.tx_dropped;
  1128. }
  1129. static struct devlink_port *am65_cpsw_ndo_get_devlink_port(struct net_device *ndev)
  1130. {
  1131. struct am65_cpsw_port *port = am65_ndev_to_port(ndev);
  1132. return &port->devlink_port;
  1133. }
  1134. static const struct net_device_ops am65_cpsw_nuss_netdev_ops = {
  1135. .ndo_open = am65_cpsw_nuss_ndo_slave_open,
  1136. .ndo_stop = am65_cpsw_nuss_ndo_slave_stop,
  1137. .ndo_start_xmit = am65_cpsw_nuss_ndo_slave_xmit,
  1138. .ndo_set_rx_mode = am65_cpsw_nuss_ndo_slave_set_rx_mode,
  1139. .ndo_get_stats64 = am65_cpsw_nuss_ndo_get_stats,
  1140. .ndo_validate_addr = eth_validate_addr,
  1141. .ndo_set_mac_address = am65_cpsw_nuss_ndo_slave_set_mac_address,
  1142. .ndo_tx_timeout = am65_cpsw_nuss_ndo_host_tx_timeout,
  1143. .ndo_vlan_rx_add_vid = am65_cpsw_nuss_ndo_slave_add_vid,
  1144. .ndo_vlan_rx_kill_vid = am65_cpsw_nuss_ndo_slave_kill_vid,
  1145. .ndo_eth_ioctl = am65_cpsw_nuss_ndo_slave_ioctl,
  1146. .ndo_setup_tc = am65_cpsw_qos_ndo_setup_tc,
  1147. .ndo_get_devlink_port = am65_cpsw_ndo_get_devlink_port,
  1148. };
  1149. static void am65_cpsw_nuss_mac_config(struct phylink_config *config, unsigned int mode,
  1150. const struct phylink_link_state *state)
  1151. {
  1152. struct am65_cpsw_slave_data *slave = container_of(config, struct am65_cpsw_slave_data,
  1153. phylink_config);
  1154. struct am65_cpsw_port *port = container_of(slave, struct am65_cpsw_port, slave);
  1155. struct am65_cpsw_common *common = port->common;
  1156. if (common->pdata.extra_modes & BIT(state->interface))
  1157. writel(AM65_CPSW_SGMII_CONTROL_MR_AN_ENABLE,
  1158. port->sgmii_base + AM65_CPSW_SGMII_CONTROL_REG);
  1159. }
  1160. static void am65_cpsw_nuss_mac_link_down(struct phylink_config *config, unsigned int mode,
  1161. phy_interface_t interface)
  1162. {
  1163. struct am65_cpsw_slave_data *slave = container_of(config, struct am65_cpsw_slave_data,
  1164. phylink_config);
  1165. struct am65_cpsw_port *port = container_of(slave, struct am65_cpsw_port, slave);
  1166. struct am65_cpsw_common *common = port->common;
  1167. struct net_device *ndev = port->ndev;
  1168. int tmo;
  1169. /* disable forwarding */
  1170. cpsw_ale_control_set(common->ale, port->port_id, ALE_PORT_STATE, ALE_PORT_STATE_DISABLE);
  1171. cpsw_sl_ctl_set(port->slave.mac_sl, CPSW_SL_CTL_CMD_IDLE);
  1172. tmo = cpsw_sl_wait_for_idle(port->slave.mac_sl, 100);
  1173. dev_dbg(common->dev, "down msc_sl %08x tmo %d\n",
  1174. cpsw_sl_reg_read(port->slave.mac_sl, CPSW_SL_MACSTATUS), tmo);
  1175. cpsw_sl_ctl_reset(port->slave.mac_sl);
  1176. am65_cpsw_qos_link_down(ndev);
  1177. netif_tx_stop_all_queues(ndev);
  1178. }
  1179. static void am65_cpsw_nuss_mac_link_up(struct phylink_config *config, struct phy_device *phy,
  1180. unsigned int mode, phy_interface_t interface, int speed,
  1181. int duplex, bool tx_pause, bool rx_pause)
  1182. {
  1183. struct am65_cpsw_slave_data *slave = container_of(config, struct am65_cpsw_slave_data,
  1184. phylink_config);
  1185. struct am65_cpsw_port *port = container_of(slave, struct am65_cpsw_port, slave);
  1186. struct am65_cpsw_common *common = port->common;
  1187. u32 mac_control = CPSW_SL_CTL_GMII_EN;
  1188. struct net_device *ndev = port->ndev;
  1189. if (speed == SPEED_1000)
  1190. mac_control |= CPSW_SL_CTL_GIG;
  1191. if (speed == SPEED_10 && phy_interface_mode_is_rgmii(interface))
  1192. /* Can be used with in band mode only */
  1193. mac_control |= CPSW_SL_CTL_EXT_EN;
  1194. if (speed == SPEED_100 && interface == PHY_INTERFACE_MODE_RMII)
  1195. mac_control |= CPSW_SL_CTL_IFCTL_A;
  1196. if (duplex)
  1197. mac_control |= CPSW_SL_CTL_FULLDUPLEX;
  1198. /* rx_pause/tx_pause */
  1199. if (rx_pause)
  1200. mac_control |= CPSW_SL_CTL_RX_FLOW_EN;
  1201. if (tx_pause)
  1202. mac_control |= CPSW_SL_CTL_TX_FLOW_EN;
  1203. cpsw_sl_ctl_set(port->slave.mac_sl, mac_control);
  1204. /* enable forwarding */
  1205. cpsw_ale_control_set(common->ale, port->port_id, ALE_PORT_STATE, ALE_PORT_STATE_FORWARD);
  1206. am65_cpsw_qos_link_up(ndev, speed);
  1207. netif_tx_wake_all_queues(ndev);
  1208. }
  1209. static const struct phylink_mac_ops am65_cpsw_phylink_mac_ops = {
  1210. .validate = phylink_generic_validate,
  1211. .mac_config = am65_cpsw_nuss_mac_config,
  1212. .mac_link_down = am65_cpsw_nuss_mac_link_down,
  1213. .mac_link_up = am65_cpsw_nuss_mac_link_up,
  1214. };
  1215. static void am65_cpsw_nuss_slave_disable_unused(struct am65_cpsw_port *port)
  1216. {
  1217. struct am65_cpsw_common *common = port->common;
  1218. if (!port->disabled)
  1219. return;
  1220. cpsw_ale_control_set(common->ale, port->port_id,
  1221. ALE_PORT_STATE, ALE_PORT_STATE_DISABLE);
  1222. cpsw_sl_reset(port->slave.mac_sl, 100);
  1223. cpsw_sl_ctl_reset(port->slave.mac_sl);
  1224. }
  1225. static void am65_cpsw_nuss_free_tx_chns(void *data)
  1226. {
  1227. struct am65_cpsw_common *common = data;
  1228. int i;
  1229. for (i = 0; i < common->tx_ch_num; i++) {
  1230. struct am65_cpsw_tx_chn *tx_chn = &common->tx_chns[i];
  1231. if (!IS_ERR_OR_NULL(tx_chn->desc_pool))
  1232. k3_cppi_desc_pool_destroy(tx_chn->desc_pool);
  1233. if (!IS_ERR_OR_NULL(tx_chn->tx_chn))
  1234. k3_udma_glue_release_tx_chn(tx_chn->tx_chn);
  1235. memset(tx_chn, 0, sizeof(*tx_chn));
  1236. }
  1237. }
  1238. void am65_cpsw_nuss_remove_tx_chns(struct am65_cpsw_common *common)
  1239. {
  1240. struct device *dev = common->dev;
  1241. int i;
  1242. devm_remove_action(dev, am65_cpsw_nuss_free_tx_chns, common);
  1243. for (i = 0; i < common->tx_ch_num; i++) {
  1244. struct am65_cpsw_tx_chn *tx_chn = &common->tx_chns[i];
  1245. if (tx_chn->irq)
  1246. devm_free_irq(dev, tx_chn->irq, tx_chn);
  1247. netif_napi_del(&tx_chn->napi_tx);
  1248. if (!IS_ERR_OR_NULL(tx_chn->desc_pool))
  1249. k3_cppi_desc_pool_destroy(tx_chn->desc_pool);
  1250. if (!IS_ERR_OR_NULL(tx_chn->tx_chn))
  1251. k3_udma_glue_release_tx_chn(tx_chn->tx_chn);
  1252. memset(tx_chn, 0, sizeof(*tx_chn));
  1253. }
  1254. }
  1255. static int am65_cpsw_nuss_init_tx_chns(struct am65_cpsw_common *common)
  1256. {
  1257. u32 max_desc_num = ALIGN(AM65_CPSW_MAX_TX_DESC, MAX_SKB_FRAGS);
  1258. struct k3_udma_glue_tx_channel_cfg tx_cfg = { 0 };
  1259. struct device *dev = common->dev;
  1260. struct k3_ring_cfg ring_cfg = {
  1261. .elm_size = K3_RINGACC_RING_ELSIZE_8,
  1262. .mode = K3_RINGACC_RING_MODE_RING,
  1263. .flags = 0
  1264. };
  1265. u32 hdesc_size;
  1266. int i, ret = 0;
  1267. hdesc_size = cppi5_hdesc_calc_size(true, AM65_CPSW_NAV_PS_DATA_SIZE,
  1268. AM65_CPSW_NAV_SW_DATA_SIZE);
  1269. tx_cfg.swdata_size = AM65_CPSW_NAV_SW_DATA_SIZE;
  1270. tx_cfg.tx_cfg = ring_cfg;
  1271. tx_cfg.txcq_cfg = ring_cfg;
  1272. tx_cfg.tx_cfg.size = max_desc_num;
  1273. tx_cfg.txcq_cfg.size = max_desc_num;
  1274. for (i = 0; i < common->tx_ch_num; i++) {
  1275. struct am65_cpsw_tx_chn *tx_chn = &common->tx_chns[i];
  1276. snprintf(tx_chn->tx_chn_name,
  1277. sizeof(tx_chn->tx_chn_name), "tx%d", i);
  1278. spin_lock_init(&tx_chn->lock);
  1279. tx_chn->common = common;
  1280. tx_chn->id = i;
  1281. tx_chn->descs_num = max_desc_num;
  1282. tx_chn->tx_chn =
  1283. k3_udma_glue_request_tx_chn(dev,
  1284. tx_chn->tx_chn_name,
  1285. &tx_cfg);
  1286. if (IS_ERR(tx_chn->tx_chn)) {
  1287. ret = dev_err_probe(dev, PTR_ERR(tx_chn->tx_chn),
  1288. "Failed to request tx dma channel\n");
  1289. goto err;
  1290. }
  1291. tx_chn->dma_dev = k3_udma_glue_tx_get_dma_device(tx_chn->tx_chn);
  1292. tx_chn->desc_pool = k3_cppi_desc_pool_create_name(tx_chn->dma_dev,
  1293. tx_chn->descs_num,
  1294. hdesc_size,
  1295. tx_chn->tx_chn_name);
  1296. if (IS_ERR(tx_chn->desc_pool)) {
  1297. ret = PTR_ERR(tx_chn->desc_pool);
  1298. dev_err(dev, "Failed to create poll %d\n", ret);
  1299. goto err;
  1300. }
  1301. tx_chn->irq = k3_udma_glue_tx_get_irq(tx_chn->tx_chn);
  1302. if (tx_chn->irq <= 0) {
  1303. dev_err(dev, "Failed to get tx dma irq %d\n",
  1304. tx_chn->irq);
  1305. ret = tx_chn->irq ?: -ENXIO;
  1306. goto err;
  1307. }
  1308. snprintf(tx_chn->tx_chn_name,
  1309. sizeof(tx_chn->tx_chn_name), "%s-tx%d",
  1310. dev_name(dev), tx_chn->id);
  1311. }
  1312. err:
  1313. i = devm_add_action(dev, am65_cpsw_nuss_free_tx_chns, common);
  1314. if (i) {
  1315. dev_err(dev, "Failed to add free_tx_chns action %d\n", i);
  1316. return i;
  1317. }
  1318. return ret;
  1319. }
  1320. static void am65_cpsw_nuss_free_rx_chns(void *data)
  1321. {
  1322. struct am65_cpsw_common *common = data;
  1323. struct am65_cpsw_rx_chn *rx_chn;
  1324. rx_chn = &common->rx_chns;
  1325. if (!IS_ERR_OR_NULL(rx_chn->desc_pool))
  1326. k3_cppi_desc_pool_destroy(rx_chn->desc_pool);
  1327. if (!IS_ERR_OR_NULL(rx_chn->rx_chn))
  1328. k3_udma_glue_release_rx_chn(rx_chn->rx_chn);
  1329. }
  1330. static int am65_cpsw_nuss_init_rx_chns(struct am65_cpsw_common *common)
  1331. {
  1332. struct am65_cpsw_rx_chn *rx_chn = &common->rx_chns;
  1333. struct k3_udma_glue_rx_channel_cfg rx_cfg = { 0 };
  1334. u32 max_desc_num = AM65_CPSW_MAX_RX_DESC;
  1335. struct device *dev = common->dev;
  1336. u32 hdesc_size;
  1337. u32 fdqring_id;
  1338. int i, ret = 0;
  1339. hdesc_size = cppi5_hdesc_calc_size(true, AM65_CPSW_NAV_PS_DATA_SIZE,
  1340. AM65_CPSW_NAV_SW_DATA_SIZE);
  1341. rx_cfg.swdata_size = AM65_CPSW_NAV_SW_DATA_SIZE;
  1342. rx_cfg.flow_id_num = AM65_CPSW_MAX_RX_FLOWS;
  1343. rx_cfg.flow_id_base = common->rx_flow_id_base;
  1344. /* init all flows */
  1345. rx_chn->dev = dev;
  1346. rx_chn->descs_num = max_desc_num;
  1347. rx_chn->rx_chn = k3_udma_glue_request_rx_chn(dev, "rx", &rx_cfg);
  1348. if (IS_ERR(rx_chn->rx_chn)) {
  1349. ret = dev_err_probe(dev, PTR_ERR(rx_chn->rx_chn),
  1350. "Failed to request rx dma channel\n");
  1351. goto err;
  1352. }
  1353. rx_chn->dma_dev = k3_udma_glue_rx_get_dma_device(rx_chn->rx_chn);
  1354. rx_chn->desc_pool = k3_cppi_desc_pool_create_name(rx_chn->dma_dev,
  1355. rx_chn->descs_num,
  1356. hdesc_size, "rx");
  1357. if (IS_ERR(rx_chn->desc_pool)) {
  1358. ret = PTR_ERR(rx_chn->desc_pool);
  1359. dev_err(dev, "Failed to create rx poll %d\n", ret);
  1360. goto err;
  1361. }
  1362. common->rx_flow_id_base =
  1363. k3_udma_glue_rx_get_flow_id_base(rx_chn->rx_chn);
  1364. dev_info(dev, "set new flow-id-base %u\n", common->rx_flow_id_base);
  1365. fdqring_id = K3_RINGACC_RING_ID_ANY;
  1366. for (i = 0; i < rx_cfg.flow_id_num; i++) {
  1367. struct k3_ring_cfg rxring_cfg = {
  1368. .elm_size = K3_RINGACC_RING_ELSIZE_8,
  1369. .mode = K3_RINGACC_RING_MODE_RING,
  1370. .flags = 0,
  1371. };
  1372. struct k3_ring_cfg fdqring_cfg = {
  1373. .elm_size = K3_RINGACC_RING_ELSIZE_8,
  1374. .flags = K3_RINGACC_RING_SHARED,
  1375. };
  1376. struct k3_udma_glue_rx_flow_cfg rx_flow_cfg = {
  1377. .rx_cfg = rxring_cfg,
  1378. .rxfdq_cfg = fdqring_cfg,
  1379. .ring_rxq_id = K3_RINGACC_RING_ID_ANY,
  1380. .src_tag_lo_sel =
  1381. K3_UDMA_GLUE_SRC_TAG_LO_USE_REMOTE_SRC_TAG,
  1382. };
  1383. rx_flow_cfg.ring_rxfdq0_id = fdqring_id;
  1384. rx_flow_cfg.rx_cfg.size = max_desc_num;
  1385. rx_flow_cfg.rxfdq_cfg.size = max_desc_num;
  1386. rx_flow_cfg.rxfdq_cfg.mode = common->pdata.fdqring_mode;
  1387. ret = k3_udma_glue_rx_flow_init(rx_chn->rx_chn,
  1388. i, &rx_flow_cfg);
  1389. if (ret) {
  1390. dev_err(dev, "Failed to init rx flow%d %d\n", i, ret);
  1391. goto err;
  1392. }
  1393. if (!i)
  1394. fdqring_id =
  1395. k3_udma_glue_rx_flow_get_fdq_id(rx_chn->rx_chn,
  1396. i);
  1397. rx_chn->irq = k3_udma_glue_rx_get_irq(rx_chn->rx_chn, i);
  1398. if (rx_chn->irq <= 0) {
  1399. dev_err(dev, "Failed to get rx dma irq %d\n",
  1400. rx_chn->irq);
  1401. ret = -ENXIO;
  1402. goto err;
  1403. }
  1404. }
  1405. err:
  1406. i = devm_add_action(dev, am65_cpsw_nuss_free_rx_chns, common);
  1407. if (i) {
  1408. dev_err(dev, "Failed to add free_rx_chns action %d\n", i);
  1409. return i;
  1410. }
  1411. return ret;
  1412. }
  1413. static int am65_cpsw_nuss_init_host_p(struct am65_cpsw_common *common)
  1414. {
  1415. struct am65_cpsw_host *host_p = am65_common_get_host(common);
  1416. host_p->common = common;
  1417. host_p->port_base = common->cpsw_base + AM65_CPSW_NU_PORTS_BASE;
  1418. host_p->stat_base = common->cpsw_base + AM65_CPSW_NU_STATS_BASE;
  1419. return 0;
  1420. }
  1421. static int am65_cpsw_am654_get_efuse_macid(struct device_node *of_node,
  1422. int slave, u8 *mac_addr)
  1423. {
  1424. u32 mac_lo, mac_hi, offset;
  1425. struct regmap *syscon;
  1426. int ret;
  1427. syscon = syscon_regmap_lookup_by_phandle(of_node, "ti,syscon-efuse");
  1428. if (IS_ERR(syscon)) {
  1429. if (PTR_ERR(syscon) == -ENODEV)
  1430. return 0;
  1431. return PTR_ERR(syscon);
  1432. }
  1433. ret = of_property_read_u32_index(of_node, "ti,syscon-efuse", 1,
  1434. &offset);
  1435. if (ret)
  1436. return ret;
  1437. regmap_read(syscon, offset, &mac_lo);
  1438. regmap_read(syscon, offset + 4, &mac_hi);
  1439. mac_addr[0] = (mac_hi >> 8) & 0xff;
  1440. mac_addr[1] = mac_hi & 0xff;
  1441. mac_addr[2] = (mac_lo >> 24) & 0xff;
  1442. mac_addr[3] = (mac_lo >> 16) & 0xff;
  1443. mac_addr[4] = (mac_lo >> 8) & 0xff;
  1444. mac_addr[5] = mac_lo & 0xff;
  1445. return 0;
  1446. }
  1447. static int am65_cpsw_init_cpts(struct am65_cpsw_common *common)
  1448. {
  1449. struct device *dev = common->dev;
  1450. struct device_node *node;
  1451. struct am65_cpts *cpts;
  1452. void __iomem *reg_base;
  1453. if (!IS_ENABLED(CONFIG_TI_K3_AM65_CPTS))
  1454. return 0;
  1455. node = of_get_child_by_name(dev->of_node, "cpts");
  1456. if (!node) {
  1457. dev_err(dev, "%s cpts not found\n", __func__);
  1458. return -ENOENT;
  1459. }
  1460. reg_base = common->cpsw_base + AM65_CPSW_NU_CPTS_BASE;
  1461. cpts = am65_cpts_create(dev, reg_base, node);
  1462. if (IS_ERR(cpts)) {
  1463. int ret = PTR_ERR(cpts);
  1464. of_node_put(node);
  1465. if (ret == -EOPNOTSUPP) {
  1466. dev_info(dev, "cpts disabled\n");
  1467. return 0;
  1468. }
  1469. dev_err(dev, "cpts create err %d\n", ret);
  1470. return ret;
  1471. }
  1472. common->cpts = cpts;
  1473. /* Forbid PM runtime if CPTS is running.
  1474. * K3 CPSWxG modules may completely lose context during ON->OFF
  1475. * transitions depending on integration.
  1476. * AM65x/J721E MCU CPSW2G: false
  1477. * J721E MAIN_CPSW9G: true
  1478. */
  1479. pm_runtime_forbid(dev);
  1480. return 0;
  1481. }
  1482. static int am65_cpsw_nuss_init_slave_ports(struct am65_cpsw_common *common)
  1483. {
  1484. struct device_node *node, *port_np;
  1485. struct device *dev = common->dev;
  1486. int ret;
  1487. node = of_get_child_by_name(dev->of_node, "ethernet-ports");
  1488. if (!node)
  1489. return -ENOENT;
  1490. for_each_child_of_node(node, port_np) {
  1491. struct am65_cpsw_port *port;
  1492. u32 port_id;
  1493. /* it is not a slave port node, continue */
  1494. if (strcmp(port_np->name, "port"))
  1495. continue;
  1496. ret = of_property_read_u32(port_np, "reg", &port_id);
  1497. if (ret < 0) {
  1498. dev_err(dev, "%pOF error reading port_id %d\n",
  1499. port_np, ret);
  1500. goto of_node_put;
  1501. }
  1502. if (!port_id || port_id > common->port_num) {
  1503. dev_err(dev, "%pOF has invalid port_id %u %s\n",
  1504. port_np, port_id, port_np->name);
  1505. ret = -EINVAL;
  1506. goto of_node_put;
  1507. }
  1508. port = am65_common_get_port(common, port_id);
  1509. port->port_id = port_id;
  1510. port->common = common;
  1511. port->port_base = common->cpsw_base + AM65_CPSW_NU_PORTS_BASE +
  1512. AM65_CPSW_NU_PORTS_OFFSET * (port_id);
  1513. if (common->pdata.extra_modes)
  1514. port->sgmii_base = common->ss_base + AM65_CPSW_SGMII_BASE * (port_id);
  1515. port->stat_base = common->cpsw_base + AM65_CPSW_NU_STATS_BASE +
  1516. (AM65_CPSW_NU_STATS_PORT_OFFSET * port_id);
  1517. port->name = of_get_property(port_np, "label", NULL);
  1518. port->fetch_ram_base =
  1519. common->cpsw_base + AM65_CPSW_NU_FRAM_BASE +
  1520. (AM65_CPSW_NU_FRAM_PORT_OFFSET * (port_id - 1));
  1521. port->slave.mac_sl = cpsw_sl_get("am65", dev, port->port_base);
  1522. if (IS_ERR(port->slave.mac_sl)) {
  1523. ret = PTR_ERR(port->slave.mac_sl);
  1524. goto of_node_put;
  1525. }
  1526. port->disabled = !of_device_is_available(port_np);
  1527. if (port->disabled) {
  1528. common->disabled_ports_mask |= BIT(port->port_id);
  1529. continue;
  1530. }
  1531. port->slave.ifphy = devm_of_phy_get(dev, port_np, NULL);
  1532. if (IS_ERR(port->slave.ifphy)) {
  1533. ret = PTR_ERR(port->slave.ifphy);
  1534. dev_err(dev, "%pOF error retrieving port phy: %d\n",
  1535. port_np, ret);
  1536. goto of_node_put;
  1537. }
  1538. port->slave.mac_only =
  1539. of_property_read_bool(port_np, "ti,mac-only");
  1540. /* get phy/link info */
  1541. port->slave.phy_node = port_np;
  1542. ret = of_get_phy_mode(port_np, &port->slave.phy_if);
  1543. if (ret) {
  1544. dev_err(dev, "%pOF read phy-mode err %d\n",
  1545. port_np, ret);
  1546. goto of_node_put;
  1547. }
  1548. ret = phy_set_mode_ext(port->slave.ifphy, PHY_MODE_ETHERNET, port->slave.phy_if);
  1549. if (ret)
  1550. goto of_node_put;
  1551. ret = of_get_mac_address(port_np, port->slave.mac_addr);
  1552. if (ret) {
  1553. am65_cpsw_am654_get_efuse_macid(port_np,
  1554. port->port_id,
  1555. port->slave.mac_addr);
  1556. if (!is_valid_ether_addr(port->slave.mac_addr)) {
  1557. eth_random_addr(port->slave.mac_addr);
  1558. dev_err(dev, "Use random MAC address\n");
  1559. }
  1560. }
  1561. }
  1562. of_node_put(node);
  1563. /* is there at least one ext.port */
  1564. if (!(~common->disabled_ports_mask & GENMASK(common->port_num, 1))) {
  1565. dev_err(dev, "No Ext. port are available\n");
  1566. return -ENODEV;
  1567. }
  1568. return 0;
  1569. of_node_put:
  1570. of_node_put(port_np);
  1571. of_node_put(node);
  1572. return ret;
  1573. }
  1574. static void am65_cpsw_pcpu_stats_free(void *data)
  1575. {
  1576. struct am65_cpsw_ndev_stats __percpu *stats = data;
  1577. free_percpu(stats);
  1578. }
  1579. static void am65_cpsw_nuss_phylink_cleanup(struct am65_cpsw_common *common)
  1580. {
  1581. struct am65_cpsw_port *port;
  1582. int i;
  1583. for (i = 0; i < common->port_num; i++) {
  1584. port = &common->ports[i];
  1585. if (port->slave.phylink)
  1586. phylink_destroy(port->slave.phylink);
  1587. }
  1588. }
  1589. static int
  1590. am65_cpsw_nuss_init_port_ndev(struct am65_cpsw_common *common, u32 port_idx)
  1591. {
  1592. struct am65_cpsw_ndev_priv *ndev_priv;
  1593. struct device *dev = common->dev;
  1594. struct am65_cpsw_port *port;
  1595. struct phylink *phylink;
  1596. int ret;
  1597. port = &common->ports[port_idx];
  1598. if (port->disabled)
  1599. return 0;
  1600. /* alloc netdev */
  1601. port->ndev = devm_alloc_etherdev_mqs(common->dev,
  1602. sizeof(struct am65_cpsw_ndev_priv),
  1603. AM65_CPSW_MAX_TX_QUEUES,
  1604. AM65_CPSW_MAX_RX_QUEUES);
  1605. if (!port->ndev) {
  1606. dev_err(dev, "error allocating slave net_device %u\n",
  1607. port->port_id);
  1608. return -ENOMEM;
  1609. }
  1610. ndev_priv = netdev_priv(port->ndev);
  1611. ndev_priv->port = port;
  1612. ndev_priv->msg_enable = AM65_CPSW_DEBUG;
  1613. SET_NETDEV_DEV(port->ndev, dev);
  1614. eth_hw_addr_set(port->ndev, port->slave.mac_addr);
  1615. port->ndev->min_mtu = AM65_CPSW_MIN_PACKET_SIZE;
  1616. port->ndev->max_mtu = AM65_CPSW_MAX_PACKET_SIZE;
  1617. port->ndev->hw_features = NETIF_F_SG |
  1618. NETIF_F_RXCSUM |
  1619. NETIF_F_HW_CSUM |
  1620. NETIF_F_HW_TC;
  1621. port->ndev->features = port->ndev->hw_features |
  1622. NETIF_F_HW_VLAN_CTAG_FILTER;
  1623. port->ndev->vlan_features |= NETIF_F_SG;
  1624. port->ndev->netdev_ops = &am65_cpsw_nuss_netdev_ops;
  1625. port->ndev->ethtool_ops = &am65_cpsw_ethtool_ops_slave;
  1626. /* Configuring Phylink */
  1627. port->slave.phylink_config.dev = &port->ndev->dev;
  1628. port->slave.phylink_config.type = PHYLINK_NETDEV;
  1629. port->slave.phylink_config.mac_capabilities = MAC_SYM_PAUSE | MAC_10 | MAC_100 | MAC_1000FD;
  1630. if (phy_interface_mode_is_rgmii(port->slave.phy_if)) {
  1631. phy_interface_set_rgmii(port->slave.phylink_config.supported_interfaces);
  1632. } else if (port->slave.phy_if == PHY_INTERFACE_MODE_RMII) {
  1633. __set_bit(PHY_INTERFACE_MODE_RMII,
  1634. port->slave.phylink_config.supported_interfaces);
  1635. } else if (common->pdata.extra_modes & BIT(port->slave.phy_if)) {
  1636. __set_bit(PHY_INTERFACE_MODE_QSGMII,
  1637. port->slave.phylink_config.supported_interfaces);
  1638. } else {
  1639. dev_err(dev, "selected phy-mode is not supported\n");
  1640. return -EOPNOTSUPP;
  1641. }
  1642. phylink = phylink_create(&port->slave.phylink_config,
  1643. of_node_to_fwnode(port->slave.phy_node),
  1644. port->slave.phy_if,
  1645. &am65_cpsw_phylink_mac_ops);
  1646. if (IS_ERR(phylink))
  1647. return PTR_ERR(phylink);
  1648. port->slave.phylink = phylink;
  1649. /* Disable TX checksum offload by default due to HW bug */
  1650. if (common->pdata.quirks & AM65_CPSW_QUIRK_I2027_NO_TX_CSUM)
  1651. port->ndev->features &= ~NETIF_F_HW_CSUM;
  1652. ndev_priv->stats = netdev_alloc_pcpu_stats(struct am65_cpsw_ndev_stats);
  1653. if (!ndev_priv->stats)
  1654. return -ENOMEM;
  1655. ret = devm_add_action_or_reset(dev, am65_cpsw_pcpu_stats_free,
  1656. ndev_priv->stats);
  1657. if (ret)
  1658. dev_err(dev, "failed to add percpu stat free action %d\n", ret);
  1659. if (!common->dma_ndev)
  1660. common->dma_ndev = port->ndev;
  1661. return ret;
  1662. }
  1663. static int am65_cpsw_nuss_init_ndevs(struct am65_cpsw_common *common)
  1664. {
  1665. int ret;
  1666. int i;
  1667. for (i = 0; i < common->port_num; i++) {
  1668. ret = am65_cpsw_nuss_init_port_ndev(common, i);
  1669. if (ret)
  1670. return ret;
  1671. }
  1672. netif_napi_add(common->dma_ndev, &common->napi_rx,
  1673. am65_cpsw_nuss_rx_poll);
  1674. return ret;
  1675. }
  1676. static int am65_cpsw_nuss_ndev_add_tx_napi(struct am65_cpsw_common *common)
  1677. {
  1678. struct device *dev = common->dev;
  1679. int i, ret = 0;
  1680. for (i = 0; i < common->tx_ch_num; i++) {
  1681. struct am65_cpsw_tx_chn *tx_chn = &common->tx_chns[i];
  1682. netif_napi_add_tx(common->dma_ndev, &tx_chn->napi_tx,
  1683. am65_cpsw_nuss_tx_poll);
  1684. ret = devm_request_irq(dev, tx_chn->irq,
  1685. am65_cpsw_nuss_tx_irq,
  1686. IRQF_TRIGGER_HIGH,
  1687. tx_chn->tx_chn_name, tx_chn);
  1688. if (ret) {
  1689. dev_err(dev, "failure requesting tx%u irq %u, %d\n",
  1690. tx_chn->id, tx_chn->irq, ret);
  1691. goto err;
  1692. }
  1693. }
  1694. err:
  1695. return ret;
  1696. }
  1697. static void am65_cpsw_nuss_cleanup_ndev(struct am65_cpsw_common *common)
  1698. {
  1699. struct am65_cpsw_port *port;
  1700. int i;
  1701. for (i = 0; i < common->port_num; i++) {
  1702. port = &common->ports[i];
  1703. if (port->ndev && port->ndev->reg_state == NETREG_REGISTERED)
  1704. unregister_netdev(port->ndev);
  1705. }
  1706. }
  1707. static void am65_cpsw_port_offload_fwd_mark_update(struct am65_cpsw_common *common)
  1708. {
  1709. int set_val = 0;
  1710. int i;
  1711. if (common->br_members == (GENMASK(common->port_num, 1) & ~common->disabled_ports_mask))
  1712. set_val = 1;
  1713. dev_dbg(common->dev, "set offload_fwd_mark %d\n", set_val);
  1714. for (i = 1; i <= common->port_num; i++) {
  1715. struct am65_cpsw_port *port = am65_common_get_port(common, i);
  1716. struct am65_cpsw_ndev_priv *priv;
  1717. if (!port->ndev)
  1718. continue;
  1719. priv = am65_ndev_to_priv(port->ndev);
  1720. priv->offload_fwd_mark = set_val;
  1721. }
  1722. }
  1723. bool am65_cpsw_port_dev_check(const struct net_device *ndev)
  1724. {
  1725. if (ndev->netdev_ops == &am65_cpsw_nuss_netdev_ops) {
  1726. struct am65_cpsw_common *common = am65_ndev_to_common(ndev);
  1727. return !common->is_emac_mode;
  1728. }
  1729. return false;
  1730. }
  1731. static int am65_cpsw_netdevice_port_link(struct net_device *ndev,
  1732. struct net_device *br_ndev,
  1733. struct netlink_ext_ack *extack)
  1734. {
  1735. struct am65_cpsw_common *common = am65_ndev_to_common(ndev);
  1736. struct am65_cpsw_ndev_priv *priv = am65_ndev_to_priv(ndev);
  1737. int err;
  1738. if (!common->br_members) {
  1739. common->hw_bridge_dev = br_ndev;
  1740. } else {
  1741. /* This is adding the port to a second bridge, this is
  1742. * unsupported
  1743. */
  1744. if (common->hw_bridge_dev != br_ndev)
  1745. return -EOPNOTSUPP;
  1746. }
  1747. err = switchdev_bridge_port_offload(ndev, ndev, NULL, NULL, NULL,
  1748. false, extack);
  1749. if (err)
  1750. return err;
  1751. common->br_members |= BIT(priv->port->port_id);
  1752. am65_cpsw_port_offload_fwd_mark_update(common);
  1753. return NOTIFY_DONE;
  1754. }
  1755. static void am65_cpsw_netdevice_port_unlink(struct net_device *ndev)
  1756. {
  1757. struct am65_cpsw_common *common = am65_ndev_to_common(ndev);
  1758. struct am65_cpsw_ndev_priv *priv = am65_ndev_to_priv(ndev);
  1759. switchdev_bridge_port_unoffload(ndev, NULL, NULL, NULL);
  1760. common->br_members &= ~BIT(priv->port->port_id);
  1761. am65_cpsw_port_offload_fwd_mark_update(common);
  1762. if (!common->br_members)
  1763. common->hw_bridge_dev = NULL;
  1764. }
  1765. /* netdev notifier */
  1766. static int am65_cpsw_netdevice_event(struct notifier_block *unused,
  1767. unsigned long event, void *ptr)
  1768. {
  1769. struct netlink_ext_ack *extack = netdev_notifier_info_to_extack(ptr);
  1770. struct net_device *ndev = netdev_notifier_info_to_dev(ptr);
  1771. struct netdev_notifier_changeupper_info *info;
  1772. int ret = NOTIFY_DONE;
  1773. if (!am65_cpsw_port_dev_check(ndev))
  1774. return NOTIFY_DONE;
  1775. switch (event) {
  1776. case NETDEV_CHANGEUPPER:
  1777. info = ptr;
  1778. if (netif_is_bridge_master(info->upper_dev)) {
  1779. if (info->linking)
  1780. ret = am65_cpsw_netdevice_port_link(ndev,
  1781. info->upper_dev,
  1782. extack);
  1783. else
  1784. am65_cpsw_netdevice_port_unlink(ndev);
  1785. }
  1786. break;
  1787. default:
  1788. return NOTIFY_DONE;
  1789. }
  1790. return notifier_from_errno(ret);
  1791. }
  1792. static int am65_cpsw_register_notifiers(struct am65_cpsw_common *cpsw)
  1793. {
  1794. int ret = 0;
  1795. if (AM65_CPSW_IS_CPSW2G(cpsw) ||
  1796. !IS_REACHABLE(CONFIG_TI_K3_AM65_CPSW_SWITCHDEV))
  1797. return 0;
  1798. cpsw->am65_cpsw_netdevice_nb.notifier_call = &am65_cpsw_netdevice_event;
  1799. ret = register_netdevice_notifier(&cpsw->am65_cpsw_netdevice_nb);
  1800. if (ret) {
  1801. dev_err(cpsw->dev, "can't register netdevice notifier\n");
  1802. return ret;
  1803. }
  1804. ret = am65_cpsw_switchdev_register_notifiers(cpsw);
  1805. if (ret)
  1806. unregister_netdevice_notifier(&cpsw->am65_cpsw_netdevice_nb);
  1807. return ret;
  1808. }
  1809. static void am65_cpsw_unregister_notifiers(struct am65_cpsw_common *cpsw)
  1810. {
  1811. if (AM65_CPSW_IS_CPSW2G(cpsw) ||
  1812. !IS_REACHABLE(CONFIG_TI_K3_AM65_CPSW_SWITCHDEV))
  1813. return;
  1814. am65_cpsw_switchdev_unregister_notifiers(cpsw);
  1815. unregister_netdevice_notifier(&cpsw->am65_cpsw_netdevice_nb);
  1816. }
  1817. static const struct devlink_ops am65_cpsw_devlink_ops = {};
  1818. static void am65_cpsw_init_stp_ale_entry(struct am65_cpsw_common *cpsw)
  1819. {
  1820. cpsw_ale_add_mcast(cpsw->ale, eth_stp_addr, ALE_PORT_HOST, ALE_SUPER, 0,
  1821. ALE_MCAST_BLOCK_LEARN_FWD);
  1822. }
  1823. static void am65_cpsw_init_host_port_switch(struct am65_cpsw_common *common)
  1824. {
  1825. struct am65_cpsw_host *host = am65_common_get_host(common);
  1826. writel(common->default_vlan, host->port_base + AM65_CPSW_PORT_VLAN_REG_OFFSET);
  1827. am65_cpsw_init_stp_ale_entry(common);
  1828. cpsw_ale_control_set(common->ale, HOST_PORT_NUM, ALE_P0_UNI_FLOOD, 1);
  1829. dev_dbg(common->dev, "Set P0_UNI_FLOOD\n");
  1830. cpsw_ale_control_set(common->ale, HOST_PORT_NUM, ALE_PORT_NOLEARN, 0);
  1831. }
  1832. static void am65_cpsw_init_host_port_emac(struct am65_cpsw_common *common)
  1833. {
  1834. struct am65_cpsw_host *host = am65_common_get_host(common);
  1835. writel(0, host->port_base + AM65_CPSW_PORT_VLAN_REG_OFFSET);
  1836. cpsw_ale_control_set(common->ale, HOST_PORT_NUM, ALE_P0_UNI_FLOOD, 0);
  1837. dev_dbg(common->dev, "unset P0_UNI_FLOOD\n");
  1838. /* learning make no sense in multi-mac mode */
  1839. cpsw_ale_control_set(common->ale, HOST_PORT_NUM, ALE_PORT_NOLEARN, 1);
  1840. }
  1841. static int am65_cpsw_dl_switch_mode_get(struct devlink *dl, u32 id,
  1842. struct devlink_param_gset_ctx *ctx)
  1843. {
  1844. struct am65_cpsw_devlink *dl_priv = devlink_priv(dl);
  1845. struct am65_cpsw_common *common = dl_priv->common;
  1846. dev_dbg(common->dev, "%s id:%u\n", __func__, id);
  1847. if (id != AM65_CPSW_DL_PARAM_SWITCH_MODE)
  1848. return -EOPNOTSUPP;
  1849. ctx->val.vbool = !common->is_emac_mode;
  1850. return 0;
  1851. }
  1852. static void am65_cpsw_init_port_emac_ale(struct am65_cpsw_port *port)
  1853. {
  1854. struct am65_cpsw_slave_data *slave = &port->slave;
  1855. struct am65_cpsw_common *common = port->common;
  1856. u32 port_mask;
  1857. writel(slave->port_vlan, port->port_base + AM65_CPSW_PORT_VLAN_REG_OFFSET);
  1858. if (slave->mac_only)
  1859. /* enable mac-only mode on port */
  1860. cpsw_ale_control_set(common->ale, port->port_id,
  1861. ALE_PORT_MACONLY, 1);
  1862. cpsw_ale_control_set(common->ale, port->port_id, ALE_PORT_NOLEARN, 1);
  1863. port_mask = BIT(port->port_id) | ALE_PORT_HOST;
  1864. cpsw_ale_add_ucast(common->ale, port->ndev->dev_addr,
  1865. HOST_PORT_NUM, ALE_SECURE, slave->port_vlan);
  1866. cpsw_ale_add_mcast(common->ale, port->ndev->broadcast,
  1867. port_mask, ALE_VLAN, slave->port_vlan, ALE_MCAST_FWD_2);
  1868. }
  1869. static void am65_cpsw_init_port_switch_ale(struct am65_cpsw_port *port)
  1870. {
  1871. struct am65_cpsw_slave_data *slave = &port->slave;
  1872. struct am65_cpsw_common *cpsw = port->common;
  1873. u32 port_mask;
  1874. cpsw_ale_control_set(cpsw->ale, port->port_id,
  1875. ALE_PORT_NOLEARN, 0);
  1876. cpsw_ale_add_ucast(cpsw->ale, port->ndev->dev_addr,
  1877. HOST_PORT_NUM, ALE_SECURE | ALE_BLOCKED | ALE_VLAN,
  1878. slave->port_vlan);
  1879. port_mask = BIT(port->port_id) | ALE_PORT_HOST;
  1880. cpsw_ale_add_mcast(cpsw->ale, port->ndev->broadcast,
  1881. port_mask, ALE_VLAN, slave->port_vlan,
  1882. ALE_MCAST_FWD_2);
  1883. writel(slave->port_vlan, port->port_base + AM65_CPSW_PORT_VLAN_REG_OFFSET);
  1884. cpsw_ale_control_set(cpsw->ale, port->port_id,
  1885. ALE_PORT_MACONLY, 0);
  1886. }
  1887. static int am65_cpsw_dl_switch_mode_set(struct devlink *dl, u32 id,
  1888. struct devlink_param_gset_ctx *ctx)
  1889. {
  1890. struct am65_cpsw_devlink *dl_priv = devlink_priv(dl);
  1891. struct am65_cpsw_common *cpsw = dl_priv->common;
  1892. bool switch_en = ctx->val.vbool;
  1893. bool if_running = false;
  1894. int i;
  1895. dev_dbg(cpsw->dev, "%s id:%u\n", __func__, id);
  1896. if (id != AM65_CPSW_DL_PARAM_SWITCH_MODE)
  1897. return -EOPNOTSUPP;
  1898. if (switch_en == !cpsw->is_emac_mode)
  1899. return 0;
  1900. if (!switch_en && cpsw->br_members) {
  1901. dev_err(cpsw->dev, "Remove ports from bridge before disabling switch mode\n");
  1902. return -EINVAL;
  1903. }
  1904. rtnl_lock();
  1905. cpsw->is_emac_mode = !switch_en;
  1906. for (i = 0; i < cpsw->port_num; i++) {
  1907. struct net_device *sl_ndev = cpsw->ports[i].ndev;
  1908. if (!sl_ndev || !netif_running(sl_ndev))
  1909. continue;
  1910. if_running = true;
  1911. }
  1912. if (!if_running) {
  1913. /* all ndevs are down */
  1914. for (i = 0; i < cpsw->port_num; i++) {
  1915. struct net_device *sl_ndev = cpsw->ports[i].ndev;
  1916. struct am65_cpsw_slave_data *slave;
  1917. if (!sl_ndev)
  1918. continue;
  1919. slave = am65_ndev_to_slave(sl_ndev);
  1920. if (switch_en)
  1921. slave->port_vlan = cpsw->default_vlan;
  1922. else
  1923. slave->port_vlan = 0;
  1924. }
  1925. goto exit;
  1926. }
  1927. cpsw_ale_control_set(cpsw->ale, 0, ALE_BYPASS, 1);
  1928. /* clean up ALE table */
  1929. cpsw_ale_control_set(cpsw->ale, HOST_PORT_NUM, ALE_CLEAR, 1);
  1930. cpsw_ale_control_get(cpsw->ale, HOST_PORT_NUM, ALE_AGEOUT);
  1931. if (switch_en) {
  1932. dev_info(cpsw->dev, "Enable switch mode\n");
  1933. am65_cpsw_init_host_port_switch(cpsw);
  1934. for (i = 0; i < cpsw->port_num; i++) {
  1935. struct net_device *sl_ndev = cpsw->ports[i].ndev;
  1936. struct am65_cpsw_slave_data *slave;
  1937. struct am65_cpsw_port *port;
  1938. if (!sl_ndev)
  1939. continue;
  1940. port = am65_ndev_to_port(sl_ndev);
  1941. slave = am65_ndev_to_slave(sl_ndev);
  1942. slave->port_vlan = cpsw->default_vlan;
  1943. if (netif_running(sl_ndev))
  1944. am65_cpsw_init_port_switch_ale(port);
  1945. }
  1946. } else {
  1947. dev_info(cpsw->dev, "Disable switch mode\n");
  1948. am65_cpsw_init_host_port_emac(cpsw);
  1949. for (i = 0; i < cpsw->port_num; i++) {
  1950. struct net_device *sl_ndev = cpsw->ports[i].ndev;
  1951. struct am65_cpsw_port *port;
  1952. if (!sl_ndev)
  1953. continue;
  1954. port = am65_ndev_to_port(sl_ndev);
  1955. port->slave.port_vlan = 0;
  1956. if (netif_running(sl_ndev))
  1957. am65_cpsw_init_port_emac_ale(port);
  1958. }
  1959. }
  1960. cpsw_ale_control_set(cpsw->ale, HOST_PORT_NUM, ALE_BYPASS, 0);
  1961. exit:
  1962. rtnl_unlock();
  1963. return 0;
  1964. }
  1965. static const struct devlink_param am65_cpsw_devlink_params[] = {
  1966. DEVLINK_PARAM_DRIVER(AM65_CPSW_DL_PARAM_SWITCH_MODE, "switch_mode",
  1967. DEVLINK_PARAM_TYPE_BOOL,
  1968. BIT(DEVLINK_PARAM_CMODE_RUNTIME),
  1969. am65_cpsw_dl_switch_mode_get,
  1970. am65_cpsw_dl_switch_mode_set, NULL),
  1971. };
  1972. static int am65_cpsw_nuss_register_devlink(struct am65_cpsw_common *common)
  1973. {
  1974. struct devlink_port_attrs attrs = {};
  1975. struct am65_cpsw_devlink *dl_priv;
  1976. struct device *dev = common->dev;
  1977. struct devlink_port *dl_port;
  1978. struct am65_cpsw_port *port;
  1979. int ret = 0;
  1980. int i;
  1981. common->devlink =
  1982. devlink_alloc(&am65_cpsw_devlink_ops, sizeof(*dl_priv), dev);
  1983. if (!common->devlink)
  1984. return -ENOMEM;
  1985. dl_priv = devlink_priv(common->devlink);
  1986. dl_priv->common = common;
  1987. /* Provide devlink hook to switch mode when multiple external ports
  1988. * are present NUSS switchdev driver is enabled.
  1989. */
  1990. if (!AM65_CPSW_IS_CPSW2G(common) &&
  1991. IS_ENABLED(CONFIG_TI_K3_AM65_CPSW_SWITCHDEV)) {
  1992. ret = devlink_params_register(common->devlink,
  1993. am65_cpsw_devlink_params,
  1994. ARRAY_SIZE(am65_cpsw_devlink_params));
  1995. if (ret) {
  1996. dev_err(dev, "devlink params reg fail ret:%d\n", ret);
  1997. goto dl_unreg;
  1998. }
  1999. }
  2000. for (i = 1; i <= common->port_num; i++) {
  2001. port = am65_common_get_port(common, i);
  2002. dl_port = &port->devlink_port;
  2003. if (port->ndev)
  2004. attrs.flavour = DEVLINK_PORT_FLAVOUR_PHYSICAL;
  2005. else
  2006. attrs.flavour = DEVLINK_PORT_FLAVOUR_UNUSED;
  2007. attrs.phys.port_number = port->port_id;
  2008. attrs.switch_id.id_len = sizeof(resource_size_t);
  2009. memcpy(attrs.switch_id.id, common->switch_id, attrs.switch_id.id_len);
  2010. devlink_port_attrs_set(dl_port, &attrs);
  2011. ret = devlink_port_register(common->devlink, dl_port, port->port_id);
  2012. if (ret) {
  2013. dev_err(dev, "devlink_port reg fail for port %d, ret:%d\n",
  2014. port->port_id, ret);
  2015. goto dl_port_unreg;
  2016. }
  2017. }
  2018. devlink_register(common->devlink);
  2019. return ret;
  2020. dl_port_unreg:
  2021. for (i = i - 1; i >= 1; i--) {
  2022. port = am65_common_get_port(common, i);
  2023. dl_port = &port->devlink_port;
  2024. devlink_port_unregister(dl_port);
  2025. }
  2026. dl_unreg:
  2027. devlink_free(common->devlink);
  2028. return ret;
  2029. }
  2030. static void am65_cpsw_unregister_devlink(struct am65_cpsw_common *common)
  2031. {
  2032. struct devlink_port *dl_port;
  2033. struct am65_cpsw_port *port;
  2034. int i;
  2035. devlink_unregister(common->devlink);
  2036. for (i = 1; i <= common->port_num; i++) {
  2037. port = am65_common_get_port(common, i);
  2038. dl_port = &port->devlink_port;
  2039. devlink_port_unregister(dl_port);
  2040. }
  2041. if (!AM65_CPSW_IS_CPSW2G(common) &&
  2042. IS_ENABLED(CONFIG_TI_K3_AM65_CPSW_SWITCHDEV))
  2043. devlink_params_unregister(common->devlink,
  2044. am65_cpsw_devlink_params,
  2045. ARRAY_SIZE(am65_cpsw_devlink_params));
  2046. devlink_free(common->devlink);
  2047. }
  2048. static int am65_cpsw_nuss_register_ndevs(struct am65_cpsw_common *common)
  2049. {
  2050. struct device *dev = common->dev;
  2051. struct devlink_port *dl_port;
  2052. struct am65_cpsw_port *port;
  2053. int ret = 0, i;
  2054. ret = am65_cpsw_nuss_ndev_add_tx_napi(common);
  2055. if (ret)
  2056. return ret;
  2057. ret = devm_request_irq(dev, common->rx_chns.irq,
  2058. am65_cpsw_nuss_rx_irq,
  2059. IRQF_TRIGGER_HIGH, dev_name(dev), common);
  2060. if (ret) {
  2061. dev_err(dev, "failure requesting rx irq %u, %d\n",
  2062. common->rx_chns.irq, ret);
  2063. return ret;
  2064. }
  2065. ret = am65_cpsw_nuss_register_devlink(common);
  2066. if (ret)
  2067. return ret;
  2068. for (i = 0; i < common->port_num; i++) {
  2069. port = &common->ports[i];
  2070. if (!port->ndev)
  2071. continue;
  2072. ret = register_netdev(port->ndev);
  2073. if (ret) {
  2074. dev_err(dev, "error registering slave net device%i %d\n",
  2075. i, ret);
  2076. goto err_cleanup_ndev;
  2077. }
  2078. dl_port = &port->devlink_port;
  2079. devlink_port_type_eth_set(dl_port, port->ndev);
  2080. }
  2081. ret = am65_cpsw_register_notifiers(common);
  2082. if (ret)
  2083. goto err_cleanup_ndev;
  2084. /* can't auto unregister ndev using devm_add_action() due to
  2085. * devres release sequence in DD core for DMA
  2086. */
  2087. return 0;
  2088. err_cleanup_ndev:
  2089. am65_cpsw_nuss_cleanup_ndev(common);
  2090. am65_cpsw_unregister_devlink(common);
  2091. return ret;
  2092. }
  2093. int am65_cpsw_nuss_update_tx_chns(struct am65_cpsw_common *common, int num_tx)
  2094. {
  2095. int ret;
  2096. common->tx_ch_num = num_tx;
  2097. ret = am65_cpsw_nuss_init_tx_chns(common);
  2098. if (ret)
  2099. return ret;
  2100. return am65_cpsw_nuss_ndev_add_tx_napi(common);
  2101. }
  2102. struct am65_cpsw_soc_pdata {
  2103. u32 quirks_dis;
  2104. };
  2105. static const struct am65_cpsw_soc_pdata am65x_soc_sr2_0 = {
  2106. .quirks_dis = AM65_CPSW_QUIRK_I2027_NO_TX_CSUM,
  2107. };
  2108. static const struct soc_device_attribute am65_cpsw_socinfo[] = {
  2109. { .family = "AM65X",
  2110. .revision = "SR2.0",
  2111. .data = &am65x_soc_sr2_0
  2112. },
  2113. {/* sentinel */}
  2114. };
  2115. static const struct am65_cpsw_pdata am65x_sr1_0 = {
  2116. .quirks = AM65_CPSW_QUIRK_I2027_NO_TX_CSUM,
  2117. .ale_dev_id = "am65x-cpsw2g",
  2118. .fdqring_mode = K3_RINGACC_RING_MODE_MESSAGE,
  2119. };
  2120. static const struct am65_cpsw_pdata j721e_pdata = {
  2121. .quirks = 0,
  2122. .ale_dev_id = "am65x-cpsw2g",
  2123. .fdqring_mode = K3_RINGACC_RING_MODE_MESSAGE,
  2124. };
  2125. static const struct am65_cpsw_pdata am64x_cpswxg_pdata = {
  2126. .quirks = AM64_CPSW_QUIRK_DMA_RX_TDOWN_IRQ,
  2127. .ale_dev_id = "am64-cpswxg",
  2128. .fdqring_mode = K3_RINGACC_RING_MODE_RING,
  2129. };
  2130. static const struct am65_cpsw_pdata j7200_cpswxg_pdata = {
  2131. .quirks = 0,
  2132. .ale_dev_id = "am64-cpswxg",
  2133. .fdqring_mode = K3_RINGACC_RING_MODE_RING,
  2134. .extra_modes = BIT(PHY_INTERFACE_MODE_QSGMII),
  2135. };
  2136. static const struct of_device_id am65_cpsw_nuss_of_mtable[] = {
  2137. { .compatible = "ti,am654-cpsw-nuss", .data = &am65x_sr1_0},
  2138. { .compatible = "ti,j721e-cpsw-nuss", .data = &j721e_pdata},
  2139. { .compatible = "ti,am642-cpsw-nuss", .data = &am64x_cpswxg_pdata},
  2140. { .compatible = "ti,j7200-cpswxg-nuss", .data = &j7200_cpswxg_pdata},
  2141. { /* sentinel */ },
  2142. };
  2143. MODULE_DEVICE_TABLE(of, am65_cpsw_nuss_of_mtable);
  2144. static void am65_cpsw_nuss_apply_socinfo(struct am65_cpsw_common *common)
  2145. {
  2146. const struct soc_device_attribute *soc;
  2147. soc = soc_device_match(am65_cpsw_socinfo);
  2148. if (soc && soc->data) {
  2149. const struct am65_cpsw_soc_pdata *socdata = soc->data;
  2150. /* disable quirks */
  2151. common->pdata.quirks &= ~socdata->quirks_dis;
  2152. }
  2153. }
  2154. static int am65_cpsw_nuss_probe(struct platform_device *pdev)
  2155. {
  2156. struct cpsw_ale_params ale_params = { 0 };
  2157. const struct of_device_id *of_id;
  2158. struct device *dev = &pdev->dev;
  2159. struct am65_cpsw_common *common;
  2160. struct device_node *node;
  2161. struct resource *res;
  2162. struct clk *clk;
  2163. u64 id_temp;
  2164. int ret, i;
  2165. common = devm_kzalloc(dev, sizeof(struct am65_cpsw_common), GFP_KERNEL);
  2166. if (!common)
  2167. return -ENOMEM;
  2168. common->dev = dev;
  2169. of_id = of_match_device(am65_cpsw_nuss_of_mtable, dev);
  2170. if (!of_id)
  2171. return -EINVAL;
  2172. common->pdata = *(const struct am65_cpsw_pdata *)of_id->data;
  2173. am65_cpsw_nuss_apply_socinfo(common);
  2174. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "cpsw_nuss");
  2175. common->ss_base = devm_ioremap_resource(&pdev->dev, res);
  2176. if (IS_ERR(common->ss_base))
  2177. return PTR_ERR(common->ss_base);
  2178. common->cpsw_base = common->ss_base + AM65_CPSW_CPSW_NU_BASE;
  2179. /* Use device's physical base address as switch id */
  2180. id_temp = cpu_to_be64(res->start);
  2181. memcpy(common->switch_id, &id_temp, sizeof(res->start));
  2182. node = of_get_child_by_name(dev->of_node, "ethernet-ports");
  2183. if (!node)
  2184. return -ENOENT;
  2185. common->port_num = of_get_child_count(node);
  2186. of_node_put(node);
  2187. if (common->port_num < 1 || common->port_num > AM65_CPSW_MAX_PORTS)
  2188. return -ENOENT;
  2189. common->rx_flow_id_base = -1;
  2190. init_completion(&common->tdown_complete);
  2191. common->tx_ch_num = 1;
  2192. common->pf_p0_rx_ptype_rrobin = false;
  2193. common->default_vlan = 1;
  2194. common->ports = devm_kcalloc(dev, common->port_num,
  2195. sizeof(*common->ports),
  2196. GFP_KERNEL);
  2197. if (!common->ports)
  2198. return -ENOMEM;
  2199. clk = devm_clk_get(dev, "fck");
  2200. if (IS_ERR(clk))
  2201. return dev_err_probe(dev, PTR_ERR(clk), "getting fck clock\n");
  2202. common->bus_freq = clk_get_rate(clk);
  2203. pm_runtime_enable(dev);
  2204. ret = pm_runtime_resume_and_get(dev);
  2205. if (ret < 0) {
  2206. pm_runtime_disable(dev);
  2207. return ret;
  2208. }
  2209. node = of_get_child_by_name(dev->of_node, "mdio");
  2210. if (!node) {
  2211. dev_warn(dev, "MDIO node not found\n");
  2212. } else if (of_device_is_available(node)) {
  2213. struct platform_device *mdio_pdev;
  2214. mdio_pdev = of_platform_device_create(node, NULL, dev);
  2215. if (!mdio_pdev) {
  2216. ret = -ENODEV;
  2217. goto err_pm_clear;
  2218. }
  2219. common->mdio_dev = &mdio_pdev->dev;
  2220. }
  2221. of_node_put(node);
  2222. am65_cpsw_nuss_get_ver(common);
  2223. /* init tx channels */
  2224. ret = am65_cpsw_nuss_init_tx_chns(common);
  2225. if (ret)
  2226. goto err_of_clear;
  2227. ret = am65_cpsw_nuss_init_rx_chns(common);
  2228. if (ret)
  2229. goto err_of_clear;
  2230. ret = am65_cpsw_nuss_init_host_p(common);
  2231. if (ret)
  2232. goto err_of_clear;
  2233. ret = am65_cpsw_nuss_init_slave_ports(common);
  2234. if (ret)
  2235. goto err_of_clear;
  2236. /* init common data */
  2237. ale_params.dev = dev;
  2238. ale_params.ale_ageout = AM65_CPSW_ALE_AGEOUT_DEFAULT;
  2239. ale_params.ale_ports = common->port_num + 1;
  2240. ale_params.ale_regs = common->cpsw_base + AM65_CPSW_NU_ALE_BASE;
  2241. ale_params.dev_id = common->pdata.ale_dev_id;
  2242. ale_params.bus_freq = common->bus_freq;
  2243. common->ale = cpsw_ale_create(&ale_params);
  2244. if (IS_ERR(common->ale)) {
  2245. dev_err(dev, "error initializing ale engine\n");
  2246. ret = PTR_ERR(common->ale);
  2247. goto err_of_clear;
  2248. }
  2249. ret = am65_cpsw_init_cpts(common);
  2250. if (ret)
  2251. goto err_of_clear;
  2252. /* init ports */
  2253. for (i = 0; i < common->port_num; i++)
  2254. am65_cpsw_nuss_slave_disable_unused(&common->ports[i]);
  2255. dev_set_drvdata(dev, common);
  2256. common->is_emac_mode = true;
  2257. ret = am65_cpsw_nuss_init_ndevs(common);
  2258. if (ret)
  2259. goto err_free_phylink;
  2260. ret = am65_cpsw_nuss_register_ndevs(common);
  2261. if (ret)
  2262. goto err_free_phylink;
  2263. pm_runtime_put(dev);
  2264. return 0;
  2265. err_free_phylink:
  2266. am65_cpsw_nuss_phylink_cleanup(common);
  2267. am65_cpts_release(common->cpts);
  2268. err_of_clear:
  2269. if (common->mdio_dev)
  2270. of_platform_device_destroy(common->mdio_dev, NULL);
  2271. err_pm_clear:
  2272. pm_runtime_put_sync(dev);
  2273. pm_runtime_disable(dev);
  2274. return ret;
  2275. }
  2276. static int am65_cpsw_nuss_remove(struct platform_device *pdev)
  2277. {
  2278. struct device *dev = &pdev->dev;
  2279. struct am65_cpsw_common *common;
  2280. int ret;
  2281. common = dev_get_drvdata(dev);
  2282. ret = pm_runtime_resume_and_get(&pdev->dev);
  2283. if (ret < 0)
  2284. return ret;
  2285. am65_cpsw_unregister_devlink(common);
  2286. am65_cpsw_unregister_notifiers(common);
  2287. /* must unregister ndevs here because DD release_driver routine calls
  2288. * dma_deconfigure(dev) before devres_release_all(dev)
  2289. */
  2290. am65_cpsw_nuss_cleanup_ndev(common);
  2291. am65_cpsw_nuss_phylink_cleanup(common);
  2292. am65_cpts_release(common->cpts);
  2293. if (common->mdio_dev)
  2294. of_platform_device_destroy(common->mdio_dev, NULL);
  2295. pm_runtime_put_sync(&pdev->dev);
  2296. pm_runtime_disable(&pdev->dev);
  2297. return 0;
  2298. }
  2299. static struct platform_driver am65_cpsw_nuss_driver = {
  2300. .driver = {
  2301. .name = AM65_CPSW_DRV_NAME,
  2302. .of_match_table = am65_cpsw_nuss_of_mtable,
  2303. },
  2304. .probe = am65_cpsw_nuss_probe,
  2305. .remove = am65_cpsw_nuss_remove,
  2306. };
  2307. module_platform_driver(am65_cpsw_nuss_driver);
  2308. MODULE_LICENSE("GPL v2");
  2309. MODULE_AUTHOR("Grygorii Strashko <[email protected]>");
  2310. MODULE_DESCRIPTION("TI AM65 CPSW Ethernet driver");