pvrdma.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. /*
  2. * Copyright (c) 2012-2016 VMware, Inc. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of EITHER the GNU General Public License
  6. * version 2 as published by the Free Software Foundation or the BSD
  7. * 2-Clause License. This program is distributed in the hope that it
  8. * will be useful, but WITHOUT ANY WARRANTY; WITHOUT EVEN THE IMPLIED
  9. * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
  10. * See the GNU General Public License version 2 for more details at
  11. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.en.html.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program available in the file COPYING in the main
  15. * directory of this source tree.
  16. *
  17. * The BSD 2-Clause License
  18. *
  19. * Redistribution and use in source and binary forms, with or
  20. * without modification, are permitted provided that the following
  21. * conditions are met:
  22. *
  23. * - Redistributions of source code must retain the above
  24. * copyright notice, this list of conditions and the following
  25. * disclaimer.
  26. *
  27. * - Redistributions in binary form must reproduce the above
  28. * copyright notice, this list of conditions and the following
  29. * disclaimer in the documentation and/or other materials
  30. * provided with the distribution.
  31. *
  32. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  33. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  34. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  35. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  36. * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  37. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  38. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  39. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  40. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  41. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  42. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  43. * OF THE POSSIBILITY OF SUCH DAMAGE.
  44. */
  45. #ifndef __PVRDMA_H__
  46. #define __PVRDMA_H__
  47. #include <linux/compiler.h>
  48. #include <linux/interrupt.h>
  49. #include <linux/list.h>
  50. #include <linux/mutex.h>
  51. #include <linux/pci.h>
  52. #include <linux/semaphore.h>
  53. #include <linux/workqueue.h>
  54. #include <rdma/ib_umem.h>
  55. #include <rdma/ib_verbs.h>
  56. #include <rdma/vmw_pvrdma-abi.h>
  57. #include "pvrdma_ring.h"
  58. #include "pvrdma_dev_api.h"
  59. #include "pvrdma_verbs.h"
  60. /* NOT the same as BIT_MASK(). */
  61. #define PVRDMA_MASK(n) ((n << 1) - 1)
  62. /*
  63. * VMware PVRDMA PCI device id.
  64. */
  65. #define PCI_DEVICE_ID_VMWARE_PVRDMA 0x0820
  66. #define PVRDMA_NUM_RING_PAGES 4
  67. #define PVRDMA_QP_NUM_HEADER_PAGES 1
  68. struct pvrdma_dev;
  69. struct pvrdma_page_dir {
  70. dma_addr_t dir_dma;
  71. u64 *dir;
  72. int ntables;
  73. u64 **tables;
  74. u64 npages;
  75. void **pages;
  76. };
  77. struct pvrdma_cq {
  78. struct ib_cq ibcq;
  79. int offset;
  80. spinlock_t cq_lock; /* Poll lock. */
  81. struct pvrdma_uar_map *uar;
  82. struct ib_umem *umem;
  83. struct pvrdma_ring_state *ring_state;
  84. struct pvrdma_page_dir pdir;
  85. u32 cq_handle;
  86. bool is_kernel;
  87. atomic_t refcnt;
  88. wait_queue_head_t wait;
  89. };
  90. struct pvrdma_id_table {
  91. u32 last;
  92. u32 top;
  93. u32 max;
  94. u32 mask;
  95. spinlock_t lock; /* Table lock. */
  96. unsigned long *table;
  97. };
  98. struct pvrdma_uar_map {
  99. unsigned long pfn;
  100. void __iomem *map;
  101. int index;
  102. };
  103. struct pvrdma_uar_table {
  104. struct pvrdma_id_table tbl;
  105. int size;
  106. };
  107. struct pvrdma_ucontext {
  108. struct ib_ucontext ibucontext;
  109. struct pvrdma_dev *dev;
  110. struct pvrdma_uar_map uar;
  111. u64 ctx_handle;
  112. };
  113. struct pvrdma_pd {
  114. struct ib_pd ibpd;
  115. u32 pdn;
  116. u32 pd_handle;
  117. int privileged;
  118. };
  119. struct pvrdma_mr {
  120. u32 mr_handle;
  121. u64 iova;
  122. u64 size;
  123. };
  124. struct pvrdma_user_mr {
  125. struct ib_mr ibmr;
  126. struct ib_umem *umem;
  127. struct pvrdma_mr mmr;
  128. struct pvrdma_page_dir pdir;
  129. u64 *pages;
  130. u32 npages;
  131. u32 max_pages;
  132. u32 page_shift;
  133. };
  134. struct pvrdma_wq {
  135. struct pvrdma_ring *ring;
  136. spinlock_t lock; /* Work queue lock. */
  137. int wqe_cnt;
  138. int wqe_size;
  139. int max_sg;
  140. int offset;
  141. };
  142. struct pvrdma_ah {
  143. struct ib_ah ibah;
  144. struct pvrdma_av av;
  145. };
  146. struct pvrdma_qp {
  147. struct ib_qp ibqp;
  148. u32 qp_handle;
  149. u32 qkey;
  150. struct pvrdma_wq sq;
  151. struct pvrdma_wq rq;
  152. struct ib_umem *rumem;
  153. struct ib_umem *sumem;
  154. struct pvrdma_page_dir pdir;
  155. int npages;
  156. int npages_send;
  157. int npages_recv;
  158. u32 flags;
  159. u8 port;
  160. u8 state;
  161. bool is_kernel;
  162. struct mutex mutex; /* QP state mutex. */
  163. atomic_t refcnt;
  164. wait_queue_head_t wait;
  165. };
  166. struct pvrdma_dev {
  167. /* PCI device-related information. */
  168. struct ib_device ib_dev;
  169. struct pci_dev *pdev;
  170. void __iomem *regs;
  171. struct pvrdma_device_shared_region *dsr; /* Shared region pointer */
  172. dma_addr_t dsrbase; /* Shared region base address */
  173. void *cmd_slot;
  174. void *resp_slot;
  175. unsigned long flags;
  176. struct list_head device_link;
  177. /* Locking and interrupt information. */
  178. spinlock_t cmd_lock; /* Command lock. */
  179. struct semaphore cmd_sema;
  180. struct completion cmd_done;
  181. unsigned int nr_vectors;
  182. /* RDMA-related device information. */
  183. union ib_gid *sgid_tbl;
  184. struct pvrdma_ring_state *async_ring_state;
  185. struct pvrdma_page_dir async_pdir;
  186. struct pvrdma_ring_state *cq_ring_state;
  187. struct pvrdma_page_dir cq_pdir;
  188. struct pvrdma_cq **cq_tbl;
  189. spinlock_t cq_tbl_lock;
  190. struct pvrdma_qp **qp_tbl;
  191. spinlock_t qp_tbl_lock;
  192. struct pvrdma_uar_table uar_table;
  193. struct pvrdma_uar_map driver_uar;
  194. __be64 sys_image_guid;
  195. spinlock_t desc_lock; /* Device modification lock. */
  196. u32 port_cap_mask;
  197. struct mutex port_mutex; /* Port modification mutex. */
  198. bool ib_active;
  199. atomic_t num_qps;
  200. atomic_t num_cqs;
  201. atomic_t num_pds;
  202. atomic_t num_ahs;
  203. /* Network device information. */
  204. struct net_device *netdev;
  205. struct notifier_block nb_netdev;
  206. };
  207. struct pvrdma_netdevice_work {
  208. struct work_struct work;
  209. struct net_device *event_netdev;
  210. unsigned long event;
  211. };
  212. static inline struct pvrdma_dev *to_vdev(struct ib_device *ibdev)
  213. {
  214. return container_of(ibdev, struct pvrdma_dev, ib_dev);
  215. }
  216. static inline struct
  217. pvrdma_ucontext *to_vucontext(struct ib_ucontext *ibucontext)
  218. {
  219. return container_of(ibucontext, struct pvrdma_ucontext, ibucontext);
  220. }
  221. static inline struct pvrdma_pd *to_vpd(struct ib_pd *ibpd)
  222. {
  223. return container_of(ibpd, struct pvrdma_pd, ibpd);
  224. }
  225. static inline struct pvrdma_cq *to_vcq(struct ib_cq *ibcq)
  226. {
  227. return container_of(ibcq, struct pvrdma_cq, ibcq);
  228. }
  229. static inline struct pvrdma_user_mr *to_vmr(struct ib_mr *ibmr)
  230. {
  231. return container_of(ibmr, struct pvrdma_user_mr, ibmr);
  232. }
  233. static inline struct pvrdma_qp *to_vqp(struct ib_qp *ibqp)
  234. {
  235. return container_of(ibqp, struct pvrdma_qp, ibqp);
  236. }
  237. static inline struct pvrdma_ah *to_vah(struct ib_ah *ibah)
  238. {
  239. return container_of(ibah, struct pvrdma_ah, ibah);
  240. }
  241. static inline void pvrdma_write_reg(struct pvrdma_dev *dev, u32 reg, u32 val)
  242. {
  243. writel(cpu_to_le32(val), dev->regs + reg);
  244. }
  245. static inline u32 pvrdma_read_reg(struct pvrdma_dev *dev, u32 reg)
  246. {
  247. return le32_to_cpu(readl(dev->regs + reg));
  248. }
  249. static inline void pvrdma_write_uar_cq(struct pvrdma_dev *dev, u32 val)
  250. {
  251. writel(cpu_to_le32(val), dev->driver_uar.map + PVRDMA_UAR_CQ_OFFSET);
  252. }
  253. static inline void pvrdma_write_uar_qp(struct pvrdma_dev *dev, u32 val)
  254. {
  255. writel(cpu_to_le32(val), dev->driver_uar.map + PVRDMA_UAR_QP_OFFSET);
  256. }
  257. static inline void *pvrdma_page_dir_get_ptr(struct pvrdma_page_dir *pdir,
  258. u64 offset)
  259. {
  260. return pdir->pages[offset / PAGE_SIZE] + (offset % PAGE_SIZE);
  261. }
  262. static inline enum pvrdma_mtu ib_mtu_to_pvrdma(enum ib_mtu mtu)
  263. {
  264. return (enum pvrdma_mtu)mtu;
  265. }
  266. static inline enum ib_mtu pvrdma_mtu_to_ib(enum pvrdma_mtu mtu)
  267. {
  268. return (enum ib_mtu)mtu;
  269. }
  270. static inline enum pvrdma_port_state ib_port_state_to_pvrdma(
  271. enum ib_port_state state)
  272. {
  273. return (enum pvrdma_port_state)state;
  274. }
  275. static inline enum ib_port_state pvrdma_port_state_to_ib(
  276. enum pvrdma_port_state state)
  277. {
  278. return (enum ib_port_state)state;
  279. }
  280. static inline int ib_port_cap_flags_to_pvrdma(int flags)
  281. {
  282. return flags & PVRDMA_MASK(PVRDMA_PORT_CAP_FLAGS_MAX);
  283. }
  284. static inline int pvrdma_port_cap_flags_to_ib(int flags)
  285. {
  286. return flags;
  287. }
  288. static inline enum pvrdma_port_width ib_port_width_to_pvrdma(
  289. enum ib_port_width width)
  290. {
  291. return (enum pvrdma_port_width)width;
  292. }
  293. static inline enum ib_port_width pvrdma_port_width_to_ib(
  294. enum pvrdma_port_width width)
  295. {
  296. return (enum ib_port_width)width;
  297. }
  298. static inline enum pvrdma_port_speed ib_port_speed_to_pvrdma(
  299. enum ib_port_speed speed)
  300. {
  301. return (enum pvrdma_port_speed)speed;
  302. }
  303. static inline enum ib_port_speed pvrdma_port_speed_to_ib(
  304. enum pvrdma_port_speed speed)
  305. {
  306. return (enum ib_port_speed)speed;
  307. }
  308. static inline int pvrdma_qp_attr_mask_to_ib(int attr_mask)
  309. {
  310. return attr_mask;
  311. }
  312. static inline int ib_qp_attr_mask_to_pvrdma(int attr_mask)
  313. {
  314. return attr_mask & PVRDMA_MASK(PVRDMA_QP_ATTR_MASK_MAX);
  315. }
  316. static inline enum pvrdma_mig_state ib_mig_state_to_pvrdma(
  317. enum ib_mig_state state)
  318. {
  319. return (enum pvrdma_mig_state)state;
  320. }
  321. static inline enum ib_mig_state pvrdma_mig_state_to_ib(
  322. enum pvrdma_mig_state state)
  323. {
  324. return (enum ib_mig_state)state;
  325. }
  326. static inline int ib_access_flags_to_pvrdma(int flags)
  327. {
  328. return flags;
  329. }
  330. static inline int pvrdma_access_flags_to_ib(int flags)
  331. {
  332. return flags & PVRDMA_MASK(PVRDMA_ACCESS_FLAGS_MAX);
  333. }
  334. static inline enum pvrdma_qp_type ib_qp_type_to_pvrdma(enum ib_qp_type type)
  335. {
  336. return (enum pvrdma_qp_type)type;
  337. }
  338. static inline enum ib_qp_type pvrdma_qp_type_to_ib(enum pvrdma_qp_type type)
  339. {
  340. return (enum ib_qp_type)type;
  341. }
  342. static inline enum pvrdma_qp_state ib_qp_state_to_pvrdma(enum ib_qp_state state)
  343. {
  344. return (enum pvrdma_qp_state)state;
  345. }
  346. static inline enum ib_qp_state pvrdma_qp_state_to_ib(enum pvrdma_qp_state state)
  347. {
  348. return (enum ib_qp_state)state;
  349. }
  350. static inline enum pvrdma_wr_opcode ib_wr_opcode_to_pvrdma(enum ib_wr_opcode op)
  351. {
  352. return (enum pvrdma_wr_opcode)op;
  353. }
  354. static inline enum ib_wc_status pvrdma_wc_status_to_ib(
  355. enum pvrdma_wc_status status)
  356. {
  357. return (enum ib_wc_status)status;
  358. }
  359. static inline int pvrdma_wc_opcode_to_ib(int opcode)
  360. {
  361. return opcode;
  362. }
  363. static inline int pvrdma_wc_flags_to_ib(int flags)
  364. {
  365. return flags;
  366. }
  367. static inline int ib_send_flags_to_pvrdma(int flags)
  368. {
  369. return flags & PVRDMA_MASK(PVRDMA_SEND_FLAGS_MAX);
  370. }
  371. void pvrdma_qp_cap_to_ib(struct ib_qp_cap *dst,
  372. const struct pvrdma_qp_cap *src);
  373. void ib_qp_cap_to_pvrdma(struct pvrdma_qp_cap *dst,
  374. const struct ib_qp_cap *src);
  375. void pvrdma_gid_to_ib(union ib_gid *dst, const union pvrdma_gid *src);
  376. void ib_gid_to_pvrdma(union pvrdma_gid *dst, const union ib_gid *src);
  377. void pvrdma_global_route_to_ib(struct ib_global_route *dst,
  378. const struct pvrdma_global_route *src);
  379. void ib_global_route_to_pvrdma(struct pvrdma_global_route *dst,
  380. const struct ib_global_route *src);
  381. void pvrdma_ah_attr_to_rdma(struct rdma_ah_attr *dst,
  382. const struct pvrdma_ah_attr *src);
  383. void rdma_ah_attr_to_pvrdma(struct pvrdma_ah_attr *dst,
  384. const struct rdma_ah_attr *src);
  385. int pvrdma_uar_table_init(struct pvrdma_dev *dev);
  386. void pvrdma_uar_table_cleanup(struct pvrdma_dev *dev);
  387. int pvrdma_uar_alloc(struct pvrdma_dev *dev, struct pvrdma_uar_map *uar);
  388. void pvrdma_uar_free(struct pvrdma_dev *dev, struct pvrdma_uar_map *uar);
  389. void _pvrdma_flush_cqe(struct pvrdma_qp *qp, struct pvrdma_cq *cq);
  390. int pvrdma_page_dir_init(struct pvrdma_dev *dev, struct pvrdma_page_dir *pdir,
  391. u64 npages, bool alloc_pages);
  392. void pvrdma_page_dir_cleanup(struct pvrdma_dev *dev,
  393. struct pvrdma_page_dir *pdir);
  394. int pvrdma_page_dir_insert_dma(struct pvrdma_page_dir *pdir, u64 idx,
  395. dma_addr_t daddr);
  396. int pvrdma_page_dir_insert_umem(struct pvrdma_page_dir *pdir,
  397. struct ib_umem *umem, u64 offset);
  398. dma_addr_t pvrdma_page_dir_get_dma(struct pvrdma_page_dir *pdir, u64 idx);
  399. int pvrdma_page_dir_insert_page_list(struct pvrdma_page_dir *pdir,
  400. u64 *page_list, int num_pages);
  401. int pvrdma_cmd_post(struct pvrdma_dev *dev, union pvrdma_cmd_req *req,
  402. union pvrdma_cmd_resp *rsp, unsigned resp_code);
  403. #endif /* __PVRDMA_H__ */