1 /* SPDX-License-Identifier: GPL-2.0 */
3 * Shared Memory Communications over RDMA (SMC-R) and RoCE
5 * Definitions for SMC Connections, Link Groups and Links
7 * Copyright IBM Corp. 2016
15 #include <linux/atomic.h>
16 #include <linux/smc.h>
17 #include <linux/pci.h>
18 #include <rdma/ib_verbs.h>
19 #include <net/genetlink.h>
26 #define SMC_RMBS_PER_LGR_MAX 255 /* max. # of RMBs per link group */
27 #define SMC_CONN_PER_LGR_MIN 16 /* min. # of connections per link group */
28 #define SMC_CONN_PER_LGR_MAX 255 /* max. # of connections per link group,
29 * also is the default value for SMC-R v1 and v2.0
31 #define SMC_CONN_PER_LGR_PREFER 255 /* Preferred connections per link group used for
32 * SMC-R v2.1 and later negotiation, vendors or
33 * distributions may modify it to a value between
37 struct smc_lgr_list { /* list of link group definition */
38 struct list_head list;
39 spinlock_t lock; /* protects list of link groups */
40 u32 num; /* unique link group number */
43 enum smc_lgr_role { /* possible roles of a link group */
44 SMC_CLNT, /* client */
48 enum smc_link_state { /* possible states of a link */
49 SMC_LNK_UNUSED, /* link is unused */
50 SMC_LNK_INACTIVE, /* link is inactive */
51 SMC_LNK_ACTIVATING, /* link is being activated */
52 SMC_LNK_ACTIVE, /* link is active */
55 #define SMC_WR_BUF_SIZE 48 /* size of work request buffer */
56 #define SMC_WR_BUF_V2_SIZE 8192 /* size of v2 work request buffer */
59 u8 raw[SMC_WR_BUF_SIZE];
62 struct smc_wr_v2_buf {
63 u8 raw[SMC_WR_BUF_V2_SIZE];
66 #define SMC_WR_REG_MR_WAIT_TIME (5 * HZ)/* wait time for ib_wr_reg_mr result */
68 enum smc_wr_reg_state {
69 POSTED, /* ib_wr_reg_mr request posted */
70 CONFIRMED, /* ib_wr_reg_mr response: successful */
71 FAILED /* ib_wr_reg_mr response: failure */
74 struct smc_rdma_sge { /* sges for RDMA writes */
75 struct ib_sge wr_tx_rdma_sge[SMC_IB_MAX_SEND_SGE];
78 #define SMC_MAX_RDMA_WRITES 2 /* max. # of RDMA writes per
82 struct smc_rdma_sges { /* sges per message send */
83 struct smc_rdma_sge tx_rdma_sge[SMC_MAX_RDMA_WRITES];
86 struct smc_rdma_wr { /* work requests per message
89 struct ib_rdma_wr wr_tx_rdma[SMC_MAX_RDMA_WRITES];
92 #define SMC_LGR_ID_SIZE 4
95 struct smc_ib_device *smcibdev; /* ib-device */
96 u8 ibport; /* port - values 1 | 2 */
97 struct ib_pd *roce_pd; /* IB protection domain,
98 * unique for every RoCE QP
100 struct ib_qp *roce_qp; /* IB queue pair */
101 struct ib_qp_attr qp_attr; /* IB queue pair attributes */
103 struct smc_wr_buf *wr_tx_bufs; /* WR send payload buffers */
104 struct ib_send_wr *wr_tx_ibs; /* WR send meta data */
105 struct ib_sge *wr_tx_sges; /* WR send gather meta data */
106 struct smc_rdma_sges *wr_tx_rdma_sges;/*RDMA WRITE gather meta data*/
107 struct smc_rdma_wr *wr_tx_rdmas; /* WR RDMA WRITE */
108 struct smc_wr_tx_pend *wr_tx_pends; /* WR send waiting for CQE */
109 struct completion *wr_tx_compl; /* WR send CQE completion */
110 /* above four vectors have wr_tx_cnt elements and use the same index */
111 struct ib_send_wr *wr_tx_v2_ib; /* WR send v2 meta data */
112 struct ib_sge *wr_tx_v2_sge; /* WR send v2 gather meta data*/
113 struct smc_wr_tx_pend *wr_tx_v2_pend; /* WR send v2 waiting for CQE */
114 dma_addr_t wr_tx_dma_addr; /* DMA address of wr_tx_bufs */
115 dma_addr_t wr_tx_v2_dma_addr; /* DMA address of v2 tx buf*/
116 atomic_long_t wr_tx_id; /* seq # of last sent WR */
117 unsigned long *wr_tx_mask; /* bit mask of used indexes */
118 u32 wr_tx_cnt; /* number of WR send buffers */
119 wait_queue_head_t wr_tx_wait; /* wait for free WR send buf */
121 struct percpu_ref wr_tx_refs;
122 } ____cacheline_aligned_in_smp;
123 struct completion tx_ref_comp;
125 u8 *wr_rx_bufs; /* WR recv payload buffers */
126 struct ib_recv_wr *wr_rx_ibs; /* WR recv meta data */
127 struct ib_sge *wr_rx_sges; /* WR recv scatter meta data */
128 /* above three vectors have wr_rx_cnt elements and use the same index */
129 int wr_rx_sge_cnt; /* rx sge, V1 is 1, V2 is either 2 or 1 */
130 int wr_rx_buflen; /* buffer len for the first sge, len for the
131 * second sge is lgr shared if rx sge is 2.
133 dma_addr_t wr_rx_dma_addr; /* DMA address of wr_rx_bufs */
134 dma_addr_t wr_rx_v2_dma_addr; /* DMA address of v2 rx buf*/
135 u64 wr_rx_id; /* seq # of last recv WR */
136 u64 wr_rx_id_compl; /* seq # of last completed WR */
137 u32 wr_rx_cnt; /* number of WR recv buffers */
138 unsigned long wr_rx_tstamp; /* jiffies when last buf rx */
139 wait_queue_head_t wr_rx_empty_wait; /* wait for RQ empty */
141 struct ib_reg_wr wr_reg; /* WR register memory region */
142 wait_queue_head_t wr_reg_wait; /* wait for wr_reg result */
144 struct percpu_ref wr_reg_refs;
145 } ____cacheline_aligned_in_smp;
146 struct completion reg_ref_comp;
147 enum smc_wr_reg_state wr_reg_state; /* state of wr_reg request */
149 u8 gid[SMC_GID_SIZE];/* gid matching used vlan id*/
150 u8 sgid_index; /* gid index for vlan id */
151 u32 peer_qpn; /* QP number of peer */
152 enum ib_mtu path_mtu; /* used mtu */
153 enum ib_mtu peer_mtu; /* mtu size of peer */
154 u32 psn_initial; /* QP tx initial packet seqno */
155 u32 peer_psn; /* QP rx initial packet seqno */
156 u8 peer_mac[ETH_ALEN]; /* = gid[8:10||13:15] */
157 u8 peer_gid[SMC_GID_SIZE]; /* gid of peer*/
158 u8 link_id; /* unique # within link group */
159 u8 link_uid[SMC_LGR_ID_SIZE]; /* unique lnk id */
160 u8 peer_link_uid[SMC_LGR_ID_SIZE]; /* peer uid */
161 u8 link_idx; /* index in lgr link array */
162 u8 link_is_asym; /* is link asymmetric? */
163 u8 clearing : 1; /* link is being cleared */
164 refcount_t refcnt; /* link reference count */
165 struct smc_link_group *lgr; /* parent link group */
166 struct work_struct link_down_wrk; /* wrk to bring link down */
167 char ibname[IB_DEVICE_NAME_MAX]; /* ib device name */
168 int ndev_ifidx; /* network device ifindex */
170 enum smc_link_state state; /* state of link */
171 struct delayed_work llc_testlink_wrk; /* testlink worker */
172 struct completion llc_testlink_resp; /* wait for rx of testlink */
173 int llc_testlink_time; /* testlink interval */
174 atomic_t conn_cnt; /* connections on this link */
177 /* For now we just allow one parallel link per link group. The SMC protocol
178 * allows more (up to 8).
180 #define SMC_LINKS_PER_LGR_MAX 3
181 #define SMC_SINGLE_LINK 0
182 #define SMC_LINKS_ADD_LNK_MIN 1 /* min. # of links per link group */
183 #define SMC_LINKS_ADD_LNK_MAX 2 /* max. # of links per link group, also is the
184 * default value for smc-r v1.0 and v2.0
186 #define SMC_LINKS_PER_LGR_MAX_PREFER 2 /* Preferred max links per link group used for
187 * SMC-R v2.1 and later negotiation, vendors or
188 * distributions may modify it to a value between
192 /* tx/rx buffer list element for sndbufs list and rmbs list of a lgr */
193 struct smc_buf_desc {
194 struct list_head list;
195 void *cpu_addr; /* virtual address of buffer */
197 int len; /* length of buffer */
198 u32 used; /* currently used / unused */
201 struct sg_table sgt[SMC_LINKS_PER_LGR_MAX];
203 struct ib_mr *mr[SMC_LINKS_PER_LGR_MAX];
204 /* memory region: for rmb and
206 * incl. rkey provided to peer
207 * and lkey provided to local
209 u32 order; /* allocation order */
212 /* confirm_rkey done */
213 u8 is_reg_mr[SMC_LINKS_PER_LGR_MAX];
214 /* mem region registered */
215 u8 is_map_ib[SMC_LINKS_PER_LGR_MAX];
216 /* mem region mapped to lnk */
219 /* buffer registration err */
221 /* virtually contiguous */
224 unsigned short sba_idx;
225 /* SBA index number */
227 /* DMB token number */
234 struct smc_rtoken { /* address/key of remote RMB */
239 #define SMC_BUF_MIN_SIZE 16384 /* minimum size of an RMB */
240 #define SMC_RMBE_SIZES 16 /* number of distinct RMBE sizes */
241 /* theoretically, the RFC states that largest size would be 512K,
242 * i.e. compressed 5 and thus 6 sizes (0..5), despite
243 * struct smc_clc_msg_accept_confirm.rmbe_size being a 4 bit value (0..15)
248 enum smc_lgr_type { /* redundancy state of lgr */
249 SMC_LGR_NONE, /* no active links, lgr to be deleted */
250 SMC_LGR_SINGLE, /* 1 active RNIC on each peer */
251 SMC_LGR_SYMMETRIC, /* 2 active RNICs on each peer */
252 SMC_LGR_ASYMMETRIC_PEER, /* local has 2, peer 1 active RNICs */
253 SMC_LGR_ASYMMETRIC_LOCAL, /* local has 1, peer 2 active RNICs */
256 enum smcr_buf_type { /* types of SMC-R sndbufs and RMBs */
257 SMCR_PHYS_CONT_BUFS = 0,
258 SMCR_VIRT_CONT_BUFS = 1,
262 enum smc_llc_flowtype {
263 SMC_LLC_FLOW_NONE = 0,
264 SMC_LLC_FLOW_ADD_LINK = 2,
265 SMC_LLC_FLOW_DEL_LINK = 4,
266 SMC_LLC_FLOW_REQ_ADD_LINK = 5,
267 SMC_LLC_FLOW_RKEY = 6,
270 struct smc_llc_qentry;
272 struct smc_llc_flow {
273 enum smc_llc_flowtype type;
274 struct smc_llc_qentry *qentry;
277 struct smc_link_group {
278 struct list_head list;
279 struct rb_root conns_all; /* connection tree */
280 rwlock_t conns_lock; /* protects conns_all */
281 unsigned int conns_num; /* current # of connections */
282 unsigned short vlan_id; /* vlan id of link group */
284 struct list_head sndbufs[SMC_RMBE_SIZES];/* tx buffers */
285 struct rw_semaphore sndbufs_lock; /* protects tx buffers */
286 struct list_head rmbs[SMC_RMBE_SIZES]; /* rx buffers */
287 struct rw_semaphore rmbs_lock; /* protects rx buffers */
288 u64 alloc_sndbufs; /* stats of tx buffers */
289 u64 alloc_rmbs; /* stats of rx buffers */
291 u8 id[SMC_LGR_ID_SIZE]; /* unique lgr id */
292 struct delayed_work free_work; /* delayed freeing of an lgr */
293 struct work_struct terminate_work; /* abnormal lgr termination */
294 struct workqueue_struct *tx_wq; /* wq for conn. tx workers */
295 u8 sync_err : 1; /* lgr no longer fits to peer */
296 u8 terminating : 1;/* lgr is terminating */
297 u8 freeing : 1; /* lgr is being freed */
299 refcount_t refcnt; /* lgr reference count */
300 bool is_smcd; /* SMC-R or SMC-D */
302 u8 negotiated_eid[SMC_MAX_EID_LEN];
303 u8 peer_os; /* peer operating system */
305 u8 peer_hostname[SMC_MAX_HOSTNAME_LEN];
308 enum smc_lgr_role role;
309 /* client or server */
310 struct smc_link lnk[SMC_LINKS_PER_LGR_MAX];
312 struct smc_wr_v2_buf *wr_rx_buf_v2;
313 /* WR v2 recv payload buffer */
314 struct smc_wr_v2_buf *wr_tx_buf_v2;
315 /* WR v2 send payload buffer */
316 char peer_systemid[SMC_SYSTEMID_LEN];
317 /* unique system_id of peer */
318 struct smc_rtoken rtokens[SMC_RMBS_PER_LGR_MAX]
319 [SMC_LINKS_PER_LGR_MAX];
320 /* remote addr/key pairs */
321 DECLARE_BITMAP(rtokens_used_mask, SMC_RMBS_PER_LGR_MAX);
322 /* used rtoken elements */
324 enum smc_lgr_type type;
325 enum smcr_buf_type buf_type;
326 /* redundancy state */
327 u8 pnet_id[SMC_MAX_PNETID_LEN + 1];
328 /* pnet id of this lgr */
329 struct list_head llc_event_q;
330 /* queue for llc events */
331 spinlock_t llc_event_q_lock;
332 /* protects llc_event_q */
333 struct rw_semaphore llc_conf_mutex;
334 /* protects lgr reconfig. */
335 struct work_struct llc_add_link_work;
336 struct work_struct llc_del_link_work;
337 struct work_struct llc_event_work;
338 /* llc event worker */
339 wait_queue_head_t llc_flow_waiter;
340 /* w4 next llc event */
341 wait_queue_head_t llc_msg_waiter;
342 /* w4 next llc msg */
343 struct smc_llc_flow llc_flow_lcl;
344 /* llc local control field */
345 struct smc_llc_flow llc_flow_rmt;
346 /* llc remote control field */
347 struct smc_llc_qentry *delayed_event;
348 /* arrived when flow active */
349 spinlock_t llc_flow_lock;
350 /* protects llc flow */
351 int llc_testlink_time;
352 /* link keep alive time */
353 u32 llc_termination_rsn;
354 /* rsn code for termination */
355 u8 nexthop_mac[ETH_ALEN];
361 /* max conn can be assigned to lgr */
363 /* max links can be added in lgr */
366 struct smcd_gid peer_gid;
367 /* Peer GID (remote) */
368 struct smcd_dev *smcd;
369 /* ISM device for VLAN reg. */
370 u8 peer_shutdown : 1;
371 /* peer triggered shutdownn */
376 struct smc_clc_msg_local;
378 #define GID_LIST_SIZE 2
382 u8 list[GID_LIST_SIZE][SMC_GID_SIZE];
385 struct smc_init_info_smcrv2 {
391 /* Output fields when saddr is set */
392 struct smc_ib_device *ib_dev_v2;
394 u8 ib_gid_v2[SMC_GID_SIZE];
396 /* Additional output fields when clc_sk and daddr is set as well */
398 u8 nexthop_mac[ETH_ALEN];
400 struct smc_gidlist gidlist;
403 #define SMC_MAX_V2_ISM_DEVS SMCD_CLC_MAX_V2_GID_ENTRIES
404 /* max # of proposed non-native ISM devices,
405 * which can't exceed the max # of CHID-GID
406 * entries in CLC proposal SMC-Dv2 extension.
408 struct smc_init_info {
415 u8 first_contact_peer;
416 u8 first_contact_local;
418 unsigned short vlan_id;
420 u8 negotiated_eid[SMC_MAX_EID_LEN];
424 u8 peer_gid[SMC_GID_SIZE];
425 u8 peer_mac[ETH_ALEN];
426 u8 peer_systemid[SMC_SYSTEMID_LEN];
427 struct smc_ib_device *ib_dev;
428 u8 ib_gid[SMC_GID_SIZE];
431 struct smc_init_info_smcrv2 smcrv2;
433 struct smcd_gid ism_peer_gid[SMC_MAX_V2_ISM_DEVS + 1];
434 struct smcd_dev *ism_dev[SMC_MAX_V2_ISM_DEVS + 1];
435 u16 ism_chid[SMC_MAX_V2_ISM_DEVS + 1];
436 u8 ism_offered_cnt; /* # of ISM devices offered */
437 u8 ism_selected; /* index of selected ISM dev*/
441 /* Find the connection associated with the given alert token in the link group.
442 * To use rbtrees we have to implement our own search core.
443 * Requires @conns_lock
444 * @token alert token to search for
445 * @lgr link group to search in
446 * Returns connection associated with token if found, NULL otherwise.
448 static inline struct smc_connection *smc_lgr_find_conn(
449 u32 token, struct smc_link_group *lgr)
451 struct smc_connection *res = NULL;
452 struct rb_node *node;
454 node = lgr->conns_all.rb_node;
456 struct smc_connection *cur = rb_entry(node,
457 struct smc_connection, alert_node);
459 if (cur->alert_token_local > token) {
460 node = node->rb_left;
462 if (cur->alert_token_local < token) {
463 node = node->rb_right;
474 static inline bool smc_conn_lgr_valid(struct smc_connection *conn)
476 return conn->lgr && conn->alert_token_local;
480 * Returns true if the specified link is usable.
482 * usable means the link is ready to receive RDMA messages, map memory
483 * on the link, etc. This doesn't ensure we are able to send RDMA messages
484 * on this link, if sending RDMA messages is needed, use smc_link_sendable()
486 static inline bool smc_link_usable(struct smc_link *lnk)
488 if (lnk->state == SMC_LNK_UNUSED || lnk->state == SMC_LNK_INACTIVE)
494 * Returns true if the specified link is ready to receive AND send RDMA
497 * For the client side in first contact, the underlying QP may still in
498 * RESET or RTR when the link state is ACTIVATING, checks in smc_link_usable()
499 * is not strong enough. For those places that need to send any CDC or LLC
500 * messages, use smc_link_sendable(), otherwise, use smc_link_usable() instead
502 static inline bool smc_link_sendable(struct smc_link *lnk)
504 return smc_link_usable(lnk) &&
505 lnk->qp_attr.cur_qp_state == IB_QPS_RTS;
508 static inline bool smc_link_active(struct smc_link *lnk)
510 return lnk->state == SMC_LNK_ACTIVE;
513 static inline bool smc_link_shared_v2_rxbuf(struct smc_link *lnk)
515 return lnk->wr_rx_sge_cnt > 1;
518 static inline void smc_gid_be16_convert(__u8 *buf, u8 *gid_raw)
520 sprintf(buf, "%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x",
521 be16_to_cpu(((__be16 *)gid_raw)[0]),
522 be16_to_cpu(((__be16 *)gid_raw)[1]),
523 be16_to_cpu(((__be16 *)gid_raw)[2]),
524 be16_to_cpu(((__be16 *)gid_raw)[3]),
525 be16_to_cpu(((__be16 *)gid_raw)[4]),
526 be16_to_cpu(((__be16 *)gid_raw)[5]),
527 be16_to_cpu(((__be16 *)gid_raw)[6]),
528 be16_to_cpu(((__be16 *)gid_raw)[7]));
536 __u8 pci_id[SMC_PCI_ID_STR_LEN];
539 static inline void smc_set_pci_values(struct pci_dev *pci_dev,
540 struct smc_pci_dev *smc_dev)
542 smc_dev->pci_vendor = pci_dev->vendor;
543 smc_dev->pci_device = pci_dev->device;
544 snprintf(smc_dev->pci_id, sizeof(smc_dev->pci_id), "%s",
546 #if IS_ENABLED(CONFIG_S390)
547 { /* Set s390 specific PCI information */
548 struct zpci_dev *zdev;
550 zdev = to_zpci(pci_dev);
551 smc_dev->pci_fid = zdev->fid;
552 smc_dev->pci_pchid = zdev->pchid;
558 struct smc_clc_msg_accept_confirm;
560 void smc_lgr_cleanup_early(struct smc_link_group *lgr);
561 void smc_lgr_terminate_sched(struct smc_link_group *lgr);
562 void smc_lgr_hold(struct smc_link_group *lgr);
563 void smc_lgr_put(struct smc_link_group *lgr);
564 void smcr_port_add(struct smc_ib_device *smcibdev, u8 ibport);
565 void smcr_port_err(struct smc_ib_device *smcibdev, u8 ibport);
566 void smc_smcd_terminate(struct smcd_dev *dev, struct smcd_gid *peer_gid,
567 unsigned short vlan);
568 void smc_smcd_terminate_all(struct smcd_dev *dev);
569 void smc_smcr_terminate_all(struct smc_ib_device *smcibdev);
570 int smc_buf_create(struct smc_sock *smc, bool is_smcd);
571 int smcd_buf_attach(struct smc_sock *smc);
572 int smc_uncompress_bufsize(u8 compressed);
573 int smc_rmb_rtoken_handling(struct smc_connection *conn, struct smc_link *link,
574 struct smc_clc_msg_accept_confirm *clc);
575 int smc_rtoken_add(struct smc_link *lnk, __be64 nw_vaddr, __be32 nw_rkey);
576 int smc_rtoken_delete(struct smc_link *lnk, __be32 nw_rkey);
577 void smc_rtoken_set(struct smc_link_group *lgr, int link_idx, int link_idx_new,
578 __be32 nw_rkey_known, __be64 nw_vaddr, __be32 nw_rkey);
579 void smc_rtoken_set2(struct smc_link_group *lgr, int rtok_idx, int link_id,
580 __be64 nw_vaddr, __be32 nw_rkey);
581 void smc_sndbuf_sync_sg_for_device(struct smc_connection *conn);
582 void smc_rmb_sync_sg_for_cpu(struct smc_connection *conn);
583 int smc_vlan_by_tcpsk(struct socket *clcsock, struct smc_init_info *ini);
585 void smc_conn_free(struct smc_connection *conn);
586 int smc_conn_create(struct smc_sock *smc, struct smc_init_info *ini);
587 int smc_core_init(void);
588 void smc_core_exit(void);
590 int smcr_link_init(struct smc_link_group *lgr, struct smc_link *lnk,
591 u8 link_idx, struct smc_init_info *ini);
592 void smcr_link_clear(struct smc_link *lnk, bool log);
593 void smcr_link_hold(struct smc_link *lnk);
594 void smcr_link_put(struct smc_link *lnk);
595 void smc_switch_link_and_count(struct smc_connection *conn,
596 struct smc_link *to_lnk);
597 int smcr_buf_map_lgr(struct smc_link *lnk);
598 int smcr_buf_reg_lgr(struct smc_link *lnk);
599 void smcr_lgr_set_type(struct smc_link_group *lgr, enum smc_lgr_type new_type);
600 void smcr_lgr_set_type_asym(struct smc_link_group *lgr,
601 enum smc_lgr_type new_type, int asym_lnk_idx);
602 int smcr_link_reg_buf(struct smc_link *link, struct smc_buf_desc *rmb_desc);
603 struct smc_link *smc_switch_conns(struct smc_link_group *lgr,
604 struct smc_link *from_lnk, bool is_dev_err);
605 void smcr_link_down_cond(struct smc_link *lnk);
606 void smcr_link_down_cond_sched(struct smc_link *lnk);
607 int smc_nl_get_sys_info(struct sk_buff *skb, struct netlink_callback *cb);
608 int smcr_nl_get_lgr(struct sk_buff *skb, struct netlink_callback *cb);
609 int smcr_nl_get_link(struct sk_buff *skb, struct netlink_callback *cb);
610 int smcd_nl_get_lgr(struct sk_buff *skb, struct netlink_callback *cb);
612 static inline struct smc_link_group *smc_get_lgr(struct smc_link *link)