xref: /linux/drivers/infiniband/hw/vmw_pvrdma/pvrdma_main.c (revision 189f164e573e18d9f8876dbd3ad8fcbe11f93037)
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 
46 #include <linux/errno.h>
47 #include <linux/inetdevice.h>
48 #include <linux/init.h>
49 #include <linux/module.h>
50 #include <linux/slab.h>
51 #include <rdma/ib_addr.h>
52 #include <rdma/ib_smi.h>
53 #include <rdma/ib_user_verbs.h>
54 #include <net/addrconf.h>
55 
56 #include "pvrdma.h"
57 
58 #define DRV_NAME	"vmw_pvrdma"
59 #define DRV_VERSION	"1.0.1.0-k"
60 
61 static DEFINE_MUTEX(pvrdma_device_list_lock);
62 static LIST_HEAD(pvrdma_device_list);
63 static struct workqueue_struct *event_wq;
64 
65 static int pvrdma_add_gid(const struct ib_gid_attr *attr, void **context);
66 static int pvrdma_del_gid(const struct ib_gid_attr *attr, void **context);
67 
hca_type_show(struct device * device,struct device_attribute * attr,char * buf)68 static ssize_t hca_type_show(struct device *device,
69 			     struct device_attribute *attr, char *buf)
70 {
71 	return sysfs_emit(buf, "VMW_PVRDMA-%s\n", DRV_VERSION);
72 }
73 static DEVICE_ATTR_RO(hca_type);
74 
hw_rev_show(struct device * device,struct device_attribute * attr,char * buf)75 static ssize_t hw_rev_show(struct device *device,
76 			   struct device_attribute *attr, char *buf)
77 {
78 	return sysfs_emit(buf, "%d\n", PVRDMA_REV_ID);
79 }
80 static DEVICE_ATTR_RO(hw_rev);
81 
board_id_show(struct device * device,struct device_attribute * attr,char * buf)82 static ssize_t board_id_show(struct device *device,
83 			     struct device_attribute *attr, char *buf)
84 {
85 	return sysfs_emit(buf, "%d\n", PVRDMA_BOARD_ID);
86 }
87 static DEVICE_ATTR_RO(board_id);
88 
89 static struct attribute *pvrdma_class_attributes[] = {
90 	&dev_attr_hw_rev.attr,
91 	&dev_attr_hca_type.attr,
92 	&dev_attr_board_id.attr,
93 	NULL,
94 };
95 
96 static const struct attribute_group pvrdma_attr_group = {
97 	.attrs = pvrdma_class_attributes,
98 };
99 
pvrdma_get_fw_ver_str(struct ib_device * device,char * str)100 static void pvrdma_get_fw_ver_str(struct ib_device *device, char *str)
101 {
102 	struct pvrdma_dev *dev =
103 		container_of(device, struct pvrdma_dev, ib_dev);
104 	snprintf(str, IB_FW_VERSION_NAME_MAX, "%d.%d.%d\n",
105 		 (int) (dev->dsr->caps.fw_ver >> 32),
106 		 (int) (dev->dsr->caps.fw_ver >> 16) & 0xffff,
107 		 (int) dev->dsr->caps.fw_ver & 0xffff);
108 }
109 
pvrdma_init_device(struct pvrdma_dev * dev)110 static int pvrdma_init_device(struct pvrdma_dev *dev)
111 {
112 	/*  Initialize some device related stuff */
113 	spin_lock_init(&dev->cmd_lock);
114 	sema_init(&dev->cmd_sema, 1);
115 	atomic_set(&dev->num_qps, 0);
116 	atomic_set(&dev->num_srqs, 0);
117 	atomic_set(&dev->num_cqs, 0);
118 	atomic_set(&dev->num_pds, 0);
119 	atomic_set(&dev->num_ahs, 0);
120 
121 	return 0;
122 }
123 
pvrdma_port_immutable(struct ib_device * ibdev,u32 port_num,struct ib_port_immutable * immutable)124 static int pvrdma_port_immutable(struct ib_device *ibdev, u32 port_num,
125 				 struct ib_port_immutable *immutable)
126 {
127 	struct pvrdma_dev *dev = to_vdev(ibdev);
128 	struct ib_port_attr attr;
129 	int err;
130 
131 	if (dev->dsr->caps.gid_types == PVRDMA_GID_TYPE_FLAG_ROCE_V1)
132 		immutable->core_cap_flags |= RDMA_CORE_PORT_IBA_ROCE;
133 	else if (dev->dsr->caps.gid_types == PVRDMA_GID_TYPE_FLAG_ROCE_V2)
134 		immutable->core_cap_flags |= RDMA_CORE_PORT_IBA_ROCE_UDP_ENCAP;
135 
136 	err = ib_query_port(ibdev, port_num, &attr);
137 	if (err)
138 		return err;
139 
140 	immutable->pkey_tbl_len = attr.pkey_tbl_len;
141 	immutable->gid_tbl_len = attr.gid_tbl_len;
142 	immutable->max_mad_size = IB_MGMT_MAD_SIZE;
143 	return 0;
144 }
145 
pvrdma_dispatch_event(struct pvrdma_dev * dev,int port,enum ib_event_type event)146 static void pvrdma_dispatch_event(struct pvrdma_dev *dev, int port,
147 				  enum ib_event_type event)
148 {
149 	struct ib_event ib_event;
150 
151 	memset(&ib_event, 0, sizeof(ib_event));
152 	ib_event.device = &dev->ib_dev;
153 	ib_event.element.port_num = port;
154 	ib_event.event = event;
155 	ib_dispatch_event(&ib_event);
156 }
157 
pvrdma_report_event_handle(struct ib_device * ibdev,struct net_device * ndev,unsigned long event)158 static void pvrdma_report_event_handle(struct ib_device *ibdev,
159 				       struct net_device *ndev,
160 				       unsigned long event)
161 {
162 	struct pvrdma_dev *dev = container_of(ibdev, struct pvrdma_dev, ib_dev);
163 
164 	switch (event) {
165 	case NETDEV_DOWN:
166 		pvrdma_dispatch_event(dev, 1, IB_EVENT_PORT_ERR);
167 		break;
168 	case NETDEV_UP:
169 		pvrdma_write_reg(dev, PVRDMA_REG_CTL,
170 				 PVRDMA_DEVICE_CTL_UNQUIESCE);
171 
172 		mb();
173 
174 		if (pvrdma_read_reg(dev, PVRDMA_REG_ERR))
175 			dev_err(&dev->pdev->dev,
176 				"failed to activate device during link up\n");
177 		else
178 			pvrdma_dispatch_event(dev, 1, IB_EVENT_PORT_ACTIVE);
179 		break;
180 
181 	default:
182 		break;
183 	}
184 }
185 
186 static const struct ib_device_ops pvrdma_dev_ops = {
187 	.owner = THIS_MODULE,
188 	.driver_id = RDMA_DRIVER_VMW_PVRDMA,
189 	.uverbs_abi_ver = PVRDMA_UVERBS_ABI_VERSION,
190 
191 	.add_gid = pvrdma_add_gid,
192 	.alloc_mr = pvrdma_alloc_mr,
193 	.alloc_pd = pvrdma_alloc_pd,
194 	.alloc_ucontext = pvrdma_alloc_ucontext,
195 	.create_ah = pvrdma_create_ah,
196 	.create_cq = pvrdma_create_cq,
197 	.create_qp = pvrdma_create_qp,
198 	.dealloc_pd = pvrdma_dealloc_pd,
199 	.dealloc_ucontext = pvrdma_dealloc_ucontext,
200 	.del_gid = pvrdma_del_gid,
201 	.dereg_mr = pvrdma_dereg_mr,
202 	.destroy_ah = pvrdma_destroy_ah,
203 	.destroy_cq = pvrdma_destroy_cq,
204 	.destroy_qp = pvrdma_destroy_qp,
205 	.device_group = &pvrdma_attr_group,
206 	.get_dev_fw_str = pvrdma_get_fw_ver_str,
207 	.get_dma_mr = pvrdma_get_dma_mr,
208 	.get_link_layer = pvrdma_port_link_layer,
209 	.get_port_immutable = pvrdma_port_immutable,
210 	.map_mr_sg = pvrdma_map_mr_sg,
211 	.mmap = pvrdma_mmap,
212 	.modify_port = pvrdma_modify_port,
213 	.modify_qp = pvrdma_modify_qp,
214 	.poll_cq = pvrdma_poll_cq,
215 	.post_recv = pvrdma_post_recv,
216 	.post_send = pvrdma_post_send,
217 	.query_device = pvrdma_query_device,
218 	.query_gid = pvrdma_query_gid,
219 	.query_pkey = pvrdma_query_pkey,
220 	.query_port = pvrdma_query_port,
221 	.query_qp = pvrdma_query_qp,
222 	.reg_user_mr = pvrdma_reg_user_mr,
223 	.req_notify_cq = pvrdma_req_notify_cq,
224 	.report_port_event = pvrdma_report_event_handle,
225 
226 	INIT_RDMA_OBJ_SIZE(ib_ah, pvrdma_ah, ibah),
227 	INIT_RDMA_OBJ_SIZE(ib_cq, pvrdma_cq, ibcq),
228 	INIT_RDMA_OBJ_SIZE(ib_pd, pvrdma_pd, ibpd),
229 	INIT_RDMA_OBJ_SIZE(ib_qp, pvrdma_qp, ibqp),
230 	INIT_RDMA_OBJ_SIZE(ib_ucontext, pvrdma_ucontext, ibucontext),
231 };
232 
233 static const struct ib_device_ops pvrdma_dev_srq_ops = {
234 	.create_srq = pvrdma_create_srq,
235 	.destroy_srq = pvrdma_destroy_srq,
236 	.modify_srq = pvrdma_modify_srq,
237 	.query_srq = pvrdma_query_srq,
238 
239 	INIT_RDMA_OBJ_SIZE(ib_srq, pvrdma_srq, ibsrq),
240 };
241 
pvrdma_register_device(struct pvrdma_dev * dev)242 static int pvrdma_register_device(struct pvrdma_dev *dev)
243 {
244 	int ret = -1;
245 
246 	dev->ib_dev.node_guid = dev->dsr->caps.node_guid;
247 	dev->sys_image_guid = dev->dsr->caps.sys_image_guid;
248 	dev->flags = 0;
249 	dev->ib_dev.num_comp_vectors = 1;
250 	dev->ib_dev.dev.parent = &dev->pdev->dev;
251 
252 	dev->ib_dev.node_type = RDMA_NODE_IB_CA;
253 	dev->ib_dev.phys_port_cnt = dev->dsr->caps.phys_port_cnt;
254 
255 	ib_set_device_ops(&dev->ib_dev, &pvrdma_dev_ops);
256 
257 	mutex_init(&dev->port_mutex);
258 	spin_lock_init(&dev->desc_lock);
259 
260 	dev->cq_tbl = kzalloc_objs(struct pvrdma_cq *, dev->dsr->caps.max_cq);
261 	if (!dev->cq_tbl)
262 		return ret;
263 	spin_lock_init(&dev->cq_tbl_lock);
264 
265 	dev->qp_tbl = kzalloc_objs(struct pvrdma_qp *, dev->dsr->caps.max_qp);
266 	if (!dev->qp_tbl)
267 		goto err_cq_free;
268 	spin_lock_init(&dev->qp_tbl_lock);
269 
270 	/* Check if SRQ is supported by backend */
271 	if (dev->dsr->caps.max_srq) {
272 		ib_set_device_ops(&dev->ib_dev, &pvrdma_dev_srq_ops);
273 
274 		dev->srq_tbl = kzalloc_objs(struct pvrdma_srq *,
275 					    dev->dsr->caps.max_srq);
276 		if (!dev->srq_tbl)
277 			goto err_qp_free;
278 	}
279 	ret = ib_device_set_netdev(&dev->ib_dev, dev->netdev, 1);
280 	if (ret)
281 		goto err_srq_free;
282 	spin_lock_init(&dev->srq_tbl_lock);
283 
284 	ret = ib_register_device(&dev->ib_dev, "vmw_pvrdma%d", &dev->pdev->dev);
285 	if (ret)
286 		goto err_srq_free;
287 
288 	dev->ib_active = true;
289 
290 	return 0;
291 
292 err_srq_free:
293 	kfree(dev->srq_tbl);
294 err_qp_free:
295 	kfree(dev->qp_tbl);
296 err_cq_free:
297 	kfree(dev->cq_tbl);
298 
299 	return ret;
300 }
301 
pvrdma_intr0_handler(int irq,void * dev_id)302 static irqreturn_t pvrdma_intr0_handler(int irq, void *dev_id)
303 {
304 	u32 icr = PVRDMA_INTR_CAUSE_RESPONSE;
305 	struct pvrdma_dev *dev = dev_id;
306 
307 	dev_dbg(&dev->pdev->dev, "interrupt 0 (response) handler\n");
308 
309 	if (!dev->pdev->msix_enabled) {
310 		/* Legacy intr */
311 		icr = pvrdma_read_reg(dev, PVRDMA_REG_ICR);
312 		if (icr == 0)
313 			return IRQ_NONE;
314 	}
315 
316 	if (icr == PVRDMA_INTR_CAUSE_RESPONSE)
317 		complete(&dev->cmd_done);
318 
319 	return IRQ_HANDLED;
320 }
321 
pvrdma_qp_event(struct pvrdma_dev * dev,u32 qpn,int type)322 static void pvrdma_qp_event(struct pvrdma_dev *dev, u32 qpn, int type)
323 {
324 	struct pvrdma_qp *qp;
325 	unsigned long flags;
326 
327 	spin_lock_irqsave(&dev->qp_tbl_lock, flags);
328 	qp = dev->qp_tbl[qpn % dev->dsr->caps.max_qp];
329 	if (qp)
330 		refcount_inc(&qp->refcnt);
331 	spin_unlock_irqrestore(&dev->qp_tbl_lock, flags);
332 
333 	if (qp && qp->ibqp.event_handler) {
334 		struct ib_qp *ibqp = &qp->ibqp;
335 		struct ib_event e;
336 
337 		e.device = ibqp->device;
338 		e.element.qp = ibqp;
339 		e.event = type; /* 1:1 mapping for now. */
340 		ibqp->event_handler(&e, ibqp->qp_context);
341 	}
342 	if (qp) {
343 		if (refcount_dec_and_test(&qp->refcnt))
344 			complete(&qp->free);
345 	}
346 }
347 
pvrdma_cq_event(struct pvrdma_dev * dev,u32 cqn,int type)348 static void pvrdma_cq_event(struct pvrdma_dev *dev, u32 cqn, int type)
349 {
350 	struct pvrdma_cq *cq;
351 	unsigned long flags;
352 
353 	spin_lock_irqsave(&dev->cq_tbl_lock, flags);
354 	cq = dev->cq_tbl[cqn % dev->dsr->caps.max_cq];
355 	if (cq)
356 		refcount_inc(&cq->refcnt);
357 	spin_unlock_irqrestore(&dev->cq_tbl_lock, flags);
358 
359 	if (cq && cq->ibcq.event_handler) {
360 		struct ib_cq *ibcq = &cq->ibcq;
361 		struct ib_event e;
362 
363 		e.device = ibcq->device;
364 		e.element.cq = ibcq;
365 		e.event = type; /* 1:1 mapping for now. */
366 		ibcq->event_handler(&e, ibcq->cq_context);
367 	}
368 	if (cq) {
369 		if (refcount_dec_and_test(&cq->refcnt))
370 			complete(&cq->free);
371 	}
372 }
373 
pvrdma_srq_event(struct pvrdma_dev * dev,u32 srqn,int type)374 static void pvrdma_srq_event(struct pvrdma_dev *dev, u32 srqn, int type)
375 {
376 	struct pvrdma_srq *srq;
377 	unsigned long flags;
378 
379 	spin_lock_irqsave(&dev->srq_tbl_lock, flags);
380 	if (dev->srq_tbl)
381 		srq = dev->srq_tbl[srqn % dev->dsr->caps.max_srq];
382 	else
383 		srq = NULL;
384 	if (srq)
385 		refcount_inc(&srq->refcnt);
386 	spin_unlock_irqrestore(&dev->srq_tbl_lock, flags);
387 
388 	if (srq && srq->ibsrq.event_handler) {
389 		struct ib_srq *ibsrq = &srq->ibsrq;
390 		struct ib_event e;
391 
392 		e.device = ibsrq->device;
393 		e.element.srq = ibsrq;
394 		e.event = type; /* 1:1 mapping for now. */
395 		ibsrq->event_handler(&e, ibsrq->srq_context);
396 	}
397 	if (srq) {
398 		if (refcount_dec_and_test(&srq->refcnt))
399 			complete(&srq->free);
400 	}
401 }
402 
pvrdma_dev_event(struct pvrdma_dev * dev,u8 port,int type)403 static void pvrdma_dev_event(struct pvrdma_dev *dev, u8 port, int type)
404 {
405 	if (port < 1 || port > dev->dsr->caps.phys_port_cnt) {
406 		dev_warn(&dev->pdev->dev, "event on port %d\n", port);
407 		return;
408 	}
409 
410 	pvrdma_dispatch_event(dev, port, type);
411 }
412 
get_eqe(struct pvrdma_dev * dev,unsigned int i)413 static inline struct pvrdma_eqe *get_eqe(struct pvrdma_dev *dev, unsigned int i)
414 {
415 	return (struct pvrdma_eqe *)pvrdma_page_dir_get_ptr(
416 					&dev->async_pdir,
417 					PAGE_SIZE +
418 					sizeof(struct pvrdma_eqe) * i);
419 }
420 
pvrdma_intr1_handler(int irq,void * dev_id)421 static irqreturn_t pvrdma_intr1_handler(int irq, void *dev_id)
422 {
423 	struct pvrdma_dev *dev = dev_id;
424 	struct pvrdma_ring *ring = &dev->async_ring_state->rx;
425 	int ring_slots = (dev->dsr->async_ring_pages.num_pages - 1) *
426 			 PAGE_SIZE / sizeof(struct pvrdma_eqe);
427 	unsigned int head;
428 
429 	dev_dbg(&dev->pdev->dev, "interrupt 1 (async event) handler\n");
430 
431 	/*
432 	 * Don't process events until the IB device is registered. Otherwise
433 	 * we'll try to ib_dispatch_event() on an invalid device.
434 	 */
435 	if (!dev->ib_active)
436 		return IRQ_HANDLED;
437 
438 	while (pvrdma_idx_ring_has_data(ring, ring_slots, &head) > 0) {
439 		struct pvrdma_eqe *eqe;
440 
441 		eqe = get_eqe(dev, head);
442 
443 		switch (eqe->type) {
444 		case PVRDMA_EVENT_QP_FATAL:
445 		case PVRDMA_EVENT_QP_REQ_ERR:
446 		case PVRDMA_EVENT_QP_ACCESS_ERR:
447 		case PVRDMA_EVENT_COMM_EST:
448 		case PVRDMA_EVENT_SQ_DRAINED:
449 		case PVRDMA_EVENT_PATH_MIG:
450 		case PVRDMA_EVENT_PATH_MIG_ERR:
451 		case PVRDMA_EVENT_QP_LAST_WQE_REACHED:
452 			pvrdma_qp_event(dev, eqe->info, eqe->type);
453 			break;
454 
455 		case PVRDMA_EVENT_CQ_ERR:
456 			pvrdma_cq_event(dev, eqe->info, eqe->type);
457 			break;
458 
459 		case PVRDMA_EVENT_SRQ_ERR:
460 		case PVRDMA_EVENT_SRQ_LIMIT_REACHED:
461 			pvrdma_srq_event(dev, eqe->info, eqe->type);
462 			break;
463 
464 		case PVRDMA_EVENT_PORT_ACTIVE:
465 		case PVRDMA_EVENT_PORT_ERR:
466 		case PVRDMA_EVENT_LID_CHANGE:
467 		case PVRDMA_EVENT_PKEY_CHANGE:
468 		case PVRDMA_EVENT_SM_CHANGE:
469 		case PVRDMA_EVENT_CLIENT_REREGISTER:
470 		case PVRDMA_EVENT_GID_CHANGE:
471 			pvrdma_dev_event(dev, eqe->info, eqe->type);
472 			break;
473 
474 		case PVRDMA_EVENT_DEVICE_FATAL:
475 			pvrdma_dev_event(dev, 1, eqe->type);
476 			break;
477 
478 		default:
479 			break;
480 		}
481 
482 		pvrdma_idx_ring_inc(&ring->cons_head, ring_slots);
483 	}
484 
485 	return IRQ_HANDLED;
486 }
487 
get_cqne(struct pvrdma_dev * dev,unsigned int i)488 static inline struct pvrdma_cqne *get_cqne(struct pvrdma_dev *dev,
489 					   unsigned int i)
490 {
491 	return (struct pvrdma_cqne *)pvrdma_page_dir_get_ptr(
492 					&dev->cq_pdir,
493 					PAGE_SIZE +
494 					sizeof(struct pvrdma_cqne) * i);
495 }
496 
pvrdma_intrx_handler(int irq,void * dev_id)497 static irqreturn_t pvrdma_intrx_handler(int irq, void *dev_id)
498 {
499 	struct pvrdma_dev *dev = dev_id;
500 	struct pvrdma_ring *ring = &dev->cq_ring_state->rx;
501 	int ring_slots = (dev->dsr->cq_ring_pages.num_pages - 1) * PAGE_SIZE /
502 			 sizeof(struct pvrdma_cqne);
503 	unsigned int head;
504 
505 	dev_dbg(&dev->pdev->dev, "interrupt x (completion) handler\n");
506 
507 	while (pvrdma_idx_ring_has_data(ring, ring_slots, &head) > 0) {
508 		struct pvrdma_cqne *cqne;
509 		struct pvrdma_cq *cq;
510 
511 		cqne = get_cqne(dev, head);
512 		spin_lock(&dev->cq_tbl_lock);
513 		cq = dev->cq_tbl[cqne->info % dev->dsr->caps.max_cq];
514 		if (cq)
515 			refcount_inc(&cq->refcnt);
516 		spin_unlock(&dev->cq_tbl_lock);
517 
518 		if (cq && cq->ibcq.comp_handler)
519 			cq->ibcq.comp_handler(&cq->ibcq, cq->ibcq.cq_context);
520 		if (cq) {
521 			if (refcount_dec_and_test(&cq->refcnt))
522 				complete(&cq->free);
523 		}
524 		pvrdma_idx_ring_inc(&ring->cons_head, ring_slots);
525 	}
526 
527 	return IRQ_HANDLED;
528 }
529 
pvrdma_free_irq(struct pvrdma_dev * dev)530 static void pvrdma_free_irq(struct pvrdma_dev *dev)
531 {
532 	int i;
533 
534 	dev_dbg(&dev->pdev->dev, "freeing interrupts\n");
535 	for (i = 0; i < dev->nr_vectors; i++)
536 		free_irq(pci_irq_vector(dev->pdev, i), dev);
537 }
538 
pvrdma_enable_intrs(struct pvrdma_dev * dev)539 static void pvrdma_enable_intrs(struct pvrdma_dev *dev)
540 {
541 	dev_dbg(&dev->pdev->dev, "enable interrupts\n");
542 	pvrdma_write_reg(dev, PVRDMA_REG_IMR, 0);
543 }
544 
pvrdma_disable_intrs(struct pvrdma_dev * dev)545 static void pvrdma_disable_intrs(struct pvrdma_dev *dev)
546 {
547 	dev_dbg(&dev->pdev->dev, "disable interrupts\n");
548 	pvrdma_write_reg(dev, PVRDMA_REG_IMR, ~0);
549 }
550 
pvrdma_alloc_intrs(struct pvrdma_dev * dev)551 static int pvrdma_alloc_intrs(struct pvrdma_dev *dev)
552 {
553 	struct pci_dev *pdev = dev->pdev;
554 	int ret = 0, i;
555 
556 	ret = pci_alloc_irq_vectors(pdev, 1, PVRDMA_MAX_INTERRUPTS,
557 			PCI_IRQ_MSIX);
558 	if (ret < 0) {
559 		ret = pci_alloc_irq_vectors(pdev, 1, 1,
560 				PCI_IRQ_MSI | PCI_IRQ_INTX);
561 		if (ret < 0)
562 			return ret;
563 	}
564 	dev->nr_vectors = ret;
565 
566 	ret = request_irq(pci_irq_vector(dev->pdev, 0), pvrdma_intr0_handler,
567 			pdev->msix_enabled ? 0 : IRQF_SHARED, DRV_NAME, dev);
568 	if (ret) {
569 		dev_err(&dev->pdev->dev,
570 			"failed to request interrupt 0\n");
571 		goto out_free_vectors;
572 	}
573 
574 	for (i = 1; i < dev->nr_vectors; i++) {
575 		ret = request_irq(pci_irq_vector(dev->pdev, i),
576 				i == 1 ? pvrdma_intr1_handler :
577 					 pvrdma_intrx_handler,
578 				0, DRV_NAME, dev);
579 		if (ret) {
580 			dev_err(&dev->pdev->dev,
581 				"failed to request interrupt %d\n", i);
582 			goto free_irqs;
583 		}
584 	}
585 
586 	return 0;
587 
588 free_irqs:
589 	while (--i >= 0)
590 		free_irq(pci_irq_vector(dev->pdev, i), dev);
591 out_free_vectors:
592 	pci_free_irq_vectors(pdev);
593 	return ret;
594 }
595 
pvrdma_free_slots(struct pvrdma_dev * dev)596 static void pvrdma_free_slots(struct pvrdma_dev *dev)
597 {
598 	struct pci_dev *pdev = dev->pdev;
599 
600 	if (dev->resp_slot)
601 		dma_free_coherent(&pdev->dev, PAGE_SIZE, dev->resp_slot,
602 				  dev->dsr->resp_slot_dma);
603 	if (dev->cmd_slot)
604 		dma_free_coherent(&pdev->dev, PAGE_SIZE, dev->cmd_slot,
605 				  dev->dsr->cmd_slot_dma);
606 }
607 
pvrdma_add_gid_at_index(struct pvrdma_dev * dev,const union ib_gid * gid,u8 gid_type,int index)608 static int pvrdma_add_gid_at_index(struct pvrdma_dev *dev,
609 				   const union ib_gid *gid,
610 				   u8 gid_type,
611 				   int index)
612 {
613 	int ret;
614 	union pvrdma_cmd_req req;
615 	struct pvrdma_cmd_create_bind *cmd_bind = &req.create_bind;
616 
617 	if (!dev->sgid_tbl) {
618 		dev_warn(&dev->pdev->dev, "sgid table not initialized\n");
619 		return -EINVAL;
620 	}
621 
622 	memset(cmd_bind, 0, sizeof(*cmd_bind));
623 	cmd_bind->hdr.cmd = PVRDMA_CMD_CREATE_BIND;
624 	memcpy(cmd_bind->new_gid, gid->raw, 16);
625 	cmd_bind->mtu = ib_mtu_enum_to_int(IB_MTU_1024);
626 	cmd_bind->vlan = 0xfff;
627 	cmd_bind->index = index;
628 	cmd_bind->gid_type = gid_type;
629 
630 	ret = pvrdma_cmd_post(dev, &req, NULL, 0);
631 	if (ret < 0) {
632 		dev_warn(&dev->pdev->dev,
633 			 "could not create binding, error: %d\n", ret);
634 		return -EFAULT;
635 	}
636 	memcpy(&dev->sgid_tbl[index], gid, sizeof(*gid));
637 	return 0;
638 }
639 
pvrdma_add_gid(const struct ib_gid_attr * attr,void ** context)640 static int pvrdma_add_gid(const struct ib_gid_attr *attr, void **context)
641 {
642 	struct pvrdma_dev *dev = to_vdev(attr->device);
643 
644 	return pvrdma_add_gid_at_index(dev, &attr->gid,
645 				       ib_gid_type_to_pvrdma(attr->gid_type),
646 				       attr->index);
647 }
648 
pvrdma_del_gid_at_index(struct pvrdma_dev * dev,int index)649 static int pvrdma_del_gid_at_index(struct pvrdma_dev *dev, int index)
650 {
651 	int ret;
652 	union pvrdma_cmd_req req;
653 	struct pvrdma_cmd_destroy_bind *cmd_dest = &req.destroy_bind;
654 
655 	/* Update sgid table. */
656 	if (!dev->sgid_tbl) {
657 		dev_warn(&dev->pdev->dev, "sgid table not initialized\n");
658 		return -EINVAL;
659 	}
660 
661 	memset(cmd_dest, 0, sizeof(*cmd_dest));
662 	cmd_dest->hdr.cmd = PVRDMA_CMD_DESTROY_BIND;
663 	memcpy(cmd_dest->dest_gid, &dev->sgid_tbl[index], 16);
664 	cmd_dest->index = index;
665 
666 	ret = pvrdma_cmd_post(dev, &req, NULL, 0);
667 	if (ret < 0) {
668 		dev_warn(&dev->pdev->dev,
669 			 "could not destroy binding, error: %d\n", ret);
670 		return ret;
671 	}
672 	memset(&dev->sgid_tbl[index], 0, 16);
673 	return 0;
674 }
675 
pvrdma_del_gid(const struct ib_gid_attr * attr,void ** context)676 static int pvrdma_del_gid(const struct ib_gid_attr *attr, void **context)
677 {
678 	struct pvrdma_dev *dev = to_vdev(attr->device);
679 
680 	dev_dbg(&dev->pdev->dev, "removing gid at index %u from %s",
681 		attr->index, dev->netdev->name);
682 
683 	return pvrdma_del_gid_at_index(dev, attr->index);
684 }
685 
pvrdma_netdevice_event_handle(struct pvrdma_dev * dev,struct net_device * ndev,unsigned long event)686 static void pvrdma_netdevice_event_handle(struct pvrdma_dev *dev,
687 					  struct net_device *ndev,
688 					  unsigned long event)
689 {
690 	struct pci_dev *pdev_net;
691 	unsigned int slot;
692 
693 	switch (event) {
694 	case NETDEV_REBOOT:
695 		pvrdma_dispatch_event(dev, 1, IB_EVENT_PORT_ERR);
696 		break;
697 	case NETDEV_UNREGISTER:
698 		ib_device_set_netdev(&dev->ib_dev, NULL, 1);
699 		dev_put(dev->netdev);
700 		dev->netdev = NULL;
701 		break;
702 	case NETDEV_REGISTER:
703 		/* vmxnet3 will have same bus, slot. But func will be 0 */
704 		slot = PCI_SLOT(dev->pdev->devfn);
705 		pdev_net = pci_get_slot(dev->pdev->bus,
706 					PCI_DEVFN(slot, 0));
707 		if ((dev->netdev == NULL) &&
708 		    (pci_get_drvdata(pdev_net) == ndev)) {
709 			/* this is our netdev */
710 			ib_device_set_netdev(&dev->ib_dev, ndev, 1);
711 			dev->netdev = ndev;
712 			dev_hold(ndev);
713 		}
714 		pci_dev_put(pdev_net);
715 		break;
716 
717 	default:
718 		dev_dbg(&dev->pdev->dev, "ignore netdevice event %ld on %s\n",
719 			event, dev_name(&dev->ib_dev.dev));
720 		break;
721 	}
722 }
723 
pvrdma_netdevice_event_work(struct work_struct * work)724 static void pvrdma_netdevice_event_work(struct work_struct *work)
725 {
726 	struct pvrdma_netdevice_work *netdev_work;
727 	struct pvrdma_dev *dev;
728 
729 	netdev_work = container_of(work, struct pvrdma_netdevice_work, work);
730 
731 	mutex_lock(&pvrdma_device_list_lock);
732 	list_for_each_entry(dev, &pvrdma_device_list, device_link) {
733 		if ((netdev_work->event == NETDEV_REGISTER) ||
734 		    (dev->netdev == netdev_work->event_netdev)) {
735 			pvrdma_netdevice_event_handle(dev,
736 						      netdev_work->event_netdev,
737 						      netdev_work->event);
738 			break;
739 		}
740 	}
741 	mutex_unlock(&pvrdma_device_list_lock);
742 
743 	kfree(netdev_work);
744 }
745 
pvrdma_netdevice_event(struct notifier_block * this,unsigned long event,void * ptr)746 static int pvrdma_netdevice_event(struct notifier_block *this,
747 				  unsigned long event, void *ptr)
748 {
749 	struct net_device *event_netdev = netdev_notifier_info_to_dev(ptr);
750 	struct pvrdma_netdevice_work *netdev_work;
751 
752 	netdev_work = kmalloc_obj(*netdev_work, GFP_ATOMIC);
753 	if (!netdev_work)
754 		return NOTIFY_BAD;
755 
756 	INIT_WORK(&netdev_work->work, pvrdma_netdevice_event_work);
757 	netdev_work->event_netdev = event_netdev;
758 	netdev_work->event = event;
759 	queue_work(event_wq, &netdev_work->work);
760 
761 	return NOTIFY_DONE;
762 }
763 
pvrdma_pci_probe(struct pci_dev * pdev,const struct pci_device_id * id)764 static int pvrdma_pci_probe(struct pci_dev *pdev,
765 			    const struct pci_device_id *id)
766 {
767 	struct pci_dev *pdev_net;
768 	struct pvrdma_dev *dev;
769 	int ret;
770 	unsigned long start;
771 	unsigned long len;
772 	dma_addr_t slot_dma = 0;
773 
774 	dev_dbg(&pdev->dev, "initializing driver %s\n", pci_name(pdev));
775 
776 	/* Allocate zero-out device */
777 	dev = ib_alloc_device(pvrdma_dev, ib_dev);
778 	if (!dev) {
779 		dev_err(&pdev->dev, "failed to allocate IB device\n");
780 		return -ENOMEM;
781 	}
782 
783 	mutex_lock(&pvrdma_device_list_lock);
784 	list_add(&dev->device_link, &pvrdma_device_list);
785 	mutex_unlock(&pvrdma_device_list_lock);
786 
787 	ret = pvrdma_init_device(dev);
788 	if (ret)
789 		goto err_free_device;
790 
791 	dev->pdev = pdev;
792 	pci_set_drvdata(pdev, dev);
793 
794 	ret = pci_enable_device(pdev);
795 	if (ret) {
796 		dev_err(&pdev->dev, "cannot enable PCI device\n");
797 		goto err_free_device;
798 	}
799 
800 	dev_dbg(&pdev->dev, "PCI resource flags BAR0 %#lx\n",
801 		pci_resource_flags(pdev, 0));
802 	dev_dbg(&pdev->dev, "PCI resource len %#llx\n",
803 		(unsigned long long)pci_resource_len(pdev, 0));
804 	dev_dbg(&pdev->dev, "PCI resource start %#llx\n",
805 		(unsigned long long)pci_resource_start(pdev, 0));
806 	dev_dbg(&pdev->dev, "PCI resource flags BAR1 %#lx\n",
807 		pci_resource_flags(pdev, 1));
808 	dev_dbg(&pdev->dev, "PCI resource len %#llx\n",
809 		(unsigned long long)pci_resource_len(pdev, 1));
810 	dev_dbg(&pdev->dev, "PCI resource start %#llx\n",
811 		(unsigned long long)pci_resource_start(pdev, 1));
812 
813 	if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM) ||
814 	    !(pci_resource_flags(pdev, 1) & IORESOURCE_MEM)) {
815 		dev_err(&pdev->dev, "PCI BAR region not MMIO\n");
816 		ret = -ENOMEM;
817 		goto err_disable_pdev;
818 	}
819 
820 	ret = pci_request_regions(pdev, DRV_NAME);
821 	if (ret) {
822 		dev_err(&pdev->dev, "cannot request PCI resources\n");
823 		goto err_disable_pdev;
824 	}
825 
826 	/* Enable 64-Bit DMA */
827 	ret = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
828 	if (ret) {
829 		dev_err(&pdev->dev, "dma_set_mask failed\n");
830 		goto err_free_resource;
831 	}
832 	dma_set_max_seg_size(&pdev->dev, UINT_MAX);
833 	pci_set_master(pdev);
834 
835 	/* Map register space */
836 	start = pci_resource_start(dev->pdev, PVRDMA_PCI_RESOURCE_REG);
837 	len = pci_resource_len(dev->pdev, PVRDMA_PCI_RESOURCE_REG);
838 	dev->regs = ioremap(start, len);
839 	if (!dev->regs) {
840 		dev_err(&pdev->dev, "register mapping failed\n");
841 		ret = -ENOMEM;
842 		goto err_free_resource;
843 	}
844 
845 	/* Setup per-device UAR. */
846 	dev->driver_uar.index = 0;
847 	dev->driver_uar.pfn =
848 		pci_resource_start(dev->pdev, PVRDMA_PCI_RESOURCE_UAR) >>
849 		PAGE_SHIFT;
850 	dev->driver_uar.map =
851 		ioremap(dev->driver_uar.pfn << PAGE_SHIFT, PAGE_SIZE);
852 	if (!dev->driver_uar.map) {
853 		dev_err(&pdev->dev, "failed to remap UAR pages\n");
854 		ret = -ENOMEM;
855 		goto err_unmap_regs;
856 	}
857 
858 	dev->dsr_version = pvrdma_read_reg(dev, PVRDMA_REG_VERSION);
859 	dev_info(&pdev->dev, "device version %d, driver version %d\n",
860 		 dev->dsr_version, PVRDMA_VERSION);
861 
862 	dev->dsr = dma_alloc_coherent(&pdev->dev, sizeof(*dev->dsr),
863 				      &dev->dsrbase, GFP_KERNEL);
864 	if (!dev->dsr) {
865 		dev_err(&pdev->dev, "failed to allocate shared region\n");
866 		ret = -ENOMEM;
867 		goto err_uar_unmap;
868 	}
869 
870 	/* Setup the shared region */
871 	dev->dsr->driver_version = PVRDMA_VERSION;
872 	dev->dsr->gos_info.gos_bits = sizeof(void *) == 4 ?
873 		PVRDMA_GOS_BITS_32 :
874 		PVRDMA_GOS_BITS_64;
875 	dev->dsr->gos_info.gos_type = PVRDMA_GOS_TYPE_LINUX;
876 	dev->dsr->gos_info.gos_ver = 1;
877 
878 	if (dev->dsr_version < PVRDMA_PPN64_VERSION)
879 		dev->dsr->uar_pfn = dev->driver_uar.pfn;
880 	else
881 		dev->dsr->uar_pfn64 = dev->driver_uar.pfn;
882 
883 	/* Command slot. */
884 	dev->cmd_slot = dma_alloc_coherent(&pdev->dev, PAGE_SIZE,
885 					   &slot_dma, GFP_KERNEL);
886 	if (!dev->cmd_slot) {
887 		ret = -ENOMEM;
888 		goto err_free_dsr;
889 	}
890 
891 	dev->dsr->cmd_slot_dma = (u64)slot_dma;
892 
893 	/* Response slot. */
894 	dev->resp_slot = dma_alloc_coherent(&pdev->dev, PAGE_SIZE,
895 					    &slot_dma, GFP_KERNEL);
896 	if (!dev->resp_slot) {
897 		ret = -ENOMEM;
898 		goto err_free_slots;
899 	}
900 
901 	dev->dsr->resp_slot_dma = (u64)slot_dma;
902 
903 	/* Async event ring */
904 	dev->dsr->async_ring_pages.num_pages = PVRDMA_NUM_RING_PAGES;
905 	ret = pvrdma_page_dir_init(dev, &dev->async_pdir,
906 				   dev->dsr->async_ring_pages.num_pages, true);
907 	if (ret)
908 		goto err_free_slots;
909 	dev->async_ring_state = dev->async_pdir.pages[0];
910 	dev->dsr->async_ring_pages.pdir_dma = dev->async_pdir.dir_dma;
911 
912 	/* CQ notification ring */
913 	dev->dsr->cq_ring_pages.num_pages = PVRDMA_NUM_RING_PAGES;
914 	ret = pvrdma_page_dir_init(dev, &dev->cq_pdir,
915 				   dev->dsr->cq_ring_pages.num_pages, true);
916 	if (ret)
917 		goto err_free_async_ring;
918 	dev->cq_ring_state = dev->cq_pdir.pages[0];
919 	dev->dsr->cq_ring_pages.pdir_dma = dev->cq_pdir.dir_dma;
920 
921 	/*
922 	 * Write the PA of the shared region to the device. The writes must be
923 	 * ordered such that the high bits are written last. When the writes
924 	 * complete, the device will have filled out the capabilities.
925 	 */
926 
927 	pvrdma_write_reg(dev, PVRDMA_REG_DSRLOW, (u32)dev->dsrbase);
928 	pvrdma_write_reg(dev, PVRDMA_REG_DSRHIGH,
929 			 (u32)((u64)(dev->dsrbase) >> 32));
930 
931 	/* Make sure the write is complete before reading status. */
932 	mb();
933 
934 	/* The driver supports RoCE V1 and V2. */
935 	if (!PVRDMA_SUPPORTED(dev)) {
936 		dev_err(&pdev->dev, "driver needs RoCE v1 or v2 support\n");
937 		ret = -EFAULT;
938 		goto err_free_cq_ring;
939 	}
940 
941 	/* Paired vmxnet3 will have same bus, slot. But func will be 0 */
942 	pdev_net = pci_get_slot(pdev->bus, PCI_DEVFN(PCI_SLOT(pdev->devfn), 0));
943 	if (!pdev_net) {
944 		dev_err(&pdev->dev, "failed to find paired net device\n");
945 		ret = -ENODEV;
946 		goto err_free_cq_ring;
947 	}
948 
949 	if (pdev_net->vendor != PCI_VENDOR_ID_VMWARE ||
950 	    pdev_net->device != PCI_DEVICE_ID_VMWARE_VMXNET3) {
951 		dev_err(&pdev->dev, "failed to find paired vmxnet3 device\n");
952 		pci_dev_put(pdev_net);
953 		ret = -ENODEV;
954 		goto err_free_cq_ring;
955 	}
956 
957 	dev->netdev = pci_get_drvdata(pdev_net);
958 	pci_dev_put(pdev_net);
959 	if (!dev->netdev) {
960 		dev_err(&pdev->dev, "failed to get vmxnet3 device\n");
961 		ret = -ENODEV;
962 		goto err_free_cq_ring;
963 	}
964 	dev_hold(dev->netdev);
965 
966 	dev_info(&pdev->dev, "paired device to %s\n", dev->netdev->name);
967 
968 	/* Interrupt setup */
969 	ret = pvrdma_alloc_intrs(dev);
970 	if (ret) {
971 		dev_err(&pdev->dev, "failed to allocate interrupts\n");
972 		ret = -ENOMEM;
973 		goto err_free_cq_ring;
974 	}
975 
976 	/* Allocate UAR table. */
977 	ret = pvrdma_uar_table_init(dev);
978 	if (ret) {
979 		dev_err(&pdev->dev, "failed to allocate UAR table\n");
980 		ret = -ENOMEM;
981 		goto err_free_intrs;
982 	}
983 
984 	/* Allocate GID table */
985 	dev->sgid_tbl = kzalloc_objs(union ib_gid, dev->dsr->caps.gid_tbl_len);
986 	if (!dev->sgid_tbl) {
987 		ret = -ENOMEM;
988 		goto err_free_uar_table;
989 	}
990 	dev_dbg(&pdev->dev, "gid table len %d\n", dev->dsr->caps.gid_tbl_len);
991 
992 	pvrdma_enable_intrs(dev);
993 
994 	/* Activate pvrdma device */
995 	pvrdma_write_reg(dev, PVRDMA_REG_CTL, PVRDMA_DEVICE_CTL_ACTIVATE);
996 
997 	/* Make sure the write is complete before reading status. */
998 	mb();
999 
1000 	/* Check if device was successfully activated */
1001 	ret = pvrdma_read_reg(dev, PVRDMA_REG_ERR);
1002 	if (ret != 0) {
1003 		dev_err(&pdev->dev, "failed to activate device\n");
1004 		ret = -EFAULT;
1005 		goto err_disable_intr;
1006 	}
1007 
1008 	/* Register IB device */
1009 	ret = pvrdma_register_device(dev);
1010 	if (ret) {
1011 		dev_err(&pdev->dev, "failed to register IB device\n");
1012 		goto err_disable_intr;
1013 	}
1014 
1015 	dev->nb_netdev.notifier_call = pvrdma_netdevice_event;
1016 	ret = register_netdevice_notifier(&dev->nb_netdev);
1017 	if (ret) {
1018 		dev_err(&pdev->dev, "failed to register netdevice events\n");
1019 		goto err_unreg_ibdev;
1020 	}
1021 
1022 	dev_info(&pdev->dev, "attached to device\n");
1023 	return 0;
1024 
1025 err_unreg_ibdev:
1026 	ib_unregister_device(&dev->ib_dev);
1027 err_disable_intr:
1028 	pvrdma_disable_intrs(dev);
1029 	kfree(dev->sgid_tbl);
1030 err_free_uar_table:
1031 	pvrdma_uar_table_cleanup(dev);
1032 err_free_intrs:
1033 	pvrdma_free_irq(dev);
1034 	pci_free_irq_vectors(pdev);
1035 err_free_cq_ring:
1036 	dev_put(dev->netdev);
1037 	dev->netdev = NULL;
1038 	pvrdma_page_dir_cleanup(dev, &dev->cq_pdir);
1039 err_free_async_ring:
1040 	pvrdma_page_dir_cleanup(dev, &dev->async_pdir);
1041 err_free_slots:
1042 	pvrdma_free_slots(dev);
1043 err_free_dsr:
1044 	dma_free_coherent(&pdev->dev, sizeof(*dev->dsr), dev->dsr,
1045 			  dev->dsrbase);
1046 err_uar_unmap:
1047 	iounmap(dev->driver_uar.map);
1048 err_unmap_regs:
1049 	iounmap(dev->regs);
1050 err_free_resource:
1051 	pci_release_regions(pdev);
1052 err_disable_pdev:
1053 	pci_disable_device(pdev);
1054 	pci_set_drvdata(pdev, NULL);
1055 err_free_device:
1056 	mutex_lock(&pvrdma_device_list_lock);
1057 	list_del(&dev->device_link);
1058 	mutex_unlock(&pvrdma_device_list_lock);
1059 	ib_dealloc_device(&dev->ib_dev);
1060 	return ret;
1061 }
1062 
pvrdma_pci_remove(struct pci_dev * pdev)1063 static void pvrdma_pci_remove(struct pci_dev *pdev)
1064 {
1065 	struct pvrdma_dev *dev = pci_get_drvdata(pdev);
1066 
1067 	if (!dev)
1068 		return;
1069 
1070 	dev_info(&pdev->dev, "detaching from device\n");
1071 
1072 	unregister_netdevice_notifier(&dev->nb_netdev);
1073 	dev->nb_netdev.notifier_call = NULL;
1074 
1075 	flush_workqueue(event_wq);
1076 
1077 	dev_put(dev->netdev);
1078 	dev->netdev = NULL;
1079 
1080 	/* Unregister ib device */
1081 	ib_unregister_device(&dev->ib_dev);
1082 
1083 	mutex_lock(&pvrdma_device_list_lock);
1084 	list_del(&dev->device_link);
1085 	mutex_unlock(&pvrdma_device_list_lock);
1086 
1087 	pvrdma_disable_intrs(dev);
1088 	pvrdma_free_irq(dev);
1089 	pci_free_irq_vectors(pdev);
1090 
1091 	/* Deactivate pvrdma device */
1092 	pvrdma_write_reg(dev, PVRDMA_REG_CTL, PVRDMA_DEVICE_CTL_RESET);
1093 	pvrdma_page_dir_cleanup(dev, &dev->cq_pdir);
1094 	pvrdma_page_dir_cleanup(dev, &dev->async_pdir);
1095 	pvrdma_free_slots(dev);
1096 	dma_free_coherent(&pdev->dev, sizeof(*dev->dsr), dev->dsr,
1097 			  dev->dsrbase);
1098 
1099 	iounmap(dev->regs);
1100 	kfree(dev->sgid_tbl);
1101 	kfree(dev->cq_tbl);
1102 	kfree(dev->srq_tbl);
1103 	kfree(dev->qp_tbl);
1104 	pvrdma_uar_table_cleanup(dev);
1105 	iounmap(dev->driver_uar.map);
1106 
1107 	ib_dealloc_device(&dev->ib_dev);
1108 
1109 	/* Free pci resources */
1110 	pci_release_regions(pdev);
1111 	pci_disable_device(pdev);
1112 	pci_set_drvdata(pdev, NULL);
1113 }
1114 
1115 static const struct pci_device_id pvrdma_pci_table[] = {
1116 	{ PCI_DEVICE(PCI_VENDOR_ID_VMWARE, PCI_DEVICE_ID_VMWARE_PVRDMA), },
1117 	{ 0 },
1118 };
1119 
1120 MODULE_DEVICE_TABLE(pci, pvrdma_pci_table);
1121 
1122 static struct pci_driver pvrdma_driver = {
1123 	.name		= DRV_NAME,
1124 	.id_table	= pvrdma_pci_table,
1125 	.probe		= pvrdma_pci_probe,
1126 	.remove		= pvrdma_pci_remove,
1127 };
1128 
pvrdma_init(void)1129 static int __init pvrdma_init(void)
1130 {
1131 	int err;
1132 
1133 	event_wq = alloc_ordered_workqueue("pvrdma_event_wq", WQ_MEM_RECLAIM);
1134 	if (!event_wq)
1135 		return -ENOMEM;
1136 
1137 	err = pci_register_driver(&pvrdma_driver);
1138 	if (err)
1139 		destroy_workqueue(event_wq);
1140 
1141 	return err;
1142 }
1143 
pvrdma_cleanup(void)1144 static void __exit pvrdma_cleanup(void)
1145 {
1146 	pci_unregister_driver(&pvrdma_driver);
1147 
1148 	destroy_workqueue(event_wq);
1149 }
1150 
1151 module_init(pvrdma_init);
1152 module_exit(pvrdma_cleanup);
1153 
1154 MODULE_AUTHOR("VMware, Inc");
1155 MODULE_DESCRIPTION("VMware Paravirtual RDMA driver");
1156 MODULE_LICENSE("Dual BSD/GPL");
1157