i40e_adminq.c 29 KB

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