cpsw.c 48 KB

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