1 /*******************************************************************
2 * This file is part of the Emulex RoCE Device Driver for *
3 * RoCE (RDMA over Converged Ethernet) adapters. *
4 * Copyright (C) 2008-2012 Emulex. All rights reserved. *
5 * EMULEX and SLI are trademarks of Emulex. *
8 * This program is free software; you can redistribute it and/or *
9 * modify it under the terms of version 2 of the GNU General *
10 * Public License as published by the Free Software Foundation. *
11 * This program is distributed in the hope that it will be useful. *
12 * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND *
13 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, *
14 * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE *
15 * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
16 * TO BE LEGALLY INVALID. See the GNU General Public License for *
17 * more details, a copy of which can be found in the file COPYING *
18 * included with this package. *
20 * Contact Information:
21 * linux-drivers@emulex.com
25 * Costa Mesa, CA 92626
26 *******************************************************************/
28 #include <linux/module.h>
29 #include <linux/idr.h>
30 #include <rdma/ib_verbs.h>
31 #include <rdma/ib_user_verbs.h>
32 #include <rdma/ib_addr.h>
33 #include <rdma/ib_mad.h>
35 #include <linux/netdevice.h>
36 #include <net/addrconf.h>
39 #include "ocrdma_verbs.h"
40 #include "ocrdma_ah.h"
42 #include "ocrdma_hw.h"
43 #include "ocrdma_stats.h"
44 #include "ocrdma_abi.h"
46 MODULE_VERSION(OCRDMA_ROCE_DRV_VERSION);
47 MODULE_DESCRIPTION(OCRDMA_ROCE_DRV_DESC " " OCRDMA_ROCE_DRV_VERSION);
48 MODULE_AUTHOR("Emulex Corporation");
49 MODULE_LICENSE("GPL");
51 static LIST_HEAD(ocrdma_dev_list);
52 static DEFINE_SPINLOCK(ocrdma_devlist_lock);
53 static DEFINE_IDR(ocrdma_dev_id);
55 static union ib_gid ocrdma_zero_sgid;
57 void ocrdma_get_guid(struct ocrdma_dev *dev, u8 *guid)
61 memcpy(&mac_addr[0], &dev->nic_info.mac_addr[0], ETH_ALEN);
62 guid[0] = mac_addr[0] ^ 2;
63 guid[1] = mac_addr[1];
64 guid[2] = mac_addr[2];
67 guid[5] = mac_addr[3];
68 guid[6] = mac_addr[4];
69 guid[7] = mac_addr[5];
72 static bool ocrdma_add_sgid(struct ocrdma_dev *dev, union ib_gid *new_sgid)
77 memset(&ocrdma_zero_sgid, 0, sizeof(union ib_gid));
80 spin_lock_irqsave(&dev->sgid_lock, flags);
81 for (i = 0; i < OCRDMA_MAX_SGID; i++) {
82 if (!memcmp(&dev->sgid_tbl[i], &ocrdma_zero_sgid,
83 sizeof(union ib_gid))) {
84 /* found free entry */
85 memcpy(&dev->sgid_tbl[i], new_sgid,
86 sizeof(union ib_gid));
87 spin_unlock_irqrestore(&dev->sgid_lock, flags);
89 } else if (!memcmp(&dev->sgid_tbl[i], new_sgid,
90 sizeof(union ib_gid))) {
91 /* entry already present, no addition is required. */
92 spin_unlock_irqrestore(&dev->sgid_lock, flags);
96 spin_unlock_irqrestore(&dev->sgid_lock, flags);
100 static bool ocrdma_del_sgid(struct ocrdma_dev *dev, union ib_gid *sgid)
107 spin_lock_irqsave(&dev->sgid_lock, flags);
108 /* first is default sgid, which cannot be deleted. */
109 for (i = 1; i < OCRDMA_MAX_SGID; i++) {
110 if (!memcmp(&dev->sgid_tbl[i], sgid, sizeof(union ib_gid))) {
111 /* found matching entry */
112 memset(&dev->sgid_tbl[i], 0, sizeof(union ib_gid));
117 spin_unlock_irqrestore(&dev->sgid_lock, flags);
121 static int ocrdma_addr_event(unsigned long event, struct net_device *netdev,
124 struct ib_event gid_event;
125 struct ocrdma_dev *dev;
127 bool updated = false;
128 bool is_vlan = false;
130 is_vlan = netdev->priv_flags & IFF_802_1Q_VLAN;
132 netdev = rdma_vlan_dev_real_dev(netdev);
135 list_for_each_entry_rcu(dev, &ocrdma_dev_list, entry) {
136 if (dev->nic_info.netdev == netdev) {
146 mutex_lock(&dev->dev_lock);
149 updated = ocrdma_add_sgid(dev, gid);
152 updated = ocrdma_del_sgid(dev, gid);
158 /* GID table updated, notify the consumers about it */
159 gid_event.device = &dev->ibdev;
160 gid_event.element.port_num = 1;
161 gid_event.event = IB_EVENT_GID_CHANGE;
162 ib_dispatch_event(&gid_event);
164 mutex_unlock(&dev->dev_lock);
168 static int ocrdma_inetaddr_event(struct notifier_block *notifier,
169 unsigned long event, void *ptr)
171 struct in_ifaddr *ifa = ptr;
173 struct net_device *netdev = ifa->ifa_dev->dev;
175 ipv6_addr_set_v4mapped(ifa->ifa_address, (struct in6_addr *)&gid);
176 return ocrdma_addr_event(event, netdev, &gid);
179 static struct notifier_block ocrdma_inetaddr_notifier = {
180 .notifier_call = ocrdma_inetaddr_event
183 #if IS_ENABLED(CONFIG_IPV6)
185 static int ocrdma_inet6addr_event(struct notifier_block *notifier,
186 unsigned long event, void *ptr)
188 struct inet6_ifaddr *ifa = (struct inet6_ifaddr *)ptr;
189 union ib_gid *gid = (union ib_gid *)&ifa->addr;
190 struct net_device *netdev = ifa->idev->dev;
191 return ocrdma_addr_event(event, netdev, gid);
194 static struct notifier_block ocrdma_inet6addr_notifier = {
195 .notifier_call = ocrdma_inet6addr_event
198 #endif /* IPV6 and VLAN */
200 static enum rdma_link_layer ocrdma_link_layer(struct ib_device *device,
203 return IB_LINK_LAYER_ETHERNET;
206 static int ocrdma_port_immutable(struct ib_device *ibdev, u8 port_num,
207 struct ib_port_immutable *immutable)
209 struct ib_port_attr attr;
212 err = ocrdma_query_port(ibdev, port_num, &attr);
216 immutable->pkey_tbl_len = attr.pkey_tbl_len;
217 immutable->gid_tbl_len = attr.gid_tbl_len;
218 immutable->core_cap_flags = RDMA_CORE_PORT_IBA_ROCE;
219 immutable->max_mad_size = IB_MGMT_MAD_SIZE;
224 static int ocrdma_register_device(struct ocrdma_dev *dev)
226 strlcpy(dev->ibdev.name, "ocrdma%d", IB_DEVICE_NAME_MAX);
227 ocrdma_get_guid(dev, (u8 *)&dev->ibdev.node_guid);
228 memcpy(dev->ibdev.node_desc, OCRDMA_NODE_DESC,
229 sizeof(OCRDMA_NODE_DESC));
230 dev->ibdev.owner = THIS_MODULE;
231 dev->ibdev.uverbs_abi_ver = OCRDMA_ABI_VERSION;
232 dev->ibdev.uverbs_cmd_mask =
233 OCRDMA_UVERBS(GET_CONTEXT) |
234 OCRDMA_UVERBS(QUERY_DEVICE) |
235 OCRDMA_UVERBS(QUERY_PORT) |
236 OCRDMA_UVERBS(ALLOC_PD) |
237 OCRDMA_UVERBS(DEALLOC_PD) |
238 OCRDMA_UVERBS(REG_MR) |
239 OCRDMA_UVERBS(DEREG_MR) |
240 OCRDMA_UVERBS(CREATE_COMP_CHANNEL) |
241 OCRDMA_UVERBS(CREATE_CQ) |
242 OCRDMA_UVERBS(RESIZE_CQ) |
243 OCRDMA_UVERBS(DESTROY_CQ) |
244 OCRDMA_UVERBS(REQ_NOTIFY_CQ) |
245 OCRDMA_UVERBS(CREATE_QP) |
246 OCRDMA_UVERBS(MODIFY_QP) |
247 OCRDMA_UVERBS(QUERY_QP) |
248 OCRDMA_UVERBS(DESTROY_QP) |
249 OCRDMA_UVERBS(POLL_CQ) |
250 OCRDMA_UVERBS(POST_SEND) |
251 OCRDMA_UVERBS(POST_RECV);
253 dev->ibdev.uverbs_cmd_mask |=
254 OCRDMA_UVERBS(CREATE_AH) |
255 OCRDMA_UVERBS(MODIFY_AH) |
256 OCRDMA_UVERBS(QUERY_AH) |
257 OCRDMA_UVERBS(DESTROY_AH);
259 dev->ibdev.node_type = RDMA_NODE_IB_CA;
260 dev->ibdev.phys_port_cnt = 1;
261 dev->ibdev.num_comp_vectors = dev->eq_cnt;
263 /* mandatory verbs. */
264 dev->ibdev.query_device = ocrdma_query_device;
265 dev->ibdev.query_port = ocrdma_query_port;
266 dev->ibdev.modify_port = ocrdma_modify_port;
267 dev->ibdev.query_gid = ocrdma_query_gid;
268 dev->ibdev.get_link_layer = ocrdma_link_layer;
269 dev->ibdev.alloc_pd = ocrdma_alloc_pd;
270 dev->ibdev.dealloc_pd = ocrdma_dealloc_pd;
272 dev->ibdev.create_cq = ocrdma_create_cq;
273 dev->ibdev.destroy_cq = ocrdma_destroy_cq;
274 dev->ibdev.resize_cq = ocrdma_resize_cq;
276 dev->ibdev.create_qp = ocrdma_create_qp;
277 dev->ibdev.modify_qp = ocrdma_modify_qp;
278 dev->ibdev.query_qp = ocrdma_query_qp;
279 dev->ibdev.destroy_qp = ocrdma_destroy_qp;
281 dev->ibdev.query_pkey = ocrdma_query_pkey;
282 dev->ibdev.create_ah = ocrdma_create_ah;
283 dev->ibdev.destroy_ah = ocrdma_destroy_ah;
284 dev->ibdev.query_ah = ocrdma_query_ah;
285 dev->ibdev.modify_ah = ocrdma_modify_ah;
287 dev->ibdev.poll_cq = ocrdma_poll_cq;
288 dev->ibdev.post_send = ocrdma_post_send;
289 dev->ibdev.post_recv = ocrdma_post_recv;
290 dev->ibdev.req_notify_cq = ocrdma_arm_cq;
292 dev->ibdev.get_dma_mr = ocrdma_get_dma_mr;
293 dev->ibdev.reg_phys_mr = ocrdma_reg_kernel_mr;
294 dev->ibdev.dereg_mr = ocrdma_dereg_mr;
295 dev->ibdev.reg_user_mr = ocrdma_reg_user_mr;
297 dev->ibdev.alloc_fast_reg_mr = ocrdma_alloc_frmr;
298 dev->ibdev.alloc_fast_reg_page_list = ocrdma_alloc_frmr_page_list;
299 dev->ibdev.free_fast_reg_page_list = ocrdma_free_frmr_page_list;
301 /* mandatory to support user space verbs consumer. */
302 dev->ibdev.alloc_ucontext = ocrdma_alloc_ucontext;
303 dev->ibdev.dealloc_ucontext = ocrdma_dealloc_ucontext;
304 dev->ibdev.mmap = ocrdma_mmap;
305 dev->ibdev.dma_device = &dev->nic_info.pdev->dev;
307 dev->ibdev.process_mad = ocrdma_process_mad;
308 dev->ibdev.get_port_immutable = ocrdma_port_immutable;
310 if (ocrdma_get_asic_type(dev) == OCRDMA_ASIC_GEN_SKH_R) {
311 dev->ibdev.uverbs_cmd_mask |=
312 OCRDMA_UVERBS(CREATE_SRQ) |
313 OCRDMA_UVERBS(MODIFY_SRQ) |
314 OCRDMA_UVERBS(QUERY_SRQ) |
315 OCRDMA_UVERBS(DESTROY_SRQ) |
316 OCRDMA_UVERBS(POST_SRQ_RECV);
318 dev->ibdev.create_srq = ocrdma_create_srq;
319 dev->ibdev.modify_srq = ocrdma_modify_srq;
320 dev->ibdev.query_srq = ocrdma_query_srq;
321 dev->ibdev.destroy_srq = ocrdma_destroy_srq;
322 dev->ibdev.post_srq_recv = ocrdma_post_srq_recv;
324 return ib_register_device(&dev->ibdev, NULL);
327 static int ocrdma_alloc_resources(struct ocrdma_dev *dev)
329 mutex_init(&dev->dev_lock);
330 dev->sgid_tbl = kzalloc(sizeof(union ib_gid) *
331 OCRDMA_MAX_SGID, GFP_KERNEL);
334 spin_lock_init(&dev->sgid_lock);
336 dev->cq_tbl = kzalloc(sizeof(struct ocrdma_cq *) *
337 OCRDMA_MAX_CQ, GFP_KERNEL);
341 if (dev->attr.max_qp) {
342 dev->qp_tbl = kzalloc(sizeof(struct ocrdma_qp *) *
343 OCRDMA_MAX_QP, GFP_KERNEL);
348 dev->stag_arr = kzalloc(sizeof(u64) * OCRDMA_MAX_STAG, GFP_KERNEL);
349 if (dev->stag_arr == NULL)
352 ocrdma_alloc_pd_pool(dev);
354 spin_lock_init(&dev->av_tbl.lock);
355 spin_lock_init(&dev->flush_q_lock);
358 pr_err("%s(%d) error.\n", __func__, dev->id);
362 static void ocrdma_free_resources(struct ocrdma_dev *dev)
364 kfree(dev->stag_arr);
367 kfree(dev->sgid_tbl);
370 /* OCRDMA sysfs interface */
371 static ssize_t show_rev(struct device *device, struct device_attribute *attr,
374 struct ocrdma_dev *dev = dev_get_drvdata(device);
376 return scnprintf(buf, PAGE_SIZE, "0x%x\n", dev->nic_info.pdev->vendor);
379 static ssize_t show_fw_ver(struct device *device, struct device_attribute *attr,
382 struct ocrdma_dev *dev = dev_get_drvdata(device);
384 return scnprintf(buf, PAGE_SIZE, "%s\n", &dev->attr.fw_ver[0]);
387 static ssize_t show_hca_type(struct device *device,
388 struct device_attribute *attr, char *buf)
390 struct ocrdma_dev *dev = dev_get_drvdata(device);
392 return scnprintf(buf, PAGE_SIZE, "%s\n", &dev->model_number[0]);
395 static DEVICE_ATTR(hw_rev, S_IRUGO, show_rev, NULL);
396 static DEVICE_ATTR(fw_ver, S_IRUGO, show_fw_ver, NULL);
397 static DEVICE_ATTR(hca_type, S_IRUGO, show_hca_type, NULL);
399 static struct device_attribute *ocrdma_attributes[] = {
405 static void ocrdma_remove_sysfiles(struct ocrdma_dev *dev)
409 for (i = 0; i < ARRAY_SIZE(ocrdma_attributes); i++)
410 device_remove_file(&dev->ibdev.dev, ocrdma_attributes[i]);
413 static void ocrdma_add_default_sgid(struct ocrdma_dev *dev)
415 /* GID Index 0 - Invariant manufacturer-assigned EUI-64 */
416 union ib_gid *sgid = &dev->sgid_tbl[0];
418 sgid->global.subnet_prefix = cpu_to_be64(0xfe80000000000000LL);
419 ocrdma_get_guid(dev, &sgid->raw[8]);
422 static void ocrdma_init_ipv4_gids(struct ocrdma_dev *dev,
423 struct net_device *net)
425 struct in_device *in_dev;
427 in_dev = in_dev_get(net);
430 ipv6_addr_set_v4mapped(ifa->ifa_address,
431 (struct in6_addr *)&gid);
432 ocrdma_add_sgid(dev, &gid);
439 static void ocrdma_init_ipv6_gids(struct ocrdma_dev *dev,
440 struct net_device *net)
442 #if IS_ENABLED(CONFIG_IPV6)
443 struct inet6_dev *in6_dev;
445 struct inet6_ifaddr *ifp;
446 in6_dev = in6_dev_get(net);
448 read_lock_bh(&in6_dev->lock);
449 list_for_each_entry(ifp, &in6_dev->addr_list, if_list) {
450 pgid = (union ib_gid *)&ifp->addr;
451 ocrdma_add_sgid(dev, pgid);
453 read_unlock_bh(&in6_dev->lock);
454 in6_dev_put(in6_dev);
459 static void ocrdma_init_gid_table(struct ocrdma_dev *dev)
461 struct net_device *net_dev;
463 for_each_netdev(&init_net, net_dev) {
464 struct net_device *real_dev = rdma_vlan_dev_real_dev(net_dev) ?
465 rdma_vlan_dev_real_dev(net_dev) : net_dev;
467 if (real_dev == dev->nic_info.netdev) {
468 ocrdma_add_default_sgid(dev);
469 ocrdma_init_ipv4_gids(dev, net_dev);
470 ocrdma_init_ipv6_gids(dev, net_dev);
475 static struct ocrdma_dev *ocrdma_add(struct be_dev_info *dev_info)
478 struct ocrdma_dev *dev;
480 dev = (struct ocrdma_dev *)ib_alloc_device(sizeof(struct ocrdma_dev));
482 pr_err("Unable to allocate ib device\n");
485 dev->mbx_cmd = kzalloc(sizeof(struct ocrdma_mqe_emb_cmd), GFP_KERNEL);
489 memcpy(&dev->nic_info, dev_info, sizeof(*dev_info));
490 dev->id = idr_alloc(&ocrdma_dev_id, NULL, 0, 0, GFP_KERNEL);
494 status = ocrdma_init_hw(dev);
498 status = ocrdma_alloc_resources(dev);
502 ocrdma_init_service_level(dev);
503 ocrdma_init_gid_table(dev);
504 status = ocrdma_register_device(dev);
508 for (i = 0; i < ARRAY_SIZE(ocrdma_attributes); i++)
509 if (device_create_file(&dev->ibdev.dev, ocrdma_attributes[i]))
511 spin_lock(&ocrdma_devlist_lock);
512 list_add_tail_rcu(&dev->entry, &ocrdma_dev_list);
513 spin_unlock(&ocrdma_devlist_lock);
515 ocrdma_add_port_stats(dev);
516 /* Interrupt Moderation */
517 INIT_DELAYED_WORK(&dev->eqd_work, ocrdma_eqd_set_task);
518 schedule_delayed_work(&dev->eqd_work, msecs_to_jiffies(1000));
520 pr_info("%s %s: %s \"%s\" port %d\n",
521 dev_name(&dev->nic_info.pdev->dev), hca_name(dev),
522 port_speed_string(dev), dev->model_number,
524 pr_info("%s ocrdma%d driver loaded successfully\n",
525 dev_name(&dev->nic_info.pdev->dev), dev->id);
529 ocrdma_remove_sysfiles(dev);
531 ocrdma_free_resources(dev);
532 ocrdma_cleanup_hw(dev);
534 idr_remove(&ocrdma_dev_id, dev->id);
537 ib_dealloc_device(&dev->ibdev);
538 pr_err("%s() leaving. ret=%d\n", __func__, status);
542 static void ocrdma_remove_free(struct rcu_head *rcu)
544 struct ocrdma_dev *dev = container_of(rcu, struct ocrdma_dev, rcu);
546 idr_remove(&ocrdma_dev_id, dev->id);
548 ib_dealloc_device(&dev->ibdev);
551 static void ocrdma_remove(struct ocrdma_dev *dev)
553 /* first unregister with stack to stop all the active traffic
554 * of the registered clients.
556 cancel_delayed_work_sync(&dev->eqd_work);
557 ocrdma_remove_sysfiles(dev);
558 ib_unregister_device(&dev->ibdev);
560 ocrdma_rem_port_stats(dev);
562 spin_lock(&ocrdma_devlist_lock);
563 list_del_rcu(&dev->entry);
564 spin_unlock(&ocrdma_devlist_lock);
566 ocrdma_free_resources(dev);
567 ocrdma_cleanup_hw(dev);
569 call_rcu(&dev->rcu, ocrdma_remove_free);
572 static int ocrdma_open(struct ocrdma_dev *dev)
574 struct ib_event port_event;
576 port_event.event = IB_EVENT_PORT_ACTIVE;
577 port_event.element.port_num = 1;
578 port_event.device = &dev->ibdev;
579 ib_dispatch_event(&port_event);
583 static int ocrdma_close(struct ocrdma_dev *dev)
586 struct ocrdma_qp *qp, **cur_qp;
587 struct ib_event err_event;
588 struct ib_qp_attr attrs;
589 int attr_mask = IB_QP_STATE;
591 attrs.qp_state = IB_QPS_ERR;
592 mutex_lock(&dev->dev_lock);
594 cur_qp = dev->qp_tbl;
595 for (i = 0; i < OCRDMA_MAX_QP; i++) {
597 if (qp && qp->ibqp.qp_type != IB_QPT_GSI) {
598 /* change the QP state to ERROR */
599 _ocrdma_modify_qp(&qp->ibqp, &attrs, attr_mask);
601 err_event.event = IB_EVENT_QP_FATAL;
602 err_event.element.qp = &qp->ibqp;
603 err_event.device = &dev->ibdev;
604 ib_dispatch_event(&err_event);
608 mutex_unlock(&dev->dev_lock);
610 err_event.event = IB_EVENT_PORT_ERR;
611 err_event.element.port_num = 1;
612 err_event.device = &dev->ibdev;
613 ib_dispatch_event(&err_event);
617 static void ocrdma_shutdown(struct ocrdma_dev *dev)
623 /* event handling via NIC driver ensures that all the NIC specific
624 * initialization done before RoCE driver notifies
627 static void ocrdma_event_handler(struct ocrdma_dev *dev, u32 event)
636 case BE_DEV_SHUTDOWN:
637 ocrdma_shutdown(dev);
642 static struct ocrdma_driver ocrdma_drv = {
643 .name = "ocrdma_driver",
645 .remove = ocrdma_remove,
646 .state_change_handler = ocrdma_event_handler,
647 .be_abi_version = OCRDMA_BE_ROCE_ABI_VERSION,
650 static void ocrdma_unregister_inet6addr_notifier(void)
652 #if IS_ENABLED(CONFIG_IPV6)
653 unregister_inet6addr_notifier(&ocrdma_inet6addr_notifier);
657 static void ocrdma_unregister_inetaddr_notifier(void)
659 unregister_inetaddr_notifier(&ocrdma_inetaddr_notifier);
662 static int __init ocrdma_init_module(void)
666 ocrdma_init_debugfs();
668 status = register_inetaddr_notifier(&ocrdma_inetaddr_notifier);
672 #if IS_ENABLED(CONFIG_IPV6)
673 status = register_inet6addr_notifier(&ocrdma_inet6addr_notifier);
678 status = be_roce_register_driver(&ocrdma_drv);
685 #if IS_ENABLED(CONFIG_IPV6)
686 ocrdma_unregister_inet6addr_notifier();
689 ocrdma_unregister_inetaddr_notifier();
693 static void __exit ocrdma_exit_module(void)
695 be_roce_unregister_driver(&ocrdma_drv);
696 ocrdma_unregister_inet6addr_notifier();
697 ocrdma_unregister_inetaddr_notifier();
698 ocrdma_rem_debugfs();
699 idr_destroy(&ocrdma_dev_id);
702 module_init(ocrdma_init_module);
703 module_exit(ocrdma_exit_module);