Merge tag 'for-linus-4.3' of git://git.code.sf.net/p/openipmi/linux-ipmi
[linux-2.6-microblaze.git] / drivers / infiniband / hw / ocrdma / ocrdma_main.c
1 /* This file is part of the Emulex RoCE Device Driver for
2  * RoCE (RDMA over Converged Ethernet) adapters.
3  * Copyright (C) 2012-2015 Emulex. All rights reserved.
4  * EMULEX and SLI are trademarks of Emulex.
5  * www.emulex.com
6  *
7  * This software is available to you under a choice of one of two licenses.
8  * You may choose to be licensed under the terms of the GNU General Public
9  * License (GPL) Version 2, available from the file COPYING in the main
10  * directory of this source tree, or the BSD license below:
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  *
16  * - Redistributions of source code must retain the above copyright notice,
17  *   this list of conditions and the following disclaimer.
18  *
19  * - Redistributions in binary form must reproduce the above copyright
20  *   notice, this list of conditions and the following disclaimer in
21  *   the documentation and/or other materials provided with the distribution.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
27  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
30  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
31  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
32  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
33  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34  *
35  * Contact Information:
36  * linux-drivers@emulex.com
37  *
38  * Emulex
39  * 3333 Susan Street
40  * Costa Mesa, CA 92626
41  */
42
43 #include <linux/module.h>
44 #include <linux/idr.h>
45 #include <rdma/ib_verbs.h>
46 #include <rdma/ib_user_verbs.h>
47 #include <rdma/ib_addr.h>
48 #include <rdma/ib_mad.h>
49
50 #include <linux/netdevice.h>
51 #include <net/addrconf.h>
52
53 #include "ocrdma.h"
54 #include "ocrdma_verbs.h"
55 #include "ocrdma_ah.h"
56 #include "be_roce.h"
57 #include "ocrdma_hw.h"
58 #include "ocrdma_stats.h"
59 #include "ocrdma_abi.h"
60
61 MODULE_VERSION(OCRDMA_ROCE_DRV_VERSION);
62 MODULE_DESCRIPTION(OCRDMA_ROCE_DRV_DESC " " OCRDMA_ROCE_DRV_VERSION);
63 MODULE_AUTHOR("Emulex Corporation");
64 MODULE_LICENSE("Dual BSD/GPL");
65
66 static LIST_HEAD(ocrdma_dev_list);
67 static DEFINE_SPINLOCK(ocrdma_devlist_lock);
68 static DEFINE_IDR(ocrdma_dev_id);
69
70 static union ib_gid ocrdma_zero_sgid;
71
72 void ocrdma_get_guid(struct ocrdma_dev *dev, u8 *guid)
73 {
74         u8 mac_addr[6];
75
76         memcpy(&mac_addr[0], &dev->nic_info.mac_addr[0], ETH_ALEN);
77         guid[0] = mac_addr[0] ^ 2;
78         guid[1] = mac_addr[1];
79         guid[2] = mac_addr[2];
80         guid[3] = 0xff;
81         guid[4] = 0xfe;
82         guid[5] = mac_addr[3];
83         guid[6] = mac_addr[4];
84         guid[7] = mac_addr[5];
85 }
86
87 static bool ocrdma_add_sgid(struct ocrdma_dev *dev, union ib_gid *new_sgid)
88 {
89         int i;
90         unsigned long flags;
91
92         memset(&ocrdma_zero_sgid, 0, sizeof(union ib_gid));
93
94
95         spin_lock_irqsave(&dev->sgid_lock, flags);
96         for (i = 0; i < OCRDMA_MAX_SGID; i++) {
97                 if (!memcmp(&dev->sgid_tbl[i], &ocrdma_zero_sgid,
98                             sizeof(union ib_gid))) {
99                         /* found free entry */
100                         memcpy(&dev->sgid_tbl[i], new_sgid,
101                                sizeof(union ib_gid));
102                         spin_unlock_irqrestore(&dev->sgid_lock, flags);
103                         return true;
104                 } else if (!memcmp(&dev->sgid_tbl[i], new_sgid,
105                                    sizeof(union ib_gid))) {
106                         /* entry already present, no addition is required. */
107                         spin_unlock_irqrestore(&dev->sgid_lock, flags);
108                         return false;
109                 }
110         }
111         spin_unlock_irqrestore(&dev->sgid_lock, flags);
112         return false;
113 }
114
115 static bool ocrdma_del_sgid(struct ocrdma_dev *dev, union ib_gid *sgid)
116 {
117         int found = false;
118         int i;
119         unsigned long flags;
120
121
122         spin_lock_irqsave(&dev->sgid_lock, flags);
123         /* first is default sgid, which cannot be deleted. */
124         for (i = 1; i < OCRDMA_MAX_SGID; i++) {
125                 if (!memcmp(&dev->sgid_tbl[i], sgid, sizeof(union ib_gid))) {
126                         /* found matching entry */
127                         memset(&dev->sgid_tbl[i], 0, sizeof(union ib_gid));
128                         found = true;
129                         break;
130                 }
131         }
132         spin_unlock_irqrestore(&dev->sgid_lock, flags);
133         return found;
134 }
135
136 static int ocrdma_addr_event(unsigned long event, struct net_device *netdev,
137                              union ib_gid *gid)
138 {
139         struct ib_event gid_event;
140         struct ocrdma_dev *dev;
141         bool found = false;
142         bool updated = false;
143         bool is_vlan = false;
144
145         is_vlan = netdev->priv_flags & IFF_802_1Q_VLAN;
146         if (is_vlan)
147                 netdev = rdma_vlan_dev_real_dev(netdev);
148
149         rcu_read_lock();
150         list_for_each_entry_rcu(dev, &ocrdma_dev_list, entry) {
151                 if (dev->nic_info.netdev == netdev) {
152                         found = true;
153                         break;
154                 }
155         }
156         rcu_read_unlock();
157
158         if (!found)
159                 return NOTIFY_DONE;
160
161         mutex_lock(&dev->dev_lock);
162         switch (event) {
163         case NETDEV_UP:
164                 updated = ocrdma_add_sgid(dev, gid);
165                 break;
166         case NETDEV_DOWN:
167                 updated = ocrdma_del_sgid(dev, gid);
168                 break;
169         default:
170                 break;
171         }
172         if (updated) {
173                 /* GID table updated, notify the consumers about it */
174                 gid_event.device = &dev->ibdev;
175                 gid_event.element.port_num = 1;
176                 gid_event.event = IB_EVENT_GID_CHANGE;
177                 ib_dispatch_event(&gid_event);
178         }
179         mutex_unlock(&dev->dev_lock);
180         return NOTIFY_OK;
181 }
182
183 static int ocrdma_inetaddr_event(struct notifier_block *notifier,
184                                   unsigned long event, void *ptr)
185 {
186         struct in_ifaddr *ifa = ptr;
187         union ib_gid gid;
188         struct net_device *netdev = ifa->ifa_dev->dev;
189
190         ipv6_addr_set_v4mapped(ifa->ifa_address, (struct in6_addr *)&gid);
191         return ocrdma_addr_event(event, netdev, &gid);
192 }
193
194 static struct notifier_block ocrdma_inetaddr_notifier = {
195         .notifier_call = ocrdma_inetaddr_event
196 };
197
198 #if IS_ENABLED(CONFIG_IPV6)
199
200 static int ocrdma_inet6addr_event(struct notifier_block *notifier,
201                                   unsigned long event, void *ptr)
202 {
203         struct inet6_ifaddr *ifa = (struct inet6_ifaddr *)ptr;
204         union  ib_gid *gid = (union ib_gid *)&ifa->addr;
205         struct net_device *netdev = ifa->idev->dev;
206         return ocrdma_addr_event(event, netdev, gid);
207 }
208
209 static struct notifier_block ocrdma_inet6addr_notifier = {
210         .notifier_call = ocrdma_inet6addr_event
211 };
212
213 #endif /* IPV6 and VLAN */
214
215 static enum rdma_link_layer ocrdma_link_layer(struct ib_device *device,
216                                               u8 port_num)
217 {
218         return IB_LINK_LAYER_ETHERNET;
219 }
220
221 static int ocrdma_port_immutable(struct ib_device *ibdev, u8 port_num,
222                                  struct ib_port_immutable *immutable)
223 {
224         struct ib_port_attr attr;
225         int err;
226
227         err = ocrdma_query_port(ibdev, port_num, &attr);
228         if (err)
229                 return err;
230
231         immutable->pkey_tbl_len = attr.pkey_tbl_len;
232         immutable->gid_tbl_len = attr.gid_tbl_len;
233         immutable->core_cap_flags = RDMA_CORE_PORT_IBA_ROCE;
234         immutable->max_mad_size = IB_MGMT_MAD_SIZE;
235
236         return 0;
237 }
238
239 static int ocrdma_register_device(struct ocrdma_dev *dev)
240 {
241         strlcpy(dev->ibdev.name, "ocrdma%d", IB_DEVICE_NAME_MAX);
242         ocrdma_get_guid(dev, (u8 *)&dev->ibdev.node_guid);
243         memcpy(dev->ibdev.node_desc, OCRDMA_NODE_DESC,
244                sizeof(OCRDMA_NODE_DESC));
245         dev->ibdev.owner = THIS_MODULE;
246         dev->ibdev.uverbs_abi_ver = OCRDMA_ABI_VERSION;
247         dev->ibdev.uverbs_cmd_mask =
248             OCRDMA_UVERBS(GET_CONTEXT) |
249             OCRDMA_UVERBS(QUERY_DEVICE) |
250             OCRDMA_UVERBS(QUERY_PORT) |
251             OCRDMA_UVERBS(ALLOC_PD) |
252             OCRDMA_UVERBS(DEALLOC_PD) |
253             OCRDMA_UVERBS(REG_MR) |
254             OCRDMA_UVERBS(DEREG_MR) |
255             OCRDMA_UVERBS(CREATE_COMP_CHANNEL) |
256             OCRDMA_UVERBS(CREATE_CQ) |
257             OCRDMA_UVERBS(RESIZE_CQ) |
258             OCRDMA_UVERBS(DESTROY_CQ) |
259             OCRDMA_UVERBS(REQ_NOTIFY_CQ) |
260             OCRDMA_UVERBS(CREATE_QP) |
261             OCRDMA_UVERBS(MODIFY_QP) |
262             OCRDMA_UVERBS(QUERY_QP) |
263             OCRDMA_UVERBS(DESTROY_QP) |
264             OCRDMA_UVERBS(POLL_CQ) |
265             OCRDMA_UVERBS(POST_SEND) |
266             OCRDMA_UVERBS(POST_RECV);
267
268         dev->ibdev.uverbs_cmd_mask |=
269             OCRDMA_UVERBS(CREATE_AH) |
270              OCRDMA_UVERBS(MODIFY_AH) |
271              OCRDMA_UVERBS(QUERY_AH) |
272              OCRDMA_UVERBS(DESTROY_AH);
273
274         dev->ibdev.node_type = RDMA_NODE_IB_CA;
275         dev->ibdev.phys_port_cnt = 1;
276         dev->ibdev.num_comp_vectors = dev->eq_cnt;
277
278         /* mandatory verbs. */
279         dev->ibdev.query_device = ocrdma_query_device;
280         dev->ibdev.query_port = ocrdma_query_port;
281         dev->ibdev.modify_port = ocrdma_modify_port;
282         dev->ibdev.query_gid = ocrdma_query_gid;
283         dev->ibdev.get_link_layer = ocrdma_link_layer;
284         dev->ibdev.alloc_pd = ocrdma_alloc_pd;
285         dev->ibdev.dealloc_pd = ocrdma_dealloc_pd;
286
287         dev->ibdev.create_cq = ocrdma_create_cq;
288         dev->ibdev.destroy_cq = ocrdma_destroy_cq;
289         dev->ibdev.resize_cq = ocrdma_resize_cq;
290
291         dev->ibdev.create_qp = ocrdma_create_qp;
292         dev->ibdev.modify_qp = ocrdma_modify_qp;
293         dev->ibdev.query_qp = ocrdma_query_qp;
294         dev->ibdev.destroy_qp = ocrdma_destroy_qp;
295
296         dev->ibdev.query_pkey = ocrdma_query_pkey;
297         dev->ibdev.create_ah = ocrdma_create_ah;
298         dev->ibdev.destroy_ah = ocrdma_destroy_ah;
299         dev->ibdev.query_ah = ocrdma_query_ah;
300         dev->ibdev.modify_ah = ocrdma_modify_ah;
301
302         dev->ibdev.poll_cq = ocrdma_poll_cq;
303         dev->ibdev.post_send = ocrdma_post_send;
304         dev->ibdev.post_recv = ocrdma_post_recv;
305         dev->ibdev.req_notify_cq = ocrdma_arm_cq;
306
307         dev->ibdev.get_dma_mr = ocrdma_get_dma_mr;
308         dev->ibdev.reg_phys_mr = ocrdma_reg_kernel_mr;
309         dev->ibdev.dereg_mr = ocrdma_dereg_mr;
310         dev->ibdev.reg_user_mr = ocrdma_reg_user_mr;
311
312         dev->ibdev.alloc_fast_reg_mr = ocrdma_alloc_frmr;
313         dev->ibdev.alloc_fast_reg_page_list = ocrdma_alloc_frmr_page_list;
314         dev->ibdev.free_fast_reg_page_list = ocrdma_free_frmr_page_list;
315
316         /* mandatory to support user space verbs consumer. */
317         dev->ibdev.alloc_ucontext = ocrdma_alloc_ucontext;
318         dev->ibdev.dealloc_ucontext = ocrdma_dealloc_ucontext;
319         dev->ibdev.mmap = ocrdma_mmap;
320         dev->ibdev.dma_device = &dev->nic_info.pdev->dev;
321
322         dev->ibdev.process_mad = ocrdma_process_mad;
323         dev->ibdev.get_port_immutable = ocrdma_port_immutable;
324
325         if (ocrdma_get_asic_type(dev) == OCRDMA_ASIC_GEN_SKH_R) {
326                 dev->ibdev.uverbs_cmd_mask |=
327                      OCRDMA_UVERBS(CREATE_SRQ) |
328                      OCRDMA_UVERBS(MODIFY_SRQ) |
329                      OCRDMA_UVERBS(QUERY_SRQ) |
330                      OCRDMA_UVERBS(DESTROY_SRQ) |
331                      OCRDMA_UVERBS(POST_SRQ_RECV);
332
333                 dev->ibdev.create_srq = ocrdma_create_srq;
334                 dev->ibdev.modify_srq = ocrdma_modify_srq;
335                 dev->ibdev.query_srq = ocrdma_query_srq;
336                 dev->ibdev.destroy_srq = ocrdma_destroy_srq;
337                 dev->ibdev.post_srq_recv = ocrdma_post_srq_recv;
338         }
339         return ib_register_device(&dev->ibdev, NULL);
340 }
341
342 static int ocrdma_alloc_resources(struct ocrdma_dev *dev)
343 {
344         mutex_init(&dev->dev_lock);
345         dev->sgid_tbl = kzalloc(sizeof(union ib_gid) *
346                                 OCRDMA_MAX_SGID, GFP_KERNEL);
347         if (!dev->sgid_tbl)
348                 goto alloc_err;
349         spin_lock_init(&dev->sgid_lock);
350
351         dev->cq_tbl = kzalloc(sizeof(struct ocrdma_cq *) *
352                               OCRDMA_MAX_CQ, GFP_KERNEL);
353         if (!dev->cq_tbl)
354                 goto alloc_err;
355
356         if (dev->attr.max_qp) {
357                 dev->qp_tbl = kzalloc(sizeof(struct ocrdma_qp *) *
358                                       OCRDMA_MAX_QP, GFP_KERNEL);
359                 if (!dev->qp_tbl)
360                         goto alloc_err;
361         }
362
363         dev->stag_arr = kzalloc(sizeof(u64) * OCRDMA_MAX_STAG, GFP_KERNEL);
364         if (dev->stag_arr == NULL)
365                 goto alloc_err;
366
367         ocrdma_alloc_pd_pool(dev);
368
369         spin_lock_init(&dev->av_tbl.lock);
370         spin_lock_init(&dev->flush_q_lock);
371         return 0;
372 alloc_err:
373         pr_err("%s(%d) error.\n", __func__, dev->id);
374         return -ENOMEM;
375 }
376
377 static void ocrdma_free_resources(struct ocrdma_dev *dev)
378 {
379         kfree(dev->stag_arr);
380         kfree(dev->qp_tbl);
381         kfree(dev->cq_tbl);
382         kfree(dev->sgid_tbl);
383 }
384
385 /* OCRDMA sysfs interface */
386 static ssize_t show_rev(struct device *device, struct device_attribute *attr,
387                         char *buf)
388 {
389         struct ocrdma_dev *dev = dev_get_drvdata(device);
390
391         return scnprintf(buf, PAGE_SIZE, "0x%x\n", dev->nic_info.pdev->vendor);
392 }
393
394 static ssize_t show_fw_ver(struct device *device, struct device_attribute *attr,
395                         char *buf)
396 {
397         struct ocrdma_dev *dev = dev_get_drvdata(device);
398
399         return scnprintf(buf, PAGE_SIZE, "%s\n", &dev->attr.fw_ver[0]);
400 }
401
402 static ssize_t show_hca_type(struct device *device,
403                              struct device_attribute *attr, char *buf)
404 {
405         struct ocrdma_dev *dev = dev_get_drvdata(device);
406
407         return scnprintf(buf, PAGE_SIZE, "%s\n", &dev->model_number[0]);
408 }
409
410 static DEVICE_ATTR(hw_rev, S_IRUGO, show_rev, NULL);
411 static DEVICE_ATTR(fw_ver, S_IRUGO, show_fw_ver, NULL);
412 static DEVICE_ATTR(hca_type, S_IRUGO, show_hca_type, NULL);
413
414 static struct device_attribute *ocrdma_attributes[] = {
415         &dev_attr_hw_rev,
416         &dev_attr_fw_ver,
417         &dev_attr_hca_type
418 };
419
420 static void ocrdma_remove_sysfiles(struct ocrdma_dev *dev)
421 {
422         int i;
423
424         for (i = 0; i < ARRAY_SIZE(ocrdma_attributes); i++)
425                 device_remove_file(&dev->ibdev.dev, ocrdma_attributes[i]);
426 }
427
428 static void ocrdma_add_default_sgid(struct ocrdma_dev *dev)
429 {
430         /* GID Index 0 - Invariant manufacturer-assigned EUI-64 */
431         union ib_gid *sgid = &dev->sgid_tbl[0];
432
433         sgid->global.subnet_prefix = cpu_to_be64(0xfe80000000000000LL);
434         ocrdma_get_guid(dev, &sgid->raw[8]);
435 }
436
437 static void ocrdma_init_ipv4_gids(struct ocrdma_dev *dev,
438                                   struct net_device *net)
439 {
440         struct in_device *in_dev;
441         union ib_gid gid;
442         in_dev = in_dev_get(net);
443         if (in_dev) {
444                 for_ifa(in_dev) {
445                         ipv6_addr_set_v4mapped(ifa->ifa_address,
446                                                (struct in6_addr *)&gid);
447                         ocrdma_add_sgid(dev, &gid);
448                 }
449                 endfor_ifa(in_dev);
450                 in_dev_put(in_dev);
451         }
452 }
453
454 static void ocrdma_init_ipv6_gids(struct ocrdma_dev *dev,
455                                   struct net_device *net)
456 {
457 #if IS_ENABLED(CONFIG_IPV6)
458         struct inet6_dev *in6_dev;
459         union ib_gid  *pgid;
460         struct inet6_ifaddr *ifp;
461         in6_dev = in6_dev_get(net);
462         if (in6_dev) {
463                 read_lock_bh(&in6_dev->lock);
464                 list_for_each_entry(ifp, &in6_dev->addr_list, if_list) {
465                         pgid = (union ib_gid *)&ifp->addr;
466                         ocrdma_add_sgid(dev, pgid);
467                 }
468                 read_unlock_bh(&in6_dev->lock);
469                 in6_dev_put(in6_dev);
470         }
471 #endif
472 }
473
474 static void ocrdma_init_gid_table(struct ocrdma_dev *dev)
475 {
476         struct  net_device *net_dev;
477
478         for_each_netdev(&init_net, net_dev) {
479                 struct net_device *real_dev = rdma_vlan_dev_real_dev(net_dev) ?
480                                 rdma_vlan_dev_real_dev(net_dev) : net_dev;
481
482                 if (real_dev == dev->nic_info.netdev) {
483                         ocrdma_add_default_sgid(dev);
484                         ocrdma_init_ipv4_gids(dev, net_dev);
485                         ocrdma_init_ipv6_gids(dev, net_dev);
486                 }
487         }
488 }
489
490 static struct ocrdma_dev *ocrdma_add(struct be_dev_info *dev_info)
491 {
492         int status = 0, i;
493         struct ocrdma_dev *dev;
494
495         dev = (struct ocrdma_dev *)ib_alloc_device(sizeof(struct ocrdma_dev));
496         if (!dev) {
497                 pr_err("Unable to allocate ib device\n");
498                 return NULL;
499         }
500         dev->mbx_cmd = kzalloc(sizeof(struct ocrdma_mqe_emb_cmd), GFP_KERNEL);
501         if (!dev->mbx_cmd)
502                 goto idr_err;
503
504         memcpy(&dev->nic_info, dev_info, sizeof(*dev_info));
505         dev->id = idr_alloc(&ocrdma_dev_id, NULL, 0, 0, GFP_KERNEL);
506         if (dev->id < 0)
507                 goto idr_err;
508
509         status = ocrdma_init_hw(dev);
510         if (status)
511                 goto init_err;
512
513         status = ocrdma_alloc_resources(dev);
514         if (status)
515                 goto alloc_err;
516
517         ocrdma_init_service_level(dev);
518         ocrdma_init_gid_table(dev);
519         status = ocrdma_register_device(dev);
520         if (status)
521                 goto alloc_err;
522
523         for (i = 0; i < ARRAY_SIZE(ocrdma_attributes); i++)
524                 if (device_create_file(&dev->ibdev.dev, ocrdma_attributes[i]))
525                         goto sysfs_err;
526         spin_lock(&ocrdma_devlist_lock);
527         list_add_tail_rcu(&dev->entry, &ocrdma_dev_list);
528         spin_unlock(&ocrdma_devlist_lock);
529         /* Init stats */
530         ocrdma_add_port_stats(dev);
531         /* Interrupt Moderation */
532         INIT_DELAYED_WORK(&dev->eqd_work, ocrdma_eqd_set_task);
533         schedule_delayed_work(&dev->eqd_work, msecs_to_jiffies(1000));
534
535         pr_info("%s %s: %s \"%s\" port %d\n",
536                 dev_name(&dev->nic_info.pdev->dev), hca_name(dev),
537                 port_speed_string(dev), dev->model_number,
538                 dev->hba_port_num);
539         pr_info("%s ocrdma%d driver loaded successfully\n",
540                 dev_name(&dev->nic_info.pdev->dev), dev->id);
541         return dev;
542
543 sysfs_err:
544         ocrdma_remove_sysfiles(dev);
545 alloc_err:
546         ocrdma_free_resources(dev);
547         ocrdma_cleanup_hw(dev);
548 init_err:
549         idr_remove(&ocrdma_dev_id, dev->id);
550 idr_err:
551         kfree(dev->mbx_cmd);
552         ib_dealloc_device(&dev->ibdev);
553         pr_err("%s() leaving. ret=%d\n", __func__, status);
554         return NULL;
555 }
556
557 static void ocrdma_remove_free(struct rcu_head *rcu)
558 {
559         struct ocrdma_dev *dev = container_of(rcu, struct ocrdma_dev, rcu);
560
561         idr_remove(&ocrdma_dev_id, dev->id);
562         kfree(dev->mbx_cmd);
563         ib_dealloc_device(&dev->ibdev);
564 }
565
566 static void ocrdma_remove(struct ocrdma_dev *dev)
567 {
568         /* first unregister with stack to stop all the active traffic
569          * of the registered clients.
570          */
571         cancel_delayed_work_sync(&dev->eqd_work);
572         ocrdma_remove_sysfiles(dev);
573         ib_unregister_device(&dev->ibdev);
574
575         ocrdma_rem_port_stats(dev);
576
577         spin_lock(&ocrdma_devlist_lock);
578         list_del_rcu(&dev->entry);
579         spin_unlock(&ocrdma_devlist_lock);
580
581         ocrdma_free_resources(dev);
582         ocrdma_cleanup_hw(dev);
583
584         call_rcu(&dev->rcu, ocrdma_remove_free);
585 }
586
587 static int ocrdma_open(struct ocrdma_dev *dev)
588 {
589         struct ib_event port_event;
590
591         port_event.event = IB_EVENT_PORT_ACTIVE;
592         port_event.element.port_num = 1;
593         port_event.device = &dev->ibdev;
594         ib_dispatch_event(&port_event);
595         return 0;
596 }
597
598 static int ocrdma_close(struct ocrdma_dev *dev)
599 {
600         int i;
601         struct ocrdma_qp *qp, **cur_qp;
602         struct ib_event err_event;
603         struct ib_qp_attr attrs;
604         int attr_mask = IB_QP_STATE;
605
606         attrs.qp_state = IB_QPS_ERR;
607         mutex_lock(&dev->dev_lock);
608         if (dev->qp_tbl) {
609                 cur_qp = dev->qp_tbl;
610                 for (i = 0; i < OCRDMA_MAX_QP; i++) {
611                         qp = cur_qp[i];
612                         if (qp && qp->ibqp.qp_type != IB_QPT_GSI) {
613                                 /* change the QP state to ERROR */
614                                 _ocrdma_modify_qp(&qp->ibqp, &attrs, attr_mask);
615
616                                 err_event.event = IB_EVENT_QP_FATAL;
617                                 err_event.element.qp = &qp->ibqp;
618                                 err_event.device = &dev->ibdev;
619                                 ib_dispatch_event(&err_event);
620                         }
621                 }
622         }
623         mutex_unlock(&dev->dev_lock);
624
625         err_event.event = IB_EVENT_PORT_ERR;
626         err_event.element.port_num = 1;
627         err_event.device = &dev->ibdev;
628         ib_dispatch_event(&err_event);
629         return 0;
630 }
631
632 static void ocrdma_shutdown(struct ocrdma_dev *dev)
633 {
634         ocrdma_close(dev);
635         ocrdma_remove(dev);
636 }
637
638 /* event handling via NIC driver ensures that all the NIC specific
639  * initialization done before RoCE driver notifies
640  * event to stack.
641  */
642 static void ocrdma_event_handler(struct ocrdma_dev *dev, u32 event)
643 {
644         switch (event) {
645         case BE_DEV_UP:
646                 ocrdma_open(dev);
647                 break;
648         case BE_DEV_DOWN:
649                 ocrdma_close(dev);
650                 break;
651         case BE_DEV_SHUTDOWN:
652                 ocrdma_shutdown(dev);
653                 break;
654         }
655 }
656
657 static struct ocrdma_driver ocrdma_drv = {
658         .name                   = "ocrdma_driver",
659         .add                    = ocrdma_add,
660         .remove                 = ocrdma_remove,
661         .state_change_handler   = ocrdma_event_handler,
662         .be_abi_version         = OCRDMA_BE_ROCE_ABI_VERSION,
663 };
664
665 static void ocrdma_unregister_inet6addr_notifier(void)
666 {
667 #if IS_ENABLED(CONFIG_IPV6)
668         unregister_inet6addr_notifier(&ocrdma_inet6addr_notifier);
669 #endif
670 }
671
672 static void ocrdma_unregister_inetaddr_notifier(void)
673 {
674         unregister_inetaddr_notifier(&ocrdma_inetaddr_notifier);
675 }
676
677 static int __init ocrdma_init_module(void)
678 {
679         int status;
680
681         ocrdma_init_debugfs();
682
683         status = register_inetaddr_notifier(&ocrdma_inetaddr_notifier);
684         if (status)
685                 return status;
686
687 #if IS_ENABLED(CONFIG_IPV6)
688         status = register_inet6addr_notifier(&ocrdma_inet6addr_notifier);
689         if (status)
690                 goto err_notifier6;
691 #endif
692
693         status = be_roce_register_driver(&ocrdma_drv);
694         if (status)
695                 goto err_be_reg;
696
697         return 0;
698
699 err_be_reg:
700 #if IS_ENABLED(CONFIG_IPV6)
701         ocrdma_unregister_inet6addr_notifier();
702 err_notifier6:
703 #endif
704         ocrdma_unregister_inetaddr_notifier();
705         return status;
706 }
707
708 static void __exit ocrdma_exit_module(void)
709 {
710         be_roce_unregister_driver(&ocrdma_drv);
711         ocrdma_unregister_inet6addr_notifier();
712         ocrdma_unregister_inetaddr_notifier();
713         ocrdma_rem_debugfs();
714         idr_destroy(&ocrdma_dev_id);
715 }
716
717 module_init(ocrdma_init_module);
718 module_exit(ocrdma_exit_module);