igc_tsn.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright (c) 2019 Intel Corporation */
  3. #include "igc.h"
  4. #include "igc_hw.h"
  5. #include "igc_tsn.h"
  6. static bool is_any_launchtime(struct igc_adapter *adapter)
  7. {
  8. int i;
  9. for (i = 0; i < adapter->num_tx_queues; i++) {
  10. struct igc_ring *ring = adapter->tx_ring[i];
  11. if (ring->launchtime_enable)
  12. return true;
  13. }
  14. return false;
  15. }
  16. static bool is_cbs_enabled(struct igc_adapter *adapter)
  17. {
  18. int i;
  19. for (i = 0; i < adapter->num_tx_queues; i++) {
  20. struct igc_ring *ring = adapter->tx_ring[i];
  21. if (ring->cbs_enable)
  22. return true;
  23. }
  24. return false;
  25. }
  26. static unsigned int igc_tsn_new_flags(struct igc_adapter *adapter)
  27. {
  28. unsigned int new_flags = adapter->flags & ~IGC_FLAG_TSN_ANY_ENABLED;
  29. if (adapter->taprio_offload_enable)
  30. new_flags |= IGC_FLAG_TSN_QBV_ENABLED;
  31. if (is_any_launchtime(adapter))
  32. new_flags |= IGC_FLAG_TSN_QBV_ENABLED;
  33. if (is_cbs_enabled(adapter))
  34. new_flags |= IGC_FLAG_TSN_QAV_ENABLED;
  35. if (adapter->strict_priority_enable)
  36. new_flags |= IGC_FLAG_TSN_LEGACY_ENABLED;
  37. return new_flags;
  38. }
  39. static bool igc_tsn_is_tx_mode_in_tsn(struct igc_adapter *adapter)
  40. {
  41. struct igc_hw *hw = &adapter->hw;
  42. return !!(rd32(IGC_TQAVCTRL) & IGC_TQAVCTRL_TRANSMIT_MODE_TSN);
  43. }
  44. void igc_tsn_adjust_txtime_offset(struct igc_adapter *adapter)
  45. {
  46. struct igc_hw *hw = &adapter->hw;
  47. u16 txoffset;
  48. if (!igc_tsn_is_tx_mode_in_tsn(adapter))
  49. return;
  50. switch (adapter->link_speed) {
  51. case SPEED_10:
  52. txoffset = IGC_TXOFFSET_SPEED_10;
  53. break;
  54. case SPEED_100:
  55. txoffset = IGC_TXOFFSET_SPEED_100;
  56. break;
  57. case SPEED_1000:
  58. txoffset = IGC_TXOFFSET_SPEED_1000;
  59. break;
  60. case SPEED_2500:
  61. txoffset = IGC_TXOFFSET_SPEED_2500;
  62. break;
  63. default:
  64. txoffset = 0;
  65. break;
  66. }
  67. wr32(IGC_GTXOFFSET, txoffset);
  68. }
  69. static void igc_tsn_restore_retx_default(struct igc_adapter *adapter)
  70. {
  71. struct igc_hw *hw = &adapter->hw;
  72. u32 retxctl;
  73. retxctl = rd32(IGC_RETX_CTL) & IGC_RETX_CTL_WATERMARK_MASK;
  74. wr32(IGC_RETX_CTL, retxctl);
  75. }
  76. bool igc_tsn_is_taprio_activated_by_user(struct igc_adapter *adapter)
  77. {
  78. struct igc_hw *hw = &adapter->hw;
  79. return (rd32(IGC_BASET_H) || rd32(IGC_BASET_L)) &&
  80. adapter->taprio_offload_enable;
  81. }
  82. static void igc_tsn_tx_arb(struct igc_adapter *adapter, u16 *queue_per_tc)
  83. {
  84. struct igc_hw *hw = &adapter->hw;
  85. u32 txarb;
  86. txarb = rd32(IGC_TXARB);
  87. txarb &= ~(IGC_TXARB_TXQ_PRIO_0_MASK |
  88. IGC_TXARB_TXQ_PRIO_1_MASK |
  89. IGC_TXARB_TXQ_PRIO_2_MASK |
  90. IGC_TXARB_TXQ_PRIO_3_MASK);
  91. txarb |= IGC_TXARB_TXQ_PRIO_0(queue_per_tc[3]);
  92. txarb |= IGC_TXARB_TXQ_PRIO_1(queue_per_tc[2]);
  93. txarb |= IGC_TXARB_TXQ_PRIO_2(queue_per_tc[1]);
  94. txarb |= IGC_TXARB_TXQ_PRIO_3(queue_per_tc[0]);
  95. wr32(IGC_TXARB, txarb);
  96. }
  97. /* Returns the TSN specific registers to their default values after
  98. * the adapter is reset.
  99. */
  100. static int igc_tsn_disable_offload(struct igc_adapter *adapter)
  101. {
  102. u16 queue_per_tc[4] = { 3, 2, 1, 0 };
  103. struct igc_hw *hw = &adapter->hw;
  104. u32 tqavctrl;
  105. int i;
  106. wr32(IGC_GTXOFFSET, 0);
  107. wr32(IGC_TXPBS, I225_TXPBSIZE_DEFAULT);
  108. wr32(IGC_DTXMXPKTSZ, IGC_DTXMXPKTSZ_DEFAULT);
  109. if (igc_is_device_id_i226(hw))
  110. igc_tsn_restore_retx_default(adapter);
  111. tqavctrl = rd32(IGC_TQAVCTRL);
  112. tqavctrl &= ~(IGC_TQAVCTRL_TRANSMIT_MODE_TSN |
  113. IGC_TQAVCTRL_ENHANCED_QAV | IGC_TQAVCTRL_FUTSCDDIS);
  114. wr32(IGC_TQAVCTRL, tqavctrl);
  115. for (i = 0; i < adapter->num_tx_queues; i++) {
  116. wr32(IGC_TXQCTL(i), 0);
  117. wr32(IGC_STQT(i), 0);
  118. wr32(IGC_ENDQT(i), NSEC_PER_SEC);
  119. }
  120. wr32(IGC_QBVCYCLET_S, 0);
  121. wr32(IGC_QBVCYCLET, NSEC_PER_SEC);
  122. /* Reset mqprio TC configuration. */
  123. netdev_reset_tc(adapter->netdev);
  124. /* Restore the default Tx arbitration: Priority 0 has the highest
  125. * priority and is assigned to queue 0 and so on and so forth.
  126. */
  127. igc_tsn_tx_arb(adapter, queue_per_tc);
  128. adapter->flags &= ~IGC_FLAG_TSN_QBV_ENABLED;
  129. adapter->flags &= ~IGC_FLAG_TSN_LEGACY_ENABLED;
  130. return 0;
  131. }
  132. /* To partially fix i226 HW errata, reduce MAC internal buffering from 192 Bytes
  133. * to 88 Bytes by setting RETX_CTL register using the recommendation from:
  134. * a) Ethernet Controller I225/I226 Specification Update Rev 2.1
  135. * Item 9: TSN: Packet Transmission Might Cross the Qbv Window
  136. * b) I225/6 SW User Manual Rev 1.2.4: Section 8.11.5 Retry Buffer Control
  137. */
  138. static void igc_tsn_set_retx_qbvfullthreshold(struct igc_adapter *adapter)
  139. {
  140. struct igc_hw *hw = &adapter->hw;
  141. u32 retxctl, watermark;
  142. retxctl = rd32(IGC_RETX_CTL);
  143. watermark = retxctl & IGC_RETX_CTL_WATERMARK_MASK;
  144. /* Set QBVFULLTH value using watermark and set QBVFULLEN */
  145. retxctl |= (watermark << IGC_RETX_CTL_QBVFULLTH_SHIFT) |
  146. IGC_RETX_CTL_QBVFULLEN;
  147. wr32(IGC_RETX_CTL, retxctl);
  148. }
  149. static int igc_tsn_enable_offload(struct igc_adapter *adapter)
  150. {
  151. struct igc_hw *hw = &adapter->hw;
  152. u32 tqavctrl, baset_l, baset_h;
  153. u32 sec, nsec, cycle;
  154. ktime_t base_time, systim;
  155. int i;
  156. wr32(IGC_TSAUXC, 0);
  157. wr32(IGC_DTXMXPKTSZ, IGC_DTXMXPKTSZ_TSN);
  158. wr32(IGC_TXPBS, IGC_TXPBSIZE_TSN);
  159. if (igc_is_device_id_i226(hw))
  160. igc_tsn_set_retx_qbvfullthreshold(adapter);
  161. if (adapter->strict_priority_enable) {
  162. int err;
  163. err = netdev_set_num_tc(adapter->netdev, adapter->num_tc);
  164. if (err)
  165. return err;
  166. for (i = 0; i < adapter->num_tc; i++) {
  167. err = netdev_set_tc_queue(adapter->netdev, i, 1,
  168. adapter->queue_per_tc[i]);
  169. if (err)
  170. return err;
  171. }
  172. /* In case the card is configured with less than four queues. */
  173. for (; i < IGC_MAX_TX_QUEUES; i++)
  174. adapter->queue_per_tc[i] = i;
  175. /* Configure queue priorities according to the user provided
  176. * mapping.
  177. */
  178. igc_tsn_tx_arb(adapter, adapter->queue_per_tc);
  179. /* Enable legacy TSN mode which will do strict priority without
  180. * any other TSN features.
  181. */
  182. tqavctrl = rd32(IGC_TQAVCTRL);
  183. tqavctrl |= IGC_TQAVCTRL_TRANSMIT_MODE_TSN;
  184. tqavctrl &= ~IGC_TQAVCTRL_ENHANCED_QAV;
  185. wr32(IGC_TQAVCTRL, tqavctrl);
  186. return 0;
  187. }
  188. for (i = 0; i < adapter->num_tx_queues; i++) {
  189. struct igc_ring *ring = adapter->tx_ring[i];
  190. u32 txqctl = 0;
  191. u16 cbs_value;
  192. u32 tqavcc;
  193. wr32(IGC_STQT(i), ring->start_time);
  194. wr32(IGC_ENDQT(i), ring->end_time);
  195. if (adapter->taprio_offload_enable) {
  196. /* If taprio_offload_enable is set we are in "taprio"
  197. * mode and we need to be strict about the
  198. * cycles: only transmit a packet if it can be
  199. * completed during that cycle.
  200. *
  201. * If taprio_offload_enable is NOT true when
  202. * enabling TSN offload, the cycle should have
  203. * no external effects, but is only used internally
  204. * to adapt the base time register after a second
  205. * has passed.
  206. *
  207. * Enabling strict mode in this case would
  208. * unnecessarily prevent the transmission of
  209. * certain packets (i.e. at the boundary of a
  210. * second) and thus interfere with the launchtime
  211. * feature that promises transmission at a
  212. * certain point in time.
  213. */
  214. txqctl |= IGC_TXQCTL_STRICT_CYCLE |
  215. IGC_TXQCTL_STRICT_END;
  216. }
  217. if (ring->launchtime_enable)
  218. txqctl |= IGC_TXQCTL_QUEUE_MODE_LAUNCHT;
  219. /* Skip configuring CBS for Q2 and Q3 */
  220. if (i > 1)
  221. goto skip_cbs;
  222. if (ring->cbs_enable) {
  223. if (i == 0)
  224. txqctl |= IGC_TXQCTL_QAV_SEL_CBS0;
  225. else
  226. txqctl |= IGC_TXQCTL_QAV_SEL_CBS1;
  227. /* According to i225 datasheet section 7.5.2.7, we
  228. * should set the 'idleSlope' field from TQAVCC
  229. * register following the equation:
  230. *
  231. * value = link-speed 0x7736 * BW * 0.2
  232. * ---------- * ----------------- (E1)
  233. * 100Mbps 2.5
  234. *
  235. * Note that 'link-speed' is in Mbps.
  236. *
  237. * 'BW' is the percentage bandwidth out of full
  238. * link speed which can be found with the
  239. * following equation. Note that idleSlope here
  240. * is the parameter from this function
  241. * which is in kbps.
  242. *
  243. * BW = idleSlope
  244. * ----------------- (E2)
  245. * link-speed * 1000
  246. *
  247. * That said, we can come up with a generic
  248. * equation to calculate the value we should set
  249. * it TQAVCC register by replacing 'BW' in E1 by E2.
  250. * The resulting equation is:
  251. *
  252. * value = link-speed * 0x7736 * idleSlope * 0.2
  253. * ------------------------------------- (E3)
  254. * 100 * 2.5 * link-speed * 1000
  255. *
  256. * 'link-speed' is present in both sides of the
  257. * fraction so it is canceled out. The final
  258. * equation is the following:
  259. *
  260. * value = idleSlope * 61036
  261. * ----------------- (E4)
  262. * 2500000
  263. *
  264. * NOTE: For i225, given the above, we can see
  265. * that idleslope is represented in
  266. * 40.959433 kbps units by the value at
  267. * the TQAVCC register (2.5Gbps / 61036),
  268. * which reduces the granularity for
  269. * idleslope increments.
  270. *
  271. * In i225 controller, the sendSlope and loCredit
  272. * parameters from CBS are not configurable
  273. * by software so we don't do any
  274. * 'controller configuration' in respect to
  275. * these parameters.
  276. */
  277. cbs_value = DIV_ROUND_UP_ULL(ring->idleslope
  278. * 61036ULL, 2500000);
  279. tqavcc = rd32(IGC_TQAVCC(i));
  280. tqavcc &= ~IGC_TQAVCC_IDLESLOPE_MASK;
  281. tqavcc |= cbs_value | IGC_TQAVCC_KEEP_CREDITS;
  282. wr32(IGC_TQAVCC(i), tqavcc);
  283. wr32(IGC_TQAVHC(i),
  284. 0x80000000 + ring->hicredit * 0x7736);
  285. } else {
  286. /* Disable any CBS for the queue */
  287. txqctl &= ~(IGC_TXQCTL_QAV_SEL_MASK);
  288. /* Set idleSlope to zero. */
  289. tqavcc = rd32(IGC_TQAVCC(i));
  290. tqavcc &= ~(IGC_TQAVCC_IDLESLOPE_MASK |
  291. IGC_TQAVCC_KEEP_CREDITS);
  292. wr32(IGC_TQAVCC(i), tqavcc);
  293. /* Set hiCredit to zero. */
  294. wr32(IGC_TQAVHC(i), 0);
  295. }
  296. skip_cbs:
  297. wr32(IGC_TXQCTL(i), txqctl);
  298. }
  299. tqavctrl = rd32(IGC_TQAVCTRL) & ~IGC_TQAVCTRL_FUTSCDDIS;
  300. tqavctrl |= IGC_TQAVCTRL_TRANSMIT_MODE_TSN | IGC_TQAVCTRL_ENHANCED_QAV;
  301. adapter->qbv_count++;
  302. cycle = adapter->cycle_time;
  303. base_time = adapter->base_time;
  304. nsec = rd32(IGC_SYSTIML);
  305. sec = rd32(IGC_SYSTIMH);
  306. systim = ktime_set(sec, nsec);
  307. if (ktime_compare(systim, base_time) > 0) {
  308. s64 n = div64_s64(ktime_sub_ns(systim, base_time), cycle);
  309. base_time = ktime_add_ns(base_time, (n + 1) * cycle);
  310. } else {
  311. if (igc_is_device_id_i226(hw)) {
  312. ktime_t adjust_time, expires_time;
  313. /* According to datasheet section 7.5.2.9.3.3, FutScdDis bit
  314. * has to be configured before the cycle time and base time.
  315. * Tx won't hang if a GCL is already running,
  316. * so in this case we don't need to set FutScdDis.
  317. */
  318. if (!(rd32(IGC_BASET_H) || rd32(IGC_BASET_L)))
  319. tqavctrl |= IGC_TQAVCTRL_FUTSCDDIS;
  320. nsec = rd32(IGC_SYSTIML);
  321. sec = rd32(IGC_SYSTIMH);
  322. systim = ktime_set(sec, nsec);
  323. adjust_time = adapter->base_time;
  324. expires_time = ktime_sub_ns(adjust_time, systim);
  325. hrtimer_start(&adapter->hrtimer, expires_time, HRTIMER_MODE_REL);
  326. }
  327. }
  328. wr32(IGC_TQAVCTRL, tqavctrl);
  329. wr32(IGC_QBVCYCLET_S, cycle);
  330. wr32(IGC_QBVCYCLET, cycle);
  331. baset_h = div_s64_rem(base_time, NSEC_PER_SEC, &baset_l);
  332. wr32(IGC_BASET_H, baset_h);
  333. /* In i226, Future base time is only supported when FutScdDis bit
  334. * is enabled and only active for re-configuration.
  335. * In this case, initialize the base time with zero to create
  336. * "re-configuration" scenario then only set the desired base time.
  337. */
  338. if (tqavctrl & IGC_TQAVCTRL_FUTSCDDIS)
  339. wr32(IGC_BASET_L, 0);
  340. wr32(IGC_BASET_L, baset_l);
  341. return 0;
  342. }
  343. int igc_tsn_reset(struct igc_adapter *adapter)
  344. {
  345. unsigned int new_flags;
  346. int err = 0;
  347. new_flags = igc_tsn_new_flags(adapter);
  348. if (!(new_flags & IGC_FLAG_TSN_ANY_ENABLED))
  349. return igc_tsn_disable_offload(adapter);
  350. err = igc_tsn_enable_offload(adapter);
  351. if (err < 0)
  352. return err;
  353. adapter->flags = new_flags;
  354. return err;
  355. }
  356. static bool igc_tsn_will_tx_mode_change(struct igc_adapter *adapter)
  357. {
  358. bool any_tsn_enabled = !!(igc_tsn_new_flags(adapter) &
  359. IGC_FLAG_TSN_ANY_ENABLED);
  360. return (any_tsn_enabled && !igc_tsn_is_tx_mode_in_tsn(adapter)) ||
  361. (!any_tsn_enabled && igc_tsn_is_tx_mode_in_tsn(adapter));
  362. }
  363. int igc_tsn_offload_apply(struct igc_adapter *adapter)
  364. {
  365. /* Per I225/6 HW Design Section 7.5.2.1 guideline, if tx mode change
  366. * from legacy->tsn or tsn->legacy, then reset adapter is needed.
  367. */
  368. if (netif_running(adapter->netdev) &&
  369. igc_tsn_will_tx_mode_change(adapter)) {
  370. schedule_work(&adapter->reset_task);
  371. return 0;
  372. }
  373. igc_tsn_reset(adapter);
  374. return 0;
  375. }