i40e_adminq.c 29 KB

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