blob: f08450b907745afefcdd6da766f7a32ae3aa57c2 [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 */
Anjali Singhai Jaine7f2e4b2014-11-11 20:06:58 +000052 if (i40e_is_vf(hw)) {
Greg Rosed358aa92013-12-21 06:13:11 +000053 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 }
64}
65
66/**
67 * i40e_alloc_adminq_asq_ring - Allocate Admin Queue send rings
68 * @hw: pointer to the hardware structure
69 **/
70static i40e_status i40e_alloc_adminq_asq_ring(struct i40e_hw *hw)
71{
72 i40e_status ret_code;
73
74 ret_code = i40e_allocate_dma_mem(hw, &hw->aq.asq.desc_buf,
75 i40e_mem_atq_ring,
76 (hw->aq.num_asq_entries *
77 sizeof(struct i40e_aq_desc)),
78 I40E_ADMINQ_DESC_ALIGNMENT);
79 if (ret_code)
80 return ret_code;
81
82 ret_code = i40e_allocate_virt_mem(hw, &hw->aq.asq.cmd_buf,
83 (hw->aq.num_asq_entries *
84 sizeof(struct i40e_asq_cmd_details)));
85 if (ret_code) {
86 i40e_free_dma_mem(hw, &hw->aq.asq.desc_buf);
87 return ret_code;
88 }
89
90 return ret_code;
91}
92
93/**
94 * i40e_alloc_adminq_arq_ring - Allocate Admin Queue receive rings
95 * @hw: pointer to the hardware structure
96 **/
97static i40e_status i40e_alloc_adminq_arq_ring(struct i40e_hw *hw)
98{
99 i40e_status ret_code;
100
101 ret_code = i40e_allocate_dma_mem(hw, &hw->aq.arq.desc_buf,
102 i40e_mem_arq_ring,
103 (hw->aq.num_arq_entries *
104 sizeof(struct i40e_aq_desc)),
105 I40E_ADMINQ_DESC_ALIGNMENT);
106
107 return ret_code;
108}
109
110/**
111 * i40e_free_adminq_asq - Free Admin Queue send rings
112 * @hw: pointer to the hardware structure
113 *
114 * This assumes the posted send buffers have already been cleaned
115 * and de-allocated
116 **/
117static void i40e_free_adminq_asq(struct i40e_hw *hw)
118{
119 i40e_free_dma_mem(hw, &hw->aq.asq.desc_buf);
120}
121
122/**
123 * i40e_free_adminq_arq - Free Admin Queue receive rings
124 * @hw: pointer to the hardware structure
125 *
126 * This assumes the posted receive buffers have already been cleaned
127 * and de-allocated
128 **/
129static void i40e_free_adminq_arq(struct i40e_hw *hw)
130{
131 i40e_free_dma_mem(hw, &hw->aq.arq.desc_buf);
132}
133
134/**
135 * i40e_alloc_arq_bufs - Allocate pre-posted buffers for the receive queue
136 * @hw: pointer to the hardware structure
137 **/
138static i40e_status i40e_alloc_arq_bufs(struct i40e_hw *hw)
139{
140 i40e_status ret_code;
141 struct i40e_aq_desc *desc;
142 struct i40e_dma_mem *bi;
143 int i;
144
145 /* We'll be allocating the buffer info memory first, then we can
146 * allocate the mapped buffers for the event processing
147 */
148
149 /* buffer_info structures do not need alignment */
150 ret_code = i40e_allocate_virt_mem(hw, &hw->aq.arq.dma_head,
151 (hw->aq.num_arq_entries * sizeof(struct i40e_dma_mem)));
152 if (ret_code)
153 goto alloc_arq_bufs;
154 hw->aq.arq.r.arq_bi = (struct i40e_dma_mem *)hw->aq.arq.dma_head.va;
155
156 /* allocate the mapped buffers */
157 for (i = 0; i < hw->aq.num_arq_entries; i++) {
158 bi = &hw->aq.arq.r.arq_bi[i];
159 ret_code = i40e_allocate_dma_mem(hw, bi,
160 i40e_mem_arq_buf,
161 hw->aq.arq_buf_size,
162 I40E_ADMINQ_DESC_ALIGNMENT);
163 if (ret_code)
164 goto unwind_alloc_arq_bufs;
165
166 /* now configure the descriptors for use */
167 desc = I40E_ADMINQ_DESC(hw->aq.arq, i);
168
169 desc->flags = cpu_to_le16(I40E_AQ_FLAG_BUF);
170 if (hw->aq.arq_buf_size > I40E_AQ_LARGE_BUF)
171 desc->flags |= cpu_to_le16(I40E_AQ_FLAG_LB);
172 desc->opcode = 0;
173 /* This is in accordance with Admin queue design, there is no
174 * register for buffer size configuration
175 */
176 desc->datalen = cpu_to_le16((u16)bi->size);
177 desc->retval = 0;
178 desc->cookie_high = 0;
179 desc->cookie_low = 0;
180 desc->params.external.addr_high =
181 cpu_to_le32(upper_32_bits(bi->pa));
182 desc->params.external.addr_low =
183 cpu_to_le32(lower_32_bits(bi->pa));
184 desc->params.external.param0 = 0;
185 desc->params.external.param1 = 0;
186 }
187
188alloc_arq_bufs:
189 return ret_code;
190
191unwind_alloc_arq_bufs:
192 /* don't try to free the one that failed... */
193 i--;
194 for (; i >= 0; i--)
195 i40e_free_dma_mem(hw, &hw->aq.arq.r.arq_bi[i]);
196 i40e_free_virt_mem(hw, &hw->aq.arq.dma_head);
197
198 return ret_code;
199}
200
201/**
202 * i40e_alloc_asq_bufs - Allocate empty buffer structs for the send queue
203 * @hw: pointer to the hardware structure
204 **/
205static i40e_status i40e_alloc_asq_bufs(struct i40e_hw *hw)
206{
207 i40e_status ret_code;
208 struct i40e_dma_mem *bi;
209 int i;
210
211 /* No mapped memory needed yet, just the buffer info structures */
212 ret_code = i40e_allocate_virt_mem(hw, &hw->aq.asq.dma_head,
213 (hw->aq.num_asq_entries * sizeof(struct i40e_dma_mem)));
214 if (ret_code)
215 goto alloc_asq_bufs;
216 hw->aq.asq.r.asq_bi = (struct i40e_dma_mem *)hw->aq.asq.dma_head.va;
217
218 /* allocate the mapped buffers */
219 for (i = 0; i < hw->aq.num_asq_entries; i++) {
220 bi = &hw->aq.asq.r.asq_bi[i];
221 ret_code = i40e_allocate_dma_mem(hw, bi,
222 i40e_mem_asq_buf,
223 hw->aq.asq_buf_size,
224 I40E_ADMINQ_DESC_ALIGNMENT);
225 if (ret_code)
226 goto unwind_alloc_asq_bufs;
227 }
228alloc_asq_bufs:
229 return ret_code;
230
231unwind_alloc_asq_bufs:
232 /* don't try to free the one that failed... */
233 i--;
234 for (; i >= 0; i--)
235 i40e_free_dma_mem(hw, &hw->aq.asq.r.asq_bi[i]);
236 i40e_free_virt_mem(hw, &hw->aq.asq.dma_head);
237
238 return ret_code;
239}
240
241/**
242 * i40e_free_arq_bufs - Free receive queue buffer info elements
243 * @hw: pointer to the hardware structure
244 **/
245static void i40e_free_arq_bufs(struct i40e_hw *hw)
246{
247 int i;
248
249 /* free descriptors */
250 for (i = 0; i < hw->aq.num_arq_entries; i++)
251 i40e_free_dma_mem(hw, &hw->aq.arq.r.arq_bi[i]);
252
253 /* free the descriptor memory */
254 i40e_free_dma_mem(hw, &hw->aq.arq.desc_buf);
255
256 /* free the dma header */
257 i40e_free_virt_mem(hw, &hw->aq.arq.dma_head);
258}
259
260/**
261 * i40e_free_asq_bufs - Free send queue buffer info elements
262 * @hw: pointer to the hardware structure
263 **/
264static void i40e_free_asq_bufs(struct i40e_hw *hw)
265{
266 int i;
267
268 /* only unmap if the address is non-NULL */
269 for (i = 0; i < hw->aq.num_asq_entries; i++)
270 if (hw->aq.asq.r.asq_bi[i].pa)
271 i40e_free_dma_mem(hw, &hw->aq.asq.r.asq_bi[i]);
272
273 /* free the buffer info list */
274 i40e_free_virt_mem(hw, &hw->aq.asq.cmd_buf);
275
276 /* free the descriptor memory */
277 i40e_free_dma_mem(hw, &hw->aq.asq.desc_buf);
278
279 /* free the dma header */
280 i40e_free_virt_mem(hw, &hw->aq.asq.dma_head);
281}
282
283/**
284 * i40e_config_asq_regs - configure ASQ registers
285 * @hw: pointer to the hardware structure
286 *
287 * Configure base address and length registers for the transmit queue
288 **/
Kamil Krawczyke03af1e2014-04-23 04:50:02 +0000289static i40e_status i40e_config_asq_regs(struct i40e_hw *hw)
Greg Rosed358aa92013-12-21 06:13:11 +0000290{
Kamil Krawczyke03af1e2014-04-23 04:50:02 +0000291 i40e_status ret_code = 0;
292 u32 reg = 0;
293
Michal Kosiarz80a977e2014-06-03 23:50:13 +0000294 /* Clear Head and Tail */
295 wr32(hw, hw->aq.asq.head, 0);
296 wr32(hw, hw->aq.asq.tail, 0);
297
Shannon Nelson87dc3462014-06-04 20:41:17 +0000298 /* set starting point */
299 wr32(hw, hw->aq.asq.len, (hw->aq.num_asq_entries |
Anjali Singhai Jaine02a7f82015-07-10 19:36:06 -0400300 I40E_VF_ATQLEN1_ATQENABLE_MASK));
Shannon Nelson87dc3462014-06-04 20:41:17 +0000301 wr32(hw, hw->aq.asq.bal, lower_32_bits(hw->aq.asq.desc_buf.pa));
302 wr32(hw, hw->aq.asq.bah, upper_32_bits(hw->aq.asq.desc_buf.pa));
Kamil Krawczyke03af1e2014-04-23 04:50:02 +0000303
304 /* Check one register to verify that config was applied */
Shannon Nelson87dc3462014-06-04 20:41:17 +0000305 reg = rd32(hw, hw->aq.asq.bal);
Kamil Krawczyke03af1e2014-04-23 04:50:02 +0000306 if (reg != lower_32_bits(hw->aq.asq.desc_buf.pa))
307 ret_code = I40E_ERR_ADMIN_QUEUE_ERROR;
308
309 return ret_code;
Greg Rosed358aa92013-12-21 06:13:11 +0000310}
311
312/**
313 * i40e_config_arq_regs - ARQ register configuration
314 * @hw: pointer to the hardware structure
315 *
316 * Configure base address and length registers for the receive (event queue)
317 **/
Kamil Krawczyke03af1e2014-04-23 04:50:02 +0000318static i40e_status i40e_config_arq_regs(struct i40e_hw *hw)
Greg Rosed358aa92013-12-21 06:13:11 +0000319{
Kamil Krawczyke03af1e2014-04-23 04:50:02 +0000320 i40e_status ret_code = 0;
321 u32 reg = 0;
322
Michal Kosiarz80a977e2014-06-03 23:50:13 +0000323 /* Clear Head and Tail */
324 wr32(hw, hw->aq.arq.head, 0);
325 wr32(hw, hw->aq.arq.tail, 0);
326
Shannon Nelson87dc3462014-06-04 20:41:17 +0000327 /* set starting point */
328 wr32(hw, hw->aq.arq.len, (hw->aq.num_arq_entries |
Anjali Singhai Jaine02a7f82015-07-10 19:36:06 -0400329 I40E_VF_ARQLEN1_ARQENABLE_MASK));
Shannon Nelson87dc3462014-06-04 20:41:17 +0000330 wr32(hw, hw->aq.arq.bal, lower_32_bits(hw->aq.arq.desc_buf.pa));
331 wr32(hw, hw->aq.arq.bah, upper_32_bits(hw->aq.arq.desc_buf.pa));
Greg Rosed358aa92013-12-21 06:13:11 +0000332
333 /* Update tail in the HW to post pre-allocated buffers */
334 wr32(hw, hw->aq.arq.tail, hw->aq.num_arq_entries - 1);
Kamil Krawczyke03af1e2014-04-23 04:50:02 +0000335
336 /* Check one register to verify that config was applied */
Shannon Nelson87dc3462014-06-04 20:41:17 +0000337 reg = rd32(hw, hw->aq.arq.bal);
Kamil Krawczyke03af1e2014-04-23 04:50:02 +0000338 if (reg != lower_32_bits(hw->aq.arq.desc_buf.pa))
339 ret_code = I40E_ERR_ADMIN_QUEUE_ERROR;
340
341 return ret_code;
Greg Rosed358aa92013-12-21 06:13:11 +0000342}
343
344/**
345 * i40e_init_asq - main initialization routine for ASQ
346 * @hw: pointer to the hardware structure
347 *
348 * This is the main initialization routine for the Admin Send Queue
349 * Prior to calling this function, drivers *MUST* set the following fields
350 * in the hw->aq structure:
351 * - hw->aq.num_asq_entries
352 * - hw->aq.arq_buf_size
353 *
354 * Do *NOT* hold the lock when calling this as the memory allocation routines
355 * called are not going to be atomic context safe
356 **/
357static i40e_status i40e_init_asq(struct i40e_hw *hw)
358{
359 i40e_status ret_code = 0;
360
361 if (hw->aq.asq.count > 0) {
362 /* queue already initialized */
363 ret_code = I40E_ERR_NOT_READY;
364 goto init_adminq_exit;
365 }
366
367 /* verify input for valid configuration */
368 if ((hw->aq.num_asq_entries == 0) ||
369 (hw->aq.asq_buf_size == 0)) {
370 ret_code = I40E_ERR_CONFIG;
371 goto init_adminq_exit;
372 }
373
374 hw->aq.asq.next_to_use = 0;
375 hw->aq.asq.next_to_clean = 0;
376 hw->aq.asq.count = hw->aq.num_asq_entries;
377
378 /* allocate the ring memory */
379 ret_code = i40e_alloc_adminq_asq_ring(hw);
380 if (ret_code)
381 goto init_adminq_exit;
382
383 /* allocate buffers in the rings */
384 ret_code = i40e_alloc_asq_bufs(hw);
385 if (ret_code)
386 goto init_adminq_free_rings;
387
388 /* initialize base registers */
Kamil Krawczyke03af1e2014-04-23 04:50:02 +0000389 ret_code = i40e_config_asq_regs(hw);
390 if (ret_code)
391 goto init_adminq_free_rings;
Greg Rosed358aa92013-12-21 06:13:11 +0000392
393 /* success! */
394 goto init_adminq_exit;
395
396init_adminq_free_rings:
397 i40e_free_adminq_asq(hw);
398
399init_adminq_exit:
400 return ret_code;
401}
402
403/**
404 * i40e_init_arq - initialize ARQ
405 * @hw: pointer to the hardware structure
406 *
407 * The main initialization routine for the Admin Receive (Event) Queue.
408 * Prior to calling this function, drivers *MUST* set the following fields
409 * in the hw->aq structure:
410 * - hw->aq.num_asq_entries
411 * - hw->aq.arq_buf_size
412 *
413 * Do *NOT* hold the lock when calling this as the memory allocation routines
414 * called are not going to be atomic context safe
415 **/
416static i40e_status i40e_init_arq(struct i40e_hw *hw)
417{
418 i40e_status ret_code = 0;
419
420 if (hw->aq.arq.count > 0) {
421 /* queue already initialized */
422 ret_code = I40E_ERR_NOT_READY;
423 goto init_adminq_exit;
424 }
425
426 /* verify input for valid configuration */
427 if ((hw->aq.num_arq_entries == 0) ||
428 (hw->aq.arq_buf_size == 0)) {
429 ret_code = I40E_ERR_CONFIG;
430 goto init_adminq_exit;
431 }
432
433 hw->aq.arq.next_to_use = 0;
434 hw->aq.arq.next_to_clean = 0;
435 hw->aq.arq.count = hw->aq.num_arq_entries;
436
437 /* allocate the ring memory */
438 ret_code = i40e_alloc_adminq_arq_ring(hw);
439 if (ret_code)
440 goto init_adminq_exit;
441
442 /* allocate buffers in the rings */
443 ret_code = i40e_alloc_arq_bufs(hw);
444 if (ret_code)
445 goto init_adminq_free_rings;
446
447 /* initialize base registers */
Kamil Krawczyke03af1e2014-04-23 04:50:02 +0000448 ret_code = i40e_config_arq_regs(hw);
449 if (ret_code)
450 goto init_adminq_free_rings;
Greg Rosed358aa92013-12-21 06:13:11 +0000451
452 /* success! */
453 goto init_adminq_exit;
454
455init_adminq_free_rings:
456 i40e_free_adminq_arq(hw);
457
458init_adminq_exit:
459 return ret_code;
460}
461
462/**
463 * i40e_shutdown_asq - shutdown the ASQ
464 * @hw: pointer to the hardware structure
465 *
466 * The main shutdown routine for the Admin Send Queue
467 **/
468static i40e_status i40e_shutdown_asq(struct i40e_hw *hw)
469{
470 i40e_status ret_code = 0;
471
472 if (hw->aq.asq.count == 0)
473 return I40E_ERR_NOT_READY;
474
475 /* Stop firmware AdminQ processing */
476 wr32(hw, hw->aq.asq.head, 0);
477 wr32(hw, hw->aq.asq.tail, 0);
478 wr32(hw, hw->aq.asq.len, 0);
Shannon Nelson4346940b92014-06-04 20:41:22 +0000479 wr32(hw, hw->aq.asq.bal, 0);
480 wr32(hw, hw->aq.asq.bah, 0);
Greg Rosed358aa92013-12-21 06:13:11 +0000481
482 /* make sure lock is available */
483 mutex_lock(&hw->aq.asq_mutex);
484
485 hw->aq.asq.count = 0; /* to indicate uninitialized queue */
486
487 /* free ring buffers */
488 i40e_free_asq_bufs(hw);
489
490 mutex_unlock(&hw->aq.asq_mutex);
491
492 return ret_code;
493}
494
495/**
496 * i40e_shutdown_arq - shutdown ARQ
497 * @hw: pointer to the hardware structure
498 *
499 * The main shutdown routine for the Admin Receive Queue
500 **/
501static i40e_status i40e_shutdown_arq(struct i40e_hw *hw)
502{
503 i40e_status ret_code = 0;
504
505 if (hw->aq.arq.count == 0)
506 return I40E_ERR_NOT_READY;
507
508 /* Stop firmware AdminQ processing */
509 wr32(hw, hw->aq.arq.head, 0);
510 wr32(hw, hw->aq.arq.tail, 0);
511 wr32(hw, hw->aq.arq.len, 0);
Shannon Nelson4346940b92014-06-04 20:41:22 +0000512 wr32(hw, hw->aq.arq.bal, 0);
513 wr32(hw, hw->aq.arq.bah, 0);
Greg Rosed358aa92013-12-21 06:13:11 +0000514
515 /* make sure lock is available */
516 mutex_lock(&hw->aq.arq_mutex);
517
518 hw->aq.arq.count = 0; /* to indicate uninitialized queue */
519
520 /* free ring buffers */
521 i40e_free_arq_bufs(hw);
522
523 mutex_unlock(&hw->aq.arq_mutex);
524
525 return ret_code;
526}
527
528/**
529 * i40evf_init_adminq - main initialization routine for Admin Queue
530 * @hw: pointer to the hardware structure
531 *
532 * Prior to calling this function, drivers *MUST* set the following fields
533 * in the hw->aq structure:
534 * - hw->aq.num_asq_entries
535 * - hw->aq.num_arq_entries
536 * - hw->aq.arq_buf_size
537 * - hw->aq.asq_buf_size
538 **/
539i40e_status i40evf_init_adminq(struct i40e_hw *hw)
540{
541 i40e_status ret_code;
542
543 /* verify input for valid configuration */
544 if ((hw->aq.num_arq_entries == 0) ||
545 (hw->aq.num_asq_entries == 0) ||
546 (hw->aq.arq_buf_size == 0) ||
547 (hw->aq.asq_buf_size == 0)) {
548 ret_code = I40E_ERR_CONFIG;
549 goto init_adminq_exit;
550 }
551
552 /* initialize locks */
553 mutex_init(&hw->aq.asq_mutex);
554 mutex_init(&hw->aq.arq_mutex);
555
556 /* Set up register offsets */
557 i40e_adminq_init_regs(hw);
558
Kamil Krawczyk09c4e562014-06-04 20:41:43 +0000559 /* setup ASQ command write back timeout */
560 hw->aq.asq_cmd_timeout = I40E_ASQ_CMD_TIMEOUT;
561
Greg Rosed358aa92013-12-21 06:13:11 +0000562 /* allocate the ASQ */
563 ret_code = i40e_init_asq(hw);
564 if (ret_code)
565 goto init_adminq_destroy_locks;
566
567 /* allocate the ARQ */
568 ret_code = i40e_init_arq(hw);
569 if (ret_code)
570 goto init_adminq_free_asq;
571
572 /* success! */
573 goto init_adminq_exit;
574
575init_adminq_free_asq:
576 i40e_shutdown_asq(hw);
577init_adminq_destroy_locks:
578
579init_adminq_exit:
580 return ret_code;
581}
582
583/**
584 * i40evf_shutdown_adminq - shutdown routine for the Admin Queue
585 * @hw: pointer to the hardware structure
586 **/
587i40e_status i40evf_shutdown_adminq(struct i40e_hw *hw)
588{
589 i40e_status ret_code = 0;
590
591 if (i40evf_check_asq_alive(hw))
592 i40evf_aq_queue_shutdown(hw, true);
593
594 i40e_shutdown_asq(hw);
595 i40e_shutdown_arq(hw);
596
597 /* destroy the locks */
598
599 return ret_code;
600}
601
602/**
603 * i40e_clean_asq - cleans Admin send queue
604 * @hw: pointer to the hardware structure
605 *
606 * returns the number of free desc
607 **/
608static u16 i40e_clean_asq(struct i40e_hw *hw)
609{
610 struct i40e_adminq_ring *asq = &(hw->aq.asq);
611 struct i40e_asq_cmd_details *details;
612 u16 ntc = asq->next_to_clean;
613 struct i40e_aq_desc desc_cb;
614 struct i40e_aq_desc *desc;
615
616 desc = I40E_ADMINQ_DESC(*asq, ntc);
617 details = I40E_ADMINQ_DETAILS(*asq, ntc);
618 while (rd32(hw, hw->aq.asq.head) != ntc) {
Michal Kosiarz80a977e2014-06-03 23:50:13 +0000619 i40e_debug(hw, I40E_DEBUG_AQ_MESSAGE,
620 "%s: ntc %d head %d.\n", __func__, ntc,
621 rd32(hw, hw->aq.asq.head));
622
Greg Rosed358aa92013-12-21 06:13:11 +0000623 if (details->callback) {
624 I40E_ADMINQ_CALLBACK cb_func =
625 (I40E_ADMINQ_CALLBACK)details->callback;
626 desc_cb = *desc;
627 cb_func(hw, &desc_cb);
628 }
629 memset((void *)desc, 0, sizeof(struct i40e_aq_desc));
630 memset((void *)details, 0,
631 sizeof(struct i40e_asq_cmd_details));
632 ntc++;
633 if (ntc == asq->count)
634 ntc = 0;
635 desc = I40E_ADMINQ_DESC(*asq, ntc);
636 details = I40E_ADMINQ_DETAILS(*asq, ntc);
637 }
638
639 asq->next_to_clean = ntc;
640
641 return I40E_DESC_UNUSED(asq);
642}
643
644/**
645 * i40evf_asq_done - check if FW has processed the Admin Send Queue
646 * @hw: pointer to the hw struct
647 *
648 * Returns true if the firmware has processed all descriptors on the
649 * admin send queue. Returns false if there are still requests pending.
650 **/
651bool i40evf_asq_done(struct i40e_hw *hw)
652{
653 /* AQ designers suggest use of head for better
654 * timing reliability than DD bit
655 */
656 return rd32(hw, hw->aq.asq.head) == hw->aq.asq.next_to_use;
657
658}
659
660/**
661 * i40evf_asq_send_command - send command to Admin Queue
662 * @hw: pointer to the hw struct
663 * @desc: prefilled descriptor describing the command (non DMA mem)
664 * @buff: buffer to use for indirect commands
665 * @buff_size: size of buffer for indirect commands
666 * @cmd_details: pointer to command details structure
667 *
668 * This is the main send command driver routine for the Admin Queue send
669 * queue. It runs the queue, cleans the queue, etc
670 **/
671i40e_status i40evf_asq_send_command(struct i40e_hw *hw,
672 struct i40e_aq_desc *desc,
673 void *buff, /* can be NULL */
674 u16 buff_size,
675 struct i40e_asq_cmd_details *cmd_details)
676{
677 i40e_status status = 0;
678 struct i40e_dma_mem *dma_buff = NULL;
679 struct i40e_asq_cmd_details *details;
680 struct i40e_aq_desc *desc_on_ring;
681 bool cmd_completed = false;
682 u16 retval = 0;
Michal Kosiarz80a977e2014-06-03 23:50:13 +0000683 u32 val = 0;
684
685 val = rd32(hw, hw->aq.asq.head);
686 if (val >= hw->aq.num_asq_entries) {
687 i40e_debug(hw, I40E_DEBUG_AQ_MESSAGE,
688 "AQTX: head overrun at %d\n", val);
689 status = I40E_ERR_QUEUE_EMPTY;
690 goto asq_send_command_exit;
691 }
Greg Rosed358aa92013-12-21 06:13:11 +0000692
693 if (hw->aq.asq.count == 0) {
694 i40e_debug(hw, I40E_DEBUG_AQ_MESSAGE,
695 "AQTX: Admin queue not initialized.\n");
696 status = I40E_ERR_QUEUE_EMPTY;
697 goto asq_send_command_exit;
698 }
699
700 details = I40E_ADMINQ_DETAILS(hw->aq.asq, hw->aq.asq.next_to_use);
701 if (cmd_details) {
702 *details = *cmd_details;
703
704 /* If the cmd_details are defined copy the cookie. The
705 * cpu_to_le32 is not needed here because the data is ignored
706 * by the FW, only used by the driver
707 */
708 if (details->cookie) {
709 desc->cookie_high =
710 cpu_to_le32(upper_32_bits(details->cookie));
711 desc->cookie_low =
712 cpu_to_le32(lower_32_bits(details->cookie));
713 }
714 } else {
715 memset(details, 0, sizeof(struct i40e_asq_cmd_details));
716 }
717
718 /* clear requested flags and then set additional flags if defined */
719 desc->flags &= ~cpu_to_le16(details->flags_dis);
720 desc->flags |= cpu_to_le16(details->flags_ena);
721
722 mutex_lock(&hw->aq.asq_mutex);
723
724 if (buff_size > hw->aq.asq_buf_size) {
725 i40e_debug(hw,
726 I40E_DEBUG_AQ_MESSAGE,
727 "AQTX: Invalid buffer size: %d.\n",
728 buff_size);
729 status = I40E_ERR_INVALID_SIZE;
730 goto asq_send_command_error;
731 }
732
733 if (details->postpone && !details->async) {
734 i40e_debug(hw,
735 I40E_DEBUG_AQ_MESSAGE,
736 "AQTX: Async flag not set along with postpone flag");
737 status = I40E_ERR_PARAM;
738 goto asq_send_command_error;
739 }
740
741 /* call clean and check queue available function to reclaim the
742 * descriptors that were processed by FW, the function returns the
743 * number of desc available
744 */
745 /* the clean function called here could be called in a separate thread
746 * in case of asynchronous completions
747 */
748 if (i40e_clean_asq(hw) == 0) {
749 i40e_debug(hw,
750 I40E_DEBUG_AQ_MESSAGE,
751 "AQTX: Error queue is full.\n");
752 status = I40E_ERR_ADMIN_QUEUE_FULL;
753 goto asq_send_command_error;
754 }
755
756 /* initialize the temp desc pointer with the right desc */
757 desc_on_ring = I40E_ADMINQ_DESC(hw->aq.asq, hw->aq.asq.next_to_use);
758
759 /* if the desc is available copy the temp desc to the right place */
760 *desc_on_ring = *desc;
761
762 /* if buff is not NULL assume indirect command */
763 if (buff != NULL) {
764 dma_buff = &(hw->aq.asq.r.asq_bi[hw->aq.asq.next_to_use]);
765 /* copy the user buff into the respective DMA buff */
766 memcpy(dma_buff->va, buff, buff_size);
767 desc_on_ring->datalen = cpu_to_le16(buff_size);
768
769 /* Update the address values in the desc with the pa value
770 * for respective buffer
771 */
772 desc_on_ring->params.external.addr_high =
773 cpu_to_le32(upper_32_bits(dma_buff->pa));
774 desc_on_ring->params.external.addr_low =
775 cpu_to_le32(lower_32_bits(dma_buff->pa));
776 }
777
778 /* bump the tail */
Kamil Krawczyk66d90e72014-06-04 00:57:12 +0000779 i40e_debug(hw, I40E_DEBUG_AQ_MESSAGE, "AQTX: desc and buffer:\n");
Shannon Nelsonf905dd62014-07-10 07:58:20 +0000780 i40evf_debug_aq(hw, I40E_DEBUG_AQ_COMMAND, (void *)desc_on_ring,
781 buff, buff_size);
Greg Rosed358aa92013-12-21 06:13:11 +0000782 (hw->aq.asq.next_to_use)++;
783 if (hw->aq.asq.next_to_use == hw->aq.asq.count)
784 hw->aq.asq.next_to_use = 0;
785 if (!details->postpone)
786 wr32(hw, hw->aq.asq.tail, hw->aq.asq.next_to_use);
787
788 /* if cmd_details are not defined or async flag is not set,
789 * we need to wait for desc write back
790 */
791 if (!details->async && !details->postpone) {
792 u32 total_delay = 0;
Greg Rosed358aa92013-12-21 06:13:11 +0000793
794 do {
795 /* AQ designers suggest use of head for better
796 * timing reliability than DD bit
797 */
798 if (i40evf_asq_done(hw))
799 break;
Kamil Krawczyk0db4e162014-10-25 03:24:30 +0000800 usleep_range(1000, 2000);
801 total_delay++;
Paul M Stillwell Jrec9a7db2014-07-09 07:46:10 +0000802 } while (total_delay < hw->aq.asq_cmd_timeout);
Greg Rosed358aa92013-12-21 06:13:11 +0000803 }
804
805 /* if ready, copy the desc back to temp */
806 if (i40evf_asq_done(hw)) {
807 *desc = *desc_on_ring;
808 if (buff != NULL)
809 memcpy(buff, dma_buff->va, buff_size);
810 retval = le16_to_cpu(desc->retval);
811 if (retval != 0) {
812 i40e_debug(hw,
813 I40E_DEBUG_AQ_MESSAGE,
814 "AQTX: Command completed with error 0x%X.\n",
815 retval);
Kamil Krawczyk66d90e72014-06-04 00:57:12 +0000816
Greg Rosed358aa92013-12-21 06:13:11 +0000817 /* strip off FW internal code */
818 retval &= 0xff;
819 }
820 cmd_completed = true;
821 if ((enum i40e_admin_queue_err)retval == I40E_AQ_RC_OK)
822 status = 0;
823 else
824 status = I40E_ERR_ADMIN_QUEUE_ERROR;
825 hw->aq.asq_last_status = (enum i40e_admin_queue_err)retval;
826 }
827
Shannon Nelsone3effd72014-07-09 07:46:19 +0000828 i40e_debug(hw, I40E_DEBUG_AQ_MESSAGE,
829 "AQTX: desc and buffer writeback:\n");
Shannon Nelsonf905dd62014-07-10 07:58:20 +0000830 i40evf_debug_aq(hw, I40E_DEBUG_AQ_COMMAND, (void *)desc, buff,
831 buff_size);
Kamil Krawczyk66d90e72014-06-04 00:57:12 +0000832
Greg Rosed358aa92013-12-21 06:13:11 +0000833 /* update the error if time out occurred */
834 if ((!cmd_completed) &&
835 (!details->async && !details->postpone)) {
836 i40e_debug(hw,
837 I40E_DEBUG_AQ_MESSAGE,
838 "AQTX: Writeback timeout.\n");
839 status = I40E_ERR_ADMIN_QUEUE_TIMEOUT;
840 }
841
842asq_send_command_error:
843 mutex_unlock(&hw->aq.asq_mutex);
844asq_send_command_exit:
845 return status;
846}
847
848/**
849 * i40evf_fill_default_direct_cmd_desc - AQ descriptor helper function
850 * @desc: pointer to the temp descriptor (non DMA mem)
851 * @opcode: the opcode can be used to decide which flags to turn off or on
852 *
853 * Fill the desc with default values
854 **/
855void i40evf_fill_default_direct_cmd_desc(struct i40e_aq_desc *desc,
856 u16 opcode)
857{
858 /* zero out the desc */
859 memset((void *)desc, 0, sizeof(struct i40e_aq_desc));
860 desc->opcode = cpu_to_le16(opcode);
861 desc->flags = cpu_to_le16(I40E_AQ_FLAG_SI);
862}
863
864/**
865 * i40evf_clean_arq_element
866 * @hw: pointer to the hw struct
867 * @e: event info from the receive descriptor, includes any buffers
868 * @pending: number of events that could be left to process
869 *
870 * This function cleans one Admin Receive Queue element and returns
871 * the contents through e. It can also return how many events are
872 * left to process through 'pending'
873 **/
874i40e_status i40evf_clean_arq_element(struct i40e_hw *hw,
875 struct i40e_arq_event_info *e,
876 u16 *pending)
877{
878 i40e_status ret_code = 0;
879 u16 ntc = hw->aq.arq.next_to_clean;
880 struct i40e_aq_desc *desc;
881 struct i40e_dma_mem *bi;
882 u16 desc_idx;
883 u16 datalen;
884 u16 flags;
885 u16 ntu;
886
887 /* take the lock before we start messing with the ring */
888 mutex_lock(&hw->aq.arq_mutex);
889
890 /* set next_to_use to head */
Anjali Singhai Jaine02a7f82015-07-10 19:36:06 -0400891 ntu = (rd32(hw, hw->aq.arq.head) & I40E_VF_ARQH1_ARQH_MASK);
Greg Rosed358aa92013-12-21 06:13:11 +0000892 if (ntu == ntc) {
893 /* nothing to do - shouldn't need to update ring's values */
Greg Rosed358aa92013-12-21 06:13:11 +0000894 ret_code = I40E_ERR_ADMIN_QUEUE_NO_WORK;
895 goto clean_arq_element_out;
896 }
897
898 /* now clean the next descriptor */
899 desc = I40E_ADMINQ_DESC(hw->aq.arq, ntc);
900 desc_idx = ntc;
Greg Rosed358aa92013-12-21 06:13:11 +0000901
902 flags = le16_to_cpu(desc->flags);
903 if (flags & I40E_AQ_FLAG_ERR) {
904 ret_code = I40E_ERR_ADMIN_QUEUE_ERROR;
905 hw->aq.arq_last_status =
906 (enum i40e_admin_queue_err)le16_to_cpu(desc->retval);
907 i40e_debug(hw,
908 I40E_DEBUG_AQ_MESSAGE,
909 "AQRX: Event received with error 0x%X.\n",
910 hw->aq.arq_last_status);
Greg Rosed358aa92013-12-21 06:13:11 +0000911 }
912
Kamil Krawczyk77813d02014-07-09 07:46:15 +0000913 e->desc = *desc;
914 datalen = le16_to_cpu(desc->datalen);
Mitch Williams1001dc32014-11-11 20:02:19 +0000915 e->msg_len = min(datalen, e->buf_len);
916 if (e->msg_buf != NULL && (e->msg_len != 0))
Kamil Krawczyk77813d02014-07-09 07:46:15 +0000917 memcpy(e->msg_buf, hw->aq.arq.r.arq_bi[desc_idx].va,
Mitch Williams1001dc32014-11-11 20:02:19 +0000918 e->msg_len);
Kamil Krawczyk77813d02014-07-09 07:46:15 +0000919
Kamil Krawczyk66d90e72014-06-04 00:57:12 +0000920 i40e_debug(hw, I40E_DEBUG_AQ_MESSAGE, "AQRX: desc and buffer:\n");
Shannon Nelsonf905dd62014-07-10 07:58:20 +0000921 i40evf_debug_aq(hw, I40E_DEBUG_AQ_COMMAND, (void *)desc, e->msg_buf,
922 hw->aq.arq_buf_size);
Kamil Krawczyk66d90e72014-06-04 00:57:12 +0000923
Greg Rosed358aa92013-12-21 06:13:11 +0000924 /* Restore the original datalen and buffer address in the desc,
925 * FW updates datalen to indicate the event message
926 * size
927 */
928 bi = &hw->aq.arq.r.arq_bi[ntc];
929 memset((void *)desc, 0, sizeof(struct i40e_aq_desc));
930
931 desc->flags = cpu_to_le16(I40E_AQ_FLAG_BUF);
932 if (hw->aq.arq_buf_size > I40E_AQ_LARGE_BUF)
933 desc->flags |= cpu_to_le16(I40E_AQ_FLAG_LB);
934 desc->datalen = cpu_to_le16((u16)bi->size);
935 desc->params.external.addr_high = cpu_to_le32(upper_32_bits(bi->pa));
936 desc->params.external.addr_low = cpu_to_le32(lower_32_bits(bi->pa));
937
938 /* set tail = the last cleaned desc index. */
939 wr32(hw, hw->aq.arq.tail, ntc);
940 /* ntc is updated to tail + 1 */
941 ntc++;
942 if (ntc == hw->aq.num_arq_entries)
943 ntc = 0;
944 hw->aq.arq.next_to_clean = ntc;
945 hw->aq.arq.next_to_use = ntu;
946
947clean_arq_element_out:
948 /* Set pending if needed, unlock and return */
949 if (pending != NULL)
950 *pending = (ntc > ntu ? hw->aq.arq.count : 0) + (ntu - ntc);
951 mutex_unlock(&hw->aq.arq_mutex);
952
953 return ret_code;
954}
955
956void i40evf_resume_aq(struct i40e_hw *hw)
957{
958 /* Registers are reset after PF reset */
959 hw->aq.asq.next_to_use = 0;
960 hw->aq.asq.next_to_clean = 0;
961
962 i40e_config_asq_regs(hw);
963
964 hw->aq.arq.next_to_use = 0;
965 hw->aq.arq.next_to_clean = 0;
966
967 i40e_config_arq_regs(hw);
968}