i40e_adminq.c 28 KB

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