8e50dcc9f00b393d43e6aa42b820bcbf1d3e1ce4brutus * CDDL HEADER START
8e50dcc9f00b393d43e6aa42b820bcbf1d3e1ce4brutus * The contents of this file are subject to the terms of the
8e50dcc9f00b393d43e6aa42b820bcbf1d3e1ce4brutus * Common Development and Distribution License (the "License").
8e50dcc9f00b393d43e6aa42b820bcbf1d3e1ce4brutus * You may not use this file except in compliance with the License.
8e50dcc9f00b393d43e6aa42b820bcbf1d3e1ce4brutus * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
8e50dcc9f00b393d43e6aa42b820bcbf1d3e1ce4brutus * See the License for the specific language governing permissions
8e50dcc9f00b393d43e6aa42b820bcbf1d3e1ce4brutus * and limitations under the License.
8e50dcc9f00b393d43e6aa42b820bcbf1d3e1ce4brutus * When distributing Covered Code, include this CDDL HEADER in each
8e50dcc9f00b393d43e6aa42b820bcbf1d3e1ce4brutus * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
8e50dcc9f00b393d43e6aa42b820bcbf1d3e1ce4brutus * If applicable, add the following below this CDDL HEADER, with the
8e50dcc9f00b393d43e6aa42b820bcbf1d3e1ce4brutus * fields enclosed by brackets "[]" replaced with your own identifying
8e50dcc9f00b393d43e6aa42b820bcbf1d3e1ce4brutus * information: Portions Copyright [yyyy] [name of copyright owner]
8e50dcc9f00b393d43e6aa42b820bcbf1d3e1ce4brutus * CDDL HEADER END
8e50dcc9f00b393d43e6aa42b820bcbf1d3e1ce4brutus * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
8e50dcc9f00b393d43e6aa42b820bcbf1d3e1ce4brutus * Use is subject to license terms.
8e50dcc9f00b393d43e6aa42b820bcbf1d3e1ce4brutus#pragma ident "%Z%%M% %I% %E% SMI"
8e50dcc9f00b393d43e6aa42b820bcbf1d3e1ce4brutus/* structure used to keep track of resources */
8e50dcc9f00b393d43e6aa42b820bcbf1d3e1ce4brutus * Bounds of resource allocation. We will start allocating at rs_min
8e50dcc9f00b393d43e6aa42b820bcbf1d3e1ce4brutus * and rollover at rs_max+1 (rs_max is included). e.g. for rs_min=0
8e50dcc9f00b393d43e6aa42b820bcbf1d3e1ce4brutus * and rs_max=7, we will have 8 total resources which can be alloced.
8e50dcc9f00b393d43e6aa42b820bcbf1d3e1ce4brutus * rs_free points to an array of 64-bit values used to track resource
8e50dcc9f00b393d43e6aa42b820bcbf1d3e1ce4brutus * allocation. rs_free_size is the free buffer size in bytes.
8e50dcc9f00b393d43e6aa42b820bcbf1d3e1ce4brutus * last tracks the last alloc'd resource. This allows us to do a round
8e50dcc9f00b393d43e6aa42b820bcbf1d3e1ce4brutus * robin allocation.
8e50dcc9f00b393d43e6aa42b820bcbf1d3e1ce4brutus * ioat_rs_init()
8e50dcc9f00b393d43e6aa42b820bcbf1d3e1ce4brutus * Initialize the resource structure. This structure will be protected
8e50dcc9f00b393d43e6aa42b820bcbf1d3e1ce4brutus * by a mutex at the iblock_cookie passed in. init() returns a handle to be
8e50dcc9f00b393d43e6aa42b820bcbf1d3e1ce4brutus * used for the rest of the resource functions. This code is written assuming
8e50dcc9f00b393d43e6aa42b820bcbf1d3e1ce4brutus * that min_val will be close to 0. Therefore, we will allocate the free
8e50dcc9f00b393d43e6aa42b820bcbf1d3e1ce4brutus * buffer only taking max_val into account.
8e50dcc9f00b393d43e6aa42b820bcbf1d3e1ce4brutusioat_rs_init(ioat_state_t *state, uint_t min_val, uint_t max_val,
8e50dcc9f00b393d43e6aa42b820bcbf1d3e1ce4brutus /* alloc space for resource structure */
8e50dcc9f00b393d43e6aa42b820bcbf1d3e1ce4brutus * Test to see if the max value is 64-bit aligned. If so, we don't need
8e50dcc9f00b393d43e6aa42b820bcbf1d3e1ce4brutus * to allocate an extra 64-bit word. alloc space for free buffer
8e50dcc9f00b393d43e6aa42b820bcbf1d3e1ce4brutus * (8 bytes per uint64_t).
8e50dcc9f00b393d43e6aa42b820bcbf1d3e1ce4brutus rstruct->rs_free = kmem_alloc(rstruct->rs_free_size, KM_SLEEP);
8e50dcc9f00b393d43e6aa42b820bcbf1d3e1ce4brutus /* Initialize resource structure */
8e50dcc9f00b393d43e6aa42b820bcbf1d3e1ce4brutus /* Mark all resources as free */
8e50dcc9f00b393d43e6aa42b820bcbf1d3e1ce4brutus rstruct->rs_free[index] = (uint64_t)0xFFFFFFFFFFFFFFFF;
8e50dcc9f00b393d43e6aa42b820bcbf1d3e1ce4brutus /* setup handle which is returned from this function */
8e50dcc9f00b393d43e6aa42b820bcbf1d3e1ce4brutus * ioat_rs_fini()
8e50dcc9f00b393d43e6aa42b820bcbf1d3e1ce4brutus * Frees up the space allocated in init(). Notice that a pointer to the
8e50dcc9f00b393d43e6aa42b820bcbf1d3e1ce4brutus * handle is used for the parameter. fini() will set the handle to NULL
8e50dcc9f00b393d43e6aa42b820bcbf1d3e1ce4brutus * before returning.
8e50dcc9f00b393d43e6aa42b820bcbf1d3e1ce4brutus /* set handle to null. This helps catch bugs. */
8e50dcc9f00b393d43e6aa42b820bcbf1d3e1ce4brutus * ioat_rs_alloc()
8e50dcc9f00b393d43e6aa42b820bcbf1d3e1ce4brutus * alloc a resource. If alloc fails, we are out of resources.
8e50dcc9f00b393d43e6aa42b820bcbf1d3e1ce4brutus * Find a free resource. This will return out of the loop once it finds
8e50dcc9f00b393d43e6aa42b820bcbf1d3e1ce4brutus * a free resource. There are a total of 'max'-'min'+1 resources.
8e50dcc9f00b393d43e6aa42b820bcbf1d3e1ce4brutus * Performs a round robin allocation.
8e50dcc9f00b393d43e6aa42b820bcbf1d3e1ce4brutus /* if the next resource to check is free */
8e50dcc9f00b393d43e6aa42b820bcbf1d3e1ce4brutus /* we are using this resource */
8e50dcc9f00b393d43e6aa42b820bcbf1d3e1ce4brutus /* take it out of the free list */
8e50dcc9f00b393d43e6aa42b820bcbf1d3e1ce4brutus rstruct->rs_free[array_idx] &= ~((uint64_t)1 << last);
8e50dcc9f00b393d43e6aa42b820bcbf1d3e1ce4brutus * increment the last count so we start checking the
8e50dcc9f00b393d43e6aa42b820bcbf1d3e1ce4brutus * next resource on the next alloc(). Note the rollover
8e50dcc9f00b393d43e6aa42b820bcbf1d3e1ce4brutus * at 'max'+1.
8e50dcc9f00b393d43e6aa42b820bcbf1d3e1ce4brutus /* unlock the resource structure */
8e50dcc9f00b393d43e6aa42b820bcbf1d3e1ce4brutus * This resource is not free, lets go to the next one. Note the
8e50dcc9f00b393d43e6aa42b820bcbf1d3e1ce4brutus * rollover at 'max'.
8e50dcc9f00b393d43e6aa42b820bcbf1d3e1ce4brutus * ioat_rs_free()
8e50dcc9f00b393d43e6aa42b820bcbf1d3e1ce4brutus * Free the previously alloc'd resource. Once a resource has been free'd,
8e50dcc9f00b393d43e6aa42b820bcbf1d3e1ce4brutus * it can be used again when alloc is called.
8e50dcc9f00b393d43e6aa42b820bcbf1d3e1ce4brutus /* Put the resource back in the free list */