i40iw_puda.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493
  1. /*******************************************************************************
  2. *
  3. * Copyright (c) 2015-2016 Intel Corporation. All rights reserved.
  4. *
  5. * This software is available to you under a choice of one of two
  6. * licenses. You may choose to be licensed under the terms of the GNU
  7. * General Public License (GPL) Version 2, available from the file
  8. * COPYING in the main directory of this source tree, or the
  9. * OpenFabrics.org BSD license below:
  10. *
  11. * Redistribution and use in source and binary forms, with or
  12. * without modification, are permitted provided that the following
  13. * conditions are met:
  14. *
  15. * - Redistributions of source code must retain the above
  16. * copyright notice, this list of conditions and the following
  17. * disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials
  22. * provided with the distribution.
  23. *
  24. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  25. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  26. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  27. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  28. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  29. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  30. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  31. * SOFTWARE.
  32. *
  33. *******************************************************************************/
  34. #include "i40iw_osdep.h"
  35. #include "i40iw_register.h"
  36. #include "i40iw_status.h"
  37. #include "i40iw_hmc.h"
  38. #include "i40iw_d.h"
  39. #include "i40iw_type.h"
  40. #include "i40iw_p.h"
  41. #include "i40iw_puda.h"
  42. static void i40iw_ieq_receive(struct i40iw_sc_vsi *vsi,
  43. struct i40iw_puda_buf *buf);
  44. static void i40iw_ieq_tx_compl(struct i40iw_sc_vsi *vsi, void *sqwrid);
  45. static void i40iw_ilq_putback_rcvbuf(struct i40iw_sc_qp *qp, u32 wqe_idx);
  46. static enum i40iw_status_code i40iw_puda_replenish_rq(struct i40iw_puda_rsrc
  47. *rsrc, bool initial);
  48. /**
  49. * i40iw_puda_get_listbuf - get buffer from puda list
  50. * @list: list to use for buffers (ILQ or IEQ)
  51. */
  52. static struct i40iw_puda_buf *i40iw_puda_get_listbuf(struct list_head *list)
  53. {
  54. struct i40iw_puda_buf *buf = NULL;
  55. if (!list_empty(list)) {
  56. buf = (struct i40iw_puda_buf *)list->next;
  57. list_del((struct list_head *)&buf->list);
  58. }
  59. return buf;
  60. }
  61. /**
  62. * i40iw_puda_get_bufpool - return buffer from resource
  63. * @rsrc: resource to use for buffer
  64. */
  65. struct i40iw_puda_buf *i40iw_puda_get_bufpool(struct i40iw_puda_rsrc *rsrc)
  66. {
  67. struct i40iw_puda_buf *buf = NULL;
  68. struct list_head *list = &rsrc->bufpool;
  69. unsigned long flags;
  70. spin_lock_irqsave(&rsrc->bufpool_lock, flags);
  71. buf = i40iw_puda_get_listbuf(list);
  72. if (buf)
  73. rsrc->avail_buf_count--;
  74. else
  75. rsrc->stats_buf_alloc_fail++;
  76. spin_unlock_irqrestore(&rsrc->bufpool_lock, flags);
  77. return buf;
  78. }
  79. /**
  80. * i40iw_puda_ret_bufpool - return buffer to rsrc list
  81. * @rsrc: resource to use for buffer
  82. * @buf: buffe to return to resouce
  83. */
  84. void i40iw_puda_ret_bufpool(struct i40iw_puda_rsrc *rsrc,
  85. struct i40iw_puda_buf *buf)
  86. {
  87. unsigned long flags;
  88. spin_lock_irqsave(&rsrc->bufpool_lock, flags);
  89. list_add(&buf->list, &rsrc->bufpool);
  90. spin_unlock_irqrestore(&rsrc->bufpool_lock, flags);
  91. rsrc->avail_buf_count++;
  92. }
  93. /**
  94. * i40iw_puda_post_recvbuf - set wqe for rcv buffer
  95. * @rsrc: resource ptr
  96. * @wqe_idx: wqe index to use
  97. * @buf: puda buffer for rcv q
  98. * @initial: flag if during init time
  99. */
  100. static void i40iw_puda_post_recvbuf(struct i40iw_puda_rsrc *rsrc, u32 wqe_idx,
  101. struct i40iw_puda_buf *buf, bool initial)
  102. {
  103. u64 *wqe;
  104. struct i40iw_sc_qp *qp = &rsrc->qp;
  105. u64 offset24 = 0;
  106. qp->qp_uk.rq_wrid_array[wqe_idx] = (uintptr_t)buf;
  107. wqe = qp->qp_uk.rq_base[wqe_idx].elem;
  108. i40iw_debug(rsrc->dev, I40IW_DEBUG_PUDA,
  109. "%s: wqe_idx= %d buf = %p wqe = %p\n", __func__,
  110. wqe_idx, buf, wqe);
  111. if (!initial)
  112. get_64bit_val(wqe, 24, &offset24);
  113. offset24 = (offset24) ? 0 : LS_64(1, I40IWQPSQ_VALID);
  114. set_64bit_val(wqe, 0, buf->mem.pa);
  115. set_64bit_val(wqe, 8,
  116. LS_64(buf->mem.size, I40IWQPSQ_FRAG_LEN));
  117. i40iw_insert_wqe_hdr(wqe, offset24);
  118. }
  119. /**
  120. * i40iw_puda_replenish_rq - post rcv buffers
  121. * @rsrc: resource to use for buffer
  122. * @initial: flag if during init time
  123. */
  124. static enum i40iw_status_code i40iw_puda_replenish_rq(struct i40iw_puda_rsrc *rsrc,
  125. bool initial)
  126. {
  127. u32 i;
  128. u32 invalid_cnt = rsrc->rxq_invalid_cnt;
  129. struct i40iw_puda_buf *buf = NULL;
  130. for (i = 0; i < invalid_cnt; i++) {
  131. buf = i40iw_puda_get_bufpool(rsrc);
  132. if (!buf)
  133. return I40IW_ERR_list_empty;
  134. i40iw_puda_post_recvbuf(rsrc, rsrc->rx_wqe_idx, buf,
  135. initial);
  136. rsrc->rx_wqe_idx =
  137. ((rsrc->rx_wqe_idx + 1) % rsrc->rq_size);
  138. rsrc->rxq_invalid_cnt--;
  139. }
  140. return 0;
  141. }
  142. /**
  143. * i40iw_puda_alloc_buf - allocate mem for buffer
  144. * @dev: iwarp device
  145. * @length: length of buffer
  146. */
  147. static struct i40iw_puda_buf *i40iw_puda_alloc_buf(struct i40iw_sc_dev *dev,
  148. u32 length)
  149. {
  150. struct i40iw_puda_buf *buf = NULL;
  151. struct i40iw_virt_mem buf_mem;
  152. enum i40iw_status_code ret;
  153. ret = i40iw_allocate_virt_mem(dev->hw, &buf_mem,
  154. sizeof(struct i40iw_puda_buf));
  155. if (ret) {
  156. i40iw_debug(dev, I40IW_DEBUG_PUDA,
  157. "%s: error mem for buf\n", __func__);
  158. return NULL;
  159. }
  160. buf = (struct i40iw_puda_buf *)buf_mem.va;
  161. ret = i40iw_allocate_dma_mem(dev->hw, &buf->mem, length, 1);
  162. if (ret) {
  163. i40iw_debug(dev, I40IW_DEBUG_PUDA,
  164. "%s: error dma mem for buf\n", __func__);
  165. i40iw_free_virt_mem(dev->hw, &buf_mem);
  166. return NULL;
  167. }
  168. buf->buf_mem.va = buf_mem.va;
  169. buf->buf_mem.size = buf_mem.size;
  170. return buf;
  171. }
  172. /**
  173. * i40iw_puda_dele_buf - delete buffer back to system
  174. * @dev: iwarp device
  175. * @buf: buffer to free
  176. */
  177. static void i40iw_puda_dele_buf(struct i40iw_sc_dev *dev,
  178. struct i40iw_puda_buf *buf)
  179. {
  180. i40iw_free_dma_mem(dev->hw, &buf->mem);
  181. i40iw_free_virt_mem(dev->hw, &buf->buf_mem);
  182. }
  183. /**
  184. * i40iw_puda_get_next_send_wqe - return next wqe for processing
  185. * @qp: puda qp for wqe
  186. * @wqe_idx: wqe index for caller
  187. */
  188. static u64 *i40iw_puda_get_next_send_wqe(struct i40iw_qp_uk *qp, u32 *wqe_idx)
  189. {
  190. u64 *wqe = NULL;
  191. enum i40iw_status_code ret_code = 0;
  192. *wqe_idx = I40IW_RING_GETCURRENT_HEAD(qp->sq_ring);
  193. if (!*wqe_idx)
  194. qp->swqe_polarity = !qp->swqe_polarity;
  195. I40IW_RING_MOVE_HEAD(qp->sq_ring, ret_code);
  196. if (ret_code)
  197. return wqe;
  198. wqe = qp->sq_base[*wqe_idx].elem;
  199. return wqe;
  200. }
  201. /**
  202. * i40iw_puda_poll_info - poll cq for completion
  203. * @cq: cq for poll
  204. * @info: info return for successful completion
  205. */
  206. static enum i40iw_status_code i40iw_puda_poll_info(struct i40iw_sc_cq *cq,
  207. struct i40iw_puda_completion_info *info)
  208. {
  209. u64 qword0, qword2, qword3;
  210. u64 *cqe;
  211. u64 comp_ctx;
  212. bool valid_bit;
  213. u32 major_err, minor_err;
  214. bool error;
  215. cqe = (u64 *)I40IW_GET_CURRENT_CQ_ELEMENT(&cq->cq_uk);
  216. get_64bit_val(cqe, 24, &qword3);
  217. valid_bit = (bool)RS_64(qword3, I40IW_CQ_VALID);
  218. if (valid_bit != cq->cq_uk.polarity)
  219. return I40IW_ERR_QUEUE_EMPTY;
  220. i40iw_debug_buf(cq->dev, I40IW_DEBUG_PUDA, "PUDA CQE", cqe, 32);
  221. error = (bool)RS_64(qword3, I40IW_CQ_ERROR);
  222. if (error) {
  223. i40iw_debug(cq->dev, I40IW_DEBUG_PUDA, "%s receive error\n", __func__);
  224. major_err = (u32)(RS_64(qword3, I40IW_CQ_MAJERR));
  225. minor_err = (u32)(RS_64(qword3, I40IW_CQ_MINERR));
  226. info->compl_error = major_err << 16 | minor_err;
  227. return I40IW_ERR_CQ_COMPL_ERROR;
  228. }
  229. get_64bit_val(cqe, 0, &qword0);
  230. get_64bit_val(cqe, 16, &qword2);
  231. info->q_type = (u8)RS_64(qword3, I40IW_CQ_SQ);
  232. info->qp_id = (u32)RS_64(qword2, I40IWCQ_QPID);
  233. get_64bit_val(cqe, 8, &comp_ctx);
  234. info->qp = (struct i40iw_qp_uk *)(unsigned long)comp_ctx;
  235. info->wqe_idx = (u32)RS_64(qword3, I40IW_CQ_WQEIDX);
  236. if (info->q_type == I40IW_CQE_QTYPE_RQ) {
  237. info->vlan_valid = (bool)RS_64(qword3, I40IW_VLAN_TAG_VALID);
  238. info->l4proto = (u8)RS_64(qword2, I40IW_UDA_L4PROTO);
  239. info->l3proto = (u8)RS_64(qword2, I40IW_UDA_L3PROTO);
  240. info->payload_len = (u16)RS_64(qword0, I40IW_UDA_PAYLOADLEN);
  241. }
  242. return 0;
  243. }
  244. /**
  245. * i40iw_puda_poll_completion - processes completion for cq
  246. * @dev: iwarp device
  247. * @cq: cq getting interrupt
  248. * @compl_err: return any completion err
  249. */
  250. enum i40iw_status_code i40iw_puda_poll_completion(struct i40iw_sc_dev *dev,
  251. struct i40iw_sc_cq *cq, u32 *compl_err)
  252. {
  253. struct i40iw_qp_uk *qp;
  254. struct i40iw_cq_uk *cq_uk = &cq->cq_uk;
  255. struct i40iw_puda_completion_info info;
  256. enum i40iw_status_code ret = 0;
  257. struct i40iw_puda_buf *buf;
  258. struct i40iw_puda_rsrc *rsrc;
  259. void *sqwrid;
  260. u8 cq_type = cq->cq_type;
  261. unsigned long flags;
  262. if ((cq_type == I40IW_CQ_TYPE_ILQ) || (cq_type == I40IW_CQ_TYPE_IEQ)) {
  263. rsrc = (cq_type == I40IW_CQ_TYPE_ILQ) ? cq->vsi->ilq : cq->vsi->ieq;
  264. } else {
  265. i40iw_debug(dev, I40IW_DEBUG_PUDA, "%s qp_type error\n", __func__);
  266. return I40IW_ERR_BAD_PTR;
  267. }
  268. memset(&info, 0, sizeof(info));
  269. ret = i40iw_puda_poll_info(cq, &info);
  270. *compl_err = info.compl_error;
  271. if (ret == I40IW_ERR_QUEUE_EMPTY)
  272. return ret;
  273. if (ret)
  274. goto done;
  275. qp = info.qp;
  276. if (!qp || !rsrc) {
  277. ret = I40IW_ERR_BAD_PTR;
  278. goto done;
  279. }
  280. if (qp->qp_id != rsrc->qp_id) {
  281. ret = I40IW_ERR_BAD_PTR;
  282. goto done;
  283. }
  284. if (info.q_type == I40IW_CQE_QTYPE_RQ) {
  285. buf = (struct i40iw_puda_buf *)(uintptr_t)qp->rq_wrid_array[info.wqe_idx];
  286. /* Get all the tcpip information in the buf header */
  287. ret = i40iw_puda_get_tcpip_info(&info, buf);
  288. if (ret) {
  289. rsrc->stats_rcvd_pkt_err++;
  290. if (cq_type == I40IW_CQ_TYPE_ILQ) {
  291. i40iw_ilq_putback_rcvbuf(&rsrc->qp,
  292. info.wqe_idx);
  293. } else {
  294. i40iw_puda_ret_bufpool(rsrc, buf);
  295. i40iw_puda_replenish_rq(rsrc, false);
  296. }
  297. goto done;
  298. }
  299. rsrc->stats_pkt_rcvd++;
  300. rsrc->compl_rxwqe_idx = info.wqe_idx;
  301. i40iw_debug(dev, I40IW_DEBUG_PUDA, "%s RQ completion\n", __func__);
  302. rsrc->receive(rsrc->vsi, buf);
  303. if (cq_type == I40IW_CQ_TYPE_ILQ)
  304. i40iw_ilq_putback_rcvbuf(&rsrc->qp, info.wqe_idx);
  305. else
  306. i40iw_puda_replenish_rq(rsrc, false);
  307. } else {
  308. i40iw_debug(dev, I40IW_DEBUG_PUDA, "%s SQ completion\n", __func__);
  309. sqwrid = (void *)(uintptr_t)qp->sq_wrtrk_array[info.wqe_idx].wrid;
  310. I40IW_RING_SET_TAIL(qp->sq_ring, info.wqe_idx);
  311. rsrc->xmit_complete(rsrc->vsi, sqwrid);
  312. spin_lock_irqsave(&rsrc->bufpool_lock, flags);
  313. rsrc->tx_wqe_avail_cnt++;
  314. spin_unlock_irqrestore(&rsrc->bufpool_lock, flags);
  315. if (!list_empty(&rsrc->txpend))
  316. i40iw_puda_send_buf(rsrc, NULL);
  317. }
  318. done:
  319. I40IW_RING_MOVE_HEAD(cq_uk->cq_ring, ret);
  320. if (I40IW_RING_GETCURRENT_HEAD(cq_uk->cq_ring) == 0)
  321. cq_uk->polarity = !cq_uk->polarity;
  322. /* update cq tail in cq shadow memory also */
  323. I40IW_RING_MOVE_TAIL(cq_uk->cq_ring);
  324. set_64bit_val(cq_uk->shadow_area, 0,
  325. I40IW_RING_GETCURRENT_HEAD(cq_uk->cq_ring));
  326. return 0;
  327. }
  328. /**
  329. * i40iw_puda_send - complete send wqe for transmit
  330. * @qp: puda qp for send
  331. * @info: buffer information for transmit
  332. */
  333. enum i40iw_status_code i40iw_puda_send(struct i40iw_sc_qp *qp,
  334. struct i40iw_puda_send_info *info)
  335. {
  336. u64 *wqe;
  337. u32 iplen, l4len;
  338. u64 header[2];
  339. u32 wqe_idx;
  340. u8 iipt;
  341. /* number of 32 bits DWORDS in header */
  342. l4len = info->tcplen >> 2;
  343. if (info->ipv4) {
  344. iipt = 3;
  345. iplen = 5;
  346. } else {
  347. iipt = 1;
  348. iplen = 10;
  349. }
  350. wqe = i40iw_puda_get_next_send_wqe(&qp->qp_uk, &wqe_idx);
  351. if (!wqe)
  352. return I40IW_ERR_QP_TOOMANY_WRS_POSTED;
  353. qp->qp_uk.sq_wrtrk_array[wqe_idx].wrid = (uintptr_t)info->scratch;
  354. /* Third line of WQE descriptor */
  355. /* maclen is in words */
  356. header[0] = LS_64((info->maclen >> 1), I40IW_UDA_QPSQ_MACLEN) |
  357. LS_64(iplen, I40IW_UDA_QPSQ_IPLEN) | LS_64(1, I40IW_UDA_QPSQ_L4T) |
  358. LS_64(iipt, I40IW_UDA_QPSQ_IIPT) |
  359. LS_64(l4len, I40IW_UDA_QPSQ_L4LEN);
  360. /* Forth line of WQE descriptor */
  361. header[1] = LS_64(I40IW_OP_TYPE_SEND, I40IW_UDA_QPSQ_OPCODE) |
  362. LS_64(1, I40IW_UDA_QPSQ_SIGCOMPL) |
  363. LS_64(info->doloopback, I40IW_UDA_QPSQ_DOLOOPBACK) |
  364. LS_64(qp->qp_uk.swqe_polarity, I40IW_UDA_QPSQ_VALID);
  365. set_64bit_val(wqe, 0, info->paddr);
  366. set_64bit_val(wqe, 8, LS_64(info->len, I40IWQPSQ_FRAG_LEN));
  367. set_64bit_val(wqe, 16, header[0]);
  368. i40iw_insert_wqe_hdr(wqe, header[1]);
  369. i40iw_debug_buf(qp->dev, I40IW_DEBUG_PUDA, "PUDA SEND WQE", wqe, 32);
  370. i40iw_qp_post_wr(&qp->qp_uk);
  371. return 0;
  372. }
  373. /**
  374. * i40iw_puda_send_buf - transmit puda buffer
  375. * @rsrc: resource to use for buffer
  376. * @buf: puda buffer to transmit
  377. */
  378. void i40iw_puda_send_buf(struct i40iw_puda_rsrc *rsrc, struct i40iw_puda_buf *buf)
  379. {
  380. struct i40iw_puda_send_info info;
  381. enum i40iw_status_code ret = 0;
  382. unsigned long flags;
  383. spin_lock_irqsave(&rsrc->bufpool_lock, flags);
  384. /* if no wqe available or not from a completion and we have
  385. * pending buffers, we must queue new buffer
  386. */
  387. if (!rsrc->tx_wqe_avail_cnt || (buf && !list_empty(&rsrc->txpend))) {
  388. list_add_tail(&buf->list, &rsrc->txpend);
  389. spin_unlock_irqrestore(&rsrc->bufpool_lock, flags);
  390. rsrc->stats_sent_pkt_q++;
  391. if (rsrc->type == I40IW_PUDA_RSRC_TYPE_ILQ)
  392. i40iw_debug(rsrc->dev, I40IW_DEBUG_PUDA,
  393. "%s: adding to txpend\n", __func__);
  394. return;
  395. }
  396. rsrc->tx_wqe_avail_cnt--;
  397. /* if we are coming from a completion and have pending buffers
  398. * then Get one from pending list
  399. */
  400. if (!buf) {
  401. buf = i40iw_puda_get_listbuf(&rsrc->txpend);
  402. if (!buf)
  403. goto done;
  404. }
  405. info.scratch = (void *)buf;
  406. info.paddr = buf->mem.pa;
  407. info.len = buf->totallen;
  408. info.tcplen = buf->tcphlen;
  409. info.maclen = buf->maclen;
  410. info.ipv4 = buf->ipv4;
  411. info.doloopback = (rsrc->type == I40IW_PUDA_RSRC_TYPE_IEQ);
  412. ret = i40iw_puda_send(&rsrc->qp, &info);
  413. if (ret) {
  414. rsrc->tx_wqe_avail_cnt++;
  415. rsrc->stats_sent_pkt_q++;
  416. list_add(&buf->list, &rsrc->txpend);
  417. if (rsrc->type == I40IW_PUDA_RSRC_TYPE_ILQ)
  418. i40iw_debug(rsrc->dev, I40IW_DEBUG_PUDA,
  419. "%s: adding to puda_send\n", __func__);
  420. } else {
  421. rsrc->stats_pkt_sent++;
  422. }
  423. done:
  424. spin_unlock_irqrestore(&rsrc->bufpool_lock, flags);
  425. }
  426. /**
  427. * i40iw_puda_qp_setctx - during init, set qp's context
  428. * @rsrc: qp's resource
  429. */
  430. static void i40iw_puda_qp_setctx(struct i40iw_puda_rsrc *rsrc)
  431. {
  432. struct i40iw_sc_qp *qp = &rsrc->qp;
  433. u64 *qp_ctx = qp->hw_host_ctx;
  434. set_64bit_val(qp_ctx, 8, qp->sq_pa);
  435. set_64bit_val(qp_ctx, 16, qp->rq_pa);
  436. set_64bit_val(qp_ctx, 24,
  437. LS_64(qp->hw_rq_size, I40IWQPC_RQSIZE) |
  438. LS_64(qp->hw_sq_size, I40IWQPC_SQSIZE));
  439. set_64bit_val(qp_ctx, 48, LS_64(rsrc->buf_size, I40IW_UDA_QPC_MAXFRAMESIZE));
  440. set_64bit_val(qp_ctx, 56, 0);
  441. set_64bit_val(qp_ctx, 64, 1);
  442. set_64bit_val(qp_ctx, 136,
  443. LS_64(rsrc->cq_id, I40IWQPC_TXCQNUM) |
  444. LS_64(rsrc->cq_id, I40IWQPC_RXCQNUM));
  445. set_64bit_val(qp_ctx, 160, LS_64(1, I40IWQPC_PRIVEN));
  446. set_64bit_val(qp_ctx, 168,
  447. LS_64((uintptr_t)qp, I40IWQPC_QPCOMPCTX));
  448. set_64bit_val(qp_ctx, 176,
  449. LS_64(qp->sq_tph_val, I40IWQPC_SQTPHVAL) |
  450. LS_64(qp->rq_tph_val, I40IWQPC_RQTPHVAL) |
  451. LS_64(qp->qs_handle, I40IWQPC_QSHANDLE));
  452. i40iw_debug_buf(rsrc->dev, I40IW_DEBUG_PUDA, "PUDA QP CONTEXT",
  453. qp_ctx, I40IW_QP_CTX_SIZE);
  454. }
  455. /**
  456. * i40iw_puda_qp_wqe - setup wqe for qp create
  457. * @rsrc: resource for qp
  458. */
  459. static enum i40iw_status_code i40iw_puda_qp_wqe(struct i40iw_sc_dev *dev, struct i40iw_sc_qp *qp)
  460. {
  461. struct i40iw_sc_cqp *cqp;
  462. u64 *wqe;
  463. u64 header;
  464. struct i40iw_ccq_cqe_info compl_info;
  465. enum i40iw_status_code status = 0;
  466. cqp = dev->cqp;
  467. wqe = i40iw_sc_cqp_get_next_send_wqe(cqp, 0);
  468. if (!wqe)
  469. return I40IW_ERR_RING_FULL;
  470. set_64bit_val(wqe, 16, qp->hw_host_ctx_pa);
  471. set_64bit_val(wqe, 40, qp->shadow_area_pa);
  472. header = qp->qp_uk.qp_id |
  473. LS_64(I40IW_CQP_OP_CREATE_QP, I40IW_CQPSQ_OPCODE) |
  474. LS_64(I40IW_QP_TYPE_UDA, I40IW_CQPSQ_QP_QPTYPE) |
  475. LS_64(1, I40IW_CQPSQ_QP_CQNUMVALID) |
  476. LS_64(2, I40IW_CQPSQ_QP_NEXTIWSTATE) |
  477. LS_64(cqp->polarity, I40IW_CQPSQ_WQEVALID);
  478. i40iw_insert_wqe_hdr(wqe, header);
  479. i40iw_debug_buf(cqp->dev, I40IW_DEBUG_PUDA, "PUDA CQE", wqe, 32);
  480. i40iw_sc_cqp_post_sq(cqp);
  481. status = dev->cqp_ops->poll_for_cqp_op_done(dev->cqp,
  482. I40IW_CQP_OP_CREATE_QP,
  483. &compl_info);
  484. return status;
  485. }
  486. /**
  487. * i40iw_puda_qp_create - create qp for resource
  488. * @rsrc: resource to use for buffer
  489. */
  490. static enum i40iw_status_code i40iw_puda_qp_create(struct i40iw_puda_rsrc *rsrc)
  491. {
  492. struct i40iw_sc_qp *qp = &rsrc->qp;
  493. struct i40iw_qp_uk *ukqp = &qp->qp_uk;
  494. enum i40iw_status_code ret = 0;
  495. u32 sq_size, rq_size, t_size;
  496. struct i40iw_dma_mem *mem;
  497. sq_size = rsrc->sq_size * I40IW_QP_WQE_MIN_SIZE;
  498. rq_size = rsrc->rq_size * I40IW_QP_WQE_MIN_SIZE;
  499. t_size = (sq_size + rq_size + (I40IW_SHADOW_AREA_SIZE << 3) +
  500. I40IW_QP_CTX_SIZE);
  501. /* Get page aligned memory */
  502. ret =
  503. i40iw_allocate_dma_mem(rsrc->dev->hw, &rsrc->qpmem, t_size,
  504. I40IW_HW_PAGE_SIZE);
  505. if (ret) {
  506. i40iw_debug(rsrc->dev, I40IW_DEBUG_PUDA, "%s: error dma mem\n", __func__);
  507. return ret;
  508. }
  509. mem = &rsrc->qpmem;
  510. memset(mem->va, 0, t_size);
  511. qp->hw_sq_size = i40iw_get_encoded_wqe_size(rsrc->sq_size, false);
  512. qp->hw_rq_size = i40iw_get_encoded_wqe_size(rsrc->rq_size, false);
  513. qp->pd = &rsrc->sc_pd;
  514. qp->qp_type = I40IW_QP_TYPE_UDA;
  515. qp->dev = rsrc->dev;
  516. qp->back_qp = (void *)rsrc;
  517. qp->sq_pa = mem->pa;
  518. qp->rq_pa = qp->sq_pa + sq_size;
  519. qp->vsi = rsrc->vsi;
  520. ukqp->sq_base = mem->va;
  521. ukqp->rq_base = &ukqp->sq_base[rsrc->sq_size];
  522. ukqp->shadow_area = ukqp->rq_base[rsrc->rq_size].elem;
  523. qp->shadow_area_pa = qp->rq_pa + rq_size;
  524. qp->hw_host_ctx = ukqp->shadow_area + I40IW_SHADOW_AREA_SIZE;
  525. qp->hw_host_ctx_pa =
  526. qp->shadow_area_pa + (I40IW_SHADOW_AREA_SIZE << 3);
  527. ukqp->qp_id = rsrc->qp_id;
  528. ukqp->sq_wrtrk_array = rsrc->sq_wrtrk_array;
  529. ukqp->rq_wrid_array = rsrc->rq_wrid_array;
  530. ukqp->qp_id = rsrc->qp_id;
  531. ukqp->sq_size = rsrc->sq_size;
  532. ukqp->rq_size = rsrc->rq_size;
  533. I40IW_RING_INIT(ukqp->sq_ring, ukqp->sq_size);
  534. I40IW_RING_INIT(ukqp->initial_ring, ukqp->sq_size);
  535. I40IW_RING_INIT(ukqp->rq_ring, ukqp->rq_size);
  536. if (qp->pd->dev->is_pf)
  537. ukqp->wqe_alloc_reg = (u32 __iomem *)(i40iw_get_hw_addr(qp->pd->dev) +
  538. I40E_PFPE_WQEALLOC);
  539. else
  540. ukqp->wqe_alloc_reg = (u32 __iomem *)(i40iw_get_hw_addr(qp->pd->dev) +
  541. I40E_VFPE_WQEALLOC1);
  542. qp->user_pri = 0;
  543. i40iw_qp_add_qos(qp);
  544. i40iw_puda_qp_setctx(rsrc);
  545. if (rsrc->dev->ceq_valid)
  546. ret = i40iw_cqp_qp_create_cmd(rsrc->dev, qp);
  547. else
  548. ret = i40iw_puda_qp_wqe(rsrc->dev, qp);
  549. if (ret) {
  550. i40iw_qp_rem_qos(qp);
  551. i40iw_free_dma_mem(rsrc->dev->hw, &rsrc->qpmem);
  552. }
  553. return ret;
  554. }
  555. /**
  556. * i40iw_puda_cq_wqe - setup wqe for cq create
  557. * @rsrc: resource for cq
  558. */
  559. static enum i40iw_status_code i40iw_puda_cq_wqe(struct i40iw_sc_dev *dev, struct i40iw_sc_cq *cq)
  560. {
  561. u64 *wqe;
  562. struct i40iw_sc_cqp *cqp;
  563. u64 header;
  564. struct i40iw_ccq_cqe_info compl_info;
  565. enum i40iw_status_code status = 0;
  566. cqp = dev->cqp;
  567. wqe = i40iw_sc_cqp_get_next_send_wqe(cqp, 0);
  568. if (!wqe)
  569. return I40IW_ERR_RING_FULL;
  570. set_64bit_val(wqe, 0, cq->cq_uk.cq_size);
  571. set_64bit_val(wqe, 8, RS_64_1(cq, 1));
  572. set_64bit_val(wqe, 16,
  573. LS_64(cq->shadow_read_threshold,
  574. I40IW_CQPSQ_CQ_SHADOW_READ_THRESHOLD));
  575. set_64bit_val(wqe, 32, cq->cq_pa);
  576. set_64bit_val(wqe, 40, cq->shadow_area_pa);
  577. header = cq->cq_uk.cq_id |
  578. LS_64(I40IW_CQP_OP_CREATE_CQ, I40IW_CQPSQ_OPCODE) |
  579. LS_64(1, I40IW_CQPSQ_CQ_CHKOVERFLOW) |
  580. LS_64(1, I40IW_CQPSQ_CQ_ENCEQEMASK) |
  581. LS_64(1, I40IW_CQPSQ_CQ_CEQIDVALID) |
  582. LS_64(cqp->polarity, I40IW_CQPSQ_WQEVALID);
  583. i40iw_insert_wqe_hdr(wqe, header);
  584. i40iw_debug_buf(dev, I40IW_DEBUG_PUDA, "PUDA CQE",
  585. wqe, I40IW_CQP_WQE_SIZE * 8);
  586. i40iw_sc_cqp_post_sq(dev->cqp);
  587. status = dev->cqp_ops->poll_for_cqp_op_done(dev->cqp,
  588. I40IW_CQP_OP_CREATE_CQ,
  589. &compl_info);
  590. return status;
  591. }
  592. /**
  593. * i40iw_puda_cq_create - create cq for resource
  594. * @rsrc: resource for which cq to create
  595. */
  596. static enum i40iw_status_code i40iw_puda_cq_create(struct i40iw_puda_rsrc *rsrc)
  597. {
  598. struct i40iw_sc_dev *dev = rsrc->dev;
  599. struct i40iw_sc_cq *cq = &rsrc->cq;
  600. enum i40iw_status_code ret = 0;
  601. u32 tsize, cqsize;
  602. struct i40iw_dma_mem *mem;
  603. struct i40iw_cq_init_info info;
  604. struct i40iw_cq_uk_init_info *init_info = &info.cq_uk_init_info;
  605. cq->vsi = rsrc->vsi;
  606. cqsize = rsrc->cq_size * (sizeof(struct i40iw_cqe));
  607. tsize = cqsize + sizeof(struct i40iw_cq_shadow_area);
  608. ret = i40iw_allocate_dma_mem(dev->hw, &rsrc->cqmem, tsize,
  609. I40IW_CQ0_ALIGNMENT);
  610. if (ret)
  611. return ret;
  612. mem = &rsrc->cqmem;
  613. memset(&info, 0, sizeof(info));
  614. info.dev = dev;
  615. info.type = (rsrc->type == I40IW_PUDA_RSRC_TYPE_ILQ) ?
  616. I40IW_CQ_TYPE_ILQ : I40IW_CQ_TYPE_IEQ;
  617. info.shadow_read_threshold = rsrc->cq_size >> 2;
  618. info.ceq_id_valid = true;
  619. info.cq_base_pa = mem->pa;
  620. info.shadow_area_pa = mem->pa + cqsize;
  621. init_info->cq_base = mem->va;
  622. init_info->shadow_area = (u64 *)((u8 *)mem->va + cqsize);
  623. init_info->cq_size = rsrc->cq_size;
  624. init_info->cq_id = rsrc->cq_id;
  625. info.ceqe_mask = true;
  626. info.ceq_id_valid = true;
  627. ret = dev->iw_priv_cq_ops->cq_init(cq, &info);
  628. if (ret)
  629. goto error;
  630. if (rsrc->dev->ceq_valid)
  631. ret = i40iw_cqp_cq_create_cmd(dev, cq);
  632. else
  633. ret = i40iw_puda_cq_wqe(dev, cq);
  634. error:
  635. if (ret)
  636. i40iw_free_dma_mem(dev->hw, &rsrc->cqmem);
  637. return ret;
  638. }
  639. /**
  640. * i40iw_puda_free_qp - free qp for resource
  641. * @rsrc: resource for which qp to free
  642. */
  643. static void i40iw_puda_free_qp(struct i40iw_puda_rsrc *rsrc)
  644. {
  645. enum i40iw_status_code ret;
  646. struct i40iw_ccq_cqe_info compl_info;
  647. struct i40iw_sc_dev *dev = rsrc->dev;
  648. if (rsrc->dev->ceq_valid) {
  649. i40iw_cqp_qp_destroy_cmd(dev, &rsrc->qp);
  650. return;
  651. }
  652. ret = dev->iw_priv_qp_ops->qp_destroy(&rsrc->qp,
  653. 0, false, true, true);
  654. if (ret)
  655. i40iw_debug(dev, I40IW_DEBUG_PUDA,
  656. "%s error puda qp destroy wqe\n",
  657. __func__);
  658. if (!ret) {
  659. ret = dev->cqp_ops->poll_for_cqp_op_done(dev->cqp,
  660. I40IW_CQP_OP_DESTROY_QP,
  661. &compl_info);
  662. if (ret)
  663. i40iw_debug(dev, I40IW_DEBUG_PUDA,
  664. "%s error puda qp destroy failed\n",
  665. __func__);
  666. }
  667. }
  668. /**
  669. * i40iw_puda_free_cq - free cq for resource
  670. * @rsrc: resource for which cq to free
  671. */
  672. static void i40iw_puda_free_cq(struct i40iw_puda_rsrc *rsrc)
  673. {
  674. enum i40iw_status_code ret;
  675. struct i40iw_ccq_cqe_info compl_info;
  676. struct i40iw_sc_dev *dev = rsrc->dev;
  677. if (rsrc->dev->ceq_valid) {
  678. i40iw_cqp_cq_destroy_cmd(dev, &rsrc->cq);
  679. return;
  680. }
  681. ret = dev->iw_priv_cq_ops->cq_destroy(&rsrc->cq, 0, true);
  682. if (ret)
  683. i40iw_debug(dev, I40IW_DEBUG_PUDA,
  684. "%s error ieq cq destroy\n",
  685. __func__);
  686. if (!ret) {
  687. ret = dev->cqp_ops->poll_for_cqp_op_done(dev->cqp,
  688. I40IW_CQP_OP_DESTROY_CQ,
  689. &compl_info);
  690. if (ret)
  691. i40iw_debug(dev, I40IW_DEBUG_PUDA,
  692. "%s error ieq qp destroy done\n",
  693. __func__);
  694. }
  695. }
  696. /**
  697. * i40iw_puda_dele_resources - delete all resources during close
  698. * @dev: iwarp device
  699. * @type: type of resource to dele
  700. * @reset: true if reset chip
  701. */
  702. void i40iw_puda_dele_resources(struct i40iw_sc_vsi *vsi,
  703. enum puda_resource_type type,
  704. bool reset)
  705. {
  706. struct i40iw_sc_dev *dev = vsi->dev;
  707. struct i40iw_puda_rsrc *rsrc;
  708. struct i40iw_puda_buf *buf = NULL;
  709. struct i40iw_puda_buf *nextbuf = NULL;
  710. struct i40iw_virt_mem *vmem;
  711. switch (type) {
  712. case I40IW_PUDA_RSRC_TYPE_ILQ:
  713. rsrc = vsi->ilq;
  714. vmem = &vsi->ilq_mem;
  715. break;
  716. case I40IW_PUDA_RSRC_TYPE_IEQ:
  717. rsrc = vsi->ieq;
  718. vmem = &vsi->ieq_mem;
  719. break;
  720. default:
  721. i40iw_debug(dev, I40IW_DEBUG_PUDA, "%s: error resource type = 0x%x\n",
  722. __func__, type);
  723. return;
  724. }
  725. switch (rsrc->completion) {
  726. case PUDA_HASH_CRC_COMPLETE:
  727. i40iw_free_hash_desc(rsrc->hash_desc);
  728. /* fall through */
  729. case PUDA_QP_CREATED:
  730. if (!reset)
  731. i40iw_puda_free_qp(rsrc);
  732. i40iw_free_dma_mem(dev->hw, &rsrc->qpmem);
  733. /* fallthrough */
  734. case PUDA_CQ_CREATED:
  735. if (!reset)
  736. i40iw_puda_free_cq(rsrc);
  737. i40iw_free_dma_mem(dev->hw, &rsrc->cqmem);
  738. break;
  739. default:
  740. i40iw_debug(rsrc->dev, I40IW_DEBUG_PUDA, "%s error no resources\n", __func__);
  741. break;
  742. }
  743. /* Free all allocated puda buffers for both tx and rx */
  744. buf = rsrc->alloclist;
  745. while (buf) {
  746. nextbuf = buf->next;
  747. i40iw_puda_dele_buf(dev, buf);
  748. buf = nextbuf;
  749. rsrc->alloc_buf_count--;
  750. }
  751. i40iw_free_virt_mem(dev->hw, vmem);
  752. }
  753. /**
  754. * i40iw_puda_allocbufs - allocate buffers for resource
  755. * @rsrc: resource for buffer allocation
  756. * @count: number of buffers to create
  757. */
  758. static enum i40iw_status_code i40iw_puda_allocbufs(struct i40iw_puda_rsrc *rsrc,
  759. u32 count)
  760. {
  761. u32 i;
  762. struct i40iw_puda_buf *buf;
  763. struct i40iw_puda_buf *nextbuf;
  764. for (i = 0; i < count; i++) {
  765. buf = i40iw_puda_alloc_buf(rsrc->dev, rsrc->buf_size);
  766. if (!buf) {
  767. rsrc->stats_buf_alloc_fail++;
  768. return I40IW_ERR_NO_MEMORY;
  769. }
  770. i40iw_puda_ret_bufpool(rsrc, buf);
  771. rsrc->alloc_buf_count++;
  772. if (!rsrc->alloclist) {
  773. rsrc->alloclist = buf;
  774. } else {
  775. nextbuf = rsrc->alloclist;
  776. rsrc->alloclist = buf;
  777. buf->next = nextbuf;
  778. }
  779. }
  780. rsrc->avail_buf_count = rsrc->alloc_buf_count;
  781. return 0;
  782. }
  783. /**
  784. * i40iw_puda_create_rsrc - create resouce (ilq or ieq)
  785. * @dev: iwarp device
  786. * @info: resource information
  787. */
  788. enum i40iw_status_code i40iw_puda_create_rsrc(struct i40iw_sc_vsi *vsi,
  789. struct i40iw_puda_rsrc_info *info)
  790. {
  791. struct i40iw_sc_dev *dev = vsi->dev;
  792. enum i40iw_status_code ret = 0;
  793. struct i40iw_puda_rsrc *rsrc;
  794. u32 pudasize;
  795. u32 sqwridsize, rqwridsize;
  796. struct i40iw_virt_mem *vmem;
  797. info->count = 1;
  798. pudasize = sizeof(struct i40iw_puda_rsrc);
  799. sqwridsize = info->sq_size * sizeof(struct i40iw_sq_uk_wr_trk_info);
  800. rqwridsize = info->rq_size * 8;
  801. switch (info->type) {
  802. case I40IW_PUDA_RSRC_TYPE_ILQ:
  803. vmem = &vsi->ilq_mem;
  804. break;
  805. case I40IW_PUDA_RSRC_TYPE_IEQ:
  806. vmem = &vsi->ieq_mem;
  807. break;
  808. default:
  809. return I40IW_NOT_SUPPORTED;
  810. }
  811. ret =
  812. i40iw_allocate_virt_mem(dev->hw, vmem,
  813. pudasize + sqwridsize + rqwridsize);
  814. if (ret)
  815. return ret;
  816. rsrc = (struct i40iw_puda_rsrc *)vmem->va;
  817. spin_lock_init(&rsrc->bufpool_lock);
  818. if (info->type == I40IW_PUDA_RSRC_TYPE_ILQ) {
  819. vsi->ilq = (struct i40iw_puda_rsrc *)vmem->va;
  820. vsi->ilq_count = info->count;
  821. rsrc->receive = info->receive;
  822. rsrc->xmit_complete = info->xmit_complete;
  823. } else {
  824. vmem = &vsi->ieq_mem;
  825. vsi->ieq_count = info->count;
  826. vsi->ieq = (struct i40iw_puda_rsrc *)vmem->va;
  827. rsrc->receive = i40iw_ieq_receive;
  828. rsrc->xmit_complete = i40iw_ieq_tx_compl;
  829. }
  830. rsrc->type = info->type;
  831. rsrc->sq_wrtrk_array = (struct i40iw_sq_uk_wr_trk_info *)((u8 *)vmem->va + pudasize);
  832. rsrc->rq_wrid_array = (u64 *)((u8 *)vmem->va + pudasize + sqwridsize);
  833. /* Initialize all ieq lists */
  834. INIT_LIST_HEAD(&rsrc->bufpool);
  835. INIT_LIST_HEAD(&rsrc->txpend);
  836. rsrc->tx_wqe_avail_cnt = info->sq_size - 1;
  837. dev->iw_pd_ops->pd_init(dev, &rsrc->sc_pd, info->pd_id, -1);
  838. rsrc->qp_id = info->qp_id;
  839. rsrc->cq_id = info->cq_id;
  840. rsrc->sq_size = info->sq_size;
  841. rsrc->rq_size = info->rq_size;
  842. rsrc->cq_size = info->rq_size + info->sq_size;
  843. rsrc->buf_size = info->buf_size;
  844. rsrc->dev = dev;
  845. rsrc->vsi = vsi;
  846. ret = i40iw_puda_cq_create(rsrc);
  847. if (!ret) {
  848. rsrc->completion = PUDA_CQ_CREATED;
  849. ret = i40iw_puda_qp_create(rsrc);
  850. }
  851. if (ret) {
  852. i40iw_debug(dev, I40IW_DEBUG_PUDA, "[%s] error qp_create\n",
  853. __func__);
  854. goto error;
  855. }
  856. rsrc->completion = PUDA_QP_CREATED;
  857. ret = i40iw_puda_allocbufs(rsrc, info->tx_buf_cnt + info->rq_size);
  858. if (ret) {
  859. i40iw_debug(dev, I40IW_DEBUG_PUDA, "[%s] error alloc_buf\n",
  860. __func__);
  861. goto error;
  862. }
  863. rsrc->rxq_invalid_cnt = info->rq_size;
  864. ret = i40iw_puda_replenish_rq(rsrc, true);
  865. if (ret)
  866. goto error;
  867. if (info->type == I40IW_PUDA_RSRC_TYPE_IEQ) {
  868. if (!i40iw_init_hash_desc(&rsrc->hash_desc)) {
  869. rsrc->check_crc = true;
  870. rsrc->completion = PUDA_HASH_CRC_COMPLETE;
  871. ret = 0;
  872. }
  873. }
  874. dev->ccq_ops->ccq_arm(&rsrc->cq);
  875. return ret;
  876. error:
  877. i40iw_puda_dele_resources(vsi, info->type, false);
  878. return ret;
  879. }
  880. /**
  881. * i40iw_ilq_putback_rcvbuf - ilq buffer to put back on rq
  882. * @qp: ilq's qp resource
  883. * @wqe_idx: wqe index of completed rcvbuf
  884. */
  885. static void i40iw_ilq_putback_rcvbuf(struct i40iw_sc_qp *qp, u32 wqe_idx)
  886. {
  887. u64 *wqe;
  888. u64 offset24;
  889. wqe = qp->qp_uk.rq_base[wqe_idx].elem;
  890. get_64bit_val(wqe, 24, &offset24);
  891. offset24 = (offset24) ? 0 : LS_64(1, I40IWQPSQ_VALID);
  892. set_64bit_val(wqe, 24, offset24);
  893. }
  894. /**
  895. * i40iw_ieq_get_fpdu - given length return fpdu length
  896. * @length: length if fpdu
  897. */
  898. static u16 i40iw_ieq_get_fpdu_length(u16 length)
  899. {
  900. u16 fpdu_len;
  901. fpdu_len = length + I40IW_IEQ_MPA_FRAMING;
  902. fpdu_len = (fpdu_len + 3) & 0xfffffffc;
  903. return fpdu_len;
  904. }
  905. /**
  906. * i40iw_ieq_copy_to_txbuf - copydata from rcv buf to tx buf
  907. * @buf: rcv buffer with partial
  908. * @txbuf: tx buffer for sendign back
  909. * @buf_offset: rcv buffer offset to copy from
  910. * @txbuf_offset: at offset in tx buf to copy
  911. * @length: length of data to copy
  912. */
  913. static void i40iw_ieq_copy_to_txbuf(struct i40iw_puda_buf *buf,
  914. struct i40iw_puda_buf *txbuf,
  915. u16 buf_offset, u32 txbuf_offset,
  916. u32 length)
  917. {
  918. void *mem1 = (u8 *)buf->mem.va + buf_offset;
  919. void *mem2 = (u8 *)txbuf->mem.va + txbuf_offset;
  920. memcpy(mem2, mem1, length);
  921. }
  922. /**
  923. * i40iw_ieq_setup_tx_buf - setup tx buffer for partial handling
  924. * @buf: reeive buffer with partial
  925. * @txbuf: buffer to prepare
  926. */
  927. static void i40iw_ieq_setup_tx_buf(struct i40iw_puda_buf *buf,
  928. struct i40iw_puda_buf *txbuf)
  929. {
  930. txbuf->maclen = buf->maclen;
  931. txbuf->tcphlen = buf->tcphlen;
  932. txbuf->ipv4 = buf->ipv4;
  933. txbuf->hdrlen = buf->hdrlen;
  934. i40iw_ieq_copy_to_txbuf(buf, txbuf, 0, 0, buf->hdrlen);
  935. }
  936. /**
  937. * i40iw_ieq_check_first_buf - check if rcv buffer's seq is in range
  938. * @buf: receive exception buffer
  939. * @fps: first partial sequence number
  940. */
  941. static void i40iw_ieq_check_first_buf(struct i40iw_puda_buf *buf, u32 fps)
  942. {
  943. u32 offset;
  944. if (buf->seqnum < fps) {
  945. offset = fps - buf->seqnum;
  946. if (offset > buf->datalen)
  947. return;
  948. buf->data += offset;
  949. buf->datalen -= (u16)offset;
  950. buf->seqnum = fps;
  951. }
  952. }
  953. /**
  954. * i40iw_ieq_compl_pfpdu - write txbuf with full fpdu
  955. * @ieq: ieq resource
  956. * @rxlist: ieq's received buffer list
  957. * @pbufl: temporary list for buffers for fpddu
  958. * @txbuf: tx buffer for fpdu
  959. * @fpdu_len: total length of fpdu
  960. */
  961. static void i40iw_ieq_compl_pfpdu(struct i40iw_puda_rsrc *ieq,
  962. struct list_head *rxlist,
  963. struct list_head *pbufl,
  964. struct i40iw_puda_buf *txbuf,
  965. u16 fpdu_len)
  966. {
  967. struct i40iw_puda_buf *buf;
  968. u32 nextseqnum;
  969. u16 txoffset, bufoffset;
  970. buf = i40iw_puda_get_listbuf(pbufl);
  971. if (!buf)
  972. return;
  973. nextseqnum = buf->seqnum + fpdu_len;
  974. txbuf->totallen = buf->hdrlen + fpdu_len;
  975. txbuf->data = (u8 *)txbuf->mem.va + buf->hdrlen;
  976. i40iw_ieq_setup_tx_buf(buf, txbuf);
  977. txoffset = buf->hdrlen;
  978. bufoffset = (u16)(buf->data - (u8 *)buf->mem.va);
  979. do {
  980. if (buf->datalen >= fpdu_len) {
  981. /* copied full fpdu */
  982. i40iw_ieq_copy_to_txbuf(buf, txbuf, bufoffset, txoffset, fpdu_len);
  983. buf->datalen -= fpdu_len;
  984. buf->data += fpdu_len;
  985. buf->seqnum = nextseqnum;
  986. break;
  987. }
  988. /* copy partial fpdu */
  989. i40iw_ieq_copy_to_txbuf(buf, txbuf, bufoffset, txoffset, buf->datalen);
  990. txoffset += buf->datalen;
  991. fpdu_len -= buf->datalen;
  992. i40iw_puda_ret_bufpool(ieq, buf);
  993. buf = i40iw_puda_get_listbuf(pbufl);
  994. if (!buf)
  995. return;
  996. bufoffset = (u16)(buf->data - (u8 *)buf->mem.va);
  997. } while (1);
  998. /* last buffer on the list*/
  999. if (buf->datalen)
  1000. list_add(&buf->list, rxlist);
  1001. else
  1002. i40iw_puda_ret_bufpool(ieq, buf);
  1003. }
  1004. /**
  1005. * i40iw_ieq_create_pbufl - create buffer list for single fpdu
  1006. * @rxlist: resource list for receive ieq buffes
  1007. * @pbufl: temp. list for buffers for fpddu
  1008. * @buf: first receive buffer
  1009. * @fpdu_len: total length of fpdu
  1010. */
  1011. static enum i40iw_status_code i40iw_ieq_create_pbufl(
  1012. struct i40iw_pfpdu *pfpdu,
  1013. struct list_head *rxlist,
  1014. struct list_head *pbufl,
  1015. struct i40iw_puda_buf *buf,
  1016. u16 fpdu_len)
  1017. {
  1018. enum i40iw_status_code status = 0;
  1019. struct i40iw_puda_buf *nextbuf;
  1020. u32 nextseqnum;
  1021. u16 plen = fpdu_len - buf->datalen;
  1022. bool done = false;
  1023. nextseqnum = buf->seqnum + buf->datalen;
  1024. do {
  1025. nextbuf = i40iw_puda_get_listbuf(rxlist);
  1026. if (!nextbuf) {
  1027. status = I40IW_ERR_list_empty;
  1028. break;
  1029. }
  1030. list_add_tail(&nextbuf->list, pbufl);
  1031. if (nextbuf->seqnum != nextseqnum) {
  1032. pfpdu->bad_seq_num++;
  1033. status = I40IW_ERR_SEQ_NUM;
  1034. break;
  1035. }
  1036. if (nextbuf->datalen >= plen) {
  1037. done = true;
  1038. } else {
  1039. plen -= nextbuf->datalen;
  1040. nextseqnum = nextbuf->seqnum + nextbuf->datalen;
  1041. }
  1042. } while (!done);
  1043. return status;
  1044. }
  1045. /**
  1046. * i40iw_ieq_handle_partial - process partial fpdu buffer
  1047. * @ieq: ieq resource
  1048. * @pfpdu: partial management per user qp
  1049. * @buf: receive buffer
  1050. * @fpdu_len: fpdu len in the buffer
  1051. */
  1052. static enum i40iw_status_code i40iw_ieq_handle_partial(struct i40iw_puda_rsrc *ieq,
  1053. struct i40iw_pfpdu *pfpdu,
  1054. struct i40iw_puda_buf *buf,
  1055. u16 fpdu_len)
  1056. {
  1057. enum i40iw_status_code status = 0;
  1058. u8 *crcptr;
  1059. u32 mpacrc;
  1060. u32 seqnum = buf->seqnum;
  1061. struct list_head pbufl; /* partial buffer list */
  1062. struct i40iw_puda_buf *txbuf = NULL;
  1063. struct list_head *rxlist = &pfpdu->rxlist;
  1064. INIT_LIST_HEAD(&pbufl);
  1065. list_add(&buf->list, &pbufl);
  1066. status = i40iw_ieq_create_pbufl(pfpdu, rxlist, &pbufl, buf, fpdu_len);
  1067. if (status)
  1068. goto error;
  1069. txbuf = i40iw_puda_get_bufpool(ieq);
  1070. if (!txbuf) {
  1071. pfpdu->no_tx_bufs++;
  1072. status = I40IW_ERR_NO_TXBUFS;
  1073. goto error;
  1074. }
  1075. i40iw_ieq_compl_pfpdu(ieq, rxlist, &pbufl, txbuf, fpdu_len);
  1076. i40iw_ieq_update_tcpip_info(txbuf, fpdu_len, seqnum);
  1077. crcptr = txbuf->data + fpdu_len - 4;
  1078. mpacrc = *(u32 *)crcptr;
  1079. if (ieq->check_crc) {
  1080. status = i40iw_ieq_check_mpacrc(ieq->hash_desc, txbuf->data,
  1081. (fpdu_len - 4), mpacrc);
  1082. if (status) {
  1083. i40iw_debug(ieq->dev, I40IW_DEBUG_IEQ,
  1084. "%s: error bad crc\n", __func__);
  1085. goto error;
  1086. }
  1087. }
  1088. i40iw_debug_buf(ieq->dev, I40IW_DEBUG_IEQ, "IEQ TX BUFFER",
  1089. txbuf->mem.va, txbuf->totallen);
  1090. i40iw_puda_send_buf(ieq, txbuf);
  1091. pfpdu->rcv_nxt = seqnum + fpdu_len;
  1092. return status;
  1093. error:
  1094. while (!list_empty(&pbufl)) {
  1095. buf = (struct i40iw_puda_buf *)(pbufl.prev);
  1096. list_del(&buf->list);
  1097. list_add(&buf->list, rxlist);
  1098. }
  1099. if (txbuf)
  1100. i40iw_puda_ret_bufpool(ieq, txbuf);
  1101. return status;
  1102. }
  1103. /**
  1104. * i40iw_ieq_process_buf - process buffer rcvd for ieq
  1105. * @ieq: ieq resource
  1106. * @pfpdu: partial management per user qp
  1107. * @buf: receive buffer
  1108. */
  1109. static enum i40iw_status_code i40iw_ieq_process_buf(struct i40iw_puda_rsrc *ieq,
  1110. struct i40iw_pfpdu *pfpdu,
  1111. struct i40iw_puda_buf *buf)
  1112. {
  1113. u16 fpdu_len = 0;
  1114. u16 datalen = buf->datalen;
  1115. u8 *datap = buf->data;
  1116. u8 *crcptr;
  1117. u16 ioffset = 0;
  1118. u32 mpacrc;
  1119. u32 seqnum = buf->seqnum;
  1120. u16 length = 0;
  1121. u16 full = 0;
  1122. bool partial = false;
  1123. struct i40iw_puda_buf *txbuf;
  1124. struct list_head *rxlist = &pfpdu->rxlist;
  1125. enum i40iw_status_code ret = 0;
  1126. enum i40iw_status_code status = 0;
  1127. ioffset = (u16)(buf->data - (u8 *)buf->mem.va);
  1128. while (datalen) {
  1129. fpdu_len = i40iw_ieq_get_fpdu_length(ntohs(*(__be16 *)datap));
  1130. if (fpdu_len > pfpdu->max_fpdu_data) {
  1131. i40iw_debug(ieq->dev, I40IW_DEBUG_IEQ,
  1132. "%s: error bad fpdu_len\n", __func__);
  1133. status = I40IW_ERR_MPA_CRC;
  1134. list_add(&buf->list, rxlist);
  1135. return status;
  1136. }
  1137. if (datalen < fpdu_len) {
  1138. partial = true;
  1139. break;
  1140. }
  1141. crcptr = datap + fpdu_len - 4;
  1142. mpacrc = *(u32 *)crcptr;
  1143. if (ieq->check_crc)
  1144. ret = i40iw_ieq_check_mpacrc(ieq->hash_desc,
  1145. datap, fpdu_len - 4, mpacrc);
  1146. if (ret) {
  1147. status = I40IW_ERR_MPA_CRC;
  1148. list_add(&buf->list, rxlist);
  1149. return status;
  1150. }
  1151. full++;
  1152. pfpdu->fpdu_processed++;
  1153. datap += fpdu_len;
  1154. length += fpdu_len;
  1155. datalen -= fpdu_len;
  1156. }
  1157. if (full) {
  1158. /* copy full pdu's in the txbuf and send them out */
  1159. txbuf = i40iw_puda_get_bufpool(ieq);
  1160. if (!txbuf) {
  1161. pfpdu->no_tx_bufs++;
  1162. status = I40IW_ERR_NO_TXBUFS;
  1163. list_add(&buf->list, rxlist);
  1164. return status;
  1165. }
  1166. /* modify txbuf's buffer header */
  1167. i40iw_ieq_setup_tx_buf(buf, txbuf);
  1168. /* copy full fpdu's to new buffer */
  1169. i40iw_ieq_copy_to_txbuf(buf, txbuf, ioffset, buf->hdrlen,
  1170. length);
  1171. txbuf->totallen = buf->hdrlen + length;
  1172. i40iw_ieq_update_tcpip_info(txbuf, length, buf->seqnum);
  1173. i40iw_puda_send_buf(ieq, txbuf);
  1174. if (!datalen) {
  1175. pfpdu->rcv_nxt = buf->seqnum + length;
  1176. i40iw_puda_ret_bufpool(ieq, buf);
  1177. return status;
  1178. }
  1179. buf->data = datap;
  1180. buf->seqnum = seqnum + length;
  1181. buf->datalen = datalen;
  1182. pfpdu->rcv_nxt = buf->seqnum;
  1183. }
  1184. if (partial)
  1185. status = i40iw_ieq_handle_partial(ieq, pfpdu, buf, fpdu_len);
  1186. return status;
  1187. }
  1188. /**
  1189. * i40iw_ieq_process_fpdus - process fpdu's buffers on its list
  1190. * @qp: qp for which partial fpdus
  1191. * @ieq: ieq resource
  1192. */
  1193. static void i40iw_ieq_process_fpdus(struct i40iw_sc_qp *qp,
  1194. struct i40iw_puda_rsrc *ieq)
  1195. {
  1196. struct i40iw_pfpdu *pfpdu = &qp->pfpdu;
  1197. struct list_head *rxlist = &pfpdu->rxlist;
  1198. struct i40iw_puda_buf *buf;
  1199. enum i40iw_status_code status;
  1200. do {
  1201. if (list_empty(rxlist))
  1202. break;
  1203. buf = i40iw_puda_get_listbuf(rxlist);
  1204. if (!buf) {
  1205. i40iw_debug(ieq->dev, I40IW_DEBUG_IEQ,
  1206. "%s: error no buf\n", __func__);
  1207. break;
  1208. }
  1209. if (buf->seqnum != pfpdu->rcv_nxt) {
  1210. /* This could be out of order or missing packet */
  1211. pfpdu->out_of_order++;
  1212. list_add(&buf->list, rxlist);
  1213. break;
  1214. }
  1215. /* keep processing buffers from the head of the list */
  1216. status = i40iw_ieq_process_buf(ieq, pfpdu, buf);
  1217. if (status == I40IW_ERR_MPA_CRC) {
  1218. pfpdu->mpa_crc_err = true;
  1219. while (!list_empty(rxlist)) {
  1220. buf = i40iw_puda_get_listbuf(rxlist);
  1221. i40iw_puda_ret_bufpool(ieq, buf);
  1222. pfpdu->crc_err++;
  1223. }
  1224. /* create CQP for AE */
  1225. i40iw_ieq_mpa_crc_ae(ieq->dev, qp);
  1226. }
  1227. } while (!status);
  1228. }
  1229. /**
  1230. * i40iw_ieq_handle_exception - handle qp's exception
  1231. * @ieq: ieq resource
  1232. * @qp: qp receiving excpetion
  1233. * @buf: receive buffer
  1234. */
  1235. static void i40iw_ieq_handle_exception(struct i40iw_puda_rsrc *ieq,
  1236. struct i40iw_sc_qp *qp,
  1237. struct i40iw_puda_buf *buf)
  1238. {
  1239. struct i40iw_puda_buf *tmpbuf = NULL;
  1240. struct i40iw_pfpdu *pfpdu = &qp->pfpdu;
  1241. u32 *hw_host_ctx = (u32 *)qp->hw_host_ctx;
  1242. u32 rcv_wnd = hw_host_ctx[23];
  1243. /* first partial seq # in q2 */
  1244. u32 fps = *(u32 *)(qp->q2_buf + Q2_FPSN_OFFSET);
  1245. struct list_head *rxlist = &pfpdu->rxlist;
  1246. struct list_head *plist;
  1247. pfpdu->total_ieq_bufs++;
  1248. if (pfpdu->mpa_crc_err) {
  1249. pfpdu->crc_err++;
  1250. goto error;
  1251. }
  1252. if (pfpdu->mode && (fps != pfpdu->fps)) {
  1253. /* clean up qp as it is new partial sequence */
  1254. i40iw_ieq_cleanup_qp(ieq, qp);
  1255. i40iw_debug(ieq->dev, I40IW_DEBUG_IEQ,
  1256. "%s: restarting new partial\n", __func__);
  1257. pfpdu->mode = false;
  1258. }
  1259. if (!pfpdu->mode) {
  1260. i40iw_debug_buf(ieq->dev, I40IW_DEBUG_IEQ, "Q2 BUFFER", (u64 *)qp->q2_buf, 128);
  1261. /* First_Partial_Sequence_Number check */
  1262. pfpdu->rcv_nxt = fps;
  1263. pfpdu->fps = fps;
  1264. pfpdu->mode = true;
  1265. pfpdu->max_fpdu_data = (buf->ipv4) ? (ieq->vsi->mtu - I40IW_MTU_TO_MSS_IPV4) :
  1266. (ieq->vsi->mtu - I40IW_MTU_TO_MSS_IPV6);
  1267. pfpdu->pmode_count++;
  1268. INIT_LIST_HEAD(rxlist);
  1269. i40iw_ieq_check_first_buf(buf, fps);
  1270. }
  1271. if (!(rcv_wnd >= (buf->seqnum - pfpdu->rcv_nxt))) {
  1272. pfpdu->bad_seq_num++;
  1273. goto error;
  1274. }
  1275. if (!list_empty(rxlist)) {
  1276. tmpbuf = (struct i40iw_puda_buf *)rxlist->next;
  1277. while ((struct list_head *)tmpbuf != rxlist) {
  1278. if ((int)(buf->seqnum - tmpbuf->seqnum) < 0)
  1279. break;
  1280. plist = &tmpbuf->list;
  1281. tmpbuf = (struct i40iw_puda_buf *)plist->next;
  1282. }
  1283. /* Insert buf before tmpbuf */
  1284. list_add_tail(&buf->list, &tmpbuf->list);
  1285. } else {
  1286. list_add_tail(&buf->list, rxlist);
  1287. }
  1288. i40iw_ieq_process_fpdus(qp, ieq);
  1289. return;
  1290. error:
  1291. i40iw_puda_ret_bufpool(ieq, buf);
  1292. }
  1293. /**
  1294. * i40iw_ieq_receive - received exception buffer
  1295. * @dev: iwarp device
  1296. * @buf: exception buffer received
  1297. */
  1298. static void i40iw_ieq_receive(struct i40iw_sc_vsi *vsi,
  1299. struct i40iw_puda_buf *buf)
  1300. {
  1301. struct i40iw_puda_rsrc *ieq = vsi->ieq;
  1302. struct i40iw_sc_qp *qp = NULL;
  1303. u32 wqe_idx = ieq->compl_rxwqe_idx;
  1304. qp = i40iw_ieq_get_qp(vsi->dev, buf);
  1305. if (!qp) {
  1306. ieq->stats_bad_qp_id++;
  1307. i40iw_puda_ret_bufpool(ieq, buf);
  1308. } else {
  1309. i40iw_ieq_handle_exception(ieq, qp, buf);
  1310. }
  1311. /*
  1312. * ieq->rx_wqe_idx is used by i40iw_puda_replenish_rq()
  1313. * on which wqe_idx to start replenish rq
  1314. */
  1315. if (!ieq->rxq_invalid_cnt)
  1316. ieq->rx_wqe_idx = wqe_idx;
  1317. ieq->rxq_invalid_cnt++;
  1318. }
  1319. /**
  1320. * i40iw_ieq_tx_compl - put back after sending completed exception buffer
  1321. * @vsi: pointer to the vsi structure
  1322. * @sqwrid: pointer to puda buffer
  1323. */
  1324. static void i40iw_ieq_tx_compl(struct i40iw_sc_vsi *vsi, void *sqwrid)
  1325. {
  1326. struct i40iw_puda_rsrc *ieq = vsi->ieq;
  1327. struct i40iw_puda_buf *buf = (struct i40iw_puda_buf *)sqwrid;
  1328. i40iw_puda_ret_bufpool(ieq, buf);
  1329. }
  1330. /**
  1331. * i40iw_ieq_cleanup_qp - qp is being destroyed
  1332. * @ieq: ieq resource
  1333. * @qp: all pending fpdu buffers
  1334. */
  1335. void i40iw_ieq_cleanup_qp(struct i40iw_puda_rsrc *ieq, struct i40iw_sc_qp *qp)
  1336. {
  1337. struct i40iw_puda_buf *buf;
  1338. struct i40iw_pfpdu *pfpdu = &qp->pfpdu;
  1339. struct list_head *rxlist = &pfpdu->rxlist;
  1340. if (!pfpdu->mode)
  1341. return;
  1342. while (!list_empty(rxlist)) {
  1343. buf = i40iw_puda_get_listbuf(rxlist);
  1344. i40iw_puda_ret_bufpool(ieq, buf);
  1345. }
  1346. }