ice_controlq.c 29 KB

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