1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause */
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * Copyright (c) 2015-2017 Oracle. All rights reserved.
4*4882a593Smuzhiyun * Copyright (c) 2003-2007 Network Appliance, Inc. All rights reserved.
5*4882a593Smuzhiyun *
6*4882a593Smuzhiyun * This software is available to you under a choice of one of two
7*4882a593Smuzhiyun * licenses. You may choose to be licensed under the terms of the GNU
8*4882a593Smuzhiyun * General Public License (GPL) Version 2, available from the file
9*4882a593Smuzhiyun * COPYING in the main directory of this source tree, or the BSD-type
10*4882a593Smuzhiyun * license below:
11*4882a593Smuzhiyun *
12*4882a593Smuzhiyun * Redistribution and use in source and binary forms, with or without
13*4882a593Smuzhiyun * modification, are permitted provided that the following conditions
14*4882a593Smuzhiyun * are met:
15*4882a593Smuzhiyun *
16*4882a593Smuzhiyun * Redistributions of source code must retain the above copyright
17*4882a593Smuzhiyun * notice, this list of conditions and the following disclaimer.
18*4882a593Smuzhiyun *
19*4882a593Smuzhiyun * Redistributions in binary form must reproduce the above
20*4882a593Smuzhiyun * copyright notice, this list of conditions and the following
21*4882a593Smuzhiyun * disclaimer in the documentation and/or other materials provided
22*4882a593Smuzhiyun * with the distribution.
23*4882a593Smuzhiyun *
24*4882a593Smuzhiyun * Neither the name of the Network Appliance, Inc. nor the names of
25*4882a593Smuzhiyun * its contributors may be used to endorse or promote products
26*4882a593Smuzhiyun * derived from this software without specific prior written
27*4882a593Smuzhiyun * permission.
28*4882a593Smuzhiyun *
29*4882a593Smuzhiyun * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
30*4882a593Smuzhiyun * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
31*4882a593Smuzhiyun * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
32*4882a593Smuzhiyun * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
33*4882a593Smuzhiyun * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
34*4882a593Smuzhiyun * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
35*4882a593Smuzhiyun * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
36*4882a593Smuzhiyun * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
37*4882a593Smuzhiyun * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
38*4882a593Smuzhiyun * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
39*4882a593Smuzhiyun * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40*4882a593Smuzhiyun */
41*4882a593Smuzhiyun
42*4882a593Smuzhiyun #ifndef _LINUX_SUNRPC_RPC_RDMA_H
43*4882a593Smuzhiyun #define _LINUX_SUNRPC_RPC_RDMA_H
44*4882a593Smuzhiyun
45*4882a593Smuzhiyun #include <linux/types.h>
46*4882a593Smuzhiyun #include <linux/bitops.h>
47*4882a593Smuzhiyun
48*4882a593Smuzhiyun #define RPCRDMA_VERSION 1
49*4882a593Smuzhiyun #define rpcrdma_version cpu_to_be32(RPCRDMA_VERSION)
50*4882a593Smuzhiyun
51*4882a593Smuzhiyun enum {
52*4882a593Smuzhiyun RPCRDMA_V1_DEF_INLINE_SIZE = 1024,
53*4882a593Smuzhiyun };
54*4882a593Smuzhiyun
55*4882a593Smuzhiyun /*
56*4882a593Smuzhiyun * XDR sizes, in quads
57*4882a593Smuzhiyun */
58*4882a593Smuzhiyun enum {
59*4882a593Smuzhiyun rpcrdma_fixed_maxsz = 4,
60*4882a593Smuzhiyun rpcrdma_segment_maxsz = 4,
61*4882a593Smuzhiyun rpcrdma_readseg_maxsz = 1 + rpcrdma_segment_maxsz,
62*4882a593Smuzhiyun rpcrdma_readchunk_maxsz = 1 + rpcrdma_readseg_maxsz,
63*4882a593Smuzhiyun };
64*4882a593Smuzhiyun
65*4882a593Smuzhiyun /*
66*4882a593Smuzhiyun * Smallest RPC/RDMA header: rm_xid through rm_type, then rm_nochunks
67*4882a593Smuzhiyun */
68*4882a593Smuzhiyun #define RPCRDMA_HDRLEN_MIN (sizeof(__be32) * 7)
69*4882a593Smuzhiyun #define RPCRDMA_HDRLEN_ERR (sizeof(__be32) * 5)
70*4882a593Smuzhiyun
71*4882a593Smuzhiyun enum rpcrdma_errcode {
72*4882a593Smuzhiyun ERR_VERS = 1,
73*4882a593Smuzhiyun ERR_CHUNK = 2
74*4882a593Smuzhiyun };
75*4882a593Smuzhiyun
76*4882a593Smuzhiyun enum rpcrdma_proc {
77*4882a593Smuzhiyun RDMA_MSG = 0, /* An RPC call or reply msg */
78*4882a593Smuzhiyun RDMA_NOMSG = 1, /* An RPC call or reply msg - separate body */
79*4882a593Smuzhiyun RDMA_MSGP = 2, /* An RPC call or reply msg with padding */
80*4882a593Smuzhiyun RDMA_DONE = 3, /* Client signals reply completion */
81*4882a593Smuzhiyun RDMA_ERROR = 4 /* An RPC RDMA encoding error */
82*4882a593Smuzhiyun };
83*4882a593Smuzhiyun
84*4882a593Smuzhiyun #define rdma_msg cpu_to_be32(RDMA_MSG)
85*4882a593Smuzhiyun #define rdma_nomsg cpu_to_be32(RDMA_NOMSG)
86*4882a593Smuzhiyun #define rdma_msgp cpu_to_be32(RDMA_MSGP)
87*4882a593Smuzhiyun #define rdma_done cpu_to_be32(RDMA_DONE)
88*4882a593Smuzhiyun #define rdma_error cpu_to_be32(RDMA_ERROR)
89*4882a593Smuzhiyun
90*4882a593Smuzhiyun #define err_vers cpu_to_be32(ERR_VERS)
91*4882a593Smuzhiyun #define err_chunk cpu_to_be32(ERR_CHUNK)
92*4882a593Smuzhiyun
93*4882a593Smuzhiyun /*
94*4882a593Smuzhiyun * Private extension to RPC-over-RDMA Version One.
95*4882a593Smuzhiyun * Message passed during RDMA-CM connection set-up.
96*4882a593Smuzhiyun *
97*4882a593Smuzhiyun * Add new fields at the end, and don't permute existing
98*4882a593Smuzhiyun * fields.
99*4882a593Smuzhiyun */
100*4882a593Smuzhiyun struct rpcrdma_connect_private {
101*4882a593Smuzhiyun __be32 cp_magic;
102*4882a593Smuzhiyun u8 cp_version;
103*4882a593Smuzhiyun u8 cp_flags;
104*4882a593Smuzhiyun u8 cp_send_size;
105*4882a593Smuzhiyun u8 cp_recv_size;
106*4882a593Smuzhiyun } __packed;
107*4882a593Smuzhiyun
108*4882a593Smuzhiyun #define rpcrdma_cmp_magic __cpu_to_be32(0xf6ab0e18)
109*4882a593Smuzhiyun
110*4882a593Smuzhiyun enum {
111*4882a593Smuzhiyun RPCRDMA_CMP_VERSION = 1,
112*4882a593Smuzhiyun RPCRDMA_CMP_F_SND_W_INV_OK = BIT(0),
113*4882a593Smuzhiyun };
114*4882a593Smuzhiyun
115*4882a593Smuzhiyun static inline u8
rpcrdma_encode_buffer_size(unsigned int size)116*4882a593Smuzhiyun rpcrdma_encode_buffer_size(unsigned int size)
117*4882a593Smuzhiyun {
118*4882a593Smuzhiyun return (size >> 10) - 1;
119*4882a593Smuzhiyun }
120*4882a593Smuzhiyun
121*4882a593Smuzhiyun static inline unsigned int
rpcrdma_decode_buffer_size(u8 val)122*4882a593Smuzhiyun rpcrdma_decode_buffer_size(u8 val)
123*4882a593Smuzhiyun {
124*4882a593Smuzhiyun return ((unsigned int)val + 1) << 10;
125*4882a593Smuzhiyun }
126*4882a593Smuzhiyun
127*4882a593Smuzhiyun /**
128*4882a593Smuzhiyun * xdr_encode_rdma_segment - Encode contents of an RDMA segment
129*4882a593Smuzhiyun * @p: Pointer into a send buffer
130*4882a593Smuzhiyun * @handle: The RDMA handle to encode
131*4882a593Smuzhiyun * @length: The RDMA length to encode
132*4882a593Smuzhiyun * @offset: The RDMA offset to encode
133*4882a593Smuzhiyun *
134*4882a593Smuzhiyun * Return value:
135*4882a593Smuzhiyun * Pointer to the XDR position that follows the encoded RDMA segment
136*4882a593Smuzhiyun */
xdr_encode_rdma_segment(__be32 * p,u32 handle,u32 length,u64 offset)137*4882a593Smuzhiyun static inline __be32 *xdr_encode_rdma_segment(__be32 *p, u32 handle,
138*4882a593Smuzhiyun u32 length, u64 offset)
139*4882a593Smuzhiyun {
140*4882a593Smuzhiyun *p++ = cpu_to_be32(handle);
141*4882a593Smuzhiyun *p++ = cpu_to_be32(length);
142*4882a593Smuzhiyun return xdr_encode_hyper(p, offset);
143*4882a593Smuzhiyun }
144*4882a593Smuzhiyun
145*4882a593Smuzhiyun /**
146*4882a593Smuzhiyun * xdr_encode_read_segment - Encode contents of a Read segment
147*4882a593Smuzhiyun * @p: Pointer into a send buffer
148*4882a593Smuzhiyun * @position: The position to encode
149*4882a593Smuzhiyun * @handle: The RDMA handle to encode
150*4882a593Smuzhiyun * @length: The RDMA length to encode
151*4882a593Smuzhiyun * @offset: The RDMA offset to encode
152*4882a593Smuzhiyun *
153*4882a593Smuzhiyun * Return value:
154*4882a593Smuzhiyun * Pointer to the XDR position that follows the encoded Read segment
155*4882a593Smuzhiyun */
xdr_encode_read_segment(__be32 * p,u32 position,u32 handle,u32 length,u64 offset)156*4882a593Smuzhiyun static inline __be32 *xdr_encode_read_segment(__be32 *p, u32 position,
157*4882a593Smuzhiyun u32 handle, u32 length,
158*4882a593Smuzhiyun u64 offset)
159*4882a593Smuzhiyun {
160*4882a593Smuzhiyun *p++ = cpu_to_be32(position);
161*4882a593Smuzhiyun return xdr_encode_rdma_segment(p, handle, length, offset);
162*4882a593Smuzhiyun }
163*4882a593Smuzhiyun
164*4882a593Smuzhiyun /**
165*4882a593Smuzhiyun * xdr_decode_rdma_segment - Decode contents of an RDMA segment
166*4882a593Smuzhiyun * @p: Pointer to the undecoded RDMA segment
167*4882a593Smuzhiyun * @handle: Upon return, the RDMA handle
168*4882a593Smuzhiyun * @length: Upon return, the RDMA length
169*4882a593Smuzhiyun * @offset: Upon return, the RDMA offset
170*4882a593Smuzhiyun *
171*4882a593Smuzhiyun * Return value:
172*4882a593Smuzhiyun * Pointer to the XDR item that follows the RDMA segment
173*4882a593Smuzhiyun */
xdr_decode_rdma_segment(__be32 * p,u32 * handle,u32 * length,u64 * offset)174*4882a593Smuzhiyun static inline __be32 *xdr_decode_rdma_segment(__be32 *p, u32 *handle,
175*4882a593Smuzhiyun u32 *length, u64 *offset)
176*4882a593Smuzhiyun {
177*4882a593Smuzhiyun *handle = be32_to_cpup(p++);
178*4882a593Smuzhiyun *length = be32_to_cpup(p++);
179*4882a593Smuzhiyun return xdr_decode_hyper(p, offset);
180*4882a593Smuzhiyun }
181*4882a593Smuzhiyun
182*4882a593Smuzhiyun /**
183*4882a593Smuzhiyun * xdr_decode_read_segment - Decode contents of a Read segment
184*4882a593Smuzhiyun * @p: Pointer to the undecoded Read segment
185*4882a593Smuzhiyun * @position: Upon return, the segment's position
186*4882a593Smuzhiyun * @handle: Upon return, the RDMA handle
187*4882a593Smuzhiyun * @length: Upon return, the RDMA length
188*4882a593Smuzhiyun * @offset: Upon return, the RDMA offset
189*4882a593Smuzhiyun *
190*4882a593Smuzhiyun * Return value:
191*4882a593Smuzhiyun * Pointer to the XDR item that follows the Read segment
192*4882a593Smuzhiyun */
xdr_decode_read_segment(__be32 * p,u32 * position,u32 * handle,u32 * length,u64 * offset)193*4882a593Smuzhiyun static inline __be32 *xdr_decode_read_segment(__be32 *p, u32 *position,
194*4882a593Smuzhiyun u32 *handle, u32 *length,
195*4882a593Smuzhiyun u64 *offset)
196*4882a593Smuzhiyun {
197*4882a593Smuzhiyun *position = be32_to_cpup(p++);
198*4882a593Smuzhiyun return xdr_decode_rdma_segment(p, handle, length, offset);
199*4882a593Smuzhiyun }
200*4882a593Smuzhiyun
201*4882a593Smuzhiyun #endif /* _LINUX_SUNRPC_RPC_RDMA_H */
202