cpsw_priv.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586
  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/bpf.h>
  8. #include <linux/bpf_trace.h>
  9. #include <linux/if_ether.h>
  10. #include <linux/if_vlan.h>
  11. #include <linux/kmemleak.h>
  12. #include <linux/module.h>
  13. #include <linux/netdevice.h>
  14. #include <linux/net_tstamp.h>
  15. #include <linux/of.h>
  16. #include <linux/phy.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/pm_runtime.h>
  19. #include <linux/skbuff.h>
  20. #include <net/page_pool/helpers.h>
  21. #include <net/pkt_cls.h>
  22. #include <net/pkt_sched.h>
  23. #include "cpsw.h"
  24. #include "cpts.h"
  25. #include "cpsw_ale.h"
  26. #include "cpsw_priv.h"
  27. #include "cpsw_sl.h"
  28. #include "davinci_cpdma.h"
  29. #define CPTS_N_ETX_TS 4
  30. int (*cpsw_slave_index)(struct cpsw_common *cpsw, struct cpsw_priv *priv);
  31. void cpsw_intr_enable(struct cpsw_common *cpsw)
  32. {
  33. writel_relaxed(0xFF, &cpsw->wr_regs->tx_en);
  34. writel_relaxed(0xFF, &cpsw->wr_regs->rx_en);
  35. cpdma_ctlr_int_ctrl(cpsw->dma, true);
  36. }
  37. void cpsw_intr_disable(struct cpsw_common *cpsw)
  38. {
  39. writel_relaxed(0, &cpsw->wr_regs->tx_en);
  40. writel_relaxed(0, &cpsw->wr_regs->rx_en);
  41. cpdma_ctlr_int_ctrl(cpsw->dma, false);
  42. }
  43. void cpsw_tx_handler(void *token, int len, int status)
  44. {
  45. struct cpsw_meta_xdp *xmeta;
  46. struct xdp_frame *xdpf;
  47. struct net_device *ndev;
  48. struct netdev_queue *txq;
  49. struct sk_buff *skb;
  50. int ch;
  51. if (cpsw_is_xdpf_handle(token)) {
  52. xdpf = cpsw_handle_to_xdpf(token);
  53. xmeta = (void *)xdpf + CPSW_XMETA_OFFSET;
  54. ndev = xmeta->ndev;
  55. ch = xmeta->ch;
  56. xdp_return_frame(xdpf);
  57. } else {
  58. skb = token;
  59. ndev = skb->dev;
  60. ch = skb_get_queue_mapping(skb);
  61. cpts_tx_timestamp(ndev_to_cpsw(ndev)->cpts, skb);
  62. dev_kfree_skb_any(skb);
  63. }
  64. /* Check whether the queue is stopped due to stalled tx dma, if the
  65. * queue is stopped then start the queue as we have free desc for tx
  66. */
  67. txq = netdev_get_tx_queue(ndev, ch);
  68. if (unlikely(netif_tx_queue_stopped(txq)))
  69. netif_tx_wake_queue(txq);
  70. ndev->stats.tx_packets++;
  71. ndev->stats.tx_bytes += len;
  72. }
  73. irqreturn_t cpsw_tx_interrupt(int irq, void *dev_id)
  74. {
  75. struct cpsw_common *cpsw = dev_id;
  76. writel(0, &cpsw->wr_regs->tx_en);
  77. cpdma_ctlr_eoi(cpsw->dma, CPDMA_EOI_TX);
  78. if (cpsw->quirk_irq) {
  79. disable_irq_nosync(cpsw->irqs_table[1]);
  80. cpsw->tx_irq_disabled = true;
  81. }
  82. napi_schedule(&cpsw->napi_tx);
  83. return IRQ_HANDLED;
  84. }
  85. irqreturn_t cpsw_rx_interrupt(int irq, void *dev_id)
  86. {
  87. struct cpsw_common *cpsw = dev_id;
  88. writel(0, &cpsw->wr_regs->rx_en);
  89. cpdma_ctlr_eoi(cpsw->dma, CPDMA_EOI_RX);
  90. if (cpsw->quirk_irq) {
  91. disable_irq_nosync(cpsw->irqs_table[0]);
  92. cpsw->rx_irq_disabled = true;
  93. }
  94. napi_schedule(&cpsw->napi_rx);
  95. return IRQ_HANDLED;
  96. }
  97. irqreturn_t cpsw_misc_interrupt(int irq, void *dev_id)
  98. {
  99. struct cpsw_common *cpsw = dev_id;
  100. writel(0, &cpsw->wr_regs->misc_en);
  101. cpdma_ctlr_eoi(cpsw->dma, CPDMA_EOI_MISC);
  102. cpts_misc_interrupt(cpsw->cpts);
  103. writel(0x10, &cpsw->wr_regs->misc_en);
  104. return IRQ_HANDLED;
  105. }
  106. int cpsw_tx_mq_poll(struct napi_struct *napi_tx, int budget)
  107. {
  108. struct cpsw_common *cpsw = napi_to_cpsw(napi_tx);
  109. int num_tx, cur_budget, ch;
  110. u32 ch_map;
  111. struct cpsw_vector *txv;
  112. /* process every unprocessed channel */
  113. ch_map = cpdma_ctrl_txchs_state(cpsw->dma);
  114. for (ch = 0, num_tx = 0; ch_map & 0xff; ch_map <<= 1, ch++) {
  115. if (!(ch_map & 0x80))
  116. continue;
  117. txv = &cpsw->txv[ch];
  118. if (unlikely(txv->budget > budget - num_tx))
  119. cur_budget = budget - num_tx;
  120. else
  121. cur_budget = txv->budget;
  122. num_tx += cpdma_chan_process(txv->ch, cur_budget);
  123. if (num_tx >= budget)
  124. break;
  125. }
  126. if (num_tx < budget) {
  127. napi_complete(napi_tx);
  128. writel(0xff, &cpsw->wr_regs->tx_en);
  129. }
  130. return num_tx;
  131. }
  132. int cpsw_tx_poll(struct napi_struct *napi_tx, int budget)
  133. {
  134. struct cpsw_common *cpsw = napi_to_cpsw(napi_tx);
  135. int num_tx;
  136. num_tx = cpdma_chan_process(cpsw->txv[0].ch, budget);
  137. if (num_tx < budget) {
  138. napi_complete(napi_tx);
  139. writel(0xff, &cpsw->wr_regs->tx_en);
  140. if (cpsw->tx_irq_disabled) {
  141. cpsw->tx_irq_disabled = false;
  142. enable_irq(cpsw->irqs_table[1]);
  143. }
  144. }
  145. return num_tx;
  146. }
  147. int cpsw_rx_mq_poll(struct napi_struct *napi_rx, int budget)
  148. {
  149. struct cpsw_common *cpsw = napi_to_cpsw(napi_rx);
  150. int num_rx, cur_budget, ch;
  151. u32 ch_map;
  152. struct cpsw_vector *rxv;
  153. /* process every unprocessed channel */
  154. ch_map = cpdma_ctrl_rxchs_state(cpsw->dma);
  155. for (ch = 0, num_rx = 0; ch_map; ch_map >>= 1, ch++) {
  156. if (!(ch_map & 0x01))
  157. continue;
  158. rxv = &cpsw->rxv[ch];
  159. if (unlikely(rxv->budget > budget - num_rx))
  160. cur_budget = budget - num_rx;
  161. else
  162. cur_budget = rxv->budget;
  163. num_rx += cpdma_chan_process(rxv->ch, cur_budget);
  164. if (num_rx >= budget)
  165. break;
  166. }
  167. if (num_rx < budget) {
  168. napi_complete_done(napi_rx, num_rx);
  169. writel(0xff, &cpsw->wr_regs->rx_en);
  170. }
  171. return num_rx;
  172. }
  173. int cpsw_rx_poll(struct napi_struct *napi_rx, int budget)
  174. {
  175. struct cpsw_common *cpsw = napi_to_cpsw(napi_rx);
  176. int num_rx;
  177. num_rx = cpdma_chan_process(cpsw->rxv[0].ch, budget);
  178. if (num_rx < budget) {
  179. napi_complete_done(napi_rx, num_rx);
  180. writel(0xff, &cpsw->wr_regs->rx_en);
  181. if (cpsw->rx_irq_disabled) {
  182. cpsw->rx_irq_disabled = false;
  183. enable_irq(cpsw->irqs_table[0]);
  184. }
  185. }
  186. return num_rx;
  187. }
  188. void cpsw_rx_vlan_encap(struct sk_buff *skb)
  189. {
  190. struct cpsw_priv *priv = netdev_priv(skb->dev);
  191. u32 rx_vlan_encap_hdr = *((u32 *)skb->data);
  192. struct cpsw_common *cpsw = priv->cpsw;
  193. u16 vtag, vid, prio, pkt_type;
  194. /* Remove VLAN header encapsulation word */
  195. skb_pull(skb, CPSW_RX_VLAN_ENCAP_HDR_SIZE);
  196. pkt_type = (rx_vlan_encap_hdr >>
  197. CPSW_RX_VLAN_ENCAP_HDR_PKT_TYPE_SHIFT) &
  198. CPSW_RX_VLAN_ENCAP_HDR_PKT_TYPE_MSK;
  199. /* Ignore unknown & Priority-tagged packets*/
  200. if (pkt_type == CPSW_RX_VLAN_ENCAP_HDR_PKT_RESERV ||
  201. pkt_type == CPSW_RX_VLAN_ENCAP_HDR_PKT_PRIO_TAG)
  202. return;
  203. vid = (rx_vlan_encap_hdr >>
  204. CPSW_RX_VLAN_ENCAP_HDR_VID_SHIFT) &
  205. VLAN_VID_MASK;
  206. /* Ignore vid 0 and pass packet as is */
  207. if (!vid)
  208. return;
  209. /* Untag P0 packets if set for vlan */
  210. if (!cpsw_ale_get_vlan_p0_untag(cpsw->ale, vid)) {
  211. prio = (rx_vlan_encap_hdr >>
  212. CPSW_RX_VLAN_ENCAP_HDR_PRIO_SHIFT) &
  213. CPSW_RX_VLAN_ENCAP_HDR_PRIO_MSK;
  214. vtag = (prio << VLAN_PRIO_SHIFT) | vid;
  215. __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vtag);
  216. }
  217. /* strip vlan tag for VLAN-tagged packet */
  218. if (pkt_type == CPSW_RX_VLAN_ENCAP_HDR_PKT_VLAN_TAG) {
  219. memmove(skb->data + VLAN_HLEN, skb->data, 2 * ETH_ALEN);
  220. skb_pull(skb, VLAN_HLEN);
  221. }
  222. }
  223. void cpsw_set_slave_mac(struct cpsw_slave *slave, struct cpsw_priv *priv)
  224. {
  225. slave_write(slave, mac_hi(priv->mac_addr), SA_HI);
  226. slave_write(slave, mac_lo(priv->mac_addr), SA_LO);
  227. }
  228. void soft_reset(const char *module, void __iomem *reg)
  229. {
  230. unsigned long timeout = jiffies + HZ;
  231. writel_relaxed(1, reg);
  232. do {
  233. cpu_relax();
  234. } while ((readl_relaxed(reg) & 1) && time_after(timeout, jiffies));
  235. WARN(readl_relaxed(reg) & 1, "failed to soft-reset %s\n", module);
  236. }
  237. void cpsw_ndo_tx_timeout(struct net_device *ndev, unsigned int txqueue)
  238. {
  239. struct cpsw_priv *priv = netdev_priv(ndev);
  240. struct cpsw_common *cpsw = priv->cpsw;
  241. int ch;
  242. cpsw_err(priv, tx_err, "transmit timeout, restarting dma\n");
  243. ndev->stats.tx_errors++;
  244. cpsw_intr_disable(cpsw);
  245. for (ch = 0; ch < cpsw->tx_ch_num; ch++) {
  246. cpdma_chan_stop(cpsw->txv[ch].ch);
  247. cpdma_chan_start(cpsw->txv[ch].ch);
  248. }
  249. cpsw_intr_enable(cpsw);
  250. netif_trans_update(ndev);
  251. netif_tx_wake_all_queues(ndev);
  252. }
  253. static int cpsw_get_common_speed(struct cpsw_common *cpsw)
  254. {
  255. int i, speed;
  256. for (i = 0, speed = 0; i < cpsw->data.slaves; i++)
  257. if (cpsw->slaves[i].phy && cpsw->slaves[i].phy->link)
  258. speed += cpsw->slaves[i].phy->speed;
  259. return speed;
  260. }
  261. int cpsw_need_resplit(struct cpsw_common *cpsw)
  262. {
  263. int i, rlim_ch_num;
  264. int speed, ch_rate;
  265. /* re-split resources only in case speed was changed */
  266. speed = cpsw_get_common_speed(cpsw);
  267. if (speed == cpsw->speed || !speed)
  268. return 0;
  269. cpsw->speed = speed;
  270. for (i = 0, rlim_ch_num = 0; i < cpsw->tx_ch_num; i++) {
  271. ch_rate = cpdma_chan_get_rate(cpsw->txv[i].ch);
  272. if (!ch_rate)
  273. break;
  274. rlim_ch_num++;
  275. }
  276. /* cases not dependent on speed */
  277. if (!rlim_ch_num || rlim_ch_num == cpsw->tx_ch_num)
  278. return 0;
  279. return 1;
  280. }
  281. void cpsw_split_res(struct cpsw_common *cpsw)
  282. {
  283. u32 consumed_rate = 0, bigest_rate = 0;
  284. struct cpsw_vector *txv = cpsw->txv;
  285. int i, ch_weight, rlim_ch_num = 0;
  286. int budget, bigest_rate_ch = 0;
  287. u32 ch_rate, max_rate;
  288. int ch_budget = 0;
  289. for (i = 0; i < cpsw->tx_ch_num; i++) {
  290. ch_rate = cpdma_chan_get_rate(txv[i].ch);
  291. if (!ch_rate)
  292. continue;
  293. rlim_ch_num++;
  294. consumed_rate += ch_rate;
  295. }
  296. if (cpsw->tx_ch_num == rlim_ch_num) {
  297. max_rate = consumed_rate;
  298. } else if (!rlim_ch_num) {
  299. ch_budget = NAPI_POLL_WEIGHT / cpsw->tx_ch_num;
  300. bigest_rate = 0;
  301. max_rate = consumed_rate;
  302. } else {
  303. max_rate = cpsw->speed * 1000;
  304. /* if max_rate is less then expected due to reduced link speed,
  305. * split proportionally according next potential max speed
  306. */
  307. if (max_rate < consumed_rate)
  308. max_rate *= 10;
  309. if (max_rate < consumed_rate)
  310. max_rate *= 10;
  311. ch_budget = (consumed_rate * NAPI_POLL_WEIGHT) / max_rate;
  312. ch_budget = (NAPI_POLL_WEIGHT - ch_budget) /
  313. (cpsw->tx_ch_num - rlim_ch_num);
  314. bigest_rate = (max_rate - consumed_rate) /
  315. (cpsw->tx_ch_num - rlim_ch_num);
  316. }
  317. /* split tx weight/budget */
  318. budget = NAPI_POLL_WEIGHT;
  319. for (i = 0; i < cpsw->tx_ch_num; i++) {
  320. ch_rate = cpdma_chan_get_rate(txv[i].ch);
  321. if (ch_rate) {
  322. txv[i].budget = (ch_rate * NAPI_POLL_WEIGHT) / max_rate;
  323. if (!txv[i].budget)
  324. txv[i].budget++;
  325. if (ch_rate > bigest_rate) {
  326. bigest_rate_ch = i;
  327. bigest_rate = ch_rate;
  328. }
  329. ch_weight = (ch_rate * 100) / max_rate;
  330. if (!ch_weight)
  331. ch_weight++;
  332. cpdma_chan_set_weight(cpsw->txv[i].ch, ch_weight);
  333. } else {
  334. txv[i].budget = ch_budget;
  335. if (!bigest_rate_ch)
  336. bigest_rate_ch = i;
  337. cpdma_chan_set_weight(cpsw->txv[i].ch, 0);
  338. }
  339. budget -= txv[i].budget;
  340. }
  341. if (budget)
  342. txv[bigest_rate_ch].budget += budget;
  343. /* split rx budget */
  344. budget = NAPI_POLL_WEIGHT;
  345. ch_budget = budget / cpsw->rx_ch_num;
  346. for (i = 0; i < cpsw->rx_ch_num; i++) {
  347. cpsw->rxv[i].budget = ch_budget;
  348. budget -= ch_budget;
  349. }
  350. if (budget)
  351. cpsw->rxv[0].budget += budget;
  352. }
  353. int cpsw_init_common(struct cpsw_common *cpsw, void __iomem *ss_regs,
  354. int ale_ageout, phys_addr_t desc_mem_phys,
  355. int descs_pool_size)
  356. {
  357. u32 slave_offset, sliver_offset, slave_size;
  358. struct cpsw_ale_params ale_params;
  359. struct cpsw_platform_data *data;
  360. struct cpdma_params dma_params;
  361. struct device *dev = cpsw->dev;
  362. struct device_node *cpts_node;
  363. void __iomem *cpts_regs;
  364. int ret = 0, i;
  365. data = &cpsw->data;
  366. cpsw->rx_ch_num = 1;
  367. cpsw->tx_ch_num = 1;
  368. cpsw->version = readl(&cpsw->regs->id_ver);
  369. memset(&dma_params, 0, sizeof(dma_params));
  370. memset(&ale_params, 0, sizeof(ale_params));
  371. switch (cpsw->version) {
  372. case CPSW_VERSION_1:
  373. cpsw->host_port_regs = ss_regs + CPSW1_HOST_PORT_OFFSET;
  374. cpts_regs = ss_regs + CPSW1_CPTS_OFFSET;
  375. cpsw->hw_stats = ss_regs + CPSW1_HW_STATS;
  376. dma_params.dmaregs = ss_regs + CPSW1_CPDMA_OFFSET;
  377. dma_params.txhdp = ss_regs + CPSW1_STATERAM_OFFSET;
  378. ale_params.ale_regs = ss_regs + CPSW1_ALE_OFFSET;
  379. slave_offset = CPSW1_SLAVE_OFFSET;
  380. slave_size = CPSW1_SLAVE_SIZE;
  381. sliver_offset = CPSW1_SLIVER_OFFSET;
  382. dma_params.desc_mem_phys = 0;
  383. break;
  384. case CPSW_VERSION_2:
  385. case CPSW_VERSION_3:
  386. case CPSW_VERSION_4:
  387. cpsw->host_port_regs = ss_regs + CPSW2_HOST_PORT_OFFSET;
  388. cpts_regs = ss_regs + CPSW2_CPTS_OFFSET;
  389. cpsw->hw_stats = ss_regs + CPSW2_HW_STATS;
  390. dma_params.dmaregs = ss_regs + CPSW2_CPDMA_OFFSET;
  391. dma_params.txhdp = ss_regs + CPSW2_STATERAM_OFFSET;
  392. ale_params.ale_regs = ss_regs + CPSW2_ALE_OFFSET;
  393. slave_offset = CPSW2_SLAVE_OFFSET;
  394. slave_size = CPSW2_SLAVE_SIZE;
  395. sliver_offset = CPSW2_SLIVER_OFFSET;
  396. dma_params.desc_mem_phys = desc_mem_phys;
  397. break;
  398. default:
  399. dev_err(dev, "unknown version 0x%08x\n", cpsw->version);
  400. return -ENODEV;
  401. }
  402. for (i = 0; i < cpsw->data.slaves; i++) {
  403. struct cpsw_slave *slave = &cpsw->slaves[i];
  404. void __iomem *regs = cpsw->regs;
  405. slave->slave_num = i;
  406. slave->data = &cpsw->data.slave_data[i];
  407. slave->regs = regs + slave_offset;
  408. slave->port_vlan = slave->data->dual_emac_res_vlan;
  409. slave->mac_sl = cpsw_sl_get("cpsw", dev, regs + sliver_offset);
  410. if (IS_ERR(slave->mac_sl))
  411. return PTR_ERR(slave->mac_sl);
  412. slave_offset += slave_size;
  413. sliver_offset += SLIVER_SIZE;
  414. }
  415. ale_params.dev = dev;
  416. ale_params.ale_ageout = ale_ageout;
  417. ale_params.ale_ports = CPSW_ALE_PORTS_NUM;
  418. ale_params.dev_id = "cpsw";
  419. ale_params.bus_freq = cpsw->bus_freq_mhz * 1000000;
  420. cpsw->ale = cpsw_ale_create(&ale_params);
  421. if (IS_ERR(cpsw->ale)) {
  422. dev_err(dev, "error initializing ale engine\n");
  423. return PTR_ERR(cpsw->ale);
  424. }
  425. dma_params.dev = dev;
  426. dma_params.rxthresh = dma_params.dmaregs + CPDMA_RXTHRESH;
  427. dma_params.rxfree = dma_params.dmaregs + CPDMA_RXFREE;
  428. dma_params.rxhdp = dma_params.txhdp + CPDMA_RXHDP;
  429. dma_params.txcp = dma_params.txhdp + CPDMA_TXCP;
  430. dma_params.rxcp = dma_params.txhdp + CPDMA_RXCP;
  431. dma_params.num_chan = data->channels;
  432. dma_params.has_soft_reset = true;
  433. dma_params.min_packet_size = CPSW_MIN_PACKET_SIZE;
  434. dma_params.desc_mem_size = data->bd_ram_size;
  435. dma_params.desc_align = 16;
  436. dma_params.has_ext_regs = true;
  437. dma_params.desc_hw_addr = dma_params.desc_mem_phys;
  438. dma_params.bus_freq_mhz = cpsw->bus_freq_mhz;
  439. dma_params.descs_pool_size = descs_pool_size;
  440. cpsw->dma = cpdma_ctlr_create(&dma_params);
  441. if (!cpsw->dma) {
  442. dev_err(dev, "error initializing dma\n");
  443. return -ENOMEM;
  444. }
  445. cpts_node = of_get_child_by_name(cpsw->dev->of_node, "cpts");
  446. if (!cpts_node)
  447. cpts_node = cpsw->dev->of_node;
  448. cpsw->cpts = cpts_create(cpsw->dev, cpts_regs, cpts_node,
  449. CPTS_N_ETX_TS);
  450. if (IS_ERR(cpsw->cpts)) {
  451. ret = PTR_ERR(cpsw->cpts);
  452. cpdma_ctlr_destroy(cpsw->dma);
  453. }
  454. of_node_put(cpts_node);
  455. return ret;
  456. }
  457. #if IS_ENABLED(CONFIG_TI_CPTS)
  458. static void cpsw_hwtstamp_v1(struct cpsw_priv *priv)
  459. {
  460. struct cpsw_common *cpsw = priv->cpsw;
  461. struct cpsw_slave *slave = &cpsw->slaves[cpsw_slave_index(cpsw, priv)];
  462. u32 ts_en, seq_id;
  463. if (!priv->tx_ts_enabled && !priv->rx_ts_enabled) {
  464. slave_write(slave, 0, CPSW1_TS_CTL);
  465. return;
  466. }
  467. seq_id = (30 << CPSW_V1_SEQ_ID_OFS_SHIFT) | ETH_P_1588;
  468. ts_en = EVENT_MSG_BITS << CPSW_V1_MSG_TYPE_OFS;
  469. if (priv->tx_ts_enabled)
  470. ts_en |= CPSW_V1_TS_TX_EN;
  471. if (priv->rx_ts_enabled)
  472. ts_en |= CPSW_V1_TS_RX_EN;
  473. slave_write(slave, ts_en, CPSW1_TS_CTL);
  474. slave_write(slave, seq_id, CPSW1_TS_SEQ_LTYPE);
  475. }
  476. static void cpsw_hwtstamp_v2(struct cpsw_priv *priv)
  477. {
  478. struct cpsw_common *cpsw = priv->cpsw;
  479. struct cpsw_slave *slave;
  480. u32 ctrl, mtype;
  481. slave = &cpsw->slaves[cpsw_slave_index(cpsw, priv)];
  482. ctrl = slave_read(slave, CPSW2_CONTROL);
  483. switch (cpsw->version) {
  484. case CPSW_VERSION_2:
  485. ctrl &= ~CTRL_V2_ALL_TS_MASK;
  486. if (priv->tx_ts_enabled)
  487. ctrl |= CTRL_V2_TX_TS_BITS;
  488. if (priv->rx_ts_enabled)
  489. ctrl |= CTRL_V2_RX_TS_BITS;
  490. break;
  491. case CPSW_VERSION_3:
  492. default:
  493. ctrl &= ~CTRL_V3_ALL_TS_MASK;
  494. if (priv->tx_ts_enabled)
  495. ctrl |= CTRL_V3_TX_TS_BITS;
  496. if (priv->rx_ts_enabled)
  497. ctrl |= CTRL_V3_RX_TS_BITS;
  498. break;
  499. }
  500. mtype = (30 << TS_SEQ_ID_OFFSET_SHIFT) | EVENT_MSG_BITS;
  501. slave_write(slave, mtype, CPSW2_TS_SEQ_MTYPE);
  502. slave_write(slave, ctrl, CPSW2_CONTROL);
  503. writel_relaxed(ETH_P_1588, &cpsw->regs->ts_ltype);
  504. writel_relaxed(ETH_P_8021Q, &cpsw->regs->vlan_ltype);
  505. }
  506. static int cpsw_hwtstamp_set(struct net_device *dev, struct ifreq *ifr)
  507. {
  508. struct cpsw_priv *priv = netdev_priv(dev);
  509. struct cpsw_common *cpsw = priv->cpsw;
  510. struct hwtstamp_config cfg;
  511. if (cpsw->version != CPSW_VERSION_1 &&
  512. cpsw->version != CPSW_VERSION_2 &&
  513. cpsw->version != CPSW_VERSION_3)
  514. return -EOPNOTSUPP;
  515. if (copy_from_user(&cfg, ifr->ifr_data, sizeof(cfg)))
  516. return -EFAULT;
  517. if (cfg.tx_type != HWTSTAMP_TX_OFF && cfg.tx_type != HWTSTAMP_TX_ON)
  518. return -ERANGE;
  519. switch (cfg.rx_filter) {
  520. case HWTSTAMP_FILTER_NONE:
  521. priv->rx_ts_enabled = 0;
  522. break;
  523. case HWTSTAMP_FILTER_ALL:
  524. case HWTSTAMP_FILTER_NTP_ALL:
  525. case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
  526. case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
  527. case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
  528. return -ERANGE;
  529. case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
  530. case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
  531. case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
  532. case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
  533. case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
  534. case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
  535. case HWTSTAMP_FILTER_PTP_V2_EVENT:
  536. case HWTSTAMP_FILTER_PTP_V2_SYNC:
  537. case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
  538. priv->rx_ts_enabled = HWTSTAMP_FILTER_PTP_V2_EVENT;
  539. cfg.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
  540. break;
  541. default:
  542. return -ERANGE;
  543. }
  544. priv->tx_ts_enabled = cfg.tx_type == HWTSTAMP_TX_ON;
  545. switch (cpsw->version) {
  546. case CPSW_VERSION_1:
  547. cpsw_hwtstamp_v1(priv);
  548. break;
  549. case CPSW_VERSION_2:
  550. case CPSW_VERSION_3:
  551. cpsw_hwtstamp_v2(priv);
  552. break;
  553. default:
  554. WARN_ON(1);
  555. }
  556. return copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)) ? -EFAULT : 0;
  557. }
  558. static int cpsw_hwtstamp_get(struct net_device *dev, struct ifreq *ifr)
  559. {
  560. struct cpsw_common *cpsw = ndev_to_cpsw(dev);
  561. struct cpsw_priv *priv = netdev_priv(dev);
  562. struct hwtstamp_config cfg;
  563. if (cpsw->version != CPSW_VERSION_1 &&
  564. cpsw->version != CPSW_VERSION_2 &&
  565. cpsw->version != CPSW_VERSION_3)
  566. return -EOPNOTSUPP;
  567. cfg.flags = 0;
  568. cfg.tx_type = priv->tx_ts_enabled ? HWTSTAMP_TX_ON : HWTSTAMP_TX_OFF;
  569. cfg.rx_filter = priv->rx_ts_enabled;
  570. return copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)) ? -EFAULT : 0;
  571. }
  572. #else
  573. static int cpsw_hwtstamp_get(struct net_device *dev, struct ifreq *ifr)
  574. {
  575. return -EOPNOTSUPP;
  576. }
  577. static int cpsw_hwtstamp_set(struct net_device *dev, struct ifreq *ifr)
  578. {
  579. return -EOPNOTSUPP;
  580. }
  581. #endif /*CONFIG_TI_CPTS*/
  582. int cpsw_ndo_ioctl(struct net_device *dev, struct ifreq *req, int cmd)
  583. {
  584. struct cpsw_priv *priv = netdev_priv(dev);
  585. struct cpsw_common *cpsw = priv->cpsw;
  586. int slave_no = cpsw_slave_index(cpsw, priv);
  587. struct phy_device *phy;
  588. if (!netif_running(dev))
  589. return -EINVAL;
  590. phy = cpsw->slaves[slave_no].phy;
  591. if (!phy_has_hwtstamp(phy)) {
  592. switch (cmd) {
  593. case SIOCSHWTSTAMP:
  594. return cpsw_hwtstamp_set(dev, req);
  595. case SIOCGHWTSTAMP:
  596. return cpsw_hwtstamp_get(dev, req);
  597. }
  598. }
  599. if (phy)
  600. return phy_mii_ioctl(phy, req, cmd);
  601. return -EOPNOTSUPP;
  602. }
  603. int cpsw_ndo_set_tx_maxrate(struct net_device *ndev, int queue, u32 rate)
  604. {
  605. struct cpsw_priv *priv = netdev_priv(ndev);
  606. struct cpsw_common *cpsw = priv->cpsw;
  607. struct cpsw_slave *slave;
  608. u32 min_rate;
  609. u32 ch_rate;
  610. int i, ret;
  611. ch_rate = netdev_get_tx_queue(ndev, queue)->tx_maxrate;
  612. if (ch_rate == rate)
  613. return 0;
  614. ch_rate = rate * 1000;
  615. min_rate = cpdma_chan_get_min_rate(cpsw->dma);
  616. if ((ch_rate < min_rate && ch_rate)) {
  617. dev_err(priv->dev, "The channel rate cannot be less than %dMbps",
  618. min_rate);
  619. return -EINVAL;
  620. }
  621. if (rate > cpsw->speed) {
  622. dev_err(priv->dev, "The channel rate cannot be more than 2Gbps");
  623. return -EINVAL;
  624. }
  625. ret = pm_runtime_resume_and_get(cpsw->dev);
  626. if (ret < 0)
  627. return ret;
  628. ret = cpdma_chan_set_rate(cpsw->txv[queue].ch, ch_rate);
  629. pm_runtime_put(cpsw->dev);
  630. if (ret)
  631. return ret;
  632. /* update rates for slaves tx queues */
  633. for (i = 0; i < cpsw->data.slaves; i++) {
  634. slave = &cpsw->slaves[i];
  635. if (!slave->ndev)
  636. continue;
  637. netdev_get_tx_queue(slave->ndev, queue)->tx_maxrate = rate;
  638. }
  639. cpsw_split_res(cpsw);
  640. return ret;
  641. }
  642. static int cpsw_tc_to_fifo(int tc, int num_tc)
  643. {
  644. if (tc == num_tc - 1)
  645. return 0;
  646. return CPSW_FIFO_SHAPERS_NUM - tc;
  647. }
  648. bool cpsw_shp_is_off(struct cpsw_priv *priv)
  649. {
  650. struct cpsw_common *cpsw = priv->cpsw;
  651. struct cpsw_slave *slave;
  652. u32 shift, mask, val;
  653. val = readl_relaxed(&cpsw->regs->ptype);
  654. slave = &cpsw->slaves[cpsw_slave_index(cpsw, priv)];
  655. shift = CPSW_FIFO_SHAPE_EN_SHIFT + 3 * slave->slave_num;
  656. mask = 7 << shift;
  657. val = val & mask;
  658. return !val;
  659. }
  660. static void cpsw_fifo_shp_on(struct cpsw_priv *priv, int fifo, int on)
  661. {
  662. struct cpsw_common *cpsw = priv->cpsw;
  663. struct cpsw_slave *slave;
  664. u32 shift, mask, val;
  665. val = readl_relaxed(&cpsw->regs->ptype);
  666. slave = &cpsw->slaves[cpsw_slave_index(cpsw, priv)];
  667. shift = CPSW_FIFO_SHAPE_EN_SHIFT + 3 * slave->slave_num;
  668. mask = (1 << --fifo) << shift;
  669. val = on ? val | mask : val & ~mask;
  670. writel_relaxed(val, &cpsw->regs->ptype);
  671. }
  672. static int cpsw_set_fifo_bw(struct cpsw_priv *priv, int fifo, int bw)
  673. {
  674. struct cpsw_common *cpsw = priv->cpsw;
  675. u32 val = 0, send_pct, shift;
  676. struct cpsw_slave *slave;
  677. int pct = 0, i;
  678. if (bw > priv->shp_cfg_speed * 1000)
  679. goto err;
  680. /* shaping has to stay enabled for highest fifos linearly
  681. * and fifo bw no more then interface can allow
  682. */
  683. slave = &cpsw->slaves[cpsw_slave_index(cpsw, priv)];
  684. send_pct = slave_read(slave, SEND_PERCENT);
  685. for (i = CPSW_FIFO_SHAPERS_NUM; i > 0; i--) {
  686. if (!bw) {
  687. if (i >= fifo || !priv->fifo_bw[i])
  688. continue;
  689. dev_warn(priv->dev, "Prev FIFO%d is shaped", i);
  690. continue;
  691. }
  692. if (!priv->fifo_bw[i] && i > fifo) {
  693. dev_err(priv->dev, "Upper FIFO%d is not shaped", i);
  694. return -EINVAL;
  695. }
  696. shift = (i - 1) * 8;
  697. if (i == fifo) {
  698. send_pct &= ~(CPSW_PCT_MASK << shift);
  699. val = DIV_ROUND_UP(bw, priv->shp_cfg_speed * 10);
  700. if (!val)
  701. val = 1;
  702. send_pct |= val << shift;
  703. pct += val;
  704. continue;
  705. }
  706. if (priv->fifo_bw[i])
  707. pct += (send_pct >> shift) & CPSW_PCT_MASK;
  708. }
  709. if (pct >= 100)
  710. goto err;
  711. slave_write(slave, send_pct, SEND_PERCENT);
  712. priv->fifo_bw[fifo] = bw;
  713. dev_warn(priv->dev, "set FIFO%d bw = %d\n", fifo,
  714. DIV_ROUND_CLOSEST(val * priv->shp_cfg_speed, 100));
  715. return 0;
  716. err:
  717. dev_err(priv->dev, "Bandwidth doesn't fit in tc configuration");
  718. return -EINVAL;
  719. }
  720. static int cpsw_set_fifo_rlimit(struct cpsw_priv *priv, int fifo, int bw)
  721. {
  722. struct cpsw_common *cpsw = priv->cpsw;
  723. struct cpsw_slave *slave;
  724. u32 tx_in_ctl_rg, val;
  725. int ret;
  726. ret = cpsw_set_fifo_bw(priv, fifo, bw);
  727. if (ret)
  728. return ret;
  729. slave = &cpsw->slaves[cpsw_slave_index(cpsw, priv)];
  730. tx_in_ctl_rg = cpsw->version == CPSW_VERSION_1 ?
  731. CPSW1_TX_IN_CTL : CPSW2_TX_IN_CTL;
  732. if (!bw)
  733. cpsw_fifo_shp_on(priv, fifo, bw);
  734. val = slave_read(slave, tx_in_ctl_rg);
  735. if (cpsw_shp_is_off(priv)) {
  736. /* disable FIFOs rate limited queues */
  737. val &= ~(0xf << CPSW_FIFO_RATE_EN_SHIFT);
  738. /* set type of FIFO queues to normal priority mode */
  739. val &= ~(3 << CPSW_FIFO_QUEUE_TYPE_SHIFT);
  740. /* set type of FIFO queues to be rate limited */
  741. if (bw)
  742. val |= 2 << CPSW_FIFO_QUEUE_TYPE_SHIFT;
  743. else
  744. priv->shp_cfg_speed = 0;
  745. }
  746. /* toggle a FIFO rate limited queue */
  747. if (bw)
  748. val |= BIT(fifo + CPSW_FIFO_RATE_EN_SHIFT);
  749. else
  750. val &= ~BIT(fifo + CPSW_FIFO_RATE_EN_SHIFT);
  751. slave_write(slave, val, tx_in_ctl_rg);
  752. /* FIFO transmit shape enable */
  753. cpsw_fifo_shp_on(priv, fifo, bw);
  754. return 0;
  755. }
  756. /* Defaults:
  757. * class A - prio 3
  758. * class B - prio 2
  759. * shaping for class A should be set first
  760. */
  761. static int cpsw_set_cbs(struct net_device *ndev,
  762. struct tc_cbs_qopt_offload *qopt)
  763. {
  764. struct cpsw_priv *priv = netdev_priv(ndev);
  765. struct cpsw_common *cpsw = priv->cpsw;
  766. struct cpsw_slave *slave;
  767. int prev_speed = 0;
  768. int tc, ret, fifo;
  769. u32 bw = 0;
  770. tc = netdev_txq_to_tc(priv->ndev, qopt->queue);
  771. /* enable channels in backward order, as highest FIFOs must be rate
  772. * limited first and for compliance with CPDMA rate limited channels
  773. * that also used in bacward order. FIFO0 cannot be rate limited.
  774. */
  775. fifo = cpsw_tc_to_fifo(tc, ndev->num_tc);
  776. if (!fifo) {
  777. dev_err(priv->dev, "Last tc%d can't be rate limited", tc);
  778. return -EINVAL;
  779. }
  780. /* do nothing, it's disabled anyway */
  781. if (!qopt->enable && !priv->fifo_bw[fifo])
  782. return 0;
  783. /* shapers can be set if link speed is known */
  784. slave = &cpsw->slaves[cpsw_slave_index(cpsw, priv)];
  785. if (slave->phy && slave->phy->link) {
  786. if (priv->shp_cfg_speed &&
  787. priv->shp_cfg_speed != slave->phy->speed)
  788. prev_speed = priv->shp_cfg_speed;
  789. priv->shp_cfg_speed = slave->phy->speed;
  790. }
  791. if (!priv->shp_cfg_speed) {
  792. dev_err(priv->dev, "Link speed is not known");
  793. return -1;
  794. }
  795. ret = pm_runtime_resume_and_get(cpsw->dev);
  796. if (ret < 0)
  797. return ret;
  798. bw = qopt->enable ? qopt->idleslope : 0;
  799. ret = cpsw_set_fifo_rlimit(priv, fifo, bw);
  800. if (ret) {
  801. priv->shp_cfg_speed = prev_speed;
  802. prev_speed = 0;
  803. }
  804. if (bw && prev_speed)
  805. dev_warn(priv->dev,
  806. "Speed was changed, CBS shaper speeds are changed!");
  807. pm_runtime_put_sync(cpsw->dev);
  808. return ret;
  809. }
  810. static int cpsw_set_mqprio(struct net_device *ndev, void *type_data)
  811. {
  812. struct tc_mqprio_qopt_offload *mqprio = type_data;
  813. struct cpsw_priv *priv = netdev_priv(ndev);
  814. struct cpsw_common *cpsw = priv->cpsw;
  815. int fifo, num_tc, count, offset;
  816. struct cpsw_slave *slave;
  817. u32 tx_prio_map = 0;
  818. int i, tc, ret;
  819. num_tc = mqprio->qopt.num_tc;
  820. if (num_tc > CPSW_TC_NUM)
  821. return -EINVAL;
  822. if (mqprio->mode != TC_MQPRIO_MODE_DCB)
  823. return -EINVAL;
  824. ret = pm_runtime_resume_and_get(cpsw->dev);
  825. if (ret < 0)
  826. return ret;
  827. if (num_tc) {
  828. for (i = 0; i < 8; i++) {
  829. tc = mqprio->qopt.prio_tc_map[i];
  830. fifo = cpsw_tc_to_fifo(tc, num_tc);
  831. tx_prio_map |= fifo << (4 * i);
  832. }
  833. netdev_set_num_tc(ndev, num_tc);
  834. for (i = 0; i < num_tc; i++) {
  835. count = mqprio->qopt.count[i];
  836. offset = mqprio->qopt.offset[i];
  837. netdev_set_tc_queue(ndev, i, count, offset);
  838. }
  839. }
  840. if (!mqprio->qopt.hw) {
  841. /* restore default configuration */
  842. netdev_reset_tc(ndev);
  843. tx_prio_map = TX_PRIORITY_MAPPING;
  844. }
  845. priv->mqprio_hw = mqprio->qopt.hw;
  846. offset = cpsw->version == CPSW_VERSION_1 ?
  847. CPSW1_TX_PRI_MAP : CPSW2_TX_PRI_MAP;
  848. slave = &cpsw->slaves[cpsw_slave_index(cpsw, priv)];
  849. slave_write(slave, tx_prio_map, offset);
  850. pm_runtime_put_sync(cpsw->dev);
  851. return 0;
  852. }
  853. static int cpsw_qos_setup_tc_block(struct net_device *ndev, struct flow_block_offload *f);
  854. int cpsw_ndo_setup_tc(struct net_device *ndev, enum tc_setup_type type,
  855. void *type_data)
  856. {
  857. switch (type) {
  858. case TC_SETUP_QDISC_CBS:
  859. return cpsw_set_cbs(ndev, type_data);
  860. case TC_SETUP_QDISC_MQPRIO:
  861. return cpsw_set_mqprio(ndev, type_data);
  862. case TC_SETUP_BLOCK:
  863. return cpsw_qos_setup_tc_block(ndev, type_data);
  864. default:
  865. return -EOPNOTSUPP;
  866. }
  867. }
  868. void cpsw_cbs_resume(struct cpsw_slave *slave, struct cpsw_priv *priv)
  869. {
  870. int fifo, bw;
  871. for (fifo = CPSW_FIFO_SHAPERS_NUM; fifo > 0; fifo--) {
  872. bw = priv->fifo_bw[fifo];
  873. if (!bw)
  874. continue;
  875. cpsw_set_fifo_rlimit(priv, fifo, bw);
  876. }
  877. }
  878. void cpsw_mqprio_resume(struct cpsw_slave *slave, struct cpsw_priv *priv)
  879. {
  880. struct cpsw_common *cpsw = priv->cpsw;
  881. u32 tx_prio_map = 0;
  882. int i, tc, fifo;
  883. u32 tx_prio_rg;
  884. if (!priv->mqprio_hw)
  885. return;
  886. for (i = 0; i < 8; i++) {
  887. tc = netdev_get_prio_tc_map(priv->ndev, i);
  888. fifo = CPSW_FIFO_SHAPERS_NUM - tc;
  889. tx_prio_map |= fifo << (4 * i);
  890. }
  891. tx_prio_rg = cpsw->version == CPSW_VERSION_1 ?
  892. CPSW1_TX_PRI_MAP : CPSW2_TX_PRI_MAP;
  893. slave_write(slave, tx_prio_map, tx_prio_rg);
  894. }
  895. int cpsw_fill_rx_channels(struct cpsw_priv *priv)
  896. {
  897. struct cpsw_common *cpsw = priv->cpsw;
  898. struct cpsw_meta_xdp *xmeta;
  899. struct page_pool *pool;
  900. struct page *page;
  901. int ch_buf_num;
  902. int ch, i, ret;
  903. dma_addr_t dma;
  904. for (ch = 0; ch < cpsw->rx_ch_num; ch++) {
  905. pool = cpsw->page_pool[ch];
  906. ch_buf_num = cpdma_chan_get_rx_buf_num(cpsw->rxv[ch].ch);
  907. for (i = 0; i < ch_buf_num; i++) {
  908. page = page_pool_dev_alloc_pages(pool);
  909. if (!page) {
  910. cpsw_err(priv, ifup, "allocate rx page err\n");
  911. return -ENOMEM;
  912. }
  913. xmeta = page_address(page) + CPSW_XMETA_OFFSET;
  914. xmeta->ndev = priv->ndev;
  915. xmeta->ch = ch;
  916. dma = page_pool_get_dma_addr(page) + CPSW_HEADROOM_NA;
  917. ret = cpdma_chan_idle_submit_mapped(cpsw->rxv[ch].ch,
  918. page, dma,
  919. cpsw->rx_packet_max,
  920. 0);
  921. if (ret < 0) {
  922. cpsw_err(priv, ifup,
  923. "cannot submit page to channel %d rx, error %d\n",
  924. ch, ret);
  925. page_pool_recycle_direct(pool, page);
  926. return ret;
  927. }
  928. }
  929. cpsw_info(priv, ifup, "ch %d rx, submitted %d descriptors\n",
  930. ch, ch_buf_num);
  931. }
  932. return 0;
  933. }
  934. static struct page_pool *cpsw_create_page_pool(struct cpsw_common *cpsw,
  935. int size)
  936. {
  937. struct page_pool_params pp_params = {};
  938. struct page_pool *pool;
  939. pp_params.order = 0;
  940. pp_params.flags = PP_FLAG_DMA_MAP;
  941. pp_params.pool_size = size;
  942. pp_params.nid = NUMA_NO_NODE;
  943. pp_params.dma_dir = DMA_BIDIRECTIONAL;
  944. pp_params.dev = cpsw->dev;
  945. pool = page_pool_create(&pp_params);
  946. if (IS_ERR(pool))
  947. dev_err(cpsw->dev, "cannot create rx page pool\n");
  948. return pool;
  949. }
  950. static int cpsw_create_rx_pool(struct cpsw_common *cpsw, int ch)
  951. {
  952. struct page_pool *pool;
  953. int ret = 0, pool_size;
  954. pool_size = cpdma_chan_get_rx_buf_num(cpsw->rxv[ch].ch);
  955. pool = cpsw_create_page_pool(cpsw, pool_size);
  956. if (IS_ERR(pool))
  957. ret = PTR_ERR(pool);
  958. else
  959. cpsw->page_pool[ch] = pool;
  960. return ret;
  961. }
  962. static int cpsw_ndev_create_xdp_rxq(struct cpsw_priv *priv, int ch)
  963. {
  964. struct cpsw_common *cpsw = priv->cpsw;
  965. struct xdp_rxq_info *rxq;
  966. struct page_pool *pool;
  967. int ret;
  968. pool = cpsw->page_pool[ch];
  969. rxq = &priv->xdp_rxq[ch];
  970. ret = xdp_rxq_info_reg(rxq, priv->ndev, ch, 0);
  971. if (ret)
  972. return ret;
  973. ret = xdp_rxq_info_reg_mem_model(rxq, MEM_TYPE_PAGE_POOL, pool);
  974. if (ret)
  975. xdp_rxq_info_unreg(rxq);
  976. return ret;
  977. }
  978. static void cpsw_ndev_destroy_xdp_rxq(struct cpsw_priv *priv, int ch)
  979. {
  980. struct xdp_rxq_info *rxq = &priv->xdp_rxq[ch];
  981. if (!xdp_rxq_info_is_reg(rxq))
  982. return;
  983. xdp_rxq_info_unreg(rxq);
  984. }
  985. void cpsw_destroy_xdp_rxqs(struct cpsw_common *cpsw)
  986. {
  987. struct net_device *ndev;
  988. int i, ch;
  989. for (ch = 0; ch < cpsw->rx_ch_num; ch++) {
  990. for (i = 0; i < cpsw->data.slaves; i++) {
  991. ndev = cpsw->slaves[i].ndev;
  992. if (!ndev)
  993. continue;
  994. cpsw_ndev_destroy_xdp_rxq(netdev_priv(ndev), ch);
  995. }
  996. page_pool_destroy(cpsw->page_pool[ch]);
  997. cpsw->page_pool[ch] = NULL;
  998. }
  999. }
  1000. int cpsw_create_xdp_rxqs(struct cpsw_common *cpsw)
  1001. {
  1002. struct net_device *ndev;
  1003. int i, ch, ret;
  1004. for (ch = 0; ch < cpsw->rx_ch_num; ch++) {
  1005. ret = cpsw_create_rx_pool(cpsw, ch);
  1006. if (ret)
  1007. goto err_cleanup;
  1008. /* using same page pool is allowed as no running rx handlers
  1009. * simultaneously for both ndevs
  1010. */
  1011. for (i = 0; i < cpsw->data.slaves; i++) {
  1012. ndev = cpsw->slaves[i].ndev;
  1013. if (!ndev)
  1014. continue;
  1015. ret = cpsw_ndev_create_xdp_rxq(netdev_priv(ndev), ch);
  1016. if (ret)
  1017. goto err_cleanup;
  1018. }
  1019. }
  1020. return 0;
  1021. err_cleanup:
  1022. cpsw_destroy_xdp_rxqs(cpsw);
  1023. return ret;
  1024. }
  1025. static int cpsw_xdp_prog_setup(struct cpsw_priv *priv, struct netdev_bpf *bpf)
  1026. {
  1027. struct bpf_prog *prog = bpf->prog;
  1028. if (!priv->xdpi.prog && !prog)
  1029. return 0;
  1030. WRITE_ONCE(priv->xdp_prog, prog);
  1031. xdp_attachment_setup(&priv->xdpi, bpf);
  1032. return 0;
  1033. }
  1034. int cpsw_ndo_bpf(struct net_device *ndev, struct netdev_bpf *bpf)
  1035. {
  1036. struct cpsw_priv *priv = netdev_priv(ndev);
  1037. switch (bpf->command) {
  1038. case XDP_SETUP_PROG:
  1039. return cpsw_xdp_prog_setup(priv, bpf);
  1040. default:
  1041. return -EINVAL;
  1042. }
  1043. }
  1044. int cpsw_xdp_tx_frame(struct cpsw_priv *priv, struct xdp_frame *xdpf,
  1045. struct page *page, int port)
  1046. {
  1047. struct cpsw_common *cpsw = priv->cpsw;
  1048. struct cpsw_meta_xdp *xmeta;
  1049. struct cpdma_chan *txch;
  1050. dma_addr_t dma;
  1051. int ret;
  1052. xmeta = (void *)xdpf + CPSW_XMETA_OFFSET;
  1053. xmeta->ndev = priv->ndev;
  1054. xmeta->ch = 0;
  1055. txch = cpsw->txv[0].ch;
  1056. if (page) {
  1057. dma = page_pool_get_dma_addr(page);
  1058. dma += xdpf->headroom + sizeof(struct xdp_frame);
  1059. ret = cpdma_chan_submit_mapped(txch, cpsw_xdpf_to_handle(xdpf),
  1060. dma, xdpf->len, port);
  1061. } else {
  1062. if (sizeof(*xmeta) > xdpf->headroom)
  1063. return -EINVAL;
  1064. ret = cpdma_chan_submit(txch, cpsw_xdpf_to_handle(xdpf),
  1065. xdpf->data, xdpf->len, port);
  1066. }
  1067. if (ret)
  1068. priv->ndev->stats.tx_dropped++;
  1069. return ret;
  1070. }
  1071. int cpsw_run_xdp(struct cpsw_priv *priv, int ch, struct xdp_buff *xdp,
  1072. struct page *page, int port, int *len)
  1073. {
  1074. struct cpsw_common *cpsw = priv->cpsw;
  1075. struct net_device *ndev = priv->ndev;
  1076. int ret = CPSW_XDP_CONSUMED;
  1077. struct xdp_frame *xdpf;
  1078. struct bpf_prog *prog;
  1079. u32 act;
  1080. prog = READ_ONCE(priv->xdp_prog);
  1081. if (!prog)
  1082. return CPSW_XDP_PASS;
  1083. act = bpf_prog_run_xdp(prog, xdp);
  1084. /* XDP prog might have changed packet data and boundaries */
  1085. *len = xdp->data_end - xdp->data;
  1086. switch (act) {
  1087. case XDP_PASS:
  1088. ret = CPSW_XDP_PASS;
  1089. goto out;
  1090. case XDP_TX:
  1091. xdpf = xdp_convert_buff_to_frame(xdp);
  1092. if (unlikely(!xdpf))
  1093. goto drop;
  1094. if (cpsw_xdp_tx_frame(priv, xdpf, page, port))
  1095. xdp_return_frame_rx_napi(xdpf);
  1096. break;
  1097. case XDP_REDIRECT:
  1098. if (xdp_do_redirect(ndev, xdp, prog))
  1099. goto drop;
  1100. /* Have to flush here, per packet, instead of doing it in bulk
  1101. * at the end of the napi handler. The RX devices on this
  1102. * particular hardware is sharing a common queue, so the
  1103. * incoming device might change per packet.
  1104. */
  1105. xdp_do_flush();
  1106. break;
  1107. default:
  1108. bpf_warn_invalid_xdp_action(ndev, prog, act);
  1109. fallthrough;
  1110. case XDP_ABORTED:
  1111. trace_xdp_exception(ndev, prog, act);
  1112. fallthrough; /* handle aborts by dropping packet */
  1113. case XDP_DROP:
  1114. ndev->stats.rx_bytes += *len;
  1115. ndev->stats.rx_packets++;
  1116. goto drop;
  1117. }
  1118. ndev->stats.rx_bytes += *len;
  1119. ndev->stats.rx_packets++;
  1120. out:
  1121. return ret;
  1122. drop:
  1123. page_pool_recycle_direct(cpsw->page_pool[ch], page);
  1124. return ret;
  1125. }
  1126. static int cpsw_qos_clsflower_add_policer(struct cpsw_priv *priv,
  1127. struct netlink_ext_ack *extack,
  1128. struct flow_cls_offload *cls,
  1129. u64 rate_pkt_ps)
  1130. {
  1131. struct flow_rule *rule = flow_cls_offload_flow_rule(cls);
  1132. struct flow_dissector *dissector = rule->match.dissector;
  1133. static const u8 mc_mac[] = {0x01, 0x00, 0x00, 0x00, 0x00, 0x00};
  1134. struct flow_match_eth_addrs match;
  1135. u32 port_id;
  1136. int ret;
  1137. if (dissector->used_keys &
  1138. ~(BIT_ULL(FLOW_DISSECTOR_KEY_BASIC) |
  1139. BIT_ULL(FLOW_DISSECTOR_KEY_CONTROL) |
  1140. BIT_ULL(FLOW_DISSECTOR_KEY_ETH_ADDRS))) {
  1141. NL_SET_ERR_MSG_MOD(extack,
  1142. "Unsupported keys used");
  1143. return -EOPNOTSUPP;
  1144. }
  1145. if (flow_rule_match_has_control_flags(rule, extack))
  1146. return -EOPNOTSUPP;
  1147. if (!flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
  1148. NL_SET_ERR_MSG_MOD(extack, "Not matching on eth address");
  1149. return -EOPNOTSUPP;
  1150. }
  1151. flow_rule_match_eth_addrs(rule, &match);
  1152. if (!is_zero_ether_addr(match.mask->src)) {
  1153. NL_SET_ERR_MSG_MOD(extack,
  1154. "Matching on source MAC not supported");
  1155. return -EOPNOTSUPP;
  1156. }
  1157. port_id = cpsw_slave_index(priv->cpsw, priv) + 1;
  1158. if (is_broadcast_ether_addr(match.key->dst) &&
  1159. is_broadcast_ether_addr(match.mask->dst)) {
  1160. ret = cpsw_ale_rx_ratelimit_bc(priv->cpsw->ale, port_id, rate_pkt_ps);
  1161. if (ret)
  1162. return ret;
  1163. priv->ale_bc_ratelimit.cookie = cls->cookie;
  1164. priv->ale_bc_ratelimit.rate_packet_ps = rate_pkt_ps;
  1165. } else if (ether_addr_equal_unaligned(match.key->dst, mc_mac) &&
  1166. ether_addr_equal_unaligned(match.mask->dst, mc_mac)) {
  1167. ret = cpsw_ale_rx_ratelimit_mc(priv->cpsw->ale, port_id, rate_pkt_ps);
  1168. if (ret)
  1169. return ret;
  1170. priv->ale_mc_ratelimit.cookie = cls->cookie;
  1171. priv->ale_mc_ratelimit.rate_packet_ps = rate_pkt_ps;
  1172. } else {
  1173. NL_SET_ERR_MSG_MOD(extack, "Not supported matching key");
  1174. return -EOPNOTSUPP;
  1175. }
  1176. return 0;
  1177. }
  1178. static int cpsw_qos_clsflower_policer_validate(const struct flow_action *action,
  1179. const struct flow_action_entry *act,
  1180. struct netlink_ext_ack *extack)
  1181. {
  1182. if (act->police.exceed.act_id != FLOW_ACTION_DROP) {
  1183. NL_SET_ERR_MSG_MOD(extack,
  1184. "Offload not supported when exceed action is not drop");
  1185. return -EOPNOTSUPP;
  1186. }
  1187. if (act->police.notexceed.act_id != FLOW_ACTION_PIPE &&
  1188. act->police.notexceed.act_id != FLOW_ACTION_ACCEPT) {
  1189. NL_SET_ERR_MSG_MOD(extack,
  1190. "Offload not supported when conform action is not pipe or ok");
  1191. return -EOPNOTSUPP;
  1192. }
  1193. if (act->police.notexceed.act_id == FLOW_ACTION_ACCEPT &&
  1194. !flow_action_is_last_entry(action, act)) {
  1195. NL_SET_ERR_MSG_MOD(extack,
  1196. "Offload not supported when conform action is ok, but action is not last");
  1197. return -EOPNOTSUPP;
  1198. }
  1199. if (act->police.rate_bytes_ps || act->police.peakrate_bytes_ps ||
  1200. act->police.avrate || act->police.overhead) {
  1201. NL_SET_ERR_MSG_MOD(extack,
  1202. "Offload not supported when bytes per second/peakrate/avrate/overhead is configured");
  1203. return -EOPNOTSUPP;
  1204. }
  1205. return 0;
  1206. }
  1207. static int cpsw_qos_configure_clsflower(struct cpsw_priv *priv, struct flow_cls_offload *cls)
  1208. {
  1209. struct flow_rule *rule = flow_cls_offload_flow_rule(cls);
  1210. struct netlink_ext_ack *extack = cls->common.extack;
  1211. const struct flow_action_entry *act;
  1212. int i, ret;
  1213. flow_action_for_each(i, act, &rule->action) {
  1214. switch (act->id) {
  1215. case FLOW_ACTION_POLICE:
  1216. ret = cpsw_qos_clsflower_policer_validate(&rule->action, act, extack);
  1217. if (ret)
  1218. return ret;
  1219. return cpsw_qos_clsflower_add_policer(priv, extack, cls,
  1220. act->police.rate_pkt_ps);
  1221. default:
  1222. NL_SET_ERR_MSG_MOD(extack, "Action not supported");
  1223. return -EOPNOTSUPP;
  1224. }
  1225. }
  1226. return -EOPNOTSUPP;
  1227. }
  1228. static int cpsw_qos_delete_clsflower(struct cpsw_priv *priv, struct flow_cls_offload *cls)
  1229. {
  1230. u32 port_id = cpsw_slave_index(priv->cpsw, priv) + 1;
  1231. if (cls->cookie == priv->ale_bc_ratelimit.cookie) {
  1232. priv->ale_bc_ratelimit.cookie = 0;
  1233. priv->ale_bc_ratelimit.rate_packet_ps = 0;
  1234. cpsw_ale_rx_ratelimit_bc(priv->cpsw->ale, port_id, 0);
  1235. }
  1236. if (cls->cookie == priv->ale_mc_ratelimit.cookie) {
  1237. priv->ale_mc_ratelimit.cookie = 0;
  1238. priv->ale_mc_ratelimit.rate_packet_ps = 0;
  1239. cpsw_ale_rx_ratelimit_mc(priv->cpsw->ale, port_id, 0);
  1240. }
  1241. return 0;
  1242. }
  1243. static int cpsw_qos_setup_tc_clsflower(struct cpsw_priv *priv, struct flow_cls_offload *cls_flower)
  1244. {
  1245. switch (cls_flower->command) {
  1246. case FLOW_CLS_REPLACE:
  1247. return cpsw_qos_configure_clsflower(priv, cls_flower);
  1248. case FLOW_CLS_DESTROY:
  1249. return cpsw_qos_delete_clsflower(priv, cls_flower);
  1250. default:
  1251. return -EOPNOTSUPP;
  1252. }
  1253. }
  1254. static int cpsw_qos_setup_tc_block_cb(enum tc_setup_type type, void *type_data, void *cb_priv)
  1255. {
  1256. struct cpsw_priv *priv = cb_priv;
  1257. int ret;
  1258. if (!tc_cls_can_offload_and_chain0(priv->ndev, type_data))
  1259. return -EOPNOTSUPP;
  1260. ret = pm_runtime_get_sync(priv->dev);
  1261. if (ret < 0) {
  1262. pm_runtime_put_noidle(priv->dev);
  1263. return ret;
  1264. }
  1265. switch (type) {
  1266. case TC_SETUP_CLSFLOWER:
  1267. ret = cpsw_qos_setup_tc_clsflower(priv, type_data);
  1268. break;
  1269. default:
  1270. ret = -EOPNOTSUPP;
  1271. }
  1272. pm_runtime_put(priv->dev);
  1273. return ret;
  1274. }
  1275. static LIST_HEAD(cpsw_qos_block_cb_list);
  1276. static int cpsw_qos_setup_tc_block(struct net_device *ndev, struct flow_block_offload *f)
  1277. {
  1278. struct cpsw_priv *priv = netdev_priv(ndev);
  1279. return flow_block_cb_setup_simple(f, &cpsw_qos_block_cb_list,
  1280. cpsw_qos_setup_tc_block_cb,
  1281. priv, priv, true);
  1282. }
  1283. void cpsw_qos_clsflower_resume(struct cpsw_priv *priv)
  1284. {
  1285. u32 port_id = cpsw_slave_index(priv->cpsw, priv) + 1;
  1286. if (priv->ale_bc_ratelimit.cookie)
  1287. cpsw_ale_rx_ratelimit_bc(priv->cpsw->ale, port_id,
  1288. priv->ale_bc_ratelimit.rate_packet_ps);
  1289. if (priv->ale_mc_ratelimit.cookie)
  1290. cpsw_ale_rx_ratelimit_mc(priv->cpsw->ale, port_id,
  1291. priv->ale_mc_ratelimit.rate_packet_ps);
  1292. }