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