^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * RDMA Transport Layer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (c) 2014 - 2018 ProfitBricks GmbH. All rights reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Copyright (c) 2018 - 2019 1&1 IONOS Cloud GmbH. All rights reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Copyright (c) 2019 - 2020 1&1 IONOS SE. All rights reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #undef pr_fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #define pr_fmt(fmt) KBUILD_MODNAME " L" __stringify(__LINE__) ": " fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/inet.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include "rtrs-pri.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include "rtrs-log.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) MODULE_DESCRIPTION("RDMA Transport Core");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) struct rtrs_iu *rtrs_iu_alloc(u32 queue_size, size_t size, gfp_t gfp_mask,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) struct ib_device *dma_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) enum dma_data_direction dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) void (*done)(struct ib_cq *cq, struct ib_wc *wc))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) struct rtrs_iu *ius, *iu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) ius = kcalloc(queue_size, sizeof(*ius), gfp_mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) if (!ius)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) for (i = 0; i < queue_size; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) iu = &ius[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) iu->direction = dir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) iu->buf = kzalloc(size, gfp_mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) if (!iu->buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) iu->dma_addr = ib_dma_map_single(dma_dev, iu->buf, size, dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) if (ib_dma_mapping_error(dma_dev, iu->dma_addr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) iu->cqe.done = done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) iu->size = size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) return ius;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) rtrs_iu_free(ius, dma_dev, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) EXPORT_SYMBOL_GPL(rtrs_iu_alloc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) void rtrs_iu_free(struct rtrs_iu *ius, struct ib_device *ibdev, u32 queue_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) struct rtrs_iu *iu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) if (!ius)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) for (i = 0; i < queue_size; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) iu = &ius[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) ib_dma_unmap_single(ibdev, iu->dma_addr, iu->size, iu->direction);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) kfree(iu->buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) kfree(ius);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) EXPORT_SYMBOL_GPL(rtrs_iu_free);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) int rtrs_iu_post_recv(struct rtrs_con *con, struct rtrs_iu *iu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) struct rtrs_sess *sess = con->sess;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) struct ib_recv_wr wr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) struct ib_sge list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) list.addr = iu->dma_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) list.length = iu->size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) list.lkey = sess->dev->ib_pd->local_dma_lkey;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) if (list.length == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) rtrs_wrn(con->sess,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) "Posting receive work request failed, sg list is empty\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) wr = (struct ib_recv_wr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) .wr_cqe = &iu->cqe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) .sg_list = &list,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) .num_sge = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) return ib_post_recv(con->qp, &wr, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) EXPORT_SYMBOL_GPL(rtrs_iu_post_recv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) int rtrs_post_recv_empty(struct rtrs_con *con, struct ib_cqe *cqe)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) struct ib_recv_wr wr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) wr = (struct ib_recv_wr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) .wr_cqe = cqe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) return ib_post_recv(con->qp, &wr, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) EXPORT_SYMBOL_GPL(rtrs_post_recv_empty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) static int rtrs_post_send(struct ib_qp *qp, struct ib_send_wr *head,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) struct ib_send_wr *wr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) if (head) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) struct ib_send_wr *tail = head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) while (tail->next)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) tail = tail->next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) tail->next = wr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) head = wr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) return ib_post_send(qp, head, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) int rtrs_iu_post_send(struct rtrs_con *con, struct rtrs_iu *iu, size_t size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) struct ib_send_wr *head)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) struct rtrs_sess *sess = con->sess;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) struct ib_send_wr wr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) struct ib_sge list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) if (WARN_ON(size == 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) list.addr = iu->dma_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) list.length = size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) list.lkey = sess->dev->ib_pd->local_dma_lkey;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) wr = (struct ib_send_wr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) .wr_cqe = &iu->cqe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) .sg_list = &list,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) .num_sge = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) .opcode = IB_WR_SEND,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) .send_flags = IB_SEND_SIGNALED,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) return rtrs_post_send(con->qp, head, &wr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) EXPORT_SYMBOL_GPL(rtrs_iu_post_send);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) int rtrs_iu_post_rdma_write_imm(struct rtrs_con *con, struct rtrs_iu *iu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) struct ib_sge *sge, unsigned int num_sge,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) u32 rkey, u64 rdma_addr, u32 imm_data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) enum ib_send_flags flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) struct ib_send_wr *head)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) struct ib_rdma_wr wr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) wr = (struct ib_rdma_wr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) .wr.wr_cqe = &iu->cqe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) .wr.sg_list = sge,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) .wr.num_sge = num_sge,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) .rkey = rkey,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) .remote_addr = rdma_addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) .wr.opcode = IB_WR_RDMA_WRITE_WITH_IMM,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) .wr.ex.imm_data = cpu_to_be32(imm_data),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) .wr.send_flags = flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) * If one of the sges has 0 size, the operation will fail with a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) * length error
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) for (i = 0; i < num_sge; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) if (WARN_ON(sge[i].length == 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) return rtrs_post_send(con->qp, head, &wr.wr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) EXPORT_SYMBOL_GPL(rtrs_iu_post_rdma_write_imm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) int rtrs_post_rdma_write_imm_empty(struct rtrs_con *con, struct ib_cqe *cqe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) u32 imm_data, enum ib_send_flags flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) struct ib_send_wr *head)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) struct ib_rdma_wr wr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) wr = (struct ib_rdma_wr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) .wr.wr_cqe = cqe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) .wr.send_flags = flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) .wr.opcode = IB_WR_RDMA_WRITE_WITH_IMM,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) .wr.ex.imm_data = cpu_to_be32(imm_data),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) return rtrs_post_send(con->qp, head, &wr.wr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) EXPORT_SYMBOL_GPL(rtrs_post_rdma_write_imm_empty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) static void qp_event_handler(struct ib_event *ev, void *ctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) struct rtrs_con *con = ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) switch (ev->event) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) case IB_EVENT_COMM_EST:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) rtrs_info(con->sess, "QP event %s (%d) received\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) ib_event_msg(ev->event), ev->event);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) rdma_notify(con->cm_id, IB_EVENT_COMM_EST);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) rtrs_info(con->sess, "Unhandled QP event %s (%d) received\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) ib_event_msg(ev->event), ev->event);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) static int create_cq(struct rtrs_con *con, int cq_vector, u16 cq_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) enum ib_poll_context poll_ctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) struct rdma_cm_id *cm_id = con->cm_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) struct ib_cq *cq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) cq = ib_alloc_cq(cm_id->device, con, cq_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) cq_vector, poll_ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) if (IS_ERR(cq)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) rtrs_err(con->sess, "Creating completion queue failed, errno: %ld\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) PTR_ERR(cq));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) return PTR_ERR(cq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) con->cq = cq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) static int create_qp(struct rtrs_con *con, struct ib_pd *pd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) u32 max_send_wr, u32 max_recv_wr, u32 max_sge)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) struct ib_qp_init_attr init_attr = {NULL};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) struct rdma_cm_id *cm_id = con->cm_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) init_attr.cap.max_send_wr = max_send_wr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) init_attr.cap.max_recv_wr = max_recv_wr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) init_attr.cap.max_recv_sge = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) init_attr.event_handler = qp_event_handler;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) init_attr.qp_context = con;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) init_attr.cap.max_send_sge = max_sge;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) init_attr.qp_type = IB_QPT_RC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) init_attr.send_cq = con->cq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) init_attr.recv_cq = con->cq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) init_attr.sq_sig_type = IB_SIGNAL_REQ_WR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) ret = rdma_create_qp(cm_id, pd, &init_attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) rtrs_err(con->sess, "Creating QP failed, err: %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) con->qp = cm_id->qp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) int rtrs_cq_qp_create(struct rtrs_sess *sess, struct rtrs_con *con,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) u32 max_send_sge, int cq_vector, int cq_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) u32 max_send_wr, u32 max_recv_wr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) enum ib_poll_context poll_ctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) err = create_cq(con, cq_vector, cq_size, poll_ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) err = create_qp(con, sess->dev->ib_pd, max_send_wr, max_recv_wr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) max_send_sge);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) ib_free_cq(con->cq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) con->cq = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) con->sess = sess;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) EXPORT_SYMBOL_GPL(rtrs_cq_qp_create);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) void rtrs_cq_qp_destroy(struct rtrs_con *con)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) if (con->qp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) rdma_destroy_qp(con->cm_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) con->qp = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) if (con->cq) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) ib_free_cq(con->cq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) con->cq = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) EXPORT_SYMBOL_GPL(rtrs_cq_qp_destroy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) static void schedule_hb(struct rtrs_sess *sess)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) queue_delayed_work(sess->hb_wq, &sess->hb_dwork,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) msecs_to_jiffies(sess->hb_interval_ms));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) void rtrs_send_hb_ack(struct rtrs_sess *sess)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) struct rtrs_con *usr_con = sess->con[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) u32 imm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) imm = rtrs_to_imm(RTRS_HB_ACK_IMM, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) err = rtrs_post_rdma_write_imm_empty(usr_con, sess->hb_cqe, imm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 0, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) sess->hb_err_handler(usr_con);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) EXPORT_SYMBOL_GPL(rtrs_send_hb_ack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) static void hb_work(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) struct rtrs_con *usr_con;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) struct rtrs_sess *sess;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) u32 imm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) sess = container_of(to_delayed_work(work), typeof(*sess), hb_dwork);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) usr_con = sess->con[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) if (sess->hb_missed_cnt > sess->hb_missed_max) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) sess->hb_err_handler(usr_con);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) if (sess->hb_missed_cnt++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) /* Reschedule work without sending hb */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) schedule_hb(sess);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) imm = rtrs_to_imm(RTRS_HB_MSG_IMM, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) err = rtrs_post_rdma_write_imm_empty(usr_con, sess->hb_cqe, imm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 0, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) sess->hb_err_handler(usr_con);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) schedule_hb(sess);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) void rtrs_init_hb(struct rtrs_sess *sess, struct ib_cqe *cqe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) unsigned int interval_ms, unsigned int missed_max,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) void (*err_handler)(struct rtrs_con *con),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) struct workqueue_struct *wq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) sess->hb_cqe = cqe;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) sess->hb_interval_ms = interval_ms;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) sess->hb_err_handler = err_handler;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) sess->hb_wq = wq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) sess->hb_missed_max = missed_max;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) sess->hb_missed_cnt = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) INIT_DELAYED_WORK(&sess->hb_dwork, hb_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) EXPORT_SYMBOL_GPL(rtrs_init_hb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) void rtrs_start_hb(struct rtrs_sess *sess)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) schedule_hb(sess);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) EXPORT_SYMBOL_GPL(rtrs_start_hb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) void rtrs_stop_hb(struct rtrs_sess *sess)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) cancel_delayed_work_sync(&sess->hb_dwork);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) sess->hb_missed_cnt = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) EXPORT_SYMBOL_GPL(rtrs_stop_hb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) static int rtrs_str_gid_to_sockaddr(const char *addr, size_t len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) short port, struct sockaddr_storage *dst)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) struct sockaddr_ib *dst_ib = (struct sockaddr_ib *)dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) * We can use some of the IPv6 functions since GID is a valid
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) * IPv6 address format
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) ret = in6_pton(addr, len, dst_ib->sib_addr.sib_raw, '\0', NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) if (ret == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) dst_ib->sib_family = AF_IB;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) * Use the same TCP server port number as the IB service ID
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) * on the IB port space range
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) dst_ib->sib_sid = cpu_to_be64(RDMA_IB_IP_PS_IB | port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) dst_ib->sib_sid_mask = cpu_to_be64(0xffffffffffffffffULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) dst_ib->sib_pkey = cpu_to_be16(0xffff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) * rtrs_str_to_sockaddr() - Convert rtrs address string to sockaddr
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) * @addr: String representation of an addr (IPv4, IPv6 or IB GID):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) * - "ip:192.168.1.1"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) * - "ip:fe80::200:5aee:feaa:20a2"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) * - "gid:fe80::200:5aee:feaa:20a2"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) * @len: String address length
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) * @port: Destination port
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) * @dst: Destination sockaddr structure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) * Returns 0 if conversion successful. Non-zero on error.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) static int rtrs_str_to_sockaddr(const char *addr, size_t len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) u16 port, struct sockaddr_storage *dst)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) if (strncmp(addr, "gid:", 4) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) return rtrs_str_gid_to_sockaddr(addr + 4, len - 4, port, dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) } else if (strncmp(addr, "ip:", 3) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) char port_str[8];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) char *cpy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) snprintf(port_str, sizeof(port_str), "%u", port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) cpy = kstrndup(addr + 3, len - 3, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) err = cpy ? inet_pton_with_scope(&init_net, AF_UNSPEC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) cpy, port_str, dst) : -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) kfree(cpy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) return -EPROTONOSUPPORT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) * sockaddr_to_str() - convert sockaddr to a string.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) * @addr: the sockadddr structure to be converted.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) * @buf: string containing socket addr.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) * @len: string length.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) * The return value is the number of characters written into buf not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) * including the trailing '\0'. If len is == 0 the function returns 0..
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) int sockaddr_to_str(const struct sockaddr *addr, char *buf, size_t len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) switch (addr->sa_family) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) case AF_IB:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) return scnprintf(buf, len, "gid:%pI6",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) &((struct sockaddr_ib *)addr)->sib_addr.sib_raw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) case AF_INET:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) return scnprintf(buf, len, "ip:%pI4",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) &((struct sockaddr_in *)addr)->sin_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) case AF_INET6:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) return scnprintf(buf, len, "ip:%pI6c",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) &((struct sockaddr_in6 *)addr)->sin6_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) return scnprintf(buf, len, "<invalid address family>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) EXPORT_SYMBOL(sockaddr_to_str);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) * rtrs_addr_to_sockaddr() - convert path string "src,dst" or "src@dst"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) * to sockaddreses
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) * @str: string containing source and destination addr of a path
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) * separated by ',' or '@' I.e. "ip:1.1.1.1,ip:1.1.1.2" or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) * "ip:1.1.1.1@ip:1.1.1.2". If str contains only one address it's
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) * considered to be destination.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) * @len: string length
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) * @port: Destination port number.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) * @addr: will be set to the source/destination address or to NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) * if str doesn't contain any source address.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) * Returns zero if conversion successful. Non-zero otherwise.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) int rtrs_addr_to_sockaddr(const char *str, size_t len, u16 port,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) struct rtrs_addr *addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) const char *d;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) d = strchr(str, ',');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) if (!d)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) d = strchr(str, '@');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) if (d) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) if (rtrs_str_to_sockaddr(str, d - str, 0, addr->src))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) d += 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) len -= d - str;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) str = d;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) addr->src = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) return rtrs_str_to_sockaddr(str, len, port, addr->dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) EXPORT_SYMBOL(rtrs_addr_to_sockaddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) void rtrs_rdma_dev_pd_init(enum ib_pd_flags pd_flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) struct rtrs_rdma_dev_pd *pool)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) WARN_ON(pool->ops && (!pool->ops->alloc ^ !pool->ops->free));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) INIT_LIST_HEAD(&pool->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) mutex_init(&pool->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) pool->pd_flags = pd_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) EXPORT_SYMBOL(rtrs_rdma_dev_pd_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) void rtrs_rdma_dev_pd_deinit(struct rtrs_rdma_dev_pd *pool)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) mutex_destroy(&pool->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) WARN_ON(!list_empty(&pool->list));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) EXPORT_SYMBOL(rtrs_rdma_dev_pd_deinit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) static void dev_free(struct kref *ref)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) struct rtrs_rdma_dev_pd *pool;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) struct rtrs_ib_dev *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) dev = container_of(ref, typeof(*dev), ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) pool = dev->pool;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) mutex_lock(&pool->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) list_del(&dev->entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) mutex_unlock(&pool->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) if (pool->ops && pool->ops->deinit)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) pool->ops->deinit(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) ib_dealloc_pd(dev->ib_pd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) if (pool->ops && pool->ops->free)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) pool->ops->free(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) kfree(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) int rtrs_ib_dev_put(struct rtrs_ib_dev *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) return kref_put(&dev->ref, dev_free);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) EXPORT_SYMBOL(rtrs_ib_dev_put);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) static int rtrs_ib_dev_get(struct rtrs_ib_dev *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) return kref_get_unless_zero(&dev->ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) struct rtrs_ib_dev *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) rtrs_ib_dev_find_or_add(struct ib_device *ib_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) struct rtrs_rdma_dev_pd *pool)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) struct rtrs_ib_dev *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) mutex_lock(&pool->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) list_for_each_entry(dev, &pool->list, entry) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) if (dev->ib_dev->node_guid == ib_dev->node_guid &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) rtrs_ib_dev_get(dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) mutex_unlock(&pool->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) if (pool->ops && pool->ops->alloc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) dev = pool->ops->alloc();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) dev = kzalloc(sizeof(*dev), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) if (IS_ERR_OR_NULL(dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) kref_init(&dev->ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) dev->pool = pool;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) dev->ib_dev = ib_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) dev->ib_pd = ib_alloc_pd(ib_dev, pool->pd_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) if (IS_ERR(dev->ib_pd))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) goto out_free_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) if (pool->ops && pool->ops->init && pool->ops->init(dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) goto out_free_pd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) mutex_lock(&pool->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) list_add(&dev->entry, &pool->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) out_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) mutex_unlock(&pool->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) return dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) out_free_pd:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) ib_dealloc_pd(dev->ib_pd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) out_free_dev:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) if (pool->ops && pool->ops->free)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) pool->ops->free(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) kfree(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) out_err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) EXPORT_SYMBOL(rtrs_ib_dev_find_or_add);