i40e_adminq.c 25 KB

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