i40e_adminq.c 28 KB

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