blob: a50e6b3479ae9c779475681634535f1c8181f309 [file] [log] [blame]
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +00001/*******************************************************************************
2 *
3 * Intel Ethernet Controller XL710 Family Linux Driver
Greg Rosedc641b72013-12-18 13:45:51 +00004 * Copyright(c) 2013 - 2014 Intel Corporation.
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +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 *
Greg Rosedc641b72013-12-18 13:45:51 +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/>.
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +000017 *
18 * 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
Stephen Hemmingeraf28eec2013-12-13 04:37:50 +000033static void i40e_resume_aq(struct i40e_hw *hw);
34
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +000035/**
36 * i40e_adminq_init_regs - Initialize AdminQ registers
37 * @hw: pointer to the hardware structure
38 *
39 * This assumes the alloc_asq and alloc_arq functions have already been called
40 **/
41static void i40e_adminq_init_regs(struct i40e_hw *hw)
42{
43 /* set head and tail registers in our local struct */
44 if (hw->mac.type == I40E_MAC_VF) {
45 hw->aq.asq.tail = I40E_VF_ATQT1;
46 hw->aq.asq.head = I40E_VF_ATQH1;
Shannon Nelson17e6a842013-11-16 10:00:36 +000047 hw->aq.asq.len = I40E_VF_ATQLEN1;
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +000048 hw->aq.arq.tail = I40E_VF_ARQT1;
49 hw->aq.arq.head = I40E_VF_ARQH1;
Shannon Nelson17e6a842013-11-16 10:00:36 +000050 hw->aq.arq.len = I40E_VF_ARQLEN1;
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +000051 } else {
52 hw->aq.asq.tail = I40E_PF_ATQT;
53 hw->aq.asq.head = I40E_PF_ATQH;
Shannon Nelson17e6a842013-11-16 10:00:36 +000054 hw->aq.asq.len = I40E_PF_ATQLEN;
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +000055 hw->aq.arq.tail = I40E_PF_ARQT;
56 hw->aq.arq.head = I40E_PF_ARQH;
Shannon Nelson17e6a842013-11-16 10:00:36 +000057 hw->aq.arq.len = I40E_PF_ARQLEN;
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +000058 }
59}
60
61/**
62 * i40e_alloc_adminq_asq_ring - Allocate Admin Queue send rings
63 * @hw: pointer to the hardware structure
64 **/
65static i40e_status i40e_alloc_adminq_asq_ring(struct i40e_hw *hw)
66{
67 i40e_status ret_code;
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +000068
David Cassard90bb7762013-11-28 06:39:35 +000069 ret_code = i40e_allocate_dma_mem(hw, &hw->aq.asq.desc_buf,
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +000070 i40e_mem_atq_ring,
71 (hw->aq.num_asq_entries *
72 sizeof(struct i40e_aq_desc)),
73 I40E_ADMINQ_DESC_ALIGNMENT);
74 if (ret_code)
75 return ret_code;
76
David Cassard90bb7762013-11-28 06:39:35 +000077 ret_code = i40e_allocate_virt_mem(hw, &hw->aq.asq.cmd_buf,
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +000078 (hw->aq.num_asq_entries *
79 sizeof(struct i40e_asq_cmd_details)));
80 if (ret_code) {
David Cassard90bb7762013-11-28 06:39:35 +000081 i40e_free_dma_mem(hw, &hw->aq.asq.desc_buf);
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +000082 return ret_code;
83 }
84
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +000085 return ret_code;
86}
87
88/**
89 * i40e_alloc_adminq_arq_ring - Allocate Admin Queue receive rings
90 * @hw: pointer to the hardware structure
91 **/
92static i40e_status i40e_alloc_adminq_arq_ring(struct i40e_hw *hw)
93{
94 i40e_status ret_code;
95
David Cassard90bb7762013-11-28 06:39:35 +000096 ret_code = i40e_allocate_dma_mem(hw, &hw->aq.arq.desc_buf,
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +000097 i40e_mem_arq_ring,
98 (hw->aq.num_arq_entries *
99 sizeof(struct i40e_aq_desc)),
100 I40E_ADMINQ_DESC_ALIGNMENT);
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000101
102 return ret_code;
103}
104
105/**
106 * i40e_free_adminq_asq - Free Admin Queue send rings
107 * @hw: pointer to the hardware structure
108 *
109 * This assumes the posted send buffers have already been cleaned
110 * and de-allocated
111 **/
112static void i40e_free_adminq_asq(struct i40e_hw *hw)
113{
David Cassard90bb7762013-11-28 06:39:35 +0000114 i40e_free_dma_mem(hw, &hw->aq.asq.desc_buf);
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000115}
116
117/**
118 * i40e_free_adminq_arq - Free Admin Queue receive rings
119 * @hw: pointer to the hardware structure
120 *
121 * This assumes the posted receive buffers have already been cleaned
122 * and de-allocated
123 **/
124static void i40e_free_adminq_arq(struct i40e_hw *hw)
125{
David Cassard90bb7762013-11-28 06:39:35 +0000126 i40e_free_dma_mem(hw, &hw->aq.arq.desc_buf);
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000127}
128
129/**
130 * i40e_alloc_arq_bufs - Allocate pre-posted buffers for the receive queue
Jeff Kirsher98d44382013-12-21 05:44:42 +0000131 * @hw: pointer to the hardware structure
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000132 **/
133static i40e_status i40e_alloc_arq_bufs(struct i40e_hw *hw)
134{
135 i40e_status ret_code;
136 struct i40e_aq_desc *desc;
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000137 struct i40e_dma_mem *bi;
138 int i;
139
140 /* We'll be allocating the buffer info memory first, then we can
141 * allocate the mapped buffers for the event processing
142 */
143
144 /* buffer_info structures do not need alignment */
David Cassard90bb7762013-11-28 06:39:35 +0000145 ret_code = i40e_allocate_virt_mem(hw, &hw->aq.arq.dma_head,
146 (hw->aq.num_arq_entries * sizeof(struct i40e_dma_mem)));
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000147 if (ret_code)
148 goto alloc_arq_bufs;
David Cassard90bb7762013-11-28 06:39:35 +0000149 hw->aq.arq.r.arq_bi = (struct i40e_dma_mem *)hw->aq.arq.dma_head.va;
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000150
151 /* allocate the mapped buffers */
152 for (i = 0; i < hw->aq.num_arq_entries; i++) {
153 bi = &hw->aq.arq.r.arq_bi[i];
154 ret_code = i40e_allocate_dma_mem(hw, bi,
155 i40e_mem_arq_buf,
156 hw->aq.arq_buf_size,
157 I40E_ADMINQ_DESC_ALIGNMENT);
158 if (ret_code)
159 goto unwind_alloc_arq_bufs;
160
161 /* now configure the descriptors for use */
162 desc = I40E_ADMINQ_DESC(hw->aq.arq, i);
163
164 desc->flags = cpu_to_le16(I40E_AQ_FLAG_BUF);
165 if (hw->aq.arq_buf_size > I40E_AQ_LARGE_BUF)
166 desc->flags |= cpu_to_le16(I40E_AQ_FLAG_LB);
167 desc->opcode = 0;
168 /* This is in accordance with Admin queue design, there is no
169 * register for buffer size configuration
170 */
171 desc->datalen = cpu_to_le16((u16)bi->size);
172 desc->retval = 0;
173 desc->cookie_high = 0;
174 desc->cookie_low = 0;
175 desc->params.external.addr_high =
176 cpu_to_le32(upper_32_bits(bi->pa));
177 desc->params.external.addr_low =
178 cpu_to_le32(lower_32_bits(bi->pa));
179 desc->params.external.param0 = 0;
180 desc->params.external.param1 = 0;
181 }
182
183alloc_arq_bufs:
184 return ret_code;
185
186unwind_alloc_arq_bufs:
187 /* don't try to free the one that failed... */
188 i--;
189 for (; i >= 0; i--)
190 i40e_free_dma_mem(hw, &hw->aq.arq.r.arq_bi[i]);
David Cassard90bb7762013-11-28 06:39:35 +0000191 i40e_free_virt_mem(hw, &hw->aq.arq.dma_head);
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000192
193 return ret_code;
194}
195
196/**
197 * i40e_alloc_asq_bufs - Allocate empty buffer structs for the send queue
Jeff Kirsher98d44382013-12-21 05:44:42 +0000198 * @hw: pointer to the hardware structure
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000199 **/
200static i40e_status i40e_alloc_asq_bufs(struct i40e_hw *hw)
201{
202 i40e_status ret_code;
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000203 struct i40e_dma_mem *bi;
204 int i;
205
206 /* No mapped memory needed yet, just the buffer info structures */
David Cassard90bb7762013-11-28 06:39:35 +0000207 ret_code = i40e_allocate_virt_mem(hw, &hw->aq.asq.dma_head,
208 (hw->aq.num_asq_entries * sizeof(struct i40e_dma_mem)));
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000209 if (ret_code)
210 goto alloc_asq_bufs;
David Cassard90bb7762013-11-28 06:39:35 +0000211 hw->aq.asq.r.asq_bi = (struct i40e_dma_mem *)hw->aq.asq.dma_head.va;
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000212
213 /* allocate the mapped buffers */
214 for (i = 0; i < hw->aq.num_asq_entries; i++) {
215 bi = &hw->aq.asq.r.asq_bi[i];
216 ret_code = i40e_allocate_dma_mem(hw, bi,
217 i40e_mem_asq_buf,
218 hw->aq.asq_buf_size,
219 I40E_ADMINQ_DESC_ALIGNMENT);
220 if (ret_code)
221 goto unwind_alloc_asq_bufs;
222 }
223alloc_asq_bufs:
224 return ret_code;
225
226unwind_alloc_asq_bufs:
227 /* don't try to free the one that failed... */
228 i--;
229 for (; i >= 0; i--)
230 i40e_free_dma_mem(hw, &hw->aq.asq.r.asq_bi[i]);
David Cassard90bb7762013-11-28 06:39:35 +0000231 i40e_free_virt_mem(hw, &hw->aq.asq.dma_head);
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000232
233 return ret_code;
234}
235
236/**
237 * i40e_free_arq_bufs - Free receive queue buffer info elements
Jeff Kirsher98d44382013-12-21 05:44:42 +0000238 * @hw: pointer to the hardware structure
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000239 **/
240static void i40e_free_arq_bufs(struct i40e_hw *hw)
241{
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000242 int i;
243
David Cassard90bb7762013-11-28 06:39:35 +0000244 /* free descriptors */
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000245 for (i = 0; i < hw->aq.num_arq_entries; i++)
246 i40e_free_dma_mem(hw, &hw->aq.arq.r.arq_bi[i]);
247
David Cassard90bb7762013-11-28 06:39:35 +0000248 /* free the descriptor memory */
249 i40e_free_dma_mem(hw, &hw->aq.arq.desc_buf);
250
251 /* free the dma header */
252 i40e_free_virt_mem(hw, &hw->aq.arq.dma_head);
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000253}
254
255/**
256 * i40e_free_asq_bufs - Free send queue buffer info elements
Jeff Kirsher98d44382013-12-21 05:44:42 +0000257 * @hw: pointer to the hardware structure
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000258 **/
259static void i40e_free_asq_bufs(struct i40e_hw *hw)
260{
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000261 int i;
262
263 /* only unmap if the address is non-NULL */
264 for (i = 0; i < hw->aq.num_asq_entries; i++)
265 if (hw->aq.asq.r.asq_bi[i].pa)
266 i40e_free_dma_mem(hw, &hw->aq.asq.r.asq_bi[i]);
267
David Cassard90bb7762013-11-28 06:39:35 +0000268 /* free the buffer info list */
269 i40e_free_virt_mem(hw, &hw->aq.asq.cmd_buf);
270
271 /* free the descriptor memory */
272 i40e_free_dma_mem(hw, &hw->aq.asq.desc_buf);
273
274 /* free the dma header */
275 i40e_free_virt_mem(hw, &hw->aq.asq.dma_head);
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000276}
277
278/**
279 * i40e_config_asq_regs - configure ASQ registers
Jeff Kirsher98d44382013-12-21 05:44:42 +0000280 * @hw: pointer to the hardware structure
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000281 *
282 * Configure base address and length registers for the transmit queue
283 **/
284static void i40e_config_asq_regs(struct i40e_hw *hw)
285{
286 if (hw->mac.type == I40E_MAC_VF) {
287 /* configure the transmit queue */
David Cassard90bb7762013-11-28 06:39:35 +0000288 wr32(hw, I40E_VF_ATQBAH1,
289 upper_32_bits(hw->aq.asq.desc_buf.pa));
290 wr32(hw, I40E_VF_ATQBAL1,
291 lower_32_bits(hw->aq.asq.desc_buf.pa));
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000292 wr32(hw, I40E_VF_ATQLEN1, (hw->aq.num_asq_entries |
293 I40E_VF_ATQLEN1_ATQENABLE_MASK));
294 } else {
295 /* configure the transmit queue */
David Cassard90bb7762013-11-28 06:39:35 +0000296 wr32(hw, I40E_PF_ATQBAH,
297 upper_32_bits(hw->aq.asq.desc_buf.pa));
298 wr32(hw, I40E_PF_ATQBAL,
299 lower_32_bits(hw->aq.asq.desc_buf.pa));
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000300 wr32(hw, I40E_PF_ATQLEN, (hw->aq.num_asq_entries |
301 I40E_PF_ATQLEN_ATQENABLE_MASK));
302 }
303}
304
305/**
306 * i40e_config_arq_regs - ARQ register configuration
Jeff Kirsher98d44382013-12-21 05:44:42 +0000307 * @hw: pointer to the hardware structure
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000308 *
309 * Configure base address and length registers for the receive (event queue)
310 **/
311static void i40e_config_arq_regs(struct i40e_hw *hw)
312{
313 if (hw->mac.type == I40E_MAC_VF) {
314 /* configure the receive queue */
David Cassard90bb7762013-11-28 06:39:35 +0000315 wr32(hw, I40E_VF_ARQBAH1,
316 upper_32_bits(hw->aq.arq.desc_buf.pa));
317 wr32(hw, I40E_VF_ARQBAL1,
318 lower_32_bits(hw->aq.arq.desc_buf.pa));
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000319 wr32(hw, I40E_VF_ARQLEN1, (hw->aq.num_arq_entries |
320 I40E_VF_ARQLEN1_ARQENABLE_MASK));
321 } else {
322 /* configure the receive queue */
David Cassard90bb7762013-11-28 06:39:35 +0000323 wr32(hw, I40E_PF_ARQBAH,
324 upper_32_bits(hw->aq.arq.desc_buf.pa));
325 wr32(hw, I40E_PF_ARQBAL,
326 lower_32_bits(hw->aq.arq.desc_buf.pa));
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000327 wr32(hw, I40E_PF_ARQLEN, (hw->aq.num_arq_entries |
328 I40E_PF_ARQLEN_ARQENABLE_MASK));
329 }
330
331 /* Update tail in the HW to post pre-allocated buffers */
332 wr32(hw, hw->aq.arq.tail, hw->aq.num_arq_entries - 1);
333}
334
335/**
336 * i40e_init_asq - main initialization routine for ASQ
Jeff Kirsher98d44382013-12-21 05:44:42 +0000337 * @hw: pointer to the hardware structure
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000338 *
339 * This is the main initialization routine for the Admin Send Queue
340 * Prior to calling this function, drivers *MUST* set the following fields
341 * in the hw->aq structure:
342 * - hw->aq.num_asq_entries
343 * - hw->aq.arq_buf_size
344 *
345 * Do *NOT* hold the lock when calling this as the memory allocation routines
346 * called are not going to be atomic context safe
347 **/
348static i40e_status i40e_init_asq(struct i40e_hw *hw)
349{
350 i40e_status ret_code = 0;
351
352 if (hw->aq.asq.count > 0) {
353 /* queue already initialized */
354 ret_code = I40E_ERR_NOT_READY;
355 goto init_adminq_exit;
356 }
357
358 /* verify input for valid configuration */
359 if ((hw->aq.num_asq_entries == 0) ||
360 (hw->aq.asq_buf_size == 0)) {
361 ret_code = I40E_ERR_CONFIG;
362 goto init_adminq_exit;
363 }
364
365 hw->aq.asq.next_to_use = 0;
366 hw->aq.asq.next_to_clean = 0;
367 hw->aq.asq.count = hw->aq.num_asq_entries;
368
369 /* allocate the ring memory */
370 ret_code = i40e_alloc_adminq_asq_ring(hw);
371 if (ret_code)
372 goto init_adminq_exit;
373
374 /* allocate buffers in the rings */
375 ret_code = i40e_alloc_asq_bufs(hw);
376 if (ret_code)
377 goto init_adminq_free_rings;
378
379 /* initialize base registers */
380 i40e_config_asq_regs(hw);
381
382 /* success! */
383 goto init_adminq_exit;
384
385init_adminq_free_rings:
386 i40e_free_adminq_asq(hw);
387
388init_adminq_exit:
389 return ret_code;
390}
391
392/**
393 * i40e_init_arq - initialize ARQ
Jeff Kirsher98d44382013-12-21 05:44:42 +0000394 * @hw: pointer to the hardware structure
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000395 *
396 * The main initialization routine for the Admin Receive (Event) Queue.
397 * Prior to calling this function, drivers *MUST* set the following fields
398 * in the hw->aq structure:
399 * - hw->aq.num_asq_entries
400 * - hw->aq.arq_buf_size
401 *
402 * Do *NOT* hold the lock when calling this as the memory allocation routines
403 * called are not going to be atomic context safe
404 **/
405static i40e_status i40e_init_arq(struct i40e_hw *hw)
406{
407 i40e_status ret_code = 0;
408
409 if (hw->aq.arq.count > 0) {
410 /* queue already initialized */
411 ret_code = I40E_ERR_NOT_READY;
412 goto init_adminq_exit;
413 }
414
415 /* verify input for valid configuration */
416 if ((hw->aq.num_arq_entries == 0) ||
417 (hw->aq.arq_buf_size == 0)) {
418 ret_code = I40E_ERR_CONFIG;
419 goto init_adminq_exit;
420 }
421
422 hw->aq.arq.next_to_use = 0;
423 hw->aq.arq.next_to_clean = 0;
424 hw->aq.arq.count = hw->aq.num_arq_entries;
425
426 /* allocate the ring memory */
427 ret_code = i40e_alloc_adminq_arq_ring(hw);
428 if (ret_code)
429 goto init_adminq_exit;
430
431 /* allocate buffers in the rings */
432 ret_code = i40e_alloc_arq_bufs(hw);
433 if (ret_code)
434 goto init_adminq_free_rings;
435
436 /* initialize base registers */
437 i40e_config_arq_regs(hw);
438
439 /* success! */
440 goto init_adminq_exit;
441
442init_adminq_free_rings:
443 i40e_free_adminq_arq(hw);
444
445init_adminq_exit:
446 return ret_code;
447}
448
449/**
450 * i40e_shutdown_asq - shutdown the ASQ
Jeff Kirsher98d44382013-12-21 05:44:42 +0000451 * @hw: pointer to the hardware structure
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000452 *
453 * The main shutdown routine for the Admin Send Queue
454 **/
455static i40e_status i40e_shutdown_asq(struct i40e_hw *hw)
456{
457 i40e_status ret_code = 0;
458
459 if (hw->aq.asq.count == 0)
460 return I40E_ERR_NOT_READY;
461
462 /* Stop firmware AdminQ processing */
Shannon Nelson17e6a842013-11-16 10:00:36 +0000463 wr32(hw, hw->aq.asq.head, 0);
464 wr32(hw, hw->aq.asq.tail, 0);
465 wr32(hw, hw->aq.asq.len, 0);
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000466
467 /* make sure lock is available */
468 mutex_lock(&hw->aq.asq_mutex);
469
470 hw->aq.asq.count = 0; /* to indicate uninitialized queue */
471
472 /* free ring buffers */
473 i40e_free_asq_bufs(hw);
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000474
475 mutex_unlock(&hw->aq.asq_mutex);
476
477 return ret_code;
478}
479
480/**
481 * i40e_shutdown_arq - shutdown ARQ
Jeff Kirsher98d44382013-12-21 05:44:42 +0000482 * @hw: pointer to the hardware structure
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000483 *
484 * The main shutdown routine for the Admin Receive Queue
485 **/
486static i40e_status i40e_shutdown_arq(struct i40e_hw *hw)
487{
488 i40e_status ret_code = 0;
489
490 if (hw->aq.arq.count == 0)
491 return I40E_ERR_NOT_READY;
492
493 /* Stop firmware AdminQ processing */
Shannon Nelson17e6a842013-11-16 10:00:36 +0000494 wr32(hw, hw->aq.arq.head, 0);
495 wr32(hw, hw->aq.arq.tail, 0);
496 wr32(hw, hw->aq.arq.len, 0);
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000497
498 /* make sure lock is available */
499 mutex_lock(&hw->aq.arq_mutex);
500
501 hw->aq.arq.count = 0; /* to indicate uninitialized queue */
502
503 /* free ring buffers */
504 i40e_free_arq_bufs(hw);
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000505
506 mutex_unlock(&hw->aq.arq_mutex);
507
508 return ret_code;
509}
510
511/**
512 * i40e_init_adminq - main initialization routine for Admin Queue
Jeff Kirsher98d44382013-12-21 05:44:42 +0000513 * @hw: pointer to the hardware structure
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000514 *
515 * Prior to calling this function, drivers *MUST* set the following fields
516 * in the hw->aq structure:
517 * - hw->aq.num_asq_entries
518 * - hw->aq.num_arq_entries
519 * - hw->aq.arq_buf_size
520 * - hw->aq.asq_buf_size
521 **/
522i40e_status i40e_init_adminq(struct i40e_hw *hw)
523{
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000524 i40e_status ret_code;
Shannon Nelsond4946cf2013-11-16 10:00:40 +0000525 u16 eetrack_lo, eetrack_hi;
526 int retry = 0;
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000527
528 /* verify input for valid configuration */
529 if ((hw->aq.num_arq_entries == 0) ||
530 (hw->aq.num_asq_entries == 0) ||
531 (hw->aq.arq_buf_size == 0) ||
532 (hw->aq.asq_buf_size == 0)) {
533 ret_code = I40E_ERR_CONFIG;
534 goto init_adminq_exit;
535 }
536
537 /* initialize locks */
538 mutex_init(&hw->aq.asq_mutex);
539 mutex_init(&hw->aq.arq_mutex);
540
541 /* Set up register offsets */
542 i40e_adminq_init_regs(hw);
543
544 /* allocate the ASQ */
545 ret_code = i40e_init_asq(hw);
546 if (ret_code)
547 goto init_adminq_destroy_locks;
548
549 /* allocate the ARQ */
550 ret_code = i40e_init_arq(hw);
551 if (ret_code)
552 goto init_adminq_free_asq;
553
Shannon Nelsond4946cf2013-11-16 10:00:40 +0000554 /* There are some cases where the firmware may not be quite ready
555 * for AdminQ operations, so we retry the AdminQ setup a few times
556 * if we see timeouts in this first AQ call.
557 */
558 do {
559 ret_code = i40e_aq_get_firmware_version(hw,
560 &hw->aq.fw_maj_ver,
561 &hw->aq.fw_min_ver,
562 &hw->aq.api_maj_ver,
563 &hw->aq.api_min_ver,
564 NULL);
565 if (ret_code != I40E_ERR_ADMIN_QUEUE_TIMEOUT)
566 break;
567 retry++;
568 msleep(100);
569 i40e_resume_aq(hw);
570 } while (retry < 10);
571 if (ret_code != I40E_SUCCESS)
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000572 goto init_adminq_free_arq;
573
Shannon Nelson981b7542013-12-11 08:17:11 +0000574 /* get the NVM version info */
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000575 i40e_read_nvm_word(hw, I40E_SR_NVM_IMAGE_VERSION, &hw->nvm.version);
576 i40e_read_nvm_word(hw, I40E_SR_NVM_EETRACK_LO, &eetrack_lo);
577 i40e_read_nvm_word(hw, I40E_SR_NVM_EETRACK_HI, &eetrack_hi);
578 hw->nvm.eetrack = (eetrack_hi << 16) | eetrack_lo;
579
Shannon Nelson981b7542013-12-11 08:17:11 +0000580 if (hw->aq.api_maj_ver != I40E_FW_API_VERSION_MAJOR ||
581 hw->aq.api_min_ver > I40E_FW_API_VERSION_MINOR) {
582 ret_code = I40E_ERR_FIRMWARE_API_VERSION;
583 goto init_adminq_free_arq;
584 }
585
Shannon Nelsonff2ff3b2013-12-18 13:45:56 +0000586 /* pre-emptive resource lock release */
587 i40e_aq_release_resource(hw, I40E_NVM_RESOURCE_ID, 0, NULL);
588
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000589 ret_code = i40e_aq_set_hmc_resource_profile(hw,
590 I40E_HMC_PROFILE_DEFAULT,
591 0,
592 NULL);
593 ret_code = 0;
594
595 /* success! */
596 goto init_adminq_exit;
597
598init_adminq_free_arq:
599 i40e_shutdown_arq(hw);
600init_adminq_free_asq:
601 i40e_shutdown_asq(hw);
602init_adminq_destroy_locks:
603
604init_adminq_exit:
605 return ret_code;
606}
607
608/**
609 * i40e_shutdown_adminq - shutdown routine for the Admin Queue
Jeff Kirsher98d44382013-12-21 05:44:42 +0000610 * @hw: pointer to the hardware structure
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000611 **/
612i40e_status i40e_shutdown_adminq(struct i40e_hw *hw)
613{
614 i40e_status ret_code = 0;
615
Anjali Singhai Jaine1860d82013-11-28 06:39:45 +0000616 if (i40e_check_asq_alive(hw))
617 i40e_aq_queue_shutdown(hw, true);
618
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000619 i40e_shutdown_asq(hw);
620 i40e_shutdown_arq(hw);
621
622 /* destroy the locks */
623
624 return ret_code;
625}
626
627/**
628 * i40e_clean_asq - cleans Admin send queue
Jeff Kirsher98d44382013-12-21 05:44:42 +0000629 * @hw: pointer to the hardware structure
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000630 *
631 * returns the number of free desc
632 **/
633static u16 i40e_clean_asq(struct i40e_hw *hw)
634{
635 struct i40e_adminq_ring *asq = &(hw->aq.asq);
636 struct i40e_asq_cmd_details *details;
637 u16 ntc = asq->next_to_clean;
638 struct i40e_aq_desc desc_cb;
639 struct i40e_aq_desc *desc;
640
641 desc = I40E_ADMINQ_DESC(*asq, ntc);
642 details = I40E_ADMINQ_DETAILS(*asq, ntc);
643 while (rd32(hw, hw->aq.asq.head) != ntc) {
644 if (details->callback) {
645 I40E_ADMINQ_CALLBACK cb_func =
646 (I40E_ADMINQ_CALLBACK)details->callback;
647 desc_cb = *desc;
648 cb_func(hw, &desc_cb);
649 }
650 memset((void *)desc, 0, sizeof(struct i40e_aq_desc));
651 memset((void *)details, 0,
652 sizeof(struct i40e_asq_cmd_details));
653 ntc++;
654 if (ntc == asq->count)
655 ntc = 0;
656 desc = I40E_ADMINQ_DESC(*asq, ntc);
657 details = I40E_ADMINQ_DETAILS(*asq, ntc);
658 }
659
660 asq->next_to_clean = ntc;
661
662 return I40E_DESC_UNUSED(asq);
663}
664
665/**
666 * i40e_asq_done - check if FW has processed the Admin Send Queue
667 * @hw: pointer to the hw struct
668 *
669 * Returns true if the firmware has processed all descriptors on the
670 * admin send queue. Returns false if there are still requests pending.
671 **/
Stephen Hemmingeraf28eec2013-12-13 04:37:50 +0000672static bool i40e_asq_done(struct i40e_hw *hw)
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000673{
674 /* AQ designers suggest use of head for better
675 * timing reliability than DD bit
676 */
Shannon Nelson922680b2013-12-18 05:29:17 +0000677 return rd32(hw, hw->aq.asq.head) == hw->aq.asq.next_to_use;
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000678
679}
680
681/**
682 * i40e_asq_send_command - send command to Admin Queue
683 * @hw: pointer to the hw struct
684 * @desc: prefilled descriptor describing the command (non DMA mem)
685 * @buff: buffer to use for indirect commands
686 * @buff_size: size of buffer for indirect commands
Shannon Nelson922680b2013-12-18 05:29:17 +0000687 * @cmd_details: pointer to command details structure
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000688 *
689 * This is the main send command driver routine for the Admin Queue send
690 * queue. It runs the queue, cleans the queue, etc
691 **/
692i40e_status i40e_asq_send_command(struct i40e_hw *hw,
693 struct i40e_aq_desc *desc,
694 void *buff, /* can be NULL */
695 u16 buff_size,
696 struct i40e_asq_cmd_details *cmd_details)
697{
698 i40e_status status = 0;
699 struct i40e_dma_mem *dma_buff = NULL;
700 struct i40e_asq_cmd_details *details;
701 struct i40e_aq_desc *desc_on_ring;
702 bool cmd_completed = false;
703 u16 retval = 0;
704
705 if (hw->aq.asq.count == 0) {
706 i40e_debug(hw, I40E_DEBUG_AQ_MESSAGE,
707 "AQTX: Admin queue not initialized.\n");
708 status = I40E_ERR_QUEUE_EMPTY;
709 goto asq_send_command_exit;
710 }
711
712 details = I40E_ADMINQ_DETAILS(hw->aq.asq, hw->aq.asq.next_to_use);
713 if (cmd_details) {
Jesse Brandeburgd7595a22013-09-13 08:23:22 +0000714 *details = *cmd_details;
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000715
716 /* If the cmd_details are defined copy the cookie. The
717 * cpu_to_le32 is not needed here because the data is ignored
718 * by the FW, only used by the driver
719 */
720 if (details->cookie) {
721 desc->cookie_high =
722 cpu_to_le32(upper_32_bits(details->cookie));
723 desc->cookie_low =
724 cpu_to_le32(lower_32_bits(details->cookie));
725 }
726 } else {
727 memset(details, 0, sizeof(struct i40e_asq_cmd_details));
728 }
729
730 /* clear requested flags and then set additional flags if defined */
731 desc->flags &= ~cpu_to_le16(details->flags_dis);
732 desc->flags |= cpu_to_le16(details->flags_ena);
733
734 mutex_lock(&hw->aq.asq_mutex);
735
736 if (buff_size > hw->aq.asq_buf_size) {
737 i40e_debug(hw,
738 I40E_DEBUG_AQ_MESSAGE,
739 "AQTX: Invalid buffer size: %d.\n",
740 buff_size);
741 status = I40E_ERR_INVALID_SIZE;
742 goto asq_send_command_error;
743 }
744
745 if (details->postpone && !details->async) {
746 i40e_debug(hw,
747 I40E_DEBUG_AQ_MESSAGE,
748 "AQTX: Async flag not set along with postpone flag");
749 status = I40E_ERR_PARAM;
750 goto asq_send_command_error;
751 }
752
753 /* call clean and check queue available function to reclaim the
754 * descriptors that were processed by FW, the function returns the
755 * number of desc available
756 */
757 /* the clean function called here could be called in a separate thread
758 * in case of asynchronous completions
759 */
760 if (i40e_clean_asq(hw) == 0) {
761 i40e_debug(hw,
762 I40E_DEBUG_AQ_MESSAGE,
763 "AQTX: Error queue is full.\n");
764 status = I40E_ERR_ADMIN_QUEUE_FULL;
765 goto asq_send_command_error;
766 }
767
768 /* initialize the temp desc pointer with the right desc */
769 desc_on_ring = I40E_ADMINQ_DESC(hw->aq.asq, hw->aq.asq.next_to_use);
770
771 /* if the desc is available copy the temp desc to the right place */
Jesse Brandeburgd7595a22013-09-13 08:23:22 +0000772 *desc_on_ring = *desc;
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000773
774 /* if buff is not NULL assume indirect command */
775 if (buff != NULL) {
776 dma_buff = &(hw->aq.asq.r.asq_bi[hw->aq.asq.next_to_use]);
777 /* copy the user buff into the respective DMA buff */
778 memcpy(dma_buff->va, buff, buff_size);
779 desc_on_ring->datalen = cpu_to_le16(buff_size);
780
781 /* Update the address values in the desc with the pa value
782 * for respective buffer
783 */
784 desc_on_ring->params.external.addr_high =
785 cpu_to_le32(upper_32_bits(dma_buff->pa));
786 desc_on_ring->params.external.addr_low =
787 cpu_to_le32(lower_32_bits(dma_buff->pa));
788 }
789
790 /* bump the tail */
791 i40e_debug_aq(hw, I40E_DEBUG_AQ_COMMAND, (void *)desc_on_ring, buff);
792 (hw->aq.asq.next_to_use)++;
793 if (hw->aq.asq.next_to_use == hw->aq.asq.count)
794 hw->aq.asq.next_to_use = 0;
795 if (!details->postpone)
796 wr32(hw, hw->aq.asq.tail, hw->aq.asq.next_to_use);
797
798 /* if cmd_details are not defined or async flag is not set,
799 * we need to wait for desc write back
800 */
801 if (!details->async && !details->postpone) {
802 u32 total_delay = 0;
803 u32 delay_len = 10;
804
805 do {
806 /* AQ designers suggest use of head for better
807 * timing reliability than DD bit
808 */
809 if (i40e_asq_done(hw))
810 break;
811 /* ugh! delay while spin_lock */
812 udelay(delay_len);
813 total_delay += delay_len;
814 } while (total_delay < I40E_ASQ_CMD_TIMEOUT);
815 }
816
817 /* if ready, copy the desc back to temp */
818 if (i40e_asq_done(hw)) {
Jesse Brandeburgd7595a22013-09-13 08:23:22 +0000819 *desc = *desc_on_ring;
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000820 if (buff != NULL)
821 memcpy(buff, dma_buff->va, buff_size);
822 retval = le16_to_cpu(desc->retval);
823 if (retval != 0) {
824 i40e_debug(hw,
825 I40E_DEBUG_AQ_MESSAGE,
826 "AQTX: Command completed with error 0x%X.\n",
827 retval);
828 /* 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
839 /* update the error if time out occurred */
840 if ((!cmd_completed) &&
841 (!details->async && !details->postpone)) {
842 i40e_debug(hw,
843 I40E_DEBUG_AQ_MESSAGE,
844 "AQTX: Writeback timeout.\n");
845 status = I40E_ERR_ADMIN_QUEUE_TIMEOUT;
846 }
847
848asq_send_command_error:
849 mutex_unlock(&hw->aq.asq_mutex);
850asq_send_command_exit:
851 return status;
852}
853
854/**
855 * i40e_fill_default_direct_cmd_desc - AQ descriptor helper function
856 * @desc: pointer to the temp descriptor (non DMA mem)
857 * @opcode: the opcode can be used to decide which flags to turn off or on
858 *
859 * Fill the desc with default values
860 **/
861void i40e_fill_default_direct_cmd_desc(struct i40e_aq_desc *desc,
862 u16 opcode)
863{
864 /* zero out the desc */
865 memset((void *)desc, 0, sizeof(struct i40e_aq_desc));
866 desc->opcode = cpu_to_le16(opcode);
Shannon Nelsonab954cb2013-12-18 13:45:57 +0000867 desc->flags = cpu_to_le16(I40E_AQ_FLAG_SI);
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000868}
869
870/**
871 * i40e_clean_arq_element
872 * @hw: pointer to the hw struct
873 * @e: event info from the receive descriptor, includes any buffers
874 * @pending: number of events that could be left to process
875 *
876 * This function cleans one Admin Receive Queue element and returns
877 * the contents through e. It can also return how many events are
878 * left to process through 'pending'
879 **/
880i40e_status i40e_clean_arq_element(struct i40e_hw *hw,
881 struct i40e_arq_event_info *e,
882 u16 *pending)
883{
884 i40e_status ret_code = 0;
885 u16 ntc = hw->aq.arq.next_to_clean;
886 struct i40e_aq_desc *desc;
887 struct i40e_dma_mem *bi;
888 u16 desc_idx;
889 u16 datalen;
890 u16 flags;
891 u16 ntu;
892
893 /* take the lock before we start messing with the ring */
894 mutex_lock(&hw->aq.arq_mutex);
895
896 /* set next_to_use to head */
897 ntu = (rd32(hw, hw->aq.arq.head) & I40E_PF_ARQH_ARQH_MASK);
898 if (ntu == ntc) {
899 /* nothing to do - shouldn't need to update ring's values */
900 i40e_debug(hw,
901 I40E_DEBUG_AQ_MESSAGE,
902 "AQRX: Queue is empty.\n");
903 ret_code = I40E_ERR_ADMIN_QUEUE_NO_WORK;
904 goto clean_arq_element_out;
905 }
906
907 /* now clean the next descriptor */
908 desc = I40E_ADMINQ_DESC(hw->aq.arq, ntc);
909 desc_idx = ntc;
910 i40e_debug_aq(hw,
911 I40E_DEBUG_AQ_COMMAND,
912 (void *)desc,
913 hw->aq.arq.r.arq_bi[desc_idx].va);
914
915 flags = le16_to_cpu(desc->flags);
916 if (flags & I40E_AQ_FLAG_ERR) {
917 ret_code = I40E_ERR_ADMIN_QUEUE_ERROR;
918 hw->aq.arq_last_status =
919 (enum i40e_admin_queue_err)le16_to_cpu(desc->retval);
920 i40e_debug(hw,
921 I40E_DEBUG_AQ_MESSAGE,
922 "AQRX: Event received with error 0x%X.\n",
923 hw->aq.arq_last_status);
924 } else {
Mitch Williamsc36bd4a72013-12-18 13:46:04 +0000925 e->desc = *desc;
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000926 datalen = le16_to_cpu(desc->datalen);
927 e->msg_size = min(datalen, e->msg_size);
928 if (e->msg_buf != NULL && (e->msg_size != 0))
929 memcpy(e->msg_buf, hw->aq.arq.r.arq_bi[desc_idx].va,
930 e->msg_size);
931 }
932
933 /* Restore the original datalen and buffer address in the desc,
934 * FW updates datalen to indicate the event message
935 * size
936 */
937 bi = &hw->aq.arq.r.arq_bi[ntc];
Mitch Williams90077772013-12-18 13:45:48 +0000938 memset((void *)desc, 0, sizeof(struct i40e_aq_desc));
939
940 desc->flags = cpu_to_le16(I40E_AQ_FLAG_BUF);
941 if (hw->aq.arq_buf_size > I40E_AQ_LARGE_BUF)
942 desc->flags |= cpu_to_le16(I40E_AQ_FLAG_LB);
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000943 desc->datalen = cpu_to_le16((u16)bi->size);
944 desc->params.external.addr_high = cpu_to_le32(upper_32_bits(bi->pa));
945 desc->params.external.addr_low = cpu_to_le32(lower_32_bits(bi->pa));
946
947 /* set tail = the last cleaned desc index. */
948 wr32(hw, hw->aq.arq.tail, ntc);
949 /* ntc is updated to tail + 1 */
950 ntc++;
951 if (ntc == hw->aq.num_arq_entries)
952 ntc = 0;
953 hw->aq.arq.next_to_clean = ntc;
954 hw->aq.arq.next_to_use = ntu;
955
956clean_arq_element_out:
957 /* Set pending if needed, unlock and return */
958 if (pending != NULL)
959 *pending = (ntc > ntu ? hw->aq.arq.count : 0) + (ntu - ntc);
960 mutex_unlock(&hw->aq.arq_mutex);
961
962 return ret_code;
963}
964
Stephen Hemmingeraf28eec2013-12-13 04:37:50 +0000965static void i40e_resume_aq(struct i40e_hw *hw)
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000966{
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000967 /* Registers are reset after PF reset */
968 hw->aq.asq.next_to_use = 0;
969 hw->aq.asq.next_to_clean = 0;
970
971 i40e_config_asq_regs(hw);
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000972
973 hw->aq.arq.next_to_use = 0;
974 hw->aq.arq.next_to_clean = 0;
975
976 i40e_config_arq_regs(hw);
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000977}