i40e_adminq.c 28 KB

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