1 /******************************************************************************
3 Copyright (c) 2013-2015, Intel Corporation
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions are met:
9 1. Redistributions of source code must retain the above copyright notice,
10 this list of conditions and the following disclaimer.
12 2. Redistributions in binary form must reproduce the above copyright
13 notice, this list of conditions and the following disclaimer in the
14 documentation and/or other materials provided with the distribution.
16 3. Neither the name of the Intel Corporation nor the names of its
17 contributors may be used to endorse or promote products derived from
18 this software without specific prior written permission.
20 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 POSSIBILITY OF SUCH DAMAGE.
32 ******************************************************************************/
35 #include "i40e_status.h"
36 #include "i40e_type.h"
37 #include "i40e_register.h"
38 #include "i40e_adminq.h"
39 #include "i40e_prototype.h"
42 * i40e_is_nvm_update_op - return TRUE if this is an NVM update operation
43 * @desc: API request descriptor
45 static INLINE bool i40e_is_nvm_update_op(struct i40e_aq_desc *desc)
47 return (desc->opcode == CPU_TO_LE16(i40e_aqc_opc_nvm_erase)) ||
48 (desc->opcode == CPU_TO_LE16(i40e_aqc_opc_nvm_update));
52 * i40e_adminq_init_regs - Initialize AdminQ registers
53 * @hw: pointer to the hardware structure
55 * This assumes the alloc_asq and alloc_arq functions have already been called
57 static void i40e_adminq_init_regs(struct i40e_hw *hw)
59 /* set head and tail registers in our local struct */
61 hw->aq.asq.tail = I40E_VF_ATQT1;
62 hw->aq.asq.head = I40E_VF_ATQH1;
63 hw->aq.asq.len = I40E_VF_ATQLEN1;
64 hw->aq.asq.bal = I40E_VF_ATQBAL1;
65 hw->aq.asq.bah = I40E_VF_ATQBAH1;
66 hw->aq.arq.tail = I40E_VF_ARQT1;
67 hw->aq.arq.head = I40E_VF_ARQH1;
68 hw->aq.arq.len = I40E_VF_ARQLEN1;
69 hw->aq.arq.bal = I40E_VF_ARQBAL1;
70 hw->aq.arq.bah = I40E_VF_ARQBAH1;
72 hw->aq.asq.tail = I40E_PF_ATQT;
73 hw->aq.asq.head = I40E_PF_ATQH;
74 hw->aq.asq.len = I40E_PF_ATQLEN;
75 hw->aq.asq.bal = I40E_PF_ATQBAL;
76 hw->aq.asq.bah = I40E_PF_ATQBAH;
77 hw->aq.arq.tail = I40E_PF_ARQT;
78 hw->aq.arq.head = I40E_PF_ARQH;
79 hw->aq.arq.len = I40E_PF_ARQLEN;
80 hw->aq.arq.bal = I40E_PF_ARQBAL;
81 hw->aq.arq.bah = I40E_PF_ARQBAH;
86 * i40e_alloc_adminq_asq_ring - Allocate Admin Queue send rings
87 * @hw: pointer to the hardware structure
89 enum i40e_status_code i40e_alloc_adminq_asq_ring(struct i40e_hw *hw)
91 enum i40e_status_code ret_code;
93 ret_code = i40e_allocate_dma_mem(hw, &hw->aq.asq.desc_buf,
95 (hw->aq.num_asq_entries *
96 sizeof(struct i40e_aq_desc)),
97 I40E_ADMINQ_DESC_ALIGNMENT);
101 ret_code = i40e_allocate_virt_mem(hw, &hw->aq.asq.cmd_buf,
102 (hw->aq.num_asq_entries *
103 sizeof(struct i40e_asq_cmd_details)));
105 i40e_free_dma_mem(hw, &hw->aq.asq.desc_buf);
113 * i40e_alloc_adminq_arq_ring - Allocate Admin Queue receive rings
114 * @hw: pointer to the hardware structure
116 enum i40e_status_code i40e_alloc_adminq_arq_ring(struct i40e_hw *hw)
118 enum i40e_status_code ret_code;
120 ret_code = i40e_allocate_dma_mem(hw, &hw->aq.arq.desc_buf,
122 (hw->aq.num_arq_entries *
123 sizeof(struct i40e_aq_desc)),
124 I40E_ADMINQ_DESC_ALIGNMENT);
130 * i40e_free_adminq_asq - Free Admin Queue send rings
131 * @hw: pointer to the hardware structure
133 * This assumes the posted send buffers have already been cleaned
136 void i40e_free_adminq_asq(struct i40e_hw *hw)
138 i40e_free_dma_mem(hw, &hw->aq.asq.desc_buf);
142 * i40e_free_adminq_arq - Free Admin Queue receive rings
143 * @hw: pointer to the hardware structure
145 * This assumes the posted receive buffers have already been cleaned
148 void i40e_free_adminq_arq(struct i40e_hw *hw)
150 i40e_free_dma_mem(hw, &hw->aq.arq.desc_buf);
154 * i40e_alloc_arq_bufs - Allocate pre-posted buffers for the receive queue
155 * @hw: pointer to the hardware structure
157 static enum i40e_status_code i40e_alloc_arq_bufs(struct i40e_hw *hw)
159 enum i40e_status_code ret_code;
160 struct i40e_aq_desc *desc;
161 struct i40e_dma_mem *bi;
164 /* We'll be allocating the buffer info memory first, then we can
165 * allocate the mapped buffers for the event processing
168 /* buffer_info structures do not need alignment */
169 ret_code = i40e_allocate_virt_mem(hw, &hw->aq.arq.dma_head,
170 (hw->aq.num_arq_entries * sizeof(struct i40e_dma_mem)));
173 hw->aq.arq.r.arq_bi = (struct i40e_dma_mem *)hw->aq.arq.dma_head.va;
175 /* allocate the mapped buffers */
176 for (i = 0; i < hw->aq.num_arq_entries; i++) {
177 bi = &hw->aq.arq.r.arq_bi[i];
178 ret_code = i40e_allocate_dma_mem(hw, bi,
181 I40E_ADMINQ_DESC_ALIGNMENT);
183 goto unwind_alloc_arq_bufs;
185 /* now configure the descriptors for use */
186 desc = I40E_ADMINQ_DESC(hw->aq.arq, i);
188 desc->flags = CPU_TO_LE16(I40E_AQ_FLAG_BUF);
189 if (hw->aq.arq_buf_size > I40E_AQ_LARGE_BUF)
190 desc->flags |= CPU_TO_LE16(I40E_AQ_FLAG_LB);
192 /* This is in accordance with Admin queue design, there is no
193 * register for buffer size configuration
195 desc->datalen = CPU_TO_LE16((u16)bi->size);
197 desc->cookie_high = 0;
198 desc->cookie_low = 0;
199 desc->params.external.addr_high =
200 CPU_TO_LE32(I40E_HI_DWORD(bi->pa));
201 desc->params.external.addr_low =
202 CPU_TO_LE32(I40E_LO_DWORD(bi->pa));
203 desc->params.external.param0 = 0;
204 desc->params.external.param1 = 0;
210 unwind_alloc_arq_bufs:
211 /* don't try to free the one that failed... */
214 i40e_free_dma_mem(hw, &hw->aq.arq.r.arq_bi[i]);
215 i40e_free_virt_mem(hw, &hw->aq.arq.dma_head);
221 * i40e_alloc_asq_bufs - Allocate empty buffer structs for the send queue
222 * @hw: pointer to the hardware structure
224 static enum i40e_status_code i40e_alloc_asq_bufs(struct i40e_hw *hw)
226 enum i40e_status_code ret_code;
227 struct i40e_dma_mem *bi;
230 /* No mapped memory needed yet, just the buffer info structures */
231 ret_code = i40e_allocate_virt_mem(hw, &hw->aq.asq.dma_head,
232 (hw->aq.num_asq_entries * sizeof(struct i40e_dma_mem)));
235 hw->aq.asq.r.asq_bi = (struct i40e_dma_mem *)hw->aq.asq.dma_head.va;
237 /* allocate the mapped buffers */
238 for (i = 0; i < hw->aq.num_asq_entries; i++) {
239 bi = &hw->aq.asq.r.asq_bi[i];
240 ret_code = i40e_allocate_dma_mem(hw, bi,
243 I40E_ADMINQ_DESC_ALIGNMENT);
245 goto unwind_alloc_asq_bufs;
250 unwind_alloc_asq_bufs:
251 /* don't try to free the one that failed... */
254 i40e_free_dma_mem(hw, &hw->aq.asq.r.asq_bi[i]);
255 i40e_free_virt_mem(hw, &hw->aq.asq.dma_head);
261 * i40e_free_arq_bufs - Free receive queue buffer info elements
262 * @hw: pointer to the hardware structure
264 static void i40e_free_arq_bufs(struct i40e_hw *hw)
268 /* free descriptors */
269 for (i = 0; i < hw->aq.num_arq_entries; i++)
270 i40e_free_dma_mem(hw, &hw->aq.arq.r.arq_bi[i]);
272 /* free the descriptor memory */
273 i40e_free_dma_mem(hw, &hw->aq.arq.desc_buf);
275 /* free the dma header */
276 i40e_free_virt_mem(hw, &hw->aq.arq.dma_head);
280 * i40e_free_asq_bufs - Free send queue buffer info elements
281 * @hw: pointer to the hardware structure
283 static void i40e_free_asq_bufs(struct i40e_hw *hw)
287 /* only unmap if the address is non-NULL */
288 for (i = 0; i < hw->aq.num_asq_entries; i++)
289 if (hw->aq.asq.r.asq_bi[i].pa)
290 i40e_free_dma_mem(hw, &hw->aq.asq.r.asq_bi[i]);
292 /* free the buffer info list */
293 i40e_free_virt_mem(hw, &hw->aq.asq.cmd_buf);
295 /* free the descriptor memory */
296 i40e_free_dma_mem(hw, &hw->aq.asq.desc_buf);
298 /* free the dma header */
299 i40e_free_virt_mem(hw, &hw->aq.asq.dma_head);
303 * i40e_config_asq_regs - configure ASQ registers
304 * @hw: pointer to the hardware structure
306 * Configure base address and length registers for the transmit queue
308 static enum i40e_status_code i40e_config_asq_regs(struct i40e_hw *hw)
310 enum i40e_status_code ret_code = I40E_SUCCESS;
313 /* Clear Head and Tail */
314 wr32(hw, hw->aq.asq.head, 0);
315 wr32(hw, hw->aq.asq.tail, 0);
317 /* set starting point */
319 wr32(hw, hw->aq.asq.len, (hw->aq.num_asq_entries |
320 I40E_PF_ATQLEN_ATQENABLE_MASK));
322 wr32(hw, hw->aq.asq.len, (hw->aq.num_asq_entries |
323 I40E_VF_ATQLEN1_ATQENABLE_MASK));
324 wr32(hw, hw->aq.asq.bal, I40E_LO_DWORD(hw->aq.asq.desc_buf.pa));
325 wr32(hw, hw->aq.asq.bah, I40E_HI_DWORD(hw->aq.asq.desc_buf.pa));
327 /* Check one register to verify that config was applied */
328 reg = rd32(hw, hw->aq.asq.bal);
329 if (reg != I40E_LO_DWORD(hw->aq.asq.desc_buf.pa))
330 ret_code = I40E_ERR_ADMIN_QUEUE_ERROR;
336 * i40e_config_arq_regs - ARQ register configuration
337 * @hw: pointer to the hardware structure
339 * Configure base address and length registers for the receive (event queue)
341 static enum i40e_status_code i40e_config_arq_regs(struct i40e_hw *hw)
343 enum i40e_status_code ret_code = I40E_SUCCESS;
346 /* Clear Head and Tail */
347 wr32(hw, hw->aq.arq.head, 0);
348 wr32(hw, hw->aq.arq.tail, 0);
350 /* set starting point */
352 wr32(hw, hw->aq.arq.len, (hw->aq.num_arq_entries |
353 I40E_PF_ARQLEN_ARQENABLE_MASK));
355 wr32(hw, hw->aq.arq.len, (hw->aq.num_arq_entries |
356 I40E_VF_ARQLEN1_ARQENABLE_MASK));
357 wr32(hw, hw->aq.arq.bal, I40E_LO_DWORD(hw->aq.arq.desc_buf.pa));
358 wr32(hw, hw->aq.arq.bah, I40E_HI_DWORD(hw->aq.arq.desc_buf.pa));
360 /* Update tail in the HW to post pre-allocated buffers */
361 wr32(hw, hw->aq.arq.tail, hw->aq.num_arq_entries - 1);
363 /* Check one register to verify that config was applied */
364 reg = rd32(hw, hw->aq.arq.bal);
365 if (reg != I40E_LO_DWORD(hw->aq.arq.desc_buf.pa))
366 ret_code = I40E_ERR_ADMIN_QUEUE_ERROR;
372 * i40e_init_asq - main initialization routine for ASQ
373 * @hw: pointer to the hardware structure
375 * This is the main initialization routine for the Admin Send Queue
376 * Prior to calling this function, drivers *MUST* set the following fields
377 * in the hw->aq structure:
378 * - hw->aq.num_asq_entries
379 * - hw->aq.arq_buf_size
381 * Do *NOT* hold the lock when calling this as the memory allocation routines
382 * called are not going to be atomic context safe
384 enum i40e_status_code i40e_init_asq(struct i40e_hw *hw)
386 enum i40e_status_code ret_code = I40E_SUCCESS;
388 if (hw->aq.asq.count > 0) {
389 /* queue already initialized */
390 ret_code = I40E_ERR_NOT_READY;
391 goto init_adminq_exit;
394 /* verify input for valid configuration */
395 if ((hw->aq.num_asq_entries == 0) ||
396 (hw->aq.asq_buf_size == 0)) {
397 ret_code = I40E_ERR_CONFIG;
398 goto init_adminq_exit;
401 hw->aq.asq.next_to_use = 0;
402 hw->aq.asq.next_to_clean = 0;
404 /* allocate the ring memory */
405 ret_code = i40e_alloc_adminq_asq_ring(hw);
406 if (ret_code != I40E_SUCCESS)
407 goto init_adminq_exit;
409 /* allocate buffers in the rings */
410 ret_code = i40e_alloc_asq_bufs(hw);
411 if (ret_code != I40E_SUCCESS)
412 goto init_adminq_free_rings;
414 /* initialize base registers */
415 ret_code = i40e_config_asq_regs(hw);
416 if (ret_code != I40E_SUCCESS)
417 goto init_adminq_free_rings;
420 hw->aq.asq.count = hw->aq.num_asq_entries;
421 goto init_adminq_exit;
423 init_adminq_free_rings:
424 i40e_free_adminq_asq(hw);
431 * i40e_init_arq - initialize ARQ
432 * @hw: pointer to the hardware structure
434 * The main initialization routine for the Admin Receive (Event) Queue.
435 * Prior to calling this function, drivers *MUST* set the following fields
436 * in the hw->aq structure:
437 * - hw->aq.num_asq_entries
438 * - hw->aq.arq_buf_size
440 * Do *NOT* hold the lock when calling this as the memory allocation routines
441 * called are not going to be atomic context safe
443 enum i40e_status_code i40e_init_arq(struct i40e_hw *hw)
445 enum i40e_status_code ret_code = I40E_SUCCESS;
447 if (hw->aq.arq.count > 0) {
448 /* queue already initialized */
449 ret_code = I40E_ERR_NOT_READY;
450 goto init_adminq_exit;
453 /* verify input for valid configuration */
454 if ((hw->aq.num_arq_entries == 0) ||
455 (hw->aq.arq_buf_size == 0)) {
456 ret_code = I40E_ERR_CONFIG;
457 goto init_adminq_exit;
460 hw->aq.arq.next_to_use = 0;
461 hw->aq.arq.next_to_clean = 0;
463 /* allocate the ring memory */
464 ret_code = i40e_alloc_adminq_arq_ring(hw);
465 if (ret_code != I40E_SUCCESS)
466 goto init_adminq_exit;
468 /* allocate buffers in the rings */
469 ret_code = i40e_alloc_arq_bufs(hw);
470 if (ret_code != I40E_SUCCESS)
471 goto init_adminq_free_rings;
473 /* initialize base registers */
474 ret_code = i40e_config_arq_regs(hw);
475 if (ret_code != I40E_SUCCESS)
476 goto init_adminq_free_rings;
479 hw->aq.arq.count = hw->aq.num_arq_entries;
480 goto init_adminq_exit;
482 init_adminq_free_rings:
483 i40e_free_adminq_arq(hw);
490 * i40e_shutdown_asq - shutdown the ASQ
491 * @hw: pointer to the hardware structure
493 * The main shutdown routine for the Admin Send Queue
495 enum i40e_status_code i40e_shutdown_asq(struct i40e_hw *hw)
497 enum i40e_status_code ret_code = I40E_SUCCESS;
499 i40e_acquire_spinlock(&hw->aq.asq_spinlock);
501 if (hw->aq.asq.count == 0) {
502 ret_code = I40E_ERR_NOT_READY;
503 goto shutdown_asq_out;
506 /* Stop firmware AdminQ processing */
507 wr32(hw, hw->aq.asq.head, 0);
508 wr32(hw, hw->aq.asq.tail, 0);
509 wr32(hw, hw->aq.asq.len, 0);
510 wr32(hw, hw->aq.asq.bal, 0);
511 wr32(hw, hw->aq.asq.bah, 0);
513 hw->aq.asq.count = 0; /* to indicate uninitialized queue */
515 /* free ring buffers */
516 i40e_free_asq_bufs(hw);
519 i40e_release_spinlock(&hw->aq.asq_spinlock);
524 * i40e_shutdown_arq - shutdown ARQ
525 * @hw: pointer to the hardware structure
527 * The main shutdown routine for the Admin Receive Queue
529 enum i40e_status_code i40e_shutdown_arq(struct i40e_hw *hw)
531 enum i40e_status_code ret_code = I40E_SUCCESS;
533 i40e_acquire_spinlock(&hw->aq.arq_spinlock);
535 if (hw->aq.arq.count == 0) {
536 ret_code = I40E_ERR_NOT_READY;
537 goto shutdown_arq_out;
540 /* Stop firmware AdminQ processing */
541 wr32(hw, hw->aq.arq.head, 0);
542 wr32(hw, hw->aq.arq.tail, 0);
543 wr32(hw, hw->aq.arq.len, 0);
544 wr32(hw, hw->aq.arq.bal, 0);
545 wr32(hw, hw->aq.arq.bah, 0);
547 hw->aq.arq.count = 0; /* to indicate uninitialized queue */
549 /* free ring buffers */
550 i40e_free_arq_bufs(hw);
553 i40e_release_spinlock(&hw->aq.arq_spinlock);
558 * i40e_resume_aq - resume AQ processing from 0
559 * @hw: pointer to the hardware structure
561 static void i40e_resume_aq(struct i40e_hw *hw)
563 /* Registers are reset after PF reset */
564 hw->aq.asq.next_to_use = 0;
565 hw->aq.asq.next_to_clean = 0;
567 i40e_config_asq_regs(hw);
569 hw->aq.arq.next_to_use = 0;
570 hw->aq.arq.next_to_clean = 0;
572 i40e_config_arq_regs(hw);
576 * i40e_init_adminq - main initialization routine for Admin Queue
577 * @hw: pointer to the hardware structure
579 * Prior to calling this function, drivers *MUST* set the following fields
580 * in the hw->aq structure:
581 * - hw->aq.num_asq_entries
582 * - hw->aq.num_arq_entries
583 * - hw->aq.arq_buf_size
584 * - hw->aq.asq_buf_size
586 enum i40e_status_code i40e_init_adminq(struct i40e_hw *hw)
588 u16 cfg_ptr, oem_hi, oem_lo;
589 u16 eetrack_lo, eetrack_hi;
590 enum i40e_status_code ret_code;
593 /* verify input for valid configuration */
594 if ((hw->aq.num_arq_entries == 0) ||
595 (hw->aq.num_asq_entries == 0) ||
596 (hw->aq.arq_buf_size == 0) ||
597 (hw->aq.asq_buf_size == 0)) {
598 ret_code = I40E_ERR_CONFIG;
599 goto init_adminq_exit;
601 i40e_init_spinlock(&hw->aq.asq_spinlock);
602 i40e_init_spinlock(&hw->aq.arq_spinlock);
604 /* Set up register offsets */
605 i40e_adminq_init_regs(hw);
607 /* setup ASQ command write back timeout */
608 hw->aq.asq_cmd_timeout = I40E_ASQ_CMD_TIMEOUT;
610 /* allocate the ASQ */
611 ret_code = i40e_init_asq(hw);
612 if (ret_code != I40E_SUCCESS)
613 goto init_adminq_destroy_spinlocks;
615 /* allocate the ARQ */
616 ret_code = i40e_init_arq(hw);
617 if (ret_code != I40E_SUCCESS)
618 goto init_adminq_free_asq;
620 /* VF has no need of firmware */
622 goto init_adminq_exit;
623 /* There are some cases where the firmware may not be quite ready
624 * for AdminQ operations, so we retry the AdminQ setup a few times
625 * if we see timeouts in this first AQ call.
628 ret_code = i40e_aq_get_firmware_version(hw,
635 if (ret_code != I40E_ERR_ADMIN_QUEUE_TIMEOUT)
638 i40e_msec_delay(100);
640 } while (retry < 10);
641 if (ret_code != I40E_SUCCESS)
642 goto init_adminq_free_arq;
644 /* get the NVM version info */
645 i40e_read_nvm_word(hw, I40E_SR_NVM_DEV_STARTER_VERSION,
647 i40e_read_nvm_word(hw, I40E_SR_NVM_EETRACK_LO, &eetrack_lo);
648 i40e_read_nvm_word(hw, I40E_SR_NVM_EETRACK_HI, &eetrack_hi);
649 hw->nvm.eetrack = (eetrack_hi << 16) | eetrack_lo;
650 i40e_read_nvm_word(hw, I40E_SR_BOOT_CONFIG_PTR, &cfg_ptr);
651 i40e_read_nvm_word(hw, (cfg_ptr + I40E_NVM_OEM_VER_OFF),
653 i40e_read_nvm_word(hw, (cfg_ptr + (I40E_NVM_OEM_VER_OFF + 1)),
655 hw->nvm.oem_ver = ((u32)oem_hi << 16) | oem_lo;
657 if (hw->aq.api_maj_ver > I40E_FW_API_VERSION_MAJOR) {
658 ret_code = I40E_ERR_FIRMWARE_API_VERSION;
659 goto init_adminq_free_arq;
662 /* pre-emptive resource lock release */
663 i40e_aq_release_resource(hw, I40E_NVM_RESOURCE_ID, 0, NULL);
664 hw->aq.nvm_release_on_done = FALSE;
665 hw->nvmupd_state = I40E_NVMUPD_STATE_INIT;
667 ret_code = i40e_aq_set_hmc_resource_profile(hw,
668 I40E_HMC_PROFILE_DEFAULT,
671 ret_code = I40E_SUCCESS;
674 goto init_adminq_exit;
676 init_adminq_free_arq:
677 i40e_shutdown_arq(hw);
678 init_adminq_free_asq:
679 i40e_shutdown_asq(hw);
680 init_adminq_destroy_spinlocks:
681 i40e_destroy_spinlock(&hw->aq.asq_spinlock);
682 i40e_destroy_spinlock(&hw->aq.arq_spinlock);
689 * i40e_shutdown_adminq - shutdown routine for the Admin Queue
690 * @hw: pointer to the hardware structure
692 enum i40e_status_code i40e_shutdown_adminq(struct i40e_hw *hw)
694 enum i40e_status_code ret_code = I40E_SUCCESS;
696 if (i40e_check_asq_alive(hw))
697 i40e_aq_queue_shutdown(hw, TRUE);
699 i40e_shutdown_asq(hw);
700 i40e_shutdown_arq(hw);
701 i40e_destroy_spinlock(&hw->aq.asq_spinlock);
702 i40e_destroy_spinlock(&hw->aq.arq_spinlock);
705 i40e_free_virt_mem(hw, &hw->nvm_buff);
711 * i40e_clean_asq - cleans Admin send queue
712 * @hw: pointer to the hardware structure
714 * returns the number of free desc
716 u16 i40e_clean_asq(struct i40e_hw *hw)
718 struct i40e_adminq_ring *asq = &(hw->aq.asq);
719 struct i40e_asq_cmd_details *details;
720 u16 ntc = asq->next_to_clean;
721 struct i40e_aq_desc desc_cb;
722 struct i40e_aq_desc *desc;
724 desc = I40E_ADMINQ_DESC(*asq, ntc);
725 details = I40E_ADMINQ_DETAILS(*asq, ntc);
726 while (rd32(hw, hw->aq.asq.head) != ntc) {
727 i40e_debug(hw, I40E_DEBUG_AQ_MESSAGE,
728 "ntc %d head %d.\n", ntc, rd32(hw, hw->aq.asq.head));
730 if (details->callback) {
731 I40E_ADMINQ_CALLBACK cb_func =
732 (I40E_ADMINQ_CALLBACK)details->callback;
733 i40e_memcpy(&desc_cb, desc, sizeof(struct i40e_aq_desc),
735 cb_func(hw, &desc_cb);
737 i40e_memset(desc, 0, sizeof(*desc), I40E_DMA_MEM);
738 i40e_memset(details, 0, sizeof(*details), I40E_NONDMA_MEM);
740 if (ntc == asq->count)
742 desc = I40E_ADMINQ_DESC(*asq, ntc);
743 details = I40E_ADMINQ_DETAILS(*asq, ntc);
746 asq->next_to_clean = ntc;
748 return I40E_DESC_UNUSED(asq);
752 * i40e_asq_done - check if FW has processed the Admin Send Queue
753 * @hw: pointer to the hw struct
755 * Returns TRUE if the firmware has processed all descriptors on the
756 * admin send queue. Returns FALSE if there are still requests pending.
758 bool i40e_asq_done(struct i40e_hw *hw)
760 /* AQ designers suggest use of head for better
761 * timing reliability than DD bit
763 return rd32(hw, hw->aq.asq.head) == hw->aq.asq.next_to_use;
768 * i40e_asq_send_command - send command to Admin Queue
769 * @hw: pointer to the hw struct
770 * @desc: prefilled descriptor describing the command (non DMA mem)
771 * @buff: buffer to use for indirect commands
772 * @buff_size: size of buffer for indirect commands
773 * @cmd_details: pointer to command details structure
775 * This is the main send command driver routine for the Admin Queue send
776 * queue. It runs the queue, cleans the queue, etc
778 enum i40e_status_code i40e_asq_send_command(struct i40e_hw *hw,
779 struct i40e_aq_desc *desc,
780 void *buff, /* can be NULL */
782 struct i40e_asq_cmd_details *cmd_details)
784 enum i40e_status_code status = I40E_SUCCESS;
785 struct i40e_dma_mem *dma_buff = NULL;
786 struct i40e_asq_cmd_details *details;
787 struct i40e_aq_desc *desc_on_ring;
788 bool cmd_completed = FALSE;
792 i40e_acquire_spinlock(&hw->aq.asq_spinlock);
794 hw->aq.asq_last_status = I40E_AQ_RC_OK;
796 if (hw->aq.asq.count == 0) {
797 i40e_debug(hw, I40E_DEBUG_AQ_MESSAGE,
798 "AQTX: Admin queue not initialized.\n");
799 status = I40E_ERR_QUEUE_EMPTY;
800 goto asq_send_command_error;
803 val = rd32(hw, hw->aq.asq.head);
804 if (val >= hw->aq.num_asq_entries) {
805 i40e_debug(hw, I40E_DEBUG_AQ_MESSAGE,
806 "AQTX: head overrun at %d\n", val);
807 status = I40E_ERR_QUEUE_EMPTY;
808 goto asq_send_command_error;
811 details = I40E_ADMINQ_DETAILS(hw->aq.asq, hw->aq.asq.next_to_use);
815 sizeof(struct i40e_asq_cmd_details),
816 I40E_NONDMA_TO_NONDMA);
818 /* If the cmd_details are defined copy the cookie. The
819 * CPU_TO_LE32 is not needed here because the data is ignored
820 * by the FW, only used by the driver
822 if (details->cookie) {
824 CPU_TO_LE32(I40E_HI_DWORD(details->cookie));
826 CPU_TO_LE32(I40E_LO_DWORD(details->cookie));
829 i40e_memset(details, 0,
830 sizeof(struct i40e_asq_cmd_details),
834 /* clear requested flags and then set additional flags if defined */
835 desc->flags &= ~CPU_TO_LE16(details->flags_dis);
836 desc->flags |= CPU_TO_LE16(details->flags_ena);
838 if (buff_size > hw->aq.asq_buf_size) {
840 I40E_DEBUG_AQ_MESSAGE,
841 "AQTX: Invalid buffer size: %d.\n",
843 status = I40E_ERR_INVALID_SIZE;
844 goto asq_send_command_error;
847 if (details->postpone && !details->async) {
849 I40E_DEBUG_AQ_MESSAGE,
850 "AQTX: Async flag not set along with postpone flag");
851 status = I40E_ERR_PARAM;
852 goto asq_send_command_error;
855 /* call clean and check queue available function to reclaim the
856 * descriptors that were processed by FW, the function returns the
857 * number of desc available
859 /* the clean function called here could be called in a separate thread
860 * in case of asynchronous completions
862 if (i40e_clean_asq(hw) == 0) {
864 I40E_DEBUG_AQ_MESSAGE,
865 "AQTX: Error queue is full.\n");
866 status = I40E_ERR_ADMIN_QUEUE_FULL;
867 goto asq_send_command_error;
870 /* initialize the temp desc pointer with the right desc */
871 desc_on_ring = I40E_ADMINQ_DESC(hw->aq.asq, hw->aq.asq.next_to_use);
873 /* if the desc is available copy the temp desc to the right place */
874 i40e_memcpy(desc_on_ring, desc, sizeof(struct i40e_aq_desc),
877 /* if buff is not NULL assume indirect command */
879 dma_buff = &(hw->aq.asq.r.asq_bi[hw->aq.asq.next_to_use]);
880 /* copy the user buff into the respective DMA buff */
881 i40e_memcpy(dma_buff->va, buff, buff_size,
883 desc_on_ring->datalen = CPU_TO_LE16(buff_size);
885 /* Update the address values in the desc with the pa value
886 * for respective buffer
888 desc_on_ring->params.external.addr_high =
889 CPU_TO_LE32(I40E_HI_DWORD(dma_buff->pa));
890 desc_on_ring->params.external.addr_low =
891 CPU_TO_LE32(I40E_LO_DWORD(dma_buff->pa));
895 i40e_debug(hw, I40E_DEBUG_AQ_MESSAGE, "AQTX: desc and buffer:\n");
896 i40e_debug_aq(hw, I40E_DEBUG_AQ_COMMAND, (void *)desc_on_ring,
898 (hw->aq.asq.next_to_use)++;
899 if (hw->aq.asq.next_to_use == hw->aq.asq.count)
900 hw->aq.asq.next_to_use = 0;
901 if (!details->postpone)
902 wr32(hw, hw->aq.asq.tail, hw->aq.asq.next_to_use);
904 /* if cmd_details are not defined or async flag is not set,
905 * we need to wait for desc write back
907 if (!details->async && !details->postpone) {
911 /* AQ designers suggest use of head for better
912 * timing reliability than DD bit
914 if (i40e_asq_done(hw))
918 } while (total_delay < hw->aq.asq_cmd_timeout);
921 /* if ready, copy the desc back to temp */
922 if (i40e_asq_done(hw)) {
923 i40e_memcpy(desc, desc_on_ring, sizeof(struct i40e_aq_desc),
926 i40e_memcpy(buff, dma_buff->va, buff_size,
928 retval = LE16_TO_CPU(desc->retval);
931 I40E_DEBUG_AQ_MESSAGE,
932 "AQTX: Command completed with error 0x%X.\n",
935 /* strip off FW internal code */
938 cmd_completed = TRUE;
939 if ((enum i40e_admin_queue_err)retval == I40E_AQ_RC_OK)
940 status = I40E_SUCCESS;
942 status = I40E_ERR_ADMIN_QUEUE_ERROR;
943 hw->aq.asq_last_status = (enum i40e_admin_queue_err)retval;
946 i40e_debug(hw, I40E_DEBUG_AQ_MESSAGE,
947 "AQTX: desc and buffer writeback:\n");
948 i40e_debug_aq(hw, I40E_DEBUG_AQ_COMMAND, (void *)desc, buff, buff_size);
950 /* save writeback aq if requested */
951 if (details->wb_desc)
952 i40e_memcpy(details->wb_desc, desc_on_ring,
953 sizeof(struct i40e_aq_desc), I40E_DMA_TO_NONDMA);
955 /* update the error if time out occurred */
956 if ((!cmd_completed) &&
957 (!details->async && !details->postpone)) {
959 I40E_DEBUG_AQ_MESSAGE,
960 "AQTX: Writeback timeout.\n");
961 status = I40E_ERR_ADMIN_QUEUE_TIMEOUT;
964 asq_send_command_error:
965 i40e_release_spinlock(&hw->aq.asq_spinlock);
970 * i40e_fill_default_direct_cmd_desc - AQ descriptor helper function
971 * @desc: pointer to the temp descriptor (non DMA mem)
972 * @opcode: the opcode can be used to decide which flags to turn off or on
974 * Fill the desc with default values
976 void i40e_fill_default_direct_cmd_desc(struct i40e_aq_desc *desc,
979 /* zero out the desc */
980 i40e_memset((void *)desc, 0, sizeof(struct i40e_aq_desc),
982 desc->opcode = CPU_TO_LE16(opcode);
983 desc->flags = CPU_TO_LE16(I40E_AQ_FLAG_SI);
987 * i40e_clean_arq_element
988 * @hw: pointer to the hw struct
989 * @e: event info from the receive descriptor, includes any buffers
990 * @pending: number of events that could be left to process
992 * This function cleans one Admin Receive Queue element and returns
993 * the contents through e. It can also return how many events are
994 * left to process through 'pending'
996 enum i40e_status_code i40e_clean_arq_element(struct i40e_hw *hw,
997 struct i40e_arq_event_info *e,
1000 enum i40e_status_code ret_code = I40E_SUCCESS;
1001 u16 ntc = hw->aq.arq.next_to_clean;
1002 struct i40e_aq_desc *desc;
1003 struct i40e_dma_mem *bi;
1009 /* pre-clean the event info */
1010 i40e_memset(&e->desc, 0, sizeof(e->desc), I40E_NONDMA_MEM);
1012 /* take the lock before we start messing with the ring */
1013 i40e_acquire_spinlock(&hw->aq.arq_spinlock);
1015 if (hw->aq.arq.count == 0) {
1016 i40e_debug(hw, I40E_DEBUG_AQ_MESSAGE,
1017 "AQRX: Admin queue not initialized.\n");
1018 ret_code = I40E_ERR_QUEUE_EMPTY;
1019 goto clean_arq_element_err;
1022 /* set next_to_use to head */
1023 if (!i40e_is_vf(hw))
1024 ntu = (rd32(hw, hw->aq.arq.head) & I40E_PF_ARQH_ARQH_MASK);
1026 ntu = (rd32(hw, hw->aq.arq.head) & I40E_VF_ARQH1_ARQH_MASK);
1028 /* nothing to do - shouldn't need to update ring's values */
1029 ret_code = I40E_ERR_ADMIN_QUEUE_NO_WORK;
1030 goto clean_arq_element_out;
1033 /* now clean the next descriptor */
1034 desc = I40E_ADMINQ_DESC(hw->aq.arq, ntc);
1037 flags = LE16_TO_CPU(desc->flags);
1038 if (flags & I40E_AQ_FLAG_ERR) {
1039 ret_code = I40E_ERR_ADMIN_QUEUE_ERROR;
1040 hw->aq.arq_last_status =
1041 (enum i40e_admin_queue_err)LE16_TO_CPU(desc->retval);
1043 I40E_DEBUG_AQ_MESSAGE,
1044 "AQRX: Event received with error 0x%X.\n",
1045 hw->aq.arq_last_status);
1048 i40e_memcpy(&e->desc, desc, sizeof(struct i40e_aq_desc),
1049 I40E_DMA_TO_NONDMA);
1050 datalen = LE16_TO_CPU(desc->datalen);
1051 e->msg_len = min(datalen, e->buf_len);
1052 if (e->msg_buf != NULL && (e->msg_len != 0))
1053 i40e_memcpy(e->msg_buf,
1054 hw->aq.arq.r.arq_bi[desc_idx].va,
1055 e->msg_len, I40E_DMA_TO_NONDMA);
1057 i40e_debug(hw, I40E_DEBUG_AQ_MESSAGE, "AQRX: desc and buffer:\n");
1058 i40e_debug_aq(hw, I40E_DEBUG_AQ_COMMAND, (void *)desc, e->msg_buf,
1059 hw->aq.arq_buf_size);
1061 /* Restore the original datalen and buffer address in the desc,
1062 * FW updates datalen to indicate the event message
1065 bi = &hw->aq.arq.r.arq_bi[ntc];
1066 i40e_memset((void *)desc, 0, sizeof(struct i40e_aq_desc), I40E_DMA_MEM);
1068 desc->flags = CPU_TO_LE16(I40E_AQ_FLAG_BUF);
1069 if (hw->aq.arq_buf_size > I40E_AQ_LARGE_BUF)
1070 desc->flags |= CPU_TO_LE16(I40E_AQ_FLAG_LB);
1071 desc->datalen = CPU_TO_LE16((u16)bi->size);
1072 desc->params.external.addr_high = CPU_TO_LE32(I40E_HI_DWORD(bi->pa));
1073 desc->params.external.addr_low = CPU_TO_LE32(I40E_LO_DWORD(bi->pa));
1075 /* set tail = the last cleaned desc index. */
1076 wr32(hw, hw->aq.arq.tail, ntc);
1077 /* ntc is updated to tail + 1 */
1079 if (ntc == hw->aq.num_arq_entries)
1081 hw->aq.arq.next_to_clean = ntc;
1082 hw->aq.arq.next_to_use = ntu;
1084 if (i40e_is_nvm_update_op(&e->desc)) {
1085 if (hw->aq.nvm_release_on_done) {
1086 i40e_release_nvm(hw);
1087 hw->aq.nvm_release_on_done = FALSE;
1090 switch (hw->nvmupd_state) {
1091 case I40E_NVMUPD_STATE_INIT_WAIT:
1092 hw->nvmupd_state = I40E_NVMUPD_STATE_INIT;
1095 case I40E_NVMUPD_STATE_WRITE_WAIT:
1096 hw->nvmupd_state = I40E_NVMUPD_STATE_WRITING;
1104 clean_arq_element_out:
1105 /* Set pending if needed, unlock and return */
1106 if (pending != NULL)
1107 *pending = (ntc > ntu ? hw->aq.arq.count : 0) + (ntu - ntc);
1108 clean_arq_element_err:
1109 i40e_release_spinlock(&hw->aq.arq_spinlock);