blob: 79a30dd8c26fa7aaee5b430d5873f99246149240 [file] [log] [blame]
Greg Rosed358aa92013-12-21 06:13:11 +00001/*******************************************************************************
2 *
3 * Intel Ethernet Controller XL710 Family Linux Virtual Function Driver
Jesse Brandeburgb8316072014-04-05 07:46:11 +00004 * Copyright(c) 2013 - 2014 Intel Corporation.
Greg Rosed358aa92013-12-21 06:13:11 +00005 *
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.
9 *
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
13 * more details.
14 *
Jesse Brandeburgb8316072014-04-05 07:46:11 +000015 * You should have received a copy of the GNU General Public License along
16 * with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
Greg Rosed358aa92013-12-21 06:13:11 +000018 * The full GNU General Public License is included in this distribution in
19 * the file called "COPYING".
20 *
21 * Contact Information:
22 * e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
23 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
24 *
25 ******************************************************************************/
26
27#include "i40e_status.h"
28#include "i40e_type.h"
29#include "i40e_register.h"
30#include "i40e_adminq.h"
31#include "i40e_prototype.h"
32
33/**
Shannon Nelsonc9296ad2014-03-14 07:32:22 +000034 * i40e_is_nvm_update_op - return true if this is an NVM update operation
35 * @desc: API request descriptor
36 **/
37static inline bool i40e_is_nvm_update_op(struct i40e_aq_desc *desc)
38{
39 return (desc->opcode == i40e_aqc_opc_nvm_erase) ||
40 (desc->opcode == i40e_aqc_opc_nvm_update);
41}
42
43/**
Greg Rosed358aa92013-12-21 06:13:11 +000044 * i40e_adminq_init_regs - Initialize AdminQ registers
45 * @hw: pointer to the hardware structure
46 *
47 * This assumes the alloc_asq and alloc_arq functions have already been called
48 **/
49static void i40e_adminq_init_regs(struct i40e_hw *hw)
50{
51 /* set head and tail registers in our local struct */
52 if (hw->mac.type == I40E_MAC_VF) {
53 hw->aq.asq.tail = I40E_VF_ATQT1;
54 hw->aq.asq.head = I40E_VF_ATQH1;
55 hw->aq.asq.len = I40E_VF_ATQLEN1;
Shannon Nelson87dc3462014-06-04 20:41:17 +000056 hw->aq.asq.bal = I40E_VF_ATQBAL1;
57 hw->aq.asq.bah = I40E_VF_ATQBAH1;
Greg Rosed358aa92013-12-21 06:13:11 +000058 hw->aq.arq.tail = I40E_VF_ARQT1;
59 hw->aq.arq.head = I40E_VF_ARQH1;
60 hw->aq.arq.len = I40E_VF_ARQLEN1;
Shannon Nelson87dc3462014-06-04 20:41:17 +000061 hw->aq.arq.bal = I40E_VF_ARQBAL1;
62 hw->aq.arq.bah = I40E_VF_ARQBAH1;
Greg Rosed358aa92013-12-21 06:13:11 +000063 } else {
64 hw->aq.asq.tail = I40E_PF_ATQT;
65 hw->aq.asq.head = I40E_PF_ATQH;
66 hw->aq.asq.len = I40E_PF_ATQLEN;
Shannon Nelson87dc3462014-06-04 20:41:17 +000067 hw->aq.asq.bal = I40E_PF_ATQBAL;
68 hw->aq.asq.bah = I40E_PF_ATQBAH;
Greg Rosed358aa92013-12-21 06:13:11 +000069 hw->aq.arq.tail = I40E_PF_ARQT;
70 hw->aq.arq.head = I40E_PF_ARQH;
71 hw->aq.arq.len = I40E_PF_ARQLEN;
Shannon Nelson87dc3462014-06-04 20:41:17 +000072 hw->aq.arq.bal = I40E_PF_ARQBAL;
73 hw->aq.arq.bah = I40E_PF_ARQBAH;
Greg Rosed358aa92013-12-21 06:13:11 +000074 }
75}
76
77/**
78 * i40e_alloc_adminq_asq_ring - Allocate Admin Queue send rings
79 * @hw: pointer to the hardware structure
80 **/
81static i40e_status i40e_alloc_adminq_asq_ring(struct i40e_hw *hw)
82{
83 i40e_status ret_code;
84
85 ret_code = i40e_allocate_dma_mem(hw, &hw->aq.asq.desc_buf,
86 i40e_mem_atq_ring,
87 (hw->aq.num_asq_entries *
88 sizeof(struct i40e_aq_desc)),
89 I40E_ADMINQ_DESC_ALIGNMENT);
90 if (ret_code)
91 return ret_code;
92
93 ret_code = i40e_allocate_virt_mem(hw, &hw->aq.asq.cmd_buf,
94 (hw->aq.num_asq_entries *
95 sizeof(struct i40e_asq_cmd_details)));
96 if (ret_code) {
97 i40e_free_dma_mem(hw, &hw->aq.asq.desc_buf);
98 return ret_code;
99 }
100
101 return ret_code;
102}
103
104/**
105 * i40e_alloc_adminq_arq_ring - Allocate Admin Queue receive rings
106 * @hw: pointer to the hardware structure
107 **/
108static i40e_status i40e_alloc_adminq_arq_ring(struct i40e_hw *hw)
109{
110 i40e_status ret_code;
111
112 ret_code = i40e_allocate_dma_mem(hw, &hw->aq.arq.desc_buf,
113 i40e_mem_arq_ring,
114 (hw->aq.num_arq_entries *
115 sizeof(struct i40e_aq_desc)),
116 I40E_ADMINQ_DESC_ALIGNMENT);
117
118 return ret_code;
119}
120
121/**
122 * i40e_free_adminq_asq - Free Admin Queue send rings
123 * @hw: pointer to the hardware structure
124 *
125 * This assumes the posted send buffers have already been cleaned
126 * and de-allocated
127 **/
128static void i40e_free_adminq_asq(struct i40e_hw *hw)
129{
130 i40e_free_dma_mem(hw, &hw->aq.asq.desc_buf);
131}
132
133/**
134 * i40e_free_adminq_arq - Free Admin Queue receive rings
135 * @hw: pointer to the hardware structure
136 *
137 * This assumes the posted receive buffers have already been cleaned
138 * and de-allocated
139 **/
140static void i40e_free_adminq_arq(struct i40e_hw *hw)
141{
142 i40e_free_dma_mem(hw, &hw->aq.arq.desc_buf);
143}
144
145/**
146 * i40e_alloc_arq_bufs - Allocate pre-posted buffers for the receive queue
147 * @hw: pointer to the hardware structure
148 **/
149static i40e_status i40e_alloc_arq_bufs(struct i40e_hw *hw)
150{
151 i40e_status ret_code;
152 struct i40e_aq_desc *desc;
153 struct i40e_dma_mem *bi;
154 int i;
155
156 /* We'll be allocating the buffer info memory first, then we can
157 * allocate the mapped buffers for the event processing
158 */
159
160 /* buffer_info structures do not need alignment */
161 ret_code = i40e_allocate_virt_mem(hw, &hw->aq.arq.dma_head,
162 (hw->aq.num_arq_entries * sizeof(struct i40e_dma_mem)));
163 if (ret_code)
164 goto alloc_arq_bufs;
165 hw->aq.arq.r.arq_bi = (struct i40e_dma_mem *)hw->aq.arq.dma_head.va;
166
167 /* allocate the mapped buffers */
168 for (i = 0; i < hw->aq.num_arq_entries; i++) {
169 bi = &hw->aq.arq.r.arq_bi[i];
170 ret_code = i40e_allocate_dma_mem(hw, bi,
171 i40e_mem_arq_buf,
172 hw->aq.arq_buf_size,
173 I40E_ADMINQ_DESC_ALIGNMENT);
174 if (ret_code)
175 goto unwind_alloc_arq_bufs;
176
177 /* now configure the descriptors for use */
178 desc = I40E_ADMINQ_DESC(hw->aq.arq, i);
179
180 desc->flags = cpu_to_le16(I40E_AQ_FLAG_BUF);
181 if (hw->aq.arq_buf_size > I40E_AQ_LARGE_BUF)
182 desc->flags |= cpu_to_le16(I40E_AQ_FLAG_LB);
183 desc->opcode = 0;
184 /* This is in accordance with Admin queue design, there is no
185 * register for buffer size configuration
186 */
187 desc->datalen = cpu_to_le16((u16)bi->size);
188 desc->retval = 0;
189 desc->cookie_high = 0;
190 desc->cookie_low = 0;
191 desc->params.external.addr_high =
192 cpu_to_le32(upper_32_bits(bi->pa));
193 desc->params.external.addr_low =
194 cpu_to_le32(lower_32_bits(bi->pa));
195 desc->params.external.param0 = 0;
196 desc->params.external.param1 = 0;
197 }
198
199alloc_arq_bufs:
200 return ret_code;
201
202unwind_alloc_arq_bufs:
203 /* don't try to free the one that failed... */
204 i--;
205 for (; i >= 0; i--)
206 i40e_free_dma_mem(hw, &hw->aq.arq.r.arq_bi[i]);
207 i40e_free_virt_mem(hw, &hw->aq.arq.dma_head);
208
209 return ret_code;
210}
211
212/**
213 * i40e_alloc_asq_bufs - Allocate empty buffer structs for the send queue
214 * @hw: pointer to the hardware structure
215 **/
216static i40e_status i40e_alloc_asq_bufs(struct i40e_hw *hw)
217{
218 i40e_status ret_code;
219 struct i40e_dma_mem *bi;
220 int i;
221
222 /* No mapped memory needed yet, just the buffer info structures */
223 ret_code = i40e_allocate_virt_mem(hw, &hw->aq.asq.dma_head,
224 (hw->aq.num_asq_entries * sizeof(struct i40e_dma_mem)));
225 if (ret_code)
226 goto alloc_asq_bufs;
227 hw->aq.asq.r.asq_bi = (struct i40e_dma_mem *)hw->aq.asq.dma_head.va;
228
229 /* allocate the mapped buffers */
230 for (i = 0; i < hw->aq.num_asq_entries; i++) {
231 bi = &hw->aq.asq.r.asq_bi[i];
232 ret_code = i40e_allocate_dma_mem(hw, bi,
233 i40e_mem_asq_buf,
234 hw->aq.asq_buf_size,
235 I40E_ADMINQ_DESC_ALIGNMENT);
236 if (ret_code)
237 goto unwind_alloc_asq_bufs;
238 }
239alloc_asq_bufs:
240 return ret_code;
241
242unwind_alloc_asq_bufs:
243 /* don't try to free the one that failed... */
244 i--;
245 for (; i >= 0; i--)
246 i40e_free_dma_mem(hw, &hw->aq.asq.r.asq_bi[i]);
247 i40e_free_virt_mem(hw, &hw->aq.asq.dma_head);
248
249 return ret_code;
250}
251
252/**
253 * i40e_free_arq_bufs - Free receive queue buffer info elements
254 * @hw: pointer to the hardware structure
255 **/
256static void i40e_free_arq_bufs(struct i40e_hw *hw)
257{
258 int i;
259
260 /* free descriptors */
261 for (i = 0; i < hw->aq.num_arq_entries; i++)
262 i40e_free_dma_mem(hw, &hw->aq.arq.r.arq_bi[i]);
263
264 /* free the descriptor memory */
265 i40e_free_dma_mem(hw, &hw->aq.arq.desc_buf);
266
267 /* free the dma header */
268 i40e_free_virt_mem(hw, &hw->aq.arq.dma_head);
269}
270
271/**
272 * i40e_free_asq_bufs - Free send queue buffer info elements
273 * @hw: pointer to the hardware structure
274 **/
275static void i40e_free_asq_bufs(struct i40e_hw *hw)
276{
277 int i;
278
279 /* only unmap if the address is non-NULL */
280 for (i = 0; i < hw->aq.num_asq_entries; i++)
281 if (hw->aq.asq.r.asq_bi[i].pa)
282 i40e_free_dma_mem(hw, &hw->aq.asq.r.asq_bi[i]);
283
284 /* free the buffer info list */
285 i40e_free_virt_mem(hw, &hw->aq.asq.cmd_buf);
286
287 /* free the descriptor memory */
288 i40e_free_dma_mem(hw, &hw->aq.asq.desc_buf);
289
290 /* free the dma header */
291 i40e_free_virt_mem(hw, &hw->aq.asq.dma_head);
292}
293
294/**
295 * i40e_config_asq_regs - configure ASQ registers
296 * @hw: pointer to the hardware structure
297 *
298 * Configure base address and length registers for the transmit queue
299 **/
Kamil Krawczyke03af1e2014-04-23 04:50:02 +0000300static i40e_status i40e_config_asq_regs(struct i40e_hw *hw)
Greg Rosed358aa92013-12-21 06:13:11 +0000301{
Kamil Krawczyke03af1e2014-04-23 04:50:02 +0000302 i40e_status ret_code = 0;
303 u32 reg = 0;
304
Michal Kosiarz80a977e2014-06-03 23:50:13 +0000305 /* Clear Head and Tail */
306 wr32(hw, hw->aq.asq.head, 0);
307 wr32(hw, hw->aq.asq.tail, 0);
308
Shannon Nelson87dc3462014-06-04 20:41:17 +0000309 /* set starting point */
310 wr32(hw, hw->aq.asq.len, (hw->aq.num_asq_entries |
311 I40E_PF_ATQLEN_ATQENABLE_MASK));
312 wr32(hw, hw->aq.asq.bal, lower_32_bits(hw->aq.asq.desc_buf.pa));
313 wr32(hw, hw->aq.asq.bah, upper_32_bits(hw->aq.asq.desc_buf.pa));
Kamil Krawczyke03af1e2014-04-23 04:50:02 +0000314
315 /* Check one register to verify that config was applied */
Shannon Nelson87dc3462014-06-04 20:41:17 +0000316 reg = rd32(hw, hw->aq.asq.bal);
Kamil Krawczyke03af1e2014-04-23 04:50:02 +0000317 if (reg != lower_32_bits(hw->aq.asq.desc_buf.pa))
318 ret_code = I40E_ERR_ADMIN_QUEUE_ERROR;
319
320 return ret_code;
Greg Rosed358aa92013-12-21 06:13:11 +0000321}
322
323/**
324 * i40e_config_arq_regs - ARQ register configuration
325 * @hw: pointer to the hardware structure
326 *
327 * Configure base address and length registers for the receive (event queue)
328 **/
Kamil Krawczyke03af1e2014-04-23 04:50:02 +0000329static i40e_status i40e_config_arq_regs(struct i40e_hw *hw)
Greg Rosed358aa92013-12-21 06:13:11 +0000330{
Kamil Krawczyke03af1e2014-04-23 04:50:02 +0000331 i40e_status ret_code = 0;
332 u32 reg = 0;
333
Michal Kosiarz80a977e2014-06-03 23:50:13 +0000334 /* Clear Head and Tail */
335 wr32(hw, hw->aq.arq.head, 0);
336 wr32(hw, hw->aq.arq.tail, 0);
337
Shannon Nelson87dc3462014-06-04 20:41:17 +0000338 /* set starting point */
339 wr32(hw, hw->aq.arq.len, (hw->aq.num_arq_entries |
340 I40E_PF_ARQLEN_ARQENABLE_MASK));
341 wr32(hw, hw->aq.arq.bal, lower_32_bits(hw->aq.arq.desc_buf.pa));
342 wr32(hw, hw->aq.arq.bah, upper_32_bits(hw->aq.arq.desc_buf.pa));
Greg Rosed358aa92013-12-21 06:13:11 +0000343
344 /* Update tail in the HW to post pre-allocated buffers */
345 wr32(hw, hw->aq.arq.tail, hw->aq.num_arq_entries - 1);
Kamil Krawczyke03af1e2014-04-23 04:50:02 +0000346
347 /* Check one register to verify that config was applied */
Shannon Nelson87dc3462014-06-04 20:41:17 +0000348 reg = rd32(hw, hw->aq.arq.bal);
Kamil Krawczyke03af1e2014-04-23 04:50:02 +0000349 if (reg != lower_32_bits(hw->aq.arq.desc_buf.pa))
350 ret_code = I40E_ERR_ADMIN_QUEUE_ERROR;
351
352 return ret_code;
Greg Rosed358aa92013-12-21 06:13:11 +0000353}
354
355/**
356 * i40e_init_asq - main initialization routine for ASQ
357 * @hw: pointer to the hardware structure
358 *
359 * This is the main initialization routine for the Admin Send Queue
360 * Prior to calling this function, drivers *MUST* set the following fields
361 * in the hw->aq structure:
362 * - hw->aq.num_asq_entries
363 * - hw->aq.arq_buf_size
364 *
365 * Do *NOT* hold the lock when calling this as the memory allocation routines
366 * called are not going to be atomic context safe
367 **/
368static i40e_status i40e_init_asq(struct i40e_hw *hw)
369{
370 i40e_status ret_code = 0;
371
372 if (hw->aq.asq.count > 0) {
373 /* queue already initialized */
374 ret_code = I40E_ERR_NOT_READY;
375 goto init_adminq_exit;
376 }
377
378 /* verify input for valid configuration */
379 if ((hw->aq.num_asq_entries == 0) ||
380 (hw->aq.asq_buf_size == 0)) {
381 ret_code = I40E_ERR_CONFIG;
382 goto init_adminq_exit;
383 }
384
385 hw->aq.asq.next_to_use = 0;
386 hw->aq.asq.next_to_clean = 0;
387 hw->aq.asq.count = hw->aq.num_asq_entries;
388
389 /* allocate the ring memory */
390 ret_code = i40e_alloc_adminq_asq_ring(hw);
391 if (ret_code)
392 goto init_adminq_exit;
393
394 /* allocate buffers in the rings */
395 ret_code = i40e_alloc_asq_bufs(hw);
396 if (ret_code)
397 goto init_adminq_free_rings;
398
399 /* initialize base registers */
Kamil Krawczyke03af1e2014-04-23 04:50:02 +0000400 ret_code = i40e_config_asq_regs(hw);
401 if (ret_code)
402 goto init_adminq_free_rings;
Greg Rosed358aa92013-12-21 06:13:11 +0000403
404 /* success! */
405 goto init_adminq_exit;
406
407init_adminq_free_rings:
408 i40e_free_adminq_asq(hw);
409
410init_adminq_exit:
411 return ret_code;
412}
413
414/**
415 * i40e_init_arq - initialize ARQ
416 * @hw: pointer to the hardware structure
417 *
418 * The main initialization routine for the Admin Receive (Event) Queue.
419 * Prior to calling this function, drivers *MUST* set the following fields
420 * in the hw->aq structure:
421 * - hw->aq.num_asq_entries
422 * - hw->aq.arq_buf_size
423 *
424 * Do *NOT* hold the lock when calling this as the memory allocation routines
425 * called are not going to be atomic context safe
426 **/
427static i40e_status i40e_init_arq(struct i40e_hw *hw)
428{
429 i40e_status ret_code = 0;
430
431 if (hw->aq.arq.count > 0) {
432 /* queue already initialized */
433 ret_code = I40E_ERR_NOT_READY;
434 goto init_adminq_exit;
435 }
436
437 /* verify input for valid configuration */
438 if ((hw->aq.num_arq_entries == 0) ||
439 (hw->aq.arq_buf_size == 0)) {
440 ret_code = I40E_ERR_CONFIG;
441 goto init_adminq_exit;
442 }
443
444 hw->aq.arq.next_to_use = 0;
445 hw->aq.arq.next_to_clean = 0;
446 hw->aq.arq.count = hw->aq.num_arq_entries;
447
448 /* allocate the ring memory */
449 ret_code = i40e_alloc_adminq_arq_ring(hw);
450 if (ret_code)
451 goto init_adminq_exit;
452
453 /* allocate buffers in the rings */
454 ret_code = i40e_alloc_arq_bufs(hw);
455 if (ret_code)
456 goto init_adminq_free_rings;
457
458 /* initialize base registers */
Kamil Krawczyke03af1e2014-04-23 04:50:02 +0000459 ret_code = i40e_config_arq_regs(hw);
460 if (ret_code)
461 goto init_adminq_free_rings;
Greg Rosed358aa92013-12-21 06:13:11 +0000462
463 /* success! */
464 goto init_adminq_exit;
465
466init_adminq_free_rings:
467 i40e_free_adminq_arq(hw);
468
469init_adminq_exit:
470 return ret_code;
471}
472
473/**
474 * i40e_shutdown_asq - shutdown the ASQ
475 * @hw: pointer to the hardware structure
476 *
477 * The main shutdown routine for the Admin Send Queue
478 **/
479static i40e_status i40e_shutdown_asq(struct i40e_hw *hw)
480{
481 i40e_status ret_code = 0;
482
483 if (hw->aq.asq.count == 0)
484 return I40E_ERR_NOT_READY;
485
486 /* Stop firmware AdminQ processing */
487 wr32(hw, hw->aq.asq.head, 0);
488 wr32(hw, hw->aq.asq.tail, 0);
489 wr32(hw, hw->aq.asq.len, 0);
490
491 /* make sure lock is available */
492 mutex_lock(&hw->aq.asq_mutex);
493
494 hw->aq.asq.count = 0; /* to indicate uninitialized queue */
495
496 /* free ring buffers */
497 i40e_free_asq_bufs(hw);
498
499 mutex_unlock(&hw->aq.asq_mutex);
500
501 return ret_code;
502}
503
504/**
505 * i40e_shutdown_arq - shutdown ARQ
506 * @hw: pointer to the hardware structure
507 *
508 * The main shutdown routine for the Admin Receive Queue
509 **/
510static i40e_status i40e_shutdown_arq(struct i40e_hw *hw)
511{
512 i40e_status ret_code = 0;
513
514 if (hw->aq.arq.count == 0)
515 return I40E_ERR_NOT_READY;
516
517 /* Stop firmware AdminQ processing */
518 wr32(hw, hw->aq.arq.head, 0);
519 wr32(hw, hw->aq.arq.tail, 0);
520 wr32(hw, hw->aq.arq.len, 0);
521
522 /* make sure lock is available */
523 mutex_lock(&hw->aq.arq_mutex);
524
525 hw->aq.arq.count = 0; /* to indicate uninitialized queue */
526
527 /* free ring buffers */
528 i40e_free_arq_bufs(hw);
529
530 mutex_unlock(&hw->aq.arq_mutex);
531
532 return ret_code;
533}
534
535/**
536 * i40evf_init_adminq - main initialization routine for Admin Queue
537 * @hw: pointer to the hardware structure
538 *
539 * Prior to calling this function, drivers *MUST* set the following fields
540 * in the hw->aq structure:
541 * - hw->aq.num_asq_entries
542 * - hw->aq.num_arq_entries
543 * - hw->aq.arq_buf_size
544 * - hw->aq.asq_buf_size
545 **/
546i40e_status i40evf_init_adminq(struct i40e_hw *hw)
547{
548 i40e_status ret_code;
549
550 /* verify input for valid configuration */
551 if ((hw->aq.num_arq_entries == 0) ||
552 (hw->aq.num_asq_entries == 0) ||
553 (hw->aq.arq_buf_size == 0) ||
554 (hw->aq.asq_buf_size == 0)) {
555 ret_code = I40E_ERR_CONFIG;
556 goto init_adminq_exit;
557 }
558
559 /* initialize locks */
560 mutex_init(&hw->aq.asq_mutex);
561 mutex_init(&hw->aq.arq_mutex);
562
563 /* Set up register offsets */
564 i40e_adminq_init_regs(hw);
565
566 /* allocate the ASQ */
567 ret_code = i40e_init_asq(hw);
568 if (ret_code)
569 goto init_adminq_destroy_locks;
570
571 /* allocate the ARQ */
572 ret_code = i40e_init_arq(hw);
573 if (ret_code)
574 goto init_adminq_free_asq;
575
576 /* success! */
577 goto init_adminq_exit;
578
579init_adminq_free_asq:
580 i40e_shutdown_asq(hw);
581init_adminq_destroy_locks:
582
583init_adminq_exit:
584 return ret_code;
585}
586
587/**
588 * i40evf_shutdown_adminq - shutdown routine for the Admin Queue
589 * @hw: pointer to the hardware structure
590 **/
591i40e_status i40evf_shutdown_adminq(struct i40e_hw *hw)
592{
593 i40e_status ret_code = 0;
594
595 if (i40evf_check_asq_alive(hw))
596 i40evf_aq_queue_shutdown(hw, true);
597
598 i40e_shutdown_asq(hw);
599 i40e_shutdown_arq(hw);
600
601 /* destroy the locks */
602
603 return ret_code;
604}
605
606/**
607 * i40e_clean_asq - cleans Admin send queue
608 * @hw: pointer to the hardware structure
609 *
610 * returns the number of free desc
611 **/
612static u16 i40e_clean_asq(struct i40e_hw *hw)
613{
614 struct i40e_adminq_ring *asq = &(hw->aq.asq);
615 struct i40e_asq_cmd_details *details;
616 u16 ntc = asq->next_to_clean;
617 struct i40e_aq_desc desc_cb;
618 struct i40e_aq_desc *desc;
619
620 desc = I40E_ADMINQ_DESC(*asq, ntc);
621 details = I40E_ADMINQ_DETAILS(*asq, ntc);
622 while (rd32(hw, hw->aq.asq.head) != ntc) {
Michal Kosiarz80a977e2014-06-03 23:50:13 +0000623 i40e_debug(hw, I40E_DEBUG_AQ_MESSAGE,
624 "%s: ntc %d head %d.\n", __func__, ntc,
625 rd32(hw, hw->aq.asq.head));
626
Greg Rosed358aa92013-12-21 06:13:11 +0000627 if (details->callback) {
628 I40E_ADMINQ_CALLBACK cb_func =
629 (I40E_ADMINQ_CALLBACK)details->callback;
630 desc_cb = *desc;
631 cb_func(hw, &desc_cb);
632 }
633 memset((void *)desc, 0, sizeof(struct i40e_aq_desc));
634 memset((void *)details, 0,
635 sizeof(struct i40e_asq_cmd_details));
636 ntc++;
637 if (ntc == asq->count)
638 ntc = 0;
639 desc = I40E_ADMINQ_DESC(*asq, ntc);
640 details = I40E_ADMINQ_DETAILS(*asq, ntc);
641 }
642
643 asq->next_to_clean = ntc;
644
645 return I40E_DESC_UNUSED(asq);
646}
647
648/**
649 * i40evf_asq_done - check if FW has processed the Admin Send Queue
650 * @hw: pointer to the hw struct
651 *
652 * Returns true if the firmware has processed all descriptors on the
653 * admin send queue. Returns false if there are still requests pending.
654 **/
655bool i40evf_asq_done(struct i40e_hw *hw)
656{
657 /* AQ designers suggest use of head for better
658 * timing reliability than DD bit
659 */
660 return rd32(hw, hw->aq.asq.head) == hw->aq.asq.next_to_use;
661
662}
663
664/**
665 * i40evf_asq_send_command - send command to Admin Queue
666 * @hw: pointer to the hw struct
667 * @desc: prefilled descriptor describing the command (non DMA mem)
668 * @buff: buffer to use for indirect commands
669 * @buff_size: size of buffer for indirect commands
670 * @cmd_details: pointer to command details structure
671 *
672 * This is the main send command driver routine for the Admin Queue send
673 * queue. It runs the queue, cleans the queue, etc
674 **/
675i40e_status i40evf_asq_send_command(struct i40e_hw *hw,
676 struct i40e_aq_desc *desc,
677 void *buff, /* can be NULL */
678 u16 buff_size,
679 struct i40e_asq_cmd_details *cmd_details)
680{
681 i40e_status status = 0;
682 struct i40e_dma_mem *dma_buff = NULL;
683 struct i40e_asq_cmd_details *details;
684 struct i40e_aq_desc *desc_on_ring;
685 bool cmd_completed = false;
686 u16 retval = 0;
Michal Kosiarz80a977e2014-06-03 23:50:13 +0000687 u32 val = 0;
688
689 val = rd32(hw, hw->aq.asq.head);
690 if (val >= hw->aq.num_asq_entries) {
691 i40e_debug(hw, I40E_DEBUG_AQ_MESSAGE,
692 "AQTX: head overrun at %d\n", val);
693 status = I40E_ERR_QUEUE_EMPTY;
694 goto asq_send_command_exit;
695 }
Greg Rosed358aa92013-12-21 06:13:11 +0000696
697 if (hw->aq.asq.count == 0) {
698 i40e_debug(hw, I40E_DEBUG_AQ_MESSAGE,
699 "AQTX: Admin queue not initialized.\n");
700 status = I40E_ERR_QUEUE_EMPTY;
701 goto asq_send_command_exit;
702 }
703
Shannon Nelsonc9296ad2014-03-14 07:32:22 +0000704 if (i40e_is_nvm_update_op(desc) && hw->aq.nvm_busy) {
705 i40e_debug(hw, I40E_DEBUG_AQ_MESSAGE, "AQTX: NVM busy.\n");
706 status = I40E_ERR_NVM;
707 goto asq_send_command_exit;
708 }
709
Greg Rosed358aa92013-12-21 06:13:11 +0000710 details = I40E_ADMINQ_DETAILS(hw->aq.asq, hw->aq.asq.next_to_use);
711 if (cmd_details) {
712 *details = *cmd_details;
713
714 /* If the cmd_details are defined copy the cookie. The
715 * cpu_to_le32 is not needed here because the data is ignored
716 * by the FW, only used by the driver
717 */
718 if (details->cookie) {
719 desc->cookie_high =
720 cpu_to_le32(upper_32_bits(details->cookie));
721 desc->cookie_low =
722 cpu_to_le32(lower_32_bits(details->cookie));
723 }
724 } else {
725 memset(details, 0, sizeof(struct i40e_asq_cmd_details));
726 }
727
728 /* clear requested flags and then set additional flags if defined */
729 desc->flags &= ~cpu_to_le16(details->flags_dis);
730 desc->flags |= cpu_to_le16(details->flags_ena);
731
732 mutex_lock(&hw->aq.asq_mutex);
733
734 if (buff_size > hw->aq.asq_buf_size) {
735 i40e_debug(hw,
736 I40E_DEBUG_AQ_MESSAGE,
737 "AQTX: Invalid buffer size: %d.\n",
738 buff_size);
739 status = I40E_ERR_INVALID_SIZE;
740 goto asq_send_command_error;
741 }
742
743 if (details->postpone && !details->async) {
744 i40e_debug(hw,
745 I40E_DEBUG_AQ_MESSAGE,
746 "AQTX: Async flag not set along with postpone flag");
747 status = I40E_ERR_PARAM;
748 goto asq_send_command_error;
749 }
750
751 /* call clean and check queue available function to reclaim the
752 * descriptors that were processed by FW, the function returns the
753 * number of desc available
754 */
755 /* the clean function called here could be called in a separate thread
756 * in case of asynchronous completions
757 */
758 if (i40e_clean_asq(hw) == 0) {
759 i40e_debug(hw,
760 I40E_DEBUG_AQ_MESSAGE,
761 "AQTX: Error queue is full.\n");
762 status = I40E_ERR_ADMIN_QUEUE_FULL;
763 goto asq_send_command_error;
764 }
765
766 /* initialize the temp desc pointer with the right desc */
767 desc_on_ring = I40E_ADMINQ_DESC(hw->aq.asq, hw->aq.asq.next_to_use);
768
769 /* if the desc is available copy the temp desc to the right place */
770 *desc_on_ring = *desc;
771
772 /* if buff is not NULL assume indirect command */
773 if (buff != NULL) {
774 dma_buff = &(hw->aq.asq.r.asq_bi[hw->aq.asq.next_to_use]);
775 /* copy the user buff into the respective DMA buff */
776 memcpy(dma_buff->va, buff, buff_size);
777 desc_on_ring->datalen = cpu_to_le16(buff_size);
778
779 /* Update the address values in the desc with the pa value
780 * for respective buffer
781 */
782 desc_on_ring->params.external.addr_high =
783 cpu_to_le32(upper_32_bits(dma_buff->pa));
784 desc_on_ring->params.external.addr_low =
785 cpu_to_le32(lower_32_bits(dma_buff->pa));
786 }
787
788 /* bump the tail */
Kamil Krawczyk66d90e72014-06-04 00:57:12 +0000789 i40e_debug(hw, I40E_DEBUG_AQ_MESSAGE, "AQTX: desc and buffer:\n");
Greg Rosed358aa92013-12-21 06:13:11 +0000790 i40evf_debug_aq(hw, I40E_DEBUG_AQ_COMMAND, (void *)desc_on_ring, buff);
791 (hw->aq.asq.next_to_use)++;
792 if (hw->aq.asq.next_to_use == hw->aq.asq.count)
793 hw->aq.asq.next_to_use = 0;
794 if (!details->postpone)
795 wr32(hw, hw->aq.asq.tail, hw->aq.asq.next_to_use);
796
797 /* if cmd_details are not defined or async flag is not set,
798 * we need to wait for desc write back
799 */
800 if (!details->async && !details->postpone) {
801 u32 total_delay = 0;
802 u32 delay_len = 10;
803
804 do {
805 /* AQ designers suggest use of head for better
806 * timing reliability than DD bit
807 */
808 if (i40evf_asq_done(hw))
809 break;
810 /* ugh! delay while spin_lock */
811 udelay(delay_len);
812 total_delay += delay_len;
813 } while (total_delay < I40E_ASQ_CMD_TIMEOUT);
814 }
815
816 /* if ready, copy the desc back to temp */
817 if (i40evf_asq_done(hw)) {
818 *desc = *desc_on_ring;
819 if (buff != NULL)
820 memcpy(buff, dma_buff->va, buff_size);
821 retval = le16_to_cpu(desc->retval);
822 if (retval != 0) {
823 i40e_debug(hw,
824 I40E_DEBUG_AQ_MESSAGE,
825 "AQTX: Command completed with error 0x%X.\n",
826 retval);
Kamil Krawczyk66d90e72014-06-04 00:57:12 +0000827
Greg Rosed358aa92013-12-21 06:13:11 +0000828 /* strip off FW internal code */
829 retval &= 0xff;
830 }
831 cmd_completed = true;
832 if ((enum i40e_admin_queue_err)retval == I40E_AQ_RC_OK)
833 status = 0;
834 else
835 status = I40E_ERR_ADMIN_QUEUE_ERROR;
836 hw->aq.asq_last_status = (enum i40e_admin_queue_err)retval;
837 }
838
Shannon Nelsonc9296ad2014-03-14 07:32:22 +0000839 if (i40e_is_nvm_update_op(desc))
840 hw->aq.nvm_busy = true;
841
Kamil Krawczyk66d90e72014-06-04 00:57:12 +0000842 if (le16_to_cpu(desc->datalen) == buff_size) {
843 i40e_debug(hw, I40E_DEBUG_AQ_MESSAGE,
844 "AQTX: desc and buffer writeback:\n");
845 i40evf_debug_aq(hw, I40E_DEBUG_AQ_COMMAND, (void *)desc, buff);
846 }
847
Greg Rosed358aa92013-12-21 06:13:11 +0000848 /* update the error if time out occurred */
849 if ((!cmd_completed) &&
850 (!details->async && !details->postpone)) {
851 i40e_debug(hw,
852 I40E_DEBUG_AQ_MESSAGE,
853 "AQTX: Writeback timeout.\n");
854 status = I40E_ERR_ADMIN_QUEUE_TIMEOUT;
855 }
856
857asq_send_command_error:
858 mutex_unlock(&hw->aq.asq_mutex);
859asq_send_command_exit:
860 return status;
861}
862
863/**
864 * i40evf_fill_default_direct_cmd_desc - AQ descriptor helper function
865 * @desc: pointer to the temp descriptor (non DMA mem)
866 * @opcode: the opcode can be used to decide which flags to turn off or on
867 *
868 * Fill the desc with default values
869 **/
870void i40evf_fill_default_direct_cmd_desc(struct i40e_aq_desc *desc,
871 u16 opcode)
872{
873 /* zero out the desc */
874 memset((void *)desc, 0, sizeof(struct i40e_aq_desc));
875 desc->opcode = cpu_to_le16(opcode);
876 desc->flags = cpu_to_le16(I40E_AQ_FLAG_SI);
877}
878
879/**
880 * i40evf_clean_arq_element
881 * @hw: pointer to the hw struct
882 * @e: event info from the receive descriptor, includes any buffers
883 * @pending: number of events that could be left to process
884 *
885 * This function cleans one Admin Receive Queue element and returns
886 * the contents through e. It can also return how many events are
887 * left to process through 'pending'
888 **/
889i40e_status i40evf_clean_arq_element(struct i40e_hw *hw,
890 struct i40e_arq_event_info *e,
891 u16 *pending)
892{
893 i40e_status ret_code = 0;
894 u16 ntc = hw->aq.arq.next_to_clean;
895 struct i40e_aq_desc *desc;
896 struct i40e_dma_mem *bi;
897 u16 desc_idx;
898 u16 datalen;
899 u16 flags;
900 u16 ntu;
901
902 /* take the lock before we start messing with the ring */
903 mutex_lock(&hw->aq.arq_mutex);
904
905 /* set next_to_use to head */
906 ntu = (rd32(hw, hw->aq.arq.head) & I40E_PF_ARQH_ARQH_MASK);
907 if (ntu == ntc) {
908 /* nothing to do - shouldn't need to update ring's values */
909 i40e_debug(hw,
910 I40E_DEBUG_AQ_MESSAGE,
911 "AQRX: Queue is empty.\n");
912 ret_code = I40E_ERR_ADMIN_QUEUE_NO_WORK;
913 goto clean_arq_element_out;
914 }
915
916 /* now clean the next descriptor */
917 desc = I40E_ADMINQ_DESC(hw->aq.arq, ntc);
918 desc_idx = ntc;
Greg Rosed358aa92013-12-21 06:13:11 +0000919
920 flags = le16_to_cpu(desc->flags);
921 if (flags & I40E_AQ_FLAG_ERR) {
922 ret_code = I40E_ERR_ADMIN_QUEUE_ERROR;
923 hw->aq.arq_last_status =
924 (enum i40e_admin_queue_err)le16_to_cpu(desc->retval);
925 i40e_debug(hw,
926 I40E_DEBUG_AQ_MESSAGE,
927 "AQRX: Event received with error 0x%X.\n",
928 hw->aq.arq_last_status);
929 } else {
930 e->desc = *desc;
931 datalen = le16_to_cpu(desc->datalen);
932 e->msg_size = min(datalen, e->msg_size);
933 if (e->msg_buf != NULL && (e->msg_size != 0))
934 memcpy(e->msg_buf, hw->aq.arq.r.arq_bi[desc_idx].va,
935 e->msg_size);
936 }
937
Shannon Nelsonc9296ad2014-03-14 07:32:22 +0000938 if (i40e_is_nvm_update_op(&e->desc))
939 hw->aq.nvm_busy = false;
940
Kamil Krawczyk66d90e72014-06-04 00:57:12 +0000941 i40e_debug(hw, I40E_DEBUG_AQ_MESSAGE, "AQRX: desc and buffer:\n");
942 i40evf_debug_aq(hw, I40E_DEBUG_AQ_COMMAND, (void *)desc, e->msg_buf);
943
Greg Rosed358aa92013-12-21 06:13:11 +0000944 /* Restore the original datalen and buffer address in the desc,
945 * FW updates datalen to indicate the event message
946 * size
947 */
948 bi = &hw->aq.arq.r.arq_bi[ntc];
949 memset((void *)desc, 0, sizeof(struct i40e_aq_desc));
950
951 desc->flags = cpu_to_le16(I40E_AQ_FLAG_BUF);
952 if (hw->aq.arq_buf_size > I40E_AQ_LARGE_BUF)
953 desc->flags |= cpu_to_le16(I40E_AQ_FLAG_LB);
954 desc->datalen = cpu_to_le16((u16)bi->size);
955 desc->params.external.addr_high = cpu_to_le32(upper_32_bits(bi->pa));
956 desc->params.external.addr_low = cpu_to_le32(lower_32_bits(bi->pa));
957
958 /* set tail = the last cleaned desc index. */
959 wr32(hw, hw->aq.arq.tail, ntc);
960 /* ntc is updated to tail + 1 */
961 ntc++;
962 if (ntc == hw->aq.num_arq_entries)
963 ntc = 0;
964 hw->aq.arq.next_to_clean = ntc;
965 hw->aq.arq.next_to_use = ntu;
966
967clean_arq_element_out:
968 /* Set pending if needed, unlock and return */
969 if (pending != NULL)
970 *pending = (ntc > ntu ? hw->aq.arq.count : 0) + (ntu - ntc);
971 mutex_unlock(&hw->aq.arq_mutex);
972
973 return ret_code;
974}
975
976void i40evf_resume_aq(struct i40e_hw *hw)
977{
978 /* Registers are reset after PF reset */
979 hw->aq.asq.next_to_use = 0;
980 hw->aq.asq.next_to_clean = 0;
981
982 i40e_config_asq_regs(hw);
983
984 hw->aq.arq.next_to_use = 0;
985 hw->aq.arq.next_to_clean = 0;
986
987 i40e_config_arq_regs(hw);
988}