efa_com.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251
  1. // SPDX-License-Identifier: GPL-2.0 OR BSD-2-Clause
  2. /*
  3. * Copyright 2018-2024 Amazon.com, Inc. or its affiliates. All rights reserved.
  4. */
  5. #include "efa_com.h"
  6. #include "efa_regs_defs.h"
  7. #define ADMIN_CMD_TIMEOUT_US 30000000 /* usecs */
  8. #define EFA_REG_READ_TIMEOUT_US 50000 /* usecs */
  9. #define EFA_MMIO_READ_INVALID 0xffffffff
  10. #define EFA_POLL_INTERVAL_MS 100 /* msecs */
  11. #define EFA_ASYNC_QUEUE_DEPTH 16
  12. #define EFA_ADMIN_QUEUE_DEPTH 32
  13. #define EFA_CTRL_MAJOR 0
  14. #define EFA_CTRL_MINOR 0
  15. #define EFA_CTRL_SUB_MINOR 1
  16. enum efa_cmd_status {
  17. EFA_CMD_SUBMITTED,
  18. EFA_CMD_COMPLETED,
  19. };
  20. struct efa_comp_ctx {
  21. struct completion wait_event;
  22. struct efa_admin_acq_entry *user_cqe;
  23. u32 comp_size;
  24. enum efa_cmd_status status;
  25. u8 cmd_opcode;
  26. u8 occupied;
  27. };
  28. static const char *efa_com_cmd_str(u8 cmd)
  29. {
  30. #define EFA_CMD_STR_CASE(_cmd) case EFA_ADMIN_##_cmd: return #_cmd
  31. switch (cmd) {
  32. EFA_CMD_STR_CASE(CREATE_QP);
  33. EFA_CMD_STR_CASE(MODIFY_QP);
  34. EFA_CMD_STR_CASE(QUERY_QP);
  35. EFA_CMD_STR_CASE(DESTROY_QP);
  36. EFA_CMD_STR_CASE(CREATE_AH);
  37. EFA_CMD_STR_CASE(DESTROY_AH);
  38. EFA_CMD_STR_CASE(REG_MR);
  39. EFA_CMD_STR_CASE(DEREG_MR);
  40. EFA_CMD_STR_CASE(CREATE_CQ);
  41. EFA_CMD_STR_CASE(DESTROY_CQ);
  42. EFA_CMD_STR_CASE(GET_FEATURE);
  43. EFA_CMD_STR_CASE(SET_FEATURE);
  44. EFA_CMD_STR_CASE(GET_STATS);
  45. EFA_CMD_STR_CASE(ALLOC_PD);
  46. EFA_CMD_STR_CASE(DEALLOC_PD);
  47. EFA_CMD_STR_CASE(ALLOC_UAR);
  48. EFA_CMD_STR_CASE(DEALLOC_UAR);
  49. EFA_CMD_STR_CASE(CREATE_EQ);
  50. EFA_CMD_STR_CASE(DESTROY_EQ);
  51. default: return "unknown command opcode";
  52. }
  53. #undef EFA_CMD_STR_CASE
  54. }
  55. void efa_com_set_dma_addr(dma_addr_t addr, u32 *addr_high, u32 *addr_low)
  56. {
  57. *addr_low = lower_32_bits(addr);
  58. *addr_high = upper_32_bits(addr);
  59. }
  60. static u32 efa_com_reg_read32(struct efa_com_dev *edev, u16 offset)
  61. {
  62. struct efa_com_mmio_read *mmio_read = &edev->mmio_read;
  63. struct efa_admin_mmio_req_read_less_resp *read_resp;
  64. unsigned long exp_time;
  65. u32 mmio_read_reg = 0;
  66. u32 err;
  67. read_resp = mmio_read->read_resp;
  68. spin_lock(&mmio_read->lock);
  69. mmio_read->seq_num++;
  70. /* trash DMA req_id to identify when hardware is done */
  71. read_resp->req_id = mmio_read->seq_num + 0x9aL;
  72. EFA_SET(&mmio_read_reg, EFA_REGS_MMIO_REG_READ_REG_OFF, offset);
  73. EFA_SET(&mmio_read_reg, EFA_REGS_MMIO_REG_READ_REQ_ID,
  74. mmio_read->seq_num);
  75. writel(mmio_read_reg, edev->reg_bar + EFA_REGS_MMIO_REG_READ_OFF);
  76. exp_time = jiffies + usecs_to_jiffies(mmio_read->mmio_read_timeout);
  77. do {
  78. if (READ_ONCE(read_resp->req_id) == mmio_read->seq_num)
  79. break;
  80. udelay(1);
  81. } while (time_is_after_jiffies(exp_time));
  82. if (read_resp->req_id != mmio_read->seq_num) {
  83. ibdev_err_ratelimited(
  84. edev->efa_dev,
  85. "Reading register timed out. expected: req id[%u] offset[%#x] actual: req id[%u] offset[%#x]\n",
  86. mmio_read->seq_num, offset, read_resp->req_id,
  87. read_resp->reg_off);
  88. err = EFA_MMIO_READ_INVALID;
  89. goto out;
  90. }
  91. if (read_resp->reg_off != offset) {
  92. ibdev_err_ratelimited(
  93. edev->efa_dev,
  94. "Reading register failed: wrong offset provided\n");
  95. err = EFA_MMIO_READ_INVALID;
  96. goto out;
  97. }
  98. err = read_resp->reg_val;
  99. out:
  100. spin_unlock(&mmio_read->lock);
  101. return err;
  102. }
  103. static int efa_com_admin_init_sq(struct efa_com_dev *edev)
  104. {
  105. struct efa_com_admin_queue *aq = &edev->aq;
  106. struct efa_com_admin_sq *sq = &aq->sq;
  107. u16 size = aq->depth * sizeof(*sq->entries);
  108. u32 aq_caps = 0;
  109. u32 addr_high;
  110. u32 addr_low;
  111. sq->entries =
  112. dma_alloc_coherent(aq->dmadev, size, &sq->dma_addr, GFP_KERNEL);
  113. if (!sq->entries)
  114. return -ENOMEM;
  115. spin_lock_init(&sq->lock);
  116. sq->cc = 0;
  117. sq->pc = 0;
  118. sq->phase = 1;
  119. sq->db_addr = (u32 __iomem *)(edev->reg_bar + EFA_REGS_AQ_PROD_DB_OFF);
  120. addr_high = upper_32_bits(sq->dma_addr);
  121. addr_low = lower_32_bits(sq->dma_addr);
  122. writel(addr_low, edev->reg_bar + EFA_REGS_AQ_BASE_LO_OFF);
  123. writel(addr_high, edev->reg_bar + EFA_REGS_AQ_BASE_HI_OFF);
  124. EFA_SET(&aq_caps, EFA_REGS_AQ_CAPS_AQ_DEPTH, aq->depth);
  125. EFA_SET(&aq_caps, EFA_REGS_AQ_CAPS_AQ_ENTRY_SIZE,
  126. sizeof(struct efa_admin_aq_entry));
  127. writel(aq_caps, edev->reg_bar + EFA_REGS_AQ_CAPS_OFF);
  128. return 0;
  129. }
  130. static int efa_com_admin_init_cq(struct efa_com_dev *edev)
  131. {
  132. struct efa_com_admin_queue *aq = &edev->aq;
  133. struct efa_com_admin_cq *cq = &aq->cq;
  134. u16 size = aq->depth * sizeof(*cq->entries);
  135. u32 acq_caps = 0;
  136. u32 addr_high;
  137. u32 addr_low;
  138. cq->entries =
  139. dma_alloc_coherent(aq->dmadev, size, &cq->dma_addr, GFP_KERNEL);
  140. if (!cq->entries)
  141. return -ENOMEM;
  142. spin_lock_init(&cq->lock);
  143. cq->cc = 0;
  144. cq->phase = 1;
  145. addr_high = upper_32_bits(cq->dma_addr);
  146. addr_low = lower_32_bits(cq->dma_addr);
  147. writel(addr_low, edev->reg_bar + EFA_REGS_ACQ_BASE_LO_OFF);
  148. writel(addr_high, edev->reg_bar + EFA_REGS_ACQ_BASE_HI_OFF);
  149. EFA_SET(&acq_caps, EFA_REGS_ACQ_CAPS_ACQ_DEPTH, aq->depth);
  150. EFA_SET(&acq_caps, EFA_REGS_ACQ_CAPS_ACQ_ENTRY_SIZE,
  151. sizeof(struct efa_admin_acq_entry));
  152. EFA_SET(&acq_caps, EFA_REGS_ACQ_CAPS_ACQ_MSIX_VECTOR,
  153. aq->msix_vector_idx);
  154. writel(acq_caps, edev->reg_bar + EFA_REGS_ACQ_CAPS_OFF);
  155. return 0;
  156. }
  157. static int efa_com_admin_init_aenq(struct efa_com_dev *edev,
  158. struct efa_aenq_handlers *aenq_handlers)
  159. {
  160. struct efa_com_aenq *aenq = &edev->aenq;
  161. u32 addr_low, addr_high;
  162. u32 aenq_caps = 0;
  163. u16 size;
  164. if (!aenq_handlers) {
  165. ibdev_err(edev->efa_dev, "aenq handlers pointer is NULL\n");
  166. return -EINVAL;
  167. }
  168. size = EFA_ASYNC_QUEUE_DEPTH * sizeof(*aenq->entries);
  169. aenq->entries = dma_alloc_coherent(edev->dmadev, size, &aenq->dma_addr,
  170. GFP_KERNEL);
  171. if (!aenq->entries)
  172. return -ENOMEM;
  173. aenq->aenq_handlers = aenq_handlers;
  174. aenq->depth = EFA_ASYNC_QUEUE_DEPTH;
  175. aenq->cc = 0;
  176. aenq->phase = 1;
  177. addr_low = lower_32_bits(aenq->dma_addr);
  178. addr_high = upper_32_bits(aenq->dma_addr);
  179. writel(addr_low, edev->reg_bar + EFA_REGS_AENQ_BASE_LO_OFF);
  180. writel(addr_high, edev->reg_bar + EFA_REGS_AENQ_BASE_HI_OFF);
  181. EFA_SET(&aenq_caps, EFA_REGS_AENQ_CAPS_AENQ_DEPTH, aenq->depth);
  182. EFA_SET(&aenq_caps, EFA_REGS_AENQ_CAPS_AENQ_ENTRY_SIZE,
  183. sizeof(struct efa_admin_aenq_entry));
  184. EFA_SET(&aenq_caps, EFA_REGS_AENQ_CAPS_AENQ_MSIX_VECTOR,
  185. aenq->msix_vector_idx);
  186. writel(aenq_caps, edev->reg_bar + EFA_REGS_AENQ_CAPS_OFF);
  187. /*
  188. * Init cons_db to mark that all entries in the queue
  189. * are initially available
  190. */
  191. writel(edev->aenq.cc, edev->reg_bar + EFA_REGS_AENQ_CONS_DB_OFF);
  192. return 0;
  193. }
  194. /* ID to be used with efa_com_get_comp_ctx */
  195. static u16 efa_com_alloc_ctx_id(struct efa_com_admin_queue *aq)
  196. {
  197. u16 ctx_id;
  198. spin_lock(&aq->comp_ctx_lock);
  199. ctx_id = aq->comp_ctx_pool[aq->comp_ctx_pool_next];
  200. aq->comp_ctx_pool_next++;
  201. spin_unlock(&aq->comp_ctx_lock);
  202. return ctx_id;
  203. }
  204. static void efa_com_dealloc_ctx_id(struct efa_com_admin_queue *aq,
  205. u16 ctx_id)
  206. {
  207. spin_lock(&aq->comp_ctx_lock);
  208. aq->comp_ctx_pool_next--;
  209. aq->comp_ctx_pool[aq->comp_ctx_pool_next] = ctx_id;
  210. spin_unlock(&aq->comp_ctx_lock);
  211. }
  212. static inline void efa_com_put_comp_ctx(struct efa_com_admin_queue *aq,
  213. struct efa_comp_ctx *comp_ctx)
  214. {
  215. u16 cmd_id = EFA_GET(&comp_ctx->user_cqe->acq_common_descriptor.command,
  216. EFA_ADMIN_ACQ_COMMON_DESC_COMMAND_ID);
  217. u16 ctx_id = cmd_id & (aq->depth - 1);
  218. ibdev_dbg(aq->efa_dev, "Put completion command_id %#x\n", cmd_id);
  219. comp_ctx->occupied = 0;
  220. efa_com_dealloc_ctx_id(aq, ctx_id);
  221. }
  222. static struct efa_comp_ctx *efa_com_get_comp_ctx(struct efa_com_admin_queue *aq,
  223. u16 cmd_id, bool capture)
  224. {
  225. u16 ctx_id = cmd_id & (aq->depth - 1);
  226. if (aq->comp_ctx[ctx_id].occupied && capture) {
  227. ibdev_err_ratelimited(
  228. aq->efa_dev,
  229. "Completion context for command_id %#x is occupied\n",
  230. cmd_id);
  231. return NULL;
  232. }
  233. if (capture) {
  234. aq->comp_ctx[ctx_id].occupied = 1;
  235. ibdev_dbg(aq->efa_dev,
  236. "Take completion ctxt for command_id %#x\n", cmd_id);
  237. }
  238. return &aq->comp_ctx[ctx_id];
  239. }
  240. static struct efa_comp_ctx *__efa_com_submit_admin_cmd(struct efa_com_admin_queue *aq,
  241. struct efa_admin_aq_entry *cmd,
  242. size_t cmd_size_in_bytes,
  243. struct efa_admin_acq_entry *comp,
  244. size_t comp_size_in_bytes)
  245. {
  246. struct efa_admin_aq_entry *aqe;
  247. struct efa_comp_ctx *comp_ctx;
  248. u16 queue_size_mask;
  249. u16 cmd_id;
  250. u16 ctx_id;
  251. u16 pi;
  252. queue_size_mask = aq->depth - 1;
  253. pi = aq->sq.pc & queue_size_mask;
  254. ctx_id = efa_com_alloc_ctx_id(aq);
  255. /* cmd_id LSBs are the ctx_id and MSBs are entropy bits from pc */
  256. cmd_id = ctx_id & queue_size_mask;
  257. cmd_id |= aq->sq.pc & ~queue_size_mask;
  258. cmd_id &= EFA_ADMIN_AQ_COMMON_DESC_COMMAND_ID_MASK;
  259. cmd->aq_common_descriptor.command_id = cmd_id;
  260. EFA_SET(&cmd->aq_common_descriptor.flags,
  261. EFA_ADMIN_AQ_COMMON_DESC_PHASE, aq->sq.phase);
  262. comp_ctx = efa_com_get_comp_ctx(aq, cmd_id, true);
  263. if (!comp_ctx) {
  264. efa_com_dealloc_ctx_id(aq, ctx_id);
  265. return ERR_PTR(-EINVAL);
  266. }
  267. comp_ctx->status = EFA_CMD_SUBMITTED;
  268. comp_ctx->comp_size = comp_size_in_bytes;
  269. comp_ctx->user_cqe = comp;
  270. comp_ctx->cmd_opcode = cmd->aq_common_descriptor.opcode;
  271. reinit_completion(&comp_ctx->wait_event);
  272. aqe = &aq->sq.entries[pi];
  273. memset(aqe, 0, sizeof(*aqe));
  274. memcpy(aqe, cmd, cmd_size_in_bytes);
  275. aq->sq.pc++;
  276. atomic64_inc(&aq->stats.submitted_cmd);
  277. if ((aq->sq.pc & queue_size_mask) == 0)
  278. aq->sq.phase = !aq->sq.phase;
  279. /* barrier not needed in case of writel */
  280. writel(aq->sq.pc, aq->sq.db_addr);
  281. return comp_ctx;
  282. }
  283. static inline int efa_com_init_comp_ctxt(struct efa_com_admin_queue *aq)
  284. {
  285. size_t pool_size = aq->depth * sizeof(*aq->comp_ctx_pool);
  286. size_t size = aq->depth * sizeof(struct efa_comp_ctx);
  287. struct efa_comp_ctx *comp_ctx;
  288. u16 i;
  289. aq->comp_ctx = devm_kzalloc(aq->dmadev, size, GFP_KERNEL);
  290. aq->comp_ctx_pool = devm_kzalloc(aq->dmadev, pool_size, GFP_KERNEL);
  291. if (!aq->comp_ctx || !aq->comp_ctx_pool) {
  292. devm_kfree(aq->dmadev, aq->comp_ctx_pool);
  293. devm_kfree(aq->dmadev, aq->comp_ctx);
  294. return -ENOMEM;
  295. }
  296. for (i = 0; i < aq->depth; i++) {
  297. comp_ctx = efa_com_get_comp_ctx(aq, i, false);
  298. if (comp_ctx)
  299. init_completion(&comp_ctx->wait_event);
  300. aq->comp_ctx_pool[i] = i;
  301. }
  302. spin_lock_init(&aq->comp_ctx_lock);
  303. aq->comp_ctx_pool_next = 0;
  304. return 0;
  305. }
  306. static struct efa_comp_ctx *efa_com_submit_admin_cmd(struct efa_com_admin_queue *aq,
  307. struct efa_admin_aq_entry *cmd,
  308. size_t cmd_size_in_bytes,
  309. struct efa_admin_acq_entry *comp,
  310. size_t comp_size_in_bytes)
  311. {
  312. struct efa_comp_ctx *comp_ctx;
  313. spin_lock(&aq->sq.lock);
  314. if (!test_bit(EFA_AQ_STATE_RUNNING_BIT, &aq->state)) {
  315. ibdev_err_ratelimited(aq->efa_dev, "Admin queue is closed\n");
  316. spin_unlock(&aq->sq.lock);
  317. return ERR_PTR(-ENODEV);
  318. }
  319. comp_ctx = __efa_com_submit_admin_cmd(aq, cmd, cmd_size_in_bytes, comp,
  320. comp_size_in_bytes);
  321. spin_unlock(&aq->sq.lock);
  322. if (IS_ERR(comp_ctx))
  323. clear_bit(EFA_AQ_STATE_RUNNING_BIT, &aq->state);
  324. return comp_ctx;
  325. }
  326. static int efa_com_handle_single_admin_completion(struct efa_com_admin_queue *aq,
  327. struct efa_admin_acq_entry *cqe)
  328. {
  329. struct efa_comp_ctx *comp_ctx;
  330. u16 cmd_id;
  331. cmd_id = EFA_GET(&cqe->acq_common_descriptor.command,
  332. EFA_ADMIN_ACQ_COMMON_DESC_COMMAND_ID);
  333. comp_ctx = efa_com_get_comp_ctx(aq, cmd_id, false);
  334. if (comp_ctx->status != EFA_CMD_SUBMITTED) {
  335. ibdev_err(aq->efa_dev,
  336. "Received completion with unexpected command id[%d], sq producer: %d, sq consumer: %d, cq consumer: %d\n",
  337. cmd_id, aq->sq.pc, aq->sq.cc, aq->cq.cc);
  338. return -EINVAL;
  339. }
  340. comp_ctx->status = EFA_CMD_COMPLETED;
  341. memcpy(comp_ctx->user_cqe, cqe, comp_ctx->comp_size);
  342. if (!test_bit(EFA_AQ_STATE_POLLING_BIT, &aq->state))
  343. complete(&comp_ctx->wait_event);
  344. return 0;
  345. }
  346. static void efa_com_handle_admin_completion(struct efa_com_admin_queue *aq)
  347. {
  348. struct efa_admin_acq_entry *cqe;
  349. u16 queue_size_mask;
  350. u16 comp_cmds = 0;
  351. u8 phase;
  352. int err;
  353. u16 ci;
  354. queue_size_mask = aq->depth - 1;
  355. ci = aq->cq.cc & queue_size_mask;
  356. phase = aq->cq.phase;
  357. cqe = &aq->cq.entries[ci];
  358. /* Go over all the completions */
  359. while ((READ_ONCE(cqe->acq_common_descriptor.flags) &
  360. EFA_ADMIN_ACQ_COMMON_DESC_PHASE_MASK) == phase) {
  361. /*
  362. * Do not read the rest of the completion entry before the
  363. * phase bit was validated
  364. */
  365. dma_rmb();
  366. err = efa_com_handle_single_admin_completion(aq, cqe);
  367. if (!err)
  368. comp_cmds++;
  369. aq->cq.cc++;
  370. ci++;
  371. if (ci == aq->depth) {
  372. ci = 0;
  373. phase = !phase;
  374. }
  375. cqe = &aq->cq.entries[ci];
  376. }
  377. aq->cq.phase = phase;
  378. aq->sq.cc += comp_cmds;
  379. atomic64_add(comp_cmds, &aq->stats.completed_cmd);
  380. }
  381. static int efa_com_comp_status_to_errno(u8 comp_status)
  382. {
  383. switch (comp_status) {
  384. case EFA_ADMIN_SUCCESS:
  385. return 0;
  386. case EFA_ADMIN_RESOURCE_ALLOCATION_FAILURE:
  387. return -ENOMEM;
  388. case EFA_ADMIN_UNSUPPORTED_OPCODE:
  389. return -EOPNOTSUPP;
  390. case EFA_ADMIN_BAD_OPCODE:
  391. case EFA_ADMIN_MALFORMED_REQUEST:
  392. case EFA_ADMIN_ILLEGAL_PARAMETER:
  393. case EFA_ADMIN_UNKNOWN_ERROR:
  394. return -EINVAL;
  395. default:
  396. return -EINVAL;
  397. }
  398. }
  399. static int efa_com_wait_and_process_admin_cq_polling(struct efa_comp_ctx *comp_ctx,
  400. struct efa_com_admin_queue *aq)
  401. {
  402. unsigned long timeout;
  403. unsigned long flags;
  404. int err;
  405. timeout = jiffies + usecs_to_jiffies(aq->completion_timeout);
  406. while (1) {
  407. spin_lock_irqsave(&aq->cq.lock, flags);
  408. efa_com_handle_admin_completion(aq);
  409. spin_unlock_irqrestore(&aq->cq.lock, flags);
  410. if (comp_ctx->status != EFA_CMD_SUBMITTED)
  411. break;
  412. if (time_is_before_jiffies(timeout)) {
  413. ibdev_err_ratelimited(
  414. aq->efa_dev,
  415. "Wait for completion (polling) timeout\n");
  416. /* EFA didn't have any completion */
  417. atomic64_inc(&aq->stats.no_completion);
  418. clear_bit(EFA_AQ_STATE_RUNNING_BIT, &aq->state);
  419. err = -ETIME;
  420. goto out;
  421. }
  422. msleep(aq->poll_interval);
  423. }
  424. err = efa_com_comp_status_to_errno(comp_ctx->user_cqe->acq_common_descriptor.status);
  425. out:
  426. efa_com_put_comp_ctx(aq, comp_ctx);
  427. return err;
  428. }
  429. static int efa_com_wait_and_process_admin_cq_interrupts(struct efa_comp_ctx *comp_ctx,
  430. struct efa_com_admin_queue *aq)
  431. {
  432. unsigned long flags;
  433. int err;
  434. wait_for_completion_timeout(&comp_ctx->wait_event,
  435. usecs_to_jiffies(aq->completion_timeout));
  436. /*
  437. * In case the command wasn't completed find out the root cause.
  438. * There might be 2 kinds of errors
  439. * 1) No completion (timeout reached)
  440. * 2) There is completion but the device didn't get any msi-x interrupt.
  441. */
  442. if (comp_ctx->status == EFA_CMD_SUBMITTED) {
  443. spin_lock_irqsave(&aq->cq.lock, flags);
  444. efa_com_handle_admin_completion(aq);
  445. spin_unlock_irqrestore(&aq->cq.lock, flags);
  446. atomic64_inc(&aq->stats.no_completion);
  447. if (comp_ctx->status == EFA_CMD_COMPLETED)
  448. ibdev_err_ratelimited(
  449. aq->efa_dev,
  450. "The device sent a completion but the driver didn't receive any MSI-X interrupt for admin cmd %s(%d) status %d (ctx: 0x%p, sq producer: %d, sq consumer: %d, cq consumer: %d)\n",
  451. efa_com_cmd_str(comp_ctx->cmd_opcode),
  452. comp_ctx->cmd_opcode, comp_ctx->status,
  453. comp_ctx, aq->sq.pc, aq->sq.cc, aq->cq.cc);
  454. else
  455. ibdev_err_ratelimited(
  456. aq->efa_dev,
  457. "The device didn't send any completion for admin cmd %s(%d) status %d (ctx 0x%p, sq producer: %d, sq consumer: %d, cq consumer: %d)\n",
  458. efa_com_cmd_str(comp_ctx->cmd_opcode),
  459. comp_ctx->cmd_opcode, comp_ctx->status,
  460. comp_ctx, aq->sq.pc, aq->sq.cc, aq->cq.cc);
  461. clear_bit(EFA_AQ_STATE_RUNNING_BIT, &aq->state);
  462. err = -ETIME;
  463. goto out;
  464. }
  465. err = efa_com_comp_status_to_errno(comp_ctx->user_cqe->acq_common_descriptor.status);
  466. out:
  467. efa_com_put_comp_ctx(aq, comp_ctx);
  468. return err;
  469. }
  470. /*
  471. * There are two types to wait for completion.
  472. * Polling mode - wait until the completion is available.
  473. * Async mode - wait on wait queue until the completion is ready
  474. * (or the timeout expired).
  475. * It is expected that the IRQ called efa_com_handle_admin_completion
  476. * to mark the completions.
  477. */
  478. static int efa_com_wait_and_process_admin_cq(struct efa_comp_ctx *comp_ctx,
  479. struct efa_com_admin_queue *aq)
  480. {
  481. if (test_bit(EFA_AQ_STATE_POLLING_BIT, &aq->state))
  482. return efa_com_wait_and_process_admin_cq_polling(comp_ctx, aq);
  483. return efa_com_wait_and_process_admin_cq_interrupts(comp_ctx, aq);
  484. }
  485. /**
  486. * efa_com_cmd_exec - Execute admin command
  487. * @aq: admin queue.
  488. * @cmd: the admin command to execute.
  489. * @cmd_size: the command size.
  490. * @comp: command completion return entry.
  491. * @comp_size: command completion size.
  492. * Submit an admin command and then wait until the device will return a
  493. * completion.
  494. * The completion will be copied into comp.
  495. *
  496. * @return - 0 on success, negative value on failure.
  497. */
  498. int efa_com_cmd_exec(struct efa_com_admin_queue *aq,
  499. struct efa_admin_aq_entry *cmd,
  500. size_t cmd_size,
  501. struct efa_admin_acq_entry *comp,
  502. size_t comp_size)
  503. {
  504. struct efa_comp_ctx *comp_ctx;
  505. int err;
  506. might_sleep();
  507. /* In case of queue FULL */
  508. down(&aq->avail_cmds);
  509. ibdev_dbg(aq->efa_dev, "%s (opcode %d)\n",
  510. efa_com_cmd_str(cmd->aq_common_descriptor.opcode),
  511. cmd->aq_common_descriptor.opcode);
  512. comp_ctx = efa_com_submit_admin_cmd(aq, cmd, cmd_size, comp, comp_size);
  513. if (IS_ERR(comp_ctx)) {
  514. ibdev_err_ratelimited(
  515. aq->efa_dev,
  516. "Failed to submit command %s (opcode %u) err %ld\n",
  517. efa_com_cmd_str(cmd->aq_common_descriptor.opcode),
  518. cmd->aq_common_descriptor.opcode, PTR_ERR(comp_ctx));
  519. up(&aq->avail_cmds);
  520. atomic64_inc(&aq->stats.cmd_err);
  521. return PTR_ERR(comp_ctx);
  522. }
  523. err = efa_com_wait_and_process_admin_cq(comp_ctx, aq);
  524. if (err) {
  525. ibdev_err_ratelimited(
  526. aq->efa_dev,
  527. "Failed to process command %s (opcode %u) comp_status %d err %d\n",
  528. efa_com_cmd_str(cmd->aq_common_descriptor.opcode),
  529. cmd->aq_common_descriptor.opcode,
  530. comp_ctx->user_cqe->acq_common_descriptor.status, err);
  531. atomic64_inc(&aq->stats.cmd_err);
  532. }
  533. up(&aq->avail_cmds);
  534. return err;
  535. }
  536. /**
  537. * efa_com_admin_destroy - Destroy the admin and the async events queues.
  538. * @edev: EFA communication layer struct
  539. */
  540. void efa_com_admin_destroy(struct efa_com_dev *edev)
  541. {
  542. struct efa_com_admin_queue *aq = &edev->aq;
  543. struct efa_com_aenq *aenq = &edev->aenq;
  544. struct efa_com_admin_cq *cq = &aq->cq;
  545. struct efa_com_admin_sq *sq = &aq->sq;
  546. u16 size;
  547. clear_bit(EFA_AQ_STATE_RUNNING_BIT, &aq->state);
  548. devm_kfree(edev->dmadev, aq->comp_ctx_pool);
  549. devm_kfree(edev->dmadev, aq->comp_ctx);
  550. size = aq->depth * sizeof(*sq->entries);
  551. dma_free_coherent(edev->dmadev, size, sq->entries, sq->dma_addr);
  552. size = aq->depth * sizeof(*cq->entries);
  553. dma_free_coherent(edev->dmadev, size, cq->entries, cq->dma_addr);
  554. size = aenq->depth * sizeof(*aenq->entries);
  555. dma_free_coherent(edev->dmadev, size, aenq->entries, aenq->dma_addr);
  556. }
  557. /**
  558. * efa_com_set_admin_polling_mode - Set the admin completion queue polling mode
  559. * @edev: EFA communication layer struct
  560. * @polling: Enable/Disable polling mode
  561. *
  562. * Set the admin completion mode.
  563. */
  564. void efa_com_set_admin_polling_mode(struct efa_com_dev *edev, bool polling)
  565. {
  566. u32 mask_value = 0;
  567. if (polling)
  568. EFA_SET(&mask_value, EFA_REGS_INTR_MASK_EN, 1);
  569. writel(mask_value, edev->reg_bar + EFA_REGS_INTR_MASK_OFF);
  570. if (polling)
  571. set_bit(EFA_AQ_STATE_POLLING_BIT, &edev->aq.state);
  572. else
  573. clear_bit(EFA_AQ_STATE_POLLING_BIT, &edev->aq.state);
  574. }
  575. static void efa_com_stats_init(struct efa_com_dev *edev)
  576. {
  577. atomic64_t *s = (atomic64_t *)&edev->aq.stats;
  578. int i;
  579. for (i = 0; i < sizeof(edev->aq.stats) / sizeof(*s); i++, s++)
  580. atomic64_set(s, 0);
  581. }
  582. /**
  583. * efa_com_admin_init - Init the admin and the async queues
  584. * @edev: EFA communication layer struct
  585. * @aenq_handlers: Those handlers to be called upon event.
  586. *
  587. * Initialize the admin submission and completion queues.
  588. * Initialize the asynchronous events notification queues.
  589. *
  590. * @return - 0 on success, negative value on failure.
  591. */
  592. int efa_com_admin_init(struct efa_com_dev *edev,
  593. struct efa_aenq_handlers *aenq_handlers)
  594. {
  595. struct efa_com_admin_queue *aq = &edev->aq;
  596. u32 timeout;
  597. u32 dev_sts;
  598. u32 cap;
  599. int err;
  600. dev_sts = efa_com_reg_read32(edev, EFA_REGS_DEV_STS_OFF);
  601. if (!EFA_GET(&dev_sts, EFA_REGS_DEV_STS_READY)) {
  602. ibdev_err(edev->efa_dev,
  603. "Device isn't ready, abort com init %#x\n", dev_sts);
  604. return -ENODEV;
  605. }
  606. aq->depth = EFA_ADMIN_QUEUE_DEPTH;
  607. aq->dmadev = edev->dmadev;
  608. aq->efa_dev = edev->efa_dev;
  609. set_bit(EFA_AQ_STATE_POLLING_BIT, &aq->state);
  610. sema_init(&aq->avail_cmds, aq->depth);
  611. efa_com_stats_init(edev);
  612. err = efa_com_init_comp_ctxt(aq);
  613. if (err)
  614. return err;
  615. err = efa_com_admin_init_sq(edev);
  616. if (err)
  617. goto err_destroy_comp_ctxt;
  618. err = efa_com_admin_init_cq(edev);
  619. if (err)
  620. goto err_destroy_sq;
  621. efa_com_set_admin_polling_mode(edev, false);
  622. err = efa_com_admin_init_aenq(edev, aenq_handlers);
  623. if (err)
  624. goto err_destroy_cq;
  625. cap = efa_com_reg_read32(edev, EFA_REGS_CAPS_OFF);
  626. timeout = EFA_GET(&cap, EFA_REGS_CAPS_ADMIN_CMD_TO);
  627. if (timeout)
  628. /* the resolution of timeout reg is 100ms */
  629. aq->completion_timeout = timeout * 100000;
  630. else
  631. aq->completion_timeout = ADMIN_CMD_TIMEOUT_US;
  632. aq->poll_interval = EFA_POLL_INTERVAL_MS;
  633. set_bit(EFA_AQ_STATE_RUNNING_BIT, &aq->state);
  634. return 0;
  635. err_destroy_cq:
  636. dma_free_coherent(edev->dmadev, aq->depth * sizeof(*aq->cq.entries),
  637. aq->cq.entries, aq->cq.dma_addr);
  638. err_destroy_sq:
  639. dma_free_coherent(edev->dmadev, aq->depth * sizeof(*aq->sq.entries),
  640. aq->sq.entries, aq->sq.dma_addr);
  641. err_destroy_comp_ctxt:
  642. devm_kfree(edev->dmadev, aq->comp_ctx);
  643. return err;
  644. }
  645. /**
  646. * efa_com_admin_q_comp_intr_handler - admin queue interrupt handler
  647. * @edev: EFA communication layer struct
  648. *
  649. * This method goes over the admin completion queue and wakes up
  650. * all the pending threads that wait on the commands wait event.
  651. *
  652. * Note: Should be called after MSI-X interrupt.
  653. */
  654. void efa_com_admin_q_comp_intr_handler(struct efa_com_dev *edev)
  655. {
  656. unsigned long flags;
  657. spin_lock_irqsave(&edev->aq.cq.lock, flags);
  658. efa_com_handle_admin_completion(&edev->aq);
  659. spin_unlock_irqrestore(&edev->aq.cq.lock, flags);
  660. }
  661. /*
  662. * efa_handle_specific_aenq_event:
  663. * return the handler that is relevant to the specific event group
  664. */
  665. static efa_aenq_handler efa_com_get_specific_aenq_cb(struct efa_com_dev *edev,
  666. u16 group)
  667. {
  668. struct efa_aenq_handlers *aenq_handlers = edev->aenq.aenq_handlers;
  669. if (group < EFA_MAX_HANDLERS && aenq_handlers->handlers[group])
  670. return aenq_handlers->handlers[group];
  671. return aenq_handlers->unimplemented_handler;
  672. }
  673. /**
  674. * efa_com_aenq_intr_handler - AENQ interrupt handler
  675. * @edev: EFA communication layer struct
  676. * @data: Data of interrupt handler.
  677. *
  678. * Go over the async event notification queue and call the proper aenq handler.
  679. */
  680. void efa_com_aenq_intr_handler(struct efa_com_dev *edev, void *data)
  681. {
  682. struct efa_admin_aenq_common_desc *aenq_common;
  683. struct efa_com_aenq *aenq = &edev->aenq;
  684. struct efa_admin_aenq_entry *aenq_e;
  685. efa_aenq_handler handler_cb;
  686. u32 processed = 0;
  687. u8 phase;
  688. u32 ci;
  689. ci = aenq->cc & (aenq->depth - 1);
  690. phase = aenq->phase;
  691. aenq_e = &aenq->entries[ci]; /* Get first entry */
  692. aenq_common = &aenq_e->aenq_common_desc;
  693. /* Go over all the events */
  694. while ((READ_ONCE(aenq_common->flags) &
  695. EFA_ADMIN_AENQ_COMMON_DESC_PHASE_MASK) == phase) {
  696. /*
  697. * Do not read the rest of the completion entry before the
  698. * phase bit was validated
  699. */
  700. dma_rmb();
  701. /* Handle specific event*/
  702. handler_cb = efa_com_get_specific_aenq_cb(edev,
  703. aenq_common->group);
  704. handler_cb(data, aenq_e); /* call the actual event handler*/
  705. /* Get next event entry */
  706. ci++;
  707. processed++;
  708. if (ci == aenq->depth) {
  709. ci = 0;
  710. phase = !phase;
  711. }
  712. aenq_e = &aenq->entries[ci];
  713. aenq_common = &aenq_e->aenq_common_desc;
  714. }
  715. aenq->cc += processed;
  716. aenq->phase = phase;
  717. /* Don't update aenq doorbell if there weren't any processed events */
  718. if (!processed)
  719. return;
  720. /* barrier not needed in case of writel */
  721. writel(aenq->cc, edev->reg_bar + EFA_REGS_AENQ_CONS_DB_OFF);
  722. }
  723. static void efa_com_mmio_reg_read_resp_addr_init(struct efa_com_dev *edev)
  724. {
  725. struct efa_com_mmio_read *mmio_read = &edev->mmio_read;
  726. u32 addr_high;
  727. u32 addr_low;
  728. /* dma_addr_bits is unknown at this point */
  729. addr_high = (mmio_read->read_resp_dma_addr >> 32) & GENMASK(31, 0);
  730. addr_low = mmio_read->read_resp_dma_addr & GENMASK(31, 0);
  731. writel(addr_high, edev->reg_bar + EFA_REGS_MMIO_RESP_HI_OFF);
  732. writel(addr_low, edev->reg_bar + EFA_REGS_MMIO_RESP_LO_OFF);
  733. }
  734. int efa_com_mmio_reg_read_init(struct efa_com_dev *edev)
  735. {
  736. struct efa_com_mmio_read *mmio_read = &edev->mmio_read;
  737. spin_lock_init(&mmio_read->lock);
  738. mmio_read->read_resp =
  739. dma_alloc_coherent(edev->dmadev, sizeof(*mmio_read->read_resp),
  740. &mmio_read->read_resp_dma_addr, GFP_KERNEL);
  741. if (!mmio_read->read_resp)
  742. return -ENOMEM;
  743. efa_com_mmio_reg_read_resp_addr_init(edev);
  744. mmio_read->read_resp->req_id = 0;
  745. mmio_read->seq_num = 0;
  746. mmio_read->mmio_read_timeout = EFA_REG_READ_TIMEOUT_US;
  747. return 0;
  748. }
  749. void efa_com_mmio_reg_read_destroy(struct efa_com_dev *edev)
  750. {
  751. struct efa_com_mmio_read *mmio_read = &edev->mmio_read;
  752. dma_free_coherent(edev->dmadev, sizeof(*mmio_read->read_resp),
  753. mmio_read->read_resp, mmio_read->read_resp_dma_addr);
  754. }
  755. int efa_com_validate_version(struct efa_com_dev *edev)
  756. {
  757. u32 min_ctrl_ver = 0;
  758. u32 ctrl_ver_masked;
  759. u32 min_ver = 0;
  760. u32 ctrl_ver;
  761. u32 ver;
  762. /*
  763. * Make sure the EFA version and the controller version are at least
  764. * as the driver expects
  765. */
  766. ver = efa_com_reg_read32(edev, EFA_REGS_VERSION_OFF);
  767. ctrl_ver = efa_com_reg_read32(edev,
  768. EFA_REGS_CONTROLLER_VERSION_OFF);
  769. ibdev_dbg(edev->efa_dev, "efa device version: %d.%d\n",
  770. EFA_GET(&ver, EFA_REGS_VERSION_MAJOR_VERSION),
  771. EFA_GET(&ver, EFA_REGS_VERSION_MINOR_VERSION));
  772. EFA_SET(&min_ver, EFA_REGS_VERSION_MAJOR_VERSION,
  773. EFA_ADMIN_API_VERSION_MAJOR);
  774. EFA_SET(&min_ver, EFA_REGS_VERSION_MINOR_VERSION,
  775. EFA_ADMIN_API_VERSION_MINOR);
  776. if (ver < min_ver) {
  777. ibdev_err(edev->efa_dev,
  778. "EFA version is lower than the minimal version the driver supports\n");
  779. return -EOPNOTSUPP;
  780. }
  781. ibdev_dbg(
  782. edev->efa_dev,
  783. "efa controller version: %d.%d.%d implementation version %d\n",
  784. EFA_GET(&ctrl_ver, EFA_REGS_CONTROLLER_VERSION_MAJOR_VERSION),
  785. EFA_GET(&ctrl_ver, EFA_REGS_CONTROLLER_VERSION_MINOR_VERSION),
  786. EFA_GET(&ctrl_ver,
  787. EFA_REGS_CONTROLLER_VERSION_SUBMINOR_VERSION),
  788. EFA_GET(&ctrl_ver, EFA_REGS_CONTROLLER_VERSION_IMPL_ID));
  789. ctrl_ver_masked =
  790. EFA_GET(&ctrl_ver, EFA_REGS_CONTROLLER_VERSION_MAJOR_VERSION) |
  791. EFA_GET(&ctrl_ver, EFA_REGS_CONTROLLER_VERSION_MINOR_VERSION) |
  792. EFA_GET(&ctrl_ver,
  793. EFA_REGS_CONTROLLER_VERSION_SUBMINOR_VERSION);
  794. EFA_SET(&min_ctrl_ver, EFA_REGS_CONTROLLER_VERSION_MAJOR_VERSION,
  795. EFA_CTRL_MAJOR);
  796. EFA_SET(&min_ctrl_ver, EFA_REGS_CONTROLLER_VERSION_MINOR_VERSION,
  797. EFA_CTRL_MINOR);
  798. EFA_SET(&min_ctrl_ver, EFA_REGS_CONTROLLER_VERSION_SUBMINOR_VERSION,
  799. EFA_CTRL_SUB_MINOR);
  800. /* Validate the ctrl version without the implementation ID */
  801. if (ctrl_ver_masked < min_ctrl_ver) {
  802. ibdev_err(edev->efa_dev,
  803. "EFA ctrl version is lower than the minimal ctrl version the driver supports\n");
  804. return -EOPNOTSUPP;
  805. }
  806. return 0;
  807. }
  808. /**
  809. * efa_com_get_dma_width - Retrieve physical dma address width the device
  810. * supports.
  811. * @edev: EFA communication layer struct
  812. *
  813. * Retrieve the maximum physical address bits the device can handle.
  814. *
  815. * @return: > 0 on Success and negative value otherwise.
  816. */
  817. int efa_com_get_dma_width(struct efa_com_dev *edev)
  818. {
  819. u32 caps = efa_com_reg_read32(edev, EFA_REGS_CAPS_OFF);
  820. int width;
  821. width = EFA_GET(&caps, EFA_REGS_CAPS_DMA_ADDR_WIDTH);
  822. ibdev_dbg(edev->efa_dev, "DMA width: %d\n", width);
  823. if (width < 32 || width > 64) {
  824. ibdev_err(edev->efa_dev, "DMA width illegal value: %d\n", width);
  825. return -EINVAL;
  826. }
  827. edev->dma_addr_bits = width;
  828. return width;
  829. }
  830. static int wait_for_reset_state(struct efa_com_dev *edev, u32 timeout, int on)
  831. {
  832. u32 val, i;
  833. for (i = 0; i < timeout; i++) {
  834. val = efa_com_reg_read32(edev, EFA_REGS_DEV_STS_OFF);
  835. if (EFA_GET(&val, EFA_REGS_DEV_STS_RESET_IN_PROGRESS) == on)
  836. return 0;
  837. ibdev_dbg(edev->efa_dev, "Reset indication val %d\n", val);
  838. msleep(EFA_POLL_INTERVAL_MS);
  839. }
  840. return -ETIME;
  841. }
  842. /**
  843. * efa_com_dev_reset - Perform device FLR to the device.
  844. * @edev: EFA communication layer struct
  845. * @reset_reason: Specify what is the trigger for the reset in case of an error.
  846. *
  847. * @return - 0 on success, negative value on failure.
  848. */
  849. int efa_com_dev_reset(struct efa_com_dev *edev,
  850. enum efa_regs_reset_reason_types reset_reason)
  851. {
  852. u32 stat, timeout, cap;
  853. u32 reset_val = 0;
  854. int err;
  855. stat = efa_com_reg_read32(edev, EFA_REGS_DEV_STS_OFF);
  856. cap = efa_com_reg_read32(edev, EFA_REGS_CAPS_OFF);
  857. if (!EFA_GET(&stat, EFA_REGS_DEV_STS_READY)) {
  858. ibdev_err(edev->efa_dev,
  859. "Device isn't ready, can't reset device\n");
  860. return -EINVAL;
  861. }
  862. timeout = EFA_GET(&cap, EFA_REGS_CAPS_RESET_TIMEOUT);
  863. if (!timeout) {
  864. ibdev_err(edev->efa_dev, "Invalid timeout value\n");
  865. return -EINVAL;
  866. }
  867. /* start reset */
  868. EFA_SET(&reset_val, EFA_REGS_DEV_CTL_DEV_RESET, 1);
  869. EFA_SET(&reset_val, EFA_REGS_DEV_CTL_RESET_REASON, reset_reason);
  870. writel(reset_val, edev->reg_bar + EFA_REGS_DEV_CTL_OFF);
  871. /* reset clears the mmio readless address, restore it */
  872. efa_com_mmio_reg_read_resp_addr_init(edev);
  873. err = wait_for_reset_state(edev, timeout, 1);
  874. if (err) {
  875. ibdev_err(edev->efa_dev, "Reset indication didn't turn on\n");
  876. return err;
  877. }
  878. /* reset done */
  879. writel(0, edev->reg_bar + EFA_REGS_DEV_CTL_OFF);
  880. err = wait_for_reset_state(edev, timeout, 0);
  881. if (err) {
  882. ibdev_err(edev->efa_dev, "Reset indication didn't turn off\n");
  883. return err;
  884. }
  885. timeout = EFA_GET(&cap, EFA_REGS_CAPS_ADMIN_CMD_TO);
  886. if (timeout)
  887. /* the resolution of timeout reg is 100ms */
  888. edev->aq.completion_timeout = timeout * 100000;
  889. else
  890. edev->aq.completion_timeout = ADMIN_CMD_TIMEOUT_US;
  891. return 0;
  892. }
  893. static int efa_com_create_eq(struct efa_com_dev *edev,
  894. struct efa_com_create_eq_params *params,
  895. struct efa_com_create_eq_result *result)
  896. {
  897. struct efa_com_admin_queue *aq = &edev->aq;
  898. struct efa_admin_create_eq_resp resp = {};
  899. struct efa_admin_create_eq_cmd cmd = {};
  900. int err;
  901. cmd.aq_common_descriptor.opcode = EFA_ADMIN_CREATE_EQ;
  902. EFA_SET(&cmd.caps, EFA_ADMIN_CREATE_EQ_CMD_ENTRY_SIZE_WORDS,
  903. params->entry_size_in_bytes / 4);
  904. cmd.depth = params->depth;
  905. cmd.event_bitmask = params->event_bitmask;
  906. cmd.msix_vec = params->msix_vec;
  907. efa_com_set_dma_addr(params->dma_addr, &cmd.ba.mem_addr_high,
  908. &cmd.ba.mem_addr_low);
  909. err = efa_com_cmd_exec(aq,
  910. (struct efa_admin_aq_entry *)&cmd,
  911. sizeof(cmd),
  912. (struct efa_admin_acq_entry *)&resp,
  913. sizeof(resp));
  914. if (err) {
  915. ibdev_err_ratelimited(edev->efa_dev,
  916. "Failed to create eq[%d]\n", err);
  917. return err;
  918. }
  919. result->eqn = resp.eqn;
  920. return 0;
  921. }
  922. static void efa_com_destroy_eq(struct efa_com_dev *edev,
  923. struct efa_com_destroy_eq_params *params)
  924. {
  925. struct efa_com_admin_queue *aq = &edev->aq;
  926. struct efa_admin_destroy_eq_resp resp = {};
  927. struct efa_admin_destroy_eq_cmd cmd = {};
  928. int err;
  929. cmd.aq_common_descriptor.opcode = EFA_ADMIN_DESTROY_EQ;
  930. cmd.eqn = params->eqn;
  931. err = efa_com_cmd_exec(aq,
  932. (struct efa_admin_aq_entry *)&cmd,
  933. sizeof(cmd),
  934. (struct efa_admin_acq_entry *)&resp,
  935. sizeof(resp));
  936. if (err)
  937. ibdev_err_ratelimited(edev->efa_dev,
  938. "Failed to destroy EQ-%u [%d]\n", cmd.eqn,
  939. err);
  940. }
  941. static void efa_com_arm_eq(struct efa_com_dev *edev, struct efa_com_eq *eeq)
  942. {
  943. u32 val = 0;
  944. EFA_SET(&val, EFA_REGS_EQ_DB_EQN, eeq->eqn);
  945. EFA_SET(&val, EFA_REGS_EQ_DB_ARM, 1);
  946. writel(val, edev->reg_bar + EFA_REGS_EQ_DB_OFF);
  947. }
  948. void efa_com_eq_comp_intr_handler(struct efa_com_dev *edev,
  949. struct efa_com_eq *eeq)
  950. {
  951. struct efa_admin_eqe *eqe;
  952. u32 processed = 0;
  953. u8 phase;
  954. u32 ci;
  955. ci = eeq->cc & (eeq->depth - 1);
  956. phase = eeq->phase;
  957. eqe = &eeq->eqes[ci];
  958. /* Go over all the events */
  959. while ((READ_ONCE(eqe->common) & EFA_ADMIN_EQE_PHASE_MASK) == phase) {
  960. /*
  961. * Do not read the rest of the completion entry before the
  962. * phase bit was validated
  963. */
  964. dma_rmb();
  965. eeq->cb(eeq, eqe);
  966. /* Get next event entry */
  967. ci++;
  968. processed++;
  969. if (ci == eeq->depth) {
  970. ci = 0;
  971. phase = !phase;
  972. }
  973. eqe = &eeq->eqes[ci];
  974. }
  975. eeq->cc += processed;
  976. eeq->phase = phase;
  977. efa_com_arm_eq(eeq->edev, eeq);
  978. }
  979. void efa_com_eq_destroy(struct efa_com_dev *edev, struct efa_com_eq *eeq)
  980. {
  981. struct efa_com_destroy_eq_params params = {
  982. .eqn = eeq->eqn,
  983. };
  984. efa_com_destroy_eq(edev, &params);
  985. dma_free_coherent(edev->dmadev, eeq->depth * sizeof(*eeq->eqes),
  986. eeq->eqes, eeq->dma_addr);
  987. }
  988. int efa_com_eq_init(struct efa_com_dev *edev, struct efa_com_eq *eeq,
  989. efa_eqe_handler cb, u16 depth, u8 msix_vec)
  990. {
  991. struct efa_com_create_eq_params params = {};
  992. struct efa_com_create_eq_result result = {};
  993. int err;
  994. params.depth = depth;
  995. params.entry_size_in_bytes = sizeof(*eeq->eqes);
  996. EFA_SET(&params.event_bitmask,
  997. EFA_ADMIN_CREATE_EQ_CMD_COMPLETION_EVENTS, 1);
  998. params.msix_vec = msix_vec;
  999. eeq->eqes = dma_alloc_coherent(edev->dmadev,
  1000. params.depth * sizeof(*eeq->eqes),
  1001. &params.dma_addr, GFP_KERNEL);
  1002. if (!eeq->eqes)
  1003. return -ENOMEM;
  1004. err = efa_com_create_eq(edev, &params, &result);
  1005. if (err)
  1006. goto err_free_coherent;
  1007. eeq->eqn = result.eqn;
  1008. eeq->edev = edev;
  1009. eeq->dma_addr = params.dma_addr;
  1010. eeq->phase = 1;
  1011. eeq->depth = params.depth;
  1012. eeq->cb = cb;
  1013. efa_com_arm_eq(edev, eeq);
  1014. return 0;
  1015. err_free_coherent:
  1016. dma_free_coherent(edev->dmadev, params.depth * sizeof(*eeq->eqes),
  1017. eeq->eqes, params.dma_addr);
  1018. return err;
  1019. }