i40iw_puda.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495
  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->vsi->ilq->txpend))
  316. i40iw_puda_send_buf(rsrc->vsi->ilq, 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(1514, I40IWQPC_SNDMSS));
  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->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_free_dma_mem(rsrc->dev->hw, &rsrc->qpmem);
  551. return ret;
  552. }
  553. /**
  554. * i40iw_puda_cq_wqe - setup wqe for cq create
  555. * @rsrc: resource for cq
  556. */
  557. static enum i40iw_status_code i40iw_puda_cq_wqe(struct i40iw_sc_dev *dev, struct i40iw_sc_cq *cq)
  558. {
  559. u64 *wqe;
  560. struct i40iw_sc_cqp *cqp;
  561. u64 header;
  562. struct i40iw_ccq_cqe_info compl_info;
  563. enum i40iw_status_code status = 0;
  564. cqp = dev->cqp;
  565. wqe = i40iw_sc_cqp_get_next_send_wqe(cqp, 0);
  566. if (!wqe)
  567. return I40IW_ERR_RING_FULL;
  568. set_64bit_val(wqe, 0, cq->cq_uk.cq_size);
  569. set_64bit_val(wqe, 8, RS_64_1(cq, 1));
  570. set_64bit_val(wqe, 16,
  571. LS_64(cq->shadow_read_threshold,
  572. I40IW_CQPSQ_CQ_SHADOW_READ_THRESHOLD));
  573. set_64bit_val(wqe, 32, cq->cq_pa);
  574. set_64bit_val(wqe, 40, cq->shadow_area_pa);
  575. header = cq->cq_uk.cq_id |
  576. LS_64(I40IW_CQP_OP_CREATE_CQ, I40IW_CQPSQ_OPCODE) |
  577. LS_64(1, I40IW_CQPSQ_CQ_CHKOVERFLOW) |
  578. LS_64(1, I40IW_CQPSQ_CQ_ENCEQEMASK) |
  579. LS_64(1, I40IW_CQPSQ_CQ_CEQIDVALID) |
  580. LS_64(cqp->polarity, I40IW_CQPSQ_WQEVALID);
  581. i40iw_insert_wqe_hdr(wqe, header);
  582. i40iw_debug_buf(dev, I40IW_DEBUG_PUDA, "PUDA CQE",
  583. wqe, I40IW_CQP_WQE_SIZE * 8);
  584. i40iw_sc_cqp_post_sq(dev->cqp);
  585. status = dev->cqp_ops->poll_for_cqp_op_done(dev->cqp,
  586. I40IW_CQP_OP_CREATE_CQ,
  587. &compl_info);
  588. return status;
  589. }
  590. /**
  591. * i40iw_puda_cq_create - create cq for resource
  592. * @rsrc: resource for which cq to create
  593. */
  594. static enum i40iw_status_code i40iw_puda_cq_create(struct i40iw_puda_rsrc *rsrc)
  595. {
  596. struct i40iw_sc_dev *dev = rsrc->dev;
  597. struct i40iw_sc_cq *cq = &rsrc->cq;
  598. enum i40iw_status_code ret = 0;
  599. u32 tsize, cqsize;
  600. struct i40iw_dma_mem *mem;
  601. struct i40iw_cq_init_info info;
  602. struct i40iw_cq_uk_init_info *init_info = &info.cq_uk_init_info;
  603. cq->vsi = rsrc->vsi;
  604. cqsize = rsrc->cq_size * (sizeof(struct i40iw_cqe));
  605. tsize = cqsize + sizeof(struct i40iw_cq_shadow_area);
  606. ret = i40iw_allocate_dma_mem(dev->hw, &rsrc->cqmem, tsize,
  607. I40IW_CQ0_ALIGNMENT);
  608. if (ret)
  609. return ret;
  610. mem = &rsrc->cqmem;
  611. memset(&info, 0, sizeof(info));
  612. info.dev = dev;
  613. info.type = (rsrc->type == I40IW_PUDA_RSRC_TYPE_ILQ) ?
  614. I40IW_CQ_TYPE_ILQ : I40IW_CQ_TYPE_IEQ;
  615. info.shadow_read_threshold = rsrc->cq_size >> 2;
  616. info.ceq_id_valid = true;
  617. info.cq_base_pa = mem->pa;
  618. info.shadow_area_pa = mem->pa + cqsize;
  619. init_info->cq_base = mem->va;
  620. init_info->shadow_area = (u64 *)((u8 *)mem->va + cqsize);
  621. init_info->cq_size = rsrc->cq_size;
  622. init_info->cq_id = rsrc->cq_id;
  623. info.ceqe_mask = true;
  624. info.ceq_id_valid = true;
  625. ret = dev->iw_priv_cq_ops->cq_init(cq, &info);
  626. if (ret)
  627. goto error;
  628. if (rsrc->ceq_valid)
  629. ret = i40iw_cqp_cq_create_cmd(dev, cq);
  630. else
  631. ret = i40iw_puda_cq_wqe(dev, cq);
  632. error:
  633. if (ret)
  634. i40iw_free_dma_mem(dev->hw, &rsrc->cqmem);
  635. return ret;
  636. }
  637. /**
  638. * i40iw_puda_free_qp - free qp for resource
  639. * @rsrc: resource for which qp to free
  640. */
  641. static void i40iw_puda_free_qp(struct i40iw_puda_rsrc *rsrc)
  642. {
  643. enum i40iw_status_code ret;
  644. struct i40iw_ccq_cqe_info compl_info;
  645. struct i40iw_sc_dev *dev = rsrc->dev;
  646. if (rsrc->ceq_valid) {
  647. i40iw_cqp_qp_destroy_cmd(dev, &rsrc->qp);
  648. return;
  649. }
  650. ret = dev->iw_priv_qp_ops->qp_destroy(&rsrc->qp,
  651. 0, false, true, true);
  652. if (ret)
  653. i40iw_debug(dev, I40IW_DEBUG_PUDA,
  654. "%s error puda qp destroy wqe\n",
  655. __func__);
  656. if (!ret) {
  657. ret = dev->cqp_ops->poll_for_cqp_op_done(dev->cqp,
  658. I40IW_CQP_OP_DESTROY_QP,
  659. &compl_info);
  660. if (ret)
  661. i40iw_debug(dev, I40IW_DEBUG_PUDA,
  662. "%s error puda qp destroy failed\n",
  663. __func__);
  664. }
  665. }
  666. /**
  667. * i40iw_puda_free_cq - free cq for resource
  668. * @rsrc: resource for which cq to free
  669. */
  670. static void i40iw_puda_free_cq(struct i40iw_puda_rsrc *rsrc)
  671. {
  672. enum i40iw_status_code ret;
  673. struct i40iw_ccq_cqe_info compl_info;
  674. struct i40iw_sc_dev *dev = rsrc->dev;
  675. if (rsrc->ceq_valid) {
  676. i40iw_cqp_cq_destroy_cmd(dev, &rsrc->cq);
  677. return;
  678. }
  679. ret = dev->iw_priv_cq_ops->cq_destroy(&rsrc->cq, 0, true);
  680. if (ret)
  681. i40iw_debug(dev, I40IW_DEBUG_PUDA,
  682. "%s error ieq cq destroy\n",
  683. __func__);
  684. if (!ret) {
  685. ret = dev->cqp_ops->poll_for_cqp_op_done(dev->cqp,
  686. I40IW_CQP_OP_DESTROY_CQ,
  687. &compl_info);
  688. if (ret)
  689. i40iw_debug(dev, I40IW_DEBUG_PUDA,
  690. "%s error ieq qp destroy done\n",
  691. __func__);
  692. }
  693. }
  694. /**
  695. * i40iw_puda_dele_resources - delete all resources during close
  696. * @dev: iwarp device
  697. * @type: type of resource to dele
  698. * @reset: true if reset chip
  699. */
  700. void i40iw_puda_dele_resources(struct i40iw_sc_vsi *vsi,
  701. enum puda_resource_type type,
  702. bool reset)
  703. {
  704. struct i40iw_sc_dev *dev = vsi->dev;
  705. struct i40iw_puda_rsrc *rsrc;
  706. struct i40iw_puda_buf *buf = NULL;
  707. struct i40iw_puda_buf *nextbuf = NULL;
  708. struct i40iw_virt_mem *vmem;
  709. switch (type) {
  710. case I40IW_PUDA_RSRC_TYPE_ILQ:
  711. rsrc = vsi->ilq;
  712. vmem = &vsi->ilq_mem;
  713. break;
  714. case I40IW_PUDA_RSRC_TYPE_IEQ:
  715. rsrc = vsi->ieq;
  716. vmem = &vsi->ieq_mem;
  717. break;
  718. default:
  719. i40iw_debug(dev, I40IW_DEBUG_PUDA, "%s: error resource type = 0x%x\n",
  720. __func__, type);
  721. return;
  722. }
  723. switch (rsrc->completion) {
  724. case PUDA_HASH_CRC_COMPLETE:
  725. i40iw_free_hash_desc(rsrc->hash_desc);
  726. case PUDA_QP_CREATED:
  727. if (!reset)
  728. i40iw_puda_free_qp(rsrc);
  729. i40iw_free_dma_mem(dev->hw, &rsrc->qpmem);
  730. /* fallthrough */
  731. case PUDA_CQ_CREATED:
  732. if (!reset)
  733. i40iw_puda_free_cq(rsrc);
  734. i40iw_free_dma_mem(dev->hw, &rsrc->cqmem);
  735. break;
  736. default:
  737. i40iw_debug(rsrc->dev, I40IW_DEBUG_PUDA, "%s error no resources\n", __func__);
  738. break;
  739. }
  740. /* Free all allocated puda buffers for both tx and rx */
  741. buf = rsrc->alloclist;
  742. while (buf) {
  743. nextbuf = buf->next;
  744. i40iw_puda_dele_buf(dev, buf);
  745. buf = nextbuf;
  746. rsrc->alloc_buf_count--;
  747. }
  748. i40iw_free_virt_mem(dev->hw, vmem);
  749. }
  750. /**
  751. * i40iw_puda_allocbufs - allocate buffers for resource
  752. * @rsrc: resource for buffer allocation
  753. * @count: number of buffers to create
  754. */
  755. static enum i40iw_status_code i40iw_puda_allocbufs(struct i40iw_puda_rsrc *rsrc,
  756. u32 count)
  757. {
  758. u32 i;
  759. struct i40iw_puda_buf *buf;
  760. struct i40iw_puda_buf *nextbuf;
  761. for (i = 0; i < count; i++) {
  762. buf = i40iw_puda_alloc_buf(rsrc->dev, rsrc->buf_size);
  763. if (!buf) {
  764. rsrc->stats_buf_alloc_fail++;
  765. return I40IW_ERR_NO_MEMORY;
  766. }
  767. i40iw_puda_ret_bufpool(rsrc, buf);
  768. rsrc->alloc_buf_count++;
  769. if (!rsrc->alloclist) {
  770. rsrc->alloclist = buf;
  771. } else {
  772. nextbuf = rsrc->alloclist;
  773. rsrc->alloclist = buf;
  774. buf->next = nextbuf;
  775. }
  776. }
  777. rsrc->avail_buf_count = rsrc->alloc_buf_count;
  778. return 0;
  779. }
  780. /**
  781. * i40iw_puda_create_rsrc - create resouce (ilq or ieq)
  782. * @dev: iwarp device
  783. * @info: resource information
  784. */
  785. enum i40iw_status_code i40iw_puda_create_rsrc(struct i40iw_sc_vsi *vsi,
  786. struct i40iw_puda_rsrc_info *info)
  787. {
  788. struct i40iw_sc_dev *dev = vsi->dev;
  789. enum i40iw_status_code ret = 0;
  790. struct i40iw_puda_rsrc *rsrc;
  791. u32 pudasize;
  792. u32 sqwridsize, rqwridsize;
  793. struct i40iw_virt_mem *vmem;
  794. info->count = 1;
  795. pudasize = sizeof(struct i40iw_puda_rsrc);
  796. sqwridsize = info->sq_size * sizeof(struct i40iw_sq_uk_wr_trk_info);
  797. rqwridsize = info->rq_size * 8;
  798. switch (info->type) {
  799. case I40IW_PUDA_RSRC_TYPE_ILQ:
  800. vmem = &vsi->ilq_mem;
  801. break;
  802. case I40IW_PUDA_RSRC_TYPE_IEQ:
  803. vmem = &vsi->ieq_mem;
  804. break;
  805. default:
  806. return I40IW_NOT_SUPPORTED;
  807. }
  808. ret =
  809. i40iw_allocate_virt_mem(dev->hw, vmem,
  810. pudasize + sqwridsize + rqwridsize);
  811. if (ret)
  812. return ret;
  813. rsrc = (struct i40iw_puda_rsrc *)vmem->va;
  814. spin_lock_init(&rsrc->bufpool_lock);
  815. if (info->type == I40IW_PUDA_RSRC_TYPE_ILQ) {
  816. vsi->ilq = (struct i40iw_puda_rsrc *)vmem->va;
  817. vsi->ilq_count = info->count;
  818. rsrc->receive = info->receive;
  819. rsrc->xmit_complete = info->xmit_complete;
  820. } else {
  821. vmem = &vsi->ieq_mem;
  822. vsi->ieq_count = info->count;
  823. vsi->ieq = (struct i40iw_puda_rsrc *)vmem->va;
  824. rsrc->receive = i40iw_ieq_receive;
  825. rsrc->xmit_complete = i40iw_ieq_tx_compl;
  826. }
  827. rsrc->ceq_valid = info->ceq_valid;
  828. rsrc->type = info->type;
  829. rsrc->sq_wrtrk_array = (struct i40iw_sq_uk_wr_trk_info *)((u8 *)vmem->va + pudasize);
  830. rsrc->rq_wrid_array = (u64 *)((u8 *)vmem->va + pudasize + sqwridsize);
  831. /* Initialize all ieq lists */
  832. INIT_LIST_HEAD(&rsrc->bufpool);
  833. INIT_LIST_HEAD(&rsrc->txpend);
  834. rsrc->tx_wqe_avail_cnt = info->sq_size - 1;
  835. dev->iw_pd_ops->pd_init(dev, &rsrc->sc_pd, info->pd_id, -1);
  836. rsrc->qp_id = info->qp_id;
  837. rsrc->cq_id = info->cq_id;
  838. rsrc->sq_size = info->sq_size;
  839. rsrc->rq_size = info->rq_size;
  840. rsrc->cq_size = info->rq_size + info->sq_size;
  841. rsrc->buf_size = info->buf_size;
  842. rsrc->dev = dev;
  843. rsrc->vsi = vsi;
  844. ret = i40iw_puda_cq_create(rsrc);
  845. if (!ret) {
  846. rsrc->completion = PUDA_CQ_CREATED;
  847. ret = i40iw_puda_qp_create(rsrc);
  848. }
  849. if (ret) {
  850. i40iw_debug(dev, I40IW_DEBUG_PUDA, "[%s] error qp_create\n",
  851. __func__);
  852. goto error;
  853. }
  854. rsrc->completion = PUDA_QP_CREATED;
  855. ret = i40iw_puda_allocbufs(rsrc, info->tx_buf_cnt + info->rq_size);
  856. if (ret) {
  857. i40iw_debug(dev, I40IW_DEBUG_PUDA, "[%s] error alloc_buf\n",
  858. __func__);
  859. goto error;
  860. }
  861. rsrc->rxq_invalid_cnt = info->rq_size;
  862. ret = i40iw_puda_replenish_rq(rsrc, true);
  863. if (ret)
  864. goto error;
  865. if (info->type == I40IW_PUDA_RSRC_TYPE_IEQ) {
  866. if (!i40iw_init_hash_desc(&rsrc->hash_desc)) {
  867. rsrc->check_crc = true;
  868. rsrc->completion = PUDA_HASH_CRC_COMPLETE;
  869. ret = 0;
  870. }
  871. }
  872. dev->ccq_ops->ccq_arm(&rsrc->cq);
  873. return ret;
  874. error:
  875. i40iw_puda_dele_resources(vsi, info->type, false);
  876. return ret;
  877. }
  878. /**
  879. * i40iw_ilq_putback_rcvbuf - ilq buffer to put back on rq
  880. * @qp: ilq's qp resource
  881. * @wqe_idx: wqe index of completed rcvbuf
  882. */
  883. static void i40iw_ilq_putback_rcvbuf(struct i40iw_sc_qp *qp, u32 wqe_idx)
  884. {
  885. u64 *wqe;
  886. u64 offset24;
  887. wqe = qp->qp_uk.rq_base[wqe_idx].elem;
  888. get_64bit_val(wqe, 24, &offset24);
  889. offset24 = (offset24) ? 0 : LS_64(1, I40IWQPSQ_VALID);
  890. set_64bit_val(wqe, 24, offset24);
  891. }
  892. /**
  893. * i40iw_ieq_get_fpdu - given length return fpdu length
  894. * @length: length if fpdu
  895. */
  896. static u16 i40iw_ieq_get_fpdu_length(u16 length)
  897. {
  898. u16 fpdu_len;
  899. fpdu_len = length + I40IW_IEQ_MPA_FRAMING;
  900. fpdu_len = (fpdu_len + 3) & 0xfffffffc;
  901. return fpdu_len;
  902. }
  903. /**
  904. * i40iw_ieq_copy_to_txbuf - copydata from rcv buf to tx buf
  905. * @buf: rcv buffer with partial
  906. * @txbuf: tx buffer for sendign back
  907. * @buf_offset: rcv buffer offset to copy from
  908. * @txbuf_offset: at offset in tx buf to copy
  909. * @length: length of data to copy
  910. */
  911. static void i40iw_ieq_copy_to_txbuf(struct i40iw_puda_buf *buf,
  912. struct i40iw_puda_buf *txbuf,
  913. u16 buf_offset, u32 txbuf_offset,
  914. u32 length)
  915. {
  916. void *mem1 = (u8 *)buf->mem.va + buf_offset;
  917. void *mem2 = (u8 *)txbuf->mem.va + txbuf_offset;
  918. memcpy(mem2, mem1, length);
  919. }
  920. /**
  921. * i40iw_ieq_setup_tx_buf - setup tx buffer for partial handling
  922. * @buf: reeive buffer with partial
  923. * @txbuf: buffer to prepare
  924. */
  925. static void i40iw_ieq_setup_tx_buf(struct i40iw_puda_buf *buf,
  926. struct i40iw_puda_buf *txbuf)
  927. {
  928. txbuf->maclen = buf->maclen;
  929. txbuf->tcphlen = buf->tcphlen;
  930. txbuf->ipv4 = buf->ipv4;
  931. txbuf->hdrlen = buf->hdrlen;
  932. i40iw_ieq_copy_to_txbuf(buf, txbuf, 0, 0, buf->hdrlen);
  933. }
  934. /**
  935. * i40iw_ieq_check_first_buf - check if rcv buffer's seq is in range
  936. * @buf: receive exception buffer
  937. * @fps: first partial sequence number
  938. */
  939. static void i40iw_ieq_check_first_buf(struct i40iw_puda_buf *buf, u32 fps)
  940. {
  941. u32 offset;
  942. if (buf->seqnum < fps) {
  943. offset = fps - buf->seqnum;
  944. if (offset > buf->datalen)
  945. return;
  946. buf->data += offset;
  947. buf->datalen -= (u16)offset;
  948. buf->seqnum = fps;
  949. }
  950. }
  951. /**
  952. * i40iw_ieq_compl_pfpdu - write txbuf with full fpdu
  953. * @ieq: ieq resource
  954. * @rxlist: ieq's received buffer list
  955. * @pbufl: temporary list for buffers for fpddu
  956. * @txbuf: tx buffer for fpdu
  957. * @fpdu_len: total length of fpdu
  958. */
  959. static void i40iw_ieq_compl_pfpdu(struct i40iw_puda_rsrc *ieq,
  960. struct list_head *rxlist,
  961. struct list_head *pbufl,
  962. struct i40iw_puda_buf *txbuf,
  963. u16 fpdu_len)
  964. {
  965. struct i40iw_puda_buf *buf;
  966. u32 nextseqnum;
  967. u16 txoffset, bufoffset;
  968. buf = i40iw_puda_get_listbuf(pbufl);
  969. if (!buf)
  970. return;
  971. nextseqnum = buf->seqnum + fpdu_len;
  972. txbuf->totallen = buf->hdrlen + fpdu_len;
  973. txbuf->data = (u8 *)txbuf->mem.va + buf->hdrlen;
  974. i40iw_ieq_setup_tx_buf(buf, txbuf);
  975. txoffset = buf->hdrlen;
  976. bufoffset = (u16)(buf->data - (u8 *)buf->mem.va);
  977. do {
  978. if (buf->datalen >= fpdu_len) {
  979. /* copied full fpdu */
  980. i40iw_ieq_copy_to_txbuf(buf, txbuf, bufoffset, txoffset, fpdu_len);
  981. buf->datalen -= fpdu_len;
  982. buf->data += fpdu_len;
  983. buf->seqnum = nextseqnum;
  984. break;
  985. }
  986. /* copy partial fpdu */
  987. i40iw_ieq_copy_to_txbuf(buf, txbuf, bufoffset, txoffset, buf->datalen);
  988. txoffset += buf->datalen;
  989. fpdu_len -= buf->datalen;
  990. i40iw_puda_ret_bufpool(ieq, buf);
  991. buf = i40iw_puda_get_listbuf(pbufl);
  992. if (!buf)
  993. return;
  994. bufoffset = (u16)(buf->data - (u8 *)buf->mem.va);
  995. } while (1);
  996. /* last buffer on the list*/
  997. if (buf->datalen)
  998. list_add(&buf->list, rxlist);
  999. else
  1000. i40iw_puda_ret_bufpool(ieq, buf);
  1001. }
  1002. /**
  1003. * i40iw_ieq_create_pbufl - create buffer list for single fpdu
  1004. * @rxlist: resource list for receive ieq buffes
  1005. * @pbufl: temp. list for buffers for fpddu
  1006. * @buf: first receive buffer
  1007. * @fpdu_len: total length of fpdu
  1008. */
  1009. static enum i40iw_status_code i40iw_ieq_create_pbufl(
  1010. struct i40iw_pfpdu *pfpdu,
  1011. struct list_head *rxlist,
  1012. struct list_head *pbufl,
  1013. struct i40iw_puda_buf *buf,
  1014. u16 fpdu_len)
  1015. {
  1016. enum i40iw_status_code status = 0;
  1017. struct i40iw_puda_buf *nextbuf;
  1018. u32 nextseqnum;
  1019. u16 plen = fpdu_len - buf->datalen;
  1020. bool done = false;
  1021. nextseqnum = buf->seqnum + buf->datalen;
  1022. do {
  1023. nextbuf = i40iw_puda_get_listbuf(rxlist);
  1024. if (!nextbuf) {
  1025. status = I40IW_ERR_list_empty;
  1026. break;
  1027. }
  1028. list_add_tail(&nextbuf->list, pbufl);
  1029. if (nextbuf->seqnum != nextseqnum) {
  1030. pfpdu->bad_seq_num++;
  1031. status = I40IW_ERR_SEQ_NUM;
  1032. break;
  1033. }
  1034. if (nextbuf->datalen >= plen) {
  1035. done = true;
  1036. } else {
  1037. plen -= nextbuf->datalen;
  1038. nextseqnum = nextbuf->seqnum + nextbuf->datalen;
  1039. }
  1040. } while (!done);
  1041. return status;
  1042. }
  1043. /**
  1044. * i40iw_ieq_handle_partial - process partial fpdu buffer
  1045. * @ieq: ieq resource
  1046. * @pfpdu: partial management per user qp
  1047. * @buf: receive buffer
  1048. * @fpdu_len: fpdu len in the buffer
  1049. */
  1050. static enum i40iw_status_code i40iw_ieq_handle_partial(struct i40iw_puda_rsrc *ieq,
  1051. struct i40iw_pfpdu *pfpdu,
  1052. struct i40iw_puda_buf *buf,
  1053. u16 fpdu_len)
  1054. {
  1055. enum i40iw_status_code status = 0;
  1056. u8 *crcptr;
  1057. u32 mpacrc;
  1058. u32 seqnum = buf->seqnum;
  1059. struct list_head pbufl; /* partial buffer list */
  1060. struct i40iw_puda_buf *txbuf = NULL;
  1061. struct list_head *rxlist = &pfpdu->rxlist;
  1062. INIT_LIST_HEAD(&pbufl);
  1063. list_add(&buf->list, &pbufl);
  1064. status = i40iw_ieq_create_pbufl(pfpdu, rxlist, &pbufl, buf, fpdu_len);
  1065. if (status)
  1066. goto error;
  1067. txbuf = i40iw_puda_get_bufpool(ieq);
  1068. if (!txbuf) {
  1069. pfpdu->no_tx_bufs++;
  1070. status = I40IW_ERR_NO_TXBUFS;
  1071. goto error;
  1072. }
  1073. i40iw_ieq_compl_pfpdu(ieq, rxlist, &pbufl, txbuf, fpdu_len);
  1074. i40iw_ieq_update_tcpip_info(txbuf, fpdu_len, seqnum);
  1075. crcptr = txbuf->data + fpdu_len - 4;
  1076. mpacrc = *(u32 *)crcptr;
  1077. if (ieq->check_crc) {
  1078. status = i40iw_ieq_check_mpacrc(ieq->hash_desc, txbuf->data,
  1079. (fpdu_len - 4), mpacrc);
  1080. if (status) {
  1081. i40iw_debug(ieq->dev, I40IW_DEBUG_IEQ,
  1082. "%s: error bad crc\n", __func__);
  1083. goto error;
  1084. }
  1085. }
  1086. i40iw_debug_buf(ieq->dev, I40IW_DEBUG_IEQ, "IEQ TX BUFFER",
  1087. txbuf->mem.va, txbuf->totallen);
  1088. i40iw_puda_send_buf(ieq, txbuf);
  1089. pfpdu->rcv_nxt = seqnum + fpdu_len;
  1090. return status;
  1091. error:
  1092. while (!list_empty(&pbufl)) {
  1093. buf = (struct i40iw_puda_buf *)(pbufl.prev);
  1094. list_del(&buf->list);
  1095. list_add(&buf->list, rxlist);
  1096. }
  1097. if (txbuf)
  1098. i40iw_puda_ret_bufpool(ieq, txbuf);
  1099. return status;
  1100. }
  1101. /**
  1102. * i40iw_ieq_process_buf - process buffer rcvd for ieq
  1103. * @ieq: ieq resource
  1104. * @pfpdu: partial management per user qp
  1105. * @buf: receive buffer
  1106. */
  1107. static enum i40iw_status_code i40iw_ieq_process_buf(struct i40iw_puda_rsrc *ieq,
  1108. struct i40iw_pfpdu *pfpdu,
  1109. struct i40iw_puda_buf *buf)
  1110. {
  1111. u16 fpdu_len = 0;
  1112. u16 datalen = buf->datalen;
  1113. u8 *datap = buf->data;
  1114. u8 *crcptr;
  1115. u16 ioffset = 0;
  1116. u32 mpacrc;
  1117. u32 seqnum = buf->seqnum;
  1118. u16 length = 0;
  1119. u16 full = 0;
  1120. bool partial = false;
  1121. struct i40iw_puda_buf *txbuf;
  1122. struct list_head *rxlist = &pfpdu->rxlist;
  1123. enum i40iw_status_code ret = 0;
  1124. enum i40iw_status_code status = 0;
  1125. ioffset = (u16)(buf->data - (u8 *)buf->mem.va);
  1126. while (datalen) {
  1127. fpdu_len = i40iw_ieq_get_fpdu_length(ntohs(*(__be16 *)datap));
  1128. if (fpdu_len > pfpdu->max_fpdu_data) {
  1129. i40iw_debug(ieq->dev, I40IW_DEBUG_IEQ,
  1130. "%s: error bad fpdu_len\n", __func__);
  1131. status = I40IW_ERR_MPA_CRC;
  1132. list_add(&buf->list, rxlist);
  1133. return status;
  1134. }
  1135. if (datalen < fpdu_len) {
  1136. partial = true;
  1137. break;
  1138. }
  1139. crcptr = datap + fpdu_len - 4;
  1140. mpacrc = *(u32 *)crcptr;
  1141. if (ieq->check_crc)
  1142. ret = i40iw_ieq_check_mpacrc(ieq->hash_desc,
  1143. datap, fpdu_len - 4, mpacrc);
  1144. if (ret) {
  1145. status = I40IW_ERR_MPA_CRC;
  1146. list_add(&buf->list, rxlist);
  1147. return status;
  1148. }
  1149. full++;
  1150. pfpdu->fpdu_processed++;
  1151. datap += fpdu_len;
  1152. length += fpdu_len;
  1153. datalen -= fpdu_len;
  1154. }
  1155. if (full) {
  1156. /* copy full pdu's in the txbuf and send them out */
  1157. txbuf = i40iw_puda_get_bufpool(ieq);
  1158. if (!txbuf) {
  1159. pfpdu->no_tx_bufs++;
  1160. status = I40IW_ERR_NO_TXBUFS;
  1161. list_add(&buf->list, rxlist);
  1162. return status;
  1163. }
  1164. /* modify txbuf's buffer header */
  1165. i40iw_ieq_setup_tx_buf(buf, txbuf);
  1166. /* copy full fpdu's to new buffer */
  1167. i40iw_ieq_copy_to_txbuf(buf, txbuf, ioffset, buf->hdrlen,
  1168. length);
  1169. txbuf->totallen = buf->hdrlen + length;
  1170. i40iw_ieq_update_tcpip_info(txbuf, length, buf->seqnum);
  1171. i40iw_puda_send_buf(ieq, txbuf);
  1172. if (!datalen) {
  1173. pfpdu->rcv_nxt = buf->seqnum + length;
  1174. i40iw_puda_ret_bufpool(ieq, buf);
  1175. return status;
  1176. }
  1177. buf->data = datap;
  1178. buf->seqnum = seqnum + length;
  1179. buf->datalen = datalen;
  1180. pfpdu->rcv_nxt = buf->seqnum;
  1181. }
  1182. if (partial)
  1183. status = i40iw_ieq_handle_partial(ieq, pfpdu, buf, fpdu_len);
  1184. return status;
  1185. }
  1186. /**
  1187. * i40iw_ieq_process_fpdus - process fpdu's buffers on its list
  1188. * @qp: qp for which partial fpdus
  1189. * @ieq: ieq resource
  1190. */
  1191. static void i40iw_ieq_process_fpdus(struct i40iw_sc_qp *qp,
  1192. struct i40iw_puda_rsrc *ieq)
  1193. {
  1194. struct i40iw_pfpdu *pfpdu = &qp->pfpdu;
  1195. struct list_head *rxlist = &pfpdu->rxlist;
  1196. struct i40iw_puda_buf *buf;
  1197. enum i40iw_status_code status;
  1198. do {
  1199. if (list_empty(rxlist))
  1200. break;
  1201. buf = i40iw_puda_get_listbuf(rxlist);
  1202. if (!buf) {
  1203. i40iw_debug(ieq->dev, I40IW_DEBUG_IEQ,
  1204. "%s: error no buf\n", __func__);
  1205. break;
  1206. }
  1207. if (buf->seqnum != pfpdu->rcv_nxt) {
  1208. /* This could be out of order or missing packet */
  1209. pfpdu->out_of_order++;
  1210. list_add(&buf->list, rxlist);
  1211. break;
  1212. }
  1213. /* keep processing buffers from the head of the list */
  1214. status = i40iw_ieq_process_buf(ieq, pfpdu, buf);
  1215. if (status == I40IW_ERR_MPA_CRC) {
  1216. pfpdu->mpa_crc_err = true;
  1217. while (!list_empty(rxlist)) {
  1218. buf = i40iw_puda_get_listbuf(rxlist);
  1219. i40iw_puda_ret_bufpool(ieq, buf);
  1220. pfpdu->crc_err++;
  1221. }
  1222. /* create CQP for AE */
  1223. i40iw_ieq_mpa_crc_ae(ieq->dev, qp);
  1224. }
  1225. } while (!status);
  1226. }
  1227. /**
  1228. * i40iw_ieq_handle_exception - handle qp's exception
  1229. * @ieq: ieq resource
  1230. * @qp: qp receiving excpetion
  1231. * @buf: receive buffer
  1232. */
  1233. static void i40iw_ieq_handle_exception(struct i40iw_puda_rsrc *ieq,
  1234. struct i40iw_sc_qp *qp,
  1235. struct i40iw_puda_buf *buf)
  1236. {
  1237. struct i40iw_puda_buf *tmpbuf = NULL;
  1238. struct i40iw_pfpdu *pfpdu = &qp->pfpdu;
  1239. u32 *hw_host_ctx = (u32 *)qp->hw_host_ctx;
  1240. u32 rcv_wnd = hw_host_ctx[23];
  1241. /* first partial seq # in q2 */
  1242. u32 fps = *(u32 *)(qp->q2_buf + Q2_FPSN_OFFSET);
  1243. struct list_head *rxlist = &pfpdu->rxlist;
  1244. struct list_head *plist;
  1245. pfpdu->total_ieq_bufs++;
  1246. if (pfpdu->mpa_crc_err) {
  1247. pfpdu->crc_err++;
  1248. goto error;
  1249. }
  1250. if (pfpdu->mode && (fps != pfpdu->fps)) {
  1251. /* clean up qp as it is new partial sequence */
  1252. i40iw_ieq_cleanup_qp(ieq, qp);
  1253. i40iw_debug(ieq->dev, I40IW_DEBUG_IEQ,
  1254. "%s: restarting new partial\n", __func__);
  1255. pfpdu->mode = false;
  1256. }
  1257. if (!pfpdu->mode) {
  1258. i40iw_debug_buf(ieq->dev, I40IW_DEBUG_IEQ, "Q2 BUFFER", (u64 *)qp->q2_buf, 128);
  1259. /* First_Partial_Sequence_Number check */
  1260. pfpdu->rcv_nxt = fps;
  1261. pfpdu->fps = fps;
  1262. pfpdu->mode = true;
  1263. pfpdu->max_fpdu_data = ieq->vsi->mss;
  1264. pfpdu->pmode_count++;
  1265. INIT_LIST_HEAD(rxlist);
  1266. i40iw_ieq_check_first_buf(buf, fps);
  1267. }
  1268. if (!(rcv_wnd >= (buf->seqnum - pfpdu->rcv_nxt))) {
  1269. pfpdu->bad_seq_num++;
  1270. goto error;
  1271. }
  1272. if (!list_empty(rxlist)) {
  1273. tmpbuf = (struct i40iw_puda_buf *)rxlist->next;
  1274. while ((struct list_head *)tmpbuf != rxlist) {
  1275. if ((int)(buf->seqnum - tmpbuf->seqnum) < 0)
  1276. break;
  1277. plist = &tmpbuf->list;
  1278. tmpbuf = (struct i40iw_puda_buf *)plist->next;
  1279. }
  1280. /* Insert buf before tmpbuf */
  1281. list_add_tail(&buf->list, &tmpbuf->list);
  1282. } else {
  1283. list_add_tail(&buf->list, rxlist);
  1284. }
  1285. i40iw_ieq_process_fpdus(qp, ieq);
  1286. return;
  1287. error:
  1288. i40iw_puda_ret_bufpool(ieq, buf);
  1289. }
  1290. /**
  1291. * i40iw_ieq_receive - received exception buffer
  1292. * @dev: iwarp device
  1293. * @buf: exception buffer received
  1294. */
  1295. static void i40iw_ieq_receive(struct i40iw_sc_vsi *vsi,
  1296. struct i40iw_puda_buf *buf)
  1297. {
  1298. struct i40iw_puda_rsrc *ieq = vsi->ieq;
  1299. struct i40iw_sc_qp *qp = NULL;
  1300. u32 wqe_idx = ieq->compl_rxwqe_idx;
  1301. qp = i40iw_ieq_get_qp(vsi->dev, buf);
  1302. if (!qp) {
  1303. ieq->stats_bad_qp_id++;
  1304. i40iw_puda_ret_bufpool(ieq, buf);
  1305. } else {
  1306. i40iw_ieq_handle_exception(ieq, qp, buf);
  1307. }
  1308. /*
  1309. * ieq->rx_wqe_idx is used by i40iw_puda_replenish_rq()
  1310. * on which wqe_idx to start replenish rq
  1311. */
  1312. if (!ieq->rxq_invalid_cnt)
  1313. ieq->rx_wqe_idx = wqe_idx;
  1314. ieq->rxq_invalid_cnt++;
  1315. }
  1316. /**
  1317. * i40iw_ieq_tx_compl - put back after sending completed exception buffer
  1318. * @vsi: pointer to the vsi structure
  1319. * @sqwrid: pointer to puda buffer
  1320. */
  1321. static void i40iw_ieq_tx_compl(struct i40iw_sc_vsi *vsi, void *sqwrid)
  1322. {
  1323. struct i40iw_puda_rsrc *ieq = vsi->ieq;
  1324. struct i40iw_puda_buf *buf = (struct i40iw_puda_buf *)sqwrid;
  1325. i40iw_puda_ret_bufpool(ieq, buf);
  1326. if (!list_empty(&ieq->txpend)) {
  1327. buf = i40iw_puda_get_listbuf(&ieq->txpend);
  1328. i40iw_puda_send_buf(ieq, buf);
  1329. }
  1330. }
  1331. /**
  1332. * i40iw_ieq_cleanup_qp - qp is being destroyed
  1333. * @ieq: ieq resource
  1334. * @qp: all pending fpdu buffers
  1335. */
  1336. void i40iw_ieq_cleanup_qp(struct i40iw_puda_rsrc *ieq, struct i40iw_sc_qp *qp)
  1337. {
  1338. struct i40iw_puda_buf *buf;
  1339. struct i40iw_pfpdu *pfpdu = &qp->pfpdu;
  1340. struct list_head *rxlist = &pfpdu->rxlist;
  1341. if (!pfpdu->mode)
  1342. return;
  1343. while (!list_empty(rxlist)) {
  1344. buf = i40iw_puda_get_listbuf(rxlist);
  1345. i40iw_puda_ret_bufpool(ieq, buf);
  1346. }
  1347. }