cpsw_new.c 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Texas Instruments Ethernet Switch Driver
  4. *
  5. * Copyright (C) 2019 Texas Instruments
  6. */
  7. #include <linux/io.h>
  8. #include <linux/clk.h>
  9. #include <linux/platform_device.h>
  10. #include <linux/timer.h>
  11. #include <linux/module.h>
  12. #include <linux/irqreturn.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/if_ether.h>
  15. #include <linux/etherdevice.h>
  16. #include <linux/net_tstamp.h>
  17. #include <linux/phy.h>
  18. #include <linux/phy/phy.h>
  19. #include <linux/delay.h>
  20. #include <linux/pinctrl/consumer.h>
  21. #include <linux/pm_runtime.h>
  22. #include <linux/gpio/consumer.h>
  23. #include <linux/of.h>
  24. #include <linux/of_mdio.h>
  25. #include <linux/of_net.h>
  26. #include <linux/of_platform.h>
  27. #include <linux/if_vlan.h>
  28. #include <linux/kmemleak.h>
  29. #include <linux/sys_soc.h>
  30. #include <net/switchdev.h>
  31. #include <net/page_pool/helpers.h>
  32. #include <net/pkt_cls.h>
  33. #include <net/devlink.h>
  34. #include "cpsw.h"
  35. #include "cpsw_ale.h"
  36. #include "cpsw_priv.h"
  37. #include "cpsw_sl.h"
  38. #include "cpsw_switchdev.h"
  39. #include "cpts.h"
  40. #include "davinci_cpdma.h"
  41. #include <net/pkt_sched.h>
  42. static int debug_level;
  43. static int ale_ageout = CPSW_ALE_AGEOUT_DEFAULT;
  44. static int rx_packet_max = CPSW_MAX_PACKET_SIZE;
  45. static int descs_pool_size = CPSW_CPDMA_DESCS_POOL_SIZE_DEFAULT;
  46. struct cpsw_devlink {
  47. struct cpsw_common *cpsw;
  48. };
  49. enum cpsw_devlink_param_id {
  50. CPSW_DEVLINK_PARAM_ID_BASE = DEVLINK_PARAM_GENERIC_ID_MAX,
  51. CPSW_DL_PARAM_SWITCH_MODE,
  52. CPSW_DL_PARAM_ALE_BYPASS,
  53. };
  54. /* struct cpsw_common is not needed, kept here for compatibility
  55. * reasons witrh the old driver
  56. */
  57. static int cpsw_slave_index_priv(struct cpsw_common *cpsw,
  58. struct cpsw_priv *priv)
  59. {
  60. if (priv->emac_port == HOST_PORT_NUM)
  61. return -1;
  62. return priv->emac_port - 1;
  63. }
  64. static bool cpsw_is_switch_en(struct cpsw_common *cpsw)
  65. {
  66. return !cpsw->data.dual_emac;
  67. }
  68. static void cpsw_set_promiscious(struct net_device *ndev, bool enable)
  69. {
  70. struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
  71. bool enable_uni = false;
  72. int i;
  73. if (cpsw_is_switch_en(cpsw))
  74. return;
  75. /* Enabling promiscuous mode for one interface will be
  76. * common for both the interface as the interface shares
  77. * the same hardware resource.
  78. */
  79. for (i = 0; i < cpsw->data.slaves; i++)
  80. if (cpsw->slaves[i].ndev &&
  81. (cpsw->slaves[i].ndev->flags & IFF_PROMISC))
  82. enable_uni = true;
  83. if (!enable && enable_uni) {
  84. enable = enable_uni;
  85. dev_dbg(cpsw->dev, "promiscuity not disabled as the other interface is still in promiscuity mode\n");
  86. }
  87. if (enable) {
  88. /* Enable unknown unicast, reg/unreg mcast */
  89. cpsw_ale_control_set(cpsw->ale, HOST_PORT_NUM,
  90. ALE_P0_UNI_FLOOD, 1);
  91. dev_dbg(cpsw->dev, "promiscuity enabled\n");
  92. } else {
  93. /* Disable unknown unicast */
  94. cpsw_ale_control_set(cpsw->ale, HOST_PORT_NUM,
  95. ALE_P0_UNI_FLOOD, 0);
  96. dev_dbg(cpsw->dev, "promiscuity disabled\n");
  97. }
  98. }
  99. /**
  100. * cpsw_set_mc - adds multicast entry to the table if it's not added or deletes
  101. * if it's not deleted
  102. * @ndev: device to sync
  103. * @addr: address to be added or deleted
  104. * @vid: vlan id, if vid < 0 set/unset address for real device
  105. * @add: add address if the flag is set or remove otherwise
  106. */
  107. static int cpsw_set_mc(struct net_device *ndev, const u8 *addr,
  108. int vid, int add)
  109. {
  110. struct cpsw_priv *priv = netdev_priv(ndev);
  111. struct cpsw_common *cpsw = priv->cpsw;
  112. int mask, flags, ret, slave_no;
  113. slave_no = cpsw_slave_index(cpsw, priv);
  114. if (vid < 0)
  115. vid = cpsw->slaves[slave_no].port_vlan;
  116. mask = ALE_PORT_HOST;
  117. flags = vid ? ALE_VLAN : 0;
  118. if (add)
  119. ret = cpsw_ale_add_mcast(cpsw->ale, addr, mask, flags, vid, 0);
  120. else
  121. ret = cpsw_ale_del_mcast(cpsw->ale, addr, 0, flags, vid);
  122. return ret;
  123. }
  124. static int cpsw_update_vlan_mc(struct net_device *vdev, int vid, void *ctx)
  125. {
  126. struct addr_sync_ctx *sync_ctx = ctx;
  127. struct netdev_hw_addr *ha;
  128. int found = 0, ret = 0;
  129. if (!vdev || !(vdev->flags & IFF_UP))
  130. return 0;
  131. /* vlan address is relevant if its sync_cnt != 0 */
  132. netdev_for_each_mc_addr(ha, vdev) {
  133. if (ether_addr_equal(ha->addr, sync_ctx->addr)) {
  134. found = ha->sync_cnt;
  135. break;
  136. }
  137. }
  138. if (found)
  139. sync_ctx->consumed++;
  140. if (sync_ctx->flush) {
  141. if (!found)
  142. cpsw_set_mc(sync_ctx->ndev, sync_ctx->addr, vid, 0);
  143. return 0;
  144. }
  145. if (found)
  146. ret = cpsw_set_mc(sync_ctx->ndev, sync_ctx->addr, vid, 1);
  147. return ret;
  148. }
  149. static int cpsw_add_mc_addr(struct net_device *ndev, const u8 *addr, int num)
  150. {
  151. struct addr_sync_ctx sync_ctx;
  152. int ret;
  153. sync_ctx.consumed = 0;
  154. sync_ctx.addr = addr;
  155. sync_ctx.ndev = ndev;
  156. sync_ctx.flush = 0;
  157. ret = vlan_for_each(ndev, cpsw_update_vlan_mc, &sync_ctx);
  158. if (sync_ctx.consumed < num && !ret)
  159. ret = cpsw_set_mc(ndev, addr, -1, 1);
  160. return ret;
  161. }
  162. static int cpsw_del_mc_addr(struct net_device *ndev, const u8 *addr, int num)
  163. {
  164. struct addr_sync_ctx sync_ctx;
  165. sync_ctx.consumed = 0;
  166. sync_ctx.addr = addr;
  167. sync_ctx.ndev = ndev;
  168. sync_ctx.flush = 1;
  169. vlan_for_each(ndev, cpsw_update_vlan_mc, &sync_ctx);
  170. if (sync_ctx.consumed == num)
  171. cpsw_set_mc(ndev, addr, -1, 0);
  172. return 0;
  173. }
  174. static int cpsw_purge_vlan_mc(struct net_device *vdev, int vid, void *ctx)
  175. {
  176. struct addr_sync_ctx *sync_ctx = ctx;
  177. struct netdev_hw_addr *ha;
  178. int found = 0;
  179. if (!vdev || !(vdev->flags & IFF_UP))
  180. return 0;
  181. /* vlan address is relevant if its sync_cnt != 0 */
  182. netdev_for_each_mc_addr(ha, vdev) {
  183. if (ether_addr_equal(ha->addr, sync_ctx->addr)) {
  184. found = ha->sync_cnt;
  185. break;
  186. }
  187. }
  188. if (!found)
  189. return 0;
  190. sync_ctx->consumed++;
  191. cpsw_set_mc(sync_ctx->ndev, sync_ctx->addr, vid, 0);
  192. return 0;
  193. }
  194. static int cpsw_purge_all_mc(struct net_device *ndev, const u8 *addr, int num)
  195. {
  196. struct addr_sync_ctx sync_ctx;
  197. sync_ctx.addr = addr;
  198. sync_ctx.ndev = ndev;
  199. sync_ctx.consumed = 0;
  200. vlan_for_each(ndev, cpsw_purge_vlan_mc, &sync_ctx);
  201. if (sync_ctx.consumed < num)
  202. cpsw_set_mc(ndev, addr, -1, 0);
  203. return 0;
  204. }
  205. static void cpsw_ndo_set_rx_mode_work(struct work_struct *work)
  206. {
  207. struct cpsw_priv *priv = container_of(work, struct cpsw_priv, rx_mode_work);
  208. struct cpsw_common *cpsw = priv->cpsw;
  209. struct net_device *ndev = priv->ndev;
  210. rtnl_lock();
  211. if (!netif_running(ndev))
  212. goto unlock_rtnl;
  213. netif_addr_lock_bh(ndev);
  214. if (ndev->flags & IFF_PROMISC) {
  215. /* Enable promiscuous mode */
  216. cpsw_set_promiscious(ndev, true);
  217. cpsw_ale_set_allmulti(cpsw->ale, IFF_ALLMULTI, priv->emac_port);
  218. goto unlock_addr;
  219. }
  220. /* Disable promiscuous mode */
  221. cpsw_set_promiscious(ndev, false);
  222. /* Restore allmulti on vlans if necessary */
  223. cpsw_ale_set_allmulti(cpsw->ale,
  224. ndev->flags & IFF_ALLMULTI, priv->emac_port);
  225. /* add/remove mcast address either for real netdev or for vlan */
  226. __hw_addr_ref_sync_dev(&ndev->mc, ndev, cpsw_add_mc_addr,
  227. cpsw_del_mc_addr);
  228. unlock_addr:
  229. netif_addr_unlock_bh(ndev);
  230. unlock_rtnl:
  231. rtnl_unlock();
  232. }
  233. static void cpsw_ndo_set_rx_mode(struct net_device *ndev)
  234. {
  235. struct cpsw_priv *priv = netdev_priv(ndev);
  236. schedule_work(&priv->rx_mode_work);
  237. }
  238. static unsigned int cpsw_rxbuf_total_len(unsigned int len)
  239. {
  240. len += CPSW_HEADROOM_NA;
  241. len += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
  242. return SKB_DATA_ALIGN(len);
  243. }
  244. static void cpsw_rx_handler(void *token, int len, int status)
  245. {
  246. struct page *new_page, *page = token;
  247. void *pa = page_address(page);
  248. int headroom = CPSW_HEADROOM_NA;
  249. struct cpsw_meta_xdp *xmeta;
  250. struct cpsw_common *cpsw;
  251. struct net_device *ndev;
  252. int port, ch, pkt_size;
  253. struct cpsw_priv *priv;
  254. struct page_pool *pool;
  255. struct sk_buff *skb;
  256. struct xdp_buff xdp;
  257. u32 metasize = 0;
  258. int ret = 0;
  259. dma_addr_t dma;
  260. xmeta = pa + CPSW_XMETA_OFFSET;
  261. cpsw = ndev_to_cpsw(xmeta->ndev);
  262. ndev = xmeta->ndev;
  263. pkt_size = cpsw->rx_packet_max;
  264. ch = xmeta->ch;
  265. if (status >= 0) {
  266. port = CPDMA_RX_SOURCE_PORT(status);
  267. if (port)
  268. ndev = cpsw->slaves[--port].ndev;
  269. }
  270. priv = netdev_priv(ndev);
  271. pool = cpsw->page_pool[ch];
  272. if (unlikely(status < 0) || unlikely(!netif_running(ndev))) {
  273. /* In dual emac mode check for all interfaces */
  274. if (cpsw->usage_count && status >= 0) {
  275. /* The packet received is for the interface which
  276. * is already down and the other interface is up
  277. * and running, instead of freeing which results
  278. * in reducing of the number of rx descriptor in
  279. * DMA engine, requeue page back to cpdma.
  280. */
  281. new_page = page;
  282. goto requeue;
  283. }
  284. /* the interface is going down, pages are purged */
  285. page_pool_recycle_direct(pool, page);
  286. return;
  287. }
  288. new_page = page_pool_dev_alloc_pages(pool);
  289. if (unlikely(!new_page)) {
  290. new_page = page;
  291. ndev->stats.rx_dropped++;
  292. goto requeue;
  293. }
  294. if (priv->xdp_prog) {
  295. int size = len;
  296. xdp_init_buff(&xdp, PAGE_SIZE, &priv->xdp_rxq[ch]);
  297. if (status & CPDMA_RX_VLAN_ENCAP) {
  298. headroom += CPSW_RX_VLAN_ENCAP_HDR_SIZE;
  299. size -= CPSW_RX_VLAN_ENCAP_HDR_SIZE;
  300. }
  301. xdp_prepare_buff(&xdp, pa, headroom, size, true);
  302. ret = cpsw_run_xdp(priv, ch, &xdp, page, priv->emac_port, &len);
  303. if (ret != CPSW_XDP_PASS)
  304. goto requeue;
  305. headroom = xdp.data - xdp.data_hard_start;
  306. metasize = xdp.data - xdp.data_meta;
  307. /* XDP prog can modify vlan tag, so can't use encap header */
  308. status &= ~CPDMA_RX_VLAN_ENCAP;
  309. }
  310. /* pass skb to netstack if no XDP prog or returned XDP_PASS */
  311. skb = build_skb(pa, cpsw_rxbuf_total_len(pkt_size));
  312. if (!skb) {
  313. ndev->stats.rx_dropped++;
  314. page_pool_recycle_direct(pool, page);
  315. goto requeue;
  316. }
  317. skb->offload_fwd_mark = priv->offload_fwd_mark;
  318. skb_reserve(skb, headroom);
  319. skb_put(skb, len);
  320. if (metasize)
  321. skb_metadata_set(skb, metasize);
  322. skb->dev = ndev;
  323. if (status & CPDMA_RX_VLAN_ENCAP)
  324. cpsw_rx_vlan_encap(skb);
  325. if (priv->rx_ts_enabled)
  326. cpts_rx_timestamp(cpsw->cpts, skb);
  327. skb->protocol = eth_type_trans(skb, ndev);
  328. /* mark skb for recycling */
  329. skb_mark_for_recycle(skb);
  330. netif_receive_skb(skb);
  331. ndev->stats.rx_bytes += len;
  332. ndev->stats.rx_packets++;
  333. requeue:
  334. xmeta = page_address(new_page) + CPSW_XMETA_OFFSET;
  335. xmeta->ndev = ndev;
  336. xmeta->ch = ch;
  337. dma = page_pool_get_dma_addr(new_page) + CPSW_HEADROOM_NA;
  338. ret = cpdma_chan_submit_mapped(cpsw->rxv[ch].ch, new_page, dma,
  339. pkt_size, 0);
  340. if (ret < 0) {
  341. WARN_ON(ret == -ENOMEM);
  342. page_pool_recycle_direct(pool, new_page);
  343. }
  344. }
  345. static int cpsw_add_vlan_ale_entry(struct cpsw_priv *priv,
  346. unsigned short vid)
  347. {
  348. struct cpsw_common *cpsw = priv->cpsw;
  349. int unreg_mcast_mask = 0;
  350. int mcast_mask;
  351. u32 port_mask;
  352. int ret;
  353. port_mask = (1 << priv->emac_port) | ALE_PORT_HOST;
  354. mcast_mask = ALE_PORT_HOST;
  355. if (priv->ndev->flags & IFF_ALLMULTI)
  356. unreg_mcast_mask = mcast_mask;
  357. ret = cpsw_ale_add_vlan(cpsw->ale, vid, port_mask, 0, port_mask,
  358. unreg_mcast_mask);
  359. if (ret != 0)
  360. return ret;
  361. ret = cpsw_ale_add_ucast(cpsw->ale, priv->mac_addr,
  362. HOST_PORT_NUM, ALE_VLAN, vid);
  363. if (ret != 0)
  364. goto clean_vid;
  365. ret = cpsw_ale_add_mcast(cpsw->ale, priv->ndev->broadcast,
  366. mcast_mask, ALE_VLAN, vid, 0);
  367. if (ret != 0)
  368. goto clean_vlan_ucast;
  369. return 0;
  370. clean_vlan_ucast:
  371. cpsw_ale_del_ucast(cpsw->ale, priv->mac_addr,
  372. HOST_PORT_NUM, ALE_VLAN, vid);
  373. clean_vid:
  374. cpsw_ale_del_vlan(cpsw->ale, vid, 0);
  375. return ret;
  376. }
  377. static int cpsw_ndo_vlan_rx_add_vid(struct net_device *ndev,
  378. __be16 proto, u16 vid)
  379. {
  380. struct cpsw_priv *priv = netdev_priv(ndev);
  381. struct cpsw_common *cpsw = priv->cpsw;
  382. int ret, i;
  383. if (cpsw_is_switch_en(cpsw)) {
  384. dev_dbg(cpsw->dev, ".ndo_vlan_rx_add_vid called in switch mode\n");
  385. return 0;
  386. }
  387. if (vid == cpsw->data.default_vlan)
  388. return 0;
  389. ret = pm_runtime_resume_and_get(cpsw->dev);
  390. if (ret < 0)
  391. return ret;
  392. /* In dual EMAC, reserved VLAN id should not be used for
  393. * creating VLAN interfaces as this can break the dual
  394. * EMAC port separation
  395. */
  396. for (i = 0; i < cpsw->data.slaves; i++) {
  397. if (cpsw->slaves[i].ndev &&
  398. vid == cpsw->slaves[i].port_vlan) {
  399. ret = -EINVAL;
  400. goto err;
  401. }
  402. }
  403. dev_dbg(priv->dev, "Adding vlanid %d to vlan filter\n", vid);
  404. ret = cpsw_add_vlan_ale_entry(priv, vid);
  405. err:
  406. pm_runtime_put(cpsw->dev);
  407. return ret;
  408. }
  409. static int cpsw_restore_vlans(struct net_device *vdev, int vid, void *arg)
  410. {
  411. struct cpsw_priv *priv = arg;
  412. if (!vdev || !vid)
  413. return 0;
  414. cpsw_ndo_vlan_rx_add_vid(priv->ndev, 0, vid);
  415. return 0;
  416. }
  417. /* restore resources after port reset */
  418. static void cpsw_restore(struct cpsw_priv *priv)
  419. {
  420. struct cpsw_common *cpsw = priv->cpsw;
  421. /* restore vlan configurations */
  422. vlan_for_each(priv->ndev, cpsw_restore_vlans, priv);
  423. /* restore MQPRIO offload */
  424. cpsw_mqprio_resume(&cpsw->slaves[priv->emac_port - 1], priv);
  425. /* restore CBS offload */
  426. cpsw_cbs_resume(&cpsw->slaves[priv->emac_port - 1], priv);
  427. cpsw_qos_clsflower_resume(priv);
  428. }
  429. static void cpsw_init_stp_ale_entry(struct cpsw_common *cpsw)
  430. {
  431. static const char stpa[] = {0x01, 0x80, 0xc2, 0x0, 0x0, 0x0};
  432. cpsw_ale_add_mcast(cpsw->ale, stpa,
  433. ALE_PORT_HOST, ALE_SUPER, 0,
  434. ALE_MCAST_BLOCK_LEARN_FWD);
  435. }
  436. static void cpsw_init_host_port_switch(struct cpsw_common *cpsw)
  437. {
  438. int vlan = cpsw->data.default_vlan;
  439. writel(CPSW_FIFO_NORMAL_MODE, &cpsw->host_port_regs->tx_in_ctl);
  440. writel(vlan, &cpsw->host_port_regs->port_vlan);
  441. cpsw_ale_add_vlan(cpsw->ale, vlan, ALE_ALL_PORTS,
  442. ALE_ALL_PORTS, ALE_ALL_PORTS,
  443. ALE_PORT_1 | ALE_PORT_2);
  444. cpsw_init_stp_ale_entry(cpsw);
  445. cpsw_ale_control_set(cpsw->ale, HOST_PORT_NUM, ALE_P0_UNI_FLOOD, 1);
  446. dev_dbg(cpsw->dev, "Set P0_UNI_FLOOD\n");
  447. cpsw_ale_control_set(cpsw->ale, HOST_PORT_NUM, ALE_PORT_NOLEARN, 0);
  448. }
  449. static void cpsw_init_host_port_dual_mac(struct cpsw_common *cpsw)
  450. {
  451. int vlan = cpsw->data.default_vlan;
  452. writel(CPSW_FIFO_DUAL_MAC_MODE, &cpsw->host_port_regs->tx_in_ctl);
  453. cpsw_ale_control_set(cpsw->ale, HOST_PORT_NUM, ALE_P0_UNI_FLOOD, 0);
  454. dev_dbg(cpsw->dev, "unset P0_UNI_FLOOD\n");
  455. writel(vlan, &cpsw->host_port_regs->port_vlan);
  456. cpsw_ale_add_vlan(cpsw->ale, vlan, ALE_ALL_PORTS, ALE_ALL_PORTS, 0, 0);
  457. /* learning make no sense in dual_mac mode */
  458. cpsw_ale_control_set(cpsw->ale, HOST_PORT_NUM, ALE_PORT_NOLEARN, 1);
  459. }
  460. static void cpsw_init_host_port(struct cpsw_priv *priv)
  461. {
  462. struct cpsw_common *cpsw = priv->cpsw;
  463. u32 control_reg;
  464. /* soft reset the controller and initialize ale */
  465. soft_reset("cpsw", &cpsw->regs->soft_reset);
  466. cpsw_ale_start(cpsw->ale);
  467. /* switch to vlan aware mode */
  468. cpsw_ale_control_set(cpsw->ale, HOST_PORT_NUM, ALE_VLAN_AWARE,
  469. CPSW_ALE_VLAN_AWARE);
  470. control_reg = readl(&cpsw->regs->control);
  471. control_reg |= CPSW_VLAN_AWARE | CPSW_RX_VLAN_ENCAP;
  472. writel(control_reg, &cpsw->regs->control);
  473. /* setup host port priority mapping */
  474. writel_relaxed(CPDMA_TX_PRIORITY_MAP,
  475. &cpsw->host_port_regs->cpdma_tx_pri_map);
  476. writel_relaxed(0, &cpsw->host_port_regs->cpdma_rx_chan_map);
  477. /* disable priority elevation */
  478. writel_relaxed(0, &cpsw->regs->ptype);
  479. /* enable statistics collection only on all ports */
  480. writel_relaxed(0x7, &cpsw->regs->stat_port_en);
  481. /* Enable internal fifo flow control */
  482. writel(0x7, &cpsw->regs->flow_control);
  483. if (cpsw_is_switch_en(cpsw))
  484. cpsw_init_host_port_switch(cpsw);
  485. else
  486. cpsw_init_host_port_dual_mac(cpsw);
  487. cpsw_ale_control_set(cpsw->ale, HOST_PORT_NUM,
  488. ALE_PORT_STATE, ALE_PORT_STATE_FORWARD);
  489. }
  490. static void cpsw_port_add_dual_emac_def_ale_entries(struct cpsw_priv *priv,
  491. struct cpsw_slave *slave)
  492. {
  493. u32 port_mask = 1 << priv->emac_port | ALE_PORT_HOST;
  494. struct cpsw_common *cpsw = priv->cpsw;
  495. u32 reg;
  496. reg = (cpsw->version == CPSW_VERSION_1) ? CPSW1_PORT_VLAN :
  497. CPSW2_PORT_VLAN;
  498. slave_write(slave, slave->port_vlan, reg);
  499. cpsw_ale_add_vlan(cpsw->ale, slave->port_vlan, port_mask,
  500. port_mask, port_mask, 0);
  501. cpsw_ale_add_mcast(cpsw->ale, priv->ndev->broadcast,
  502. ALE_PORT_HOST, ALE_VLAN, slave->port_vlan,
  503. ALE_MCAST_FWD);
  504. cpsw_ale_add_ucast(cpsw->ale, priv->mac_addr,
  505. HOST_PORT_NUM, ALE_VLAN |
  506. ALE_SECURE, slave->port_vlan);
  507. cpsw_ale_control_set(cpsw->ale, priv->emac_port,
  508. ALE_PORT_DROP_UNKNOWN_VLAN, 1);
  509. /* learning make no sense in dual_mac mode */
  510. cpsw_ale_control_set(cpsw->ale, priv->emac_port,
  511. ALE_PORT_NOLEARN, 1);
  512. }
  513. static void cpsw_port_add_switch_def_ale_entries(struct cpsw_priv *priv,
  514. struct cpsw_slave *slave)
  515. {
  516. u32 port_mask = 1 << priv->emac_port | ALE_PORT_HOST;
  517. struct cpsw_common *cpsw = priv->cpsw;
  518. u32 reg;
  519. cpsw_ale_control_set(cpsw->ale, priv->emac_port,
  520. ALE_PORT_DROP_UNKNOWN_VLAN, 0);
  521. cpsw_ale_control_set(cpsw->ale, priv->emac_port,
  522. ALE_PORT_NOLEARN, 0);
  523. /* disabling SA_UPDATE required to make stp work, without this setting
  524. * Host MAC addresses will jump between ports.
  525. * As per TRM MAC address can be defined as unicast supervisory (super)
  526. * by setting both (ALE_BLOCKED | ALE_SECURE) which should prevent
  527. * SA_UPDATE, but HW seems works incorrectly and setting ALE_SECURE
  528. * causes STP packets to be dropped due to ingress filter
  529. * if (source address found) and (secure) and
  530. * (receive port number != port_number))
  531. * then discard the packet
  532. */
  533. cpsw_ale_control_set(cpsw->ale, priv->emac_port,
  534. ALE_PORT_NO_SA_UPDATE, 1);
  535. cpsw_ale_add_mcast(cpsw->ale, priv->ndev->broadcast,
  536. port_mask, ALE_VLAN, slave->port_vlan,
  537. ALE_MCAST_FWD_2);
  538. cpsw_ale_add_ucast(cpsw->ale, priv->mac_addr,
  539. HOST_PORT_NUM, ALE_VLAN, slave->port_vlan);
  540. reg = (cpsw->version == CPSW_VERSION_1) ? CPSW1_PORT_VLAN :
  541. CPSW2_PORT_VLAN;
  542. slave_write(slave, slave->port_vlan, reg);
  543. }
  544. static void cpsw_adjust_link(struct net_device *ndev)
  545. {
  546. struct cpsw_priv *priv = netdev_priv(ndev);
  547. struct cpsw_common *cpsw = priv->cpsw;
  548. struct cpsw_slave *slave;
  549. struct phy_device *phy;
  550. u32 mac_control = 0;
  551. slave = &cpsw->slaves[priv->emac_port - 1];
  552. phy = slave->phy;
  553. if (!phy)
  554. return;
  555. if (phy->link) {
  556. mac_control = CPSW_SL_CTL_GMII_EN;
  557. if (phy->speed == 1000)
  558. mac_control |= CPSW_SL_CTL_GIG;
  559. if (phy->duplex)
  560. mac_control |= CPSW_SL_CTL_FULLDUPLEX;
  561. /* set speed_in input in case RMII mode is used in 100Mbps */
  562. if (phy->speed == 100)
  563. mac_control |= CPSW_SL_CTL_IFCTL_A;
  564. /* in band mode only works in 10Mbps RGMII mode */
  565. else if ((phy->speed == 10) && phy_interface_is_rgmii(phy))
  566. mac_control |= CPSW_SL_CTL_EXT_EN; /* In Band mode */
  567. if (priv->rx_pause)
  568. mac_control |= CPSW_SL_CTL_RX_FLOW_EN;
  569. if (priv->tx_pause)
  570. mac_control |= CPSW_SL_CTL_TX_FLOW_EN;
  571. if (mac_control != slave->mac_control)
  572. cpsw_sl_ctl_set(slave->mac_sl, mac_control);
  573. /* enable forwarding */
  574. cpsw_ale_control_set(cpsw->ale, priv->emac_port,
  575. ALE_PORT_STATE, ALE_PORT_STATE_FORWARD);
  576. netif_tx_wake_all_queues(ndev);
  577. if (priv->shp_cfg_speed &&
  578. priv->shp_cfg_speed != slave->phy->speed &&
  579. !cpsw_shp_is_off(priv))
  580. dev_warn(priv->dev, "Speed was changed, CBS shaper speeds are changed!");
  581. } else {
  582. netif_tx_stop_all_queues(ndev);
  583. mac_control = 0;
  584. /* disable forwarding */
  585. cpsw_ale_control_set(cpsw->ale, priv->emac_port,
  586. ALE_PORT_STATE, ALE_PORT_STATE_DISABLE);
  587. cpsw_sl_wait_for_idle(slave->mac_sl, 100);
  588. cpsw_sl_ctl_reset(slave->mac_sl);
  589. }
  590. if (mac_control != slave->mac_control)
  591. phy_print_status(phy);
  592. slave->mac_control = mac_control;
  593. if (phy->link && cpsw_need_resplit(cpsw))
  594. cpsw_split_res(cpsw);
  595. }
  596. static void cpsw_slave_open(struct cpsw_slave *slave, struct cpsw_priv *priv)
  597. {
  598. struct cpsw_common *cpsw = priv->cpsw;
  599. struct phy_device *phy;
  600. cpsw_sl_reset(slave->mac_sl, 100);
  601. cpsw_sl_ctl_reset(slave->mac_sl);
  602. /* setup priority mapping */
  603. cpsw_sl_reg_write(slave->mac_sl, CPSW_SL_RX_PRI_MAP,
  604. RX_PRIORITY_MAPPING);
  605. switch (cpsw->version) {
  606. case CPSW_VERSION_1:
  607. slave_write(slave, TX_PRIORITY_MAPPING, CPSW1_TX_PRI_MAP);
  608. /* Increase RX FIFO size to 5 for supporting fullduplex
  609. * flow control mode
  610. */
  611. slave_write(slave,
  612. (CPSW_MAX_BLKS_TX << CPSW_MAX_BLKS_TX_SHIFT) |
  613. CPSW_MAX_BLKS_RX, CPSW1_MAX_BLKS);
  614. break;
  615. case CPSW_VERSION_2:
  616. case CPSW_VERSION_3:
  617. case CPSW_VERSION_4:
  618. slave_write(slave, TX_PRIORITY_MAPPING, CPSW2_TX_PRI_MAP);
  619. /* Increase RX FIFO size to 5 for supporting fullduplex
  620. * flow control mode
  621. */
  622. slave_write(slave,
  623. (CPSW_MAX_BLKS_TX << CPSW_MAX_BLKS_TX_SHIFT) |
  624. CPSW_MAX_BLKS_RX, CPSW2_MAX_BLKS);
  625. break;
  626. }
  627. /* setup max packet size, and mac address */
  628. cpsw_sl_reg_write(slave->mac_sl, CPSW_SL_RX_MAXLEN,
  629. cpsw->rx_packet_max);
  630. cpsw_set_slave_mac(slave, priv);
  631. slave->mac_control = 0; /* no link yet */
  632. if (cpsw_is_switch_en(cpsw))
  633. cpsw_port_add_switch_def_ale_entries(priv, slave);
  634. else
  635. cpsw_port_add_dual_emac_def_ale_entries(priv, slave);
  636. if (!slave->data->phy_node)
  637. dev_err(priv->dev, "no phy found on slave %d\n",
  638. slave->slave_num);
  639. phy = of_phy_connect(priv->ndev, slave->data->phy_node,
  640. &cpsw_adjust_link, 0, slave->data->phy_if);
  641. if (!phy) {
  642. dev_err(priv->dev, "phy \"%pOF\" not found on slave %d\n",
  643. slave->data->phy_node,
  644. slave->slave_num);
  645. return;
  646. }
  647. phy->mac_managed_pm = true;
  648. slave->phy = phy;
  649. phy_disable_eee(slave->phy);
  650. phy_attached_info(slave->phy);
  651. phy_start(slave->phy);
  652. /* Configure GMII_SEL register */
  653. phy_set_mode_ext(slave->data->ifphy, PHY_MODE_ETHERNET,
  654. slave->data->phy_if);
  655. }
  656. static int cpsw_ndo_stop(struct net_device *ndev)
  657. {
  658. struct cpsw_priv *priv = netdev_priv(ndev);
  659. struct cpsw_common *cpsw = priv->cpsw;
  660. struct cpsw_slave *slave;
  661. cpsw_info(priv, ifdown, "shutting down ndev\n");
  662. slave = &cpsw->slaves[priv->emac_port - 1];
  663. if (slave->phy)
  664. phy_stop(slave->phy);
  665. netif_tx_stop_all_queues(priv->ndev);
  666. if (slave->phy) {
  667. phy_disconnect(slave->phy);
  668. slave->phy = NULL;
  669. }
  670. __hw_addr_ref_unsync_dev(&ndev->mc, ndev, cpsw_purge_all_mc);
  671. if (cpsw->usage_count <= 1) {
  672. napi_disable(&cpsw->napi_rx);
  673. napi_disable(&cpsw->napi_tx);
  674. cpts_unregister(cpsw->cpts);
  675. cpsw_intr_disable(cpsw);
  676. cpdma_ctlr_stop(cpsw->dma);
  677. cpsw_ale_stop(cpsw->ale);
  678. cpsw_destroy_xdp_rxqs(cpsw);
  679. }
  680. if (cpsw_need_resplit(cpsw))
  681. cpsw_split_res(cpsw);
  682. cpsw->usage_count--;
  683. pm_runtime_put_sync(cpsw->dev);
  684. return 0;
  685. }
  686. static int cpsw_ndo_open(struct net_device *ndev)
  687. {
  688. struct cpsw_priv *priv = netdev_priv(ndev);
  689. struct cpsw_common *cpsw = priv->cpsw;
  690. int ret;
  691. dev_info(priv->dev, "starting ndev. mode: %s\n",
  692. cpsw_is_switch_en(cpsw) ? "switch" : "dual_mac");
  693. ret = pm_runtime_resume_and_get(cpsw->dev);
  694. if (ret < 0)
  695. return ret;
  696. /* Notify the stack of the actual queue counts. */
  697. ret = netif_set_real_num_tx_queues(ndev, cpsw->tx_ch_num);
  698. if (ret) {
  699. dev_err(priv->dev, "cannot set real number of tx queues\n");
  700. goto pm_cleanup;
  701. }
  702. ret = netif_set_real_num_rx_queues(ndev, cpsw->rx_ch_num);
  703. if (ret) {
  704. dev_err(priv->dev, "cannot set real number of rx queues\n");
  705. goto pm_cleanup;
  706. }
  707. /* Initialize host and slave ports */
  708. if (!cpsw->usage_count)
  709. cpsw_init_host_port(priv);
  710. cpsw_slave_open(&cpsw->slaves[priv->emac_port - 1], priv);
  711. /* initialize shared resources for every ndev */
  712. if (!cpsw->usage_count) {
  713. /* create rxqs for both infs in dual mac as they use same pool
  714. * and must be destroyed together when no users.
  715. */
  716. ret = cpsw_create_xdp_rxqs(cpsw);
  717. if (ret < 0)
  718. goto err_cleanup;
  719. ret = cpsw_fill_rx_channels(priv);
  720. if (ret < 0)
  721. goto err_cleanup;
  722. if (cpsw->cpts) {
  723. if (cpts_register(cpsw->cpts))
  724. dev_err(priv->dev, "error registering cpts device\n");
  725. else
  726. writel(0x10, &cpsw->wr_regs->misc_en);
  727. }
  728. napi_enable(&cpsw->napi_rx);
  729. napi_enable(&cpsw->napi_tx);
  730. if (cpsw->tx_irq_disabled) {
  731. cpsw->tx_irq_disabled = false;
  732. enable_irq(cpsw->irqs_table[1]);
  733. }
  734. if (cpsw->rx_irq_disabled) {
  735. cpsw->rx_irq_disabled = false;
  736. enable_irq(cpsw->irqs_table[0]);
  737. }
  738. }
  739. cpsw_restore(priv);
  740. /* Enable Interrupt pacing if configured */
  741. if (cpsw->coal_intvl != 0) {
  742. struct ethtool_coalesce coal;
  743. coal.rx_coalesce_usecs = cpsw->coal_intvl;
  744. cpsw_set_coalesce(ndev, &coal, NULL, NULL);
  745. }
  746. cpdma_ctlr_start(cpsw->dma);
  747. cpsw_intr_enable(cpsw);
  748. cpsw->usage_count++;
  749. return 0;
  750. err_cleanup:
  751. cpsw_ndo_stop(ndev);
  752. pm_cleanup:
  753. pm_runtime_put_sync(cpsw->dev);
  754. return ret;
  755. }
  756. static netdev_tx_t cpsw_ndo_start_xmit(struct sk_buff *skb,
  757. struct net_device *ndev)
  758. {
  759. struct cpsw_priv *priv = netdev_priv(ndev);
  760. struct cpsw_common *cpsw = priv->cpsw;
  761. struct cpts *cpts = cpsw->cpts;
  762. struct netdev_queue *txq;
  763. struct cpdma_chan *txch;
  764. int ret, q_idx;
  765. if (skb_put_padto(skb, READ_ONCE(priv->tx_packet_min))) {
  766. cpsw_err(priv, tx_err, "packet pad failed\n");
  767. ndev->stats.tx_dropped++;
  768. return NET_XMIT_DROP;
  769. }
  770. if (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP &&
  771. priv->tx_ts_enabled && cpts_can_timestamp(cpts, skb))
  772. skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
  773. q_idx = skb_get_queue_mapping(skb);
  774. if (q_idx >= cpsw->tx_ch_num)
  775. q_idx = q_idx % cpsw->tx_ch_num;
  776. txch = cpsw->txv[q_idx].ch;
  777. txq = netdev_get_tx_queue(ndev, q_idx);
  778. skb_tx_timestamp(skb);
  779. ret = cpdma_chan_submit(txch, skb, skb->data, skb->len,
  780. priv->emac_port);
  781. if (unlikely(ret != 0)) {
  782. cpsw_err(priv, tx_err, "desc submit failed\n");
  783. goto fail;
  784. }
  785. /* If there is no more tx desc left free then we need to
  786. * tell the kernel to stop sending us tx frames.
  787. */
  788. if (unlikely(!cpdma_check_free_tx_desc(txch))) {
  789. netif_tx_stop_queue(txq);
  790. /* Barrier, so that stop_queue visible to other cpus */
  791. smp_mb__after_atomic();
  792. if (cpdma_check_free_tx_desc(txch))
  793. netif_tx_wake_queue(txq);
  794. }
  795. return NETDEV_TX_OK;
  796. fail:
  797. ndev->stats.tx_dropped++;
  798. netif_tx_stop_queue(txq);
  799. /* Barrier, so that stop_queue visible to other cpus */
  800. smp_mb__after_atomic();
  801. if (cpdma_check_free_tx_desc(txch))
  802. netif_tx_wake_queue(txq);
  803. return NETDEV_TX_BUSY;
  804. }
  805. static int cpsw_ndo_set_mac_address(struct net_device *ndev, void *p)
  806. {
  807. struct sockaddr *addr = (struct sockaddr *)p;
  808. struct cpsw_priv *priv = netdev_priv(ndev);
  809. struct cpsw_common *cpsw = priv->cpsw;
  810. int ret, slave_no;
  811. int flags = 0;
  812. u16 vid = 0;
  813. slave_no = cpsw_slave_index(cpsw, priv);
  814. if (!is_valid_ether_addr(addr->sa_data))
  815. return -EADDRNOTAVAIL;
  816. ret = pm_runtime_resume_and_get(cpsw->dev);
  817. if (ret < 0)
  818. return ret;
  819. vid = cpsw->slaves[slave_no].port_vlan;
  820. flags = ALE_VLAN | ALE_SECURE;
  821. cpsw_ale_del_ucast(cpsw->ale, priv->mac_addr, HOST_PORT_NUM,
  822. flags, vid);
  823. cpsw_ale_add_ucast(cpsw->ale, addr->sa_data, HOST_PORT_NUM,
  824. flags, vid);
  825. ether_addr_copy(priv->mac_addr, addr->sa_data);
  826. eth_hw_addr_set(ndev, priv->mac_addr);
  827. cpsw_set_slave_mac(&cpsw->slaves[slave_no], priv);
  828. pm_runtime_put(cpsw->dev);
  829. return 0;
  830. }
  831. static int cpsw_ndo_vlan_rx_kill_vid(struct net_device *ndev,
  832. __be16 proto, u16 vid)
  833. {
  834. struct cpsw_priv *priv = netdev_priv(ndev);
  835. struct cpsw_common *cpsw = priv->cpsw;
  836. int ret;
  837. int i;
  838. if (cpsw_is_switch_en(cpsw)) {
  839. dev_dbg(cpsw->dev, "ndo del vlan is called in switch mode\n");
  840. return 0;
  841. }
  842. if (vid == cpsw->data.default_vlan)
  843. return 0;
  844. ret = pm_runtime_resume_and_get(cpsw->dev);
  845. if (ret < 0)
  846. return ret;
  847. /* reset the return code as pm_runtime_get_sync() can return
  848. * non zero values as well.
  849. */
  850. ret = 0;
  851. for (i = 0; i < cpsw->data.slaves; i++) {
  852. if (cpsw->slaves[i].ndev &&
  853. vid == cpsw->slaves[i].port_vlan) {
  854. ret = -EINVAL;
  855. goto err;
  856. }
  857. }
  858. dev_dbg(priv->dev, "removing vlanid %d from vlan filter\n", vid);
  859. ret = cpsw_ale_del_vlan(cpsw->ale, vid, 0);
  860. if (ret)
  861. dev_err(priv->dev, "cpsw_ale_del_vlan() failed: ret %d\n", ret);
  862. ret = cpsw_ale_del_ucast(cpsw->ale, priv->mac_addr,
  863. HOST_PORT_NUM, ALE_VLAN, vid);
  864. if (ret)
  865. dev_err(priv->dev, "cpsw_ale_del_ucast() failed: ret %d\n",
  866. ret);
  867. ret = cpsw_ale_del_mcast(cpsw->ale, priv->ndev->broadcast,
  868. 0, ALE_VLAN, vid);
  869. if (ret)
  870. dev_err(priv->dev, "cpsw_ale_del_mcast failed. ret %d\n",
  871. ret);
  872. cpsw_ale_flush_multicast(cpsw->ale, ALE_PORT_HOST, vid);
  873. ret = 0;
  874. err:
  875. pm_runtime_put(cpsw->dev);
  876. return ret;
  877. }
  878. static int cpsw_ndo_get_phys_port_name(struct net_device *ndev, char *name,
  879. size_t len)
  880. {
  881. struct cpsw_priv *priv = netdev_priv(ndev);
  882. int err;
  883. err = snprintf(name, len, "p%d", priv->emac_port);
  884. if (err >= len)
  885. return -EINVAL;
  886. return 0;
  887. }
  888. #ifdef CONFIG_NET_POLL_CONTROLLER
  889. static void cpsw_ndo_poll_controller(struct net_device *ndev)
  890. {
  891. struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
  892. cpsw_intr_disable(cpsw);
  893. cpsw_rx_interrupt(cpsw->irqs_table[0], cpsw);
  894. cpsw_tx_interrupt(cpsw->irqs_table[1], cpsw);
  895. cpsw_intr_enable(cpsw);
  896. }
  897. #endif
  898. static int cpsw_ndo_xdp_xmit(struct net_device *ndev, int n,
  899. struct xdp_frame **frames, u32 flags)
  900. {
  901. struct cpsw_priv *priv = netdev_priv(ndev);
  902. struct xdp_frame *xdpf;
  903. int i, nxmit = 0;
  904. if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK))
  905. return -EINVAL;
  906. for (i = 0; i < n; i++) {
  907. xdpf = frames[i];
  908. if (xdpf->len < READ_ONCE(priv->tx_packet_min))
  909. break;
  910. if (cpsw_xdp_tx_frame(priv, xdpf, NULL, priv->emac_port))
  911. break;
  912. nxmit++;
  913. }
  914. return nxmit;
  915. }
  916. static int cpsw_get_port_parent_id(struct net_device *ndev,
  917. struct netdev_phys_item_id *ppid)
  918. {
  919. struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
  920. ppid->id_len = sizeof(cpsw->base_mac);
  921. memcpy(&ppid->id, &cpsw->base_mac, ppid->id_len);
  922. return 0;
  923. }
  924. static const struct net_device_ops cpsw_netdev_ops = {
  925. .ndo_open = cpsw_ndo_open,
  926. .ndo_stop = cpsw_ndo_stop,
  927. .ndo_start_xmit = cpsw_ndo_start_xmit,
  928. .ndo_set_mac_address = cpsw_ndo_set_mac_address,
  929. .ndo_eth_ioctl = phy_do_ioctl_running,
  930. .ndo_validate_addr = eth_validate_addr,
  931. .ndo_tx_timeout = cpsw_ndo_tx_timeout,
  932. .ndo_set_rx_mode = cpsw_ndo_set_rx_mode,
  933. .ndo_set_tx_maxrate = cpsw_ndo_set_tx_maxrate,
  934. #ifdef CONFIG_NET_POLL_CONTROLLER
  935. .ndo_poll_controller = cpsw_ndo_poll_controller,
  936. #endif
  937. .ndo_vlan_rx_add_vid = cpsw_ndo_vlan_rx_add_vid,
  938. .ndo_vlan_rx_kill_vid = cpsw_ndo_vlan_rx_kill_vid,
  939. .ndo_setup_tc = cpsw_ndo_setup_tc,
  940. .ndo_get_phys_port_name = cpsw_ndo_get_phys_port_name,
  941. .ndo_bpf = cpsw_ndo_bpf,
  942. .ndo_xdp_xmit = cpsw_ndo_xdp_xmit,
  943. .ndo_get_port_parent_id = cpsw_get_port_parent_id,
  944. .ndo_hwtstamp_get = cpsw_hwtstamp_get,
  945. .ndo_hwtstamp_set = cpsw_hwtstamp_set,
  946. };
  947. static void cpsw_get_drvinfo(struct net_device *ndev,
  948. struct ethtool_drvinfo *info)
  949. {
  950. struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
  951. struct platform_device *pdev;
  952. pdev = to_platform_device(cpsw->dev);
  953. strscpy(info->driver, "cpsw-switch", sizeof(info->driver));
  954. strscpy(info->version, "2.0", sizeof(info->version));
  955. strscpy(info->bus_info, pdev->name, sizeof(info->bus_info));
  956. }
  957. static int cpsw_set_pauseparam(struct net_device *ndev,
  958. struct ethtool_pauseparam *pause)
  959. {
  960. struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
  961. struct cpsw_priv *priv = netdev_priv(ndev);
  962. int slave_no;
  963. slave_no = cpsw_slave_index(cpsw, priv);
  964. if (!cpsw->slaves[slave_no].phy)
  965. return -EINVAL;
  966. if (!phy_validate_pause(cpsw->slaves[slave_no].phy, pause))
  967. return -EINVAL;
  968. priv->rx_pause = pause->rx_pause ? true : false;
  969. priv->tx_pause = pause->tx_pause ? true : false;
  970. phy_set_asym_pause(cpsw->slaves[slave_no].phy,
  971. priv->rx_pause, priv->tx_pause);
  972. return 0;
  973. }
  974. static int cpsw_set_channels(struct net_device *ndev,
  975. struct ethtool_channels *chs)
  976. {
  977. return cpsw_set_channels_common(ndev, chs, cpsw_rx_handler);
  978. }
  979. static const struct ethtool_ops cpsw_ethtool_ops = {
  980. .supported_coalesce_params = ETHTOOL_COALESCE_RX_USECS,
  981. .get_drvinfo = cpsw_get_drvinfo,
  982. .get_msglevel = cpsw_get_msglevel,
  983. .set_msglevel = cpsw_set_msglevel,
  984. .get_link = ethtool_op_get_link,
  985. .get_ts_info = cpsw_get_ts_info,
  986. .get_coalesce = cpsw_get_coalesce,
  987. .set_coalesce = cpsw_set_coalesce,
  988. .get_sset_count = cpsw_get_sset_count,
  989. .get_strings = cpsw_get_strings,
  990. .get_ethtool_stats = cpsw_get_ethtool_stats,
  991. .get_pauseparam = cpsw_get_pauseparam,
  992. .set_pauseparam = cpsw_set_pauseparam,
  993. .get_wol = cpsw_get_wol,
  994. .set_wol = cpsw_set_wol,
  995. .get_regs_len = cpsw_get_regs_len,
  996. .get_regs = cpsw_get_regs,
  997. .begin = cpsw_ethtool_op_begin,
  998. .complete = cpsw_ethtool_op_complete,
  999. .get_channels = cpsw_get_channels,
  1000. .set_channels = cpsw_set_channels,
  1001. .get_link_ksettings = cpsw_get_link_ksettings,
  1002. .set_link_ksettings = cpsw_set_link_ksettings,
  1003. .get_eee = cpsw_get_eee,
  1004. .nway_reset = cpsw_nway_reset,
  1005. .get_ringparam = cpsw_get_ringparam,
  1006. .set_ringparam = cpsw_set_ringparam,
  1007. };
  1008. static int cpsw_probe_dt(struct cpsw_common *cpsw)
  1009. {
  1010. struct device_node *node = cpsw->dev->of_node, *tmp_node, *port_np;
  1011. struct cpsw_platform_data *data = &cpsw->data;
  1012. struct device *dev = cpsw->dev;
  1013. int ret;
  1014. u32 prop;
  1015. if (!node)
  1016. return -EINVAL;
  1017. tmp_node = of_get_child_by_name(node, "ethernet-ports");
  1018. if (!tmp_node)
  1019. return -ENOENT;
  1020. data->slaves = of_get_child_count(tmp_node);
  1021. if (data->slaves != CPSW_SLAVE_PORTS_NUM) {
  1022. of_node_put(tmp_node);
  1023. return -ENOENT;
  1024. }
  1025. data->active_slave = 0;
  1026. data->channels = CPSW_MAX_QUEUES;
  1027. data->dual_emac = true;
  1028. data->bd_ram_size = CPSW_BD_RAM_SIZE;
  1029. data->mac_control = 0;
  1030. data->slave_data = devm_kcalloc(dev, CPSW_SLAVE_PORTS_NUM,
  1031. sizeof(struct cpsw_slave_data),
  1032. GFP_KERNEL);
  1033. if (!data->slave_data) {
  1034. of_node_put(tmp_node);
  1035. return -ENOMEM;
  1036. }
  1037. /* Populate all the child nodes here...
  1038. */
  1039. ret = devm_of_platform_populate(dev);
  1040. /* We do not want to force this, as in some cases may not have child */
  1041. if (ret)
  1042. dev_warn(dev, "Doesn't have any child node\n");
  1043. for_each_child_of_node(tmp_node, port_np) {
  1044. struct cpsw_slave_data *slave_data;
  1045. u32 port_id;
  1046. ret = of_property_read_u32(port_np, "reg", &port_id);
  1047. if (ret < 0) {
  1048. dev_err(dev, "%pOF error reading port_id %d\n",
  1049. port_np, ret);
  1050. goto err_node_put;
  1051. }
  1052. if (!port_id || port_id > CPSW_SLAVE_PORTS_NUM) {
  1053. dev_err(dev, "%pOF has invalid port_id %u\n",
  1054. port_np, port_id);
  1055. ret = -EINVAL;
  1056. goto err_node_put;
  1057. }
  1058. slave_data = &data->slave_data[port_id - 1];
  1059. slave_data->disabled = !of_device_is_available(port_np);
  1060. if (slave_data->disabled)
  1061. continue;
  1062. slave_data->slave_node = port_np;
  1063. slave_data->ifphy = devm_of_phy_get(dev, port_np, NULL);
  1064. if (IS_ERR(slave_data->ifphy)) {
  1065. ret = PTR_ERR(slave_data->ifphy);
  1066. dev_err(dev, "%pOF: Error retrieving port phy: %d\n",
  1067. port_np, ret);
  1068. goto err_node_put;
  1069. }
  1070. if (of_phy_is_fixed_link(port_np)) {
  1071. ret = of_phy_register_fixed_link(port_np);
  1072. if (ret) {
  1073. dev_err_probe(dev, ret, "%pOF failed to register fixed-link phy\n",
  1074. port_np);
  1075. goto err_node_put;
  1076. }
  1077. slave_data->phy_node = of_node_get(port_np);
  1078. } else {
  1079. slave_data->phy_node =
  1080. of_parse_phandle(port_np, "phy-handle", 0);
  1081. }
  1082. if (!slave_data->phy_node) {
  1083. dev_err(dev, "%pOF no phy found\n", port_np);
  1084. ret = -ENODEV;
  1085. goto err_node_put;
  1086. }
  1087. ret = of_get_phy_mode(port_np, &slave_data->phy_if);
  1088. if (ret) {
  1089. dev_err(dev, "%pOF read phy-mode err %d\n",
  1090. port_np, ret);
  1091. goto err_node_put;
  1092. }
  1093. ret = of_get_mac_address(port_np, slave_data->mac_addr);
  1094. if (ret) {
  1095. ret = ti_cm_get_macid(dev, port_id - 1,
  1096. slave_data->mac_addr);
  1097. if (ret)
  1098. goto err_node_put;
  1099. }
  1100. if (of_property_read_u32(port_np, "ti,dual-emac-pvid",
  1101. &prop)) {
  1102. dev_err(dev, "%pOF Missing dual_emac_res_vlan in DT.\n",
  1103. port_np);
  1104. slave_data->dual_emac_res_vlan = port_id;
  1105. dev_err(dev, "%pOF Using %d as Reserved VLAN\n",
  1106. port_np, slave_data->dual_emac_res_vlan);
  1107. } else {
  1108. slave_data->dual_emac_res_vlan = prop;
  1109. }
  1110. }
  1111. of_node_put(tmp_node);
  1112. return 0;
  1113. err_node_put:
  1114. of_node_put(port_np);
  1115. of_node_put(tmp_node);
  1116. return ret;
  1117. }
  1118. static void cpsw_remove_dt(struct cpsw_common *cpsw)
  1119. {
  1120. struct cpsw_platform_data *data = &cpsw->data;
  1121. int i = 0;
  1122. for (i = 0; i < cpsw->data.slaves; i++) {
  1123. struct cpsw_slave_data *slave_data = &data->slave_data[i];
  1124. struct device_node *port_np = slave_data->phy_node;
  1125. if (port_np) {
  1126. if (of_phy_is_fixed_link(port_np))
  1127. of_phy_deregister_fixed_link(port_np);
  1128. of_node_put(port_np);
  1129. }
  1130. }
  1131. }
  1132. static int cpsw_create_ports(struct cpsw_common *cpsw)
  1133. {
  1134. struct cpsw_platform_data *data = &cpsw->data;
  1135. struct net_device *ndev, *napi_ndev = NULL;
  1136. struct device *dev = cpsw->dev;
  1137. struct cpsw_priv *priv;
  1138. int ret = 0, i = 0;
  1139. for (i = 0; i < cpsw->data.slaves; i++) {
  1140. struct cpsw_slave_data *slave_data = &data->slave_data[i];
  1141. if (slave_data->disabled)
  1142. continue;
  1143. ndev = devm_alloc_etherdev_mqs(dev, sizeof(struct cpsw_priv),
  1144. CPSW_MAX_QUEUES,
  1145. CPSW_MAX_QUEUES);
  1146. if (!ndev) {
  1147. dev_err(dev, "error allocating net_device\n");
  1148. return -ENOMEM;
  1149. }
  1150. priv = netdev_priv(ndev);
  1151. priv->cpsw = cpsw;
  1152. priv->ndev = ndev;
  1153. priv->dev = dev;
  1154. priv->msg_enable = netif_msg_init(debug_level, CPSW_DEBUG);
  1155. priv->emac_port = i + 1;
  1156. priv->tx_packet_min = CPSW_MIN_PACKET_SIZE;
  1157. INIT_WORK(&priv->rx_mode_work, cpsw_ndo_set_rx_mode_work);
  1158. if (is_valid_ether_addr(slave_data->mac_addr)) {
  1159. ether_addr_copy(priv->mac_addr, slave_data->mac_addr);
  1160. dev_info(cpsw->dev, "Detected MACID = %pM\n",
  1161. priv->mac_addr);
  1162. } else {
  1163. eth_random_addr(slave_data->mac_addr);
  1164. dev_info(cpsw->dev, "Random MACID = %pM\n",
  1165. priv->mac_addr);
  1166. }
  1167. eth_hw_addr_set(ndev, slave_data->mac_addr);
  1168. ether_addr_copy(priv->mac_addr, slave_data->mac_addr);
  1169. cpsw->slaves[i].ndev = ndev;
  1170. ndev->features |= NETIF_F_HW_VLAN_CTAG_FILTER |
  1171. NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_TC;
  1172. ndev->netns_immutable = true;
  1173. ndev->xdp_features = NETDEV_XDP_ACT_BASIC |
  1174. NETDEV_XDP_ACT_REDIRECT |
  1175. NETDEV_XDP_ACT_NDO_XMIT;
  1176. ndev->netdev_ops = &cpsw_netdev_ops;
  1177. ndev->ethtool_ops = &cpsw_ethtool_ops;
  1178. SET_NETDEV_DEV(ndev, dev);
  1179. ndev->dev.of_node = slave_data->slave_node;
  1180. if (!napi_ndev) {
  1181. /* CPSW Host port CPDMA interface is shared between
  1182. * ports and there is only one TX and one RX IRQs
  1183. * available for all possible TX and RX channels
  1184. * accordingly.
  1185. */
  1186. netif_napi_add(ndev, &cpsw->napi_rx,
  1187. cpsw->quirk_irq ? cpsw_rx_poll : cpsw_rx_mq_poll);
  1188. netif_napi_add_tx(ndev, &cpsw->napi_tx,
  1189. cpsw->quirk_irq ?
  1190. cpsw_tx_poll : cpsw_tx_mq_poll);
  1191. }
  1192. napi_ndev = ndev;
  1193. }
  1194. return ret;
  1195. }
  1196. static void cpsw_unregister_ports(struct cpsw_common *cpsw)
  1197. {
  1198. struct net_device *ndev;
  1199. struct cpsw_priv *priv;
  1200. int i = 0;
  1201. for (i = 0; i < cpsw->data.slaves; i++) {
  1202. ndev = cpsw->slaves[i].ndev;
  1203. if (!ndev || ndev->reg_state != NETREG_REGISTERED)
  1204. continue;
  1205. priv = netdev_priv(ndev);
  1206. unregister_netdev(ndev);
  1207. disable_work_sync(&priv->rx_mode_work);
  1208. }
  1209. }
  1210. static int cpsw_register_ports(struct cpsw_common *cpsw)
  1211. {
  1212. int ret = 0, i = 0;
  1213. for (i = 0; i < cpsw->data.slaves; i++) {
  1214. if (!cpsw->slaves[i].ndev)
  1215. continue;
  1216. /* register the network device */
  1217. ret = register_netdev(cpsw->slaves[i].ndev);
  1218. if (ret) {
  1219. dev_err(cpsw->dev,
  1220. "cpsw: err registering net device%d\n", i);
  1221. break;
  1222. }
  1223. }
  1224. if (ret)
  1225. cpsw_unregister_ports(cpsw);
  1226. return ret;
  1227. }
  1228. bool cpsw_port_dev_check(const struct net_device *ndev)
  1229. {
  1230. if (ndev->netdev_ops == &cpsw_netdev_ops) {
  1231. struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
  1232. return !cpsw->data.dual_emac;
  1233. }
  1234. return false;
  1235. }
  1236. static void cpsw_port_offload_fwd_mark_update(struct cpsw_common *cpsw)
  1237. {
  1238. int set_val = 0;
  1239. int i;
  1240. if (!cpsw->ale_bypass &&
  1241. (cpsw->br_members == (ALE_PORT_1 | ALE_PORT_2)))
  1242. set_val = 1;
  1243. dev_dbg(cpsw->dev, "set offload_fwd_mark %d\n", set_val);
  1244. for (i = 0; i < cpsw->data.slaves; i++) {
  1245. struct net_device *sl_ndev = cpsw->slaves[i].ndev;
  1246. struct cpsw_priv *priv = netdev_priv(sl_ndev);
  1247. priv->offload_fwd_mark = set_val;
  1248. }
  1249. }
  1250. static int cpsw_netdevice_port_link(struct net_device *ndev,
  1251. struct net_device *br_ndev,
  1252. struct netlink_ext_ack *extack)
  1253. {
  1254. struct cpsw_priv *priv = netdev_priv(ndev);
  1255. struct cpsw_common *cpsw = priv->cpsw;
  1256. int err;
  1257. if (!cpsw->br_members) {
  1258. cpsw->hw_bridge_dev = br_ndev;
  1259. } else {
  1260. /* This is adding the port to a second bridge, this is
  1261. * unsupported
  1262. */
  1263. if (cpsw->hw_bridge_dev != br_ndev)
  1264. return -EOPNOTSUPP;
  1265. }
  1266. err = switchdev_bridge_port_offload(ndev, ndev, NULL, NULL, NULL,
  1267. false, extack);
  1268. if (err)
  1269. return err;
  1270. cpsw->br_members |= BIT(priv->emac_port);
  1271. cpsw_port_offload_fwd_mark_update(cpsw);
  1272. return NOTIFY_DONE;
  1273. }
  1274. static void cpsw_netdevice_port_unlink(struct net_device *ndev)
  1275. {
  1276. struct cpsw_priv *priv = netdev_priv(ndev);
  1277. struct cpsw_common *cpsw = priv->cpsw;
  1278. switchdev_bridge_port_unoffload(ndev, NULL, NULL, NULL);
  1279. cpsw->br_members &= ~BIT(priv->emac_port);
  1280. cpsw_port_offload_fwd_mark_update(cpsw);
  1281. if (!cpsw->br_members)
  1282. cpsw->hw_bridge_dev = NULL;
  1283. }
  1284. /* netdev notifier */
  1285. static int cpsw_netdevice_event(struct notifier_block *unused,
  1286. unsigned long event, void *ptr)
  1287. {
  1288. struct netlink_ext_ack *extack = netdev_notifier_info_to_extack(ptr);
  1289. struct net_device *ndev = netdev_notifier_info_to_dev(ptr);
  1290. struct netdev_notifier_changeupper_info *info;
  1291. int ret = NOTIFY_DONE;
  1292. if (!cpsw_port_dev_check(ndev))
  1293. return NOTIFY_DONE;
  1294. switch (event) {
  1295. case NETDEV_CHANGEUPPER:
  1296. info = ptr;
  1297. if (netif_is_bridge_master(info->upper_dev)) {
  1298. if (info->linking)
  1299. ret = cpsw_netdevice_port_link(ndev,
  1300. info->upper_dev,
  1301. extack);
  1302. else
  1303. cpsw_netdevice_port_unlink(ndev);
  1304. }
  1305. break;
  1306. default:
  1307. return NOTIFY_DONE;
  1308. }
  1309. return notifier_from_errno(ret);
  1310. }
  1311. static struct notifier_block cpsw_netdevice_nb __read_mostly = {
  1312. .notifier_call = cpsw_netdevice_event,
  1313. };
  1314. static int cpsw_register_notifiers(struct cpsw_common *cpsw)
  1315. {
  1316. int ret = 0;
  1317. ret = register_netdevice_notifier(&cpsw_netdevice_nb);
  1318. if (ret) {
  1319. dev_err(cpsw->dev, "can't register netdevice notifier\n");
  1320. return ret;
  1321. }
  1322. ret = cpsw_switchdev_register_notifiers(cpsw);
  1323. if (ret)
  1324. unregister_netdevice_notifier(&cpsw_netdevice_nb);
  1325. return ret;
  1326. }
  1327. static void cpsw_unregister_notifiers(struct cpsw_common *cpsw)
  1328. {
  1329. cpsw_switchdev_unregister_notifiers(cpsw);
  1330. unregister_netdevice_notifier(&cpsw_netdevice_nb);
  1331. }
  1332. static const struct devlink_ops cpsw_devlink_ops = {
  1333. };
  1334. static int cpsw_dl_switch_mode_get(struct devlink *dl, u32 id,
  1335. struct devlink_param_gset_ctx *ctx,
  1336. struct netlink_ext_ack *extack)
  1337. {
  1338. struct cpsw_devlink *dl_priv = devlink_priv(dl);
  1339. struct cpsw_common *cpsw = dl_priv->cpsw;
  1340. dev_dbg(cpsw->dev, "%s id:%u\n", __func__, id);
  1341. if (id != CPSW_DL_PARAM_SWITCH_MODE)
  1342. return -EOPNOTSUPP;
  1343. ctx->val.vbool = !cpsw->data.dual_emac;
  1344. return 0;
  1345. }
  1346. static int cpsw_dl_switch_mode_set(struct devlink *dl, u32 id,
  1347. struct devlink_param_gset_ctx *ctx,
  1348. struct netlink_ext_ack *extack)
  1349. {
  1350. struct cpsw_devlink *dl_priv = devlink_priv(dl);
  1351. struct cpsw_common *cpsw = dl_priv->cpsw;
  1352. int vlan = cpsw->data.default_vlan;
  1353. bool switch_en = ctx->val.vbool;
  1354. bool if_running = false;
  1355. int i;
  1356. dev_dbg(cpsw->dev, "%s id:%u\n", __func__, id);
  1357. if (id != CPSW_DL_PARAM_SWITCH_MODE)
  1358. return -EOPNOTSUPP;
  1359. if (switch_en == !cpsw->data.dual_emac)
  1360. return 0;
  1361. if (!switch_en && cpsw->br_members) {
  1362. dev_err(cpsw->dev, "Remove ports from BR before disabling switch mode\n");
  1363. return -EINVAL;
  1364. }
  1365. rtnl_lock();
  1366. for (i = 0; i < cpsw->data.slaves; i++) {
  1367. struct cpsw_slave *slave = &cpsw->slaves[i];
  1368. struct net_device *sl_ndev = slave->ndev;
  1369. if (!sl_ndev || !netif_running(sl_ndev))
  1370. continue;
  1371. if_running = true;
  1372. }
  1373. if (!if_running) {
  1374. /* all ndevs are down */
  1375. cpsw->data.dual_emac = !switch_en;
  1376. for (i = 0; i < cpsw->data.slaves; i++) {
  1377. struct cpsw_slave *slave = &cpsw->slaves[i];
  1378. struct net_device *sl_ndev = slave->ndev;
  1379. if (!sl_ndev)
  1380. continue;
  1381. if (switch_en)
  1382. vlan = cpsw->data.default_vlan;
  1383. else
  1384. vlan = slave->data->dual_emac_res_vlan;
  1385. slave->port_vlan = vlan;
  1386. }
  1387. goto exit;
  1388. }
  1389. if (switch_en) {
  1390. dev_info(cpsw->dev, "Enable switch mode\n");
  1391. /* enable bypass - no forwarding; all traffic goes to Host */
  1392. cpsw_ale_control_set(cpsw->ale, 0, ALE_BYPASS, 1);
  1393. /* clean up ALE table */
  1394. cpsw_ale_control_set(cpsw->ale, 0, ALE_CLEAR, 1);
  1395. cpsw_ale_control_get(cpsw->ale, 0, ALE_AGEOUT);
  1396. cpsw_init_host_port_switch(cpsw);
  1397. for (i = 0; i < cpsw->data.slaves; i++) {
  1398. struct cpsw_slave *slave = &cpsw->slaves[i];
  1399. struct net_device *sl_ndev = slave->ndev;
  1400. struct cpsw_priv *priv;
  1401. if (!sl_ndev)
  1402. continue;
  1403. priv = netdev_priv(sl_ndev);
  1404. slave->port_vlan = vlan;
  1405. WRITE_ONCE(priv->tx_packet_min, CPSW_MIN_PACKET_SIZE_VLAN);
  1406. if (netif_running(sl_ndev))
  1407. cpsw_port_add_switch_def_ale_entries(priv,
  1408. slave);
  1409. }
  1410. cpsw_ale_control_set(cpsw->ale, 0, ALE_BYPASS, 0);
  1411. cpsw->data.dual_emac = false;
  1412. } else {
  1413. dev_info(cpsw->dev, "Disable switch mode\n");
  1414. /* enable bypass - no forwarding; all traffic goes to Host */
  1415. cpsw_ale_control_set(cpsw->ale, 0, ALE_BYPASS, 1);
  1416. cpsw_ale_control_set(cpsw->ale, 0, ALE_CLEAR, 1);
  1417. cpsw_ale_control_get(cpsw->ale, 0, ALE_AGEOUT);
  1418. cpsw_init_host_port_dual_mac(cpsw);
  1419. for (i = 0; i < cpsw->data.slaves; i++) {
  1420. struct cpsw_slave *slave = &cpsw->slaves[i];
  1421. struct net_device *sl_ndev = slave->ndev;
  1422. struct cpsw_priv *priv;
  1423. if (!sl_ndev)
  1424. continue;
  1425. priv = netdev_priv(slave->ndev);
  1426. slave->port_vlan = slave->data->dual_emac_res_vlan;
  1427. WRITE_ONCE(priv->tx_packet_min, CPSW_MIN_PACKET_SIZE);
  1428. cpsw_port_add_dual_emac_def_ale_entries(priv, slave);
  1429. }
  1430. cpsw_ale_control_set(cpsw->ale, 0, ALE_BYPASS, 0);
  1431. cpsw->data.dual_emac = true;
  1432. }
  1433. exit:
  1434. rtnl_unlock();
  1435. return 0;
  1436. }
  1437. static int cpsw_dl_ale_ctrl_get(struct devlink *dl, u32 id,
  1438. struct devlink_param_gset_ctx *ctx,
  1439. struct netlink_ext_ack *extack)
  1440. {
  1441. struct cpsw_devlink *dl_priv = devlink_priv(dl);
  1442. struct cpsw_common *cpsw = dl_priv->cpsw;
  1443. dev_dbg(cpsw->dev, "%s id:%u\n", __func__, id);
  1444. switch (id) {
  1445. case CPSW_DL_PARAM_ALE_BYPASS:
  1446. ctx->val.vbool = cpsw_ale_control_get(cpsw->ale, 0, ALE_BYPASS);
  1447. break;
  1448. default:
  1449. return -EOPNOTSUPP;
  1450. }
  1451. return 0;
  1452. }
  1453. static int cpsw_dl_ale_ctrl_set(struct devlink *dl, u32 id,
  1454. struct devlink_param_gset_ctx *ctx,
  1455. struct netlink_ext_ack *extack)
  1456. {
  1457. struct cpsw_devlink *dl_priv = devlink_priv(dl);
  1458. struct cpsw_common *cpsw = dl_priv->cpsw;
  1459. int ret = -EOPNOTSUPP;
  1460. dev_dbg(cpsw->dev, "%s id:%u\n", __func__, id);
  1461. switch (id) {
  1462. case CPSW_DL_PARAM_ALE_BYPASS:
  1463. ret = cpsw_ale_control_set(cpsw->ale, 0, ALE_BYPASS,
  1464. ctx->val.vbool);
  1465. if (!ret) {
  1466. cpsw->ale_bypass = ctx->val.vbool;
  1467. cpsw_port_offload_fwd_mark_update(cpsw);
  1468. }
  1469. break;
  1470. default:
  1471. return -EOPNOTSUPP;
  1472. }
  1473. return 0;
  1474. }
  1475. static const struct devlink_param cpsw_devlink_params[] = {
  1476. DEVLINK_PARAM_DRIVER(CPSW_DL_PARAM_SWITCH_MODE,
  1477. "switch_mode", DEVLINK_PARAM_TYPE_BOOL,
  1478. BIT(DEVLINK_PARAM_CMODE_RUNTIME),
  1479. cpsw_dl_switch_mode_get, cpsw_dl_switch_mode_set,
  1480. NULL),
  1481. DEVLINK_PARAM_DRIVER(CPSW_DL_PARAM_ALE_BYPASS,
  1482. "ale_bypass", DEVLINK_PARAM_TYPE_BOOL,
  1483. BIT(DEVLINK_PARAM_CMODE_RUNTIME),
  1484. cpsw_dl_ale_ctrl_get, cpsw_dl_ale_ctrl_set, NULL),
  1485. };
  1486. static int cpsw_register_devlink(struct cpsw_common *cpsw)
  1487. {
  1488. struct device *dev = cpsw->dev;
  1489. struct cpsw_devlink *dl_priv;
  1490. int ret = 0;
  1491. cpsw->devlink = devlink_alloc(&cpsw_devlink_ops, sizeof(*dl_priv), dev);
  1492. if (!cpsw->devlink)
  1493. return -ENOMEM;
  1494. dl_priv = devlink_priv(cpsw->devlink);
  1495. dl_priv->cpsw = cpsw;
  1496. ret = devlink_params_register(cpsw->devlink, cpsw_devlink_params,
  1497. ARRAY_SIZE(cpsw_devlink_params));
  1498. if (ret) {
  1499. dev_err(dev, "DL params reg fail ret:%d\n", ret);
  1500. goto dl_unreg;
  1501. }
  1502. devlink_register(cpsw->devlink);
  1503. return ret;
  1504. dl_unreg:
  1505. devlink_free(cpsw->devlink);
  1506. return ret;
  1507. }
  1508. static void cpsw_unregister_devlink(struct cpsw_common *cpsw)
  1509. {
  1510. devlink_unregister(cpsw->devlink);
  1511. devlink_params_unregister(cpsw->devlink, cpsw_devlink_params,
  1512. ARRAY_SIZE(cpsw_devlink_params));
  1513. devlink_free(cpsw->devlink);
  1514. }
  1515. static const struct of_device_id cpsw_of_mtable[] = {
  1516. { .compatible = "ti,cpsw-switch"},
  1517. { .compatible = "ti,am335x-cpsw-switch"},
  1518. { .compatible = "ti,am4372-cpsw-switch"},
  1519. { .compatible = "ti,dra7-cpsw-switch"},
  1520. { /* sentinel */ },
  1521. };
  1522. MODULE_DEVICE_TABLE(of, cpsw_of_mtable);
  1523. static const struct soc_device_attribute cpsw_soc_devices[] = {
  1524. { .family = "AM33xx", .revision = "ES1.0"},
  1525. { /* sentinel */ }
  1526. };
  1527. static int cpsw_probe(struct platform_device *pdev)
  1528. {
  1529. const struct soc_device_attribute *soc;
  1530. struct device *dev = &pdev->dev;
  1531. struct cpsw_common *cpsw;
  1532. struct resource *ss_res;
  1533. struct gpio_descs *mode;
  1534. void __iomem *ss_regs;
  1535. int ret = 0, ch;
  1536. struct clk *clk;
  1537. int irq;
  1538. cpsw = devm_kzalloc(dev, sizeof(struct cpsw_common), GFP_KERNEL);
  1539. if (!cpsw)
  1540. return -ENOMEM;
  1541. cpsw_slave_index = cpsw_slave_index_priv;
  1542. cpsw->dev = dev;
  1543. cpsw->slaves = devm_kcalloc(dev,
  1544. CPSW_SLAVE_PORTS_NUM,
  1545. sizeof(struct cpsw_slave),
  1546. GFP_KERNEL);
  1547. if (!cpsw->slaves)
  1548. return -ENOMEM;
  1549. mode = devm_gpiod_get_array_optional(dev, "mode", GPIOD_OUT_LOW);
  1550. if (IS_ERR(mode)) {
  1551. ret = PTR_ERR(mode);
  1552. dev_err(dev, "gpio request failed, ret %d\n", ret);
  1553. return ret;
  1554. }
  1555. clk = devm_clk_get(dev, "fck");
  1556. if (IS_ERR(clk)) {
  1557. ret = PTR_ERR(clk);
  1558. dev_err(dev, "fck is not found %d\n", ret);
  1559. return ret;
  1560. }
  1561. cpsw->bus_freq_mhz = clk_get_rate(clk) / 1000000;
  1562. ss_regs = devm_platform_get_and_ioremap_resource(pdev, 0, &ss_res);
  1563. if (IS_ERR(ss_regs)) {
  1564. ret = PTR_ERR(ss_regs);
  1565. return ret;
  1566. }
  1567. cpsw->regs = ss_regs;
  1568. irq = platform_get_irq_byname(pdev, "rx");
  1569. if (irq < 0)
  1570. return irq;
  1571. cpsw->irqs_table[0] = irq;
  1572. irq = platform_get_irq_byname(pdev, "tx");
  1573. if (irq < 0)
  1574. return irq;
  1575. cpsw->irqs_table[1] = irq;
  1576. irq = platform_get_irq_byname(pdev, "misc");
  1577. if (irq <= 0)
  1578. return irq;
  1579. cpsw->misc_irq = irq;
  1580. platform_set_drvdata(pdev, cpsw);
  1581. /* This may be required here for child devices. */
  1582. pm_runtime_enable(dev);
  1583. /* Need to enable clocks with runtime PM api to access module
  1584. * registers
  1585. */
  1586. ret = pm_runtime_resume_and_get(dev);
  1587. if (ret < 0) {
  1588. pm_runtime_disable(dev);
  1589. return ret;
  1590. }
  1591. ret = cpsw_probe_dt(cpsw);
  1592. if (ret)
  1593. goto clean_dt_ret;
  1594. soc = soc_device_match(cpsw_soc_devices);
  1595. if (soc)
  1596. cpsw->quirk_irq = true;
  1597. cpsw->rx_packet_max = rx_packet_max;
  1598. cpsw->descs_pool_size = descs_pool_size;
  1599. eth_random_addr(cpsw->base_mac);
  1600. ret = cpsw_init_common(cpsw, ss_regs, ale_ageout,
  1601. (u32 __force)ss_res->start + CPSW2_BD_OFFSET,
  1602. descs_pool_size);
  1603. if (ret)
  1604. goto clean_dt_ret;
  1605. cpsw->wr_regs = cpsw->version == CPSW_VERSION_1 ?
  1606. ss_regs + CPSW1_WR_OFFSET :
  1607. ss_regs + CPSW2_WR_OFFSET;
  1608. ch = cpsw->quirk_irq ? 0 : 7;
  1609. cpsw->txv[0].ch = cpdma_chan_create(cpsw->dma, ch, cpsw_tx_handler, 0);
  1610. if (IS_ERR(cpsw->txv[0].ch)) {
  1611. dev_err(dev, "error initializing tx dma channel\n");
  1612. ret = PTR_ERR(cpsw->txv[0].ch);
  1613. goto clean_cpts;
  1614. }
  1615. cpsw->rxv[0].ch = cpdma_chan_create(cpsw->dma, 0, cpsw_rx_handler, 1);
  1616. if (IS_ERR(cpsw->rxv[0].ch)) {
  1617. dev_err(dev, "error initializing rx dma channel\n");
  1618. ret = PTR_ERR(cpsw->rxv[0].ch);
  1619. goto clean_cpts;
  1620. }
  1621. cpsw_split_res(cpsw);
  1622. /* setup netdevs */
  1623. ret = cpsw_create_ports(cpsw);
  1624. if (ret)
  1625. goto clean_cpts;
  1626. /* Grab RX and TX IRQs. Note that we also have RX_THRESHOLD and
  1627. * MISC IRQs which are always kept disabled with this driver so
  1628. * we will not request them.
  1629. *
  1630. * If anyone wants to implement support for those, make sure to
  1631. * first request and append them to irqs_table array.
  1632. */
  1633. ret = devm_request_irq(dev, cpsw->irqs_table[0], cpsw_rx_interrupt,
  1634. 0, dev_name(dev), cpsw);
  1635. if (ret < 0) {
  1636. dev_err(dev, "error attaching irq (%d)\n", ret);
  1637. goto clean_cpts;
  1638. }
  1639. ret = devm_request_irq(dev, cpsw->irqs_table[1], cpsw_tx_interrupt,
  1640. 0, dev_name(dev), cpsw);
  1641. if (ret < 0) {
  1642. dev_err(dev, "error attaching irq (%d)\n", ret);
  1643. goto clean_cpts;
  1644. }
  1645. if (!cpsw->cpts)
  1646. goto skip_cpts;
  1647. ret = devm_request_irq(dev, cpsw->misc_irq, cpsw_misc_interrupt,
  1648. 0, dev_name(&pdev->dev), cpsw);
  1649. if (ret < 0) {
  1650. dev_err(dev, "error attaching misc irq (%d)\n", ret);
  1651. goto clean_cpts;
  1652. }
  1653. /* Enable misc CPTS evnt_pend IRQ */
  1654. cpts_set_irqpoll(cpsw->cpts, false);
  1655. skip_cpts:
  1656. ret = cpsw_register_notifiers(cpsw);
  1657. if (ret)
  1658. goto clean_cpts;
  1659. ret = cpsw_register_devlink(cpsw);
  1660. if (ret)
  1661. goto clean_unregister_notifiers;
  1662. ret = cpsw_register_ports(cpsw);
  1663. if (ret)
  1664. goto clean_unregister_notifiers;
  1665. dev_notice(dev, "initialized (regs %pa, pool size %d) hw_ver:%08X %d.%d (%d)\n",
  1666. &ss_res->start, descs_pool_size,
  1667. cpsw->version, CPSW_MAJOR_VERSION(cpsw->version),
  1668. CPSW_MINOR_VERSION(cpsw->version),
  1669. CPSW_RTL_VERSION(cpsw->version));
  1670. pm_runtime_put(dev);
  1671. return 0;
  1672. clean_unregister_notifiers:
  1673. cpsw_unregister_notifiers(cpsw);
  1674. clean_cpts:
  1675. cpts_release(cpsw->cpts);
  1676. cpdma_ctlr_destroy(cpsw->dma);
  1677. clean_dt_ret:
  1678. cpsw_remove_dt(cpsw);
  1679. pm_runtime_put_sync(dev);
  1680. pm_runtime_disable(dev);
  1681. return ret;
  1682. }
  1683. static void cpsw_remove(struct platform_device *pdev)
  1684. {
  1685. struct cpsw_common *cpsw = platform_get_drvdata(pdev);
  1686. int ret;
  1687. ret = pm_runtime_resume_and_get(&pdev->dev);
  1688. if (ret < 0) {
  1689. /* Note, if this error path is taken, we're leaking some
  1690. * resources.
  1691. */
  1692. dev_err(&pdev->dev, "Failed to resume device (%pe)\n",
  1693. ERR_PTR(ret));
  1694. return;
  1695. }
  1696. cpsw_unregister_notifiers(cpsw);
  1697. cpsw_unregister_devlink(cpsw);
  1698. cpsw_unregister_ports(cpsw);
  1699. cpts_release(cpsw->cpts);
  1700. cpdma_ctlr_destroy(cpsw->dma);
  1701. cpsw_remove_dt(cpsw);
  1702. pm_runtime_put_sync(&pdev->dev);
  1703. pm_runtime_disable(&pdev->dev);
  1704. }
  1705. static int __maybe_unused cpsw_suspend(struct device *dev)
  1706. {
  1707. struct cpsw_common *cpsw = dev_get_drvdata(dev);
  1708. int i;
  1709. rtnl_lock();
  1710. for (i = 0; i < cpsw->data.slaves; i++) {
  1711. struct net_device *ndev = cpsw->slaves[i].ndev;
  1712. if (!(ndev && netif_running(ndev)))
  1713. continue;
  1714. cpsw_ndo_stop(ndev);
  1715. }
  1716. rtnl_unlock();
  1717. /* Select sleep pin state */
  1718. pinctrl_pm_select_sleep_state(dev);
  1719. return 0;
  1720. }
  1721. static int __maybe_unused cpsw_resume(struct device *dev)
  1722. {
  1723. struct cpsw_common *cpsw = dev_get_drvdata(dev);
  1724. int i;
  1725. /* Select default pin state */
  1726. pinctrl_pm_select_default_state(dev);
  1727. /* shut up ASSERT_RTNL() warning in netif_set_real_num_tx/rx_queues */
  1728. rtnl_lock();
  1729. for (i = 0; i < cpsw->data.slaves; i++) {
  1730. struct net_device *ndev = cpsw->slaves[i].ndev;
  1731. if (!(ndev && netif_running(ndev)))
  1732. continue;
  1733. cpsw_ndo_open(ndev);
  1734. }
  1735. rtnl_unlock();
  1736. return 0;
  1737. }
  1738. static SIMPLE_DEV_PM_OPS(cpsw_pm_ops, cpsw_suspend, cpsw_resume);
  1739. static struct platform_driver cpsw_driver = {
  1740. .driver = {
  1741. .name = "cpsw-switch",
  1742. .pm = &cpsw_pm_ops,
  1743. .of_match_table = cpsw_of_mtable,
  1744. },
  1745. .probe = cpsw_probe,
  1746. .remove = cpsw_remove,
  1747. };
  1748. module_platform_driver(cpsw_driver);
  1749. MODULE_LICENSE("GPL");
  1750. MODULE_DESCRIPTION("TI CPSW switchdev Ethernet driver");