18329232Gordon Ross
28329232Gordon Ross/*
38329232Gordon Ross * CDDL HEADER START
48329232Gordon Ross *
58329232Gordon Ross * The contents of this file are subject to the terms of the
68329232Gordon Ross * Common Development and Distribution License (the "License").
78329232Gordon Ross * You may not use this file except in compliance with the License.
88329232Gordon Ross *
98329232Gordon Ross * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
108329232Gordon Ross * or http://www.opensolaris.org/os/licensing.
118329232Gordon Ross * See the License for the specific language governing permissions
128329232Gordon Ross * and limitations under the License.
138329232Gordon Ross *
148329232Gordon Ross * When distributing Covered Code, include this CDDL HEADER in each
158329232Gordon Ross * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
168329232Gordon Ross * If applicable, add the following below this CDDL HEADER, with the
178329232Gordon Ross * fields enclosed by brackets "[]" replaced with your own identifying
188329232Gordon Ross * information: Portions Copyright [yyyy] [name of copyright owner]
198329232Gordon Ross *
208329232Gordon Ross * CDDL HEADER END
218329232Gordon Ross */
228329232Gordon Ross
238329232Gordon Ross/*
248329232Gordon Ross * Copyright (c) 1990, 2010, Oracle and/or its affiliates. All rights reserved.
258329232Gordon Ross * Copyright 2014 Garrett D'Amore <garrett@damore.org>
268329232Gordon Ross * Copyright 2017 Nexenta Systems, Inc.  All rights reserved.
278329232Gordon Ross */
288329232Gordon Ross
298329232Gordon Ross#include <sys/types.h>
308329232Gordon Ross#include <sys/debug.h>
318329232Gordon Ross#include <sys/sunddi.h>
328329232Gordon Ross
338329232Gordon Ross#define	MIN_N_ITEMS	8
348329232Gordon Ross
358329232Gordon Rosstypedef struct i_ddi_soft_state {
368329232Gordon Ross	void		**array;	/* the array of pointers */
378329232Gordon Ross	kmutex_t	lock;		/* serialize access to this struct */
388329232Gordon Ross	size_t		size;		/* how many bytes per state struct */
398329232Gordon Ross	size_t		n_items;	/* how many structs herein */
408329232Gordon Ross	void		*next;		/* unused */
418329232Gordon Ross} i_ddi_soft_state;
428329232Gordon Ross
438329232Gordon Ross
448329232Gordon Rossvoid *
458329232Gordon Rossddi_get_soft_state(void *state, int item)
468329232Gordon Ross{
478329232Gordon Ross	i_ddi_soft_state	*ss = (i_ddi_soft_state *)state;
488329232Gordon Ross	void *ret = NULL;
498329232Gordon Ross
508329232Gordon Ross	ASSERT((ss != NULL) && (item >= 0));
518329232Gordon Ross
528329232Gordon Ross	mutex_enter(&ss->lock);
538329232Gordon Ross
548329232Gordon Ross	if (item < ss->n_items && ss->array != NULL)
558329232Gordon Ross		ret = ss->array[item];
568329232Gordon Ross
578329232Gordon Ross	mutex_exit(&ss->lock);
588329232Gordon Ross
598329232Gordon Ross	return (ret);
608329232Gordon Ross}
618329232Gordon Ross
628329232Gordon Ross
638329232Gordon Rossint
648329232Gordon Rossddi_soft_state_init(void **state_p, size_t size, size_t n_items)
658329232Gordon Ross{
668329232Gordon Ross	i_ddi_soft_state	*ss;
678329232Gordon Ross
688329232Gordon Ross	if (state_p == NULL || size == 0)
698329232Gordon Ross		return (EINVAL);
708329232Gordon Ross
718329232Gordon Ross	ss = kmem_zalloc(sizeof (*ss), KM_SLEEP);
728329232Gordon Ross	mutex_init(&ss->lock, NULL, MUTEX_DRIVER, NULL);
738329232Gordon Ross	ss->size = size;
748329232Gordon Ross
758329232Gordon Ross	if (n_items < MIN_N_ITEMS)
768329232Gordon Ross		ss->n_items = MIN_N_ITEMS;
778329232Gordon Ross	else {
788329232Gordon Ross		ss->n_items = n_items;
798329232Gordon Ross	}
808329232Gordon Ross
818329232Gordon Ross	ASSERT(ss->n_items >= n_items);
828329232Gordon Ross
838329232Gordon Ross	ss->array = kmem_zalloc(ss->n_items * sizeof (void *), KM_SLEEP);
848329232Gordon Ross
858329232Gordon Ross	*state_p = ss;
868329232Gordon Ross	return (0);
878329232Gordon Ross}
888329232Gordon Ross
898329232Gordon Ross/*
908329232Gordon Ross * Allocate a state structure of size 'size' to be associated
918329232Gordon Ross * with item 'item'.
928329232Gordon Ross *
938329232Gordon Ross * In this implementation, the array is extended to
948329232Gordon Ross * allow the requested offset, if needed.
958329232Gordon Ross */
968329232Gordon Rossint
978329232Gordon Rossddi_soft_state_zalloc(void *state, int item)
988329232Gordon Ross{
998329232Gordon Ross	i_ddi_soft_state	*ss = (i_ddi_soft_state *)state;
1008329232Gordon Ross	void			**array;
1018329232Gordon Ross	void			*new_element;
1028329232Gordon Ross
1038329232Gordon Ross	if ((state == NULL) || (item < 0))
1048329232Gordon Ross		return (DDI_FAILURE);
1058329232Gordon Ross
1068329232Gordon Ross	mutex_enter(&ss->lock);
1078329232Gordon Ross	if (ss->size == 0) {
1088329232Gordon Ross		mutex_exit(&ss->lock);
1098329232Gordon Ross		cmn_err(CE_WARN, "ddi_soft_state_zalloc: bad handle: %s",
1108329232Gordon Ross		    "fake");
1118329232Gordon Ross		return (DDI_FAILURE);
1128329232Gordon Ross	}
1138329232Gordon Ross
1148329232Gordon Ross	array = ss->array;	/* NULL if ss->n_items == 0 */
1158329232Gordon Ross	ASSERT(ss->n_items != 0 && array != NULL);
1168329232Gordon Ross
1178329232Gordon Ross	/*
1188329232Gordon Ross	 * refuse to tread on an existing element
1198329232Gordon Ross	 */
1208329232Gordon Ross	if (item < ss->n_items && array[item] != NULL) {
1218329232Gordon Ross		mutex_exit(&ss->lock);
1228329232Gordon Ross		return (DDI_FAILURE);
1238329232Gordon Ross	}
1248329232Gordon Ross
1258329232Gordon Ross	/*
1268329232Gordon Ross	 * Allocate a new element to plug in
1278329232Gordon Ross	 */
1288329232Gordon Ross	new_element = kmem_zalloc(ss->size, KM_SLEEP);
1298329232Gordon Ross
1308329232Gordon Ross	/*
1318329232Gordon Ross	 * Check if the array is big enough, if not, grow it.
1328329232Gordon Ross	 */
1338329232Gordon Ross	if (item >= ss->n_items) {
1348329232Gordon Ross		void			**new_array;
1358329232Gordon Ross		size_t			new_n_items;
1368329232Gordon Ross
1378329232Gordon Ross		/*
1388329232Gordon Ross		 * Allocate a new array of the right length, copy
1398329232Gordon Ross		 * all the old pointers to the new array, then
1408329232Gordon Ross		 * if it exists at all, put the old array on the
1418329232Gordon Ross		 * dirty list.
1428329232Gordon Ross		 *
1438329232Gordon Ross		 * Note that we can't kmem_free() the old array.
1448329232Gordon Ross		 *
1458329232Gordon Ross		 * Why -- well the 'get' operation is 'mutex-free', so we
1468329232Gordon Ross		 * can't easily catch a suspended thread that is just about
1478329232Gordon Ross		 * to dereference the array we just grew out of.  So we
1488329232Gordon Ross		 * cons up a header and put it on a list of 'dirty'
1498329232Gordon Ross		 * pointer arrays.  (Dirty in the sense that there may
1508329232Gordon Ross		 * be suspended threads somewhere that are in the middle
1518329232Gordon Ross		 * of referencing them).  Fortunately, we -can- garbage
1528329232Gordon Ross		 * collect it all at ddi_soft_state_fini time.
1538329232Gordon Ross		 */
1548329232Gordon Ross		new_n_items = ss->n_items;
1558329232Gordon Ross		while (new_n_items < (1 + item))
1568329232Gordon Ross			new_n_items <<= 1;	/* double array size .. */
1578329232Gordon Ross
1588329232Gordon Ross		ASSERT(new_n_items >= (1 + item));	/* sanity check! */
1598329232Gordon Ross
1608329232Gordon Ross		new_array = kmem_zalloc(new_n_items * sizeof (void *),
1618329232Gordon Ross		    KM_SLEEP);
1628329232Gordon Ross		/*
1638329232Gordon Ross		 * Copy the pointers into the new array
1648329232Gordon Ross		 */
1658329232Gordon Ross		bcopy(array, new_array, ss->n_items * sizeof (void *));
1668329232Gordon Ross
1678329232Gordon Ross		/*
1688329232Gordon Ross		 * Free the old array now.  Note that
1698329232Gordon Ross		 * ddi_get_soft_state takes the mutex.
1708329232Gordon Ross		 */
1718329232Gordon Ross		kmem_free(ss->array, ss->n_items * sizeof (void *));
1728329232Gordon Ross
1738329232Gordon Ross		ss->array = (array = new_array);
1748329232Gordon Ross		ss->n_items = new_n_items;
1758329232Gordon Ross	}
1768329232Gordon Ross
1778329232Gordon Ross	ASSERT(array != NULL && item < ss->n_items && array[item] == NULL);
1788329232Gordon Ross
1798329232Gordon Ross	array[item] = new_element;
1808329232Gordon Ross
1818329232Gordon Ross	mutex_exit(&ss->lock);
1828329232Gordon Ross	return (DDI_SUCCESS);
1838329232Gordon Ross}
1848329232Gordon Ross
1858329232Gordon Rossvoid
1868329232Gordon Rossddi_soft_state_free(void *state, int item)
1878329232Gordon Ross{
1888329232Gordon Ross	i_ddi_soft_state	*ss = (i_ddi_soft_state *)state;
1898329232Gordon Ross	void			**array;
1908329232Gordon Ross	void			*element;
1918329232Gordon Ross	static char		msg[] = "ddi_soft_state_free:";
1928329232Gordon Ross
1938329232Gordon Ross	if (ss == NULL) {
1948329232Gordon Ross		cmn_err(CE_WARN, "%s null handle: %s",
1958329232Gordon Ross		    msg, "fake");
1968329232Gordon Ross		return;
1978329232Gordon Ross	}
1988329232Gordon Ross
1998329232Gordon Ross	element = NULL;
2008329232Gordon Ross
2018329232Gordon Ross	mutex_enter(&ss->lock);
2028329232Gordon Ross
2038329232Gordon Ross	if ((array = ss->array) == NULL || ss->size == 0) {
2048329232Gordon Ross		cmn_err(CE_WARN, "%s bad handle: %s",
2058329232Gordon Ross		    msg, "fake");
2068329232Gordon Ross	} else if (item < 0 || item >= ss->n_items) {
2078329232Gordon Ross		cmn_err(CE_WARN, "%s item %d not in range [0..%lu]: %s",
2088329232Gordon Ross		    msg, item, (ulong_t)ss->n_items - 1, "fake");
2098329232Gordon Ross	} else if (array[item] != NULL) {
2108329232Gordon Ross		element = array[item];
2118329232Gordon Ross		array[item] = NULL;
2128329232Gordon Ross	}
2138329232Gordon Ross
2148329232Gordon Ross	mutex_exit(&ss->lock);
2158329232Gordon Ross
2168329232Gordon Ross	if (element)
2178329232Gordon Ross		kmem_free(element, ss->size);
2188329232Gordon Ross}
2198329232Gordon Ross
2208329232Gordon Ross/*
2218329232Gordon Ross * Free the entire set of pointers, and any
2228329232Gordon Ross * soft state structures contained therein.
2238329232Gordon Ross *
2248329232Gordon Ross * Note that we don't grab the ss->lock mutex, even though
2258329232Gordon Ross * we're inspecting the various fields of the data structure.
2268329232Gordon Ross *
2278329232Gordon Ross * There is an implicit assumption that this routine will
2288329232Gordon Ross * never run concurrently with any of the above on this
2298329232Gordon Ross * particular state structure i.e. by the time the driver
2308329232Gordon Ross * calls this routine, there should be no other threads
2318329232Gordon Ross * running in the driver.
2328329232Gordon Ross */
2338329232Gordon Rossvoid
2348329232Gordon Rossddi_soft_state_fini(void **state_p)
2358329232Gordon Ross{
2368329232Gordon Ross	i_ddi_soft_state	*ss;
2378329232Gordon Ross	int			item;
2388329232Gordon Ross	static char		msg[] = "ddi_soft_state_fini:";
2398329232Gordon Ross
2408329232Gordon Ross	if (state_p == NULL ||
2418329232Gordon Ross	    (ss = (i_ddi_soft_state *)(*state_p)) == NULL) {
2428329232Gordon Ross		cmn_err(CE_WARN, "%s null handle: %s",
2438329232Gordon Ross		    msg, "fake");
2448329232Gordon Ross		return;
2458329232Gordon Ross	}
2468329232Gordon Ross
2478329232Gordon Ross	if (ss->size == 0) {
2488329232Gordon Ross		cmn_err(CE_WARN, "%s bad handle: %s",
2498329232Gordon Ross		    msg, "fake");
2508329232Gordon Ross		return;
2518329232Gordon Ross	}
2528329232Gordon Ross
2538329232Gordon Ross	if (ss->n_items > 0) {
2548329232Gordon Ross		for (item = 0; item < ss->n_items; item++)
2558329232Gordon Ross			ddi_soft_state_free(ss, item);
2568329232Gordon Ross		kmem_free(ss->array, ss->n_items * sizeof (void *));
2578329232Gordon Ross	}
2588329232Gordon Ross
2598329232Gordon Ross	mutex_destroy(&ss->lock);
2608329232Gordon Ross	kmem_free(ss, sizeof (*ss));
2618329232Gordon Ross
2628329232Gordon Ross	*state_p = NULL;
2638329232Gordon Ross}