ice_controlq.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077
  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. if (cq->rq.head) {
  515. ice_shutdown_rq(hw, cq);
  516. mutex_destroy(&cq->rq_lock);
  517. }
  518. if (cq->sq.head) {
  519. ice_shutdown_sq(hw, cq);
  520. mutex_destroy(&cq->sq_lock);
  521. }
  522. return status;
  523. }
  524. /**
  525. * ice_init_ctrlq - main initialization routine for any control Queue
  526. * @hw: pointer to the hardware structure
  527. * @q_type: specific Control queue type
  528. *
  529. * Prior to calling this function, drivers *MUST* set the following fields
  530. * in the cq->structure:
  531. * - cq->num_sq_entries
  532. * - cq->num_rq_entries
  533. * - cq->rq_buf_size
  534. * - cq->sq_buf_size
  535. *
  536. */
  537. static enum ice_status ice_init_ctrlq(struct ice_hw *hw, enum ice_ctl_q q_type)
  538. {
  539. struct ice_ctl_q_info *cq;
  540. enum ice_status ret_code;
  541. switch (q_type) {
  542. case ICE_CTL_Q_ADMIN:
  543. ice_adminq_init_regs(hw);
  544. cq = &hw->adminq;
  545. break;
  546. default:
  547. return ICE_ERR_PARAM;
  548. }
  549. cq->qtype = q_type;
  550. /* verify input for valid configuration */
  551. if (!cq->num_rq_entries || !cq->num_sq_entries ||
  552. !cq->rq_buf_size || !cq->sq_buf_size) {
  553. return ICE_ERR_CFG;
  554. }
  555. mutex_init(&cq->sq_lock);
  556. mutex_init(&cq->rq_lock);
  557. /* setup SQ command write back timeout */
  558. cq->sq_cmd_timeout = ICE_CTL_Q_SQ_CMD_TIMEOUT;
  559. /* allocate the ATQ */
  560. ret_code = ice_init_sq(hw, cq);
  561. if (ret_code)
  562. goto init_ctrlq_destroy_locks;
  563. /* allocate the ARQ */
  564. ret_code = ice_init_rq(hw, cq);
  565. if (ret_code)
  566. goto init_ctrlq_free_sq;
  567. /* success! */
  568. return 0;
  569. init_ctrlq_free_sq:
  570. ice_shutdown_sq(hw, cq);
  571. init_ctrlq_destroy_locks:
  572. mutex_destroy(&cq->sq_lock);
  573. mutex_destroy(&cq->rq_lock);
  574. return ret_code;
  575. }
  576. /**
  577. * ice_init_all_ctrlq - main initialization routine for all control queues
  578. * @hw: pointer to the hardware structure
  579. *
  580. * Prior to calling this function, drivers *MUST* set the following fields
  581. * in the cq->structure for all control queues:
  582. * - cq->num_sq_entries
  583. * - cq->num_rq_entries
  584. * - cq->rq_buf_size
  585. * - cq->sq_buf_size
  586. */
  587. enum ice_status ice_init_all_ctrlq(struct ice_hw *hw)
  588. {
  589. enum ice_status ret_code;
  590. /* Init FW admin queue */
  591. ret_code = ice_init_ctrlq(hw, ICE_CTL_Q_ADMIN);
  592. if (ret_code)
  593. return ret_code;
  594. return ice_init_check_adminq(hw);
  595. }
  596. /**
  597. * ice_shutdown_ctrlq - shutdown routine for any control queue
  598. * @hw: pointer to the hardware structure
  599. * @q_type: specific Control queue type
  600. */
  601. static void ice_shutdown_ctrlq(struct ice_hw *hw, enum ice_ctl_q q_type)
  602. {
  603. struct ice_ctl_q_info *cq;
  604. switch (q_type) {
  605. case ICE_CTL_Q_ADMIN:
  606. cq = &hw->adminq;
  607. if (ice_check_sq_alive(hw, cq))
  608. ice_aq_q_shutdown(hw, true);
  609. break;
  610. default:
  611. return;
  612. }
  613. if (cq->sq.head) {
  614. ice_shutdown_sq(hw, cq);
  615. mutex_destroy(&cq->sq_lock);
  616. }
  617. if (cq->rq.head) {
  618. ice_shutdown_rq(hw, cq);
  619. mutex_destroy(&cq->rq_lock);
  620. }
  621. }
  622. /**
  623. * ice_shutdown_all_ctrlq - shutdown routine for all control queues
  624. * @hw: pointer to the hardware structure
  625. */
  626. void ice_shutdown_all_ctrlq(struct ice_hw *hw)
  627. {
  628. /* Shutdown FW admin queue */
  629. ice_shutdown_ctrlq(hw, ICE_CTL_Q_ADMIN);
  630. }
  631. /**
  632. * ice_clean_sq - cleans Admin send queue (ATQ)
  633. * @hw: pointer to the hardware structure
  634. * @cq: pointer to the specific Control queue
  635. *
  636. * returns the number of free desc
  637. */
  638. static u16 ice_clean_sq(struct ice_hw *hw, struct ice_ctl_q_info *cq)
  639. {
  640. struct ice_ctl_q_ring *sq = &cq->sq;
  641. u16 ntc = sq->next_to_clean;
  642. struct ice_sq_cd *details;
  643. struct ice_aq_desc *desc;
  644. desc = ICE_CTL_Q_DESC(*sq, ntc);
  645. details = ICE_CTL_Q_DETAILS(*sq, ntc);
  646. while (rd32(hw, cq->sq.head) != ntc) {
  647. ice_debug(hw, ICE_DBG_AQ_MSG,
  648. "ntc %d head %d.\n", ntc, rd32(hw, cq->sq.head));
  649. memset(desc, 0, sizeof(*desc));
  650. memset(details, 0, sizeof(*details));
  651. ntc++;
  652. if (ntc == sq->count)
  653. ntc = 0;
  654. desc = ICE_CTL_Q_DESC(*sq, ntc);
  655. details = ICE_CTL_Q_DETAILS(*sq, ntc);
  656. }
  657. sq->next_to_clean = ntc;
  658. return ICE_CTL_Q_DESC_UNUSED(sq);
  659. }
  660. /**
  661. * ice_sq_done - check if FW has processed the Admin Send Queue (ATQ)
  662. * @hw: pointer to the hw struct
  663. * @cq: pointer to the specific Control queue
  664. *
  665. * Returns true if the firmware has processed all descriptors on the
  666. * admin send queue. Returns false if there are still requests pending.
  667. */
  668. static bool ice_sq_done(struct ice_hw *hw, struct ice_ctl_q_info *cq)
  669. {
  670. /* AQ designers suggest use of head for better
  671. * timing reliability than DD bit
  672. */
  673. return rd32(hw, cq->sq.head) == cq->sq.next_to_use;
  674. }
  675. /**
  676. * ice_sq_send_cmd - send command to Control Queue (ATQ)
  677. * @hw: pointer to the hw struct
  678. * @cq: pointer to the specific Control queue
  679. * @desc: prefilled descriptor describing the command (non DMA mem)
  680. * @buf: buffer to use for indirect commands (or NULL for direct commands)
  681. * @buf_size: size of buffer for indirect commands (or 0 for direct commands)
  682. * @cd: pointer to command details structure
  683. *
  684. * This is the main send command routine for the ATQ. It runs the q,
  685. * cleans the queue, etc.
  686. */
  687. enum ice_status
  688. ice_sq_send_cmd(struct ice_hw *hw, struct ice_ctl_q_info *cq,
  689. struct ice_aq_desc *desc, void *buf, u16 buf_size,
  690. struct ice_sq_cd *cd)
  691. {
  692. struct ice_dma_mem *dma_buf = NULL;
  693. struct ice_aq_desc *desc_on_ring;
  694. bool cmd_completed = false;
  695. enum ice_status status = 0;
  696. struct ice_sq_cd *details;
  697. u32 total_delay = 0;
  698. u16 retval = 0;
  699. u32 val = 0;
  700. mutex_lock(&cq->sq_lock);
  701. cq->sq_last_status = ICE_AQ_RC_OK;
  702. if (!cq->sq.count) {
  703. ice_debug(hw, ICE_DBG_AQ_MSG,
  704. "Control Send queue not initialized.\n");
  705. status = ICE_ERR_AQ_EMPTY;
  706. goto sq_send_command_error;
  707. }
  708. if ((buf && !buf_size) || (!buf && buf_size)) {
  709. status = ICE_ERR_PARAM;
  710. goto sq_send_command_error;
  711. }
  712. if (buf) {
  713. if (buf_size > cq->sq_buf_size) {
  714. ice_debug(hw, ICE_DBG_AQ_MSG,
  715. "Invalid buffer size for Control Send queue: %d.\n",
  716. buf_size);
  717. status = ICE_ERR_INVAL_SIZE;
  718. goto sq_send_command_error;
  719. }
  720. desc->flags |= cpu_to_le16(ICE_AQ_FLAG_BUF);
  721. if (buf_size > ICE_AQ_LG_BUF)
  722. desc->flags |= cpu_to_le16(ICE_AQ_FLAG_LB);
  723. }
  724. val = rd32(hw, cq->sq.head);
  725. if (val >= cq->num_sq_entries) {
  726. ice_debug(hw, ICE_DBG_AQ_MSG,
  727. "head overrun at %d in the Control Send Queue ring\n",
  728. val);
  729. status = ICE_ERR_AQ_EMPTY;
  730. goto sq_send_command_error;
  731. }
  732. details = ICE_CTL_Q_DETAILS(cq->sq, cq->sq.next_to_use);
  733. if (cd)
  734. memcpy(details, cd, sizeof(*details));
  735. else
  736. memset(details, 0, sizeof(*details));
  737. /* Call clean and check queue available function to reclaim the
  738. * descriptors that were processed by FW/MBX; the function returns the
  739. * number of desc available. The clean function called here could be
  740. * called in a separate thread in case of asynchronous completions.
  741. */
  742. if (ice_clean_sq(hw, cq) == 0) {
  743. ice_debug(hw, ICE_DBG_AQ_MSG,
  744. "Error: Control Send Queue is full.\n");
  745. status = ICE_ERR_AQ_FULL;
  746. goto sq_send_command_error;
  747. }
  748. /* initialize the temp desc pointer with the right desc */
  749. desc_on_ring = ICE_CTL_Q_DESC(cq->sq, cq->sq.next_to_use);
  750. /* if the desc is available copy the temp desc to the right place */
  751. memcpy(desc_on_ring, desc, sizeof(*desc_on_ring));
  752. /* if buf is not NULL assume indirect command */
  753. if (buf) {
  754. dma_buf = &cq->sq.r.sq_bi[cq->sq.next_to_use];
  755. /* copy the user buf into the respective DMA buf */
  756. memcpy(dma_buf->va, buf, buf_size);
  757. desc_on_ring->datalen = cpu_to_le16(buf_size);
  758. /* Update the address values in the desc with the pa value
  759. * for respective buffer
  760. */
  761. desc_on_ring->params.generic.addr_high =
  762. cpu_to_le32(upper_32_bits(dma_buf->pa));
  763. desc_on_ring->params.generic.addr_low =
  764. cpu_to_le32(lower_32_bits(dma_buf->pa));
  765. }
  766. /* Debug desc and buffer */
  767. ice_debug(hw, ICE_DBG_AQ_MSG,
  768. "ATQ: Control Send queue desc and buffer:\n");
  769. ice_debug_cq(hw, ICE_DBG_AQ_CMD, (void *)desc_on_ring, buf, buf_size);
  770. (cq->sq.next_to_use)++;
  771. if (cq->sq.next_to_use == cq->sq.count)
  772. cq->sq.next_to_use = 0;
  773. wr32(hw, cq->sq.tail, cq->sq.next_to_use);
  774. do {
  775. if (ice_sq_done(hw, cq))
  776. break;
  777. mdelay(1);
  778. total_delay++;
  779. } while (total_delay < cq->sq_cmd_timeout);
  780. /* if ready, copy the desc back to temp */
  781. if (ice_sq_done(hw, cq)) {
  782. memcpy(desc, desc_on_ring, sizeof(*desc));
  783. if (buf) {
  784. /* get returned length to copy */
  785. u16 copy_size = le16_to_cpu(desc->datalen);
  786. if (copy_size > buf_size) {
  787. ice_debug(hw, ICE_DBG_AQ_MSG,
  788. "Return len %d > than buf len %d\n",
  789. copy_size, buf_size);
  790. status = ICE_ERR_AQ_ERROR;
  791. } else {
  792. memcpy(buf, dma_buf->va, copy_size);
  793. }
  794. }
  795. retval = le16_to_cpu(desc->retval);
  796. if (retval) {
  797. ice_debug(hw, ICE_DBG_AQ_MSG,
  798. "Control Send Queue command completed with error 0x%x\n",
  799. retval);
  800. /* strip off FW internal code */
  801. retval &= 0xff;
  802. }
  803. cmd_completed = true;
  804. if (!status && retval != ICE_AQ_RC_OK)
  805. status = ICE_ERR_AQ_ERROR;
  806. cq->sq_last_status = (enum ice_aq_err)retval;
  807. }
  808. ice_debug(hw, ICE_DBG_AQ_MSG,
  809. "ATQ: desc and buffer writeback:\n");
  810. ice_debug_cq(hw, ICE_DBG_AQ_CMD, (void *)desc, buf, buf_size);
  811. /* save writeback AQ if requested */
  812. if (details->wb_desc)
  813. memcpy(details->wb_desc, desc_on_ring,
  814. sizeof(*details->wb_desc));
  815. /* update the error if time out occurred */
  816. if (!cmd_completed) {
  817. ice_debug(hw, ICE_DBG_AQ_MSG,
  818. "Control Send Queue Writeback timeout.\n");
  819. status = ICE_ERR_AQ_TIMEOUT;
  820. }
  821. sq_send_command_error:
  822. mutex_unlock(&cq->sq_lock);
  823. return status;
  824. }
  825. /**
  826. * ice_fill_dflt_direct_cmd_desc - AQ descriptor helper function
  827. * @desc: pointer to the temp descriptor (non DMA mem)
  828. * @opcode: the opcode can be used to decide which flags to turn off or on
  829. *
  830. * Fill the desc with default values
  831. */
  832. void ice_fill_dflt_direct_cmd_desc(struct ice_aq_desc *desc, u16 opcode)
  833. {
  834. /* zero out the desc */
  835. memset(desc, 0, sizeof(*desc));
  836. desc->opcode = cpu_to_le16(opcode);
  837. desc->flags = cpu_to_le16(ICE_AQ_FLAG_SI);
  838. }
  839. /**
  840. * ice_clean_rq_elem
  841. * @hw: pointer to the hw struct
  842. * @cq: pointer to the specific Control queue
  843. * @e: event info from the receive descriptor, includes any buffers
  844. * @pending: number of events that could be left to process
  845. *
  846. * This function cleans one Admin Receive Queue element and returns
  847. * the contents through e. It can also return how many events are
  848. * left to process through 'pending'.
  849. */
  850. enum ice_status
  851. ice_clean_rq_elem(struct ice_hw *hw, struct ice_ctl_q_info *cq,
  852. struct ice_rq_event_info *e, u16 *pending)
  853. {
  854. u16 ntc = cq->rq.next_to_clean;
  855. enum ice_status ret_code = 0;
  856. struct ice_aq_desc *desc;
  857. struct ice_dma_mem *bi;
  858. u16 desc_idx;
  859. u16 datalen;
  860. u16 flags;
  861. u16 ntu;
  862. /* pre-clean the event info */
  863. memset(&e->desc, 0, sizeof(e->desc));
  864. /* take the lock before we start messing with the ring */
  865. mutex_lock(&cq->rq_lock);
  866. if (!cq->rq.count) {
  867. ice_debug(hw, ICE_DBG_AQ_MSG,
  868. "Control Receive queue not initialized.\n");
  869. ret_code = ICE_ERR_AQ_EMPTY;
  870. goto clean_rq_elem_err;
  871. }
  872. /* set next_to_use to head */
  873. ntu = (u16)(rd32(hw, cq->rq.head) & cq->rq.head_mask);
  874. if (ntu == ntc) {
  875. /* nothing to do - shouldn't need to update ring's values */
  876. ret_code = ICE_ERR_AQ_NO_WORK;
  877. goto clean_rq_elem_out;
  878. }
  879. /* now clean the next descriptor */
  880. desc = ICE_CTL_Q_DESC(cq->rq, ntc);
  881. desc_idx = ntc;
  882. cq->rq_last_status = (enum ice_aq_err)le16_to_cpu(desc->retval);
  883. flags = le16_to_cpu(desc->flags);
  884. if (flags & ICE_AQ_FLAG_ERR) {
  885. ret_code = ICE_ERR_AQ_ERROR;
  886. ice_debug(hw, ICE_DBG_AQ_MSG,
  887. "Control Receive Queue Event received with error 0x%x\n",
  888. cq->rq_last_status);
  889. }
  890. memcpy(&e->desc, desc, sizeof(e->desc));
  891. datalen = le16_to_cpu(desc->datalen);
  892. e->msg_len = min(datalen, e->buf_len);
  893. if (e->msg_buf && e->msg_len)
  894. memcpy(e->msg_buf, cq->rq.r.rq_bi[desc_idx].va, e->msg_len);
  895. ice_debug(hw, ICE_DBG_AQ_MSG, "ARQ: desc and buffer:\n");
  896. ice_debug_cq(hw, ICE_DBG_AQ_CMD, (void *)desc, e->msg_buf,
  897. cq->rq_buf_size);
  898. /* Restore the original datalen and buffer address in the desc,
  899. * FW updates datalen to indicate the event message size
  900. */
  901. bi = &cq->rq.r.rq_bi[ntc];
  902. memset(desc, 0, sizeof(*desc));
  903. desc->flags = cpu_to_le16(ICE_AQ_FLAG_BUF);
  904. if (cq->rq_buf_size > ICE_AQ_LG_BUF)
  905. desc->flags |= cpu_to_le16(ICE_AQ_FLAG_LB);
  906. desc->datalen = cpu_to_le16(bi->size);
  907. desc->params.generic.addr_high = cpu_to_le32(upper_32_bits(bi->pa));
  908. desc->params.generic.addr_low = cpu_to_le32(lower_32_bits(bi->pa));
  909. /* set tail = the last cleaned desc index. */
  910. wr32(hw, cq->rq.tail, ntc);
  911. /* ntc is updated to tail + 1 */
  912. ntc++;
  913. if (ntc == cq->num_rq_entries)
  914. ntc = 0;
  915. cq->rq.next_to_clean = ntc;
  916. cq->rq.next_to_use = ntu;
  917. clean_rq_elem_out:
  918. /* Set pending if needed, unlock and return */
  919. if (pending) {
  920. /* re-read HW head to calculate actual pending messages */
  921. ntu = (u16)(rd32(hw, cq->rq.head) & cq->rq.head_mask);
  922. *pending = (u16)((ntc > ntu ? cq->rq.count : 0) + (ntu - ntc));
  923. }
  924. clean_rq_elem_err:
  925. mutex_unlock(&cq->rq_lock);
  926. return ret_code;
  927. }