i40e_adminq.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047
  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->mac.type == I40E_MAC_XL710 &&
  522. hw->aq.api_maj_ver == I40E_FW_API_VERSION_MAJOR &&
  523. hw->aq.api_min_ver >= I40E_MINOR_VER_GET_LINK_INFO_XL710) {
  524. hw->flags |= I40E_HW_FLAG_AQ_PHY_ACCESS_CAPABLE;
  525. }
  526. /* The ability to RX (not drop) 802.1ad frames was added in API 1.7 */
  527. if (hw->aq.api_maj_ver > 1 ||
  528. (hw->aq.api_maj_ver == 1 &&
  529. hw->aq.api_min_ver >= 7))
  530. hw->flags |= I40E_HW_FLAG_802_1AD_CAPABLE;
  531. if (hw->aq.api_maj_ver > I40E_FW_API_VERSION_MAJOR) {
  532. ret_code = I40E_ERR_FIRMWARE_API_VERSION;
  533. goto init_adminq_free_arq;
  534. }
  535. /* pre-emptive resource lock release */
  536. i40e_aq_release_resource(hw, I40E_NVM_RESOURCE_ID, 0, NULL);
  537. hw->nvm_release_on_done = false;
  538. hw->nvmupd_state = I40E_NVMUPD_STATE_INIT;
  539. ret_code = 0;
  540. /* success! */
  541. goto init_adminq_exit;
  542. init_adminq_free_arq:
  543. i40e_shutdown_arq(hw);
  544. init_adminq_free_asq:
  545. i40e_shutdown_asq(hw);
  546. init_adminq_destroy_locks:
  547. init_adminq_exit:
  548. return ret_code;
  549. }
  550. /**
  551. * i40e_shutdown_adminq - shutdown routine for the Admin Queue
  552. * @hw: pointer to the hardware structure
  553. **/
  554. i40e_status i40e_shutdown_adminq(struct i40e_hw *hw)
  555. {
  556. i40e_status ret_code = 0;
  557. if (i40e_check_asq_alive(hw))
  558. i40e_aq_queue_shutdown(hw, true);
  559. i40e_shutdown_asq(hw);
  560. i40e_shutdown_arq(hw);
  561. if (hw->nvm_buff.va)
  562. i40e_free_virt_mem(hw, &hw->nvm_buff);
  563. return ret_code;
  564. }
  565. /**
  566. * i40e_clean_asq - cleans Admin send queue
  567. * @hw: pointer to the hardware structure
  568. *
  569. * returns the number of free desc
  570. **/
  571. static u16 i40e_clean_asq(struct i40e_hw *hw)
  572. {
  573. struct i40e_adminq_ring *asq = &(hw->aq.asq);
  574. struct i40e_asq_cmd_details *details;
  575. u16 ntc = asq->next_to_clean;
  576. struct i40e_aq_desc desc_cb;
  577. struct i40e_aq_desc *desc;
  578. desc = I40E_ADMINQ_DESC(*asq, ntc);
  579. details = I40E_ADMINQ_DETAILS(*asq, ntc);
  580. while (rd32(hw, hw->aq.asq.head) != ntc) {
  581. i40e_debug(hw, I40E_DEBUG_AQ_MESSAGE,
  582. "ntc %d head %d.\n", ntc, rd32(hw, hw->aq.asq.head));
  583. if (details->callback) {
  584. I40E_ADMINQ_CALLBACK cb_func =
  585. (I40E_ADMINQ_CALLBACK)details->callback;
  586. desc_cb = *desc;
  587. cb_func(hw, &desc_cb);
  588. }
  589. memset(desc, 0, sizeof(*desc));
  590. memset(details, 0, sizeof(*details));
  591. ntc++;
  592. if (ntc == asq->count)
  593. ntc = 0;
  594. desc = I40E_ADMINQ_DESC(*asq, ntc);
  595. details = I40E_ADMINQ_DETAILS(*asq, ntc);
  596. }
  597. asq->next_to_clean = ntc;
  598. return I40E_DESC_UNUSED(asq);
  599. }
  600. /**
  601. * i40e_asq_done - check if FW has processed the Admin Send Queue
  602. * @hw: pointer to the hw struct
  603. *
  604. * Returns true if the firmware has processed all descriptors on the
  605. * admin send queue. Returns false if there are still requests pending.
  606. **/
  607. static bool i40e_asq_done(struct i40e_hw *hw)
  608. {
  609. /* AQ designers suggest use of head for better
  610. * timing reliability than DD bit
  611. */
  612. return rd32(hw, hw->aq.asq.head) == hw->aq.asq.next_to_use;
  613. }
  614. /**
  615. * i40e_asq_send_command - send command to Admin Queue
  616. * @hw: pointer to the hw struct
  617. * @desc: prefilled descriptor describing the command (non DMA mem)
  618. * @buff: buffer to use for indirect commands
  619. * @buff_size: size of buffer for indirect commands
  620. * @cmd_details: pointer to command details structure
  621. *
  622. * This is the main send command driver routine for the Admin Queue send
  623. * queue. It runs the queue, cleans the queue, etc
  624. **/
  625. i40e_status i40e_asq_send_command(struct i40e_hw *hw,
  626. struct i40e_aq_desc *desc,
  627. void *buff, /* can be NULL */
  628. u16 buff_size,
  629. struct i40e_asq_cmd_details *cmd_details)
  630. {
  631. i40e_status status = 0;
  632. struct i40e_dma_mem *dma_buff = NULL;
  633. struct i40e_asq_cmd_details *details;
  634. struct i40e_aq_desc *desc_on_ring;
  635. bool cmd_completed = false;
  636. u16 retval = 0;
  637. u32 val = 0;
  638. mutex_lock(&hw->aq.asq_mutex);
  639. if (hw->aq.asq.count == 0) {
  640. i40e_debug(hw, I40E_DEBUG_AQ_MESSAGE,
  641. "AQTX: Admin queue not initialized.\n");
  642. status = I40E_ERR_QUEUE_EMPTY;
  643. goto asq_send_command_error;
  644. }
  645. hw->aq.asq_last_status = I40E_AQ_RC_OK;
  646. val = rd32(hw, hw->aq.asq.head);
  647. if (val >= hw->aq.num_asq_entries) {
  648. i40e_debug(hw, I40E_DEBUG_AQ_MESSAGE,
  649. "AQTX: head overrun at %d\n", val);
  650. status = I40E_ERR_QUEUE_EMPTY;
  651. goto asq_send_command_error;
  652. }
  653. details = I40E_ADMINQ_DETAILS(hw->aq.asq, hw->aq.asq.next_to_use);
  654. if (cmd_details) {
  655. *details = *cmd_details;
  656. /* If the cmd_details are defined copy the cookie. The
  657. * cpu_to_le32 is not needed here because the data is ignored
  658. * by the FW, only used by the driver
  659. */
  660. if (details->cookie) {
  661. desc->cookie_high =
  662. cpu_to_le32(upper_32_bits(details->cookie));
  663. desc->cookie_low =
  664. cpu_to_le32(lower_32_bits(details->cookie));
  665. }
  666. } else {
  667. memset(details, 0, sizeof(struct i40e_asq_cmd_details));
  668. }
  669. /* clear requested flags and then set additional flags if defined */
  670. desc->flags &= ~cpu_to_le16(details->flags_dis);
  671. desc->flags |= cpu_to_le16(details->flags_ena);
  672. if (buff_size > hw->aq.asq_buf_size) {
  673. i40e_debug(hw,
  674. I40E_DEBUG_AQ_MESSAGE,
  675. "AQTX: Invalid buffer size: %d.\n",
  676. buff_size);
  677. status = I40E_ERR_INVALID_SIZE;
  678. goto asq_send_command_error;
  679. }
  680. if (details->postpone && !details->async) {
  681. i40e_debug(hw,
  682. I40E_DEBUG_AQ_MESSAGE,
  683. "AQTX: Async flag not set along with postpone flag");
  684. status = I40E_ERR_PARAM;
  685. goto asq_send_command_error;
  686. }
  687. /* call clean and check queue available function to reclaim the
  688. * descriptors that were processed by FW, the function returns the
  689. * number of desc available
  690. */
  691. /* the clean function called here could be called in a separate thread
  692. * in case of asynchronous completions
  693. */
  694. if (i40e_clean_asq(hw) == 0) {
  695. i40e_debug(hw,
  696. I40E_DEBUG_AQ_MESSAGE,
  697. "AQTX: Error queue is full.\n");
  698. status = I40E_ERR_ADMIN_QUEUE_FULL;
  699. goto asq_send_command_error;
  700. }
  701. /* initialize the temp desc pointer with the right desc */
  702. desc_on_ring = I40E_ADMINQ_DESC(hw->aq.asq, hw->aq.asq.next_to_use);
  703. /* if the desc is available copy the temp desc to the right place */
  704. *desc_on_ring = *desc;
  705. /* if buff is not NULL assume indirect command */
  706. if (buff != NULL) {
  707. dma_buff = &(hw->aq.asq.r.asq_bi[hw->aq.asq.next_to_use]);
  708. /* copy the user buff into the respective DMA buff */
  709. memcpy(dma_buff->va, buff, buff_size);
  710. desc_on_ring->datalen = cpu_to_le16(buff_size);
  711. /* Update the address values in the desc with the pa value
  712. * for respective buffer
  713. */
  714. desc_on_ring->params.external.addr_high =
  715. cpu_to_le32(upper_32_bits(dma_buff->pa));
  716. desc_on_ring->params.external.addr_low =
  717. cpu_to_le32(lower_32_bits(dma_buff->pa));
  718. }
  719. /* bump the tail */
  720. i40e_debug(hw, I40E_DEBUG_AQ_MESSAGE, "AQTX: desc and buffer:\n");
  721. i40e_debug_aq(hw, I40E_DEBUG_AQ_COMMAND, (void *)desc_on_ring,
  722. buff, buff_size);
  723. (hw->aq.asq.next_to_use)++;
  724. if (hw->aq.asq.next_to_use == hw->aq.asq.count)
  725. hw->aq.asq.next_to_use = 0;
  726. if (!details->postpone)
  727. wr32(hw, hw->aq.asq.tail, hw->aq.asq.next_to_use);
  728. /* if cmd_details are not defined or async flag is not set,
  729. * we need to wait for desc write back
  730. */
  731. if (!details->async && !details->postpone) {
  732. u32 total_delay = 0;
  733. do {
  734. /* AQ designers suggest use of head for better
  735. * timing reliability than DD bit
  736. */
  737. if (i40e_asq_done(hw))
  738. break;
  739. udelay(50);
  740. total_delay += 50;
  741. } while (total_delay < hw->aq.asq_cmd_timeout);
  742. }
  743. /* if ready, copy the desc back to temp */
  744. if (i40e_asq_done(hw)) {
  745. *desc = *desc_on_ring;
  746. if (buff != NULL)
  747. memcpy(buff, dma_buff->va, buff_size);
  748. retval = le16_to_cpu(desc->retval);
  749. if (retval != 0) {
  750. i40e_debug(hw,
  751. I40E_DEBUG_AQ_MESSAGE,
  752. "AQTX: Command completed with error 0x%X.\n",
  753. retval);
  754. /* strip off FW internal code */
  755. retval &= 0xff;
  756. }
  757. cmd_completed = true;
  758. if ((enum i40e_admin_queue_err)retval == I40E_AQ_RC_OK)
  759. status = 0;
  760. else
  761. status = I40E_ERR_ADMIN_QUEUE_ERROR;
  762. hw->aq.asq_last_status = (enum i40e_admin_queue_err)retval;
  763. }
  764. i40e_debug(hw, I40E_DEBUG_AQ_MESSAGE,
  765. "AQTX: desc and buffer writeback:\n");
  766. i40e_debug_aq(hw, I40E_DEBUG_AQ_COMMAND, (void *)desc, buff, buff_size);
  767. /* save writeback aq if requested */
  768. if (details->wb_desc)
  769. *details->wb_desc = *desc_on_ring;
  770. /* update the error if time out occurred */
  771. if ((!cmd_completed) &&
  772. (!details->async && !details->postpone)) {
  773. i40e_debug(hw,
  774. I40E_DEBUG_AQ_MESSAGE,
  775. "AQTX: Writeback timeout.\n");
  776. status = I40E_ERR_ADMIN_QUEUE_TIMEOUT;
  777. }
  778. asq_send_command_error:
  779. mutex_unlock(&hw->aq.asq_mutex);
  780. return status;
  781. }
  782. /**
  783. * i40e_fill_default_direct_cmd_desc - AQ descriptor helper function
  784. * @desc: pointer to the temp descriptor (non DMA mem)
  785. * @opcode: the opcode can be used to decide which flags to turn off or on
  786. *
  787. * Fill the desc with default values
  788. **/
  789. void i40e_fill_default_direct_cmd_desc(struct i40e_aq_desc *desc,
  790. u16 opcode)
  791. {
  792. /* zero out the desc */
  793. memset((void *)desc, 0, sizeof(struct i40e_aq_desc));
  794. desc->opcode = cpu_to_le16(opcode);
  795. desc->flags = cpu_to_le16(I40E_AQ_FLAG_SI);
  796. }
  797. /**
  798. * i40e_clean_arq_element
  799. * @hw: pointer to the hw struct
  800. * @e: event info from the receive descriptor, includes any buffers
  801. * @pending: number of events that could be left to process
  802. *
  803. * This function cleans one Admin Receive Queue element and returns
  804. * the contents through e. It can also return how many events are
  805. * left to process through 'pending'
  806. **/
  807. i40e_status i40e_clean_arq_element(struct i40e_hw *hw,
  808. struct i40e_arq_event_info *e,
  809. u16 *pending)
  810. {
  811. i40e_status ret_code = 0;
  812. u16 ntc = hw->aq.arq.next_to_clean;
  813. struct i40e_aq_desc *desc;
  814. struct i40e_dma_mem *bi;
  815. u16 desc_idx;
  816. u16 datalen;
  817. u16 flags;
  818. u16 ntu;
  819. /* pre-clean the event info */
  820. memset(&e->desc, 0, sizeof(e->desc));
  821. /* take the lock before we start messing with the ring */
  822. mutex_lock(&hw->aq.arq_mutex);
  823. if (hw->aq.arq.count == 0) {
  824. i40e_debug(hw, I40E_DEBUG_AQ_MESSAGE,
  825. "AQRX: Admin queue not initialized.\n");
  826. ret_code = I40E_ERR_QUEUE_EMPTY;
  827. goto clean_arq_element_err;
  828. }
  829. /* set next_to_use to head */
  830. ntu = (rd32(hw, hw->aq.arq.head) & I40E_PF_ARQH_ARQH_MASK);
  831. if (ntu == ntc) {
  832. /* nothing to do - shouldn't need to update ring's values */
  833. ret_code = I40E_ERR_ADMIN_QUEUE_NO_WORK;
  834. goto clean_arq_element_out;
  835. }
  836. /* now clean the next descriptor */
  837. desc = I40E_ADMINQ_DESC(hw->aq.arq, ntc);
  838. desc_idx = ntc;
  839. hw->aq.arq_last_status =
  840. (enum i40e_admin_queue_err)le16_to_cpu(desc->retval);
  841. flags = le16_to_cpu(desc->flags);
  842. if (flags & I40E_AQ_FLAG_ERR) {
  843. ret_code = I40E_ERR_ADMIN_QUEUE_ERROR;
  844. i40e_debug(hw,
  845. I40E_DEBUG_AQ_MESSAGE,
  846. "AQRX: Event received with error 0x%X.\n",
  847. hw->aq.arq_last_status);
  848. }
  849. e->desc = *desc;
  850. datalen = le16_to_cpu(desc->datalen);
  851. e->msg_len = min(datalen, e->buf_len);
  852. if (e->msg_buf != NULL && (e->msg_len != 0))
  853. memcpy(e->msg_buf, hw->aq.arq.r.arq_bi[desc_idx].va,
  854. e->msg_len);
  855. i40e_debug(hw, I40E_DEBUG_AQ_MESSAGE, "AQRX: desc and buffer:\n");
  856. i40e_debug_aq(hw, I40E_DEBUG_AQ_COMMAND, (void *)desc, e->msg_buf,
  857. hw->aq.arq_buf_size);
  858. /* Restore the original datalen and buffer address in the desc,
  859. * FW updates datalen to indicate the event message
  860. * size
  861. */
  862. bi = &hw->aq.arq.r.arq_bi[ntc];
  863. memset((void *)desc, 0, sizeof(struct i40e_aq_desc));
  864. desc->flags = cpu_to_le16(I40E_AQ_FLAG_BUF);
  865. if (hw->aq.arq_buf_size > I40E_AQ_LARGE_BUF)
  866. desc->flags |= cpu_to_le16(I40E_AQ_FLAG_LB);
  867. desc->datalen = cpu_to_le16((u16)bi->size);
  868. desc->params.external.addr_high = cpu_to_le32(upper_32_bits(bi->pa));
  869. desc->params.external.addr_low = cpu_to_le32(lower_32_bits(bi->pa));
  870. /* set tail = the last cleaned desc index. */
  871. wr32(hw, hw->aq.arq.tail, ntc);
  872. /* ntc is updated to tail + 1 */
  873. ntc++;
  874. if (ntc == hw->aq.num_arq_entries)
  875. ntc = 0;
  876. hw->aq.arq.next_to_clean = ntc;
  877. hw->aq.arq.next_to_use = ntu;
  878. i40e_nvmupd_check_wait_event(hw, le16_to_cpu(e->desc.opcode));
  879. clean_arq_element_out:
  880. /* Set pending if needed, unlock and return */
  881. if (pending)
  882. *pending = (ntc > ntu ? hw->aq.arq.count : 0) + (ntu - ntc);
  883. clean_arq_element_err:
  884. mutex_unlock(&hw->aq.arq_mutex);
  885. return ret_code;
  886. }
  887. static void i40e_resume_aq(struct i40e_hw *hw)
  888. {
  889. /* Registers are reset after PF reset */
  890. hw->aq.asq.next_to_use = 0;
  891. hw->aq.asq.next_to_clean = 0;
  892. i40e_config_asq_regs(hw);
  893. hw->aq.arq.next_to_use = 0;
  894. hw->aq.arq.next_to_clean = 0;
  895. i40e_config_arq_regs(hw);
  896. }