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