uk.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646
  1. // SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
  2. /* Copyright (c) 2015 - 2021 Intel Corporation */
  3. #include "osdep.h"
  4. #include "defs.h"
  5. #include "user.h"
  6. #include "irdma.h"
  7. /**
  8. * irdma_set_fragment - set fragment in wqe
  9. * @wqe: wqe for setting fragment
  10. * @offset: offset value
  11. * @sge: sge length and stag
  12. * @valid: The wqe valid
  13. */
  14. static void irdma_set_fragment(__le64 *wqe, u32 offset, struct ib_sge *sge,
  15. u8 valid)
  16. {
  17. if (sge) {
  18. set_64bit_val(wqe, offset,
  19. FIELD_PREP(IRDMAQPSQ_FRAG_TO, sge->addr));
  20. set_64bit_val(wqe, offset + 8,
  21. FIELD_PREP(IRDMAQPSQ_VALID, valid) |
  22. FIELD_PREP(IRDMAQPSQ_FRAG_LEN, sge->length) |
  23. FIELD_PREP(IRDMAQPSQ_FRAG_STAG, sge->lkey));
  24. } else {
  25. set_64bit_val(wqe, offset, 0);
  26. set_64bit_val(wqe, offset + 8,
  27. FIELD_PREP(IRDMAQPSQ_VALID, valid));
  28. }
  29. }
  30. /**
  31. * irdma_set_fragment_gen_1 - set fragment in wqe
  32. * @wqe: wqe for setting fragment
  33. * @offset: offset value
  34. * @sge: sge length and stag
  35. * @valid: wqe valid flag
  36. */
  37. static void irdma_set_fragment_gen_1(__le64 *wqe, u32 offset,
  38. struct ib_sge *sge, u8 valid)
  39. {
  40. if (sge) {
  41. set_64bit_val(wqe, offset,
  42. FIELD_PREP(IRDMAQPSQ_FRAG_TO, sge->addr));
  43. set_64bit_val(wqe, offset + 8,
  44. FIELD_PREP(IRDMAQPSQ_GEN1_FRAG_LEN, sge->length) |
  45. FIELD_PREP(IRDMAQPSQ_GEN1_FRAG_STAG, sge->lkey));
  46. } else {
  47. set_64bit_val(wqe, offset, 0);
  48. set_64bit_val(wqe, offset + 8, 0);
  49. }
  50. }
  51. /**
  52. * irdma_nop_1 - insert a NOP wqe
  53. * @qp: hw qp ptr
  54. */
  55. static int irdma_nop_1(struct irdma_qp_uk *qp)
  56. {
  57. u64 hdr;
  58. __le64 *wqe;
  59. u32 wqe_idx;
  60. bool signaled = false;
  61. if (!qp->sq_ring.head)
  62. return -EINVAL;
  63. wqe_idx = IRDMA_RING_CURRENT_HEAD(qp->sq_ring);
  64. wqe = qp->sq_base[wqe_idx].elem;
  65. qp->sq_wrtrk_array[wqe_idx].quanta = IRDMA_QP_WQE_MIN_QUANTA;
  66. set_64bit_val(wqe, 0, 0);
  67. set_64bit_val(wqe, 8, 0);
  68. set_64bit_val(wqe, 16, 0);
  69. hdr = FIELD_PREP(IRDMAQPSQ_OPCODE, IRDMAQP_OP_NOP) |
  70. FIELD_PREP(IRDMAQPSQ_SIGCOMPL, signaled) |
  71. FIELD_PREP(IRDMAQPSQ_VALID, qp->swqe_polarity);
  72. /* make sure WQE is written before valid bit is set */
  73. dma_wmb();
  74. set_64bit_val(wqe, 24, hdr);
  75. return 0;
  76. }
  77. /**
  78. * irdma_clr_wqes - clear next 128 sq entries
  79. * @qp: hw qp ptr
  80. * @qp_wqe_idx: wqe_idx
  81. */
  82. void irdma_clr_wqes(struct irdma_qp_uk *qp, u32 qp_wqe_idx)
  83. {
  84. struct irdma_qp_quanta *sq;
  85. u32 wqe_idx;
  86. if (!(qp_wqe_idx & 0x7F)) {
  87. wqe_idx = (qp_wqe_idx + 128) % qp->sq_ring.size;
  88. sq = qp->sq_base + wqe_idx;
  89. if (wqe_idx)
  90. memset(sq, qp->swqe_polarity ? 0 : 0xFF,
  91. 128 * sizeof(*sq));
  92. else
  93. memset(sq, qp->swqe_polarity ? 0xFF : 0,
  94. 128 * sizeof(*sq));
  95. }
  96. }
  97. /**
  98. * irdma_uk_qp_post_wr - ring doorbell
  99. * @qp: hw qp ptr
  100. */
  101. void irdma_uk_qp_post_wr(struct irdma_qp_uk *qp)
  102. {
  103. u64 temp;
  104. u32 hw_sq_tail;
  105. u32 sw_sq_head;
  106. /* valid bit is written and loads completed before reading shadow */
  107. mb();
  108. /* read the doorbell shadow area */
  109. get_64bit_val(qp->shadow_area, 0, &temp);
  110. hw_sq_tail = (u32)FIELD_GET(IRDMA_QP_DBSA_HW_SQ_TAIL, temp);
  111. sw_sq_head = IRDMA_RING_CURRENT_HEAD(qp->sq_ring);
  112. if (sw_sq_head != qp->initial_ring.head) {
  113. if (sw_sq_head != hw_sq_tail) {
  114. if (sw_sq_head > qp->initial_ring.head) {
  115. if (hw_sq_tail >= qp->initial_ring.head &&
  116. hw_sq_tail < sw_sq_head)
  117. writel(qp->qp_id, qp->wqe_alloc_db);
  118. } else {
  119. if (hw_sq_tail >= qp->initial_ring.head ||
  120. hw_sq_tail < sw_sq_head)
  121. writel(qp->qp_id, qp->wqe_alloc_db);
  122. }
  123. }
  124. }
  125. qp->initial_ring.head = qp->sq_ring.head;
  126. }
  127. /**
  128. * irdma_qp_get_next_send_wqe - pad with NOP if needed, return where next WR should go
  129. * @qp: hw qp ptr
  130. * @wqe_idx: return wqe index
  131. * @quanta: size of WR in quanta
  132. * @total_size: size of WR in bytes
  133. * @info: info on WR
  134. */
  135. __le64 *irdma_qp_get_next_send_wqe(struct irdma_qp_uk *qp, u32 *wqe_idx,
  136. u16 quanta, u32 total_size,
  137. struct irdma_post_sq_info *info)
  138. {
  139. __le64 *wqe;
  140. __le64 *wqe_0 = NULL;
  141. u16 avail_quanta;
  142. u16 i;
  143. avail_quanta = qp->uk_attrs->max_hw_sq_chunk -
  144. (IRDMA_RING_CURRENT_HEAD(qp->sq_ring) %
  145. qp->uk_attrs->max_hw_sq_chunk);
  146. if (quanta <= avail_quanta) {
  147. /* WR fits in current chunk */
  148. if (quanta > IRDMA_SQ_RING_FREE_QUANTA(qp->sq_ring))
  149. return NULL;
  150. } else {
  151. /* Need to pad with NOP */
  152. if (quanta + avail_quanta >
  153. IRDMA_SQ_RING_FREE_QUANTA(qp->sq_ring))
  154. return NULL;
  155. for (i = 0; i < avail_quanta; i++) {
  156. irdma_nop_1(qp);
  157. IRDMA_RING_MOVE_HEAD_NOCHECK(qp->sq_ring);
  158. }
  159. }
  160. *wqe_idx = IRDMA_RING_CURRENT_HEAD(qp->sq_ring);
  161. if (!*wqe_idx)
  162. qp->swqe_polarity = !qp->swqe_polarity;
  163. IRDMA_RING_MOVE_HEAD_BY_COUNT_NOCHECK(qp->sq_ring, quanta);
  164. wqe = qp->sq_base[*wqe_idx].elem;
  165. if (qp->uk_attrs->hw_rev == IRDMA_GEN_1 && quanta == 1 &&
  166. (IRDMA_RING_CURRENT_HEAD(qp->sq_ring) & 1)) {
  167. wqe_0 = qp->sq_base[IRDMA_RING_CURRENT_HEAD(qp->sq_ring)].elem;
  168. wqe_0[3] = cpu_to_le64(FIELD_PREP(IRDMAQPSQ_VALID, qp->swqe_polarity ? 0 : 1));
  169. }
  170. qp->sq_wrtrk_array[*wqe_idx].wrid = info->wr_id;
  171. qp->sq_wrtrk_array[*wqe_idx].wr_len = total_size;
  172. qp->sq_wrtrk_array[*wqe_idx].quanta = quanta;
  173. return wqe;
  174. }
  175. /**
  176. * irdma_qp_get_next_recv_wqe - get next qp's rcv wqe
  177. * @qp: hw qp ptr
  178. * @wqe_idx: return wqe index
  179. */
  180. __le64 *irdma_qp_get_next_recv_wqe(struct irdma_qp_uk *qp, u32 *wqe_idx)
  181. {
  182. __le64 *wqe;
  183. int ret_code;
  184. if (IRDMA_RING_FULL_ERR(qp->rq_ring))
  185. return NULL;
  186. IRDMA_ATOMIC_RING_MOVE_HEAD(qp->rq_ring, *wqe_idx, ret_code);
  187. if (ret_code)
  188. return NULL;
  189. if (!*wqe_idx)
  190. qp->rwqe_polarity = !qp->rwqe_polarity;
  191. /* rq_wqe_size_multiplier is no of 32 byte quanta in one rq wqe */
  192. wqe = qp->rq_base[*wqe_idx * qp->rq_wqe_size_multiplier].elem;
  193. return wqe;
  194. }
  195. /**
  196. * irdma_uk_rdma_write - rdma write operation
  197. * @qp: hw qp ptr
  198. * @info: post sq information
  199. * @post_sq: flag to post sq
  200. */
  201. int irdma_uk_rdma_write(struct irdma_qp_uk *qp, struct irdma_post_sq_info *info,
  202. bool post_sq)
  203. {
  204. u64 hdr;
  205. __le64 *wqe;
  206. struct irdma_rdma_write *op_info;
  207. u32 i, wqe_idx;
  208. u32 total_size = 0, byte_off;
  209. int ret_code;
  210. u32 frag_cnt, addl_frag_cnt;
  211. bool read_fence = false;
  212. u16 quanta;
  213. op_info = &info->op.rdma_write;
  214. if (op_info->num_lo_sges > qp->max_sq_frag_cnt)
  215. return -EINVAL;
  216. for (i = 0; i < op_info->num_lo_sges; i++)
  217. total_size += op_info->lo_sg_list[i].length;
  218. read_fence |= info->read_fence;
  219. if (info->imm_data_valid)
  220. frag_cnt = op_info->num_lo_sges + 1;
  221. else
  222. frag_cnt = op_info->num_lo_sges;
  223. addl_frag_cnt = frag_cnt > 1 ? (frag_cnt - 1) : 0;
  224. ret_code = irdma_fragcnt_to_quanta_sq(frag_cnt, &quanta);
  225. if (ret_code)
  226. return ret_code;
  227. wqe = irdma_qp_get_next_send_wqe(qp, &wqe_idx, quanta, total_size,
  228. info);
  229. if (!wqe)
  230. return -ENOMEM;
  231. irdma_clr_wqes(qp, wqe_idx);
  232. set_64bit_val(wqe, 16,
  233. FIELD_PREP(IRDMAQPSQ_FRAG_TO, op_info->rem_addr.addr));
  234. if (info->imm_data_valid) {
  235. set_64bit_val(wqe, 0,
  236. FIELD_PREP(IRDMAQPSQ_IMMDATA, info->imm_data));
  237. i = 0;
  238. } else {
  239. qp->wqe_ops.iw_set_fragment(wqe, 0,
  240. op_info->lo_sg_list,
  241. qp->swqe_polarity);
  242. i = 1;
  243. }
  244. for (byte_off = 32; i < op_info->num_lo_sges; i++) {
  245. qp->wqe_ops.iw_set_fragment(wqe, byte_off,
  246. &op_info->lo_sg_list[i],
  247. qp->swqe_polarity);
  248. byte_off += 16;
  249. }
  250. /* if not an odd number set valid bit in next fragment */
  251. if (qp->uk_attrs->hw_rev >= IRDMA_GEN_2 && !(frag_cnt & 0x01) &&
  252. frag_cnt) {
  253. qp->wqe_ops.iw_set_fragment(wqe, byte_off, NULL,
  254. qp->swqe_polarity);
  255. if (qp->uk_attrs->hw_rev == IRDMA_GEN_2)
  256. ++addl_frag_cnt;
  257. }
  258. hdr = FIELD_PREP(IRDMAQPSQ_REMSTAG, op_info->rem_addr.lkey) |
  259. FIELD_PREP(IRDMAQPSQ_OPCODE, info->op_type) |
  260. FIELD_PREP(IRDMAQPSQ_IMMDATAFLAG, info->imm_data_valid) |
  261. FIELD_PREP(IRDMAQPSQ_REPORTRTT, info->report_rtt) |
  262. FIELD_PREP(IRDMAQPSQ_ADDFRAGCNT, addl_frag_cnt) |
  263. FIELD_PREP(IRDMAQPSQ_READFENCE, read_fence) |
  264. FIELD_PREP(IRDMAQPSQ_LOCALFENCE, info->local_fence) |
  265. FIELD_PREP(IRDMAQPSQ_SIGCOMPL, info->signaled) |
  266. FIELD_PREP(IRDMAQPSQ_VALID, qp->swqe_polarity);
  267. dma_wmb(); /* make sure WQE is populated before valid bit is set */
  268. set_64bit_val(wqe, 24, hdr);
  269. if (post_sq)
  270. irdma_uk_qp_post_wr(qp);
  271. return 0;
  272. }
  273. /**
  274. * irdma_uk_rdma_read - rdma read command
  275. * @qp: hw qp ptr
  276. * @info: post sq information
  277. * @inv_stag: flag for inv_stag
  278. * @post_sq: flag to post sq
  279. */
  280. int irdma_uk_rdma_read(struct irdma_qp_uk *qp, struct irdma_post_sq_info *info,
  281. bool inv_stag, bool post_sq)
  282. {
  283. struct irdma_rdma_read *op_info;
  284. int ret_code;
  285. u32 i, byte_off, total_size = 0;
  286. bool local_fence = false;
  287. u32 addl_frag_cnt;
  288. __le64 *wqe;
  289. u32 wqe_idx;
  290. u16 quanta;
  291. u64 hdr;
  292. op_info = &info->op.rdma_read;
  293. if (qp->max_sq_frag_cnt < op_info->num_lo_sges)
  294. return -EINVAL;
  295. for (i = 0; i < op_info->num_lo_sges; i++)
  296. total_size += op_info->lo_sg_list[i].length;
  297. ret_code = irdma_fragcnt_to_quanta_sq(op_info->num_lo_sges, &quanta);
  298. if (ret_code)
  299. return ret_code;
  300. wqe = irdma_qp_get_next_send_wqe(qp, &wqe_idx, quanta, total_size,
  301. info);
  302. if (!wqe)
  303. return -ENOMEM;
  304. irdma_clr_wqes(qp, wqe_idx);
  305. addl_frag_cnt = op_info->num_lo_sges > 1 ?
  306. (op_info->num_lo_sges - 1) : 0;
  307. local_fence |= info->local_fence;
  308. qp->wqe_ops.iw_set_fragment(wqe, 0, op_info->lo_sg_list,
  309. qp->swqe_polarity);
  310. for (i = 1, byte_off = 32; i < op_info->num_lo_sges; ++i) {
  311. qp->wqe_ops.iw_set_fragment(wqe, byte_off,
  312. &op_info->lo_sg_list[i],
  313. qp->swqe_polarity);
  314. byte_off += 16;
  315. }
  316. /* if not an odd number set valid bit in next fragment */
  317. if (qp->uk_attrs->hw_rev >= IRDMA_GEN_2 &&
  318. !(op_info->num_lo_sges & 0x01) && op_info->num_lo_sges) {
  319. qp->wqe_ops.iw_set_fragment(wqe, byte_off, NULL,
  320. qp->swqe_polarity);
  321. if (qp->uk_attrs->hw_rev == IRDMA_GEN_2)
  322. ++addl_frag_cnt;
  323. }
  324. set_64bit_val(wqe, 16,
  325. FIELD_PREP(IRDMAQPSQ_FRAG_TO, op_info->rem_addr.addr));
  326. hdr = FIELD_PREP(IRDMAQPSQ_REMSTAG, op_info->rem_addr.lkey) |
  327. FIELD_PREP(IRDMAQPSQ_REPORTRTT, (info->report_rtt ? 1 : 0)) |
  328. FIELD_PREP(IRDMAQPSQ_ADDFRAGCNT, addl_frag_cnt) |
  329. FIELD_PREP(IRDMAQPSQ_OPCODE,
  330. (inv_stag ? IRDMAQP_OP_RDMA_READ_LOC_INV : IRDMAQP_OP_RDMA_READ)) |
  331. FIELD_PREP(IRDMAQPSQ_READFENCE, info->read_fence) |
  332. FIELD_PREP(IRDMAQPSQ_LOCALFENCE, local_fence) |
  333. FIELD_PREP(IRDMAQPSQ_SIGCOMPL, info->signaled) |
  334. FIELD_PREP(IRDMAQPSQ_VALID, qp->swqe_polarity);
  335. dma_wmb(); /* make sure WQE is populated before valid bit is set */
  336. set_64bit_val(wqe, 24, hdr);
  337. if (post_sq)
  338. irdma_uk_qp_post_wr(qp);
  339. return 0;
  340. }
  341. /**
  342. * irdma_uk_send - rdma send command
  343. * @qp: hw qp ptr
  344. * @info: post sq information
  345. * @post_sq: flag to post sq
  346. */
  347. int irdma_uk_send(struct irdma_qp_uk *qp, struct irdma_post_sq_info *info,
  348. bool post_sq)
  349. {
  350. __le64 *wqe;
  351. struct irdma_post_send *op_info;
  352. u64 hdr;
  353. u32 i, wqe_idx, total_size = 0, byte_off;
  354. int ret_code;
  355. u32 frag_cnt, addl_frag_cnt;
  356. bool read_fence = false;
  357. u16 quanta;
  358. op_info = &info->op.send;
  359. if (qp->max_sq_frag_cnt < op_info->num_sges)
  360. return -EINVAL;
  361. for (i = 0; i < op_info->num_sges; i++)
  362. total_size += op_info->sg_list[i].length;
  363. if (info->imm_data_valid)
  364. frag_cnt = op_info->num_sges + 1;
  365. else
  366. frag_cnt = op_info->num_sges;
  367. ret_code = irdma_fragcnt_to_quanta_sq(frag_cnt, &quanta);
  368. if (ret_code)
  369. return ret_code;
  370. wqe = irdma_qp_get_next_send_wqe(qp, &wqe_idx, quanta, total_size,
  371. info);
  372. if (!wqe)
  373. return -ENOMEM;
  374. irdma_clr_wqes(qp, wqe_idx);
  375. read_fence |= info->read_fence;
  376. addl_frag_cnt = frag_cnt > 1 ? (frag_cnt - 1) : 0;
  377. if (info->imm_data_valid) {
  378. set_64bit_val(wqe, 0,
  379. FIELD_PREP(IRDMAQPSQ_IMMDATA, info->imm_data));
  380. i = 0;
  381. } else {
  382. qp->wqe_ops.iw_set_fragment(wqe, 0,
  383. frag_cnt ? op_info->sg_list : NULL,
  384. qp->swqe_polarity);
  385. i = 1;
  386. }
  387. for (byte_off = 32; i < op_info->num_sges; i++) {
  388. qp->wqe_ops.iw_set_fragment(wqe, byte_off, &op_info->sg_list[i],
  389. qp->swqe_polarity);
  390. byte_off += 16;
  391. }
  392. /* if not an odd number set valid bit in next fragment */
  393. if (qp->uk_attrs->hw_rev >= IRDMA_GEN_2 && !(frag_cnt & 0x01) &&
  394. frag_cnt) {
  395. qp->wqe_ops.iw_set_fragment(wqe, byte_off, NULL,
  396. qp->swqe_polarity);
  397. if (qp->uk_attrs->hw_rev == IRDMA_GEN_2)
  398. ++addl_frag_cnt;
  399. }
  400. set_64bit_val(wqe, 16,
  401. FIELD_PREP(IRDMAQPSQ_DESTQKEY, op_info->qkey) |
  402. FIELD_PREP(IRDMAQPSQ_DESTQPN, op_info->dest_qp));
  403. hdr = FIELD_PREP(IRDMAQPSQ_REMSTAG, info->stag_to_inv) |
  404. FIELD_PREP(IRDMAQPSQ_AHID, op_info->ah_id) |
  405. FIELD_PREP(IRDMAQPSQ_IMMDATAFLAG,
  406. (info->imm_data_valid ? 1 : 0)) |
  407. FIELD_PREP(IRDMAQPSQ_REPORTRTT, (info->report_rtt ? 1 : 0)) |
  408. FIELD_PREP(IRDMAQPSQ_OPCODE, info->op_type) |
  409. FIELD_PREP(IRDMAQPSQ_ADDFRAGCNT, addl_frag_cnt) |
  410. FIELD_PREP(IRDMAQPSQ_READFENCE, read_fence) |
  411. FIELD_PREP(IRDMAQPSQ_LOCALFENCE, info->local_fence) |
  412. FIELD_PREP(IRDMAQPSQ_SIGCOMPL, info->signaled) |
  413. FIELD_PREP(IRDMAQPSQ_UDPHEADER, info->udp_hdr) |
  414. FIELD_PREP(IRDMAQPSQ_L4LEN, info->l4len) |
  415. FIELD_PREP(IRDMAQPSQ_VALID, qp->swqe_polarity);
  416. dma_wmb(); /* make sure WQE is populated before valid bit is set */
  417. set_64bit_val(wqe, 24, hdr);
  418. if (post_sq)
  419. irdma_uk_qp_post_wr(qp);
  420. return 0;
  421. }
  422. /**
  423. * irdma_set_mw_bind_wqe_gen_1 - set mw bind wqe
  424. * @wqe: wqe for setting fragment
  425. * @op_info: info for setting bind wqe values
  426. */
  427. static void irdma_set_mw_bind_wqe_gen_1(__le64 *wqe,
  428. struct irdma_bind_window *op_info)
  429. {
  430. set_64bit_val(wqe, 0, (uintptr_t)op_info->va);
  431. set_64bit_val(wqe, 8,
  432. FIELD_PREP(IRDMAQPSQ_PARENTMRSTAG, op_info->mw_stag) |
  433. FIELD_PREP(IRDMAQPSQ_MWSTAG, op_info->mr_stag));
  434. set_64bit_val(wqe, 16, op_info->bind_len);
  435. }
  436. /**
  437. * irdma_copy_inline_data_gen_1 - Copy inline data to wqe
  438. * @wqe: pointer to wqe
  439. * @sge_list: table of pointers to inline data
  440. * @num_sges: Total inline data length
  441. * @polarity: compatibility parameter
  442. */
  443. static void irdma_copy_inline_data_gen_1(u8 *wqe, struct ib_sge *sge_list,
  444. u32 num_sges, u8 polarity)
  445. {
  446. u32 quanta_bytes_remaining = 16;
  447. int i;
  448. for (i = 0; i < num_sges; i++) {
  449. u8 *cur_sge = (u8 *)(uintptr_t)sge_list[i].addr;
  450. u32 sge_len = sge_list[i].length;
  451. while (sge_len) {
  452. u32 bytes_copied;
  453. bytes_copied = min(sge_len, quanta_bytes_remaining);
  454. memcpy(wqe, cur_sge, bytes_copied);
  455. wqe += bytes_copied;
  456. cur_sge += bytes_copied;
  457. quanta_bytes_remaining -= bytes_copied;
  458. sge_len -= bytes_copied;
  459. if (!quanta_bytes_remaining) {
  460. /* Remaining inline bytes reside after hdr */
  461. wqe += 16;
  462. quanta_bytes_remaining = 32;
  463. }
  464. }
  465. }
  466. }
  467. /**
  468. * irdma_inline_data_size_to_quanta_gen_1 - based on inline data, quanta
  469. * @data_size: data size for inline
  470. *
  471. * Gets the quanta based on inline and immediate data.
  472. */
  473. static inline u16 irdma_inline_data_size_to_quanta_gen_1(u32 data_size)
  474. {
  475. return data_size <= 16 ? IRDMA_QP_WQE_MIN_QUANTA : 2;
  476. }
  477. /**
  478. * irdma_set_mw_bind_wqe - set mw bind in wqe
  479. * @wqe: wqe for setting mw bind
  480. * @op_info: info for setting wqe values
  481. */
  482. static void irdma_set_mw_bind_wqe(__le64 *wqe,
  483. struct irdma_bind_window *op_info)
  484. {
  485. set_64bit_val(wqe, 0, (uintptr_t)op_info->va);
  486. set_64bit_val(wqe, 8,
  487. FIELD_PREP(IRDMAQPSQ_PARENTMRSTAG, op_info->mr_stag) |
  488. FIELD_PREP(IRDMAQPSQ_MWSTAG, op_info->mw_stag));
  489. set_64bit_val(wqe, 16, op_info->bind_len);
  490. }
  491. /**
  492. * irdma_copy_inline_data - Copy inline data to wqe
  493. * @wqe: pointer to wqe
  494. * @sge_list: table of pointers to inline data
  495. * @num_sges: number of SGE's
  496. * @polarity: polarity of wqe valid bit
  497. */
  498. static void irdma_copy_inline_data(u8 *wqe, struct ib_sge *sge_list,
  499. u32 num_sges, u8 polarity)
  500. {
  501. u8 inline_valid = polarity << IRDMA_INLINE_VALID_S;
  502. u32 quanta_bytes_remaining = 8;
  503. bool first_quanta = true;
  504. int i;
  505. wqe += 8;
  506. for (i = 0; i < num_sges; i++) {
  507. u8 *cur_sge = (u8 *)(uintptr_t)sge_list[i].addr;
  508. u32 sge_len = sge_list[i].length;
  509. while (sge_len) {
  510. u32 bytes_copied;
  511. bytes_copied = min(sge_len, quanta_bytes_remaining);
  512. memcpy(wqe, cur_sge, bytes_copied);
  513. wqe += bytes_copied;
  514. cur_sge += bytes_copied;
  515. quanta_bytes_remaining -= bytes_copied;
  516. sge_len -= bytes_copied;
  517. if (!quanta_bytes_remaining) {
  518. quanta_bytes_remaining = 31;
  519. /* Remaining inline bytes reside after hdr */
  520. if (first_quanta) {
  521. first_quanta = false;
  522. wqe += 16;
  523. } else {
  524. *wqe = inline_valid;
  525. wqe++;
  526. }
  527. }
  528. }
  529. }
  530. if (!first_quanta && quanta_bytes_remaining < 31)
  531. *(wqe + quanta_bytes_remaining) = inline_valid;
  532. }
  533. /**
  534. * irdma_inline_data_size_to_quanta - based on inline data, quanta
  535. * @data_size: data size for inline
  536. *
  537. * Gets the quanta based on inline and immediate data.
  538. */
  539. static u16 irdma_inline_data_size_to_quanta(u32 data_size)
  540. {
  541. if (data_size <= 8)
  542. return IRDMA_QP_WQE_MIN_QUANTA;
  543. else if (data_size <= 39)
  544. return 2;
  545. else if (data_size <= 70)
  546. return 3;
  547. else if (data_size <= 101)
  548. return 4;
  549. else if (data_size <= 132)
  550. return 5;
  551. else if (data_size <= 163)
  552. return 6;
  553. else if (data_size <= 194)
  554. return 7;
  555. else
  556. return 8;
  557. }
  558. /**
  559. * irdma_uk_inline_rdma_write - inline rdma write operation
  560. * @qp: hw qp ptr
  561. * @info: post sq information
  562. * @post_sq: flag to post sq
  563. */
  564. int irdma_uk_inline_rdma_write(struct irdma_qp_uk *qp,
  565. struct irdma_post_sq_info *info, bool post_sq)
  566. {
  567. __le64 *wqe;
  568. struct irdma_rdma_write *op_info;
  569. u64 hdr = 0;
  570. u32 wqe_idx;
  571. bool read_fence = false;
  572. u32 i, total_size = 0;
  573. u16 quanta;
  574. op_info = &info->op.rdma_write;
  575. if (unlikely(qp->max_sq_frag_cnt < op_info->num_lo_sges))
  576. return -EINVAL;
  577. for (i = 0; i < op_info->num_lo_sges; i++)
  578. total_size += op_info->lo_sg_list[i].length;
  579. if (unlikely(total_size > qp->max_inline_data))
  580. return -EINVAL;
  581. quanta = qp->wqe_ops.iw_inline_data_size_to_quanta(total_size);
  582. wqe = irdma_qp_get_next_send_wqe(qp, &wqe_idx, quanta, total_size,
  583. info);
  584. if (!wqe)
  585. return -ENOMEM;
  586. irdma_clr_wqes(qp, wqe_idx);
  587. read_fence |= info->read_fence;
  588. set_64bit_val(wqe, 16,
  589. FIELD_PREP(IRDMAQPSQ_FRAG_TO, op_info->rem_addr.addr));
  590. hdr = FIELD_PREP(IRDMAQPSQ_REMSTAG, op_info->rem_addr.lkey) |
  591. FIELD_PREP(IRDMAQPSQ_OPCODE, info->op_type) |
  592. FIELD_PREP(IRDMAQPSQ_INLINEDATALEN, total_size) |
  593. FIELD_PREP(IRDMAQPSQ_REPORTRTT, info->report_rtt ? 1 : 0) |
  594. FIELD_PREP(IRDMAQPSQ_INLINEDATAFLAG, 1) |
  595. FIELD_PREP(IRDMAQPSQ_IMMDATAFLAG, info->imm_data_valid ? 1 : 0) |
  596. FIELD_PREP(IRDMAQPSQ_READFENCE, read_fence) |
  597. FIELD_PREP(IRDMAQPSQ_LOCALFENCE, info->local_fence) |
  598. FIELD_PREP(IRDMAQPSQ_SIGCOMPL, info->signaled) |
  599. FIELD_PREP(IRDMAQPSQ_VALID, qp->swqe_polarity);
  600. if (info->imm_data_valid)
  601. set_64bit_val(wqe, 0,
  602. FIELD_PREP(IRDMAQPSQ_IMMDATA, info->imm_data));
  603. qp->wqe_ops.iw_copy_inline_data((u8 *)wqe, op_info->lo_sg_list,
  604. op_info->num_lo_sges,
  605. qp->swqe_polarity);
  606. dma_wmb(); /* make sure WQE is populated before valid bit is set */
  607. set_64bit_val(wqe, 24, hdr);
  608. if (post_sq)
  609. irdma_uk_qp_post_wr(qp);
  610. return 0;
  611. }
  612. /**
  613. * irdma_uk_inline_send - inline send operation
  614. * @qp: hw qp ptr
  615. * @info: post sq information
  616. * @post_sq: flag to post sq
  617. */
  618. int irdma_uk_inline_send(struct irdma_qp_uk *qp,
  619. struct irdma_post_sq_info *info, bool post_sq)
  620. {
  621. __le64 *wqe;
  622. struct irdma_post_send *op_info;
  623. u64 hdr;
  624. u32 wqe_idx;
  625. bool read_fence = false;
  626. u32 i, total_size = 0;
  627. u16 quanta;
  628. op_info = &info->op.send;
  629. if (unlikely(qp->max_sq_frag_cnt < op_info->num_sges))
  630. return -EINVAL;
  631. for (i = 0; i < op_info->num_sges; i++)
  632. total_size += op_info->sg_list[i].length;
  633. if (unlikely(total_size > qp->max_inline_data))
  634. return -EINVAL;
  635. quanta = qp->wqe_ops.iw_inline_data_size_to_quanta(total_size);
  636. wqe = irdma_qp_get_next_send_wqe(qp, &wqe_idx, quanta, total_size,
  637. info);
  638. if (!wqe)
  639. return -ENOMEM;
  640. irdma_clr_wqes(qp, wqe_idx);
  641. set_64bit_val(wqe, 16,
  642. FIELD_PREP(IRDMAQPSQ_DESTQKEY, op_info->qkey) |
  643. FIELD_PREP(IRDMAQPSQ_DESTQPN, op_info->dest_qp));
  644. read_fence |= info->read_fence;
  645. hdr = FIELD_PREP(IRDMAQPSQ_REMSTAG, info->stag_to_inv) |
  646. FIELD_PREP(IRDMAQPSQ_AHID, op_info->ah_id) |
  647. FIELD_PREP(IRDMAQPSQ_OPCODE, info->op_type) |
  648. FIELD_PREP(IRDMAQPSQ_INLINEDATALEN, total_size) |
  649. FIELD_PREP(IRDMAQPSQ_IMMDATAFLAG,
  650. (info->imm_data_valid ? 1 : 0)) |
  651. FIELD_PREP(IRDMAQPSQ_REPORTRTT, (info->report_rtt ? 1 : 0)) |
  652. FIELD_PREP(IRDMAQPSQ_INLINEDATAFLAG, 1) |
  653. FIELD_PREP(IRDMAQPSQ_READFENCE, read_fence) |
  654. FIELD_PREP(IRDMAQPSQ_LOCALFENCE, info->local_fence) |
  655. FIELD_PREP(IRDMAQPSQ_SIGCOMPL, info->signaled) |
  656. FIELD_PREP(IRDMAQPSQ_UDPHEADER, info->udp_hdr) |
  657. FIELD_PREP(IRDMAQPSQ_L4LEN, info->l4len) |
  658. FIELD_PREP(IRDMAQPSQ_VALID, qp->swqe_polarity);
  659. if (info->imm_data_valid)
  660. set_64bit_val(wqe, 0,
  661. FIELD_PREP(IRDMAQPSQ_IMMDATA, info->imm_data));
  662. qp->wqe_ops.iw_copy_inline_data((u8 *)wqe, op_info->sg_list,
  663. op_info->num_sges, qp->swqe_polarity);
  664. dma_wmb(); /* make sure WQE is populated before valid bit is set */
  665. set_64bit_val(wqe, 24, hdr);
  666. if (post_sq)
  667. irdma_uk_qp_post_wr(qp);
  668. return 0;
  669. }
  670. /**
  671. * irdma_uk_stag_local_invalidate - stag invalidate operation
  672. * @qp: hw qp ptr
  673. * @info: post sq information
  674. * @post_sq: flag to post sq
  675. */
  676. int irdma_uk_stag_local_invalidate(struct irdma_qp_uk *qp,
  677. struct irdma_post_sq_info *info,
  678. bool post_sq)
  679. {
  680. __le64 *wqe;
  681. struct irdma_inv_local_stag *op_info;
  682. u64 hdr;
  683. u32 wqe_idx;
  684. bool local_fence = false;
  685. struct ib_sge sge = {};
  686. op_info = &info->op.inv_local_stag;
  687. local_fence = info->local_fence;
  688. wqe = irdma_qp_get_next_send_wqe(qp, &wqe_idx, IRDMA_QP_WQE_MIN_QUANTA,
  689. 0, info);
  690. if (!wqe)
  691. return -ENOMEM;
  692. irdma_clr_wqes(qp, wqe_idx);
  693. sge.lkey = op_info->target_stag;
  694. qp->wqe_ops.iw_set_fragment(wqe, 0, &sge, 0);
  695. set_64bit_val(wqe, 16, 0);
  696. hdr = FIELD_PREP(IRDMAQPSQ_OPCODE, IRDMA_OP_TYPE_INV_STAG) |
  697. FIELD_PREP(IRDMAQPSQ_READFENCE, info->read_fence) |
  698. FIELD_PREP(IRDMAQPSQ_LOCALFENCE, local_fence) |
  699. FIELD_PREP(IRDMAQPSQ_SIGCOMPL, info->signaled) |
  700. FIELD_PREP(IRDMAQPSQ_VALID, qp->swqe_polarity);
  701. dma_wmb(); /* make sure WQE is populated before valid bit is set */
  702. set_64bit_val(wqe, 24, hdr);
  703. if (post_sq)
  704. irdma_uk_qp_post_wr(qp);
  705. return 0;
  706. }
  707. /**
  708. * irdma_uk_post_receive - post receive wqe
  709. * @qp: hw qp ptr
  710. * @info: post rq information
  711. */
  712. int irdma_uk_post_receive(struct irdma_qp_uk *qp,
  713. struct irdma_post_rq_info *info)
  714. {
  715. u32 wqe_idx, i, byte_off;
  716. u32 addl_frag_cnt;
  717. __le64 *wqe;
  718. u64 hdr;
  719. if (qp->max_rq_frag_cnt < info->num_sges)
  720. return -EINVAL;
  721. wqe = irdma_qp_get_next_recv_wqe(qp, &wqe_idx);
  722. if (!wqe)
  723. return -ENOMEM;
  724. qp->rq_wrid_array[wqe_idx] = info->wr_id;
  725. addl_frag_cnt = info->num_sges > 1 ? (info->num_sges - 1) : 0;
  726. qp->wqe_ops.iw_set_fragment(wqe, 0, info->sg_list,
  727. qp->rwqe_polarity);
  728. for (i = 1, byte_off = 32; i < info->num_sges; i++) {
  729. qp->wqe_ops.iw_set_fragment(wqe, byte_off, &info->sg_list[i],
  730. qp->rwqe_polarity);
  731. byte_off += 16;
  732. }
  733. /* if not an odd number set valid bit in next fragment */
  734. if (qp->uk_attrs->hw_rev >= IRDMA_GEN_2 && !(info->num_sges & 0x01) &&
  735. info->num_sges) {
  736. qp->wqe_ops.iw_set_fragment(wqe, byte_off, NULL,
  737. qp->rwqe_polarity);
  738. if (qp->uk_attrs->hw_rev == IRDMA_GEN_2)
  739. ++addl_frag_cnt;
  740. }
  741. set_64bit_val(wqe, 16, 0);
  742. hdr = FIELD_PREP(IRDMAQPSQ_ADDFRAGCNT, addl_frag_cnt) |
  743. FIELD_PREP(IRDMAQPSQ_VALID, qp->rwqe_polarity);
  744. dma_wmb(); /* make sure WQE is populated before valid bit is set */
  745. set_64bit_val(wqe, 24, hdr);
  746. return 0;
  747. }
  748. /**
  749. * irdma_uk_cq_resize - reset the cq buffer info
  750. * @cq: cq to resize
  751. * @cq_base: new cq buffer addr
  752. * @cq_size: number of cqes
  753. */
  754. void irdma_uk_cq_resize(struct irdma_cq_uk *cq, void *cq_base, int cq_size)
  755. {
  756. cq->cq_base = cq_base;
  757. cq->cq_size = cq_size;
  758. IRDMA_RING_INIT(cq->cq_ring, cq->cq_size);
  759. cq->polarity = 1;
  760. }
  761. /**
  762. * irdma_uk_cq_set_resized_cnt - record the count of the resized buffers
  763. * @cq: cq to resize
  764. * @cq_cnt: the count of the resized cq buffers
  765. */
  766. void irdma_uk_cq_set_resized_cnt(struct irdma_cq_uk *cq, u16 cq_cnt)
  767. {
  768. u64 temp_val;
  769. u16 sw_cq_sel;
  770. u8 arm_next_se;
  771. u8 arm_next;
  772. u8 arm_seq_num;
  773. get_64bit_val(cq->shadow_area, 32, &temp_val);
  774. sw_cq_sel = (u16)FIELD_GET(IRDMA_CQ_DBSA_SW_CQ_SELECT, temp_val);
  775. sw_cq_sel += cq_cnt;
  776. arm_seq_num = (u8)FIELD_GET(IRDMA_CQ_DBSA_ARM_SEQ_NUM, temp_val);
  777. arm_next_se = (u8)FIELD_GET(IRDMA_CQ_DBSA_ARM_NEXT_SE, temp_val);
  778. arm_next = (u8)FIELD_GET(IRDMA_CQ_DBSA_ARM_NEXT, temp_val);
  779. temp_val = FIELD_PREP(IRDMA_CQ_DBSA_ARM_SEQ_NUM, arm_seq_num) |
  780. FIELD_PREP(IRDMA_CQ_DBSA_SW_CQ_SELECT, sw_cq_sel) |
  781. FIELD_PREP(IRDMA_CQ_DBSA_ARM_NEXT_SE, arm_next_se) |
  782. FIELD_PREP(IRDMA_CQ_DBSA_ARM_NEXT, arm_next);
  783. set_64bit_val(cq->shadow_area, 32, temp_val);
  784. }
  785. /**
  786. * irdma_uk_cq_request_notification - cq notification request (door bell)
  787. * @cq: hw cq
  788. * @cq_notify: notification type
  789. */
  790. void irdma_uk_cq_request_notification(struct irdma_cq_uk *cq,
  791. enum irdma_cmpl_notify cq_notify)
  792. {
  793. u64 temp_val;
  794. u16 sw_cq_sel;
  795. u8 arm_next_se = 0;
  796. u8 arm_next = 0;
  797. u8 arm_seq_num;
  798. get_64bit_val(cq->shadow_area, 32, &temp_val);
  799. arm_seq_num = (u8)FIELD_GET(IRDMA_CQ_DBSA_ARM_SEQ_NUM, temp_val);
  800. arm_seq_num++;
  801. sw_cq_sel = (u16)FIELD_GET(IRDMA_CQ_DBSA_SW_CQ_SELECT, temp_val);
  802. arm_next_se = (u8)FIELD_GET(IRDMA_CQ_DBSA_ARM_NEXT_SE, temp_val);
  803. arm_next_se |= 1;
  804. if (cq_notify == IRDMA_CQ_COMPL_EVENT)
  805. arm_next = 1;
  806. temp_val = FIELD_PREP(IRDMA_CQ_DBSA_ARM_SEQ_NUM, arm_seq_num) |
  807. FIELD_PREP(IRDMA_CQ_DBSA_SW_CQ_SELECT, sw_cq_sel) |
  808. FIELD_PREP(IRDMA_CQ_DBSA_ARM_NEXT_SE, arm_next_se) |
  809. FIELD_PREP(IRDMA_CQ_DBSA_ARM_NEXT, arm_next);
  810. set_64bit_val(cq->shadow_area, 32, temp_val);
  811. dma_wmb(); /* make sure WQE is populated before valid bit is set */
  812. writel(cq->cq_id, cq->cqe_alloc_db);
  813. }
  814. /**
  815. * irdma_uk_cq_poll_cmpl - get cq completion info
  816. * @cq: hw cq
  817. * @info: cq poll information returned
  818. */
  819. int irdma_uk_cq_poll_cmpl(struct irdma_cq_uk *cq,
  820. struct irdma_cq_poll_info *info)
  821. {
  822. u64 comp_ctx, qword0, qword2, qword3;
  823. __le64 *cqe;
  824. struct irdma_qp_uk *qp;
  825. struct irdma_ring *pring = NULL;
  826. u32 wqe_idx;
  827. int ret_code;
  828. bool move_cq_head = true;
  829. u8 polarity;
  830. bool ext_valid;
  831. __le64 *ext_cqe;
  832. if (cq->avoid_mem_cflct)
  833. cqe = IRDMA_GET_CURRENT_EXTENDED_CQ_ELEM(cq);
  834. else
  835. cqe = IRDMA_GET_CURRENT_CQ_ELEM(cq);
  836. get_64bit_val(cqe, 24, &qword3);
  837. polarity = (u8)FIELD_GET(IRDMA_CQ_VALID, qword3);
  838. if (polarity != cq->polarity)
  839. return -ENOENT;
  840. /* Ensure CQE contents are read after valid bit is checked */
  841. dma_rmb();
  842. ext_valid = (bool)FIELD_GET(IRDMA_CQ_EXTCQE, qword3);
  843. if (ext_valid) {
  844. u64 qword6, qword7;
  845. u32 peek_head;
  846. if (cq->avoid_mem_cflct) {
  847. ext_cqe = (__le64 *)((u8 *)cqe + 32);
  848. get_64bit_val(ext_cqe, 24, &qword7);
  849. polarity = (u8)FIELD_GET(IRDMA_CQ_VALID, qword7);
  850. } else {
  851. peek_head = (cq->cq_ring.head + 1) % cq->cq_ring.size;
  852. ext_cqe = cq->cq_base[peek_head].buf;
  853. get_64bit_val(ext_cqe, 24, &qword7);
  854. polarity = (u8)FIELD_GET(IRDMA_CQ_VALID, qword7);
  855. if (!peek_head)
  856. polarity ^= 1;
  857. }
  858. if (polarity != cq->polarity)
  859. return -ENOENT;
  860. /* Ensure ext CQE contents are read after ext valid bit is checked */
  861. dma_rmb();
  862. info->imm_valid = (bool)FIELD_GET(IRDMA_CQ_IMMVALID, qword7);
  863. if (info->imm_valid) {
  864. u64 qword4;
  865. get_64bit_val(ext_cqe, 0, &qword4);
  866. info->imm_data = (u32)FIELD_GET(IRDMA_CQ_IMMDATALOW32, qword4);
  867. }
  868. info->ud_smac_valid = (bool)FIELD_GET(IRDMA_CQ_UDSMACVALID, qword7);
  869. info->ud_vlan_valid = (bool)FIELD_GET(IRDMA_CQ_UDVLANVALID, qword7);
  870. if (info->ud_smac_valid || info->ud_vlan_valid) {
  871. get_64bit_val(ext_cqe, 16, &qword6);
  872. if (info->ud_vlan_valid)
  873. info->ud_vlan = (u16)FIELD_GET(IRDMA_CQ_UDVLAN, qword6);
  874. if (info->ud_smac_valid) {
  875. info->ud_smac[5] = qword6 & 0xFF;
  876. info->ud_smac[4] = (qword6 >> 8) & 0xFF;
  877. info->ud_smac[3] = (qword6 >> 16) & 0xFF;
  878. info->ud_smac[2] = (qword6 >> 24) & 0xFF;
  879. info->ud_smac[1] = (qword6 >> 32) & 0xFF;
  880. info->ud_smac[0] = (qword6 >> 40) & 0xFF;
  881. }
  882. }
  883. } else {
  884. info->imm_valid = false;
  885. info->ud_smac_valid = false;
  886. info->ud_vlan_valid = false;
  887. }
  888. info->q_type = (u8)FIELD_GET(IRDMA_CQ_SQ, qword3);
  889. info->error = (bool)FIELD_GET(IRDMA_CQ_ERROR, qword3);
  890. info->ipv4 = (bool)FIELD_GET(IRDMACQ_IPV4, qword3);
  891. if (info->error) {
  892. info->major_err = FIELD_GET(IRDMA_CQ_MAJERR, qword3);
  893. info->minor_err = FIELD_GET(IRDMA_CQ_MINERR, qword3);
  894. if (info->major_err == IRDMA_FLUSH_MAJOR_ERR) {
  895. info->comp_status = IRDMA_COMPL_STATUS_FLUSHED;
  896. /* Set the min error to standard flush error code for remaining cqes */
  897. if (info->minor_err != FLUSH_GENERAL_ERR) {
  898. qword3 &= ~IRDMA_CQ_MINERR;
  899. qword3 |= FIELD_PREP(IRDMA_CQ_MINERR, FLUSH_GENERAL_ERR);
  900. set_64bit_val(cqe, 24, qword3);
  901. }
  902. } else {
  903. info->comp_status = IRDMA_COMPL_STATUS_UNKNOWN;
  904. }
  905. } else {
  906. info->comp_status = IRDMA_COMPL_STATUS_SUCCESS;
  907. }
  908. get_64bit_val(cqe, 0, &qword0);
  909. get_64bit_val(cqe, 16, &qword2);
  910. info->tcp_seq_num_rtt = (u32)FIELD_GET(IRDMACQ_TCPSEQNUMRTT, qword0);
  911. info->qp_id = (u32)FIELD_GET(IRDMACQ_QPID, qword2);
  912. info->ud_src_qpn = (u32)FIELD_GET(IRDMACQ_UDSRCQPN, qword2);
  913. get_64bit_val(cqe, 8, &comp_ctx);
  914. info->solicited_event = (bool)FIELD_GET(IRDMACQ_SOEVENT, qword3);
  915. qp = (struct irdma_qp_uk *)(unsigned long)comp_ctx;
  916. if (!qp || qp->destroy_pending) {
  917. ret_code = -EFAULT;
  918. goto exit;
  919. }
  920. wqe_idx = (u32)FIELD_GET(IRDMA_CQ_WQEIDX, qword3);
  921. info->qp_handle = (irdma_qp_handle)(unsigned long)qp;
  922. info->op_type = (u8)FIELD_GET(IRDMACQ_OP, qword3);
  923. if (info->q_type == IRDMA_CQE_QTYPE_RQ) {
  924. u32 array_idx;
  925. array_idx = wqe_idx / qp->rq_wqe_size_multiplier;
  926. if (info->comp_status == IRDMA_COMPL_STATUS_FLUSHED ||
  927. info->comp_status == IRDMA_COMPL_STATUS_UNKNOWN) {
  928. if (!IRDMA_RING_MORE_WORK(qp->rq_ring)) {
  929. ret_code = -ENOENT;
  930. goto exit;
  931. }
  932. info->wr_id = qp->rq_wrid_array[qp->rq_ring.tail];
  933. array_idx = qp->rq_ring.tail;
  934. } else {
  935. info->wr_id = qp->rq_wrid_array[array_idx];
  936. }
  937. info->bytes_xfered = (u32)FIELD_GET(IRDMACQ_PAYLDLEN, qword0);
  938. if (qword3 & IRDMACQ_STAG) {
  939. info->stag_invalid_set = true;
  940. info->inv_stag = (u32)FIELD_GET(IRDMACQ_INVSTAG, qword2);
  941. } else {
  942. info->stag_invalid_set = false;
  943. }
  944. IRDMA_RING_SET_TAIL(qp->rq_ring, array_idx + 1);
  945. if (info->comp_status == IRDMA_COMPL_STATUS_FLUSHED) {
  946. qp->rq_flush_seen = true;
  947. if (!IRDMA_RING_MORE_WORK(qp->rq_ring))
  948. qp->rq_flush_complete = true;
  949. else
  950. move_cq_head = false;
  951. }
  952. pring = &qp->rq_ring;
  953. } else { /* q_type is IRDMA_CQE_QTYPE_SQ */
  954. if (qp->first_sq_wq) {
  955. if (wqe_idx + 1 >= qp->conn_wqes)
  956. qp->first_sq_wq = false;
  957. if (wqe_idx < qp->conn_wqes && qp->sq_ring.head == qp->sq_ring.tail) {
  958. IRDMA_RING_MOVE_HEAD_NOCHECK(cq->cq_ring);
  959. IRDMA_RING_MOVE_TAIL(cq->cq_ring);
  960. set_64bit_val(cq->shadow_area, 0,
  961. IRDMA_RING_CURRENT_HEAD(cq->cq_ring));
  962. memset(info, 0,
  963. sizeof(struct irdma_cq_poll_info));
  964. return irdma_uk_cq_poll_cmpl(cq, info);
  965. }
  966. }
  967. if (info->comp_status != IRDMA_COMPL_STATUS_FLUSHED) {
  968. info->wr_id = qp->sq_wrtrk_array[wqe_idx].wrid;
  969. if (!info->comp_status)
  970. info->bytes_xfered = qp->sq_wrtrk_array[wqe_idx].wr_len;
  971. info->op_type = (u8)FIELD_GET(IRDMACQ_OP, qword3);
  972. IRDMA_RING_SET_TAIL(qp->sq_ring,
  973. wqe_idx + qp->sq_wrtrk_array[wqe_idx].quanta);
  974. } else {
  975. if (!IRDMA_RING_MORE_WORK(qp->sq_ring)) {
  976. ret_code = -ENOENT;
  977. goto exit;
  978. }
  979. do {
  980. __le64 *sw_wqe;
  981. u64 wqe_qword;
  982. u32 tail;
  983. tail = qp->sq_ring.tail;
  984. sw_wqe = qp->sq_base[tail].elem;
  985. get_64bit_val(sw_wqe, 24,
  986. &wqe_qword);
  987. info->op_type = (u8)FIELD_GET(IRDMAQPSQ_OPCODE,
  988. wqe_qword);
  989. IRDMA_RING_SET_TAIL(qp->sq_ring,
  990. tail + qp->sq_wrtrk_array[tail].quanta);
  991. if (info->op_type != IRDMAQP_OP_NOP) {
  992. info->wr_id = qp->sq_wrtrk_array[tail].wrid;
  993. info->bytes_xfered = qp->sq_wrtrk_array[tail].wr_len;
  994. break;
  995. }
  996. } while (1);
  997. if (info->op_type == IRDMA_OP_TYPE_BIND_MW &&
  998. info->minor_err == FLUSH_PROT_ERR)
  999. info->minor_err = FLUSH_MW_BIND_ERR;
  1000. qp->sq_flush_seen = true;
  1001. if (!IRDMA_RING_MORE_WORK(qp->sq_ring))
  1002. qp->sq_flush_complete = true;
  1003. }
  1004. pring = &qp->sq_ring;
  1005. }
  1006. ret_code = 0;
  1007. exit:
  1008. if (!ret_code && info->comp_status == IRDMA_COMPL_STATUS_FLUSHED)
  1009. if (pring && IRDMA_RING_MORE_WORK(*pring))
  1010. move_cq_head = false;
  1011. if (move_cq_head) {
  1012. IRDMA_RING_MOVE_HEAD_NOCHECK(cq->cq_ring);
  1013. if (!IRDMA_RING_CURRENT_HEAD(cq->cq_ring))
  1014. cq->polarity ^= 1;
  1015. if (ext_valid && !cq->avoid_mem_cflct) {
  1016. IRDMA_RING_MOVE_HEAD_NOCHECK(cq->cq_ring);
  1017. if (!IRDMA_RING_CURRENT_HEAD(cq->cq_ring))
  1018. cq->polarity ^= 1;
  1019. }
  1020. IRDMA_RING_MOVE_TAIL(cq->cq_ring);
  1021. if (!cq->avoid_mem_cflct && ext_valid)
  1022. IRDMA_RING_MOVE_TAIL(cq->cq_ring);
  1023. set_64bit_val(cq->shadow_area, 0,
  1024. IRDMA_RING_CURRENT_HEAD(cq->cq_ring));
  1025. } else {
  1026. qword3 &= ~IRDMA_CQ_WQEIDX;
  1027. qword3 |= FIELD_PREP(IRDMA_CQ_WQEIDX, pring->tail);
  1028. set_64bit_val(cqe, 24, qword3);
  1029. }
  1030. return ret_code;
  1031. }
  1032. /**
  1033. * irdma_qp_round_up - return round up qp wq depth
  1034. * @wqdepth: wq depth in quanta to round up
  1035. */
  1036. static int irdma_qp_round_up(u32 wqdepth)
  1037. {
  1038. int scount = 1;
  1039. for (wqdepth--; scount <= 16; scount *= 2)
  1040. wqdepth |= wqdepth >> scount;
  1041. return ++wqdepth;
  1042. }
  1043. /**
  1044. * irdma_get_wqe_shift - get shift count for maximum wqe size
  1045. * @uk_attrs: qp HW attributes
  1046. * @sge: Maximum Scatter Gather Elements wqe
  1047. * @inline_data: Maximum inline data size
  1048. * @shift: Returns the shift needed based on sge
  1049. *
  1050. * Shift can be used to left shift the wqe size based on number of SGEs and inlind data size.
  1051. * For 1 SGE or inline data <= 8, shift = 0 (wqe size of 32
  1052. * bytes). For 2 or 3 SGEs or inline data <= 39, shift = 1 (wqe
  1053. * size of 64 bytes).
  1054. * For 4-7 SGE's and inline <= 101 Shift of 2 otherwise (wqe
  1055. * size of 256 bytes).
  1056. */
  1057. void irdma_get_wqe_shift(struct irdma_uk_attrs *uk_attrs, u32 sge,
  1058. u32 inline_data, u8 *shift)
  1059. {
  1060. *shift = 0;
  1061. if (uk_attrs->hw_rev >= IRDMA_GEN_2) {
  1062. if (sge > 1 || inline_data > 8) {
  1063. if (sge < 4 && inline_data <= 39)
  1064. *shift = 1;
  1065. else if (sge < 8 && inline_data <= 101)
  1066. *shift = 2;
  1067. else
  1068. *shift = 3;
  1069. }
  1070. } else if (sge > 1 || inline_data > 16) {
  1071. *shift = (sge < 4 && inline_data <= 48) ? 1 : 2;
  1072. }
  1073. }
  1074. /*
  1075. * irdma_get_sqdepth - get SQ depth (quanta)
  1076. * @uk_attrs: qp HW attributes
  1077. * @sq_size: SQ size
  1078. * @shift: shift which determines size of WQE
  1079. * @sqdepth: depth of SQ
  1080. *
  1081. */
  1082. int irdma_get_sqdepth(struct irdma_uk_attrs *uk_attrs, u32 sq_size, u8 shift,
  1083. u32 *sqdepth)
  1084. {
  1085. u32 min_size = (u32)uk_attrs->min_hw_wq_size << shift;
  1086. *sqdepth = irdma_qp_round_up((sq_size << shift) + IRDMA_SQ_RSVD);
  1087. if (*sqdepth < min_size)
  1088. *sqdepth = min_size;
  1089. else if (*sqdepth > uk_attrs->max_hw_wq_quanta)
  1090. return -EINVAL;
  1091. return 0;
  1092. }
  1093. /*
  1094. * irdma_get_rqdepth - get RQ depth (quanta)
  1095. * @uk_attrs: qp HW attributes
  1096. * @rq_size: RQ size
  1097. * @shift: shift which determines size of WQE
  1098. * @rqdepth: depth of RQ
  1099. */
  1100. int irdma_get_rqdepth(struct irdma_uk_attrs *uk_attrs, u32 rq_size, u8 shift,
  1101. u32 *rqdepth)
  1102. {
  1103. u32 min_size = (u32)uk_attrs->min_hw_wq_size << shift;
  1104. *rqdepth = irdma_qp_round_up((rq_size << shift) + IRDMA_RQ_RSVD);
  1105. if (*rqdepth < min_size)
  1106. *rqdepth = min_size;
  1107. else if (*rqdepth > uk_attrs->max_hw_rq_quanta)
  1108. return -EINVAL;
  1109. return 0;
  1110. }
  1111. static const struct irdma_wqe_uk_ops iw_wqe_uk_ops = {
  1112. .iw_copy_inline_data = irdma_copy_inline_data,
  1113. .iw_inline_data_size_to_quanta = irdma_inline_data_size_to_quanta,
  1114. .iw_set_fragment = irdma_set_fragment,
  1115. .iw_set_mw_bind_wqe = irdma_set_mw_bind_wqe,
  1116. };
  1117. static const struct irdma_wqe_uk_ops iw_wqe_uk_ops_gen_1 = {
  1118. .iw_copy_inline_data = irdma_copy_inline_data_gen_1,
  1119. .iw_inline_data_size_to_quanta = irdma_inline_data_size_to_quanta_gen_1,
  1120. .iw_set_fragment = irdma_set_fragment_gen_1,
  1121. .iw_set_mw_bind_wqe = irdma_set_mw_bind_wqe_gen_1,
  1122. };
  1123. /**
  1124. * irdma_setup_connection_wqes - setup WQEs necessary to complete
  1125. * connection.
  1126. * @qp: hw qp (user and kernel)
  1127. * @info: qp initialization info
  1128. */
  1129. static void irdma_setup_connection_wqes(struct irdma_qp_uk *qp,
  1130. struct irdma_qp_uk_init_info *info)
  1131. {
  1132. u16 move_cnt = 1;
  1133. if (!info->legacy_mode &&
  1134. (qp->uk_attrs->feature_flags & IRDMA_FEATURE_RTS_AE))
  1135. move_cnt = 3;
  1136. qp->conn_wqes = move_cnt;
  1137. IRDMA_RING_MOVE_HEAD_BY_COUNT_NOCHECK(qp->sq_ring, move_cnt);
  1138. IRDMA_RING_MOVE_TAIL_BY_COUNT(qp->sq_ring, move_cnt);
  1139. IRDMA_RING_MOVE_HEAD_BY_COUNT_NOCHECK(qp->initial_ring, move_cnt);
  1140. }
  1141. /**
  1142. * irdma_uk_calc_shift_wq - calculate WQE shift for both SQ and RQ
  1143. * @ukinfo: qp initialization info
  1144. * @sq_shift: Returns shift of SQ
  1145. * @rq_shift: Returns shift of RQ
  1146. */
  1147. void irdma_uk_calc_shift_wq(struct irdma_qp_uk_init_info *ukinfo, u8 *sq_shift,
  1148. u8 *rq_shift)
  1149. {
  1150. bool imm_support = ukinfo->uk_attrs->hw_rev >= IRDMA_GEN_2;
  1151. irdma_get_wqe_shift(ukinfo->uk_attrs,
  1152. imm_support ? ukinfo->max_sq_frag_cnt + 1 :
  1153. ukinfo->max_sq_frag_cnt,
  1154. ukinfo->max_inline_data, sq_shift);
  1155. irdma_get_wqe_shift(ukinfo->uk_attrs, ukinfo->max_rq_frag_cnt, 0,
  1156. rq_shift);
  1157. if (ukinfo->uk_attrs->hw_rev == IRDMA_GEN_1) {
  1158. if (ukinfo->abi_ver > 4)
  1159. *rq_shift = IRDMA_MAX_RQ_WQE_SHIFT_GEN1;
  1160. }
  1161. }
  1162. /**
  1163. * irdma_uk_calc_depth_shift_sq - calculate depth and shift for SQ size.
  1164. * @ukinfo: qp initialization info
  1165. * @sq_depth: Returns depth of SQ
  1166. * @sq_shift: Returns shift of SQ
  1167. */
  1168. int irdma_uk_calc_depth_shift_sq(struct irdma_qp_uk_init_info *ukinfo,
  1169. u32 *sq_depth, u8 *sq_shift)
  1170. {
  1171. bool imm_support = ukinfo->uk_attrs->hw_rev >= IRDMA_GEN_2;
  1172. int status;
  1173. irdma_get_wqe_shift(ukinfo->uk_attrs,
  1174. imm_support ? ukinfo->max_sq_frag_cnt + 1 :
  1175. ukinfo->max_sq_frag_cnt,
  1176. ukinfo->max_inline_data, sq_shift);
  1177. status = irdma_get_sqdepth(ukinfo->uk_attrs, ukinfo->sq_size,
  1178. *sq_shift, sq_depth);
  1179. return status;
  1180. }
  1181. /**
  1182. * irdma_uk_calc_depth_shift_rq - calculate depth and shift for RQ size.
  1183. * @ukinfo: qp initialization info
  1184. * @rq_depth: Returns depth of RQ
  1185. * @rq_shift: Returns shift of RQ
  1186. */
  1187. int irdma_uk_calc_depth_shift_rq(struct irdma_qp_uk_init_info *ukinfo,
  1188. u32 *rq_depth, u8 *rq_shift)
  1189. {
  1190. int status;
  1191. irdma_get_wqe_shift(ukinfo->uk_attrs, ukinfo->max_rq_frag_cnt, 0,
  1192. rq_shift);
  1193. if (ukinfo->uk_attrs->hw_rev == IRDMA_GEN_1) {
  1194. if (ukinfo->abi_ver > 4)
  1195. *rq_shift = IRDMA_MAX_RQ_WQE_SHIFT_GEN1;
  1196. }
  1197. status = irdma_get_rqdepth(ukinfo->uk_attrs, ukinfo->rq_size,
  1198. *rq_shift, rq_depth);
  1199. return status;
  1200. }
  1201. /**
  1202. * irdma_uk_qp_init - initialize shared qp
  1203. * @qp: hw qp (user and kernel)
  1204. * @info: qp initialization info
  1205. *
  1206. * initializes the vars used in both user and kernel mode.
  1207. * size of the wqe depends on numbers of max. fragements
  1208. * allowed. Then size of wqe * the number of wqes should be the
  1209. * amount of memory allocated for sq and rq.
  1210. */
  1211. int irdma_uk_qp_init(struct irdma_qp_uk *qp, struct irdma_qp_uk_init_info *info)
  1212. {
  1213. int ret_code = 0;
  1214. u32 sq_ring_size;
  1215. qp->uk_attrs = info->uk_attrs;
  1216. if (info->max_sq_frag_cnt > qp->uk_attrs->max_hw_wq_frags ||
  1217. info->max_rq_frag_cnt > qp->uk_attrs->max_hw_wq_frags)
  1218. return -EINVAL;
  1219. qp->qp_caps = info->qp_caps;
  1220. qp->sq_base = info->sq;
  1221. qp->rq_base = info->rq;
  1222. qp->qp_type = info->type ? info->type : IRDMA_QP_TYPE_IWARP;
  1223. qp->shadow_area = info->shadow_area;
  1224. qp->sq_wrtrk_array = info->sq_wrtrk_array;
  1225. qp->rq_wrid_array = info->rq_wrid_array;
  1226. qp->wqe_alloc_db = info->wqe_alloc_db;
  1227. qp->qp_id = info->qp_id;
  1228. qp->sq_size = info->sq_size;
  1229. qp->max_sq_frag_cnt = info->max_sq_frag_cnt;
  1230. sq_ring_size = qp->sq_size << info->sq_shift;
  1231. IRDMA_RING_INIT(qp->sq_ring, sq_ring_size);
  1232. IRDMA_RING_INIT(qp->initial_ring, sq_ring_size);
  1233. if (info->first_sq_wq) {
  1234. irdma_setup_connection_wqes(qp, info);
  1235. qp->swqe_polarity = 1;
  1236. qp->first_sq_wq = true;
  1237. } else {
  1238. qp->swqe_polarity = 0;
  1239. }
  1240. qp->swqe_polarity_deferred = 1;
  1241. qp->rwqe_polarity = 0;
  1242. qp->rq_size = info->rq_size;
  1243. qp->max_rq_frag_cnt = info->max_rq_frag_cnt;
  1244. qp->max_inline_data = info->max_inline_data;
  1245. qp->rq_wqe_size = info->rq_shift;
  1246. IRDMA_RING_INIT(qp->rq_ring, qp->rq_size);
  1247. qp->rq_wqe_size_multiplier = 1 << info->rq_shift;
  1248. if (qp->uk_attrs->hw_rev == IRDMA_GEN_1)
  1249. qp->wqe_ops = iw_wqe_uk_ops_gen_1;
  1250. else
  1251. qp->wqe_ops = iw_wqe_uk_ops;
  1252. return ret_code;
  1253. }
  1254. /**
  1255. * irdma_uk_cq_init - initialize shared cq (user and kernel)
  1256. * @cq: hw cq
  1257. * @info: hw cq initialization info
  1258. */
  1259. void irdma_uk_cq_init(struct irdma_cq_uk *cq,
  1260. struct irdma_cq_uk_init_info *info)
  1261. {
  1262. cq->cq_base = info->cq_base;
  1263. cq->cq_id = info->cq_id;
  1264. cq->cq_size = info->cq_size;
  1265. cq->cqe_alloc_db = info->cqe_alloc_db;
  1266. cq->cq_ack_db = info->cq_ack_db;
  1267. cq->shadow_area = info->shadow_area;
  1268. cq->avoid_mem_cflct = info->avoid_mem_cflct;
  1269. IRDMA_RING_INIT(cq->cq_ring, cq->cq_size);
  1270. cq->polarity = 1;
  1271. }
  1272. /**
  1273. * irdma_uk_clean_cq - clean cq entries
  1274. * @q: completion context
  1275. * @cq: cq to clean
  1276. */
  1277. void irdma_uk_clean_cq(void *q, struct irdma_cq_uk *cq)
  1278. {
  1279. __le64 *cqe;
  1280. u64 qword3, comp_ctx;
  1281. u32 cq_head;
  1282. u8 polarity, temp;
  1283. cq_head = cq->cq_ring.head;
  1284. temp = cq->polarity;
  1285. do {
  1286. if (cq->avoid_mem_cflct)
  1287. cqe = ((struct irdma_extended_cqe *)(cq->cq_base))[cq_head].buf;
  1288. else
  1289. cqe = cq->cq_base[cq_head].buf;
  1290. get_64bit_val(cqe, 24, &qword3);
  1291. polarity = (u8)FIELD_GET(IRDMA_CQ_VALID, qword3);
  1292. if (polarity != temp)
  1293. break;
  1294. /* Ensure CQE contents are read after valid bit is checked */
  1295. dma_rmb();
  1296. get_64bit_val(cqe, 8, &comp_ctx);
  1297. if ((void *)(unsigned long)comp_ctx == q)
  1298. set_64bit_val(cqe, 8, 0);
  1299. cq_head = (cq_head + 1) % cq->cq_ring.size;
  1300. if (!cq_head)
  1301. temp ^= 1;
  1302. } while (true);
  1303. }
  1304. /**
  1305. * irdma_nop - post a nop
  1306. * @qp: hw qp ptr
  1307. * @wr_id: work request id
  1308. * @signaled: signaled for completion
  1309. * @post_sq: ring doorbell
  1310. */
  1311. int irdma_nop(struct irdma_qp_uk *qp, u64 wr_id, bool signaled, bool post_sq)
  1312. {
  1313. __le64 *wqe;
  1314. u64 hdr;
  1315. u32 wqe_idx;
  1316. struct irdma_post_sq_info info = {};
  1317. info.wr_id = wr_id;
  1318. wqe = irdma_qp_get_next_send_wqe(qp, &wqe_idx, IRDMA_QP_WQE_MIN_QUANTA,
  1319. 0, &info);
  1320. if (!wqe)
  1321. return -ENOMEM;
  1322. irdma_clr_wqes(qp, wqe_idx);
  1323. set_64bit_val(wqe, 0, 0);
  1324. set_64bit_val(wqe, 8, 0);
  1325. set_64bit_val(wqe, 16, 0);
  1326. hdr = FIELD_PREP(IRDMAQPSQ_OPCODE, IRDMAQP_OP_NOP) |
  1327. FIELD_PREP(IRDMAQPSQ_SIGCOMPL, signaled) |
  1328. FIELD_PREP(IRDMAQPSQ_VALID, qp->swqe_polarity);
  1329. dma_wmb(); /* make sure WQE is populated before valid bit is set */
  1330. set_64bit_val(wqe, 24, hdr);
  1331. if (post_sq)
  1332. irdma_uk_qp_post_wr(qp);
  1333. return 0;
  1334. }
  1335. /**
  1336. * irdma_fragcnt_to_quanta_sq - calculate quanta based on fragment count for SQ
  1337. * @frag_cnt: number of fragments
  1338. * @quanta: quanta for frag_cnt
  1339. */
  1340. int irdma_fragcnt_to_quanta_sq(u32 frag_cnt, u16 *quanta)
  1341. {
  1342. switch (frag_cnt) {
  1343. case 0:
  1344. case 1:
  1345. *quanta = IRDMA_QP_WQE_MIN_QUANTA;
  1346. break;
  1347. case 2:
  1348. case 3:
  1349. *quanta = 2;
  1350. break;
  1351. case 4:
  1352. case 5:
  1353. *quanta = 3;
  1354. break;
  1355. case 6:
  1356. case 7:
  1357. *quanta = 4;
  1358. break;
  1359. case 8:
  1360. case 9:
  1361. *quanta = 5;
  1362. break;
  1363. case 10:
  1364. case 11:
  1365. *quanta = 6;
  1366. break;
  1367. case 12:
  1368. case 13:
  1369. *quanta = 7;
  1370. break;
  1371. case 14:
  1372. case 15: /* when immediate data is present */
  1373. *quanta = 8;
  1374. break;
  1375. default:
  1376. return -EINVAL;
  1377. }
  1378. return 0;
  1379. }
  1380. /**
  1381. * irdma_fragcnt_to_wqesize_rq - calculate wqe size based on fragment count for RQ
  1382. * @frag_cnt: number of fragments
  1383. * @wqe_size: size in bytes given frag_cnt
  1384. */
  1385. int irdma_fragcnt_to_wqesize_rq(u32 frag_cnt, u16 *wqe_size)
  1386. {
  1387. switch (frag_cnt) {
  1388. case 0:
  1389. case 1:
  1390. *wqe_size = 32;
  1391. break;
  1392. case 2:
  1393. case 3:
  1394. *wqe_size = 64;
  1395. break;
  1396. case 4:
  1397. case 5:
  1398. case 6:
  1399. case 7:
  1400. *wqe_size = 128;
  1401. break;
  1402. case 8:
  1403. case 9:
  1404. case 10:
  1405. case 11:
  1406. case 12:
  1407. case 13:
  1408. case 14:
  1409. *wqe_size = 256;
  1410. break;
  1411. default:
  1412. return -EINVAL;
  1413. }
  1414. return 0;
  1415. }