#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: dm_table.c,v 1.21 2021/08/21 22:23:33 andvar Exp $");
#include <sys/types.h>
#include <sys/param.h>
#include <sys/kmem.h>
#include "dm.h"
static int dm_table_busy(dm_table_head_t *, uint8_t);
static void dm_table_unbusy(dm_table_head_t *);
static void dm_table_free_deps(dm_table_entry_t *);
static int
dm_table_busy(dm_table_head_t *head, uint8_t table_id)
{
uint8_t id;
mutex_enter(&head->table_mtx);
if (table_id == DM_TABLE_ACTIVE)
id = head->cur_active_table;
else
id = 1 - head->cur_active_table;
head->io_cnt++;
mutex_exit(&head->table_mtx);
return id;
}
static void
dm_table_unbusy(dm_table_head_t *head)
{
KASSERT(head->io_cnt != 0);
mutex_enter(&head->table_mtx);
if (--head->io_cnt == 0)
cv_broadcast(&head->table_cv);
mutex_exit(&head->table_mtx);
}
dm_table_t *
dm_table_get_entry(dm_table_head_t *head, uint8_t table_id)
{
uint8_t id;
id = dm_table_busy(head, table_id);
return &head->tables[id];
}
void
dm_table_release(dm_table_head_t *head, uint8_t table_id)
{
dm_table_unbusy(head);
}
void
dm_table_switch_tables(dm_table_head_t *head)
{
mutex_enter(&head->table_mtx);
while (head->io_cnt != 0)
cv_wait(&head->table_cv, &head->table_mtx);
head->cur_active_table = 1 - head->cur_active_table;
mutex_exit(&head->table_mtx);
}
int
dm_table_destroy(dm_table_head_t *head, uint8_t table_id)
{
dm_table_t *tbl;
dm_table_entry_t *table_en;
uint8_t id;
mutex_enter(&head->table_mtx);
aprint_debug("dm_Table_destroy called with %d--%d\n", table_id, head->io_cnt);
while (head->io_cnt != 0)
cv_wait(&head->table_cv, &head->table_mtx);
if (table_id == DM_TABLE_ACTIVE)
id = head->cur_active_table;
else
id = 1 - head->cur_active_table;
tbl = &head->tables[id];
while ((table_en = SLIST_FIRST(tbl)) != NULL) {
SLIST_REMOVE(tbl, table_en, dm_table_entry, next);
if (table_en->target->destroy(table_en) == 0)
table_en->target_config = NULL;
dm_table_free_deps(table_en);
kmem_free(table_en, sizeof(*table_en));
}
KASSERT(SLIST_EMPTY(tbl));
mutex_exit(&head->table_mtx);
return 0;
}
static uint64_t
dm_table_size_impl(dm_table_head_t *head, int table)
{
dm_table_t *tbl;
dm_table_entry_t *table_en;
uint64_t length;
uint8_t id;
length = 0;
id = dm_table_busy(head, table);
tbl = &head->tables[id];
SLIST_FOREACH(table_en, tbl, next)
length += table_en->length;
dm_table_unbusy(head);
return length;
}
uint64_t
dm_table_size(dm_table_head_t *head)
{
return dm_table_size_impl(head, DM_TABLE_ACTIVE);
}
uint64_t
dm_inactive_table_size(dm_table_head_t *head)
{
return dm_table_size_impl(head, DM_TABLE_INACTIVE);
}
void
dm_table_disksize(dm_table_head_t *head, uint64_t *numsecp,
unsigned int *secsizep)
{
dm_table_t *tbl;
dm_table_entry_t *table_en;
uint64_t length;
unsigned int secsize, tsecsize;
uint8_t id;
length = 0;
id = dm_table_busy(head, DM_TABLE_ACTIVE);
tbl = &head->tables[id];
secsize = 0;
SLIST_FOREACH(table_en, tbl, next) {
length += table_en->length;
if (table_en->target->secsize)
table_en->target->secsize(table_en, &tsecsize);
else
tsecsize = 0;
if (secsize < tsecsize)
secsize = tsecsize;
}
if (numsecp)
*numsecp = secsize > 0 ? dbtob(length) / secsize : 0;
if (secsizep)
*secsizep = secsize;
dm_table_unbusy(head);
}
int
dm_table_get_target_count(dm_table_head_t *head, uint8_t table_id)
{
dm_table_entry_t *table_en;
dm_table_t *tbl;
uint32_t target_count;
uint8_t id;
target_count = 0;
id = dm_table_busy(head, table_id);
tbl = &head->tables[id];
SLIST_FOREACH(table_en, tbl, next)
target_count++;
dm_table_unbusy(head);
return target_count;
}
void
dm_table_head_init(dm_table_head_t *head)
{
head->cur_active_table = 0;
head->io_cnt = 0;
SLIST_INIT(&head->tables[0]);
SLIST_INIT(&head->tables[1]);
mutex_init(&head->table_mtx, MUTEX_DEFAULT, IPL_NONE);
cv_init(&head->table_cv, "dm_io");
}
void
dm_table_head_destroy(dm_table_head_t *head)
{
KASSERT(!mutex_owned(&head->table_mtx));
KASSERT(!cv_has_waiters(&head->table_cv));
KASSERT(head->io_cnt == 0);
cv_destroy(&head->table_cv);
mutex_destroy(&head->table_mtx);
}
int
dm_table_add_deps(dm_table_entry_t *table_en, dm_pdev_t *pdev)
{
dm_table_head_t *head;
dm_mapping_t *map;
if (!pdev)
return -1;
head = &table_en->dm_dev->table_head;
mutex_enter(&head->table_mtx);
TAILQ_FOREACH(map, &table_en->pdev_maps, next) {
if (map->data.pdev->pdev_vnode->v_rdev ==
pdev->pdev_vnode->v_rdev) {
mutex_exit(&head->table_mtx);
return -1;
}
}
map = kmem_alloc(sizeof(*map), KM_SLEEP);
map->data.pdev = pdev;
aprint_debug("%s: %s\n", __func__, pdev->name);
TAILQ_INSERT_TAIL(&table_en->pdev_maps, map, next);
mutex_exit(&head->table_mtx);
return 0;
}
static void
dm_table_free_deps(dm_table_entry_t *table_en)
{
dm_mapping_t *map;
while ((map = TAILQ_FIRST(&table_en->pdev_maps)) != NULL) {
TAILQ_REMOVE(&table_en->pdev_maps, map, next);
aprint_debug("%s: %s\n", __func__, map->data.pdev->name);
kmem_free(map, sizeof(*map));
}
KASSERT(TAILQ_EMPTY(&table_en->pdev_maps));
}