1 /* SPDX-License-Identifier: BSD-3-Clause */
2 /* Copyright (c) 2021, Intel Corporation
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the Intel Corporation nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
35 * @brief Resource allocation manager
37 * Manage device resource allocations for a PF, including assigning queues to
38 * VSIs, or managing interrupt allocations across the PF.
40 * It can handle contiguous and scattered resource allocations, and upon
41 * assigning them, will fill in the mapping array with a map of
42 * resource IDs to PF-space resource indices.
45 #include "ice_resmgr.h"
49 * @brief PF resource manager allocation type
51 * malloc(9) allocation type used by the resource manager code.
53 MALLOC_DEFINE(M_ICE_RESMGR, "ice-resmgr", "Intel(R) 100Gb Network Driver resmgr allocations");
56 * Public resource manager allocation functions
60 * ice_resmgr_init - Initialize a resource manager structure
61 * @resmgr: structure to track the resource manager state
62 * @num_res: the maximum number of resources it can assign
64 * Initialize the state of a resource manager structure, allocating space to
65 * assign up to the requested number of resources. Uses bit strings to track
66 * which resources have been assigned. This type of resmgr is intended to be
67 * used for tracking LAN queue assignments between VSIs.
70 ice_resmgr_init(struct ice_resmgr *resmgr, u16 num_res)
72 resmgr->resources = bit_alloc(num_res, M_ICE_RESMGR, M_NOWAIT);
73 if (resmgr->resources == NULL)
76 resmgr->num_res = num_res;
77 resmgr->contig_only = false;
82 * ice_resmgr_init_contig_only - Initialize a resource manager structure
83 * @resmgr: structure to track the resource manager state
84 * @num_res: the maximum number of resources it can assign
86 * Functions similarly to ice_resmgr_init(), but the resulting resmgr structure
87 * will only allow contiguous allocations. This type of resmgr is intended to
88 * be used with tracking device MSI-X interrupt allocations.
91 ice_resmgr_init_contig_only(struct ice_resmgr *resmgr, u16 num_res)
95 error = ice_resmgr_init(resmgr, num_res);
99 resmgr->contig_only = true;
104 * ice_resmgr_destroy - Deallocate memory associated with a resource manager
105 * @resmgr: resource manager structure
107 * De-allocates the bit string associated with this resource manager. It is
108 * expected that this function will not be called until all of the assigned
109 * resources have been released.
112 ice_resmgr_destroy(struct ice_resmgr *resmgr)
114 if (resmgr->resources != NULL) {
118 bit_count(resmgr->resources, 0, resmgr->num_res, &set);
122 free(resmgr->resources, M_ICE_RESMGR);
123 resmgr->resources = NULL;
129 * Resource allocation functions
133 * ice_resmgr_assign_contiguous - Assign contiguous mapping of resources
134 * @resmgr: resource manager structure
135 * @idx: memory to store mapping, at least num_res wide
136 * @num_res: the number of resources to assign
138 * Assign num_res number of contiguous resources into the idx mapping. On
139 * success, idx will be updated to map each index to a PF resource.
141 * This function guarantees that the resource mapping will be contiguous, and
142 * will fail if that is not possible.
145 ice_resmgr_assign_contiguous(struct ice_resmgr *resmgr, u16 *idx, u16 num_res)
149 bit_ffc_area(resmgr->resources, resmgr->num_res, num_res, &start);
153 /* Set each bit and update the index array */
154 for (i = 0; i < num_res; i++) {
155 bit_set(resmgr->resources, start + i);
163 * ice_resmgr_assign_scattered - Assign possibly scattered resources
164 * @resmgr: the resource manager structure
165 * @idx: memory to store associated resource mapping, at least num_res wide
166 * @num_res: the number of resources to assign
168 * Assign num_res number of resources into the idx_mapping. On success, idx
169 * will be updated to map each index to a PF-space resource.
171 * Queues may be allocated non-contiguously, and this function requires that
172 * num_res be less than the ICE_MAX_SCATTERED_QUEUES due to hardware
173 * limitations on scattered queue assignment.
176 ice_resmgr_assign_scattered(struct ice_resmgr *resmgr, u16 *idx, u16 num_res)
180 /* Scattered allocations won't work if they weren't allowed at resmgr
183 if (resmgr->contig_only)
186 /* Hardware can only support a limited total of scattered queues for
189 if (num_res > ICE_MAX_SCATTERED_QUEUES)
192 for (i = 0; i < num_res; i++) {
193 bit_ffc_at(resmgr->resources, index, resmgr->num_res, &index);
197 bit_set(resmgr->resources, index);
203 /* Release any resources we did assign up to this point. */
204 ice_resmgr_release_map(resmgr, idx, i);
209 * ice_resmgr_release_map - Release previously assigned resource mapping
210 * @resmgr: the resource manager structure
211 * @idx: previously assigned resource mapping
212 * @num_res: number of resources in the mapping
214 * Clears the assignment of each resource in the provided resource index. Updates
215 * the idx to indicate that each of the virtual indexes have invalid resource
216 * mappings by assigning them to ICE_INVALID_RES_IDX.
219 ice_resmgr_release_map(struct ice_resmgr *resmgr, u16 *idx, u16 num_res)
223 for (i = 0; i < num_res; i++) {
224 if (idx[i] < resmgr->num_res)
225 bit_clear(resmgr->resources, idx[i]);
226 idx[i] = ICE_INVALID_RES_IDX;