1 /*******************************************************************************
3 * Intel Ethernet Controller XL710 Family Linux Driver
4 * Copyright(c) 2013 Intel Corporation.
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
10 * This program is distributed in the hope it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
19 * The full GNU General Public License is included in this distribution in
20 * the file called "COPYING".
22 * Contact Information:
23 * e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
24 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
26 ******************************************************************************/
28 #include "i40e_status.h"
29 #include "i40e_type.h"
30 #include "i40e_register.h"
31 #include "i40e_adminq.h"
32 #include "i40e_prototype.h"
34 static void i40e_resume_aq(struct i40e_hw *hw);
37 * i40e_adminq_init_regs - Initialize AdminQ registers
38 * @hw: pointer to the hardware structure
40 * This assumes the alloc_asq and alloc_arq functions have already been called
42 static void i40e_adminq_init_regs(struct i40e_hw *hw)
44 /* set head and tail registers in our local struct */
45 if (hw->mac.type == I40E_MAC_VF) {
46 hw->aq.asq.tail = I40E_VF_ATQT1;
47 hw->aq.asq.head = I40E_VF_ATQH1;
48 hw->aq.asq.len = I40E_VF_ATQLEN1;
49 hw->aq.arq.tail = I40E_VF_ARQT1;
50 hw->aq.arq.head = I40E_VF_ARQH1;
51 hw->aq.arq.len = I40E_VF_ARQLEN1;
53 hw->aq.asq.tail = I40E_PF_ATQT;
54 hw->aq.asq.head = I40E_PF_ATQH;
55 hw->aq.asq.len = I40E_PF_ATQLEN;
56 hw->aq.arq.tail = I40E_PF_ARQT;
57 hw->aq.arq.head = I40E_PF_ARQH;
58 hw->aq.arq.len = I40E_PF_ARQLEN;
63 * i40e_alloc_adminq_asq_ring - Allocate Admin Queue send rings
64 * @hw: pointer to the hardware structure
66 static i40e_status i40e_alloc_adminq_asq_ring(struct i40e_hw *hw)
70 ret_code = i40e_allocate_dma_mem(hw, &hw->aq.asq.desc_buf,
72 (hw->aq.num_asq_entries *
73 sizeof(struct i40e_aq_desc)),
74 I40E_ADMINQ_DESC_ALIGNMENT);
78 ret_code = i40e_allocate_virt_mem(hw, &hw->aq.asq.cmd_buf,
79 (hw->aq.num_asq_entries *
80 sizeof(struct i40e_asq_cmd_details)));
82 i40e_free_dma_mem(hw, &hw->aq.asq.desc_buf);
90 * i40e_alloc_adminq_arq_ring - Allocate Admin Queue receive rings
91 * @hw: pointer to the hardware structure
93 static i40e_status i40e_alloc_adminq_arq_ring(struct i40e_hw *hw)
97 ret_code = i40e_allocate_dma_mem(hw, &hw->aq.arq.desc_buf,
99 (hw->aq.num_arq_entries *
100 sizeof(struct i40e_aq_desc)),
101 I40E_ADMINQ_DESC_ALIGNMENT);
107 * i40e_free_adminq_asq - Free Admin Queue send rings
108 * @hw: pointer to the hardware structure
110 * This assumes the posted send buffers have already been cleaned
113 static void i40e_free_adminq_asq(struct i40e_hw *hw)
115 i40e_free_dma_mem(hw, &hw->aq.asq.desc_buf);
119 * i40e_free_adminq_arq - Free Admin Queue receive rings
120 * @hw: pointer to the hardware structure
122 * This assumes the posted receive buffers have already been cleaned
125 static void i40e_free_adminq_arq(struct i40e_hw *hw)
127 i40e_free_dma_mem(hw, &hw->aq.arq.desc_buf);
131 * i40e_alloc_arq_bufs - Allocate pre-posted buffers for the receive queue
132 * @hw: pointer to the hardware structure
134 static i40e_status i40e_alloc_arq_bufs(struct i40e_hw *hw)
136 i40e_status ret_code;
137 struct i40e_aq_desc *desc;
138 struct i40e_dma_mem *bi;
141 /* We'll be allocating the buffer info memory first, then we can
142 * allocate the mapped buffers for the event processing
145 /* buffer_info structures do not need alignment */
146 ret_code = i40e_allocate_virt_mem(hw, &hw->aq.arq.dma_head,
147 (hw->aq.num_arq_entries * sizeof(struct i40e_dma_mem)));
150 hw->aq.arq.r.arq_bi = (struct i40e_dma_mem *)hw->aq.arq.dma_head.va;
152 /* allocate the mapped buffers */
153 for (i = 0; i < hw->aq.num_arq_entries; i++) {
154 bi = &hw->aq.arq.r.arq_bi[i];
155 ret_code = i40e_allocate_dma_mem(hw, bi,
158 I40E_ADMINQ_DESC_ALIGNMENT);
160 goto unwind_alloc_arq_bufs;
162 /* now configure the descriptors for use */
163 desc = I40E_ADMINQ_DESC(hw->aq.arq, i);
165 desc->flags = cpu_to_le16(I40E_AQ_FLAG_BUF);
166 if (hw->aq.arq_buf_size > I40E_AQ_LARGE_BUF)
167 desc->flags |= cpu_to_le16(I40E_AQ_FLAG_LB);
169 /* This is in accordance with Admin queue design, there is no
170 * register for buffer size configuration
172 desc->datalen = cpu_to_le16((u16)bi->size);
174 desc->cookie_high = 0;
175 desc->cookie_low = 0;
176 desc->params.external.addr_high =
177 cpu_to_le32(upper_32_bits(bi->pa));
178 desc->params.external.addr_low =
179 cpu_to_le32(lower_32_bits(bi->pa));
180 desc->params.external.param0 = 0;
181 desc->params.external.param1 = 0;
187 unwind_alloc_arq_bufs:
188 /* don't try to free the one that failed... */
191 i40e_free_dma_mem(hw, &hw->aq.arq.r.arq_bi[i]);
192 i40e_free_virt_mem(hw, &hw->aq.arq.dma_head);
198 * i40e_alloc_asq_bufs - Allocate empty buffer structs for the send queue
199 * @hw: pointer to the hardware structure
201 static i40e_status i40e_alloc_asq_bufs(struct i40e_hw *hw)
203 i40e_status ret_code;
204 struct i40e_dma_mem *bi;
207 /* No mapped memory needed yet, just the buffer info structures */
208 ret_code = i40e_allocate_virt_mem(hw, &hw->aq.asq.dma_head,
209 (hw->aq.num_asq_entries * sizeof(struct i40e_dma_mem)));
212 hw->aq.asq.r.asq_bi = (struct i40e_dma_mem *)hw->aq.asq.dma_head.va;
214 /* allocate the mapped buffers */
215 for (i = 0; i < hw->aq.num_asq_entries; i++) {
216 bi = &hw->aq.asq.r.asq_bi[i];
217 ret_code = i40e_allocate_dma_mem(hw, bi,
220 I40E_ADMINQ_DESC_ALIGNMENT);
222 goto unwind_alloc_asq_bufs;
227 unwind_alloc_asq_bufs:
228 /* don't try to free the one that failed... */
231 i40e_free_dma_mem(hw, &hw->aq.asq.r.asq_bi[i]);
232 i40e_free_virt_mem(hw, &hw->aq.asq.dma_head);
238 * i40e_free_arq_bufs - Free receive queue buffer info elements
239 * @hw: pointer to the hardware structure
241 static void i40e_free_arq_bufs(struct i40e_hw *hw)
245 /* free descriptors */
246 for (i = 0; i < hw->aq.num_arq_entries; i++)
247 i40e_free_dma_mem(hw, &hw->aq.arq.r.arq_bi[i]);
249 /* free the descriptor memory */
250 i40e_free_dma_mem(hw, &hw->aq.arq.desc_buf);
252 /* free the dma header */
253 i40e_free_virt_mem(hw, &hw->aq.arq.dma_head);
257 * i40e_free_asq_bufs - Free send queue buffer info elements
258 * @hw: pointer to the hardware structure
260 static void i40e_free_asq_bufs(struct i40e_hw *hw)
264 /* only unmap if the address is non-NULL */
265 for (i = 0; i < hw->aq.num_asq_entries; i++)
266 if (hw->aq.asq.r.asq_bi[i].pa)
267 i40e_free_dma_mem(hw, &hw->aq.asq.r.asq_bi[i]);
269 /* free the buffer info list */
270 i40e_free_virt_mem(hw, &hw->aq.asq.cmd_buf);
272 /* free the descriptor memory */
273 i40e_free_dma_mem(hw, &hw->aq.asq.desc_buf);
275 /* free the dma header */
276 i40e_free_virt_mem(hw, &hw->aq.asq.dma_head);
280 * i40e_config_asq_regs - configure ASQ registers
281 * @hw: pointer to the hardware structure
283 * Configure base address and length registers for the transmit queue
285 static void i40e_config_asq_regs(struct i40e_hw *hw)
287 if (hw->mac.type == I40E_MAC_VF) {
288 /* configure the transmit queue */
289 wr32(hw, I40E_VF_ATQBAH1,
290 upper_32_bits(hw->aq.asq.desc_buf.pa));
291 wr32(hw, I40E_VF_ATQBAL1,
292 lower_32_bits(hw->aq.asq.desc_buf.pa));
293 wr32(hw, I40E_VF_ATQLEN1, (hw->aq.num_asq_entries |
294 I40E_VF_ATQLEN1_ATQENABLE_MASK));
296 /* configure the transmit queue */
297 wr32(hw, I40E_PF_ATQBAH,
298 upper_32_bits(hw->aq.asq.desc_buf.pa));
299 wr32(hw, I40E_PF_ATQBAL,
300 lower_32_bits(hw->aq.asq.desc_buf.pa));
301 wr32(hw, I40E_PF_ATQLEN, (hw->aq.num_asq_entries |
302 I40E_PF_ATQLEN_ATQENABLE_MASK));
307 * i40e_config_arq_regs - ARQ register configuration
308 * @hw: pointer to the hardware structure
310 * Configure base address and length registers for the receive (event queue)
312 static void i40e_config_arq_regs(struct i40e_hw *hw)
314 if (hw->mac.type == I40E_MAC_VF) {
315 /* configure the receive queue */
316 wr32(hw, I40E_VF_ARQBAH1,
317 upper_32_bits(hw->aq.arq.desc_buf.pa));
318 wr32(hw, I40E_VF_ARQBAL1,
319 lower_32_bits(hw->aq.arq.desc_buf.pa));
320 wr32(hw, I40E_VF_ARQLEN1, (hw->aq.num_arq_entries |
321 I40E_VF_ARQLEN1_ARQENABLE_MASK));
323 /* configure the receive queue */
324 wr32(hw, I40E_PF_ARQBAH,
325 upper_32_bits(hw->aq.arq.desc_buf.pa));
326 wr32(hw, I40E_PF_ARQBAL,
327 lower_32_bits(hw->aq.arq.desc_buf.pa));
328 wr32(hw, I40E_PF_ARQLEN, (hw->aq.num_arq_entries |
329 I40E_PF_ARQLEN_ARQENABLE_MASK));
332 /* Update tail in the HW to post pre-allocated buffers */
333 wr32(hw, hw->aq.arq.tail, hw->aq.num_arq_entries - 1);
337 * i40e_init_asq - main initialization routine for ASQ
338 * @hw: pointer to the hardware structure
340 * This is the main initialization routine for the Admin Send Queue
341 * Prior to calling this function, drivers *MUST* set the following fields
342 * in the hw->aq structure:
343 * - hw->aq.num_asq_entries
344 * - hw->aq.arq_buf_size
346 * Do *NOT* hold the lock when calling this as the memory allocation routines
347 * called are not going to be atomic context safe
349 static i40e_status i40e_init_asq(struct i40e_hw *hw)
351 i40e_status ret_code = 0;
353 if (hw->aq.asq.count > 0) {
354 /* queue already initialized */
355 ret_code = I40E_ERR_NOT_READY;
356 goto init_adminq_exit;
359 /* verify input for valid configuration */
360 if ((hw->aq.num_asq_entries == 0) ||
361 (hw->aq.asq_buf_size == 0)) {
362 ret_code = I40E_ERR_CONFIG;
363 goto init_adminq_exit;
366 hw->aq.asq.next_to_use = 0;
367 hw->aq.asq.next_to_clean = 0;
368 hw->aq.asq.count = hw->aq.num_asq_entries;
370 /* allocate the ring memory */
371 ret_code = i40e_alloc_adminq_asq_ring(hw);
373 goto init_adminq_exit;
375 /* allocate buffers in the rings */
376 ret_code = i40e_alloc_asq_bufs(hw);
378 goto init_adminq_free_rings;
380 /* initialize base registers */
381 i40e_config_asq_regs(hw);
384 goto init_adminq_exit;
386 init_adminq_free_rings:
387 i40e_free_adminq_asq(hw);
394 * i40e_init_arq - initialize ARQ
395 * @hw: pointer to the hardware structure
397 * The main initialization routine for the Admin Receive (Event) Queue.
398 * Prior to calling this function, drivers *MUST* set the following fields
399 * in the hw->aq structure:
400 * - hw->aq.num_asq_entries
401 * - hw->aq.arq_buf_size
403 * Do *NOT* hold the lock when calling this as the memory allocation routines
404 * called are not going to be atomic context safe
406 static i40e_status i40e_init_arq(struct i40e_hw *hw)
408 i40e_status ret_code = 0;
410 if (hw->aq.arq.count > 0) {
411 /* queue already initialized */
412 ret_code = I40E_ERR_NOT_READY;
413 goto init_adminq_exit;
416 /* verify input for valid configuration */
417 if ((hw->aq.num_arq_entries == 0) ||
418 (hw->aq.arq_buf_size == 0)) {
419 ret_code = I40E_ERR_CONFIG;
420 goto init_adminq_exit;
423 hw->aq.arq.next_to_use = 0;
424 hw->aq.arq.next_to_clean = 0;
425 hw->aq.arq.count = hw->aq.num_arq_entries;
427 /* allocate the ring memory */
428 ret_code = i40e_alloc_adminq_arq_ring(hw);
430 goto init_adminq_exit;
432 /* allocate buffers in the rings */
433 ret_code = i40e_alloc_arq_bufs(hw);
435 goto init_adminq_free_rings;
437 /* initialize base registers */
438 i40e_config_arq_regs(hw);
441 goto init_adminq_exit;
443 init_adminq_free_rings:
444 i40e_free_adminq_arq(hw);
451 * i40e_shutdown_asq - shutdown the ASQ
452 * @hw: pointer to the hardware structure
454 * The main shutdown routine for the Admin Send Queue
456 static i40e_status i40e_shutdown_asq(struct i40e_hw *hw)
458 i40e_status ret_code = 0;
460 if (hw->aq.asq.count == 0)
461 return I40E_ERR_NOT_READY;
463 /* Stop firmware AdminQ processing */
464 wr32(hw, hw->aq.asq.head, 0);
465 wr32(hw, hw->aq.asq.tail, 0);
466 wr32(hw, hw->aq.asq.len, 0);
468 /* make sure lock is available */
469 mutex_lock(&hw->aq.asq_mutex);
471 hw->aq.asq.count = 0; /* to indicate uninitialized queue */
473 /* free ring buffers */
474 i40e_free_asq_bufs(hw);
476 mutex_unlock(&hw->aq.asq_mutex);
482 * i40e_shutdown_arq - shutdown ARQ
483 * @hw: pointer to the hardware structure
485 * The main shutdown routine for the Admin Receive Queue
487 static i40e_status i40e_shutdown_arq(struct i40e_hw *hw)
489 i40e_status ret_code = 0;
491 if (hw->aq.arq.count == 0)
492 return I40E_ERR_NOT_READY;
494 /* Stop firmware AdminQ processing */
495 wr32(hw, hw->aq.arq.head, 0);
496 wr32(hw, hw->aq.arq.tail, 0);
497 wr32(hw, hw->aq.arq.len, 0);
499 /* make sure lock is available */
500 mutex_lock(&hw->aq.arq_mutex);
502 hw->aq.arq.count = 0; /* to indicate uninitialized queue */
504 /* free ring buffers */
505 i40e_free_arq_bufs(hw);
507 mutex_unlock(&hw->aq.arq_mutex);
513 * i40e_init_adminq - main initialization routine for Admin Queue
514 * @hw: pointer to the hardware structure
516 * Prior to calling this function, drivers *MUST* set the following fields
517 * in the hw->aq structure:
518 * - hw->aq.num_asq_entries
519 * - hw->aq.num_arq_entries
520 * - hw->aq.arq_buf_size
521 * - hw->aq.asq_buf_size
523 i40e_status i40e_init_adminq(struct i40e_hw *hw)
525 i40e_status ret_code;
526 u16 eetrack_lo, eetrack_hi;
529 /* verify input for valid configuration */
530 if ((hw->aq.num_arq_entries == 0) ||
531 (hw->aq.num_asq_entries == 0) ||
532 (hw->aq.arq_buf_size == 0) ||
533 (hw->aq.asq_buf_size == 0)) {
534 ret_code = I40E_ERR_CONFIG;
535 goto init_adminq_exit;
538 /* initialize locks */
539 mutex_init(&hw->aq.asq_mutex);
540 mutex_init(&hw->aq.arq_mutex);
542 /* Set up register offsets */
543 i40e_adminq_init_regs(hw);
545 /* allocate the ASQ */
546 ret_code = i40e_init_asq(hw);
548 goto init_adminq_destroy_locks;
550 /* allocate the ARQ */
551 ret_code = i40e_init_arq(hw);
553 goto init_adminq_free_asq;
555 /* There are some cases where the firmware may not be quite ready
556 * for AdminQ operations, so we retry the AdminQ setup a few times
557 * if we see timeouts in this first AQ call.
560 ret_code = i40e_aq_get_firmware_version(hw,
566 if (ret_code != I40E_ERR_ADMIN_QUEUE_TIMEOUT)
571 } while (retry < 10);
572 if (ret_code != I40E_SUCCESS)
573 goto init_adminq_free_arq;
575 if (hw->aq.api_maj_ver != I40E_FW_API_VERSION_MAJOR ||
576 hw->aq.api_min_ver != I40E_FW_API_VERSION_MINOR) {
577 ret_code = I40E_ERR_FIRMWARE_API_VERSION;
578 goto init_adminq_free_arq;
580 i40e_read_nvm_word(hw, I40E_SR_NVM_IMAGE_VERSION, &hw->nvm.version);
581 i40e_read_nvm_word(hw, I40E_SR_NVM_EETRACK_LO, &eetrack_lo);
582 i40e_read_nvm_word(hw, I40E_SR_NVM_EETRACK_HI, &eetrack_hi);
583 hw->nvm.eetrack = (eetrack_hi << 16) | eetrack_lo;
585 ret_code = i40e_aq_set_hmc_resource_profile(hw,
586 I40E_HMC_PROFILE_DEFAULT,
592 goto init_adminq_exit;
594 init_adminq_free_arq:
595 i40e_shutdown_arq(hw);
596 init_adminq_free_asq:
597 i40e_shutdown_asq(hw);
598 init_adminq_destroy_locks:
605 * i40e_shutdown_adminq - shutdown routine for the Admin Queue
606 * @hw: pointer to the hardware structure
608 i40e_status i40e_shutdown_adminq(struct i40e_hw *hw)
610 i40e_status ret_code = 0;
612 i40e_shutdown_asq(hw);
613 i40e_shutdown_arq(hw);
615 /* destroy the locks */
621 * i40e_clean_asq - cleans Admin send queue
622 * @asq: pointer to the adminq send ring
624 * returns the number of free desc
626 static u16 i40e_clean_asq(struct i40e_hw *hw)
628 struct i40e_adminq_ring *asq = &(hw->aq.asq);
629 struct i40e_asq_cmd_details *details;
630 u16 ntc = asq->next_to_clean;
631 struct i40e_aq_desc desc_cb;
632 struct i40e_aq_desc *desc;
634 desc = I40E_ADMINQ_DESC(*asq, ntc);
635 details = I40E_ADMINQ_DETAILS(*asq, ntc);
636 while (rd32(hw, hw->aq.asq.head) != ntc) {
637 if (details->callback) {
638 I40E_ADMINQ_CALLBACK cb_func =
639 (I40E_ADMINQ_CALLBACK)details->callback;
641 cb_func(hw, &desc_cb);
643 memset((void *)desc, 0, sizeof(struct i40e_aq_desc));
644 memset((void *)details, 0,
645 sizeof(struct i40e_asq_cmd_details));
647 if (ntc == asq->count)
649 desc = I40E_ADMINQ_DESC(*asq, ntc);
650 details = I40E_ADMINQ_DETAILS(*asq, ntc);
653 asq->next_to_clean = ntc;
655 return I40E_DESC_UNUSED(asq);
659 * i40e_asq_done - check if FW has processed the Admin Send Queue
660 * @hw: pointer to the hw struct
662 * Returns true if the firmware has processed all descriptors on the
663 * admin send queue. Returns false if there are still requests pending.
665 static bool i40e_asq_done(struct i40e_hw *hw)
667 /* AQ designers suggest use of head for better
668 * timing reliability than DD bit
670 return (rd32(hw, hw->aq.asq.head) == hw->aq.asq.next_to_use);
675 * i40e_asq_send_command - send command to Admin Queue
676 * @hw: pointer to the hw struct
677 * @desc: prefilled descriptor describing the command (non DMA mem)
678 * @buff: buffer to use for indirect commands
679 * @buff_size: size of buffer for indirect commands
680 * @opaque: pointer to info to be used in async cleanup
682 * This is the main send command driver routine for the Admin Queue send
683 * queue. It runs the queue, cleans the queue, etc
685 i40e_status i40e_asq_send_command(struct i40e_hw *hw,
686 struct i40e_aq_desc *desc,
687 void *buff, /* can be NULL */
689 struct i40e_asq_cmd_details *cmd_details)
691 i40e_status status = 0;
692 struct i40e_dma_mem *dma_buff = NULL;
693 struct i40e_asq_cmd_details *details;
694 struct i40e_aq_desc *desc_on_ring;
695 bool cmd_completed = false;
698 if (hw->aq.asq.count == 0) {
699 i40e_debug(hw, I40E_DEBUG_AQ_MESSAGE,
700 "AQTX: Admin queue not initialized.\n");
701 status = I40E_ERR_QUEUE_EMPTY;
702 goto asq_send_command_exit;
705 details = I40E_ADMINQ_DETAILS(hw->aq.asq, hw->aq.asq.next_to_use);
707 *details = *cmd_details;
709 /* If the cmd_details are defined copy the cookie. The
710 * cpu_to_le32 is not needed here because the data is ignored
711 * by the FW, only used by the driver
713 if (details->cookie) {
715 cpu_to_le32(upper_32_bits(details->cookie));
717 cpu_to_le32(lower_32_bits(details->cookie));
720 memset(details, 0, sizeof(struct i40e_asq_cmd_details));
723 /* clear requested flags and then set additional flags if defined */
724 desc->flags &= ~cpu_to_le16(details->flags_dis);
725 desc->flags |= cpu_to_le16(details->flags_ena);
727 mutex_lock(&hw->aq.asq_mutex);
729 if (buff_size > hw->aq.asq_buf_size) {
731 I40E_DEBUG_AQ_MESSAGE,
732 "AQTX: Invalid buffer size: %d.\n",
734 status = I40E_ERR_INVALID_SIZE;
735 goto asq_send_command_error;
738 if (details->postpone && !details->async) {
740 I40E_DEBUG_AQ_MESSAGE,
741 "AQTX: Async flag not set along with postpone flag");
742 status = I40E_ERR_PARAM;
743 goto asq_send_command_error;
746 /* call clean and check queue available function to reclaim the
747 * descriptors that were processed by FW, the function returns the
748 * number of desc available
750 /* the clean function called here could be called in a separate thread
751 * in case of asynchronous completions
753 if (i40e_clean_asq(hw) == 0) {
755 I40E_DEBUG_AQ_MESSAGE,
756 "AQTX: Error queue is full.\n");
757 status = I40E_ERR_ADMIN_QUEUE_FULL;
758 goto asq_send_command_error;
761 /* initialize the temp desc pointer with the right desc */
762 desc_on_ring = I40E_ADMINQ_DESC(hw->aq.asq, hw->aq.asq.next_to_use);
764 /* if the desc is available copy the temp desc to the right place */
765 *desc_on_ring = *desc;
767 /* if buff is not NULL assume indirect command */
769 dma_buff = &(hw->aq.asq.r.asq_bi[hw->aq.asq.next_to_use]);
770 /* copy the user buff into the respective DMA buff */
771 memcpy(dma_buff->va, buff, buff_size);
772 desc_on_ring->datalen = cpu_to_le16(buff_size);
774 /* Update the address values in the desc with the pa value
775 * for respective buffer
777 desc_on_ring->params.external.addr_high =
778 cpu_to_le32(upper_32_bits(dma_buff->pa));
779 desc_on_ring->params.external.addr_low =
780 cpu_to_le32(lower_32_bits(dma_buff->pa));
784 i40e_debug_aq(hw, I40E_DEBUG_AQ_COMMAND, (void *)desc_on_ring, buff);
785 (hw->aq.asq.next_to_use)++;
786 if (hw->aq.asq.next_to_use == hw->aq.asq.count)
787 hw->aq.asq.next_to_use = 0;
788 if (!details->postpone)
789 wr32(hw, hw->aq.asq.tail, hw->aq.asq.next_to_use);
791 /* if cmd_details are not defined or async flag is not set,
792 * we need to wait for desc write back
794 if (!details->async && !details->postpone) {
799 /* AQ designers suggest use of head for better
800 * timing reliability than DD bit
802 if (i40e_asq_done(hw))
804 /* ugh! delay while spin_lock */
806 total_delay += delay_len;
807 } while (total_delay < I40E_ASQ_CMD_TIMEOUT);
810 /* if ready, copy the desc back to temp */
811 if (i40e_asq_done(hw)) {
812 *desc = *desc_on_ring;
814 memcpy(buff, dma_buff->va, buff_size);
815 retval = le16_to_cpu(desc->retval);
818 I40E_DEBUG_AQ_MESSAGE,
819 "AQTX: Command completed with error 0x%X.\n",
821 /* strip off FW internal code */
824 cmd_completed = true;
825 if ((enum i40e_admin_queue_err)retval == I40E_AQ_RC_OK)
828 status = I40E_ERR_ADMIN_QUEUE_ERROR;
829 hw->aq.asq_last_status = (enum i40e_admin_queue_err)retval;
832 /* update the error if time out occurred */
833 if ((!cmd_completed) &&
834 (!details->async && !details->postpone)) {
836 I40E_DEBUG_AQ_MESSAGE,
837 "AQTX: Writeback timeout.\n");
838 status = I40E_ERR_ADMIN_QUEUE_TIMEOUT;
841 asq_send_command_error:
842 mutex_unlock(&hw->aq.asq_mutex);
843 asq_send_command_exit:
848 * i40e_fill_default_direct_cmd_desc - AQ descriptor helper function
849 * @desc: pointer to the temp descriptor (non DMA mem)
850 * @opcode: the opcode can be used to decide which flags to turn off or on
852 * Fill the desc with default values
854 void i40e_fill_default_direct_cmd_desc(struct i40e_aq_desc *desc,
857 /* zero out the desc */
858 memset((void *)desc, 0, sizeof(struct i40e_aq_desc));
859 desc->opcode = cpu_to_le16(opcode);
860 desc->flags = cpu_to_le16(I40E_AQ_FLAG_EI | I40E_AQ_FLAG_SI);
864 * i40e_clean_arq_element
865 * @hw: pointer to the hw struct
866 * @e: event info from the receive descriptor, includes any buffers
867 * @pending: number of events that could be left to process
869 * This function cleans one Admin Receive Queue element and returns
870 * the contents through e. It can also return how many events are
871 * left to process through 'pending'
873 i40e_status i40e_clean_arq_element(struct i40e_hw *hw,
874 struct i40e_arq_event_info *e,
877 i40e_status ret_code = 0;
878 u16 ntc = hw->aq.arq.next_to_clean;
879 struct i40e_aq_desc *desc;
880 struct i40e_dma_mem *bi;
886 /* take the lock before we start messing with the ring */
887 mutex_lock(&hw->aq.arq_mutex);
889 /* set next_to_use to head */
890 ntu = (rd32(hw, hw->aq.arq.head) & I40E_PF_ARQH_ARQH_MASK);
892 /* nothing to do - shouldn't need to update ring's values */
894 I40E_DEBUG_AQ_MESSAGE,
895 "AQRX: Queue is empty.\n");
896 ret_code = I40E_ERR_ADMIN_QUEUE_NO_WORK;
897 goto clean_arq_element_out;
900 /* now clean the next descriptor */
901 desc = I40E_ADMINQ_DESC(hw->aq.arq, ntc);
904 I40E_DEBUG_AQ_COMMAND,
906 hw->aq.arq.r.arq_bi[desc_idx].va);
908 flags = le16_to_cpu(desc->flags);
909 if (flags & I40E_AQ_FLAG_ERR) {
910 ret_code = I40E_ERR_ADMIN_QUEUE_ERROR;
911 hw->aq.arq_last_status =
912 (enum i40e_admin_queue_err)le16_to_cpu(desc->retval);
914 I40E_DEBUG_AQ_MESSAGE,
915 "AQRX: Event received with error 0x%X.\n",
916 hw->aq.arq_last_status);
918 memcpy(&e->desc, desc, sizeof(struct i40e_aq_desc));
919 datalen = le16_to_cpu(desc->datalen);
920 e->msg_size = min(datalen, e->msg_size);
921 if (e->msg_buf != NULL && (e->msg_size != 0))
922 memcpy(e->msg_buf, hw->aq.arq.r.arq_bi[desc_idx].va,
926 /* Restore the original datalen and buffer address in the desc,
927 * FW updates datalen to indicate the event message
930 bi = &hw->aq.arq.r.arq_bi[ntc];
931 desc->datalen = cpu_to_le16((u16)bi->size);
932 desc->params.external.addr_high = cpu_to_le32(upper_32_bits(bi->pa));
933 desc->params.external.addr_low = cpu_to_le32(lower_32_bits(bi->pa));
935 /* set tail = the last cleaned desc index. */
936 wr32(hw, hw->aq.arq.tail, ntc);
937 /* ntc is updated to tail + 1 */
939 if (ntc == hw->aq.num_arq_entries)
941 hw->aq.arq.next_to_clean = ntc;
942 hw->aq.arq.next_to_use = ntu;
944 clean_arq_element_out:
945 /* Set pending if needed, unlock and return */
947 *pending = (ntc > ntu ? hw->aq.arq.count : 0) + (ntu - ntc);
948 mutex_unlock(&hw->aq.arq_mutex);
953 static void i40e_resume_aq(struct i40e_hw *hw)
957 /* Registers are reset after PF reset */
958 hw->aq.asq.next_to_use = 0;
959 hw->aq.asq.next_to_clean = 0;
961 i40e_config_asq_regs(hw);
962 reg = hw->aq.num_asq_entries | I40E_PF_ATQLEN_ATQENABLE_MASK;
963 wr32(hw, hw->aq.asq.len, reg);
965 hw->aq.arq.next_to_use = 0;
966 hw->aq.arq.next_to_clean = 0;
968 i40e_config_arq_regs(hw);
969 reg = hw->aq.num_arq_entries | I40E_PF_ATQLEN_ATQENABLE_MASK;
970 wr32(hw, hw->aq.arq.len, reg);