i40e_adminq.c 26 KB

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