| @@ -0,0 +1,100 @@ |
| | 1 | // SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB |
| | 2 | /* |
| | 3 | * Copyright (c) 2018, Mellanox Technologies inc. All rights reserved. |
| | 4 | * |
| | 5 | * This software is available to you under a choice of one of two |
| | 6 | * licenses. You may choose to be licensed under the terms of the GNU |
| | 7 | * General Public License (GPL) Version 2, available from the file |
| | 8 | * COPYING in the main directory of this source tree, or the |
| | 9 | * OpenIB.org BSD license below: |
| | 10 | * |
| | 11 | * Redistribution and use in source and binary forms, with or |
| | 12 | * without modification, are permitted provided that the following |
| | 13 | * conditions are met: |
| | 14 | * |
| | 15 | * - Redistributions of source code must retain the above |
| | 16 | * copyright notice, this list of conditions and the following |
| | 17 | * disclaimer. |
| | 18 | * |
| | 19 | * - Redistributions in binary form must reproduce the above |
| | 20 | * copyright notice, this list of conditions and the following |
| | 21 | * disclaimer in the documentation and/or other materials |
| | 22 | * provided with the distribution. |
| | 23 | * |
| | 24 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| | 25 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| | 26 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| | 27 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS |
| | 28 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
| | 29 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| | 30 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| | 31 | * SOFTWARE. |
| | 32 | */ |
| | 33 | |
| | 34 | #include "uverbs.h" |
| | 35 | #include <rdma/uverbs_std_types.h> |
| | 36 | |
| | 37 | static int uverbs_free_counters(struct ib_uobject *uobject, |
| | 38 | enum rdma_remove_reason why) |
| | 39 | { |
| | 40 | struct ib_counters *counters = uobject->object; |
| | 41 | |
| | 42 | if (why == RDMA_REMOVE_DESTROY && |
| | 43 | atomic_read(&counters->usecnt)) |
| | 44 | return -EBUSY; |
| | 45 | |
| | 46 | return counters->device->destroy_counters(counters); |
| | 47 | } |
| | 48 | |
| | 49 | static int UVERBS_HANDLER(UVERBS_METHOD_COUNTERS_CREATE)(struct ib_device *ib_dev, |
| | 50 | struct ib_uverbs_file *file, |
| | 51 | struct uverbs_attr_bundle *attrs) |
| | 52 | { |
| | 53 | struct ib_counters *counters; |
| | 54 | struct ib_uobject *uobj; |
| | 55 | int ret; |
| | 56 | |
| | 57 | /* |
| | 58 | * This check should be removed once the infrastructure |
| | 59 | * have the ability to remove methods from parse tree once |
| | 60 | * such condition is met. |
| | 61 | */ |
| | 62 | if (!ib_dev->create_counters) |
| | 63 | return -EOPNOTSUPP; |
| | 64 | |
| | 65 | uobj = uverbs_attr_get_uobject(attrs, UVERBS_ATTR_CREATE_COUNTERS_HANDLE); |
| | 66 | counters = ib_dev->create_counters(ib_dev, attrs); |
| | 67 | if (IS_ERR(counters)) { |
| | 68 | ret = PTR_ERR(counters); |
| | 69 | goto err_create_counters; |
| | 70 | } |
| | 71 | |
| | 72 | counters->device = ib_dev; |
| | 73 | counters->uobject = uobj; |
| | 74 | uobj->object = counters; |
| | 75 | atomic_set(&counters->usecnt, 0); |
| | 76 | |
| | 77 | return 0; |
| | 78 | |
| | 79 | err_create_counters: |
| | 80 | return ret; |
| | 81 | } |
| | 82 | |
| | 83 | static DECLARE_UVERBS_NAMED_METHOD(UVERBS_METHOD_COUNTERS_CREATE, |
| | 84 | &UVERBS_ATTR_IDR(UVERBS_ATTR_CREATE_COUNTERS_HANDLE, |
| | 85 | UVERBS_OBJECT_COUNTERS, |
| | 86 | UVERBS_ACCESS_NEW, |
| | 87 | UA_FLAGS(UVERBS_ATTR_SPEC_F_MANDATORY))); |
| | 88 | |
| | 89 | static DECLARE_UVERBS_NAMED_METHOD_WITH_HANDLER(UVERBS_METHOD_COUNTERS_DESTROY, |
| | 90 | uverbs_destroy_def_handler, |
| | 91 | &UVERBS_ATTR_IDR(UVERBS_ATTR_DESTROY_COUNTERS_HANDLE, |
| | 92 | UVERBS_OBJECT_COUNTERS, |
| | 93 | UVERBS_ACCESS_DESTROY, |
| | 94 | UA_FLAGS(UVERBS_ATTR_SPEC_F_MANDATORY))); |
| | 95 | |
| | 96 | DECLARE_UVERBS_NAMED_OBJECT(UVERBS_OBJECT_COUNTERS, |
| | 97 | &UVERBS_TYPE_ALLOC_IDR(0, uverbs_free_counters), |
| | 98 | &UVERBS_METHOD(UVERBS_METHOD_COUNTERS_CREATE), |
| | 99 | &UVERBS_METHOD(UVERBS_METHOD_COUNTERS_DESTROY)); |
| | 100 | |