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/version.h>
30 #include <linux/idr.h>
31 #include <rdma/ib_verbs.h>
32 #include <rdma/ib_user_verbs.h>
33 #include <rdma/ib_addr.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"
44 MODULE_VERSION(OCRDMA_ROCE_DEV_VERSION);
45 MODULE_DESCRIPTION("Emulex RoCE HCA Driver");
46 MODULE_AUTHOR("Emulex Corporation");
47 MODULE_LICENSE("GPL");
49 static LIST_HEAD(ocrdma_dev_list);
50 static DEFINE_MUTEX(ocrdma_devlist_lock);
51 static DEFINE_IDR(ocrdma_dev_id);
53 static union ib_gid ocrdma_zero_sgid;
54 static int ocrdma_inet6addr_event(struct notifier_block *,
55 unsigned long, void *);
57 static struct notifier_block ocrdma_inet6addr_notifier = {
58 .notifier_call = ocrdma_inet6addr_event
61 int ocrdma_get_instance(void)
65 /* Assign an unused number */
66 if (!idr_pre_get(&ocrdma_dev_id, GFP_KERNEL))
68 if (idr_get_new(&ocrdma_dev_id, NULL, &instance))
73 void ocrdma_get_guid(struct ocrdma_dev *dev, u8 *guid)
77 memcpy(&mac_addr[0], &dev->nic_info.mac_addr[0], ETH_ALEN);
78 guid[0] = mac_addr[0] ^ 2;
79 guid[1] = mac_addr[1];
80 guid[2] = mac_addr[2];
83 guid[5] = mac_addr[3];
84 guid[6] = mac_addr[4];
85 guid[7] = mac_addr[5];
88 static void ocrdma_build_sgid_mac(union ib_gid *sgid, unsigned char *mac_addr,
89 bool is_vlan, u16 vlan_id)
91 sgid->global.subnet_prefix = cpu_to_be64(0xfe80000000000000LL);
92 sgid->raw[8] = mac_addr[0] ^ 2;
93 sgid->raw[9] = mac_addr[1];
94 sgid->raw[10] = mac_addr[2];
96 sgid->raw[11] = vlan_id >> 8;
97 sgid->raw[12] = vlan_id & 0xff;
100 sgid->raw[12] = 0xfe;
102 sgid->raw[13] = mac_addr[3];
103 sgid->raw[14] = mac_addr[4];
104 sgid->raw[15] = mac_addr[5];
107 static void ocrdma_add_sgid(struct ocrdma_dev *dev, unsigned char *mac_addr,
108 bool is_vlan, u16 vlan_id)
112 union ib_gid new_sgid;
113 int free_idx = OCRDMA_MAX_SGID;
116 memset(&ocrdma_zero_sgid, 0, sizeof(union ib_gid));
118 ocrdma_build_sgid_mac(&new_sgid, mac_addr, is_vlan, vlan_id);
120 spin_lock_irqsave(&dev->sgid_lock, flags);
121 for (i = 0; i < OCRDMA_MAX_SGID; i++) {
122 if (!memcmp(&dev->sgid_tbl[i], &ocrdma_zero_sgid,
123 sizeof(union ib_gid))) {
124 /* found free entry */
130 } else if (!memcmp(&dev->sgid_tbl[i], &new_sgid,
131 sizeof(union ib_gid))) {
132 /* entry already present, no addition is required. */
133 spin_unlock_irqrestore(&dev->sgid_lock, flags);
137 /* if entry doesn't exist and if table has some space, add entry */
139 memcpy(&dev->sgid_tbl[free_idx], &new_sgid,
140 sizeof(union ib_gid));
141 spin_unlock_irqrestore(&dev->sgid_lock, flags);
144 static bool ocrdma_del_sgid(struct ocrdma_dev *dev, unsigned char *mac_addr,
145 bool is_vlan, u16 vlan_id)
152 ocrdma_build_sgid_mac(&sgid, mac_addr, is_vlan, vlan_id);
154 spin_lock_irqsave(&dev->sgid_lock, flags);
155 /* first is default sgid, which cannot be deleted. */
156 for (i = 1; i < OCRDMA_MAX_SGID; i++) {
157 if (!memcmp(&dev->sgid_tbl[i], &sgid, sizeof(union ib_gid))) {
158 /* found matching entry */
159 memset(&dev->sgid_tbl[i], 0, sizeof(union ib_gid));
164 spin_unlock_irqrestore(&dev->sgid_lock, flags);
168 static void ocrdma_add_default_sgid(struct ocrdma_dev *dev)
170 /* GID Index 0 - Invariant manufacturer-assigned EUI-64 */
171 union ib_gid *sgid = &dev->sgid_tbl[0];
173 sgid->global.subnet_prefix = cpu_to_be64(0xfe80000000000000LL);
174 ocrdma_get_guid(dev, &sgid->raw[8]);
177 static int ocrdma_build_sgid_tbl(struct ocrdma_dev *dev)
179 struct net_device *netdev, *tmp;
183 netdev = dev->nic_info.netdev;
185 ocrdma_add_default_sgid(dev);
188 for_each_netdev_rcu(&init_net, tmp) {
189 if (netdev == tmp || vlan_dev_real_dev(tmp) == netdev) {
190 if (!netif_running(tmp) || !netif_oper_up(tmp))
193 vlan_id = vlan_dev_vlan_id(tmp);
200 ocrdma_add_sgid(dev, tmp->dev_addr, is_vlan, vlan_id);
207 static int ocrdma_inet6addr_event(struct notifier_block *notifier,
208 unsigned long event, void *ptr)
210 struct inet6_ifaddr *ifa = (struct inet6_ifaddr *)ptr;
211 struct net_device *event_netdev = ifa->idev->dev;
212 struct net_device *netdev = NULL;
213 struct ib_event gid_event;
214 struct ocrdma_dev *dev;
216 bool is_vlan = false;
219 netdev = vlan_dev_real_dev(event_netdev);
220 if (netdev != event_netdev) {
222 vid = vlan_dev_vlan_id(event_netdev);
224 mutex_lock(&ocrdma_devlist_lock);
225 list_for_each_entry(dev, &ocrdma_dev_list, entry) {
226 if (dev->nic_info.netdev == netdev) {
231 mutex_unlock(&ocrdma_devlist_lock);
235 if (!rdma_link_local_addr((struct in6_addr *)&ifa->addr))
238 mutex_lock(&dev->dev_lock);
241 ocrdma_add_sgid(dev, netdev->dev_addr, is_vlan, vid);
244 found = ocrdma_del_sgid(dev, netdev->dev_addr, is_vlan, vid);
246 /* found the matching entry, notify
247 * the consumers about it
249 gid_event.device = &dev->ibdev;
250 gid_event.element.port_num = 1;
251 gid_event.event = IB_EVENT_GID_CHANGE;
252 ib_dispatch_event(&gid_event);
258 mutex_unlock(&dev->dev_lock);
262 static enum rdma_link_layer ocrdma_link_layer(struct ib_device *device,
265 return IB_LINK_LAYER_ETHERNET;
268 int ocrdma_register_device(struct ocrdma_dev *dev)
270 strlcpy(dev->ibdev.name, "ocrdma%d", IB_DEVICE_NAME_MAX);
271 ocrdma_get_guid(dev, (u8 *)&dev->ibdev.node_guid);
272 memcpy(dev->ibdev.node_desc, OCRDMA_NODE_DESC,
273 sizeof(OCRDMA_NODE_DESC));
274 dev->ibdev.owner = THIS_MODULE;
275 dev->ibdev.uverbs_cmd_mask =
276 OCRDMA_UVERBS(GET_CONTEXT) |
277 OCRDMA_UVERBS(QUERY_DEVICE) |
278 OCRDMA_UVERBS(QUERY_PORT) |
279 OCRDMA_UVERBS(ALLOC_PD) |
280 OCRDMA_UVERBS(DEALLOC_PD) |
281 OCRDMA_UVERBS(REG_MR) |
282 OCRDMA_UVERBS(DEREG_MR) |
283 OCRDMA_UVERBS(CREATE_COMP_CHANNEL) |
284 OCRDMA_UVERBS(CREATE_CQ) |
285 OCRDMA_UVERBS(RESIZE_CQ) |
286 OCRDMA_UVERBS(DESTROY_CQ) |
287 OCRDMA_UVERBS(REQ_NOTIFY_CQ) |
288 OCRDMA_UVERBS(CREATE_QP) |
289 OCRDMA_UVERBS(MODIFY_QP) |
290 OCRDMA_UVERBS(QUERY_QP) |
291 OCRDMA_UVERBS(DESTROY_QP) |
292 OCRDMA_UVERBS(POLL_CQ) |
293 OCRDMA_UVERBS(POST_SEND) |
294 OCRDMA_UVERBS(POST_RECV);
296 dev->ibdev.uverbs_cmd_mask |=
297 OCRDMA_UVERBS(CREATE_AH) |
298 OCRDMA_UVERBS(MODIFY_AH) |
299 OCRDMA_UVERBS(QUERY_AH) |
300 OCRDMA_UVERBS(DESTROY_AH);
302 dev->ibdev.node_type = RDMA_NODE_IB_CA;
303 dev->ibdev.phys_port_cnt = 1;
304 dev->ibdev.num_comp_vectors = 1;
306 /* mandatory verbs. */
307 dev->ibdev.query_device = ocrdma_query_device;
308 dev->ibdev.query_port = ocrdma_query_port;
309 dev->ibdev.modify_port = ocrdma_modify_port;
310 dev->ibdev.query_gid = ocrdma_query_gid;
311 dev->ibdev.get_link_layer = ocrdma_link_layer;
312 dev->ibdev.alloc_pd = ocrdma_alloc_pd;
313 dev->ibdev.dealloc_pd = ocrdma_dealloc_pd;
315 dev->ibdev.create_cq = ocrdma_create_cq;
316 dev->ibdev.destroy_cq = ocrdma_destroy_cq;
317 dev->ibdev.resize_cq = ocrdma_resize_cq;
319 dev->ibdev.create_qp = ocrdma_create_qp;
320 dev->ibdev.modify_qp = ocrdma_modify_qp;
321 dev->ibdev.query_qp = ocrdma_query_qp;
322 dev->ibdev.destroy_qp = ocrdma_destroy_qp;
324 dev->ibdev.query_pkey = ocrdma_query_pkey;
325 dev->ibdev.create_ah = ocrdma_create_ah;
326 dev->ibdev.destroy_ah = ocrdma_destroy_ah;
327 dev->ibdev.query_ah = ocrdma_query_ah;
328 dev->ibdev.modify_ah = ocrdma_modify_ah;
330 dev->ibdev.poll_cq = ocrdma_poll_cq;
331 dev->ibdev.post_send = ocrdma_post_send;
332 dev->ibdev.post_recv = ocrdma_post_recv;
333 dev->ibdev.req_notify_cq = ocrdma_arm_cq;
335 dev->ibdev.get_dma_mr = ocrdma_get_dma_mr;
336 dev->ibdev.dereg_mr = ocrdma_dereg_mr;
337 dev->ibdev.reg_user_mr = ocrdma_reg_user_mr;
339 /* mandatory to support user space verbs consumer. */
340 dev->ibdev.alloc_ucontext = ocrdma_alloc_ucontext;
341 dev->ibdev.dealloc_ucontext = ocrdma_dealloc_ucontext;
342 dev->ibdev.mmap = ocrdma_mmap;
343 dev->ibdev.dma_device = &dev->nic_info.pdev->dev;
345 dev->ibdev.process_mad = ocrdma_process_mad;
347 if (dev->nic_info.dev_family == OCRDMA_GEN2_FAMILY) {
348 dev->ibdev.uverbs_cmd_mask |=
349 OCRDMA_UVERBS(CREATE_SRQ) |
350 OCRDMA_UVERBS(MODIFY_SRQ) |
351 OCRDMA_UVERBS(QUERY_SRQ) |
352 OCRDMA_UVERBS(DESTROY_SRQ) |
353 OCRDMA_UVERBS(POST_SRQ_RECV);
355 dev->ibdev.create_srq = ocrdma_create_srq;
356 dev->ibdev.modify_srq = ocrdma_modify_srq;
357 dev->ibdev.query_srq = ocrdma_query_srq;
358 dev->ibdev.destroy_srq = ocrdma_destroy_srq;
359 dev->ibdev.post_srq_recv = ocrdma_post_srq_recv;
361 return ib_register_device(&dev->ibdev, NULL);
364 static int ocrdma_alloc_resources(struct ocrdma_dev *dev)
366 mutex_init(&dev->dev_lock);
367 dev->sgid_tbl = kzalloc(sizeof(union ib_gid) *
368 OCRDMA_MAX_SGID, GFP_KERNEL);
371 spin_lock_init(&dev->sgid_lock);
373 dev->cq_tbl = kzalloc(sizeof(struct ocrdma_cq *) *
374 OCRDMA_MAX_CQ, GFP_KERNEL);
378 if (dev->attr.max_qp) {
379 dev->qp_tbl = kzalloc(sizeof(struct ocrdma_qp *) *
380 OCRDMA_MAX_QP, GFP_KERNEL);
384 spin_lock_init(&dev->av_tbl.lock);
385 spin_lock_init(&dev->flush_q_lock);
388 ocrdma_err("%s(%d) error.\n", __func__, dev->id);
392 static void ocrdma_free_resources(struct ocrdma_dev *dev)
396 kfree(dev->sgid_tbl);
399 static struct ocrdma_dev *ocrdma_add(struct be_dev_info *dev_info)
402 struct ocrdma_dev *dev;
404 dev = (struct ocrdma_dev *)ib_alloc_device(sizeof(struct ocrdma_dev));
406 ocrdma_err("Unable to allocate ib device\n");
409 dev->mbx_cmd = kzalloc(sizeof(struct ocrdma_mqe_emb_cmd), GFP_KERNEL);
413 memcpy(&dev->nic_info, dev_info, sizeof(*dev_info));
414 dev->id = ocrdma_get_instance();
418 status = ocrdma_init_hw(dev);
422 status = ocrdma_alloc_resources(dev);
426 status = ocrdma_build_sgid_tbl(dev);
430 status = ocrdma_register_device(dev);
434 mutex_lock(&ocrdma_devlist_lock);
435 list_add_tail(&dev->entry, &ocrdma_dev_list);
436 mutex_unlock(&ocrdma_devlist_lock);
440 ocrdma_free_resources(dev);
441 ocrdma_cleanup_hw(dev);
443 idr_remove(&ocrdma_dev_id, dev->id);
446 ib_dealloc_device(&dev->ibdev);
447 ocrdma_err("%s() leaving. ret=%d\n", __func__, status);
451 static void ocrdma_remove(struct ocrdma_dev *dev)
453 /* first unregister with stack to stop all the active traffic
454 * of the registered clients.
456 ib_unregister_device(&dev->ibdev);
458 mutex_lock(&ocrdma_devlist_lock);
459 list_del(&dev->entry);
460 mutex_unlock(&ocrdma_devlist_lock);
462 ocrdma_free_resources(dev);
463 ocrdma_cleanup_hw(dev);
465 idr_remove(&ocrdma_dev_id, dev->id);
467 ib_dealloc_device(&dev->ibdev);
470 static int ocrdma_open(struct ocrdma_dev *dev)
472 struct ib_event port_event;
474 port_event.event = IB_EVENT_PORT_ACTIVE;
475 port_event.element.port_num = 1;
476 port_event.device = &dev->ibdev;
477 ib_dispatch_event(&port_event);
481 static int ocrdma_close(struct ocrdma_dev *dev)
484 struct ocrdma_qp *qp, **cur_qp;
485 struct ib_event err_event;
486 struct ib_qp_attr attrs;
487 int attr_mask = IB_QP_STATE;
489 attrs.qp_state = IB_QPS_ERR;
490 mutex_lock(&dev->dev_lock);
492 cur_qp = dev->qp_tbl;
493 for (i = 0; i < OCRDMA_MAX_QP; i++) {
496 /* change the QP state to ERROR */
497 _ocrdma_modify_qp(&qp->ibqp, &attrs, attr_mask);
499 err_event.event = IB_EVENT_QP_FATAL;
500 err_event.element.qp = &qp->ibqp;
501 err_event.device = &dev->ibdev;
502 ib_dispatch_event(&err_event);
506 mutex_unlock(&dev->dev_lock);
508 err_event.event = IB_EVENT_PORT_ERR;
509 err_event.element.port_num = 1;
510 err_event.device = &dev->ibdev;
511 ib_dispatch_event(&err_event);
515 /* event handling via NIC driver ensures that all the NIC specific
516 * initialization done before RoCE driver notifies
519 static void ocrdma_event_handler(struct ocrdma_dev *dev, u32 event)
531 struct ocrdma_driver ocrdma_drv = {
532 .name = "ocrdma_driver",
534 .remove = ocrdma_remove,
535 .state_change_handler = ocrdma_event_handler,
538 static int __init ocrdma_init_module(void)
542 status = register_inet6addr_notifier(&ocrdma_inet6addr_notifier);
545 status = be_roce_register_driver(&ocrdma_drv);
547 unregister_inet6addr_notifier(&ocrdma_inet6addr_notifier);
551 static void __exit ocrdma_exit_module(void)
553 be_roce_unregister_driver(&ocrdma_drv);
554 unregister_inet6addr_notifier(&ocrdma_inet6addr_notifier);
557 module_init(ocrdma_init_module);
558 module_exit(ocrdma_exit_module);