Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 #ifndef _RDS_IB_H
0003 #define _RDS_IB_H
0004 
0005 #include <rdma/ib_verbs.h>
0006 #include <rdma/rdma_cm.h>
0007 #include <linux/interrupt.h>
0008 #include <linux/pci.h>
0009 #include <linux/slab.h>
0010 #include "rds.h"
0011 #include "rdma_transport.h"
0012 
0013 #define RDS_IB_MAX_SGE          8
0014 #define RDS_IB_RECV_SGE         2
0015 
0016 #define RDS_IB_DEFAULT_RECV_WR      1024
0017 #define RDS_IB_DEFAULT_SEND_WR      256
0018 #define RDS_IB_DEFAULT_FR_WR        512
0019 
0020 #define RDS_IB_DEFAULT_RETRY_COUNT  1
0021 
0022 #define RDS_IB_SUPPORTED_PROTOCOLS  0x00000003  /* minor versions supported */
0023 
0024 #define RDS_IB_RECYCLE_BATCH_COUNT  32
0025 
0026 #define RDS_IB_WC_MAX           32
0027 
0028 extern struct rw_semaphore rds_ib_devices_lock;
0029 extern struct list_head rds_ib_devices;
0030 
0031 /*
0032  * IB posts RDS_FRAG_SIZE fragments of pages to the receive queues to
0033  * try and minimize the amount of memory tied up both the device and
0034  * socket receive queues.
0035  */
0036 struct rds_page_frag {
0037     struct list_head    f_item;
0038     struct list_head    f_cache_entry;
0039     struct scatterlist  f_sg;
0040 };
0041 
0042 struct rds_ib_incoming {
0043     struct list_head    ii_frags;
0044     struct list_head    ii_cache_entry;
0045     struct rds_incoming ii_inc;
0046 };
0047 
0048 struct rds_ib_cache_head {
0049     struct list_head *first;
0050     unsigned long count;
0051 };
0052 
0053 struct rds_ib_refill_cache {
0054     struct rds_ib_cache_head __percpu *percpu;
0055     struct list_head     *xfer;
0056     struct list_head     *ready;
0057 };
0058 
0059 /* This is the common structure for the IB private data exchange in setting up
0060  * an RDS connection.  The exchange is different for IPv4 and IPv6 connections.
0061  * The reason is that the address size is different and the addresses
0062  * exchanged are in the beginning of the structure.  Hence it is not possible
0063  * for interoperability if same structure is used.
0064  */
0065 struct rds_ib_conn_priv_cmn {
0066     u8          ricpc_protocol_major;
0067     u8          ricpc_protocol_minor;
0068     __be16          ricpc_protocol_minor_mask;  /* bitmask */
0069     u8          ricpc_dp_toss;
0070     u8          ripc_reserved1;
0071     __be16          ripc_reserved2;
0072     __be64          ricpc_ack_seq;
0073     __be32          ricpc_credit;   /* non-zero enables flow ctl */
0074 };
0075 
0076 struct rds_ib_connect_private {
0077     /* Add new fields at the end, and don't permute existing fields. */
0078     __be32              dp_saddr;
0079     __be32              dp_daddr;
0080     struct rds_ib_conn_priv_cmn dp_cmn;
0081 };
0082 
0083 struct rds6_ib_connect_private {
0084     /* Add new fields at the end, and don't permute existing fields. */
0085     struct in6_addr         dp_saddr;
0086     struct in6_addr         dp_daddr;
0087     struct rds_ib_conn_priv_cmn dp_cmn;
0088 };
0089 
0090 #define dp_protocol_major   dp_cmn.ricpc_protocol_major
0091 #define dp_protocol_minor   dp_cmn.ricpc_protocol_minor
0092 #define dp_protocol_minor_mask  dp_cmn.ricpc_protocol_minor_mask
0093 #define dp_ack_seq      dp_cmn.ricpc_ack_seq
0094 #define dp_credit       dp_cmn.ricpc_credit
0095 
0096 union rds_ib_conn_priv {
0097     struct rds_ib_connect_private   ricp_v4;
0098     struct rds6_ib_connect_private  ricp_v6;
0099 };
0100 
0101 struct rds_ib_send_work {
0102     void            *s_op;
0103     union {
0104         struct ib_send_wr   s_wr;
0105         struct ib_rdma_wr   s_rdma_wr;
0106         struct ib_atomic_wr s_atomic_wr;
0107     };
0108     struct ib_sge       s_sge[RDS_IB_MAX_SGE];
0109     unsigned long       s_queued;
0110 };
0111 
0112 struct rds_ib_recv_work {
0113     struct rds_ib_incoming  *r_ibinc;
0114     struct rds_page_frag    *r_frag;
0115     struct ib_recv_wr   r_wr;
0116     struct ib_sge       r_sge[2];
0117 };
0118 
0119 struct rds_ib_work_ring {
0120     u32     w_nr;
0121     u32     w_alloc_ptr;
0122     u32     w_alloc_ctr;
0123     u32     w_free_ptr;
0124     atomic_t    w_free_ctr;
0125 };
0126 
0127 /* Rings are posted with all the allocations they'll need to queue the
0128  * incoming message to the receiving socket so this can't fail.
0129  * All fragments start with a header, so we can make sure we're not receiving
0130  * garbage, and we can tell a small 8 byte fragment from an ACK frame.
0131  */
0132 struct rds_ib_ack_state {
0133     u64     ack_next;
0134     u64     ack_recv;
0135     unsigned int    ack_required:1;
0136     unsigned int    ack_next_valid:1;
0137     unsigned int    ack_recv_valid:1;
0138 };
0139 
0140 
0141 struct rds_ib_device;
0142 
0143 struct rds_ib_connection {
0144 
0145     struct list_head    ib_node;
0146     struct rds_ib_device    *rds_ibdev;
0147     struct rds_connection   *conn;
0148 
0149     /* alphabet soup, IBTA style */
0150     struct rdma_cm_id   *i_cm_id;
0151     struct ib_pd        *i_pd;
0152     struct ib_cq        *i_send_cq;
0153     struct ib_cq        *i_recv_cq;
0154     struct ib_wc        i_send_wc[RDS_IB_WC_MAX];
0155     struct ib_wc        i_recv_wc[RDS_IB_WC_MAX];
0156 
0157     /* To control the number of wrs from fastreg */
0158     atomic_t        i_fastreg_wrs;
0159     atomic_t        i_fastreg_inuse_count;
0160 
0161     /* interrupt handling */
0162     struct tasklet_struct   i_send_tasklet;
0163     struct tasklet_struct   i_recv_tasklet;
0164 
0165     /* tx */
0166     struct rds_ib_work_ring i_send_ring;
0167     struct rm_data_op   *i_data_op;
0168     struct rds_header   **i_send_hdrs;
0169     dma_addr_t      *i_send_hdrs_dma;
0170     struct rds_ib_send_work *i_sends;
0171     atomic_t        i_signaled_sends;
0172 
0173     /* rx */
0174     struct mutex        i_recv_mutex;
0175     struct rds_ib_work_ring i_recv_ring;
0176     struct rds_ib_incoming  *i_ibinc;
0177     u32         i_recv_data_rem;
0178     struct rds_header   **i_recv_hdrs;
0179     dma_addr_t      *i_recv_hdrs_dma;
0180     struct rds_ib_recv_work *i_recvs;
0181     u64         i_ack_recv; /* last ACK received */
0182     struct rds_ib_refill_cache i_cache_incs;
0183     struct rds_ib_refill_cache i_cache_frags;
0184     atomic_t        i_cache_allocs;
0185 
0186     /* sending acks */
0187     unsigned long       i_ack_flags;
0188 #ifdef KERNEL_HAS_ATOMIC64
0189     atomic64_t      i_ack_next; /* next ACK to send */
0190 #else
0191     spinlock_t      i_ack_lock; /* protect i_ack_next */
0192     u64         i_ack_next; /* next ACK to send */
0193 #endif
0194     struct rds_header   *i_ack;
0195     struct ib_send_wr   i_ack_wr;
0196     struct ib_sge       i_ack_sge;
0197     dma_addr_t      i_ack_dma;
0198     unsigned long       i_ack_queued;
0199 
0200     /* Flow control related information
0201      *
0202      * Our algorithm uses a pair variables that we need to access
0203      * atomically - one for the send credits, and one posted
0204      * recv credits we need to transfer to remote.
0205      * Rather than protect them using a slow spinlock, we put both into
0206      * a single atomic_t and update it using cmpxchg
0207      */
0208     atomic_t        i_credits;
0209 
0210     /* Protocol version specific information */
0211     unsigned int        i_flowctl:1;    /* enable/disable flow ctl */
0212 
0213     /* Batched completions */
0214     unsigned int        i_unsignaled_wrs;
0215 
0216     /* Endpoint role in connection */
0217     bool            i_active_side;
0218     atomic_t        i_cq_quiesce;
0219 
0220     /* Send/Recv vectors */
0221     int         i_scq_vector;
0222     int         i_rcq_vector;
0223     u8          i_sl;
0224 };
0225 
0226 /* This assumes that atomic_t is at least 32 bits */
0227 #define IB_GET_SEND_CREDITS(v)  ((v) & 0xffff)
0228 #define IB_GET_POST_CREDITS(v)  ((v) >> 16)
0229 #define IB_SET_SEND_CREDITS(v)  ((v) & 0xffff)
0230 #define IB_SET_POST_CREDITS(v)  ((v) << 16)
0231 
0232 struct rds_ib_ipaddr {
0233     struct list_head    list;
0234     __be32          ipaddr;
0235     struct rcu_head     rcu;
0236 };
0237 
0238 enum {
0239     RDS_IB_MR_8K_POOL,
0240     RDS_IB_MR_1M_POOL,
0241 };
0242 
0243 struct rds_ib_device {
0244     struct list_head    list;
0245     struct list_head    ipaddr_list;
0246     struct list_head    conn_list;
0247     struct ib_device    *dev;
0248     struct ib_pd        *pd;
0249     u8          odp_capable:1;
0250 
0251     unsigned int        max_mrs;
0252     struct rds_ib_mr_pool   *mr_1m_pool;
0253     struct rds_ib_mr_pool   *mr_8k_pool;
0254     unsigned int        max_8k_mrs;
0255     unsigned int        max_1m_mrs;
0256     int         max_sge;
0257     unsigned int        max_wrs;
0258     unsigned int        max_initiator_depth;
0259     unsigned int        max_responder_resources;
0260     spinlock_t      spinlock;   /* protect the above */
0261     refcount_t      refcount;
0262     struct work_struct  free_work;
0263     int         *vector_load;
0264 };
0265 
0266 #define rdsibdev_to_node(rdsibdev) ibdev_to_node(rdsibdev->dev)
0267 
0268 /* bits for i_ack_flags */
0269 #define IB_ACK_IN_FLIGHT    0
0270 #define IB_ACK_REQUESTED    1
0271 
0272 /* Magic WR_ID for ACKs */
0273 #define RDS_IB_ACK_WR_ID    (~(u64) 0)
0274 
0275 struct rds_ib_statistics {
0276     uint64_t    s_ib_connect_raced;
0277     uint64_t    s_ib_listen_closed_stale;
0278     uint64_t    s_ib_evt_handler_call;
0279     uint64_t    s_ib_tasklet_call;
0280     uint64_t    s_ib_tx_cq_event;
0281     uint64_t    s_ib_tx_ring_full;
0282     uint64_t    s_ib_tx_throttle;
0283     uint64_t    s_ib_tx_sg_mapping_failure;
0284     uint64_t    s_ib_tx_stalled;
0285     uint64_t    s_ib_tx_credit_updates;
0286     uint64_t    s_ib_rx_cq_event;
0287     uint64_t    s_ib_rx_ring_empty;
0288     uint64_t    s_ib_rx_refill_from_cq;
0289     uint64_t    s_ib_rx_refill_from_thread;
0290     uint64_t    s_ib_rx_alloc_limit;
0291     uint64_t    s_ib_rx_total_frags;
0292     uint64_t    s_ib_rx_total_incs;
0293     uint64_t    s_ib_rx_credit_updates;
0294     uint64_t    s_ib_ack_sent;
0295     uint64_t    s_ib_ack_send_failure;
0296     uint64_t    s_ib_ack_send_delayed;
0297     uint64_t    s_ib_ack_send_piggybacked;
0298     uint64_t    s_ib_ack_received;
0299     uint64_t    s_ib_rdma_mr_8k_alloc;
0300     uint64_t    s_ib_rdma_mr_8k_free;
0301     uint64_t    s_ib_rdma_mr_8k_used;
0302     uint64_t    s_ib_rdma_mr_8k_pool_flush;
0303     uint64_t    s_ib_rdma_mr_8k_pool_wait;
0304     uint64_t    s_ib_rdma_mr_8k_pool_depleted;
0305     uint64_t    s_ib_rdma_mr_1m_alloc;
0306     uint64_t    s_ib_rdma_mr_1m_free;
0307     uint64_t    s_ib_rdma_mr_1m_used;
0308     uint64_t    s_ib_rdma_mr_1m_pool_flush;
0309     uint64_t    s_ib_rdma_mr_1m_pool_wait;
0310     uint64_t    s_ib_rdma_mr_1m_pool_depleted;
0311     uint64_t    s_ib_rdma_mr_8k_reused;
0312     uint64_t    s_ib_rdma_mr_1m_reused;
0313     uint64_t    s_ib_atomic_cswp;
0314     uint64_t    s_ib_atomic_fadd;
0315     uint64_t    s_ib_recv_added_to_cache;
0316     uint64_t    s_ib_recv_removed_from_cache;
0317 };
0318 
0319 extern struct workqueue_struct *rds_ib_wq;
0320 
0321 /*
0322  * Fake ib_dma_sync_sg_for_{cpu,device} as long as ib_verbs.h
0323  * doesn't define it.
0324  */
0325 static inline void rds_ib_dma_sync_sg_for_cpu(struct ib_device *dev,
0326                           struct scatterlist *sglist,
0327                           unsigned int sg_dma_len,
0328                           int direction)
0329 {
0330     struct scatterlist *sg;
0331     unsigned int i;
0332 
0333     for_each_sg(sglist, sg, sg_dma_len, i) {
0334         ib_dma_sync_single_for_cpu(dev, sg_dma_address(sg),
0335                        sg_dma_len(sg), direction);
0336     }
0337 }
0338 #define ib_dma_sync_sg_for_cpu  rds_ib_dma_sync_sg_for_cpu
0339 
0340 static inline void rds_ib_dma_sync_sg_for_device(struct ib_device *dev,
0341                          struct scatterlist *sglist,
0342                          unsigned int sg_dma_len,
0343                          int direction)
0344 {
0345     struct scatterlist *sg;
0346     unsigned int i;
0347 
0348     for_each_sg(sglist, sg, sg_dma_len, i) {
0349         ib_dma_sync_single_for_device(dev, sg_dma_address(sg),
0350                           sg_dma_len(sg), direction);
0351     }
0352 }
0353 #define ib_dma_sync_sg_for_device   rds_ib_dma_sync_sg_for_device
0354 
0355 
0356 /* ib.c */
0357 extern struct rds_transport rds_ib_transport;
0358 struct rds_ib_device *rds_ib_get_client_data(struct ib_device *device);
0359 void rds_ib_dev_put(struct rds_ib_device *rds_ibdev);
0360 extern struct ib_client rds_ib_client;
0361 
0362 extern unsigned int rds_ib_retry_count;
0363 
0364 extern spinlock_t ib_nodev_conns_lock;
0365 extern struct list_head ib_nodev_conns;
0366 
0367 /* ib_cm.c */
0368 int rds_ib_conn_alloc(struct rds_connection *conn, gfp_t gfp);
0369 void rds_ib_conn_free(void *arg);
0370 int rds_ib_conn_path_connect(struct rds_conn_path *cp);
0371 void rds_ib_conn_path_shutdown(struct rds_conn_path *cp);
0372 void rds_ib_state_change(struct sock *sk);
0373 int rds_ib_listen_init(void);
0374 void rds_ib_listen_stop(void);
0375 __printf(2, 3)
0376 void __rds_ib_conn_error(struct rds_connection *conn, const char *, ...);
0377 int rds_ib_cm_handle_connect(struct rdma_cm_id *cm_id,
0378                  struct rdma_cm_event *event, bool isv6);
0379 int rds_ib_cm_initiate_connect(struct rdma_cm_id *cm_id, bool isv6);
0380 void rds_ib_cm_connect_complete(struct rds_connection *conn,
0381                 struct rdma_cm_event *event);
0382 
0383 #define rds_ib_conn_error(conn, fmt...) \
0384     __rds_ib_conn_error(conn, KERN_WARNING "RDS/IB: " fmt)
0385 
0386 /* ib_rdma.c */
0387 int rds_ib_update_ipaddr(struct rds_ib_device *rds_ibdev,
0388              struct in6_addr *ipaddr);
0389 void rds_ib_add_conn(struct rds_ib_device *rds_ibdev, struct rds_connection *conn);
0390 void rds_ib_remove_conn(struct rds_ib_device *rds_ibdev, struct rds_connection *conn);
0391 void rds_ib_destroy_nodev_conns(void);
0392 void rds_ib_mr_cqe_handler(struct rds_ib_connection *ic, struct ib_wc *wc);
0393 
0394 /* ib_recv.c */
0395 int rds_ib_recv_init(void);
0396 void rds_ib_recv_exit(void);
0397 int rds_ib_recv_path(struct rds_conn_path *conn);
0398 int rds_ib_recv_alloc_caches(struct rds_ib_connection *ic, gfp_t gfp);
0399 void rds_ib_recv_free_caches(struct rds_ib_connection *ic);
0400 void rds_ib_recv_refill(struct rds_connection *conn, int prefill, gfp_t gfp);
0401 void rds_ib_inc_free(struct rds_incoming *inc);
0402 int rds_ib_inc_copy_to_user(struct rds_incoming *inc, struct iov_iter *to);
0403 void rds_ib_recv_cqe_handler(struct rds_ib_connection *ic, struct ib_wc *wc,
0404                  struct rds_ib_ack_state *state);
0405 void rds_ib_recv_tasklet_fn(unsigned long data);
0406 void rds_ib_recv_init_ring(struct rds_ib_connection *ic);
0407 void rds_ib_recv_clear_ring(struct rds_ib_connection *ic);
0408 void rds_ib_recv_init_ack(struct rds_ib_connection *ic);
0409 void rds_ib_attempt_ack(struct rds_ib_connection *ic);
0410 void rds_ib_ack_send_complete(struct rds_ib_connection *ic);
0411 u64 rds_ib_piggyb_ack(struct rds_ib_connection *ic);
0412 void rds_ib_set_ack(struct rds_ib_connection *ic, u64 seq, int ack_required);
0413 
0414 /* ib_ring.c */
0415 void rds_ib_ring_init(struct rds_ib_work_ring *ring, u32 nr);
0416 void rds_ib_ring_resize(struct rds_ib_work_ring *ring, u32 nr);
0417 u32 rds_ib_ring_alloc(struct rds_ib_work_ring *ring, u32 val, u32 *pos);
0418 void rds_ib_ring_free(struct rds_ib_work_ring *ring, u32 val);
0419 void rds_ib_ring_unalloc(struct rds_ib_work_ring *ring, u32 val);
0420 int rds_ib_ring_empty(struct rds_ib_work_ring *ring);
0421 int rds_ib_ring_low(struct rds_ib_work_ring *ring);
0422 u32 rds_ib_ring_oldest(struct rds_ib_work_ring *ring);
0423 u32 rds_ib_ring_completed(struct rds_ib_work_ring *ring, u32 wr_id, u32 oldest);
0424 extern wait_queue_head_t rds_ib_ring_empty_wait;
0425 
0426 /* ib_send.c */
0427 void rds_ib_xmit_path_complete(struct rds_conn_path *cp);
0428 int rds_ib_xmit(struct rds_connection *conn, struct rds_message *rm,
0429         unsigned int hdr_off, unsigned int sg, unsigned int off);
0430 void rds_ib_send_cqe_handler(struct rds_ib_connection *ic, struct ib_wc *wc);
0431 void rds_ib_send_init_ring(struct rds_ib_connection *ic);
0432 void rds_ib_send_clear_ring(struct rds_ib_connection *ic);
0433 int rds_ib_xmit_rdma(struct rds_connection *conn, struct rm_rdma_op *op);
0434 void rds_ib_send_add_credits(struct rds_connection *conn, unsigned int credits);
0435 void rds_ib_advertise_credits(struct rds_connection *conn, unsigned int posted);
0436 int rds_ib_send_grab_credits(struct rds_ib_connection *ic, u32 wanted,
0437                  u32 *adv_credits, int need_posted, int max_posted);
0438 int rds_ib_xmit_atomic(struct rds_connection *conn, struct rm_atomic_op *op);
0439 
0440 /* ib_stats.c */
0441 DECLARE_PER_CPU_SHARED_ALIGNED(struct rds_ib_statistics, rds_ib_stats);
0442 #define rds_ib_stats_inc(member) rds_stats_inc_which(rds_ib_stats, member)
0443 #define rds_ib_stats_add(member, count) \
0444         rds_stats_add_which(rds_ib_stats, member, count)
0445 unsigned int rds_ib_stats_info_copy(struct rds_info_iterator *iter,
0446                     unsigned int avail);
0447 
0448 /* ib_sysctl.c */
0449 int rds_ib_sysctl_init(void);
0450 void rds_ib_sysctl_exit(void);
0451 extern unsigned long rds_ib_sysctl_max_send_wr;
0452 extern unsigned long rds_ib_sysctl_max_recv_wr;
0453 extern unsigned long rds_ib_sysctl_max_unsig_wrs;
0454 extern unsigned long rds_ib_sysctl_max_unsig_bytes;
0455 extern unsigned long rds_ib_sysctl_max_recv_allocation;
0456 extern unsigned int rds_ib_sysctl_flow_control;
0457 
0458 #endif