1 /* 2 * Copyright (c) 2015-2019 The Linux Foundation. All rights reserved. 3 * 4 * Permission to use, copy, modify, and/or distribute this software for 5 * any purpose with or without fee is hereby granted, provided that the 6 * above copyright notice and this permission notice appear in all 7 * copies. 8 * 9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL 10 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED 11 * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE 12 * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL 13 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR 14 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER 15 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 16 * PERFORMANCE OF THIS SOFTWARE. 17 */ 18 19 #include "targcfg.h" 20 #include "qdf_lock.h" 21 #include "qdf_status.h" 22 #include "qdf_status.h" 23 #include <qdf_atomic.h> /* qdf_atomic_read */ 24 #include <targaddrs.h> 25 #include "hif_io32.h" 26 #include <hif.h> 27 #include "regtable.h" 28 #define ATH_MODULE_NAME hif 29 #include <a_debug.h> 30 #include "hif_main.h" 31 #include "ce_api.h" 32 #include "ce_bmi.h" 33 #include "qdf_trace.h" 34 #include "hif_debug.h" 35 #include "bmi_msg.h" 36 #include "qdf_module.h" 37 38 /* Track a BMI transaction that is in progress */ 39 #ifndef BIT 40 #define BIT(n) (1 << (n)) 41 #endif 42 43 enum { 44 BMI_REQ_SEND_DONE = BIT(0), /* the bmi tx completion */ 45 BMI_RESP_RECV_DONE = BIT(1), /* the bmi respond is received */ 46 }; 47 48 struct BMI_transaction { 49 struct HIF_CE_state *hif_state; 50 qdf_semaphore_t bmi_transaction_sem; 51 uint8_t *bmi_request_host; /* Req BMI msg in Host addr space */ 52 qdf_dma_addr_t bmi_request_CE; /* Req BMI msg in CE addr space */ 53 uint32_t bmi_request_length; /* Length of BMI request */ 54 uint8_t *bmi_response_host; /* Rsp BMI msg in Host addr space */ 55 qdf_dma_addr_t bmi_response_CE; /* Rsp BMI msg in CE addr space */ 56 unsigned int bmi_response_length; /* Length of received response */ 57 unsigned int bmi_timeout_ms; 58 uint32_t bmi_transaction_flags; /* flags for the transcation */ 59 }; 60 61 /* 62 * send/recv completion functions for BMI. 63 * NB: The "net_buf" parameter is actually just a 64 * straight buffer, not an sk_buff. 65 */ 66 void hif_bmi_send_done(struct CE_handle *copyeng, void *ce_context, 67 void *transfer_context, qdf_dma_addr_t data, 68 unsigned int nbytes, 69 unsigned int transfer_id, unsigned int sw_index, 70 unsigned int hw_index, uint32_t toeplitz_hash_result) 71 { 72 struct BMI_transaction *transaction = 73 (struct BMI_transaction *)transfer_context; 74 75 #ifdef BMI_RSP_POLLING 76 /* 77 * Fix EV118783, Release a semaphore after sending 78 * no matter whether a response is been expecting now. 79 */ 80 qdf_semaphore_release(&transaction->bmi_transaction_sem); 81 #else 82 /* 83 * If a response is anticipated, we'll complete the 84 * transaction if the response has been received. 85 * If no response is anticipated, complete the 86 * transaction now. 87 */ 88 transaction->bmi_transaction_flags |= BMI_REQ_SEND_DONE; 89 90 /* resp is't needed or has already been received, 91 * never assume resp comes later then this 92 */ 93 if (!transaction->bmi_response_CE || 94 (transaction->bmi_transaction_flags & BMI_RESP_RECV_DONE)) { 95 qdf_semaphore_release(&transaction->bmi_transaction_sem); 96 } 97 #endif 98 } 99 100 #ifndef BMI_RSP_POLLING 101 void hif_bmi_recv_data(struct CE_handle *copyeng, void *ce_context, 102 void *transfer_context, qdf_dma_addr_t data, 103 unsigned int nbytes, 104 unsigned int transfer_id, unsigned int flags) 105 { 106 struct BMI_transaction *transaction = 107 (struct BMI_transaction *)transfer_context; 108 109 transaction->bmi_response_length = nbytes; 110 transaction->bmi_transaction_flags |= BMI_RESP_RECV_DONE; 111 112 /* when both send/recv are done, the sem can be released */ 113 if (transaction->bmi_transaction_flags & BMI_REQ_SEND_DONE) 114 qdf_semaphore_release(&transaction->bmi_transaction_sem); 115 } 116 #endif 117 118 /* Timeout for BMI message exchange */ 119 #define HIF_EXCHANGE_BMI_MSG_TIMEOUT 6000 120 121 QDF_STATUS hif_exchange_bmi_msg(struct hif_opaque_softc *hif_ctx, 122 qdf_dma_addr_t bmi_cmd_da, 123 qdf_dma_addr_t bmi_rsp_da, 124 uint8_t *bmi_request, 125 uint32_t request_length, 126 uint8_t *bmi_response, 127 uint32_t *bmi_response_lengthp, 128 uint32_t TimeoutMS) 129 { 130 struct hif_softc *scn = HIF_GET_SOFTC(hif_ctx); 131 struct HIF_CE_state *hif_state = HIF_GET_CE_STATE(hif_ctx); 132 struct HIF_CE_pipe_info *send_pipe_info = 133 &(hif_state->pipe_info[BMI_CE_NUM_TO_TARG]); 134 struct CE_handle *ce_send_hdl = send_pipe_info->ce_hdl; 135 qdf_dma_addr_t CE_request, CE_response = 0; 136 struct BMI_transaction *transaction = NULL; 137 int status = QDF_STATUS_SUCCESS; 138 struct HIF_CE_pipe_info *recv_pipe_info = 139 &(hif_state->pipe_info[BMI_CE_NUM_TO_HOST]); 140 struct CE_handle *ce_recv = recv_pipe_info->ce_hdl; 141 unsigned int mux_id = 0; 142 unsigned int transaction_id = 0xffff; 143 unsigned int user_flags = 0; 144 #ifdef BMI_RSP_POLLING 145 qdf_dma_addr_t buf; 146 unsigned int completed_nbytes, id, flags; 147 int i; 148 #endif 149 150 transaction = 151 (struct BMI_transaction *)qdf_mem_malloc(sizeof(*transaction)); 152 if (unlikely(!transaction)) 153 return QDF_STATUS_E_NOMEM; 154 155 transaction_id = (mux_id & MUX_ID_MASK) | 156 (transaction_id & TRANSACTION_ID_MASK); 157 #ifdef QCA_WIFI_3_0 158 user_flags &= DESC_DATA_FLAG_MASK; 159 #endif 160 A_TARGET_ACCESS_LIKELY(scn); 161 162 /* Initialize bmi_transaction_sem to block */ 163 qdf_semaphore_init(&transaction->bmi_transaction_sem); 164 qdf_semaphore_acquire(&transaction->bmi_transaction_sem); 165 166 transaction->hif_state = hif_state; 167 transaction->bmi_request_host = bmi_request; 168 transaction->bmi_request_length = request_length; 169 transaction->bmi_response_length = 0; 170 transaction->bmi_timeout_ms = TimeoutMS; 171 transaction->bmi_transaction_flags = 0; 172 173 /* 174 * CE_request = dma_map_single(dev, 175 * (void *)bmi_request, request_length, DMA_TO_DEVICE); 176 */ 177 CE_request = bmi_cmd_da; 178 transaction->bmi_request_CE = CE_request; 179 180 if (bmi_response) { 181 182 /* 183 * CE_response = dma_map_single(dev, bmi_response, 184 * BMI_DATASZ_MAX, DMA_FROM_DEVICE); 185 */ 186 CE_response = bmi_rsp_da; 187 transaction->bmi_response_host = bmi_response; 188 transaction->bmi_response_CE = CE_response; 189 /* dma_cache_sync(dev, bmi_response, 190 * BMI_DATASZ_MAX, DMA_FROM_DEVICE); 191 */ 192 qdf_mem_dma_sync_single_for_device(scn->qdf_dev, 193 CE_response, 194 BMI_DATASZ_MAX, 195 DMA_FROM_DEVICE); 196 ce_recv_buf_enqueue(ce_recv, transaction, 197 transaction->bmi_response_CE); 198 /* NB: see HIF_BMI_recv_done */ 199 } else { 200 transaction->bmi_response_host = NULL; 201 transaction->bmi_response_CE = 0; 202 } 203 204 /* dma_cache_sync(dev, bmi_request, request_length, DMA_TO_DEVICE); */ 205 qdf_mem_dma_sync_single_for_device(scn->qdf_dev, CE_request, 206 request_length, DMA_TO_DEVICE); 207 208 status = 209 ce_send(ce_send_hdl, transaction, 210 CE_request, request_length, 211 transaction_id, 0, user_flags); 212 ASSERT(status == QDF_STATUS_SUCCESS); 213 /* NB: see hif_bmi_send_done */ 214 215 /* TBDXXX: handle timeout */ 216 217 /* Wait for BMI request/response transaction to complete */ 218 /* Always just wait for BMI request here if 219 * BMI_RSP_POLLING is defined 220 */ 221 if (qdf_semaphore_acquire_timeout 222 (&transaction->bmi_transaction_sem, 223 HIF_EXCHANGE_BMI_MSG_TIMEOUT)) { 224 HIF_ERROR("%s: Fatal error, BMI transaction timeout. Please check the HW interface!!", 225 __func__); 226 qdf_mem_free(transaction); 227 return QDF_STATUS_E_TIMEOUT; 228 } 229 230 if (bmi_response) { 231 #ifdef BMI_RSP_POLLING 232 /* Fix EV118783, do not wait a semaphore for the BMI response 233 * since the relative interruption may be lost. 234 * poll the BMI response instead. 235 */ 236 i = 0; 237 while (ce_completed_recv_next( 238 ce_recv, NULL, NULL, &buf, 239 &completed_nbytes, &id, 240 &flags) != QDF_STATUS_SUCCESS) { 241 if (i++ > BMI_RSP_TO_MILLISEC) { 242 HIF_ERROR("%s:error, can't get bmi response", 243 __func__); 244 status = QDF_STATUS_E_BUSY; 245 break; 246 } 247 OS_DELAY(1000); 248 } 249 250 if ((status == QDF_STATUS_SUCCESS) && bmi_response_lengthp) 251 *bmi_response_lengthp = completed_nbytes; 252 #else 253 if ((status == QDF_STATUS_SUCCESS) && bmi_response_lengthp) { 254 *bmi_response_lengthp = 255 transaction->bmi_response_length; 256 } 257 #endif 258 259 } 260 261 /* dma_unmap_single(dev, transaction->bmi_request_CE, 262 * request_length, DMA_TO_DEVICE); 263 * bus_unmap_single(scn->sc_osdev, 264 * transaction->bmi_request_CE, 265 * request_length, BUS_DMA_TODEVICE); 266 */ 267 268 if (status != QDF_STATUS_SUCCESS) { 269 qdf_dma_addr_t unused_buffer; 270 unsigned int unused_nbytes; 271 unsigned int unused_id; 272 unsigned int toeplitz_hash_result; 273 274 ce_cancel_send_next(ce_send_hdl, 275 NULL, NULL, &unused_buffer, 276 &unused_nbytes, &unused_id, 277 &toeplitz_hash_result); 278 } 279 280 A_TARGET_ACCESS_UNLIKELY(scn); 281 qdf_mem_free(transaction); 282 return status; 283 } 284 qdf_export_symbol(hif_exchange_bmi_msg); 285 286 #ifdef BMI_RSP_POLLING 287 #define BMI_RSP_CB_REGISTER 0 288 #else 289 #define BMI_RSP_CB_REGISTER 1 290 #endif 291 292 /** 293 * hif_register_bmi_callbacks() - register bmi callbacks 294 * @hif_sc: hif context 295 * 296 * Bmi phase uses different copy complete callbacks than mission mode. 297 */ 298 void hif_register_bmi_callbacks(struct hif_opaque_softc *hif_ctx) 299 { 300 struct HIF_CE_pipe_info *pipe_info; 301 struct hif_softc *hif_sc = HIF_GET_SOFTC(hif_ctx); 302 struct HIF_CE_state *hif_state = HIF_GET_CE_STATE(hif_sc); 303 304 /* 305 * Initially, establish CE completion handlers for use with BMI. 306 * These are overwritten with generic handlers after we exit BMI phase. 307 */ 308 pipe_info = &hif_state->pipe_info[BMI_CE_NUM_TO_TARG]; 309 ce_send_cb_register(pipe_info->ce_hdl, hif_bmi_send_done, pipe_info, 0); 310 311 if (BMI_RSP_CB_REGISTER) { 312 pipe_info = &hif_state->pipe_info[BMI_CE_NUM_TO_HOST]; 313 ce_recv_cb_register( 314 pipe_info->ce_hdl, hif_bmi_recv_data, pipe_info, 0); 315 } 316 } 317