ice_dcb_lib.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright (c) 2019, Intel Corporation. */
  3. #include "ice_dcb_lib.h"
  4. #include "ice_dcb_nl.h"
  5. #include "devlink/devlink.h"
  6. /**
  7. * ice_dcb_get_ena_tc - return bitmap of enabled TCs
  8. * @dcbcfg: DCB config to evaluate for enabled TCs
  9. */
  10. static u8 ice_dcb_get_ena_tc(struct ice_dcbx_cfg *dcbcfg)
  11. {
  12. u8 i, num_tc, ena_tc = 1;
  13. num_tc = ice_dcb_get_num_tc(dcbcfg);
  14. for (i = 0; i < num_tc; i++)
  15. ena_tc |= BIT(i);
  16. return ena_tc;
  17. }
  18. /**
  19. * ice_is_pfc_causing_hung_q
  20. * @pf: pointer to PF structure
  21. * @txqueue: Tx queue which is supposedly hung queue
  22. *
  23. * find if PFC is causing the hung queue, if yes return true else false
  24. */
  25. bool ice_is_pfc_causing_hung_q(struct ice_pf *pf, unsigned int txqueue)
  26. {
  27. u8 num_tcs = 0, i, tc, up_mapped_tc, up_in_tc = 0;
  28. u64 ref_prio_xoff[ICE_MAX_UP];
  29. struct ice_vsi *vsi;
  30. u32 up2tc;
  31. vsi = ice_get_main_vsi(pf);
  32. if (!vsi)
  33. return false;
  34. ice_for_each_traffic_class(i)
  35. if (vsi->tc_cfg.ena_tc & BIT(i))
  36. num_tcs++;
  37. /* first find out the TC to which the hung queue belongs to */
  38. for (tc = 0; tc < num_tcs - 1; tc++)
  39. if (ice_find_q_in_range(vsi->tc_cfg.tc_info[tc].qoffset,
  40. vsi->tc_cfg.tc_info[tc + 1].qoffset,
  41. txqueue))
  42. break;
  43. /* Build a bit map of all UPs associated to the suspect hung queue TC,
  44. * so that we check for its counter increment.
  45. */
  46. up2tc = rd32(&pf->hw, PRTDCB_TUP2TC);
  47. for (i = 0; i < ICE_MAX_UP; i++) {
  48. up_mapped_tc = (up2tc >> (i * 3)) & 0x7;
  49. if (up_mapped_tc == tc)
  50. up_in_tc |= BIT(i);
  51. }
  52. /* Now that we figured out that hung queue is PFC enabled, still the
  53. * Tx timeout can be legitimate. So to make sure Tx timeout is
  54. * absolutely caused by PFC storm, check if the counters are
  55. * incrementing.
  56. */
  57. for (i = 0; i < ICE_MAX_UP; i++)
  58. if (up_in_tc & BIT(i))
  59. ref_prio_xoff[i] = pf->stats.priority_xoff_rx[i];
  60. ice_update_dcb_stats(pf);
  61. for (i = 0; i < ICE_MAX_UP; i++)
  62. if (up_in_tc & BIT(i))
  63. if (pf->stats.priority_xoff_rx[i] > ref_prio_xoff[i])
  64. return true;
  65. return false;
  66. }
  67. /**
  68. * ice_dcb_get_mode - gets the DCB mode
  69. * @port_info: pointer to port info structure
  70. * @host: if set it's HOST if not it's MANAGED
  71. */
  72. static u8 ice_dcb_get_mode(struct ice_port_info *port_info, bool host)
  73. {
  74. u8 mode;
  75. if (host)
  76. mode = DCB_CAP_DCBX_HOST;
  77. else
  78. mode = DCB_CAP_DCBX_LLD_MANAGED;
  79. if (port_info->qos_cfg.local_dcbx_cfg.dcbx_mode & ICE_DCBX_MODE_CEE)
  80. return mode | DCB_CAP_DCBX_VER_CEE;
  81. else
  82. return mode | DCB_CAP_DCBX_VER_IEEE;
  83. }
  84. /**
  85. * ice_dcb_get_num_tc - Get the number of TCs from DCBX config
  86. * @dcbcfg: config to retrieve number of TCs from
  87. */
  88. u8 ice_dcb_get_num_tc(struct ice_dcbx_cfg *dcbcfg)
  89. {
  90. bool tc_unused = false;
  91. u8 num_tc = 0;
  92. u8 ret = 0;
  93. int i;
  94. /* Scan the ETS Config Priority Table to find traffic classes
  95. * enabled and create a bitmask of enabled TCs
  96. */
  97. for (i = 0; i < CEE_DCBX_MAX_PRIO; i++)
  98. num_tc |= BIT(dcbcfg->etscfg.prio_table[i]);
  99. /* Scan bitmask for contiguous TCs starting with TC0 */
  100. for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++) {
  101. if (num_tc & BIT(i)) {
  102. if (!tc_unused) {
  103. ret++;
  104. } else {
  105. pr_err("Non-contiguous TCs - Disabling DCB\n");
  106. return 1;
  107. }
  108. } else {
  109. tc_unused = true;
  110. }
  111. }
  112. /* There is always at least 1 TC */
  113. if (!ret)
  114. ret = 1;
  115. return ret;
  116. }
  117. /**
  118. * ice_get_first_droptc - returns number of first droptc
  119. * @vsi: used to find the first droptc
  120. *
  121. * This function returns the value of first_droptc.
  122. * When DCB is enabled, first droptc information is derived from enabled_tc
  123. * and PFC enabled bits. otherwise this function returns 0 as there is one
  124. * TC without DCB (tc0)
  125. */
  126. static u8 ice_get_first_droptc(struct ice_vsi *vsi)
  127. {
  128. struct ice_dcbx_cfg *cfg = &vsi->port_info->qos_cfg.local_dcbx_cfg;
  129. struct device *dev = ice_pf_to_dev(vsi->back);
  130. u8 num_tc, ena_tc_map, pfc_ena_map;
  131. u8 i;
  132. num_tc = ice_dcb_get_num_tc(cfg);
  133. /* get bitmap of enabled TCs */
  134. ena_tc_map = ice_dcb_get_ena_tc(cfg);
  135. /* get bitmap of PFC enabled TCs */
  136. pfc_ena_map = cfg->pfc.pfcena;
  137. /* get first TC that is not PFC enabled */
  138. for (i = 0; i < num_tc; i++) {
  139. if ((ena_tc_map & BIT(i)) && (!(pfc_ena_map & BIT(i)))) {
  140. dev_dbg(dev, "first drop tc = %d\n", i);
  141. return i;
  142. }
  143. }
  144. dev_dbg(dev, "first drop tc = 0\n");
  145. return 0;
  146. }
  147. /**
  148. * ice_vsi_set_dcb_tc_cfg - Set VSI's TC based on DCB configuration
  149. * @vsi: pointer to the VSI instance
  150. */
  151. void ice_vsi_set_dcb_tc_cfg(struct ice_vsi *vsi)
  152. {
  153. struct ice_dcbx_cfg *cfg = &vsi->port_info->qos_cfg.local_dcbx_cfg;
  154. switch (vsi->type) {
  155. case ICE_VSI_PF:
  156. vsi->tc_cfg.ena_tc = ice_dcb_get_ena_tc(cfg);
  157. vsi->tc_cfg.numtc = ice_dcb_get_num_tc(cfg);
  158. break;
  159. case ICE_VSI_CHNL:
  160. case ICE_VSI_SF:
  161. vsi->tc_cfg.ena_tc = BIT(ice_get_first_droptc(vsi));
  162. vsi->tc_cfg.numtc = 1;
  163. break;
  164. case ICE_VSI_CTRL:
  165. case ICE_VSI_LB:
  166. default:
  167. vsi->tc_cfg.ena_tc = ICE_DFLT_TRAFFIC_CLASS;
  168. vsi->tc_cfg.numtc = 1;
  169. }
  170. }
  171. /**
  172. * ice_dcb_get_tc - Get the TC associated with the queue
  173. * @vsi: ptr to the VSI
  174. * @queue_index: queue number associated with VSI
  175. */
  176. u8 ice_dcb_get_tc(struct ice_vsi *vsi, int queue_index)
  177. {
  178. return vsi->tx_rings[queue_index]->dcb_tc;
  179. }
  180. /**
  181. * ice_vsi_cfg_dcb_rings - Update rings to reflect DCB TC
  182. * @vsi: VSI owner of rings being updated
  183. */
  184. void ice_vsi_cfg_dcb_rings(struct ice_vsi *vsi)
  185. {
  186. struct ice_tx_ring *tx_ring;
  187. struct ice_rx_ring *rx_ring;
  188. u16 qoffset, qcount;
  189. int i, n;
  190. if (!test_bit(ICE_FLAG_DCB_ENA, vsi->back->flags)) {
  191. /* Reset the TC information */
  192. ice_for_each_txq(vsi, i) {
  193. tx_ring = vsi->tx_rings[i];
  194. tx_ring->dcb_tc = 0;
  195. }
  196. ice_for_each_rxq(vsi, i) {
  197. rx_ring = vsi->rx_rings[i];
  198. rx_ring->dcb_tc = 0;
  199. }
  200. return;
  201. }
  202. ice_for_each_traffic_class(n) {
  203. if (!(vsi->tc_cfg.ena_tc & BIT(n)))
  204. break;
  205. qoffset = vsi->tc_cfg.tc_info[n].qoffset;
  206. qcount = vsi->tc_cfg.tc_info[n].qcount_tx;
  207. for (i = qoffset; i < (qoffset + qcount); i++)
  208. vsi->tx_rings[i]->dcb_tc = n;
  209. qcount = vsi->tc_cfg.tc_info[n].qcount_rx;
  210. for (i = qoffset; i < (qoffset + qcount); i++)
  211. vsi->rx_rings[i]->dcb_tc = n;
  212. }
  213. /* applicable only if "all_enatc" is set, which will be set from
  214. * setup_tc method as part of configuring channels
  215. */
  216. if (vsi->all_enatc) {
  217. u8 first_droptc = ice_get_first_droptc(vsi);
  218. /* When DCB is configured, TC for ADQ queues (which are really
  219. * PF queues) should be the first drop TC of the main VSI
  220. */
  221. ice_for_each_chnl_tc(n) {
  222. if (!(vsi->all_enatc & BIT(n)))
  223. break;
  224. qoffset = vsi->mqprio_qopt.qopt.offset[n];
  225. qcount = vsi->mqprio_qopt.qopt.count[n];
  226. for (i = qoffset; i < (qoffset + qcount); i++) {
  227. vsi->tx_rings[i]->dcb_tc = first_droptc;
  228. vsi->rx_rings[i]->dcb_tc = first_droptc;
  229. }
  230. }
  231. }
  232. }
  233. /**
  234. * ice_dcb_ena_dis_vsi - disable certain VSIs for DCB config/reconfig
  235. * @pf: pointer to the PF instance
  236. * @ena: true to enable VSIs, false to disable
  237. * @locked: true if caller holds RTNL lock, false otherwise
  238. *
  239. * Before a new DCB configuration can be applied, VSIs of type PF, SWITCHDEV
  240. * and CHNL need to be brought down. Following completion of DCB configuration
  241. * the VSIs that were downed need to be brought up again. This helper function
  242. * does both.
  243. */
  244. static void ice_dcb_ena_dis_vsi(struct ice_pf *pf, bool ena, bool locked)
  245. {
  246. int i;
  247. ice_for_each_vsi(pf, i) {
  248. struct ice_vsi *vsi = pf->vsi[i];
  249. if (!vsi)
  250. continue;
  251. switch (vsi->type) {
  252. case ICE_VSI_CHNL:
  253. case ICE_VSI_PF:
  254. if (ena)
  255. ice_ena_vsi(vsi, locked);
  256. else
  257. ice_dis_vsi(vsi, locked);
  258. break;
  259. default:
  260. continue;
  261. }
  262. }
  263. }
  264. /**
  265. * ice_dcb_bwchk - check if ETS bandwidth input parameters are correct
  266. * @pf: pointer to the PF struct
  267. * @dcbcfg: pointer to DCB config structure
  268. */
  269. int ice_dcb_bwchk(struct ice_pf *pf, struct ice_dcbx_cfg *dcbcfg)
  270. {
  271. struct ice_dcb_ets_cfg *etscfg = &dcbcfg->etscfg;
  272. u8 num_tc, total_bw = 0;
  273. int i;
  274. /* returns number of contigous TCs and 1 TC for non-contigous TCs,
  275. * since at least 1 TC has to be configured
  276. */
  277. num_tc = ice_dcb_get_num_tc(dcbcfg);
  278. /* no bandwidth checks required if there's only one TC, so assign
  279. * all bandwidth to TC0 and return
  280. */
  281. if (num_tc == 1) {
  282. etscfg->tcbwtable[0] = ICE_TC_MAX_BW;
  283. return 0;
  284. }
  285. for (i = 0; i < num_tc; i++)
  286. total_bw += etscfg->tcbwtable[i];
  287. if (!total_bw) {
  288. etscfg->tcbwtable[0] = ICE_TC_MAX_BW;
  289. } else if (total_bw != ICE_TC_MAX_BW) {
  290. dev_err(ice_pf_to_dev(pf), "Invalid config, total bandwidth must equal 100\n");
  291. return -EINVAL;
  292. }
  293. return 0;
  294. }
  295. /**
  296. * ice_pf_dcb_cfg - Apply new DCB configuration
  297. * @pf: pointer to the PF struct
  298. * @new_cfg: DCBX config to apply
  299. * @locked: is the RTNL held
  300. */
  301. int ice_pf_dcb_cfg(struct ice_pf *pf, struct ice_dcbx_cfg *new_cfg, bool locked)
  302. {
  303. struct ice_aqc_port_ets_elem buf = { 0 };
  304. struct ice_dcbx_cfg *old_cfg, *curr_cfg;
  305. struct device *dev = ice_pf_to_dev(pf);
  306. int ret = ICE_DCB_NO_HW_CHG;
  307. struct iidc_event *event;
  308. struct ice_vsi *pf_vsi;
  309. curr_cfg = &pf->hw.port_info->qos_cfg.local_dcbx_cfg;
  310. /* FW does not care if change happened */
  311. if (!pf->hw.port_info->qos_cfg.is_sw_lldp)
  312. ret = ICE_DCB_HW_CHG_RST;
  313. /* Enable DCB tagging only when more than one TC */
  314. if (ice_dcb_get_num_tc(new_cfg) > 1) {
  315. dev_dbg(dev, "DCB tagging enabled (num TC > 1)\n");
  316. if (pf->hw.port_info->is_custom_tx_enabled) {
  317. dev_err(dev, "Custom Tx scheduler feature enabled, can't configure DCB\n");
  318. return -EBUSY;
  319. }
  320. ice_tear_down_devlink_rate_tree(pf);
  321. set_bit(ICE_FLAG_DCB_ENA, pf->flags);
  322. } else {
  323. dev_dbg(dev, "DCB tagging disabled (num TC = 1)\n");
  324. clear_bit(ICE_FLAG_DCB_ENA, pf->flags);
  325. }
  326. if (!memcmp(new_cfg, curr_cfg, sizeof(*new_cfg))) {
  327. dev_dbg(dev, "No change in DCB config required\n");
  328. return ret;
  329. }
  330. if (ice_dcb_bwchk(pf, new_cfg))
  331. return -EINVAL;
  332. /* Store old config in case FW config fails */
  333. old_cfg = kmemdup(curr_cfg, sizeof(*old_cfg), GFP_KERNEL);
  334. if (!old_cfg)
  335. return -ENOMEM;
  336. dev_info(dev, "Commit DCB Configuration to the hardware\n");
  337. pf_vsi = ice_get_main_vsi(pf);
  338. if (!pf_vsi) {
  339. dev_dbg(dev, "PF VSI doesn't exist\n");
  340. ret = -EINVAL;
  341. goto free_cfg;
  342. }
  343. /* Notify AUX drivers about impending change to TCs */
  344. event = kzalloc(sizeof(*event), GFP_KERNEL);
  345. if (!event) {
  346. ret = -ENOMEM;
  347. goto free_cfg;
  348. }
  349. set_bit(IIDC_EVENT_BEFORE_TC_CHANGE, event->type);
  350. ice_send_event_to_aux(pf, event);
  351. kfree(event);
  352. /* avoid race conditions by holding the lock while disabling and
  353. * re-enabling the VSI
  354. */
  355. if (!locked)
  356. rtnl_lock();
  357. /* disable VSIs affected by DCB changes */
  358. ice_dcb_ena_dis_vsi(pf, false, true);
  359. memcpy(curr_cfg, new_cfg, sizeof(*curr_cfg));
  360. memcpy(&curr_cfg->etsrec, &curr_cfg->etscfg, sizeof(curr_cfg->etsrec));
  361. memcpy(&new_cfg->etsrec, &curr_cfg->etscfg, sizeof(curr_cfg->etsrec));
  362. /* Only send new config to HW if we are in SW LLDP mode. Otherwise,
  363. * the new config came from the HW in the first place.
  364. */
  365. if (pf->hw.port_info->qos_cfg.is_sw_lldp) {
  366. ret = ice_set_dcb_cfg(pf->hw.port_info);
  367. if (ret) {
  368. dev_err(dev, "Set DCB Config failed\n");
  369. /* Restore previous settings to local config */
  370. memcpy(curr_cfg, old_cfg, sizeof(*curr_cfg));
  371. goto out;
  372. }
  373. }
  374. ret = ice_query_port_ets(pf->hw.port_info, &buf, sizeof(buf), NULL);
  375. if (ret) {
  376. dev_err(dev, "Query Port ETS failed\n");
  377. goto out;
  378. }
  379. ice_pf_dcb_recfg(pf, false);
  380. out:
  381. /* enable previously downed VSIs */
  382. ice_dcb_ena_dis_vsi(pf, true, true);
  383. if (!locked)
  384. rtnl_unlock();
  385. free_cfg:
  386. kfree(old_cfg);
  387. return ret;
  388. }
  389. /**
  390. * ice_cfg_etsrec_defaults - Set default ETS recommended DCB config
  391. * @pi: port information structure
  392. */
  393. static void ice_cfg_etsrec_defaults(struct ice_port_info *pi)
  394. {
  395. struct ice_dcbx_cfg *dcbcfg = &pi->qos_cfg.local_dcbx_cfg;
  396. u8 i;
  397. /* Ensure ETS recommended DCB configuration is not already set */
  398. if (dcbcfg->etsrec.maxtcs)
  399. return;
  400. /* In CEE mode, set the default to 1 TC */
  401. dcbcfg->etsrec.maxtcs = 1;
  402. for (i = 0; i < ICE_MAX_TRAFFIC_CLASS; i++) {
  403. dcbcfg->etsrec.tcbwtable[i] = i ? 0 : 100;
  404. dcbcfg->etsrec.tsatable[i] = i ? ICE_IEEE_TSA_STRICT :
  405. ICE_IEEE_TSA_ETS;
  406. }
  407. }
  408. /**
  409. * ice_dcb_need_recfg - Check if DCB needs reconfig
  410. * @pf: board private structure
  411. * @old_cfg: current DCB config
  412. * @new_cfg: new DCB config
  413. */
  414. static bool
  415. ice_dcb_need_recfg(struct ice_pf *pf, struct ice_dcbx_cfg *old_cfg,
  416. struct ice_dcbx_cfg *new_cfg)
  417. {
  418. struct device *dev = ice_pf_to_dev(pf);
  419. bool need_reconfig = false;
  420. /* Check if ETS configuration has changed */
  421. if (memcmp(&new_cfg->etscfg, &old_cfg->etscfg,
  422. sizeof(new_cfg->etscfg))) {
  423. /* If Priority Table has changed reconfig is needed */
  424. if (memcmp(&new_cfg->etscfg.prio_table,
  425. &old_cfg->etscfg.prio_table,
  426. sizeof(new_cfg->etscfg.prio_table))) {
  427. need_reconfig = true;
  428. dev_dbg(dev, "ETS UP2TC changed.\n");
  429. }
  430. if (memcmp(&new_cfg->etscfg.tcbwtable,
  431. &old_cfg->etscfg.tcbwtable,
  432. sizeof(new_cfg->etscfg.tcbwtable)))
  433. dev_dbg(dev, "ETS TC BW Table changed.\n");
  434. if (memcmp(&new_cfg->etscfg.tsatable,
  435. &old_cfg->etscfg.tsatable,
  436. sizeof(new_cfg->etscfg.tsatable)))
  437. dev_dbg(dev, "ETS TSA Table changed.\n");
  438. }
  439. /* Check if PFC configuration has changed */
  440. if (memcmp(&new_cfg->pfc, &old_cfg->pfc, sizeof(new_cfg->pfc))) {
  441. need_reconfig = true;
  442. dev_dbg(dev, "PFC config change detected.\n");
  443. }
  444. /* Check if APP Table has changed */
  445. if (memcmp(&new_cfg->app, &old_cfg->app, sizeof(new_cfg->app))) {
  446. need_reconfig = true;
  447. dev_dbg(dev, "APP Table change detected.\n");
  448. }
  449. dev_dbg(dev, "dcb need_reconfig=%d\n", need_reconfig);
  450. return need_reconfig;
  451. }
  452. /**
  453. * ice_dcb_rebuild - rebuild DCB post reset
  454. * @pf: physical function instance
  455. */
  456. void ice_dcb_rebuild(struct ice_pf *pf)
  457. {
  458. struct ice_aqc_port_ets_elem buf = { 0 };
  459. struct device *dev = ice_pf_to_dev(pf);
  460. struct ice_dcbx_cfg *err_cfg;
  461. int ret;
  462. ret = ice_query_port_ets(pf->hw.port_info, &buf, sizeof(buf), NULL);
  463. if (ret) {
  464. dev_err(dev, "Query Port ETS failed\n");
  465. goto dcb_error;
  466. }
  467. mutex_lock(&pf->tc_mutex);
  468. if (!pf->hw.port_info->qos_cfg.is_sw_lldp)
  469. ice_cfg_etsrec_defaults(pf->hw.port_info);
  470. ret = ice_set_dcb_cfg(pf->hw.port_info);
  471. if (ret) {
  472. dev_err(dev, "Failed to set DCB config in rebuild\n");
  473. goto dcb_error;
  474. }
  475. if (!pf->hw.port_info->qos_cfg.is_sw_lldp) {
  476. ret = ice_cfg_lldp_mib_change(&pf->hw, true);
  477. if (ret && !pf->hw.port_info->qos_cfg.is_sw_lldp) {
  478. dev_err(dev, "Failed to register for MIB changes\n");
  479. goto dcb_error;
  480. }
  481. }
  482. dev_info(dev, "DCB info restored\n");
  483. ret = ice_query_port_ets(pf->hw.port_info, &buf, sizeof(buf), NULL);
  484. if (ret) {
  485. dev_err(dev, "Query Port ETS failed\n");
  486. goto dcb_error;
  487. }
  488. mutex_unlock(&pf->tc_mutex);
  489. return;
  490. dcb_error:
  491. dev_err(dev, "Disabling DCB until new settings occur\n");
  492. err_cfg = kzalloc(sizeof(*err_cfg), GFP_KERNEL);
  493. if (!err_cfg) {
  494. mutex_unlock(&pf->tc_mutex);
  495. return;
  496. }
  497. err_cfg->etscfg.willing = true;
  498. err_cfg->etscfg.tcbwtable[0] = ICE_TC_MAX_BW;
  499. err_cfg->etscfg.tsatable[0] = ICE_IEEE_TSA_ETS;
  500. memcpy(&err_cfg->etsrec, &err_cfg->etscfg, sizeof(err_cfg->etsrec));
  501. /* Coverity warns the return code of ice_pf_dcb_cfg() is not checked
  502. * here as is done for other calls to that function. That check is
  503. * not necessary since this is in this function's error cleanup path.
  504. * Suppress the Coverity warning with the following comment...
  505. */
  506. /* coverity[check_return] */
  507. ice_pf_dcb_cfg(pf, err_cfg, false);
  508. kfree(err_cfg);
  509. mutex_unlock(&pf->tc_mutex);
  510. }
  511. /**
  512. * ice_dcb_init_cfg - set the initial DCB config in SW
  513. * @pf: PF to apply config to
  514. * @locked: Is the RTNL held
  515. */
  516. static int ice_dcb_init_cfg(struct ice_pf *pf, bool locked)
  517. {
  518. struct ice_dcbx_cfg *newcfg;
  519. struct ice_port_info *pi;
  520. int ret = 0;
  521. pi = pf->hw.port_info;
  522. newcfg = kmemdup(&pi->qos_cfg.local_dcbx_cfg, sizeof(*newcfg),
  523. GFP_KERNEL);
  524. if (!newcfg)
  525. return -ENOMEM;
  526. memset(&pi->qos_cfg.local_dcbx_cfg, 0, sizeof(*newcfg));
  527. dev_info(ice_pf_to_dev(pf), "Configuring initial DCB values\n");
  528. if (ice_pf_dcb_cfg(pf, newcfg, locked))
  529. ret = -EINVAL;
  530. kfree(newcfg);
  531. return ret;
  532. }
  533. /**
  534. * ice_dcb_sw_dflt_cfg - Apply a default DCB config
  535. * @pf: PF to apply config to
  536. * @ets_willing: configure ETS willing
  537. * @locked: was this function called with RTNL held
  538. */
  539. int ice_dcb_sw_dflt_cfg(struct ice_pf *pf, bool ets_willing, bool locked)
  540. {
  541. struct ice_aqc_port_ets_elem buf = { 0 };
  542. struct ice_dcbx_cfg *dcbcfg;
  543. struct ice_port_info *pi;
  544. struct ice_hw *hw;
  545. int ret;
  546. hw = &pf->hw;
  547. pi = hw->port_info;
  548. dcbcfg = kzalloc(sizeof(*dcbcfg), GFP_KERNEL);
  549. if (!dcbcfg)
  550. return -ENOMEM;
  551. memset(&pi->qos_cfg.local_dcbx_cfg, 0, sizeof(*dcbcfg));
  552. dcbcfg->etscfg.willing = ets_willing ? 1 : 0;
  553. dcbcfg->etscfg.maxtcs = hw->func_caps.common_cap.maxtc;
  554. dcbcfg->etscfg.tcbwtable[0] = 100;
  555. dcbcfg->etscfg.tsatable[0] = ICE_IEEE_TSA_ETS;
  556. memcpy(&dcbcfg->etsrec, &dcbcfg->etscfg,
  557. sizeof(dcbcfg->etsrec));
  558. dcbcfg->etsrec.willing = 0;
  559. dcbcfg->pfc.willing = 1;
  560. dcbcfg->pfc.pfccap = hw->func_caps.common_cap.maxtc;
  561. dcbcfg->numapps = 1;
  562. dcbcfg->app[0].selector = ICE_APP_SEL_ETHTYPE;
  563. dcbcfg->app[0].priority = 3;
  564. dcbcfg->app[0].prot_id = ETH_P_FCOE;
  565. ret = ice_pf_dcb_cfg(pf, dcbcfg, locked);
  566. kfree(dcbcfg);
  567. if (ret)
  568. return ret;
  569. return ice_query_port_ets(pi, &buf, sizeof(buf), NULL);
  570. }
  571. /**
  572. * ice_dcb_tc_contig - Check that TCs are contiguous
  573. * @prio_table: pointer to priority table
  574. *
  575. * Check if TCs begin with TC0 and are contiguous
  576. */
  577. static bool ice_dcb_tc_contig(u8 *prio_table)
  578. {
  579. bool found_empty = false;
  580. u8 used_tc = 0;
  581. int i;
  582. /* Create a bitmap of used TCs */
  583. for (i = 0; i < CEE_DCBX_MAX_PRIO; i++)
  584. used_tc |= BIT(prio_table[i]);
  585. for (i = 0; i < CEE_DCBX_MAX_PRIO; i++) {
  586. if (used_tc & BIT(i)) {
  587. if (found_empty)
  588. return false;
  589. } else {
  590. found_empty = true;
  591. }
  592. }
  593. return true;
  594. }
  595. /**
  596. * ice_dcb_noncontig_cfg - Configure DCB for non-contiguous TCs
  597. * @pf: pointer to the PF struct
  598. *
  599. * If non-contiguous TCs, then configure SW DCB with TC0 and ETS non-willing
  600. */
  601. static int ice_dcb_noncontig_cfg(struct ice_pf *pf)
  602. {
  603. struct ice_dcbx_cfg *dcbcfg = &pf->hw.port_info->qos_cfg.local_dcbx_cfg;
  604. struct device *dev = ice_pf_to_dev(pf);
  605. int ret;
  606. /* Configure SW DCB default with ETS non-willing */
  607. ret = ice_dcb_sw_dflt_cfg(pf, false, true);
  608. if (ret) {
  609. dev_err(dev, "Failed to set local DCB config %d\n", ret);
  610. return ret;
  611. }
  612. /* Reconfigure with ETS willing so that FW will send LLDP MIB event */
  613. dcbcfg->etscfg.willing = 1;
  614. ret = ice_set_dcb_cfg(pf->hw.port_info);
  615. if (ret)
  616. dev_err(dev, "Failed to set DCB to unwilling\n");
  617. return ret;
  618. }
  619. /**
  620. * ice_pf_dcb_recfg - Reconfigure all VEBs and VSIs
  621. * @pf: pointer to the PF struct
  622. * @locked: is adev device lock held
  623. *
  624. * Assumed caller has already disabled all VSIs before
  625. * calling this function. Reconfiguring DCB based on
  626. * local_dcbx_cfg.
  627. */
  628. void ice_pf_dcb_recfg(struct ice_pf *pf, bool locked)
  629. {
  630. struct ice_dcbx_cfg *dcbcfg = &pf->hw.port_info->qos_cfg.local_dcbx_cfg;
  631. struct iidc_event *event;
  632. u8 tc_map = 0;
  633. int v, ret;
  634. /* Update each VSI */
  635. ice_for_each_vsi(pf, v) {
  636. struct ice_vsi *vsi = pf->vsi[v];
  637. if (!vsi)
  638. continue;
  639. if (vsi->type == ICE_VSI_PF) {
  640. tc_map = ice_dcb_get_ena_tc(dcbcfg);
  641. /* If DCBX request non-contiguous TC, then configure
  642. * default TC
  643. */
  644. if (!ice_dcb_tc_contig(dcbcfg->etscfg.prio_table)) {
  645. tc_map = ICE_DFLT_TRAFFIC_CLASS;
  646. ice_dcb_noncontig_cfg(pf);
  647. }
  648. } else if (vsi->type == ICE_VSI_CHNL) {
  649. tc_map = BIT(ice_get_first_droptc(vsi));
  650. } else {
  651. tc_map = ICE_DFLT_TRAFFIC_CLASS;
  652. }
  653. ret = ice_vsi_cfg_tc(vsi, tc_map);
  654. if (ret) {
  655. dev_err(ice_pf_to_dev(pf), "Failed to config TC for VSI index: %d\n",
  656. vsi->idx);
  657. continue;
  658. }
  659. /* no need to proceed with remaining cfg if it is CHNL
  660. * or switchdev VSI
  661. */
  662. if (vsi->type == ICE_VSI_CHNL)
  663. continue;
  664. ice_vsi_map_rings_to_vectors(vsi);
  665. if (vsi->type == ICE_VSI_PF)
  666. ice_dcbnl_set_all(vsi);
  667. }
  668. if (!locked) {
  669. /* Notify the AUX drivers that TC change is finished */
  670. event = kzalloc(sizeof(*event), GFP_KERNEL);
  671. if (!event)
  672. return;
  673. set_bit(IIDC_EVENT_AFTER_TC_CHANGE, event->type);
  674. ice_send_event_to_aux(pf, event);
  675. kfree(event);
  676. }
  677. }
  678. /**
  679. * ice_init_pf_dcb - initialize DCB for a PF
  680. * @pf: PF to initialize DCB for
  681. * @locked: Was function called with RTNL held
  682. */
  683. int ice_init_pf_dcb(struct ice_pf *pf, bool locked)
  684. {
  685. struct device *dev = ice_pf_to_dev(pf);
  686. struct ice_port_info *port_info;
  687. struct ice_hw *hw = &pf->hw;
  688. int err;
  689. port_info = hw->port_info;
  690. err = ice_init_dcb(hw, false);
  691. if (err && !port_info->qos_cfg.is_sw_lldp) {
  692. dev_err(dev, "Error initializing DCB %d\n", err);
  693. goto dcb_init_err;
  694. }
  695. dev_info(dev, "DCB is enabled in the hardware, max number of TCs supported on this port are %d\n",
  696. pf->hw.func_caps.common_cap.maxtc);
  697. if (err) {
  698. struct ice_vsi *pf_vsi;
  699. /* FW LLDP is disabled, activate SW DCBX/LLDP mode */
  700. dev_info(dev, "FW LLDP is disabled, DCBx/LLDP in SW mode.\n");
  701. clear_bit(ICE_FLAG_FW_LLDP_AGENT, pf->flags);
  702. err = ice_aq_set_pfc_mode(&pf->hw, ICE_AQC_PFC_VLAN_BASED_PFC,
  703. NULL);
  704. if (err)
  705. dev_info(dev, "Failed to set VLAN PFC mode\n");
  706. err = ice_dcb_sw_dflt_cfg(pf, true, locked);
  707. if (err) {
  708. dev_err(dev, "Failed to set local DCB config %d\n",
  709. err);
  710. err = -EIO;
  711. goto dcb_init_err;
  712. }
  713. /* If the FW DCBX engine is not running then Rx LLDP packets
  714. * need to be redirected up the stack.
  715. */
  716. pf_vsi = ice_get_main_vsi(pf);
  717. if (!pf_vsi) {
  718. dev_err(dev, "Failed to set local DCB config\n");
  719. err = -EIO;
  720. goto dcb_init_err;
  721. }
  722. ice_cfg_sw_lldp(pf_vsi, false, true);
  723. pf->dcbx_cap = ice_dcb_get_mode(port_info, true);
  724. return 0;
  725. }
  726. set_bit(ICE_FLAG_FW_LLDP_AGENT, pf->flags);
  727. /* DCBX/LLDP enabled in FW, set DCBNL mode advertisement */
  728. pf->dcbx_cap = ice_dcb_get_mode(port_info, false);
  729. err = ice_dcb_init_cfg(pf, locked);
  730. if (err)
  731. goto dcb_init_err;
  732. return 0;
  733. dcb_init_err:
  734. dev_err(dev, "DCB init failed\n");
  735. return err;
  736. }
  737. /**
  738. * ice_update_dcb_stats - Update DCB stats counters
  739. * @pf: PF whose stats needs to be updated
  740. */
  741. void ice_update_dcb_stats(struct ice_pf *pf)
  742. {
  743. struct ice_hw_port_stats *prev_ps, *cur_ps;
  744. struct ice_hw *hw = &pf->hw;
  745. u8 port;
  746. int i;
  747. port = hw->port_info->lport;
  748. prev_ps = &pf->stats_prev;
  749. cur_ps = &pf->stats;
  750. if (ice_is_reset_in_progress(pf->state))
  751. pf->stat_prev_loaded = false;
  752. for (i = 0; i < 8; i++) {
  753. ice_stat_update32(hw, GLPRT_PXOFFRXC(port, i),
  754. pf->stat_prev_loaded,
  755. &prev_ps->priority_xoff_rx[i],
  756. &cur_ps->priority_xoff_rx[i]);
  757. ice_stat_update32(hw, GLPRT_PXONRXC(port, i),
  758. pf->stat_prev_loaded,
  759. &prev_ps->priority_xon_rx[i],
  760. &cur_ps->priority_xon_rx[i]);
  761. ice_stat_update32(hw, GLPRT_PXONTXC(port, i),
  762. pf->stat_prev_loaded,
  763. &prev_ps->priority_xon_tx[i],
  764. &cur_ps->priority_xon_tx[i]);
  765. ice_stat_update32(hw, GLPRT_PXOFFTXC(port, i),
  766. pf->stat_prev_loaded,
  767. &prev_ps->priority_xoff_tx[i],
  768. &cur_ps->priority_xoff_tx[i]);
  769. ice_stat_update32(hw, GLPRT_RXON2OFFCNT(port, i),
  770. pf->stat_prev_loaded,
  771. &prev_ps->priority_xon_2_xoff[i],
  772. &cur_ps->priority_xon_2_xoff[i]);
  773. }
  774. }
  775. /**
  776. * ice_tx_prepare_vlan_flags_dcb - prepare VLAN tagging for DCB
  777. * @tx_ring: ring to send buffer on
  778. * @first: pointer to struct ice_tx_buf
  779. *
  780. * This should not be called if the outer VLAN is software offloaded as the VLAN
  781. * tag will already be configured with the correct ID and priority bits
  782. */
  783. void
  784. ice_tx_prepare_vlan_flags_dcb(struct ice_tx_ring *tx_ring,
  785. struct ice_tx_buf *first)
  786. {
  787. struct sk_buff *skb = first->skb;
  788. if (!test_bit(ICE_FLAG_DCB_ENA, tx_ring->vsi->back->flags))
  789. return;
  790. /* Insert 802.1p priority into VLAN header */
  791. if ((first->tx_flags & ICE_TX_FLAGS_HW_VLAN ||
  792. first->tx_flags & ICE_TX_FLAGS_HW_OUTER_SINGLE_VLAN) ||
  793. skb->priority != TC_PRIO_CONTROL) {
  794. first->vid &= ~VLAN_PRIO_MASK;
  795. /* Mask the lower 3 bits to set the 802.1p priority */
  796. first->vid |= FIELD_PREP(VLAN_PRIO_MASK, skb->priority);
  797. /* if this is not already set it means a VLAN 0 + priority needs
  798. * to be offloaded
  799. */
  800. if (tx_ring->flags & ICE_TX_FLAGS_RING_VLAN_L2TAG2)
  801. first->tx_flags |= ICE_TX_FLAGS_HW_OUTER_SINGLE_VLAN;
  802. else
  803. first->tx_flags |= ICE_TX_FLAGS_HW_VLAN;
  804. }
  805. }
  806. /**
  807. * ice_dcb_is_mib_change_pending - Check if MIB change is pending
  808. * @state: MIB change state
  809. */
  810. static bool ice_dcb_is_mib_change_pending(u8 state)
  811. {
  812. return ICE_AQ_LLDP_MIB_CHANGE_PENDING ==
  813. FIELD_GET(ICE_AQ_LLDP_MIB_CHANGE_STATE_M, state);
  814. }
  815. /**
  816. * ice_dcb_process_lldp_set_mib_change - Process MIB change
  817. * @pf: ptr to ice_pf
  818. * @event: pointer to the admin queue receive event
  819. */
  820. void
  821. ice_dcb_process_lldp_set_mib_change(struct ice_pf *pf,
  822. struct ice_rq_event_info *event)
  823. {
  824. struct ice_aqc_port_ets_elem buf = { 0 };
  825. struct device *dev = ice_pf_to_dev(pf);
  826. struct ice_aqc_lldp_get_mib *mib;
  827. struct ice_dcbx_cfg tmp_dcbx_cfg;
  828. bool pending_handled = true;
  829. bool need_reconfig = false;
  830. struct ice_port_info *pi;
  831. u8 mib_type;
  832. int ret;
  833. /* Not DCB capable or capability disabled */
  834. if (!(test_bit(ICE_FLAG_DCB_CAPABLE, pf->flags)))
  835. return;
  836. if (pf->dcbx_cap & DCB_CAP_DCBX_HOST) {
  837. dev_dbg(dev, "MIB Change Event in HOST mode\n");
  838. return;
  839. }
  840. pi = pf->hw.port_info;
  841. mib = (struct ice_aqc_lldp_get_mib *)&event->desc.params.raw;
  842. /* Ignore if event is not for Nearest Bridge */
  843. mib_type = FIELD_GET(ICE_AQ_LLDP_BRID_TYPE_M, mib->type);
  844. dev_dbg(dev, "LLDP event MIB bridge type 0x%x\n", mib_type);
  845. if (mib_type != ICE_AQ_LLDP_BRID_TYPE_NEAREST_BRID)
  846. return;
  847. /* A pending change event contains accurate config information, and
  848. * the FW setting has not been updaed yet, so detect if change is
  849. * pending to determine where to pull config information from
  850. * (FW vs event)
  851. */
  852. if (ice_dcb_is_mib_change_pending(mib->state))
  853. pending_handled = false;
  854. /* Check MIB Type and return if event for Remote MIB update */
  855. mib_type = FIELD_GET(ICE_AQ_LLDP_MIB_TYPE_M, mib->type);
  856. dev_dbg(dev, "LLDP event mib type %s\n", mib_type ? "remote" : "local");
  857. if (mib_type == ICE_AQ_LLDP_MIB_REMOTE) {
  858. /* Update the remote cached instance and return */
  859. if (!pending_handled) {
  860. ice_get_dcb_cfg_from_mib_change(pi, event);
  861. } else {
  862. ret =
  863. ice_aq_get_dcb_cfg(pi->hw, ICE_AQ_LLDP_MIB_REMOTE,
  864. ICE_AQ_LLDP_BRID_TYPE_NEAREST_BRID,
  865. &pi->qos_cfg.remote_dcbx_cfg);
  866. if (ret)
  867. dev_dbg(dev, "Failed to get remote DCB config\n");
  868. }
  869. return;
  870. }
  871. /* That a DCB change has happened is now determined */
  872. mutex_lock(&pf->tc_mutex);
  873. /* store the old configuration */
  874. tmp_dcbx_cfg = pi->qos_cfg.local_dcbx_cfg;
  875. /* Reset the old DCBX configuration data */
  876. memset(&pi->qos_cfg.local_dcbx_cfg, 0,
  877. sizeof(pi->qos_cfg.local_dcbx_cfg));
  878. /* Get updated DCBX data from firmware */
  879. if (!pending_handled) {
  880. ice_get_dcb_cfg_from_mib_change(pi, event);
  881. } else {
  882. ret = ice_get_dcb_cfg(pi);
  883. if (ret) {
  884. dev_err(dev, "Failed to get DCB config\n");
  885. goto out;
  886. }
  887. }
  888. /* No change detected in DCBX configs */
  889. if (!memcmp(&tmp_dcbx_cfg, &pi->qos_cfg.local_dcbx_cfg,
  890. sizeof(tmp_dcbx_cfg))) {
  891. dev_dbg(dev, "No change detected in DCBX configuration.\n");
  892. goto out;
  893. }
  894. pf->dcbx_cap = ice_dcb_get_mode(pi, false);
  895. need_reconfig = ice_dcb_need_recfg(pf, &tmp_dcbx_cfg,
  896. &pi->qos_cfg.local_dcbx_cfg);
  897. ice_dcbnl_flush_apps(pf, &tmp_dcbx_cfg, &pi->qos_cfg.local_dcbx_cfg);
  898. if (!need_reconfig)
  899. goto out;
  900. /* Enable DCB tagging only when more than one TC */
  901. if (ice_dcb_get_num_tc(&pi->qos_cfg.local_dcbx_cfg) > 1) {
  902. dev_dbg(dev, "DCB tagging enabled (num TC > 1)\n");
  903. set_bit(ICE_FLAG_DCB_ENA, pf->flags);
  904. } else {
  905. dev_dbg(dev, "DCB tagging disabled (num TC = 1)\n");
  906. clear_bit(ICE_FLAG_DCB_ENA, pf->flags);
  907. }
  908. /* Send Execute Pending MIB Change event if it is a Pending event */
  909. if (!pending_handled) {
  910. ice_lldp_execute_pending_mib(&pf->hw);
  911. pending_handled = true;
  912. }
  913. rtnl_lock();
  914. /* disable VSIs affected by DCB changes */
  915. ice_dcb_ena_dis_vsi(pf, false, true);
  916. ret = ice_query_port_ets(pi, &buf, sizeof(buf), NULL);
  917. if (ret) {
  918. dev_err(dev, "Query Port ETS failed\n");
  919. goto unlock_rtnl;
  920. }
  921. /* changes in configuration update VSI */
  922. ice_pf_dcb_recfg(pf, false);
  923. /* enable previously downed VSIs */
  924. ice_dcb_ena_dis_vsi(pf, true, true);
  925. unlock_rtnl:
  926. rtnl_unlock();
  927. out:
  928. mutex_unlock(&pf->tc_mutex);
  929. /* Send Execute Pending MIB Change event if it is a Pending event */
  930. if (!pending_handled)
  931. ice_lldp_execute_pending_mib(&pf->hw);
  932. }