Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /* Copyright (c) 2018-2019, Vladimir Oltean <olteanv@gmail.com>
0003  * Copyright 2020 NXP
0004  */
0005 #include "sja1105.h"
0006 
0007 /* Since devlink regions have a fixed size and the static config has a variable
0008  * size, we need to calculate the maximum possible static config size by
0009  * creating a dummy config with all table entries populated to the max, and get
0010  * its packed length. This is done dynamically as opposed to simply hardcoding
0011  * a number, since currently not all static config tables are implemented, so
0012  * we are avoiding a possible code desynchronization.
0013  */
0014 static size_t sja1105_static_config_get_max_size(struct sja1105_private *priv)
0015 {
0016     struct sja1105_static_config config;
0017     enum sja1105_blk_idx blk_idx;
0018     int rc;
0019 
0020     rc = sja1105_static_config_init(&config,
0021                     priv->info->static_ops,
0022                     priv->info->device_id);
0023     if (rc)
0024         return 0;
0025 
0026     for (blk_idx = 0; blk_idx < BLK_IDX_MAX; blk_idx++) {
0027         struct sja1105_table *table = &config.tables[blk_idx];
0028 
0029         table->entry_count = table->ops->max_entry_count;
0030     }
0031 
0032     return sja1105_static_config_get_length(&config);
0033 }
0034 
0035 static int
0036 sja1105_region_static_config_snapshot(struct devlink *dl,
0037                       const struct devlink_region_ops *ops,
0038                       struct netlink_ext_ack *extack,
0039                       u8 **data)
0040 {
0041     struct dsa_switch *ds = dsa_devlink_to_ds(dl);
0042     struct sja1105_private *priv = ds->priv;
0043     size_t max_len, len;
0044 
0045     len = sja1105_static_config_get_length(&priv->static_config);
0046     max_len = sja1105_static_config_get_max_size(priv);
0047 
0048     *data = kcalloc(max_len, sizeof(u8), GFP_KERNEL);
0049     if (!*data)
0050         return -ENOMEM;
0051 
0052     return static_config_buf_prepare_for_upload(priv, *data, len);
0053 }
0054 
0055 static struct devlink_region_ops sja1105_region_static_config_ops = {
0056     .name = "static-config",
0057     .snapshot = sja1105_region_static_config_snapshot,
0058     .destructor = kfree,
0059 };
0060 
0061 enum sja1105_region_id {
0062     SJA1105_REGION_STATIC_CONFIG = 0,
0063 };
0064 
0065 struct sja1105_region {
0066     const struct devlink_region_ops *ops;
0067     size_t (*get_size)(struct sja1105_private *priv);
0068 };
0069 
0070 static struct sja1105_region sja1105_regions[] = {
0071     [SJA1105_REGION_STATIC_CONFIG] = {
0072         .ops = &sja1105_region_static_config_ops,
0073         .get_size = sja1105_static_config_get_max_size,
0074     },
0075 };
0076 
0077 static int sja1105_setup_devlink_regions(struct dsa_switch *ds)
0078 {
0079     int i, num_regions = ARRAY_SIZE(sja1105_regions);
0080     struct sja1105_private *priv = ds->priv;
0081     const struct devlink_region_ops *ops;
0082     struct devlink_region *region;
0083     u64 size;
0084 
0085     priv->regions = kcalloc(num_regions, sizeof(struct devlink_region *),
0086                 GFP_KERNEL);
0087     if (!priv->regions)
0088         return -ENOMEM;
0089 
0090     for (i = 0; i < num_regions; i++) {
0091         size = sja1105_regions[i].get_size(priv);
0092         ops = sja1105_regions[i].ops;
0093 
0094         region = dsa_devlink_region_create(ds, ops, 1, size);
0095         if (IS_ERR(region)) {
0096             while (--i >= 0)
0097                 dsa_devlink_region_destroy(priv->regions[i]);
0098             return PTR_ERR(region);
0099         }
0100 
0101         priv->regions[i] = region;
0102     }
0103 
0104     return 0;
0105 }
0106 
0107 static void sja1105_teardown_devlink_regions(struct dsa_switch *ds)
0108 {
0109     int i, num_regions = ARRAY_SIZE(sja1105_regions);
0110     struct sja1105_private *priv = ds->priv;
0111 
0112     for (i = 0; i < num_regions; i++)
0113         dsa_devlink_region_destroy(priv->regions[i]);
0114 
0115     kfree(priv->regions);
0116 }
0117 
0118 int sja1105_devlink_info_get(struct dsa_switch *ds,
0119                  struct devlink_info_req *req,
0120                  struct netlink_ext_ack *extack)
0121 {
0122     struct sja1105_private *priv = ds->priv;
0123     int rc;
0124 
0125     rc = devlink_info_driver_name_put(req, "sja1105");
0126     if (rc)
0127         return rc;
0128 
0129     rc = devlink_info_version_fixed_put(req,
0130                         DEVLINK_INFO_VERSION_GENERIC_ASIC_ID,
0131                         priv->info->name);
0132     return rc;
0133 }
0134 
0135 int sja1105_devlink_setup(struct dsa_switch *ds)
0136 {
0137     return sja1105_setup_devlink_regions(ds);
0138 }
0139 
0140 void sja1105_devlink_teardown(struct dsa_switch *ds)
0141 {
0142     sja1105_teardown_devlink_regions(ds);
0143 }