i40e_adminq.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024
  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 == i40e_aqc_opc_nvm_erase) ||
  39. (desc->opcode == 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 (hw->mac.type == I40E_MAC_VF) {
  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.arq.tail = I40E_VF_ARQT1;
  55. hw->aq.arq.head = I40E_VF_ARQH1;
  56. hw->aq.arq.len = I40E_VF_ARQLEN1;
  57. } else {
  58. hw->aq.asq.tail = I40E_PF_ATQT;
  59. hw->aq.asq.head = I40E_PF_ATQH;
  60. hw->aq.asq.len = I40E_PF_ATQLEN;
  61. hw->aq.arq.tail = I40E_PF_ARQT;
  62. hw->aq.arq.head = I40E_PF_ARQH;
  63. hw->aq.arq.len = I40E_PF_ARQLEN;
  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. if (hw->mac.type == I40E_MAC_VF) {
  262. /* configure the transmit queue */
  263. wr32(hw, I40E_VF_ATQBAH1,
  264. upper_32_bits(hw->aq.asq.desc_buf.pa));
  265. wr32(hw, I40E_VF_ATQBAL1,
  266. lower_32_bits(hw->aq.asq.desc_buf.pa));
  267. wr32(hw, I40E_VF_ATQLEN1, (hw->aq.num_asq_entries |
  268. I40E_VF_ATQLEN1_ATQENABLE_MASK));
  269. reg = rd32(hw, I40E_VF_ATQBAL1);
  270. } else {
  271. /* configure the transmit queue */
  272. wr32(hw, I40E_PF_ATQBAH,
  273. upper_32_bits(hw->aq.asq.desc_buf.pa));
  274. wr32(hw, I40E_PF_ATQBAL,
  275. lower_32_bits(hw->aq.asq.desc_buf.pa));
  276. wr32(hw, I40E_PF_ATQLEN, (hw->aq.num_asq_entries |
  277. I40E_PF_ATQLEN_ATQENABLE_MASK));
  278. reg = rd32(hw, I40E_PF_ATQBAL);
  279. }
  280. /* Check one register to verify that config was applied */
  281. if (reg != lower_32_bits(hw->aq.asq.desc_buf.pa))
  282. ret_code = I40E_ERR_ADMIN_QUEUE_ERROR;
  283. return ret_code;
  284. }
  285. /**
  286. * i40e_config_arq_regs - ARQ register configuration
  287. * @hw: pointer to the hardware structure
  288. *
  289. * Configure base address and length registers for the receive (event queue)
  290. **/
  291. static i40e_status i40e_config_arq_regs(struct i40e_hw *hw)
  292. {
  293. i40e_status ret_code = 0;
  294. u32 reg = 0;
  295. if (hw->mac.type == I40E_MAC_VF) {
  296. /* configure the receive queue */
  297. wr32(hw, I40E_VF_ARQBAH1,
  298. upper_32_bits(hw->aq.arq.desc_buf.pa));
  299. wr32(hw, I40E_VF_ARQBAL1,
  300. lower_32_bits(hw->aq.arq.desc_buf.pa));
  301. wr32(hw, I40E_VF_ARQLEN1, (hw->aq.num_arq_entries |
  302. I40E_VF_ARQLEN1_ARQENABLE_MASK));
  303. reg = rd32(hw, I40E_VF_ARQBAL1);
  304. } else {
  305. /* configure the receive queue */
  306. wr32(hw, I40E_PF_ARQBAH,
  307. upper_32_bits(hw->aq.arq.desc_buf.pa));
  308. wr32(hw, I40E_PF_ARQBAL,
  309. lower_32_bits(hw->aq.arq.desc_buf.pa));
  310. wr32(hw, I40E_PF_ARQLEN, (hw->aq.num_arq_entries |
  311. I40E_PF_ARQLEN_ARQENABLE_MASK));
  312. reg = rd32(hw, I40E_PF_ARQBAL);
  313. }
  314. /* Update tail in the HW to post pre-allocated buffers */
  315. wr32(hw, hw->aq.arq.tail, hw->aq.num_arq_entries - 1);
  316. /* Check one register to verify that config was applied */
  317. if (reg != lower_32_bits(hw->aq.arq.desc_buf.pa))
  318. ret_code = I40E_ERR_ADMIN_QUEUE_ERROR;
  319. return ret_code;
  320. }
  321. /**
  322. * i40e_init_asq - main initialization routine for ASQ
  323. * @hw: pointer to the hardware structure
  324. *
  325. * This is the main initialization routine for the Admin Send Queue
  326. * Prior to calling this function, drivers *MUST* set the following fields
  327. * in the hw->aq structure:
  328. * - hw->aq.num_asq_entries
  329. * - hw->aq.arq_buf_size
  330. *
  331. * Do *NOT* hold the lock when calling this as the memory allocation routines
  332. * called are not going to be atomic context safe
  333. **/
  334. static i40e_status i40e_init_asq(struct i40e_hw *hw)
  335. {
  336. i40e_status ret_code = 0;
  337. if (hw->aq.asq.count > 0) {
  338. /* queue already initialized */
  339. ret_code = I40E_ERR_NOT_READY;
  340. goto init_adminq_exit;
  341. }
  342. /* verify input for valid configuration */
  343. if ((hw->aq.num_asq_entries == 0) ||
  344. (hw->aq.asq_buf_size == 0)) {
  345. ret_code = I40E_ERR_CONFIG;
  346. goto init_adminq_exit;
  347. }
  348. hw->aq.asq.next_to_use = 0;
  349. hw->aq.asq.next_to_clean = 0;
  350. hw->aq.asq.count = hw->aq.num_asq_entries;
  351. /* allocate the ring memory */
  352. ret_code = i40e_alloc_adminq_asq_ring(hw);
  353. if (ret_code)
  354. goto init_adminq_exit;
  355. /* allocate buffers in the rings */
  356. ret_code = i40e_alloc_asq_bufs(hw);
  357. if (ret_code)
  358. goto init_adminq_free_rings;
  359. /* initialize base registers */
  360. ret_code = i40e_config_asq_regs(hw);
  361. if (ret_code)
  362. goto init_adminq_free_rings;
  363. /* success! */
  364. goto init_adminq_exit;
  365. init_adminq_free_rings:
  366. i40e_free_adminq_asq(hw);
  367. init_adminq_exit:
  368. return ret_code;
  369. }
  370. /**
  371. * i40e_init_arq - initialize ARQ
  372. * @hw: pointer to the hardware structure
  373. *
  374. * The main initialization routine for the Admin Receive (Event) Queue.
  375. * Prior to calling this function, drivers *MUST* set the following fields
  376. * in the hw->aq structure:
  377. * - hw->aq.num_asq_entries
  378. * - hw->aq.arq_buf_size
  379. *
  380. * Do *NOT* hold the lock when calling this as the memory allocation routines
  381. * called are not going to be atomic context safe
  382. **/
  383. static i40e_status i40e_init_arq(struct i40e_hw *hw)
  384. {
  385. i40e_status ret_code = 0;
  386. if (hw->aq.arq.count > 0) {
  387. /* queue already initialized */
  388. ret_code = I40E_ERR_NOT_READY;
  389. goto init_adminq_exit;
  390. }
  391. /* verify input for valid configuration */
  392. if ((hw->aq.num_arq_entries == 0) ||
  393. (hw->aq.arq_buf_size == 0)) {
  394. ret_code = I40E_ERR_CONFIG;
  395. goto init_adminq_exit;
  396. }
  397. hw->aq.arq.next_to_use = 0;
  398. hw->aq.arq.next_to_clean = 0;
  399. hw->aq.arq.count = hw->aq.num_arq_entries;
  400. /* allocate the ring memory */
  401. ret_code = i40e_alloc_adminq_arq_ring(hw);
  402. if (ret_code)
  403. goto init_adminq_exit;
  404. /* allocate buffers in the rings */
  405. ret_code = i40e_alloc_arq_bufs(hw);
  406. if (ret_code)
  407. goto init_adminq_free_rings;
  408. /* initialize base registers */
  409. ret_code = i40e_config_arq_regs(hw);
  410. if (ret_code)
  411. goto init_adminq_free_rings;
  412. /* success! */
  413. goto init_adminq_exit;
  414. init_adminq_free_rings:
  415. i40e_free_adminq_arq(hw);
  416. init_adminq_exit:
  417. return ret_code;
  418. }
  419. /**
  420. * i40e_shutdown_asq - shutdown the ASQ
  421. * @hw: pointer to the hardware structure
  422. *
  423. * The main shutdown routine for the Admin Send Queue
  424. **/
  425. static i40e_status i40e_shutdown_asq(struct i40e_hw *hw)
  426. {
  427. i40e_status ret_code = 0;
  428. if (hw->aq.asq.count == 0)
  429. return I40E_ERR_NOT_READY;
  430. /* Stop firmware AdminQ processing */
  431. wr32(hw, hw->aq.asq.head, 0);
  432. wr32(hw, hw->aq.asq.tail, 0);
  433. wr32(hw, hw->aq.asq.len, 0);
  434. /* make sure lock is available */
  435. mutex_lock(&hw->aq.asq_mutex);
  436. hw->aq.asq.count = 0; /* to indicate uninitialized queue */
  437. /* free ring buffers */
  438. i40e_free_asq_bufs(hw);
  439. mutex_unlock(&hw->aq.asq_mutex);
  440. return ret_code;
  441. }
  442. /**
  443. * i40e_shutdown_arq - shutdown ARQ
  444. * @hw: pointer to the hardware structure
  445. *
  446. * The main shutdown routine for the Admin Receive Queue
  447. **/
  448. static i40e_status i40e_shutdown_arq(struct i40e_hw *hw)
  449. {
  450. i40e_status ret_code = 0;
  451. if (hw->aq.arq.count == 0)
  452. return I40E_ERR_NOT_READY;
  453. /* Stop firmware AdminQ processing */
  454. wr32(hw, hw->aq.arq.head, 0);
  455. wr32(hw, hw->aq.arq.tail, 0);
  456. wr32(hw, hw->aq.arq.len, 0);
  457. /* make sure lock is available */
  458. mutex_lock(&hw->aq.arq_mutex);
  459. hw->aq.arq.count = 0; /* to indicate uninitialized queue */
  460. /* free ring buffers */
  461. i40e_free_arq_bufs(hw);
  462. mutex_unlock(&hw->aq.arq_mutex);
  463. return ret_code;
  464. }
  465. /**
  466. * i40e_init_adminq - main initialization routine for Admin Queue
  467. * @hw: pointer to the hardware structure
  468. *
  469. * Prior to calling this function, drivers *MUST* set the following fields
  470. * in the hw->aq structure:
  471. * - hw->aq.num_asq_entries
  472. * - hw->aq.num_arq_entries
  473. * - hw->aq.arq_buf_size
  474. * - hw->aq.asq_buf_size
  475. **/
  476. i40e_status i40e_init_adminq(struct i40e_hw *hw)
  477. {
  478. i40e_status ret_code;
  479. u16 eetrack_lo, eetrack_hi;
  480. int retry = 0;
  481. /* verify input for valid configuration */
  482. if ((hw->aq.num_arq_entries == 0) ||
  483. (hw->aq.num_asq_entries == 0) ||
  484. (hw->aq.arq_buf_size == 0) ||
  485. (hw->aq.asq_buf_size == 0)) {
  486. ret_code = I40E_ERR_CONFIG;
  487. goto init_adminq_exit;
  488. }
  489. /* initialize locks */
  490. mutex_init(&hw->aq.asq_mutex);
  491. mutex_init(&hw->aq.arq_mutex);
  492. /* Set up register offsets */
  493. i40e_adminq_init_regs(hw);
  494. /* allocate the ASQ */
  495. ret_code = i40e_init_asq(hw);
  496. if (ret_code)
  497. goto init_adminq_destroy_locks;
  498. /* allocate the ARQ */
  499. ret_code = i40e_init_arq(hw);
  500. if (ret_code)
  501. goto init_adminq_free_asq;
  502. /* There are some cases where the firmware may not be quite ready
  503. * for AdminQ operations, so we retry the AdminQ setup a few times
  504. * if we see timeouts in this first AQ call.
  505. */
  506. do {
  507. ret_code = i40e_aq_get_firmware_version(hw,
  508. &hw->aq.fw_maj_ver,
  509. &hw->aq.fw_min_ver,
  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_IMAGE_VERSION, &hw->nvm.version);
  523. i40e_read_nvm_word(hw, I40E_SR_NVM_EETRACK_LO, &eetrack_lo);
  524. i40e_read_nvm_word(hw, I40E_SR_NVM_EETRACK_HI, &eetrack_hi);
  525. hw->nvm.eetrack = (eetrack_hi << 16) | eetrack_lo;
  526. if (hw->aq.api_maj_ver > I40E_FW_API_VERSION_MAJOR) {
  527. ret_code = I40E_ERR_FIRMWARE_API_VERSION;
  528. goto init_adminq_free_arq;
  529. }
  530. /* pre-emptive resource lock release */
  531. i40e_aq_release_resource(hw, I40E_NVM_RESOURCE_ID, 0, NULL);
  532. hw->aq.nvm_busy = false;
  533. ret_code = i40e_aq_set_hmc_resource_profile(hw,
  534. I40E_HMC_PROFILE_DEFAULT,
  535. 0,
  536. NULL);
  537. ret_code = 0;
  538. /* success! */
  539. goto init_adminq_exit;
  540. init_adminq_free_arq:
  541. i40e_shutdown_arq(hw);
  542. init_adminq_free_asq:
  543. i40e_shutdown_asq(hw);
  544. init_adminq_destroy_locks:
  545. init_adminq_exit:
  546. return ret_code;
  547. }
  548. /**
  549. * i40e_shutdown_adminq - shutdown routine for the Admin Queue
  550. * @hw: pointer to the hardware structure
  551. **/
  552. i40e_status i40e_shutdown_adminq(struct i40e_hw *hw)
  553. {
  554. i40e_status ret_code = 0;
  555. if (i40e_check_asq_alive(hw))
  556. i40e_aq_queue_shutdown(hw, true);
  557. i40e_shutdown_asq(hw);
  558. i40e_shutdown_arq(hw);
  559. /* destroy the locks */
  560. return ret_code;
  561. }
  562. /**
  563. * i40e_clean_asq - cleans Admin send queue
  564. * @hw: pointer to the hardware structure
  565. *
  566. * returns the number of free desc
  567. **/
  568. static u16 i40e_clean_asq(struct i40e_hw *hw)
  569. {
  570. struct i40e_adminq_ring *asq = &(hw->aq.asq);
  571. struct i40e_asq_cmd_details *details;
  572. u16 ntc = asq->next_to_clean;
  573. struct i40e_aq_desc desc_cb;
  574. struct i40e_aq_desc *desc;
  575. desc = I40E_ADMINQ_DESC(*asq, ntc);
  576. details = I40E_ADMINQ_DETAILS(*asq, ntc);
  577. while (rd32(hw, hw->aq.asq.head) != ntc) {
  578. if (details->callback) {
  579. I40E_ADMINQ_CALLBACK cb_func =
  580. (I40E_ADMINQ_CALLBACK)details->callback;
  581. desc_cb = *desc;
  582. cb_func(hw, &desc_cb);
  583. }
  584. memset(desc, 0, sizeof(*desc));
  585. memset(details, 0, sizeof(*details));
  586. ntc++;
  587. if (ntc == asq->count)
  588. ntc = 0;
  589. desc = I40E_ADMINQ_DESC(*asq, ntc);
  590. details = I40E_ADMINQ_DETAILS(*asq, ntc);
  591. }
  592. asq->next_to_clean = ntc;
  593. return I40E_DESC_UNUSED(asq);
  594. }
  595. /**
  596. * i40e_asq_done - check if FW has processed the Admin Send Queue
  597. * @hw: pointer to the hw struct
  598. *
  599. * Returns true if the firmware has processed all descriptors on the
  600. * admin send queue. Returns false if there are still requests pending.
  601. **/
  602. static bool i40e_asq_done(struct i40e_hw *hw)
  603. {
  604. /* AQ designers suggest use of head for better
  605. * timing reliability than DD bit
  606. */
  607. return rd32(hw, hw->aq.asq.head) == hw->aq.asq.next_to_use;
  608. }
  609. /**
  610. * i40e_asq_send_command - send command to Admin Queue
  611. * @hw: pointer to the hw struct
  612. * @desc: prefilled descriptor describing the command (non DMA mem)
  613. * @buff: buffer to use for indirect commands
  614. * @buff_size: size of buffer for indirect commands
  615. * @cmd_details: pointer to command details structure
  616. *
  617. * This is the main send command driver routine for the Admin Queue send
  618. * queue. It runs the queue, cleans the queue, etc
  619. **/
  620. i40e_status i40e_asq_send_command(struct i40e_hw *hw,
  621. struct i40e_aq_desc *desc,
  622. void *buff, /* can be NULL */
  623. u16 buff_size,
  624. struct i40e_asq_cmd_details *cmd_details)
  625. {
  626. i40e_status status = 0;
  627. struct i40e_dma_mem *dma_buff = NULL;
  628. struct i40e_asq_cmd_details *details;
  629. struct i40e_aq_desc *desc_on_ring;
  630. bool cmd_completed = false;
  631. u16 retval = 0;
  632. if (hw->aq.asq.count == 0) {
  633. i40e_debug(hw, I40E_DEBUG_AQ_MESSAGE,
  634. "AQTX: Admin queue not initialized.\n");
  635. status = I40E_ERR_QUEUE_EMPTY;
  636. goto asq_send_command_exit;
  637. }
  638. if (i40e_is_nvm_update_op(desc) && hw->aq.nvm_busy) {
  639. i40e_debug(hw, I40E_DEBUG_AQ_MESSAGE, "AQTX: NVM busy.\n");
  640. status = I40E_ERR_NVM;
  641. goto asq_send_command_exit;
  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. mutex_lock(&hw->aq.asq_mutex);
  663. if (buff_size > hw->aq.asq_buf_size) {
  664. i40e_debug(hw,
  665. I40E_DEBUG_AQ_MESSAGE,
  666. "AQTX: Invalid buffer size: %d.\n",
  667. buff_size);
  668. status = I40E_ERR_INVALID_SIZE;
  669. goto asq_send_command_error;
  670. }
  671. if (details->postpone && !details->async) {
  672. i40e_debug(hw,
  673. I40E_DEBUG_AQ_MESSAGE,
  674. "AQTX: Async flag not set along with postpone flag");
  675. status = I40E_ERR_PARAM;
  676. goto asq_send_command_error;
  677. }
  678. /* call clean and check queue available function to reclaim the
  679. * descriptors that were processed by FW, the function returns the
  680. * number of desc available
  681. */
  682. /* the clean function called here could be called in a separate thread
  683. * in case of asynchronous completions
  684. */
  685. if (i40e_clean_asq(hw) == 0) {
  686. i40e_debug(hw,
  687. I40E_DEBUG_AQ_MESSAGE,
  688. "AQTX: Error queue is full.\n");
  689. status = I40E_ERR_ADMIN_QUEUE_FULL;
  690. goto asq_send_command_error;
  691. }
  692. /* initialize the temp desc pointer with the right desc */
  693. desc_on_ring = I40E_ADMINQ_DESC(hw->aq.asq, hw->aq.asq.next_to_use);
  694. /* if the desc is available copy the temp desc to the right place */
  695. *desc_on_ring = *desc;
  696. /* if buff is not NULL assume indirect command */
  697. if (buff != NULL) {
  698. dma_buff = &(hw->aq.asq.r.asq_bi[hw->aq.asq.next_to_use]);
  699. /* copy the user buff into the respective DMA buff */
  700. memcpy(dma_buff->va, buff, buff_size);
  701. desc_on_ring->datalen = cpu_to_le16(buff_size);
  702. /* Update the address values in the desc with the pa value
  703. * for respective buffer
  704. */
  705. desc_on_ring->params.external.addr_high =
  706. cpu_to_le32(upper_32_bits(dma_buff->pa));
  707. desc_on_ring->params.external.addr_low =
  708. cpu_to_le32(lower_32_bits(dma_buff->pa));
  709. }
  710. /* bump the tail */
  711. i40e_debug_aq(hw, I40E_DEBUG_AQ_COMMAND, (void *)desc_on_ring, buff);
  712. (hw->aq.asq.next_to_use)++;
  713. if (hw->aq.asq.next_to_use == hw->aq.asq.count)
  714. hw->aq.asq.next_to_use = 0;
  715. if (!details->postpone)
  716. wr32(hw, hw->aq.asq.tail, hw->aq.asq.next_to_use);
  717. /* if cmd_details are not defined or async flag is not set,
  718. * we need to wait for desc write back
  719. */
  720. if (!details->async && !details->postpone) {
  721. u32 total_delay = 0;
  722. u32 delay_len = 10;
  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. /* ugh! delay while spin_lock */
  730. udelay(delay_len);
  731. total_delay += delay_len;
  732. } while (total_delay < I40E_ASQ_CMD_TIMEOUT);
  733. }
  734. /* if ready, copy the desc back to temp */
  735. if (i40e_asq_done(hw)) {
  736. *desc = *desc_on_ring;
  737. if (buff != NULL)
  738. memcpy(buff, dma_buff->va, buff_size);
  739. retval = le16_to_cpu(desc->retval);
  740. if (retval != 0) {
  741. i40e_debug(hw,
  742. I40E_DEBUG_AQ_MESSAGE,
  743. "AQTX: Command completed with error 0x%X.\n",
  744. retval);
  745. /* strip off FW internal code */
  746. retval &= 0xff;
  747. }
  748. cmd_completed = true;
  749. if ((enum i40e_admin_queue_err)retval == I40E_AQ_RC_OK)
  750. status = 0;
  751. else
  752. status = I40E_ERR_ADMIN_QUEUE_ERROR;
  753. hw->aq.asq_last_status = (enum i40e_admin_queue_err)retval;
  754. }
  755. if (i40e_is_nvm_update_op(desc))
  756. hw->aq.nvm_busy = true;
  757. /* update the error if time out occurred */
  758. if ((!cmd_completed) &&
  759. (!details->async && !details->postpone)) {
  760. i40e_debug(hw,
  761. I40E_DEBUG_AQ_MESSAGE,
  762. "AQTX: Writeback timeout.\n");
  763. status = I40E_ERR_ADMIN_QUEUE_TIMEOUT;
  764. }
  765. asq_send_command_error:
  766. mutex_unlock(&hw->aq.asq_mutex);
  767. asq_send_command_exit:
  768. return status;
  769. }
  770. /**
  771. * i40e_fill_default_direct_cmd_desc - AQ descriptor helper function
  772. * @desc: pointer to the temp descriptor (non DMA mem)
  773. * @opcode: the opcode can be used to decide which flags to turn off or on
  774. *
  775. * Fill the desc with default values
  776. **/
  777. void i40e_fill_default_direct_cmd_desc(struct i40e_aq_desc *desc,
  778. u16 opcode)
  779. {
  780. /* zero out the desc */
  781. memset((void *)desc, 0, sizeof(struct i40e_aq_desc));
  782. desc->opcode = cpu_to_le16(opcode);
  783. desc->flags = cpu_to_le16(I40E_AQ_FLAG_SI);
  784. }
  785. /**
  786. * i40e_clean_arq_element
  787. * @hw: pointer to the hw struct
  788. * @e: event info from the receive descriptor, includes any buffers
  789. * @pending: number of events that could be left to process
  790. *
  791. * This function cleans one Admin Receive Queue element and returns
  792. * the contents through e. It can also return how many events are
  793. * left to process through 'pending'
  794. **/
  795. i40e_status i40e_clean_arq_element(struct i40e_hw *hw,
  796. struct i40e_arq_event_info *e,
  797. u16 *pending)
  798. {
  799. i40e_status ret_code = 0;
  800. u16 ntc = hw->aq.arq.next_to_clean;
  801. struct i40e_aq_desc *desc;
  802. struct i40e_dma_mem *bi;
  803. u16 desc_idx;
  804. u16 datalen;
  805. u16 flags;
  806. u16 ntu;
  807. /* take the lock before we start messing with the ring */
  808. mutex_lock(&hw->aq.arq_mutex);
  809. /* set next_to_use to head */
  810. ntu = (rd32(hw, hw->aq.arq.head) & I40E_PF_ARQH_ARQH_MASK);
  811. if (ntu == ntc) {
  812. /* nothing to do - shouldn't need to update ring's values */
  813. i40e_debug(hw,
  814. I40E_DEBUG_AQ_MESSAGE,
  815. "AQRX: Queue is empty.\n");
  816. ret_code = I40E_ERR_ADMIN_QUEUE_NO_WORK;
  817. goto clean_arq_element_out;
  818. }
  819. /* now clean the next descriptor */
  820. desc = I40E_ADMINQ_DESC(hw->aq.arq, ntc);
  821. desc_idx = ntc;
  822. i40e_debug_aq(hw,
  823. I40E_DEBUG_AQ_COMMAND,
  824. (void *)desc,
  825. hw->aq.arq.r.arq_bi[desc_idx].va);
  826. flags = le16_to_cpu(desc->flags);
  827. if (flags & I40E_AQ_FLAG_ERR) {
  828. ret_code = I40E_ERR_ADMIN_QUEUE_ERROR;
  829. hw->aq.arq_last_status =
  830. (enum i40e_admin_queue_err)le16_to_cpu(desc->retval);
  831. i40e_debug(hw,
  832. I40E_DEBUG_AQ_MESSAGE,
  833. "AQRX: Event received with error 0x%X.\n",
  834. hw->aq.arq_last_status);
  835. } else {
  836. e->desc = *desc;
  837. datalen = le16_to_cpu(desc->datalen);
  838. e->msg_size = min(datalen, e->msg_size);
  839. if (e->msg_buf != NULL && (e->msg_size != 0))
  840. memcpy(e->msg_buf, hw->aq.arq.r.arq_bi[desc_idx].va,
  841. e->msg_size);
  842. }
  843. if (i40e_is_nvm_update_op(&e->desc))
  844. hw->aq.nvm_busy = false;
  845. /* Restore the original datalen and buffer address in the desc,
  846. * FW updates datalen to indicate the event message
  847. * size
  848. */
  849. bi = &hw->aq.arq.r.arq_bi[ntc];
  850. memset((void *)desc, 0, sizeof(struct i40e_aq_desc));
  851. desc->flags = cpu_to_le16(I40E_AQ_FLAG_BUF);
  852. if (hw->aq.arq_buf_size > I40E_AQ_LARGE_BUF)
  853. desc->flags |= cpu_to_le16(I40E_AQ_FLAG_LB);
  854. desc->datalen = cpu_to_le16((u16)bi->size);
  855. desc->params.external.addr_high = cpu_to_le32(upper_32_bits(bi->pa));
  856. desc->params.external.addr_low = cpu_to_le32(lower_32_bits(bi->pa));
  857. /* set tail = the last cleaned desc index. */
  858. wr32(hw, hw->aq.arq.tail, ntc);
  859. /* ntc is updated to tail + 1 */
  860. ntc++;
  861. if (ntc == hw->aq.num_arq_entries)
  862. ntc = 0;
  863. hw->aq.arq.next_to_clean = ntc;
  864. hw->aq.arq.next_to_use = ntu;
  865. clean_arq_element_out:
  866. /* Set pending if needed, unlock and return */
  867. if (pending != NULL)
  868. *pending = (ntc > ntu ? hw->aq.arq.count : 0) + (ntu - ntc);
  869. mutex_unlock(&hw->aq.arq_mutex);
  870. return ret_code;
  871. }
  872. static void i40e_resume_aq(struct i40e_hw *hw)
  873. {
  874. /* Registers are reset after PF reset */
  875. hw->aq.asq.next_to_use = 0;
  876. hw->aq.asq.next_to_clean = 0;
  877. i40e_config_asq_regs(hw);
  878. hw->aq.arq.next_to_use = 0;
  879. hw->aq.arq.next_to_clean = 0;
  880. i40e_config_arq_regs(hw);
  881. }