mlx5_ib.h 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022
  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 next_fence;
  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. int ndescs;
  446. };
  447. struct mlx5_ib_umr_context {
  448. struct ib_cqe cqe;
  449. enum ib_wc_status status;
  450. struct completion done;
  451. };
  452. struct umr_common {
  453. struct ib_pd *pd;
  454. struct ib_cq *cq;
  455. struct ib_qp *qp;
  456. /* control access to UMR QP
  457. */
  458. struct semaphore sem;
  459. };
  460. enum {
  461. MLX5_FMR_INVALID,
  462. MLX5_FMR_VALID,
  463. MLX5_FMR_BUSY,
  464. };
  465. struct mlx5_cache_ent {
  466. struct list_head head;
  467. /* sync access to the cahce entry
  468. */
  469. spinlock_t lock;
  470. struct dentry *dir;
  471. char name[4];
  472. u32 order;
  473. u32 xlt;
  474. u32 access_mode;
  475. u32 page;
  476. u32 size;
  477. u32 cur;
  478. u32 miss;
  479. u32 limit;
  480. struct dentry *fsize;
  481. struct dentry *fcur;
  482. struct dentry *fmiss;
  483. struct dentry *flimit;
  484. struct mlx5_ib_dev *dev;
  485. struct work_struct work;
  486. struct delayed_work dwork;
  487. int pending;
  488. struct completion compl;
  489. };
  490. struct mlx5_mr_cache {
  491. struct workqueue_struct *wq;
  492. struct mlx5_cache_ent ent[MAX_MR_CACHE_ENTRIES];
  493. int stopped;
  494. struct dentry *root;
  495. unsigned long last_add;
  496. };
  497. struct mlx5_ib_gsi_qp;
  498. struct mlx5_ib_port_resources {
  499. struct mlx5_ib_resources *devr;
  500. struct mlx5_ib_gsi_qp *gsi;
  501. struct work_struct pkey_change_work;
  502. };
  503. struct mlx5_ib_resources {
  504. struct ib_cq *c0;
  505. struct ib_xrcd *x0;
  506. struct ib_xrcd *x1;
  507. struct ib_pd *p0;
  508. struct ib_srq *s0;
  509. struct ib_srq *s1;
  510. struct mlx5_ib_port_resources ports[2];
  511. /* Protects changes to the port resources */
  512. struct mutex mutex;
  513. };
  514. struct mlx5_ib_counters {
  515. const char **names;
  516. size_t *offsets;
  517. u32 num_q_counters;
  518. u32 num_cong_counters;
  519. u16 set_id;
  520. };
  521. struct mlx5_ib_port {
  522. struct mlx5_ib_counters cnts;
  523. };
  524. struct mlx5_roce {
  525. /* Protect mlx5_ib_get_netdev from invoking dev_hold() with a NULL
  526. * netdev pointer
  527. */
  528. rwlock_t netdev_lock;
  529. struct net_device *netdev;
  530. struct notifier_block nb;
  531. atomic_t next_port;
  532. };
  533. struct mlx5_ib_dev {
  534. struct ib_device ib_dev;
  535. struct mlx5_core_dev *mdev;
  536. struct mlx5_roce roce;
  537. int num_ports;
  538. /* serialize update of capability mask
  539. */
  540. struct mutex cap_mask_mutex;
  541. bool ib_active;
  542. struct umr_common umrc;
  543. /* sync used page count stats
  544. */
  545. struct mlx5_ib_resources devr;
  546. struct mlx5_mr_cache cache;
  547. struct timer_list delay_timer;
  548. /* Prevents soft lock on massive reg MRs */
  549. struct mutex slow_path_mutex;
  550. int fill_delay;
  551. #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
  552. struct ib_odp_caps odp_caps;
  553. u64 odp_max_size;
  554. /*
  555. * Sleepable RCU that prevents destruction of MRs while they are still
  556. * being used by a page fault handler.
  557. */
  558. struct srcu_struct mr_srcu;
  559. u32 null_mkey;
  560. #endif
  561. struct mlx5_ib_flow_db flow_db;
  562. /* protect resources needed as part of reset flow */
  563. spinlock_t reset_flow_resource_lock;
  564. struct list_head qp_list;
  565. /* Array with num_ports elements */
  566. struct mlx5_ib_port *port;
  567. struct mlx5_sq_bfreg bfreg;
  568. struct mlx5_sq_bfreg fp_bfreg;
  569. u8 umr_fence;
  570. };
  571. static inline struct mlx5_ib_cq *to_mibcq(struct mlx5_core_cq *mcq)
  572. {
  573. return container_of(mcq, struct mlx5_ib_cq, mcq);
  574. }
  575. static inline struct mlx5_ib_xrcd *to_mxrcd(struct ib_xrcd *ibxrcd)
  576. {
  577. return container_of(ibxrcd, struct mlx5_ib_xrcd, ibxrcd);
  578. }
  579. static inline struct mlx5_ib_dev *to_mdev(struct ib_device *ibdev)
  580. {
  581. return container_of(ibdev, struct mlx5_ib_dev, ib_dev);
  582. }
  583. static inline struct mlx5_ib_cq *to_mcq(struct ib_cq *ibcq)
  584. {
  585. return container_of(ibcq, struct mlx5_ib_cq, ibcq);
  586. }
  587. static inline struct mlx5_ib_qp *to_mibqp(struct mlx5_core_qp *mqp)
  588. {
  589. return container_of(mqp, struct mlx5_ib_qp_base, mqp)->container_mibqp;
  590. }
  591. static inline struct mlx5_ib_rwq *to_mibrwq(struct mlx5_core_qp *core_qp)
  592. {
  593. return container_of(core_qp, struct mlx5_ib_rwq, core_qp);
  594. }
  595. static inline struct mlx5_ib_mr *to_mibmr(struct mlx5_core_mkey *mmkey)
  596. {
  597. return container_of(mmkey, struct mlx5_ib_mr, mmkey);
  598. }
  599. static inline struct mlx5_ib_pd *to_mpd(struct ib_pd *ibpd)
  600. {
  601. return container_of(ibpd, struct mlx5_ib_pd, ibpd);
  602. }
  603. static inline struct mlx5_ib_srq *to_msrq(struct ib_srq *ibsrq)
  604. {
  605. return container_of(ibsrq, struct mlx5_ib_srq, ibsrq);
  606. }
  607. static inline struct mlx5_ib_qp *to_mqp(struct ib_qp *ibqp)
  608. {
  609. return container_of(ibqp, struct mlx5_ib_qp, ibqp);
  610. }
  611. static inline struct mlx5_ib_rwq *to_mrwq(struct ib_wq *ibwq)
  612. {
  613. return container_of(ibwq, struct mlx5_ib_rwq, ibwq);
  614. }
  615. static inline struct mlx5_ib_rwq_ind_table *to_mrwq_ind_table(struct ib_rwq_ind_table *ib_rwq_ind_tbl)
  616. {
  617. return container_of(ib_rwq_ind_tbl, struct mlx5_ib_rwq_ind_table, ib_rwq_ind_tbl);
  618. }
  619. static inline struct mlx5_ib_srq *to_mibsrq(struct mlx5_core_srq *msrq)
  620. {
  621. return container_of(msrq, struct mlx5_ib_srq, msrq);
  622. }
  623. static inline struct mlx5_ib_mr *to_mmr(struct ib_mr *ibmr)
  624. {
  625. return container_of(ibmr, struct mlx5_ib_mr, ibmr);
  626. }
  627. static inline struct mlx5_ib_mw *to_mmw(struct ib_mw *ibmw)
  628. {
  629. return container_of(ibmw, struct mlx5_ib_mw, ibmw);
  630. }
  631. int mlx5_ib_db_map_user(struct mlx5_ib_ucontext *context, unsigned long virt,
  632. struct mlx5_db *db);
  633. void mlx5_ib_db_unmap_user(struct mlx5_ib_ucontext *context, struct mlx5_db *db);
  634. void __mlx5_ib_cq_clean(struct mlx5_ib_cq *cq, u32 qpn, struct mlx5_ib_srq *srq);
  635. void mlx5_ib_cq_clean(struct mlx5_ib_cq *cq, u32 qpn, struct mlx5_ib_srq *srq);
  636. void mlx5_ib_free_srq_wqe(struct mlx5_ib_srq *srq, int wqe_index);
  637. int mlx5_MAD_IFC(struct mlx5_ib_dev *dev, int ignore_mkey, int ignore_bkey,
  638. u8 port, const struct ib_wc *in_wc, const struct ib_grh *in_grh,
  639. const void *in_mad, void *response_mad);
  640. struct ib_ah *mlx5_ib_create_ah(struct ib_pd *pd, struct rdma_ah_attr *ah_attr,
  641. struct ib_udata *udata);
  642. int mlx5_ib_query_ah(struct ib_ah *ibah, struct rdma_ah_attr *ah_attr);
  643. int mlx5_ib_destroy_ah(struct ib_ah *ah);
  644. struct ib_srq *mlx5_ib_create_srq(struct ib_pd *pd,
  645. struct ib_srq_init_attr *init_attr,
  646. struct ib_udata *udata);
  647. int mlx5_ib_modify_srq(struct ib_srq *ibsrq, struct ib_srq_attr *attr,
  648. enum ib_srq_attr_mask attr_mask, struct ib_udata *udata);
  649. int mlx5_ib_query_srq(struct ib_srq *ibsrq, struct ib_srq_attr *srq_attr);
  650. int mlx5_ib_destroy_srq(struct ib_srq *srq);
  651. int mlx5_ib_post_srq_recv(struct ib_srq *ibsrq, struct ib_recv_wr *wr,
  652. struct ib_recv_wr **bad_wr);
  653. struct ib_qp *mlx5_ib_create_qp(struct ib_pd *pd,
  654. struct ib_qp_init_attr *init_attr,
  655. struct ib_udata *udata);
  656. int mlx5_ib_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
  657. int attr_mask, struct ib_udata *udata);
  658. int mlx5_ib_query_qp(struct ib_qp *ibqp, struct ib_qp_attr *qp_attr, int qp_attr_mask,
  659. struct ib_qp_init_attr *qp_init_attr);
  660. int mlx5_ib_destroy_qp(struct ib_qp *qp);
  661. int mlx5_ib_post_send(struct ib_qp *ibqp, struct ib_send_wr *wr,
  662. struct ib_send_wr **bad_wr);
  663. int mlx5_ib_post_recv(struct ib_qp *ibqp, struct ib_recv_wr *wr,
  664. struct ib_recv_wr **bad_wr);
  665. void *mlx5_get_send_wqe(struct mlx5_ib_qp *qp, int n);
  666. int mlx5_ib_read_user_wqe(struct mlx5_ib_qp *qp, int send, int wqe_index,
  667. void *buffer, u32 length,
  668. struct mlx5_ib_qp_base *base);
  669. struct ib_cq *mlx5_ib_create_cq(struct ib_device *ibdev,
  670. const struct ib_cq_init_attr *attr,
  671. struct ib_ucontext *context,
  672. struct ib_udata *udata);
  673. int mlx5_ib_destroy_cq(struct ib_cq *cq);
  674. int mlx5_ib_poll_cq(struct ib_cq *ibcq, int num_entries, struct ib_wc *wc);
  675. int mlx5_ib_arm_cq(struct ib_cq *ibcq, enum ib_cq_notify_flags flags);
  676. int mlx5_ib_modify_cq(struct ib_cq *cq, u16 cq_count, u16 cq_period);
  677. int mlx5_ib_resize_cq(struct ib_cq *ibcq, int entries, struct ib_udata *udata);
  678. struct ib_mr *mlx5_ib_get_dma_mr(struct ib_pd *pd, int acc);
  679. struct ib_mr *mlx5_ib_reg_user_mr(struct ib_pd *pd, u64 start, u64 length,
  680. u64 virt_addr, int access_flags,
  681. struct ib_udata *udata);
  682. struct ib_mw *mlx5_ib_alloc_mw(struct ib_pd *pd, enum ib_mw_type type,
  683. struct ib_udata *udata);
  684. int mlx5_ib_dealloc_mw(struct ib_mw *mw);
  685. int mlx5_ib_update_xlt(struct mlx5_ib_mr *mr, u64 idx, int npages,
  686. int page_shift, int flags);
  687. struct mlx5_ib_mr *mlx5_ib_alloc_implicit_mr(struct mlx5_ib_pd *pd,
  688. int access_flags);
  689. void mlx5_ib_free_implicit_mr(struct mlx5_ib_mr *mr);
  690. int mlx5_ib_rereg_user_mr(struct ib_mr *ib_mr, int flags, u64 start,
  691. u64 length, u64 virt_addr, int access_flags,
  692. struct ib_pd *pd, struct ib_udata *udata);
  693. int mlx5_ib_dereg_mr(struct ib_mr *ibmr);
  694. struct ib_mr *mlx5_ib_alloc_mr(struct ib_pd *pd,
  695. enum ib_mr_type mr_type,
  696. u32 max_num_sg);
  697. int mlx5_ib_map_mr_sg(struct ib_mr *ibmr, struct scatterlist *sg, int sg_nents,
  698. unsigned int *sg_offset);
  699. int mlx5_ib_process_mad(struct ib_device *ibdev, int mad_flags, u8 port_num,
  700. const struct ib_wc *in_wc, const struct ib_grh *in_grh,
  701. const struct ib_mad_hdr *in, size_t in_mad_size,
  702. struct ib_mad_hdr *out, size_t *out_mad_size,
  703. u16 *out_mad_pkey_index);
  704. struct ib_xrcd *mlx5_ib_alloc_xrcd(struct ib_device *ibdev,
  705. struct ib_ucontext *context,
  706. struct ib_udata *udata);
  707. int mlx5_ib_dealloc_xrcd(struct ib_xrcd *xrcd);
  708. int mlx5_ib_get_buf_offset(u64 addr, int page_shift, u32 *offset);
  709. int mlx5_query_ext_port_caps(struct mlx5_ib_dev *dev, u8 port);
  710. int mlx5_query_mad_ifc_smp_attr_node_info(struct ib_device *ibdev,
  711. struct ib_smp *out_mad);
  712. int mlx5_query_mad_ifc_system_image_guid(struct ib_device *ibdev,
  713. __be64 *sys_image_guid);
  714. int mlx5_query_mad_ifc_max_pkeys(struct ib_device *ibdev,
  715. u16 *max_pkeys);
  716. int mlx5_query_mad_ifc_vendor_id(struct ib_device *ibdev,
  717. u32 *vendor_id);
  718. int mlx5_query_mad_ifc_node_desc(struct mlx5_ib_dev *dev, char *node_desc);
  719. int mlx5_query_mad_ifc_node_guid(struct mlx5_ib_dev *dev, __be64 *node_guid);
  720. int mlx5_query_mad_ifc_pkey(struct ib_device *ibdev, u8 port, u16 index,
  721. u16 *pkey);
  722. int mlx5_query_mad_ifc_gids(struct ib_device *ibdev, u8 port, int index,
  723. union ib_gid *gid);
  724. int mlx5_query_mad_ifc_port(struct ib_device *ibdev, u8 port,
  725. struct ib_port_attr *props);
  726. int mlx5_ib_query_port(struct ib_device *ibdev, u8 port,
  727. struct ib_port_attr *props);
  728. int mlx5_ib_init_fmr(struct mlx5_ib_dev *dev);
  729. void mlx5_ib_cleanup_fmr(struct mlx5_ib_dev *dev);
  730. void mlx5_ib_cont_pages(struct ib_umem *umem, u64 addr,
  731. unsigned long max_page_shift,
  732. int *count, int *shift,
  733. int *ncont, int *order);
  734. void __mlx5_ib_populate_pas(struct mlx5_ib_dev *dev, struct ib_umem *umem,
  735. int page_shift, size_t offset, size_t num_pages,
  736. __be64 *pas, int access_flags);
  737. void mlx5_ib_populate_pas(struct mlx5_ib_dev *dev, struct ib_umem *umem,
  738. int page_shift, __be64 *pas, int access_flags);
  739. void mlx5_ib_copy_pas(u64 *old, u64 *new, int step, int num);
  740. int mlx5_ib_get_cqe_size(struct mlx5_ib_dev *dev, struct ib_cq *ibcq);
  741. int mlx5_mr_cache_init(struct mlx5_ib_dev *dev);
  742. int mlx5_mr_cache_cleanup(struct mlx5_ib_dev *dev);
  743. struct mlx5_ib_mr *mlx5_mr_cache_alloc(struct mlx5_ib_dev *dev, int entry);
  744. void mlx5_mr_cache_free(struct mlx5_ib_dev *dev, struct mlx5_ib_mr *mr);
  745. int mlx5_ib_check_mr_status(struct ib_mr *ibmr, u32 check_mask,
  746. struct ib_mr_status *mr_status);
  747. struct ib_wq *mlx5_ib_create_wq(struct ib_pd *pd,
  748. struct ib_wq_init_attr *init_attr,
  749. struct ib_udata *udata);
  750. int mlx5_ib_destroy_wq(struct ib_wq *wq);
  751. int mlx5_ib_modify_wq(struct ib_wq *wq, struct ib_wq_attr *wq_attr,
  752. u32 wq_attr_mask, struct ib_udata *udata);
  753. struct ib_rwq_ind_table *mlx5_ib_create_rwq_ind_table(struct ib_device *device,
  754. struct ib_rwq_ind_table_init_attr *init_attr,
  755. struct ib_udata *udata);
  756. int mlx5_ib_destroy_rwq_ind_table(struct ib_rwq_ind_table *wq_ind_table);
  757. #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
  758. void mlx5_ib_internal_fill_odp_caps(struct mlx5_ib_dev *dev);
  759. void mlx5_ib_pfault(struct mlx5_core_dev *mdev, void *context,
  760. struct mlx5_pagefault *pfault);
  761. int mlx5_ib_odp_init_one(struct mlx5_ib_dev *ibdev);
  762. void mlx5_ib_odp_remove_one(struct mlx5_ib_dev *ibdev);
  763. int __init mlx5_ib_odp_init(void);
  764. void mlx5_ib_odp_cleanup(void);
  765. void mlx5_ib_invalidate_range(struct ib_umem *umem, unsigned long start,
  766. unsigned long end);
  767. void mlx5_odp_init_mr_cache_entry(struct mlx5_cache_ent *ent);
  768. void mlx5_odp_populate_klm(struct mlx5_klm *pklm, size_t offset,
  769. size_t nentries, struct mlx5_ib_mr *mr, int flags);
  770. #else /* CONFIG_INFINIBAND_ON_DEMAND_PAGING */
  771. static inline void mlx5_ib_internal_fill_odp_caps(struct mlx5_ib_dev *dev)
  772. {
  773. return;
  774. }
  775. static inline int mlx5_ib_odp_init_one(struct mlx5_ib_dev *ibdev) { return 0; }
  776. static inline void mlx5_ib_odp_remove_one(struct mlx5_ib_dev *ibdev) {}
  777. static inline int mlx5_ib_odp_init(void) { return 0; }
  778. static inline void mlx5_ib_odp_cleanup(void) {}
  779. static inline void mlx5_odp_init_mr_cache_entry(struct mlx5_cache_ent *ent) {}
  780. static inline void mlx5_odp_populate_klm(struct mlx5_klm *pklm, size_t offset,
  781. size_t nentries, struct mlx5_ib_mr *mr,
  782. int flags) {}
  783. #endif /* CONFIG_INFINIBAND_ON_DEMAND_PAGING */
  784. int mlx5_ib_get_vf_config(struct ib_device *device, int vf,
  785. u8 port, struct ifla_vf_info *info);
  786. int mlx5_ib_set_vf_link_state(struct ib_device *device, int vf,
  787. u8 port, int state);
  788. int mlx5_ib_get_vf_stats(struct ib_device *device, int vf,
  789. u8 port, struct ifla_vf_stats *stats);
  790. int mlx5_ib_set_vf_guid(struct ib_device *device, int vf, u8 port,
  791. u64 guid, int type);
  792. __be16 mlx5_get_roce_udp_sport(struct mlx5_ib_dev *dev, u8 port_num,
  793. int index);
  794. int mlx5_get_roce_gid_type(struct mlx5_ib_dev *dev, u8 port_num,
  795. int index, enum ib_gid_type *gid_type);
  796. /* GSI QP helper functions */
  797. struct ib_qp *mlx5_ib_gsi_create_qp(struct ib_pd *pd,
  798. struct ib_qp_init_attr *init_attr);
  799. int mlx5_ib_gsi_destroy_qp(struct ib_qp *qp);
  800. int mlx5_ib_gsi_modify_qp(struct ib_qp *qp, struct ib_qp_attr *attr,
  801. int attr_mask);
  802. int mlx5_ib_gsi_query_qp(struct ib_qp *qp, struct ib_qp_attr *qp_attr,
  803. int qp_attr_mask,
  804. struct ib_qp_init_attr *qp_init_attr);
  805. int mlx5_ib_gsi_post_send(struct ib_qp *qp, struct ib_send_wr *wr,
  806. struct ib_send_wr **bad_wr);
  807. int mlx5_ib_gsi_post_recv(struct ib_qp *qp, struct ib_recv_wr *wr,
  808. struct ib_recv_wr **bad_wr);
  809. void mlx5_ib_gsi_pkey_change(struct mlx5_ib_gsi_qp *gsi);
  810. int mlx5_ib_generate_wc(struct ib_cq *ibcq, struct ib_wc *wc);
  811. static inline void init_query_mad(struct ib_smp *mad)
  812. {
  813. mad->base_version = 1;
  814. mad->mgmt_class = IB_MGMT_CLASS_SUBN_LID_ROUTED;
  815. mad->class_version = 1;
  816. mad->method = IB_MGMT_METHOD_GET;
  817. }
  818. static inline u8 convert_access(int acc)
  819. {
  820. return (acc & IB_ACCESS_REMOTE_ATOMIC ? MLX5_PERM_ATOMIC : 0) |
  821. (acc & IB_ACCESS_REMOTE_WRITE ? MLX5_PERM_REMOTE_WRITE : 0) |
  822. (acc & IB_ACCESS_REMOTE_READ ? MLX5_PERM_REMOTE_READ : 0) |
  823. (acc & IB_ACCESS_LOCAL_WRITE ? MLX5_PERM_LOCAL_WRITE : 0) |
  824. MLX5_PERM_LOCAL_READ;
  825. }
  826. static inline int is_qp1(enum ib_qp_type qp_type)
  827. {
  828. return qp_type == MLX5_IB_QPT_HW_GSI;
  829. }
  830. #define MLX5_MAX_UMR_SHIFT 16
  831. #define MLX5_MAX_UMR_PAGES (1 << MLX5_MAX_UMR_SHIFT)
  832. static inline u32 check_cq_create_flags(u32 flags)
  833. {
  834. /*
  835. * It returns non-zero value for unsupported CQ
  836. * create flags, otherwise it returns zero.
  837. */
  838. return (flags & ~(IB_CQ_FLAGS_IGNORE_OVERRUN |
  839. IB_CQ_FLAGS_TIMESTAMP_COMPLETION));
  840. }
  841. static inline int verify_assign_uidx(u8 cqe_version, u32 cmd_uidx,
  842. u32 *user_index)
  843. {
  844. if (cqe_version) {
  845. if ((cmd_uidx == MLX5_IB_DEFAULT_UIDX) ||
  846. (cmd_uidx & ~MLX5_USER_ASSIGNED_UIDX_MASK))
  847. return -EINVAL;
  848. *user_index = cmd_uidx;
  849. } else {
  850. *user_index = MLX5_IB_DEFAULT_UIDX;
  851. }
  852. return 0;
  853. }
  854. static inline int get_qp_user_index(struct mlx5_ib_ucontext *ucontext,
  855. struct mlx5_ib_create_qp *ucmd,
  856. int inlen,
  857. u32 *user_index)
  858. {
  859. u8 cqe_version = ucontext->cqe_version;
  860. if (field_avail(struct mlx5_ib_create_qp, uidx, inlen) &&
  861. !cqe_version && (ucmd->uidx == MLX5_IB_DEFAULT_UIDX))
  862. return 0;
  863. if (!!(field_avail(struct mlx5_ib_create_qp, uidx, inlen) !=
  864. !!cqe_version))
  865. return -EINVAL;
  866. return verify_assign_uidx(cqe_version, ucmd->uidx, user_index);
  867. }
  868. static inline int get_srq_user_index(struct mlx5_ib_ucontext *ucontext,
  869. struct mlx5_ib_create_srq *ucmd,
  870. int inlen,
  871. u32 *user_index)
  872. {
  873. u8 cqe_version = ucontext->cqe_version;
  874. if (field_avail(struct mlx5_ib_create_srq, uidx, inlen) &&
  875. !cqe_version && (ucmd->uidx == MLX5_IB_DEFAULT_UIDX))
  876. return 0;
  877. if (!!(field_avail(struct mlx5_ib_create_srq, uidx, inlen) !=
  878. !!cqe_version))
  879. return -EINVAL;
  880. return verify_assign_uidx(cqe_version, ucmd->uidx, user_index);
  881. }
  882. static inline int get_uars_per_sys_page(struct mlx5_ib_dev *dev, bool lib_support)
  883. {
  884. return lib_support && MLX5_CAP_GEN(dev->mdev, uar_4k) ?
  885. MLX5_UARS_IN_PAGE : 1;
  886. }
  887. static inline int get_num_uars(struct mlx5_ib_dev *dev,
  888. struct mlx5_bfreg_info *bfregi)
  889. {
  890. return get_uars_per_sys_page(dev, bfregi->lib_uar_4k) * bfregi->num_sys_pages;
  891. }
  892. #endif /* MLX5_IB_H */