blob: f75b5733f34c77474e6f699f36e1e744d347b558 [file] [log] [blame]
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +00001/*******************************************************************************
2 *
3 * Intel Ethernet Controller XL710 Family Linux Driver
4 * Copyright(c) 2013 Intel Corporation.
5 *
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 *
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.
18 *
19 * The full GNU General Public License is included in this distribution in
20 * the file called "COPYING".
21 *
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
25 *
26 ******************************************************************************/
27
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"
33
Stephen Hemmingeraf28eec2013-12-13 04:37:50 +000034static void i40e_resume_aq(struct i40e_hw *hw);
35
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +000036/**
37 * i40e_adminq_init_regs - Initialize AdminQ registers
38 * @hw: pointer to the hardware structure
39 *
40 * This assumes the alloc_asq and alloc_arq functions have already been called
41 **/
42static void i40e_adminq_init_regs(struct i40e_hw *hw)
43{
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;
Shannon Nelson17e6a842013-11-16 10:00:36 +000048 hw->aq.asq.len = I40E_VF_ATQLEN1;
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +000049 hw->aq.arq.tail = I40E_VF_ARQT1;
50 hw->aq.arq.head = I40E_VF_ARQH1;
Shannon Nelson17e6a842013-11-16 10:00:36 +000051 hw->aq.arq.len = I40E_VF_ARQLEN1;
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +000052 } else {
53 hw->aq.asq.tail = I40E_PF_ATQT;
54 hw->aq.asq.head = I40E_PF_ATQH;
Shannon Nelson17e6a842013-11-16 10:00:36 +000055 hw->aq.asq.len = I40E_PF_ATQLEN;
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +000056 hw->aq.arq.tail = I40E_PF_ARQT;
57 hw->aq.arq.head = I40E_PF_ARQH;
Shannon Nelson17e6a842013-11-16 10:00:36 +000058 hw->aq.arq.len = I40E_PF_ARQLEN;
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +000059 }
60}
61
62/**
63 * i40e_alloc_adminq_asq_ring - Allocate Admin Queue send rings
64 * @hw: pointer to the hardware structure
65 **/
66static i40e_status i40e_alloc_adminq_asq_ring(struct i40e_hw *hw)
67{
68 i40e_status ret_code;
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +000069
David Cassard90bb7762013-11-28 06:39:35 +000070 ret_code = i40e_allocate_dma_mem(hw, &hw->aq.asq.desc_buf,
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +000071 i40e_mem_atq_ring,
72 (hw->aq.num_asq_entries *
73 sizeof(struct i40e_aq_desc)),
74 I40E_ADMINQ_DESC_ALIGNMENT);
75 if (ret_code)
76 return ret_code;
77
David Cassard90bb7762013-11-28 06:39:35 +000078 ret_code = i40e_allocate_virt_mem(hw, &hw->aq.asq.cmd_buf,
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +000079 (hw->aq.num_asq_entries *
80 sizeof(struct i40e_asq_cmd_details)));
81 if (ret_code) {
David Cassard90bb7762013-11-28 06:39:35 +000082 i40e_free_dma_mem(hw, &hw->aq.asq.desc_buf);
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +000083 return ret_code;
84 }
85
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +000086 return ret_code;
87}
88
89/**
90 * i40e_alloc_adminq_arq_ring - Allocate Admin Queue receive rings
91 * @hw: pointer to the hardware structure
92 **/
93static i40e_status i40e_alloc_adminq_arq_ring(struct i40e_hw *hw)
94{
95 i40e_status ret_code;
96
David Cassard90bb7762013-11-28 06:39:35 +000097 ret_code = i40e_allocate_dma_mem(hw, &hw->aq.arq.desc_buf,
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +000098 i40e_mem_arq_ring,
99 (hw->aq.num_arq_entries *
100 sizeof(struct i40e_aq_desc)),
101 I40E_ADMINQ_DESC_ALIGNMENT);
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000102
103 return ret_code;
104}
105
106/**
107 * i40e_free_adminq_asq - Free Admin Queue send rings
108 * @hw: pointer to the hardware structure
109 *
110 * This assumes the posted send buffers have already been cleaned
111 * and de-allocated
112 **/
113static void i40e_free_adminq_asq(struct i40e_hw *hw)
114{
David Cassard90bb7762013-11-28 06:39:35 +0000115 i40e_free_dma_mem(hw, &hw->aq.asq.desc_buf);
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000116}
117
118/**
119 * i40e_free_adminq_arq - Free Admin Queue receive rings
120 * @hw: pointer to the hardware structure
121 *
122 * This assumes the posted receive buffers have already been cleaned
123 * and de-allocated
124 **/
125static void i40e_free_adminq_arq(struct i40e_hw *hw)
126{
David Cassard90bb7762013-11-28 06:39:35 +0000127 i40e_free_dma_mem(hw, &hw->aq.arq.desc_buf);
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000128}
129
130/**
131 * i40e_alloc_arq_bufs - Allocate pre-posted buffers for the receive queue
132 * @hw: pointer to the hardware structure
133 **/
134static i40e_status i40e_alloc_arq_bufs(struct i40e_hw *hw)
135{
136 i40e_status ret_code;
137 struct i40e_aq_desc *desc;
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000138 struct i40e_dma_mem *bi;
139 int i;
140
141 /* We'll be allocating the buffer info memory first, then we can
142 * allocate the mapped buffers for the event processing
143 */
144
145 /* buffer_info structures do not need alignment */
David Cassard90bb7762013-11-28 06:39:35 +0000146 ret_code = i40e_allocate_virt_mem(hw, &hw->aq.arq.dma_head,
147 (hw->aq.num_arq_entries * sizeof(struct i40e_dma_mem)));
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000148 if (ret_code)
149 goto alloc_arq_bufs;
David Cassard90bb7762013-11-28 06:39:35 +0000150 hw->aq.arq.r.arq_bi = (struct i40e_dma_mem *)hw->aq.arq.dma_head.va;
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000151
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,
156 i40e_mem_arq_buf,
157 hw->aq.arq_buf_size,
158 I40E_ADMINQ_DESC_ALIGNMENT);
159 if (ret_code)
160 goto unwind_alloc_arq_bufs;
161
162 /* now configure the descriptors for use */
163 desc = I40E_ADMINQ_DESC(hw->aq.arq, i);
164
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);
168 desc->opcode = 0;
169 /* This is in accordance with Admin queue design, there is no
170 * register for buffer size configuration
171 */
172 desc->datalen = cpu_to_le16((u16)bi->size);
173 desc->retval = 0;
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;
182 }
183
184alloc_arq_bufs:
185 return ret_code;
186
187unwind_alloc_arq_bufs:
188 /* don't try to free the one that failed... */
189 i--;
190 for (; i >= 0; i--)
191 i40e_free_dma_mem(hw, &hw->aq.arq.r.arq_bi[i]);
David Cassard90bb7762013-11-28 06:39:35 +0000192 i40e_free_virt_mem(hw, &hw->aq.arq.dma_head);
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000193
194 return ret_code;
195}
196
197/**
198 * i40e_alloc_asq_bufs - Allocate empty buffer structs for the send queue
199 * @hw: pointer to the hardware structure
200 **/
201static i40e_status i40e_alloc_asq_bufs(struct i40e_hw *hw)
202{
203 i40e_status ret_code;
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000204 struct i40e_dma_mem *bi;
205 int i;
206
207 /* No mapped memory needed yet, just the buffer info structures */
David Cassard90bb7762013-11-28 06:39:35 +0000208 ret_code = i40e_allocate_virt_mem(hw, &hw->aq.asq.dma_head,
209 (hw->aq.num_asq_entries * sizeof(struct i40e_dma_mem)));
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000210 if (ret_code)
211 goto alloc_asq_bufs;
David Cassard90bb7762013-11-28 06:39:35 +0000212 hw->aq.asq.r.asq_bi = (struct i40e_dma_mem *)hw->aq.asq.dma_head.va;
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000213
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,
218 i40e_mem_asq_buf,
219 hw->aq.asq_buf_size,
220 I40E_ADMINQ_DESC_ALIGNMENT);
221 if (ret_code)
222 goto unwind_alloc_asq_bufs;
223 }
224alloc_asq_bufs:
225 return ret_code;
226
227unwind_alloc_asq_bufs:
228 /* don't try to free the one that failed... */
229 i--;
230 for (; i >= 0; i--)
231 i40e_free_dma_mem(hw, &hw->aq.asq.r.asq_bi[i]);
David Cassard90bb7762013-11-28 06:39:35 +0000232 i40e_free_virt_mem(hw, &hw->aq.asq.dma_head);
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000233
234 return ret_code;
235}
236
237/**
238 * i40e_free_arq_bufs - Free receive queue buffer info elements
239 * @hw: pointer to the hardware structure
240 **/
241static void i40e_free_arq_bufs(struct i40e_hw *hw)
242{
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000243 int i;
244
David Cassard90bb7762013-11-28 06:39:35 +0000245 /* free descriptors */
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000246 for (i = 0; i < hw->aq.num_arq_entries; i++)
247 i40e_free_dma_mem(hw, &hw->aq.arq.r.arq_bi[i]);
248
David Cassard90bb7762013-11-28 06:39:35 +0000249 /* free the descriptor memory */
250 i40e_free_dma_mem(hw, &hw->aq.arq.desc_buf);
251
252 /* free the dma header */
253 i40e_free_virt_mem(hw, &hw->aq.arq.dma_head);
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000254}
255
256/**
257 * i40e_free_asq_bufs - Free send queue buffer info elements
258 * @hw: pointer to the hardware structure
259 **/
260static void i40e_free_asq_bufs(struct i40e_hw *hw)
261{
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000262 int i;
263
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]);
268
David Cassard90bb7762013-11-28 06:39:35 +0000269 /* free the buffer info list */
270 i40e_free_virt_mem(hw, &hw->aq.asq.cmd_buf);
271
272 /* free the descriptor memory */
273 i40e_free_dma_mem(hw, &hw->aq.asq.desc_buf);
274
275 /* free the dma header */
276 i40e_free_virt_mem(hw, &hw->aq.asq.dma_head);
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000277}
278
279/**
280 * i40e_config_asq_regs - configure ASQ registers
281 * @hw: pointer to the hardware structure
282 *
283 * Configure base address and length registers for the transmit queue
284 **/
285static void i40e_config_asq_regs(struct i40e_hw *hw)
286{
287 if (hw->mac.type == I40E_MAC_VF) {
288 /* configure the transmit queue */
David Cassard90bb7762013-11-28 06:39:35 +0000289 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));
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000293 wr32(hw, I40E_VF_ATQLEN1, (hw->aq.num_asq_entries |
294 I40E_VF_ATQLEN1_ATQENABLE_MASK));
295 } else {
296 /* configure the transmit queue */
David Cassard90bb7762013-11-28 06:39:35 +0000297 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));
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000301 wr32(hw, I40E_PF_ATQLEN, (hw->aq.num_asq_entries |
302 I40E_PF_ATQLEN_ATQENABLE_MASK));
303 }
304}
305
306/**
307 * i40e_config_arq_regs - ARQ register configuration
308 * @hw: pointer to the hardware structure
309 *
310 * Configure base address and length registers for the receive (event queue)
311 **/
312static void i40e_config_arq_regs(struct i40e_hw *hw)
313{
314 if (hw->mac.type == I40E_MAC_VF) {
315 /* configure the receive queue */
David Cassard90bb7762013-11-28 06:39:35 +0000316 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));
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000320 wr32(hw, I40E_VF_ARQLEN1, (hw->aq.num_arq_entries |
321 I40E_VF_ARQLEN1_ARQENABLE_MASK));
322 } else {
323 /* configure the receive queue */
David Cassard90bb7762013-11-28 06:39:35 +0000324 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));
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000328 wr32(hw, I40E_PF_ARQLEN, (hw->aq.num_arq_entries |
329 I40E_PF_ARQLEN_ARQENABLE_MASK));
330 }
331
332 /* Update tail in the HW to post pre-allocated buffers */
333 wr32(hw, hw->aq.arq.tail, hw->aq.num_arq_entries - 1);
334}
335
336/**
337 * i40e_init_asq - main initialization routine for ASQ
338 * @hw: pointer to the hardware structure
339 *
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
345 *
346 * Do *NOT* hold the lock when calling this as the memory allocation routines
347 * called are not going to be atomic context safe
348 **/
349static i40e_status i40e_init_asq(struct i40e_hw *hw)
350{
351 i40e_status ret_code = 0;
352
353 if (hw->aq.asq.count > 0) {
354 /* queue already initialized */
355 ret_code = I40E_ERR_NOT_READY;
356 goto init_adminq_exit;
357 }
358
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;
364 }
365
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;
369
370 /* allocate the ring memory */
371 ret_code = i40e_alloc_adminq_asq_ring(hw);
372 if (ret_code)
373 goto init_adminq_exit;
374
375 /* allocate buffers in the rings */
376 ret_code = i40e_alloc_asq_bufs(hw);
377 if (ret_code)
378 goto init_adminq_free_rings;
379
380 /* initialize base registers */
381 i40e_config_asq_regs(hw);
382
383 /* success! */
384 goto init_adminq_exit;
385
386init_adminq_free_rings:
387 i40e_free_adminq_asq(hw);
388
389init_adminq_exit:
390 return ret_code;
391}
392
393/**
394 * i40e_init_arq - initialize ARQ
395 * @hw: pointer to the hardware structure
396 *
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
402 *
403 * Do *NOT* hold the lock when calling this as the memory allocation routines
404 * called are not going to be atomic context safe
405 **/
406static i40e_status i40e_init_arq(struct i40e_hw *hw)
407{
408 i40e_status ret_code = 0;
409
410 if (hw->aq.arq.count > 0) {
411 /* queue already initialized */
412 ret_code = I40E_ERR_NOT_READY;
413 goto init_adminq_exit;
414 }
415
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;
421 }
422
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;
426
427 /* allocate the ring memory */
428 ret_code = i40e_alloc_adminq_arq_ring(hw);
429 if (ret_code)
430 goto init_adminq_exit;
431
432 /* allocate buffers in the rings */
433 ret_code = i40e_alloc_arq_bufs(hw);
434 if (ret_code)
435 goto init_adminq_free_rings;
436
437 /* initialize base registers */
438 i40e_config_arq_regs(hw);
439
440 /* success! */
441 goto init_adminq_exit;
442
443init_adminq_free_rings:
444 i40e_free_adminq_arq(hw);
445
446init_adminq_exit:
447 return ret_code;
448}
449
450/**
451 * i40e_shutdown_asq - shutdown the ASQ
452 * @hw: pointer to the hardware structure
453 *
454 * The main shutdown routine for the Admin Send Queue
455 **/
456static i40e_status i40e_shutdown_asq(struct i40e_hw *hw)
457{
458 i40e_status ret_code = 0;
459
460 if (hw->aq.asq.count == 0)
461 return I40E_ERR_NOT_READY;
462
463 /* Stop firmware AdminQ processing */
Shannon Nelson17e6a842013-11-16 10:00:36 +0000464 wr32(hw, hw->aq.asq.head, 0);
465 wr32(hw, hw->aq.asq.tail, 0);
466 wr32(hw, hw->aq.asq.len, 0);
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000467
468 /* make sure lock is available */
469 mutex_lock(&hw->aq.asq_mutex);
470
471 hw->aq.asq.count = 0; /* to indicate uninitialized queue */
472
473 /* free ring buffers */
474 i40e_free_asq_bufs(hw);
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000475
476 mutex_unlock(&hw->aq.asq_mutex);
477
478 return ret_code;
479}
480
481/**
482 * i40e_shutdown_arq - shutdown ARQ
483 * @hw: pointer to the hardware structure
484 *
485 * The main shutdown routine for the Admin Receive Queue
486 **/
487static i40e_status i40e_shutdown_arq(struct i40e_hw *hw)
488{
489 i40e_status ret_code = 0;
490
491 if (hw->aq.arq.count == 0)
492 return I40E_ERR_NOT_READY;
493
494 /* Stop firmware AdminQ processing */
Shannon Nelson17e6a842013-11-16 10:00:36 +0000495 wr32(hw, hw->aq.arq.head, 0);
496 wr32(hw, hw->aq.arq.tail, 0);
497 wr32(hw, hw->aq.arq.len, 0);
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000498
499 /* make sure lock is available */
500 mutex_lock(&hw->aq.arq_mutex);
501
502 hw->aq.arq.count = 0; /* to indicate uninitialized queue */
503
504 /* free ring buffers */
505 i40e_free_arq_bufs(hw);
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000506
507 mutex_unlock(&hw->aq.arq_mutex);
508
509 return ret_code;
510}
511
512/**
513 * i40e_init_adminq - main initialization routine for Admin Queue
514 * @hw: pointer to the hardware structure
515 *
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
522 **/
523i40e_status i40e_init_adminq(struct i40e_hw *hw)
524{
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000525 i40e_status ret_code;
Shannon Nelsond4946cf2013-11-16 10:00:40 +0000526 u16 eetrack_lo, eetrack_hi;
527 int retry = 0;
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000528
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;
536 }
537
538 /* initialize locks */
539 mutex_init(&hw->aq.asq_mutex);
540 mutex_init(&hw->aq.arq_mutex);
541
542 /* Set up register offsets */
543 i40e_adminq_init_regs(hw);
544
545 /* allocate the ASQ */
546 ret_code = i40e_init_asq(hw);
547 if (ret_code)
548 goto init_adminq_destroy_locks;
549
550 /* allocate the ARQ */
551 ret_code = i40e_init_arq(hw);
552 if (ret_code)
553 goto init_adminq_free_asq;
554
Shannon Nelsond4946cf2013-11-16 10:00:40 +0000555 /* 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.
558 */
559 do {
560 ret_code = i40e_aq_get_firmware_version(hw,
561 &hw->aq.fw_maj_ver,
562 &hw->aq.fw_min_ver,
563 &hw->aq.api_maj_ver,
564 &hw->aq.api_min_ver,
565 NULL);
566 if (ret_code != I40E_ERR_ADMIN_QUEUE_TIMEOUT)
567 break;
568 retry++;
569 msleep(100);
570 i40e_resume_aq(hw);
571 } while (retry < 10);
572 if (ret_code != I40E_SUCCESS)
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000573 goto init_adminq_free_arq;
574
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;
579 }
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;
584
585 ret_code = i40e_aq_set_hmc_resource_profile(hw,
586 I40E_HMC_PROFILE_DEFAULT,
587 0,
588 NULL);
589 ret_code = 0;
590
591 /* success! */
592 goto init_adminq_exit;
593
594init_adminq_free_arq:
595 i40e_shutdown_arq(hw);
596init_adminq_free_asq:
597 i40e_shutdown_asq(hw);
598init_adminq_destroy_locks:
599
600init_adminq_exit:
601 return ret_code;
602}
603
604/**
605 * i40e_shutdown_adminq - shutdown routine for the Admin Queue
606 * @hw: pointer to the hardware structure
607 **/
608i40e_status i40e_shutdown_adminq(struct i40e_hw *hw)
609{
610 i40e_status ret_code = 0;
611
Anjali Singhai Jaine1860d82013-11-28 06:39:45 +0000612 if (i40e_check_asq_alive(hw))
613 i40e_aq_queue_shutdown(hw, true);
614
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000615 i40e_shutdown_asq(hw);
616 i40e_shutdown_arq(hw);
617
618 /* destroy the locks */
619
620 return ret_code;
621}
622
623/**
624 * i40e_clean_asq - cleans Admin send queue
625 * @asq: pointer to the adminq send ring
626 *
627 * returns the number of free desc
628 **/
629static u16 i40e_clean_asq(struct i40e_hw *hw)
630{
631 struct i40e_adminq_ring *asq = &(hw->aq.asq);
632 struct i40e_asq_cmd_details *details;
633 u16 ntc = asq->next_to_clean;
634 struct i40e_aq_desc desc_cb;
635 struct i40e_aq_desc *desc;
636
637 desc = I40E_ADMINQ_DESC(*asq, ntc);
638 details = I40E_ADMINQ_DETAILS(*asq, ntc);
639 while (rd32(hw, hw->aq.asq.head) != ntc) {
640 if (details->callback) {
641 I40E_ADMINQ_CALLBACK cb_func =
642 (I40E_ADMINQ_CALLBACK)details->callback;
643 desc_cb = *desc;
644 cb_func(hw, &desc_cb);
645 }
646 memset((void *)desc, 0, sizeof(struct i40e_aq_desc));
647 memset((void *)details, 0,
648 sizeof(struct i40e_asq_cmd_details));
649 ntc++;
650 if (ntc == asq->count)
651 ntc = 0;
652 desc = I40E_ADMINQ_DESC(*asq, ntc);
653 details = I40E_ADMINQ_DETAILS(*asq, ntc);
654 }
655
656 asq->next_to_clean = ntc;
657
658 return I40E_DESC_UNUSED(asq);
659}
660
661/**
662 * i40e_asq_done - check if FW has processed the Admin Send Queue
663 * @hw: pointer to the hw struct
664 *
665 * Returns true if the firmware has processed all descriptors on the
666 * admin send queue. Returns false if there are still requests pending.
667 **/
Stephen Hemmingeraf28eec2013-12-13 04:37:50 +0000668static bool i40e_asq_done(struct i40e_hw *hw)
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000669{
670 /* AQ designers suggest use of head for better
671 * timing reliability than DD bit
672 */
673 return (rd32(hw, hw->aq.asq.head) == hw->aq.asq.next_to_use);
674
675}
676
677/**
678 * i40e_asq_send_command - send command to Admin Queue
679 * @hw: pointer to the hw struct
680 * @desc: prefilled descriptor describing the command (non DMA mem)
681 * @buff: buffer to use for indirect commands
682 * @buff_size: size of buffer for indirect commands
683 * @opaque: pointer to info to be used in async cleanup
684 *
685 * This is the main send command driver routine for the Admin Queue send
686 * queue. It runs the queue, cleans the queue, etc
687 **/
688i40e_status i40e_asq_send_command(struct i40e_hw *hw,
689 struct i40e_aq_desc *desc,
690 void *buff, /* can be NULL */
691 u16 buff_size,
692 struct i40e_asq_cmd_details *cmd_details)
693{
694 i40e_status status = 0;
695 struct i40e_dma_mem *dma_buff = NULL;
696 struct i40e_asq_cmd_details *details;
697 struct i40e_aq_desc *desc_on_ring;
698 bool cmd_completed = false;
699 u16 retval = 0;
700
701 if (hw->aq.asq.count == 0) {
702 i40e_debug(hw, I40E_DEBUG_AQ_MESSAGE,
703 "AQTX: Admin queue not initialized.\n");
704 status = I40E_ERR_QUEUE_EMPTY;
705 goto asq_send_command_exit;
706 }
707
708 details = I40E_ADMINQ_DETAILS(hw->aq.asq, hw->aq.asq.next_to_use);
709 if (cmd_details) {
Jesse Brandeburgd7595a22013-09-13 08:23:22 +0000710 *details = *cmd_details;
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000711
712 /* If the cmd_details are defined copy the cookie. The
713 * cpu_to_le32 is not needed here because the data is ignored
714 * by the FW, only used by the driver
715 */
716 if (details->cookie) {
717 desc->cookie_high =
718 cpu_to_le32(upper_32_bits(details->cookie));
719 desc->cookie_low =
720 cpu_to_le32(lower_32_bits(details->cookie));
721 }
722 } else {
723 memset(details, 0, sizeof(struct i40e_asq_cmd_details));
724 }
725
726 /* clear requested flags and then set additional flags if defined */
727 desc->flags &= ~cpu_to_le16(details->flags_dis);
728 desc->flags |= cpu_to_le16(details->flags_ena);
729
730 mutex_lock(&hw->aq.asq_mutex);
731
732 if (buff_size > hw->aq.asq_buf_size) {
733 i40e_debug(hw,
734 I40E_DEBUG_AQ_MESSAGE,
735 "AQTX: Invalid buffer size: %d.\n",
736 buff_size);
737 status = I40E_ERR_INVALID_SIZE;
738 goto asq_send_command_error;
739 }
740
741 if (details->postpone && !details->async) {
742 i40e_debug(hw,
743 I40E_DEBUG_AQ_MESSAGE,
744 "AQTX: Async flag not set along with postpone flag");
745 status = I40E_ERR_PARAM;
746 goto asq_send_command_error;
747 }
748
749 /* call clean and check queue available function to reclaim the
750 * descriptors that were processed by FW, the function returns the
751 * number of desc available
752 */
753 /* the clean function called here could be called in a separate thread
754 * in case of asynchronous completions
755 */
756 if (i40e_clean_asq(hw) == 0) {
757 i40e_debug(hw,
758 I40E_DEBUG_AQ_MESSAGE,
759 "AQTX: Error queue is full.\n");
760 status = I40E_ERR_ADMIN_QUEUE_FULL;
761 goto asq_send_command_error;
762 }
763
764 /* initialize the temp desc pointer with the right desc */
765 desc_on_ring = I40E_ADMINQ_DESC(hw->aq.asq, hw->aq.asq.next_to_use);
766
767 /* if the desc is available copy the temp desc to the right place */
Jesse Brandeburgd7595a22013-09-13 08:23:22 +0000768 *desc_on_ring = *desc;
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000769
770 /* if buff is not NULL assume indirect command */
771 if (buff != NULL) {
772 dma_buff = &(hw->aq.asq.r.asq_bi[hw->aq.asq.next_to_use]);
773 /* copy the user buff into the respective DMA buff */
774 memcpy(dma_buff->va, buff, buff_size);
775 desc_on_ring->datalen = cpu_to_le16(buff_size);
776
777 /* Update the address values in the desc with the pa value
778 * for respective buffer
779 */
780 desc_on_ring->params.external.addr_high =
781 cpu_to_le32(upper_32_bits(dma_buff->pa));
782 desc_on_ring->params.external.addr_low =
783 cpu_to_le32(lower_32_bits(dma_buff->pa));
784 }
785
786 /* bump the tail */
787 i40e_debug_aq(hw, I40E_DEBUG_AQ_COMMAND, (void *)desc_on_ring, buff);
788 (hw->aq.asq.next_to_use)++;
789 if (hw->aq.asq.next_to_use == hw->aq.asq.count)
790 hw->aq.asq.next_to_use = 0;
791 if (!details->postpone)
792 wr32(hw, hw->aq.asq.tail, hw->aq.asq.next_to_use);
793
794 /* if cmd_details are not defined or async flag is not set,
795 * we need to wait for desc write back
796 */
797 if (!details->async && !details->postpone) {
798 u32 total_delay = 0;
799 u32 delay_len = 10;
800
801 do {
802 /* AQ designers suggest use of head for better
803 * timing reliability than DD bit
804 */
805 if (i40e_asq_done(hw))
806 break;
807 /* ugh! delay while spin_lock */
808 udelay(delay_len);
809 total_delay += delay_len;
810 } while (total_delay < I40E_ASQ_CMD_TIMEOUT);
811 }
812
813 /* if ready, copy the desc back to temp */
814 if (i40e_asq_done(hw)) {
Jesse Brandeburgd7595a22013-09-13 08:23:22 +0000815 *desc = *desc_on_ring;
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000816 if (buff != NULL)
817 memcpy(buff, dma_buff->va, buff_size);
818 retval = le16_to_cpu(desc->retval);
819 if (retval != 0) {
820 i40e_debug(hw,
821 I40E_DEBUG_AQ_MESSAGE,
822 "AQTX: Command completed with error 0x%X.\n",
823 retval);
824 /* strip off FW internal code */
825 retval &= 0xff;
826 }
827 cmd_completed = true;
828 if ((enum i40e_admin_queue_err)retval == I40E_AQ_RC_OK)
829 status = 0;
830 else
831 status = I40E_ERR_ADMIN_QUEUE_ERROR;
832 hw->aq.asq_last_status = (enum i40e_admin_queue_err)retval;
833 }
834
835 /* update the error if time out occurred */
836 if ((!cmd_completed) &&
837 (!details->async && !details->postpone)) {
838 i40e_debug(hw,
839 I40E_DEBUG_AQ_MESSAGE,
840 "AQTX: Writeback timeout.\n");
841 status = I40E_ERR_ADMIN_QUEUE_TIMEOUT;
842 }
843
844asq_send_command_error:
845 mutex_unlock(&hw->aq.asq_mutex);
846asq_send_command_exit:
847 return status;
848}
849
850/**
851 * i40e_fill_default_direct_cmd_desc - AQ descriptor helper function
852 * @desc: pointer to the temp descriptor (non DMA mem)
853 * @opcode: the opcode can be used to decide which flags to turn off or on
854 *
855 * Fill the desc with default values
856 **/
857void i40e_fill_default_direct_cmd_desc(struct i40e_aq_desc *desc,
858 u16 opcode)
859{
860 /* zero out the desc */
861 memset((void *)desc, 0, sizeof(struct i40e_aq_desc));
862 desc->opcode = cpu_to_le16(opcode);
863 desc->flags = cpu_to_le16(I40E_AQ_FLAG_EI | I40E_AQ_FLAG_SI);
864}
865
866/**
867 * i40e_clean_arq_element
868 * @hw: pointer to the hw struct
869 * @e: event info from the receive descriptor, includes any buffers
870 * @pending: number of events that could be left to process
871 *
872 * This function cleans one Admin Receive Queue element and returns
873 * the contents through e. It can also return how many events are
874 * left to process through 'pending'
875 **/
876i40e_status i40e_clean_arq_element(struct i40e_hw *hw,
877 struct i40e_arq_event_info *e,
878 u16 *pending)
879{
880 i40e_status ret_code = 0;
881 u16 ntc = hw->aq.arq.next_to_clean;
882 struct i40e_aq_desc *desc;
883 struct i40e_dma_mem *bi;
884 u16 desc_idx;
885 u16 datalen;
886 u16 flags;
887 u16 ntu;
888
889 /* take the lock before we start messing with the ring */
890 mutex_lock(&hw->aq.arq_mutex);
891
892 /* set next_to_use to head */
893 ntu = (rd32(hw, hw->aq.arq.head) & I40E_PF_ARQH_ARQH_MASK);
894 if (ntu == ntc) {
895 /* nothing to do - shouldn't need to update ring's values */
896 i40e_debug(hw,
897 I40E_DEBUG_AQ_MESSAGE,
898 "AQRX: Queue is empty.\n");
899 ret_code = I40E_ERR_ADMIN_QUEUE_NO_WORK;
900 goto clean_arq_element_out;
901 }
902
903 /* now clean the next descriptor */
904 desc = I40E_ADMINQ_DESC(hw->aq.arq, ntc);
905 desc_idx = ntc;
906 i40e_debug_aq(hw,
907 I40E_DEBUG_AQ_COMMAND,
908 (void *)desc,
909 hw->aq.arq.r.arq_bi[desc_idx].va);
910
911 flags = le16_to_cpu(desc->flags);
912 if (flags & I40E_AQ_FLAG_ERR) {
913 ret_code = I40E_ERR_ADMIN_QUEUE_ERROR;
914 hw->aq.arq_last_status =
915 (enum i40e_admin_queue_err)le16_to_cpu(desc->retval);
916 i40e_debug(hw,
917 I40E_DEBUG_AQ_MESSAGE,
918 "AQRX: Event received with error 0x%X.\n",
919 hw->aq.arq_last_status);
920 } else {
921 memcpy(&e->desc, desc, sizeof(struct i40e_aq_desc));
922 datalen = le16_to_cpu(desc->datalen);
923 e->msg_size = min(datalen, e->msg_size);
924 if (e->msg_buf != NULL && (e->msg_size != 0))
925 memcpy(e->msg_buf, hw->aq.arq.r.arq_bi[desc_idx].va,
926 e->msg_size);
927 }
928
929 /* Restore the original datalen and buffer address in the desc,
930 * FW updates datalen to indicate the event message
931 * size
932 */
933 bi = &hw->aq.arq.r.arq_bi[ntc];
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
Stephen Hemmingeraf28eec2013-12-13 04:37:50 +0000956static void i40e_resume_aq(struct i40e_hw *hw)
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000957{
958 u32 reg = 0;
959
960 /* Registers are reset after PF reset */
961 hw->aq.asq.next_to_use = 0;
962 hw->aq.asq.next_to_clean = 0;
963
964 i40e_config_asq_regs(hw);
Shannon Nelson17e6a842013-11-16 10:00:36 +0000965 reg = hw->aq.num_asq_entries | I40E_PF_ATQLEN_ATQENABLE_MASK;
966 wr32(hw, hw->aq.asq.len, reg);
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000967
968 hw->aq.arq.next_to_use = 0;
969 hw->aq.arq.next_to_clean = 0;
970
971 i40e_config_arq_regs(hw);
Shannon Nelson17e6a842013-11-16 10:00:36 +0000972 reg = hw->aq.num_arq_entries | I40E_PF_ATQLEN_ATQENABLE_MASK;
973 wr32(hw, hw->aq.arq.len, reg);
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000974}