i40e_adminq.c 25 KB

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