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