[6319] | 1 | /** @file wlan_mac_pkt_buf_util.c |
---|
| 2 | * @brief Packet Buffer Communication Framework |
---|
| 3 | * |
---|
| 4 | * This contains code common to both CPU_LOW and CPU_HIGH that allows them |
---|
| 5 | * to pass messages to one another. |
---|
| 6 | * |
---|
| 7 | * @copyright Copyright 2013-2019, Mango Communications. All rights reserved. |
---|
| 8 | * Distributed under the Mango Communications Reference Design License |
---|
| 9 | * See LICENSE.txt included in the design archive or |
---|
| 10 | * at http://mangocomm.com/802.11/license |
---|
| 11 | * |
---|
| 12 | * This file is part of the Mango 802.11 Reference Design (https://mangocomm.com/802.11) |
---|
| 13 | */ |
---|
| 14 | |
---|
| 15 | /***************************** Include Files *********************************/ |
---|
| 16 | |
---|
| 17 | #include "xmutex.h" |
---|
| 18 | |
---|
| 19 | #include "wlan_mac_common.h" |
---|
| 20 | #include "wlan_platform_common.h" |
---|
| 21 | #include "wlan_mac_pkt_buf_util.h" |
---|
| 22 | |
---|
| 23 | /*********************** Global Variable Definitions *************************/ |
---|
| 24 | |
---|
| 25 | /*************************** Variable Definitions ****************************/ |
---|
| 26 | |
---|
| 27 | static XMutex pkt_buf_mutex; |
---|
| 28 | static platform_common_dev_info_t platform_common_dev_info; |
---|
| 29 | |
---|
| 30 | |
---|
| 31 | /*************************** Functions Prototypes ****************************/ |
---|
| 32 | |
---|
| 33 | |
---|
| 34 | /******************************** Functions **********************************/ |
---|
| 35 | |
---|
| 36 | int init_pkt_buf() { |
---|
| 37 | u32 i; |
---|
| 38 | XMutex_Config* mutex_config_ptr; |
---|
| 39 | |
---|
| 40 | platform_common_dev_info = wlan_platform_common_get_dev_info(); |
---|
| 41 | |
---|
| 42 | //Initialize the pkt buffer mutex core |
---|
| 43 | mutex_config_ptr = XMutex_LookupConfig(platform_common_dev_info.pkt_buf_mutex_dev_id); |
---|
| 44 | XMutex_CfgInitialize(&pkt_buf_mutex, mutex_config_ptr, mutex_config_ptr->BaseAddress); |
---|
| 45 | |
---|
| 46 | // Unlock all mutexes this CPU might own at boot |
---|
| 47 | // Most unlocks will fail harmlessly, but this helps cleanup state on soft reset |
---|
| 48 | for (i = 0; i < NUM_TX_PKT_BUFS; i++) { |
---|
| 49 | unlock_tx_pkt_buf(i); |
---|
| 50 | } |
---|
| 51 | for (i = 0; i < NUM_RX_PKT_BUFS; i++) { |
---|
| 52 | unlock_rx_pkt_buf(i); |
---|
| 53 | } |
---|
| 54 | |
---|
| 55 | return WLAN_SUCCESS; |
---|
| 56 | } |
---|
| 57 | |
---|
| 58 | |
---|
| 59 | |
---|
| 60 | /************** Pkt Buffer Mutex Management ************/ |
---|
| 61 | int lock_tx_pkt_buf(u8 pkt_buf_ind) { |
---|
| 62 | int status; |
---|
| 63 | |
---|
| 64 | //Check inputs |
---|
| 65 | if(pkt_buf_ind >= NUM_TX_PKT_BUFS) |
---|
| 66 | return PKT_BUF_MUTEX_FAIL_INVALID_BUF; |
---|
| 67 | |
---|
| 68 | status = XMutex_Trylock(&pkt_buf_mutex, (pkt_buf_ind + PKT_BUF_MUTEX_TX_BASE)); |
---|
| 69 | |
---|
| 70 | if(status == XST_SUCCESS) |
---|
| 71 | return PKT_BUF_MUTEX_SUCCESS; |
---|
| 72 | else |
---|
| 73 | return PKT_BUF_MUTEX_FAIL_ALREADY_LOCKED; |
---|
| 74 | } |
---|
| 75 | |
---|
| 76 | int lock_rx_pkt_buf(u8 pkt_buf_ind) { |
---|
| 77 | int status; |
---|
| 78 | |
---|
| 79 | //Check inputs |
---|
| 80 | if(pkt_buf_ind >= NUM_RX_PKT_BUFS) |
---|
| 81 | return PKT_BUF_MUTEX_FAIL_INVALID_BUF; |
---|
| 82 | |
---|
| 83 | |
---|
| 84 | status = XMutex_Trylock(&pkt_buf_mutex, (pkt_buf_ind + PKT_BUF_MUTEX_RX_BASE)); |
---|
| 85 | |
---|
| 86 | if(status == XST_SUCCESS) |
---|
| 87 | return PKT_BUF_MUTEX_SUCCESS; |
---|
| 88 | else |
---|
| 89 | return PKT_BUF_MUTEX_FAIL_ALREADY_LOCKED; |
---|
| 90 | } |
---|
| 91 | |
---|
| 92 | int force_lock_tx_pkt_buf(u8 pkt_buf_ind) { |
---|
| 93 | u32 UnLockPattern; |
---|
| 94 | |
---|
| 95 | //Check inputs |
---|
| 96 | if(pkt_buf_ind >= NUM_TX_PKT_BUFS) |
---|
| 97 | return PKT_BUF_MUTEX_FAIL_INVALID_BUF; |
---|
| 98 | |
---|
| 99 | //Unlock the packet buffer |
---|
| 100 | if(unlock_tx_pkt_buf(pkt_buf_ind) == PKT_BUF_MUTEX_FAIL_NOT_LOCK_OWNER ){ |
---|
| 101 | //Force an unlock as the other CPU_ID |
---|
| 102 | UnLockPattern = (((platform_common_dev_info.cpu_id+1)%2 << OWNER_SHIFT) | 0x0); |
---|
| 103 | |
---|
| 104 | XMutex_ReadReg(pkt_buf_mutex.Config.BaseAddress, (pkt_buf_ind + PKT_BUF_MUTEX_TX_BASE), |
---|
| 105 | XMU_MUTEX_REG_OFFSET); |
---|
| 106 | |
---|
| 107 | XMutex_WriteReg(pkt_buf_mutex.Config.BaseAddress, (pkt_buf_ind + PKT_BUF_MUTEX_TX_BASE), |
---|
| 108 | XMU_MUTEX_REG_OFFSET, UnLockPattern); |
---|
| 109 | |
---|
| 110 | } |
---|
| 111 | |
---|
| 112 | //At this point in the function, the packet buffer will be unlocked regardless of who originally |
---|
| 113 | //owned an existing lock. |
---|
| 114 | |
---|
| 115 | return lock_tx_pkt_buf(pkt_buf_ind); |
---|
| 116 | } |
---|
| 117 | |
---|
| 118 | int force_lock_rx_pkt_buf(u8 pkt_buf_ind) { |
---|
| 119 | u32 UnLockPattern; |
---|
| 120 | |
---|
| 121 | //Check inputs |
---|
| 122 | if(pkt_buf_ind >= NUM_RX_PKT_BUFS) |
---|
| 123 | return PKT_BUF_MUTEX_FAIL_INVALID_BUF; |
---|
| 124 | |
---|
| 125 | //Unlock the packet buffer |
---|
| 126 | if(unlock_rx_pkt_buf(pkt_buf_ind) == PKT_BUF_MUTEX_FAIL_NOT_LOCK_OWNER ){ |
---|
| 127 | //Force an unlock as the other CPU_ID |
---|
| 128 | UnLockPattern = (((platform_common_dev_info.cpu_id+1)%2 << OWNER_SHIFT) | 0x0); |
---|
| 129 | |
---|
| 130 | XMutex_ReadReg(pkt_buf_mutex.Config.BaseAddress, (pkt_buf_ind + PKT_BUF_MUTEX_RX_BASE), |
---|
| 131 | XMU_MUTEX_REG_OFFSET); |
---|
| 132 | |
---|
| 133 | XMutex_WriteReg(pkt_buf_mutex.Config.BaseAddress, (pkt_buf_ind + PKT_BUF_MUTEX_RX_BASE), |
---|
| 134 | XMU_MUTEX_REG_OFFSET, UnLockPattern); |
---|
| 135 | |
---|
| 136 | } |
---|
| 137 | |
---|
| 138 | //At this point in the function, the packet buffer will be unlocked regardless of who originally |
---|
| 139 | //owned an existing lock. |
---|
| 140 | |
---|
| 141 | return lock_rx_pkt_buf(pkt_buf_ind); |
---|
| 142 | } |
---|
| 143 | |
---|
| 144 | int unlock_tx_pkt_buf(u8 pkt_buf_ind) { |
---|
| 145 | int status; |
---|
| 146 | |
---|
| 147 | //Check inputs |
---|
| 148 | if(pkt_buf_ind >= NUM_TX_PKT_BUFS) |
---|
| 149 | return PKT_BUF_MUTEX_FAIL_INVALID_BUF; |
---|
| 150 | |
---|
| 151 | if(XMutex_IsLocked(&pkt_buf_mutex, (pkt_buf_ind + PKT_BUF_MUTEX_TX_BASE))){ |
---|
| 152 | status = XMutex_Unlock(&pkt_buf_mutex, (pkt_buf_ind + PKT_BUF_MUTEX_TX_BASE)); |
---|
| 153 | |
---|
| 154 | if(status == XST_SUCCESS) |
---|
| 155 | return PKT_BUF_MUTEX_SUCCESS; |
---|
| 156 | else |
---|
| 157 | return PKT_BUF_MUTEX_FAIL_NOT_LOCK_OWNER; |
---|
| 158 | } else { |
---|
| 159 | return PKT_BUF_MUTEX_ALREADY_UNLOCKED; |
---|
| 160 | } |
---|
| 161 | } |
---|
| 162 | |
---|
| 163 | int unlock_rx_pkt_buf(u8 pkt_buf_ind) { |
---|
| 164 | int status; |
---|
| 165 | |
---|
| 166 | //Check inputs |
---|
| 167 | if(pkt_buf_ind >= NUM_RX_PKT_BUFS) |
---|
| 168 | return PKT_BUF_MUTEX_FAIL_INVALID_BUF; |
---|
| 169 | |
---|
| 170 | status = XMutex_Unlock(&pkt_buf_mutex, (pkt_buf_ind + PKT_BUF_MUTEX_RX_BASE)); |
---|
| 171 | |
---|
| 172 | if(status == XST_SUCCESS) |
---|
| 173 | return PKT_BUF_MUTEX_SUCCESS; |
---|
| 174 | else |
---|
| 175 | return PKT_BUF_MUTEX_FAIL_NOT_LOCK_OWNER; |
---|
| 176 | } |
---|
| 177 | |
---|
| 178 | int get_tx_pkt_buf_status(u8 pkt_buf_ind, u32 * locked, u32 * owner){ |
---|
| 179 | |
---|
| 180 | //Check inputs |
---|
| 181 | if(pkt_buf_ind >= NUM_TX_PKT_BUFS) |
---|
| 182 | return PKT_BUF_MUTEX_FAIL_INVALID_BUF; |
---|
| 183 | |
---|
| 184 | XMutex_GetStatus(&pkt_buf_mutex, (pkt_buf_ind + PKT_BUF_MUTEX_TX_BASE), locked, owner); |
---|
| 185 | |
---|
| 186 | return PKT_BUF_MUTEX_SUCCESS; |
---|
| 187 | } |
---|
| 188 | |
---|
| 189 | int get_rx_pkt_buf_status(u8 pkt_buf_ind, u32 * locked, u32 * owner){ |
---|
| 190 | |
---|
| 191 | //Check inputs |
---|
| 192 | if(pkt_buf_ind >= NUM_RX_PKT_BUFS) |
---|
| 193 | return PKT_BUF_MUTEX_FAIL_INVALID_BUF; |
---|
| 194 | |
---|
| 195 | XMutex_GetStatus(&pkt_buf_mutex, (pkt_buf_ind + PKT_BUF_MUTEX_RX_BASE), locked, owner); |
---|
| 196 | |
---|
| 197 | return PKT_BUF_MUTEX_SUCCESS; |
---|
| 198 | } |
---|
| 199 | |
---|