Changeset View
Changeset View
Standalone View
Standalone View
sys/dev/smartpqi/smartpqi_tag.c
/*- | /*- | ||||
* Copyright (c) 2018 Microsemi Corporation. | * Copyright 2016-2021 Microchip Technology, Inc. and/or its subsidiaries. | ||||
* All rights reserved. | |||||
* | * | ||||
* Redistribution and use in source and binary forms, with or without | * Redistribution and use in source and binary forms, with or without | ||||
* modification, are permitted provided that the following conditions | * modification, are permitted provided that the following conditions | ||||
* are met: | * are met: | ||||
* 1. Redistributions of source code must retain the above copyright | * 1. Redistributions of source code must retain the above copyright | ||||
* notice, this list of conditions and the following disclaimer. | * notice, this list of conditions and the following disclaimer. | ||||
* 2. Redistributions in binary form must reproduce the above copyright | * 2. Redistributions in binary form must reproduce the above copyright | ||||
* notice, this list of conditions and the following disclaimer in the | * notice, this list of conditions and the following disclaimer in the | ||||
Show All 16 Lines | |||||
#include "smartpqi_includes.h" | #include "smartpqi_includes.h" | ||||
#ifndef LOCKFREE_STACK | #ifndef LOCKFREE_STACK | ||||
/* | /* | ||||
* Function used to release the tag from taglist. | * Function used to release the tag from taglist. | ||||
*/ | */ | ||||
void pqisrc_put_tag(pqi_taglist_t *taglist, uint32_t elem) | void | ||||
pqisrc_put_tag(pqi_taglist_t *taglist, uint32_t elem) | |||||
{ | { | ||||
OS_ACQUIRE_SPINLOCK(&(taglist->lock)); | OS_ACQUIRE_SPINLOCK(&(taglist->lock)); | ||||
/*DBG_FUNC("IN\n");*/ | DBG_FUNC("IN\n"); | ||||
ASSERT(taglist->num_elem < taglist->max_elem); | ASSERT(taglist->num_elem < taglist->max_elem); | ||||
if (taglist->num_elem < taglist->max_elem) { | if (taglist->num_elem < taglist->max_elem) { | ||||
taglist->elem_array[taglist->tail] = elem; | taglist->elem_array[taglist->tail] = elem; | ||||
taglist->num_elem++; | taglist->num_elem++; | ||||
taglist->tail = (taglist->tail + 1) % taglist->max_elem; | taglist->tail = (taglist->tail + 1) % taglist->max_elem; | ||||
} | } | ||||
OS_RELEASE_SPINLOCK(&taglist->lock); | OS_RELEASE_SPINLOCK(&taglist->lock); | ||||
/*DBG_FUNC("OUT\n");*/ | DBG_FUNC("OUT\n"); | ||||
} | } | ||||
/* | /* | ||||
* Function used to get an unoccupied tag from the tag list. | * Function used to get an unoccupied tag from the tag list. | ||||
*/ | */ | ||||
uint32_t pqisrc_get_tag(pqi_taglist_t *taglist) | uint32_t | ||||
pqisrc_get_tag(pqi_taglist_t *taglist) | |||||
{ | { | ||||
uint32_t elem = INVALID_ELEM; | uint32_t elem = INVALID_ELEM; | ||||
/*DBG_FUNC("IN\n");*/ | /* DBG_FUNC("IN\n");*/ | ||||
OS_ACQUIRE_SPINLOCK(&taglist->lock); | OS_ACQUIRE_SPINLOCK(&taglist->lock); | ||||
ASSERT(taglist->num_elem > 0); | ASSERT(taglist->num_elem > 0); | ||||
if (taglist->num_elem > 0) { | if (taglist->num_elem > 0) { | ||||
elem = taglist->elem_array[taglist->head]; | elem = taglist->elem_array[taglist->head]; | ||||
taglist->num_elem--; | taglist->num_elem--; | ||||
taglist->head = (taglist->head + 1) % taglist->max_elem; | taglist->head = (taglist->head + 1) % taglist->max_elem; | ||||
} | } | ||||
OS_RELEASE_SPINLOCK(&taglist->lock); | OS_RELEASE_SPINLOCK(&taglist->lock); | ||||
/*DBG_FUNC("OUT got %d\n", elem);*/ | /* DBG_FUNC("OUT got %d\n", elem);*/ | ||||
return elem; | return elem; | ||||
} | } | ||||
/* | /* | ||||
* Initialize circular queue implementation of tag list. | * Initialize circular queue implementation of tag list. | ||||
*/ | */ | ||||
int pqisrc_init_taglist(pqisrc_softstate_t *softs, pqi_taglist_t *taglist, | int | ||||
pqisrc_init_taglist(pqisrc_softstate_t *softs, pqi_taglist_t *taglist, | |||||
uint32_t max_elem) | uint32_t max_elem) | ||||
{ | { | ||||
int ret = PQI_STATUS_SUCCESS; | int ret = PQI_STATUS_SUCCESS; | ||||
int i = 0; | int i = 0; | ||||
DBG_FUNC("IN\n"); | DBG_FUNC("IN\n"); | ||||
taglist->max_elem = max_elem; | taglist->max_elem = max_elem; | ||||
taglist->num_elem = 0; | taglist->num_elem = 0; | ||||
taglist->head = 0; | taglist->head = 0; | ||||
taglist->tail = 0; | taglist->tail = 0; | ||||
taglist->elem_array = os_mem_alloc(softs, | taglist->elem_array = os_mem_alloc(softs, | ||||
(max_elem * sizeof(uint32_t))); | (max_elem * sizeof(uint32_t))); | ||||
if (!(taglist->elem_array)) { | if (!(taglist->elem_array)) { | ||||
DBG_FUNC("Unable to allocate memory for taglist\n"); | DBG_FUNC("Unable to allocate memory for taglist\n"); | ||||
ret = PQI_STATUS_FAILURE; | ret = PQI_STATUS_FAILURE; | ||||
goto err_out; | goto err_out; | ||||
} | } | ||||
os_strlcpy(taglist->lockname, "tag_lock", LOCKNAME_SIZE); | os_strlcpy(taglist->lockname, "tag_lock", LOCKNAME_SIZE); | ||||
ret = os_init_spinlock(softs, &taglist->lock, taglist->lockname); | ret = os_init_spinlock(softs, &taglist->lock, taglist->lockname); | ||||
if(ret){ | if(ret){ | ||||
DBG_ERR("tag lock initialization failed\n"); | DBG_ERR("tag lock initialization failed\n"); | ||||
taglist->lockcreated=false; | taglist->lockcreated=false; | ||||
goto err_lock; | goto err_lock; | ||||
} | } | ||||
taglist->lockcreated = true; | taglist->lockcreated = true; | ||||
/* indices 1 to max_elem are considered as valid tags */ | /* indices 1 to max_elem are considered as valid tags */ | ||||
for (i=1; i <= max_elem; i++) { | for (i=1; i <= max_elem; i++) { | ||||
softs->rcb[i].tag = INVALID_ELEM; | softs->rcb[i].tag = INVALID_ELEM; | ||||
pqisrc_put_tag(taglist, i); | pqisrc_put_tag(taglist, i); | ||||
} | } | ||||
DBG_FUNC("OUT\n"); | DBG_FUNC("OUT\n"); | ||||
return ret; | return ret; | ||||
err_lock: | err_lock: | ||||
os_mem_free(softs, (char *)taglist->elem_array, | os_mem_free(softs, (char *)taglist->elem_array, | ||||
(taglist->max_elem * sizeof(uint32_t))); | (taglist->max_elem * sizeof(uint32_t))); | ||||
taglist->elem_array = NULL; | taglist->elem_array = NULL; | ||||
err_out: | err_out: | ||||
DBG_FUNC("OUT failed\n"); | DBG_FUNC("OUT failed\n"); | ||||
return ret; | return ret; | ||||
} | } | ||||
/* | /* | ||||
* Destroy circular queue implementation of tag list. | * Destroy circular queue implementation of tag list. | ||||
*/ | */ | ||||
void pqisrc_destroy_taglist(pqisrc_softstate_t *softs, pqi_taglist_t *taglist) | void | ||||
pqisrc_destroy_taglist(pqisrc_softstate_t *softs, pqi_taglist_t *taglist) | |||||
{ | { | ||||
DBG_FUNC("IN\n"); | DBG_FUNC("IN\n"); | ||||
os_mem_free(softs, (char *)taglist->elem_array, | os_mem_free(softs, (char *)taglist->elem_array, | ||||
(taglist->max_elem * sizeof(uint32_t))); | (taglist->max_elem * sizeof(uint32_t))); | ||||
taglist->elem_array = NULL; | taglist->elem_array = NULL; | ||||
if(taglist->lockcreated==true){ | if(taglist->lockcreated==true){ | ||||
os_uninit_spinlock(&taglist->lock); | os_uninit_spinlock(&taglist->lock); | ||||
taglist->lockcreated = false; | taglist->lockcreated = false; | ||||
} | } | ||||
DBG_FUNC("OUT\n"); | DBG_FUNC("OUT\n"); | ||||
} | } | ||||
#else /* LOCKFREE_STACK */ | #else /* LOCKFREE_STACK */ | ||||
/* | /* | ||||
* Initialize circular queue implementation of tag list. | * Initialize circular queue implementation of tag list. | ||||
*/ | */ | ||||
int pqisrc_init_taglist(pqisrc_softstate_t *softs, lockless_stack_t *stack, | int | ||||
pqisrc_init_taglist(pqisrc_softstate_t *softs, lockless_stack_t *stack, | |||||
uint32_t max_elem) | uint32_t max_elem) | ||||
{ | { | ||||
int ret = PQI_STATUS_SUCCESS; | int ret = PQI_STATUS_SUCCESS; | ||||
int index = 0; | int index = 0; | ||||
DBG_FUNC("IN\n"); | DBG_FUNC("IN\n"); | ||||
/* indices 1 to max_elem are considered as valid tags */ | /* indices 1 to max_elem are considered as valid tags */ | ||||
stack->num_elements = max_elem + 1; | stack->max_elem = max_elem + 1; | ||||
stack->head.data = 0; | stack->head.data = 0; | ||||
DBG_INFO("Stack head address :%p\n",&stack->head); | DBG_INFO("Stack head address :%p\n",&stack->head); | ||||
/*Allocate memory for stack*/ | /*Allocate memory for stack*/ | ||||
stack->next_index_array = (uint32_t*)os_mem_alloc(softs, | stack->next_index_array = (uint32_t*)os_mem_alloc(softs, | ||||
(stack->num_elements * sizeof(uint32_t))); | (stack->max_elem * sizeof(uint32_t))); | ||||
if (!(stack->next_index_array)) { | if (!(stack->next_index_array)) { | ||||
DBG_ERR("Unable to allocate memory for stack\n"); | DBG_ERR("Unable to allocate memory for stack\n"); | ||||
ret = PQI_STATUS_FAILURE; | ret = PQI_STATUS_FAILURE; | ||||
goto err_out; | goto err_out; | ||||
} | } | ||||
/* push all the entries to the stack */ | /* push all the entries to the stack */ | ||||
for (index = 1; index < stack->num_elements ; index++) { | for (index = 1; index < stack->max_elem ; index++) { | ||||
softs->rcb[index].tag = INVALID_ELEM; | softs->rcb[index].tag = INVALID_ELEM; | ||||
pqisrc_put_tag(stack, index); | pqisrc_put_tag(stack, index); | ||||
} | } | ||||
DBG_FUNC("OUT\n"); | DBG_FUNC("OUT\n"); | ||||
return ret; | return ret; | ||||
err_out: | err_out: | ||||
DBG_FUNC("Failed OUT\n"); | DBG_FUNC("Failed OUT\n"); | ||||
return ret; | return ret; | ||||
} | } | ||||
/* | /* | ||||
* Destroy circular queue implementation of tag list. | * Destroy circular queue implementation of tag list. | ||||
*/ | */ | ||||
void pqisrc_destroy_taglist(pqisrc_softstate_t *softs, lockless_stack_t *stack) | void | ||||
pqisrc_destroy_taglist(pqisrc_softstate_t *softs, lockless_stack_t *stack) | |||||
{ | { | ||||
DBG_FUNC("IN\n"); | DBG_FUNC("IN\n"); | ||||
/* de-allocate stack memory */ | /* de-allocate stack memory */ | ||||
if (stack->next_index_array) { | if (stack->next_index_array) { | ||||
os_mem_free(softs,(char*)stack->next_index_array, | os_mem_free(softs,(char*)stack->next_index_array, | ||||
(stack->num_elements * sizeof(uint32_t))); | (stack->max_elem * sizeof(uint32_t))); | ||||
stack->next_index_array = NULL; | stack->next_index_array = NULL; | ||||
} | } | ||||
DBG_FUNC("OUT\n"); | DBG_FUNC("OUT\n"); | ||||
} | } | ||||
/* | /* | ||||
* Function used to release the tag from taglist. | * Function used to release the tag from taglist. | ||||
*/ | */ | ||||
void pqisrc_put_tag(lockless_stack_t *stack, uint32_t index) | void | ||||
pqisrc_put_tag(lockless_stack_t *stack, uint32_t index) | |||||
{ | { | ||||
union head_list cur_head, new_head; | union head_list cur_head, new_head; | ||||
DBG_FUNC("IN\n"); | DBG_FUNC("IN\n"); | ||||
DBG_INFO("push tag :%d\n",index); | DBG_INFO("push tag :%d\n",index); | ||||
if ( index >= stack->num_elements ) { | if (index >= stack->max_elem) { | ||||
ASSERT(false); | ASSERT(false); | ||||
DBG_ERR("Pushed Invalid index\n"); /* stack full */ | DBG_INFO("Pushed Invalid index\n"); /* stack full */ | ||||
return; | return; | ||||
} | } | ||||
if ( stack->next_index_array[index] != 0) { | if (stack->next_index_array[index] != 0) { | ||||
ASSERT(false); | ASSERT(false); | ||||
DBG_ERR("Index already present as tag in the stack\n"); | DBG_INFO("Index already present as tag in the stack\n"); | ||||
return; | return; | ||||
} | } | ||||
do { | do { | ||||
cur_head = stack->head; | cur_head = stack->head; | ||||
/* increment seq_no */ | /* increment seq_no */ | ||||
new_head.top.seq_no = cur_head.top.seq_no + 1; | new_head.top.seq_no = cur_head.top.seq_no + 1; | ||||
/* update the index at the top of the stack with the new index */ | /* update the index at the top of the stack with the new index */ | ||||
new_head.top.index = index; | new_head.top.index = index; | ||||
/* Create a link to the previous index */ | /* Create a link to the previous index */ | ||||
stack->next_index_array[index] = cur_head.top.index; | stack->next_index_array[index] = cur_head.top.index; | ||||
}while(OS_ATOMIC64_CAS(&stack->head.data,cur_head.data,new_head.data) | }while(!os_atomic64_cas(&stack->head.data,cur_head.data,new_head.data)); | ||||
!= cur_head.data); | stack->num_elem++; | ||||
DBG_FUNC("OUT\n"); | DBG_FUNC("OUT\n"); | ||||
return; | return; | ||||
} | } | ||||
/* | /* | ||||
* Function used to get an unoccupied tag from the tag list. | * Function used to get an unoccupied tag from the tag list. | ||||
*/ | */ | ||||
uint32_t pqisrc_get_tag(lockless_stack_t *stack) | uint32_t | ||||
pqisrc_get_tag(lockless_stack_t *stack) | |||||
{ | { | ||||
union head_list cur_head, new_head; | union head_list cur_head, new_head; | ||||
DBG_FUNC("IN\n"); | DBG_FUNC("IN\n"); | ||||
do { | do { | ||||
cur_head = stack->head; | cur_head = stack->head; | ||||
if (cur_head.top.index == 0) /* stack empty */ | if (cur_head.top.index == 0) /* stack empty */ | ||||
return INVALID_ELEM; | return INVALID_ELEM; | ||||
/* increment seq_no field */ | /* increment seq_no field */ | ||||
new_head.top.seq_no = cur_head.top.seq_no + 1; | new_head.top.seq_no = cur_head.top.seq_no + 1; | ||||
/* update the index at the top of the stack with the next index */ | /* update the index at the top of the stack with the next index */ | ||||
new_head.top.index = stack->next_index_array[cur_head.top.index]; | new_head.top.index = stack->next_index_array[cur_head.top.index]; | ||||
}while(OS_ATOMIC64_CAS(&stack->head.data,cur_head.data,new_head.data) | }while(!os_atomic64_cas(&stack->head.data,cur_head.data,new_head.data)); | ||||
!= cur_head.data); | |||||
stack->next_index_array[cur_head.top.index] = 0; | stack->next_index_array[cur_head.top.index] = 0; | ||||
stack->num_elem--; | |||||
DBG_INFO("pop tag: %d\n",cur_head.top.index); | DBG_INFO("pop tag: %d\n",cur_head.top.index); | ||||
DBG_FUNC("OUT\n"); | DBG_FUNC("OUT\n"); | ||||
return cur_head.top.index; /*tag*/ | return cur_head.top.index; /*tag*/ | ||||
} | } | ||||
#endif /* LOCKFREE_STACK */ | #endif /* LOCKFREE_STACK */ |