i40e_adminq.c 26 KB

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