rxe_av.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. // SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
  2. /*
  3. * Copyright (c) 2016 Mellanox Technologies Ltd. All rights reserved.
  4. * Copyright (c) 2015 System Fabric Works, Inc. All rights reserved.
  5. */
  6. #include "rxe.h"
  7. #include "rxe_loc.h"
  8. void rxe_init_av(struct rdma_ah_attr *attr, struct rxe_av *av)
  9. {
  10. rxe_av_from_attr(rdma_ah_get_port_num(attr), av, attr);
  11. rxe_av_fill_ip_info(av, attr);
  12. memcpy(av->dmac, attr->roce.dmac, ETH_ALEN);
  13. }
  14. int rxe_av_chk_attr(struct rxe_dev *rxe, struct rdma_ah_attr *attr)
  15. {
  16. const struct ib_global_route *grh = rdma_ah_read_grh(attr);
  17. struct rxe_port *port;
  18. int type;
  19. port = &rxe->port;
  20. if (rdma_ah_get_ah_flags(attr) & IB_AH_GRH) {
  21. if (grh->sgid_index > port->attr.gid_tbl_len) {
  22. pr_warn("invalid sgid index = %d\n",
  23. grh->sgid_index);
  24. return -EINVAL;
  25. }
  26. type = rdma_gid_attr_network_type(grh->sgid_attr);
  27. if (type < RDMA_NETWORK_IPV4 ||
  28. type > RDMA_NETWORK_IPV6) {
  29. pr_warn("invalid network type for rdma_rxe = %d\n",
  30. type);
  31. return -EINVAL;
  32. }
  33. }
  34. return 0;
  35. }
  36. void rxe_av_from_attr(u8 port_num, struct rxe_av *av,
  37. struct rdma_ah_attr *attr)
  38. {
  39. const struct ib_global_route *grh = rdma_ah_read_grh(attr);
  40. memset(av, 0, sizeof(*av));
  41. memcpy(av->grh.dgid.raw, grh->dgid.raw, sizeof(grh->dgid.raw));
  42. av->grh.flow_label = grh->flow_label;
  43. av->grh.sgid_index = grh->sgid_index;
  44. av->grh.hop_limit = grh->hop_limit;
  45. av->grh.traffic_class = grh->traffic_class;
  46. av->port_num = port_num;
  47. }
  48. void rxe_av_to_attr(struct rxe_av *av, struct rdma_ah_attr *attr)
  49. {
  50. struct ib_global_route *grh = rdma_ah_retrieve_grh(attr);
  51. attr->type = RDMA_AH_ATTR_TYPE_ROCE;
  52. memcpy(grh->dgid.raw, av->grh.dgid.raw, sizeof(av->grh.dgid.raw));
  53. grh->flow_label = av->grh.flow_label;
  54. grh->sgid_index = av->grh.sgid_index;
  55. grh->hop_limit = av->grh.hop_limit;
  56. grh->traffic_class = av->grh.traffic_class;
  57. rdma_ah_set_ah_flags(attr, IB_AH_GRH);
  58. rdma_ah_set_port_num(attr, av->port_num);
  59. }
  60. void rxe_av_fill_ip_info(struct rxe_av *av, struct rdma_ah_attr *attr)
  61. {
  62. const struct ib_gid_attr *sgid_attr = attr->grh.sgid_attr;
  63. int ibtype;
  64. int type;
  65. rdma_gid2ip((struct sockaddr *)&av->sgid_addr, &sgid_attr->gid);
  66. rdma_gid2ip((struct sockaddr *)&av->dgid_addr,
  67. &rdma_ah_read_grh(attr)->dgid);
  68. ibtype = rdma_gid_attr_network_type(sgid_attr);
  69. switch (ibtype) {
  70. case RDMA_NETWORK_IPV4:
  71. type = RXE_NETWORK_TYPE_IPV4;
  72. break;
  73. case RDMA_NETWORK_IPV6:
  74. type = RXE_NETWORK_TYPE_IPV6;
  75. break;
  76. default:
  77. /* not reached - checked in rxe_av_chk_attr */
  78. type = 0;
  79. break;
  80. }
  81. av->network_type = type;
  82. }
  83. struct rxe_av *rxe_get_av(struct rxe_pkt_info *pkt, struct rxe_ah **ahp)
  84. {
  85. struct rxe_ah *ah;
  86. u32 ah_num;
  87. if (ahp)
  88. *ahp = NULL;
  89. if (!pkt || !pkt->qp)
  90. return NULL;
  91. if (qp_type(pkt->qp) == IB_QPT_RC || qp_type(pkt->qp) == IB_QPT_UC)
  92. return &pkt->qp->pri_av;
  93. if (!pkt->wqe)
  94. return NULL;
  95. ah_num = pkt->wqe->wr.wr.ud.ah_num;
  96. if (ah_num) {
  97. /* only new user provider or kernel client */
  98. ah = rxe_pool_get_index(&pkt->rxe->ah_pool, ah_num);
  99. if (!ah) {
  100. pr_warn("Unable to find AH matching ah_num\n");
  101. return NULL;
  102. }
  103. if (rxe_ah_pd(ah) != pkt->qp->pd) {
  104. pr_warn("PDs don't match for AH and QP\n");
  105. rxe_put(ah);
  106. return NULL;
  107. }
  108. if (ahp)
  109. *ahp = ah;
  110. else
  111. rxe_put(ah);
  112. return &ah->av;
  113. }
  114. /* only old user provider for UD sends*/
  115. return &pkt->wqe->wr.wr.ud.av;
  116. }