pvrdma.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  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. struct pvrdma_dev;
  67. struct pvrdma_page_dir {
  68. dma_addr_t dir_dma;
  69. u64 *dir;
  70. int ntables;
  71. u64 **tables;
  72. u64 npages;
  73. void **pages;
  74. };
  75. struct pvrdma_cq {
  76. struct ib_cq ibcq;
  77. int offset;
  78. spinlock_t cq_lock; /* Poll lock. */
  79. struct pvrdma_uar_map *uar;
  80. struct ib_umem *umem;
  81. struct pvrdma_ring_state *ring_state;
  82. struct pvrdma_page_dir pdir;
  83. u32 cq_handle;
  84. bool is_kernel;
  85. atomic_t refcnt;
  86. wait_queue_head_t wait;
  87. };
  88. struct pvrdma_id_table {
  89. u32 last;
  90. u32 top;
  91. u32 max;
  92. u32 mask;
  93. spinlock_t lock; /* Table lock. */
  94. unsigned long *table;
  95. };
  96. struct pvrdma_uar_map {
  97. unsigned long pfn;
  98. void __iomem *map;
  99. int index;
  100. };
  101. struct pvrdma_uar_table {
  102. struct pvrdma_id_table tbl;
  103. int size;
  104. };
  105. struct pvrdma_ucontext {
  106. struct ib_ucontext ibucontext;
  107. struct pvrdma_dev *dev;
  108. struct pvrdma_uar_map uar;
  109. u64 ctx_handle;
  110. };
  111. struct pvrdma_pd {
  112. struct ib_pd ibpd;
  113. u32 pdn;
  114. u32 pd_handle;
  115. int privileged;
  116. };
  117. struct pvrdma_mr {
  118. u32 mr_handle;
  119. u64 iova;
  120. u64 size;
  121. };
  122. struct pvrdma_user_mr {
  123. struct ib_mr ibmr;
  124. struct ib_umem *umem;
  125. struct pvrdma_mr mmr;
  126. struct pvrdma_page_dir pdir;
  127. u64 *pages;
  128. u32 npages;
  129. u32 max_pages;
  130. u32 page_shift;
  131. };
  132. struct pvrdma_wq {
  133. struct pvrdma_ring *ring;
  134. spinlock_t lock; /* Work queue lock. */
  135. int wqe_cnt;
  136. int wqe_size;
  137. int max_sg;
  138. int offset;
  139. };
  140. struct pvrdma_ah {
  141. struct ib_ah ibah;
  142. struct pvrdma_av av;
  143. };
  144. struct pvrdma_qp {
  145. struct ib_qp ibqp;
  146. u32 qp_handle;
  147. u32 qkey;
  148. struct pvrdma_wq sq;
  149. struct pvrdma_wq rq;
  150. struct ib_umem *rumem;
  151. struct ib_umem *sumem;
  152. struct pvrdma_page_dir pdir;
  153. int npages;
  154. int npages_send;
  155. int npages_recv;
  156. u32 flags;
  157. u8 port;
  158. u8 state;
  159. bool is_kernel;
  160. struct mutex mutex; /* QP state mutex. */
  161. atomic_t refcnt;
  162. wait_queue_head_t wait;
  163. };
  164. struct pvrdma_dev {
  165. /* PCI device-related information. */
  166. struct ib_device ib_dev;
  167. struct pci_dev *pdev;
  168. void __iomem *regs;
  169. struct pvrdma_device_shared_region *dsr; /* Shared region pointer */
  170. dma_addr_t dsrbase; /* Shared region base address */
  171. void *cmd_slot;
  172. void *resp_slot;
  173. unsigned long flags;
  174. struct list_head device_link;
  175. /* Locking and interrupt information. */
  176. spinlock_t cmd_lock; /* Command lock. */
  177. struct semaphore cmd_sema;
  178. struct completion cmd_done;
  179. struct {
  180. enum pvrdma_intr_type type; /* Intr type */
  181. struct msix_entry msix_entry[PVRDMA_MAX_INTERRUPTS];
  182. irq_handler_t handler[PVRDMA_MAX_INTERRUPTS];
  183. u8 enabled[PVRDMA_MAX_INTERRUPTS];
  184. u8 size;
  185. } intr;
  186. /* RDMA-related device information. */
  187. union ib_gid *sgid_tbl;
  188. struct pvrdma_ring_state *async_ring_state;
  189. struct pvrdma_page_dir async_pdir;
  190. struct pvrdma_ring_state *cq_ring_state;
  191. struct pvrdma_page_dir cq_pdir;
  192. struct pvrdma_cq **cq_tbl;
  193. spinlock_t cq_tbl_lock;
  194. struct pvrdma_qp **qp_tbl;
  195. spinlock_t qp_tbl_lock;
  196. struct pvrdma_uar_table uar_table;
  197. struct pvrdma_uar_map driver_uar;
  198. __be64 sys_image_guid;
  199. spinlock_t desc_lock; /* Device modification lock. */
  200. u32 port_cap_mask;
  201. struct mutex port_mutex; /* Port modification mutex. */
  202. bool ib_active;
  203. atomic_t num_qps;
  204. atomic_t num_cqs;
  205. atomic_t num_pds;
  206. atomic_t num_ahs;
  207. /* Network device information. */
  208. struct net_device *netdev;
  209. struct notifier_block nb_netdev;
  210. };
  211. struct pvrdma_netdevice_work {
  212. struct work_struct work;
  213. struct net_device *event_netdev;
  214. unsigned long event;
  215. };
  216. static inline struct pvrdma_dev *to_vdev(struct ib_device *ibdev)
  217. {
  218. return container_of(ibdev, struct pvrdma_dev, ib_dev);
  219. }
  220. static inline struct
  221. pvrdma_ucontext *to_vucontext(struct ib_ucontext *ibucontext)
  222. {
  223. return container_of(ibucontext, struct pvrdma_ucontext, ibucontext);
  224. }
  225. static inline struct pvrdma_pd *to_vpd(struct ib_pd *ibpd)
  226. {
  227. return container_of(ibpd, struct pvrdma_pd, ibpd);
  228. }
  229. static inline struct pvrdma_cq *to_vcq(struct ib_cq *ibcq)
  230. {
  231. return container_of(ibcq, struct pvrdma_cq, ibcq);
  232. }
  233. static inline struct pvrdma_user_mr *to_vmr(struct ib_mr *ibmr)
  234. {
  235. return container_of(ibmr, struct pvrdma_user_mr, ibmr);
  236. }
  237. static inline struct pvrdma_qp *to_vqp(struct ib_qp *ibqp)
  238. {
  239. return container_of(ibqp, struct pvrdma_qp, ibqp);
  240. }
  241. static inline struct pvrdma_ah *to_vah(struct ib_ah *ibah)
  242. {
  243. return container_of(ibah, struct pvrdma_ah, ibah);
  244. }
  245. static inline void pvrdma_write_reg(struct pvrdma_dev *dev, u32 reg, u32 val)
  246. {
  247. writel(cpu_to_le32(val), dev->regs + reg);
  248. }
  249. static inline u32 pvrdma_read_reg(struct pvrdma_dev *dev, u32 reg)
  250. {
  251. return le32_to_cpu(readl(dev->regs + reg));
  252. }
  253. static inline void pvrdma_write_uar_cq(struct pvrdma_dev *dev, u32 val)
  254. {
  255. writel(cpu_to_le32(val), dev->driver_uar.map + PVRDMA_UAR_CQ_OFFSET);
  256. }
  257. static inline void pvrdma_write_uar_qp(struct pvrdma_dev *dev, u32 val)
  258. {
  259. writel(cpu_to_le32(val), dev->driver_uar.map + PVRDMA_UAR_QP_OFFSET);
  260. }
  261. static inline void *pvrdma_page_dir_get_ptr(struct pvrdma_page_dir *pdir,
  262. u64 offset)
  263. {
  264. return pdir->pages[offset / PAGE_SIZE] + (offset % PAGE_SIZE);
  265. }
  266. static inline enum pvrdma_mtu ib_mtu_to_pvrdma(enum ib_mtu mtu)
  267. {
  268. return (enum pvrdma_mtu)mtu;
  269. }
  270. static inline enum ib_mtu pvrdma_mtu_to_ib(enum pvrdma_mtu mtu)
  271. {
  272. return (enum ib_mtu)mtu;
  273. }
  274. static inline enum pvrdma_port_state ib_port_state_to_pvrdma(
  275. enum ib_port_state state)
  276. {
  277. return (enum pvrdma_port_state)state;
  278. }
  279. static inline enum ib_port_state pvrdma_port_state_to_ib(
  280. enum pvrdma_port_state state)
  281. {
  282. return (enum ib_port_state)state;
  283. }
  284. static inline int ib_port_cap_flags_to_pvrdma(int flags)
  285. {
  286. return flags & PVRDMA_MASK(PVRDMA_PORT_CAP_FLAGS_MAX);
  287. }
  288. static inline int pvrdma_port_cap_flags_to_ib(int flags)
  289. {
  290. return flags;
  291. }
  292. static inline enum pvrdma_port_width ib_port_width_to_pvrdma(
  293. enum ib_port_width width)
  294. {
  295. return (enum pvrdma_port_width)width;
  296. }
  297. static inline enum ib_port_width pvrdma_port_width_to_ib(
  298. enum pvrdma_port_width width)
  299. {
  300. return (enum ib_port_width)width;
  301. }
  302. static inline enum pvrdma_port_speed ib_port_speed_to_pvrdma(
  303. enum ib_port_speed speed)
  304. {
  305. return (enum pvrdma_port_speed)speed;
  306. }
  307. static inline enum ib_port_speed pvrdma_port_speed_to_ib(
  308. enum pvrdma_port_speed speed)
  309. {
  310. return (enum ib_port_speed)speed;
  311. }
  312. static inline int pvrdma_qp_attr_mask_to_ib(int attr_mask)
  313. {
  314. return attr_mask;
  315. }
  316. static inline int ib_qp_attr_mask_to_pvrdma(int attr_mask)
  317. {
  318. return attr_mask & PVRDMA_MASK(PVRDMA_QP_ATTR_MASK_MAX);
  319. }
  320. static inline enum pvrdma_mig_state ib_mig_state_to_pvrdma(
  321. enum ib_mig_state state)
  322. {
  323. return (enum pvrdma_mig_state)state;
  324. }
  325. static inline enum ib_mig_state pvrdma_mig_state_to_ib(
  326. enum pvrdma_mig_state state)
  327. {
  328. return (enum ib_mig_state)state;
  329. }
  330. static inline int ib_access_flags_to_pvrdma(int flags)
  331. {
  332. return flags;
  333. }
  334. static inline int pvrdma_access_flags_to_ib(int flags)
  335. {
  336. return flags & PVRDMA_MASK(PVRDMA_ACCESS_FLAGS_MAX);
  337. }
  338. static inline enum pvrdma_qp_type ib_qp_type_to_pvrdma(enum ib_qp_type type)
  339. {
  340. return (enum pvrdma_qp_type)type;
  341. }
  342. static inline enum ib_qp_type pvrdma_qp_type_to_ib(enum pvrdma_qp_type type)
  343. {
  344. return (enum ib_qp_type)type;
  345. }
  346. static inline enum pvrdma_qp_state ib_qp_state_to_pvrdma(enum ib_qp_state state)
  347. {
  348. return (enum pvrdma_qp_state)state;
  349. }
  350. static inline enum ib_qp_state pvrdma_qp_state_to_ib(enum pvrdma_qp_state state)
  351. {
  352. return (enum ib_qp_state)state;
  353. }
  354. static inline enum pvrdma_wr_opcode ib_wr_opcode_to_pvrdma(enum ib_wr_opcode op)
  355. {
  356. return (enum pvrdma_wr_opcode)op;
  357. }
  358. static inline enum ib_wc_status pvrdma_wc_status_to_ib(
  359. enum pvrdma_wc_status status)
  360. {
  361. return (enum ib_wc_status)status;
  362. }
  363. static inline int pvrdma_wc_opcode_to_ib(int opcode)
  364. {
  365. return opcode;
  366. }
  367. static inline int pvrdma_wc_flags_to_ib(int flags)
  368. {
  369. return flags;
  370. }
  371. static inline int ib_send_flags_to_pvrdma(int flags)
  372. {
  373. return flags & PVRDMA_MASK(PVRDMA_SEND_FLAGS_MAX);
  374. }
  375. void pvrdma_qp_cap_to_ib(struct ib_qp_cap *dst,
  376. const struct pvrdma_qp_cap *src);
  377. void ib_qp_cap_to_pvrdma(struct pvrdma_qp_cap *dst,
  378. const struct ib_qp_cap *src);
  379. void pvrdma_gid_to_ib(union ib_gid *dst, const union pvrdma_gid *src);
  380. void ib_gid_to_pvrdma(union pvrdma_gid *dst, const union ib_gid *src);
  381. void pvrdma_global_route_to_ib(struct ib_global_route *dst,
  382. const struct pvrdma_global_route *src);
  383. void ib_global_route_to_pvrdma(struct pvrdma_global_route *dst,
  384. const struct ib_global_route *src);
  385. void pvrdma_ah_attr_to_ib(struct ib_ah_attr *dst,
  386. const struct pvrdma_ah_attr *src);
  387. void ib_ah_attr_to_pvrdma(struct pvrdma_ah_attr *dst,
  388. const struct ib_ah_attr *src);
  389. int pvrdma_uar_table_init(struct pvrdma_dev *dev);
  390. void pvrdma_uar_table_cleanup(struct pvrdma_dev *dev);
  391. int pvrdma_uar_alloc(struct pvrdma_dev *dev, struct pvrdma_uar_map *uar);
  392. void pvrdma_uar_free(struct pvrdma_dev *dev, struct pvrdma_uar_map *uar);
  393. void _pvrdma_flush_cqe(struct pvrdma_qp *qp, struct pvrdma_cq *cq);
  394. int pvrdma_page_dir_init(struct pvrdma_dev *dev, struct pvrdma_page_dir *pdir,
  395. u64 npages, bool alloc_pages);
  396. void pvrdma_page_dir_cleanup(struct pvrdma_dev *dev,
  397. struct pvrdma_page_dir *pdir);
  398. int pvrdma_page_dir_insert_dma(struct pvrdma_page_dir *pdir, u64 idx,
  399. dma_addr_t daddr);
  400. int pvrdma_page_dir_insert_umem(struct pvrdma_page_dir *pdir,
  401. struct ib_umem *umem, u64 offset);
  402. dma_addr_t pvrdma_page_dir_get_dma(struct pvrdma_page_dir *pdir, u64 idx);
  403. int pvrdma_page_dir_insert_page_list(struct pvrdma_page_dir *pdir,
  404. u64 *page_list, int num_pages);
  405. int pvrdma_cmd_post(struct pvrdma_dev *dev, union pvrdma_cmd_req *req,
  406. union pvrdma_cmd_resp *rsp, unsigned resp_code);
  407. #endif /* __PVRDMA_H__ */