2 * Copyright (c) 2012-2016 VMware, Inc. All rights reserved.
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.
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.
17 * The BSD 2-Clause License
19 * Redistribution and use in source and binary forms, with or
20 * without modification, are permitted provided that the following
23 * - Redistributions of source code must retain the above
24 * copyright notice, this list of conditions and the following
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.
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.
49 #include <linux/compiler.h>
50 #include <linux/interrupt.h>
51 #include <linux/list.h>
52 #include <linux/mutex.h>
53 #include <linux/pci.h>
54 #include <linux/semaphore.h>
55 #include <linux/workqueue.h>
56 #include <rdma/ib_umem.h>
57 #include <rdma/ib_verbs.h>
58 #include <rdma/vmw_pvrdma-abi.h>
60 #include "pvrdma_ring.h"
61 #include "pvrdma_dev_api.h"
62 #include "pvrdma_verbs.h"
64 /* NOT the same as BIT_MASK(). */
65 #define PVRDMA_MASK(n) ((n << 1) - 1)
68 * VMware PVRDMA PCI device id.
70 #define PCI_DEVICE_ID_VMWARE_PVRDMA 0x0820
72 #define PVRDMA_NUM_RING_PAGES 4
73 #define PVRDMA_QP_NUM_HEADER_PAGES 1
77 struct pvrdma_page_dir {
89 spinlock_t cq_lock; /* Poll lock. */
90 struct pvrdma_uar_map *uar;
92 struct pvrdma_ring_state *ring_state;
93 struct pvrdma_page_dir pdir;
97 wait_queue_head_t wait;
100 struct pvrdma_id_table {
105 spinlock_t lock; /* Table lock. */
106 unsigned long *table;
109 struct pvrdma_uar_map {
115 struct pvrdma_uar_table {
116 struct pvrdma_id_table tbl;
120 struct pvrdma_ucontext {
121 struct ib_ucontext ibucontext;
122 struct pvrdma_dev *dev;
123 struct pvrdma_uar_map uar;
140 struct pvrdma_user_mr {
142 struct ib_umem *umem;
143 struct pvrdma_mr mmr;
144 struct pvrdma_page_dir pdir;
152 struct pvrdma_ring *ring;
153 spinlock_t lock; /* Work queue lock. */
171 struct ib_umem *rumem;
172 struct ib_umem *sumem;
173 struct pvrdma_page_dir pdir;
181 struct mutex mutex; /* QP state mutex. */
183 wait_queue_head_t wait;
187 /* PCI device-related information. */
188 struct ib_device ib_dev;
189 struct pci_dev *pdev;
191 struct pvrdma_device_shared_region *dsr; /* Shared region pointer */
192 dma_addr_t dsrbase; /* Shared region base address */
196 struct list_head device_link;
198 /* Locking and interrupt information. */
199 spinlock_t cmd_lock; /* Command lock. */
200 struct semaphore cmd_sema;
201 struct completion cmd_done;
202 unsigned int nr_vectors;
204 /* RDMA-related device information. */
205 union ib_gid *sgid_tbl;
206 struct pvrdma_ring_state *async_ring_state;
207 struct pvrdma_page_dir async_pdir;
208 struct pvrdma_ring_state *cq_ring_state;
209 struct pvrdma_page_dir cq_pdir;
210 struct pvrdma_cq **cq_tbl;
211 spinlock_t cq_tbl_lock;
212 struct pvrdma_qp **qp_tbl;
213 spinlock_t qp_tbl_lock;
214 struct pvrdma_uar_table uar_table;
215 struct pvrdma_uar_map driver_uar;
216 __be64 sys_image_guid;
217 spinlock_t desc_lock; /* Device modification lock. */
219 struct mutex port_mutex; /* Port modification mutex. */
226 /* Network device information. */
227 struct net_device *netdev;
228 struct notifier_block nb_netdev;
231 struct pvrdma_netdevice_work {
232 struct work_struct work;
233 struct net_device *event_netdev;
237 static inline struct pvrdma_dev *to_vdev(struct ib_device *ibdev)
239 return container_of(ibdev, struct pvrdma_dev, ib_dev);
243 pvrdma_ucontext *to_vucontext(struct ib_ucontext *ibucontext)
245 return container_of(ibucontext, struct pvrdma_ucontext, ibucontext);
248 static inline struct pvrdma_pd *to_vpd(struct ib_pd *ibpd)
250 return container_of(ibpd, struct pvrdma_pd, ibpd);
253 static inline struct pvrdma_cq *to_vcq(struct ib_cq *ibcq)
255 return container_of(ibcq, struct pvrdma_cq, ibcq);
258 static inline struct pvrdma_user_mr *to_vmr(struct ib_mr *ibmr)
260 return container_of(ibmr, struct pvrdma_user_mr, ibmr);
263 static inline struct pvrdma_qp *to_vqp(struct ib_qp *ibqp)
265 return container_of(ibqp, struct pvrdma_qp, ibqp);
268 static inline struct pvrdma_ah *to_vah(struct ib_ah *ibah)
270 return container_of(ibah, struct pvrdma_ah, ibah);
273 static inline void pvrdma_write_reg(struct pvrdma_dev *dev, u32 reg, u32 val)
275 writel(cpu_to_le32(val), dev->regs + reg);
278 static inline u32 pvrdma_read_reg(struct pvrdma_dev *dev, u32 reg)
280 return le32_to_cpu(readl(dev->regs + reg));
283 static inline void pvrdma_write_uar_cq(struct pvrdma_dev *dev, u32 val)
285 writel(cpu_to_le32(val), dev->driver_uar.map + PVRDMA_UAR_CQ_OFFSET);
288 static inline void pvrdma_write_uar_qp(struct pvrdma_dev *dev, u32 val)
290 writel(cpu_to_le32(val), dev->driver_uar.map + PVRDMA_UAR_QP_OFFSET);
293 static inline void *pvrdma_page_dir_get_ptr(struct pvrdma_page_dir *pdir,
296 return pdir->pages[offset / PAGE_SIZE] + (offset % PAGE_SIZE);
299 static inline enum pvrdma_mtu ib_mtu_to_pvrdma(enum ib_mtu mtu)
301 return (enum pvrdma_mtu)mtu;
304 static inline enum ib_mtu pvrdma_mtu_to_ib(enum pvrdma_mtu mtu)
306 return (enum ib_mtu)mtu;
309 static inline enum pvrdma_port_state ib_port_state_to_pvrdma(
310 enum ib_port_state state)
312 return (enum pvrdma_port_state)state;
315 static inline enum ib_port_state pvrdma_port_state_to_ib(
316 enum pvrdma_port_state state)
318 return (enum ib_port_state)state;
321 static inline int ib_port_cap_flags_to_pvrdma(int flags)
323 return flags & PVRDMA_MASK(PVRDMA_PORT_CAP_FLAGS_MAX);
326 static inline int pvrdma_port_cap_flags_to_ib(int flags)
331 static inline enum pvrdma_port_width ib_port_width_to_pvrdma(
332 enum ib_port_width width)
334 return (enum pvrdma_port_width)width;
337 static inline enum ib_port_width pvrdma_port_width_to_ib(
338 enum pvrdma_port_width width)
340 return (enum ib_port_width)width;
343 static inline enum pvrdma_port_speed ib_port_speed_to_pvrdma(
344 enum ib_port_speed speed)
346 return (enum pvrdma_port_speed)speed;
349 static inline enum ib_port_speed pvrdma_port_speed_to_ib(
350 enum pvrdma_port_speed speed)
352 return (enum ib_port_speed)speed;
355 static inline int pvrdma_qp_attr_mask_to_ib(int attr_mask)
360 static inline int ib_qp_attr_mask_to_pvrdma(int attr_mask)
362 return attr_mask & PVRDMA_MASK(PVRDMA_QP_ATTR_MASK_MAX);
365 static inline enum pvrdma_mig_state ib_mig_state_to_pvrdma(
366 enum ib_mig_state state)
368 return (enum pvrdma_mig_state)state;
371 static inline enum ib_mig_state pvrdma_mig_state_to_ib(
372 enum pvrdma_mig_state state)
374 return (enum ib_mig_state)state;
377 static inline int ib_access_flags_to_pvrdma(int flags)
382 static inline int pvrdma_access_flags_to_ib(int flags)
384 return flags & PVRDMA_MASK(PVRDMA_ACCESS_FLAGS_MAX);
387 static inline enum pvrdma_qp_type ib_qp_type_to_pvrdma(enum ib_qp_type type)
389 return (enum pvrdma_qp_type)type;
392 static inline enum ib_qp_type pvrdma_qp_type_to_ib(enum pvrdma_qp_type type)
394 return (enum ib_qp_type)type;
397 static inline enum pvrdma_qp_state ib_qp_state_to_pvrdma(enum ib_qp_state state)
399 return (enum pvrdma_qp_state)state;
402 static inline enum ib_qp_state pvrdma_qp_state_to_ib(enum pvrdma_qp_state state)
404 return (enum ib_qp_state)state;
407 static inline enum pvrdma_wr_opcode ib_wr_opcode_to_pvrdma(enum ib_wr_opcode op)
409 return (enum pvrdma_wr_opcode)op;
412 static inline enum ib_wc_status pvrdma_wc_status_to_ib(
413 enum pvrdma_wc_status status)
415 return (enum ib_wc_status)status;
418 static inline int pvrdma_wc_opcode_to_ib(int opcode)
423 static inline int pvrdma_wc_flags_to_ib(int flags)
428 static inline int ib_send_flags_to_pvrdma(int flags)
430 return flags & PVRDMA_MASK(PVRDMA_SEND_FLAGS_MAX);
433 void pvrdma_qp_cap_to_ib(struct ib_qp_cap *dst,
434 const struct pvrdma_qp_cap *src);
435 void ib_qp_cap_to_pvrdma(struct pvrdma_qp_cap *dst,
436 const struct ib_qp_cap *src);
437 void pvrdma_gid_to_ib(union ib_gid *dst, const union pvrdma_gid *src);
438 void ib_gid_to_pvrdma(union pvrdma_gid *dst, const union ib_gid *src);
439 void pvrdma_global_route_to_ib(struct ib_global_route *dst,
440 const struct pvrdma_global_route *src);
441 void ib_global_route_to_pvrdma(struct pvrdma_global_route *dst,
442 const struct ib_global_route *src);
443 void pvrdma_ah_attr_to_ib(struct ib_ah_attr *dst,
444 const struct pvrdma_ah_attr *src);
445 void ib_ah_attr_to_pvrdma(struct pvrdma_ah_attr *dst,
446 const struct ib_ah_attr *src);
448 int pvrdma_uar_table_init(struct pvrdma_dev *dev);
449 void pvrdma_uar_table_cleanup(struct pvrdma_dev *dev);
451 int pvrdma_uar_alloc(struct pvrdma_dev *dev, struct pvrdma_uar_map *uar);
452 void pvrdma_uar_free(struct pvrdma_dev *dev, struct pvrdma_uar_map *uar);
454 void _pvrdma_flush_cqe(struct pvrdma_qp *qp, struct pvrdma_cq *cq);
456 int pvrdma_page_dir_init(struct pvrdma_dev *dev, struct pvrdma_page_dir *pdir,
457 u64 npages, bool alloc_pages);
458 void pvrdma_page_dir_cleanup(struct pvrdma_dev *dev,
459 struct pvrdma_page_dir *pdir);
460 int pvrdma_page_dir_insert_dma(struct pvrdma_page_dir *pdir, u64 idx,
462 int pvrdma_page_dir_insert_umem(struct pvrdma_page_dir *pdir,
463 struct ib_umem *umem, u64 offset);
464 dma_addr_t pvrdma_page_dir_get_dma(struct pvrdma_page_dir *pdir, u64 idx);
465 int pvrdma_page_dir_insert_page_list(struct pvrdma_page_dir *pdir,
466 u64 *page_list, int num_pages);
468 int pvrdma_cmd_post(struct pvrdma_dev *dev, union pvrdma_cmd_req *req,
469 union pvrdma_cmd_resp *rsp, unsigned resp_code);
471 #endif /* __PVRDMA_H__ */