i40e_adminq.c 28 KB

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