1 /*******************************************************************
2 * This file is part of the Emulex RoCE Device Driver for *
3 * RoCE (RDMA over Converged Ethernet) adapters. *
4 * Copyright (C) 2008-2012 Emulex. All rights reserved. *
5 * EMULEX and SLI are trademarks of Emulex. *
8 * This program is free software; you can redistribute it and/or *
9 * modify it under the terms of version 2 of the GNU General *
10 * Public License as published by the Free Software Foundation. *
11 * This program is distributed in the hope that it will be useful. *
12 * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND *
13 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, *
14 * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE *
15 * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
16 * TO BE LEGALLY INVALID. See the GNU General Public License for *
17 * more details, a copy of which can be found in the file COPYING *
18 * included with this package. *
20 * Contact Information:
21 * linux-drivers@emulex.com
25 * Costa Mesa, CA 92626
26 *******************************************************************/
31 #include <linux/mutex.h>
32 #include <linux/list.h>
33 #include <linux/spinlock.h>
34 #include <linux/pci.h>
36 #include <rdma/ib_verbs.h>
37 #include <rdma/ib_user_verbs.h>
38 #include <rdma/ib_addr.h>
41 #include "ocrdma_sli.h"
43 #define OCRDMA_ROCE_DRV_VERSION "10.2.287.0u"
45 #define OCRDMA_ROCE_DRV_DESC "Emulex OneConnect RoCE Driver"
46 #define OCRDMA_NODE_DESC "Emulex OneConnect RoCE HCA"
48 #define OC_NAME_SH OCRDMA_NODE_DESC "(Skyhawk)"
49 #define OC_NAME_UNKNOWN OCRDMA_NODE_DESC "(Unknown)"
51 #define OC_SKH_DEVICE_PF 0x720
52 #define OC_SKH_DEVICE_VF 0x728
53 #define OCRDMA_MAX_AH 512
55 #define OCRDMA_UVERBS(CMD_NAME) (1ull << IB_USER_VERBS_CMD_##CMD_NAME)
57 #define convert_to_64bit(lo, hi) ((u64)hi << 32 | (u64)lo)
59 struct ocrdma_dev_attr {
82 int max_pages_per_frmr;
87 u8 cq_overflow_detect;
93 u8 local_ca_ack_delay;
98 struct ocrdma_dma_mem {
109 struct ocrdma_queue_info {
114 u16 entry_size; /* Size of an element in the queue */
115 u16 id; /* qid, where to ring the doorbell. */
121 struct ocrdma_queue_info q;
124 struct ocrdma_dev *dev;
129 struct ocrdma_queue_info sq;
130 struct ocrdma_queue_info cq;
135 struct mutex lock; /* for serializing mailbox commands on MQ */
136 wait_queue_head_t cmd_wait;
144 struct ocrdma_hw_mr {
155 struct ocrdma_pbl *pbl_table;
166 struct ib_umem *umem;
167 struct ocrdma_hw_mr hwmr;
170 struct ocrdma_stats {
172 struct ocrdma_dev *dev;
175 struct ocrdma_pd_resource_mgr {
185 unsigned long *pd_norm_bitmap;
186 unsigned long *pd_dpp_bitmap;
187 bool pd_prealloc_valid;
191 struct ocrdma_mqe mqe;
199 u16 auto_speeds_supported;
200 u16 fixed_speeds_supported;
206 struct ib_device ibdev;
207 struct ocrdma_dev_attr attr;
209 struct mutex dev_lock; /* provides syncronise access to device data */
210 spinlock_t flush_q_lock ____cacheline_aligned;
212 struct ocrdma_cq **cq_tbl;
213 struct ocrdma_qp **qp_tbl;
215 struct ocrdma_eq *eq_tbl;
220 union ib_gid *sgid_tbl;
221 /* provided synchronization to sgid table for
222 * updating gid entries triggered by notifier.
224 spinlock_t sgid_lock;
227 struct ocrdma_cq *gsi_sqcq;
228 struct ocrdma_cq *gsi_rqcq;
231 struct ocrdma_av *va;
235 /* provide synchronization for av
240 struct ocrdma_pbl pbl;
245 struct mqe_ctx mqe_ctx;
247 struct be_dev_info nic_info;
249 char model_number[32];
252 struct list_head entry;
256 u8 sl; /* service level */
262 ulong last_stats_time;
263 struct mutex stats_lock; /* provide synch for debugfs operations */
264 struct stats_mem stats_mem;
265 struct ocrdma_stats rsrc_stats;
266 struct ocrdma_stats rx_stats;
267 struct ocrdma_stats wqe_stats;
268 struct ocrdma_stats tx_stats;
269 struct ocrdma_stats db_err_stats;
270 struct ocrdma_stats tx_qp_err_stats;
271 struct ocrdma_stats rx_qp_err_stats;
272 struct ocrdma_stats tx_dbg_stats;
273 struct ocrdma_stats rx_dbg_stats;
275 struct ocrdma_pd_resource_mgr *pd_mgr;
280 struct ocrdma_cqe *va;
282 u32 getp; /* pointer to pending wrs to
283 * return to stack, wrap arounds
288 bool deferred_arm, deferred_sol;
291 spinlock_t cq_lock ____cacheline_aligned; /* provide synchronization
294 /* syncronizes cq completion handler invoked from multiple context */
295 spinlock_t comp_handler_lock ____cacheline_aligned;
299 struct ocrdma_ucontext *ucontext;
304 /* head of all qp's sq and rq for which cqes need to be flushed
307 struct list_head sq_head, rq_head;
312 struct ocrdma_ucontext *uctx;
321 struct ocrdma_av *av;
326 struct ocrdma_qp_hwq_info {
327 u8 *va; /* virtual address */
333 u16 dbid; /* qid, where to ring the doorbell. */
341 struct ocrdma_qp_hwq_info rq;
346 /* provide synchronization to multiple context(s) posting rqe */
347 spinlock_t q_lock ____cacheline_aligned;
349 struct ocrdma_pd *pd;
355 struct ocrdma_dev *dev;
358 struct ocrdma_qp_hwq_info sq;
361 uint16_t dpp_wqe_idx;
368 /* provide synchronization to multiple context(s) posting wqe, rqe */
369 spinlock_t q_lock ____cacheline_aligned;
370 struct ocrdma_cq *sq_cq;
371 /* list maintained per CQ to flush SQ errors */
372 struct list_head sq_entry;
375 struct ocrdma_qp_hwq_info rq;
377 struct ocrdma_cq *rq_cq;
378 struct ocrdma_srq *srq;
379 /* list maintained per CQ to flush RQ errors */
380 struct list_head rq_entry;
382 enum ocrdma_qp_state state; /* QP state */
384 u32 max_ord, max_ird;
387 struct ocrdma_pd *pd;
389 enum ib_qp_type qp_type;
398 struct ocrdma_ucontext {
399 struct ib_ucontext ibucontext;
401 struct list_head mm_head;
402 struct mutex mm_list_lock; /* protects list entries of mm type */
403 struct ocrdma_pd *cntxt_pd;
418 struct list_head entry;
421 static inline struct ocrdma_dev *get_ocrdma_dev(struct ib_device *ibdev)
423 return container_of(ibdev, struct ocrdma_dev, ibdev);
426 static inline struct ocrdma_ucontext *get_ocrdma_ucontext(struct ib_ucontext
429 return container_of(ibucontext, struct ocrdma_ucontext, ibucontext);
432 static inline struct ocrdma_pd *get_ocrdma_pd(struct ib_pd *ibpd)
434 return container_of(ibpd, struct ocrdma_pd, ibpd);
437 static inline struct ocrdma_cq *get_ocrdma_cq(struct ib_cq *ibcq)
439 return container_of(ibcq, struct ocrdma_cq, ibcq);
442 static inline struct ocrdma_qp *get_ocrdma_qp(struct ib_qp *ibqp)
444 return container_of(ibqp, struct ocrdma_qp, ibqp);
447 static inline struct ocrdma_mr *get_ocrdma_mr(struct ib_mr *ibmr)
449 return container_of(ibmr, struct ocrdma_mr, ibmr);
452 static inline struct ocrdma_ah *get_ocrdma_ah(struct ib_ah *ibah)
454 return container_of(ibah, struct ocrdma_ah, ibah);
457 static inline struct ocrdma_srq *get_ocrdma_srq(struct ib_srq *ibsrq)
459 return container_of(ibsrq, struct ocrdma_srq, ibsrq);
462 static inline int is_cqe_valid(struct ocrdma_cq *cq, struct ocrdma_cqe *cqe)
465 cqe_valid = le32_to_cpu(cqe->flags_status_srcqpn) & OCRDMA_CQE_VALID;
466 return (cqe_valid == cq->phase);
469 static inline int is_cqe_for_sq(struct ocrdma_cqe *cqe)
471 return (le32_to_cpu(cqe->flags_status_srcqpn) &
472 OCRDMA_CQE_QTYPE) ? 0 : 1;
475 static inline int is_cqe_invalidated(struct ocrdma_cqe *cqe)
477 return (le32_to_cpu(cqe->flags_status_srcqpn) &
478 OCRDMA_CQE_INVALIDATE) ? 1 : 0;
481 static inline int is_cqe_imm(struct ocrdma_cqe *cqe)
483 return (le32_to_cpu(cqe->flags_status_srcqpn) &
484 OCRDMA_CQE_IMM) ? 1 : 0;
487 static inline int is_cqe_wr_imm(struct ocrdma_cqe *cqe)
489 return (le32_to_cpu(cqe->flags_status_srcqpn) &
490 OCRDMA_CQE_WRITE_IMM) ? 1 : 0;
493 static inline int ocrdma_resolve_dmac(struct ocrdma_dev *dev,
494 struct ib_ah_attr *ah_attr, u8 *mac_addr)
498 memcpy(&in6, ah_attr->grh.dgid.raw, sizeof(in6));
499 if (rdma_is_multicast_addr(&in6))
500 rdma_get_mcast_mac(&in6, mac_addr);
502 memcpy(mac_addr, ah_attr->dmac, ETH_ALEN);
506 static inline char *hca_name(struct ocrdma_dev *dev)
508 switch (dev->nic_info.pdev->device) {
509 case OC_SKH_DEVICE_PF:
510 case OC_SKH_DEVICE_VF:
513 return OC_NAME_UNKNOWN;
517 static inline int ocrdma_get_eq_table_index(struct ocrdma_dev *dev,
522 for (indx = 0; indx < dev->eq_cnt; indx++) {
523 if (dev->eq_tbl[indx].q.id == eqid)
530 static inline u8 ocrdma_get_asic_type(struct ocrdma_dev *dev)
532 if (dev->nic_info.dev_family == 0xF && !dev->asic_id) {
533 pci_read_config_dword(
535 OCRDMA_SLI_ASIC_ID_OFFSET, &dev->asic_id);
538 return (dev->asic_id & OCRDMA_SLI_ASIC_GEN_NUM_MASK) >>
539 OCRDMA_SLI_ASIC_GEN_NUM_SHIFT;
542 static inline u8 ocrdma_get_pfc_prio(u8 *pfc, u8 prio)
544 return *(pfc + prio);
547 static inline u8 ocrdma_get_app_prio(u8 *app_prio, u8 prio)
549 return *(app_prio + prio);
552 static inline u8 ocrdma_is_enabled_and_synced(u32 state)
553 { /* May also be used to interpret TC-state, QCN-state
554 * Appl-state and Logical-link-state in future.
556 return (state & OCRDMA_STATE_FLAG_ENABLED) &&
557 (state & OCRDMA_STATE_FLAG_SYNC);