1 /* This file is part of the Emulex RoCE Device Driver for
2 * RoCE (RDMA over Converged Ethernet) adapters.
3 * Copyright (C) 2012-2015 Emulex. All rights reserved.
4 * EMULEX and SLI are trademarks of Emulex.
7 * This software is available to you under a choice of one of two licenses.
8 * You may choose to be licensed under the terms of the GNU General Public
9 * License (GPL) Version 2, available from the file COPYING in the main
10 * directory of this source tree, or the BSD license below:
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
16 * - Redistributions of source code must retain the above copyright notice,
17 * this list of conditions and the following disclaimer.
19 * - Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in
21 * the documentation and/or other materials provided with the distribution.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
27 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
30 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
31 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
32 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
33 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 * Contact Information:
36 * linux-drivers@emulex.com
40 * Costa Mesa, CA 92626
46 #include <linux/mutex.h>
47 #include <linux/list.h>
48 #include <linux/spinlock.h>
49 #include <linux/pci.h>
51 #include <rdma/ib_verbs.h>
52 #include <rdma/ib_user_verbs.h>
53 #include <rdma/ib_addr.h>
56 #include "ocrdma_sli.h"
58 #define OCRDMA_ROCE_DRV_VERSION "11.0.0.0"
60 #define OCRDMA_ROCE_DRV_DESC "Emulex OneConnect RoCE Driver"
61 #define OCRDMA_NODE_DESC "Emulex OneConnect RoCE HCA"
63 #define OC_NAME_SH OCRDMA_NODE_DESC "(Skyhawk)"
64 #define OC_NAME_UNKNOWN OCRDMA_NODE_DESC "(Unknown)"
66 #define OC_SKH_DEVICE_PF 0x720
67 #define OC_SKH_DEVICE_VF 0x728
68 #define OCRDMA_MAX_AH 512
70 #define OCRDMA_UVERBS(CMD_NAME) (1ull << IB_USER_VERBS_CMD_##CMD_NAME)
72 #define convert_to_64bit(lo, hi) ((u64)hi << 32 | (u64)lo)
73 #define EQ_INTR_PER_SEC_THRSH_HI 150000
74 #define EQ_INTR_PER_SEC_THRSH_LOW 100000
75 #define EQ_AIC_MAX_EQD 20
76 #define EQ_AIC_MIN_EQD 0
78 void ocrdma_eqd_set_task(struct work_struct
*work
);
80 struct ocrdma_dev_attr
{
102 int max_pages_per_frmr
;
106 int device_cap_flags
;
107 u8 cq_overflow_detect
;
113 u8 local_ca_ack_delay
;
119 struct ocrdma_dma_mem
{
130 struct ocrdma_queue_info
{
135 u16 entry_size
; /* Size of an element in the queue */
136 u16 id
; /* qid, where to ring the doorbell. */
141 struct ocrdma_aic_obj
{ /* Adaptive interrupt coalescing (AIC) info */
144 u64 prev_eq_intr_cnt
;
148 struct ocrdma_queue_info q
;
151 struct ocrdma_dev
*dev
;
153 struct ocrdma_aic_obj aic_obj
;
157 struct ocrdma_queue_info sq
;
158 struct ocrdma_queue_info cq
;
163 struct mutex lock
; /* for serializing mailbox commands on MQ */
164 wait_queue_head_t cmd_wait
;
172 struct ocrdma_hw_mr
{
183 struct ocrdma_pbl
*pbl_table
;
193 struct ib_umem
*umem
;
194 struct ocrdma_hw_mr hwmr
;
199 struct ocrdma_stats
{
201 struct ocrdma_dev
*dev
;
204 struct ocrdma_pd_resource_mgr
{
214 unsigned long *pd_norm_bitmap
;
215 unsigned long *pd_dpp_bitmap
;
216 bool pd_prealloc_valid
;
220 struct ocrdma_mqe mqe
;
228 u16 auto_speeds_supported
;
229 u16 fixed_speeds_supported
;
235 OCRDMA_FLAGS_LINK_STATUS_INIT
= 0x01
239 struct ib_device ibdev
;
240 struct ocrdma_dev_attr attr
;
242 struct mutex dev_lock
; /* provides syncronise access to device data */
243 spinlock_t flush_q_lock ____cacheline_aligned
;
245 struct ocrdma_cq
**cq_tbl
;
246 struct ocrdma_qp
**qp_tbl
;
248 struct ocrdma_eq
*eq_tbl
;
250 struct delayed_work eqd_work
;
254 /* provided synchronization to sgid table for
255 * updating gid entries triggered by notifier.
257 spinlock_t sgid_lock
;
260 struct ocrdma_cq
*gsi_sqcq
;
261 struct ocrdma_cq
*gsi_rqcq
;
264 struct ocrdma_av
*va
;
268 /* provide synchronization for av
273 struct ocrdma_pbl pbl
;
278 struct mqe_ctx mqe_ctx
;
280 struct be_dev_info nic_info
;
282 char model_number
[32];
285 struct list_head entry
;
288 u8 sl
; /* service level */
295 ulong last_stats_time
;
296 struct mutex stats_lock
; /* provide synch for debugfs operations */
297 struct stats_mem stats_mem
;
298 struct ocrdma_stats rsrc_stats
;
299 struct ocrdma_stats rx_stats
;
300 struct ocrdma_stats wqe_stats
;
301 struct ocrdma_stats tx_stats
;
302 struct ocrdma_stats db_err_stats
;
303 struct ocrdma_stats tx_qp_err_stats
;
304 struct ocrdma_stats rx_qp_err_stats
;
305 struct ocrdma_stats tx_dbg_stats
;
306 struct ocrdma_stats rx_dbg_stats
;
307 struct ocrdma_stats driver_stats
;
308 struct ocrdma_stats reset_stats
;
310 atomic_t async_err_stats
[OCRDMA_MAX_ASYNC_ERRORS
];
311 atomic_t cqe_err_stats
[OCRDMA_MAX_CQE_ERR
];
312 struct ocrdma_pd_resource_mgr
*pd_mgr
;
317 struct ocrdma_cqe
*va
;
319 u32 getp
; /* pointer to pending wrs to
320 * return to stack, wrap arounds
325 spinlock_t cq_lock ____cacheline_aligned
; /* provide synchronization
328 /* syncronizes cq completion handler invoked from multiple context */
329 spinlock_t comp_handler_lock ____cacheline_aligned
;
333 struct ocrdma_ucontext
*ucontext
;
338 /* head of all qp's sq and rq for which cqes need to be flushed
341 struct list_head sq_head
, rq_head
;
346 struct ocrdma_ucontext
*uctx
;
355 struct ocrdma_av
*av
;
361 struct ocrdma_qp_hwq_info
{
362 u8
*va
; /* virtual address */
368 u16 dbid
; /* qid, where to ring the doorbell. */
376 struct ocrdma_qp_hwq_info rq
;
381 /* provide synchronization to multiple context(s) posting rqe */
382 spinlock_t q_lock ____cacheline_aligned
;
384 struct ocrdma_pd
*pd
;
392 struct ocrdma_qp_hwq_info sq
;
395 uint16_t dpp_wqe_idx
;
402 /* provide synchronization to multiple context(s) posting wqe, rqe */
403 spinlock_t q_lock ____cacheline_aligned
;
404 struct ocrdma_cq
*sq_cq
;
405 /* list maintained per CQ to flush SQ errors */
406 struct list_head sq_entry
;
409 struct ocrdma_qp_hwq_info rq
;
411 struct ocrdma_cq
*rq_cq
;
412 struct ocrdma_srq
*srq
;
413 /* list maintained per CQ to flush RQ errors */
414 struct list_head rq_entry
;
416 enum ocrdma_qp_state state
; /* QP state */
418 u32 max_ord
, max_ird
;
421 struct ocrdma_pd
*pd
;
423 enum ib_qp_type qp_type
;
432 struct ocrdma_ucontext
{
433 struct ib_ucontext ibucontext
;
435 struct list_head mm_head
;
436 struct mutex mm_list_lock
; /* protects list entries of mm type */
437 struct ocrdma_pd
*cntxt_pd
;
452 struct list_head entry
;
455 static inline struct ocrdma_dev
*get_ocrdma_dev(struct ib_device
*ibdev
)
457 return container_of(ibdev
, struct ocrdma_dev
, ibdev
);
460 static inline struct ocrdma_ucontext
*get_ocrdma_ucontext(struct ib_ucontext
463 return container_of(ibucontext
, struct ocrdma_ucontext
, ibucontext
);
466 static inline struct ocrdma_pd
*get_ocrdma_pd(struct ib_pd
*ibpd
)
468 return container_of(ibpd
, struct ocrdma_pd
, ibpd
);
471 static inline struct ocrdma_cq
*get_ocrdma_cq(struct ib_cq
*ibcq
)
473 return container_of(ibcq
, struct ocrdma_cq
, ibcq
);
476 static inline struct ocrdma_qp
*get_ocrdma_qp(struct ib_qp
*ibqp
)
478 return container_of(ibqp
, struct ocrdma_qp
, ibqp
);
481 static inline struct ocrdma_mr
*get_ocrdma_mr(struct ib_mr
*ibmr
)
483 return container_of(ibmr
, struct ocrdma_mr
, ibmr
);
486 static inline struct ocrdma_ah
*get_ocrdma_ah(struct ib_ah
*ibah
)
488 return container_of(ibah
, struct ocrdma_ah
, ibah
);
491 static inline struct ocrdma_srq
*get_ocrdma_srq(struct ib_srq
*ibsrq
)
493 return container_of(ibsrq
, struct ocrdma_srq
, ibsrq
);
496 static inline int is_cqe_valid(struct ocrdma_cq
*cq
, struct ocrdma_cqe
*cqe
)
499 cqe_valid
= le32_to_cpu(cqe
->flags_status_srcqpn
) & OCRDMA_CQE_VALID
;
500 return (cqe_valid
== cq
->phase
);
503 static inline int is_cqe_for_sq(struct ocrdma_cqe
*cqe
)
505 return (le32_to_cpu(cqe
->flags_status_srcqpn
) &
506 OCRDMA_CQE_QTYPE
) ? 0 : 1;
509 static inline int is_cqe_invalidated(struct ocrdma_cqe
*cqe
)
511 return (le32_to_cpu(cqe
->flags_status_srcqpn
) &
512 OCRDMA_CQE_INVALIDATE
) ? 1 : 0;
515 static inline int is_cqe_imm(struct ocrdma_cqe
*cqe
)
517 return (le32_to_cpu(cqe
->flags_status_srcqpn
) &
518 OCRDMA_CQE_IMM
) ? 1 : 0;
521 static inline int is_cqe_wr_imm(struct ocrdma_cqe
*cqe
)
523 return (le32_to_cpu(cqe
->flags_status_srcqpn
) &
524 OCRDMA_CQE_WRITE_IMM
) ? 1 : 0;
527 static inline int ocrdma_resolve_dmac(struct ocrdma_dev
*dev
,
528 struct rdma_ah_attr
*ah_attr
, u8
*mac_addr
)
532 memcpy(&in6
, rdma_ah_read_grh(ah_attr
)->dgid
.raw
, sizeof(in6
));
533 if (rdma_is_multicast_addr(&in6
))
534 rdma_get_mcast_mac(&in6
, mac_addr
);
535 else if (rdma_link_local_addr(&in6
))
536 rdma_get_ll_mac(&in6
, mac_addr
);
538 memcpy(mac_addr
, ah_attr
->roce
.dmac
, ETH_ALEN
);
542 static inline char *hca_name(struct ocrdma_dev
*dev
)
544 switch (dev
->nic_info
.pdev
->device
) {
545 case OC_SKH_DEVICE_PF
:
546 case OC_SKH_DEVICE_VF
:
549 return OC_NAME_UNKNOWN
;
553 static inline int ocrdma_get_eq_table_index(struct ocrdma_dev
*dev
,
558 for (indx
= 0; indx
< dev
->eq_cnt
; indx
++) {
559 if (dev
->eq_tbl
[indx
].q
.id
== eqid
)
566 static inline u8
ocrdma_get_asic_type(struct ocrdma_dev
*dev
)
568 if (dev
->nic_info
.dev_family
== 0xF && !dev
->asic_id
) {
569 pci_read_config_dword(
571 OCRDMA_SLI_ASIC_ID_OFFSET
, &dev
->asic_id
);
574 return (dev
->asic_id
& OCRDMA_SLI_ASIC_GEN_NUM_MASK
) >>
575 OCRDMA_SLI_ASIC_GEN_NUM_SHIFT
;
578 static inline u8
ocrdma_get_pfc_prio(u8
*pfc
, u8 prio
)
580 return *(pfc
+ prio
);
583 static inline u8
ocrdma_get_app_prio(u8
*app_prio
, u8 prio
)
585 return *(app_prio
+ prio
);
588 static inline u8
ocrdma_is_enabled_and_synced(u32 state
)
589 { /* May also be used to interpret TC-state, QCN-state
590 * Appl-state and Logical-link-state in future.
592 return (state
& OCRDMA_STATE_FLAG_ENABLED
) &&
593 (state
& OCRDMA_STATE_FLAG_SYNC
);
596 static inline u8
ocrdma_get_ae_link_state(u32 ae_state
)
598 return ((ae_state
& OCRDMA_AE_LSC_LS_MASK
) >> OCRDMA_AE_LSC_LS_SHIFT
);
601 static inline bool ocrdma_is_udp_encap_supported(struct ocrdma_dev
*dev
)
603 return (dev
->attr
.udp_encap
& OCRDMA_L3_TYPE_IPV4
) ||
604 (dev
->attr
.udp_encap
& OCRDMA_L3_TYPE_IPV6
);