#ifndef DEF_RDMA_VT_H1#define DEF_RDMA_VT_H23/*-4* SPDX-License-Identifier: BSD-2-Clause OR GPL-2.05*6* Copyright(c) 2016 Intel Corporation.7*8* This file is provided under a dual BSD/GPLv2 license. When using or9* redistributing this file, you may do so under either license.10*11* GPL LICENSE SUMMARY12*13* This program is free software; you can redistribute it and/or modify14* it under the terms of version 2 of the GNU General Public License as15* published by the Free Software Foundation.16*17* This program is distributed in the hope that it will be useful, but18* WITHOUT ANY WARRANTY; without even the implied warranty of19* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU20* General Public License for more details.21*22* BSD LICENSE23*24* Redistribution and use in source and binary forms, with or without25* modification, are permitted provided that the following conditions26* are met:27*28* - Redistributions of source code must retain the above copyright29* notice, this list of conditions and the following disclaimer.30* - Redistributions in binary form must reproduce the above copyright31* notice, this list of conditions and the following disclaimer in32* the documentation and/or other materials provided with the33* distribution.34* - Neither the name of Intel Corporation nor the names of its35* contributors may be used to endorse or promote products derived36* from this software without specific prior written permission.37*38* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS39* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT40* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR41* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT42* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,43* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT44* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,45* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY46* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT47* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE48* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.49*/5051/*52* Structure that low level drivers will populate in order to register with the53* rdmavt layer.54*/5556#include <linux/spinlock.h>57#include <linux/list.h>58#include <linux/hash.h>59#include <rdma/ib_verbs.h>60#include <rdma/rdmavt_mr.h>61#include <rdma/rdmavt_qp.h>6263#define RVT_MAX_PKEY_VALUES 166465struct rvt_ibport {66struct rvt_qp __rcu *qp[2];67struct ib_mad_agent *send_agent; /* agent for SMI (traps) */68struct rb_root mcast_tree;69spinlock_t lock; /* protect changes in this struct */7071/* non-zero when timer is set */72unsigned long mkey_lease_timeout;73unsigned long trap_timeout;74__be64 gid_prefix; /* in network order */75__be64 mkey;76u64 tid;77u32 port_cap_flags;78u32 pma_sample_start;79u32 pma_sample_interval;80__be16 pma_counter_select[5];81u16 pma_tag;82u16 mkey_lease_period;83u16 sm_lid;84u8 sm_sl;85u8 mkeyprot;86u8 subnet_timeout;87u8 vl_high_limit;8889/*90* Driver is expected to keep these up to date. These91* counters are informational only and not required to be92* completely accurate.93*/94u64 n_rc_resends;95u64 n_seq_naks;96u64 n_rdma_seq;97u64 n_rnr_naks;98u64 n_other_naks;99u64 n_loop_pkts;100u64 n_pkt_drops;101u64 n_vl15_dropped;102u64 n_rc_timeouts;103u64 n_dmawait;104u64 n_unaligned;105u64 n_rc_dupreq;106u64 n_rc_seqnak;107u16 pkey_violations;108u16 qkey_violations;109u16 mkey_violations;110111/* Hot-path per CPU counters to avoid cacheline trading to update */112u64 z_rc_acks;113u64 z_rc_qacks;114u64 z_rc_delayed_comp;115u64 __percpu *rc_acks;116u64 __percpu *rc_qacks;117u64 __percpu *rc_delayed_comp;118119void *priv; /* driver private data */120121/*122* The pkey table is allocated and maintained by the driver. Drivers123* need to have access to this before registering with rdmav. However124* rdmavt will need access to it so drivers need to proviee this during125* the attach port API call.126*/127u16 *pkey_table;128129struct rvt_ah *sm_ah;130};131132#define RVT_CQN_MAX 16 /* maximum length of cq name */133134/*135* Things that are driver specific, module parameters in hfi1 and qib136*/137struct rvt_driver_params {138struct ib_device_attr props;139140/*141* Anything driver specific that is not covered by props142* For instance special module parameters. Goes here.143*/144unsigned int lkey_table_size;145unsigned int qp_table_size;146int qpn_start;147int qpn_inc;148int qpn_res_start;149int qpn_res_end;150int nports;151int npkeys;152char cq_name[RVT_CQN_MAX];153int node;154int psn_mask;155int psn_shift;156int psn_modify_mask;157u32 core_cap_flags;158u32 max_mad_size;159u8 qos_shift;160u8 max_rdma_atomic;161u8 reserved_operations;162};163164/* Protection domain */165struct rvt_pd {166struct ib_pd ibpd;167int user; /* non-zero if created from user space */168};169170/* Address handle */171struct rvt_ah {172struct ib_ah ibah;173struct ib_ah_attr attr;174atomic_t refcount;175u8 vl;176u8 log_pmtu;177};178179struct rvt_dev_info;180struct rvt_swqe;181struct rvt_driver_provided {182/*183* Which functions are required depends on which verbs rdmavt is184* providing and which verbs the driver is overriding. See185* check_support() for details.186*/187188/* Passed to ib core registration. Callback to create syfs files */189int (*port_callback)(struct ib_device *, u8, struct kobject *);190191/*192* Returns a string to represent the device for which is being193* registered. This is primarily used for error and debug messages on194* the console.195*/196const char * (*get_card_name)(struct rvt_dev_info *rdi);197198/*199* Returns a pointer to the undelying hardware's PCI device. This is200* used to display information as to what hardware is being referenced201* in an output message202*/203struct pci_dev * (*get_pci_dev)(struct rvt_dev_info *rdi);204205/*206* Allocate a private queue pair data structure for driver specific207* information which is opaque to rdmavt. Errors are returned via208* ERR_PTR(err). The driver is free to return NULL or a valid209* pointer.210*/211void * (*qp_priv_alloc)(struct rvt_dev_info *rdi, struct rvt_qp *qp,212gfp_t gfp);213214/*215* Free the driver's private qp structure.216*/217void (*qp_priv_free)(struct rvt_dev_info *rdi, struct rvt_qp *qp);218219/*220* Inform the driver the particular qp in quesiton has been reset so221* that it can clean up anything it needs to.222*/223void (*notify_qp_reset)(struct rvt_qp *qp);224225/*226* Give the driver a notice that there is send work to do. It is up to227* the driver to generally push the packets out, this just queues the228* work with the driver. There are two variants here. The no_lock229* version requires the s_lock not to be held. The other assumes the230* s_lock is held.231*/232void (*schedule_send)(struct rvt_qp *qp);233void (*schedule_send_no_lock)(struct rvt_qp *qp);234235/*236* Sometimes rdmavt needs to kick the driver's send progress. That is237* done by this call back.238*/239void (*do_send)(struct rvt_qp *qp);240241/*242* Get a path mtu from the driver based on qp attributes.243*/244int (*get_pmtu_from_attr)(struct rvt_dev_info *rdi, struct rvt_qp *qp,245struct ib_qp_attr *attr);246247/*248* Notify driver that it needs to flush any outstanding IO requests that249* are waiting on a qp.250*/251void (*flush_qp_waiters)(struct rvt_qp *qp);252253/*254* Notify driver to stop its queue of sending packets. Nothing else255* should be posted to the queue pair after this has been called.256*/257void (*stop_send_queue)(struct rvt_qp *qp);258259/*260* Have the drivr drain any in progress operations261*/262void (*quiesce_qp)(struct rvt_qp *qp);263264/*265* Inform the driver a qp has went to error state.266*/267void (*notify_error_qp)(struct rvt_qp *qp);268269/*270* Get an MTU for a qp.271*/272u32 (*mtu_from_qp)(struct rvt_dev_info *rdi, struct rvt_qp *qp,273u32 pmtu);274/*275* Convert an mtu to a path mtu276*/277int (*mtu_to_path_mtu)(u32 mtu);278279/*280* Get the guid of a port in big endian byte order281*/282int (*get_guid_be)(struct rvt_dev_info *rdi, struct rvt_ibport *rvp,283int guid_index, __be64 *guid);284285/*286* Query driver for the state of the port.287*/288int (*query_port_state)(struct rvt_dev_info *rdi, u8 port_num,289struct ib_port_attr *props);290291/*292* Tell driver to shutdown a port293*/294int (*shut_down_port)(struct rvt_dev_info *rdi, u8 port_num);295296/* Tell driver to send a trap for changed port capabilities */297void (*cap_mask_chg)(struct rvt_dev_info *rdi, u8 port_num);298299/*300* The following functions can be safely ignored completely. Any use of301* these is checked for NULL before blindly calling. Rdmavt should also302* be functional if drivers omit these.303*/304305/* Called to inform the driver that all qps should now be freed. */306unsigned (*free_all_qps)(struct rvt_dev_info *rdi);307308/* Driver specific AH validation */309int (*check_ah)(struct ib_device *, struct ib_ah_attr *);310311/* Inform the driver a new AH has been created */312void (*notify_new_ah)(struct ib_device *, struct ib_ah_attr *,313struct rvt_ah *);314315/* Let the driver pick the next queue pair number*/316int (*alloc_qpn)(struct rvt_dev_info *rdi, struct rvt_qpn_table *qpt,317enum ib_qp_type type, u8 port_num, gfp_t gfp);318319/* Determine if its safe or allowed to modify the qp */320int (*check_modify_qp)(struct rvt_qp *qp, struct ib_qp_attr *attr,321int attr_mask, struct ib_udata *udata);322323/* Driver specific QP modification/notification-of */324void (*modify_qp)(struct rvt_qp *qp, struct ib_qp_attr *attr,325int attr_mask, struct ib_udata *udata);326327/* Driver specific work request checking */328int (*check_send_wqe)(struct rvt_qp *qp, struct rvt_swqe *wqe);329330/* Notify driver a mad agent has been created */331void (*notify_create_mad_agent)(struct rvt_dev_info *rdi, int port_idx);332333/* Notify driver a mad agent has been removed */334void (*notify_free_mad_agent)(struct rvt_dev_info *rdi, int port_idx);335336};337338struct rvt_dev_info {339struct ib_device ibdev; /* Keep this first. Nothing above here */340341/*342* Prior to calling for registration the driver will be responsible for343* allocating space for this structure.344*345* The driver will also be responsible for filling in certain members of346* dparms.props. The driver needs to fill in dparms exactly as it would347* want values reported to a ULP. This will be returned to the caller348* in rdmavt's device. The driver should also therefore refrain from349* modifying this directly after registration with rdmavt.350*/351352/* Driver specific properties */353struct rvt_driver_params dparms;354355/* post send table */356const struct rvt_operation_params *post_parms;357358struct rvt_mregion __rcu *dma_mr;359struct rvt_lkey_table lkey_table;360361/* Driver specific helper functions */362struct rvt_driver_provided driver_f;363364/* Internal use */365int n_pds_allocated;366spinlock_t n_pds_lock; /* Protect pd allocated count */367368int n_ahs_allocated;369spinlock_t n_ahs_lock; /* Protect ah allocated count */370371u32 n_srqs_allocated;372spinlock_t n_srqs_lock; /* Protect srqs allocated count */373374int flags;375struct rvt_ibport **ports;376377/* QP */378struct rvt_qp_ibdev *qp_dev;379u32 n_qps_allocated; /* number of QPs allocated for device */380u32 n_rc_qps; /* number of RC QPs allocated for device */381u32 busy_jiffies; /* timeout scaling based on RC QP count */382spinlock_t n_qps_lock; /* protect qps, rc qps and busy jiffy counts */383384/* memory maps */385struct list_head pending_mmaps;386spinlock_t mmap_offset_lock; /* protect mmap_offset */387u32 mmap_offset;388spinlock_t pending_lock; /* protect pending mmap list */389390/* CQ */391struct kthread_worker *worker; /* per device cq worker */392u32 n_cqs_allocated; /* number of CQs allocated for device */393spinlock_t n_cqs_lock; /* protect count of in use cqs */394395/* Multicast */396u32 n_mcast_grps_allocated; /* number of mcast groups allocated */397spinlock_t n_mcast_grps_lock;398399};400401static inline struct rvt_pd *ibpd_to_rvtpd(struct ib_pd *ibpd)402{403return container_of(ibpd, struct rvt_pd, ibpd);404}405406static inline struct rvt_ah *ibah_to_rvtah(struct ib_ah *ibah)407{408return container_of(ibah, struct rvt_ah, ibah);409}410411static inline struct rvt_dev_info *ib_to_rvt(struct ib_device *ibdev)412{413return container_of(ibdev, struct rvt_dev_info, ibdev);414}415416static inline struct rvt_srq *ibsrq_to_rvtsrq(struct ib_srq *ibsrq)417{418return container_of(ibsrq, struct rvt_srq, ibsrq);419}420421static inline struct rvt_qp *ibqp_to_rvtqp(struct ib_qp *ibqp)422{423return container_of(ibqp, struct rvt_qp, ibqp);424}425426static inline unsigned rvt_get_npkeys(struct rvt_dev_info *rdi)427{428/*429* All ports have same number of pkeys.430*/431return rdi->dparms.npkeys;432}433434/*435* Return the max atomic suitable for determining436* the size of the ack ring buffer in a QP.437*/438static inline unsigned int rvt_max_atomic(struct rvt_dev_info *rdi)439{440return rdi->dparms.max_rdma_atomic + 1;441}442443/*444* Return the indexed PKEY from the port PKEY table.445*/446static inline u16 rvt_get_pkey(struct rvt_dev_info *rdi,447int port_index,448unsigned index)449{450if (index >= rvt_get_npkeys(rdi))451return 0;452else453return rdi->ports[port_index]->pkey_table[index];454}455456/**457* rvt_lookup_qpn - return the QP with the given QPN458* @ibp: the ibport459* @qpn: the QP number to look up460*461* The caller must hold the rcu_read_lock(), and keep the lock until462* the returned qp is no longer in use.463*/464/* TODO: Remove this and put in rdmavt/qp.h when no longer needed by drivers */465static inline struct rvt_qp *rvt_lookup_qpn(struct rvt_dev_info *rdi,466struct rvt_ibport *rvp,467u32 qpn) __must_hold(RCU)468{469struct rvt_qp *qp = NULL;470471if (unlikely(qpn <= 1)) {472qp = rcu_dereference(rvp->qp[qpn]);473} else {474u32 n = hash_32(qpn, rdi->qp_dev->qp_table_bits);475476for (qp = rcu_dereference(rdi->qp_dev->qp_table[n]); qp;477qp = rcu_dereference(qp->next))478if (qp->ibqp.qp_num == qpn)479break;480}481return qp;482}483484struct rvt_dev_info *rvt_alloc_device(size_t size, int nports);485void rvt_dealloc_device(struct rvt_dev_info *rdi);486int rvt_register_device(struct rvt_dev_info *rvd);487void rvt_unregister_device(struct rvt_dev_info *rvd);488int rvt_check_ah(struct ib_device *ibdev, struct ib_ah_attr *ah_attr);489int rvt_init_port(struct rvt_dev_info *rdi, struct rvt_ibport *port,490int port_index, u16 *pkey_table);491int rvt_fast_reg_mr(struct rvt_qp *qp, struct ib_mr *ibmr, u32 key,492int access);493int rvt_invalidate_rkey(struct rvt_qp *qp, u32 rkey);494int rvt_rkey_ok(struct rvt_qp *qp, struct rvt_sge *sge,495u32 len, u64 vaddr, u32 rkey, int acc);496int rvt_lkey_ok(struct rvt_lkey_table *rkt, struct rvt_pd *pd,497struct rvt_sge *isge, struct ib_sge *sge, int acc);498struct rvt_mcast *rvt_mcast_find(struct rvt_ibport *ibp, union ib_gid *mgid);499500#endif /* DEF_RDMA_VT_H */501502503