i40e_adminq.c 26 KB

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