i40e_adminq.c 26 KB

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