123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934 |
- #include <linux/spinlock.h>
- #include <asm/unaligned.h>
- #include <rdma/ib_verbs.h>
- #include <rdma/rdma_cm.h>
- #include <linux/sunrpc/debug.h>
- #include <linux/sunrpc/rpc_rdma.h>
- #include <linux/sunrpc/svc_rdma.h>
- #include "xprt_rdma.h"
- #include <trace/events/rpcrdma.h>
- #define RPCDBG_FACILITY RPCDBG_SVCXPRT
- static void svc_rdma_wc_send(struct ib_cq *cq, struct ib_wc *wc);
- static inline struct svc_rdma_send_ctxt *
- svc_rdma_next_send_ctxt(struct list_head *list)
- {
- return list_first_entry_or_null(list, struct svc_rdma_send_ctxt,
- sc_list);
- }
- static struct svc_rdma_send_ctxt *
- svc_rdma_send_ctxt_alloc(struct svcxprt_rdma *rdma)
- {
- struct svc_rdma_send_ctxt *ctxt;
- dma_addr_t addr;
- void *buffer;
- size_t size;
- int i;
- size = sizeof(*ctxt);
- size += rdma->sc_max_send_sges * sizeof(struct ib_sge);
- ctxt = kmalloc(size, GFP_KERNEL);
- if (!ctxt)
- goto fail0;
- buffer = kmalloc(rdma->sc_max_req_size, GFP_KERNEL);
- if (!buffer)
- goto fail1;
- addr = ib_dma_map_single(rdma->sc_pd->device, buffer,
- rdma->sc_max_req_size, DMA_TO_DEVICE);
- if (ib_dma_mapping_error(rdma->sc_pd->device, addr))
- goto fail2;
- ctxt->sc_send_wr.next = NULL;
- ctxt->sc_send_wr.wr_cqe = &ctxt->sc_cqe;
- ctxt->sc_send_wr.sg_list = ctxt->sc_sges;
- ctxt->sc_send_wr.send_flags = IB_SEND_SIGNALED;
- ctxt->sc_cqe.done = svc_rdma_wc_send;
- ctxt->sc_xprt_buf = buffer;
- ctxt->sc_sges[0].addr = addr;
- for (i = 0; i < rdma->sc_max_send_sges; i++)
- ctxt->sc_sges[i].lkey = rdma->sc_pd->local_dma_lkey;
- return ctxt;
- fail2:
- kfree(buffer);
- fail1:
- kfree(ctxt);
- fail0:
- return NULL;
- }
- void svc_rdma_send_ctxts_destroy(struct svcxprt_rdma *rdma)
- {
- struct svc_rdma_send_ctxt *ctxt;
- while ((ctxt = svc_rdma_next_send_ctxt(&rdma->sc_send_ctxts))) {
- list_del(&ctxt->sc_list);
- ib_dma_unmap_single(rdma->sc_pd->device,
- ctxt->sc_sges[0].addr,
- rdma->sc_max_req_size,
- DMA_TO_DEVICE);
- kfree(ctxt->sc_xprt_buf);
- kfree(ctxt);
- }
- }
- struct svc_rdma_send_ctxt *svc_rdma_send_ctxt_get(struct svcxprt_rdma *rdma)
- {
- struct svc_rdma_send_ctxt *ctxt;
- spin_lock(&rdma->sc_send_lock);
- ctxt = svc_rdma_next_send_ctxt(&rdma->sc_send_ctxts);
- if (!ctxt)
- goto out_empty;
- list_del(&ctxt->sc_list);
- spin_unlock(&rdma->sc_send_lock);
- out:
- ctxt->sc_send_wr.num_sge = 0;
- ctxt->sc_cur_sge_no = 0;
- ctxt->sc_page_count = 0;
- return ctxt;
- out_empty:
- spin_unlock(&rdma->sc_send_lock);
- ctxt = svc_rdma_send_ctxt_alloc(rdma);
- if (!ctxt)
- return NULL;
- goto out;
- }
- void svc_rdma_send_ctxt_put(struct svcxprt_rdma *rdma,
- struct svc_rdma_send_ctxt *ctxt)
- {
- struct ib_device *device = rdma->sc_cm_id->device;
- unsigned int i;
-
- for (i = 1; i < ctxt->sc_send_wr.num_sge; i++)
- ib_dma_unmap_page(device,
- ctxt->sc_sges[i].addr,
- ctxt->sc_sges[i].length,
- DMA_TO_DEVICE);
- for (i = 0; i < ctxt->sc_page_count; ++i)
- put_page(ctxt->sc_pages[i]);
- spin_lock(&rdma->sc_send_lock);
- list_add(&ctxt->sc_list, &rdma->sc_send_ctxts);
- spin_unlock(&rdma->sc_send_lock);
- }
- static void svc_rdma_wc_send(struct ib_cq *cq, struct ib_wc *wc)
- {
- struct svcxprt_rdma *rdma = cq->cq_context;
- struct ib_cqe *cqe = wc->wr_cqe;
- struct svc_rdma_send_ctxt *ctxt;
- trace_svcrdma_wc_send(wc);
- atomic_inc(&rdma->sc_sq_avail);
- wake_up(&rdma->sc_send_wait);
- ctxt = container_of(cqe, struct svc_rdma_send_ctxt, sc_cqe);
- svc_rdma_send_ctxt_put(rdma, ctxt);
- if (unlikely(wc->status != IB_WC_SUCCESS)) {
- set_bit(XPT_CLOSE, &rdma->sc_xprt.xpt_flags);
- svc_xprt_enqueue(&rdma->sc_xprt);
- if (wc->status != IB_WC_WR_FLUSH_ERR)
- pr_err("svcrdma: Send: %s (%u/0x%x)\n",
- ib_wc_status_msg(wc->status),
- wc->status, wc->vendor_err);
- }
- svc_xprt_put(&rdma->sc_xprt);
- }
- int svc_rdma_send(struct svcxprt_rdma *rdma, struct ib_send_wr *wr)
- {
- int ret;
- might_sleep();
-
- while (1) {
- if ((atomic_dec_return(&rdma->sc_sq_avail) < 0)) {
- atomic_inc(&rdma_stat_sq_starve);
- trace_svcrdma_sq_full(rdma);
- atomic_inc(&rdma->sc_sq_avail);
- wait_event(rdma->sc_send_wait,
- atomic_read(&rdma->sc_sq_avail) > 1);
- if (test_bit(XPT_CLOSE, &rdma->sc_xprt.xpt_flags))
- return -ENOTCONN;
- trace_svcrdma_sq_retry(rdma);
- continue;
- }
- svc_xprt_get(&rdma->sc_xprt);
- ret = ib_post_send(rdma->sc_qp, wr, NULL);
- trace_svcrdma_post_send(wr, ret);
- if (ret) {
- set_bit(XPT_CLOSE, &rdma->sc_xprt.xpt_flags);
- svc_xprt_put(&rdma->sc_xprt);
- wake_up(&rdma->sc_send_wait);
- }
- break;
- }
- return ret;
- }
- static u32 xdr_padsize(u32 len)
- {
- return (len & 3) ? (4 - (len & 3)) : 0;
- }
- static unsigned int svc_rdma_reply_hdr_len(__be32 *rdma_resp)
- {
- unsigned int nsegs;
- __be32 *p;
- p = rdma_resp;
-
- p += rpcrdma_fixed_maxsz + 1;
-
- while (*p++ != xdr_zero) {
- nsegs = be32_to_cpup(p++);
- p += nsegs * rpcrdma_segment_maxsz;
- }
-
- if (*p++ != xdr_zero) {
- nsegs = be32_to_cpup(p++);
- p += nsegs * rpcrdma_segment_maxsz;
- }
- return (unsigned long)p - (unsigned long)rdma_resp;
- }
- static unsigned int xdr_encode_write_chunk(__be32 *dst, __be32 *src,
- unsigned int remaining)
- {
- unsigned int i, nsegs;
- u32 seg_len;
-
- *dst++ = *src++;
-
- nsegs = be32_to_cpup(src);
- *dst++ = *src++;
- for (i = nsegs; i; i--) {
-
- *dst++ = *src++;
-
- seg_len = be32_to_cpu(*src);
- if (remaining >= seg_len) {
-
- *dst = *src;
- remaining -= seg_len;
- } else {
-
- *dst = cpu_to_be32(remaining);
- remaining = 0;
- }
- dst++; src++;
-
- *dst++ = *src++;
- *dst++ = *src++;
- }
- return nsegs;
- }
- static void svc_rdma_xdr_encode_write_list(__be32 *rdma_resp, __be32 *wr_ch,
- unsigned int consumed)
- {
- unsigned int nsegs;
- __be32 *p, *q;
-
- p = rdma_resp + rpcrdma_fixed_maxsz + 1;
- q = wr_ch;
- while (*q != xdr_zero) {
- nsegs = xdr_encode_write_chunk(p, q, consumed);
- q += 2 + nsegs * rpcrdma_segment_maxsz;
- p += 2 + nsegs * rpcrdma_segment_maxsz;
- consumed = 0;
- }
-
- *p++ = xdr_zero;
-
- *p = xdr_zero;
- }
- static void svc_rdma_xdr_encode_reply_chunk(__be32 *rdma_resp, __be32 *rp_ch,
- unsigned int consumed)
- {
- __be32 *p;
-
- p = rdma_resp + rpcrdma_fixed_maxsz + 1;
-
- while (*p++ != xdr_zero)
- p += 1 + be32_to_cpup(p) * rpcrdma_segment_maxsz;
- xdr_encode_write_chunk(p, rp_ch, consumed);
- }
- static void svc_rdma_get_write_arrays(__be32 *rdma_argp,
- __be32 **write, __be32 **reply)
- {
- __be32 *p;
- p = rdma_argp + rpcrdma_fixed_maxsz;
-
- while (*p++ != xdr_zero)
- p += 5;
-
- if (*p != xdr_zero) {
- *write = p;
- while (*p++ != xdr_zero)
- p += 1 + be32_to_cpu(*p) * 4;
- } else {
- *write = NULL;
- p++;
- }
-
- if (*p != xdr_zero)
- *reply = p;
- else
- *reply = NULL;
- }
- static u32 svc_rdma_get_inv_rkey(__be32 *rdma_argp,
- __be32 *wr_lst, __be32 *rp_ch)
- {
- __be32 *p;
- p = rdma_argp + rpcrdma_fixed_maxsz;
- if (*p != xdr_zero)
- p += 2;
- else if (wr_lst && be32_to_cpup(wr_lst + 1))
- p = wr_lst + 2;
- else if (rp_ch && be32_to_cpup(rp_ch + 1))
- p = rp_ch + 2;
- else
- return 0;
- return be32_to_cpup(p);
- }
- static int svc_rdma_dma_map_page(struct svcxprt_rdma *rdma,
- struct svc_rdma_send_ctxt *ctxt,
- struct page *page,
- unsigned long offset,
- unsigned int len)
- {
- struct ib_device *dev = rdma->sc_cm_id->device;
- dma_addr_t dma_addr;
- dma_addr = ib_dma_map_page(dev, page, offset, len, DMA_TO_DEVICE);
- if (ib_dma_mapping_error(dev, dma_addr))
- goto out_maperr;
- ctxt->sc_sges[ctxt->sc_cur_sge_no].addr = dma_addr;
- ctxt->sc_sges[ctxt->sc_cur_sge_no].length = len;
- ctxt->sc_send_wr.num_sge++;
- return 0;
- out_maperr:
- trace_svcrdma_dma_map_page(rdma, page);
- return -EIO;
- }
- static int svc_rdma_dma_map_buf(struct svcxprt_rdma *rdma,
- struct svc_rdma_send_ctxt *ctxt,
- unsigned char *base,
- unsigned int len)
- {
- return svc_rdma_dma_map_page(rdma, ctxt, virt_to_page(base),
- offset_in_page(base), len);
- }
- void svc_rdma_sync_reply_hdr(struct svcxprt_rdma *rdma,
- struct svc_rdma_send_ctxt *ctxt,
- unsigned int len)
- {
- ctxt->sc_sges[0].length = len;
- ctxt->sc_send_wr.num_sge++;
- ib_dma_sync_single_for_device(rdma->sc_pd->device,
- ctxt->sc_sges[0].addr, len,
- DMA_TO_DEVICE);
- }
- static bool svc_rdma_pull_up_needed(struct svcxprt_rdma *rdma,
- struct xdr_buf *xdr,
- __be32 *wr_lst)
- {
- int elements;
-
- elements = 1;
-
- if (!wr_lst) {
- unsigned int remaining;
- unsigned long pageoff;
- pageoff = xdr->page_base & ~PAGE_MASK;
- remaining = xdr->page_len;
- while (remaining) {
- ++elements;
- remaining -= min_t(u32, PAGE_SIZE - pageoff,
- remaining);
- pageoff = 0;
- }
- }
-
- if (xdr->tail[0].iov_len)
- ++elements;
-
- return elements >= rdma->sc_max_send_sges;
- }
- static int svc_rdma_pull_up_reply_msg(struct svcxprt_rdma *rdma,
- struct svc_rdma_send_ctxt *ctxt,
- struct xdr_buf *xdr, __be32 *wr_lst)
- {
- unsigned char *dst, *tailbase;
- unsigned int taillen;
- dst = ctxt->sc_xprt_buf;
- dst += ctxt->sc_sges[0].length;
- memcpy(dst, xdr->head[0].iov_base, xdr->head[0].iov_len);
- dst += xdr->head[0].iov_len;
- tailbase = xdr->tail[0].iov_base;
- taillen = xdr->tail[0].iov_len;
- if (wr_lst) {
- u32 xdrpad;
- xdrpad = xdr_padsize(xdr->page_len);
- if (taillen && xdrpad) {
- tailbase += xdrpad;
- taillen -= xdrpad;
- }
- } else {
- unsigned int len, remaining;
- unsigned long pageoff;
- struct page **ppages;
- ppages = xdr->pages + (xdr->page_base >> PAGE_SHIFT);
- pageoff = xdr->page_base & ~PAGE_MASK;
- remaining = xdr->page_len;
- while (remaining) {
- len = min_t(u32, PAGE_SIZE - pageoff, remaining);
- memcpy(dst, page_address(*ppages), len);
- remaining -= len;
- dst += len;
- pageoff = 0;
- }
- }
- if (taillen)
- memcpy(dst, tailbase, taillen);
- ctxt->sc_sges[0].length += xdr->len;
- ib_dma_sync_single_for_device(rdma->sc_pd->device,
- ctxt->sc_sges[0].addr,
- ctxt->sc_sges[0].length,
- DMA_TO_DEVICE);
- return 0;
- }
- int svc_rdma_map_reply_msg(struct svcxprt_rdma *rdma,
- struct svc_rdma_send_ctxt *ctxt,
- struct xdr_buf *xdr, __be32 *wr_lst)
- {
- unsigned int len, remaining;
- unsigned long page_off;
- struct page **ppages;
- unsigned char *base;
- u32 xdr_pad;
- int ret;
- if (svc_rdma_pull_up_needed(rdma, xdr, wr_lst))
- return svc_rdma_pull_up_reply_msg(rdma, ctxt, xdr, wr_lst);
- ++ctxt->sc_cur_sge_no;
- ret = svc_rdma_dma_map_buf(rdma, ctxt,
- xdr->head[0].iov_base,
- xdr->head[0].iov_len);
- if (ret < 0)
- return ret;
-
- if (wr_lst) {
- base = xdr->tail[0].iov_base;
- len = xdr->tail[0].iov_len;
- xdr_pad = xdr_padsize(xdr->page_len);
- if (len && xdr_pad) {
- base += xdr_pad;
- len -= xdr_pad;
- }
- goto tail;
- }
- ppages = xdr->pages + (xdr->page_base >> PAGE_SHIFT);
- page_off = xdr->page_base & ~PAGE_MASK;
- remaining = xdr->page_len;
- while (remaining) {
- len = min_t(u32, PAGE_SIZE - page_off, remaining);
- ++ctxt->sc_cur_sge_no;
- ret = svc_rdma_dma_map_page(rdma, ctxt, *ppages++,
- page_off, len);
- if (ret < 0)
- return ret;
- remaining -= len;
- page_off = 0;
- }
- base = xdr->tail[0].iov_base;
- len = xdr->tail[0].iov_len;
- tail:
- if (len) {
- ++ctxt->sc_cur_sge_no;
- ret = svc_rdma_dma_map_buf(rdma, ctxt, base, len);
- if (ret < 0)
- return ret;
- }
- return 0;
- }
- static void svc_rdma_save_io_pages(struct svc_rqst *rqstp,
- struct svc_rdma_send_ctxt *ctxt)
- {
- int i, pages = rqstp->rq_next_page - rqstp->rq_respages;
- ctxt->sc_page_count += pages;
- for (i = 0; i < pages; i++) {
- ctxt->sc_pages[i] = rqstp->rq_respages[i];
- rqstp->rq_respages[i] = NULL;
- }
-
- rqstp->rq_next_page = rqstp->rq_respages;
- }
- static int svc_rdma_send_reply_msg(struct svcxprt_rdma *rdma,
- struct svc_rdma_send_ctxt *ctxt,
- __be32 *rdma_argp,
- struct svc_rqst *rqstp,
- __be32 *wr_lst, __be32 *rp_ch)
- {
- int ret;
- if (!rp_ch) {
- ret = svc_rdma_map_reply_msg(rdma, ctxt,
- &rqstp->rq_res, wr_lst);
- if (ret < 0)
- return ret;
- }
- svc_rdma_save_io_pages(rqstp, ctxt);
- ctxt->sc_send_wr.opcode = IB_WR_SEND;
- if (rdma->sc_snd_w_inv) {
- ctxt->sc_send_wr.ex.invalidate_rkey =
- svc_rdma_get_inv_rkey(rdma_argp, wr_lst, rp_ch);
- if (ctxt->sc_send_wr.ex.invalidate_rkey)
- ctxt->sc_send_wr.opcode = IB_WR_SEND_WITH_INV;
- }
- dprintk("svcrdma: posting Send WR with %u sge(s)\n",
- ctxt->sc_send_wr.num_sge);
- return svc_rdma_send(rdma, &ctxt->sc_send_wr);
- }
- static int svc_rdma_send_error_msg(struct svcxprt_rdma *rdma,
- struct svc_rdma_send_ctxt *ctxt,
- struct svc_rqst *rqstp)
- {
- __be32 *p;
- int ret;
- p = ctxt->sc_xprt_buf;
- trace_svcrdma_err_chunk(*p);
- p += 3;
- *p++ = rdma_error;
- *p = err_chunk;
- svc_rdma_sync_reply_hdr(rdma, ctxt, RPCRDMA_HDRLEN_ERR);
- svc_rdma_save_io_pages(rqstp, ctxt);
- ctxt->sc_send_wr.opcode = IB_WR_SEND;
- ret = svc_rdma_send(rdma, &ctxt->sc_send_wr);
- if (ret) {
- svc_rdma_send_ctxt_put(rdma, ctxt);
- return ret;
- }
- return 0;
- }
- void svc_rdma_prep_reply_hdr(struct svc_rqst *rqstp)
- {
- }
- int svc_rdma_sendto(struct svc_rqst *rqstp)
- {
- struct svc_xprt *xprt = rqstp->rq_xprt;
- struct svcxprt_rdma *rdma =
- container_of(xprt, struct svcxprt_rdma, sc_xprt);
- struct svc_rdma_recv_ctxt *rctxt = rqstp->rq_xprt_ctxt;
- __be32 *p, *rdma_argp, *rdma_resp, *wr_lst, *rp_ch;
- struct xdr_buf *xdr = &rqstp->rq_res;
- struct svc_rdma_send_ctxt *sctxt;
- int ret;
- rdma_argp = rctxt->rc_recv_buf;
- svc_rdma_get_write_arrays(rdma_argp, &wr_lst, &rp_ch);
-
- ret = -ENOMEM;
- sctxt = svc_rdma_send_ctxt_get(rdma);
- if (!sctxt)
- goto err0;
- rdma_resp = sctxt->sc_xprt_buf;
- p = rdma_resp;
- *p++ = *rdma_argp;
- *p++ = *(rdma_argp + 1);
- *p++ = rdma->sc_fc_credits;
- *p++ = rp_ch ? rdma_nomsg : rdma_msg;
-
- *p++ = xdr_zero;
- *p++ = xdr_zero;
- *p = xdr_zero;
- if (wr_lst) {
-
- ret = svc_rdma_send_write_chunk(rdma, wr_lst, xdr);
- if (ret < 0)
- goto err2;
- svc_rdma_xdr_encode_write_list(rdma_resp, wr_lst, ret);
- }
- if (rp_ch) {
- ret = svc_rdma_send_reply_chunk(rdma, rp_ch, wr_lst, xdr);
- if (ret < 0)
- goto err2;
- svc_rdma_xdr_encode_reply_chunk(rdma_resp, rp_ch, ret);
- }
- svc_rdma_sync_reply_hdr(rdma, sctxt, svc_rdma_reply_hdr_len(rdma_resp));
- ret = svc_rdma_send_reply_msg(rdma, sctxt, rdma_argp, rqstp,
- wr_lst, rp_ch);
- if (ret < 0)
- goto err1;
- ret = 0;
- out:
- rqstp->rq_xprt_ctxt = NULL;
- svc_rdma_recv_ctxt_put(rdma, rctxt);
- return ret;
- err2:
- if (ret != -E2BIG && ret != -EINVAL)
- goto err1;
- ret = svc_rdma_send_error_msg(rdma, sctxt, rqstp);
- if (ret < 0)
- goto err1;
- ret = 0;
- goto out;
- err1:
- svc_rdma_send_ctxt_put(rdma, sctxt);
- err0:
- trace_svcrdma_send_failed(rqstp, ret);
- set_bit(XPT_CLOSE, &xprt->xpt_flags);
- ret = -ENOTCONN;
- goto out;
- }
|