devlink.c 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright (c) 2020, Intel Corporation. */
  3. #include <linux/vmalloc.h>
  4. #include "ice.h"
  5. #include "ice_lib.h"
  6. #include "devlink.h"
  7. #include "devlink_port.h"
  8. #include "ice_eswitch.h"
  9. #include "ice_fw_update.h"
  10. #include "ice_dcb_lib.h"
  11. #include "ice_sf_eth.h"
  12. /* context for devlink info version reporting */
  13. struct ice_info_ctx {
  14. char buf[128];
  15. struct ice_orom_info pending_orom;
  16. struct ice_nvm_info pending_nvm;
  17. struct ice_netlist_info pending_netlist;
  18. struct ice_hw_dev_caps dev_caps;
  19. };
  20. /* The following functions are used to format specific strings for various
  21. * devlink info versions. The ctx parameter is used to provide the storage
  22. * buffer, as well as any ancillary information calculated when the info
  23. * request was made.
  24. *
  25. * If a version does not exist, for example when attempting to get the
  26. * inactive version of flash when there is no pending update, the function
  27. * should leave the buffer in the ctx structure empty.
  28. */
  29. static void ice_info_get_dsn(struct ice_pf *pf, struct ice_info_ctx *ctx)
  30. {
  31. u8 dsn[8];
  32. /* Copy the DSN into an array in Big Endian format */
  33. put_unaligned_be64(pci_get_dsn(pf->pdev), dsn);
  34. snprintf(ctx->buf, sizeof(ctx->buf), "%8phD", dsn);
  35. }
  36. static void ice_info_pba(struct ice_pf *pf, struct ice_info_ctx *ctx)
  37. {
  38. struct ice_hw *hw = &pf->hw;
  39. int status;
  40. status = ice_read_pba_string(hw, (u8 *)ctx->buf, sizeof(ctx->buf));
  41. if (status)
  42. /* We failed to locate the PBA, so just skip this entry */
  43. dev_dbg(ice_pf_to_dev(pf), "Failed to read Product Board Assembly string, status %d\n",
  44. status);
  45. }
  46. static void ice_info_fw_mgmt(struct ice_pf *pf, struct ice_info_ctx *ctx)
  47. {
  48. struct ice_hw *hw = &pf->hw;
  49. snprintf(ctx->buf, sizeof(ctx->buf), "%u.%u.%u",
  50. hw->fw_maj_ver, hw->fw_min_ver, hw->fw_patch);
  51. }
  52. static void ice_info_fw_api(struct ice_pf *pf, struct ice_info_ctx *ctx)
  53. {
  54. struct ice_hw *hw = &pf->hw;
  55. snprintf(ctx->buf, sizeof(ctx->buf), "%u.%u.%u", hw->api_maj_ver,
  56. hw->api_min_ver, hw->api_patch);
  57. }
  58. static void ice_info_fw_build(struct ice_pf *pf, struct ice_info_ctx *ctx)
  59. {
  60. struct ice_hw *hw = &pf->hw;
  61. snprintf(ctx->buf, sizeof(ctx->buf), "0x%08x", hw->fw_build);
  62. }
  63. static void ice_info_orom_ver(struct ice_pf *pf, struct ice_info_ctx *ctx)
  64. {
  65. struct ice_orom_info *orom = &pf->hw.flash.orom;
  66. snprintf(ctx->buf, sizeof(ctx->buf), "%u.%u.%u",
  67. orom->major, orom->build, orom->patch);
  68. }
  69. static void
  70. ice_info_pending_orom_ver(struct ice_pf __always_unused *pf,
  71. struct ice_info_ctx *ctx)
  72. {
  73. struct ice_orom_info *orom = &ctx->pending_orom;
  74. if (ctx->dev_caps.common_cap.nvm_update_pending_orom)
  75. snprintf(ctx->buf, sizeof(ctx->buf), "%u.%u.%u",
  76. orom->major, orom->build, orom->patch);
  77. }
  78. static void ice_info_nvm_ver(struct ice_pf *pf, struct ice_info_ctx *ctx)
  79. {
  80. struct ice_nvm_info *nvm = &pf->hw.flash.nvm;
  81. snprintf(ctx->buf, sizeof(ctx->buf), "%x.%02x", nvm->major, nvm->minor);
  82. }
  83. static void
  84. ice_info_pending_nvm_ver(struct ice_pf __always_unused *pf,
  85. struct ice_info_ctx *ctx)
  86. {
  87. struct ice_nvm_info *nvm = &ctx->pending_nvm;
  88. if (ctx->dev_caps.common_cap.nvm_update_pending_nvm)
  89. snprintf(ctx->buf, sizeof(ctx->buf), "%x.%02x",
  90. nvm->major, nvm->minor);
  91. }
  92. static void ice_info_eetrack(struct ice_pf *pf, struct ice_info_ctx *ctx)
  93. {
  94. struct ice_nvm_info *nvm = &pf->hw.flash.nvm;
  95. snprintf(ctx->buf, sizeof(ctx->buf), "0x%08x", nvm->eetrack);
  96. }
  97. static void
  98. ice_info_pending_eetrack(struct ice_pf *pf, struct ice_info_ctx *ctx)
  99. {
  100. struct ice_nvm_info *nvm = &ctx->pending_nvm;
  101. if (ctx->dev_caps.common_cap.nvm_update_pending_nvm)
  102. snprintf(ctx->buf, sizeof(ctx->buf), "0x%08x", nvm->eetrack);
  103. }
  104. static void ice_info_ddp_pkg_name(struct ice_pf *pf, struct ice_info_ctx *ctx)
  105. {
  106. struct ice_hw *hw = &pf->hw;
  107. snprintf(ctx->buf, sizeof(ctx->buf), "%s", hw->active_pkg_name);
  108. }
  109. static void
  110. ice_info_ddp_pkg_version(struct ice_pf *pf, struct ice_info_ctx *ctx)
  111. {
  112. struct ice_pkg_ver *pkg = &pf->hw.active_pkg_ver;
  113. snprintf(ctx->buf, sizeof(ctx->buf), "%u.%u.%u.%u",
  114. pkg->major, pkg->minor, pkg->update, pkg->draft);
  115. }
  116. static void
  117. ice_info_ddp_pkg_bundle_id(struct ice_pf *pf, struct ice_info_ctx *ctx)
  118. {
  119. snprintf(ctx->buf, sizeof(ctx->buf), "0x%08x", pf->hw.active_track_id);
  120. }
  121. static void ice_info_netlist_ver(struct ice_pf *pf, struct ice_info_ctx *ctx)
  122. {
  123. struct ice_netlist_info *netlist = &pf->hw.flash.netlist;
  124. /* The netlist version fields are BCD formatted */
  125. snprintf(ctx->buf, sizeof(ctx->buf), "%x.%x.%x-%x.%x.%x",
  126. netlist->major, netlist->minor,
  127. netlist->type >> 16, netlist->type & 0xFFFF,
  128. netlist->rev, netlist->cust_ver);
  129. }
  130. static void ice_info_netlist_build(struct ice_pf *pf, struct ice_info_ctx *ctx)
  131. {
  132. struct ice_netlist_info *netlist = &pf->hw.flash.netlist;
  133. snprintf(ctx->buf, sizeof(ctx->buf), "0x%08x", netlist->hash);
  134. }
  135. static void
  136. ice_info_pending_netlist_ver(struct ice_pf __always_unused *pf,
  137. struct ice_info_ctx *ctx)
  138. {
  139. struct ice_netlist_info *netlist = &ctx->pending_netlist;
  140. /* The netlist version fields are BCD formatted */
  141. if (ctx->dev_caps.common_cap.nvm_update_pending_netlist)
  142. snprintf(ctx->buf, sizeof(ctx->buf), "%x.%x.%x-%x.%x.%x",
  143. netlist->major, netlist->minor,
  144. netlist->type >> 16, netlist->type & 0xFFFF,
  145. netlist->rev, netlist->cust_ver);
  146. }
  147. static void
  148. ice_info_pending_netlist_build(struct ice_pf __always_unused *pf,
  149. struct ice_info_ctx *ctx)
  150. {
  151. struct ice_netlist_info *netlist = &ctx->pending_netlist;
  152. if (ctx->dev_caps.common_cap.nvm_update_pending_netlist)
  153. snprintf(ctx->buf, sizeof(ctx->buf), "0x%08x", netlist->hash);
  154. }
  155. static void ice_info_cgu_fw_build(struct ice_pf *pf, struct ice_info_ctx *ctx)
  156. {
  157. u32 id, cfg_ver, fw_ver;
  158. if (!ice_is_feature_supported(pf, ICE_F_CGU))
  159. return;
  160. if (ice_aq_get_cgu_info(&pf->hw, &id, &cfg_ver, &fw_ver))
  161. return;
  162. snprintf(ctx->buf, sizeof(ctx->buf), "%u.%u.%u", id, cfg_ver, fw_ver);
  163. }
  164. static void ice_info_cgu_id(struct ice_pf *pf, struct ice_info_ctx *ctx)
  165. {
  166. if (!ice_is_feature_supported(pf, ICE_F_CGU))
  167. return;
  168. snprintf(ctx->buf, sizeof(ctx->buf), "%u", pf->hw.cgu_part_number);
  169. }
  170. #define fixed(key, getter) { ICE_VERSION_FIXED, key, getter, NULL }
  171. #define running(key, getter) { ICE_VERSION_RUNNING, key, getter, NULL }
  172. #define stored(key, getter, fallback) { ICE_VERSION_STORED, key, getter, fallback }
  173. /* The combined() macro inserts both the running entry as well as a stored
  174. * entry. The running entry will always report the version from the active
  175. * handler. The stored entry will first try the pending handler, and fallback
  176. * to the active handler if the pending function does not report a version.
  177. * The pending handler should check the status of a pending update for the
  178. * relevant flash component. It should only fill in the buffer in the case
  179. * where a valid pending version is available. This ensures that the related
  180. * stored and running versions remain in sync, and that stored versions are
  181. * correctly reported as expected.
  182. */
  183. #define combined(key, active, pending) \
  184. running(key, active), \
  185. stored(key, pending, active)
  186. enum ice_version_type {
  187. ICE_VERSION_FIXED,
  188. ICE_VERSION_RUNNING,
  189. ICE_VERSION_STORED,
  190. };
  191. static const struct ice_devlink_version {
  192. enum ice_version_type type;
  193. const char *key;
  194. void (*getter)(struct ice_pf *pf, struct ice_info_ctx *ctx);
  195. void (*fallback)(struct ice_pf *pf, struct ice_info_ctx *ctx);
  196. } ice_devlink_versions[] = {
  197. fixed(DEVLINK_INFO_VERSION_GENERIC_BOARD_ID, ice_info_pba),
  198. running(DEVLINK_INFO_VERSION_GENERIC_FW_MGMT, ice_info_fw_mgmt),
  199. running("fw.mgmt.api", ice_info_fw_api),
  200. running("fw.mgmt.build", ice_info_fw_build),
  201. combined(DEVLINK_INFO_VERSION_GENERIC_FW_UNDI, ice_info_orom_ver, ice_info_pending_orom_ver),
  202. combined("fw.psid.api", ice_info_nvm_ver, ice_info_pending_nvm_ver),
  203. combined(DEVLINK_INFO_VERSION_GENERIC_FW_BUNDLE_ID, ice_info_eetrack, ice_info_pending_eetrack),
  204. running("fw.app.name", ice_info_ddp_pkg_name),
  205. running(DEVLINK_INFO_VERSION_GENERIC_FW_APP, ice_info_ddp_pkg_version),
  206. running("fw.app.bundle_id", ice_info_ddp_pkg_bundle_id),
  207. combined("fw.netlist", ice_info_netlist_ver, ice_info_pending_netlist_ver),
  208. combined("fw.netlist.build", ice_info_netlist_build, ice_info_pending_netlist_build),
  209. fixed("cgu.id", ice_info_cgu_id),
  210. running("fw.cgu", ice_info_cgu_fw_build),
  211. };
  212. /**
  213. * ice_devlink_info_get - .info_get devlink handler
  214. * @devlink: devlink instance structure
  215. * @req: the devlink info request
  216. * @extack: extended netdev ack structure
  217. *
  218. * Callback for the devlink .info_get operation. Reports information about the
  219. * device.
  220. *
  221. * Return: zero on success or an error code on failure.
  222. */
  223. static int ice_devlink_info_get(struct devlink *devlink,
  224. struct devlink_info_req *req,
  225. struct netlink_ext_ack *extack)
  226. {
  227. struct ice_pf *pf = devlink_priv(devlink);
  228. struct device *dev = ice_pf_to_dev(pf);
  229. struct ice_hw *hw = &pf->hw;
  230. struct ice_info_ctx *ctx;
  231. size_t i;
  232. int err;
  233. err = ice_wait_for_reset(pf, 10 * HZ);
  234. if (err) {
  235. NL_SET_ERR_MSG_MOD(extack, "Device is busy resetting");
  236. return err;
  237. }
  238. ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
  239. if (!ctx)
  240. return -ENOMEM;
  241. /* discover capabilities first */
  242. err = ice_discover_dev_caps(hw, &ctx->dev_caps);
  243. if (err) {
  244. dev_dbg(dev, "Failed to discover device capabilities, status %d aq_err %s\n",
  245. err, ice_aq_str(hw->adminq.sq_last_status));
  246. NL_SET_ERR_MSG_MOD(extack, "Unable to discover device capabilities");
  247. goto out_free_ctx;
  248. }
  249. if (ctx->dev_caps.common_cap.nvm_update_pending_orom) {
  250. err = ice_get_inactive_orom_ver(hw, &ctx->pending_orom);
  251. if (err) {
  252. dev_dbg(dev, "Unable to read inactive Option ROM version data, status %d aq_err %s\n",
  253. err, ice_aq_str(hw->adminq.sq_last_status));
  254. /* disable display of pending Option ROM */
  255. ctx->dev_caps.common_cap.nvm_update_pending_orom = false;
  256. }
  257. }
  258. if (ctx->dev_caps.common_cap.nvm_update_pending_nvm) {
  259. err = ice_get_inactive_nvm_ver(hw, &ctx->pending_nvm);
  260. if (err) {
  261. dev_dbg(dev, "Unable to read inactive NVM version data, status %d aq_err %s\n",
  262. err, ice_aq_str(hw->adminq.sq_last_status));
  263. /* disable display of pending Option ROM */
  264. ctx->dev_caps.common_cap.nvm_update_pending_nvm = false;
  265. }
  266. }
  267. if (ctx->dev_caps.common_cap.nvm_update_pending_netlist) {
  268. err = ice_get_inactive_netlist_ver(hw, &ctx->pending_netlist);
  269. if (err) {
  270. dev_dbg(dev, "Unable to read inactive Netlist version data, status %d aq_err %s\n",
  271. err, ice_aq_str(hw->adminq.sq_last_status));
  272. /* disable display of pending Option ROM */
  273. ctx->dev_caps.common_cap.nvm_update_pending_netlist = false;
  274. }
  275. }
  276. ice_info_get_dsn(pf, ctx);
  277. err = devlink_info_serial_number_put(req, ctx->buf);
  278. if (err) {
  279. NL_SET_ERR_MSG_MOD(extack, "Unable to set serial number");
  280. goto out_free_ctx;
  281. }
  282. for (i = 0; i < ARRAY_SIZE(ice_devlink_versions); i++) {
  283. enum ice_version_type type = ice_devlink_versions[i].type;
  284. const char *key = ice_devlink_versions[i].key;
  285. memset(ctx->buf, 0, sizeof(ctx->buf));
  286. ice_devlink_versions[i].getter(pf, ctx);
  287. /* If the default getter doesn't report a version, use the
  288. * fallback function. This is primarily useful in the case of
  289. * "stored" versions that want to report the same value as the
  290. * running version in the normal case of no pending update.
  291. */
  292. if (ctx->buf[0] == '\0' && ice_devlink_versions[i].fallback)
  293. ice_devlink_versions[i].fallback(pf, ctx);
  294. /* Do not report missing versions */
  295. if (ctx->buf[0] == '\0')
  296. continue;
  297. switch (type) {
  298. case ICE_VERSION_FIXED:
  299. err = devlink_info_version_fixed_put(req, key, ctx->buf);
  300. if (err) {
  301. NL_SET_ERR_MSG_MOD(extack, "Unable to set fixed version");
  302. goto out_free_ctx;
  303. }
  304. break;
  305. case ICE_VERSION_RUNNING:
  306. err = devlink_info_version_running_put(req, key, ctx->buf);
  307. if (err) {
  308. NL_SET_ERR_MSG_MOD(extack, "Unable to set running version");
  309. goto out_free_ctx;
  310. }
  311. break;
  312. case ICE_VERSION_STORED:
  313. err = devlink_info_version_stored_put(req, key, ctx->buf);
  314. if (err) {
  315. NL_SET_ERR_MSG_MOD(extack, "Unable to set stored version");
  316. goto out_free_ctx;
  317. }
  318. break;
  319. }
  320. }
  321. out_free_ctx:
  322. kfree(ctx);
  323. return err;
  324. }
  325. /**
  326. * ice_devlink_reload_empr_start - Start EMP reset to activate new firmware
  327. * @pf: pointer to the pf instance
  328. * @extack: netlink extended ACK structure
  329. *
  330. * Allow user to activate new Embedded Management Processor firmware by
  331. * issuing device specific EMP reset. Called in response to
  332. * a DEVLINK_CMD_RELOAD with the DEVLINK_RELOAD_ACTION_FW_ACTIVATE.
  333. *
  334. * Note that teardown and rebuild of the driver state happens automatically as
  335. * part of an interrupt and watchdog task. This is because all physical
  336. * functions on the device must be able to reset when an EMP reset occurs from
  337. * any source.
  338. */
  339. static int
  340. ice_devlink_reload_empr_start(struct ice_pf *pf,
  341. struct netlink_ext_ack *extack)
  342. {
  343. struct device *dev = ice_pf_to_dev(pf);
  344. struct ice_hw *hw = &pf->hw;
  345. u8 pending;
  346. int err;
  347. err = ice_get_pending_updates(pf, &pending, extack);
  348. if (err)
  349. return err;
  350. /* pending is a bitmask of which flash banks have a pending update,
  351. * including the main NVM bank, the Option ROM bank, and the netlist
  352. * bank. If any of these bits are set, then there is a pending update
  353. * waiting to be activated.
  354. */
  355. if (!pending) {
  356. NL_SET_ERR_MSG_MOD(extack, "No pending firmware update");
  357. return -ECANCELED;
  358. }
  359. if (pf->fw_emp_reset_disabled) {
  360. NL_SET_ERR_MSG_MOD(extack, "EMP reset is not available. To activate firmware, a reboot or power cycle is needed");
  361. return -ECANCELED;
  362. }
  363. dev_dbg(dev, "Issuing device EMP reset to activate firmware\n");
  364. err = ice_aq_nvm_update_empr(hw);
  365. if (err) {
  366. dev_err(dev, "Failed to trigger EMP device reset to reload firmware, err %d aq_err %s\n",
  367. err, ice_aq_str(hw->adminq.sq_last_status));
  368. NL_SET_ERR_MSG_MOD(extack, "Failed to trigger EMP device reset to reload firmware");
  369. return err;
  370. }
  371. return 0;
  372. }
  373. /**
  374. * ice_devlink_reinit_down - unload given PF
  375. * @pf: pointer to the PF struct
  376. */
  377. static void ice_devlink_reinit_down(struct ice_pf *pf)
  378. {
  379. /* No need to take devl_lock, it's already taken by devlink API */
  380. ice_unload(pf);
  381. rtnl_lock();
  382. ice_vsi_decfg(ice_get_main_vsi(pf));
  383. rtnl_unlock();
  384. ice_deinit_dev(pf);
  385. }
  386. /**
  387. * ice_devlink_reload_down - prepare for reload
  388. * @devlink: pointer to the devlink instance to reload
  389. * @netns_change: if true, the network namespace is changing
  390. * @action: the action to perform
  391. * @limit: limits on what reload should do, such as not resetting
  392. * @extack: netlink extended ACK structure
  393. */
  394. static int
  395. ice_devlink_reload_down(struct devlink *devlink, bool netns_change,
  396. enum devlink_reload_action action,
  397. enum devlink_reload_limit limit,
  398. struct netlink_ext_ack *extack)
  399. {
  400. struct ice_pf *pf = devlink_priv(devlink);
  401. switch (action) {
  402. case DEVLINK_RELOAD_ACTION_DRIVER_REINIT:
  403. if (ice_is_eswitch_mode_switchdev(pf)) {
  404. NL_SET_ERR_MSG_MOD(extack,
  405. "Go to legacy mode before doing reinit");
  406. return -EOPNOTSUPP;
  407. }
  408. if (ice_is_adq_active(pf)) {
  409. NL_SET_ERR_MSG_MOD(extack,
  410. "Turn off ADQ before doing reinit");
  411. return -EOPNOTSUPP;
  412. }
  413. if (ice_has_vfs(pf)) {
  414. NL_SET_ERR_MSG_MOD(extack,
  415. "Remove all VFs before doing reinit");
  416. return -EOPNOTSUPP;
  417. }
  418. ice_devlink_reinit_down(pf);
  419. return 0;
  420. case DEVLINK_RELOAD_ACTION_FW_ACTIVATE:
  421. return ice_devlink_reload_empr_start(pf, extack);
  422. default:
  423. WARN_ON(1);
  424. return -EOPNOTSUPP;
  425. }
  426. }
  427. /**
  428. * ice_devlink_reload_empr_finish - Wait for EMP reset to finish
  429. * @pf: pointer to the pf instance
  430. * @extack: netlink extended ACK structure
  431. *
  432. * Wait for driver to finish rebuilding after EMP reset is completed. This
  433. * includes time to wait for both the actual device reset as well as the time
  434. * for the driver's rebuild to complete.
  435. */
  436. static int
  437. ice_devlink_reload_empr_finish(struct ice_pf *pf,
  438. struct netlink_ext_ack *extack)
  439. {
  440. int err;
  441. err = ice_wait_for_reset(pf, 60 * HZ);
  442. if (err) {
  443. NL_SET_ERR_MSG_MOD(extack, "Device still resetting after 1 minute");
  444. return err;
  445. }
  446. return 0;
  447. }
  448. /**
  449. * ice_get_tx_topo_user_sel - Read user's choice from flash
  450. * @pf: pointer to pf structure
  451. * @layers: value read from flash will be saved here
  452. *
  453. * Reads user's preference for Tx Scheduler Topology Tree from PFA TLV.
  454. *
  455. * Return: zero when read was successful, negative values otherwise.
  456. */
  457. static int ice_get_tx_topo_user_sel(struct ice_pf *pf, uint8_t *layers)
  458. {
  459. struct ice_aqc_nvm_tx_topo_user_sel usr_sel = {};
  460. struct ice_hw *hw = &pf->hw;
  461. int err;
  462. err = ice_acquire_nvm(hw, ICE_RES_READ);
  463. if (err)
  464. return err;
  465. err = ice_aq_read_nvm(hw, ICE_AQC_NVM_TX_TOPO_MOD_ID, 0,
  466. sizeof(usr_sel), &usr_sel, true, true, NULL);
  467. if (err)
  468. goto exit_release_res;
  469. if (usr_sel.data & ICE_AQC_NVM_TX_TOPO_USER_SEL)
  470. *layers = ICE_SCHED_5_LAYERS;
  471. else
  472. *layers = ICE_SCHED_9_LAYERS;
  473. exit_release_res:
  474. ice_release_nvm(hw);
  475. return err;
  476. }
  477. /**
  478. * ice_update_tx_topo_user_sel - Save user's preference in flash
  479. * @pf: pointer to pf structure
  480. * @layers: value to be saved in flash
  481. *
  482. * Variable "layers" defines user's preference about number of layers in Tx
  483. * Scheduler Topology Tree. This choice should be stored in PFA TLV field
  484. * and be picked up by driver, next time during init.
  485. *
  486. * Return: zero when save was successful, negative values otherwise.
  487. */
  488. static int ice_update_tx_topo_user_sel(struct ice_pf *pf, int layers)
  489. {
  490. struct ice_aqc_nvm_tx_topo_user_sel usr_sel = {};
  491. struct ice_hw *hw = &pf->hw;
  492. int err;
  493. err = ice_acquire_nvm(hw, ICE_RES_WRITE);
  494. if (err)
  495. return err;
  496. err = ice_aq_read_nvm(hw, ICE_AQC_NVM_TX_TOPO_MOD_ID, 0,
  497. sizeof(usr_sel), &usr_sel, true, true, NULL);
  498. if (err)
  499. goto exit_release_res;
  500. if (layers == ICE_SCHED_5_LAYERS)
  501. usr_sel.data |= ICE_AQC_NVM_TX_TOPO_USER_SEL;
  502. else
  503. usr_sel.data &= ~ICE_AQC_NVM_TX_TOPO_USER_SEL;
  504. err = ice_write_one_nvm_block(pf, ICE_AQC_NVM_TX_TOPO_MOD_ID, 2,
  505. sizeof(usr_sel.data), &usr_sel.data,
  506. true, NULL, NULL);
  507. exit_release_res:
  508. ice_release_nvm(hw);
  509. return err;
  510. }
  511. /**
  512. * ice_devlink_tx_sched_layers_get - Get tx_scheduling_layers parameter
  513. * @devlink: pointer to the devlink instance
  514. * @id: the parameter ID to set
  515. * @ctx: context to store the parameter value
  516. *
  517. * Return: zero on success and negative value on failure.
  518. */
  519. static int ice_devlink_tx_sched_layers_get(struct devlink *devlink, u32 id,
  520. struct devlink_param_gset_ctx *ctx)
  521. {
  522. struct ice_pf *pf = devlink_priv(devlink);
  523. int err;
  524. err = ice_get_tx_topo_user_sel(pf, &ctx->val.vu8);
  525. if (err)
  526. return err;
  527. return 0;
  528. }
  529. /**
  530. * ice_devlink_tx_sched_layers_set - Set tx_scheduling_layers parameter
  531. * @devlink: pointer to the devlink instance
  532. * @id: the parameter ID to set
  533. * @ctx: context to get the parameter value
  534. * @extack: netlink extended ACK structure
  535. *
  536. * Return: zero on success and negative value on failure.
  537. */
  538. static int ice_devlink_tx_sched_layers_set(struct devlink *devlink, u32 id,
  539. struct devlink_param_gset_ctx *ctx,
  540. struct netlink_ext_ack *extack)
  541. {
  542. struct ice_pf *pf = devlink_priv(devlink);
  543. int err;
  544. err = ice_update_tx_topo_user_sel(pf, ctx->val.vu8);
  545. if (err)
  546. return err;
  547. NL_SET_ERR_MSG_MOD(extack,
  548. "Tx scheduling layers have been changed on this device. You must do the PCI slot powercycle for the change to take effect.");
  549. return 0;
  550. }
  551. /**
  552. * ice_devlink_tx_sched_layers_validate - Validate passed tx_scheduling_layers
  553. * parameter value
  554. * @devlink: unused pointer to devlink instance
  555. * @id: the parameter ID to validate
  556. * @val: value to validate
  557. * @extack: netlink extended ACK structure
  558. *
  559. * Supported values are:
  560. * - 5 - five layers Tx Scheduler Topology Tree
  561. * - 9 - nine layers Tx Scheduler Topology Tree
  562. *
  563. * Return: zero when passed parameter value is supported. Negative value on
  564. * error.
  565. */
  566. static int ice_devlink_tx_sched_layers_validate(struct devlink *devlink, u32 id,
  567. union devlink_param_value val,
  568. struct netlink_ext_ack *extack)
  569. {
  570. if (val.vu8 != ICE_SCHED_5_LAYERS && val.vu8 != ICE_SCHED_9_LAYERS) {
  571. NL_SET_ERR_MSG_MOD(extack,
  572. "Wrong number of tx scheduler layers provided.");
  573. return -EINVAL;
  574. }
  575. return 0;
  576. }
  577. /**
  578. * ice_tear_down_devlink_rate_tree - removes devlink-rate exported tree
  579. * @pf: pf struct
  580. *
  581. * This function tears down tree exported during VF's creation.
  582. */
  583. void ice_tear_down_devlink_rate_tree(struct ice_pf *pf)
  584. {
  585. struct devlink *devlink;
  586. struct ice_vf *vf;
  587. unsigned int bkt;
  588. devlink = priv_to_devlink(pf);
  589. devl_lock(devlink);
  590. mutex_lock(&pf->vfs.table_lock);
  591. ice_for_each_vf(pf, bkt, vf) {
  592. if (vf->devlink_port.devlink_rate)
  593. devl_rate_leaf_destroy(&vf->devlink_port);
  594. }
  595. mutex_unlock(&pf->vfs.table_lock);
  596. devl_rate_nodes_destroy(devlink);
  597. devl_unlock(devlink);
  598. }
  599. /**
  600. * ice_enable_custom_tx - try to enable custom Tx feature
  601. * @pf: pf struct
  602. *
  603. * This function tries to enable custom Tx feature,
  604. * it's not possible to enable it, if DCB or ADQ is active.
  605. */
  606. static bool ice_enable_custom_tx(struct ice_pf *pf)
  607. {
  608. struct ice_port_info *pi = ice_get_main_vsi(pf)->port_info;
  609. struct device *dev = ice_pf_to_dev(pf);
  610. if (pi->is_custom_tx_enabled)
  611. /* already enabled, return true */
  612. return true;
  613. if (ice_is_adq_active(pf)) {
  614. dev_err(dev, "ADQ active, can't modify Tx scheduler tree\n");
  615. return false;
  616. }
  617. if (ice_is_dcb_active(pf)) {
  618. dev_err(dev, "DCB active, can't modify Tx scheduler tree\n");
  619. return false;
  620. }
  621. pi->is_custom_tx_enabled = true;
  622. return true;
  623. }
  624. /**
  625. * ice_traverse_tx_tree - traverse Tx scheduler tree
  626. * @devlink: devlink struct
  627. * @node: current node, used for recursion
  628. * @tc_node: tc_node struct, that is treated as a root
  629. * @pf: pf struct
  630. *
  631. * This function traverses Tx scheduler tree and exports
  632. * entire structure to the devlink-rate.
  633. */
  634. static void ice_traverse_tx_tree(struct devlink *devlink, struct ice_sched_node *node,
  635. struct ice_sched_node *tc_node, struct ice_pf *pf)
  636. {
  637. struct devlink_rate *rate_node = NULL;
  638. struct ice_dynamic_port *sf;
  639. struct ice_vf *vf;
  640. int i;
  641. if (node->rate_node)
  642. /* already added, skip to the next */
  643. goto traverse_children;
  644. if (node->parent == tc_node) {
  645. /* create root node */
  646. rate_node = devl_rate_node_create(devlink, node, node->name, NULL);
  647. } else if (node->vsi_handle &&
  648. pf->vsi[node->vsi_handle]->type == ICE_VSI_VF &&
  649. pf->vsi[node->vsi_handle]->vf) {
  650. vf = pf->vsi[node->vsi_handle]->vf;
  651. if (!vf->devlink_port.devlink_rate)
  652. /* leaf nodes doesn't have children
  653. * so we don't set rate_node
  654. */
  655. devl_rate_leaf_create(&vf->devlink_port, node,
  656. node->parent->rate_node);
  657. } else if (node->vsi_handle &&
  658. pf->vsi[node->vsi_handle]->type == ICE_VSI_SF &&
  659. pf->vsi[node->vsi_handle]->sf) {
  660. sf = pf->vsi[node->vsi_handle]->sf;
  661. if (!sf->devlink_port.devlink_rate)
  662. /* leaf nodes doesn't have children
  663. * so we don't set rate_node
  664. */
  665. devl_rate_leaf_create(&sf->devlink_port, node,
  666. node->parent->rate_node);
  667. } else if (node->info.data.elem_type != ICE_AQC_ELEM_TYPE_LEAF &&
  668. node->parent->rate_node) {
  669. rate_node = devl_rate_node_create(devlink, node, node->name,
  670. node->parent->rate_node);
  671. }
  672. if (rate_node && !IS_ERR(rate_node))
  673. node->rate_node = rate_node;
  674. traverse_children:
  675. for (i = 0; i < node->num_children; i++)
  676. ice_traverse_tx_tree(devlink, node->children[i], tc_node, pf);
  677. }
  678. /**
  679. * ice_devlink_rate_init_tx_topology - export Tx scheduler tree to devlink rate
  680. * @devlink: devlink struct
  681. * @vsi: main vsi struct
  682. *
  683. * This function finds a root node, then calls ice_traverse_tx tree, which
  684. * traverses the tree and exports it's contents to devlink rate.
  685. */
  686. int ice_devlink_rate_init_tx_topology(struct devlink *devlink, struct ice_vsi *vsi)
  687. {
  688. struct ice_port_info *pi = vsi->port_info;
  689. struct ice_sched_node *tc_node;
  690. struct ice_pf *pf = vsi->back;
  691. int i;
  692. tc_node = pi->root->children[0];
  693. mutex_lock(&pi->sched_lock);
  694. for (i = 0; i < tc_node->num_children; i++)
  695. ice_traverse_tx_tree(devlink, tc_node->children[i], tc_node, pf);
  696. mutex_unlock(&pi->sched_lock);
  697. return 0;
  698. }
  699. static void ice_clear_rate_nodes(struct ice_sched_node *node)
  700. {
  701. node->rate_node = NULL;
  702. for (int i = 0; i < node->num_children; i++)
  703. ice_clear_rate_nodes(node->children[i]);
  704. }
  705. /**
  706. * ice_devlink_rate_clear_tx_topology - clear node->rate_node
  707. * @vsi: main vsi struct
  708. *
  709. * Clear rate_node to cleanup creation of Tx topology.
  710. *
  711. */
  712. void ice_devlink_rate_clear_tx_topology(struct ice_vsi *vsi)
  713. {
  714. struct ice_port_info *pi = vsi->port_info;
  715. mutex_lock(&pi->sched_lock);
  716. ice_clear_rate_nodes(pi->root->children[0]);
  717. mutex_unlock(&pi->sched_lock);
  718. }
  719. /**
  720. * ice_set_object_tx_share - sets node scheduling parameter
  721. * @pi: devlink struct instance
  722. * @node: node struct instance
  723. * @bw: bandwidth in bytes per second
  724. * @extack: extended netdev ack structure
  725. *
  726. * This function sets ICE_MIN_BW scheduling BW limit.
  727. */
  728. static int ice_set_object_tx_share(struct ice_port_info *pi, struct ice_sched_node *node,
  729. u64 bw, struct netlink_ext_ack *extack)
  730. {
  731. int status;
  732. mutex_lock(&pi->sched_lock);
  733. /* converts bytes per second to kilo bits per second */
  734. node->tx_share = div_u64(bw, 125);
  735. status = ice_sched_set_node_bw_lmt(pi, node, ICE_MIN_BW, node->tx_share);
  736. mutex_unlock(&pi->sched_lock);
  737. if (status)
  738. NL_SET_ERR_MSG_MOD(extack, "Can't set scheduling node tx_share");
  739. return status;
  740. }
  741. /**
  742. * ice_set_object_tx_max - sets node scheduling parameter
  743. * @pi: devlink struct instance
  744. * @node: node struct instance
  745. * @bw: bandwidth in bytes per second
  746. * @extack: extended netdev ack structure
  747. *
  748. * This function sets ICE_MAX_BW scheduling BW limit.
  749. */
  750. static int ice_set_object_tx_max(struct ice_port_info *pi, struct ice_sched_node *node,
  751. u64 bw, struct netlink_ext_ack *extack)
  752. {
  753. int status;
  754. mutex_lock(&pi->sched_lock);
  755. /* converts bytes per second value to kilo bits per second */
  756. node->tx_max = div_u64(bw, 125);
  757. status = ice_sched_set_node_bw_lmt(pi, node, ICE_MAX_BW, node->tx_max);
  758. mutex_unlock(&pi->sched_lock);
  759. if (status)
  760. NL_SET_ERR_MSG_MOD(extack, "Can't set scheduling node tx_max");
  761. return status;
  762. }
  763. /**
  764. * ice_set_object_tx_priority - sets node scheduling parameter
  765. * @pi: devlink struct instance
  766. * @node: node struct instance
  767. * @priority: value representing priority for strict priority arbitration
  768. * @extack: extended netdev ack structure
  769. *
  770. * This function sets priority of node among siblings.
  771. */
  772. static int ice_set_object_tx_priority(struct ice_port_info *pi, struct ice_sched_node *node,
  773. u32 priority, struct netlink_ext_ack *extack)
  774. {
  775. int status;
  776. if (priority >= 8) {
  777. NL_SET_ERR_MSG_MOD(extack, "Priority should be less than 8");
  778. return -EINVAL;
  779. }
  780. mutex_lock(&pi->sched_lock);
  781. node->tx_priority = priority;
  782. status = ice_sched_set_node_priority(pi, node, node->tx_priority);
  783. mutex_unlock(&pi->sched_lock);
  784. if (status)
  785. NL_SET_ERR_MSG_MOD(extack, "Can't set scheduling node tx_priority");
  786. return status;
  787. }
  788. /**
  789. * ice_set_object_tx_weight - sets node scheduling parameter
  790. * @pi: devlink struct instance
  791. * @node: node struct instance
  792. * @weight: value represeting relative weight for WFQ arbitration
  793. * @extack: extended netdev ack structure
  794. *
  795. * This function sets node weight for WFQ algorithm.
  796. */
  797. static int ice_set_object_tx_weight(struct ice_port_info *pi, struct ice_sched_node *node,
  798. u32 weight, struct netlink_ext_ack *extack)
  799. {
  800. int status;
  801. if (weight > 200 || weight < 1) {
  802. NL_SET_ERR_MSG_MOD(extack, "Weight must be between 1 and 200");
  803. return -EINVAL;
  804. }
  805. mutex_lock(&pi->sched_lock);
  806. node->tx_weight = weight;
  807. status = ice_sched_set_node_weight(pi, node, node->tx_weight);
  808. mutex_unlock(&pi->sched_lock);
  809. if (status)
  810. NL_SET_ERR_MSG_MOD(extack, "Can't set scheduling node tx_weight");
  811. return status;
  812. }
  813. /**
  814. * ice_get_pi_from_dev_rate - get port info from devlink_rate
  815. * @rate_node: devlink struct instance
  816. *
  817. * This function returns corresponding port_info struct of devlink_rate
  818. */
  819. static struct ice_port_info *ice_get_pi_from_dev_rate(struct devlink_rate *rate_node)
  820. {
  821. struct ice_pf *pf = devlink_priv(rate_node->devlink);
  822. return ice_get_main_vsi(pf)->port_info;
  823. }
  824. static int ice_devlink_rate_node_new(struct devlink_rate *rate_node, void **priv,
  825. struct netlink_ext_ack *extack)
  826. {
  827. struct ice_sched_node *node;
  828. struct ice_port_info *pi;
  829. pi = ice_get_pi_from_dev_rate(rate_node);
  830. if (!ice_enable_custom_tx(devlink_priv(rate_node->devlink)))
  831. return -EBUSY;
  832. /* preallocate memory for ice_sched_node */
  833. node = devm_kzalloc(ice_hw_to_dev(pi->hw), sizeof(*node), GFP_KERNEL);
  834. if (!node)
  835. return -ENOMEM;
  836. *priv = node;
  837. return 0;
  838. }
  839. static int ice_devlink_rate_node_del(struct devlink_rate *rate_node, void *priv,
  840. struct netlink_ext_ack *extack)
  841. {
  842. struct ice_sched_node *node, *tc_node;
  843. struct ice_port_info *pi;
  844. pi = ice_get_pi_from_dev_rate(rate_node);
  845. tc_node = pi->root->children[0];
  846. node = priv;
  847. if (!rate_node->parent || !node || tc_node == node || !extack)
  848. return 0;
  849. if (!ice_enable_custom_tx(devlink_priv(rate_node->devlink)))
  850. return -EBUSY;
  851. /* can't allow to delete a node with children */
  852. if (node->num_children)
  853. return -EINVAL;
  854. mutex_lock(&pi->sched_lock);
  855. ice_free_sched_node(pi, node);
  856. mutex_unlock(&pi->sched_lock);
  857. return 0;
  858. }
  859. static int ice_devlink_rate_leaf_tx_max_set(struct devlink_rate *rate_leaf, void *priv,
  860. u64 tx_max, struct netlink_ext_ack *extack)
  861. {
  862. struct ice_sched_node *node = priv;
  863. if (!ice_enable_custom_tx(devlink_priv(rate_leaf->devlink)))
  864. return -EBUSY;
  865. if (!node)
  866. return 0;
  867. return ice_set_object_tx_max(ice_get_pi_from_dev_rate(rate_leaf),
  868. node, tx_max, extack);
  869. }
  870. static int ice_devlink_rate_leaf_tx_share_set(struct devlink_rate *rate_leaf, void *priv,
  871. u64 tx_share, struct netlink_ext_ack *extack)
  872. {
  873. struct ice_sched_node *node = priv;
  874. if (!ice_enable_custom_tx(devlink_priv(rate_leaf->devlink)))
  875. return -EBUSY;
  876. if (!node)
  877. return 0;
  878. return ice_set_object_tx_share(ice_get_pi_from_dev_rate(rate_leaf), node,
  879. tx_share, extack);
  880. }
  881. static int ice_devlink_rate_leaf_tx_priority_set(struct devlink_rate *rate_leaf, void *priv,
  882. u32 tx_priority, struct netlink_ext_ack *extack)
  883. {
  884. struct ice_sched_node *node = priv;
  885. if (!ice_enable_custom_tx(devlink_priv(rate_leaf->devlink)))
  886. return -EBUSY;
  887. if (!node)
  888. return 0;
  889. return ice_set_object_tx_priority(ice_get_pi_from_dev_rate(rate_leaf), node,
  890. tx_priority, extack);
  891. }
  892. static int ice_devlink_rate_leaf_tx_weight_set(struct devlink_rate *rate_leaf, void *priv,
  893. u32 tx_weight, struct netlink_ext_ack *extack)
  894. {
  895. struct ice_sched_node *node = priv;
  896. if (!ice_enable_custom_tx(devlink_priv(rate_leaf->devlink)))
  897. return -EBUSY;
  898. if (!node)
  899. return 0;
  900. return ice_set_object_tx_weight(ice_get_pi_from_dev_rate(rate_leaf), node,
  901. tx_weight, extack);
  902. }
  903. static int ice_devlink_rate_node_tx_max_set(struct devlink_rate *rate_node, void *priv,
  904. u64 tx_max, struct netlink_ext_ack *extack)
  905. {
  906. struct ice_sched_node *node = priv;
  907. if (!ice_enable_custom_tx(devlink_priv(rate_node->devlink)))
  908. return -EBUSY;
  909. if (!node)
  910. return 0;
  911. return ice_set_object_tx_max(ice_get_pi_from_dev_rate(rate_node),
  912. node, tx_max, extack);
  913. }
  914. static int ice_devlink_rate_node_tx_share_set(struct devlink_rate *rate_node, void *priv,
  915. u64 tx_share, struct netlink_ext_ack *extack)
  916. {
  917. struct ice_sched_node *node = priv;
  918. if (!ice_enable_custom_tx(devlink_priv(rate_node->devlink)))
  919. return -EBUSY;
  920. if (!node)
  921. return 0;
  922. return ice_set_object_tx_share(ice_get_pi_from_dev_rate(rate_node),
  923. node, tx_share, extack);
  924. }
  925. static int ice_devlink_rate_node_tx_priority_set(struct devlink_rate *rate_node, void *priv,
  926. u32 tx_priority, struct netlink_ext_ack *extack)
  927. {
  928. struct ice_sched_node *node = priv;
  929. if (!ice_enable_custom_tx(devlink_priv(rate_node->devlink)))
  930. return -EBUSY;
  931. if (!node)
  932. return 0;
  933. return ice_set_object_tx_priority(ice_get_pi_from_dev_rate(rate_node),
  934. node, tx_priority, extack);
  935. }
  936. static int ice_devlink_rate_node_tx_weight_set(struct devlink_rate *rate_node, void *priv,
  937. u32 tx_weight, struct netlink_ext_ack *extack)
  938. {
  939. struct ice_sched_node *node = priv;
  940. if (!ice_enable_custom_tx(devlink_priv(rate_node->devlink)))
  941. return -EBUSY;
  942. if (!node)
  943. return 0;
  944. return ice_set_object_tx_weight(ice_get_pi_from_dev_rate(rate_node),
  945. node, tx_weight, extack);
  946. }
  947. static int ice_devlink_set_parent(struct devlink_rate *devlink_rate,
  948. struct devlink_rate *parent,
  949. void *priv, void *parent_priv,
  950. struct netlink_ext_ack *extack)
  951. {
  952. struct ice_port_info *pi = ice_get_pi_from_dev_rate(devlink_rate);
  953. struct ice_sched_node *tc_node, *node, *parent_node;
  954. u16 num_nodes_added;
  955. u32 first_node_teid;
  956. u32 node_teid;
  957. int status;
  958. tc_node = pi->root->children[0];
  959. node = priv;
  960. if (!extack)
  961. return 0;
  962. if (!ice_enable_custom_tx(devlink_priv(devlink_rate->devlink)))
  963. return -EBUSY;
  964. if (!parent) {
  965. if (!node || tc_node == node || node->num_children)
  966. return -EINVAL;
  967. mutex_lock(&pi->sched_lock);
  968. ice_free_sched_node(pi, node);
  969. mutex_unlock(&pi->sched_lock);
  970. return 0;
  971. }
  972. parent_node = parent_priv;
  973. /* if the node doesn't exist, create it */
  974. if (!node->parent) {
  975. mutex_lock(&pi->sched_lock);
  976. status = ice_sched_add_elems(pi, tc_node, parent_node,
  977. parent_node->tx_sched_layer + 1,
  978. 1, &num_nodes_added, &first_node_teid,
  979. &node);
  980. mutex_unlock(&pi->sched_lock);
  981. if (status) {
  982. NL_SET_ERR_MSG_MOD(extack, "Can't add a new node");
  983. return status;
  984. }
  985. if (devlink_rate->tx_share)
  986. ice_set_object_tx_share(pi, node, devlink_rate->tx_share, extack);
  987. if (devlink_rate->tx_max)
  988. ice_set_object_tx_max(pi, node, devlink_rate->tx_max, extack);
  989. if (devlink_rate->tx_priority)
  990. ice_set_object_tx_priority(pi, node, devlink_rate->tx_priority, extack);
  991. if (devlink_rate->tx_weight)
  992. ice_set_object_tx_weight(pi, node, devlink_rate->tx_weight, extack);
  993. } else {
  994. node_teid = le32_to_cpu(node->info.node_teid);
  995. mutex_lock(&pi->sched_lock);
  996. status = ice_sched_move_nodes(pi, parent_node, 1, &node_teid);
  997. mutex_unlock(&pi->sched_lock);
  998. if (status)
  999. NL_SET_ERR_MSG_MOD(extack, "Can't move existing node to a new parent");
  1000. }
  1001. return status;
  1002. }
  1003. /**
  1004. * ice_devlink_reinit_up - do reinit of the given PF
  1005. * @pf: pointer to the PF struct
  1006. */
  1007. static int ice_devlink_reinit_up(struct ice_pf *pf)
  1008. {
  1009. struct ice_vsi *vsi = ice_get_main_vsi(pf);
  1010. int err;
  1011. err = ice_init_dev(pf);
  1012. if (err)
  1013. return err;
  1014. vsi->flags = ICE_VSI_FLAG_INIT;
  1015. rtnl_lock();
  1016. err = ice_vsi_cfg(vsi);
  1017. rtnl_unlock();
  1018. if (err)
  1019. goto err_vsi_cfg;
  1020. /* No need to take devl_lock, it's already taken by devlink API */
  1021. err = ice_load(pf);
  1022. if (err)
  1023. goto err_load;
  1024. return 0;
  1025. err_load:
  1026. rtnl_lock();
  1027. ice_vsi_decfg(vsi);
  1028. rtnl_unlock();
  1029. err_vsi_cfg:
  1030. ice_deinit_dev(pf);
  1031. return err;
  1032. }
  1033. /**
  1034. * ice_devlink_reload_up - do reload up after reinit
  1035. * @devlink: pointer to the devlink instance reloading
  1036. * @action: the action requested
  1037. * @limit: limits imposed by userspace, such as not resetting
  1038. * @actions_performed: on return, indicate what actions actually performed
  1039. * @extack: netlink extended ACK structure
  1040. */
  1041. static int
  1042. ice_devlink_reload_up(struct devlink *devlink,
  1043. enum devlink_reload_action action,
  1044. enum devlink_reload_limit limit,
  1045. u32 *actions_performed,
  1046. struct netlink_ext_ack *extack)
  1047. {
  1048. struct ice_pf *pf = devlink_priv(devlink);
  1049. switch (action) {
  1050. case DEVLINK_RELOAD_ACTION_DRIVER_REINIT:
  1051. *actions_performed = BIT(DEVLINK_RELOAD_ACTION_DRIVER_REINIT);
  1052. return ice_devlink_reinit_up(pf);
  1053. case DEVLINK_RELOAD_ACTION_FW_ACTIVATE:
  1054. *actions_performed = BIT(DEVLINK_RELOAD_ACTION_FW_ACTIVATE);
  1055. return ice_devlink_reload_empr_finish(pf, extack);
  1056. default:
  1057. WARN_ON(1);
  1058. return -EOPNOTSUPP;
  1059. }
  1060. }
  1061. static const struct devlink_ops ice_devlink_ops = {
  1062. .supported_flash_update_params = DEVLINK_SUPPORT_FLASH_UPDATE_OVERWRITE_MASK,
  1063. .reload_actions = BIT(DEVLINK_RELOAD_ACTION_DRIVER_REINIT) |
  1064. BIT(DEVLINK_RELOAD_ACTION_FW_ACTIVATE),
  1065. .reload_down = ice_devlink_reload_down,
  1066. .reload_up = ice_devlink_reload_up,
  1067. .eswitch_mode_get = ice_eswitch_mode_get,
  1068. .eswitch_mode_set = ice_eswitch_mode_set,
  1069. .info_get = ice_devlink_info_get,
  1070. .flash_update = ice_devlink_flash_update,
  1071. .rate_node_new = ice_devlink_rate_node_new,
  1072. .rate_node_del = ice_devlink_rate_node_del,
  1073. .rate_leaf_tx_max_set = ice_devlink_rate_leaf_tx_max_set,
  1074. .rate_leaf_tx_share_set = ice_devlink_rate_leaf_tx_share_set,
  1075. .rate_leaf_tx_priority_set = ice_devlink_rate_leaf_tx_priority_set,
  1076. .rate_leaf_tx_weight_set = ice_devlink_rate_leaf_tx_weight_set,
  1077. .rate_node_tx_max_set = ice_devlink_rate_node_tx_max_set,
  1078. .rate_node_tx_share_set = ice_devlink_rate_node_tx_share_set,
  1079. .rate_node_tx_priority_set = ice_devlink_rate_node_tx_priority_set,
  1080. .rate_node_tx_weight_set = ice_devlink_rate_node_tx_weight_set,
  1081. .rate_leaf_parent_set = ice_devlink_set_parent,
  1082. .rate_node_parent_set = ice_devlink_set_parent,
  1083. .port_new = ice_devlink_port_new,
  1084. };
  1085. static const struct devlink_ops ice_sf_devlink_ops;
  1086. static int
  1087. ice_devlink_enable_roce_get(struct devlink *devlink, u32 id,
  1088. struct devlink_param_gset_ctx *ctx)
  1089. {
  1090. struct ice_pf *pf = devlink_priv(devlink);
  1091. ctx->val.vbool = pf->rdma_mode & IIDC_RDMA_PROTOCOL_ROCEV2 ? true : false;
  1092. return 0;
  1093. }
  1094. static int ice_devlink_enable_roce_set(struct devlink *devlink, u32 id,
  1095. struct devlink_param_gset_ctx *ctx,
  1096. struct netlink_ext_ack *extack)
  1097. {
  1098. struct ice_pf *pf = devlink_priv(devlink);
  1099. bool roce_ena = ctx->val.vbool;
  1100. int ret;
  1101. if (!roce_ena) {
  1102. ice_unplug_aux_dev(pf);
  1103. pf->rdma_mode &= ~IIDC_RDMA_PROTOCOL_ROCEV2;
  1104. return 0;
  1105. }
  1106. pf->rdma_mode |= IIDC_RDMA_PROTOCOL_ROCEV2;
  1107. ret = ice_plug_aux_dev(pf);
  1108. if (ret)
  1109. pf->rdma_mode &= ~IIDC_RDMA_PROTOCOL_ROCEV2;
  1110. return ret;
  1111. }
  1112. static int
  1113. ice_devlink_enable_roce_validate(struct devlink *devlink, u32 id,
  1114. union devlink_param_value val,
  1115. struct netlink_ext_ack *extack)
  1116. {
  1117. struct ice_pf *pf = devlink_priv(devlink);
  1118. if (!test_bit(ICE_FLAG_RDMA_ENA, pf->flags))
  1119. return -EOPNOTSUPP;
  1120. if (pf->rdma_mode & IIDC_RDMA_PROTOCOL_IWARP) {
  1121. NL_SET_ERR_MSG_MOD(extack, "iWARP is currently enabled. This device cannot enable iWARP and RoCEv2 simultaneously");
  1122. return -EOPNOTSUPP;
  1123. }
  1124. return 0;
  1125. }
  1126. static int
  1127. ice_devlink_enable_iw_get(struct devlink *devlink, u32 id,
  1128. struct devlink_param_gset_ctx *ctx)
  1129. {
  1130. struct ice_pf *pf = devlink_priv(devlink);
  1131. ctx->val.vbool = pf->rdma_mode & IIDC_RDMA_PROTOCOL_IWARP;
  1132. return 0;
  1133. }
  1134. static int ice_devlink_enable_iw_set(struct devlink *devlink, u32 id,
  1135. struct devlink_param_gset_ctx *ctx,
  1136. struct netlink_ext_ack *extack)
  1137. {
  1138. struct ice_pf *pf = devlink_priv(devlink);
  1139. bool iw_ena = ctx->val.vbool;
  1140. int ret;
  1141. if (!iw_ena) {
  1142. ice_unplug_aux_dev(pf);
  1143. pf->rdma_mode &= ~IIDC_RDMA_PROTOCOL_IWARP;
  1144. return 0;
  1145. }
  1146. pf->rdma_mode |= IIDC_RDMA_PROTOCOL_IWARP;
  1147. ret = ice_plug_aux_dev(pf);
  1148. if (ret)
  1149. pf->rdma_mode &= ~IIDC_RDMA_PROTOCOL_IWARP;
  1150. return ret;
  1151. }
  1152. static int
  1153. ice_devlink_enable_iw_validate(struct devlink *devlink, u32 id,
  1154. union devlink_param_value val,
  1155. struct netlink_ext_ack *extack)
  1156. {
  1157. struct ice_pf *pf = devlink_priv(devlink);
  1158. if (!test_bit(ICE_FLAG_RDMA_ENA, pf->flags))
  1159. return -EOPNOTSUPP;
  1160. if (pf->rdma_mode & IIDC_RDMA_PROTOCOL_ROCEV2) {
  1161. NL_SET_ERR_MSG_MOD(extack, "RoCEv2 is currently enabled. This device cannot enable iWARP and RoCEv2 simultaneously");
  1162. return -EOPNOTSUPP;
  1163. }
  1164. return 0;
  1165. }
  1166. #define DEVLINK_LOCAL_FWD_DISABLED_STR "disabled"
  1167. #define DEVLINK_LOCAL_FWD_ENABLED_STR "enabled"
  1168. #define DEVLINK_LOCAL_FWD_PRIORITIZED_STR "prioritized"
  1169. /**
  1170. * ice_devlink_local_fwd_mode_to_str - Get string for local_fwd mode.
  1171. * @mode: local forwarding for mode used in port_info struct.
  1172. *
  1173. * Return: Mode respective string or "Invalid".
  1174. */
  1175. static const char *
  1176. ice_devlink_local_fwd_mode_to_str(enum ice_local_fwd_mode mode)
  1177. {
  1178. switch (mode) {
  1179. case ICE_LOCAL_FWD_MODE_ENABLED:
  1180. return DEVLINK_LOCAL_FWD_ENABLED_STR;
  1181. case ICE_LOCAL_FWD_MODE_PRIORITIZED:
  1182. return DEVLINK_LOCAL_FWD_PRIORITIZED_STR;
  1183. case ICE_LOCAL_FWD_MODE_DISABLED:
  1184. return DEVLINK_LOCAL_FWD_DISABLED_STR;
  1185. }
  1186. return "Invalid";
  1187. }
  1188. /**
  1189. * ice_devlink_local_fwd_str_to_mode - Get local_fwd mode from string name.
  1190. * @mode_str: local forwarding mode string.
  1191. *
  1192. * Return: Mode value or negative number if invalid.
  1193. */
  1194. static int ice_devlink_local_fwd_str_to_mode(const char *mode_str)
  1195. {
  1196. if (!strcmp(mode_str, DEVLINK_LOCAL_FWD_ENABLED_STR))
  1197. return ICE_LOCAL_FWD_MODE_ENABLED;
  1198. else if (!strcmp(mode_str, DEVLINK_LOCAL_FWD_PRIORITIZED_STR))
  1199. return ICE_LOCAL_FWD_MODE_PRIORITIZED;
  1200. else if (!strcmp(mode_str, DEVLINK_LOCAL_FWD_DISABLED_STR))
  1201. return ICE_LOCAL_FWD_MODE_DISABLED;
  1202. return -EINVAL;
  1203. }
  1204. /**
  1205. * ice_devlink_local_fwd_get - Get local_fwd parameter.
  1206. * @devlink: Pointer to the devlink instance.
  1207. * @id: The parameter ID to set.
  1208. * @ctx: Context to store the parameter value.
  1209. *
  1210. * Return: Zero.
  1211. */
  1212. static int ice_devlink_local_fwd_get(struct devlink *devlink, u32 id,
  1213. struct devlink_param_gset_ctx *ctx)
  1214. {
  1215. struct ice_pf *pf = devlink_priv(devlink);
  1216. struct ice_port_info *pi;
  1217. const char *mode_str;
  1218. pi = pf->hw.port_info;
  1219. mode_str = ice_devlink_local_fwd_mode_to_str(pi->local_fwd_mode);
  1220. snprintf(ctx->val.vstr, sizeof(ctx->val.vstr), "%s", mode_str);
  1221. return 0;
  1222. }
  1223. /**
  1224. * ice_devlink_local_fwd_set - Set local_fwd parameter.
  1225. * @devlink: Pointer to the devlink instance.
  1226. * @id: The parameter ID to set.
  1227. * @ctx: Context to get the parameter value.
  1228. * @extack: Netlink extended ACK structure.
  1229. *
  1230. * Return: Zero.
  1231. */
  1232. static int ice_devlink_local_fwd_set(struct devlink *devlink, u32 id,
  1233. struct devlink_param_gset_ctx *ctx,
  1234. struct netlink_ext_ack *extack)
  1235. {
  1236. int new_local_fwd_mode = ice_devlink_local_fwd_str_to_mode(ctx->val.vstr);
  1237. struct ice_pf *pf = devlink_priv(devlink);
  1238. struct device *dev = ice_pf_to_dev(pf);
  1239. struct ice_port_info *pi;
  1240. pi = pf->hw.port_info;
  1241. if (pi->local_fwd_mode != new_local_fwd_mode) {
  1242. pi->local_fwd_mode = new_local_fwd_mode;
  1243. dev_info(dev, "Setting local_fwd to %s\n", ctx->val.vstr);
  1244. ice_schedule_reset(pf, ICE_RESET_CORER);
  1245. }
  1246. return 0;
  1247. }
  1248. /**
  1249. * ice_devlink_local_fwd_validate - Validate passed local_fwd parameter value.
  1250. * @devlink: Unused pointer to devlink instance.
  1251. * @id: The parameter ID to validate.
  1252. * @val: Value to validate.
  1253. * @extack: Netlink extended ACK structure.
  1254. *
  1255. * Supported values are:
  1256. * "enabled" - local_fwd is enabled, "disabled" - local_fwd is disabled
  1257. * "prioritized" - local_fwd traffic is prioritized in scheduling.
  1258. *
  1259. * Return: Zero when passed parameter value is supported. Negative value on
  1260. * error.
  1261. */
  1262. static int ice_devlink_local_fwd_validate(struct devlink *devlink, u32 id,
  1263. union devlink_param_value val,
  1264. struct netlink_ext_ack *extack)
  1265. {
  1266. if (ice_devlink_local_fwd_str_to_mode(val.vstr) < 0) {
  1267. NL_SET_ERR_MSG_MOD(extack, "Error: Requested value is not supported.");
  1268. return -EINVAL;
  1269. }
  1270. return 0;
  1271. }
  1272. enum ice_param_id {
  1273. ICE_DEVLINK_PARAM_ID_BASE = DEVLINK_PARAM_GENERIC_ID_MAX,
  1274. ICE_DEVLINK_PARAM_ID_TX_SCHED_LAYERS,
  1275. ICE_DEVLINK_PARAM_ID_LOCAL_FWD,
  1276. };
  1277. static const struct devlink_param ice_dvl_rdma_params[] = {
  1278. DEVLINK_PARAM_GENERIC(ENABLE_ROCE, BIT(DEVLINK_PARAM_CMODE_RUNTIME),
  1279. ice_devlink_enable_roce_get,
  1280. ice_devlink_enable_roce_set,
  1281. ice_devlink_enable_roce_validate),
  1282. DEVLINK_PARAM_GENERIC(ENABLE_IWARP, BIT(DEVLINK_PARAM_CMODE_RUNTIME),
  1283. ice_devlink_enable_iw_get,
  1284. ice_devlink_enable_iw_set,
  1285. ice_devlink_enable_iw_validate),
  1286. };
  1287. static const struct devlink_param ice_dvl_sched_params[] = {
  1288. DEVLINK_PARAM_DRIVER(ICE_DEVLINK_PARAM_ID_TX_SCHED_LAYERS,
  1289. "tx_scheduling_layers",
  1290. DEVLINK_PARAM_TYPE_U8,
  1291. BIT(DEVLINK_PARAM_CMODE_PERMANENT),
  1292. ice_devlink_tx_sched_layers_get,
  1293. ice_devlink_tx_sched_layers_set,
  1294. ice_devlink_tx_sched_layers_validate),
  1295. DEVLINK_PARAM_DRIVER(ICE_DEVLINK_PARAM_ID_LOCAL_FWD,
  1296. "local_forwarding", DEVLINK_PARAM_TYPE_STRING,
  1297. BIT(DEVLINK_PARAM_CMODE_RUNTIME),
  1298. ice_devlink_local_fwd_get,
  1299. ice_devlink_local_fwd_set,
  1300. ice_devlink_local_fwd_validate),
  1301. };
  1302. static void ice_devlink_free(void *devlink_ptr)
  1303. {
  1304. devlink_free((struct devlink *)devlink_ptr);
  1305. }
  1306. /**
  1307. * ice_allocate_pf - Allocate devlink and return PF structure pointer
  1308. * @dev: the device to allocate for
  1309. *
  1310. * Allocate a devlink instance for this device and return the private area as
  1311. * the PF structure. The devlink memory is kept track of through devres by
  1312. * adding an action to remove it when unwinding.
  1313. */
  1314. struct ice_pf *ice_allocate_pf(struct device *dev)
  1315. {
  1316. struct devlink *devlink;
  1317. devlink = devlink_alloc(&ice_devlink_ops, sizeof(struct ice_pf), dev);
  1318. if (!devlink)
  1319. return NULL;
  1320. /* Add an action to teardown the devlink when unwinding the driver */
  1321. if (devm_add_action_or_reset(dev, ice_devlink_free, devlink))
  1322. return NULL;
  1323. return devlink_priv(devlink);
  1324. }
  1325. /**
  1326. * ice_allocate_sf - Allocate devlink and return SF structure pointer
  1327. * @dev: the device to allocate for
  1328. * @pf: pointer to the PF structure
  1329. *
  1330. * Allocate a devlink instance for SF.
  1331. *
  1332. * Return: ice_sf_priv pointer to allocated memory or ERR_PTR in case of error
  1333. */
  1334. struct ice_sf_priv *ice_allocate_sf(struct device *dev, struct ice_pf *pf)
  1335. {
  1336. struct devlink *devlink;
  1337. int err;
  1338. devlink = devlink_alloc(&ice_sf_devlink_ops, sizeof(struct ice_sf_priv),
  1339. dev);
  1340. if (!devlink)
  1341. return ERR_PTR(-ENOMEM);
  1342. err = devl_nested_devlink_set(priv_to_devlink(pf), devlink);
  1343. if (err) {
  1344. devlink_free(devlink);
  1345. return ERR_PTR(err);
  1346. }
  1347. return devlink_priv(devlink);
  1348. }
  1349. /**
  1350. * ice_devlink_register - Register devlink interface for this PF
  1351. * @pf: the PF to register the devlink for.
  1352. *
  1353. * Register the devlink instance associated with this physical function.
  1354. *
  1355. * Return: zero on success or an error code on failure.
  1356. */
  1357. void ice_devlink_register(struct ice_pf *pf)
  1358. {
  1359. struct devlink *devlink = priv_to_devlink(pf);
  1360. devl_register(devlink);
  1361. }
  1362. /**
  1363. * ice_devlink_unregister - Unregister devlink resources for this PF.
  1364. * @pf: the PF structure to cleanup
  1365. *
  1366. * Releases resources used by devlink and cleans up associated memory.
  1367. */
  1368. void ice_devlink_unregister(struct ice_pf *pf)
  1369. {
  1370. devl_unregister(priv_to_devlink(pf));
  1371. }
  1372. int ice_devlink_register_params(struct ice_pf *pf)
  1373. {
  1374. struct devlink *devlink = priv_to_devlink(pf);
  1375. struct ice_hw *hw = &pf->hw;
  1376. int status;
  1377. status = devl_params_register(devlink, ice_dvl_rdma_params,
  1378. ARRAY_SIZE(ice_dvl_rdma_params));
  1379. if (status)
  1380. return status;
  1381. if (hw->func_caps.common_cap.tx_sched_topo_comp_mode_en)
  1382. status = devl_params_register(devlink, ice_dvl_sched_params,
  1383. ARRAY_SIZE(ice_dvl_sched_params));
  1384. return status;
  1385. }
  1386. void ice_devlink_unregister_params(struct ice_pf *pf)
  1387. {
  1388. struct devlink *devlink = priv_to_devlink(pf);
  1389. struct ice_hw *hw = &pf->hw;
  1390. devl_params_unregister(devlink, ice_dvl_rdma_params,
  1391. ARRAY_SIZE(ice_dvl_rdma_params));
  1392. if (hw->func_caps.common_cap.tx_sched_topo_comp_mode_en)
  1393. devl_params_unregister(devlink, ice_dvl_sched_params,
  1394. ARRAY_SIZE(ice_dvl_sched_params));
  1395. }
  1396. #define ICE_DEVLINK_READ_BLK_SIZE (1024 * 1024)
  1397. static const struct devlink_region_ops ice_nvm_region_ops;
  1398. static const struct devlink_region_ops ice_sram_region_ops;
  1399. /**
  1400. * ice_devlink_nvm_snapshot - Capture a snapshot of the NVM flash contents
  1401. * @devlink: the devlink instance
  1402. * @ops: the devlink region to snapshot
  1403. * @extack: extended ACK response structure
  1404. * @data: on exit points to snapshot data buffer
  1405. *
  1406. * This function is called in response to a DEVLINK_CMD_REGION_NEW for either
  1407. * the nvm-flash or shadow-ram region.
  1408. *
  1409. * It captures a snapshot of the NVM or Shadow RAM flash contents. This
  1410. * snapshot can then later be viewed via the DEVLINK_CMD_REGION_READ netlink
  1411. * interface.
  1412. *
  1413. * @returns zero on success, and updates the data pointer. Returns a non-zero
  1414. * error code on failure.
  1415. */
  1416. static int ice_devlink_nvm_snapshot(struct devlink *devlink,
  1417. const struct devlink_region_ops *ops,
  1418. struct netlink_ext_ack *extack, u8 **data)
  1419. {
  1420. struct ice_pf *pf = devlink_priv(devlink);
  1421. struct device *dev = ice_pf_to_dev(pf);
  1422. struct ice_hw *hw = &pf->hw;
  1423. bool read_shadow_ram;
  1424. u8 *nvm_data, *tmp, i;
  1425. u32 nvm_size, left;
  1426. s8 num_blks;
  1427. int status;
  1428. if (ops == &ice_nvm_region_ops) {
  1429. read_shadow_ram = false;
  1430. nvm_size = hw->flash.flash_size;
  1431. } else if (ops == &ice_sram_region_ops) {
  1432. read_shadow_ram = true;
  1433. nvm_size = hw->flash.sr_words * 2u;
  1434. } else {
  1435. NL_SET_ERR_MSG_MOD(extack, "Unexpected region in snapshot function");
  1436. return -EOPNOTSUPP;
  1437. }
  1438. nvm_data = vzalloc(nvm_size);
  1439. if (!nvm_data)
  1440. return -ENOMEM;
  1441. num_blks = DIV_ROUND_UP(nvm_size, ICE_DEVLINK_READ_BLK_SIZE);
  1442. tmp = nvm_data;
  1443. left = nvm_size;
  1444. /* Some systems take longer to read the NVM than others which causes the
  1445. * FW to reclaim the NVM lock before the entire NVM has been read. Fix
  1446. * this by breaking the reads of the NVM into smaller chunks that will
  1447. * probably not take as long. This has some overhead since we are
  1448. * increasing the number of AQ commands, but it should always work
  1449. */
  1450. for (i = 0; i < num_blks; i++) {
  1451. u32 read_sz = min_t(u32, ICE_DEVLINK_READ_BLK_SIZE, left);
  1452. status = ice_acquire_nvm(hw, ICE_RES_READ);
  1453. if (status) {
  1454. dev_dbg(dev, "ice_acquire_nvm failed, err %d aq_err %d\n",
  1455. status, hw->adminq.sq_last_status);
  1456. NL_SET_ERR_MSG_MOD(extack, "Failed to acquire NVM semaphore");
  1457. vfree(nvm_data);
  1458. return -EIO;
  1459. }
  1460. status = ice_read_flat_nvm(hw, i * ICE_DEVLINK_READ_BLK_SIZE,
  1461. &read_sz, tmp, read_shadow_ram);
  1462. if (status) {
  1463. dev_dbg(dev, "ice_read_flat_nvm failed after reading %u bytes, err %d aq_err %d\n",
  1464. read_sz, status, hw->adminq.sq_last_status);
  1465. NL_SET_ERR_MSG_MOD(extack, "Failed to read NVM contents");
  1466. ice_release_nvm(hw);
  1467. vfree(nvm_data);
  1468. return -EIO;
  1469. }
  1470. ice_release_nvm(hw);
  1471. tmp += read_sz;
  1472. left -= read_sz;
  1473. }
  1474. *data = nvm_data;
  1475. return 0;
  1476. }
  1477. /**
  1478. * ice_devlink_nvm_read - Read a portion of NVM flash contents
  1479. * @devlink: the devlink instance
  1480. * @ops: the devlink region to snapshot
  1481. * @extack: extended ACK response structure
  1482. * @offset: the offset to start at
  1483. * @size: the amount to read
  1484. * @data: the data buffer to read into
  1485. *
  1486. * This function is called in response to DEVLINK_CMD_REGION_READ to directly
  1487. * read a section of the NVM contents.
  1488. *
  1489. * It reads from either the nvm-flash or shadow-ram region contents.
  1490. *
  1491. * @returns zero on success, and updates the data pointer. Returns a non-zero
  1492. * error code on failure.
  1493. */
  1494. static int ice_devlink_nvm_read(struct devlink *devlink,
  1495. const struct devlink_region_ops *ops,
  1496. struct netlink_ext_ack *extack,
  1497. u64 offset, u32 size, u8 *data)
  1498. {
  1499. struct ice_pf *pf = devlink_priv(devlink);
  1500. struct device *dev = ice_pf_to_dev(pf);
  1501. struct ice_hw *hw = &pf->hw;
  1502. bool read_shadow_ram;
  1503. u64 nvm_size;
  1504. int status;
  1505. if (ops == &ice_nvm_region_ops) {
  1506. read_shadow_ram = false;
  1507. nvm_size = hw->flash.flash_size;
  1508. } else if (ops == &ice_sram_region_ops) {
  1509. read_shadow_ram = true;
  1510. nvm_size = hw->flash.sr_words * 2u;
  1511. } else {
  1512. NL_SET_ERR_MSG_MOD(extack, "Unexpected region in snapshot function");
  1513. return -EOPNOTSUPP;
  1514. }
  1515. if (offset + size >= nvm_size) {
  1516. NL_SET_ERR_MSG_MOD(extack, "Cannot read beyond the region size");
  1517. return -ERANGE;
  1518. }
  1519. status = ice_acquire_nvm(hw, ICE_RES_READ);
  1520. if (status) {
  1521. dev_dbg(dev, "ice_acquire_nvm failed, err %d aq_err %d\n",
  1522. status, hw->adminq.sq_last_status);
  1523. NL_SET_ERR_MSG_MOD(extack, "Failed to acquire NVM semaphore");
  1524. return -EIO;
  1525. }
  1526. status = ice_read_flat_nvm(hw, (u32)offset, &size, data,
  1527. read_shadow_ram);
  1528. if (status) {
  1529. dev_dbg(dev, "ice_read_flat_nvm failed after reading %u bytes, err %d aq_err %d\n",
  1530. size, status, hw->adminq.sq_last_status);
  1531. NL_SET_ERR_MSG_MOD(extack, "Failed to read NVM contents");
  1532. ice_release_nvm(hw);
  1533. return -EIO;
  1534. }
  1535. ice_release_nvm(hw);
  1536. return 0;
  1537. }
  1538. /**
  1539. * ice_devlink_devcaps_snapshot - Capture snapshot of device capabilities
  1540. * @devlink: the devlink instance
  1541. * @ops: the devlink region being snapshotted
  1542. * @extack: extended ACK response structure
  1543. * @data: on exit points to snapshot data buffer
  1544. *
  1545. * This function is called in response to the DEVLINK_CMD_REGION_TRIGGER for
  1546. * the device-caps devlink region. It captures a snapshot of the device
  1547. * capabilities reported by firmware.
  1548. *
  1549. * @returns zero on success, and updates the data pointer. Returns a non-zero
  1550. * error code on failure.
  1551. */
  1552. static int
  1553. ice_devlink_devcaps_snapshot(struct devlink *devlink,
  1554. const struct devlink_region_ops *ops,
  1555. struct netlink_ext_ack *extack, u8 **data)
  1556. {
  1557. struct ice_pf *pf = devlink_priv(devlink);
  1558. struct device *dev = ice_pf_to_dev(pf);
  1559. struct ice_hw *hw = &pf->hw;
  1560. void *devcaps;
  1561. int status;
  1562. devcaps = vzalloc(ICE_AQ_MAX_BUF_LEN);
  1563. if (!devcaps)
  1564. return -ENOMEM;
  1565. status = ice_aq_list_caps(hw, devcaps, ICE_AQ_MAX_BUF_LEN, NULL,
  1566. ice_aqc_opc_list_dev_caps, NULL);
  1567. if (status) {
  1568. dev_dbg(dev, "ice_aq_list_caps: failed to read device capabilities, err %d aq_err %d\n",
  1569. status, hw->adminq.sq_last_status);
  1570. NL_SET_ERR_MSG_MOD(extack, "Failed to read device capabilities");
  1571. vfree(devcaps);
  1572. return status;
  1573. }
  1574. *data = (u8 *)devcaps;
  1575. return 0;
  1576. }
  1577. static const struct devlink_region_ops ice_nvm_region_ops = {
  1578. .name = "nvm-flash",
  1579. .destructor = vfree,
  1580. .snapshot = ice_devlink_nvm_snapshot,
  1581. .read = ice_devlink_nvm_read,
  1582. };
  1583. static const struct devlink_region_ops ice_sram_region_ops = {
  1584. .name = "shadow-ram",
  1585. .destructor = vfree,
  1586. .snapshot = ice_devlink_nvm_snapshot,
  1587. .read = ice_devlink_nvm_read,
  1588. };
  1589. static const struct devlink_region_ops ice_devcaps_region_ops = {
  1590. .name = "device-caps",
  1591. .destructor = vfree,
  1592. .snapshot = ice_devlink_devcaps_snapshot,
  1593. };
  1594. /**
  1595. * ice_devlink_init_regions - Initialize devlink regions
  1596. * @pf: the PF device structure
  1597. *
  1598. * Create devlink regions used to enable access to dump the contents of the
  1599. * flash memory on the device.
  1600. */
  1601. void ice_devlink_init_regions(struct ice_pf *pf)
  1602. {
  1603. struct devlink *devlink = priv_to_devlink(pf);
  1604. struct device *dev = ice_pf_to_dev(pf);
  1605. u64 nvm_size, sram_size;
  1606. nvm_size = pf->hw.flash.flash_size;
  1607. pf->nvm_region = devl_region_create(devlink, &ice_nvm_region_ops, 1,
  1608. nvm_size);
  1609. if (IS_ERR(pf->nvm_region)) {
  1610. dev_err(dev, "failed to create NVM devlink region, err %ld\n",
  1611. PTR_ERR(pf->nvm_region));
  1612. pf->nvm_region = NULL;
  1613. }
  1614. sram_size = pf->hw.flash.sr_words * 2u;
  1615. pf->sram_region = devl_region_create(devlink, &ice_sram_region_ops,
  1616. 1, sram_size);
  1617. if (IS_ERR(pf->sram_region)) {
  1618. dev_err(dev, "failed to create shadow-ram devlink region, err %ld\n",
  1619. PTR_ERR(pf->sram_region));
  1620. pf->sram_region = NULL;
  1621. }
  1622. pf->devcaps_region = devl_region_create(devlink,
  1623. &ice_devcaps_region_ops, 10,
  1624. ICE_AQ_MAX_BUF_LEN);
  1625. if (IS_ERR(pf->devcaps_region)) {
  1626. dev_err(dev, "failed to create device-caps devlink region, err %ld\n",
  1627. PTR_ERR(pf->devcaps_region));
  1628. pf->devcaps_region = NULL;
  1629. }
  1630. }
  1631. /**
  1632. * ice_devlink_destroy_regions - Destroy devlink regions
  1633. * @pf: the PF device structure
  1634. *
  1635. * Remove previously created regions for this PF.
  1636. */
  1637. void ice_devlink_destroy_regions(struct ice_pf *pf)
  1638. {
  1639. if (pf->nvm_region)
  1640. devl_region_destroy(pf->nvm_region);
  1641. if (pf->sram_region)
  1642. devl_region_destroy(pf->sram_region);
  1643. if (pf->devcaps_region)
  1644. devl_region_destroy(pf->devcaps_region);
  1645. }