18be454c9SAlice Michael // SPDX-License-Identifier: GPL-2.0 28be454c9SAlice Michael /* Copyright(c) 2013 - 2018 Intel Corporation. */ 38be454c9SAlice Michael 48be454c9SAlice Michael #include "iavf_status.h" 58be454c9SAlice Michael #include "iavf_type.h" 68be454c9SAlice Michael #include "iavf_register.h" 78be454c9SAlice Michael #include "iavf_adminq.h" 88be454c9SAlice Michael #include "iavf_prototype.h" 98be454c9SAlice Michael 108be454c9SAlice Michael /** 11db950599SAlice Michael * iavf_alloc_adminq_asq_ring - Allocate Admin Queue send rings 128be454c9SAlice Michael * @hw: pointer to the hardware structure 138be454c9SAlice Michael **/ 14db950599SAlice Michael static enum iavf_status iavf_alloc_adminq_asq_ring(struct iavf_hw *hw) 158be454c9SAlice Michael { 1680754bbcSSergey Nemov enum iavf_status ret_code; 178be454c9SAlice Michael 188be454c9SAlice Michael ret_code = iavf_allocate_dma_mem(hw, &hw->aq.asq.desc_buf, 197af36e32SAlice Michael iavf_mem_atq_ring, 208be454c9SAlice Michael (hw->aq.num_asq_entries * 217af36e32SAlice Michael sizeof(struct iavf_aq_desc)), 228be454c9SAlice Michael IAVF_ADMINQ_DESC_ALIGNMENT); 238be454c9SAlice Michael if (ret_code) 248be454c9SAlice Michael return ret_code; 258be454c9SAlice Michael 268be454c9SAlice Michael ret_code = iavf_allocate_virt_mem(hw, &hw->aq.asq.cmd_buf, 278be454c9SAlice Michael (hw->aq.num_asq_entries * 287af36e32SAlice Michael sizeof(struct iavf_asq_cmd_details))); 298be454c9SAlice Michael if (ret_code) { 308be454c9SAlice Michael iavf_free_dma_mem(hw, &hw->aq.asq.desc_buf); 318be454c9SAlice Michael return ret_code; 328be454c9SAlice Michael } 338be454c9SAlice Michael 348be454c9SAlice Michael return ret_code; 358be454c9SAlice Michael } 368be454c9SAlice Michael 378be454c9SAlice Michael /** 38db950599SAlice Michael * iavf_alloc_adminq_arq_ring - Allocate Admin Queue receive rings 398be454c9SAlice Michael * @hw: pointer to the hardware structure 408be454c9SAlice Michael **/ 41db950599SAlice Michael static enum iavf_status iavf_alloc_adminq_arq_ring(struct iavf_hw *hw) 428be454c9SAlice Michael { 4380754bbcSSergey Nemov enum iavf_status ret_code; 448be454c9SAlice Michael 458be454c9SAlice Michael ret_code = iavf_allocate_dma_mem(hw, &hw->aq.arq.desc_buf, 467af36e32SAlice Michael iavf_mem_arq_ring, 478be454c9SAlice Michael (hw->aq.num_arq_entries * 487af36e32SAlice Michael sizeof(struct iavf_aq_desc)), 498be454c9SAlice Michael IAVF_ADMINQ_DESC_ALIGNMENT); 508be454c9SAlice Michael 518be454c9SAlice Michael return ret_code; 528be454c9SAlice Michael } 538be454c9SAlice Michael 548be454c9SAlice Michael /** 55db950599SAlice Michael * iavf_free_adminq_asq - Free Admin Queue send rings 568be454c9SAlice Michael * @hw: pointer to the hardware structure 578be454c9SAlice Michael * 588be454c9SAlice Michael * This assumes the posted send buffers have already been cleaned 598be454c9SAlice Michael * and de-allocated 608be454c9SAlice Michael **/ 61db950599SAlice Michael static void iavf_free_adminq_asq(struct iavf_hw *hw) 628be454c9SAlice Michael { 638be454c9SAlice Michael iavf_free_dma_mem(hw, &hw->aq.asq.desc_buf); 648be454c9SAlice Michael } 658be454c9SAlice Michael 668be454c9SAlice Michael /** 67db950599SAlice Michael * iavf_free_adminq_arq - Free Admin Queue receive rings 688be454c9SAlice Michael * @hw: pointer to the hardware structure 698be454c9SAlice Michael * 708be454c9SAlice Michael * This assumes the posted receive buffers have already been cleaned 718be454c9SAlice Michael * and de-allocated 728be454c9SAlice Michael **/ 73db950599SAlice Michael static void iavf_free_adminq_arq(struct iavf_hw *hw) 748be454c9SAlice Michael { 758be454c9SAlice Michael iavf_free_dma_mem(hw, &hw->aq.arq.desc_buf); 768be454c9SAlice Michael } 778be454c9SAlice Michael 788be454c9SAlice Michael /** 79db950599SAlice Michael * iavf_alloc_arq_bufs - Allocate pre-posted buffers for the receive queue 808be454c9SAlice Michael * @hw: pointer to the hardware structure 818be454c9SAlice Michael **/ 82db950599SAlice Michael static enum iavf_status iavf_alloc_arq_bufs(struct iavf_hw *hw) 838be454c9SAlice Michael { 847af36e32SAlice Michael struct iavf_aq_desc *desc; 858be454c9SAlice Michael struct iavf_dma_mem *bi; 8680754bbcSSergey Nemov enum iavf_status ret_code; 878be454c9SAlice Michael int i; 888be454c9SAlice Michael 898be454c9SAlice Michael /* We'll be allocating the buffer info memory first, then we can 908be454c9SAlice Michael * allocate the mapped buffers for the event processing 918be454c9SAlice Michael */ 928be454c9SAlice Michael 938be454c9SAlice Michael /* buffer_info structures do not need alignment */ 948be454c9SAlice Michael ret_code = iavf_allocate_virt_mem(hw, &hw->aq.arq.dma_head, 958be454c9SAlice Michael (hw->aq.num_arq_entries * 968be454c9SAlice Michael sizeof(struct iavf_dma_mem))); 978be454c9SAlice Michael if (ret_code) 988be454c9SAlice Michael goto alloc_arq_bufs; 998be454c9SAlice Michael hw->aq.arq.r.arq_bi = (struct iavf_dma_mem *)hw->aq.arq.dma_head.va; 1008be454c9SAlice Michael 1018be454c9SAlice Michael /* allocate the mapped buffers */ 1028be454c9SAlice Michael for (i = 0; i < hw->aq.num_arq_entries; i++) { 1038be454c9SAlice Michael bi = &hw->aq.arq.r.arq_bi[i]; 1048be454c9SAlice Michael ret_code = iavf_allocate_dma_mem(hw, bi, 1057af36e32SAlice Michael iavf_mem_arq_buf, 1068be454c9SAlice Michael hw->aq.arq_buf_size, 1078be454c9SAlice Michael IAVF_ADMINQ_DESC_ALIGNMENT); 1088be454c9SAlice Michael if (ret_code) 1098be454c9SAlice Michael goto unwind_alloc_arq_bufs; 1108be454c9SAlice Michael 1118be454c9SAlice Michael /* now configure the descriptors for use */ 1128be454c9SAlice Michael desc = IAVF_ADMINQ_DESC(hw->aq.arq, i); 1138be454c9SAlice Michael 1147af36e32SAlice Michael desc->flags = cpu_to_le16(IAVF_AQ_FLAG_BUF); 1157af36e32SAlice Michael if (hw->aq.arq_buf_size > IAVF_AQ_LARGE_BUF) 1167af36e32SAlice Michael desc->flags |= cpu_to_le16(IAVF_AQ_FLAG_LB); 1178be454c9SAlice Michael desc->opcode = 0; 1188be454c9SAlice Michael /* This is in accordance with Admin queue design, there is no 1198be454c9SAlice Michael * register for buffer size configuration 1208be454c9SAlice Michael */ 1218be454c9SAlice Michael desc->datalen = cpu_to_le16((u16)bi->size); 1228be454c9SAlice Michael desc->retval = 0; 1238be454c9SAlice Michael desc->cookie_high = 0; 1248be454c9SAlice Michael desc->cookie_low = 0; 1258be454c9SAlice Michael desc->params.external.addr_high = 1268be454c9SAlice Michael cpu_to_le32(upper_32_bits(bi->pa)); 1278be454c9SAlice Michael desc->params.external.addr_low = 1288be454c9SAlice Michael cpu_to_le32(lower_32_bits(bi->pa)); 1298be454c9SAlice Michael desc->params.external.param0 = 0; 1308be454c9SAlice Michael desc->params.external.param1 = 0; 1318be454c9SAlice Michael } 1328be454c9SAlice Michael 1338be454c9SAlice Michael alloc_arq_bufs: 1348be454c9SAlice Michael return ret_code; 1358be454c9SAlice Michael 1368be454c9SAlice Michael unwind_alloc_arq_bufs: 1378be454c9SAlice Michael /* don't try to free the one that failed... */ 1388be454c9SAlice Michael i--; 1398be454c9SAlice Michael for (; i >= 0; i--) 1408be454c9SAlice Michael iavf_free_dma_mem(hw, &hw->aq.arq.r.arq_bi[i]); 1418be454c9SAlice Michael iavf_free_virt_mem(hw, &hw->aq.arq.dma_head); 1428be454c9SAlice Michael 1438be454c9SAlice Michael return ret_code; 1448be454c9SAlice Michael } 1458be454c9SAlice Michael 1468be454c9SAlice Michael /** 147db950599SAlice Michael * iavf_alloc_asq_bufs - Allocate empty buffer structs for the send queue 1488be454c9SAlice Michael * @hw: pointer to the hardware structure 1498be454c9SAlice Michael **/ 150db950599SAlice Michael static enum iavf_status iavf_alloc_asq_bufs(struct iavf_hw *hw) 1518be454c9SAlice Michael { 1528be454c9SAlice Michael struct iavf_dma_mem *bi; 15380754bbcSSergey Nemov enum iavf_status ret_code; 1548be454c9SAlice Michael int i; 1558be454c9SAlice Michael 1568be454c9SAlice Michael /* No mapped memory needed yet, just the buffer info structures */ 1578be454c9SAlice Michael ret_code = iavf_allocate_virt_mem(hw, &hw->aq.asq.dma_head, 1588be454c9SAlice Michael (hw->aq.num_asq_entries * 1598be454c9SAlice Michael sizeof(struct iavf_dma_mem))); 1608be454c9SAlice Michael if (ret_code) 1618be454c9SAlice Michael goto alloc_asq_bufs; 1628be454c9SAlice Michael hw->aq.asq.r.asq_bi = (struct iavf_dma_mem *)hw->aq.asq.dma_head.va; 1638be454c9SAlice Michael 1648be454c9SAlice Michael /* allocate the mapped buffers */ 1658be454c9SAlice Michael for (i = 0; i < hw->aq.num_asq_entries; i++) { 1668be454c9SAlice Michael bi = &hw->aq.asq.r.asq_bi[i]; 1678be454c9SAlice Michael ret_code = iavf_allocate_dma_mem(hw, bi, 1687af36e32SAlice Michael iavf_mem_asq_buf, 1698be454c9SAlice Michael hw->aq.asq_buf_size, 1708be454c9SAlice Michael IAVF_ADMINQ_DESC_ALIGNMENT); 1718be454c9SAlice Michael if (ret_code) 1728be454c9SAlice Michael goto unwind_alloc_asq_bufs; 1738be454c9SAlice Michael } 1748be454c9SAlice Michael alloc_asq_bufs: 1758be454c9SAlice Michael return ret_code; 1768be454c9SAlice Michael 1778be454c9SAlice Michael unwind_alloc_asq_bufs: 1788be454c9SAlice Michael /* don't try to free the one that failed... */ 1798be454c9SAlice Michael i--; 1808be454c9SAlice Michael for (; i >= 0; i--) 1818be454c9SAlice Michael iavf_free_dma_mem(hw, &hw->aq.asq.r.asq_bi[i]); 1828be454c9SAlice Michael iavf_free_virt_mem(hw, &hw->aq.asq.dma_head); 1838be454c9SAlice Michael 1848be454c9SAlice Michael return ret_code; 1858be454c9SAlice Michael } 1868be454c9SAlice Michael 1878be454c9SAlice Michael /** 188db950599SAlice Michael * iavf_free_arq_bufs - Free receive queue buffer info elements 1898be454c9SAlice Michael * @hw: pointer to the hardware structure 1908be454c9SAlice Michael **/ 191db950599SAlice Michael static void iavf_free_arq_bufs(struct iavf_hw *hw) 1928be454c9SAlice Michael { 1938be454c9SAlice Michael int i; 1948be454c9SAlice Michael 1958be454c9SAlice Michael /* free descriptors */ 1968be454c9SAlice Michael for (i = 0; i < hw->aq.num_arq_entries; i++) 1978be454c9SAlice Michael iavf_free_dma_mem(hw, &hw->aq.arq.r.arq_bi[i]); 1988be454c9SAlice Michael 1998be454c9SAlice Michael /* free the descriptor memory */ 2008be454c9SAlice Michael iavf_free_dma_mem(hw, &hw->aq.arq.desc_buf); 2018be454c9SAlice Michael 2028be454c9SAlice Michael /* free the dma header */ 2038be454c9SAlice Michael iavf_free_virt_mem(hw, &hw->aq.arq.dma_head); 2048be454c9SAlice Michael } 2058be454c9SAlice Michael 2068be454c9SAlice Michael /** 207db950599SAlice Michael * iavf_free_asq_bufs - Free send queue buffer info elements 2088be454c9SAlice Michael * @hw: pointer to the hardware structure 2098be454c9SAlice Michael **/ 210db950599SAlice Michael static void iavf_free_asq_bufs(struct iavf_hw *hw) 2118be454c9SAlice Michael { 2128be454c9SAlice Michael int i; 2138be454c9SAlice Michael 2148be454c9SAlice Michael /* only unmap if the address is non-NULL */ 2158be454c9SAlice Michael for (i = 0; i < hw->aq.num_asq_entries; i++) 2168be454c9SAlice Michael if (hw->aq.asq.r.asq_bi[i].pa) 2178be454c9SAlice Michael iavf_free_dma_mem(hw, &hw->aq.asq.r.asq_bi[i]); 2188be454c9SAlice Michael 2198be454c9SAlice Michael /* free the buffer info list */ 2208be454c9SAlice Michael iavf_free_virt_mem(hw, &hw->aq.asq.cmd_buf); 2218be454c9SAlice Michael 2228be454c9SAlice Michael /* free the descriptor memory */ 2238be454c9SAlice Michael iavf_free_dma_mem(hw, &hw->aq.asq.desc_buf); 2248be454c9SAlice Michael 2258be454c9SAlice Michael /* free the dma header */ 2268be454c9SAlice Michael iavf_free_virt_mem(hw, &hw->aq.asq.dma_head); 2278be454c9SAlice Michael } 2288be454c9SAlice Michael 2298be454c9SAlice Michael /** 230db950599SAlice Michael * iavf_config_asq_regs - configure ASQ registers 2318be454c9SAlice Michael * @hw: pointer to the hardware structure 2328be454c9SAlice Michael * 2338be454c9SAlice Michael * Configure base address and length registers for the transmit queue 2348be454c9SAlice Michael **/ 235db950599SAlice Michael static enum iavf_status iavf_config_asq_regs(struct iavf_hw *hw) 2368be454c9SAlice Michael { 23780754bbcSSergey Nemov enum iavf_status ret_code = 0; 2388be454c9SAlice Michael u32 reg = 0; 2398be454c9SAlice Michael 2408be454c9SAlice Michael /* Clear Head and Tail */ 241*3d66f215SIvan Vecera wr32(hw, IAVF_VF_ATQH1, 0); 242*3d66f215SIvan Vecera wr32(hw, IAVF_VF_ATQT1, 0); 2438be454c9SAlice Michael 2448be454c9SAlice Michael /* set starting point */ 245*3d66f215SIvan Vecera wr32(hw, IAVF_VF_ATQLEN1, (hw->aq.num_asq_entries | 2468be454c9SAlice Michael IAVF_VF_ATQLEN1_ATQENABLE_MASK)); 247*3d66f215SIvan Vecera wr32(hw, IAVF_VF_ATQBAL1, lower_32_bits(hw->aq.asq.desc_buf.pa)); 248*3d66f215SIvan Vecera wr32(hw, IAVF_VF_ATQBAH1, upper_32_bits(hw->aq.asq.desc_buf.pa)); 2498be454c9SAlice Michael 2508be454c9SAlice Michael /* Check one register to verify that config was applied */ 251*3d66f215SIvan Vecera reg = rd32(hw, IAVF_VF_ATQBAL1); 2528be454c9SAlice Michael if (reg != lower_32_bits(hw->aq.asq.desc_buf.pa)) 2538821b3faSAlice Michael ret_code = IAVF_ERR_ADMIN_QUEUE_ERROR; 2548be454c9SAlice Michael 2558be454c9SAlice Michael return ret_code; 2568be454c9SAlice Michael } 2578be454c9SAlice Michael 2588be454c9SAlice Michael /** 259db950599SAlice Michael * iavf_config_arq_regs - ARQ register configuration 2608be454c9SAlice Michael * @hw: pointer to the hardware structure 2618be454c9SAlice Michael * 2628be454c9SAlice Michael * Configure base address and length registers for the receive (event queue) 2638be454c9SAlice Michael **/ 264db950599SAlice Michael static enum iavf_status iavf_config_arq_regs(struct iavf_hw *hw) 2658be454c9SAlice Michael { 26680754bbcSSergey Nemov enum iavf_status ret_code = 0; 2678be454c9SAlice Michael u32 reg = 0; 2688be454c9SAlice Michael 2698be454c9SAlice Michael /* Clear Head and Tail */ 270*3d66f215SIvan Vecera wr32(hw, IAVF_VF_ARQH1, 0); 271*3d66f215SIvan Vecera wr32(hw, IAVF_VF_ARQT1, 0); 2728be454c9SAlice Michael 2738be454c9SAlice Michael /* set starting point */ 274*3d66f215SIvan Vecera wr32(hw, IAVF_VF_ARQLEN1, (hw->aq.num_arq_entries | 2758be454c9SAlice Michael IAVF_VF_ARQLEN1_ARQENABLE_MASK)); 276*3d66f215SIvan Vecera wr32(hw, IAVF_VF_ARQBAL1, lower_32_bits(hw->aq.arq.desc_buf.pa)); 277*3d66f215SIvan Vecera wr32(hw, IAVF_VF_ARQBAH1, upper_32_bits(hw->aq.arq.desc_buf.pa)); 2788be454c9SAlice Michael 2798be454c9SAlice Michael /* Update tail in the HW to post pre-allocated buffers */ 280*3d66f215SIvan Vecera wr32(hw, IAVF_VF_ARQT1, hw->aq.num_arq_entries - 1); 2818be454c9SAlice Michael 2828be454c9SAlice Michael /* Check one register to verify that config was applied */ 283*3d66f215SIvan Vecera reg = rd32(hw, IAVF_VF_ARQBAL1); 2848be454c9SAlice Michael if (reg != lower_32_bits(hw->aq.arq.desc_buf.pa)) 2858821b3faSAlice Michael ret_code = IAVF_ERR_ADMIN_QUEUE_ERROR; 2868be454c9SAlice Michael 2878be454c9SAlice Michael return ret_code; 2888be454c9SAlice Michael } 2898be454c9SAlice Michael 2908be454c9SAlice Michael /** 291db950599SAlice Michael * iavf_init_asq - main initialization routine for ASQ 2928be454c9SAlice Michael * @hw: pointer to the hardware structure 2938be454c9SAlice Michael * 2948be454c9SAlice Michael * This is the main initialization routine for the Admin Send Queue 2958be454c9SAlice Michael * Prior to calling this function, drivers *MUST* set the following fields 2968be454c9SAlice Michael * in the hw->aq structure: 2978be454c9SAlice Michael * - hw->aq.num_asq_entries 2988be454c9SAlice Michael * - hw->aq.arq_buf_size 2998be454c9SAlice Michael * 3008be454c9SAlice Michael * Do *NOT* hold the lock when calling this as the memory allocation routines 3018be454c9SAlice Michael * called are not going to be atomic context safe 3028be454c9SAlice Michael **/ 303db950599SAlice Michael static enum iavf_status iavf_init_asq(struct iavf_hw *hw) 3048be454c9SAlice Michael { 30580754bbcSSergey Nemov enum iavf_status ret_code = 0; 30641983161SPrzemyslaw Patynowski int i; 3078be454c9SAlice Michael 3088be454c9SAlice Michael if (hw->aq.asq.count > 0) { 3098be454c9SAlice Michael /* queue already initialized */ 3108821b3faSAlice Michael ret_code = IAVF_ERR_NOT_READY; 3118be454c9SAlice Michael goto init_adminq_exit; 3128be454c9SAlice Michael } 3138be454c9SAlice Michael 3148be454c9SAlice Michael /* verify input for valid configuration */ 3158be454c9SAlice Michael if ((hw->aq.num_asq_entries == 0) || 3168be454c9SAlice Michael (hw->aq.asq_buf_size == 0)) { 3178821b3faSAlice Michael ret_code = IAVF_ERR_CONFIG; 3188be454c9SAlice Michael goto init_adminq_exit; 3198be454c9SAlice Michael } 3208be454c9SAlice Michael 3218be454c9SAlice Michael hw->aq.asq.next_to_use = 0; 3228be454c9SAlice Michael hw->aq.asq.next_to_clean = 0; 3238be454c9SAlice Michael 3248be454c9SAlice Michael /* allocate the ring memory */ 325db950599SAlice Michael ret_code = iavf_alloc_adminq_asq_ring(hw); 3268be454c9SAlice Michael if (ret_code) 3278be454c9SAlice Michael goto init_adminq_exit; 3288be454c9SAlice Michael 3298be454c9SAlice Michael /* allocate buffers in the rings */ 330db950599SAlice Michael ret_code = iavf_alloc_asq_bufs(hw); 3318be454c9SAlice Michael if (ret_code) 3328be454c9SAlice Michael goto init_adminq_free_rings; 3338be454c9SAlice Michael 3348be454c9SAlice Michael /* initialize base registers */ 335db950599SAlice Michael ret_code = iavf_config_asq_regs(hw); 3368be454c9SAlice Michael if (ret_code) 33741983161SPrzemyslaw Patynowski goto init_free_asq_bufs; 3388be454c9SAlice Michael 3398be454c9SAlice Michael /* success! */ 3408be454c9SAlice Michael hw->aq.asq.count = hw->aq.num_asq_entries; 3418be454c9SAlice Michael goto init_adminq_exit; 3428be454c9SAlice Michael 34341983161SPrzemyslaw Patynowski init_free_asq_bufs: 34441983161SPrzemyslaw Patynowski for (i = 0; i < hw->aq.num_asq_entries; i++) 34541983161SPrzemyslaw Patynowski iavf_free_dma_mem(hw, &hw->aq.asq.r.asq_bi[i]); 34641983161SPrzemyslaw Patynowski iavf_free_virt_mem(hw, &hw->aq.asq.dma_head); 34741983161SPrzemyslaw Patynowski 3488be454c9SAlice Michael init_adminq_free_rings: 349db950599SAlice Michael iavf_free_adminq_asq(hw); 3508be454c9SAlice Michael 3518be454c9SAlice Michael init_adminq_exit: 3528be454c9SAlice Michael return ret_code; 3538be454c9SAlice Michael } 3548be454c9SAlice Michael 3558be454c9SAlice Michael /** 356db950599SAlice Michael * iavf_init_arq - initialize ARQ 3578be454c9SAlice Michael * @hw: pointer to the hardware structure 3588be454c9SAlice Michael * 3598be454c9SAlice Michael * The main initialization routine for the Admin Receive (Event) Queue. 3608be454c9SAlice Michael * Prior to calling this function, drivers *MUST* set the following fields 3618be454c9SAlice Michael * in the hw->aq structure: 3628be454c9SAlice Michael * - hw->aq.num_asq_entries 3638be454c9SAlice Michael * - hw->aq.arq_buf_size 3648be454c9SAlice Michael * 3658be454c9SAlice Michael * Do *NOT* hold the lock when calling this as the memory allocation routines 3668be454c9SAlice Michael * called are not going to be atomic context safe 3678be454c9SAlice Michael **/ 368db950599SAlice Michael static enum iavf_status iavf_init_arq(struct iavf_hw *hw) 3698be454c9SAlice Michael { 37080754bbcSSergey Nemov enum iavf_status ret_code = 0; 37141983161SPrzemyslaw Patynowski int i; 3728be454c9SAlice Michael 3738be454c9SAlice Michael if (hw->aq.arq.count > 0) { 3748be454c9SAlice Michael /* queue already initialized */ 3758821b3faSAlice Michael ret_code = IAVF_ERR_NOT_READY; 3768be454c9SAlice Michael goto init_adminq_exit; 3778be454c9SAlice Michael } 3788be454c9SAlice Michael 3798be454c9SAlice Michael /* verify input for valid configuration */ 3808be454c9SAlice Michael if ((hw->aq.num_arq_entries == 0) || 3818be454c9SAlice Michael (hw->aq.arq_buf_size == 0)) { 3828821b3faSAlice Michael ret_code = IAVF_ERR_CONFIG; 3838be454c9SAlice Michael goto init_adminq_exit; 3848be454c9SAlice Michael } 3858be454c9SAlice Michael 3868be454c9SAlice Michael hw->aq.arq.next_to_use = 0; 3878be454c9SAlice Michael hw->aq.arq.next_to_clean = 0; 3888be454c9SAlice Michael 3898be454c9SAlice Michael /* allocate the ring memory */ 390db950599SAlice Michael ret_code = iavf_alloc_adminq_arq_ring(hw); 3918be454c9SAlice Michael if (ret_code) 3928be454c9SAlice Michael goto init_adminq_exit; 3938be454c9SAlice Michael 3948be454c9SAlice Michael /* allocate buffers in the rings */ 395db950599SAlice Michael ret_code = iavf_alloc_arq_bufs(hw); 3968be454c9SAlice Michael if (ret_code) 3978be454c9SAlice Michael goto init_adminq_free_rings; 3988be454c9SAlice Michael 3998be454c9SAlice Michael /* initialize base registers */ 400db950599SAlice Michael ret_code = iavf_config_arq_regs(hw); 4018be454c9SAlice Michael if (ret_code) 40241983161SPrzemyslaw Patynowski goto init_free_arq_bufs; 4038be454c9SAlice Michael 4048be454c9SAlice Michael /* success! */ 4058be454c9SAlice Michael hw->aq.arq.count = hw->aq.num_arq_entries; 4068be454c9SAlice Michael goto init_adminq_exit; 4078be454c9SAlice Michael 40841983161SPrzemyslaw Patynowski init_free_arq_bufs: 40941983161SPrzemyslaw Patynowski for (i = 0; i < hw->aq.num_arq_entries; i++) 41041983161SPrzemyslaw Patynowski iavf_free_dma_mem(hw, &hw->aq.arq.r.arq_bi[i]); 41141983161SPrzemyslaw Patynowski iavf_free_virt_mem(hw, &hw->aq.arq.dma_head); 4128be454c9SAlice Michael init_adminq_free_rings: 413db950599SAlice Michael iavf_free_adminq_arq(hw); 4148be454c9SAlice Michael 4158be454c9SAlice Michael init_adminq_exit: 4168be454c9SAlice Michael return ret_code; 4178be454c9SAlice Michael } 4188be454c9SAlice Michael 4198be454c9SAlice Michael /** 420db950599SAlice Michael * iavf_shutdown_asq - shutdown the ASQ 4218be454c9SAlice Michael * @hw: pointer to the hardware structure 4228be454c9SAlice Michael * 4238be454c9SAlice Michael * The main shutdown routine for the Admin Send Queue 4248be454c9SAlice Michael **/ 425db950599SAlice Michael static enum iavf_status iavf_shutdown_asq(struct iavf_hw *hw) 4268be454c9SAlice Michael { 42780754bbcSSergey Nemov enum iavf_status ret_code = 0; 4288be454c9SAlice Michael 4298be454c9SAlice Michael mutex_lock(&hw->aq.asq_mutex); 4308be454c9SAlice Michael 4318be454c9SAlice Michael if (hw->aq.asq.count == 0) { 4328821b3faSAlice Michael ret_code = IAVF_ERR_NOT_READY; 4338be454c9SAlice Michael goto shutdown_asq_out; 4348be454c9SAlice Michael } 4358be454c9SAlice Michael 4368be454c9SAlice Michael /* Stop firmware AdminQ processing */ 437*3d66f215SIvan Vecera wr32(hw, IAVF_VF_ATQH1, 0); 438*3d66f215SIvan Vecera wr32(hw, IAVF_VF_ATQT1, 0); 439*3d66f215SIvan Vecera wr32(hw, IAVF_VF_ATQLEN1, 0); 440*3d66f215SIvan Vecera wr32(hw, IAVF_VF_ATQBAL1, 0); 441*3d66f215SIvan Vecera wr32(hw, IAVF_VF_ATQBAH1, 0); 4428be454c9SAlice Michael 4438be454c9SAlice Michael hw->aq.asq.count = 0; /* to indicate uninitialized queue */ 4448be454c9SAlice Michael 4458be454c9SAlice Michael /* free ring buffers */ 446db950599SAlice Michael iavf_free_asq_bufs(hw); 4478be454c9SAlice Michael 4488be454c9SAlice Michael shutdown_asq_out: 4498be454c9SAlice Michael mutex_unlock(&hw->aq.asq_mutex); 4508be454c9SAlice Michael return ret_code; 4518be454c9SAlice Michael } 4528be454c9SAlice Michael 4538be454c9SAlice Michael /** 454db950599SAlice Michael * iavf_shutdown_arq - shutdown ARQ 4558be454c9SAlice Michael * @hw: pointer to the hardware structure 4568be454c9SAlice Michael * 4578be454c9SAlice Michael * The main shutdown routine for the Admin Receive Queue 4588be454c9SAlice Michael **/ 459db950599SAlice Michael static enum iavf_status iavf_shutdown_arq(struct iavf_hw *hw) 4608be454c9SAlice Michael { 46180754bbcSSergey Nemov enum iavf_status ret_code = 0; 4628be454c9SAlice Michael 4638be454c9SAlice Michael mutex_lock(&hw->aq.arq_mutex); 4648be454c9SAlice Michael 4658be454c9SAlice Michael if (hw->aq.arq.count == 0) { 4668821b3faSAlice Michael ret_code = IAVF_ERR_NOT_READY; 4678be454c9SAlice Michael goto shutdown_arq_out; 4688be454c9SAlice Michael } 4698be454c9SAlice Michael 4708be454c9SAlice Michael /* Stop firmware AdminQ processing */ 471*3d66f215SIvan Vecera wr32(hw, IAVF_VF_ARQH1, 0); 472*3d66f215SIvan Vecera wr32(hw, IAVF_VF_ARQT1, 0); 473*3d66f215SIvan Vecera wr32(hw, IAVF_VF_ARQLEN1, 0); 474*3d66f215SIvan Vecera wr32(hw, IAVF_VF_ARQBAL1, 0); 475*3d66f215SIvan Vecera wr32(hw, IAVF_VF_ARQBAH1, 0); 4768be454c9SAlice Michael 4778be454c9SAlice Michael hw->aq.arq.count = 0; /* to indicate uninitialized queue */ 4788be454c9SAlice Michael 4798be454c9SAlice Michael /* free ring buffers */ 480db950599SAlice Michael iavf_free_arq_bufs(hw); 4818be454c9SAlice Michael 4828be454c9SAlice Michael shutdown_arq_out: 4838be454c9SAlice Michael mutex_unlock(&hw->aq.arq_mutex); 4848be454c9SAlice Michael return ret_code; 4858be454c9SAlice Michael } 4868be454c9SAlice Michael 4878be454c9SAlice Michael /** 4888be454c9SAlice Michael * iavf_init_adminq - main initialization routine for Admin Queue 4898be454c9SAlice Michael * @hw: pointer to the hardware structure 4908be454c9SAlice Michael * 4918be454c9SAlice Michael * Prior to calling this function, drivers *MUST* set the following fields 4928be454c9SAlice Michael * in the hw->aq structure: 4938be454c9SAlice Michael * - hw->aq.num_asq_entries 4948be454c9SAlice Michael * - hw->aq.num_arq_entries 4958be454c9SAlice Michael * - hw->aq.arq_buf_size 4968be454c9SAlice Michael * - hw->aq.asq_buf_size 4978be454c9SAlice Michael **/ 49880754bbcSSergey Nemov enum iavf_status iavf_init_adminq(struct iavf_hw *hw) 4998be454c9SAlice Michael { 50080754bbcSSergey Nemov enum iavf_status ret_code; 5018be454c9SAlice Michael 5028be454c9SAlice Michael /* verify input for valid configuration */ 5038be454c9SAlice Michael if ((hw->aq.num_arq_entries == 0) || 5048be454c9SAlice Michael (hw->aq.num_asq_entries == 0) || 5058be454c9SAlice Michael (hw->aq.arq_buf_size == 0) || 5068be454c9SAlice Michael (hw->aq.asq_buf_size == 0)) { 5078821b3faSAlice Michael ret_code = IAVF_ERR_CONFIG; 5088be454c9SAlice Michael goto init_adminq_exit; 5098be454c9SAlice Michael } 5108be454c9SAlice Michael 5118be454c9SAlice Michael /* setup ASQ command write back timeout */ 5127af36e32SAlice Michael hw->aq.asq_cmd_timeout = IAVF_ASQ_CMD_TIMEOUT; 5138be454c9SAlice Michael 5148be454c9SAlice Michael /* allocate the ASQ */ 515db950599SAlice Michael ret_code = iavf_init_asq(hw); 5168be454c9SAlice Michael if (ret_code) 5178be454c9SAlice Michael goto init_adminq_destroy_locks; 5188be454c9SAlice Michael 5198be454c9SAlice Michael /* allocate the ARQ */ 520db950599SAlice Michael ret_code = iavf_init_arq(hw); 5218be454c9SAlice Michael if (ret_code) 5228be454c9SAlice Michael goto init_adminq_free_asq; 5238be454c9SAlice Michael 5248be454c9SAlice Michael /* success! */ 5258be454c9SAlice Michael goto init_adminq_exit; 5268be454c9SAlice Michael 5278be454c9SAlice Michael init_adminq_free_asq: 528db950599SAlice Michael iavf_shutdown_asq(hw); 5298be454c9SAlice Michael init_adminq_destroy_locks: 5308be454c9SAlice Michael 5318be454c9SAlice Michael init_adminq_exit: 5328be454c9SAlice Michael return ret_code; 5338be454c9SAlice Michael } 5348be454c9SAlice Michael 5358be454c9SAlice Michael /** 5368be454c9SAlice Michael * iavf_shutdown_adminq - shutdown routine for the Admin Queue 5378be454c9SAlice Michael * @hw: pointer to the hardware structure 5388be454c9SAlice Michael **/ 53980754bbcSSergey Nemov enum iavf_status iavf_shutdown_adminq(struct iavf_hw *hw) 5408be454c9SAlice Michael { 5418be454c9SAlice Michael if (iavf_check_asq_alive(hw)) 5428be454c9SAlice Michael iavf_aq_queue_shutdown(hw, true); 5438be454c9SAlice Michael 544db950599SAlice Michael iavf_shutdown_asq(hw); 545db950599SAlice Michael iavf_shutdown_arq(hw); 5468be454c9SAlice Michael 5475322c68eSJason Wang return 0; 5488be454c9SAlice Michael } 5498be454c9SAlice Michael 5508be454c9SAlice Michael /** 551db950599SAlice Michael * iavf_clean_asq - cleans Admin send queue 5528be454c9SAlice Michael * @hw: pointer to the hardware structure 5538be454c9SAlice Michael * 5548be454c9SAlice Michael * returns the number of free desc 5558be454c9SAlice Michael **/ 556db950599SAlice Michael static u16 iavf_clean_asq(struct iavf_hw *hw) 5578be454c9SAlice Michael { 5588be454c9SAlice Michael struct iavf_adminq_ring *asq = &hw->aq.asq; 5597af36e32SAlice Michael struct iavf_asq_cmd_details *details; 5608be454c9SAlice Michael u16 ntc = asq->next_to_clean; 5617af36e32SAlice Michael struct iavf_aq_desc desc_cb; 5627af36e32SAlice Michael struct iavf_aq_desc *desc; 5638be454c9SAlice Michael 5648be454c9SAlice Michael desc = IAVF_ADMINQ_DESC(*asq, ntc); 5657af36e32SAlice Michael details = IAVF_ADMINQ_DETAILS(*asq, ntc); 566*3d66f215SIvan Vecera while (rd32(hw, IAVF_VF_ATQH1) != ntc) { 5678be454c9SAlice Michael iavf_debug(hw, IAVF_DEBUG_AQ_MESSAGE, 568*3d66f215SIvan Vecera "ntc %d head %d.\n", ntc, rd32(hw, IAVF_VF_ATQH1)); 5698be454c9SAlice Michael 5708be454c9SAlice Michael if (details->callback) { 571db950599SAlice Michael IAVF_ADMINQ_CALLBACK cb_func = 572db950599SAlice Michael (IAVF_ADMINQ_CALLBACK)details->callback; 5738be454c9SAlice Michael desc_cb = *desc; 5748be454c9SAlice Michael cb_func(hw, &desc_cb); 5758be454c9SAlice Michael } 5767af36e32SAlice Michael memset((void *)desc, 0, sizeof(struct iavf_aq_desc)); 5778be454c9SAlice Michael memset((void *)details, 0, 5787af36e32SAlice Michael sizeof(struct iavf_asq_cmd_details)); 5798be454c9SAlice Michael ntc++; 5808be454c9SAlice Michael if (ntc == asq->count) 5818be454c9SAlice Michael ntc = 0; 5828be454c9SAlice Michael desc = IAVF_ADMINQ_DESC(*asq, ntc); 5837af36e32SAlice Michael details = IAVF_ADMINQ_DETAILS(*asq, ntc); 5848be454c9SAlice Michael } 5858be454c9SAlice Michael 5868be454c9SAlice Michael asq->next_to_clean = ntc; 5878be454c9SAlice Michael 5888be454c9SAlice Michael return IAVF_DESC_UNUSED(asq); 5898be454c9SAlice Michael } 5908be454c9SAlice Michael 5918be454c9SAlice Michael /** 5928be454c9SAlice Michael * iavf_asq_done - check if FW has processed the Admin Send Queue 5938be454c9SAlice Michael * @hw: pointer to the hw struct 5948be454c9SAlice Michael * 5958be454c9SAlice Michael * Returns true if the firmware has processed all descriptors on the 5968be454c9SAlice Michael * admin send queue. Returns false if there are still requests pending. 5978be454c9SAlice Michael **/ 5988be454c9SAlice Michael bool iavf_asq_done(struct iavf_hw *hw) 5998be454c9SAlice Michael { 6008be454c9SAlice Michael /* AQ designers suggest use of head for better 6018be454c9SAlice Michael * timing reliability than DD bit 6028be454c9SAlice Michael */ 603*3d66f215SIvan Vecera return rd32(hw, IAVF_VF_ATQH1) == hw->aq.asq.next_to_use; 6048be454c9SAlice Michael } 6058be454c9SAlice Michael 6068be454c9SAlice Michael /** 6078be454c9SAlice Michael * iavf_asq_send_command - send command to Admin Queue 6088be454c9SAlice Michael * @hw: pointer to the hw struct 6098be454c9SAlice Michael * @desc: prefilled descriptor describing the command (non DMA mem) 6108be454c9SAlice Michael * @buff: buffer to use for indirect commands 6118be454c9SAlice Michael * @buff_size: size of buffer for indirect commands 6128be454c9SAlice Michael * @cmd_details: pointer to command details structure 6138be454c9SAlice Michael * 6148be454c9SAlice Michael * This is the main send command driver routine for the Admin Queue send 6158be454c9SAlice Michael * queue. It runs the queue, cleans the queue, etc 6168be454c9SAlice Michael **/ 61780754bbcSSergey Nemov enum iavf_status iavf_asq_send_command(struct iavf_hw *hw, 6187af36e32SAlice Michael struct iavf_aq_desc *desc, 6198be454c9SAlice Michael void *buff, /* can be NULL */ 6208be454c9SAlice Michael u16 buff_size, 6217af36e32SAlice Michael struct iavf_asq_cmd_details *cmd_details) 6228be454c9SAlice Michael { 6238be454c9SAlice Michael struct iavf_dma_mem *dma_buff = NULL; 6247af36e32SAlice Michael struct iavf_asq_cmd_details *details; 6257af36e32SAlice Michael struct iavf_aq_desc *desc_on_ring; 6268be454c9SAlice Michael bool cmd_completed = false; 62780754bbcSSergey Nemov enum iavf_status status = 0; 6288be454c9SAlice Michael u16 retval = 0; 6298be454c9SAlice Michael u32 val = 0; 6308be454c9SAlice Michael 6318be454c9SAlice Michael mutex_lock(&hw->aq.asq_mutex); 6328be454c9SAlice Michael 6338be454c9SAlice Michael if (hw->aq.asq.count == 0) { 6348be454c9SAlice Michael iavf_debug(hw, IAVF_DEBUG_AQ_MESSAGE, 6358be454c9SAlice Michael "AQTX: Admin queue not initialized.\n"); 6368821b3faSAlice Michael status = IAVF_ERR_QUEUE_EMPTY; 6378be454c9SAlice Michael goto asq_send_command_error; 6388be454c9SAlice Michael } 6398be454c9SAlice Michael 6407af36e32SAlice Michael hw->aq.asq_last_status = IAVF_AQ_RC_OK; 6418be454c9SAlice Michael 642*3d66f215SIvan Vecera val = rd32(hw, IAVF_VF_ATQH1); 6438be454c9SAlice Michael if (val >= hw->aq.num_asq_entries) { 6448be454c9SAlice Michael iavf_debug(hw, IAVF_DEBUG_AQ_MESSAGE, 6458be454c9SAlice Michael "AQTX: head overrun at %d\n", val); 6468821b3faSAlice Michael status = IAVF_ERR_QUEUE_EMPTY; 6478be454c9SAlice Michael goto asq_send_command_error; 6488be454c9SAlice Michael } 6498be454c9SAlice Michael 6507af36e32SAlice Michael details = IAVF_ADMINQ_DETAILS(hw->aq.asq, hw->aq.asq.next_to_use); 6518be454c9SAlice Michael if (cmd_details) { 6528be454c9SAlice Michael *details = *cmd_details; 6538be454c9SAlice Michael 6548be454c9SAlice Michael /* If the cmd_details are defined copy the cookie. The 6558be454c9SAlice Michael * cpu_to_le32 is not needed here because the data is ignored 6568be454c9SAlice Michael * by the FW, only used by the driver 6578be454c9SAlice Michael */ 6588be454c9SAlice Michael if (details->cookie) { 6598be454c9SAlice Michael desc->cookie_high = 6608be454c9SAlice Michael cpu_to_le32(upper_32_bits(details->cookie)); 6618be454c9SAlice Michael desc->cookie_low = 6628be454c9SAlice Michael cpu_to_le32(lower_32_bits(details->cookie)); 6638be454c9SAlice Michael } 6648be454c9SAlice Michael } else { 6657af36e32SAlice Michael memset(details, 0, sizeof(struct iavf_asq_cmd_details)); 6668be454c9SAlice Michael } 6678be454c9SAlice Michael 6688be454c9SAlice Michael /* clear requested flags and then set additional flags if defined */ 6698be454c9SAlice Michael desc->flags &= ~cpu_to_le16(details->flags_dis); 6708be454c9SAlice Michael desc->flags |= cpu_to_le16(details->flags_ena); 6718be454c9SAlice Michael 6728be454c9SAlice Michael if (buff_size > hw->aq.asq_buf_size) { 6738be454c9SAlice Michael iavf_debug(hw, 6748be454c9SAlice Michael IAVF_DEBUG_AQ_MESSAGE, 6758be454c9SAlice Michael "AQTX: Invalid buffer size: %d.\n", 6768be454c9SAlice Michael buff_size); 6778821b3faSAlice Michael status = IAVF_ERR_INVALID_SIZE; 6788be454c9SAlice Michael goto asq_send_command_error; 6798be454c9SAlice Michael } 6808be454c9SAlice Michael 6818be454c9SAlice Michael if (details->postpone && !details->async) { 6828be454c9SAlice Michael iavf_debug(hw, 6838be454c9SAlice Michael IAVF_DEBUG_AQ_MESSAGE, 6848be454c9SAlice Michael "AQTX: Async flag not set along with postpone flag"); 6858821b3faSAlice Michael status = IAVF_ERR_PARAM; 6868be454c9SAlice Michael goto asq_send_command_error; 6878be454c9SAlice Michael } 6888be454c9SAlice Michael 6898be454c9SAlice Michael /* call clean and check queue available function to reclaim the 6908be454c9SAlice Michael * descriptors that were processed by FW, the function returns the 6918be454c9SAlice Michael * number of desc available 6928be454c9SAlice Michael */ 6938be454c9SAlice Michael /* the clean function called here could be called in a separate thread 6948be454c9SAlice Michael * in case of asynchronous completions 6958be454c9SAlice Michael */ 696db950599SAlice Michael if (iavf_clean_asq(hw) == 0) { 6978be454c9SAlice Michael iavf_debug(hw, 6988be454c9SAlice Michael IAVF_DEBUG_AQ_MESSAGE, 6998be454c9SAlice Michael "AQTX: Error queue is full.\n"); 7008821b3faSAlice Michael status = IAVF_ERR_ADMIN_QUEUE_FULL; 7018be454c9SAlice Michael goto asq_send_command_error; 7028be454c9SAlice Michael } 7038be454c9SAlice Michael 7048be454c9SAlice Michael /* initialize the temp desc pointer with the right desc */ 7058be454c9SAlice Michael desc_on_ring = IAVF_ADMINQ_DESC(hw->aq.asq, hw->aq.asq.next_to_use); 7068be454c9SAlice Michael 7078be454c9SAlice Michael /* if the desc is available copy the temp desc to the right place */ 7088be454c9SAlice Michael *desc_on_ring = *desc; 7098be454c9SAlice Michael 7108be454c9SAlice Michael /* if buff is not NULL assume indirect command */ 7118be454c9SAlice Michael if (buff) { 7128be454c9SAlice Michael dma_buff = &hw->aq.asq.r.asq_bi[hw->aq.asq.next_to_use]; 7138be454c9SAlice Michael /* copy the user buff into the respective DMA buff */ 7148be454c9SAlice Michael memcpy(dma_buff->va, buff, buff_size); 7158be454c9SAlice Michael desc_on_ring->datalen = cpu_to_le16(buff_size); 7168be454c9SAlice Michael 7178be454c9SAlice Michael /* Update the address values in the desc with the pa value 7188be454c9SAlice Michael * for respective buffer 7198be454c9SAlice Michael */ 7208be454c9SAlice Michael desc_on_ring->params.external.addr_high = 7218be454c9SAlice Michael cpu_to_le32(upper_32_bits(dma_buff->pa)); 7228be454c9SAlice Michael desc_on_ring->params.external.addr_low = 7238be454c9SAlice Michael cpu_to_le32(lower_32_bits(dma_buff->pa)); 7248be454c9SAlice Michael } 7258be454c9SAlice Michael 7268be454c9SAlice Michael /* bump the tail */ 7278be454c9SAlice Michael iavf_debug(hw, IAVF_DEBUG_AQ_MESSAGE, "AQTX: desc and buffer:\n"); 7288be454c9SAlice Michael iavf_debug_aq(hw, IAVF_DEBUG_AQ_COMMAND, (void *)desc_on_ring, 7298be454c9SAlice Michael buff, buff_size); 7308be454c9SAlice Michael (hw->aq.asq.next_to_use)++; 7318be454c9SAlice Michael if (hw->aq.asq.next_to_use == hw->aq.asq.count) 7328be454c9SAlice Michael hw->aq.asq.next_to_use = 0; 7338be454c9SAlice Michael if (!details->postpone) 734*3d66f215SIvan Vecera wr32(hw, IAVF_VF_ATQT1, hw->aq.asq.next_to_use); 7358be454c9SAlice Michael 7368be454c9SAlice Michael /* if cmd_details are not defined or async flag is not set, 7378be454c9SAlice Michael * we need to wait for desc write back 7388be454c9SAlice Michael */ 7398be454c9SAlice Michael if (!details->async && !details->postpone) { 7408be454c9SAlice Michael u32 total_delay = 0; 7418be454c9SAlice Michael 7428be454c9SAlice Michael do { 7438be454c9SAlice Michael /* AQ designers suggest use of head for better 7448be454c9SAlice Michael * timing reliability than DD bit 7458be454c9SAlice Michael */ 7468be454c9SAlice Michael if (iavf_asq_done(hw)) 7478be454c9SAlice Michael break; 7488be454c9SAlice Michael udelay(50); 7498be454c9SAlice Michael total_delay += 50; 7508be454c9SAlice Michael } while (total_delay < hw->aq.asq_cmd_timeout); 7518be454c9SAlice Michael } 7528be454c9SAlice Michael 7538be454c9SAlice Michael /* if ready, copy the desc back to temp */ 7548be454c9SAlice Michael if (iavf_asq_done(hw)) { 7558be454c9SAlice Michael *desc = *desc_on_ring; 7568be454c9SAlice Michael if (buff) 7578be454c9SAlice Michael memcpy(buff, dma_buff->va, buff_size); 7588be454c9SAlice Michael retval = le16_to_cpu(desc->retval); 7598be454c9SAlice Michael if (retval != 0) { 7608be454c9SAlice Michael iavf_debug(hw, 7618be454c9SAlice Michael IAVF_DEBUG_AQ_MESSAGE, 7628be454c9SAlice Michael "AQTX: Command completed with error 0x%X.\n", 7638be454c9SAlice Michael retval); 7648be454c9SAlice Michael 7658be454c9SAlice Michael /* strip off FW internal code */ 7668be454c9SAlice Michael retval &= 0xff; 7678be454c9SAlice Michael } 7688be454c9SAlice Michael cmd_completed = true; 7697af36e32SAlice Michael if ((enum iavf_admin_queue_err)retval == IAVF_AQ_RC_OK) 7708be454c9SAlice Michael status = 0; 7717af36e32SAlice Michael else if ((enum iavf_admin_queue_err)retval == IAVF_AQ_RC_EBUSY) 7728821b3faSAlice Michael status = IAVF_ERR_NOT_READY; 7738be454c9SAlice Michael else 7748821b3faSAlice Michael status = IAVF_ERR_ADMIN_QUEUE_ERROR; 7757af36e32SAlice Michael hw->aq.asq_last_status = (enum iavf_admin_queue_err)retval; 7768be454c9SAlice Michael } 7778be454c9SAlice Michael 7788be454c9SAlice Michael iavf_debug(hw, IAVF_DEBUG_AQ_MESSAGE, 7798be454c9SAlice Michael "AQTX: desc and buffer writeback:\n"); 7808be454c9SAlice Michael iavf_debug_aq(hw, IAVF_DEBUG_AQ_COMMAND, (void *)desc, buff, buff_size); 7818be454c9SAlice Michael 7828be454c9SAlice Michael /* save writeback aq if requested */ 7838be454c9SAlice Michael if (details->wb_desc) 7848be454c9SAlice Michael *details->wb_desc = *desc_on_ring; 7858be454c9SAlice Michael 7868be454c9SAlice Michael /* update the error if time out occurred */ 7878be454c9SAlice Michael if ((!cmd_completed) && 7888be454c9SAlice Michael (!details->async && !details->postpone)) { 789*3d66f215SIvan Vecera if (rd32(hw, IAVF_VF_ATQLEN1) & IAVF_VF_ATQLEN1_ATQCRIT_MASK) { 7908be454c9SAlice Michael iavf_debug(hw, IAVF_DEBUG_AQ_MESSAGE, 7918be454c9SAlice Michael "AQTX: AQ Critical error.\n"); 7928821b3faSAlice Michael status = IAVF_ERR_ADMIN_QUEUE_CRITICAL_ERROR; 7938be454c9SAlice Michael } else { 7948be454c9SAlice Michael iavf_debug(hw, IAVF_DEBUG_AQ_MESSAGE, 7958be454c9SAlice Michael "AQTX: Writeback timeout.\n"); 7968821b3faSAlice Michael status = IAVF_ERR_ADMIN_QUEUE_TIMEOUT; 7978be454c9SAlice Michael } 7988be454c9SAlice Michael } 7998be454c9SAlice Michael 8008be454c9SAlice Michael asq_send_command_error: 8018be454c9SAlice Michael mutex_unlock(&hw->aq.asq_mutex); 8028be454c9SAlice Michael return status; 8038be454c9SAlice Michael } 8048be454c9SAlice Michael 8058be454c9SAlice Michael /** 8068be454c9SAlice Michael * iavf_fill_default_direct_cmd_desc - AQ descriptor helper function 8078be454c9SAlice Michael * @desc: pointer to the temp descriptor (non DMA mem) 8088be454c9SAlice Michael * @opcode: the opcode can be used to decide which flags to turn off or on 8098be454c9SAlice Michael * 8108be454c9SAlice Michael * Fill the desc with default values 8118be454c9SAlice Michael **/ 8127af36e32SAlice Michael void iavf_fill_default_direct_cmd_desc(struct iavf_aq_desc *desc, u16 opcode) 8138be454c9SAlice Michael { 8148be454c9SAlice Michael /* zero out the desc */ 8157af36e32SAlice Michael memset((void *)desc, 0, sizeof(struct iavf_aq_desc)); 8168be454c9SAlice Michael desc->opcode = cpu_to_le16(opcode); 8177af36e32SAlice Michael desc->flags = cpu_to_le16(IAVF_AQ_FLAG_SI); 8188be454c9SAlice Michael } 8198be454c9SAlice Michael 8208be454c9SAlice Michael /** 8218be454c9SAlice Michael * iavf_clean_arq_element 8228be454c9SAlice Michael * @hw: pointer to the hw struct 8238be454c9SAlice Michael * @e: event info from the receive descriptor, includes any buffers 8248be454c9SAlice Michael * @pending: number of events that could be left to process 8258be454c9SAlice Michael * 8268be454c9SAlice Michael * This function cleans one Admin Receive Queue element and returns 8278be454c9SAlice Michael * the contents through e. It can also return how many events are 8288be454c9SAlice Michael * left to process through 'pending' 8298be454c9SAlice Michael **/ 83080754bbcSSergey Nemov enum iavf_status iavf_clean_arq_element(struct iavf_hw *hw, 8317af36e32SAlice Michael struct iavf_arq_event_info *e, 8328be454c9SAlice Michael u16 *pending) 8338be454c9SAlice Michael { 8348be454c9SAlice Michael u16 ntc = hw->aq.arq.next_to_clean; 8357af36e32SAlice Michael struct iavf_aq_desc *desc; 83680754bbcSSergey Nemov enum iavf_status ret_code = 0; 8378be454c9SAlice Michael struct iavf_dma_mem *bi; 8388be454c9SAlice Michael u16 desc_idx; 8398be454c9SAlice Michael u16 datalen; 8408be454c9SAlice Michael u16 flags; 8418be454c9SAlice Michael u16 ntu; 8428be454c9SAlice Michael 8438be454c9SAlice Michael /* pre-clean the event info */ 8448be454c9SAlice Michael memset(&e->desc, 0, sizeof(e->desc)); 8458be454c9SAlice Michael 8468be454c9SAlice Michael /* take the lock before we start messing with the ring */ 8478be454c9SAlice Michael mutex_lock(&hw->aq.arq_mutex); 8488be454c9SAlice Michael 8498be454c9SAlice Michael if (hw->aq.arq.count == 0) { 8508be454c9SAlice Michael iavf_debug(hw, IAVF_DEBUG_AQ_MESSAGE, 8518be454c9SAlice Michael "AQRX: Admin queue not initialized.\n"); 8528821b3faSAlice Michael ret_code = IAVF_ERR_QUEUE_EMPTY; 8538be454c9SAlice Michael goto clean_arq_element_err; 8548be454c9SAlice Michael } 8558be454c9SAlice Michael 8568be454c9SAlice Michael /* set next_to_use to head */ 857*3d66f215SIvan Vecera ntu = rd32(hw, IAVF_VF_ARQH1) & IAVF_VF_ARQH1_ARQH_MASK; 8588be454c9SAlice Michael if (ntu == ntc) { 8598be454c9SAlice Michael /* nothing to do - shouldn't need to update ring's values */ 8608821b3faSAlice Michael ret_code = IAVF_ERR_ADMIN_QUEUE_NO_WORK; 8618be454c9SAlice Michael goto clean_arq_element_out; 8628be454c9SAlice Michael } 8638be454c9SAlice Michael 8648be454c9SAlice Michael /* now clean the next descriptor */ 8658be454c9SAlice Michael desc = IAVF_ADMINQ_DESC(hw->aq.arq, ntc); 8668be454c9SAlice Michael desc_idx = ntc; 8678be454c9SAlice Michael 8688be454c9SAlice Michael hw->aq.arq_last_status = 8697af36e32SAlice Michael (enum iavf_admin_queue_err)le16_to_cpu(desc->retval); 8708be454c9SAlice Michael flags = le16_to_cpu(desc->flags); 8717af36e32SAlice Michael if (flags & IAVF_AQ_FLAG_ERR) { 8728821b3faSAlice Michael ret_code = IAVF_ERR_ADMIN_QUEUE_ERROR; 8738be454c9SAlice Michael iavf_debug(hw, 8748be454c9SAlice Michael IAVF_DEBUG_AQ_MESSAGE, 8758be454c9SAlice Michael "AQRX: Event received with error 0x%X.\n", 8768be454c9SAlice Michael hw->aq.arq_last_status); 8778be454c9SAlice Michael } 8788be454c9SAlice Michael 8798be454c9SAlice Michael e->desc = *desc; 8808be454c9SAlice Michael datalen = le16_to_cpu(desc->datalen); 8818be454c9SAlice Michael e->msg_len = min(datalen, e->buf_len); 8828be454c9SAlice Michael if (e->msg_buf && (e->msg_len != 0)) 8838be454c9SAlice Michael memcpy(e->msg_buf, hw->aq.arq.r.arq_bi[desc_idx].va, 8848be454c9SAlice Michael e->msg_len); 8858be454c9SAlice Michael 8868be454c9SAlice Michael iavf_debug(hw, IAVF_DEBUG_AQ_MESSAGE, "AQRX: desc and buffer:\n"); 8878be454c9SAlice Michael iavf_debug_aq(hw, IAVF_DEBUG_AQ_COMMAND, (void *)desc, e->msg_buf, 8888be454c9SAlice Michael hw->aq.arq_buf_size); 8898be454c9SAlice Michael 8908be454c9SAlice Michael /* Restore the original datalen and buffer address in the desc, 8918be454c9SAlice Michael * FW updates datalen to indicate the event message 8928be454c9SAlice Michael * size 8938be454c9SAlice Michael */ 8948be454c9SAlice Michael bi = &hw->aq.arq.r.arq_bi[ntc]; 8957af36e32SAlice Michael memset((void *)desc, 0, sizeof(struct iavf_aq_desc)); 8968be454c9SAlice Michael 8977af36e32SAlice Michael desc->flags = cpu_to_le16(IAVF_AQ_FLAG_BUF); 8987af36e32SAlice Michael if (hw->aq.arq_buf_size > IAVF_AQ_LARGE_BUF) 8997af36e32SAlice Michael desc->flags |= cpu_to_le16(IAVF_AQ_FLAG_LB); 9008be454c9SAlice Michael desc->datalen = cpu_to_le16((u16)bi->size); 9018be454c9SAlice Michael desc->params.external.addr_high = cpu_to_le32(upper_32_bits(bi->pa)); 9028be454c9SAlice Michael desc->params.external.addr_low = cpu_to_le32(lower_32_bits(bi->pa)); 9038be454c9SAlice Michael 9048be454c9SAlice Michael /* set tail = the last cleaned desc index. */ 905*3d66f215SIvan Vecera wr32(hw, IAVF_VF_ARQT1, ntc); 9068be454c9SAlice Michael /* ntc is updated to tail + 1 */ 9078be454c9SAlice Michael ntc++; 9088be454c9SAlice Michael if (ntc == hw->aq.num_arq_entries) 9098be454c9SAlice Michael ntc = 0; 9108be454c9SAlice Michael hw->aq.arq.next_to_clean = ntc; 9118be454c9SAlice Michael hw->aq.arq.next_to_use = ntu; 9128be454c9SAlice Michael 9138be454c9SAlice Michael clean_arq_element_out: 9148be454c9SAlice Michael /* Set pending if needed, unlock and return */ 9158be454c9SAlice Michael if (pending) 9168be454c9SAlice Michael *pending = (ntc > ntu ? hw->aq.arq.count : 0) + (ntu - ntc); 9178be454c9SAlice Michael 9188be454c9SAlice Michael clean_arq_element_err: 9198be454c9SAlice Michael mutex_unlock(&hw->aq.arq_mutex); 9208be454c9SAlice Michael 9218be454c9SAlice Michael return ret_code; 9228be454c9SAlice Michael } 923