cpsw_ethtool.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Texas Instruments Ethernet Switch Driver ethtool intf
  4. *
  5. * Copyright (C) 2019 Texas Instruments
  6. */
  7. #include <linux/if_ether.h>
  8. #include <linux/if_vlan.h>
  9. #include <linux/kmemleak.h>
  10. #include <linux/module.h>
  11. #include <linux/netdevice.h>
  12. #include <linux/net_tstamp.h>
  13. #include <linux/phy.h>
  14. #include <linux/pm_runtime.h>
  15. #include <linux/skbuff.h>
  16. #include "cpsw.h"
  17. #include "cpts.h"
  18. #include "cpsw_ale.h"
  19. #include "cpsw_priv.h"
  20. #include "davinci_cpdma.h"
  21. struct cpsw_hw_stats {
  22. u32 rxgoodframes;
  23. u32 rxbroadcastframes;
  24. u32 rxmulticastframes;
  25. u32 rxpauseframes;
  26. u32 rxcrcerrors;
  27. u32 rxaligncodeerrors;
  28. u32 rxoversizedframes;
  29. u32 rxjabberframes;
  30. u32 rxundersizedframes;
  31. u32 rxfragments;
  32. u32 __pad_0[2];
  33. u32 rxoctets;
  34. u32 txgoodframes;
  35. u32 txbroadcastframes;
  36. u32 txmulticastframes;
  37. u32 txpauseframes;
  38. u32 txdeferredframes;
  39. u32 txcollisionframes;
  40. u32 txsinglecollframes;
  41. u32 txmultcollframes;
  42. u32 txexcessivecollisions;
  43. u32 txlatecollisions;
  44. u32 txunderrun;
  45. u32 txcarriersenseerrors;
  46. u32 txoctets;
  47. u32 octetframes64;
  48. u32 octetframes65t127;
  49. u32 octetframes128t255;
  50. u32 octetframes256t511;
  51. u32 octetframes512t1023;
  52. u32 octetframes1024tup;
  53. u32 netoctets;
  54. u32 rxsofoverruns;
  55. u32 rxmofoverruns;
  56. u32 rxdmaoverruns;
  57. };
  58. struct cpsw_stats {
  59. char stat_string[ETH_GSTRING_LEN];
  60. int type;
  61. int sizeof_stat;
  62. int stat_offset;
  63. };
  64. enum {
  65. CPSW_STATS,
  66. CPDMA_RX_STATS,
  67. CPDMA_TX_STATS,
  68. };
  69. #define CPSW_STAT(m) CPSW_STATS, \
  70. sizeof_field(struct cpsw_hw_stats, m), \
  71. offsetof(struct cpsw_hw_stats, m)
  72. #define CPDMA_RX_STAT(m) CPDMA_RX_STATS, \
  73. sizeof_field(struct cpdma_chan_stats, m), \
  74. offsetof(struct cpdma_chan_stats, m)
  75. #define CPDMA_TX_STAT(m) CPDMA_TX_STATS, \
  76. sizeof_field(struct cpdma_chan_stats, m), \
  77. offsetof(struct cpdma_chan_stats, m)
  78. static const struct cpsw_stats cpsw_gstrings_stats[] = {
  79. { "Good Rx Frames", CPSW_STAT(rxgoodframes) },
  80. { "Broadcast Rx Frames", CPSW_STAT(rxbroadcastframes) },
  81. { "Multicast Rx Frames", CPSW_STAT(rxmulticastframes) },
  82. { "Pause Rx Frames", CPSW_STAT(rxpauseframes) },
  83. { "Rx CRC Errors", CPSW_STAT(rxcrcerrors) },
  84. { "Rx Align/Code Errors", CPSW_STAT(rxaligncodeerrors) },
  85. { "Oversize Rx Frames", CPSW_STAT(rxoversizedframes) },
  86. { "Rx Jabbers", CPSW_STAT(rxjabberframes) },
  87. { "Undersize (Short) Rx Frames", CPSW_STAT(rxundersizedframes) },
  88. { "Rx Fragments", CPSW_STAT(rxfragments) },
  89. { "Rx Octets", CPSW_STAT(rxoctets) },
  90. { "Good Tx Frames", CPSW_STAT(txgoodframes) },
  91. { "Broadcast Tx Frames", CPSW_STAT(txbroadcastframes) },
  92. { "Multicast Tx Frames", CPSW_STAT(txmulticastframes) },
  93. { "Pause Tx Frames", CPSW_STAT(txpauseframes) },
  94. { "Deferred Tx Frames", CPSW_STAT(txdeferredframes) },
  95. { "Collisions", CPSW_STAT(txcollisionframes) },
  96. { "Single Collision Tx Frames", CPSW_STAT(txsinglecollframes) },
  97. { "Multiple Collision Tx Frames", CPSW_STAT(txmultcollframes) },
  98. { "Excessive Collisions", CPSW_STAT(txexcessivecollisions) },
  99. { "Late Collisions", CPSW_STAT(txlatecollisions) },
  100. { "Tx Underrun", CPSW_STAT(txunderrun) },
  101. { "Carrier Sense Errors", CPSW_STAT(txcarriersenseerrors) },
  102. { "Tx Octets", CPSW_STAT(txoctets) },
  103. { "Rx + Tx 64 Octet Frames", CPSW_STAT(octetframes64) },
  104. { "Rx + Tx 65-127 Octet Frames", CPSW_STAT(octetframes65t127) },
  105. { "Rx + Tx 128-255 Octet Frames", CPSW_STAT(octetframes128t255) },
  106. { "Rx + Tx 256-511 Octet Frames", CPSW_STAT(octetframes256t511) },
  107. { "Rx + Tx 512-1023 Octet Frames", CPSW_STAT(octetframes512t1023) },
  108. { "Rx + Tx 1024-Up Octet Frames", CPSW_STAT(octetframes1024tup) },
  109. { "Net Octets", CPSW_STAT(netoctets) },
  110. { "Rx Start of Frame Overruns", CPSW_STAT(rxsofoverruns) },
  111. { "Rx Middle of Frame Overruns", CPSW_STAT(rxmofoverruns) },
  112. { "Rx DMA Overruns", CPSW_STAT(rxdmaoverruns) },
  113. };
  114. static const struct cpsw_stats cpsw_gstrings_ch_stats[] = {
  115. { "head_enqueue", CPDMA_RX_STAT(head_enqueue) },
  116. { "tail_enqueue", CPDMA_RX_STAT(tail_enqueue) },
  117. { "pad_enqueue", CPDMA_RX_STAT(pad_enqueue) },
  118. { "misqueued", CPDMA_RX_STAT(misqueued) },
  119. { "desc_alloc_fail", CPDMA_RX_STAT(desc_alloc_fail) },
  120. { "pad_alloc_fail", CPDMA_RX_STAT(pad_alloc_fail) },
  121. { "runt_receive_buf", CPDMA_RX_STAT(runt_receive_buff) },
  122. { "runt_transmit_buf", CPDMA_RX_STAT(runt_transmit_buff) },
  123. { "empty_dequeue", CPDMA_RX_STAT(empty_dequeue) },
  124. { "busy_dequeue", CPDMA_RX_STAT(busy_dequeue) },
  125. { "good_dequeue", CPDMA_RX_STAT(good_dequeue) },
  126. { "requeue", CPDMA_RX_STAT(requeue) },
  127. { "teardown_dequeue", CPDMA_RX_STAT(teardown_dequeue) },
  128. };
  129. #define CPSW_STATS_COMMON_LEN ARRAY_SIZE(cpsw_gstrings_stats)
  130. #define CPSW_STATS_CH_LEN ARRAY_SIZE(cpsw_gstrings_ch_stats)
  131. u32 cpsw_get_msglevel(struct net_device *ndev)
  132. {
  133. struct cpsw_priv *priv = netdev_priv(ndev);
  134. return priv->msg_enable;
  135. }
  136. void cpsw_set_msglevel(struct net_device *ndev, u32 value)
  137. {
  138. struct cpsw_priv *priv = netdev_priv(ndev);
  139. priv->msg_enable = value;
  140. }
  141. int cpsw_get_coalesce(struct net_device *ndev, struct ethtool_coalesce *coal,
  142. struct kernel_ethtool_coalesce *kernel_coal,
  143. struct netlink_ext_ack *extack)
  144. {
  145. struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
  146. coal->rx_coalesce_usecs = cpsw->coal_intvl;
  147. return 0;
  148. }
  149. int cpsw_set_coalesce(struct net_device *ndev, struct ethtool_coalesce *coal,
  150. struct kernel_ethtool_coalesce *kernel_coal,
  151. struct netlink_ext_ack *extack)
  152. {
  153. struct cpsw_priv *priv = netdev_priv(ndev);
  154. u32 int_ctrl;
  155. u32 num_interrupts = 0;
  156. u32 prescale = 0;
  157. u32 addnl_dvdr = 1;
  158. u32 coal_intvl = 0;
  159. struct cpsw_common *cpsw = priv->cpsw;
  160. coal_intvl = coal->rx_coalesce_usecs;
  161. int_ctrl = readl(&cpsw->wr_regs->int_control);
  162. prescale = cpsw->bus_freq_mhz * 4;
  163. if (!coal->rx_coalesce_usecs) {
  164. int_ctrl &= ~(CPSW_INTPRESCALE_MASK | CPSW_INTPACEEN);
  165. goto update_return;
  166. }
  167. if (coal_intvl < CPSW_CMINTMIN_INTVL)
  168. coal_intvl = CPSW_CMINTMIN_INTVL;
  169. if (coal_intvl > CPSW_CMINTMAX_INTVL) {
  170. /* Interrupt pacer works with 4us Pulse, we can
  171. * throttle further by dilating the 4us pulse.
  172. */
  173. addnl_dvdr = CPSW_INTPRESCALE_MASK / prescale;
  174. if (addnl_dvdr > 1) {
  175. prescale *= addnl_dvdr;
  176. if (coal_intvl > (CPSW_CMINTMAX_INTVL * addnl_dvdr))
  177. coal_intvl = (CPSW_CMINTMAX_INTVL
  178. * addnl_dvdr);
  179. } else {
  180. addnl_dvdr = 1;
  181. coal_intvl = CPSW_CMINTMAX_INTVL;
  182. }
  183. }
  184. num_interrupts = (1000 * addnl_dvdr) / coal_intvl;
  185. writel(num_interrupts, &cpsw->wr_regs->rx_imax);
  186. writel(num_interrupts, &cpsw->wr_regs->tx_imax);
  187. int_ctrl |= CPSW_INTPACEEN;
  188. int_ctrl &= (~CPSW_INTPRESCALE_MASK);
  189. int_ctrl |= (prescale & CPSW_INTPRESCALE_MASK);
  190. update_return:
  191. writel(int_ctrl, &cpsw->wr_regs->int_control);
  192. cpsw_notice(priv, timer, "Set coalesce to %d usecs.\n", coal_intvl);
  193. cpsw->coal_intvl = coal_intvl;
  194. return 0;
  195. }
  196. int cpsw_get_sset_count(struct net_device *ndev, int sset)
  197. {
  198. struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
  199. switch (sset) {
  200. case ETH_SS_STATS:
  201. return (CPSW_STATS_COMMON_LEN +
  202. (cpsw->rx_ch_num + cpsw->tx_ch_num) *
  203. CPSW_STATS_CH_LEN);
  204. default:
  205. return -EOPNOTSUPP;
  206. }
  207. }
  208. static void cpsw_add_ch_strings(u8 **p, int ch_num, int rx_dir)
  209. {
  210. int ch_stats_len;
  211. int line;
  212. int i;
  213. ch_stats_len = CPSW_STATS_CH_LEN * ch_num;
  214. for (i = 0; i < ch_stats_len; i++) {
  215. line = i % CPSW_STATS_CH_LEN;
  216. snprintf(*p, ETH_GSTRING_LEN,
  217. "%s DMA chan %ld: %s", rx_dir ? "Rx" : "Tx",
  218. (long)(i / CPSW_STATS_CH_LEN),
  219. cpsw_gstrings_ch_stats[line].stat_string);
  220. *p += ETH_GSTRING_LEN;
  221. }
  222. }
  223. void cpsw_get_strings(struct net_device *ndev, u32 stringset, u8 *data)
  224. {
  225. struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
  226. u8 *p = data;
  227. int i;
  228. switch (stringset) {
  229. case ETH_SS_STATS:
  230. for (i = 0; i < CPSW_STATS_COMMON_LEN; i++) {
  231. memcpy(p, cpsw_gstrings_stats[i].stat_string,
  232. ETH_GSTRING_LEN);
  233. p += ETH_GSTRING_LEN;
  234. }
  235. cpsw_add_ch_strings(&p, cpsw->rx_ch_num, 1);
  236. cpsw_add_ch_strings(&p, cpsw->tx_ch_num, 0);
  237. break;
  238. }
  239. }
  240. void cpsw_get_ethtool_stats(struct net_device *ndev,
  241. struct ethtool_stats *stats, u64 *data)
  242. {
  243. u8 *p;
  244. struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
  245. struct cpdma_chan_stats ch_stats;
  246. int i, l, ch;
  247. /* Collect Davinci CPDMA stats for Rx and Tx Channel */
  248. for (l = 0; l < CPSW_STATS_COMMON_LEN; l++)
  249. data[l] = readl(cpsw->hw_stats +
  250. cpsw_gstrings_stats[l].stat_offset);
  251. for (ch = 0; ch < cpsw->rx_ch_num; ch++) {
  252. cpdma_chan_get_stats(cpsw->rxv[ch].ch, &ch_stats);
  253. for (i = 0; i < CPSW_STATS_CH_LEN; i++, l++) {
  254. p = (u8 *)&ch_stats +
  255. cpsw_gstrings_ch_stats[i].stat_offset;
  256. data[l] = *(u32 *)p;
  257. }
  258. }
  259. for (ch = 0; ch < cpsw->tx_ch_num; ch++) {
  260. cpdma_chan_get_stats(cpsw->txv[ch].ch, &ch_stats);
  261. for (i = 0; i < CPSW_STATS_CH_LEN; i++, l++) {
  262. p = (u8 *)&ch_stats +
  263. cpsw_gstrings_ch_stats[i].stat_offset;
  264. data[l] = *(u32 *)p;
  265. }
  266. }
  267. }
  268. void cpsw_get_pauseparam(struct net_device *ndev,
  269. struct ethtool_pauseparam *pause)
  270. {
  271. struct cpsw_priv *priv = netdev_priv(ndev);
  272. pause->autoneg = AUTONEG_DISABLE;
  273. pause->rx_pause = priv->rx_pause ? true : false;
  274. pause->tx_pause = priv->tx_pause ? true : false;
  275. }
  276. void cpsw_get_wol(struct net_device *ndev, struct ethtool_wolinfo *wol)
  277. {
  278. struct cpsw_priv *priv = netdev_priv(ndev);
  279. struct cpsw_common *cpsw = priv->cpsw;
  280. int slave_no = cpsw_slave_index(cpsw, priv);
  281. wol->supported = 0;
  282. wol->wolopts = 0;
  283. if (cpsw->slaves[slave_no].phy)
  284. phy_ethtool_get_wol(cpsw->slaves[slave_no].phy, wol);
  285. }
  286. int cpsw_set_wol(struct net_device *ndev, struct ethtool_wolinfo *wol)
  287. {
  288. struct cpsw_priv *priv = netdev_priv(ndev);
  289. struct cpsw_common *cpsw = priv->cpsw;
  290. int slave_no = cpsw_slave_index(cpsw, priv);
  291. if (cpsw->slaves[slave_no].phy)
  292. return phy_ethtool_set_wol(cpsw->slaves[slave_no].phy, wol);
  293. else
  294. return -EOPNOTSUPP;
  295. }
  296. int cpsw_get_regs_len(struct net_device *ndev)
  297. {
  298. struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
  299. return cpsw_ale_get_num_entries(cpsw->ale) *
  300. ALE_ENTRY_WORDS * sizeof(u32);
  301. }
  302. void cpsw_get_regs(struct net_device *ndev, struct ethtool_regs *regs, void *p)
  303. {
  304. u32 *reg = p;
  305. struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
  306. /* update CPSW IP version */
  307. regs->version = cpsw->version;
  308. cpsw_ale_dump(cpsw->ale, reg);
  309. }
  310. int cpsw_ethtool_op_begin(struct net_device *ndev)
  311. {
  312. struct cpsw_priv *priv = netdev_priv(ndev);
  313. struct cpsw_common *cpsw = priv->cpsw;
  314. int ret;
  315. ret = pm_runtime_resume_and_get(cpsw->dev);
  316. if (ret < 0)
  317. cpsw_err(priv, drv, "ethtool begin failed %d\n", ret);
  318. return ret;
  319. }
  320. void cpsw_ethtool_op_complete(struct net_device *ndev)
  321. {
  322. struct cpsw_priv *priv = netdev_priv(ndev);
  323. pm_runtime_put(priv->cpsw->dev);
  324. }
  325. void cpsw_get_channels(struct net_device *ndev, struct ethtool_channels *ch)
  326. {
  327. struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
  328. ch->max_rx = cpsw->quirk_irq ? 1 : CPSW_MAX_QUEUES;
  329. ch->max_tx = cpsw->quirk_irq ? 1 : CPSW_MAX_QUEUES;
  330. ch->max_combined = 0;
  331. ch->max_other = 0;
  332. ch->other_count = 0;
  333. ch->rx_count = cpsw->rx_ch_num;
  334. ch->tx_count = cpsw->tx_ch_num;
  335. ch->combined_count = 0;
  336. }
  337. int cpsw_get_link_ksettings(struct net_device *ndev,
  338. struct ethtool_link_ksettings *ecmd)
  339. {
  340. struct cpsw_priv *priv = netdev_priv(ndev);
  341. struct cpsw_common *cpsw = priv->cpsw;
  342. int slave_no = cpsw_slave_index(cpsw, priv);
  343. if (!cpsw->slaves[slave_no].phy)
  344. return -EOPNOTSUPP;
  345. phy_ethtool_ksettings_get(cpsw->slaves[slave_no].phy, ecmd);
  346. return 0;
  347. }
  348. int cpsw_set_link_ksettings(struct net_device *ndev,
  349. const struct ethtool_link_ksettings *ecmd)
  350. {
  351. struct cpsw_priv *priv = netdev_priv(ndev);
  352. struct cpsw_common *cpsw = priv->cpsw;
  353. int slave_no = cpsw_slave_index(cpsw, priv);
  354. if (!cpsw->slaves[slave_no].phy)
  355. return -EOPNOTSUPP;
  356. return phy_ethtool_ksettings_set(cpsw->slaves[slave_no].phy, ecmd);
  357. }
  358. int cpsw_get_eee(struct net_device *ndev, struct ethtool_keee *edata)
  359. {
  360. struct cpsw_priv *priv = netdev_priv(ndev);
  361. struct cpsw_common *cpsw = priv->cpsw;
  362. int slave_no = cpsw_slave_index(cpsw, priv);
  363. if (cpsw->slaves[slave_no].phy)
  364. return phy_ethtool_get_eee(cpsw->slaves[slave_no].phy, edata);
  365. else
  366. return -EOPNOTSUPP;
  367. }
  368. int cpsw_nway_reset(struct net_device *ndev)
  369. {
  370. struct cpsw_priv *priv = netdev_priv(ndev);
  371. struct cpsw_common *cpsw = priv->cpsw;
  372. int slave_no = cpsw_slave_index(cpsw, priv);
  373. if (cpsw->slaves[slave_no].phy)
  374. return genphy_restart_aneg(cpsw->slaves[slave_no].phy);
  375. else
  376. return -EOPNOTSUPP;
  377. }
  378. static void cpsw_suspend_data_pass(struct net_device *ndev)
  379. {
  380. struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
  381. int i;
  382. /* Disable NAPI scheduling */
  383. cpsw_intr_disable(cpsw);
  384. /* Stop all transmit queues for every network device.
  385. */
  386. for (i = 0; i < cpsw->data.slaves; i++) {
  387. ndev = cpsw->slaves[i].ndev;
  388. if (!(ndev && netif_running(ndev)))
  389. continue;
  390. netif_tx_stop_all_queues(ndev);
  391. /* Barrier, so that stop_queue visible to other cpus */
  392. smp_mb__after_atomic();
  393. }
  394. /* Handle rest of tx packets and stop cpdma channels */
  395. cpdma_ctlr_stop(cpsw->dma);
  396. }
  397. static int cpsw_resume_data_pass(struct net_device *ndev)
  398. {
  399. struct cpsw_priv *priv = netdev_priv(ndev);
  400. struct cpsw_common *cpsw = priv->cpsw;
  401. int i, ret;
  402. /* After this receive is started */
  403. if (cpsw->usage_count) {
  404. ret = cpsw_fill_rx_channels(priv);
  405. if (ret)
  406. return ret;
  407. cpdma_ctlr_start(cpsw->dma);
  408. cpsw_intr_enable(cpsw);
  409. }
  410. /* Resume transmit for every affected interface */
  411. for (i = 0; i < cpsw->data.slaves; i++) {
  412. ndev = cpsw->slaves[i].ndev;
  413. if (ndev && netif_running(ndev))
  414. netif_tx_start_all_queues(ndev);
  415. }
  416. return 0;
  417. }
  418. static int cpsw_check_ch_settings(struct cpsw_common *cpsw,
  419. struct ethtool_channels *ch)
  420. {
  421. if (cpsw->quirk_irq) {
  422. dev_err(cpsw->dev, "Maximum one tx/rx queue is allowed");
  423. return -EOPNOTSUPP;
  424. }
  425. if (ch->combined_count)
  426. return -EINVAL;
  427. /* verify we have at least one channel in each direction */
  428. if (!ch->rx_count || !ch->tx_count)
  429. return -EINVAL;
  430. if (ch->rx_count > cpsw->data.channels ||
  431. ch->tx_count > cpsw->data.channels)
  432. return -EINVAL;
  433. return 0;
  434. }
  435. static int cpsw_update_channels_res(struct cpsw_priv *priv, int ch_num, int rx,
  436. cpdma_handler_fn rx_handler)
  437. {
  438. struct cpsw_common *cpsw = priv->cpsw;
  439. void (*handler)(void *, int, int);
  440. struct netdev_queue *queue;
  441. struct cpsw_vector *vec;
  442. int ret, *ch, vch;
  443. if (rx) {
  444. ch = &cpsw->rx_ch_num;
  445. vec = cpsw->rxv;
  446. handler = rx_handler;
  447. } else {
  448. ch = &cpsw->tx_ch_num;
  449. vec = cpsw->txv;
  450. handler = cpsw_tx_handler;
  451. }
  452. while (*ch < ch_num) {
  453. vch = rx ? *ch : 7 - *ch;
  454. vec[*ch].ch = cpdma_chan_create(cpsw->dma, vch, handler, rx);
  455. queue = netdev_get_tx_queue(priv->ndev, *ch);
  456. queue->tx_maxrate = 0;
  457. if (IS_ERR(vec[*ch].ch))
  458. return PTR_ERR(vec[*ch].ch);
  459. if (!vec[*ch].ch)
  460. return -EINVAL;
  461. cpsw_info(priv, ifup, "created new %d %s channel\n", *ch,
  462. (rx ? "rx" : "tx"));
  463. (*ch)++;
  464. }
  465. while (*ch > ch_num) {
  466. (*ch)--;
  467. ret = cpdma_chan_destroy(vec[*ch].ch);
  468. if (ret)
  469. return ret;
  470. cpsw_info(priv, ifup, "destroyed %d %s channel\n", *ch,
  471. (rx ? "rx" : "tx"));
  472. }
  473. return 0;
  474. }
  475. static void cpsw_fail(struct cpsw_common *cpsw)
  476. {
  477. struct net_device *ndev;
  478. int i;
  479. for (i = 0; i < cpsw->data.slaves; i++) {
  480. ndev = cpsw->slaves[i].ndev;
  481. if (ndev)
  482. dev_close(ndev);
  483. }
  484. }
  485. int cpsw_set_channels_common(struct net_device *ndev,
  486. struct ethtool_channels *chs,
  487. cpdma_handler_fn rx_handler)
  488. {
  489. struct cpsw_priv *priv = netdev_priv(ndev);
  490. struct cpsw_common *cpsw = priv->cpsw;
  491. struct net_device *sl_ndev;
  492. int i, new_pools, ret;
  493. ret = cpsw_check_ch_settings(cpsw, chs);
  494. if (ret < 0)
  495. return ret;
  496. cpsw_suspend_data_pass(ndev);
  497. new_pools = (chs->rx_count != cpsw->rx_ch_num) && cpsw->usage_count;
  498. ret = cpsw_update_channels_res(priv, chs->rx_count, 1, rx_handler);
  499. if (ret)
  500. goto err;
  501. ret = cpsw_update_channels_res(priv, chs->tx_count, 0, rx_handler);
  502. if (ret)
  503. goto err;
  504. for (i = 0; i < cpsw->data.slaves; i++) {
  505. sl_ndev = cpsw->slaves[i].ndev;
  506. if (!(sl_ndev && netif_running(sl_ndev)))
  507. continue;
  508. /* Inform stack about new count of queues */
  509. ret = netif_set_real_num_tx_queues(sl_ndev, cpsw->tx_ch_num);
  510. if (ret) {
  511. dev_err(priv->dev, "cannot set real number of tx queues\n");
  512. goto err;
  513. }
  514. ret = netif_set_real_num_rx_queues(sl_ndev, cpsw->rx_ch_num);
  515. if (ret) {
  516. dev_err(priv->dev, "cannot set real number of rx queues\n");
  517. goto err;
  518. }
  519. }
  520. cpsw_split_res(cpsw);
  521. if (new_pools) {
  522. cpsw_destroy_xdp_rxqs(cpsw);
  523. ret = cpsw_create_xdp_rxqs(cpsw);
  524. if (ret)
  525. goto err;
  526. }
  527. ret = cpsw_resume_data_pass(ndev);
  528. if (!ret)
  529. return 0;
  530. err:
  531. dev_err(priv->dev, "cannot update channels number, closing device\n");
  532. cpsw_fail(cpsw);
  533. return ret;
  534. }
  535. void cpsw_get_ringparam(struct net_device *ndev,
  536. struct ethtool_ringparam *ering,
  537. struct kernel_ethtool_ringparam *kernel_ering,
  538. struct netlink_ext_ack *extack)
  539. {
  540. struct cpsw_priv *priv = netdev_priv(ndev);
  541. struct cpsw_common *cpsw = priv->cpsw;
  542. /* not supported */
  543. ering->tx_max_pending = cpsw->descs_pool_size - CPSW_MAX_QUEUES;
  544. ering->tx_pending = cpdma_get_num_tx_descs(cpsw->dma);
  545. ering->rx_max_pending = cpsw->descs_pool_size - CPSW_MAX_QUEUES;
  546. ering->rx_pending = cpdma_get_num_rx_descs(cpsw->dma);
  547. }
  548. int cpsw_set_ringparam(struct net_device *ndev,
  549. struct ethtool_ringparam *ering,
  550. struct kernel_ethtool_ringparam *kernel_ering,
  551. struct netlink_ext_ack *extack)
  552. {
  553. struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
  554. int descs_num, ret;
  555. /* ignore ering->tx_pending - only rx_pending adjustment is supported */
  556. if (ering->rx_mini_pending || ering->rx_jumbo_pending ||
  557. ering->rx_pending < CPSW_MAX_QUEUES ||
  558. ering->rx_pending > (cpsw->descs_pool_size - CPSW_MAX_QUEUES))
  559. return -EINVAL;
  560. descs_num = cpdma_get_num_rx_descs(cpsw->dma);
  561. if (ering->rx_pending == descs_num)
  562. return 0;
  563. cpsw_suspend_data_pass(ndev);
  564. ret = cpdma_set_num_rx_descs(cpsw->dma, ering->rx_pending);
  565. if (ret) {
  566. if (cpsw_resume_data_pass(ndev))
  567. goto err;
  568. return ret;
  569. }
  570. if (cpsw->usage_count) {
  571. cpsw_destroy_xdp_rxqs(cpsw);
  572. ret = cpsw_create_xdp_rxqs(cpsw);
  573. if (ret)
  574. goto err;
  575. }
  576. ret = cpsw_resume_data_pass(ndev);
  577. if (!ret)
  578. return 0;
  579. err:
  580. cpdma_set_num_rx_descs(cpsw->dma, descs_num);
  581. dev_err(cpsw->dev, "cannot set ring params, closing device\n");
  582. cpsw_fail(cpsw);
  583. return ret;
  584. }
  585. #if IS_ENABLED(CONFIG_TI_CPTS)
  586. int cpsw_get_ts_info(struct net_device *ndev, struct kernel_ethtool_ts_info *info)
  587. {
  588. struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
  589. info->so_timestamping =
  590. SOF_TIMESTAMPING_TX_HARDWARE |
  591. SOF_TIMESTAMPING_TX_SOFTWARE |
  592. SOF_TIMESTAMPING_RX_HARDWARE |
  593. SOF_TIMESTAMPING_RAW_HARDWARE;
  594. info->phc_index = cpsw->cpts->phc_index;
  595. info->tx_types =
  596. (1 << HWTSTAMP_TX_OFF) |
  597. (1 << HWTSTAMP_TX_ON);
  598. info->rx_filters =
  599. (1 << HWTSTAMP_FILTER_NONE) |
  600. (1 << HWTSTAMP_FILTER_PTP_V2_EVENT);
  601. return 0;
  602. }
  603. #else
  604. int cpsw_get_ts_info(struct net_device *ndev, struct kernel_ethtool_ts_info *info)
  605. {
  606. info->so_timestamping =
  607. SOF_TIMESTAMPING_TX_SOFTWARE;
  608. info->tx_types = 0;
  609. info->rx_filters = 0;
  610. return 0;
  611. }
  612. #endif