qedr_roce_cm.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729
  1. /* QLogic qedr NIC Driver
  2. * Copyright (c) 2015-2016 QLogic Corporation
  3. *
  4. * This software is available to you under a choice of one of two
  5. * licenses. You may choose to be licensed under the terms of the GNU
  6. * General Public License (GPL) Version 2, available from the file
  7. * COPYING in the main directory of this source tree, or the
  8. * OpenIB.org BSD license below:
  9. *
  10. * Redistribution and use in source and binary forms, with or
  11. * without modification, are permitted provided that the following
  12. * conditions are met:
  13. *
  14. * - Redistributions of source code must retain the above
  15. * copyright notice, this list of conditions and the following
  16. * disclaimer.
  17. *
  18. * - Redistributions in binary form must reproduce the above
  19. * copyright notice, this list of conditions and the following
  20. * disclaimer in the documentation and /or other materials
  21. * provided with the distribution.
  22. *
  23. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  27. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  28. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  29. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  30. * SOFTWARE.
  31. */
  32. #include <linux/dma-mapping.h>
  33. #include <linux/crc32.h>
  34. #include <linux/iommu.h>
  35. #include <net/ip.h>
  36. #include <net/ipv6.h>
  37. #include <net/udp.h>
  38. #include <rdma/ib_verbs.h>
  39. #include <rdma/ib_user_verbs.h>
  40. #include <rdma/iw_cm.h>
  41. #include <rdma/ib_umem.h>
  42. #include <rdma/ib_addr.h>
  43. #include <rdma/ib_cache.h>
  44. #include <linux/qed/qed_if.h>
  45. #include <linux/qed/qed_rdma_if.h>
  46. #include "qedr.h"
  47. #include "verbs.h"
  48. #include <rdma/qedr-abi.h>
  49. #include "qedr_roce_cm.h"
  50. void qedr_inc_sw_gsi_cons(struct qedr_qp_hwq_info *info)
  51. {
  52. info->gsi_cons = (info->gsi_cons + 1) % info->max_wr;
  53. }
  54. void qedr_store_gsi_qp_cq(struct qedr_dev *dev, struct qedr_qp *qp,
  55. struct ib_qp_init_attr *attrs)
  56. {
  57. dev->gsi_qp_created = 1;
  58. dev->gsi_sqcq = get_qedr_cq(attrs->send_cq);
  59. dev->gsi_rqcq = get_qedr_cq(attrs->recv_cq);
  60. dev->gsi_qp = qp;
  61. }
  62. static void qedr_ll2_complete_tx_packet(void *cxt, u8 connection_handle,
  63. void *cookie,
  64. dma_addr_t first_frag_addr,
  65. bool b_last_fragment,
  66. bool b_last_packet)
  67. {
  68. struct qedr_dev *dev = (struct qedr_dev *)cxt;
  69. struct qed_roce_ll2_packet *pkt = cookie;
  70. struct qedr_cq *cq = dev->gsi_sqcq;
  71. struct qedr_qp *qp = dev->gsi_qp;
  72. unsigned long flags;
  73. DP_DEBUG(dev, QEDR_MSG_GSI,
  74. "LL2 TX CB: gsi_sqcq=%p, gsi_rqcq=%p, gsi_cons=%d, ibcq_comp=%s\n",
  75. dev->gsi_sqcq, dev->gsi_rqcq, qp->sq.gsi_cons,
  76. cq->ibcq.comp_handler ? "Yes" : "No");
  77. dma_free_coherent(&dev->pdev->dev, pkt->header.len, pkt->header.vaddr,
  78. pkt->header.baddr);
  79. kfree(pkt);
  80. spin_lock_irqsave(&qp->q_lock, flags);
  81. qedr_inc_sw_gsi_cons(&qp->sq);
  82. spin_unlock_irqrestore(&qp->q_lock, flags);
  83. if (cq->ibcq.comp_handler)
  84. (*cq->ibcq.comp_handler) (&cq->ibcq, cq->ibcq.cq_context);
  85. }
  86. static void qedr_ll2_complete_rx_packet(void *cxt,
  87. struct qed_ll2_comp_rx_data *data)
  88. {
  89. struct qedr_dev *dev = (struct qedr_dev *)cxt;
  90. struct qedr_cq *cq = dev->gsi_rqcq;
  91. struct qedr_qp *qp = dev->gsi_qp;
  92. unsigned long flags;
  93. spin_lock_irqsave(&qp->q_lock, flags);
  94. qp->rqe_wr_id[qp->rq.gsi_cons].rc = data->u.data_length_error ?
  95. -EINVAL : 0;
  96. qp->rqe_wr_id[qp->rq.gsi_cons].vlan = data->vlan;
  97. /* note: length stands for data length i.e. GRH is excluded */
  98. qp->rqe_wr_id[qp->rq.gsi_cons].sg_list[0].length =
  99. data->length.data_length;
  100. *((u32 *)&qp->rqe_wr_id[qp->rq.gsi_cons].smac[0]) =
  101. ntohl(data->opaque_data_0);
  102. *((u16 *)&qp->rqe_wr_id[qp->rq.gsi_cons].smac[4]) =
  103. ntohs((u16)data->opaque_data_1);
  104. qedr_inc_sw_gsi_cons(&qp->rq);
  105. spin_unlock_irqrestore(&qp->q_lock, flags);
  106. if (cq->ibcq.comp_handler)
  107. (*cq->ibcq.comp_handler) (&cq->ibcq, cq->ibcq.cq_context);
  108. }
  109. static void qedr_ll2_release_rx_packet(void *cxt, u8 connection_handle,
  110. void *cookie, dma_addr_t rx_buf_addr,
  111. bool b_last_packet)
  112. {
  113. /* Do nothing... */
  114. }
  115. static void qedr_destroy_gsi_cq(struct qedr_dev *dev,
  116. struct ib_qp_init_attr *attrs)
  117. {
  118. struct qed_rdma_destroy_cq_in_params iparams;
  119. struct qed_rdma_destroy_cq_out_params oparams;
  120. struct qedr_cq *cq;
  121. cq = get_qedr_cq(attrs->send_cq);
  122. iparams.icid = cq->icid;
  123. dev->ops->rdma_destroy_cq(dev->rdma_ctx, &iparams, &oparams);
  124. dev->ops->common->chain_free(dev->cdev, &cq->pbl);
  125. cq = get_qedr_cq(attrs->recv_cq);
  126. /* if a dedicated recv_cq was used, delete it too */
  127. if (iparams.icid != cq->icid) {
  128. iparams.icid = cq->icid;
  129. dev->ops->rdma_destroy_cq(dev->rdma_ctx, &iparams, &oparams);
  130. dev->ops->common->chain_free(dev->cdev, &cq->pbl);
  131. }
  132. }
  133. static inline int qedr_check_gsi_qp_attrs(struct qedr_dev *dev,
  134. struct ib_qp_init_attr *attrs)
  135. {
  136. if (attrs->cap.max_recv_sge > QEDR_GSI_MAX_RECV_SGE) {
  137. DP_ERR(dev,
  138. " create gsi qp: failed. max_recv_sge is larger the max %d>%d\n",
  139. attrs->cap.max_recv_sge, QEDR_GSI_MAX_RECV_SGE);
  140. return -EINVAL;
  141. }
  142. if (attrs->cap.max_recv_wr > QEDR_GSI_MAX_RECV_WR) {
  143. DP_ERR(dev,
  144. " create gsi qp: failed. max_recv_wr is too large %d>%d\n",
  145. attrs->cap.max_recv_wr, QEDR_GSI_MAX_RECV_WR);
  146. return -EINVAL;
  147. }
  148. if (attrs->cap.max_send_wr > QEDR_GSI_MAX_SEND_WR) {
  149. DP_ERR(dev,
  150. " create gsi qp: failed. max_send_wr is too large %d>%d\n",
  151. attrs->cap.max_send_wr, QEDR_GSI_MAX_SEND_WR);
  152. return -EINVAL;
  153. }
  154. return 0;
  155. }
  156. static int qedr_ll2_post_tx(struct qedr_dev *dev,
  157. struct qed_roce_ll2_packet *pkt)
  158. {
  159. enum qed_ll2_roce_flavor_type roce_flavor;
  160. struct qed_ll2_tx_pkt_info ll2_tx_pkt;
  161. int rc;
  162. int i;
  163. memset(&ll2_tx_pkt, 0, sizeof(ll2_tx_pkt));
  164. roce_flavor = (pkt->roce_mode == ROCE_V1) ?
  165. QED_LL2_ROCE : QED_LL2_RROCE;
  166. if (pkt->roce_mode == ROCE_V2_IPV4)
  167. ll2_tx_pkt.enable_ip_cksum = 1;
  168. ll2_tx_pkt.num_of_bds = 1 /* hdr */ + pkt->n_seg;
  169. ll2_tx_pkt.vlan = 0;
  170. ll2_tx_pkt.tx_dest = pkt->tx_dest;
  171. ll2_tx_pkt.qed_roce_flavor = roce_flavor;
  172. ll2_tx_pkt.first_frag = pkt->header.baddr;
  173. ll2_tx_pkt.first_frag_len = pkt->header.len;
  174. ll2_tx_pkt.cookie = pkt;
  175. /* tx header */
  176. rc = dev->ops->ll2_prepare_tx_packet(dev->rdma_ctx,
  177. dev->gsi_ll2_handle,
  178. &ll2_tx_pkt, 1);
  179. if (rc) {
  180. /* TX failed while posting header - release resources */
  181. dma_free_coherent(&dev->pdev->dev, pkt->header.len,
  182. pkt->header.vaddr, pkt->header.baddr);
  183. kfree(pkt);
  184. DP_ERR(dev, "roce ll2 tx: header failed (rc=%d)\n", rc);
  185. return rc;
  186. }
  187. /* tx payload */
  188. for (i = 0; i < pkt->n_seg; i++) {
  189. rc = dev->ops->ll2_set_fragment_of_tx_packet(
  190. dev->rdma_ctx,
  191. dev->gsi_ll2_handle,
  192. pkt->payload[i].baddr,
  193. pkt->payload[i].len);
  194. if (rc) {
  195. /* if failed not much to do here, partial packet has
  196. * been posted we can't free memory, will need to wait
  197. * for completion
  198. */
  199. DP_ERR(dev, "ll2 tx: payload failed (rc=%d)\n", rc);
  200. return rc;
  201. }
  202. }
  203. return 0;
  204. }
  205. static int qedr_ll2_stop(struct qedr_dev *dev)
  206. {
  207. int rc;
  208. if (dev->gsi_ll2_handle == QED_LL2_UNUSED_HANDLE)
  209. return 0;
  210. /* remove LL2 MAC address filter */
  211. rc = dev->ops->ll2_set_mac_filter(dev->cdev,
  212. dev->gsi_ll2_mac_address, NULL);
  213. rc = dev->ops->ll2_terminate_connection(dev->rdma_ctx,
  214. dev->gsi_ll2_handle);
  215. if (rc)
  216. DP_ERR(dev, "Failed to terminate LL2 connection (rc=%d)\n", rc);
  217. dev->ops->ll2_release_connection(dev->rdma_ctx, dev->gsi_ll2_handle);
  218. dev->gsi_ll2_handle = QED_LL2_UNUSED_HANDLE;
  219. return rc;
  220. }
  221. static int qedr_ll2_start(struct qedr_dev *dev,
  222. struct ib_qp_init_attr *attrs, struct qedr_qp *qp)
  223. {
  224. struct qed_ll2_acquire_data data;
  225. struct qed_ll2_cbs cbs;
  226. int rc;
  227. /* configure and start LL2 */
  228. cbs.rx_comp_cb = qedr_ll2_complete_rx_packet;
  229. cbs.tx_comp_cb = qedr_ll2_complete_tx_packet;
  230. cbs.rx_release_cb = qedr_ll2_release_rx_packet;
  231. cbs.tx_release_cb = qedr_ll2_complete_tx_packet;
  232. cbs.cookie = dev;
  233. memset(&data, 0, sizeof(data));
  234. data.input.conn_type = QED_LL2_TYPE_ROCE;
  235. data.input.mtu = dev->ndev->mtu;
  236. data.input.rx_num_desc = attrs->cap.max_recv_wr;
  237. data.input.rx_drop_ttl0_flg = true;
  238. data.input.rx_vlan_removal_en = false;
  239. data.input.tx_num_desc = attrs->cap.max_send_wr;
  240. data.input.tx_tc = 0;
  241. data.input.tx_dest = QED_LL2_TX_DEST_NW;
  242. data.input.ai_err_packet_too_big = QED_LL2_DROP_PACKET;
  243. data.input.ai_err_no_buf = QED_LL2_DROP_PACKET;
  244. data.input.gsi_enable = 1;
  245. data.p_connection_handle = &dev->gsi_ll2_handle;
  246. data.cbs = &cbs;
  247. rc = dev->ops->ll2_acquire_connection(dev->rdma_ctx, &data);
  248. if (rc) {
  249. DP_ERR(dev,
  250. "ll2 start: failed to acquire LL2 connection (rc=%d)\n",
  251. rc);
  252. return rc;
  253. }
  254. rc = dev->ops->ll2_establish_connection(dev->rdma_ctx,
  255. dev->gsi_ll2_handle);
  256. if (rc) {
  257. DP_ERR(dev,
  258. "ll2 start: failed to establish LL2 connection (rc=%d)\n",
  259. rc);
  260. goto err1;
  261. }
  262. rc = dev->ops->ll2_set_mac_filter(dev->cdev, NULL, dev->ndev->dev_addr);
  263. if (rc)
  264. goto err2;
  265. return 0;
  266. err2:
  267. dev->ops->ll2_terminate_connection(dev->rdma_ctx, dev->gsi_ll2_handle);
  268. err1:
  269. dev->ops->ll2_release_connection(dev->rdma_ctx, dev->gsi_ll2_handle);
  270. return rc;
  271. }
  272. struct ib_qp *qedr_create_gsi_qp(struct qedr_dev *dev,
  273. struct ib_qp_init_attr *attrs,
  274. struct qedr_qp *qp)
  275. {
  276. int rc;
  277. rc = qedr_check_gsi_qp_attrs(dev, attrs);
  278. if (rc)
  279. return ERR_PTR(rc);
  280. rc = qedr_ll2_start(dev, attrs, qp);
  281. if (rc) {
  282. DP_ERR(dev, "create gsi qp: failed on ll2 start. rc=%d\n", rc);
  283. return ERR_PTR(rc);
  284. }
  285. /* create QP */
  286. qp->ibqp.qp_num = 1;
  287. qp->rq.max_wr = attrs->cap.max_recv_wr;
  288. qp->sq.max_wr = attrs->cap.max_send_wr;
  289. qp->rqe_wr_id = kcalloc(qp->rq.max_wr, sizeof(*qp->rqe_wr_id),
  290. GFP_KERNEL);
  291. if (!qp->rqe_wr_id)
  292. goto err;
  293. qp->wqe_wr_id = kcalloc(qp->sq.max_wr, sizeof(*qp->wqe_wr_id),
  294. GFP_KERNEL);
  295. if (!qp->wqe_wr_id)
  296. goto err;
  297. qedr_store_gsi_qp_cq(dev, qp, attrs);
  298. ether_addr_copy(dev->gsi_ll2_mac_address, dev->ndev->dev_addr);
  299. /* the GSI CQ is handled by the driver so remove it from the FW */
  300. qedr_destroy_gsi_cq(dev, attrs);
  301. dev->gsi_rqcq->cq_type = QEDR_CQ_TYPE_GSI;
  302. dev->gsi_rqcq->cq_type = QEDR_CQ_TYPE_GSI;
  303. DP_DEBUG(dev, QEDR_MSG_GSI, "created GSI QP %p\n", qp);
  304. return &qp->ibqp;
  305. err:
  306. kfree(qp->rqe_wr_id);
  307. rc = qedr_ll2_stop(dev);
  308. if (rc)
  309. DP_ERR(dev, "create gsi qp: failed destroy on create\n");
  310. return ERR_PTR(-ENOMEM);
  311. }
  312. int qedr_destroy_gsi_qp(struct qedr_dev *dev)
  313. {
  314. return qedr_ll2_stop(dev);
  315. }
  316. #define QEDR_MAX_UD_HEADER_SIZE (100)
  317. #define QEDR_GSI_QPN (1)
  318. static inline int qedr_gsi_build_header(struct qedr_dev *dev,
  319. struct qedr_qp *qp,
  320. const struct ib_send_wr *swr,
  321. struct ib_ud_header *udh,
  322. int *roce_mode)
  323. {
  324. bool has_vlan = false, has_grh_ipv6 = true;
  325. struct rdma_ah_attr *ah_attr = &get_qedr_ah(ud_wr(swr)->ah)->attr;
  326. const struct ib_global_route *grh = rdma_ah_read_grh(ah_attr);
  327. const struct ib_gid_attr *sgid_attr = grh->sgid_attr;
  328. int send_size = 0;
  329. u16 vlan_id = 0;
  330. u16 ether_type;
  331. int rc;
  332. int ip_ver = 0;
  333. bool has_udp = false;
  334. int i;
  335. send_size = 0;
  336. for (i = 0; i < swr->num_sge; ++i)
  337. send_size += swr->sg_list[i].length;
  338. vlan_id = rdma_vlan_dev_vlan_id(sgid_attr->ndev);
  339. if (vlan_id < VLAN_CFI_MASK)
  340. has_vlan = true;
  341. has_udp = (sgid_attr->gid_type == IB_GID_TYPE_ROCE_UDP_ENCAP);
  342. if (!has_udp) {
  343. /* RoCE v1 */
  344. ether_type = ETH_P_IBOE;
  345. *roce_mode = ROCE_V1;
  346. } else if (ipv6_addr_v4mapped((struct in6_addr *)&sgid_attr->gid)) {
  347. /* RoCE v2 IPv4 */
  348. ip_ver = 4;
  349. ether_type = ETH_P_IP;
  350. has_grh_ipv6 = false;
  351. *roce_mode = ROCE_V2_IPV4;
  352. } else {
  353. /* RoCE v2 IPv6 */
  354. ip_ver = 6;
  355. ether_type = ETH_P_IPV6;
  356. *roce_mode = ROCE_V2_IPV6;
  357. }
  358. rc = ib_ud_header_init(send_size, false, true, has_vlan,
  359. has_grh_ipv6, ip_ver, has_udp, 0, udh);
  360. if (rc) {
  361. DP_ERR(dev, "gsi post send: failed to init header\n");
  362. return rc;
  363. }
  364. /* ENET + VLAN headers */
  365. ether_addr_copy(udh->eth.dmac_h, ah_attr->roce.dmac);
  366. ether_addr_copy(udh->eth.smac_h, dev->ndev->dev_addr);
  367. if (has_vlan) {
  368. udh->eth.type = htons(ETH_P_8021Q);
  369. udh->vlan.tag = htons(vlan_id);
  370. udh->vlan.type = htons(ether_type);
  371. } else {
  372. udh->eth.type = htons(ether_type);
  373. }
  374. /* BTH */
  375. udh->bth.solicited_event = !!(swr->send_flags & IB_SEND_SOLICITED);
  376. udh->bth.pkey = QEDR_ROCE_PKEY_DEFAULT;
  377. udh->bth.destination_qpn = htonl(ud_wr(swr)->remote_qpn);
  378. udh->bth.psn = htonl((qp->sq_psn++) & ((1 << 24) - 1));
  379. udh->bth.opcode = IB_OPCODE_UD_SEND_ONLY;
  380. /* DETH */
  381. udh->deth.qkey = htonl(0x80010000);
  382. udh->deth.source_qpn = htonl(QEDR_GSI_QPN);
  383. if (has_grh_ipv6) {
  384. /* GRH / IPv6 header */
  385. udh->grh.traffic_class = grh->traffic_class;
  386. udh->grh.flow_label = grh->flow_label;
  387. udh->grh.hop_limit = grh->hop_limit;
  388. udh->grh.destination_gid = grh->dgid;
  389. memcpy(&udh->grh.source_gid.raw, sgid_attr->gid.raw,
  390. sizeof(udh->grh.source_gid.raw));
  391. } else {
  392. /* IPv4 header */
  393. u32 ipv4_addr;
  394. udh->ip4.protocol = IPPROTO_UDP;
  395. udh->ip4.tos = htonl(grh->flow_label);
  396. udh->ip4.frag_off = htons(IP_DF);
  397. udh->ip4.ttl = grh->hop_limit;
  398. ipv4_addr = qedr_get_ipv4_from_gid(sgid_attr->gid.raw);
  399. udh->ip4.saddr = ipv4_addr;
  400. ipv4_addr = qedr_get_ipv4_from_gid(grh->dgid.raw);
  401. udh->ip4.daddr = ipv4_addr;
  402. /* note: checksum is calculated by the device */
  403. }
  404. /* UDP */
  405. if (has_udp) {
  406. udh->udp.sport = htons(QEDR_ROCE_V2_UDP_SPORT);
  407. udh->udp.dport = htons(ROCE_V2_UDP_DPORT);
  408. udh->udp.csum = 0;
  409. /* UDP length is untouched hence is zero */
  410. }
  411. return 0;
  412. }
  413. static inline int qedr_gsi_build_packet(struct qedr_dev *dev,
  414. struct qedr_qp *qp,
  415. const struct ib_send_wr *swr,
  416. struct qed_roce_ll2_packet **p_packet)
  417. {
  418. u8 ud_header_buffer[QEDR_MAX_UD_HEADER_SIZE];
  419. struct qed_roce_ll2_packet *packet;
  420. struct pci_dev *pdev = dev->pdev;
  421. int roce_mode, header_size;
  422. struct ib_ud_header udh;
  423. int i, rc;
  424. *p_packet = NULL;
  425. rc = qedr_gsi_build_header(dev, qp, swr, &udh, &roce_mode);
  426. if (rc)
  427. return rc;
  428. header_size = ib_ud_header_pack(&udh, &ud_header_buffer);
  429. packet = kzalloc(sizeof(*packet), GFP_ATOMIC);
  430. if (!packet)
  431. return -ENOMEM;
  432. packet->header.vaddr = dma_alloc_coherent(&pdev->dev, header_size,
  433. &packet->header.baddr,
  434. GFP_ATOMIC);
  435. if (!packet->header.vaddr) {
  436. kfree(packet);
  437. return -ENOMEM;
  438. }
  439. if (ether_addr_equal(udh.eth.smac_h, udh.eth.dmac_h))
  440. packet->tx_dest = QED_ROCE_LL2_TX_DEST_LB;
  441. else
  442. packet->tx_dest = QED_ROCE_LL2_TX_DEST_NW;
  443. packet->roce_mode = roce_mode;
  444. memcpy(packet->header.vaddr, ud_header_buffer, header_size);
  445. packet->header.len = header_size;
  446. packet->n_seg = swr->num_sge;
  447. for (i = 0; i < packet->n_seg; i++) {
  448. packet->payload[i].baddr = swr->sg_list[i].addr;
  449. packet->payload[i].len = swr->sg_list[i].length;
  450. }
  451. *p_packet = packet;
  452. return 0;
  453. }
  454. int qedr_gsi_post_send(struct ib_qp *ibqp, const struct ib_send_wr *wr,
  455. const struct ib_send_wr **bad_wr)
  456. {
  457. struct qed_roce_ll2_packet *pkt = NULL;
  458. struct qedr_qp *qp = get_qedr_qp(ibqp);
  459. struct qedr_dev *dev = qp->dev;
  460. unsigned long flags;
  461. int rc;
  462. if (qp->state != QED_ROCE_QP_STATE_RTS) {
  463. *bad_wr = wr;
  464. DP_ERR(dev,
  465. "gsi post recv: failed to post rx buffer. state is %d and not QED_ROCE_QP_STATE_RTS\n",
  466. qp->state);
  467. return -EINVAL;
  468. }
  469. if (wr->num_sge > RDMA_MAX_SGE_PER_SQ_WQE) {
  470. DP_ERR(dev, "gsi post send: num_sge is too large (%d>%d)\n",
  471. wr->num_sge, RDMA_MAX_SGE_PER_SQ_WQE);
  472. rc = -EINVAL;
  473. goto err;
  474. }
  475. if (wr->opcode != IB_WR_SEND) {
  476. DP_ERR(dev,
  477. "gsi post send: failed due to unsupported opcode %d\n",
  478. wr->opcode);
  479. rc = -EINVAL;
  480. goto err;
  481. }
  482. spin_lock_irqsave(&qp->q_lock, flags);
  483. rc = qedr_gsi_build_packet(dev, qp, wr, &pkt);
  484. if (rc) {
  485. spin_unlock_irqrestore(&qp->q_lock, flags);
  486. goto err;
  487. }
  488. rc = qedr_ll2_post_tx(dev, pkt);
  489. if (!rc) {
  490. qp->wqe_wr_id[qp->sq.prod].wr_id = wr->wr_id;
  491. qedr_inc_sw_prod(&qp->sq);
  492. DP_DEBUG(qp->dev, QEDR_MSG_GSI,
  493. "gsi post send: opcode=%d, in_irq=%ld, irqs_disabled=%d, wr_id=%llx\n",
  494. wr->opcode, in_irq(), irqs_disabled(), wr->wr_id);
  495. } else {
  496. DP_ERR(dev, "gsi post send: failed to transmit (rc=%d)\n", rc);
  497. rc = -EAGAIN;
  498. *bad_wr = wr;
  499. }
  500. spin_unlock_irqrestore(&qp->q_lock, flags);
  501. if (wr->next) {
  502. DP_ERR(dev,
  503. "gsi post send: failed second WR. Only one WR may be passed at a time\n");
  504. *bad_wr = wr->next;
  505. rc = -EINVAL;
  506. }
  507. return rc;
  508. err:
  509. *bad_wr = wr;
  510. return rc;
  511. }
  512. int qedr_gsi_post_recv(struct ib_qp *ibqp, const struct ib_recv_wr *wr,
  513. const struct ib_recv_wr **bad_wr)
  514. {
  515. struct qedr_dev *dev = get_qedr_dev(ibqp->device);
  516. struct qedr_qp *qp = get_qedr_qp(ibqp);
  517. unsigned long flags;
  518. int rc = 0;
  519. if ((qp->state != QED_ROCE_QP_STATE_RTR) &&
  520. (qp->state != QED_ROCE_QP_STATE_RTS)) {
  521. *bad_wr = wr;
  522. DP_ERR(dev,
  523. "gsi post recv: failed to post rx buffer. state is %d and not QED_ROCE_QP_STATE_RTR/S\n",
  524. qp->state);
  525. return -EINVAL;
  526. }
  527. spin_lock_irqsave(&qp->q_lock, flags);
  528. while (wr) {
  529. if (wr->num_sge > QEDR_GSI_MAX_RECV_SGE) {
  530. DP_ERR(dev,
  531. "gsi post recv: failed to post rx buffer. too many sges %d>%d\n",
  532. wr->num_sge, QEDR_GSI_MAX_RECV_SGE);
  533. goto err;
  534. }
  535. rc = dev->ops->ll2_post_rx_buffer(dev->rdma_ctx,
  536. dev->gsi_ll2_handle,
  537. wr->sg_list[0].addr,
  538. wr->sg_list[0].length,
  539. NULL /* cookie */,
  540. 1 /* notify_fw */);
  541. if (rc) {
  542. DP_ERR(dev,
  543. "gsi post recv: failed to post rx buffer (rc=%d)\n",
  544. rc);
  545. goto err;
  546. }
  547. memset(&qp->rqe_wr_id[qp->rq.prod], 0,
  548. sizeof(qp->rqe_wr_id[qp->rq.prod]));
  549. qp->rqe_wr_id[qp->rq.prod].sg_list[0] = wr->sg_list[0];
  550. qp->rqe_wr_id[qp->rq.prod].wr_id = wr->wr_id;
  551. qedr_inc_sw_prod(&qp->rq);
  552. wr = wr->next;
  553. }
  554. spin_unlock_irqrestore(&qp->q_lock, flags);
  555. return rc;
  556. err:
  557. spin_unlock_irqrestore(&qp->q_lock, flags);
  558. *bad_wr = wr;
  559. return -ENOMEM;
  560. }
  561. int qedr_gsi_poll_cq(struct ib_cq *ibcq, int num_entries, struct ib_wc *wc)
  562. {
  563. struct qedr_dev *dev = get_qedr_dev(ibcq->device);
  564. struct qedr_cq *cq = get_qedr_cq(ibcq);
  565. struct qedr_qp *qp = dev->gsi_qp;
  566. unsigned long flags;
  567. u16 vlan_id;
  568. int i = 0;
  569. spin_lock_irqsave(&cq->cq_lock, flags);
  570. while (i < num_entries && qp->rq.cons != qp->rq.gsi_cons) {
  571. memset(&wc[i], 0, sizeof(*wc));
  572. wc[i].qp = &qp->ibqp;
  573. wc[i].wr_id = qp->rqe_wr_id[qp->rq.cons].wr_id;
  574. wc[i].opcode = IB_WC_RECV;
  575. wc[i].pkey_index = 0;
  576. wc[i].status = (qp->rqe_wr_id[qp->rq.cons].rc) ?
  577. IB_WC_GENERAL_ERR : IB_WC_SUCCESS;
  578. /* 0 - currently only one recv sg is supported */
  579. wc[i].byte_len = qp->rqe_wr_id[qp->rq.cons].sg_list[0].length;
  580. wc[i].wc_flags |= IB_WC_GRH | IB_WC_IP_CSUM_OK;
  581. ether_addr_copy(wc[i].smac, qp->rqe_wr_id[qp->rq.cons].smac);
  582. wc[i].wc_flags |= IB_WC_WITH_SMAC;
  583. vlan_id = qp->rqe_wr_id[qp->rq.cons].vlan &
  584. VLAN_VID_MASK;
  585. if (vlan_id) {
  586. wc[i].wc_flags |= IB_WC_WITH_VLAN;
  587. wc[i].vlan_id = vlan_id;
  588. wc[i].sl = (qp->rqe_wr_id[qp->rq.cons].vlan &
  589. VLAN_PRIO_MASK) >> VLAN_PRIO_SHIFT;
  590. }
  591. qedr_inc_sw_cons(&qp->rq);
  592. i++;
  593. }
  594. while (i < num_entries && qp->sq.cons != qp->sq.gsi_cons) {
  595. memset(&wc[i], 0, sizeof(*wc));
  596. wc[i].qp = &qp->ibqp;
  597. wc[i].wr_id = qp->wqe_wr_id[qp->sq.cons].wr_id;
  598. wc[i].opcode = IB_WC_SEND;
  599. wc[i].status = IB_WC_SUCCESS;
  600. qedr_inc_sw_cons(&qp->sq);
  601. i++;
  602. }
  603. spin_unlock_irqrestore(&cq->cq_lock, flags);
  604. DP_DEBUG(dev, QEDR_MSG_GSI,
  605. "gsi poll_cq: requested entries=%d, actual=%d, qp->rq.cons=%d, qp->rq.gsi_cons=%x, qp->sq.cons=%d, qp->sq.gsi_cons=%d, qp_num=%d\n",
  606. num_entries, i, qp->rq.cons, qp->rq.gsi_cons, qp->sq.cons,
  607. qp->sq.gsi_cons, qp->ibqp.qp_num);
  608. return i;
  609. }