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.
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>
58 #define DRV_NAME "vmw_pvrdma"
59 #define DRV_VERSION "1.0.1.0-k"
61 static DEFINE_MUTEX(pvrdma_device_list_lock);
62 static LIST_HEAD(pvrdma_device_list);
63 static struct workqueue_struct *event_wq;
65 static int pvrdma_add_gid(struct ib_device *ibdev,
68 const union ib_gid *gid,
69 const struct ib_gid_attr *attr,
71 static int pvrdma_del_gid(struct ib_device *ibdev,
77 static ssize_t show_hca(struct device *device, struct device_attribute *attr,
80 return sprintf(buf, "VMW_PVRDMA-%s\n", DRV_VERSION);
83 static ssize_t show_rev(struct device *device, struct device_attribute *attr,
86 return sprintf(buf, "%d\n", PVRDMA_REV_ID);
89 static ssize_t show_board(struct device *device, struct device_attribute *attr,
92 return sprintf(buf, "%d\n", PVRDMA_BOARD_ID);
95 static DEVICE_ATTR(hw_rev, S_IRUGO, show_rev, NULL);
96 static DEVICE_ATTR(hca_type, S_IRUGO, show_hca, NULL);
97 static DEVICE_ATTR(board_id, S_IRUGO, show_board, NULL);
99 static struct device_attribute *pvrdma_class_attributes[] = {
105 static void pvrdma_get_fw_ver_str(struct ib_device *device, char *str)
107 struct pvrdma_dev *dev =
108 container_of(device, struct pvrdma_dev, ib_dev);
109 snprintf(str, IB_FW_VERSION_NAME_MAX, "%d.%d.%d\n",
110 (int) (dev->dsr->caps.fw_ver >> 32),
111 (int) (dev->dsr->caps.fw_ver >> 16) & 0xffff,
112 (int) dev->dsr->caps.fw_ver & 0xffff);
115 static int pvrdma_init_device(struct pvrdma_dev *dev)
117 /* Initialize some device related stuff */
118 spin_lock_init(&dev->cmd_lock);
119 sema_init(&dev->cmd_sema, 1);
120 atomic_set(&dev->num_qps, 0);
121 atomic_set(&dev->num_cqs, 0);
122 atomic_set(&dev->num_pds, 0);
123 atomic_set(&dev->num_ahs, 0);
128 static int pvrdma_port_immutable(struct ib_device *ibdev, u8 port_num,
129 struct ib_port_immutable *immutable)
131 struct pvrdma_dev *dev = to_vdev(ibdev);
132 struct ib_port_attr attr;
135 if (dev->dsr->caps.gid_types == PVRDMA_GID_TYPE_FLAG_ROCE_V1)
136 immutable->core_cap_flags |= RDMA_CORE_PORT_IBA_ROCE;
137 else if (dev->dsr->caps.gid_types == PVRDMA_GID_TYPE_FLAG_ROCE_V2)
138 immutable->core_cap_flags |= RDMA_CORE_PORT_IBA_ROCE_UDP_ENCAP;
140 err = ib_query_port(ibdev, port_num, &attr);
144 immutable->pkey_tbl_len = attr.pkey_tbl_len;
145 immutable->gid_tbl_len = attr.gid_tbl_len;
146 immutable->max_mad_size = IB_MGMT_MAD_SIZE;
150 static struct net_device *pvrdma_get_netdev(struct ib_device *ibdev,
153 struct net_device *netdev;
154 struct pvrdma_dev *dev = to_vdev(ibdev);
160 netdev = dev->netdev;
168 static int pvrdma_register_device(struct pvrdma_dev *dev)
173 strlcpy(dev->ib_dev.name, "vmw_pvrdma%d", IB_DEVICE_NAME_MAX);
174 dev->ib_dev.node_guid = dev->dsr->caps.node_guid;
175 dev->sys_image_guid = dev->dsr->caps.sys_image_guid;
177 dev->ib_dev.owner = THIS_MODULE;
178 dev->ib_dev.num_comp_vectors = 1;
179 dev->ib_dev.dev.parent = &dev->pdev->dev;
180 dev->ib_dev.uverbs_abi_ver = PVRDMA_UVERBS_ABI_VERSION;
181 dev->ib_dev.uverbs_cmd_mask =
182 (1ull << IB_USER_VERBS_CMD_GET_CONTEXT) |
183 (1ull << IB_USER_VERBS_CMD_QUERY_DEVICE) |
184 (1ull << IB_USER_VERBS_CMD_QUERY_PORT) |
185 (1ull << IB_USER_VERBS_CMD_ALLOC_PD) |
186 (1ull << IB_USER_VERBS_CMD_DEALLOC_PD) |
187 (1ull << IB_USER_VERBS_CMD_REG_MR) |
188 (1ull << IB_USER_VERBS_CMD_DEREG_MR) |
189 (1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL) |
190 (1ull << IB_USER_VERBS_CMD_CREATE_CQ) |
191 (1ull << IB_USER_VERBS_CMD_POLL_CQ) |
192 (1ull << IB_USER_VERBS_CMD_REQ_NOTIFY_CQ) |
193 (1ull << IB_USER_VERBS_CMD_DESTROY_CQ) |
194 (1ull << IB_USER_VERBS_CMD_CREATE_QP) |
195 (1ull << IB_USER_VERBS_CMD_MODIFY_QP) |
196 (1ull << IB_USER_VERBS_CMD_QUERY_QP) |
197 (1ull << IB_USER_VERBS_CMD_DESTROY_QP) |
198 (1ull << IB_USER_VERBS_CMD_POST_SEND) |
199 (1ull << IB_USER_VERBS_CMD_POST_RECV) |
200 (1ull << IB_USER_VERBS_CMD_CREATE_AH) |
201 (1ull << IB_USER_VERBS_CMD_DESTROY_AH);
203 dev->ib_dev.node_type = RDMA_NODE_IB_CA;
204 dev->ib_dev.phys_port_cnt = dev->dsr->caps.phys_port_cnt;
206 dev->ib_dev.query_device = pvrdma_query_device;
207 dev->ib_dev.query_port = pvrdma_query_port;
208 dev->ib_dev.query_gid = pvrdma_query_gid;
209 dev->ib_dev.query_pkey = pvrdma_query_pkey;
210 dev->ib_dev.modify_port = pvrdma_modify_port;
211 dev->ib_dev.alloc_ucontext = pvrdma_alloc_ucontext;
212 dev->ib_dev.dealloc_ucontext = pvrdma_dealloc_ucontext;
213 dev->ib_dev.mmap = pvrdma_mmap;
214 dev->ib_dev.alloc_pd = pvrdma_alloc_pd;
215 dev->ib_dev.dealloc_pd = pvrdma_dealloc_pd;
216 dev->ib_dev.create_ah = pvrdma_create_ah;
217 dev->ib_dev.destroy_ah = pvrdma_destroy_ah;
218 dev->ib_dev.create_qp = pvrdma_create_qp;
219 dev->ib_dev.modify_qp = pvrdma_modify_qp;
220 dev->ib_dev.query_qp = pvrdma_query_qp;
221 dev->ib_dev.destroy_qp = pvrdma_destroy_qp;
222 dev->ib_dev.post_send = pvrdma_post_send;
223 dev->ib_dev.post_recv = pvrdma_post_recv;
224 dev->ib_dev.create_cq = pvrdma_create_cq;
225 dev->ib_dev.modify_cq = pvrdma_modify_cq;
226 dev->ib_dev.resize_cq = pvrdma_resize_cq;
227 dev->ib_dev.destroy_cq = pvrdma_destroy_cq;
228 dev->ib_dev.poll_cq = pvrdma_poll_cq;
229 dev->ib_dev.req_notify_cq = pvrdma_req_notify_cq;
230 dev->ib_dev.get_dma_mr = pvrdma_get_dma_mr;
231 dev->ib_dev.reg_user_mr = pvrdma_reg_user_mr;
232 dev->ib_dev.dereg_mr = pvrdma_dereg_mr;
233 dev->ib_dev.alloc_mr = pvrdma_alloc_mr;
234 dev->ib_dev.map_mr_sg = pvrdma_map_mr_sg;
235 dev->ib_dev.add_gid = pvrdma_add_gid;
236 dev->ib_dev.del_gid = pvrdma_del_gid;
237 dev->ib_dev.get_netdev = pvrdma_get_netdev;
238 dev->ib_dev.get_port_immutable = pvrdma_port_immutable;
239 dev->ib_dev.get_link_layer = pvrdma_port_link_layer;
240 dev->ib_dev.get_dev_fw_str = pvrdma_get_fw_ver_str;
242 mutex_init(&dev->port_mutex);
243 spin_lock_init(&dev->desc_lock);
245 dev->cq_tbl = kcalloc(dev->dsr->caps.max_cq, sizeof(void *),
249 spin_lock_init(&dev->cq_tbl_lock);
251 dev->qp_tbl = kcalloc(dev->dsr->caps.max_qp, sizeof(void *),
255 spin_lock_init(&dev->qp_tbl_lock);
257 ret = ib_register_device(&dev->ib_dev, NULL);
261 for (i = 0; i < ARRAY_SIZE(pvrdma_class_attributes); ++i) {
262 ret = device_create_file(&dev->ib_dev.dev,
263 pvrdma_class_attributes[i]);
268 dev->ib_active = true;
273 ib_unregister_device(&dev->ib_dev);
282 static irqreturn_t pvrdma_intr0_handler(int irq, void *dev_id)
284 u32 icr = PVRDMA_INTR_CAUSE_RESPONSE;
285 struct pvrdma_dev *dev = dev_id;
287 dev_dbg(&dev->pdev->dev, "interrupt 0 (response) handler\n");
289 if (!dev->pdev->msix_enabled) {
291 icr = pvrdma_read_reg(dev, PVRDMA_REG_ICR);
296 if (icr == PVRDMA_INTR_CAUSE_RESPONSE)
297 complete(&dev->cmd_done);
302 static void pvrdma_qp_event(struct pvrdma_dev *dev, u32 qpn, int type)
304 struct pvrdma_qp *qp;
307 spin_lock_irqsave(&dev->qp_tbl_lock, flags);
308 qp = dev->qp_tbl[qpn % dev->dsr->caps.max_qp];
310 atomic_inc(&qp->refcnt);
311 spin_unlock_irqrestore(&dev->qp_tbl_lock, flags);
313 if (qp && qp->ibqp.event_handler) {
314 struct ib_qp *ibqp = &qp->ibqp;
317 e.device = ibqp->device;
319 e.event = type; /* 1:1 mapping for now. */
320 ibqp->event_handler(&e, ibqp->qp_context);
323 atomic_dec(&qp->refcnt);
324 if (atomic_read(&qp->refcnt) == 0)
329 static void pvrdma_cq_event(struct pvrdma_dev *dev, u32 cqn, int type)
331 struct pvrdma_cq *cq;
334 spin_lock_irqsave(&dev->cq_tbl_lock, flags);
335 cq = dev->cq_tbl[cqn % dev->dsr->caps.max_cq];
337 atomic_inc(&cq->refcnt);
338 spin_unlock_irqrestore(&dev->cq_tbl_lock, flags);
340 if (cq && cq->ibcq.event_handler) {
341 struct ib_cq *ibcq = &cq->ibcq;
344 e.device = ibcq->device;
346 e.event = type; /* 1:1 mapping for now. */
347 ibcq->event_handler(&e, ibcq->cq_context);
350 atomic_dec(&cq->refcnt);
351 if (atomic_read(&cq->refcnt) == 0)
356 static void pvrdma_dispatch_event(struct pvrdma_dev *dev, int port,
357 enum ib_event_type event)
359 struct ib_event ib_event;
361 memset(&ib_event, 0, sizeof(ib_event));
362 ib_event.device = &dev->ib_dev;
363 ib_event.element.port_num = port;
364 ib_event.event = event;
365 ib_dispatch_event(&ib_event);
368 static void pvrdma_dev_event(struct pvrdma_dev *dev, u8 port, int type)
370 if (port < 1 || port > dev->dsr->caps.phys_port_cnt) {
371 dev_warn(&dev->pdev->dev, "event on port %d\n", port);
375 pvrdma_dispatch_event(dev, port, type);
378 static inline struct pvrdma_eqe *get_eqe(struct pvrdma_dev *dev, unsigned int i)
380 return (struct pvrdma_eqe *)pvrdma_page_dir_get_ptr(
383 sizeof(struct pvrdma_eqe) * i);
386 static irqreturn_t pvrdma_intr1_handler(int irq, void *dev_id)
388 struct pvrdma_dev *dev = dev_id;
389 struct pvrdma_ring *ring = &dev->async_ring_state->rx;
390 int ring_slots = (dev->dsr->async_ring_pages.num_pages - 1) *
391 PAGE_SIZE / sizeof(struct pvrdma_eqe);
394 dev_dbg(&dev->pdev->dev, "interrupt 1 (async event) handler\n");
397 * Don't process events until the IB device is registered. Otherwise
398 * we'll try to ib_dispatch_event() on an invalid device.
403 while (pvrdma_idx_ring_has_data(ring, ring_slots, &head) > 0) {
404 struct pvrdma_eqe *eqe;
406 eqe = get_eqe(dev, head);
409 case PVRDMA_EVENT_QP_FATAL:
410 case PVRDMA_EVENT_QP_REQ_ERR:
411 case PVRDMA_EVENT_QP_ACCESS_ERR:
412 case PVRDMA_EVENT_COMM_EST:
413 case PVRDMA_EVENT_SQ_DRAINED:
414 case PVRDMA_EVENT_PATH_MIG:
415 case PVRDMA_EVENT_PATH_MIG_ERR:
416 case PVRDMA_EVENT_QP_LAST_WQE_REACHED:
417 pvrdma_qp_event(dev, eqe->info, eqe->type);
420 case PVRDMA_EVENT_CQ_ERR:
421 pvrdma_cq_event(dev, eqe->info, eqe->type);
424 case PVRDMA_EVENT_SRQ_ERR:
425 case PVRDMA_EVENT_SRQ_LIMIT_REACHED:
428 case PVRDMA_EVENT_PORT_ACTIVE:
429 case PVRDMA_EVENT_PORT_ERR:
430 case PVRDMA_EVENT_LID_CHANGE:
431 case PVRDMA_EVENT_PKEY_CHANGE:
432 case PVRDMA_EVENT_SM_CHANGE:
433 case PVRDMA_EVENT_CLIENT_REREGISTER:
434 case PVRDMA_EVENT_GID_CHANGE:
435 pvrdma_dev_event(dev, eqe->info, eqe->type);
438 case PVRDMA_EVENT_DEVICE_FATAL:
439 pvrdma_dev_event(dev, 1, eqe->type);
446 pvrdma_idx_ring_inc(&ring->cons_head, ring_slots);
452 static inline struct pvrdma_cqne *get_cqne(struct pvrdma_dev *dev,
455 return (struct pvrdma_cqne *)pvrdma_page_dir_get_ptr(
458 sizeof(struct pvrdma_cqne) * i);
461 static irqreturn_t pvrdma_intrx_handler(int irq, void *dev_id)
463 struct pvrdma_dev *dev = dev_id;
464 struct pvrdma_ring *ring = &dev->cq_ring_state->rx;
465 int ring_slots = (dev->dsr->cq_ring_pages.num_pages - 1) * PAGE_SIZE /
466 sizeof(struct pvrdma_cqne);
470 dev_dbg(&dev->pdev->dev, "interrupt x (completion) handler\n");
472 while (pvrdma_idx_ring_has_data(ring, ring_slots, &head) > 0) {
473 struct pvrdma_cqne *cqne;
474 struct pvrdma_cq *cq;
476 cqne = get_cqne(dev, head);
477 spin_lock_irqsave(&dev->cq_tbl_lock, flags);
478 cq = dev->cq_tbl[cqne->info % dev->dsr->caps.max_cq];
480 atomic_inc(&cq->refcnt);
481 spin_unlock_irqrestore(&dev->cq_tbl_lock, flags);
483 if (cq && cq->ibcq.comp_handler)
484 cq->ibcq.comp_handler(&cq->ibcq, cq->ibcq.cq_context);
486 atomic_dec(&cq->refcnt);
487 if (atomic_read(&cq->refcnt))
490 pvrdma_idx_ring_inc(&ring->cons_head, ring_slots);
496 static void pvrdma_free_irq(struct pvrdma_dev *dev)
500 dev_dbg(&dev->pdev->dev, "freeing interrupts\n");
501 for (i = 0; i < dev->nr_vectors; i++)
502 free_irq(pci_irq_vector(dev->pdev, i), dev);
505 static void pvrdma_enable_intrs(struct pvrdma_dev *dev)
507 dev_dbg(&dev->pdev->dev, "enable interrupts\n");
508 pvrdma_write_reg(dev, PVRDMA_REG_IMR, 0);
511 static void pvrdma_disable_intrs(struct pvrdma_dev *dev)
513 dev_dbg(&dev->pdev->dev, "disable interrupts\n");
514 pvrdma_write_reg(dev, PVRDMA_REG_IMR, ~0);
517 static int pvrdma_alloc_intrs(struct pvrdma_dev *dev)
519 struct pci_dev *pdev = dev->pdev;
522 ret = pci_alloc_irq_vectors(pdev, 1, PVRDMA_MAX_INTERRUPTS,
525 ret = pci_alloc_irq_vectors(pdev, 1, 1,
526 PCI_IRQ_MSI | PCI_IRQ_LEGACY);
530 dev->nr_vectors = ret;
532 ret = request_irq(pci_irq_vector(dev->pdev, 0), pvrdma_intr0_handler,
533 pdev->msix_enabled ? 0 : IRQF_SHARED, DRV_NAME, dev);
535 dev_err(&dev->pdev->dev,
536 "failed to request interrupt 0\n");
537 goto out_free_vectors;
540 for (i = 1; i < dev->nr_vectors; i++) {
541 ret = request_irq(pci_irq_vector(dev->pdev, i),
542 i == 1 ? pvrdma_intr1_handler :
543 pvrdma_intrx_handler,
546 dev_err(&dev->pdev->dev,
547 "failed to request interrupt %d\n", i);
556 free_irq(pci_irq_vector(dev->pdev, i), dev);
558 pci_free_irq_vectors(pdev);
562 static void pvrdma_free_slots(struct pvrdma_dev *dev)
564 struct pci_dev *pdev = dev->pdev;
567 dma_free_coherent(&pdev->dev, PAGE_SIZE, dev->resp_slot,
568 dev->dsr->resp_slot_dma);
570 dma_free_coherent(&pdev->dev, PAGE_SIZE, dev->cmd_slot,
571 dev->dsr->cmd_slot_dma);
574 static int pvrdma_add_gid_at_index(struct pvrdma_dev *dev,
575 const union ib_gid *gid,
580 union pvrdma_cmd_req req;
581 struct pvrdma_cmd_create_bind *cmd_bind = &req.create_bind;
583 if (!dev->sgid_tbl) {
584 dev_warn(&dev->pdev->dev, "sgid table not initialized\n");
588 memset(cmd_bind, 0, sizeof(*cmd_bind));
589 cmd_bind->hdr.cmd = PVRDMA_CMD_CREATE_BIND;
590 memcpy(cmd_bind->new_gid, gid->raw, 16);
591 cmd_bind->mtu = ib_mtu_enum_to_int(IB_MTU_1024);
592 cmd_bind->vlan = 0xfff;
593 cmd_bind->index = index;
594 cmd_bind->gid_type = gid_type;
596 ret = pvrdma_cmd_post(dev, &req, NULL, 0);
598 dev_warn(&dev->pdev->dev,
599 "could not create binding, error: %d\n", ret);
602 memcpy(&dev->sgid_tbl[index], gid, sizeof(*gid));
606 static int pvrdma_add_gid(struct ib_device *ibdev,
609 const union ib_gid *gid,
610 const struct ib_gid_attr *attr,
613 struct pvrdma_dev *dev = to_vdev(ibdev);
615 return pvrdma_add_gid_at_index(dev, gid,
616 ib_gid_type_to_pvrdma(attr->gid_type),
620 static int pvrdma_del_gid_at_index(struct pvrdma_dev *dev, int index)
623 union pvrdma_cmd_req req;
624 struct pvrdma_cmd_destroy_bind *cmd_dest = &req.destroy_bind;
626 /* Update sgid table. */
627 if (!dev->sgid_tbl) {
628 dev_warn(&dev->pdev->dev, "sgid table not initialized\n");
632 memset(cmd_dest, 0, sizeof(*cmd_dest));
633 cmd_dest->hdr.cmd = PVRDMA_CMD_DESTROY_BIND;
634 memcpy(cmd_dest->dest_gid, &dev->sgid_tbl[index], 16);
635 cmd_dest->index = index;
637 ret = pvrdma_cmd_post(dev, &req, NULL, 0);
639 dev_warn(&dev->pdev->dev,
640 "could not destroy binding, error: %d\n", ret);
643 memset(&dev->sgid_tbl[index], 0, 16);
647 static int pvrdma_del_gid(struct ib_device *ibdev,
652 struct pvrdma_dev *dev = to_vdev(ibdev);
654 dev_dbg(&dev->pdev->dev, "removing gid at index %u from %s",
655 index, dev->netdev->name);
657 return pvrdma_del_gid_at_index(dev, index);
660 static void pvrdma_netdevice_event_handle(struct pvrdma_dev *dev,
666 pvrdma_dispatch_event(dev, 1, IB_EVENT_PORT_ERR);
669 pvrdma_write_reg(dev, PVRDMA_REG_CTL,
670 PVRDMA_DEVICE_CTL_UNQUIESCE);
674 if (pvrdma_read_reg(dev, PVRDMA_REG_ERR))
675 dev_err(&dev->pdev->dev,
676 "failed to activate device during link up\n");
678 pvrdma_dispatch_event(dev, 1, IB_EVENT_PORT_ACTIVE);
681 dev_dbg(&dev->pdev->dev, "ignore netdevice event %ld on %s\n",
682 event, dev->ib_dev.name);
687 static void pvrdma_netdevice_event_work(struct work_struct *work)
689 struct pvrdma_netdevice_work *netdev_work;
690 struct pvrdma_dev *dev;
692 netdev_work = container_of(work, struct pvrdma_netdevice_work, work);
694 mutex_lock(&pvrdma_device_list_lock);
695 list_for_each_entry(dev, &pvrdma_device_list, device_link) {
696 if (dev->netdev == netdev_work->event_netdev) {
697 pvrdma_netdevice_event_handle(dev, netdev_work->event);
701 mutex_unlock(&pvrdma_device_list_lock);
706 static int pvrdma_netdevice_event(struct notifier_block *this,
707 unsigned long event, void *ptr)
709 struct net_device *event_netdev = netdev_notifier_info_to_dev(ptr);
710 struct pvrdma_netdevice_work *netdev_work;
712 netdev_work = kmalloc(sizeof(*netdev_work), GFP_ATOMIC);
716 INIT_WORK(&netdev_work->work, pvrdma_netdevice_event_work);
717 netdev_work->event_netdev = event_netdev;
718 netdev_work->event = event;
719 queue_work(event_wq, &netdev_work->work);
724 static int pvrdma_pci_probe(struct pci_dev *pdev,
725 const struct pci_device_id *id)
727 struct pci_dev *pdev_net;
728 struct pvrdma_dev *dev;
732 dma_addr_t slot_dma = 0;
734 dev_dbg(&pdev->dev, "initializing driver %s\n", pci_name(pdev));
736 /* Allocate zero-out device */
737 dev = (struct pvrdma_dev *)ib_alloc_device(sizeof(*dev));
739 dev_err(&pdev->dev, "failed to allocate IB device\n");
743 mutex_lock(&pvrdma_device_list_lock);
744 list_add(&dev->device_link, &pvrdma_device_list);
745 mutex_unlock(&pvrdma_device_list_lock);
747 ret = pvrdma_init_device(dev);
749 goto err_free_device;
752 pci_set_drvdata(pdev, dev);
754 ret = pci_enable_device(pdev);
756 dev_err(&pdev->dev, "cannot enable PCI device\n");
757 goto err_free_device;
760 dev_dbg(&pdev->dev, "PCI resource flags BAR0 %#lx\n",
761 pci_resource_flags(pdev, 0));
762 dev_dbg(&pdev->dev, "PCI resource len %#llx\n",
763 (unsigned long long)pci_resource_len(pdev, 0));
764 dev_dbg(&pdev->dev, "PCI resource start %#llx\n",
765 (unsigned long long)pci_resource_start(pdev, 0));
766 dev_dbg(&pdev->dev, "PCI resource flags BAR1 %#lx\n",
767 pci_resource_flags(pdev, 1));
768 dev_dbg(&pdev->dev, "PCI resource len %#llx\n",
769 (unsigned long long)pci_resource_len(pdev, 1));
770 dev_dbg(&pdev->dev, "PCI resource start %#llx\n",
771 (unsigned long long)pci_resource_start(pdev, 1));
773 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM) ||
774 !(pci_resource_flags(pdev, 1) & IORESOURCE_MEM)) {
775 dev_err(&pdev->dev, "PCI BAR region not MMIO\n");
777 goto err_free_device;
780 ret = pci_request_regions(pdev, DRV_NAME);
782 dev_err(&pdev->dev, "cannot request PCI resources\n");
783 goto err_disable_pdev;
786 /* Enable 64-Bit DMA */
787 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) == 0) {
788 ret = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
791 "pci_set_consistent_dma_mask failed\n");
792 goto err_free_resource;
795 ret = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
798 "pci_set_dma_mask failed\n");
799 goto err_free_resource;
803 pci_set_master(pdev);
805 /* Map register space */
806 start = pci_resource_start(dev->pdev, PVRDMA_PCI_RESOURCE_REG);
807 len = pci_resource_len(dev->pdev, PVRDMA_PCI_RESOURCE_REG);
808 dev->regs = ioremap(start, len);
810 dev_err(&pdev->dev, "register mapping failed\n");
812 goto err_free_resource;
815 /* Setup per-device UAR. */
816 dev->driver_uar.index = 0;
817 dev->driver_uar.pfn =
818 pci_resource_start(dev->pdev, PVRDMA_PCI_RESOURCE_UAR) >>
820 dev->driver_uar.map =
821 ioremap(dev->driver_uar.pfn << PAGE_SHIFT, PAGE_SIZE);
822 if (!dev->driver_uar.map) {
823 dev_err(&pdev->dev, "failed to remap UAR pages\n");
828 dev->dsr_version = pvrdma_read_reg(dev, PVRDMA_REG_VERSION);
829 dev_info(&pdev->dev, "device version %d, driver version %d\n",
830 dev->dsr_version, PVRDMA_VERSION);
832 dev->dsr = dma_alloc_coherent(&pdev->dev, sizeof(*dev->dsr),
833 &dev->dsrbase, GFP_KERNEL);
835 dev_err(&pdev->dev, "failed to allocate shared region\n");
840 /* Setup the shared region */
841 memset(dev->dsr, 0, sizeof(*dev->dsr));
842 dev->dsr->driver_version = PVRDMA_VERSION;
843 dev->dsr->gos_info.gos_bits = sizeof(void *) == 4 ?
846 dev->dsr->gos_info.gos_type = PVRDMA_GOS_TYPE_LINUX;
847 dev->dsr->gos_info.gos_ver = 1;
848 dev->dsr->uar_pfn = dev->driver_uar.pfn;
851 dev->cmd_slot = dma_alloc_coherent(&pdev->dev, PAGE_SIZE,
852 &slot_dma, GFP_KERNEL);
853 if (!dev->cmd_slot) {
858 dev->dsr->cmd_slot_dma = (u64)slot_dma;
861 dev->resp_slot = dma_alloc_coherent(&pdev->dev, PAGE_SIZE,
862 &slot_dma, GFP_KERNEL);
863 if (!dev->resp_slot) {
868 dev->dsr->resp_slot_dma = (u64)slot_dma;
870 /* Async event ring */
871 dev->dsr->async_ring_pages.num_pages = PVRDMA_NUM_RING_PAGES;
872 ret = pvrdma_page_dir_init(dev, &dev->async_pdir,
873 dev->dsr->async_ring_pages.num_pages, true);
876 dev->async_ring_state = dev->async_pdir.pages[0];
877 dev->dsr->async_ring_pages.pdir_dma = dev->async_pdir.dir_dma;
879 /* CQ notification ring */
880 dev->dsr->cq_ring_pages.num_pages = PVRDMA_NUM_RING_PAGES;
881 ret = pvrdma_page_dir_init(dev, &dev->cq_pdir,
882 dev->dsr->cq_ring_pages.num_pages, true);
884 goto err_free_async_ring;
885 dev->cq_ring_state = dev->cq_pdir.pages[0];
886 dev->dsr->cq_ring_pages.pdir_dma = dev->cq_pdir.dir_dma;
889 * Write the PA of the shared region to the device. The writes must be
890 * ordered such that the high bits are written last. When the writes
891 * complete, the device will have filled out the capabilities.
894 pvrdma_write_reg(dev, PVRDMA_REG_DSRLOW, (u32)dev->dsrbase);
895 pvrdma_write_reg(dev, PVRDMA_REG_DSRHIGH,
896 (u32)((u64)(dev->dsrbase) >> 32));
898 /* Make sure the write is complete before reading status. */
901 /* The driver supports RoCE V1 and V2. */
902 if (!PVRDMA_SUPPORTED(dev)) {
903 dev_err(&pdev->dev, "driver needs RoCE v1 or v2 support\n");
905 goto err_free_cq_ring;
908 /* Paired vmxnet3 will have same bus, slot. But func will be 0 */
909 pdev_net = pci_get_slot(pdev->bus, PCI_DEVFN(PCI_SLOT(pdev->devfn), 0));
911 dev_err(&pdev->dev, "failed to find paired net device\n");
913 goto err_free_cq_ring;
916 if (pdev_net->vendor != PCI_VENDOR_ID_VMWARE ||
917 pdev_net->device != PCI_DEVICE_ID_VMWARE_VMXNET3) {
918 dev_err(&pdev->dev, "failed to find paired vmxnet3 device\n");
919 pci_dev_put(pdev_net);
921 goto err_free_cq_ring;
924 dev->netdev = pci_get_drvdata(pdev_net);
925 pci_dev_put(pdev_net);
927 dev_err(&pdev->dev, "failed to get vmxnet3 device\n");
929 goto err_free_cq_ring;
932 dev_info(&pdev->dev, "paired device to %s\n", dev->netdev->name);
934 /* Interrupt setup */
935 ret = pvrdma_alloc_intrs(dev);
937 dev_err(&pdev->dev, "failed to allocate interrupts\n");
939 goto err_free_cq_ring;
942 /* Allocate UAR table. */
943 ret = pvrdma_uar_table_init(dev);
945 dev_err(&pdev->dev, "failed to allocate UAR table\n");
950 /* Allocate GID table */
951 dev->sgid_tbl = kcalloc(dev->dsr->caps.gid_tbl_len,
952 sizeof(union ib_gid), GFP_KERNEL);
953 if (!dev->sgid_tbl) {
955 goto err_free_uar_table;
957 dev_dbg(&pdev->dev, "gid table len %d\n", dev->dsr->caps.gid_tbl_len);
959 pvrdma_enable_intrs(dev);
961 /* Activate pvrdma device */
962 pvrdma_write_reg(dev, PVRDMA_REG_CTL, PVRDMA_DEVICE_CTL_ACTIVATE);
964 /* Make sure the write is complete before reading status. */
967 /* Check if device was successfully activated */
968 ret = pvrdma_read_reg(dev, PVRDMA_REG_ERR);
970 dev_err(&pdev->dev, "failed to activate device\n");
972 goto err_disable_intr;
975 /* Register IB device */
976 ret = pvrdma_register_device(dev);
978 dev_err(&pdev->dev, "failed to register IB device\n");
979 goto err_disable_intr;
982 dev->nb_netdev.notifier_call = pvrdma_netdevice_event;
983 ret = register_netdevice_notifier(&dev->nb_netdev);
985 dev_err(&pdev->dev, "failed to register netdevice events\n");
986 goto err_unreg_ibdev;
989 dev_info(&pdev->dev, "attached to device\n");
993 ib_unregister_device(&dev->ib_dev);
995 pvrdma_disable_intrs(dev);
996 kfree(dev->sgid_tbl);
998 pvrdma_uar_table_cleanup(dev);
1000 pvrdma_free_irq(dev);
1001 pci_free_irq_vectors(pdev);
1003 pvrdma_page_dir_cleanup(dev, &dev->cq_pdir);
1004 err_free_async_ring:
1005 pvrdma_page_dir_cleanup(dev, &dev->async_pdir);
1007 pvrdma_free_slots(dev);
1009 dma_free_coherent(&pdev->dev, sizeof(*dev->dsr), dev->dsr,
1012 iounmap(dev->driver_uar.map);
1016 pci_release_regions(pdev);
1018 pci_disable_device(pdev);
1019 pci_set_drvdata(pdev, NULL);
1021 mutex_lock(&pvrdma_device_list_lock);
1022 list_del(&dev->device_link);
1023 mutex_unlock(&pvrdma_device_list_lock);
1024 ib_dealloc_device(&dev->ib_dev);
1028 static void pvrdma_pci_remove(struct pci_dev *pdev)
1030 struct pvrdma_dev *dev = pci_get_drvdata(pdev);
1035 dev_info(&pdev->dev, "detaching from device\n");
1037 unregister_netdevice_notifier(&dev->nb_netdev);
1038 dev->nb_netdev.notifier_call = NULL;
1040 flush_workqueue(event_wq);
1042 /* Unregister ib device */
1043 ib_unregister_device(&dev->ib_dev);
1045 mutex_lock(&pvrdma_device_list_lock);
1046 list_del(&dev->device_link);
1047 mutex_unlock(&pvrdma_device_list_lock);
1049 pvrdma_disable_intrs(dev);
1050 pvrdma_free_irq(dev);
1051 pci_free_irq_vectors(pdev);
1053 /* Deactivate pvrdma device */
1054 pvrdma_write_reg(dev, PVRDMA_REG_CTL, PVRDMA_DEVICE_CTL_RESET);
1055 pvrdma_page_dir_cleanup(dev, &dev->cq_pdir);
1056 pvrdma_page_dir_cleanup(dev, &dev->async_pdir);
1057 pvrdma_free_slots(dev);
1060 kfree(dev->sgid_tbl);
1063 pvrdma_uar_table_cleanup(dev);
1064 iounmap(dev->driver_uar.map);
1066 ib_dealloc_device(&dev->ib_dev);
1068 /* Free pci resources */
1069 pci_release_regions(pdev);
1070 pci_disable_device(pdev);
1071 pci_set_drvdata(pdev, NULL);
1074 static const struct pci_device_id pvrdma_pci_table[] = {
1075 { PCI_DEVICE(PCI_VENDOR_ID_VMWARE, PCI_DEVICE_ID_VMWARE_PVRDMA), },
1079 MODULE_DEVICE_TABLE(pci, pvrdma_pci_table);
1081 static struct pci_driver pvrdma_driver = {
1083 .id_table = pvrdma_pci_table,
1084 .probe = pvrdma_pci_probe,
1085 .remove = pvrdma_pci_remove,
1088 static int __init pvrdma_init(void)
1092 event_wq = alloc_ordered_workqueue("pvrdma_event_wq", WQ_MEM_RECLAIM);
1096 err = pci_register_driver(&pvrdma_driver);
1098 destroy_workqueue(event_wq);
1103 static void __exit pvrdma_cleanup(void)
1105 pci_unregister_driver(&pvrdma_driver);
1107 destroy_workqueue(event_wq);
1110 module_init(pvrdma_init);
1111 module_exit(pvrdma_cleanup);
1113 MODULE_AUTHOR("VMware, Inc");
1114 MODULE_DESCRIPTION("VMware Paravirtual RDMA driver");
1115 MODULE_LICENSE("Dual BSD/GPL");