ice_controlq.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright (c) 2018, Intel Corporation. */
  3. #include "ice_common.h"
  4. /**
  5. * ice_adminq_init_regs - Initialize AdminQ registers
  6. * @hw: pointer to the hardware structure
  7. *
  8. * This assumes the alloc_sq and alloc_rq functions have already been called
  9. */
  10. static void ice_adminq_init_regs(struct ice_hw *hw)
  11. {
  12. struct ice_ctl_q_info *cq = &hw->adminq;
  13. cq->sq.head = PF_FW_ATQH;
  14. cq->sq.tail = PF_FW_ATQT;
  15. cq->sq.len = PF_FW_ATQLEN;
  16. cq->sq.bah = PF_FW_ATQBAH;
  17. cq->sq.bal = PF_FW_ATQBAL;
  18. cq->sq.len_mask = PF_FW_ATQLEN_ATQLEN_M;
  19. cq->sq.len_ena_mask = PF_FW_ATQLEN_ATQENABLE_M;
  20. cq->sq.head_mask = PF_FW_ATQH_ATQH_M;
  21. cq->rq.head = PF_FW_ARQH;
  22. cq->rq.tail = PF_FW_ARQT;
  23. cq->rq.len = PF_FW_ARQLEN;
  24. cq->rq.bah = PF_FW_ARQBAH;
  25. cq->rq.bal = PF_FW_ARQBAL;
  26. cq->rq.len_mask = PF_FW_ARQLEN_ARQLEN_M;
  27. cq->rq.len_ena_mask = PF_FW_ARQLEN_ARQENABLE_M;
  28. cq->rq.head_mask = PF_FW_ARQH_ARQH_M;
  29. }
  30. /**
  31. * ice_check_sq_alive
  32. * @hw: pointer to the hw struct
  33. * @cq: pointer to the specific Control queue
  34. *
  35. * Returns true if Queue is enabled else false.
  36. */
  37. bool ice_check_sq_alive(struct ice_hw *hw, struct ice_ctl_q_info *cq)
  38. {
  39. /* check both queue-length and queue-enable fields */
  40. if (cq->sq.len && cq->sq.len_mask && cq->sq.len_ena_mask)
  41. return (rd32(hw, cq->sq.len) & (cq->sq.len_mask |
  42. cq->sq.len_ena_mask)) ==
  43. (cq->num_sq_entries | cq->sq.len_ena_mask);
  44. return false;
  45. }
  46. /**
  47. * ice_alloc_ctrlq_sq_ring - Allocate Control Transmit Queue (ATQ) rings
  48. * @hw: pointer to the hardware structure
  49. * @cq: pointer to the specific Control queue
  50. */
  51. static enum ice_status
  52. ice_alloc_ctrlq_sq_ring(struct ice_hw *hw, struct ice_ctl_q_info *cq)
  53. {
  54. size_t size = cq->num_sq_entries * sizeof(struct ice_aq_desc);
  55. cq->sq.desc_buf.va = dmam_alloc_coherent(ice_hw_to_dev(hw), size,
  56. &cq->sq.desc_buf.pa,
  57. GFP_KERNEL | __GFP_ZERO);
  58. if (!cq->sq.desc_buf.va)
  59. return ICE_ERR_NO_MEMORY;
  60. cq->sq.desc_buf.size = size;
  61. cq->sq.cmd_buf = devm_kcalloc(ice_hw_to_dev(hw), cq->num_sq_entries,
  62. sizeof(struct ice_sq_cd), GFP_KERNEL);
  63. if (!cq->sq.cmd_buf) {
  64. dmam_free_coherent(ice_hw_to_dev(hw), cq->sq.desc_buf.size,
  65. cq->sq.desc_buf.va, cq->sq.desc_buf.pa);
  66. cq->sq.desc_buf.va = NULL;
  67. cq->sq.desc_buf.pa = 0;
  68. cq->sq.desc_buf.size = 0;
  69. return ICE_ERR_NO_MEMORY;
  70. }
  71. return 0;
  72. }
  73. /**
  74. * ice_alloc_ctrlq_rq_ring - Allocate Control Receive Queue (ARQ) rings
  75. * @hw: pointer to the hardware structure
  76. * @cq: pointer to the specific Control queue
  77. */
  78. static enum ice_status
  79. ice_alloc_ctrlq_rq_ring(struct ice_hw *hw, struct ice_ctl_q_info *cq)
  80. {
  81. size_t size = cq->num_rq_entries * sizeof(struct ice_aq_desc);
  82. cq->rq.desc_buf.va = dmam_alloc_coherent(ice_hw_to_dev(hw), size,
  83. &cq->rq.desc_buf.pa,
  84. GFP_KERNEL | __GFP_ZERO);
  85. if (!cq->rq.desc_buf.va)
  86. return ICE_ERR_NO_MEMORY;
  87. cq->rq.desc_buf.size = size;
  88. return 0;
  89. }
  90. /**
  91. * ice_free_ctrlq_sq_ring - Free Control Transmit Queue (ATQ) rings
  92. * @hw: pointer to the hardware structure
  93. * @cq: pointer to the specific Control queue
  94. *
  95. * This assumes the posted send buffers have already been cleaned
  96. * and de-allocated
  97. */
  98. static void ice_free_ctrlq_sq_ring(struct ice_hw *hw, struct ice_ctl_q_info *cq)
  99. {
  100. dmam_free_coherent(ice_hw_to_dev(hw), cq->sq.desc_buf.size,
  101. cq->sq.desc_buf.va, cq->sq.desc_buf.pa);
  102. cq->sq.desc_buf.va = NULL;
  103. cq->sq.desc_buf.pa = 0;
  104. cq->sq.desc_buf.size = 0;
  105. }
  106. /**
  107. * ice_free_ctrlq_rq_ring - Free Control Receive Queue (ARQ) rings
  108. * @hw: pointer to the hardware structure
  109. * @cq: pointer to the specific Control queue
  110. *
  111. * This assumes the posted receive buffers have already been cleaned
  112. * and de-allocated
  113. */
  114. static void ice_free_ctrlq_rq_ring(struct ice_hw *hw, struct ice_ctl_q_info *cq)
  115. {
  116. dmam_free_coherent(ice_hw_to_dev(hw), cq->rq.desc_buf.size,
  117. cq->rq.desc_buf.va, cq->rq.desc_buf.pa);
  118. cq->rq.desc_buf.va = NULL;
  119. cq->rq.desc_buf.pa = 0;
  120. cq->rq.desc_buf.size = 0;
  121. }
  122. /**
  123. * ice_alloc_rq_bufs - Allocate pre-posted buffers for the ARQ
  124. * @hw: pointer to the hardware structure
  125. * @cq: pointer to the specific Control queue
  126. */
  127. static enum ice_status
  128. ice_alloc_rq_bufs(struct ice_hw *hw, struct ice_ctl_q_info *cq)
  129. {
  130. int i;
  131. /* We'll be allocating the buffer info memory first, then we can
  132. * allocate the mapped buffers for the event processing
  133. */
  134. cq->rq.dma_head = devm_kcalloc(ice_hw_to_dev(hw), cq->num_rq_entries,
  135. sizeof(cq->rq.desc_buf), GFP_KERNEL);
  136. if (!cq->rq.dma_head)
  137. return ICE_ERR_NO_MEMORY;
  138. cq->rq.r.rq_bi = (struct ice_dma_mem *)cq->rq.dma_head;
  139. /* allocate the mapped buffers */
  140. for (i = 0; i < cq->num_rq_entries; i++) {
  141. struct ice_aq_desc *desc;
  142. struct ice_dma_mem *bi;
  143. bi = &cq->rq.r.rq_bi[i];
  144. bi->va = dmam_alloc_coherent(ice_hw_to_dev(hw),
  145. cq->rq_buf_size, &bi->pa,
  146. GFP_KERNEL | __GFP_ZERO);
  147. if (!bi->va)
  148. goto unwind_alloc_rq_bufs;
  149. bi->size = cq->rq_buf_size;
  150. /* now configure the descriptors for use */
  151. desc = ICE_CTL_Q_DESC(cq->rq, i);
  152. desc->flags = cpu_to_le16(ICE_AQ_FLAG_BUF);
  153. if (cq->rq_buf_size > ICE_AQ_LG_BUF)
  154. desc->flags |= cpu_to_le16(ICE_AQ_FLAG_LB);
  155. desc->opcode = 0;
  156. /* This is in accordance with Admin queue design, there is no
  157. * register for buffer size configuration
  158. */
  159. desc->datalen = cpu_to_le16(bi->size);
  160. desc->retval = 0;
  161. desc->cookie_high = 0;
  162. desc->cookie_low = 0;
  163. desc->params.generic.addr_high =
  164. cpu_to_le32(upper_32_bits(bi->pa));
  165. desc->params.generic.addr_low =
  166. cpu_to_le32(lower_32_bits(bi->pa));
  167. desc->params.generic.param0 = 0;
  168. desc->params.generic.param1 = 0;
  169. }
  170. return 0;
  171. unwind_alloc_rq_bufs:
  172. /* don't try to free the one that failed... */
  173. i--;
  174. for (; i >= 0; i--) {
  175. dmam_free_coherent(ice_hw_to_dev(hw), cq->rq.r.rq_bi[i].size,
  176. cq->rq.r.rq_bi[i].va, cq->rq.r.rq_bi[i].pa);
  177. cq->rq.r.rq_bi[i].va = NULL;
  178. cq->rq.r.rq_bi[i].pa = 0;
  179. cq->rq.r.rq_bi[i].size = 0;
  180. }
  181. devm_kfree(ice_hw_to_dev(hw), cq->rq.dma_head);
  182. return ICE_ERR_NO_MEMORY;
  183. }
  184. /**
  185. * ice_alloc_sq_bufs - Allocate empty buffer structs for the ATQ
  186. * @hw: pointer to the hardware structure
  187. * @cq: pointer to the specific Control queue
  188. */
  189. static enum ice_status
  190. ice_alloc_sq_bufs(struct ice_hw *hw, struct ice_ctl_q_info *cq)
  191. {
  192. int i;
  193. /* No mapped memory needed yet, just the buffer info structures */
  194. cq->sq.dma_head = devm_kcalloc(ice_hw_to_dev(hw), cq->num_sq_entries,
  195. sizeof(cq->sq.desc_buf), GFP_KERNEL);
  196. if (!cq->sq.dma_head)
  197. return ICE_ERR_NO_MEMORY;
  198. cq->sq.r.sq_bi = (struct ice_dma_mem *)cq->sq.dma_head;
  199. /* allocate the mapped buffers */
  200. for (i = 0; i < cq->num_sq_entries; i++) {
  201. struct ice_dma_mem *bi;
  202. bi = &cq->sq.r.sq_bi[i];
  203. bi->va = dmam_alloc_coherent(ice_hw_to_dev(hw),
  204. cq->sq_buf_size, &bi->pa,
  205. GFP_KERNEL | __GFP_ZERO);
  206. if (!bi->va)
  207. goto unwind_alloc_sq_bufs;
  208. bi->size = cq->sq_buf_size;
  209. }
  210. return 0;
  211. unwind_alloc_sq_bufs:
  212. /* don't try to free the one that failed... */
  213. i--;
  214. for (; i >= 0; i--) {
  215. dmam_free_coherent(ice_hw_to_dev(hw), cq->sq.r.sq_bi[i].size,
  216. cq->sq.r.sq_bi[i].va, cq->sq.r.sq_bi[i].pa);
  217. cq->sq.r.sq_bi[i].va = NULL;
  218. cq->sq.r.sq_bi[i].pa = 0;
  219. cq->sq.r.sq_bi[i].size = 0;
  220. }
  221. devm_kfree(ice_hw_to_dev(hw), cq->sq.dma_head);
  222. return ICE_ERR_NO_MEMORY;
  223. }
  224. /**
  225. * ice_free_rq_bufs - Free ARQ buffer info elements
  226. * @hw: pointer to the hardware structure
  227. * @cq: pointer to the specific Control queue
  228. */
  229. static void ice_free_rq_bufs(struct ice_hw *hw, struct ice_ctl_q_info *cq)
  230. {
  231. int i;
  232. /* free descriptors */
  233. for (i = 0; i < cq->num_rq_entries; i++) {
  234. dmam_free_coherent(ice_hw_to_dev(hw), cq->rq.r.rq_bi[i].size,
  235. cq->rq.r.rq_bi[i].va, cq->rq.r.rq_bi[i].pa);
  236. cq->rq.r.rq_bi[i].va = NULL;
  237. cq->rq.r.rq_bi[i].pa = 0;
  238. cq->rq.r.rq_bi[i].size = 0;
  239. }
  240. /* free the dma header */
  241. devm_kfree(ice_hw_to_dev(hw), cq->rq.dma_head);
  242. }
  243. /**
  244. * ice_free_sq_bufs - Free ATQ buffer info elements
  245. * @hw: pointer to the hardware structure
  246. * @cq: pointer to the specific Control queue
  247. */
  248. static void ice_free_sq_bufs(struct ice_hw *hw, struct ice_ctl_q_info *cq)
  249. {
  250. int i;
  251. /* only unmap if the address is non-NULL */
  252. for (i = 0; i < cq->num_sq_entries; i++)
  253. if (cq->sq.r.sq_bi[i].pa) {
  254. dmam_free_coherent(ice_hw_to_dev(hw),
  255. cq->sq.r.sq_bi[i].size,
  256. cq->sq.r.sq_bi[i].va,
  257. cq->sq.r.sq_bi[i].pa);
  258. cq->sq.r.sq_bi[i].va = NULL;
  259. cq->sq.r.sq_bi[i].pa = 0;
  260. cq->sq.r.sq_bi[i].size = 0;
  261. }
  262. /* free the buffer info list */
  263. devm_kfree(ice_hw_to_dev(hw), cq->sq.cmd_buf);
  264. /* free the dma header */
  265. devm_kfree(ice_hw_to_dev(hw), cq->sq.dma_head);
  266. }
  267. /**
  268. * ice_cfg_sq_regs - configure Control ATQ registers
  269. * @hw: pointer to the hardware structure
  270. * @cq: pointer to the specific Control queue
  271. *
  272. * Configure base address and length registers for the transmit queue
  273. */
  274. static enum ice_status
  275. ice_cfg_sq_regs(struct ice_hw *hw, struct ice_ctl_q_info *cq)
  276. {
  277. u32 reg = 0;
  278. /* Clear Head and Tail */
  279. wr32(hw, cq->sq.head, 0);
  280. wr32(hw, cq->sq.tail, 0);
  281. /* set starting point */
  282. wr32(hw, cq->sq.len, (cq->num_sq_entries | cq->sq.len_ena_mask));
  283. wr32(hw, cq->sq.bal, lower_32_bits(cq->sq.desc_buf.pa));
  284. wr32(hw, cq->sq.bah, upper_32_bits(cq->sq.desc_buf.pa));
  285. /* Check one register to verify that config was applied */
  286. reg = rd32(hw, cq->sq.bal);
  287. if (reg != lower_32_bits(cq->sq.desc_buf.pa))
  288. return ICE_ERR_AQ_ERROR;
  289. return 0;
  290. }
  291. /**
  292. * ice_cfg_rq_regs - configure Control ARQ register
  293. * @hw: pointer to the hardware structure
  294. * @cq: pointer to the specific Control queue
  295. *
  296. * Configure base address and length registers for the receive (event q)
  297. */
  298. static enum ice_status
  299. ice_cfg_rq_regs(struct ice_hw *hw, struct ice_ctl_q_info *cq)
  300. {
  301. u32 reg = 0;
  302. /* Clear Head and Tail */
  303. wr32(hw, cq->rq.head, 0);
  304. wr32(hw, cq->rq.tail, 0);
  305. /* set starting point */
  306. wr32(hw, cq->rq.len, (cq->num_rq_entries | cq->rq.len_ena_mask));
  307. wr32(hw, cq->rq.bal, lower_32_bits(cq->rq.desc_buf.pa));
  308. wr32(hw, cq->rq.bah, upper_32_bits(cq->rq.desc_buf.pa));
  309. /* Update tail in the HW to post pre-allocated buffers */
  310. wr32(hw, cq->rq.tail, (u32)(cq->num_rq_entries - 1));
  311. /* Check one register to verify that config was applied */
  312. reg = rd32(hw, cq->rq.bal);
  313. if (reg != lower_32_bits(cq->rq.desc_buf.pa))
  314. return ICE_ERR_AQ_ERROR;
  315. return 0;
  316. }
  317. /**
  318. * ice_init_sq - main initialization routine for Control ATQ
  319. * @hw: pointer to the hardware structure
  320. * @cq: pointer to the specific Control queue
  321. *
  322. * This is the main initialization routine for the Control Send Queue
  323. * Prior to calling this function, drivers *MUST* set the following fields
  324. * in the cq->structure:
  325. * - cq->num_sq_entries
  326. * - cq->sq_buf_size
  327. *
  328. * Do *NOT* hold the lock when calling this as the memory allocation routines
  329. * called are not going to be atomic context safe
  330. */
  331. static enum ice_status ice_init_sq(struct ice_hw *hw, struct ice_ctl_q_info *cq)
  332. {
  333. enum ice_status ret_code;
  334. if (cq->sq.count > 0) {
  335. /* queue already initialized */
  336. ret_code = ICE_ERR_NOT_READY;
  337. goto init_ctrlq_exit;
  338. }
  339. /* verify input for valid configuration */
  340. if (!cq->num_sq_entries || !cq->sq_buf_size) {
  341. ret_code = ICE_ERR_CFG;
  342. goto init_ctrlq_exit;
  343. }
  344. cq->sq.next_to_use = 0;
  345. cq->sq.next_to_clean = 0;
  346. /* allocate the ring memory */
  347. ret_code = ice_alloc_ctrlq_sq_ring(hw, cq);
  348. if (ret_code)
  349. goto init_ctrlq_exit;
  350. /* allocate buffers in the rings */
  351. ret_code = ice_alloc_sq_bufs(hw, cq);
  352. if (ret_code)
  353. goto init_ctrlq_free_rings;
  354. /* initialize base registers */
  355. ret_code = ice_cfg_sq_regs(hw, cq);
  356. if (ret_code)
  357. goto init_ctrlq_free_rings;
  358. /* success! */
  359. cq->sq.count = cq->num_sq_entries;
  360. goto init_ctrlq_exit;
  361. init_ctrlq_free_rings:
  362. ice_free_ctrlq_sq_ring(hw, cq);
  363. init_ctrlq_exit:
  364. return ret_code;
  365. }
  366. /**
  367. * ice_init_rq - initialize ARQ
  368. * @hw: pointer to the hardware structure
  369. * @cq: pointer to the specific Control queue
  370. *
  371. * The main initialization routine for the Admin Receive (Event) Queue.
  372. * Prior to calling this function, drivers *MUST* set the following fields
  373. * in the cq->structure:
  374. * - cq->num_rq_entries
  375. * - cq->rq_buf_size
  376. *
  377. * Do *NOT* hold the lock when calling this as the memory allocation routines
  378. * called are not going to be atomic context safe
  379. */
  380. static enum ice_status ice_init_rq(struct ice_hw *hw, struct ice_ctl_q_info *cq)
  381. {
  382. enum ice_status ret_code;
  383. if (cq->rq.count > 0) {
  384. /* queue already initialized */
  385. ret_code = ICE_ERR_NOT_READY;
  386. goto init_ctrlq_exit;
  387. }
  388. /* verify input for valid configuration */
  389. if (!cq->num_rq_entries || !cq->rq_buf_size) {
  390. ret_code = ICE_ERR_CFG;
  391. goto init_ctrlq_exit;
  392. }
  393. cq->rq.next_to_use = 0;
  394. cq->rq.next_to_clean = 0;
  395. /* allocate the ring memory */
  396. ret_code = ice_alloc_ctrlq_rq_ring(hw, cq);
  397. if (ret_code)
  398. goto init_ctrlq_exit;
  399. /* allocate buffers in the rings */
  400. ret_code = ice_alloc_rq_bufs(hw, cq);
  401. if (ret_code)
  402. goto init_ctrlq_free_rings;
  403. /* initialize base registers */
  404. ret_code = ice_cfg_rq_regs(hw, cq);
  405. if (ret_code)
  406. goto init_ctrlq_free_rings;
  407. /* success! */
  408. cq->rq.count = cq->num_rq_entries;
  409. goto init_ctrlq_exit;
  410. init_ctrlq_free_rings:
  411. ice_free_ctrlq_rq_ring(hw, cq);
  412. init_ctrlq_exit:
  413. return ret_code;
  414. }
  415. /**
  416. * ice_shutdown_sq - shutdown the Control ATQ
  417. * @hw: pointer to the hardware structure
  418. * @cq: pointer to the specific Control queue
  419. *
  420. * The main shutdown routine for the Control Transmit Queue
  421. */
  422. static enum ice_status
  423. ice_shutdown_sq(struct ice_hw *hw, struct ice_ctl_q_info *cq)
  424. {
  425. enum ice_status ret_code = 0;
  426. mutex_lock(&cq->sq_lock);
  427. if (!cq->sq.count) {
  428. ret_code = ICE_ERR_NOT_READY;
  429. goto shutdown_sq_out;
  430. }
  431. /* Stop firmware AdminQ processing */
  432. wr32(hw, cq->sq.head, 0);
  433. wr32(hw, cq->sq.tail, 0);
  434. wr32(hw, cq->sq.len, 0);
  435. wr32(hw, cq->sq.bal, 0);
  436. wr32(hw, cq->sq.bah, 0);
  437. cq->sq.count = 0; /* to indicate uninitialized queue */
  438. /* free ring buffers and the ring itself */
  439. ice_free_sq_bufs(hw, cq);
  440. ice_free_ctrlq_sq_ring(hw, cq);
  441. shutdown_sq_out:
  442. mutex_unlock(&cq->sq_lock);
  443. return ret_code;
  444. }
  445. /**
  446. * ice_aq_ver_check - Check the reported AQ API version.
  447. * @fw_branch: The "branch" of FW, typically describes the device type
  448. * @fw_major: The major version of the FW API
  449. * @fw_minor: The minor version increment of the FW API
  450. *
  451. * Checks if the driver should load on a given AQ API version.
  452. *
  453. * Return: 'true' iff the driver should attempt to load. 'false' otherwise.
  454. */
  455. static bool ice_aq_ver_check(u8 fw_branch, u8 fw_major, u8 fw_minor)
  456. {
  457. if (fw_branch != EXP_FW_API_VER_BRANCH)
  458. return false;
  459. if (fw_major != EXP_FW_API_VER_MAJOR)
  460. return false;
  461. if (fw_minor != EXP_FW_API_VER_MINOR)
  462. return false;
  463. return true;
  464. }
  465. /**
  466. * ice_shutdown_rq - shutdown Control ARQ
  467. * @hw: pointer to the hardware structure
  468. * @cq: pointer to the specific Control queue
  469. *
  470. * The main shutdown routine for the Control Receive Queue
  471. */
  472. static enum ice_status
  473. ice_shutdown_rq(struct ice_hw *hw, struct ice_ctl_q_info *cq)
  474. {
  475. enum ice_status ret_code = 0;
  476. mutex_lock(&cq->rq_lock);
  477. if (!cq->rq.count) {
  478. ret_code = ICE_ERR_NOT_READY;
  479. goto shutdown_rq_out;
  480. }
  481. /* Stop Control Queue processing */
  482. wr32(hw, cq->rq.head, 0);
  483. wr32(hw, cq->rq.tail, 0);
  484. wr32(hw, cq->rq.len, 0);
  485. wr32(hw, cq->rq.bal, 0);
  486. wr32(hw, cq->rq.bah, 0);
  487. /* set rq.count to 0 to indicate uninitialized queue */
  488. cq->rq.count = 0;
  489. /* free ring buffers and the ring itself */
  490. ice_free_rq_bufs(hw, cq);
  491. ice_free_ctrlq_rq_ring(hw, cq);
  492. shutdown_rq_out:
  493. mutex_unlock(&cq->rq_lock);
  494. return ret_code;
  495. }
  496. /**
  497. * ice_init_check_adminq - Check version for Admin Queue to know if its alive
  498. * @hw: pointer to the hardware structure
  499. */
  500. static enum ice_status ice_init_check_adminq(struct ice_hw *hw)
  501. {
  502. struct ice_ctl_q_info *cq = &hw->adminq;
  503. enum ice_status status;
  504. status = ice_aq_get_fw_ver(hw, NULL);
  505. if (status)
  506. goto init_ctrlq_free_rq;
  507. if (!ice_aq_ver_check(hw->api_branch, hw->api_maj_ver,
  508. hw->api_min_ver)) {
  509. status = ICE_ERR_FW_API_VER;
  510. goto init_ctrlq_free_rq;
  511. }
  512. return 0;
  513. init_ctrlq_free_rq:
  514. ice_shutdown_rq(hw, cq);
  515. ice_shutdown_sq(hw, cq);
  516. mutex_destroy(&cq->sq_lock);
  517. mutex_destroy(&cq->rq_lock);
  518. return status;
  519. }
  520. /**
  521. * ice_init_ctrlq - main initialization routine for any control Queue
  522. * @hw: pointer to the hardware structure
  523. * @q_type: specific Control queue type
  524. *
  525. * Prior to calling this function, drivers *MUST* set the following fields
  526. * in the cq->structure:
  527. * - cq->num_sq_entries
  528. * - cq->num_rq_entries
  529. * - cq->rq_buf_size
  530. * - cq->sq_buf_size
  531. *
  532. */
  533. static enum ice_status ice_init_ctrlq(struct ice_hw *hw, enum ice_ctl_q q_type)
  534. {
  535. struct ice_ctl_q_info *cq;
  536. enum ice_status ret_code;
  537. switch (q_type) {
  538. case ICE_CTL_Q_ADMIN:
  539. ice_adminq_init_regs(hw);
  540. cq = &hw->adminq;
  541. break;
  542. default:
  543. return ICE_ERR_PARAM;
  544. }
  545. cq->qtype = q_type;
  546. /* verify input for valid configuration */
  547. if (!cq->num_rq_entries || !cq->num_sq_entries ||
  548. !cq->rq_buf_size || !cq->sq_buf_size) {
  549. return ICE_ERR_CFG;
  550. }
  551. mutex_init(&cq->sq_lock);
  552. mutex_init(&cq->rq_lock);
  553. /* setup SQ command write back timeout */
  554. cq->sq_cmd_timeout = ICE_CTL_Q_SQ_CMD_TIMEOUT;
  555. /* allocate the ATQ */
  556. ret_code = ice_init_sq(hw, cq);
  557. if (ret_code)
  558. goto init_ctrlq_destroy_locks;
  559. /* allocate the ARQ */
  560. ret_code = ice_init_rq(hw, cq);
  561. if (ret_code)
  562. goto init_ctrlq_free_sq;
  563. /* success! */
  564. return 0;
  565. init_ctrlq_free_sq:
  566. ice_shutdown_sq(hw, cq);
  567. init_ctrlq_destroy_locks:
  568. mutex_destroy(&cq->sq_lock);
  569. mutex_destroy(&cq->rq_lock);
  570. return ret_code;
  571. }
  572. /**
  573. * ice_init_all_ctrlq - main initialization routine for all control queues
  574. * @hw: pointer to the hardware structure
  575. *
  576. * Prior to calling this function, drivers *MUST* set the following fields
  577. * in the cq->structure for all control queues:
  578. * - cq->num_sq_entries
  579. * - cq->num_rq_entries
  580. * - cq->rq_buf_size
  581. * - cq->sq_buf_size
  582. */
  583. enum ice_status ice_init_all_ctrlq(struct ice_hw *hw)
  584. {
  585. enum ice_status ret_code;
  586. /* Init FW admin queue */
  587. ret_code = ice_init_ctrlq(hw, ICE_CTL_Q_ADMIN);
  588. if (ret_code)
  589. return ret_code;
  590. return ice_init_check_adminq(hw);
  591. }
  592. /**
  593. * ice_shutdown_ctrlq - shutdown routine for any control queue
  594. * @hw: pointer to the hardware structure
  595. * @q_type: specific Control queue type
  596. */
  597. static void ice_shutdown_ctrlq(struct ice_hw *hw, enum ice_ctl_q q_type)
  598. {
  599. struct ice_ctl_q_info *cq;
  600. switch (q_type) {
  601. case ICE_CTL_Q_ADMIN:
  602. cq = &hw->adminq;
  603. if (ice_check_sq_alive(hw, cq))
  604. ice_aq_q_shutdown(hw, true);
  605. break;
  606. default:
  607. return;
  608. }
  609. ice_shutdown_sq(hw, cq);
  610. ice_shutdown_rq(hw, cq);
  611. mutex_destroy(&cq->sq_lock);
  612. mutex_destroy(&cq->rq_lock);
  613. }
  614. /**
  615. * ice_shutdown_all_ctrlq - shutdown routine for all control queues
  616. * @hw: pointer to the hardware structure
  617. */
  618. void ice_shutdown_all_ctrlq(struct ice_hw *hw)
  619. {
  620. /* Shutdown FW admin queue */
  621. ice_shutdown_ctrlq(hw, ICE_CTL_Q_ADMIN);
  622. }
  623. /**
  624. * ice_clean_sq - cleans Admin send queue (ATQ)
  625. * @hw: pointer to the hardware structure
  626. * @cq: pointer to the specific Control queue
  627. *
  628. * returns the number of free desc
  629. */
  630. static u16 ice_clean_sq(struct ice_hw *hw, struct ice_ctl_q_info *cq)
  631. {
  632. struct ice_ctl_q_ring *sq = &cq->sq;
  633. u16 ntc = sq->next_to_clean;
  634. struct ice_sq_cd *details;
  635. struct ice_aq_desc *desc;
  636. desc = ICE_CTL_Q_DESC(*sq, ntc);
  637. details = ICE_CTL_Q_DETAILS(*sq, ntc);
  638. while (rd32(hw, cq->sq.head) != ntc) {
  639. ice_debug(hw, ICE_DBG_AQ_MSG,
  640. "ntc %d head %d.\n", ntc, rd32(hw, cq->sq.head));
  641. memset(desc, 0, sizeof(*desc));
  642. memset(details, 0, sizeof(*details));
  643. ntc++;
  644. if (ntc == sq->count)
  645. ntc = 0;
  646. desc = ICE_CTL_Q_DESC(*sq, ntc);
  647. details = ICE_CTL_Q_DETAILS(*sq, ntc);
  648. }
  649. sq->next_to_clean = ntc;
  650. return ICE_CTL_Q_DESC_UNUSED(sq);
  651. }
  652. /**
  653. * ice_sq_done - check if FW has processed the Admin Send Queue (ATQ)
  654. * @hw: pointer to the hw struct
  655. * @cq: pointer to the specific Control queue
  656. *
  657. * Returns true if the firmware has processed all descriptors on the
  658. * admin send queue. Returns false if there are still requests pending.
  659. */
  660. static bool ice_sq_done(struct ice_hw *hw, struct ice_ctl_q_info *cq)
  661. {
  662. /* AQ designers suggest use of head for better
  663. * timing reliability than DD bit
  664. */
  665. return rd32(hw, cq->sq.head) == cq->sq.next_to_use;
  666. }
  667. /**
  668. * ice_sq_send_cmd - send command to Control Queue (ATQ)
  669. * @hw: pointer to the hw struct
  670. * @cq: pointer to the specific Control queue
  671. * @desc: prefilled descriptor describing the command (non DMA mem)
  672. * @buf: buffer to use for indirect commands (or NULL for direct commands)
  673. * @buf_size: size of buffer for indirect commands (or 0 for direct commands)
  674. * @cd: pointer to command details structure
  675. *
  676. * This is the main send command routine for the ATQ. It runs the q,
  677. * cleans the queue, etc.
  678. */
  679. enum ice_status
  680. ice_sq_send_cmd(struct ice_hw *hw, struct ice_ctl_q_info *cq,
  681. struct ice_aq_desc *desc, void *buf, u16 buf_size,
  682. struct ice_sq_cd *cd)
  683. {
  684. struct ice_dma_mem *dma_buf = NULL;
  685. struct ice_aq_desc *desc_on_ring;
  686. bool cmd_completed = false;
  687. enum ice_status status = 0;
  688. struct ice_sq_cd *details;
  689. u32 total_delay = 0;
  690. u16 retval = 0;
  691. u32 val = 0;
  692. mutex_lock(&cq->sq_lock);
  693. cq->sq_last_status = ICE_AQ_RC_OK;
  694. if (!cq->sq.count) {
  695. ice_debug(hw, ICE_DBG_AQ_MSG,
  696. "Control Send queue not initialized.\n");
  697. status = ICE_ERR_AQ_EMPTY;
  698. goto sq_send_command_error;
  699. }
  700. if ((buf && !buf_size) || (!buf && buf_size)) {
  701. status = ICE_ERR_PARAM;
  702. goto sq_send_command_error;
  703. }
  704. if (buf) {
  705. if (buf_size > cq->sq_buf_size) {
  706. ice_debug(hw, ICE_DBG_AQ_MSG,
  707. "Invalid buffer size for Control Send queue: %d.\n",
  708. buf_size);
  709. status = ICE_ERR_INVAL_SIZE;
  710. goto sq_send_command_error;
  711. }
  712. desc->flags |= cpu_to_le16(ICE_AQ_FLAG_BUF);
  713. if (buf_size > ICE_AQ_LG_BUF)
  714. desc->flags |= cpu_to_le16(ICE_AQ_FLAG_LB);
  715. }
  716. val = rd32(hw, cq->sq.head);
  717. if (val >= cq->num_sq_entries) {
  718. ice_debug(hw, ICE_DBG_AQ_MSG,
  719. "head overrun at %d in the Control Send Queue ring\n",
  720. val);
  721. status = ICE_ERR_AQ_EMPTY;
  722. goto sq_send_command_error;
  723. }
  724. details = ICE_CTL_Q_DETAILS(cq->sq, cq->sq.next_to_use);
  725. if (cd)
  726. memcpy(details, cd, sizeof(*details));
  727. else
  728. memset(details, 0, sizeof(*details));
  729. /* Call clean and check queue available function to reclaim the
  730. * descriptors that were processed by FW/MBX; the function returns the
  731. * number of desc available. The clean function called here could be
  732. * called in a separate thread in case of asynchronous completions.
  733. */
  734. if (ice_clean_sq(hw, cq) == 0) {
  735. ice_debug(hw, ICE_DBG_AQ_MSG,
  736. "Error: Control Send Queue is full.\n");
  737. status = ICE_ERR_AQ_FULL;
  738. goto sq_send_command_error;
  739. }
  740. /* initialize the temp desc pointer with the right desc */
  741. desc_on_ring = ICE_CTL_Q_DESC(cq->sq, cq->sq.next_to_use);
  742. /* if the desc is available copy the temp desc to the right place */
  743. memcpy(desc_on_ring, desc, sizeof(*desc_on_ring));
  744. /* if buf is not NULL assume indirect command */
  745. if (buf) {
  746. dma_buf = &cq->sq.r.sq_bi[cq->sq.next_to_use];
  747. /* copy the user buf into the respective DMA buf */
  748. memcpy(dma_buf->va, buf, buf_size);
  749. desc_on_ring->datalen = cpu_to_le16(buf_size);
  750. /* Update the address values in the desc with the pa value
  751. * for respective buffer
  752. */
  753. desc_on_ring->params.generic.addr_high =
  754. cpu_to_le32(upper_32_bits(dma_buf->pa));
  755. desc_on_ring->params.generic.addr_low =
  756. cpu_to_le32(lower_32_bits(dma_buf->pa));
  757. }
  758. /* Debug desc and buffer */
  759. ice_debug(hw, ICE_DBG_AQ_MSG,
  760. "ATQ: Control Send queue desc and buffer:\n");
  761. ice_debug_cq(hw, ICE_DBG_AQ_CMD, (void *)desc_on_ring, buf, buf_size);
  762. (cq->sq.next_to_use)++;
  763. if (cq->sq.next_to_use == cq->sq.count)
  764. cq->sq.next_to_use = 0;
  765. wr32(hw, cq->sq.tail, cq->sq.next_to_use);
  766. do {
  767. if (ice_sq_done(hw, cq))
  768. break;
  769. mdelay(1);
  770. total_delay++;
  771. } while (total_delay < cq->sq_cmd_timeout);
  772. /* if ready, copy the desc back to temp */
  773. if (ice_sq_done(hw, cq)) {
  774. memcpy(desc, desc_on_ring, sizeof(*desc));
  775. if (buf) {
  776. /* get returned length to copy */
  777. u16 copy_size = le16_to_cpu(desc->datalen);
  778. if (copy_size > buf_size) {
  779. ice_debug(hw, ICE_DBG_AQ_MSG,
  780. "Return len %d > than buf len %d\n",
  781. copy_size, buf_size);
  782. status = ICE_ERR_AQ_ERROR;
  783. } else {
  784. memcpy(buf, dma_buf->va, copy_size);
  785. }
  786. }
  787. retval = le16_to_cpu(desc->retval);
  788. if (retval) {
  789. ice_debug(hw, ICE_DBG_AQ_MSG,
  790. "Control Send Queue command completed with error 0x%x\n",
  791. retval);
  792. /* strip off FW internal code */
  793. retval &= 0xff;
  794. }
  795. cmd_completed = true;
  796. if (!status && retval != ICE_AQ_RC_OK)
  797. status = ICE_ERR_AQ_ERROR;
  798. cq->sq_last_status = (enum ice_aq_err)retval;
  799. }
  800. ice_debug(hw, ICE_DBG_AQ_MSG,
  801. "ATQ: desc and buffer writeback:\n");
  802. ice_debug_cq(hw, ICE_DBG_AQ_CMD, (void *)desc, buf, buf_size);
  803. /* save writeback AQ if requested */
  804. if (details->wb_desc)
  805. memcpy(details->wb_desc, desc_on_ring,
  806. sizeof(*details->wb_desc));
  807. /* update the error if time out occurred */
  808. if (!cmd_completed) {
  809. ice_debug(hw, ICE_DBG_AQ_MSG,
  810. "Control Send Queue Writeback timeout.\n");
  811. status = ICE_ERR_AQ_TIMEOUT;
  812. }
  813. sq_send_command_error:
  814. mutex_unlock(&cq->sq_lock);
  815. return status;
  816. }
  817. /**
  818. * ice_fill_dflt_direct_cmd_desc - AQ descriptor helper function
  819. * @desc: pointer to the temp descriptor (non DMA mem)
  820. * @opcode: the opcode can be used to decide which flags to turn off or on
  821. *
  822. * Fill the desc with default values
  823. */
  824. void ice_fill_dflt_direct_cmd_desc(struct ice_aq_desc *desc, u16 opcode)
  825. {
  826. /* zero out the desc */
  827. memset(desc, 0, sizeof(*desc));
  828. desc->opcode = cpu_to_le16(opcode);
  829. desc->flags = cpu_to_le16(ICE_AQ_FLAG_SI);
  830. }
  831. /**
  832. * ice_clean_rq_elem
  833. * @hw: pointer to the hw struct
  834. * @cq: pointer to the specific Control queue
  835. * @e: event info from the receive descriptor, includes any buffers
  836. * @pending: number of events that could be left to process
  837. *
  838. * This function cleans one Admin Receive Queue element and returns
  839. * the contents through e. It can also return how many events are
  840. * left to process through 'pending'.
  841. */
  842. enum ice_status
  843. ice_clean_rq_elem(struct ice_hw *hw, struct ice_ctl_q_info *cq,
  844. struct ice_rq_event_info *e, u16 *pending)
  845. {
  846. u16 ntc = cq->rq.next_to_clean;
  847. enum ice_status ret_code = 0;
  848. struct ice_aq_desc *desc;
  849. struct ice_dma_mem *bi;
  850. u16 desc_idx;
  851. u16 datalen;
  852. u16 flags;
  853. u16 ntu;
  854. /* pre-clean the event info */
  855. memset(&e->desc, 0, sizeof(e->desc));
  856. /* take the lock before we start messing with the ring */
  857. mutex_lock(&cq->rq_lock);
  858. if (!cq->rq.count) {
  859. ice_debug(hw, ICE_DBG_AQ_MSG,
  860. "Control Receive queue not initialized.\n");
  861. ret_code = ICE_ERR_AQ_EMPTY;
  862. goto clean_rq_elem_err;
  863. }
  864. /* set next_to_use to head */
  865. ntu = (u16)(rd32(hw, cq->rq.head) & cq->rq.head_mask);
  866. if (ntu == ntc) {
  867. /* nothing to do - shouldn't need to update ring's values */
  868. ret_code = ICE_ERR_AQ_NO_WORK;
  869. goto clean_rq_elem_out;
  870. }
  871. /* now clean the next descriptor */
  872. desc = ICE_CTL_Q_DESC(cq->rq, ntc);
  873. desc_idx = ntc;
  874. cq->rq_last_status = (enum ice_aq_err)le16_to_cpu(desc->retval);
  875. flags = le16_to_cpu(desc->flags);
  876. if (flags & ICE_AQ_FLAG_ERR) {
  877. ret_code = ICE_ERR_AQ_ERROR;
  878. ice_debug(hw, ICE_DBG_AQ_MSG,
  879. "Control Receive Queue Event received with error 0x%x\n",
  880. cq->rq_last_status);
  881. }
  882. memcpy(&e->desc, desc, sizeof(e->desc));
  883. datalen = le16_to_cpu(desc->datalen);
  884. e->msg_len = min(datalen, e->buf_len);
  885. if (e->msg_buf && e->msg_len)
  886. memcpy(e->msg_buf, cq->rq.r.rq_bi[desc_idx].va, e->msg_len);
  887. ice_debug(hw, ICE_DBG_AQ_MSG, "ARQ: desc and buffer:\n");
  888. ice_debug_cq(hw, ICE_DBG_AQ_CMD, (void *)desc, e->msg_buf,
  889. cq->rq_buf_size);
  890. /* Restore the original datalen and buffer address in the desc,
  891. * FW updates datalen to indicate the event message size
  892. */
  893. bi = &cq->rq.r.rq_bi[ntc];
  894. memset(desc, 0, sizeof(*desc));
  895. desc->flags = cpu_to_le16(ICE_AQ_FLAG_BUF);
  896. if (cq->rq_buf_size > ICE_AQ_LG_BUF)
  897. desc->flags |= cpu_to_le16(ICE_AQ_FLAG_LB);
  898. desc->datalen = cpu_to_le16(bi->size);
  899. desc->params.generic.addr_high = cpu_to_le32(upper_32_bits(bi->pa));
  900. desc->params.generic.addr_low = cpu_to_le32(lower_32_bits(bi->pa));
  901. /* set tail = the last cleaned desc index. */
  902. wr32(hw, cq->rq.tail, ntc);
  903. /* ntc is updated to tail + 1 */
  904. ntc++;
  905. if (ntc == cq->num_rq_entries)
  906. ntc = 0;
  907. cq->rq.next_to_clean = ntc;
  908. cq->rq.next_to_use = ntu;
  909. clean_rq_elem_out:
  910. /* Set pending if needed, unlock and return */
  911. if (pending)
  912. *pending = (u16)((ntc > ntu ? cq->rq.count : 0) + (ntu - ntc));
  913. clean_rq_elem_err:
  914. mutex_unlock(&cq->rq_lock);
  915. return ret_code;
  916. }