cmd.c (664b0bae0b87f69bc9deb098f5e0158b9cf18e04) cmd.c (24da00164f7a9c247d2224a54494d0e955199630)
1/*
2 * Copyright (c) 2017, Mellanox Technologies. All rights reserved.
3 *
4 * This software is available to you under a choice of one of two
5 * licenses. You may choose to be licensed under the terms of the GNU
6 * General Public License (GPL) Version 2, available from the file
7 * COPYING in the main directory of this source tree, or the
8 * OpenIB.org BSD license below:

--- 52 unchanged lines hidden (view full) ---

61
62int mlx5_cmd_modify_cong_params(struct mlx5_core_dev *dev,
63 void *in, int in_size)
64{
65 u32 out[MLX5_ST_SZ_DW(modify_cong_params_out)] = { };
66
67 return mlx5_cmd_exec(dev, in, in_size, out, sizeof(out));
68}
1/*
2 * Copyright (c) 2017, Mellanox Technologies. All rights reserved.
3 *
4 * This software is available to you under a choice of one of two
5 * licenses. You may choose to be licensed under the terms of the GNU
6 * General Public License (GPL) Version 2, available from the file
7 * COPYING in the main directory of this source tree, or the
8 * OpenIB.org BSD license below:

--- 52 unchanged lines hidden (view full) ---

61
62int mlx5_cmd_modify_cong_params(struct mlx5_core_dev *dev,
63 void *in, int in_size)
64{
65 u32 out[MLX5_ST_SZ_DW(modify_cong_params_out)] = { };
66
67 return mlx5_cmd_exec(dev, in, in_size, out, sizeof(out));
68}
69
70int mlx5_cmd_alloc_memic(struct mlx5_memic *memic, phys_addr_t *addr,
71 u64 length, u32 alignment)
72{
73 struct mlx5_core_dev *dev = memic->dev;
74 u64 num_memic_hw_pages = MLX5_CAP_DEV_MEM(dev, memic_bar_size)
75 >> PAGE_SHIFT;
76 u64 hw_start_addr = MLX5_CAP64_DEV_MEM(dev, memic_bar_start_addr);
77 u32 max_alignment = MLX5_CAP_DEV_MEM(dev, log_max_memic_addr_alignment);
78 u32 num_pages = DIV_ROUND_UP(length, PAGE_SIZE);
79 u32 out[MLX5_ST_SZ_DW(alloc_memic_out)] = {};
80 u32 in[MLX5_ST_SZ_DW(alloc_memic_in)] = {};
81 u32 mlx5_alignment;
82 u64 page_idx = 0;
83 int ret = 0;
84
85 if (!length || (length & MLX5_MEMIC_ALLOC_SIZE_MASK))
86 return -EINVAL;
87
88 /* mlx5 device sets alignment as 64*2^driver_value
89 * so normalizing is needed.
90 */
91 mlx5_alignment = (alignment < MLX5_MEMIC_BASE_ALIGN) ? 0 :
92 alignment - MLX5_MEMIC_BASE_ALIGN;
93 if (mlx5_alignment > max_alignment)
94 return -EINVAL;
95
96 MLX5_SET(alloc_memic_in, in, opcode, MLX5_CMD_OP_ALLOC_MEMIC);
97 MLX5_SET(alloc_memic_in, in, range_size, num_pages * PAGE_SIZE);
98 MLX5_SET(alloc_memic_in, in, memic_size, length);
99 MLX5_SET(alloc_memic_in, in, log_memic_addr_alignment,
100 mlx5_alignment);
101
102 do {
103 spin_lock(&memic->memic_lock);
104 page_idx = bitmap_find_next_zero_area(memic->memic_alloc_pages,
105 num_memic_hw_pages,
106 page_idx,
107 num_pages, 0);
108
109 if (page_idx + num_pages <= num_memic_hw_pages)
110 bitmap_set(memic->memic_alloc_pages,
111 page_idx, num_pages);
112 else
113 ret = -ENOMEM;
114
115 spin_unlock(&memic->memic_lock);
116
117 if (ret)
118 return ret;
119
120 MLX5_SET64(alloc_memic_in, in, range_start_addr,
121 hw_start_addr + (page_idx * PAGE_SIZE));
122
123 ret = mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out));
124 if (ret) {
125 spin_lock(&memic->memic_lock);
126 bitmap_clear(memic->memic_alloc_pages,
127 page_idx, num_pages);
128 spin_unlock(&memic->memic_lock);
129
130 if (ret == -EAGAIN) {
131 page_idx++;
132 continue;
133 }
134
135 return ret;
136 }
137
138 *addr = pci_resource_start(dev->pdev, 0) +
139 MLX5_GET64(alloc_memic_out, out, memic_start_addr);
140
141 return ret;
142 } while (page_idx < num_memic_hw_pages);
143
144 return ret;
145}
146
147int mlx5_cmd_dealloc_memic(struct mlx5_memic *memic, u64 addr, u64 length)
148{
149 struct mlx5_core_dev *dev = memic->dev;
150 u64 hw_start_addr = MLX5_CAP64_DEV_MEM(dev, memic_bar_start_addr);
151 u32 num_pages = DIV_ROUND_UP(length, PAGE_SIZE);
152 u32 out[MLX5_ST_SZ_DW(dealloc_memic_out)] = {0};
153 u32 in[MLX5_ST_SZ_DW(dealloc_memic_in)] = {0};
154 u64 start_page_idx;
155 int err;
156
157 addr -= pci_resource_start(dev->pdev, 0);
158 start_page_idx = (addr - hw_start_addr) >> PAGE_SHIFT;
159
160 MLX5_SET(dealloc_memic_in, in, opcode, MLX5_CMD_OP_DEALLOC_MEMIC);
161 MLX5_SET64(dealloc_memic_in, in, memic_start_addr, addr);
162 MLX5_SET(dealloc_memic_in, in, memic_size, length);
163
164 err = mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out));
165
166 if (!err) {
167 spin_lock(&memic->memic_lock);
168 bitmap_clear(memic->memic_alloc_pages,
169 start_page_idx, num_pages);
170 spin_unlock(&memic->memic_lock);
171 }
172
173 return err;
174}