net/i40e/base: gracefully clean the resources
[dpdk.git] / drivers / net / i40e / base / i40e_adminq.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2001-2018
3  */
4
5 #include "i40e_status.h"
6 #include "i40e_type.h"
7 #include "i40e_register.h"
8 #include "i40e_adminq.h"
9 #include "i40e_prototype.h"
10
11 /**
12  *  i40e_adminq_init_regs - Initialize AdminQ registers
13  *  @hw: pointer to the hardware structure
14  *
15  *  This assumes the alloc_asq and alloc_arq functions have already been called
16  **/
17 STATIC void i40e_adminq_init_regs(struct i40e_hw *hw)
18 {
19         /* set head and tail registers in our local struct */
20         if (i40e_is_vf(hw)) {
21                 hw->aq.asq.tail = I40E_VF_ATQT1;
22                 hw->aq.asq.head = I40E_VF_ATQH1;
23                 hw->aq.asq.len  = I40E_VF_ATQLEN1;
24                 hw->aq.asq.bal  = I40E_VF_ATQBAL1;
25                 hw->aq.asq.bah  = I40E_VF_ATQBAH1;
26                 hw->aq.arq.tail = I40E_VF_ARQT1;
27                 hw->aq.arq.head = I40E_VF_ARQH1;
28                 hw->aq.arq.len  = I40E_VF_ARQLEN1;
29                 hw->aq.arq.bal  = I40E_VF_ARQBAL1;
30                 hw->aq.arq.bah  = I40E_VF_ARQBAH1;
31 #ifdef PF_DRIVER
32         } else {
33                 hw->aq.asq.tail = I40E_PF_ATQT;
34                 hw->aq.asq.head = I40E_PF_ATQH;
35                 hw->aq.asq.len  = I40E_PF_ATQLEN;
36                 hw->aq.asq.bal  = I40E_PF_ATQBAL;
37                 hw->aq.asq.bah  = I40E_PF_ATQBAH;
38                 hw->aq.arq.tail = I40E_PF_ARQT;
39                 hw->aq.arq.head = I40E_PF_ARQH;
40                 hw->aq.arq.len  = I40E_PF_ARQLEN;
41                 hw->aq.arq.bal  = I40E_PF_ARQBAL;
42                 hw->aq.arq.bah  = I40E_PF_ARQBAH;
43 #endif
44         }
45 }
46
47 /**
48  *  i40e_alloc_adminq_asq_ring - Allocate Admin Queue send rings
49  *  @hw: pointer to the hardware structure
50  **/
51 enum i40e_status_code i40e_alloc_adminq_asq_ring(struct i40e_hw *hw)
52 {
53         enum i40e_status_code ret_code;
54
55         ret_code = i40e_allocate_dma_mem(hw, &hw->aq.asq.desc_buf,
56                                          i40e_mem_atq_ring,
57                                          (hw->aq.num_asq_entries *
58                                          sizeof(struct i40e_aq_desc)),
59                                          I40E_ADMINQ_DESC_ALIGNMENT);
60         if (ret_code)
61                 return ret_code;
62
63         ret_code = i40e_allocate_virt_mem(hw, &hw->aq.asq.cmd_buf,
64                                           (hw->aq.num_asq_entries *
65                                           sizeof(struct i40e_asq_cmd_details)));
66         if (ret_code) {
67                 i40e_free_dma_mem(hw, &hw->aq.asq.desc_buf);
68                 return ret_code;
69         }
70
71         return ret_code;
72 }
73
74 /**
75  *  i40e_alloc_adminq_arq_ring - Allocate Admin Queue receive rings
76  *  @hw: pointer to the hardware structure
77  **/
78 enum i40e_status_code i40e_alloc_adminq_arq_ring(struct i40e_hw *hw)
79 {
80         enum i40e_status_code ret_code;
81
82         ret_code = i40e_allocate_dma_mem(hw, &hw->aq.arq.desc_buf,
83                                          i40e_mem_arq_ring,
84                                          (hw->aq.num_arq_entries *
85                                          sizeof(struct i40e_aq_desc)),
86                                          I40E_ADMINQ_DESC_ALIGNMENT);
87
88         return ret_code;
89 }
90
91 /**
92  *  i40e_free_adminq_asq - Free Admin Queue send rings
93  *  @hw: pointer to the hardware structure
94  *
95  *  This assumes the posted send buffers have already been cleaned
96  *  and de-allocated
97  **/
98 void i40e_free_adminq_asq(struct i40e_hw *hw)
99 {
100         i40e_free_virt_mem(hw, &hw->aq.asq.cmd_buf);
101         i40e_free_dma_mem(hw, &hw->aq.asq.desc_buf);
102 }
103
104 /**
105  *  i40e_free_adminq_arq - Free Admin Queue receive rings
106  *  @hw: pointer to the hardware structure
107  *
108  *  This assumes the posted receive buffers have already been cleaned
109  *  and de-allocated
110  **/
111 void i40e_free_adminq_arq(struct i40e_hw *hw)
112 {
113         i40e_free_dma_mem(hw, &hw->aq.arq.desc_buf);
114 }
115
116 /**
117  *  i40e_alloc_arq_bufs - Allocate pre-posted buffers for the receive queue
118  *  @hw: pointer to the hardware structure
119  **/
120 STATIC enum i40e_status_code i40e_alloc_arq_bufs(struct i40e_hw *hw)
121 {
122         enum i40e_status_code ret_code;
123         struct i40e_aq_desc *desc;
124         struct i40e_dma_mem *bi;
125         int i;
126
127         /* We'll be allocating the buffer info memory first, then we can
128          * allocate the mapped buffers for the event processing
129          */
130
131         /* buffer_info structures do not need alignment */
132         ret_code = i40e_allocate_virt_mem(hw, &hw->aq.arq.dma_head,
133                 (hw->aq.num_arq_entries * sizeof(struct i40e_dma_mem)));
134         if (ret_code)
135                 goto alloc_arq_bufs;
136         hw->aq.arq.r.arq_bi = (struct i40e_dma_mem *)hw->aq.arq.dma_head.va;
137
138         /* allocate the mapped buffers */
139         for (i = 0; i < hw->aq.num_arq_entries; i++) {
140                 bi = &hw->aq.arq.r.arq_bi[i];
141                 ret_code = i40e_allocate_dma_mem(hw, bi,
142                                                  i40e_mem_arq_buf,
143                                                  hw->aq.arq_buf_size,
144                                                  I40E_ADMINQ_DESC_ALIGNMENT);
145                 if (ret_code)
146                         goto unwind_alloc_arq_bufs;
147
148                 /* now configure the descriptors for use */
149                 desc = I40E_ADMINQ_DESC(hw->aq.arq, i);
150
151                 desc->flags = CPU_TO_LE16(I40E_AQ_FLAG_BUF);
152                 if (hw->aq.arq_buf_size > I40E_AQ_LARGE_BUF)
153                         desc->flags |= CPU_TO_LE16(I40E_AQ_FLAG_LB);
154                 desc->opcode = 0;
155                 /* This is in accordance with Admin queue design, there is no
156                  * register for buffer size configuration
157                  */
158                 desc->datalen = CPU_TO_LE16((u16)bi->size);
159                 desc->retval = 0;
160                 desc->cookie_high = 0;
161                 desc->cookie_low = 0;
162                 desc->params.external.addr_high =
163                         CPU_TO_LE32(I40E_HI_DWORD(bi->pa));
164                 desc->params.external.addr_low =
165                         CPU_TO_LE32(I40E_LO_DWORD(bi->pa));
166                 desc->params.external.param0 = 0;
167                 desc->params.external.param1 = 0;
168         }
169
170 alloc_arq_bufs:
171         return ret_code;
172
173 unwind_alloc_arq_bufs:
174         /* don't try to free the one that failed... */
175         i--;
176         for (; i >= 0; i--)
177                 i40e_free_dma_mem(hw, &hw->aq.arq.r.arq_bi[i]);
178         i40e_free_virt_mem(hw, &hw->aq.arq.dma_head);
179
180         return ret_code;
181 }
182
183 /**
184  *  i40e_alloc_asq_bufs - Allocate empty buffer structs for the send queue
185  *  @hw: pointer to the hardware structure
186  **/
187 STATIC enum i40e_status_code i40e_alloc_asq_bufs(struct i40e_hw *hw)
188 {
189         enum i40e_status_code ret_code;
190         struct i40e_dma_mem *bi;
191         int i;
192
193         /* No mapped memory needed yet, just the buffer info structures */
194         ret_code = i40e_allocate_virt_mem(hw, &hw->aq.asq.dma_head,
195                 (hw->aq.num_asq_entries * sizeof(struct i40e_dma_mem)));
196         if (ret_code)
197                 goto alloc_asq_bufs;
198         hw->aq.asq.r.asq_bi = (struct i40e_dma_mem *)hw->aq.asq.dma_head.va;
199
200         /* allocate the mapped buffers */
201         for (i = 0; i < hw->aq.num_asq_entries; i++) {
202                 bi = &hw->aq.asq.r.asq_bi[i];
203                 ret_code = i40e_allocate_dma_mem(hw, bi,
204                                                  i40e_mem_asq_buf,
205                                                  hw->aq.asq_buf_size,
206                                                  I40E_ADMINQ_DESC_ALIGNMENT);
207                 if (ret_code)
208                         goto unwind_alloc_asq_bufs;
209         }
210 alloc_asq_bufs:
211         return ret_code;
212
213 unwind_alloc_asq_bufs:
214         /* don't try to free the one that failed... */
215         i--;
216         for (; i >= 0; i--)
217                 i40e_free_dma_mem(hw, &hw->aq.asq.r.asq_bi[i]);
218         i40e_free_virt_mem(hw, &hw->aq.asq.dma_head);
219
220         return ret_code;
221 }
222
223 /**
224  *  i40e_free_arq_bufs - Free receive queue buffer info elements
225  *  @hw: pointer to the hardware structure
226  **/
227 STATIC void i40e_free_arq_bufs(struct i40e_hw *hw)
228 {
229         int i;
230
231         /* free descriptors */
232         for (i = 0; i < hw->aq.num_arq_entries; i++)
233                 i40e_free_dma_mem(hw, &hw->aq.arq.r.arq_bi[i]);
234
235         /* free the descriptor memory */
236         i40e_free_dma_mem(hw, &hw->aq.arq.desc_buf);
237
238         /* free the dma header */
239         i40e_free_virt_mem(hw, &hw->aq.arq.dma_head);
240 }
241
242 /**
243  *  i40e_free_asq_bufs - Free send queue buffer info elements
244  *  @hw: pointer to the hardware structure
245  **/
246 STATIC void i40e_free_asq_bufs(struct i40e_hw *hw)
247 {
248         int i;
249
250         /* only unmap if the address is non-NULL */
251         for (i = 0; i < hw->aq.num_asq_entries; i++)
252                 if (hw->aq.asq.r.asq_bi[i].pa)
253                         i40e_free_dma_mem(hw, &hw->aq.asq.r.asq_bi[i]);
254
255         /* free the buffer info list */
256         i40e_free_virt_mem(hw, &hw->aq.asq.cmd_buf);
257
258         /* free the descriptor memory */
259         i40e_free_dma_mem(hw, &hw->aq.asq.desc_buf);
260
261         /* free the dma header */
262         i40e_free_virt_mem(hw, &hw->aq.asq.dma_head);
263 }
264
265 /**
266  *  i40e_config_asq_regs - configure ASQ registers
267  *  @hw: pointer to the hardware structure
268  *
269  *  Configure base address and length registers for the transmit queue
270  **/
271 STATIC enum i40e_status_code i40e_config_asq_regs(struct i40e_hw *hw)
272 {
273         enum i40e_status_code ret_code = I40E_SUCCESS;
274         u32 reg = 0;
275
276         /* Clear Head and Tail */
277         wr32(hw, hw->aq.asq.head, 0);
278         wr32(hw, hw->aq.asq.tail, 0);
279
280         /* set starting point */
281 #ifdef PF_DRIVER
282 #ifdef INTEGRATED_VF
283         if (!i40e_is_vf(hw))
284                 wr32(hw, hw->aq.asq.len, (hw->aq.num_asq_entries |
285                                           I40E_PF_ATQLEN_ATQENABLE_MASK));
286 #else
287         wr32(hw, hw->aq.asq.len, (hw->aq.num_asq_entries |
288                                   I40E_PF_ATQLEN_ATQENABLE_MASK));
289 #endif /* INTEGRATED_VF */
290 #endif /* PF_DRIVER */
291 #ifdef VF_DRIVER
292 #ifdef INTEGRATED_VF
293         if (i40e_is_vf(hw))
294                 wr32(hw, hw->aq.asq.len, (hw->aq.num_asq_entries |
295                                           I40E_VF_ATQLEN1_ATQENABLE_MASK));
296 #else
297         wr32(hw, hw->aq.asq.len, (hw->aq.num_asq_entries |
298                                   I40E_VF_ATQLEN1_ATQENABLE_MASK));
299 #endif /* INTEGRATED_VF */
300 #endif /* VF_DRIVER */
301         wr32(hw, hw->aq.asq.bal, I40E_LO_DWORD(hw->aq.asq.desc_buf.pa));
302         wr32(hw, hw->aq.asq.bah, I40E_HI_DWORD(hw->aq.asq.desc_buf.pa));
303
304         /* Check one register to verify that config was applied */
305         reg = rd32(hw, hw->aq.asq.bal);
306         if (reg != I40E_LO_DWORD(hw->aq.asq.desc_buf.pa))
307                 ret_code = I40E_ERR_ADMIN_QUEUE_ERROR;
308
309         return ret_code;
310 }
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  **/
318 STATIC enum i40e_status_code i40e_config_arq_regs(struct i40e_hw *hw)
319 {
320         enum i40e_status_code ret_code = I40E_SUCCESS;
321         u32 reg = 0;
322
323         /* Clear Head and Tail */
324         wr32(hw, hw->aq.arq.head, 0);
325         wr32(hw, hw->aq.arq.tail, 0);
326
327         /* set starting point */
328 #ifdef PF_DRIVER
329 #ifdef INTEGRATED_VF
330         if (!i40e_is_vf(hw))
331                 wr32(hw, hw->aq.arq.len, (hw->aq.num_arq_entries |
332                                           I40E_PF_ARQLEN_ARQENABLE_MASK));
333 #else
334         wr32(hw, hw->aq.arq.len, (hw->aq.num_arq_entries |
335                                   I40E_PF_ARQLEN_ARQENABLE_MASK));
336 #endif /* INTEGRATED_VF */
337 #endif /* PF_DRIVER */
338 #ifdef VF_DRIVER
339 #ifdef INTEGRATED_VF
340         if (i40e_is_vf(hw))
341                 wr32(hw, hw->aq.arq.len, (hw->aq.num_arq_entries |
342                                           I40E_VF_ARQLEN1_ARQENABLE_MASK));
343 #else
344         wr32(hw, hw->aq.arq.len, (hw->aq.num_arq_entries |
345                                   I40E_VF_ARQLEN1_ARQENABLE_MASK));
346 #endif /* INTEGRATED_VF */
347 #endif /* VF_DRIVER */
348         wr32(hw, hw->aq.arq.bal, I40E_LO_DWORD(hw->aq.arq.desc_buf.pa));
349         wr32(hw, hw->aq.arq.bah, I40E_HI_DWORD(hw->aq.arq.desc_buf.pa));
350
351         /* Update tail in the HW to post pre-allocated buffers */
352         wr32(hw, hw->aq.arq.tail, hw->aq.num_arq_entries - 1);
353
354         /* Check one register to verify that config was applied */
355         reg = rd32(hw, hw->aq.arq.bal);
356         if (reg != I40E_LO_DWORD(hw->aq.arq.desc_buf.pa))
357                 ret_code = I40E_ERR_ADMIN_QUEUE_ERROR;
358
359         return ret_code;
360 }
361
362 /**
363  *  i40e_init_asq - main initialization routine for ASQ
364  *  @hw: pointer to the hardware structure
365  *
366  *  This is the main initialization routine for the Admin Send Queue
367  *  Prior to calling this function, drivers *MUST* set the following fields
368  *  in the hw->aq structure:
369  *     - hw->aq.num_asq_entries
370  *     - hw->aq.arq_buf_size
371  *
372  *  Do *NOT* hold the lock when calling this as the memory allocation routines
373  *  called are not going to be atomic context safe
374  **/
375 enum i40e_status_code i40e_init_asq(struct i40e_hw *hw)
376 {
377         enum i40e_status_code ret_code = I40E_SUCCESS;
378
379         if (hw->aq.asq.count > 0) {
380                 /* queue already initialized */
381                 ret_code = I40E_ERR_NOT_READY;
382                 goto init_adminq_exit;
383         }
384
385         /* verify input for valid configuration */
386         if ((hw->aq.num_asq_entries == 0) ||
387             (hw->aq.asq_buf_size == 0)) {
388                 ret_code = I40E_ERR_CONFIG;
389                 goto init_adminq_exit;
390         }
391
392         hw->aq.asq.next_to_use = 0;
393         hw->aq.asq.next_to_clean = 0;
394
395         /* allocate the ring memory */
396         ret_code = i40e_alloc_adminq_asq_ring(hw);
397         if (ret_code != I40E_SUCCESS)
398                 goto init_adminq_exit;
399
400         /* allocate buffers in the rings */
401         ret_code = i40e_alloc_asq_bufs(hw);
402         if (ret_code != I40E_SUCCESS)
403                 goto init_adminq_free_rings;
404
405         /* initialize base registers */
406         ret_code = i40e_config_asq_regs(hw);
407         if (ret_code != I40E_SUCCESS)
408                 goto init_config_regs;
409
410         /* success! */
411         hw->aq.asq.count = hw->aq.num_asq_entries;
412         goto init_adminq_exit;
413
414 init_adminq_free_rings:
415         i40e_free_adminq_asq(hw);
416         return ret_code;
417
418 init_config_regs:
419         i40e_free_asq_bufs(hw);
420
421 init_adminq_exit:
422         return ret_code;
423 }
424
425 /**
426  *  i40e_init_arq - initialize ARQ
427  *  @hw: pointer to the hardware structure
428  *
429  *  The main initialization routine for the Admin Receive (Event) Queue.
430  *  Prior to calling this function, drivers *MUST* set the following fields
431  *  in the hw->aq structure:
432  *     - hw->aq.num_asq_entries
433  *     - hw->aq.arq_buf_size
434  *
435  *  Do *NOT* hold the lock when calling this as the memory allocation routines
436  *  called are not going to be atomic context safe
437  **/
438 enum i40e_status_code i40e_init_arq(struct i40e_hw *hw)
439 {
440         enum i40e_status_code ret_code = I40E_SUCCESS;
441
442         if (hw->aq.arq.count > 0) {
443                 /* queue already initialized */
444                 ret_code = I40E_ERR_NOT_READY;
445                 goto init_adminq_exit;
446         }
447
448         /* verify input for valid configuration */
449         if ((hw->aq.num_arq_entries == 0) ||
450             (hw->aq.arq_buf_size == 0)) {
451                 ret_code = I40E_ERR_CONFIG;
452                 goto init_adminq_exit;
453         }
454
455         hw->aq.arq.next_to_use = 0;
456         hw->aq.arq.next_to_clean = 0;
457
458         /* allocate the ring memory */
459         ret_code = i40e_alloc_adminq_arq_ring(hw);
460         if (ret_code != I40E_SUCCESS)
461                 goto init_adminq_exit;
462
463         /* allocate buffers in the rings */
464         ret_code = i40e_alloc_arq_bufs(hw);
465         if (ret_code != I40E_SUCCESS)
466                 goto init_adminq_free_rings;
467
468         /* initialize base registers */
469         ret_code = i40e_config_arq_regs(hw);
470         if (ret_code != I40E_SUCCESS)
471                 goto init_adminq_free_rings;
472
473         /* success! */
474         hw->aq.arq.count = hw->aq.num_arq_entries;
475         goto init_adminq_exit;
476
477 init_adminq_free_rings:
478         i40e_free_adminq_arq(hw);
479
480 init_adminq_exit:
481         return ret_code;
482 }
483
484 /**
485  *  i40e_shutdown_asq - shutdown the ASQ
486  *  @hw: pointer to the hardware structure
487  *
488  *  The main shutdown routine for the Admin Send Queue
489  **/
490 enum i40e_status_code i40e_shutdown_asq(struct i40e_hw *hw)
491 {
492         enum i40e_status_code ret_code = I40E_SUCCESS;
493
494         i40e_acquire_spinlock(&hw->aq.asq_spinlock);
495
496         if (hw->aq.asq.count == 0) {
497                 ret_code = I40E_ERR_NOT_READY;
498                 goto shutdown_asq_out;
499         }
500
501         /* Stop firmware AdminQ processing */
502         wr32(hw, hw->aq.asq.head, 0);
503         wr32(hw, hw->aq.asq.tail, 0);
504         wr32(hw, hw->aq.asq.len, 0);
505         wr32(hw, hw->aq.asq.bal, 0);
506         wr32(hw, hw->aq.asq.bah, 0);
507
508         hw->aq.asq.count = 0; /* to indicate uninitialized queue */
509
510         /* free ring buffers */
511         i40e_free_asq_bufs(hw);
512
513 shutdown_asq_out:
514         i40e_release_spinlock(&hw->aq.asq_spinlock);
515         return ret_code;
516 }
517
518 /**
519  *  i40e_shutdown_arq - shutdown ARQ
520  *  @hw: pointer to the hardware structure
521  *
522  *  The main shutdown routine for the Admin Receive Queue
523  **/
524 enum i40e_status_code i40e_shutdown_arq(struct i40e_hw *hw)
525 {
526         enum i40e_status_code ret_code = I40E_SUCCESS;
527
528         i40e_acquire_spinlock(&hw->aq.arq_spinlock);
529
530         if (hw->aq.arq.count == 0) {
531                 ret_code = I40E_ERR_NOT_READY;
532                 goto shutdown_arq_out;
533         }
534
535         /* Stop firmware AdminQ processing */
536         wr32(hw, hw->aq.arq.head, 0);
537         wr32(hw, hw->aq.arq.tail, 0);
538         wr32(hw, hw->aq.arq.len, 0);
539         wr32(hw, hw->aq.arq.bal, 0);
540         wr32(hw, hw->aq.arq.bah, 0);
541
542         hw->aq.arq.count = 0; /* to indicate uninitialized queue */
543
544         /* free ring buffers */
545         i40e_free_arq_bufs(hw);
546
547 shutdown_arq_out:
548         i40e_release_spinlock(&hw->aq.arq_spinlock);
549         return ret_code;
550 }
551 #ifdef PF_DRIVER
552
553 /**
554  *  i40e_resume_aq - resume AQ processing from 0
555  *  @hw: pointer to the hardware structure
556  **/
557 STATIC void i40e_resume_aq(struct i40e_hw *hw)
558 {
559         /* Registers are reset after PF reset */
560         hw->aq.asq.next_to_use = 0;
561         hw->aq.asq.next_to_clean = 0;
562
563         i40e_config_asq_regs(hw);
564
565         hw->aq.arq.next_to_use = 0;
566         hw->aq.arq.next_to_clean = 0;
567
568         i40e_config_arq_regs(hw);
569 }
570 #endif /* PF_DRIVER */
571
572 /**
573  *  i40e_init_adminq - main initialization routine for Admin Queue
574  *  @hw: pointer to the hardware structure
575  *
576  *  Prior to calling this function, drivers *MUST* set the following fields
577  *  in the hw->aq structure:
578  *     - hw->aq.num_asq_entries
579  *     - hw->aq.num_arq_entries
580  *     - hw->aq.arq_buf_size
581  *     - hw->aq.asq_buf_size
582  **/
583 enum i40e_status_code i40e_init_adminq(struct i40e_hw *hw)
584 {
585 #ifdef PF_DRIVER
586         u16 cfg_ptr, oem_hi, oem_lo;
587         u16 eetrack_lo, eetrack_hi;
588 #endif
589         enum i40e_status_code ret_code;
590 #ifdef PF_DRIVER
591         int retry = 0;
592 #endif
593
594         /* verify input for valid configuration */
595         if ((hw->aq.num_arq_entries == 0) ||
596             (hw->aq.num_asq_entries == 0) ||
597             (hw->aq.arq_buf_size == 0) ||
598             (hw->aq.asq_buf_size == 0)) {
599                 ret_code = I40E_ERR_CONFIG;
600                 goto init_adminq_exit;
601         }
602         i40e_init_spinlock(&hw->aq.asq_spinlock);
603         i40e_init_spinlock(&hw->aq.arq_spinlock);
604
605         /* Set up register offsets */
606         i40e_adminq_init_regs(hw);
607
608         /* setup ASQ command write back timeout */
609         hw->aq.asq_cmd_timeout = I40E_ASQ_CMD_TIMEOUT;
610
611         /* allocate the ASQ */
612         ret_code = i40e_init_asq(hw);
613         if (ret_code != I40E_SUCCESS)
614                 goto init_adminq_destroy_spinlocks;
615
616         /* allocate the ARQ */
617         ret_code = i40e_init_arq(hw);
618         if (ret_code != I40E_SUCCESS)
619                 goto init_adminq_free_asq;
620
621 #ifdef PF_DRIVER
622 #ifdef INTEGRATED_VF
623         /* VF has no need of firmware */
624         if (i40e_is_vf(hw))
625                 goto init_adminq_exit;
626 #endif
627         /* There are some cases where the firmware may not be quite ready
628          * for AdminQ operations, so we retry the AdminQ setup a few times
629          * if we see timeouts in this first AQ call.
630          */
631         do {
632                 ret_code = i40e_aq_get_firmware_version(hw,
633                                                         &hw->aq.fw_maj_ver,
634                                                         &hw->aq.fw_min_ver,
635                                                         &hw->aq.fw_build,
636                                                         &hw->aq.api_maj_ver,
637                                                         &hw->aq.api_min_ver,
638                                                         NULL);
639                 if (ret_code != I40E_ERR_ADMIN_QUEUE_TIMEOUT)
640                         break;
641                 retry++;
642                 i40e_msec_delay(100);
643                 i40e_resume_aq(hw);
644         } while (retry < 10);
645         if (ret_code != I40E_SUCCESS)
646                 goto init_adminq_free_arq;
647
648         /* get the NVM version info */
649         i40e_read_nvm_word(hw, I40E_SR_NVM_DEV_STARTER_VERSION,
650                            &hw->nvm.version);
651         i40e_read_nvm_word(hw, I40E_SR_NVM_EETRACK_LO, &eetrack_lo);
652         i40e_read_nvm_word(hw, I40E_SR_NVM_EETRACK_HI, &eetrack_hi);
653         hw->nvm.eetrack = (eetrack_hi << 16) | eetrack_lo;
654         i40e_read_nvm_word(hw, I40E_SR_BOOT_CONFIG_PTR, &cfg_ptr);
655         i40e_read_nvm_word(hw, (cfg_ptr + I40E_NVM_OEM_VER_OFF),
656                            &oem_hi);
657         i40e_read_nvm_word(hw, (cfg_ptr + (I40E_NVM_OEM_VER_OFF + 1)),
658                            &oem_lo);
659         hw->nvm.oem_ver = ((u32)oem_hi << 16) | oem_lo;
660
661         /* The ability to RX (not drop) 802.1ad frames was added in API 1.7 */
662         if ((hw->aq.api_maj_ver > 1) ||
663             ((hw->aq.api_maj_ver == 1) &&
664              (hw->aq.api_min_ver >= 7)))
665                 hw->flags |= I40E_HW_FLAG_802_1AD_CAPABLE;
666
667         if (hw->mac.type == I40E_MAC_XL710 &&
668             hw->aq.api_maj_ver == I40E_FW_API_VERSION_MAJOR &&
669             hw->aq.api_min_ver >= I40E_MINOR_VER_GET_LINK_INFO_XL710) {
670                 hw->flags |= I40E_HW_FLAG_AQ_PHY_ACCESS_CAPABLE;
671         }
672
673         /* Newer versions of firmware require lock when reading the NVM */
674         if ((hw->aq.api_maj_ver > 1) ||
675             ((hw->aq.api_maj_ver == 1) &&
676              (hw->aq.api_min_ver >= 5)))
677                 hw->flags |= I40E_HW_FLAG_NVM_READ_REQUIRES_LOCK;
678
679         if (hw->aq.api_maj_ver > I40E_FW_API_VERSION_MAJOR) {
680                 ret_code = I40E_ERR_FIRMWARE_API_VERSION;
681                 goto init_adminq_free_arq;
682         }
683
684         /* pre-emptive resource lock release */
685         i40e_aq_release_resource(hw, I40E_NVM_RESOURCE_ID, 0, NULL);
686         hw->nvm_release_on_done = false;
687         hw->nvmupd_state = I40E_NVMUPD_STATE_INIT;
688
689 #endif /* PF_DRIVER */
690         ret_code = I40E_SUCCESS;
691
692         /* success! */
693         goto init_adminq_exit;
694
695 #ifdef PF_DRIVER
696 init_adminq_free_arq:
697         i40e_shutdown_arq(hw);
698 #endif
699 init_adminq_free_asq:
700         i40e_shutdown_asq(hw);
701 init_adminq_destroy_spinlocks:
702         i40e_destroy_spinlock(&hw->aq.asq_spinlock);
703         i40e_destroy_spinlock(&hw->aq.arq_spinlock);
704
705 init_adminq_exit:
706         return ret_code;
707 }
708
709 /**
710  *  i40e_shutdown_adminq - shutdown routine for the Admin Queue
711  *  @hw: pointer to the hardware structure
712  **/
713 enum i40e_status_code i40e_shutdown_adminq(struct i40e_hw *hw)
714 {
715         enum i40e_status_code ret_code = I40E_SUCCESS;
716
717         if (i40e_check_asq_alive(hw))
718                 i40e_aq_queue_shutdown(hw, true);
719
720         i40e_shutdown_asq(hw);
721         i40e_shutdown_arq(hw);
722         i40e_destroy_spinlock(&hw->aq.asq_spinlock);
723         i40e_destroy_spinlock(&hw->aq.arq_spinlock);
724
725         if (hw->nvm_buff.va)
726                 i40e_free_virt_mem(hw, &hw->nvm_buff);
727
728         return ret_code;
729 }
730
731 /**
732  *  i40e_clean_asq - cleans Admin send queue
733  *  @hw: pointer to the hardware structure
734  *
735  *  returns the number of free desc
736  **/
737 u16 i40e_clean_asq(struct i40e_hw *hw)
738 {
739         struct i40e_adminq_ring *asq = &(hw->aq.asq);
740         struct i40e_asq_cmd_details *details;
741         u16 ntc = asq->next_to_clean;
742         struct i40e_aq_desc desc_cb;
743         struct i40e_aq_desc *desc;
744
745         desc = I40E_ADMINQ_DESC(*asq, ntc);
746         details = I40E_ADMINQ_DETAILS(*asq, ntc);
747         while (rd32(hw, hw->aq.asq.head) != ntc) {
748                 i40e_debug(hw, I40E_DEBUG_AQ_MESSAGE,
749                            "ntc %d head %d.\n", ntc, rd32(hw, hw->aq.asq.head));
750
751                 if (details->callback) {
752                         I40E_ADMINQ_CALLBACK cb_func =
753                                         (I40E_ADMINQ_CALLBACK)details->callback;
754                         i40e_memcpy(&desc_cb, desc, sizeof(struct i40e_aq_desc),
755                                     I40E_DMA_TO_DMA);
756                         cb_func(hw, &desc_cb);
757                 }
758                 i40e_memset(desc, 0, sizeof(*desc), I40E_DMA_MEM);
759                 i40e_memset(details, 0, sizeof(*details), I40E_NONDMA_MEM);
760                 ntc++;
761                 if (ntc == asq->count)
762                         ntc = 0;
763                 desc = I40E_ADMINQ_DESC(*asq, ntc);
764                 details = I40E_ADMINQ_DETAILS(*asq, ntc);
765         }
766
767         asq->next_to_clean = ntc;
768
769         return I40E_DESC_UNUSED(asq);
770 }
771
772 /**
773  *  i40e_asq_done - check if FW has processed the Admin Send Queue
774  *  @hw: pointer to the hw struct
775  *
776  *  Returns true if the firmware has processed all descriptors on the
777  *  admin send queue. Returns false if there are still requests pending.
778  **/
779 #ifdef VF_DRIVER
780 bool i40e_asq_done(struct i40e_hw *hw)
781 #else
782 STATIC bool i40e_asq_done(struct i40e_hw *hw)
783 #endif
784 {
785         /* AQ designers suggest use of head for better
786          * timing reliability than DD bit
787          */
788         return rd32(hw, hw->aq.asq.head) == hw->aq.asq.next_to_use;
789
790 }
791
792 /**
793  *  i40e_asq_send_command - send command to Admin Queue
794  *  @hw: pointer to the hw struct
795  *  @desc: prefilled descriptor describing the command (non DMA mem)
796  *  @buff: buffer to use for indirect commands
797  *  @buff_size: size of buffer for indirect commands
798  *  @cmd_details: pointer to command details structure
799  *
800  *  This is the main send command driver routine for the Admin Queue send
801  *  queue.  It runs the queue, cleans the queue, etc
802  **/
803 enum i40e_status_code i40e_asq_send_command(struct i40e_hw *hw,
804                                 struct i40e_aq_desc *desc,
805                                 void *buff, /* can be NULL */
806                                 u16  buff_size,
807                                 struct i40e_asq_cmd_details *cmd_details)
808 {
809         enum i40e_status_code status = I40E_SUCCESS;
810         struct i40e_dma_mem *dma_buff = NULL;
811         struct i40e_asq_cmd_details *details;
812         struct i40e_aq_desc *desc_on_ring;
813         bool cmd_completed = false;
814         u16  retval = 0;
815         u32  val = 0;
816
817         i40e_acquire_spinlock(&hw->aq.asq_spinlock);
818
819         hw->aq.asq_last_status = I40E_AQ_RC_OK;
820
821         if (hw->aq.asq.count == 0) {
822                 i40e_debug(hw, I40E_DEBUG_AQ_MESSAGE,
823                            "AQTX: Admin queue not initialized.\n");
824                 status = I40E_ERR_QUEUE_EMPTY;
825                 goto asq_send_command_error;
826         }
827
828         val = rd32(hw, hw->aq.asq.head);
829         if (val >= hw->aq.num_asq_entries) {
830                 i40e_debug(hw, I40E_DEBUG_AQ_MESSAGE,
831                            "AQTX: head overrun at %d\n", val);
832                 status = I40E_ERR_QUEUE_EMPTY;
833                 goto asq_send_command_error;
834         }
835
836         details = I40E_ADMINQ_DETAILS(hw->aq.asq, hw->aq.asq.next_to_use);
837         if (cmd_details) {
838                 i40e_memcpy(details,
839                             cmd_details,
840                             sizeof(struct i40e_asq_cmd_details),
841                             I40E_NONDMA_TO_NONDMA);
842
843                 /* If the cmd_details are defined copy the cookie.  The
844                  * CPU_TO_LE32 is not needed here because the data is ignored
845                  * by the FW, only used by the driver
846                  */
847                 if (details->cookie) {
848                         desc->cookie_high =
849                                 CPU_TO_LE32(I40E_HI_DWORD(details->cookie));
850                         desc->cookie_low =
851                                 CPU_TO_LE32(I40E_LO_DWORD(details->cookie));
852                 }
853         } else {
854                 i40e_memset(details, 0,
855                             sizeof(struct i40e_asq_cmd_details),
856                             I40E_NONDMA_MEM);
857         }
858
859         /* clear requested flags and then set additional flags if defined */
860         desc->flags &= ~CPU_TO_LE16(details->flags_dis);
861         desc->flags |= CPU_TO_LE16(details->flags_ena);
862
863         if (buff_size > hw->aq.asq_buf_size) {
864                 i40e_debug(hw,
865                            I40E_DEBUG_AQ_MESSAGE,
866                            "AQTX: Invalid buffer size: %d.\n",
867                            buff_size);
868                 status = I40E_ERR_INVALID_SIZE;
869                 goto asq_send_command_error;
870         }
871
872         if (details->postpone && !details->async) {
873                 i40e_debug(hw,
874                            I40E_DEBUG_AQ_MESSAGE,
875                            "AQTX: Async flag not set along with postpone flag");
876                 status = I40E_ERR_PARAM;
877                 goto asq_send_command_error;
878         }
879
880         /* call clean and check queue available function to reclaim the
881          * descriptors that were processed by FW, the function returns the
882          * number of desc available
883          */
884         /* the clean function called here could be called in a separate thread
885          * in case of asynchronous completions
886          */
887         if (i40e_clean_asq(hw) == 0) {
888                 i40e_debug(hw,
889                            I40E_DEBUG_AQ_MESSAGE,
890                            "AQTX: Error queue is full.\n");
891                 status = I40E_ERR_ADMIN_QUEUE_FULL;
892                 goto asq_send_command_error;
893         }
894
895         /* initialize the temp desc pointer with the right desc */
896         desc_on_ring = I40E_ADMINQ_DESC(hw->aq.asq, hw->aq.asq.next_to_use);
897
898         /* if the desc is available copy the temp desc to the right place */
899         i40e_memcpy(desc_on_ring, desc, sizeof(struct i40e_aq_desc),
900                     I40E_NONDMA_TO_DMA);
901
902         /* if buff is not NULL assume indirect command */
903         if (buff != NULL) {
904                 dma_buff = &(hw->aq.asq.r.asq_bi[hw->aq.asq.next_to_use]);
905                 /* copy the user buff into the respective DMA buff */
906                 i40e_memcpy(dma_buff->va, buff, buff_size,
907                             I40E_NONDMA_TO_DMA);
908                 desc_on_ring->datalen = CPU_TO_LE16(buff_size);
909
910                 /* Update the address values in the desc with the pa value
911                  * for respective buffer
912                  */
913                 desc_on_ring->params.external.addr_high =
914                                 CPU_TO_LE32(I40E_HI_DWORD(dma_buff->pa));
915                 desc_on_ring->params.external.addr_low =
916                                 CPU_TO_LE32(I40E_LO_DWORD(dma_buff->pa));
917         }
918
919         /* bump the tail */
920         i40e_debug(hw, I40E_DEBUG_AQ_MESSAGE, "AQTX: desc and buffer:\n");
921         i40e_debug_aq(hw, I40E_DEBUG_AQ_COMMAND, (void *)desc_on_ring,
922                       buff, buff_size);
923         (hw->aq.asq.next_to_use)++;
924         if (hw->aq.asq.next_to_use == hw->aq.asq.count)
925                 hw->aq.asq.next_to_use = 0;
926         if (!details->postpone)
927                 wr32(hw, hw->aq.asq.tail, hw->aq.asq.next_to_use);
928
929         /* if cmd_details are not defined or async flag is not set,
930          * we need to wait for desc write back
931          */
932         if (!details->async && !details->postpone) {
933                 u32 total_delay = 0;
934
935                 do {
936                         /* AQ designers suggest use of head for better
937                          * timing reliability than DD bit
938                          */
939                         if (i40e_asq_done(hw))
940                                 break;
941                         i40e_usec_delay(50);
942                         total_delay += 50;
943                 } while (total_delay < hw->aq.asq_cmd_timeout);
944         }
945
946         /* if ready, copy the desc back to temp */
947         if (i40e_asq_done(hw)) {
948                 i40e_memcpy(desc, desc_on_ring, sizeof(struct i40e_aq_desc),
949                             I40E_DMA_TO_NONDMA);
950                 if (buff != NULL)
951                         i40e_memcpy(buff, dma_buff->va, buff_size,
952                                     I40E_DMA_TO_NONDMA);
953                 retval = LE16_TO_CPU(desc->retval);
954                 if (retval != 0) {
955                         i40e_debug(hw,
956                                    I40E_DEBUG_AQ_MESSAGE,
957                                    "AQTX: Command completed with error 0x%X.\n",
958                                    retval);
959
960                         /* strip off FW internal code */
961                         retval &= 0xff;
962                 }
963                 cmd_completed = true;
964                 if ((enum i40e_admin_queue_err)retval == I40E_AQ_RC_OK)
965                         status = I40E_SUCCESS;
966                 else
967                         status = I40E_ERR_ADMIN_QUEUE_ERROR;
968                 hw->aq.asq_last_status = (enum i40e_admin_queue_err)retval;
969         }
970
971         i40e_debug(hw, I40E_DEBUG_AQ_MESSAGE,
972                    "AQTX: desc and buffer writeback:\n");
973         i40e_debug_aq(hw, I40E_DEBUG_AQ_COMMAND, (void *)desc, buff, buff_size);
974
975         /* save writeback aq if requested */
976         if (details->wb_desc)
977                 i40e_memcpy(details->wb_desc, desc_on_ring,
978                             sizeof(struct i40e_aq_desc), I40E_DMA_TO_NONDMA);
979
980         /* update the error if time out occurred */
981         if ((!cmd_completed) &&
982             (!details->async && !details->postpone)) {
983 #ifdef PF_DRIVER
984                 if (rd32(hw, hw->aq.asq.len) & I40E_GL_ATQLEN_ATQCRIT_MASK) {
985 #else
986                 if (rd32(hw, hw->aq.asq.len) & I40E_VF_ATQLEN1_ATQCRIT_MASK) {
987 #endif
988                         i40e_debug(hw, I40E_DEBUG_AQ_MESSAGE,
989                                    "AQTX: AQ Critical error.\n");
990                         status = I40E_ERR_ADMIN_QUEUE_CRITICAL_ERROR;
991                 } else {
992                         i40e_debug(hw, I40E_DEBUG_AQ_MESSAGE,
993                                    "AQTX: Writeback timeout.\n");
994                         status = I40E_ERR_ADMIN_QUEUE_TIMEOUT;
995                 }
996         }
997
998 asq_send_command_error:
999         i40e_release_spinlock(&hw->aq.asq_spinlock);
1000         return status;
1001 }
1002
1003 /**
1004  *  i40e_fill_default_direct_cmd_desc - AQ descriptor helper function
1005  *  @desc:     pointer to the temp descriptor (non DMA mem)
1006  *  @opcode:   the opcode can be used to decide which flags to turn off or on
1007  *
1008  *  Fill the desc with default values
1009  **/
1010 void i40e_fill_default_direct_cmd_desc(struct i40e_aq_desc *desc,
1011                                        u16 opcode)
1012 {
1013         /* zero out the desc */
1014         i40e_memset((void *)desc, 0, sizeof(struct i40e_aq_desc),
1015                     I40E_NONDMA_MEM);
1016         desc->opcode = CPU_TO_LE16(opcode);
1017         desc->flags = CPU_TO_LE16(I40E_AQ_FLAG_SI);
1018 }
1019
1020 /**
1021  *  i40e_clean_arq_element
1022  *  @hw: pointer to the hw struct
1023  *  @e: event info from the receive descriptor, includes any buffers
1024  *  @pending: number of events that could be left to process
1025  *
1026  *  This function cleans one Admin Receive Queue element and returns
1027  *  the contents through e.  It can also return how many events are
1028  *  left to process through 'pending'
1029  **/
1030 enum i40e_status_code i40e_clean_arq_element(struct i40e_hw *hw,
1031                                              struct i40e_arq_event_info *e,
1032                                              u16 *pending)
1033 {
1034         enum i40e_status_code ret_code = I40E_SUCCESS;
1035         u16 ntc = hw->aq.arq.next_to_clean;
1036         struct i40e_aq_desc *desc;
1037         struct i40e_dma_mem *bi;
1038         u16 desc_idx;
1039         u16 datalen;
1040         u16 flags;
1041         u16 ntu;
1042
1043         /* pre-clean the event info */
1044         i40e_memset(&e->desc, 0, sizeof(e->desc), I40E_NONDMA_MEM);
1045
1046         /* take the lock before we start messing with the ring */
1047         i40e_acquire_spinlock(&hw->aq.arq_spinlock);
1048
1049         if (hw->aq.arq.count == 0) {
1050                 i40e_debug(hw, I40E_DEBUG_AQ_MESSAGE,
1051                            "AQRX: Admin queue not initialized.\n");
1052                 ret_code = I40E_ERR_QUEUE_EMPTY;
1053                 goto clean_arq_element_err;
1054         }
1055
1056         /* set next_to_use to head */
1057 #ifdef INTEGRATED_VF
1058         if (!i40e_is_vf(hw))
1059                 ntu = rd32(hw, hw->aq.arq.head) & I40E_PF_ARQH_ARQH_MASK;
1060         else
1061                 ntu = rd32(hw, hw->aq.arq.head) & I40E_VF_ARQH1_ARQH_MASK;
1062 #else
1063 #ifdef PF_DRIVER
1064         ntu = rd32(hw, hw->aq.arq.head) & I40E_PF_ARQH_ARQH_MASK;
1065 #endif /* PF_DRIVER */
1066 #ifdef VF_DRIVER
1067         ntu = rd32(hw, hw->aq.arq.head) & I40E_VF_ARQH1_ARQH_MASK;
1068 #endif /* VF_DRIVER */
1069 #endif /* INTEGRATED_VF */
1070         if (ntu == ntc) {
1071                 /* nothing to do - shouldn't need to update ring's values */
1072                 ret_code = I40E_ERR_ADMIN_QUEUE_NO_WORK;
1073                 goto clean_arq_element_out;
1074         }
1075
1076         /* now clean the next descriptor */
1077         desc = I40E_ADMINQ_DESC(hw->aq.arq, ntc);
1078         desc_idx = ntc;
1079
1080         hw->aq.arq_last_status =
1081                 (enum i40e_admin_queue_err)LE16_TO_CPU(desc->retval);
1082         flags = LE16_TO_CPU(desc->flags);
1083         if (flags & I40E_AQ_FLAG_ERR) {
1084                 ret_code = I40E_ERR_ADMIN_QUEUE_ERROR;
1085                 i40e_debug(hw,
1086                            I40E_DEBUG_AQ_MESSAGE,
1087                            "AQRX: Event received with error 0x%X.\n",
1088                            hw->aq.arq_last_status);
1089         }
1090
1091         i40e_memcpy(&e->desc, desc, sizeof(struct i40e_aq_desc),
1092                     I40E_DMA_TO_NONDMA);
1093         datalen = LE16_TO_CPU(desc->datalen);
1094         e->msg_len = min(datalen, e->buf_len);
1095         if (e->msg_buf != NULL && (e->msg_len != 0))
1096                 i40e_memcpy(e->msg_buf,
1097                             hw->aq.arq.r.arq_bi[desc_idx].va,
1098                             e->msg_len, I40E_DMA_TO_NONDMA);
1099
1100         i40e_debug(hw, I40E_DEBUG_AQ_MESSAGE, "AQRX: desc and buffer:\n");
1101         i40e_debug_aq(hw, I40E_DEBUG_AQ_COMMAND, (void *)desc, e->msg_buf,
1102                       hw->aq.arq_buf_size);
1103
1104         /* Restore the original datalen and buffer address in the desc,
1105          * FW updates datalen to indicate the event message
1106          * size
1107          */
1108         bi = &hw->aq.arq.r.arq_bi[ntc];
1109         i40e_memset((void *)desc, 0, sizeof(struct i40e_aq_desc), I40E_DMA_MEM);
1110
1111         desc->flags = CPU_TO_LE16(I40E_AQ_FLAG_BUF);
1112         if (hw->aq.arq_buf_size > I40E_AQ_LARGE_BUF)
1113                 desc->flags |= CPU_TO_LE16(I40E_AQ_FLAG_LB);
1114         desc->datalen = CPU_TO_LE16((u16)bi->size);
1115         desc->params.external.addr_high = CPU_TO_LE32(I40E_HI_DWORD(bi->pa));
1116         desc->params.external.addr_low = CPU_TO_LE32(I40E_LO_DWORD(bi->pa));
1117
1118         /* set tail = the last cleaned desc index. */
1119         wr32(hw, hw->aq.arq.tail, ntc);
1120         /* ntc is updated to tail + 1 */
1121         ntc++;
1122         if (ntc == hw->aq.num_arq_entries)
1123                 ntc = 0;
1124         hw->aq.arq.next_to_clean = ntc;
1125         hw->aq.arq.next_to_use = ntu;
1126
1127 #ifdef PF_DRIVER
1128         i40e_nvmupd_check_wait_event(hw, LE16_TO_CPU(e->desc.opcode), &e->desc);
1129 #endif /* PF_DRIVER */
1130 clean_arq_element_out:
1131         /* Set pending if needed, unlock and return */
1132         if (pending != NULL)
1133                 *pending = (ntc > ntu ? hw->aq.arq.count : 0) + (ntu - ntc);
1134 clean_arq_element_err:
1135         i40e_release_spinlock(&hw->aq.arq_spinlock);
1136
1137         return ret_code;
1138 }
1139