i40e_adminq.c 26 KB

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