ice_controlq.c 30 KB

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