162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 262306a36Sopenharmony_ci/* 362306a36Sopenharmony_ci * Copyright (c) 2016 Mellanox Technologies. All rights reserved. 462306a36Sopenharmony_ci * Copyright (c) 2016 Jiri Pirko <jiri@mellanox.com> 562306a36Sopenharmony_ci */ 662306a36Sopenharmony_ci 762306a36Sopenharmony_ci#include "devl_internal.h" 862306a36Sopenharmony_ci 962306a36Sopenharmony_cistruct devlink_region { 1062306a36Sopenharmony_ci struct devlink *devlink; 1162306a36Sopenharmony_ci struct devlink_port *port; 1262306a36Sopenharmony_ci struct list_head list; 1362306a36Sopenharmony_ci union { 1462306a36Sopenharmony_ci const struct devlink_region_ops *ops; 1562306a36Sopenharmony_ci const struct devlink_port_region_ops *port_ops; 1662306a36Sopenharmony_ci }; 1762306a36Sopenharmony_ci struct mutex snapshot_lock; /* protects snapshot_list, 1862306a36Sopenharmony_ci * max_snapshots and cur_snapshots 1962306a36Sopenharmony_ci * consistency. 2062306a36Sopenharmony_ci */ 2162306a36Sopenharmony_ci struct list_head snapshot_list; 2262306a36Sopenharmony_ci u32 max_snapshots; 2362306a36Sopenharmony_ci u32 cur_snapshots; 2462306a36Sopenharmony_ci u64 size; 2562306a36Sopenharmony_ci}; 2662306a36Sopenharmony_ci 2762306a36Sopenharmony_cistruct devlink_snapshot { 2862306a36Sopenharmony_ci struct list_head list; 2962306a36Sopenharmony_ci struct devlink_region *region; 3062306a36Sopenharmony_ci u8 *data; 3162306a36Sopenharmony_ci u32 id; 3262306a36Sopenharmony_ci}; 3362306a36Sopenharmony_ci 3462306a36Sopenharmony_cistatic struct devlink_region * 3562306a36Sopenharmony_cidevlink_region_get_by_name(struct devlink *devlink, const char *region_name) 3662306a36Sopenharmony_ci{ 3762306a36Sopenharmony_ci struct devlink_region *region; 3862306a36Sopenharmony_ci 3962306a36Sopenharmony_ci list_for_each_entry(region, &devlink->region_list, list) 4062306a36Sopenharmony_ci if (!strcmp(region->ops->name, region_name)) 4162306a36Sopenharmony_ci return region; 4262306a36Sopenharmony_ci 4362306a36Sopenharmony_ci return NULL; 4462306a36Sopenharmony_ci} 4562306a36Sopenharmony_ci 4662306a36Sopenharmony_cistatic struct devlink_region * 4762306a36Sopenharmony_cidevlink_port_region_get_by_name(struct devlink_port *port, 4862306a36Sopenharmony_ci const char *region_name) 4962306a36Sopenharmony_ci{ 5062306a36Sopenharmony_ci struct devlink_region *region; 5162306a36Sopenharmony_ci 5262306a36Sopenharmony_ci list_for_each_entry(region, &port->region_list, list) 5362306a36Sopenharmony_ci if (!strcmp(region->ops->name, region_name)) 5462306a36Sopenharmony_ci return region; 5562306a36Sopenharmony_ci 5662306a36Sopenharmony_ci return NULL; 5762306a36Sopenharmony_ci} 5862306a36Sopenharmony_ci 5962306a36Sopenharmony_cistatic struct devlink_snapshot * 6062306a36Sopenharmony_cidevlink_region_snapshot_get_by_id(struct devlink_region *region, u32 id) 6162306a36Sopenharmony_ci{ 6262306a36Sopenharmony_ci struct devlink_snapshot *snapshot; 6362306a36Sopenharmony_ci 6462306a36Sopenharmony_ci list_for_each_entry(snapshot, ®ion->snapshot_list, list) 6562306a36Sopenharmony_ci if (snapshot->id == id) 6662306a36Sopenharmony_ci return snapshot; 6762306a36Sopenharmony_ci 6862306a36Sopenharmony_ci return NULL; 6962306a36Sopenharmony_ci} 7062306a36Sopenharmony_ci 7162306a36Sopenharmony_cistatic int devlink_nl_region_snapshot_id_put(struct sk_buff *msg, 7262306a36Sopenharmony_ci struct devlink *devlink, 7362306a36Sopenharmony_ci struct devlink_snapshot *snapshot) 7462306a36Sopenharmony_ci{ 7562306a36Sopenharmony_ci struct nlattr *snap_attr; 7662306a36Sopenharmony_ci int err; 7762306a36Sopenharmony_ci 7862306a36Sopenharmony_ci snap_attr = nla_nest_start_noflag(msg, DEVLINK_ATTR_REGION_SNAPSHOT); 7962306a36Sopenharmony_ci if (!snap_attr) 8062306a36Sopenharmony_ci return -EINVAL; 8162306a36Sopenharmony_ci 8262306a36Sopenharmony_ci err = nla_put_u32(msg, DEVLINK_ATTR_REGION_SNAPSHOT_ID, snapshot->id); 8362306a36Sopenharmony_ci if (err) 8462306a36Sopenharmony_ci goto nla_put_failure; 8562306a36Sopenharmony_ci 8662306a36Sopenharmony_ci nla_nest_end(msg, snap_attr); 8762306a36Sopenharmony_ci return 0; 8862306a36Sopenharmony_ci 8962306a36Sopenharmony_cinla_put_failure: 9062306a36Sopenharmony_ci nla_nest_cancel(msg, snap_attr); 9162306a36Sopenharmony_ci return err; 9262306a36Sopenharmony_ci} 9362306a36Sopenharmony_ci 9462306a36Sopenharmony_cistatic int devlink_nl_region_snapshots_id_put(struct sk_buff *msg, 9562306a36Sopenharmony_ci struct devlink *devlink, 9662306a36Sopenharmony_ci struct devlink_region *region) 9762306a36Sopenharmony_ci{ 9862306a36Sopenharmony_ci struct devlink_snapshot *snapshot; 9962306a36Sopenharmony_ci struct nlattr *snapshots_attr; 10062306a36Sopenharmony_ci int err; 10162306a36Sopenharmony_ci 10262306a36Sopenharmony_ci snapshots_attr = nla_nest_start_noflag(msg, 10362306a36Sopenharmony_ci DEVLINK_ATTR_REGION_SNAPSHOTS); 10462306a36Sopenharmony_ci if (!snapshots_attr) 10562306a36Sopenharmony_ci return -EINVAL; 10662306a36Sopenharmony_ci 10762306a36Sopenharmony_ci list_for_each_entry(snapshot, ®ion->snapshot_list, list) { 10862306a36Sopenharmony_ci err = devlink_nl_region_snapshot_id_put(msg, devlink, snapshot); 10962306a36Sopenharmony_ci if (err) 11062306a36Sopenharmony_ci goto nla_put_failure; 11162306a36Sopenharmony_ci } 11262306a36Sopenharmony_ci 11362306a36Sopenharmony_ci nla_nest_end(msg, snapshots_attr); 11462306a36Sopenharmony_ci return 0; 11562306a36Sopenharmony_ci 11662306a36Sopenharmony_cinla_put_failure: 11762306a36Sopenharmony_ci nla_nest_cancel(msg, snapshots_attr); 11862306a36Sopenharmony_ci return err; 11962306a36Sopenharmony_ci} 12062306a36Sopenharmony_ci 12162306a36Sopenharmony_cistatic int devlink_nl_region_fill(struct sk_buff *msg, struct devlink *devlink, 12262306a36Sopenharmony_ci enum devlink_command cmd, u32 portid, 12362306a36Sopenharmony_ci u32 seq, int flags, 12462306a36Sopenharmony_ci struct devlink_region *region) 12562306a36Sopenharmony_ci{ 12662306a36Sopenharmony_ci void *hdr; 12762306a36Sopenharmony_ci int err; 12862306a36Sopenharmony_ci 12962306a36Sopenharmony_ci hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd); 13062306a36Sopenharmony_ci if (!hdr) 13162306a36Sopenharmony_ci return -EMSGSIZE; 13262306a36Sopenharmony_ci 13362306a36Sopenharmony_ci err = devlink_nl_put_handle(msg, devlink); 13462306a36Sopenharmony_ci if (err) 13562306a36Sopenharmony_ci goto nla_put_failure; 13662306a36Sopenharmony_ci 13762306a36Sopenharmony_ci if (region->port) { 13862306a36Sopenharmony_ci err = nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, 13962306a36Sopenharmony_ci region->port->index); 14062306a36Sopenharmony_ci if (err) 14162306a36Sopenharmony_ci goto nla_put_failure; 14262306a36Sopenharmony_ci } 14362306a36Sopenharmony_ci 14462306a36Sopenharmony_ci err = nla_put_string(msg, DEVLINK_ATTR_REGION_NAME, region->ops->name); 14562306a36Sopenharmony_ci if (err) 14662306a36Sopenharmony_ci goto nla_put_failure; 14762306a36Sopenharmony_ci 14862306a36Sopenharmony_ci err = nla_put_u64_64bit(msg, DEVLINK_ATTR_REGION_SIZE, 14962306a36Sopenharmony_ci region->size, 15062306a36Sopenharmony_ci DEVLINK_ATTR_PAD); 15162306a36Sopenharmony_ci if (err) 15262306a36Sopenharmony_ci goto nla_put_failure; 15362306a36Sopenharmony_ci 15462306a36Sopenharmony_ci err = nla_put_u32(msg, DEVLINK_ATTR_REGION_MAX_SNAPSHOTS, 15562306a36Sopenharmony_ci region->max_snapshots); 15662306a36Sopenharmony_ci if (err) 15762306a36Sopenharmony_ci goto nla_put_failure; 15862306a36Sopenharmony_ci 15962306a36Sopenharmony_ci err = devlink_nl_region_snapshots_id_put(msg, devlink, region); 16062306a36Sopenharmony_ci if (err) 16162306a36Sopenharmony_ci goto nla_put_failure; 16262306a36Sopenharmony_ci 16362306a36Sopenharmony_ci genlmsg_end(msg, hdr); 16462306a36Sopenharmony_ci return 0; 16562306a36Sopenharmony_ci 16662306a36Sopenharmony_cinla_put_failure: 16762306a36Sopenharmony_ci genlmsg_cancel(msg, hdr); 16862306a36Sopenharmony_ci return err; 16962306a36Sopenharmony_ci} 17062306a36Sopenharmony_ci 17162306a36Sopenharmony_cistatic struct sk_buff * 17262306a36Sopenharmony_cidevlink_nl_region_notify_build(struct devlink_region *region, 17362306a36Sopenharmony_ci struct devlink_snapshot *snapshot, 17462306a36Sopenharmony_ci enum devlink_command cmd, u32 portid, u32 seq) 17562306a36Sopenharmony_ci{ 17662306a36Sopenharmony_ci struct devlink *devlink = region->devlink; 17762306a36Sopenharmony_ci struct sk_buff *msg; 17862306a36Sopenharmony_ci void *hdr; 17962306a36Sopenharmony_ci int err; 18062306a36Sopenharmony_ci 18162306a36Sopenharmony_ci msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); 18262306a36Sopenharmony_ci if (!msg) 18362306a36Sopenharmony_ci return ERR_PTR(-ENOMEM); 18462306a36Sopenharmony_ci 18562306a36Sopenharmony_ci hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, 0, cmd); 18662306a36Sopenharmony_ci if (!hdr) { 18762306a36Sopenharmony_ci err = -EMSGSIZE; 18862306a36Sopenharmony_ci goto out_free_msg; 18962306a36Sopenharmony_ci } 19062306a36Sopenharmony_ci 19162306a36Sopenharmony_ci err = devlink_nl_put_handle(msg, devlink); 19262306a36Sopenharmony_ci if (err) 19362306a36Sopenharmony_ci goto out_cancel_msg; 19462306a36Sopenharmony_ci 19562306a36Sopenharmony_ci if (region->port) { 19662306a36Sopenharmony_ci err = nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, 19762306a36Sopenharmony_ci region->port->index); 19862306a36Sopenharmony_ci if (err) 19962306a36Sopenharmony_ci goto out_cancel_msg; 20062306a36Sopenharmony_ci } 20162306a36Sopenharmony_ci 20262306a36Sopenharmony_ci err = nla_put_string(msg, DEVLINK_ATTR_REGION_NAME, 20362306a36Sopenharmony_ci region->ops->name); 20462306a36Sopenharmony_ci if (err) 20562306a36Sopenharmony_ci goto out_cancel_msg; 20662306a36Sopenharmony_ci 20762306a36Sopenharmony_ci if (snapshot) { 20862306a36Sopenharmony_ci err = nla_put_u32(msg, DEVLINK_ATTR_REGION_SNAPSHOT_ID, 20962306a36Sopenharmony_ci snapshot->id); 21062306a36Sopenharmony_ci if (err) 21162306a36Sopenharmony_ci goto out_cancel_msg; 21262306a36Sopenharmony_ci } else { 21362306a36Sopenharmony_ci err = nla_put_u64_64bit(msg, DEVLINK_ATTR_REGION_SIZE, 21462306a36Sopenharmony_ci region->size, DEVLINK_ATTR_PAD); 21562306a36Sopenharmony_ci if (err) 21662306a36Sopenharmony_ci goto out_cancel_msg; 21762306a36Sopenharmony_ci } 21862306a36Sopenharmony_ci genlmsg_end(msg, hdr); 21962306a36Sopenharmony_ci 22062306a36Sopenharmony_ci return msg; 22162306a36Sopenharmony_ci 22262306a36Sopenharmony_ciout_cancel_msg: 22362306a36Sopenharmony_ci genlmsg_cancel(msg, hdr); 22462306a36Sopenharmony_ciout_free_msg: 22562306a36Sopenharmony_ci nlmsg_free(msg); 22662306a36Sopenharmony_ci return ERR_PTR(err); 22762306a36Sopenharmony_ci} 22862306a36Sopenharmony_ci 22962306a36Sopenharmony_cistatic void devlink_nl_region_notify(struct devlink_region *region, 23062306a36Sopenharmony_ci struct devlink_snapshot *snapshot, 23162306a36Sopenharmony_ci enum devlink_command cmd) 23262306a36Sopenharmony_ci{ 23362306a36Sopenharmony_ci struct devlink *devlink = region->devlink; 23462306a36Sopenharmony_ci struct sk_buff *msg; 23562306a36Sopenharmony_ci 23662306a36Sopenharmony_ci WARN_ON(cmd != DEVLINK_CMD_REGION_NEW && cmd != DEVLINK_CMD_REGION_DEL); 23762306a36Sopenharmony_ci if (!xa_get_mark(&devlinks, devlink->index, DEVLINK_REGISTERED)) 23862306a36Sopenharmony_ci return; 23962306a36Sopenharmony_ci 24062306a36Sopenharmony_ci msg = devlink_nl_region_notify_build(region, snapshot, cmd, 0, 0); 24162306a36Sopenharmony_ci if (IS_ERR(msg)) 24262306a36Sopenharmony_ci return; 24362306a36Sopenharmony_ci 24462306a36Sopenharmony_ci genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink), msg, 24562306a36Sopenharmony_ci 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL); 24662306a36Sopenharmony_ci} 24762306a36Sopenharmony_ci 24862306a36Sopenharmony_civoid devlink_regions_notify_register(struct devlink *devlink) 24962306a36Sopenharmony_ci{ 25062306a36Sopenharmony_ci struct devlink_region *region; 25162306a36Sopenharmony_ci 25262306a36Sopenharmony_ci list_for_each_entry(region, &devlink->region_list, list) 25362306a36Sopenharmony_ci devlink_nl_region_notify(region, NULL, DEVLINK_CMD_REGION_NEW); 25462306a36Sopenharmony_ci} 25562306a36Sopenharmony_ci 25662306a36Sopenharmony_civoid devlink_regions_notify_unregister(struct devlink *devlink) 25762306a36Sopenharmony_ci{ 25862306a36Sopenharmony_ci struct devlink_region *region; 25962306a36Sopenharmony_ci 26062306a36Sopenharmony_ci list_for_each_entry_reverse(region, &devlink->region_list, list) 26162306a36Sopenharmony_ci devlink_nl_region_notify(region, NULL, DEVLINK_CMD_REGION_DEL); 26262306a36Sopenharmony_ci} 26362306a36Sopenharmony_ci 26462306a36Sopenharmony_ci/** 26562306a36Sopenharmony_ci * __devlink_snapshot_id_increment - Increment number of snapshots using an id 26662306a36Sopenharmony_ci * @devlink: devlink instance 26762306a36Sopenharmony_ci * @id: the snapshot id 26862306a36Sopenharmony_ci * 26962306a36Sopenharmony_ci * Track when a new snapshot begins using an id. Load the count for the 27062306a36Sopenharmony_ci * given id from the snapshot xarray, increment it, and store it back. 27162306a36Sopenharmony_ci * 27262306a36Sopenharmony_ci * Called when a new snapshot is created with the given id. 27362306a36Sopenharmony_ci * 27462306a36Sopenharmony_ci * The id *must* have been previously allocated by 27562306a36Sopenharmony_ci * devlink_region_snapshot_id_get(). 27662306a36Sopenharmony_ci * 27762306a36Sopenharmony_ci * Returns 0 on success, or an error on failure. 27862306a36Sopenharmony_ci */ 27962306a36Sopenharmony_cistatic int __devlink_snapshot_id_increment(struct devlink *devlink, u32 id) 28062306a36Sopenharmony_ci{ 28162306a36Sopenharmony_ci unsigned long count; 28262306a36Sopenharmony_ci void *p; 28362306a36Sopenharmony_ci int err; 28462306a36Sopenharmony_ci 28562306a36Sopenharmony_ci xa_lock(&devlink->snapshot_ids); 28662306a36Sopenharmony_ci p = xa_load(&devlink->snapshot_ids, id); 28762306a36Sopenharmony_ci if (WARN_ON(!p)) { 28862306a36Sopenharmony_ci err = -EINVAL; 28962306a36Sopenharmony_ci goto unlock; 29062306a36Sopenharmony_ci } 29162306a36Sopenharmony_ci 29262306a36Sopenharmony_ci if (WARN_ON(!xa_is_value(p))) { 29362306a36Sopenharmony_ci err = -EINVAL; 29462306a36Sopenharmony_ci goto unlock; 29562306a36Sopenharmony_ci } 29662306a36Sopenharmony_ci 29762306a36Sopenharmony_ci count = xa_to_value(p); 29862306a36Sopenharmony_ci count++; 29962306a36Sopenharmony_ci 30062306a36Sopenharmony_ci err = xa_err(__xa_store(&devlink->snapshot_ids, id, xa_mk_value(count), 30162306a36Sopenharmony_ci GFP_ATOMIC)); 30262306a36Sopenharmony_ciunlock: 30362306a36Sopenharmony_ci xa_unlock(&devlink->snapshot_ids); 30462306a36Sopenharmony_ci return err; 30562306a36Sopenharmony_ci} 30662306a36Sopenharmony_ci 30762306a36Sopenharmony_ci/** 30862306a36Sopenharmony_ci * __devlink_snapshot_id_decrement - Decrease number of snapshots using an id 30962306a36Sopenharmony_ci * @devlink: devlink instance 31062306a36Sopenharmony_ci * @id: the snapshot id 31162306a36Sopenharmony_ci * 31262306a36Sopenharmony_ci * Track when a snapshot is deleted and stops using an id. Load the count 31362306a36Sopenharmony_ci * for the given id from the snapshot xarray, decrement it, and store it 31462306a36Sopenharmony_ci * back. 31562306a36Sopenharmony_ci * 31662306a36Sopenharmony_ci * If the count reaches zero, erase this id from the xarray, freeing it 31762306a36Sopenharmony_ci * up for future re-use by devlink_region_snapshot_id_get(). 31862306a36Sopenharmony_ci * 31962306a36Sopenharmony_ci * Called when a snapshot using the given id is deleted, and when the 32062306a36Sopenharmony_ci * initial allocator of the id is finished using it. 32162306a36Sopenharmony_ci */ 32262306a36Sopenharmony_cistatic void __devlink_snapshot_id_decrement(struct devlink *devlink, u32 id) 32362306a36Sopenharmony_ci{ 32462306a36Sopenharmony_ci unsigned long count; 32562306a36Sopenharmony_ci void *p; 32662306a36Sopenharmony_ci 32762306a36Sopenharmony_ci xa_lock(&devlink->snapshot_ids); 32862306a36Sopenharmony_ci p = xa_load(&devlink->snapshot_ids, id); 32962306a36Sopenharmony_ci if (WARN_ON(!p)) 33062306a36Sopenharmony_ci goto unlock; 33162306a36Sopenharmony_ci 33262306a36Sopenharmony_ci if (WARN_ON(!xa_is_value(p))) 33362306a36Sopenharmony_ci goto unlock; 33462306a36Sopenharmony_ci 33562306a36Sopenharmony_ci count = xa_to_value(p); 33662306a36Sopenharmony_ci 33762306a36Sopenharmony_ci if (count > 1) { 33862306a36Sopenharmony_ci count--; 33962306a36Sopenharmony_ci __xa_store(&devlink->snapshot_ids, id, xa_mk_value(count), 34062306a36Sopenharmony_ci GFP_ATOMIC); 34162306a36Sopenharmony_ci } else { 34262306a36Sopenharmony_ci /* If this was the last user, we can erase this id */ 34362306a36Sopenharmony_ci __xa_erase(&devlink->snapshot_ids, id); 34462306a36Sopenharmony_ci } 34562306a36Sopenharmony_ciunlock: 34662306a36Sopenharmony_ci xa_unlock(&devlink->snapshot_ids); 34762306a36Sopenharmony_ci} 34862306a36Sopenharmony_ci 34962306a36Sopenharmony_ci/** 35062306a36Sopenharmony_ci * __devlink_snapshot_id_insert - Insert a specific snapshot ID 35162306a36Sopenharmony_ci * @devlink: devlink instance 35262306a36Sopenharmony_ci * @id: the snapshot id 35362306a36Sopenharmony_ci * 35462306a36Sopenharmony_ci * Mark the given snapshot id as used by inserting a zero value into the 35562306a36Sopenharmony_ci * snapshot xarray. 35662306a36Sopenharmony_ci * 35762306a36Sopenharmony_ci * This must be called while holding the devlink instance lock. Unlike 35862306a36Sopenharmony_ci * devlink_snapshot_id_get, the initial reference count is zero, not one. 35962306a36Sopenharmony_ci * It is expected that the id will immediately be used before 36062306a36Sopenharmony_ci * releasing the devlink instance lock. 36162306a36Sopenharmony_ci * 36262306a36Sopenharmony_ci * Returns zero on success, or an error code if the snapshot id could not 36362306a36Sopenharmony_ci * be inserted. 36462306a36Sopenharmony_ci */ 36562306a36Sopenharmony_cistatic int __devlink_snapshot_id_insert(struct devlink *devlink, u32 id) 36662306a36Sopenharmony_ci{ 36762306a36Sopenharmony_ci int err; 36862306a36Sopenharmony_ci 36962306a36Sopenharmony_ci xa_lock(&devlink->snapshot_ids); 37062306a36Sopenharmony_ci if (xa_load(&devlink->snapshot_ids, id)) { 37162306a36Sopenharmony_ci xa_unlock(&devlink->snapshot_ids); 37262306a36Sopenharmony_ci return -EEXIST; 37362306a36Sopenharmony_ci } 37462306a36Sopenharmony_ci err = xa_err(__xa_store(&devlink->snapshot_ids, id, xa_mk_value(0), 37562306a36Sopenharmony_ci GFP_ATOMIC)); 37662306a36Sopenharmony_ci xa_unlock(&devlink->snapshot_ids); 37762306a36Sopenharmony_ci return err; 37862306a36Sopenharmony_ci} 37962306a36Sopenharmony_ci 38062306a36Sopenharmony_ci/** 38162306a36Sopenharmony_ci * __devlink_region_snapshot_id_get - get snapshot ID 38262306a36Sopenharmony_ci * @devlink: devlink instance 38362306a36Sopenharmony_ci * @id: storage to return snapshot id 38462306a36Sopenharmony_ci * 38562306a36Sopenharmony_ci * Allocates a new snapshot id. Returns zero on success, or a negative 38662306a36Sopenharmony_ci * error on failure. Must be called while holding the devlink instance 38762306a36Sopenharmony_ci * lock. 38862306a36Sopenharmony_ci * 38962306a36Sopenharmony_ci * Snapshot IDs are tracked using an xarray which stores the number of 39062306a36Sopenharmony_ci * users of the snapshot id. 39162306a36Sopenharmony_ci * 39262306a36Sopenharmony_ci * Note that the caller of this function counts as a 'user', in order to 39362306a36Sopenharmony_ci * avoid race conditions. The caller must release its hold on the 39462306a36Sopenharmony_ci * snapshot by using devlink_region_snapshot_id_put. 39562306a36Sopenharmony_ci */ 39662306a36Sopenharmony_cistatic int __devlink_region_snapshot_id_get(struct devlink *devlink, u32 *id) 39762306a36Sopenharmony_ci{ 39862306a36Sopenharmony_ci return xa_alloc(&devlink->snapshot_ids, id, xa_mk_value(1), 39962306a36Sopenharmony_ci xa_limit_32b, GFP_KERNEL); 40062306a36Sopenharmony_ci} 40162306a36Sopenharmony_ci 40262306a36Sopenharmony_ci/** 40362306a36Sopenharmony_ci * __devlink_region_snapshot_create - create a new snapshot 40462306a36Sopenharmony_ci * This will add a new snapshot of a region. The snapshot 40562306a36Sopenharmony_ci * will be stored on the region struct and can be accessed 40662306a36Sopenharmony_ci * from devlink. This is useful for future analyses of snapshots. 40762306a36Sopenharmony_ci * Multiple snapshots can be created on a region. 40862306a36Sopenharmony_ci * The @snapshot_id should be obtained using the getter function. 40962306a36Sopenharmony_ci * 41062306a36Sopenharmony_ci * Must be called only while holding the region snapshot lock. 41162306a36Sopenharmony_ci * 41262306a36Sopenharmony_ci * @region: devlink region of the snapshot 41362306a36Sopenharmony_ci * @data: snapshot data 41462306a36Sopenharmony_ci * @snapshot_id: snapshot id to be created 41562306a36Sopenharmony_ci */ 41662306a36Sopenharmony_cistatic int 41762306a36Sopenharmony_ci__devlink_region_snapshot_create(struct devlink_region *region, 41862306a36Sopenharmony_ci u8 *data, u32 snapshot_id) 41962306a36Sopenharmony_ci{ 42062306a36Sopenharmony_ci struct devlink *devlink = region->devlink; 42162306a36Sopenharmony_ci struct devlink_snapshot *snapshot; 42262306a36Sopenharmony_ci int err; 42362306a36Sopenharmony_ci 42462306a36Sopenharmony_ci lockdep_assert_held(®ion->snapshot_lock); 42562306a36Sopenharmony_ci 42662306a36Sopenharmony_ci /* check if region can hold one more snapshot */ 42762306a36Sopenharmony_ci if (region->cur_snapshots == region->max_snapshots) 42862306a36Sopenharmony_ci return -ENOSPC; 42962306a36Sopenharmony_ci 43062306a36Sopenharmony_ci if (devlink_region_snapshot_get_by_id(region, snapshot_id)) 43162306a36Sopenharmony_ci return -EEXIST; 43262306a36Sopenharmony_ci 43362306a36Sopenharmony_ci snapshot = kzalloc(sizeof(*snapshot), GFP_KERNEL); 43462306a36Sopenharmony_ci if (!snapshot) 43562306a36Sopenharmony_ci return -ENOMEM; 43662306a36Sopenharmony_ci 43762306a36Sopenharmony_ci err = __devlink_snapshot_id_increment(devlink, snapshot_id); 43862306a36Sopenharmony_ci if (err) 43962306a36Sopenharmony_ci goto err_snapshot_id_increment; 44062306a36Sopenharmony_ci 44162306a36Sopenharmony_ci snapshot->id = snapshot_id; 44262306a36Sopenharmony_ci snapshot->region = region; 44362306a36Sopenharmony_ci snapshot->data = data; 44462306a36Sopenharmony_ci 44562306a36Sopenharmony_ci list_add_tail(&snapshot->list, ®ion->snapshot_list); 44662306a36Sopenharmony_ci 44762306a36Sopenharmony_ci region->cur_snapshots++; 44862306a36Sopenharmony_ci 44962306a36Sopenharmony_ci devlink_nl_region_notify(region, snapshot, DEVLINK_CMD_REGION_NEW); 45062306a36Sopenharmony_ci return 0; 45162306a36Sopenharmony_ci 45262306a36Sopenharmony_cierr_snapshot_id_increment: 45362306a36Sopenharmony_ci kfree(snapshot); 45462306a36Sopenharmony_ci return err; 45562306a36Sopenharmony_ci} 45662306a36Sopenharmony_ci 45762306a36Sopenharmony_cistatic void devlink_region_snapshot_del(struct devlink_region *region, 45862306a36Sopenharmony_ci struct devlink_snapshot *snapshot) 45962306a36Sopenharmony_ci{ 46062306a36Sopenharmony_ci struct devlink *devlink = region->devlink; 46162306a36Sopenharmony_ci 46262306a36Sopenharmony_ci lockdep_assert_held(®ion->snapshot_lock); 46362306a36Sopenharmony_ci 46462306a36Sopenharmony_ci devlink_nl_region_notify(region, snapshot, DEVLINK_CMD_REGION_DEL); 46562306a36Sopenharmony_ci region->cur_snapshots--; 46662306a36Sopenharmony_ci list_del(&snapshot->list); 46762306a36Sopenharmony_ci region->ops->destructor(snapshot->data); 46862306a36Sopenharmony_ci __devlink_snapshot_id_decrement(devlink, snapshot->id); 46962306a36Sopenharmony_ci kfree(snapshot); 47062306a36Sopenharmony_ci} 47162306a36Sopenharmony_ci 47262306a36Sopenharmony_ciint devlink_nl_region_get_doit(struct sk_buff *skb, struct genl_info *info) 47362306a36Sopenharmony_ci{ 47462306a36Sopenharmony_ci struct devlink *devlink = info->user_ptr[0]; 47562306a36Sopenharmony_ci struct devlink_port *port = NULL; 47662306a36Sopenharmony_ci struct devlink_region *region; 47762306a36Sopenharmony_ci const char *region_name; 47862306a36Sopenharmony_ci struct sk_buff *msg; 47962306a36Sopenharmony_ci unsigned int index; 48062306a36Sopenharmony_ci int err; 48162306a36Sopenharmony_ci 48262306a36Sopenharmony_ci if (GENL_REQ_ATTR_CHECK(info, DEVLINK_ATTR_REGION_NAME)) 48362306a36Sopenharmony_ci return -EINVAL; 48462306a36Sopenharmony_ci 48562306a36Sopenharmony_ci if (info->attrs[DEVLINK_ATTR_PORT_INDEX]) { 48662306a36Sopenharmony_ci index = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_INDEX]); 48762306a36Sopenharmony_ci 48862306a36Sopenharmony_ci port = devlink_port_get_by_index(devlink, index); 48962306a36Sopenharmony_ci if (!port) 49062306a36Sopenharmony_ci return -ENODEV; 49162306a36Sopenharmony_ci } 49262306a36Sopenharmony_ci 49362306a36Sopenharmony_ci region_name = nla_data(info->attrs[DEVLINK_ATTR_REGION_NAME]); 49462306a36Sopenharmony_ci if (port) 49562306a36Sopenharmony_ci region = devlink_port_region_get_by_name(port, region_name); 49662306a36Sopenharmony_ci else 49762306a36Sopenharmony_ci region = devlink_region_get_by_name(devlink, region_name); 49862306a36Sopenharmony_ci 49962306a36Sopenharmony_ci if (!region) 50062306a36Sopenharmony_ci return -EINVAL; 50162306a36Sopenharmony_ci 50262306a36Sopenharmony_ci msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); 50362306a36Sopenharmony_ci if (!msg) 50462306a36Sopenharmony_ci return -ENOMEM; 50562306a36Sopenharmony_ci 50662306a36Sopenharmony_ci err = devlink_nl_region_fill(msg, devlink, DEVLINK_CMD_REGION_GET, 50762306a36Sopenharmony_ci info->snd_portid, info->snd_seq, 0, 50862306a36Sopenharmony_ci region); 50962306a36Sopenharmony_ci if (err) { 51062306a36Sopenharmony_ci nlmsg_free(msg); 51162306a36Sopenharmony_ci return err; 51262306a36Sopenharmony_ci } 51362306a36Sopenharmony_ci 51462306a36Sopenharmony_ci return genlmsg_reply(msg, info); 51562306a36Sopenharmony_ci} 51662306a36Sopenharmony_ci 51762306a36Sopenharmony_cistatic int devlink_nl_cmd_region_get_port_dumpit(struct sk_buff *msg, 51862306a36Sopenharmony_ci struct netlink_callback *cb, 51962306a36Sopenharmony_ci struct devlink_port *port, 52062306a36Sopenharmony_ci int *idx, int start, int flags) 52162306a36Sopenharmony_ci{ 52262306a36Sopenharmony_ci struct devlink_region *region; 52362306a36Sopenharmony_ci int err = 0; 52462306a36Sopenharmony_ci 52562306a36Sopenharmony_ci list_for_each_entry(region, &port->region_list, list) { 52662306a36Sopenharmony_ci if (*idx < start) { 52762306a36Sopenharmony_ci (*idx)++; 52862306a36Sopenharmony_ci continue; 52962306a36Sopenharmony_ci } 53062306a36Sopenharmony_ci err = devlink_nl_region_fill(msg, port->devlink, 53162306a36Sopenharmony_ci DEVLINK_CMD_REGION_GET, 53262306a36Sopenharmony_ci NETLINK_CB(cb->skb).portid, 53362306a36Sopenharmony_ci cb->nlh->nlmsg_seq, 53462306a36Sopenharmony_ci flags, region); 53562306a36Sopenharmony_ci if (err) 53662306a36Sopenharmony_ci goto out; 53762306a36Sopenharmony_ci (*idx)++; 53862306a36Sopenharmony_ci } 53962306a36Sopenharmony_ci 54062306a36Sopenharmony_ciout: 54162306a36Sopenharmony_ci return err; 54262306a36Sopenharmony_ci} 54362306a36Sopenharmony_ci 54462306a36Sopenharmony_cistatic int devlink_nl_region_get_dump_one(struct sk_buff *msg, 54562306a36Sopenharmony_ci struct devlink *devlink, 54662306a36Sopenharmony_ci struct netlink_callback *cb, 54762306a36Sopenharmony_ci int flags) 54862306a36Sopenharmony_ci{ 54962306a36Sopenharmony_ci struct devlink_nl_dump_state *state = devlink_dump_state(cb); 55062306a36Sopenharmony_ci struct devlink_region *region; 55162306a36Sopenharmony_ci struct devlink_port *port; 55262306a36Sopenharmony_ci unsigned long port_index; 55362306a36Sopenharmony_ci int idx = 0; 55462306a36Sopenharmony_ci int err; 55562306a36Sopenharmony_ci 55662306a36Sopenharmony_ci list_for_each_entry(region, &devlink->region_list, list) { 55762306a36Sopenharmony_ci if (idx < state->idx) { 55862306a36Sopenharmony_ci idx++; 55962306a36Sopenharmony_ci continue; 56062306a36Sopenharmony_ci } 56162306a36Sopenharmony_ci err = devlink_nl_region_fill(msg, devlink, 56262306a36Sopenharmony_ci DEVLINK_CMD_REGION_GET, 56362306a36Sopenharmony_ci NETLINK_CB(cb->skb).portid, 56462306a36Sopenharmony_ci cb->nlh->nlmsg_seq, flags, 56562306a36Sopenharmony_ci region); 56662306a36Sopenharmony_ci if (err) { 56762306a36Sopenharmony_ci state->idx = idx; 56862306a36Sopenharmony_ci return err; 56962306a36Sopenharmony_ci } 57062306a36Sopenharmony_ci idx++; 57162306a36Sopenharmony_ci } 57262306a36Sopenharmony_ci 57362306a36Sopenharmony_ci xa_for_each(&devlink->ports, port_index, port) { 57462306a36Sopenharmony_ci err = devlink_nl_cmd_region_get_port_dumpit(msg, cb, port, &idx, 57562306a36Sopenharmony_ci state->idx, flags); 57662306a36Sopenharmony_ci if (err) { 57762306a36Sopenharmony_ci state->idx = idx; 57862306a36Sopenharmony_ci return err; 57962306a36Sopenharmony_ci } 58062306a36Sopenharmony_ci } 58162306a36Sopenharmony_ci 58262306a36Sopenharmony_ci return 0; 58362306a36Sopenharmony_ci} 58462306a36Sopenharmony_ci 58562306a36Sopenharmony_ciint devlink_nl_region_get_dumpit(struct sk_buff *skb, 58662306a36Sopenharmony_ci struct netlink_callback *cb) 58762306a36Sopenharmony_ci{ 58862306a36Sopenharmony_ci return devlink_nl_dumpit(skb, cb, devlink_nl_region_get_dump_one); 58962306a36Sopenharmony_ci} 59062306a36Sopenharmony_ci 59162306a36Sopenharmony_ciint devlink_nl_cmd_region_del(struct sk_buff *skb, struct genl_info *info) 59262306a36Sopenharmony_ci{ 59362306a36Sopenharmony_ci struct devlink *devlink = info->user_ptr[0]; 59462306a36Sopenharmony_ci struct devlink_snapshot *snapshot; 59562306a36Sopenharmony_ci struct devlink_port *port = NULL; 59662306a36Sopenharmony_ci struct devlink_region *region; 59762306a36Sopenharmony_ci const char *region_name; 59862306a36Sopenharmony_ci unsigned int index; 59962306a36Sopenharmony_ci u32 snapshot_id; 60062306a36Sopenharmony_ci 60162306a36Sopenharmony_ci if (GENL_REQ_ATTR_CHECK(info, DEVLINK_ATTR_REGION_NAME) || 60262306a36Sopenharmony_ci GENL_REQ_ATTR_CHECK(info, DEVLINK_ATTR_REGION_SNAPSHOT_ID)) 60362306a36Sopenharmony_ci return -EINVAL; 60462306a36Sopenharmony_ci 60562306a36Sopenharmony_ci region_name = nla_data(info->attrs[DEVLINK_ATTR_REGION_NAME]); 60662306a36Sopenharmony_ci snapshot_id = nla_get_u32(info->attrs[DEVLINK_ATTR_REGION_SNAPSHOT_ID]); 60762306a36Sopenharmony_ci 60862306a36Sopenharmony_ci if (info->attrs[DEVLINK_ATTR_PORT_INDEX]) { 60962306a36Sopenharmony_ci index = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_INDEX]); 61062306a36Sopenharmony_ci 61162306a36Sopenharmony_ci port = devlink_port_get_by_index(devlink, index); 61262306a36Sopenharmony_ci if (!port) 61362306a36Sopenharmony_ci return -ENODEV; 61462306a36Sopenharmony_ci } 61562306a36Sopenharmony_ci 61662306a36Sopenharmony_ci if (port) 61762306a36Sopenharmony_ci region = devlink_port_region_get_by_name(port, region_name); 61862306a36Sopenharmony_ci else 61962306a36Sopenharmony_ci region = devlink_region_get_by_name(devlink, region_name); 62062306a36Sopenharmony_ci 62162306a36Sopenharmony_ci if (!region) 62262306a36Sopenharmony_ci return -EINVAL; 62362306a36Sopenharmony_ci 62462306a36Sopenharmony_ci mutex_lock(®ion->snapshot_lock); 62562306a36Sopenharmony_ci snapshot = devlink_region_snapshot_get_by_id(region, snapshot_id); 62662306a36Sopenharmony_ci if (!snapshot) { 62762306a36Sopenharmony_ci mutex_unlock(®ion->snapshot_lock); 62862306a36Sopenharmony_ci return -EINVAL; 62962306a36Sopenharmony_ci } 63062306a36Sopenharmony_ci 63162306a36Sopenharmony_ci devlink_region_snapshot_del(region, snapshot); 63262306a36Sopenharmony_ci mutex_unlock(®ion->snapshot_lock); 63362306a36Sopenharmony_ci return 0; 63462306a36Sopenharmony_ci} 63562306a36Sopenharmony_ci 63662306a36Sopenharmony_ciint devlink_nl_cmd_region_new(struct sk_buff *skb, struct genl_info *info) 63762306a36Sopenharmony_ci{ 63862306a36Sopenharmony_ci struct devlink *devlink = info->user_ptr[0]; 63962306a36Sopenharmony_ci struct devlink_snapshot *snapshot; 64062306a36Sopenharmony_ci struct devlink_port *port = NULL; 64162306a36Sopenharmony_ci struct nlattr *snapshot_id_attr; 64262306a36Sopenharmony_ci struct devlink_region *region; 64362306a36Sopenharmony_ci const char *region_name; 64462306a36Sopenharmony_ci unsigned int index; 64562306a36Sopenharmony_ci u32 snapshot_id; 64662306a36Sopenharmony_ci u8 *data; 64762306a36Sopenharmony_ci int err; 64862306a36Sopenharmony_ci 64962306a36Sopenharmony_ci if (GENL_REQ_ATTR_CHECK(info, DEVLINK_ATTR_REGION_NAME)) { 65062306a36Sopenharmony_ci NL_SET_ERR_MSG(info->extack, "No region name provided"); 65162306a36Sopenharmony_ci return -EINVAL; 65262306a36Sopenharmony_ci } 65362306a36Sopenharmony_ci 65462306a36Sopenharmony_ci region_name = nla_data(info->attrs[DEVLINK_ATTR_REGION_NAME]); 65562306a36Sopenharmony_ci 65662306a36Sopenharmony_ci if (info->attrs[DEVLINK_ATTR_PORT_INDEX]) { 65762306a36Sopenharmony_ci index = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_INDEX]); 65862306a36Sopenharmony_ci 65962306a36Sopenharmony_ci port = devlink_port_get_by_index(devlink, index); 66062306a36Sopenharmony_ci if (!port) 66162306a36Sopenharmony_ci return -ENODEV; 66262306a36Sopenharmony_ci } 66362306a36Sopenharmony_ci 66462306a36Sopenharmony_ci if (port) 66562306a36Sopenharmony_ci region = devlink_port_region_get_by_name(port, region_name); 66662306a36Sopenharmony_ci else 66762306a36Sopenharmony_ci region = devlink_region_get_by_name(devlink, region_name); 66862306a36Sopenharmony_ci 66962306a36Sopenharmony_ci if (!region) { 67062306a36Sopenharmony_ci NL_SET_ERR_MSG(info->extack, "The requested region does not exist"); 67162306a36Sopenharmony_ci return -EINVAL; 67262306a36Sopenharmony_ci } 67362306a36Sopenharmony_ci 67462306a36Sopenharmony_ci if (!region->ops->snapshot) { 67562306a36Sopenharmony_ci NL_SET_ERR_MSG(info->extack, "The requested region does not support taking an immediate snapshot"); 67662306a36Sopenharmony_ci return -EOPNOTSUPP; 67762306a36Sopenharmony_ci } 67862306a36Sopenharmony_ci 67962306a36Sopenharmony_ci mutex_lock(®ion->snapshot_lock); 68062306a36Sopenharmony_ci 68162306a36Sopenharmony_ci if (region->cur_snapshots == region->max_snapshots) { 68262306a36Sopenharmony_ci NL_SET_ERR_MSG(info->extack, "The region has reached the maximum number of stored snapshots"); 68362306a36Sopenharmony_ci err = -ENOSPC; 68462306a36Sopenharmony_ci goto unlock; 68562306a36Sopenharmony_ci } 68662306a36Sopenharmony_ci 68762306a36Sopenharmony_ci snapshot_id_attr = info->attrs[DEVLINK_ATTR_REGION_SNAPSHOT_ID]; 68862306a36Sopenharmony_ci if (snapshot_id_attr) { 68962306a36Sopenharmony_ci snapshot_id = nla_get_u32(snapshot_id_attr); 69062306a36Sopenharmony_ci 69162306a36Sopenharmony_ci if (devlink_region_snapshot_get_by_id(region, snapshot_id)) { 69262306a36Sopenharmony_ci NL_SET_ERR_MSG(info->extack, "The requested snapshot id is already in use"); 69362306a36Sopenharmony_ci err = -EEXIST; 69462306a36Sopenharmony_ci goto unlock; 69562306a36Sopenharmony_ci } 69662306a36Sopenharmony_ci 69762306a36Sopenharmony_ci err = __devlink_snapshot_id_insert(devlink, snapshot_id); 69862306a36Sopenharmony_ci if (err) 69962306a36Sopenharmony_ci goto unlock; 70062306a36Sopenharmony_ci } else { 70162306a36Sopenharmony_ci err = __devlink_region_snapshot_id_get(devlink, &snapshot_id); 70262306a36Sopenharmony_ci if (err) { 70362306a36Sopenharmony_ci NL_SET_ERR_MSG(info->extack, "Failed to allocate a new snapshot id"); 70462306a36Sopenharmony_ci goto unlock; 70562306a36Sopenharmony_ci } 70662306a36Sopenharmony_ci } 70762306a36Sopenharmony_ci 70862306a36Sopenharmony_ci if (port) 70962306a36Sopenharmony_ci err = region->port_ops->snapshot(port, region->port_ops, 71062306a36Sopenharmony_ci info->extack, &data); 71162306a36Sopenharmony_ci else 71262306a36Sopenharmony_ci err = region->ops->snapshot(devlink, region->ops, 71362306a36Sopenharmony_ci info->extack, &data); 71462306a36Sopenharmony_ci if (err) 71562306a36Sopenharmony_ci goto err_snapshot_capture; 71662306a36Sopenharmony_ci 71762306a36Sopenharmony_ci err = __devlink_region_snapshot_create(region, data, snapshot_id); 71862306a36Sopenharmony_ci if (err) 71962306a36Sopenharmony_ci goto err_snapshot_create; 72062306a36Sopenharmony_ci 72162306a36Sopenharmony_ci if (!snapshot_id_attr) { 72262306a36Sopenharmony_ci struct sk_buff *msg; 72362306a36Sopenharmony_ci 72462306a36Sopenharmony_ci snapshot = devlink_region_snapshot_get_by_id(region, 72562306a36Sopenharmony_ci snapshot_id); 72662306a36Sopenharmony_ci if (WARN_ON(!snapshot)) { 72762306a36Sopenharmony_ci err = -EINVAL; 72862306a36Sopenharmony_ci goto unlock; 72962306a36Sopenharmony_ci } 73062306a36Sopenharmony_ci 73162306a36Sopenharmony_ci msg = devlink_nl_region_notify_build(region, snapshot, 73262306a36Sopenharmony_ci DEVLINK_CMD_REGION_NEW, 73362306a36Sopenharmony_ci info->snd_portid, 73462306a36Sopenharmony_ci info->snd_seq); 73562306a36Sopenharmony_ci err = PTR_ERR_OR_ZERO(msg); 73662306a36Sopenharmony_ci if (err) 73762306a36Sopenharmony_ci goto err_notify; 73862306a36Sopenharmony_ci 73962306a36Sopenharmony_ci err = genlmsg_reply(msg, info); 74062306a36Sopenharmony_ci if (err) 74162306a36Sopenharmony_ci goto err_notify; 74262306a36Sopenharmony_ci } 74362306a36Sopenharmony_ci 74462306a36Sopenharmony_ci mutex_unlock(®ion->snapshot_lock); 74562306a36Sopenharmony_ci return 0; 74662306a36Sopenharmony_ci 74762306a36Sopenharmony_cierr_snapshot_create: 74862306a36Sopenharmony_ci region->ops->destructor(data); 74962306a36Sopenharmony_cierr_snapshot_capture: 75062306a36Sopenharmony_ci __devlink_snapshot_id_decrement(devlink, snapshot_id); 75162306a36Sopenharmony_ci mutex_unlock(®ion->snapshot_lock); 75262306a36Sopenharmony_ci return err; 75362306a36Sopenharmony_ci 75462306a36Sopenharmony_cierr_notify: 75562306a36Sopenharmony_ci devlink_region_snapshot_del(region, snapshot); 75662306a36Sopenharmony_ciunlock: 75762306a36Sopenharmony_ci mutex_unlock(®ion->snapshot_lock); 75862306a36Sopenharmony_ci return err; 75962306a36Sopenharmony_ci} 76062306a36Sopenharmony_ci 76162306a36Sopenharmony_cistatic int devlink_nl_cmd_region_read_chunk_fill(struct sk_buff *msg, 76262306a36Sopenharmony_ci u8 *chunk, u32 chunk_size, 76362306a36Sopenharmony_ci u64 addr) 76462306a36Sopenharmony_ci{ 76562306a36Sopenharmony_ci struct nlattr *chunk_attr; 76662306a36Sopenharmony_ci int err; 76762306a36Sopenharmony_ci 76862306a36Sopenharmony_ci chunk_attr = nla_nest_start_noflag(msg, DEVLINK_ATTR_REGION_CHUNK); 76962306a36Sopenharmony_ci if (!chunk_attr) 77062306a36Sopenharmony_ci return -EINVAL; 77162306a36Sopenharmony_ci 77262306a36Sopenharmony_ci err = nla_put(msg, DEVLINK_ATTR_REGION_CHUNK_DATA, chunk_size, chunk); 77362306a36Sopenharmony_ci if (err) 77462306a36Sopenharmony_ci goto nla_put_failure; 77562306a36Sopenharmony_ci 77662306a36Sopenharmony_ci err = nla_put_u64_64bit(msg, DEVLINK_ATTR_REGION_CHUNK_ADDR, addr, 77762306a36Sopenharmony_ci DEVLINK_ATTR_PAD); 77862306a36Sopenharmony_ci if (err) 77962306a36Sopenharmony_ci goto nla_put_failure; 78062306a36Sopenharmony_ci 78162306a36Sopenharmony_ci nla_nest_end(msg, chunk_attr); 78262306a36Sopenharmony_ci return 0; 78362306a36Sopenharmony_ci 78462306a36Sopenharmony_cinla_put_failure: 78562306a36Sopenharmony_ci nla_nest_cancel(msg, chunk_attr); 78662306a36Sopenharmony_ci return err; 78762306a36Sopenharmony_ci} 78862306a36Sopenharmony_ci 78962306a36Sopenharmony_ci#define DEVLINK_REGION_READ_CHUNK_SIZE 256 79062306a36Sopenharmony_ci 79162306a36Sopenharmony_citypedef int devlink_chunk_fill_t(void *cb_priv, u8 *chunk, u32 chunk_size, 79262306a36Sopenharmony_ci u64 curr_offset, 79362306a36Sopenharmony_ci struct netlink_ext_ack *extack); 79462306a36Sopenharmony_ci 79562306a36Sopenharmony_cistatic int 79662306a36Sopenharmony_cidevlink_nl_region_read_fill(struct sk_buff *skb, devlink_chunk_fill_t *cb, 79762306a36Sopenharmony_ci void *cb_priv, u64 start_offset, u64 end_offset, 79862306a36Sopenharmony_ci u64 *new_offset, struct netlink_ext_ack *extack) 79962306a36Sopenharmony_ci{ 80062306a36Sopenharmony_ci u64 curr_offset = start_offset; 80162306a36Sopenharmony_ci int err = 0; 80262306a36Sopenharmony_ci u8 *data; 80362306a36Sopenharmony_ci 80462306a36Sopenharmony_ci /* Allocate and re-use a single buffer */ 80562306a36Sopenharmony_ci data = kmalloc(DEVLINK_REGION_READ_CHUNK_SIZE, GFP_KERNEL); 80662306a36Sopenharmony_ci if (!data) 80762306a36Sopenharmony_ci return -ENOMEM; 80862306a36Sopenharmony_ci 80962306a36Sopenharmony_ci *new_offset = start_offset; 81062306a36Sopenharmony_ci 81162306a36Sopenharmony_ci while (curr_offset < end_offset) { 81262306a36Sopenharmony_ci u32 data_size; 81362306a36Sopenharmony_ci 81462306a36Sopenharmony_ci data_size = min_t(u32, end_offset - curr_offset, 81562306a36Sopenharmony_ci DEVLINK_REGION_READ_CHUNK_SIZE); 81662306a36Sopenharmony_ci 81762306a36Sopenharmony_ci err = cb(cb_priv, data, data_size, curr_offset, extack); 81862306a36Sopenharmony_ci if (err) 81962306a36Sopenharmony_ci break; 82062306a36Sopenharmony_ci 82162306a36Sopenharmony_ci err = devlink_nl_cmd_region_read_chunk_fill(skb, data, data_size, curr_offset); 82262306a36Sopenharmony_ci if (err) 82362306a36Sopenharmony_ci break; 82462306a36Sopenharmony_ci 82562306a36Sopenharmony_ci curr_offset += data_size; 82662306a36Sopenharmony_ci } 82762306a36Sopenharmony_ci *new_offset = curr_offset; 82862306a36Sopenharmony_ci 82962306a36Sopenharmony_ci kfree(data); 83062306a36Sopenharmony_ci 83162306a36Sopenharmony_ci return err; 83262306a36Sopenharmony_ci} 83362306a36Sopenharmony_ci 83462306a36Sopenharmony_cistatic int 83562306a36Sopenharmony_cidevlink_region_snapshot_fill(void *cb_priv, u8 *chunk, u32 chunk_size, 83662306a36Sopenharmony_ci u64 curr_offset, 83762306a36Sopenharmony_ci struct netlink_ext_ack __always_unused *extack) 83862306a36Sopenharmony_ci{ 83962306a36Sopenharmony_ci struct devlink_snapshot *snapshot = cb_priv; 84062306a36Sopenharmony_ci 84162306a36Sopenharmony_ci memcpy(chunk, &snapshot->data[curr_offset], chunk_size); 84262306a36Sopenharmony_ci 84362306a36Sopenharmony_ci return 0; 84462306a36Sopenharmony_ci} 84562306a36Sopenharmony_ci 84662306a36Sopenharmony_cistatic int 84762306a36Sopenharmony_cidevlink_region_port_direct_fill(void *cb_priv, u8 *chunk, u32 chunk_size, 84862306a36Sopenharmony_ci u64 curr_offset, struct netlink_ext_ack *extack) 84962306a36Sopenharmony_ci{ 85062306a36Sopenharmony_ci struct devlink_region *region = cb_priv; 85162306a36Sopenharmony_ci 85262306a36Sopenharmony_ci return region->port_ops->read(region->port, region->port_ops, extack, 85362306a36Sopenharmony_ci curr_offset, chunk_size, chunk); 85462306a36Sopenharmony_ci} 85562306a36Sopenharmony_ci 85662306a36Sopenharmony_cistatic int 85762306a36Sopenharmony_cidevlink_region_direct_fill(void *cb_priv, u8 *chunk, u32 chunk_size, 85862306a36Sopenharmony_ci u64 curr_offset, struct netlink_ext_ack *extack) 85962306a36Sopenharmony_ci{ 86062306a36Sopenharmony_ci struct devlink_region *region = cb_priv; 86162306a36Sopenharmony_ci 86262306a36Sopenharmony_ci return region->ops->read(region->devlink, region->ops, extack, 86362306a36Sopenharmony_ci curr_offset, chunk_size, chunk); 86462306a36Sopenharmony_ci} 86562306a36Sopenharmony_ci 86662306a36Sopenharmony_ciint devlink_nl_cmd_region_read_dumpit(struct sk_buff *skb, 86762306a36Sopenharmony_ci struct netlink_callback *cb) 86862306a36Sopenharmony_ci{ 86962306a36Sopenharmony_ci const struct genl_dumpit_info *info = genl_dumpit_info(cb); 87062306a36Sopenharmony_ci struct devlink_nl_dump_state *state = devlink_dump_state(cb); 87162306a36Sopenharmony_ci struct nlattr *chunks_attr, *region_attr, *snapshot_attr; 87262306a36Sopenharmony_ci u64 ret_offset, start_offset, end_offset = U64_MAX; 87362306a36Sopenharmony_ci struct nlattr **attrs = info->info.attrs; 87462306a36Sopenharmony_ci struct devlink_port *port = NULL; 87562306a36Sopenharmony_ci devlink_chunk_fill_t *region_cb; 87662306a36Sopenharmony_ci struct devlink_region *region; 87762306a36Sopenharmony_ci const char *region_name; 87862306a36Sopenharmony_ci struct devlink *devlink; 87962306a36Sopenharmony_ci unsigned int index; 88062306a36Sopenharmony_ci void *region_cb_priv; 88162306a36Sopenharmony_ci void *hdr; 88262306a36Sopenharmony_ci int err; 88362306a36Sopenharmony_ci 88462306a36Sopenharmony_ci start_offset = state->start_offset; 88562306a36Sopenharmony_ci 88662306a36Sopenharmony_ci devlink = devlink_get_from_attrs_lock(sock_net(cb->skb->sk), attrs); 88762306a36Sopenharmony_ci if (IS_ERR(devlink)) 88862306a36Sopenharmony_ci return PTR_ERR(devlink); 88962306a36Sopenharmony_ci 89062306a36Sopenharmony_ci if (!attrs[DEVLINK_ATTR_REGION_NAME]) { 89162306a36Sopenharmony_ci NL_SET_ERR_MSG(cb->extack, "No region name provided"); 89262306a36Sopenharmony_ci err = -EINVAL; 89362306a36Sopenharmony_ci goto out_unlock; 89462306a36Sopenharmony_ci } 89562306a36Sopenharmony_ci 89662306a36Sopenharmony_ci if (attrs[DEVLINK_ATTR_PORT_INDEX]) { 89762306a36Sopenharmony_ci index = nla_get_u32(attrs[DEVLINK_ATTR_PORT_INDEX]); 89862306a36Sopenharmony_ci 89962306a36Sopenharmony_ci port = devlink_port_get_by_index(devlink, index); 90062306a36Sopenharmony_ci if (!port) { 90162306a36Sopenharmony_ci err = -ENODEV; 90262306a36Sopenharmony_ci goto out_unlock; 90362306a36Sopenharmony_ci } 90462306a36Sopenharmony_ci } 90562306a36Sopenharmony_ci 90662306a36Sopenharmony_ci region_attr = attrs[DEVLINK_ATTR_REGION_NAME]; 90762306a36Sopenharmony_ci region_name = nla_data(region_attr); 90862306a36Sopenharmony_ci 90962306a36Sopenharmony_ci if (port) 91062306a36Sopenharmony_ci region = devlink_port_region_get_by_name(port, region_name); 91162306a36Sopenharmony_ci else 91262306a36Sopenharmony_ci region = devlink_region_get_by_name(devlink, region_name); 91362306a36Sopenharmony_ci 91462306a36Sopenharmony_ci if (!region) { 91562306a36Sopenharmony_ci NL_SET_ERR_MSG_ATTR(cb->extack, region_attr, "Requested region does not exist"); 91662306a36Sopenharmony_ci err = -EINVAL; 91762306a36Sopenharmony_ci goto out_unlock; 91862306a36Sopenharmony_ci } 91962306a36Sopenharmony_ci 92062306a36Sopenharmony_ci snapshot_attr = attrs[DEVLINK_ATTR_REGION_SNAPSHOT_ID]; 92162306a36Sopenharmony_ci if (!snapshot_attr) { 92262306a36Sopenharmony_ci if (!nla_get_flag(attrs[DEVLINK_ATTR_REGION_DIRECT])) { 92362306a36Sopenharmony_ci NL_SET_ERR_MSG(cb->extack, "No snapshot id provided"); 92462306a36Sopenharmony_ci err = -EINVAL; 92562306a36Sopenharmony_ci goto out_unlock; 92662306a36Sopenharmony_ci } 92762306a36Sopenharmony_ci 92862306a36Sopenharmony_ci if (!region->ops->read) { 92962306a36Sopenharmony_ci NL_SET_ERR_MSG(cb->extack, "Requested region does not support direct read"); 93062306a36Sopenharmony_ci err = -EOPNOTSUPP; 93162306a36Sopenharmony_ci goto out_unlock; 93262306a36Sopenharmony_ci } 93362306a36Sopenharmony_ci 93462306a36Sopenharmony_ci if (port) 93562306a36Sopenharmony_ci region_cb = &devlink_region_port_direct_fill; 93662306a36Sopenharmony_ci else 93762306a36Sopenharmony_ci region_cb = &devlink_region_direct_fill; 93862306a36Sopenharmony_ci region_cb_priv = region; 93962306a36Sopenharmony_ci } else { 94062306a36Sopenharmony_ci struct devlink_snapshot *snapshot; 94162306a36Sopenharmony_ci u32 snapshot_id; 94262306a36Sopenharmony_ci 94362306a36Sopenharmony_ci if (nla_get_flag(attrs[DEVLINK_ATTR_REGION_DIRECT])) { 94462306a36Sopenharmony_ci NL_SET_ERR_MSG_ATTR(cb->extack, snapshot_attr, "Direct region read does not use snapshot"); 94562306a36Sopenharmony_ci err = -EINVAL; 94662306a36Sopenharmony_ci goto out_unlock; 94762306a36Sopenharmony_ci } 94862306a36Sopenharmony_ci 94962306a36Sopenharmony_ci snapshot_id = nla_get_u32(snapshot_attr); 95062306a36Sopenharmony_ci snapshot = devlink_region_snapshot_get_by_id(region, snapshot_id); 95162306a36Sopenharmony_ci if (!snapshot) { 95262306a36Sopenharmony_ci NL_SET_ERR_MSG_ATTR(cb->extack, snapshot_attr, "Requested snapshot does not exist"); 95362306a36Sopenharmony_ci err = -EINVAL; 95462306a36Sopenharmony_ci goto out_unlock; 95562306a36Sopenharmony_ci } 95662306a36Sopenharmony_ci region_cb = &devlink_region_snapshot_fill; 95762306a36Sopenharmony_ci region_cb_priv = snapshot; 95862306a36Sopenharmony_ci } 95962306a36Sopenharmony_ci 96062306a36Sopenharmony_ci if (attrs[DEVLINK_ATTR_REGION_CHUNK_ADDR] && 96162306a36Sopenharmony_ci attrs[DEVLINK_ATTR_REGION_CHUNK_LEN]) { 96262306a36Sopenharmony_ci if (!start_offset) 96362306a36Sopenharmony_ci start_offset = 96462306a36Sopenharmony_ci nla_get_u64(attrs[DEVLINK_ATTR_REGION_CHUNK_ADDR]); 96562306a36Sopenharmony_ci 96662306a36Sopenharmony_ci end_offset = nla_get_u64(attrs[DEVLINK_ATTR_REGION_CHUNK_ADDR]); 96762306a36Sopenharmony_ci end_offset += nla_get_u64(attrs[DEVLINK_ATTR_REGION_CHUNK_LEN]); 96862306a36Sopenharmony_ci } 96962306a36Sopenharmony_ci 97062306a36Sopenharmony_ci if (end_offset > region->size) 97162306a36Sopenharmony_ci end_offset = region->size; 97262306a36Sopenharmony_ci 97362306a36Sopenharmony_ci /* return 0 if there is no further data to read */ 97462306a36Sopenharmony_ci if (start_offset == end_offset) { 97562306a36Sopenharmony_ci err = 0; 97662306a36Sopenharmony_ci goto out_unlock; 97762306a36Sopenharmony_ci } 97862306a36Sopenharmony_ci 97962306a36Sopenharmony_ci hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq, 98062306a36Sopenharmony_ci &devlink_nl_family, NLM_F_ACK | NLM_F_MULTI, 98162306a36Sopenharmony_ci DEVLINK_CMD_REGION_READ); 98262306a36Sopenharmony_ci if (!hdr) { 98362306a36Sopenharmony_ci err = -EMSGSIZE; 98462306a36Sopenharmony_ci goto out_unlock; 98562306a36Sopenharmony_ci } 98662306a36Sopenharmony_ci 98762306a36Sopenharmony_ci err = devlink_nl_put_handle(skb, devlink); 98862306a36Sopenharmony_ci if (err) 98962306a36Sopenharmony_ci goto nla_put_failure; 99062306a36Sopenharmony_ci 99162306a36Sopenharmony_ci if (region->port) { 99262306a36Sopenharmony_ci err = nla_put_u32(skb, DEVLINK_ATTR_PORT_INDEX, 99362306a36Sopenharmony_ci region->port->index); 99462306a36Sopenharmony_ci if (err) 99562306a36Sopenharmony_ci goto nla_put_failure; 99662306a36Sopenharmony_ci } 99762306a36Sopenharmony_ci 99862306a36Sopenharmony_ci err = nla_put_string(skb, DEVLINK_ATTR_REGION_NAME, region_name); 99962306a36Sopenharmony_ci if (err) 100062306a36Sopenharmony_ci goto nla_put_failure; 100162306a36Sopenharmony_ci 100262306a36Sopenharmony_ci chunks_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_REGION_CHUNKS); 100362306a36Sopenharmony_ci if (!chunks_attr) { 100462306a36Sopenharmony_ci err = -EMSGSIZE; 100562306a36Sopenharmony_ci goto nla_put_failure; 100662306a36Sopenharmony_ci } 100762306a36Sopenharmony_ci 100862306a36Sopenharmony_ci err = devlink_nl_region_read_fill(skb, region_cb, region_cb_priv, 100962306a36Sopenharmony_ci start_offset, end_offset, &ret_offset, 101062306a36Sopenharmony_ci cb->extack); 101162306a36Sopenharmony_ci 101262306a36Sopenharmony_ci if (err && err != -EMSGSIZE) 101362306a36Sopenharmony_ci goto nla_put_failure; 101462306a36Sopenharmony_ci 101562306a36Sopenharmony_ci /* Check if there was any progress done to prevent infinite loop */ 101662306a36Sopenharmony_ci if (ret_offset == start_offset) { 101762306a36Sopenharmony_ci err = -EINVAL; 101862306a36Sopenharmony_ci goto nla_put_failure; 101962306a36Sopenharmony_ci } 102062306a36Sopenharmony_ci 102162306a36Sopenharmony_ci state->start_offset = ret_offset; 102262306a36Sopenharmony_ci 102362306a36Sopenharmony_ci nla_nest_end(skb, chunks_attr); 102462306a36Sopenharmony_ci genlmsg_end(skb, hdr); 102562306a36Sopenharmony_ci devl_unlock(devlink); 102662306a36Sopenharmony_ci devlink_put(devlink); 102762306a36Sopenharmony_ci return skb->len; 102862306a36Sopenharmony_ci 102962306a36Sopenharmony_cinla_put_failure: 103062306a36Sopenharmony_ci genlmsg_cancel(skb, hdr); 103162306a36Sopenharmony_ciout_unlock: 103262306a36Sopenharmony_ci devl_unlock(devlink); 103362306a36Sopenharmony_ci devlink_put(devlink); 103462306a36Sopenharmony_ci return err; 103562306a36Sopenharmony_ci} 103662306a36Sopenharmony_ci 103762306a36Sopenharmony_ci/** 103862306a36Sopenharmony_ci * devl_region_create - create a new address region 103962306a36Sopenharmony_ci * 104062306a36Sopenharmony_ci * @devlink: devlink 104162306a36Sopenharmony_ci * @ops: region operations and name 104262306a36Sopenharmony_ci * @region_max_snapshots: Maximum supported number of snapshots for region 104362306a36Sopenharmony_ci * @region_size: size of region 104462306a36Sopenharmony_ci */ 104562306a36Sopenharmony_cistruct devlink_region *devl_region_create(struct devlink *devlink, 104662306a36Sopenharmony_ci const struct devlink_region_ops *ops, 104762306a36Sopenharmony_ci u32 region_max_snapshots, 104862306a36Sopenharmony_ci u64 region_size) 104962306a36Sopenharmony_ci{ 105062306a36Sopenharmony_ci struct devlink_region *region; 105162306a36Sopenharmony_ci 105262306a36Sopenharmony_ci devl_assert_locked(devlink); 105362306a36Sopenharmony_ci 105462306a36Sopenharmony_ci if (WARN_ON(!ops) || WARN_ON(!ops->destructor)) 105562306a36Sopenharmony_ci return ERR_PTR(-EINVAL); 105662306a36Sopenharmony_ci 105762306a36Sopenharmony_ci if (devlink_region_get_by_name(devlink, ops->name)) 105862306a36Sopenharmony_ci return ERR_PTR(-EEXIST); 105962306a36Sopenharmony_ci 106062306a36Sopenharmony_ci region = kzalloc(sizeof(*region), GFP_KERNEL); 106162306a36Sopenharmony_ci if (!region) 106262306a36Sopenharmony_ci return ERR_PTR(-ENOMEM); 106362306a36Sopenharmony_ci 106462306a36Sopenharmony_ci region->devlink = devlink; 106562306a36Sopenharmony_ci region->max_snapshots = region_max_snapshots; 106662306a36Sopenharmony_ci region->ops = ops; 106762306a36Sopenharmony_ci region->size = region_size; 106862306a36Sopenharmony_ci INIT_LIST_HEAD(®ion->snapshot_list); 106962306a36Sopenharmony_ci mutex_init(®ion->snapshot_lock); 107062306a36Sopenharmony_ci list_add_tail(®ion->list, &devlink->region_list); 107162306a36Sopenharmony_ci devlink_nl_region_notify(region, NULL, DEVLINK_CMD_REGION_NEW); 107262306a36Sopenharmony_ci 107362306a36Sopenharmony_ci return region; 107462306a36Sopenharmony_ci} 107562306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(devl_region_create); 107662306a36Sopenharmony_ci 107762306a36Sopenharmony_ci/** 107862306a36Sopenharmony_ci * devlink_region_create - create a new address region 107962306a36Sopenharmony_ci * 108062306a36Sopenharmony_ci * @devlink: devlink 108162306a36Sopenharmony_ci * @ops: region operations and name 108262306a36Sopenharmony_ci * @region_max_snapshots: Maximum supported number of snapshots for region 108362306a36Sopenharmony_ci * @region_size: size of region 108462306a36Sopenharmony_ci * 108562306a36Sopenharmony_ci * Context: Takes and release devlink->lock <mutex>. 108662306a36Sopenharmony_ci */ 108762306a36Sopenharmony_cistruct devlink_region * 108862306a36Sopenharmony_cidevlink_region_create(struct devlink *devlink, 108962306a36Sopenharmony_ci const struct devlink_region_ops *ops, 109062306a36Sopenharmony_ci u32 region_max_snapshots, u64 region_size) 109162306a36Sopenharmony_ci{ 109262306a36Sopenharmony_ci struct devlink_region *region; 109362306a36Sopenharmony_ci 109462306a36Sopenharmony_ci devl_lock(devlink); 109562306a36Sopenharmony_ci region = devl_region_create(devlink, ops, region_max_snapshots, 109662306a36Sopenharmony_ci region_size); 109762306a36Sopenharmony_ci devl_unlock(devlink); 109862306a36Sopenharmony_ci return region; 109962306a36Sopenharmony_ci} 110062306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(devlink_region_create); 110162306a36Sopenharmony_ci 110262306a36Sopenharmony_ci/** 110362306a36Sopenharmony_ci * devlink_port_region_create - create a new address region for a port 110462306a36Sopenharmony_ci * 110562306a36Sopenharmony_ci * @port: devlink port 110662306a36Sopenharmony_ci * @ops: region operations and name 110762306a36Sopenharmony_ci * @region_max_snapshots: Maximum supported number of snapshots for region 110862306a36Sopenharmony_ci * @region_size: size of region 110962306a36Sopenharmony_ci * 111062306a36Sopenharmony_ci * Context: Takes and release devlink->lock <mutex>. 111162306a36Sopenharmony_ci */ 111262306a36Sopenharmony_cistruct devlink_region * 111362306a36Sopenharmony_cidevlink_port_region_create(struct devlink_port *port, 111462306a36Sopenharmony_ci const struct devlink_port_region_ops *ops, 111562306a36Sopenharmony_ci u32 region_max_snapshots, u64 region_size) 111662306a36Sopenharmony_ci{ 111762306a36Sopenharmony_ci struct devlink *devlink = port->devlink; 111862306a36Sopenharmony_ci struct devlink_region *region; 111962306a36Sopenharmony_ci int err = 0; 112062306a36Sopenharmony_ci 112162306a36Sopenharmony_ci ASSERT_DEVLINK_PORT_INITIALIZED(port); 112262306a36Sopenharmony_ci 112362306a36Sopenharmony_ci if (WARN_ON(!ops) || WARN_ON(!ops->destructor)) 112462306a36Sopenharmony_ci return ERR_PTR(-EINVAL); 112562306a36Sopenharmony_ci 112662306a36Sopenharmony_ci devl_lock(devlink); 112762306a36Sopenharmony_ci 112862306a36Sopenharmony_ci if (devlink_port_region_get_by_name(port, ops->name)) { 112962306a36Sopenharmony_ci err = -EEXIST; 113062306a36Sopenharmony_ci goto unlock; 113162306a36Sopenharmony_ci } 113262306a36Sopenharmony_ci 113362306a36Sopenharmony_ci region = kzalloc(sizeof(*region), GFP_KERNEL); 113462306a36Sopenharmony_ci if (!region) { 113562306a36Sopenharmony_ci err = -ENOMEM; 113662306a36Sopenharmony_ci goto unlock; 113762306a36Sopenharmony_ci } 113862306a36Sopenharmony_ci 113962306a36Sopenharmony_ci region->devlink = devlink; 114062306a36Sopenharmony_ci region->port = port; 114162306a36Sopenharmony_ci region->max_snapshots = region_max_snapshots; 114262306a36Sopenharmony_ci region->port_ops = ops; 114362306a36Sopenharmony_ci region->size = region_size; 114462306a36Sopenharmony_ci INIT_LIST_HEAD(®ion->snapshot_list); 114562306a36Sopenharmony_ci mutex_init(®ion->snapshot_lock); 114662306a36Sopenharmony_ci list_add_tail(®ion->list, &port->region_list); 114762306a36Sopenharmony_ci devlink_nl_region_notify(region, NULL, DEVLINK_CMD_REGION_NEW); 114862306a36Sopenharmony_ci 114962306a36Sopenharmony_ci devl_unlock(devlink); 115062306a36Sopenharmony_ci return region; 115162306a36Sopenharmony_ci 115262306a36Sopenharmony_ciunlock: 115362306a36Sopenharmony_ci devl_unlock(devlink); 115462306a36Sopenharmony_ci return ERR_PTR(err); 115562306a36Sopenharmony_ci} 115662306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(devlink_port_region_create); 115762306a36Sopenharmony_ci 115862306a36Sopenharmony_ci/** 115962306a36Sopenharmony_ci * devl_region_destroy - destroy address region 116062306a36Sopenharmony_ci * 116162306a36Sopenharmony_ci * @region: devlink region to destroy 116262306a36Sopenharmony_ci */ 116362306a36Sopenharmony_civoid devl_region_destroy(struct devlink_region *region) 116462306a36Sopenharmony_ci{ 116562306a36Sopenharmony_ci struct devlink *devlink = region->devlink; 116662306a36Sopenharmony_ci struct devlink_snapshot *snapshot, *ts; 116762306a36Sopenharmony_ci 116862306a36Sopenharmony_ci devl_assert_locked(devlink); 116962306a36Sopenharmony_ci 117062306a36Sopenharmony_ci /* Free all snapshots of region */ 117162306a36Sopenharmony_ci mutex_lock(®ion->snapshot_lock); 117262306a36Sopenharmony_ci list_for_each_entry_safe(snapshot, ts, ®ion->snapshot_list, list) 117362306a36Sopenharmony_ci devlink_region_snapshot_del(region, snapshot); 117462306a36Sopenharmony_ci mutex_unlock(®ion->snapshot_lock); 117562306a36Sopenharmony_ci 117662306a36Sopenharmony_ci list_del(®ion->list); 117762306a36Sopenharmony_ci mutex_destroy(®ion->snapshot_lock); 117862306a36Sopenharmony_ci 117962306a36Sopenharmony_ci devlink_nl_region_notify(region, NULL, DEVLINK_CMD_REGION_DEL); 118062306a36Sopenharmony_ci kfree(region); 118162306a36Sopenharmony_ci} 118262306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(devl_region_destroy); 118362306a36Sopenharmony_ci 118462306a36Sopenharmony_ci/** 118562306a36Sopenharmony_ci * devlink_region_destroy - destroy address region 118662306a36Sopenharmony_ci * 118762306a36Sopenharmony_ci * @region: devlink region to destroy 118862306a36Sopenharmony_ci * 118962306a36Sopenharmony_ci * Context: Takes and release devlink->lock <mutex>. 119062306a36Sopenharmony_ci */ 119162306a36Sopenharmony_civoid devlink_region_destroy(struct devlink_region *region) 119262306a36Sopenharmony_ci{ 119362306a36Sopenharmony_ci struct devlink *devlink = region->devlink; 119462306a36Sopenharmony_ci 119562306a36Sopenharmony_ci devl_lock(devlink); 119662306a36Sopenharmony_ci devl_region_destroy(region); 119762306a36Sopenharmony_ci devl_unlock(devlink); 119862306a36Sopenharmony_ci} 119962306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(devlink_region_destroy); 120062306a36Sopenharmony_ci 120162306a36Sopenharmony_ci/** 120262306a36Sopenharmony_ci * devlink_region_snapshot_id_get - get snapshot ID 120362306a36Sopenharmony_ci * 120462306a36Sopenharmony_ci * This callback should be called when adding a new snapshot, 120562306a36Sopenharmony_ci * Driver should use the same id for multiple snapshots taken 120662306a36Sopenharmony_ci * on multiple regions at the same time/by the same trigger. 120762306a36Sopenharmony_ci * 120862306a36Sopenharmony_ci * The caller of this function must use devlink_region_snapshot_id_put 120962306a36Sopenharmony_ci * when finished creating regions using this id. 121062306a36Sopenharmony_ci * 121162306a36Sopenharmony_ci * Returns zero on success, or a negative error code on failure. 121262306a36Sopenharmony_ci * 121362306a36Sopenharmony_ci * @devlink: devlink 121462306a36Sopenharmony_ci * @id: storage to return id 121562306a36Sopenharmony_ci */ 121662306a36Sopenharmony_ciint devlink_region_snapshot_id_get(struct devlink *devlink, u32 *id) 121762306a36Sopenharmony_ci{ 121862306a36Sopenharmony_ci return __devlink_region_snapshot_id_get(devlink, id); 121962306a36Sopenharmony_ci} 122062306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(devlink_region_snapshot_id_get); 122162306a36Sopenharmony_ci 122262306a36Sopenharmony_ci/** 122362306a36Sopenharmony_ci * devlink_region_snapshot_id_put - put snapshot ID reference 122462306a36Sopenharmony_ci * 122562306a36Sopenharmony_ci * This should be called by a driver after finishing creating snapshots 122662306a36Sopenharmony_ci * with an id. Doing so ensures that the ID can later be released in the 122762306a36Sopenharmony_ci * event that all snapshots using it have been destroyed. 122862306a36Sopenharmony_ci * 122962306a36Sopenharmony_ci * @devlink: devlink 123062306a36Sopenharmony_ci * @id: id to release reference on 123162306a36Sopenharmony_ci */ 123262306a36Sopenharmony_civoid devlink_region_snapshot_id_put(struct devlink *devlink, u32 id) 123362306a36Sopenharmony_ci{ 123462306a36Sopenharmony_ci __devlink_snapshot_id_decrement(devlink, id); 123562306a36Sopenharmony_ci} 123662306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(devlink_region_snapshot_id_put); 123762306a36Sopenharmony_ci 123862306a36Sopenharmony_ci/** 123962306a36Sopenharmony_ci * devlink_region_snapshot_create - create a new snapshot 124062306a36Sopenharmony_ci * This will add a new snapshot of a region. The snapshot 124162306a36Sopenharmony_ci * will be stored on the region struct and can be accessed 124262306a36Sopenharmony_ci * from devlink. This is useful for future analyses of snapshots. 124362306a36Sopenharmony_ci * Multiple snapshots can be created on a region. 124462306a36Sopenharmony_ci * The @snapshot_id should be obtained using the getter function. 124562306a36Sopenharmony_ci * 124662306a36Sopenharmony_ci * @region: devlink region of the snapshot 124762306a36Sopenharmony_ci * @data: snapshot data 124862306a36Sopenharmony_ci * @snapshot_id: snapshot id to be created 124962306a36Sopenharmony_ci */ 125062306a36Sopenharmony_ciint devlink_region_snapshot_create(struct devlink_region *region, 125162306a36Sopenharmony_ci u8 *data, u32 snapshot_id) 125262306a36Sopenharmony_ci{ 125362306a36Sopenharmony_ci int err; 125462306a36Sopenharmony_ci 125562306a36Sopenharmony_ci mutex_lock(®ion->snapshot_lock); 125662306a36Sopenharmony_ci err = __devlink_region_snapshot_create(region, data, snapshot_id); 125762306a36Sopenharmony_ci mutex_unlock(®ion->snapshot_lock); 125862306a36Sopenharmony_ci return err; 125962306a36Sopenharmony_ci} 126062306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(devlink_region_snapshot_create); 1261