i40e_adminq.c 29 KB

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