i40iw_puda.c 39 KB

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