mlx5_ib.h 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029
  1. /*
  2. * Copyright (c) 2013-2015, Mellanox Technologies. All rights reserved.
  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 MLX5_IB_H
  33. #define MLX5_IB_H
  34. #include <linux/kernel.h>
  35. #include <linux/sched.h>
  36. #include <rdma/ib_verbs.h>
  37. #include <rdma/ib_smi.h>
  38. #include <linux/mlx5/driver.h>
  39. #include <linux/mlx5/cq.h>
  40. #include <linux/mlx5/qp.h>
  41. #include <linux/mlx5/srq.h>
  42. #include <linux/types.h>
  43. #include <linux/mlx5/transobj.h>
  44. #include <rdma/ib_user_verbs.h>
  45. #include <rdma/mlx5-abi.h>
  46. #define mlx5_ib_dbg(dev, format, arg...) \
  47. pr_debug("%s:%s:%d:(pid %d): " format, (dev)->ib_dev.name, __func__, \
  48. __LINE__, current->pid, ##arg)
  49. #define mlx5_ib_err(dev, format, arg...) \
  50. pr_err("%s:%s:%d:(pid %d): " format, (dev)->ib_dev.name, __func__, \
  51. __LINE__, current->pid, ##arg)
  52. #define mlx5_ib_warn(dev, format, arg...) \
  53. pr_warn("%s:%s:%d:(pid %d): " format, (dev)->ib_dev.name, __func__, \
  54. __LINE__, current->pid, ##arg)
  55. #define field_avail(type, fld, sz) (offsetof(type, fld) + \
  56. sizeof(((type *)0)->fld) <= (sz))
  57. #define MLX5_IB_DEFAULT_UIDX 0xffffff
  58. #define MLX5_USER_ASSIGNED_UIDX_MASK __mlx5_mask(qpc, user_index)
  59. #define MLX5_MKEY_PAGE_SHIFT_MASK __mlx5_mask(mkc, log_page_size)
  60. enum {
  61. MLX5_IB_MMAP_CMD_SHIFT = 8,
  62. MLX5_IB_MMAP_CMD_MASK = 0xff,
  63. };
  64. enum mlx5_ib_mmap_cmd {
  65. MLX5_IB_MMAP_REGULAR_PAGE = 0,
  66. MLX5_IB_MMAP_GET_CONTIGUOUS_PAGES = 1,
  67. MLX5_IB_MMAP_WC_PAGE = 2,
  68. MLX5_IB_MMAP_NC_PAGE = 3,
  69. /* 5 is chosen in order to be compatible with old versions of libmlx5 */
  70. MLX5_IB_MMAP_CORE_CLOCK = 5,
  71. };
  72. enum {
  73. MLX5_RES_SCAT_DATA32_CQE = 0x1,
  74. MLX5_RES_SCAT_DATA64_CQE = 0x2,
  75. MLX5_REQ_SCAT_DATA32_CQE = 0x11,
  76. MLX5_REQ_SCAT_DATA64_CQE = 0x22,
  77. };
  78. enum mlx5_ib_latency_class {
  79. MLX5_IB_LATENCY_CLASS_LOW,
  80. MLX5_IB_LATENCY_CLASS_MEDIUM,
  81. MLX5_IB_LATENCY_CLASS_HIGH,
  82. };
  83. enum mlx5_ib_mad_ifc_flags {
  84. MLX5_MAD_IFC_IGNORE_MKEY = 1,
  85. MLX5_MAD_IFC_IGNORE_BKEY = 2,
  86. MLX5_MAD_IFC_NET_VIEW = 4,
  87. };
  88. enum {
  89. MLX5_CROSS_CHANNEL_BFREG = 0,
  90. };
  91. enum {
  92. MLX5_CQE_VERSION_V0,
  93. MLX5_CQE_VERSION_V1,
  94. };
  95. struct mlx5_ib_vma_private_data {
  96. struct list_head list;
  97. struct vm_area_struct *vma;
  98. };
  99. struct mlx5_ib_ucontext {
  100. struct ib_ucontext ibucontext;
  101. struct list_head db_page_list;
  102. /* protect doorbell record alloc/free
  103. */
  104. struct mutex db_page_mutex;
  105. struct mlx5_bfreg_info bfregi;
  106. u8 cqe_version;
  107. /* Transport Domain number */
  108. u32 tdn;
  109. struct list_head vma_private_list;
  110. unsigned long upd_xlt_page;
  111. /* protect ODP/KSM */
  112. struct mutex upd_xlt_page_mutex;
  113. u64 lib_caps;
  114. };
  115. static inline struct mlx5_ib_ucontext *to_mucontext(struct ib_ucontext *ibucontext)
  116. {
  117. return container_of(ibucontext, struct mlx5_ib_ucontext, ibucontext);
  118. }
  119. struct mlx5_ib_pd {
  120. struct ib_pd ibpd;
  121. u32 pdn;
  122. };
  123. #define MLX5_IB_FLOW_MCAST_PRIO (MLX5_BY_PASS_NUM_PRIOS - 1)
  124. #define MLX5_IB_FLOW_LAST_PRIO (MLX5_BY_PASS_NUM_REGULAR_PRIOS - 1)
  125. #if (MLX5_IB_FLOW_LAST_PRIO <= 0)
  126. #error "Invalid number of bypass priorities"
  127. #endif
  128. #define MLX5_IB_FLOW_LEFTOVERS_PRIO (MLX5_IB_FLOW_MCAST_PRIO + 1)
  129. #define MLX5_IB_NUM_FLOW_FT (MLX5_IB_FLOW_LEFTOVERS_PRIO + 1)
  130. #define MLX5_IB_NUM_SNIFFER_FTS 2
  131. struct mlx5_ib_flow_prio {
  132. struct mlx5_flow_table *flow_table;
  133. unsigned int refcount;
  134. };
  135. struct mlx5_ib_flow_handler {
  136. struct list_head list;
  137. struct ib_flow ibflow;
  138. struct mlx5_ib_flow_prio *prio;
  139. struct mlx5_flow_handle *rule;
  140. };
  141. struct mlx5_ib_flow_db {
  142. struct mlx5_ib_flow_prio prios[MLX5_IB_NUM_FLOW_FT];
  143. struct mlx5_ib_flow_prio sniffer[MLX5_IB_NUM_SNIFFER_FTS];
  144. struct mlx5_flow_table *lag_demux_ft;
  145. /* Protect flow steering bypass flow tables
  146. * when add/del flow rules.
  147. * only single add/removal of flow steering rule could be done
  148. * simultaneously.
  149. */
  150. struct mutex lock;
  151. };
  152. /* Use macros here so that don't have to duplicate
  153. * enum ib_send_flags and enum ib_qp_type for low-level driver
  154. */
  155. #define MLX5_IB_SEND_UMR_ENABLE_MR (IB_SEND_RESERVED_START << 0)
  156. #define MLX5_IB_SEND_UMR_DISABLE_MR (IB_SEND_RESERVED_START << 1)
  157. #define MLX5_IB_SEND_UMR_FAIL_IF_FREE (IB_SEND_RESERVED_START << 2)
  158. #define MLX5_IB_SEND_UMR_UPDATE_XLT (IB_SEND_RESERVED_START << 3)
  159. #define MLX5_IB_SEND_UMR_UPDATE_TRANSLATION (IB_SEND_RESERVED_START << 4)
  160. #define MLX5_IB_SEND_UMR_UPDATE_PD_ACCESS IB_SEND_RESERVED_END
  161. #define MLX5_IB_QPT_REG_UMR IB_QPT_RESERVED1
  162. /*
  163. * IB_QPT_GSI creates the software wrapper around GSI, and MLX5_IB_QPT_HW_GSI
  164. * creates the actual hardware QP.
  165. */
  166. #define MLX5_IB_QPT_HW_GSI IB_QPT_RESERVED2
  167. #define MLX5_IB_WR_UMR IB_WR_RESERVED1
  168. #define MLX5_IB_UMR_OCTOWORD 16
  169. #define MLX5_IB_UMR_XLT_ALIGNMENT 64
  170. #define MLX5_IB_UPD_XLT_ZAP BIT(0)
  171. #define MLX5_IB_UPD_XLT_ENABLE BIT(1)
  172. #define MLX5_IB_UPD_XLT_ATOMIC BIT(2)
  173. #define MLX5_IB_UPD_XLT_ADDR BIT(3)
  174. #define MLX5_IB_UPD_XLT_PD BIT(4)
  175. #define MLX5_IB_UPD_XLT_ACCESS BIT(5)
  176. #define MLX5_IB_UPD_XLT_INDIRECT BIT(6)
  177. /* Private QP creation flags to be passed in ib_qp_init_attr.create_flags.
  178. *
  179. * These flags are intended for internal use by the mlx5_ib driver, and they
  180. * rely on the range reserved for that use in the ib_qp_create_flags enum.
  181. */
  182. /* Create a UD QP whose source QP number is 1 */
  183. static inline enum ib_qp_create_flags mlx5_ib_create_qp_sqpn_qp1(void)
  184. {
  185. return IB_QP_CREATE_RESERVED_START;
  186. }
  187. struct wr_list {
  188. u16 opcode;
  189. u16 next;
  190. };
  191. enum mlx5_ib_rq_flags {
  192. MLX5_IB_RQ_CVLAN_STRIPPING = 1 << 0,
  193. };
  194. struct mlx5_ib_wq {
  195. u64 *wrid;
  196. u32 *wr_data;
  197. struct wr_list *w_list;
  198. unsigned *wqe_head;
  199. u16 unsig_count;
  200. /* serialize post to the work queue
  201. */
  202. spinlock_t lock;
  203. int wqe_cnt;
  204. int max_post;
  205. int max_gs;
  206. int offset;
  207. int wqe_shift;
  208. unsigned head;
  209. unsigned tail;
  210. u16 cur_post;
  211. u16 last_poll;
  212. void *qend;
  213. };
  214. struct mlx5_ib_rwq {
  215. struct ib_wq ibwq;
  216. struct mlx5_core_qp core_qp;
  217. u32 rq_num_pas;
  218. u32 log_rq_stride;
  219. u32 log_rq_size;
  220. u32 rq_page_offset;
  221. u32 log_page_size;
  222. struct ib_umem *umem;
  223. size_t buf_size;
  224. unsigned int page_shift;
  225. int create_type;
  226. struct mlx5_db db;
  227. u32 user_index;
  228. u32 wqe_count;
  229. u32 wqe_shift;
  230. int wq_sig;
  231. };
  232. enum {
  233. MLX5_QP_USER,
  234. MLX5_QP_KERNEL,
  235. MLX5_QP_EMPTY
  236. };
  237. enum {
  238. MLX5_WQ_USER,
  239. MLX5_WQ_KERNEL
  240. };
  241. struct mlx5_ib_rwq_ind_table {
  242. struct ib_rwq_ind_table ib_rwq_ind_tbl;
  243. u32 rqtn;
  244. };
  245. struct mlx5_ib_ubuffer {
  246. struct ib_umem *umem;
  247. int buf_size;
  248. u64 buf_addr;
  249. };
  250. struct mlx5_ib_qp_base {
  251. struct mlx5_ib_qp *container_mibqp;
  252. struct mlx5_core_qp mqp;
  253. struct mlx5_ib_ubuffer ubuffer;
  254. };
  255. struct mlx5_ib_qp_trans {
  256. struct mlx5_ib_qp_base base;
  257. u16 xrcdn;
  258. u8 alt_port;
  259. u8 atomic_rd_en;
  260. u8 resp_depth;
  261. };
  262. struct mlx5_ib_rss_qp {
  263. u32 tirn;
  264. };
  265. struct mlx5_ib_rq {
  266. struct mlx5_ib_qp_base base;
  267. struct mlx5_ib_wq *rq;
  268. struct mlx5_ib_ubuffer ubuffer;
  269. struct mlx5_db *doorbell;
  270. u32 tirn;
  271. u8 state;
  272. u32 flags;
  273. };
  274. struct mlx5_ib_sq {
  275. struct mlx5_ib_qp_base base;
  276. struct mlx5_ib_wq *sq;
  277. struct mlx5_ib_ubuffer ubuffer;
  278. struct mlx5_db *doorbell;
  279. u32 tisn;
  280. u8 state;
  281. };
  282. struct mlx5_ib_raw_packet_qp {
  283. struct mlx5_ib_sq sq;
  284. struct mlx5_ib_rq rq;
  285. };
  286. struct mlx5_bf {
  287. int buf_size;
  288. unsigned long offset;
  289. struct mlx5_sq_bfreg *bfreg;
  290. };
  291. struct mlx5_ib_qp {
  292. struct ib_qp ibqp;
  293. union {
  294. struct mlx5_ib_qp_trans trans_qp;
  295. struct mlx5_ib_raw_packet_qp raw_packet_qp;
  296. struct mlx5_ib_rss_qp rss_qp;
  297. };
  298. struct mlx5_buf buf;
  299. struct mlx5_db db;
  300. struct mlx5_ib_wq rq;
  301. u8 sq_signal_bits;
  302. u8 fm_cache;
  303. struct mlx5_ib_wq sq;
  304. /* serialize qp state modifications
  305. */
  306. struct mutex mutex;
  307. u32 flags;
  308. u8 port;
  309. u8 state;
  310. int wq_sig;
  311. int scat_cqe;
  312. int max_inline_data;
  313. struct mlx5_bf bf;
  314. int has_rq;
  315. /* only for user space QPs. For kernel
  316. * we have it from the bf object
  317. */
  318. int bfregn;
  319. int create_type;
  320. /* Store signature errors */
  321. bool signature_en;
  322. struct list_head qps_list;
  323. struct list_head cq_recv_list;
  324. struct list_head cq_send_list;
  325. u32 rate_limit;
  326. };
  327. struct mlx5_ib_cq_buf {
  328. struct mlx5_buf buf;
  329. struct ib_umem *umem;
  330. int cqe_size;
  331. int nent;
  332. };
  333. enum mlx5_ib_qp_flags {
  334. MLX5_IB_QP_LSO = IB_QP_CREATE_IPOIB_UD_LSO,
  335. MLX5_IB_QP_BLOCK_MULTICAST_LOOPBACK = IB_QP_CREATE_BLOCK_MULTICAST_LOOPBACK,
  336. MLX5_IB_QP_CROSS_CHANNEL = IB_QP_CREATE_CROSS_CHANNEL,
  337. MLX5_IB_QP_MANAGED_SEND = IB_QP_CREATE_MANAGED_SEND,
  338. MLX5_IB_QP_MANAGED_RECV = IB_QP_CREATE_MANAGED_RECV,
  339. MLX5_IB_QP_SIGNATURE_HANDLING = 1 << 5,
  340. /* QP uses 1 as its source QP number */
  341. MLX5_IB_QP_SQPN_QP1 = 1 << 6,
  342. MLX5_IB_QP_CAP_SCATTER_FCS = 1 << 7,
  343. MLX5_IB_QP_RSS = 1 << 8,
  344. MLX5_IB_QP_CVLAN_STRIPPING = 1 << 9,
  345. };
  346. struct mlx5_umr_wr {
  347. struct ib_send_wr wr;
  348. u64 virt_addr;
  349. u64 offset;
  350. struct ib_pd *pd;
  351. unsigned int page_shift;
  352. unsigned int xlt_size;
  353. u64 length;
  354. int access_flags;
  355. u32 mkey;
  356. };
  357. static inline struct mlx5_umr_wr *umr_wr(struct ib_send_wr *wr)
  358. {
  359. return container_of(wr, struct mlx5_umr_wr, wr);
  360. }
  361. struct mlx5_shared_mr_info {
  362. int mr_id;
  363. struct ib_umem *umem;
  364. };
  365. struct mlx5_ib_cq {
  366. struct ib_cq ibcq;
  367. struct mlx5_core_cq mcq;
  368. struct mlx5_ib_cq_buf buf;
  369. struct mlx5_db db;
  370. /* serialize access to the CQ
  371. */
  372. spinlock_t lock;
  373. /* protect resize cq
  374. */
  375. struct mutex resize_mutex;
  376. struct mlx5_ib_cq_buf *resize_buf;
  377. struct ib_umem *resize_umem;
  378. int cqe_size;
  379. struct list_head list_send_qp;
  380. struct list_head list_recv_qp;
  381. u32 create_flags;
  382. struct list_head wc_list;
  383. enum ib_cq_notify_flags notify_flags;
  384. struct work_struct notify_work;
  385. };
  386. struct mlx5_ib_wc {
  387. struct ib_wc wc;
  388. struct list_head list;
  389. };
  390. struct mlx5_ib_srq {
  391. struct ib_srq ibsrq;
  392. struct mlx5_core_srq msrq;
  393. struct mlx5_buf buf;
  394. struct mlx5_db db;
  395. u64 *wrid;
  396. /* protect SRQ hanlding
  397. */
  398. spinlock_t lock;
  399. int head;
  400. int tail;
  401. u16 wqe_ctr;
  402. struct ib_umem *umem;
  403. /* serialize arming a SRQ
  404. */
  405. struct mutex mutex;
  406. int wq_sig;
  407. };
  408. struct mlx5_ib_xrcd {
  409. struct ib_xrcd ibxrcd;
  410. u32 xrcdn;
  411. };
  412. enum mlx5_ib_mtt_access_flags {
  413. MLX5_IB_MTT_READ = (1 << 0),
  414. MLX5_IB_MTT_WRITE = (1 << 1),
  415. };
  416. #define MLX5_IB_MTT_PRESENT (MLX5_IB_MTT_READ | MLX5_IB_MTT_WRITE)
  417. struct mlx5_ib_mr {
  418. struct ib_mr ibmr;
  419. void *descs;
  420. dma_addr_t desc_map;
  421. int ndescs;
  422. int max_descs;
  423. int desc_size;
  424. int access_mode;
  425. struct mlx5_core_mkey mmkey;
  426. struct ib_umem *umem;
  427. struct mlx5_shared_mr_info *smr_info;
  428. struct list_head list;
  429. int order;
  430. int umred;
  431. int npages;
  432. struct mlx5_ib_dev *dev;
  433. u32 out[MLX5_ST_SZ_DW(create_mkey_out)];
  434. struct mlx5_core_sig_ctx *sig;
  435. int live;
  436. void *descs_alloc;
  437. int access_flags; /* Needed for rereg MR */
  438. struct mlx5_ib_mr *parent;
  439. atomic_t num_leaf_free;
  440. wait_queue_head_t q_leaf_free;
  441. };
  442. struct mlx5_ib_mw {
  443. struct ib_mw ibmw;
  444. struct mlx5_core_mkey mmkey;
  445. };
  446. struct mlx5_ib_umr_context {
  447. struct ib_cqe cqe;
  448. enum ib_wc_status status;
  449. struct completion done;
  450. };
  451. struct umr_common {
  452. struct ib_pd *pd;
  453. struct ib_cq *cq;
  454. struct ib_qp *qp;
  455. /* control access to UMR QP
  456. */
  457. struct semaphore sem;
  458. };
  459. enum {
  460. MLX5_FMR_INVALID,
  461. MLX5_FMR_VALID,
  462. MLX5_FMR_BUSY,
  463. };
  464. struct mlx5_cache_ent {
  465. struct list_head head;
  466. /* sync access to the cahce entry
  467. */
  468. spinlock_t lock;
  469. struct dentry *dir;
  470. char name[4];
  471. u32 order;
  472. u32 xlt;
  473. u32 access_mode;
  474. u32 page;
  475. u32 size;
  476. u32 cur;
  477. u32 miss;
  478. u32 limit;
  479. struct dentry *fsize;
  480. struct dentry *fcur;
  481. struct dentry *fmiss;
  482. struct dentry *flimit;
  483. struct mlx5_ib_dev *dev;
  484. struct work_struct work;
  485. struct delayed_work dwork;
  486. int pending;
  487. struct completion compl;
  488. };
  489. struct mlx5_mr_cache {
  490. struct workqueue_struct *wq;
  491. struct mlx5_cache_ent ent[MAX_MR_CACHE_ENTRIES];
  492. int stopped;
  493. struct dentry *root;
  494. unsigned long last_add;
  495. };
  496. struct mlx5_ib_gsi_qp;
  497. struct mlx5_ib_port_resources {
  498. struct mlx5_ib_resources *devr;
  499. struct mlx5_ib_gsi_qp *gsi;
  500. struct work_struct pkey_change_work;
  501. };
  502. struct mlx5_ib_resources {
  503. struct ib_cq *c0;
  504. struct ib_xrcd *x0;
  505. struct ib_xrcd *x1;
  506. struct ib_pd *p0;
  507. struct ib_srq *s0;
  508. struct ib_srq *s1;
  509. struct mlx5_ib_port_resources ports[2];
  510. /* Protects changes to the port resources */
  511. struct mutex mutex;
  512. };
  513. struct mlx5_ib_q_counters {
  514. const char **names;
  515. size_t *offsets;
  516. u32 num_counters;
  517. u16 set_id;
  518. };
  519. struct mlx5_ib_port {
  520. struct mlx5_ib_q_counters q_cnts;
  521. };
  522. struct mlx5_roce {
  523. /* Protect mlx5_ib_get_netdev from invoking dev_hold() with a NULL
  524. * netdev pointer
  525. */
  526. rwlock_t netdev_lock;
  527. struct net_device *netdev;
  528. struct notifier_block nb;
  529. atomic_t next_port;
  530. };
  531. struct mlx5_ib_dev {
  532. struct ib_device ib_dev;
  533. struct mlx5_core_dev *mdev;
  534. struct mlx5_roce roce;
  535. int num_ports;
  536. /* serialize update of capability mask
  537. */
  538. struct mutex cap_mask_mutex;
  539. bool ib_active;
  540. struct umr_common umrc;
  541. /* sync used page count stats
  542. */
  543. struct mlx5_ib_resources devr;
  544. struct mlx5_mr_cache cache;
  545. struct timer_list delay_timer;
  546. /* Prevents soft lock on massive reg MRs */
  547. struct mutex slow_path_mutex;
  548. int fill_delay;
  549. #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
  550. struct ib_odp_caps odp_caps;
  551. u64 odp_max_size;
  552. /*
  553. * Sleepable RCU that prevents destruction of MRs while they are still
  554. * being used by a page fault handler.
  555. */
  556. struct srcu_struct mr_srcu;
  557. u32 null_mkey;
  558. #endif
  559. struct mlx5_ib_flow_db flow_db;
  560. /* protect resources needed as part of reset flow */
  561. spinlock_t reset_flow_resource_lock;
  562. struct list_head qp_list;
  563. /* Array with num_ports elements */
  564. struct mlx5_ib_port *port;
  565. struct mlx5_sq_bfreg bfreg;
  566. struct mlx5_sq_bfreg fp_bfreg;
  567. };
  568. static inline struct mlx5_ib_cq *to_mibcq(struct mlx5_core_cq *mcq)
  569. {
  570. return container_of(mcq, struct mlx5_ib_cq, mcq);
  571. }
  572. static inline struct mlx5_ib_xrcd *to_mxrcd(struct ib_xrcd *ibxrcd)
  573. {
  574. return container_of(ibxrcd, struct mlx5_ib_xrcd, ibxrcd);
  575. }
  576. static inline struct mlx5_ib_dev *to_mdev(struct ib_device *ibdev)
  577. {
  578. return container_of(ibdev, struct mlx5_ib_dev, ib_dev);
  579. }
  580. static inline struct mlx5_ib_cq *to_mcq(struct ib_cq *ibcq)
  581. {
  582. return container_of(ibcq, struct mlx5_ib_cq, ibcq);
  583. }
  584. static inline struct mlx5_ib_qp *to_mibqp(struct mlx5_core_qp *mqp)
  585. {
  586. return container_of(mqp, struct mlx5_ib_qp_base, mqp)->container_mibqp;
  587. }
  588. static inline struct mlx5_ib_rwq *to_mibrwq(struct mlx5_core_qp *core_qp)
  589. {
  590. return container_of(core_qp, struct mlx5_ib_rwq, core_qp);
  591. }
  592. static inline struct mlx5_ib_mr *to_mibmr(struct mlx5_core_mkey *mmkey)
  593. {
  594. return container_of(mmkey, struct mlx5_ib_mr, mmkey);
  595. }
  596. static inline struct mlx5_ib_pd *to_mpd(struct ib_pd *ibpd)
  597. {
  598. return container_of(ibpd, struct mlx5_ib_pd, ibpd);
  599. }
  600. static inline struct mlx5_ib_srq *to_msrq(struct ib_srq *ibsrq)
  601. {
  602. return container_of(ibsrq, struct mlx5_ib_srq, ibsrq);
  603. }
  604. static inline struct mlx5_ib_qp *to_mqp(struct ib_qp *ibqp)
  605. {
  606. return container_of(ibqp, struct mlx5_ib_qp, ibqp);
  607. }
  608. static inline struct mlx5_ib_rwq *to_mrwq(struct ib_wq *ibwq)
  609. {
  610. return container_of(ibwq, struct mlx5_ib_rwq, ibwq);
  611. }
  612. static inline struct mlx5_ib_rwq_ind_table *to_mrwq_ind_table(struct ib_rwq_ind_table *ib_rwq_ind_tbl)
  613. {
  614. return container_of(ib_rwq_ind_tbl, struct mlx5_ib_rwq_ind_table, ib_rwq_ind_tbl);
  615. }
  616. static inline struct mlx5_ib_srq *to_mibsrq(struct mlx5_core_srq *msrq)
  617. {
  618. return container_of(msrq, struct mlx5_ib_srq, msrq);
  619. }
  620. static inline struct mlx5_ib_mr *to_mmr(struct ib_mr *ibmr)
  621. {
  622. return container_of(ibmr, struct mlx5_ib_mr, ibmr);
  623. }
  624. static inline struct mlx5_ib_mw *to_mmw(struct ib_mw *ibmw)
  625. {
  626. return container_of(ibmw, struct mlx5_ib_mw, ibmw);
  627. }
  628. struct mlx5_ib_ah {
  629. struct ib_ah ibah;
  630. struct mlx5_av av;
  631. };
  632. static inline struct mlx5_ib_ah *to_mah(struct ib_ah *ibah)
  633. {
  634. return container_of(ibah, struct mlx5_ib_ah, ibah);
  635. }
  636. int mlx5_ib_db_map_user(struct mlx5_ib_ucontext *context, unsigned long virt,
  637. struct mlx5_db *db);
  638. void mlx5_ib_db_unmap_user(struct mlx5_ib_ucontext *context, struct mlx5_db *db);
  639. void __mlx5_ib_cq_clean(struct mlx5_ib_cq *cq, u32 qpn, struct mlx5_ib_srq *srq);
  640. void mlx5_ib_cq_clean(struct mlx5_ib_cq *cq, u32 qpn, struct mlx5_ib_srq *srq);
  641. void mlx5_ib_free_srq_wqe(struct mlx5_ib_srq *srq, int wqe_index);
  642. int mlx5_MAD_IFC(struct mlx5_ib_dev *dev, int ignore_mkey, int ignore_bkey,
  643. u8 port, const struct ib_wc *in_wc, const struct ib_grh *in_grh,
  644. const void *in_mad, void *response_mad);
  645. struct ib_ah *mlx5_ib_create_ah(struct ib_pd *pd, struct ib_ah_attr *ah_attr,
  646. struct ib_udata *udata);
  647. int mlx5_ib_query_ah(struct ib_ah *ibah, struct ib_ah_attr *ah_attr);
  648. int mlx5_ib_destroy_ah(struct ib_ah *ah);
  649. struct ib_srq *mlx5_ib_create_srq(struct ib_pd *pd,
  650. struct ib_srq_init_attr *init_attr,
  651. struct ib_udata *udata);
  652. int mlx5_ib_modify_srq(struct ib_srq *ibsrq, struct ib_srq_attr *attr,
  653. enum ib_srq_attr_mask attr_mask, struct ib_udata *udata);
  654. int mlx5_ib_query_srq(struct ib_srq *ibsrq, struct ib_srq_attr *srq_attr);
  655. int mlx5_ib_destroy_srq(struct ib_srq *srq);
  656. int mlx5_ib_post_srq_recv(struct ib_srq *ibsrq, struct ib_recv_wr *wr,
  657. struct ib_recv_wr **bad_wr);
  658. struct ib_qp *mlx5_ib_create_qp(struct ib_pd *pd,
  659. struct ib_qp_init_attr *init_attr,
  660. struct ib_udata *udata);
  661. int mlx5_ib_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
  662. int attr_mask, struct ib_udata *udata);
  663. int mlx5_ib_query_qp(struct ib_qp *ibqp, struct ib_qp_attr *qp_attr, int qp_attr_mask,
  664. struct ib_qp_init_attr *qp_init_attr);
  665. int mlx5_ib_destroy_qp(struct ib_qp *qp);
  666. int mlx5_ib_post_send(struct ib_qp *ibqp, struct ib_send_wr *wr,
  667. struct ib_send_wr **bad_wr);
  668. int mlx5_ib_post_recv(struct ib_qp *ibqp, struct ib_recv_wr *wr,
  669. struct ib_recv_wr **bad_wr);
  670. void *mlx5_get_send_wqe(struct mlx5_ib_qp *qp, int n);
  671. int mlx5_ib_read_user_wqe(struct mlx5_ib_qp *qp, int send, int wqe_index,
  672. void *buffer, u32 length,
  673. struct mlx5_ib_qp_base *base);
  674. struct ib_cq *mlx5_ib_create_cq(struct ib_device *ibdev,
  675. const struct ib_cq_init_attr *attr,
  676. struct ib_ucontext *context,
  677. struct ib_udata *udata);
  678. int mlx5_ib_destroy_cq(struct ib_cq *cq);
  679. int mlx5_ib_poll_cq(struct ib_cq *ibcq, int num_entries, struct ib_wc *wc);
  680. int mlx5_ib_arm_cq(struct ib_cq *ibcq, enum ib_cq_notify_flags flags);
  681. int mlx5_ib_modify_cq(struct ib_cq *cq, u16 cq_count, u16 cq_period);
  682. int mlx5_ib_resize_cq(struct ib_cq *ibcq, int entries, struct ib_udata *udata);
  683. struct ib_mr *mlx5_ib_get_dma_mr(struct ib_pd *pd, int acc);
  684. struct ib_mr *mlx5_ib_reg_user_mr(struct ib_pd *pd, u64 start, u64 length,
  685. u64 virt_addr, int access_flags,
  686. struct ib_udata *udata);
  687. struct ib_mw *mlx5_ib_alloc_mw(struct ib_pd *pd, enum ib_mw_type type,
  688. struct ib_udata *udata);
  689. int mlx5_ib_dealloc_mw(struct ib_mw *mw);
  690. int mlx5_ib_update_xlt(struct mlx5_ib_mr *mr, u64 idx, int npages,
  691. int page_shift, int flags);
  692. struct mlx5_ib_mr *mlx5_ib_alloc_implicit_mr(struct mlx5_ib_pd *pd,
  693. int access_flags);
  694. void mlx5_ib_free_implicit_mr(struct mlx5_ib_mr *mr);
  695. int mlx5_ib_rereg_user_mr(struct ib_mr *ib_mr, int flags, u64 start,
  696. u64 length, u64 virt_addr, int access_flags,
  697. struct ib_pd *pd, struct ib_udata *udata);
  698. int mlx5_ib_dereg_mr(struct ib_mr *ibmr);
  699. struct ib_mr *mlx5_ib_alloc_mr(struct ib_pd *pd,
  700. enum ib_mr_type mr_type,
  701. u32 max_num_sg);
  702. int mlx5_ib_map_mr_sg(struct ib_mr *ibmr, struct scatterlist *sg, int sg_nents,
  703. unsigned int *sg_offset);
  704. int mlx5_ib_process_mad(struct ib_device *ibdev, int mad_flags, u8 port_num,
  705. const struct ib_wc *in_wc, const struct ib_grh *in_grh,
  706. const struct ib_mad_hdr *in, size_t in_mad_size,
  707. struct ib_mad_hdr *out, size_t *out_mad_size,
  708. u16 *out_mad_pkey_index);
  709. struct ib_xrcd *mlx5_ib_alloc_xrcd(struct ib_device *ibdev,
  710. struct ib_ucontext *context,
  711. struct ib_udata *udata);
  712. int mlx5_ib_dealloc_xrcd(struct ib_xrcd *xrcd);
  713. int mlx5_ib_get_buf_offset(u64 addr, int page_shift, u32 *offset);
  714. int mlx5_query_ext_port_caps(struct mlx5_ib_dev *dev, u8 port);
  715. int mlx5_query_mad_ifc_smp_attr_node_info(struct ib_device *ibdev,
  716. struct ib_smp *out_mad);
  717. int mlx5_query_mad_ifc_system_image_guid(struct ib_device *ibdev,
  718. __be64 *sys_image_guid);
  719. int mlx5_query_mad_ifc_max_pkeys(struct ib_device *ibdev,
  720. u16 *max_pkeys);
  721. int mlx5_query_mad_ifc_vendor_id(struct ib_device *ibdev,
  722. u32 *vendor_id);
  723. int mlx5_query_mad_ifc_node_desc(struct mlx5_ib_dev *dev, char *node_desc);
  724. int mlx5_query_mad_ifc_node_guid(struct mlx5_ib_dev *dev, __be64 *node_guid);
  725. int mlx5_query_mad_ifc_pkey(struct ib_device *ibdev, u8 port, u16 index,
  726. u16 *pkey);
  727. int mlx5_query_mad_ifc_gids(struct ib_device *ibdev, u8 port, int index,
  728. union ib_gid *gid);
  729. int mlx5_query_mad_ifc_port(struct ib_device *ibdev, u8 port,
  730. struct ib_port_attr *props);
  731. int mlx5_ib_query_port(struct ib_device *ibdev, u8 port,
  732. struct ib_port_attr *props);
  733. int mlx5_ib_init_fmr(struct mlx5_ib_dev *dev);
  734. void mlx5_ib_cleanup_fmr(struct mlx5_ib_dev *dev);
  735. void mlx5_ib_cont_pages(struct ib_umem *umem, u64 addr,
  736. unsigned long max_page_shift,
  737. int *count, int *shift,
  738. int *ncont, int *order);
  739. void __mlx5_ib_populate_pas(struct mlx5_ib_dev *dev, struct ib_umem *umem,
  740. int page_shift, size_t offset, size_t num_pages,
  741. __be64 *pas, int access_flags);
  742. void mlx5_ib_populate_pas(struct mlx5_ib_dev *dev, struct ib_umem *umem,
  743. int page_shift, __be64 *pas, int access_flags);
  744. void mlx5_ib_copy_pas(u64 *old, u64 *new, int step, int num);
  745. int mlx5_ib_get_cqe_size(struct mlx5_ib_dev *dev, struct ib_cq *ibcq);
  746. int mlx5_mr_cache_init(struct mlx5_ib_dev *dev);
  747. int mlx5_mr_cache_cleanup(struct mlx5_ib_dev *dev);
  748. struct mlx5_ib_mr *mlx5_mr_cache_alloc(struct mlx5_ib_dev *dev, int entry);
  749. void mlx5_mr_cache_free(struct mlx5_ib_dev *dev, struct mlx5_ib_mr *mr);
  750. int mlx5_ib_check_mr_status(struct ib_mr *ibmr, u32 check_mask,
  751. struct ib_mr_status *mr_status);
  752. struct ib_wq *mlx5_ib_create_wq(struct ib_pd *pd,
  753. struct ib_wq_init_attr *init_attr,
  754. struct ib_udata *udata);
  755. int mlx5_ib_destroy_wq(struct ib_wq *wq);
  756. int mlx5_ib_modify_wq(struct ib_wq *wq, struct ib_wq_attr *wq_attr,
  757. u32 wq_attr_mask, struct ib_udata *udata);
  758. struct ib_rwq_ind_table *mlx5_ib_create_rwq_ind_table(struct ib_device *device,
  759. struct ib_rwq_ind_table_init_attr *init_attr,
  760. struct ib_udata *udata);
  761. int mlx5_ib_destroy_rwq_ind_table(struct ib_rwq_ind_table *wq_ind_table);
  762. #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
  763. void mlx5_ib_internal_fill_odp_caps(struct mlx5_ib_dev *dev);
  764. void mlx5_ib_pfault(struct mlx5_core_dev *mdev, void *context,
  765. struct mlx5_pagefault *pfault);
  766. int mlx5_ib_odp_init_one(struct mlx5_ib_dev *ibdev);
  767. void mlx5_ib_odp_remove_one(struct mlx5_ib_dev *ibdev);
  768. int __init mlx5_ib_odp_init(void);
  769. void mlx5_ib_odp_cleanup(void);
  770. void mlx5_ib_invalidate_range(struct ib_umem *umem, unsigned long start,
  771. unsigned long end);
  772. void mlx5_odp_init_mr_cache_entry(struct mlx5_cache_ent *ent);
  773. void mlx5_odp_populate_klm(struct mlx5_klm *pklm, size_t offset,
  774. size_t nentries, struct mlx5_ib_mr *mr, int flags);
  775. #else /* CONFIG_INFINIBAND_ON_DEMAND_PAGING */
  776. static inline void mlx5_ib_internal_fill_odp_caps(struct mlx5_ib_dev *dev)
  777. {
  778. return;
  779. }
  780. static inline int mlx5_ib_odp_init_one(struct mlx5_ib_dev *ibdev) { return 0; }
  781. static inline void mlx5_ib_odp_remove_one(struct mlx5_ib_dev *ibdev) {}
  782. static inline int mlx5_ib_odp_init(void) { return 0; }
  783. static inline void mlx5_ib_odp_cleanup(void) {}
  784. static inline void mlx5_odp_init_mr_cache_entry(struct mlx5_cache_ent *ent) {}
  785. static inline void mlx5_odp_populate_klm(struct mlx5_klm *pklm, size_t offset,
  786. size_t nentries, struct mlx5_ib_mr *mr,
  787. int flags) {}
  788. #endif /* CONFIG_INFINIBAND_ON_DEMAND_PAGING */
  789. int mlx5_ib_get_vf_config(struct ib_device *device, int vf,
  790. u8 port, struct ifla_vf_info *info);
  791. int mlx5_ib_set_vf_link_state(struct ib_device *device, int vf,
  792. u8 port, int state);
  793. int mlx5_ib_get_vf_stats(struct ib_device *device, int vf,
  794. u8 port, struct ifla_vf_stats *stats);
  795. int mlx5_ib_set_vf_guid(struct ib_device *device, int vf, u8 port,
  796. u64 guid, int type);
  797. __be16 mlx5_get_roce_udp_sport(struct mlx5_ib_dev *dev, u8 port_num,
  798. int index);
  799. int mlx5_get_roce_gid_type(struct mlx5_ib_dev *dev, u8 port_num,
  800. int index, enum ib_gid_type *gid_type);
  801. /* GSI QP helper functions */
  802. struct ib_qp *mlx5_ib_gsi_create_qp(struct ib_pd *pd,
  803. struct ib_qp_init_attr *init_attr);
  804. int mlx5_ib_gsi_destroy_qp(struct ib_qp *qp);
  805. int mlx5_ib_gsi_modify_qp(struct ib_qp *qp, struct ib_qp_attr *attr,
  806. int attr_mask);
  807. int mlx5_ib_gsi_query_qp(struct ib_qp *qp, struct ib_qp_attr *qp_attr,
  808. int qp_attr_mask,
  809. struct ib_qp_init_attr *qp_init_attr);
  810. int mlx5_ib_gsi_post_send(struct ib_qp *qp, struct ib_send_wr *wr,
  811. struct ib_send_wr **bad_wr);
  812. int mlx5_ib_gsi_post_recv(struct ib_qp *qp, struct ib_recv_wr *wr,
  813. struct ib_recv_wr **bad_wr);
  814. void mlx5_ib_gsi_pkey_change(struct mlx5_ib_gsi_qp *gsi);
  815. int mlx5_ib_generate_wc(struct ib_cq *ibcq, struct ib_wc *wc);
  816. static inline void init_query_mad(struct ib_smp *mad)
  817. {
  818. mad->base_version = 1;
  819. mad->mgmt_class = IB_MGMT_CLASS_SUBN_LID_ROUTED;
  820. mad->class_version = 1;
  821. mad->method = IB_MGMT_METHOD_GET;
  822. }
  823. static inline u8 convert_access(int acc)
  824. {
  825. return (acc & IB_ACCESS_REMOTE_ATOMIC ? MLX5_PERM_ATOMIC : 0) |
  826. (acc & IB_ACCESS_REMOTE_WRITE ? MLX5_PERM_REMOTE_WRITE : 0) |
  827. (acc & IB_ACCESS_REMOTE_READ ? MLX5_PERM_REMOTE_READ : 0) |
  828. (acc & IB_ACCESS_LOCAL_WRITE ? MLX5_PERM_LOCAL_WRITE : 0) |
  829. MLX5_PERM_LOCAL_READ;
  830. }
  831. static inline int is_qp1(enum ib_qp_type qp_type)
  832. {
  833. return qp_type == MLX5_IB_QPT_HW_GSI;
  834. }
  835. #define MLX5_MAX_UMR_SHIFT 16
  836. #define MLX5_MAX_UMR_PAGES (1 << MLX5_MAX_UMR_SHIFT)
  837. static inline u32 check_cq_create_flags(u32 flags)
  838. {
  839. /*
  840. * It returns non-zero value for unsupported CQ
  841. * create flags, otherwise it returns zero.
  842. */
  843. return (flags & ~(IB_CQ_FLAGS_IGNORE_OVERRUN |
  844. IB_CQ_FLAGS_TIMESTAMP_COMPLETION));
  845. }
  846. static inline int verify_assign_uidx(u8 cqe_version, u32 cmd_uidx,
  847. u32 *user_index)
  848. {
  849. if (cqe_version) {
  850. if ((cmd_uidx == MLX5_IB_DEFAULT_UIDX) ||
  851. (cmd_uidx & ~MLX5_USER_ASSIGNED_UIDX_MASK))
  852. return -EINVAL;
  853. *user_index = cmd_uidx;
  854. } else {
  855. *user_index = MLX5_IB_DEFAULT_UIDX;
  856. }
  857. return 0;
  858. }
  859. static inline int get_qp_user_index(struct mlx5_ib_ucontext *ucontext,
  860. struct mlx5_ib_create_qp *ucmd,
  861. int inlen,
  862. u32 *user_index)
  863. {
  864. u8 cqe_version = ucontext->cqe_version;
  865. if (field_avail(struct mlx5_ib_create_qp, uidx, inlen) &&
  866. !cqe_version && (ucmd->uidx == MLX5_IB_DEFAULT_UIDX))
  867. return 0;
  868. if (!!(field_avail(struct mlx5_ib_create_qp, uidx, inlen) !=
  869. !!cqe_version))
  870. return -EINVAL;
  871. return verify_assign_uidx(cqe_version, ucmd->uidx, user_index);
  872. }
  873. static inline int get_srq_user_index(struct mlx5_ib_ucontext *ucontext,
  874. struct mlx5_ib_create_srq *ucmd,
  875. int inlen,
  876. u32 *user_index)
  877. {
  878. u8 cqe_version = ucontext->cqe_version;
  879. if (field_avail(struct mlx5_ib_create_srq, uidx, inlen) &&
  880. !cqe_version && (ucmd->uidx == MLX5_IB_DEFAULT_UIDX))
  881. return 0;
  882. if (!!(field_avail(struct mlx5_ib_create_srq, uidx, inlen) !=
  883. !!cqe_version))
  884. return -EINVAL;
  885. return verify_assign_uidx(cqe_version, ucmd->uidx, user_index);
  886. }
  887. static inline int get_uars_per_sys_page(struct mlx5_ib_dev *dev, bool lib_support)
  888. {
  889. return lib_support && MLX5_CAP_GEN(dev->mdev, uar_4k) ?
  890. MLX5_UARS_IN_PAGE : 1;
  891. }
  892. static inline int get_num_uars(struct mlx5_ib_dev *dev,
  893. struct mlx5_bfreg_info *bfregi)
  894. {
  895. return get_uars_per_sys_page(dev, bfregi->lib_uar_4k) * bfregi->num_sys_pages;
  896. }
  897. #endif /* MLX5_IB_H */