sja1105_devlink.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright (c) 2018-2019, Vladimir Oltean <[email protected]>
  3. * Copyright 2020 NXP
  4. */
  5. #include "sja1105.h"
  6. /* Since devlink regions have a fixed size and the static config has a variable
  7. * size, we need to calculate the maximum possible static config size by
  8. * creating a dummy config with all table entries populated to the max, and get
  9. * its packed length. This is done dynamically as opposed to simply hardcoding
  10. * a number, since currently not all static config tables are implemented, so
  11. * we are avoiding a possible code desynchronization.
  12. */
  13. static size_t sja1105_static_config_get_max_size(struct sja1105_private *priv)
  14. {
  15. struct sja1105_static_config config;
  16. enum sja1105_blk_idx blk_idx;
  17. int rc;
  18. rc = sja1105_static_config_init(&config,
  19. priv->info->static_ops,
  20. priv->info->device_id);
  21. if (rc)
  22. return 0;
  23. for (blk_idx = 0; blk_idx < BLK_IDX_MAX; blk_idx++) {
  24. struct sja1105_table *table = &config.tables[blk_idx];
  25. table->entry_count = table->ops->max_entry_count;
  26. }
  27. return sja1105_static_config_get_length(&config);
  28. }
  29. static int
  30. sja1105_region_static_config_snapshot(struct devlink *dl,
  31. const struct devlink_region_ops *ops,
  32. struct netlink_ext_ack *extack,
  33. u8 **data)
  34. {
  35. struct dsa_switch *ds = dsa_devlink_to_ds(dl);
  36. struct sja1105_private *priv = ds->priv;
  37. size_t max_len, len;
  38. len = sja1105_static_config_get_length(&priv->static_config);
  39. max_len = sja1105_static_config_get_max_size(priv);
  40. *data = kcalloc(max_len, sizeof(u8), GFP_KERNEL);
  41. if (!*data)
  42. return -ENOMEM;
  43. return static_config_buf_prepare_for_upload(priv, *data, len);
  44. }
  45. static struct devlink_region_ops sja1105_region_static_config_ops = {
  46. .name = "static-config",
  47. .snapshot = sja1105_region_static_config_snapshot,
  48. .destructor = kfree,
  49. };
  50. enum sja1105_region_id {
  51. SJA1105_REGION_STATIC_CONFIG = 0,
  52. };
  53. struct sja1105_region {
  54. const struct devlink_region_ops *ops;
  55. size_t (*get_size)(struct sja1105_private *priv);
  56. };
  57. static struct sja1105_region sja1105_regions[] = {
  58. [SJA1105_REGION_STATIC_CONFIG] = {
  59. .ops = &sja1105_region_static_config_ops,
  60. .get_size = sja1105_static_config_get_max_size,
  61. },
  62. };
  63. static int sja1105_setup_devlink_regions(struct dsa_switch *ds)
  64. {
  65. int i, num_regions = ARRAY_SIZE(sja1105_regions);
  66. struct sja1105_private *priv = ds->priv;
  67. const struct devlink_region_ops *ops;
  68. struct devlink_region *region;
  69. u64 size;
  70. priv->regions = kcalloc(num_regions, sizeof(struct devlink_region *),
  71. GFP_KERNEL);
  72. if (!priv->regions)
  73. return -ENOMEM;
  74. for (i = 0; i < num_regions; i++) {
  75. size = sja1105_regions[i].get_size(priv);
  76. ops = sja1105_regions[i].ops;
  77. region = dsa_devlink_region_create(ds, ops, 1, size);
  78. if (IS_ERR(region)) {
  79. while (--i >= 0)
  80. dsa_devlink_region_destroy(priv->regions[i]);
  81. kfree(priv->regions);
  82. return PTR_ERR(region);
  83. }
  84. priv->regions[i] = region;
  85. }
  86. return 0;
  87. }
  88. static void sja1105_teardown_devlink_regions(struct dsa_switch *ds)
  89. {
  90. int i, num_regions = ARRAY_SIZE(sja1105_regions);
  91. struct sja1105_private *priv = ds->priv;
  92. for (i = 0; i < num_regions; i++)
  93. dsa_devlink_region_destroy(priv->regions[i]);
  94. kfree(priv->regions);
  95. }
  96. int sja1105_devlink_info_get(struct dsa_switch *ds,
  97. struct devlink_info_req *req,
  98. struct netlink_ext_ack *extack)
  99. {
  100. struct sja1105_private *priv = ds->priv;
  101. int rc;
  102. rc = devlink_info_driver_name_put(req, "sja1105");
  103. if (rc)
  104. return rc;
  105. rc = devlink_info_version_fixed_put(req,
  106. DEVLINK_INFO_VERSION_GENERIC_ASIC_ID,
  107. priv->info->name);
  108. return rc;
  109. }
  110. int sja1105_devlink_setup(struct dsa_switch *ds)
  111. {
  112. return sja1105_setup_devlink_regions(ds);
  113. }
  114. void sja1105_devlink_teardown(struct dsa_switch *ds)
  115. {
  116. sja1105_teardown_devlink_regions(ds);
  117. }