RDMA/ocrdma: Query controller information
[cascardo/linux.git] / drivers / infiniband / hw / ocrdma / ocrdma.h
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.                        *
6  * www.emulex.com                                                  *
7  *                                                                 *
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.                                     *
19  *
20  * Contact Information:
21  * linux-drivers@emulex.com
22  *
23  * Emulex
24  * 3333 Susan Street
25  * Costa Mesa, CA 92626
26  *******************************************************************/
27
28 #ifndef __OCRDMA_H__
29 #define __OCRDMA_H__
30
31 #include <linux/mutex.h>
32 #include <linux/list.h>
33 #include <linux/spinlock.h>
34 #include <linux/pci.h>
35
36 #include <rdma/ib_verbs.h>
37 #include <rdma/ib_user_verbs.h>
38
39 #include <be_roce.h>
40 #include "ocrdma_sli.h"
41
42 #define OCRDMA_ROCE_DRV_VERSION "10.2.145.0u"
43
44 #define OCRDMA_ROCE_DRV_DESC "Emulex OneConnect RoCE Driver"
45 #define OCRDMA_NODE_DESC "Emulex OneConnect RoCE HCA"
46
47 #define OC_NAME_SH      OCRDMA_NODE_DESC "(Skyhawk)"
48 #define OC_NAME_UNKNOWN OCRDMA_NODE_DESC "(Unknown)"
49
50 #define OC_SKH_DEVICE_PF 0x720
51 #define OC_SKH_DEVICE_VF 0x728
52 #define OCRDMA_MAX_AH 512
53
54 #define OCRDMA_UVERBS(CMD_NAME) (1ull << IB_USER_VERBS_CMD_##CMD_NAME)
55
56 #define convert_to_64bit(lo, hi) ((u64)hi << 32 | (u64)lo)
57
58 struct ocrdma_dev_attr {
59         u8 fw_ver[32];
60         u32 vendor_id;
61         u32 device_id;
62         u16 max_pd;
63         u16 max_cq;
64         u16 max_cqe;
65         u16 max_qp;
66         u16 max_wqe;
67         u16 max_rqe;
68         u16 max_srq;
69         u32 max_inline_data;
70         int max_send_sge;
71         int max_recv_sge;
72         int max_srq_sge;
73         int max_rdma_sge;
74         int max_mr;
75         u64 max_mr_size;
76         u32 max_num_mr_pbl;
77         int max_mw;
78         int max_fmr;
79         int max_map_per_fmr;
80         int max_pages_per_frmr;
81         u16 max_ord_per_qp;
82         u16 max_ird_per_qp;
83
84         int device_cap_flags;
85         u8 cq_overflow_detect;
86         u8 srq_supported;
87
88         u32 wqe_size;
89         u32 rqe_size;
90         u32 ird_page_size;
91         u8 local_ca_ack_delay;
92         u8 ird;
93         u8 num_ird_pages;
94 };
95
96 struct ocrdma_dma_mem {
97         void *va;
98         dma_addr_t pa;
99         u32 size;
100 };
101
102 struct ocrdma_pbl {
103         void *va;
104         dma_addr_t pa;
105 };
106
107 struct ocrdma_queue_info {
108         void *va;
109         dma_addr_t dma;
110         u32 size;
111         u16 len;
112         u16 entry_size;         /* Size of an element in the queue */
113         u16 id;                 /* qid, where to ring the doorbell. */
114         u16 head, tail;
115         bool created;
116 };
117
118 struct ocrdma_eq {
119         struct ocrdma_queue_info q;
120         u32 vector;
121         int cq_cnt;
122         struct ocrdma_dev *dev;
123         char irq_name[32];
124 };
125
126 struct ocrdma_mq {
127         struct ocrdma_queue_info sq;
128         struct ocrdma_queue_info cq;
129         bool rearm_cq;
130 };
131
132 struct mqe_ctx {
133         struct mutex lock; /* for serializing mailbox commands on MQ */
134         wait_queue_head_t cmd_wait;
135         u32 tag;
136         u16 cqe_status;
137         u16 ext_status;
138         bool cmd_done;
139 };
140
141 struct ocrdma_hw_mr {
142         u32 lkey;
143         u8 fr_mr;
144         u8 remote_atomic;
145         u8 remote_rd;
146         u8 remote_wr;
147         u8 local_rd;
148         u8 local_wr;
149         u8 mw_bind;
150         u8 rsvd;
151         u64 len;
152         struct ocrdma_pbl *pbl_table;
153         u32 num_pbls;
154         u32 num_pbes;
155         u32 pbl_size;
156         u32 pbe_size;
157         u64 fbo;
158         u64 va;
159 };
160
161 struct ocrdma_mr {
162         struct ib_mr ibmr;
163         struct ib_umem *umem;
164         struct ocrdma_hw_mr hwmr;
165 };
166
167 struct ocrdma_stats {
168         u8 type;
169         struct ocrdma_dev *dev;
170 };
171
172 struct stats_mem {
173         struct ocrdma_mqe mqe;
174         void *va;
175         dma_addr_t pa;
176         u32 size;
177         char *debugfs_mem;
178 };
179
180 struct phy_info {
181         u16 auto_speeds_supported;
182         u16 fixed_speeds_supported;
183         u16 phy_type;
184         u16 interface_type;
185 };
186
187 struct ocrdma_dev {
188         struct ib_device ibdev;
189         struct ocrdma_dev_attr attr;
190
191         struct mutex dev_lock; /* provides syncronise access to device data */
192         spinlock_t flush_q_lock ____cacheline_aligned;
193
194         struct ocrdma_cq **cq_tbl;
195         struct ocrdma_qp **qp_tbl;
196
197         struct ocrdma_eq *eq_tbl;
198         int eq_cnt;
199         u16 base_eqid;
200         u16 max_eq;
201
202         union ib_gid *sgid_tbl;
203         /* provided synchronization to sgid table for
204          * updating gid entries triggered by notifier.
205          */
206         spinlock_t sgid_lock;
207
208         int gsi_qp_created;
209         struct ocrdma_cq *gsi_sqcq;
210         struct ocrdma_cq *gsi_rqcq;
211
212         struct {
213                 struct ocrdma_av *va;
214                 dma_addr_t pa;
215                 u32 size;
216                 u32 num_ah;
217                 /* provide synchronization for av
218                  * entry allocations.
219                  */
220                 spinlock_t lock;
221                 u32 ahid;
222                 struct ocrdma_pbl pbl;
223         } av_tbl;
224
225         void *mbx_cmd;
226         struct ocrdma_mq mq;
227         struct mqe_ctx mqe_ctx;
228
229         struct be_dev_info nic_info;
230         struct phy_info phy;
231         char model_number[32];
232         u32 hba_port_num;
233
234         struct list_head entry;
235         struct rcu_head rcu;
236         int id;
237         struct ocrdma_mr *stag_arr[OCRDMA_MAX_STAG];
238         u16 pvid;
239         u32 asic_id;
240
241         ulong last_stats_time;
242         struct mutex stats_lock; /* provide synch for debugfs operations */
243         struct stats_mem stats_mem;
244         struct ocrdma_stats rsrc_stats;
245         struct ocrdma_stats rx_stats;
246         struct ocrdma_stats wqe_stats;
247         struct ocrdma_stats tx_stats;
248         struct ocrdma_stats db_err_stats;
249         struct ocrdma_stats tx_qp_err_stats;
250         struct ocrdma_stats rx_qp_err_stats;
251         struct ocrdma_stats tx_dbg_stats;
252         struct ocrdma_stats rx_dbg_stats;
253         struct dentry *dir;
254 };
255
256 struct ocrdma_cq {
257         struct ib_cq ibcq;
258         struct ocrdma_cqe *va;
259         u32 phase;
260         u32 getp;       /* pointer to pending wrs to
261                          * return to stack, wrap arounds
262                          * at max_hw_cqe
263                          */
264         u32 max_hw_cqe;
265         bool phase_change;
266         bool deferred_arm, deferred_sol;
267         bool first_arm;
268
269         spinlock_t cq_lock ____cacheline_aligned; /* provide synchronization
270                                                    * to cq polling
271                                                    */
272         /* syncronizes cq completion handler invoked from multiple context */
273         spinlock_t comp_handler_lock ____cacheline_aligned;
274         u16 id;
275         u16 eqn;
276
277         struct ocrdma_ucontext *ucontext;
278         dma_addr_t pa;
279         u32 len;
280         u32 cqe_cnt;
281
282         /* head of all qp's sq and rq for which cqes need to be flushed
283          * by the software.
284          */
285         struct list_head sq_head, rq_head;
286 };
287
288 struct ocrdma_pd {
289         struct ib_pd ibpd;
290         struct ocrdma_dev *dev;
291         struct ocrdma_ucontext *uctx;
292         u32 id;
293         int num_dpp_qp;
294         u32 dpp_page;
295         bool dpp_enabled;
296 };
297
298 struct ocrdma_ah {
299         struct ib_ah ibah;
300         struct ocrdma_av *av;
301         u16 sgid_index;
302         u32 id;
303 };
304
305 struct ocrdma_qp_hwq_info {
306         u8 *va;                 /* virtual address */
307         u32 max_sges;
308         u32 head, tail;
309         u32 entry_size;
310         u32 max_cnt;
311         u32 max_wqe_idx;
312         u16 dbid;               /* qid, where to ring the doorbell. */
313         u32 len;
314         dma_addr_t pa;
315 };
316
317 struct ocrdma_srq {
318         struct ib_srq ibsrq;
319         u8 __iomem *db;
320         struct ocrdma_qp_hwq_info rq;
321         u64 *rqe_wr_id_tbl;
322         u32 *idx_bit_fields;
323         u32 bit_fields_len;
324
325         /* provide synchronization to multiple context(s) posting rqe */
326         spinlock_t q_lock ____cacheline_aligned;
327
328         struct ocrdma_pd *pd;
329         u32 id;
330 };
331
332 struct ocrdma_qp {
333         struct ib_qp ibqp;
334         struct ocrdma_dev *dev;
335
336         u8 __iomem *sq_db;
337         struct ocrdma_qp_hwq_info sq;
338         struct {
339                 uint64_t wrid;
340                 uint16_t dpp_wqe_idx;
341                 uint16_t dpp_wqe;
342                 uint8_t  signaled;
343                 uint8_t  rsvd[3];
344         } *wqe_wr_id_tbl;
345         u32 max_inline_data;
346
347         /* provide synchronization to multiple context(s) posting wqe, rqe */
348         spinlock_t q_lock ____cacheline_aligned;
349         struct ocrdma_cq *sq_cq;
350         /* list maintained per CQ to flush SQ errors */
351         struct list_head sq_entry;
352
353         u8 __iomem *rq_db;
354         struct ocrdma_qp_hwq_info rq;
355         u64 *rqe_wr_id_tbl;
356         struct ocrdma_cq *rq_cq;
357         struct ocrdma_srq *srq;
358         /* list maintained per CQ to flush RQ errors */
359         struct list_head rq_entry;
360
361         enum ocrdma_qp_state state;     /*  QP state */
362         int cap_flags;
363         u32 max_ord, max_ird;
364
365         u32 id;
366         struct ocrdma_pd *pd;
367
368         enum ib_qp_type qp_type;
369
370         int sgid_idx;
371         u32 qkey;
372         bool dpp_enabled;
373         u8 *ird_q_va;
374         bool signaled;
375         u16 db_cache;
376 };
377
378
379 struct ocrdma_ucontext {
380         struct ib_ucontext ibucontext;
381
382         struct list_head mm_head;
383         struct mutex mm_list_lock; /* protects list entries of mm type */
384         struct ocrdma_pd *cntxt_pd;
385         int pd_in_use;
386
387         struct {
388                 u32 *va;
389                 dma_addr_t pa;
390                 u32 len;
391         } ah_tbl;
392 };
393
394 struct ocrdma_mm {
395         struct {
396                 u64 phy_addr;
397                 unsigned long len;
398         } key;
399         struct list_head entry;
400 };
401
402 static inline struct ocrdma_dev *get_ocrdma_dev(struct ib_device *ibdev)
403 {
404         return container_of(ibdev, struct ocrdma_dev, ibdev);
405 }
406
407 static inline struct ocrdma_ucontext *get_ocrdma_ucontext(struct ib_ucontext
408                                                           *ibucontext)
409 {
410         return container_of(ibucontext, struct ocrdma_ucontext, ibucontext);
411 }
412
413 static inline struct ocrdma_pd *get_ocrdma_pd(struct ib_pd *ibpd)
414 {
415         return container_of(ibpd, struct ocrdma_pd, ibpd);
416 }
417
418 static inline struct ocrdma_cq *get_ocrdma_cq(struct ib_cq *ibcq)
419 {
420         return container_of(ibcq, struct ocrdma_cq, ibcq);
421 }
422
423 static inline struct ocrdma_qp *get_ocrdma_qp(struct ib_qp *ibqp)
424 {
425         return container_of(ibqp, struct ocrdma_qp, ibqp);
426 }
427
428 static inline struct ocrdma_mr *get_ocrdma_mr(struct ib_mr *ibmr)
429 {
430         return container_of(ibmr, struct ocrdma_mr, ibmr);
431 }
432
433 static inline struct ocrdma_ah *get_ocrdma_ah(struct ib_ah *ibah)
434 {
435         return container_of(ibah, struct ocrdma_ah, ibah);
436 }
437
438 static inline struct ocrdma_srq *get_ocrdma_srq(struct ib_srq *ibsrq)
439 {
440         return container_of(ibsrq, struct ocrdma_srq, ibsrq);
441 }
442
443 static inline int is_cqe_valid(struct ocrdma_cq *cq, struct ocrdma_cqe *cqe)
444 {
445         int cqe_valid;
446         cqe_valid = le32_to_cpu(cqe->flags_status_srcqpn) & OCRDMA_CQE_VALID;
447         return (cqe_valid == cq->phase);
448 }
449
450 static inline int is_cqe_for_sq(struct ocrdma_cqe *cqe)
451 {
452         return (le32_to_cpu(cqe->flags_status_srcqpn) &
453                 OCRDMA_CQE_QTYPE) ? 0 : 1;
454 }
455
456 static inline int is_cqe_invalidated(struct ocrdma_cqe *cqe)
457 {
458         return (le32_to_cpu(cqe->flags_status_srcqpn) &
459                 OCRDMA_CQE_INVALIDATE) ? 1 : 0;
460 }
461
462 static inline int is_cqe_imm(struct ocrdma_cqe *cqe)
463 {
464         return (le32_to_cpu(cqe->flags_status_srcqpn) &
465                 OCRDMA_CQE_IMM) ? 1 : 0;
466 }
467
468 static inline int is_cqe_wr_imm(struct ocrdma_cqe *cqe)
469 {
470         return (le32_to_cpu(cqe->flags_status_srcqpn) &
471                 OCRDMA_CQE_WRITE_IMM) ? 1 : 0;
472 }
473
474 static inline int ocrdma_resolve_dmac(struct ocrdma_dev *dev,
475                 struct ib_ah_attr *ah_attr, u8 *mac_addr)
476 {
477         struct in6_addr in6;
478
479         memcpy(&in6, ah_attr->grh.dgid.raw, sizeof(in6));
480         if (rdma_is_multicast_addr(&in6))
481                 rdma_get_mcast_mac(&in6, mac_addr);
482         else
483                 memcpy(mac_addr, ah_attr->dmac, ETH_ALEN);
484         return 0;
485 }
486
487 static inline char *hca_name(struct ocrdma_dev *dev)
488 {
489         switch (dev->nic_info.pdev->device) {
490         case OC_SKH_DEVICE_PF:
491         case OC_SKH_DEVICE_VF:
492                 return OC_NAME_SH;
493         default:
494                 return OC_NAME_UNKNOWN;
495         }
496 }
497
498 static inline int ocrdma_get_eq_table_index(struct ocrdma_dev *dev,
499                 int eqid)
500 {
501         int indx;
502
503         for (indx = 0; indx < dev->eq_cnt; indx++) {
504                 if (dev->eq_tbl[indx].q.id == eqid)
505                         return indx;
506         }
507
508         return -EINVAL;
509 }
510
511 static inline u8 ocrdma_get_asic_type(struct ocrdma_dev *dev)
512 {
513         if (dev->nic_info.dev_family == 0xF && !dev->asic_id) {
514                 pci_read_config_dword(
515                         dev->nic_info.pdev,
516                         OCRDMA_SLI_ASIC_ID_OFFSET, &dev->asic_id);
517         }
518
519         return (dev->asic_id & OCRDMA_SLI_ASIC_GEN_NUM_MASK) >>
520                                 OCRDMA_SLI_ASIC_GEN_NUM_SHIFT;
521 }
522
523 #endif