i40iw_puda.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493
  1. /*******************************************************************************
  2. *
  3. * Copyright (c) 2015-2016 Intel Corporation. All rights reserved.
  4. *
  5. * This software is available to you under a choice of one of two
  6. * licenses. You may choose to be licensed under the terms of the GNU
  7. * General Public License (GPL) Version 2, available from the file
  8. * COPYING in the main directory of this source tree, or the
  9. * OpenFabrics.org BSD license below:
  10. *
  11. * Redistribution and use in source and binary forms, with or
  12. * without modification, are permitted provided that the following
  13. * conditions are met:
  14. *
  15. * - Redistributions of source code must retain the above
  16. * copyright notice, this list of conditions and the following
  17. * disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials
  22. * provided with the distribution.
  23. *
  24. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  25. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  26. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  27. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  28. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  29. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  30. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  31. * SOFTWARE.
  32. *
  33. *******************************************************************************/
  34. #include "i40iw_osdep.h"
  35. #include "i40iw_register.h"
  36. #include "i40iw_status.h"
  37. #include "i40iw_hmc.h"
  38. #include "i40iw_d.h"
  39. #include "i40iw_type.h"
  40. #include "i40iw_p.h"
  41. #include "i40iw_puda.h"
  42. static void i40iw_ieq_receive(struct i40iw_sc_vsi *vsi,
  43. struct i40iw_puda_buf *buf);
  44. static void i40iw_ieq_tx_compl(struct i40iw_sc_vsi *vsi, void *sqwrid);
  45. static void i40iw_ilq_putback_rcvbuf(struct i40iw_sc_qp *qp, u32 wqe_idx);
  46. static enum i40iw_status_code i40iw_puda_replenish_rq(struct i40iw_puda_rsrc
  47. *rsrc, bool initial);
  48. static void i40iw_ieq_cleanup_qp(struct i40iw_puda_rsrc *ieq, struct i40iw_sc_qp *qp);
  49. /**
  50. * i40iw_puda_get_listbuf - get buffer from puda list
  51. * @list: list to use for buffers (ILQ or IEQ)
  52. */
  53. static struct i40iw_puda_buf *i40iw_puda_get_listbuf(struct list_head *list)
  54. {
  55. struct i40iw_puda_buf *buf = NULL;
  56. if (!list_empty(list)) {
  57. buf = (struct i40iw_puda_buf *)list->next;
  58. list_del((struct list_head *)&buf->list);
  59. }
  60. return buf;
  61. }
  62. /**
  63. * i40iw_puda_get_bufpool - return buffer from resource
  64. * @rsrc: resource to use for buffer
  65. */
  66. struct i40iw_puda_buf *i40iw_puda_get_bufpool(struct i40iw_puda_rsrc *rsrc)
  67. {
  68. struct i40iw_puda_buf *buf = NULL;
  69. struct list_head *list = &rsrc->bufpool;
  70. unsigned long flags;
  71. spin_lock_irqsave(&rsrc->bufpool_lock, flags);
  72. buf = i40iw_puda_get_listbuf(list);
  73. if (buf)
  74. rsrc->avail_buf_count--;
  75. else
  76. rsrc->stats_buf_alloc_fail++;
  77. spin_unlock_irqrestore(&rsrc->bufpool_lock, flags);
  78. return buf;
  79. }
  80. /**
  81. * i40iw_puda_ret_bufpool - return buffer to rsrc list
  82. * @rsrc: resource to use for buffer
  83. * @buf: buffe to return to resouce
  84. */
  85. void i40iw_puda_ret_bufpool(struct i40iw_puda_rsrc *rsrc,
  86. struct i40iw_puda_buf *buf)
  87. {
  88. unsigned long flags;
  89. spin_lock_irqsave(&rsrc->bufpool_lock, flags);
  90. list_add(&buf->list, &rsrc->bufpool);
  91. spin_unlock_irqrestore(&rsrc->bufpool_lock, flags);
  92. rsrc->avail_buf_count++;
  93. }
  94. /**
  95. * i40iw_puda_post_recvbuf - set wqe for rcv buffer
  96. * @rsrc: resource ptr
  97. * @wqe_idx: wqe index to use
  98. * @buf: puda buffer for rcv q
  99. * @initial: flag if during init time
  100. */
  101. static void i40iw_puda_post_recvbuf(struct i40iw_puda_rsrc *rsrc, u32 wqe_idx,
  102. struct i40iw_puda_buf *buf, bool initial)
  103. {
  104. u64 *wqe;
  105. struct i40iw_sc_qp *qp = &rsrc->qp;
  106. u64 offset24 = 0;
  107. qp->qp_uk.rq_wrid_array[wqe_idx] = (uintptr_t)buf;
  108. wqe = qp->qp_uk.rq_base[wqe_idx].elem;
  109. i40iw_debug(rsrc->dev, I40IW_DEBUG_PUDA,
  110. "%s: wqe_idx= %d buf = %p wqe = %p\n", __func__,
  111. wqe_idx, buf, wqe);
  112. if (!initial)
  113. get_64bit_val(wqe, 24, &offset24);
  114. offset24 = (offset24) ? 0 : LS_64(1, I40IWQPSQ_VALID);
  115. set_64bit_val(wqe, 24, offset24);
  116. set_64bit_val(wqe, 0, buf->mem.pa);
  117. set_64bit_val(wqe, 8,
  118. LS_64(buf->mem.size, I40IWQPSQ_FRAG_LEN));
  119. set_64bit_val(wqe, 24, offset24);
  120. }
  121. /**
  122. * i40iw_puda_replenish_rq - post rcv buffers
  123. * @rsrc: resource to use for buffer
  124. * @initial: flag if during init time
  125. */
  126. static enum i40iw_status_code i40iw_puda_replenish_rq(struct i40iw_puda_rsrc *rsrc,
  127. bool initial)
  128. {
  129. u32 i;
  130. u32 invalid_cnt = rsrc->rxq_invalid_cnt;
  131. struct i40iw_puda_buf *buf = NULL;
  132. for (i = 0; i < invalid_cnt; i++) {
  133. buf = i40iw_puda_get_bufpool(rsrc);
  134. if (!buf)
  135. return I40IW_ERR_list_empty;
  136. i40iw_puda_post_recvbuf(rsrc, rsrc->rx_wqe_idx, buf,
  137. initial);
  138. rsrc->rx_wqe_idx =
  139. ((rsrc->rx_wqe_idx + 1) % rsrc->rq_size);
  140. rsrc->rxq_invalid_cnt--;
  141. }
  142. return 0;
  143. }
  144. /**
  145. * i40iw_puda_alloc_buf - allocate mem for buffer
  146. * @dev: iwarp device
  147. * @length: length of buffer
  148. */
  149. static struct i40iw_puda_buf *i40iw_puda_alloc_buf(struct i40iw_sc_dev *dev,
  150. u32 length)
  151. {
  152. struct i40iw_puda_buf *buf = NULL;
  153. struct i40iw_virt_mem buf_mem;
  154. enum i40iw_status_code ret;
  155. ret = i40iw_allocate_virt_mem(dev->hw, &buf_mem,
  156. sizeof(struct i40iw_puda_buf));
  157. if (ret) {
  158. i40iw_debug(dev, I40IW_DEBUG_PUDA,
  159. "%s: error mem for buf\n", __func__);
  160. return NULL;
  161. }
  162. buf = (struct i40iw_puda_buf *)buf_mem.va;
  163. ret = i40iw_allocate_dma_mem(dev->hw, &buf->mem, length, 1);
  164. if (ret) {
  165. i40iw_debug(dev, I40IW_DEBUG_PUDA,
  166. "%s: error dma mem for buf\n", __func__);
  167. i40iw_free_virt_mem(dev->hw, &buf_mem);
  168. return NULL;
  169. }
  170. buf->buf_mem.va = buf_mem.va;
  171. buf->buf_mem.size = buf_mem.size;
  172. return buf;
  173. }
  174. /**
  175. * i40iw_puda_dele_buf - delete buffer back to system
  176. * @dev: iwarp device
  177. * @buf: buffer to free
  178. */
  179. static void i40iw_puda_dele_buf(struct i40iw_sc_dev *dev,
  180. struct i40iw_puda_buf *buf)
  181. {
  182. i40iw_free_dma_mem(dev->hw, &buf->mem);
  183. i40iw_free_virt_mem(dev->hw, &buf->buf_mem);
  184. }
  185. /**
  186. * i40iw_puda_get_next_send_wqe - return next wqe for processing
  187. * @qp: puda qp for wqe
  188. * @wqe_idx: wqe index for caller
  189. */
  190. static u64 *i40iw_puda_get_next_send_wqe(struct i40iw_qp_uk *qp, u32 *wqe_idx)
  191. {
  192. u64 *wqe = NULL;
  193. enum i40iw_status_code ret_code = 0;
  194. *wqe_idx = I40IW_RING_GETCURRENT_HEAD(qp->sq_ring);
  195. if (!*wqe_idx)
  196. qp->swqe_polarity = !qp->swqe_polarity;
  197. I40IW_RING_MOVE_HEAD(qp->sq_ring, ret_code);
  198. if (ret_code)
  199. return wqe;
  200. wqe = qp->sq_base[*wqe_idx].elem;
  201. return wqe;
  202. }
  203. /**
  204. * i40iw_puda_poll_info - poll cq for completion
  205. * @cq: cq for poll
  206. * @info: info return for successful completion
  207. */
  208. static enum i40iw_status_code i40iw_puda_poll_info(struct i40iw_sc_cq *cq,
  209. struct i40iw_puda_completion_info *info)
  210. {
  211. u64 qword0, qword2, qword3;
  212. u64 *cqe;
  213. u64 comp_ctx;
  214. bool valid_bit;
  215. u32 major_err, minor_err;
  216. bool error;
  217. cqe = (u64 *)I40IW_GET_CURRENT_CQ_ELEMENT(&cq->cq_uk);
  218. get_64bit_val(cqe, 24, &qword3);
  219. valid_bit = (bool)RS_64(qword3, I40IW_CQ_VALID);
  220. if (valid_bit != cq->cq_uk.polarity)
  221. return I40IW_ERR_QUEUE_EMPTY;
  222. i40iw_debug_buf(cq->dev, I40IW_DEBUG_PUDA, "PUDA CQE", cqe, 32);
  223. error = (bool)RS_64(qword3, I40IW_CQ_ERROR);
  224. if (error) {
  225. i40iw_debug(cq->dev, I40IW_DEBUG_PUDA, "%s receive error\n", __func__);
  226. major_err = (u32)(RS_64(qword3, I40IW_CQ_MAJERR));
  227. minor_err = (u32)(RS_64(qword3, I40IW_CQ_MINERR));
  228. info->compl_error = major_err << 16 | minor_err;
  229. return I40IW_ERR_CQ_COMPL_ERROR;
  230. }
  231. get_64bit_val(cqe, 0, &qword0);
  232. get_64bit_val(cqe, 16, &qword2);
  233. info->q_type = (u8)RS_64(qword3, I40IW_CQ_SQ);
  234. info->qp_id = (u32)RS_64(qword2, I40IWCQ_QPID);
  235. get_64bit_val(cqe, 8, &comp_ctx);
  236. info->qp = (struct i40iw_qp_uk *)(unsigned long)comp_ctx;
  237. info->wqe_idx = (u32)RS_64(qword3, I40IW_CQ_WQEIDX);
  238. if (info->q_type == I40IW_CQE_QTYPE_RQ) {
  239. info->vlan_valid = (bool)RS_64(qword3, I40IW_VLAN_TAG_VALID);
  240. info->l4proto = (u8)RS_64(qword2, I40IW_UDA_L4PROTO);
  241. info->l3proto = (u8)RS_64(qword2, I40IW_UDA_L3PROTO);
  242. info->payload_len = (u16)RS_64(qword0, I40IW_UDA_PAYLOADLEN);
  243. }
  244. return 0;
  245. }
  246. /**
  247. * i40iw_puda_poll_completion - processes completion for cq
  248. * @dev: iwarp device
  249. * @cq: cq getting interrupt
  250. * @compl_err: return any completion err
  251. */
  252. enum i40iw_status_code i40iw_puda_poll_completion(struct i40iw_sc_dev *dev,
  253. struct i40iw_sc_cq *cq, u32 *compl_err)
  254. {
  255. struct i40iw_qp_uk *qp;
  256. struct i40iw_cq_uk *cq_uk = &cq->cq_uk;
  257. struct i40iw_puda_completion_info info;
  258. enum i40iw_status_code ret = 0;
  259. struct i40iw_puda_buf *buf;
  260. struct i40iw_puda_rsrc *rsrc;
  261. void *sqwrid;
  262. u8 cq_type = cq->cq_type;
  263. unsigned long flags;
  264. if ((cq_type == I40IW_CQ_TYPE_ILQ) || (cq_type == I40IW_CQ_TYPE_IEQ)) {
  265. rsrc = (cq_type == I40IW_CQ_TYPE_ILQ) ? cq->vsi->ilq : cq->vsi->ieq;
  266. } else {
  267. i40iw_debug(dev, I40IW_DEBUG_PUDA, "%s qp_type error\n", __func__);
  268. return I40IW_ERR_BAD_PTR;
  269. }
  270. memset(&info, 0, sizeof(info));
  271. ret = i40iw_puda_poll_info(cq, &info);
  272. *compl_err = info.compl_error;
  273. if (ret == I40IW_ERR_QUEUE_EMPTY)
  274. return ret;
  275. if (ret)
  276. goto done;
  277. qp = info.qp;
  278. if (!qp || !rsrc) {
  279. ret = I40IW_ERR_BAD_PTR;
  280. goto done;
  281. }
  282. if (qp->qp_id != rsrc->qp_id) {
  283. ret = I40IW_ERR_BAD_PTR;
  284. goto done;
  285. }
  286. if (info.q_type == I40IW_CQE_QTYPE_RQ) {
  287. buf = (struct i40iw_puda_buf *)(uintptr_t)qp->rq_wrid_array[info.wqe_idx];
  288. /* Get all the tcpip information in the buf header */
  289. ret = i40iw_puda_get_tcpip_info(&info, buf);
  290. if (ret) {
  291. rsrc->stats_rcvd_pkt_err++;
  292. if (cq_type == I40IW_CQ_TYPE_ILQ) {
  293. i40iw_ilq_putback_rcvbuf(&rsrc->qp,
  294. info.wqe_idx);
  295. } else {
  296. i40iw_puda_ret_bufpool(rsrc, buf);
  297. i40iw_puda_replenish_rq(rsrc, false);
  298. }
  299. goto done;
  300. }
  301. rsrc->stats_pkt_rcvd++;
  302. rsrc->compl_rxwqe_idx = info.wqe_idx;
  303. i40iw_debug(dev, I40IW_DEBUG_PUDA, "%s RQ completion\n", __func__);
  304. rsrc->receive(rsrc->vsi, buf);
  305. if (cq_type == I40IW_CQ_TYPE_ILQ)
  306. i40iw_ilq_putback_rcvbuf(&rsrc->qp, info.wqe_idx);
  307. else
  308. i40iw_puda_replenish_rq(rsrc, false);
  309. } else {
  310. i40iw_debug(dev, I40IW_DEBUG_PUDA, "%s SQ completion\n", __func__);
  311. sqwrid = (void *)(uintptr_t)qp->sq_wrtrk_array[info.wqe_idx].wrid;
  312. I40IW_RING_SET_TAIL(qp->sq_ring, info.wqe_idx);
  313. rsrc->xmit_complete(rsrc->vsi, sqwrid);
  314. spin_lock_irqsave(&rsrc->bufpool_lock, flags);
  315. rsrc->tx_wqe_avail_cnt++;
  316. spin_unlock_irqrestore(&rsrc->bufpool_lock, flags);
  317. if (!list_empty(&rsrc->vsi->ilq->txpend))
  318. i40iw_puda_send_buf(rsrc->vsi->ilq, NULL);
  319. }
  320. done:
  321. I40IW_RING_MOVE_HEAD(cq_uk->cq_ring, ret);
  322. if (I40IW_RING_GETCURRENT_HEAD(cq_uk->cq_ring) == 0)
  323. cq_uk->polarity = !cq_uk->polarity;
  324. /* update cq tail in cq shadow memory also */
  325. I40IW_RING_MOVE_TAIL(cq_uk->cq_ring);
  326. set_64bit_val(cq_uk->shadow_area, 0,
  327. I40IW_RING_GETCURRENT_HEAD(cq_uk->cq_ring));
  328. return 0;
  329. }
  330. /**
  331. * i40iw_puda_send - complete send wqe for transmit
  332. * @qp: puda qp for send
  333. * @info: buffer information for transmit
  334. */
  335. enum i40iw_status_code i40iw_puda_send(struct i40iw_sc_qp *qp,
  336. struct i40iw_puda_send_info *info)
  337. {
  338. u64 *wqe;
  339. u32 iplen, l4len;
  340. u64 header[2];
  341. u32 wqe_idx;
  342. u8 iipt;
  343. /* number of 32 bits DWORDS in header */
  344. l4len = info->tcplen >> 2;
  345. if (info->ipv4) {
  346. iipt = 3;
  347. iplen = 5;
  348. } else {
  349. iipt = 1;
  350. iplen = 10;
  351. }
  352. wqe = i40iw_puda_get_next_send_wqe(&qp->qp_uk, &wqe_idx);
  353. if (!wqe)
  354. return I40IW_ERR_QP_TOOMANY_WRS_POSTED;
  355. qp->qp_uk.sq_wrtrk_array[wqe_idx].wrid = (uintptr_t)info->scratch;
  356. /* Third line of WQE descriptor */
  357. /* maclen is in words */
  358. header[0] = LS_64((info->maclen >> 1), I40IW_UDA_QPSQ_MACLEN) |
  359. LS_64(iplen, I40IW_UDA_QPSQ_IPLEN) | LS_64(1, I40IW_UDA_QPSQ_L4T) |
  360. LS_64(iipt, I40IW_UDA_QPSQ_IIPT) |
  361. LS_64(l4len, I40IW_UDA_QPSQ_L4LEN);
  362. /* Forth line of WQE descriptor */
  363. header[1] = LS_64(I40IW_OP_TYPE_SEND, I40IW_UDA_QPSQ_OPCODE) |
  364. LS_64(1, I40IW_UDA_QPSQ_SIGCOMPL) |
  365. LS_64(info->doloopback, I40IW_UDA_QPSQ_DOLOOPBACK) |
  366. LS_64(qp->qp_uk.swqe_polarity, I40IW_UDA_QPSQ_VALID);
  367. set_64bit_val(wqe, 0, info->paddr);
  368. set_64bit_val(wqe, 8, LS_64(info->len, I40IWQPSQ_FRAG_LEN));
  369. set_64bit_val(wqe, 16, header[0]);
  370. set_64bit_val(wqe, 24, header[1]);
  371. i40iw_debug_buf(qp->dev, I40IW_DEBUG_PUDA, "PUDA SEND WQE", wqe, 32);
  372. i40iw_qp_post_wr(&qp->qp_uk);
  373. return 0;
  374. }
  375. /**
  376. * i40iw_puda_send_buf - transmit puda buffer
  377. * @rsrc: resource to use for buffer
  378. * @buf: puda buffer to transmit
  379. */
  380. void i40iw_puda_send_buf(struct i40iw_puda_rsrc *rsrc, struct i40iw_puda_buf *buf)
  381. {
  382. struct i40iw_puda_send_info info;
  383. enum i40iw_status_code ret = 0;
  384. unsigned long flags;
  385. spin_lock_irqsave(&rsrc->bufpool_lock, flags);
  386. /* if no wqe available or not from a completion and we have
  387. * pending buffers, we must queue new buffer
  388. */
  389. if (!rsrc->tx_wqe_avail_cnt || (buf && !list_empty(&rsrc->txpend))) {
  390. list_add_tail(&buf->list, &rsrc->txpend);
  391. spin_unlock_irqrestore(&rsrc->bufpool_lock, flags);
  392. rsrc->stats_sent_pkt_q++;
  393. if (rsrc->type == I40IW_PUDA_RSRC_TYPE_ILQ)
  394. i40iw_debug(rsrc->dev, I40IW_DEBUG_PUDA,
  395. "%s: adding to txpend\n", __func__);
  396. return;
  397. }
  398. rsrc->tx_wqe_avail_cnt--;
  399. /* if we are coming from a completion and have pending buffers
  400. * then Get one from pending list
  401. */
  402. if (!buf) {
  403. buf = i40iw_puda_get_listbuf(&rsrc->txpend);
  404. if (!buf)
  405. goto done;
  406. }
  407. info.scratch = (void *)buf;
  408. info.paddr = buf->mem.pa;
  409. info.len = buf->totallen;
  410. info.tcplen = buf->tcphlen;
  411. info.maclen = buf->maclen;
  412. info.ipv4 = buf->ipv4;
  413. info.doloopback = (rsrc->type == I40IW_PUDA_RSRC_TYPE_IEQ);
  414. ret = i40iw_puda_send(&rsrc->qp, &info);
  415. if (ret) {
  416. rsrc->tx_wqe_avail_cnt++;
  417. rsrc->stats_sent_pkt_q++;
  418. list_add(&buf->list, &rsrc->txpend);
  419. if (rsrc->type == I40IW_PUDA_RSRC_TYPE_ILQ)
  420. i40iw_debug(rsrc->dev, I40IW_DEBUG_PUDA,
  421. "%s: adding to puda_send\n", __func__);
  422. } else {
  423. rsrc->stats_pkt_sent++;
  424. }
  425. done:
  426. spin_unlock_irqrestore(&rsrc->bufpool_lock, flags);
  427. }
  428. /**
  429. * i40iw_puda_qp_setctx - during init, set qp's context
  430. * @rsrc: qp's resource
  431. */
  432. static void i40iw_puda_qp_setctx(struct i40iw_puda_rsrc *rsrc)
  433. {
  434. struct i40iw_sc_qp *qp = &rsrc->qp;
  435. u64 *qp_ctx = qp->hw_host_ctx;
  436. set_64bit_val(qp_ctx, 8, qp->sq_pa);
  437. set_64bit_val(qp_ctx, 16, qp->rq_pa);
  438. set_64bit_val(qp_ctx, 24,
  439. LS_64(qp->hw_rq_size, I40IWQPC_RQSIZE) |
  440. LS_64(qp->hw_sq_size, I40IWQPC_SQSIZE));
  441. set_64bit_val(qp_ctx, 48, LS_64(1514, I40IWQPC_SNDMSS));
  442. set_64bit_val(qp_ctx, 56, 0);
  443. set_64bit_val(qp_ctx, 64, 1);
  444. set_64bit_val(qp_ctx, 136,
  445. LS_64(rsrc->cq_id, I40IWQPC_TXCQNUM) |
  446. LS_64(rsrc->cq_id, I40IWQPC_RXCQNUM));
  447. set_64bit_val(qp_ctx, 160, LS_64(1, I40IWQPC_PRIVEN));
  448. set_64bit_val(qp_ctx, 168,
  449. LS_64((uintptr_t)qp, I40IWQPC_QPCOMPCTX));
  450. set_64bit_val(qp_ctx, 176,
  451. LS_64(qp->sq_tph_val, I40IWQPC_SQTPHVAL) |
  452. LS_64(qp->rq_tph_val, I40IWQPC_RQTPHVAL) |
  453. LS_64(qp->qs_handle, I40IWQPC_QSHANDLE));
  454. i40iw_debug_buf(rsrc->dev, I40IW_DEBUG_PUDA, "PUDA QP CONTEXT",
  455. qp_ctx, I40IW_QP_CTX_SIZE);
  456. }
  457. /**
  458. * i40iw_puda_qp_wqe - setup wqe for qp create
  459. * @rsrc: resource for qp
  460. */
  461. static enum i40iw_status_code i40iw_puda_qp_wqe(struct i40iw_sc_dev *dev, struct i40iw_sc_qp *qp)
  462. {
  463. struct i40iw_sc_cqp *cqp;
  464. u64 *wqe;
  465. u64 header;
  466. struct i40iw_ccq_cqe_info compl_info;
  467. enum i40iw_status_code status = 0;
  468. cqp = dev->cqp;
  469. wqe = i40iw_sc_cqp_get_next_send_wqe(cqp, 0);
  470. if (!wqe)
  471. return I40IW_ERR_RING_FULL;
  472. set_64bit_val(wqe, 16, qp->hw_host_ctx_pa);
  473. set_64bit_val(wqe, 40, qp->shadow_area_pa);
  474. header = qp->qp_uk.qp_id |
  475. LS_64(I40IW_CQP_OP_CREATE_QP, I40IW_CQPSQ_OPCODE) |
  476. LS_64(I40IW_QP_TYPE_UDA, I40IW_CQPSQ_QP_QPTYPE) |
  477. LS_64(1, I40IW_CQPSQ_QP_CQNUMVALID) |
  478. LS_64(2, I40IW_CQPSQ_QP_NEXTIWSTATE) |
  479. LS_64(cqp->polarity, I40IW_CQPSQ_WQEVALID);
  480. set_64bit_val(wqe, 24, header);
  481. i40iw_debug_buf(cqp->dev, I40IW_DEBUG_PUDA, "PUDA CQE", wqe, 32);
  482. i40iw_sc_cqp_post_sq(cqp);
  483. status = dev->cqp_ops->poll_for_cqp_op_done(dev->cqp,
  484. I40IW_CQP_OP_CREATE_QP,
  485. &compl_info);
  486. return status;
  487. }
  488. /**
  489. * i40iw_puda_qp_create - create qp for resource
  490. * @rsrc: resource to use for buffer
  491. */
  492. static enum i40iw_status_code i40iw_puda_qp_create(struct i40iw_puda_rsrc *rsrc)
  493. {
  494. struct i40iw_sc_qp *qp = &rsrc->qp;
  495. struct i40iw_qp_uk *ukqp = &qp->qp_uk;
  496. enum i40iw_status_code ret = 0;
  497. u32 sq_size, rq_size, t_size;
  498. struct i40iw_dma_mem *mem;
  499. sq_size = rsrc->sq_size * I40IW_QP_WQE_MIN_SIZE;
  500. rq_size = rsrc->rq_size * I40IW_QP_WQE_MIN_SIZE;
  501. t_size = (sq_size + rq_size + (I40IW_SHADOW_AREA_SIZE << 3) +
  502. I40IW_QP_CTX_SIZE);
  503. /* Get page aligned memory */
  504. ret =
  505. i40iw_allocate_dma_mem(rsrc->dev->hw, &rsrc->qpmem, t_size,
  506. I40IW_HW_PAGE_SIZE);
  507. if (ret) {
  508. i40iw_debug(rsrc->dev, I40IW_DEBUG_PUDA, "%s: error dma mem\n", __func__);
  509. return ret;
  510. }
  511. mem = &rsrc->qpmem;
  512. memset(mem->va, 0, t_size);
  513. qp->hw_sq_size = i40iw_get_encoded_wqe_size(rsrc->sq_size, false);
  514. qp->hw_rq_size = i40iw_get_encoded_wqe_size(rsrc->rq_size, false);
  515. qp->pd = &rsrc->sc_pd;
  516. qp->qp_type = I40IW_QP_TYPE_UDA;
  517. qp->dev = rsrc->dev;
  518. qp->back_qp = (void *)rsrc;
  519. qp->sq_pa = mem->pa;
  520. qp->rq_pa = qp->sq_pa + sq_size;
  521. qp->vsi = rsrc->vsi;
  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->user_pri = 0;
  545. i40iw_qp_add_qos(qp);
  546. i40iw_puda_qp_setctx(rsrc);
  547. if (rsrc->ceq_valid)
  548. ret = i40iw_cqp_qp_create_cmd(rsrc->dev, qp);
  549. else
  550. ret = i40iw_puda_qp_wqe(rsrc->dev, qp);
  551. if (ret)
  552. i40iw_free_dma_mem(rsrc->dev->hw, &rsrc->qpmem);
  553. return ret;
  554. }
  555. /**
  556. * i40iw_puda_cq_wqe - setup wqe for cq create
  557. * @rsrc: resource for cq
  558. */
  559. static enum i40iw_status_code i40iw_puda_cq_wqe(struct i40iw_sc_dev *dev, struct i40iw_sc_cq *cq)
  560. {
  561. u64 *wqe;
  562. struct i40iw_sc_cqp *cqp;
  563. u64 header;
  564. struct i40iw_ccq_cqe_info compl_info;
  565. enum i40iw_status_code status = 0;
  566. cqp = dev->cqp;
  567. wqe = i40iw_sc_cqp_get_next_send_wqe(cqp, 0);
  568. if (!wqe)
  569. return I40IW_ERR_RING_FULL;
  570. set_64bit_val(wqe, 0, cq->cq_uk.cq_size);
  571. set_64bit_val(wqe, 8, RS_64_1(cq, 1));
  572. set_64bit_val(wqe, 16,
  573. LS_64(cq->shadow_read_threshold,
  574. I40IW_CQPSQ_CQ_SHADOW_READ_THRESHOLD));
  575. set_64bit_val(wqe, 32, cq->cq_pa);
  576. set_64bit_val(wqe, 40, cq->shadow_area_pa);
  577. header = cq->cq_uk.cq_id |
  578. LS_64(I40IW_CQP_OP_CREATE_CQ, I40IW_CQPSQ_OPCODE) |
  579. LS_64(1, I40IW_CQPSQ_CQ_CHKOVERFLOW) |
  580. LS_64(1, I40IW_CQPSQ_CQ_ENCEQEMASK) |
  581. LS_64(1, I40IW_CQPSQ_CQ_CEQIDVALID) |
  582. LS_64(cqp->polarity, I40IW_CQPSQ_WQEVALID);
  583. set_64bit_val(wqe, 24, header);
  584. i40iw_debug_buf(dev, I40IW_DEBUG_PUDA, "PUDA CQE",
  585. wqe, I40IW_CQP_WQE_SIZE * 8);
  586. i40iw_sc_cqp_post_sq(dev->cqp);
  587. status = dev->cqp_ops->poll_for_cqp_op_done(dev->cqp,
  588. I40IW_CQP_OP_CREATE_CQ,
  589. &compl_info);
  590. return status;
  591. }
  592. /**
  593. * i40iw_puda_cq_create - create cq for resource
  594. * @rsrc: resource for which cq to create
  595. */
  596. static enum i40iw_status_code i40iw_puda_cq_create(struct i40iw_puda_rsrc *rsrc)
  597. {
  598. struct i40iw_sc_dev *dev = rsrc->dev;
  599. struct i40iw_sc_cq *cq = &rsrc->cq;
  600. enum i40iw_status_code ret = 0;
  601. u32 tsize, cqsize;
  602. struct i40iw_dma_mem *mem;
  603. struct i40iw_cq_init_info info;
  604. struct i40iw_cq_uk_init_info *init_info = &info.cq_uk_init_info;
  605. cq->vsi = rsrc->vsi;
  606. cqsize = rsrc->cq_size * (sizeof(struct i40iw_cqe));
  607. tsize = cqsize + sizeof(struct i40iw_cq_shadow_area);
  608. ret = i40iw_allocate_dma_mem(dev->hw, &rsrc->cqmem, tsize,
  609. I40IW_CQ0_ALIGNMENT_MASK);
  610. if (ret)
  611. return ret;
  612. mem = &rsrc->cqmem;
  613. memset(&info, 0, sizeof(info));
  614. info.dev = dev;
  615. info.type = (rsrc->type == I40IW_PUDA_RSRC_TYPE_ILQ) ?
  616. I40IW_CQ_TYPE_ILQ : I40IW_CQ_TYPE_IEQ;
  617. info.shadow_read_threshold = rsrc->cq_size >> 2;
  618. info.ceq_id_valid = true;
  619. info.cq_base_pa = mem->pa;
  620. info.shadow_area_pa = mem->pa + cqsize;
  621. init_info->cq_base = mem->va;
  622. init_info->shadow_area = (u64 *)((u8 *)mem->va + cqsize);
  623. init_info->cq_size = rsrc->cq_size;
  624. init_info->cq_id = rsrc->cq_id;
  625. info.ceqe_mask = true;
  626. info.ceq_id_valid = true;
  627. ret = dev->iw_priv_cq_ops->cq_init(cq, &info);
  628. if (ret)
  629. goto error;
  630. if (rsrc->ceq_valid)
  631. ret = i40iw_cqp_cq_create_cmd(dev, cq);
  632. else
  633. ret = i40iw_puda_cq_wqe(dev, cq);
  634. error:
  635. if (ret)
  636. i40iw_free_dma_mem(dev->hw, &rsrc->cqmem);
  637. return ret;
  638. }
  639. /**
  640. * i40iw_puda_free_qp - free qp for resource
  641. * @rsrc: resource for which qp to free
  642. */
  643. static void i40iw_puda_free_qp(struct i40iw_puda_rsrc *rsrc)
  644. {
  645. enum i40iw_status_code ret;
  646. struct i40iw_ccq_cqe_info compl_info;
  647. struct i40iw_sc_dev *dev = rsrc->dev;
  648. if (rsrc->ceq_valid) {
  649. i40iw_cqp_qp_destroy_cmd(dev, &rsrc->qp);
  650. return;
  651. }
  652. ret = dev->iw_priv_qp_ops->qp_destroy(&rsrc->qp,
  653. 0, false, true, true);
  654. if (ret)
  655. i40iw_debug(dev, I40IW_DEBUG_PUDA,
  656. "%s error puda qp destroy wqe\n",
  657. __func__);
  658. if (!ret) {
  659. ret = dev->cqp_ops->poll_for_cqp_op_done(dev->cqp,
  660. I40IW_CQP_OP_DESTROY_QP,
  661. &compl_info);
  662. if (ret)
  663. i40iw_debug(dev, I40IW_DEBUG_PUDA,
  664. "%s error puda qp destroy failed\n",
  665. __func__);
  666. }
  667. }
  668. /**
  669. * i40iw_puda_free_cq - free cq for resource
  670. * @rsrc: resource for which cq to free
  671. */
  672. static void i40iw_puda_free_cq(struct i40iw_puda_rsrc *rsrc)
  673. {
  674. enum i40iw_status_code ret;
  675. struct i40iw_ccq_cqe_info compl_info;
  676. struct i40iw_sc_dev *dev = rsrc->dev;
  677. if (rsrc->ceq_valid) {
  678. i40iw_cqp_cq_destroy_cmd(dev, &rsrc->cq);
  679. return;
  680. }
  681. ret = dev->iw_priv_cq_ops->cq_destroy(&rsrc->cq, 0, true);
  682. if (ret)
  683. i40iw_debug(dev, I40IW_DEBUG_PUDA,
  684. "%s error ieq cq destroy\n",
  685. __func__);
  686. if (!ret) {
  687. ret = dev->cqp_ops->poll_for_cqp_op_done(dev->cqp,
  688. I40IW_CQP_OP_DESTROY_CQ,
  689. &compl_info);
  690. if (ret)
  691. i40iw_debug(dev, I40IW_DEBUG_PUDA,
  692. "%s error ieq qp destroy done\n",
  693. __func__);
  694. }
  695. }
  696. /**
  697. * i40iw_puda_dele_resources - delete all resources during close
  698. * @dev: iwarp device
  699. * @type: type of resource to dele
  700. * @reset: true if reset chip
  701. */
  702. void i40iw_puda_dele_resources(struct i40iw_sc_vsi *vsi,
  703. enum puda_resource_type type,
  704. bool reset)
  705. {
  706. struct i40iw_sc_dev *dev = vsi->dev;
  707. struct i40iw_puda_rsrc *rsrc;
  708. struct i40iw_puda_buf *buf = NULL;
  709. struct i40iw_puda_buf *nextbuf = NULL;
  710. struct i40iw_virt_mem *vmem;
  711. switch (type) {
  712. case I40IW_PUDA_RSRC_TYPE_ILQ:
  713. rsrc = vsi->ilq;
  714. vmem = &vsi->ilq_mem;
  715. break;
  716. case I40IW_PUDA_RSRC_TYPE_IEQ:
  717. rsrc = vsi->ieq;
  718. vmem = &vsi->ieq_mem;
  719. break;
  720. default:
  721. i40iw_debug(dev, I40IW_DEBUG_PUDA, "%s: error resource type = 0x%x\n",
  722. __func__, type);
  723. return;
  724. }
  725. switch (rsrc->completion) {
  726. case PUDA_HASH_CRC_COMPLETE:
  727. i40iw_free_hash_desc(rsrc->hash_desc);
  728. case PUDA_QP_CREATED:
  729. if (!reset)
  730. i40iw_puda_free_qp(rsrc);
  731. i40iw_free_dma_mem(dev->hw, &rsrc->qpmem);
  732. /* fallthrough */
  733. case PUDA_CQ_CREATED:
  734. if (!reset)
  735. i40iw_puda_free_cq(rsrc);
  736. i40iw_free_dma_mem(dev->hw, &rsrc->cqmem);
  737. break;
  738. default:
  739. i40iw_debug(rsrc->dev, I40IW_DEBUG_PUDA, "%s error no resources\n", __func__);
  740. break;
  741. }
  742. /* Free all allocated puda buffers for both tx and rx */
  743. buf = rsrc->alloclist;
  744. while (buf) {
  745. nextbuf = buf->next;
  746. i40iw_puda_dele_buf(dev, buf);
  747. buf = nextbuf;
  748. rsrc->alloc_buf_count--;
  749. }
  750. i40iw_free_virt_mem(dev->hw, vmem);
  751. }
  752. /**
  753. * i40iw_puda_allocbufs - allocate buffers for resource
  754. * @rsrc: resource for buffer allocation
  755. * @count: number of buffers to create
  756. */
  757. static enum i40iw_status_code i40iw_puda_allocbufs(struct i40iw_puda_rsrc *rsrc,
  758. u32 count)
  759. {
  760. u32 i;
  761. struct i40iw_puda_buf *buf;
  762. struct i40iw_puda_buf *nextbuf;
  763. for (i = 0; i < count; i++) {
  764. buf = i40iw_puda_alloc_buf(rsrc->dev, rsrc->buf_size);
  765. if (!buf) {
  766. rsrc->stats_buf_alloc_fail++;
  767. return I40IW_ERR_NO_MEMORY;
  768. }
  769. i40iw_puda_ret_bufpool(rsrc, buf);
  770. rsrc->alloc_buf_count++;
  771. if (!rsrc->alloclist) {
  772. rsrc->alloclist = buf;
  773. } else {
  774. nextbuf = rsrc->alloclist;
  775. rsrc->alloclist = buf;
  776. buf->next = nextbuf;
  777. }
  778. }
  779. rsrc->avail_buf_count = rsrc->alloc_buf_count;
  780. return 0;
  781. }
  782. /**
  783. * i40iw_puda_create_rsrc - create resouce (ilq or ieq)
  784. * @dev: iwarp device
  785. * @info: resource information
  786. */
  787. enum i40iw_status_code i40iw_puda_create_rsrc(struct i40iw_sc_vsi *vsi,
  788. struct i40iw_puda_rsrc_info *info)
  789. {
  790. struct i40iw_sc_dev *dev = vsi->dev;
  791. enum i40iw_status_code ret = 0;
  792. struct i40iw_puda_rsrc *rsrc;
  793. u32 pudasize;
  794. u32 sqwridsize, rqwridsize;
  795. struct i40iw_virt_mem *vmem;
  796. info->count = 1;
  797. pudasize = sizeof(struct i40iw_puda_rsrc);
  798. sqwridsize = info->sq_size * sizeof(struct i40iw_sq_uk_wr_trk_info);
  799. rqwridsize = info->rq_size * 8;
  800. switch (info->type) {
  801. case I40IW_PUDA_RSRC_TYPE_ILQ:
  802. vmem = &vsi->ilq_mem;
  803. break;
  804. case I40IW_PUDA_RSRC_TYPE_IEQ:
  805. vmem = &vsi->ieq_mem;
  806. break;
  807. default:
  808. return I40IW_NOT_SUPPORTED;
  809. }
  810. ret =
  811. i40iw_allocate_virt_mem(dev->hw, vmem,
  812. pudasize + sqwridsize + rqwridsize);
  813. if (ret)
  814. return ret;
  815. rsrc = (struct i40iw_puda_rsrc *)vmem->va;
  816. spin_lock_init(&rsrc->bufpool_lock);
  817. if (info->type == I40IW_PUDA_RSRC_TYPE_ILQ) {
  818. vsi->ilq = (struct i40iw_puda_rsrc *)vmem->va;
  819. vsi->ilq_count = info->count;
  820. rsrc->receive = info->receive;
  821. rsrc->xmit_complete = info->xmit_complete;
  822. } else {
  823. vmem = &vsi->ieq_mem;
  824. vsi->ieq_count = info->count;
  825. vsi->ieq = (struct i40iw_puda_rsrc *)vmem->va;
  826. rsrc->receive = i40iw_ieq_receive;
  827. rsrc->xmit_complete = i40iw_ieq_tx_compl;
  828. }
  829. rsrc->ceq_valid = info->ceq_valid;
  830. rsrc->type = info->type;
  831. rsrc->sq_wrtrk_array = (struct i40iw_sq_uk_wr_trk_info *)((u8 *)vmem->va + pudasize);
  832. rsrc->rq_wrid_array = (u64 *)((u8 *)vmem->va + pudasize + sqwridsize);
  833. /* Initialize all ieq lists */
  834. INIT_LIST_HEAD(&rsrc->bufpool);
  835. INIT_LIST_HEAD(&rsrc->txpend);
  836. rsrc->tx_wqe_avail_cnt = info->sq_size - 1;
  837. dev->iw_pd_ops->pd_init(dev, &rsrc->sc_pd, info->pd_id, -1);
  838. rsrc->qp_id = info->qp_id;
  839. rsrc->cq_id = info->cq_id;
  840. rsrc->sq_size = info->sq_size;
  841. rsrc->rq_size = info->rq_size;
  842. rsrc->cq_size = info->rq_size + info->sq_size;
  843. rsrc->buf_size = info->buf_size;
  844. rsrc->dev = dev;
  845. rsrc->vsi = vsi;
  846. ret = i40iw_puda_cq_create(rsrc);
  847. if (!ret) {
  848. rsrc->completion = PUDA_CQ_CREATED;
  849. ret = i40iw_puda_qp_create(rsrc);
  850. }
  851. if (ret) {
  852. i40iw_debug(dev, I40IW_DEBUG_PUDA, "[%s] error qp_create\n", __func__);
  853. goto error;
  854. }
  855. rsrc->completion = PUDA_QP_CREATED;
  856. ret = i40iw_puda_allocbufs(rsrc, info->tx_buf_cnt + info->rq_size);
  857. if (ret) {
  858. i40iw_debug(dev, I40IW_DEBUG_PUDA, "[%s] error allloc_buf\n", __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 = qp->q2_buf[16];
  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. plist = &tmpbuf->list;
  1275. while ((struct list_head *)tmpbuf != rxlist) {
  1276. if ((int)(buf->seqnum - tmpbuf->seqnum) < 0)
  1277. break;
  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. static 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. }