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