qedr.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  1. /* QLogic qedr NIC Driver
  2. * Copyright (c) 2015-2016 QLogic Corporation
  3. *
  4. * This software is available to you under a choice of one of two
  5. * licenses. You may choose to be licensed under the terms of the GNU
  6. * General Public License (GPL) Version 2, available from the file
  7. * COPYING in the main directory of this source tree, or the
  8. * OpenIB.org BSD license below:
  9. *
  10. * Redistribution and use in source and binary forms, with or
  11. * without modification, are permitted provided that the following
  12. * conditions are met:
  13. *
  14. * - Redistributions of source code must retain the above
  15. * copyright notice, this list of conditions and the following
  16. * disclaimer.
  17. *
  18. * - Redistributions in binary form must reproduce the above
  19. * copyright notice, this list of conditions and the following
  20. * disclaimer in the documentation and /or other materials
  21. * provided with the distribution.
  22. *
  23. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  27. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  28. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  29. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  30. * SOFTWARE.
  31. */
  32. #ifndef __QEDR_H__
  33. #define __QEDR_H__
  34. #include <linux/pci.h>
  35. #include <linux/idr.h>
  36. #include <rdma/ib_addr.h>
  37. #include <linux/qed/qed_if.h>
  38. #include <linux/qed/qed_chain.h>
  39. #include <linux/qed/qed_rdma_if.h>
  40. #include <linux/qed/qede_rdma.h>
  41. #include <linux/qed/roce_common.h>
  42. #include "qedr_hsi_rdma.h"
  43. #define QEDR_NODE_DESC "QLogic 579xx RoCE HCA"
  44. #define DP_NAME(dev) ((dev)->ibdev.name)
  45. #define IS_IWARP(_dev) ((_dev)->rdma_type == QED_RDMA_TYPE_IWARP)
  46. #define IS_ROCE(_dev) ((_dev)->rdma_type == QED_RDMA_TYPE_ROCE)
  47. #define DP_DEBUG(dev, module, fmt, ...) \
  48. pr_debug("(%s) " module ": " fmt, \
  49. DP_NAME(dev) ? DP_NAME(dev) : "", ## __VA_ARGS__)
  50. #define QEDR_MSG_INIT "INIT"
  51. #define QEDR_MSG_MISC "MISC"
  52. #define QEDR_MSG_CQ " CQ"
  53. #define QEDR_MSG_MR " MR"
  54. #define QEDR_MSG_RQ " RQ"
  55. #define QEDR_MSG_SQ " SQ"
  56. #define QEDR_MSG_QP " QP"
  57. #define QEDR_MSG_GSI " GSI"
  58. #define QEDR_MSG_IWARP " IW"
  59. #define QEDR_CQ_MAGIC_NUMBER (0x11223344)
  60. #define FW_PAGE_SIZE (RDMA_RING_PAGE_SIZE)
  61. #define FW_PAGE_SHIFT (12)
  62. struct qedr_dev;
  63. struct qedr_cnq {
  64. struct qedr_dev *dev;
  65. struct qed_chain pbl;
  66. struct qed_sb_info *sb;
  67. char name[32];
  68. u64 n_comp;
  69. __le16 *hw_cons_ptr;
  70. u8 index;
  71. };
  72. #define QEDR_MAX_SGID 128
  73. struct qedr_device_attr {
  74. u32 vendor_id;
  75. u32 vendor_part_id;
  76. u32 hw_ver;
  77. u64 fw_ver;
  78. u64 node_guid;
  79. u64 sys_image_guid;
  80. u8 max_cnq;
  81. u8 max_sge;
  82. u16 max_inline;
  83. u32 max_sqe;
  84. u32 max_rqe;
  85. u8 max_qp_resp_rd_atomic_resc;
  86. u8 max_qp_req_rd_atomic_resc;
  87. u64 max_dev_resp_rd_atomic_resc;
  88. u32 max_cq;
  89. u32 max_qp;
  90. u32 max_mr;
  91. u64 max_mr_size;
  92. u32 max_cqe;
  93. u32 max_mw;
  94. u32 max_fmr;
  95. u32 max_mr_mw_fmr_pbl;
  96. u64 max_mr_mw_fmr_size;
  97. u32 max_pd;
  98. u32 max_ah;
  99. u8 max_pkey;
  100. u32 max_srq;
  101. u32 max_srq_wr;
  102. u8 max_srq_sge;
  103. u8 max_stats_queues;
  104. u32 dev_caps;
  105. u64 page_size_caps;
  106. u8 dev_ack_delay;
  107. u32 reserved_lkey;
  108. u32 bad_pkey_counter;
  109. struct qed_rdma_events events;
  110. };
  111. #define QEDR_ENET_STATE_BIT (0)
  112. struct qedr_dev {
  113. struct ib_device ibdev;
  114. struct qed_dev *cdev;
  115. struct pci_dev *pdev;
  116. struct net_device *ndev;
  117. enum ib_atomic_cap atomic_cap;
  118. void *rdma_ctx;
  119. struct qedr_device_attr attr;
  120. const struct qed_rdma_ops *ops;
  121. struct qed_int_info int_info;
  122. struct qed_sb_info *sb_array;
  123. struct qedr_cnq *cnq_array;
  124. int num_cnq;
  125. int sb_start;
  126. void __iomem *db_addr;
  127. u64 db_phys_addr;
  128. u32 db_size;
  129. u16 dpi;
  130. union ib_gid *sgid_tbl;
  131. /* Lock for sgid table */
  132. spinlock_t sgid_lock;
  133. u64 guid;
  134. u32 dp_module;
  135. u8 dp_level;
  136. u8 num_hwfns;
  137. u8 gsi_ll2_handle;
  138. uint wq_multiplier;
  139. u8 gsi_ll2_mac_address[ETH_ALEN];
  140. int gsi_qp_created;
  141. struct qedr_cq *gsi_sqcq;
  142. struct qedr_cq *gsi_rqcq;
  143. struct qedr_qp *gsi_qp;
  144. enum qed_rdma_type rdma_type;
  145. spinlock_t idr_lock; /* Protect qpidr data-structure */
  146. struct idr qpidr;
  147. struct workqueue_struct *iwarp_wq;
  148. u16 iwarp_max_mtu;
  149. unsigned long enet_state;
  150. u8 user_dpm_enabled;
  151. };
  152. #define QEDR_MAX_SQ_PBL (0x8000)
  153. #define QEDR_MAX_SQ_PBL_ENTRIES (0x10000 / sizeof(void *))
  154. #define QEDR_SQE_ELEMENT_SIZE (sizeof(struct rdma_sq_sge))
  155. #define QEDR_MAX_SQE_ELEMENTS_PER_SQE (ROCE_REQ_MAX_SINGLE_SQ_WQE_SIZE / \
  156. QEDR_SQE_ELEMENT_SIZE)
  157. #define QEDR_MAX_SQE_ELEMENTS_PER_PAGE ((RDMA_RING_PAGE_SIZE) / \
  158. QEDR_SQE_ELEMENT_SIZE)
  159. #define QEDR_MAX_SQE ((QEDR_MAX_SQ_PBL_ENTRIES) *\
  160. (RDMA_RING_PAGE_SIZE) / \
  161. (QEDR_SQE_ELEMENT_SIZE) /\
  162. (QEDR_MAX_SQE_ELEMENTS_PER_SQE))
  163. /* RQ */
  164. #define QEDR_MAX_RQ_PBL (0x2000)
  165. #define QEDR_MAX_RQ_PBL_ENTRIES (0x10000 / sizeof(void *))
  166. #define QEDR_RQE_ELEMENT_SIZE (sizeof(struct rdma_rq_sge))
  167. #define QEDR_MAX_RQE_ELEMENTS_PER_RQE (RDMA_MAX_SGE_PER_RQ_WQE)
  168. #define QEDR_MAX_RQE_ELEMENTS_PER_PAGE ((RDMA_RING_PAGE_SIZE) / \
  169. QEDR_RQE_ELEMENT_SIZE)
  170. #define QEDR_MAX_RQE ((QEDR_MAX_RQ_PBL_ENTRIES) *\
  171. (RDMA_RING_PAGE_SIZE) / \
  172. (QEDR_RQE_ELEMENT_SIZE) /\
  173. (QEDR_MAX_RQE_ELEMENTS_PER_RQE))
  174. #define QEDR_CQE_SIZE (sizeof(union rdma_cqe))
  175. #define QEDR_MAX_CQE_PBL_SIZE (512 * 1024)
  176. #define QEDR_MAX_CQE_PBL_ENTRIES (((QEDR_MAX_CQE_PBL_SIZE) / \
  177. sizeof(u64)) - 1)
  178. #define QEDR_MAX_CQES ((u32)((QEDR_MAX_CQE_PBL_ENTRIES) * \
  179. (QED_CHAIN_PAGE_SIZE) / QEDR_CQE_SIZE))
  180. #define QEDR_ROCE_MAX_CNQ_SIZE (0x4000)
  181. #define QEDR_MAX_PORT (1)
  182. #define QEDR_PORT (1)
  183. #define QEDR_UVERBS(CMD_NAME) (1ull << IB_USER_VERBS_CMD_##CMD_NAME)
  184. #define QEDR_ROCE_PKEY_MAX 1
  185. #define QEDR_ROCE_PKEY_TABLE_LEN 1
  186. #define QEDR_ROCE_PKEY_DEFAULT 0xffff
  187. struct qedr_pbl {
  188. struct list_head list_entry;
  189. void *va;
  190. dma_addr_t pa;
  191. };
  192. struct qedr_ucontext {
  193. struct ib_ucontext ibucontext;
  194. struct qedr_dev *dev;
  195. struct qedr_pd *pd;
  196. u64 dpi_addr;
  197. u64 dpi_phys_addr;
  198. u32 dpi_size;
  199. u16 dpi;
  200. struct list_head mm_head;
  201. /* Lock to protect mm list */
  202. struct mutex mm_list_lock;
  203. };
  204. union db_prod64 {
  205. struct rdma_pwm_val32_data data;
  206. u64 raw;
  207. };
  208. enum qedr_cq_type {
  209. QEDR_CQ_TYPE_GSI,
  210. QEDR_CQ_TYPE_KERNEL,
  211. QEDR_CQ_TYPE_USER,
  212. };
  213. struct qedr_pbl_info {
  214. u32 num_pbls;
  215. u32 num_pbes;
  216. u32 pbl_size;
  217. u32 pbe_size;
  218. bool two_layered;
  219. };
  220. struct qedr_userq {
  221. struct ib_umem *umem;
  222. struct qedr_pbl_info pbl_info;
  223. struct qedr_pbl *pbl_tbl;
  224. u64 buf_addr;
  225. size_t buf_len;
  226. };
  227. struct qedr_cq {
  228. struct ib_cq ibcq;
  229. enum qedr_cq_type cq_type;
  230. u32 sig;
  231. u16 icid;
  232. /* Lock to protect multiplem CQ's */
  233. spinlock_t cq_lock;
  234. u8 arm_flags;
  235. struct qed_chain pbl;
  236. void __iomem *db_addr;
  237. union db_prod64 db;
  238. u8 pbl_toggle;
  239. union rdma_cqe *latest_cqe;
  240. union rdma_cqe *toggle_cqe;
  241. u32 cq_cons;
  242. struct qedr_userq q;
  243. u8 destroyed;
  244. u16 cnq_notif;
  245. };
  246. struct qedr_pd {
  247. struct ib_pd ibpd;
  248. u32 pd_id;
  249. struct qedr_ucontext *uctx;
  250. };
  251. struct qedr_mm {
  252. struct {
  253. u64 phy_addr;
  254. unsigned long len;
  255. } key;
  256. struct list_head entry;
  257. };
  258. union db_prod32 {
  259. struct rdma_pwm_val16_data data;
  260. u32 raw;
  261. };
  262. struct qedr_qp_hwq_info {
  263. /* WQE Elements */
  264. struct qed_chain pbl;
  265. u64 p_phys_addr_tbl;
  266. u32 max_sges;
  267. /* WQE */
  268. u16 prod;
  269. u16 cons;
  270. u16 wqe_cons;
  271. u16 gsi_cons;
  272. u16 max_wr;
  273. /* DB */
  274. void __iomem *db;
  275. union db_prod32 db_data;
  276. void __iomem *iwarp_db2;
  277. union db_prod32 iwarp_db2_data;
  278. };
  279. #define QEDR_INC_SW_IDX(p_info, index) \
  280. do { \
  281. p_info->index = (p_info->index + 1) & \
  282. qed_chain_get_capacity(p_info->pbl) \
  283. } while (0)
  284. enum qedr_qp_err_bitmap {
  285. QEDR_QP_ERR_SQ_FULL = 1,
  286. QEDR_QP_ERR_RQ_FULL = 2,
  287. QEDR_QP_ERR_BAD_SR = 4,
  288. QEDR_QP_ERR_BAD_RR = 8,
  289. QEDR_QP_ERR_SQ_PBL_FULL = 16,
  290. QEDR_QP_ERR_RQ_PBL_FULL = 32,
  291. };
  292. struct qedr_qp {
  293. struct ib_qp ibqp; /* must be first */
  294. struct qedr_dev *dev;
  295. struct qedr_iw_ep *ep;
  296. struct qedr_qp_hwq_info sq;
  297. struct qedr_qp_hwq_info rq;
  298. u32 max_inline_data;
  299. /* Lock for QP's */
  300. spinlock_t q_lock;
  301. struct qedr_cq *sq_cq;
  302. struct qedr_cq *rq_cq;
  303. struct qedr_srq *srq;
  304. enum qed_roce_qp_state state;
  305. u32 id;
  306. struct qedr_pd *pd;
  307. enum ib_qp_type qp_type;
  308. struct qed_rdma_qp *qed_qp;
  309. u32 qp_id;
  310. u16 icid;
  311. u16 mtu;
  312. int sgid_idx;
  313. u32 rq_psn;
  314. u32 sq_psn;
  315. u32 qkey;
  316. u32 dest_qp_num;
  317. /* Relevant to qps created from kernel space only (ULPs) */
  318. u8 prev_wqe_size;
  319. u16 wqe_cons;
  320. u32 err_bitmap;
  321. bool signaled;
  322. /* SQ shadow */
  323. struct {
  324. u64 wr_id;
  325. enum ib_wc_opcode opcode;
  326. u32 bytes_len;
  327. u8 wqe_size;
  328. bool signaled;
  329. dma_addr_t icrc_mapping;
  330. u32 *icrc;
  331. struct qedr_mr *mr;
  332. } *wqe_wr_id;
  333. /* RQ shadow */
  334. struct {
  335. u64 wr_id;
  336. struct ib_sge sg_list[RDMA_MAX_SGE_PER_RQ_WQE];
  337. u8 wqe_size;
  338. u8 smac[ETH_ALEN];
  339. u16 vlan;
  340. int rc;
  341. } *rqe_wr_id;
  342. /* Relevant to qps created from user space only (applications) */
  343. struct qedr_userq usq;
  344. struct qedr_userq urq;
  345. atomic_t refcnt;
  346. bool destroyed;
  347. };
  348. struct qedr_ah {
  349. struct ib_ah ibah;
  350. struct rdma_ah_attr attr;
  351. };
  352. enum qedr_mr_type {
  353. QEDR_MR_USER,
  354. QEDR_MR_KERNEL,
  355. QEDR_MR_DMA,
  356. QEDR_MR_FRMR,
  357. };
  358. struct mr_info {
  359. struct qedr_pbl *pbl_table;
  360. struct qedr_pbl_info pbl_info;
  361. struct list_head free_pbl_list;
  362. struct list_head inuse_pbl_list;
  363. u32 completed;
  364. u32 completed_handled;
  365. };
  366. struct qedr_mr {
  367. struct ib_mr ibmr;
  368. struct ib_umem *umem;
  369. struct qed_rdma_register_tid_in_params hw_mr;
  370. enum qedr_mr_type type;
  371. struct qedr_dev *dev;
  372. struct mr_info info;
  373. u64 *pages;
  374. u32 npages;
  375. };
  376. #define SET_FIELD2(value, name, flag) ((value) |= ((flag) << (name ## _SHIFT)))
  377. #define QEDR_RESP_IMM (RDMA_CQE_RESPONDER_IMM_FLG_MASK << \
  378. RDMA_CQE_RESPONDER_IMM_FLG_SHIFT)
  379. #define QEDR_RESP_RDMA (RDMA_CQE_RESPONDER_RDMA_FLG_MASK << \
  380. RDMA_CQE_RESPONDER_RDMA_FLG_SHIFT)
  381. #define QEDR_RESP_INV (RDMA_CQE_RESPONDER_INV_FLG_MASK << \
  382. RDMA_CQE_RESPONDER_INV_FLG_SHIFT)
  383. static inline void qedr_inc_sw_cons(struct qedr_qp_hwq_info *info)
  384. {
  385. info->cons = (info->cons + 1) % info->max_wr;
  386. info->wqe_cons++;
  387. }
  388. static inline void qedr_inc_sw_prod(struct qedr_qp_hwq_info *info)
  389. {
  390. info->prod = (info->prod + 1) % info->max_wr;
  391. }
  392. static inline int qedr_get_dmac(struct qedr_dev *dev,
  393. struct rdma_ah_attr *ah_attr, u8 *mac_addr)
  394. {
  395. union ib_gid zero_sgid = { { 0 } };
  396. struct in6_addr in6;
  397. const struct ib_global_route *grh = rdma_ah_read_grh(ah_attr);
  398. u8 *dmac;
  399. if (!memcmp(&grh->dgid, &zero_sgid, sizeof(union ib_gid))) {
  400. DP_ERR(dev, "Local port GID not supported\n");
  401. eth_zero_addr(mac_addr);
  402. return -EINVAL;
  403. }
  404. memcpy(&in6, grh->dgid.raw, sizeof(in6));
  405. dmac = rdma_ah_retrieve_dmac(ah_attr);
  406. if (!dmac)
  407. return -EINVAL;
  408. ether_addr_copy(mac_addr, dmac);
  409. return 0;
  410. }
  411. struct qedr_iw_listener {
  412. struct qedr_dev *dev;
  413. struct iw_cm_id *cm_id;
  414. int backlog;
  415. void *qed_handle;
  416. };
  417. struct qedr_iw_ep {
  418. struct qedr_dev *dev;
  419. struct iw_cm_id *cm_id;
  420. struct qedr_qp *qp;
  421. void *qed_context;
  422. u8 during_connect;
  423. };
  424. static inline
  425. struct qedr_ucontext *get_qedr_ucontext(struct ib_ucontext *ibucontext)
  426. {
  427. return container_of(ibucontext, struct qedr_ucontext, ibucontext);
  428. }
  429. static inline struct qedr_dev *get_qedr_dev(struct ib_device *ibdev)
  430. {
  431. return container_of(ibdev, struct qedr_dev, ibdev);
  432. }
  433. static inline struct qedr_pd *get_qedr_pd(struct ib_pd *ibpd)
  434. {
  435. return container_of(ibpd, struct qedr_pd, ibpd);
  436. }
  437. static inline struct qedr_cq *get_qedr_cq(struct ib_cq *ibcq)
  438. {
  439. return container_of(ibcq, struct qedr_cq, ibcq);
  440. }
  441. static inline struct qedr_qp *get_qedr_qp(struct ib_qp *ibqp)
  442. {
  443. return container_of(ibqp, struct qedr_qp, ibqp);
  444. }
  445. static inline struct qedr_ah *get_qedr_ah(struct ib_ah *ibah)
  446. {
  447. return container_of(ibah, struct qedr_ah, ibah);
  448. }
  449. static inline struct qedr_mr *get_qedr_mr(struct ib_mr *ibmr)
  450. {
  451. return container_of(ibmr, struct qedr_mr, ibmr);
  452. }
  453. #endif