1 /******************************************************************************
3 Copyright (c) 2013-2017, Intel Corporation
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions are met:
9 1. Redistributions of source code must retain the above copyright notice,
10 this list of conditions and the following disclaimer.
12 2. Redistributions in binary form must reproduce the above copyright
13 notice, this list of conditions and the following disclaimer in the
14 documentation and/or other materials provided with the distribution.
16 3. Neither the name of the Intel Corporation nor the names of its
17 contributors may be used to endorse or promote products derived from
18 this software without specific prior written permission.
20 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 POSSIBILITY OF SUCH DAMAGE.
32 ******************************************************************************/
36 #include "ixl_pf_qmgr.h"
38 static int ixl_pf_qmgr_find_free_contiguous_block(struct ixl_pf_qmgr *qmgr, int num);
41 ixl_pf_qmgr_init(struct ixl_pf_qmgr *qmgr, u16 num_queues)
46 qmgr->num_queues = num_queues;
47 qmgr->qinfo = malloc(num_queues * sizeof(struct ixl_pf_qmgr_qinfo),
48 M_IXL, M_ZERO | M_WAITOK);
49 if (qmgr->qinfo == NULL)
56 ixl_pf_qmgr_alloc_contiguous(struct ixl_pf_qmgr *qmgr, u16 num, struct ixl_pf_qtag *qtag)
63 if (qtag == NULL || num < 1)
66 /* We have to allocate in power-of-two chunks, so get next power of two */
67 alloc_size = (u16)next_power_of_two(num);
69 /* Don't try if there aren't enough queues */
70 avail = ixl_pf_qmgr_get_num_free(qmgr);
71 if (avail < alloc_size)
74 block_start = ixl_pf_qmgr_find_free_contiguous_block(qmgr, alloc_size);
78 /* Mark queues as allocated */
79 for (i = block_start; i < block_start + alloc_size; i++)
80 qmgr->qinfo[i].allocated = true;
82 bzero(qtag, sizeof(*qtag));
84 qtag->type = IXL_PF_QALLOC_CONTIGUOUS;
85 qtag->qidx[0] = block_start;
86 qtag->num_allocated = num;
87 qtag->num_active = alloc_size;
93 * NB: indices is u16 because this is the queue index width used in the Add VSI AQ command
96 ixl_pf_qmgr_alloc_scattered(struct ixl_pf_qmgr *qmgr, u16 num, struct ixl_pf_qtag *qtag)
102 if (qtag == NULL || num < 1 || num > 16)
105 /* We have to allocate in power-of-two chunks, so get next power of two */
106 alloc_size = (u16)next_power_of_two(num);
108 avail = ixl_pf_qmgr_get_num_free(qmgr);
109 if (avail < alloc_size)
112 bzero(qtag, sizeof(*qtag));
114 qtag->type = IXL_PF_QALLOC_SCATTERED;
115 qtag->num_active = num;
116 qtag->num_allocated = alloc_size;
118 for (i = 0; i < qmgr->num_queues; i++) {
119 if (!qmgr->qinfo[i].allocated) {
120 qtag->qidx[count] = i;
122 qmgr->qinfo[i].allocated = true;
123 if (count == alloc_size)
128 // Shouldn't get here
133 ixl_pf_qmgr_release(struct ixl_pf_qmgr *qmgr, struct ixl_pf_qtag *qtag)
140 if (qtag->type == IXL_PF_QALLOC_SCATTERED) {
141 for (i = 0; i < qtag->num_allocated; i++) {
142 qidx = qtag->qidx[i];
143 bzero(&qmgr->qinfo[qidx], sizeof(qmgr->qinfo[qidx]));
146 u16 first_index = qtag->qidx[0];
147 for (i = first_index; i < first_index + qtag->num_allocated; i++)
148 bzero(&qmgr->qinfo[i], sizeof(qmgr->qinfo[qidx]));
156 ixl_pf_qmgr_get_num_queues(struct ixl_pf_qmgr *qmgr)
158 return (qmgr->num_queues);
162 * ERJ: This assumes the info array isn't longer than INT_MAX.
163 * This assumption might cause a y3k bug or something, I'm sure.
166 ixl_pf_qmgr_get_num_free(struct ixl_pf_qmgr *qmgr)
170 for (int i = 0; i < qmgr->num_queues; i++) {
171 if (!qmgr->qinfo[i].allocated)
179 ixl_pf_qmgr_get_first_free(struct ixl_pf_qmgr *qmgr, u16 start)
183 if (start > qmgr->num_queues - 1)
186 for (i = start; i < qmgr->num_queues; i++) {
187 if (qmgr->qinfo[i].allocated)
198 ixl_pf_qmgr_destroy(struct ixl_pf_qmgr *qmgr)
200 free(qmgr->qinfo, M_IXL);
205 ixl_pf_qmgr_mark_queue_enabled(struct ixl_pf_qtag *qtag, u16 vsi_qidx, bool tx)
209 struct ixl_pf_qmgr *qmgr = qtag->qmgr;
210 u16 pf_qidx = ixl_pf_qidx_from_vsi_qidx(qtag, vsi_qidx);
212 qmgr->qinfo[pf_qidx].tx_enabled = true;
214 qmgr->qinfo[pf_qidx].rx_enabled = true;
218 ixl_pf_qmgr_mark_queue_disabled(struct ixl_pf_qtag *qtag, u16 vsi_qidx, bool tx)
222 struct ixl_pf_qmgr *qmgr = qtag->qmgr;
223 u16 pf_qidx = ixl_pf_qidx_from_vsi_qidx(qtag, vsi_qidx);
225 qmgr->qinfo[pf_qidx].tx_enabled = false;
227 qmgr->qinfo[pf_qidx].rx_enabled = false;
231 ixl_pf_qmgr_mark_queue_configured(struct ixl_pf_qtag *qtag, u16 vsi_qidx, bool tx)
235 struct ixl_pf_qmgr *qmgr = qtag->qmgr;
236 u16 pf_qidx = ixl_pf_qidx_from_vsi_qidx(qtag, vsi_qidx);
238 qmgr->qinfo[pf_qidx].tx_configured = true;
240 qmgr->qinfo[pf_qidx].rx_configured = true;
244 ixl_pf_qmgr_is_queue_enabled(struct ixl_pf_qtag *qtag, u16 vsi_qidx, bool tx)
248 struct ixl_pf_qmgr *qmgr = qtag->qmgr;
249 u16 pf_qidx = ixl_pf_qidx_from_vsi_qidx(qtag, vsi_qidx);
251 return (qmgr->qinfo[pf_qidx].tx_enabled);
253 return (qmgr->qinfo[pf_qidx].rx_enabled);
257 ixl_pf_qmgr_is_queue_configured(struct ixl_pf_qtag *qtag, u16 vsi_qidx, bool tx)
261 struct ixl_pf_qmgr *qmgr = qtag->qmgr;
262 u16 pf_qidx = ixl_pf_qidx_from_vsi_qidx(qtag, vsi_qidx);
264 return (qmgr->qinfo[pf_qidx].tx_configured);
266 return (qmgr->qinfo[pf_qidx].rx_configured);
270 ixl_pf_qidx_from_vsi_qidx(struct ixl_pf_qtag *qtag, u16 index)
272 MPASS(index < qtag->num_allocated);
274 if (qtag->type == IXL_PF_QALLOC_CONTIGUOUS)
275 return qtag->qidx[0] + index;
277 return qtag->qidx[index];
280 /* Static Functions */
283 ixl_pf_qmgr_find_free_contiguous_block(struct ixl_pf_qmgr *qmgr, int num)
287 bool block_started = false;
290 for (i = 0; i < qmgr->num_queues; i++) {
291 if (!qmgr->qinfo[i].allocated) {
292 if (!block_started) {
293 block_started = true;
298 return (possible_start);
299 } else { /* this queue is already allocated */
300 block_started = false;
305 /* Can't find a contiguous block of the requested size */