]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - drivers/infiniband/hw/ocrdma/ocrdma_main.c
Merge remote-tracking branches 'asoc/topic/tas2552', 'asoc/topic/tas5086', 'asoc...
[mirror_ubuntu-zesty-kernel.git] / drivers / infiniband / hw / ocrdma / ocrdma_main.c
CommitLineData
71ee6730
DS
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.
fe2caefc
PP
34 *
35 * Contact Information:
36 * linux-drivers@emulex.com
37 *
38 * Emulex
39 * 3333 Susan Street
40 * Costa Mesa, CA 92626
71ee6730 41 */
fe2caefc
PP
42
43#include <linux/module.h>
fe2caefc
PP
44#include <linux/idr.h>
45#include <rdma/ib_verbs.h>
46#include <rdma/ib_user_verbs.h>
47#include <rdma/ib_addr.h>
337877a4 48#include <rdma/ib_mad.h>
fe2caefc
PP
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"
a51f06e1 58#include "ocrdma_stats.h"
38754397 59#include "ocrdma_abi.h"
fe2caefc 60
0154410b
DS
61MODULE_VERSION(OCRDMA_ROCE_DRV_VERSION);
62MODULE_DESCRIPTION(OCRDMA_ROCE_DRV_DESC " " OCRDMA_ROCE_DRV_VERSION);
fe2caefc 63MODULE_AUTHOR("Emulex Corporation");
b8f5595e 64MODULE_LICENSE("Dual BSD/GPL");
fe2caefc
PP
65
66static LIST_HEAD(ocrdma_dev_list);
3e4d60a8 67static DEFINE_SPINLOCK(ocrdma_devlist_lock);
fe2caefc
PP
68static DEFINE_IDR(ocrdma_dev_id);
69
70static union ib_gid ocrdma_zero_sgid;
fe2caefc 71
fe2caefc
PP
72void 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
37721d85 87static bool ocrdma_add_sgid(struct ocrdma_dev *dev, union ib_gid *new_sgid)
fe2caefc
PP
88{
89 int i;
fe2caefc
PP
90 unsigned long flags;
91
92 memset(&ocrdma_zero_sgid, 0, sizeof(union ib_gid));
93
fe2caefc
PP
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 */
37721d85 100 memcpy(&dev->sgid_tbl[i], new_sgid,
6ab6827e
PP
101 sizeof(union ib_gid));
102 spin_unlock_irqrestore(&dev->sgid_lock, flags);
103 return true;
37721d85 104 } else if (!memcmp(&dev->sgid_tbl[i], new_sgid,
fe2caefc
PP
105 sizeof(union ib_gid))) {
106 /* entry already present, no addition is required. */
107 spin_unlock_irqrestore(&dev->sgid_lock, flags);
6ab6827e 108 return false;
fe2caefc
PP
109 }
110 }
fe2caefc 111 spin_unlock_irqrestore(&dev->sgid_lock, flags);
6ab6827e 112 return false;
fe2caefc
PP
113}
114
37721d85 115static bool ocrdma_del_sgid(struct ocrdma_dev *dev, union ib_gid *sgid)
fe2caefc
PP
116{
117 int found = false;
118 int i;
fe2caefc
PP
119 unsigned long flags;
120
fe2caefc
PP
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++) {
37721d85 125 if (!memcmp(&dev->sgid_tbl[i], sgid, sizeof(union ib_gid))) {
fe2caefc
PP
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
37721d85
MS
136static int ocrdma_addr_event(unsigned long event, struct net_device *netdev,
137 union ib_gid *gid)
6ab6827e 138{
fe2caefc
PP
139 struct ib_event gid_event;
140 struct ocrdma_dev *dev;
141 bool found = false;
6ab6827e 142 bool updated = false;
fe2caefc 143 bool is_vlan = false;
fe2caefc 144
d549f55f 145 is_vlan = netdev->priv_flags & IFF_802_1Q_VLAN;
37721d85 146 if (is_vlan)
09de3f13 147 netdev = rdma_vlan_dev_real_dev(netdev);
d549f55f 148
3e4d60a8
SL
149 rcu_read_lock();
150 list_for_each_entry_rcu(dev, &ocrdma_dev_list, entry) {
fe2caefc
PP
151 if (dev->nic_info.netdev == netdev) {
152 found = true;
153 break;
154 }
155 }
3e4d60a8 156 rcu_read_unlock();
fe2caefc
PP
157
158 if (!found)
159 return NOTIFY_DONE;
fe2caefc
PP
160
161 mutex_lock(&dev->dev_lock);
162 switch (event) {
163 case NETDEV_UP:
37721d85 164 updated = ocrdma_add_sgid(dev, gid);
fe2caefc
PP
165 break;
166 case NETDEV_DOWN:
37721d85 167 updated = ocrdma_del_sgid(dev, gid);
fe2caefc
PP
168 break;
169 default:
170 break;
171 }
6ab6827e
PP
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 }
fe2caefc
PP
179 mutex_unlock(&dev->dev_lock);
180 return NOTIFY_OK;
181}
182
37721d85
MS
183static 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
31ab8acb
RD
194static struct notifier_block ocrdma_inetaddr_notifier = {
195 .notifier_call = ocrdma_inetaddr_event
196};
197
37721d85
MS
198#if IS_ENABLED(CONFIG_IPV6)
199
200static 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
34955669
RD
209static struct notifier_block ocrdma_inet6addr_notifier = {
210 .notifier_call = ocrdma_inet6addr_event
211};
212
6ab6827e 213#endif /* IPV6 and VLAN */
34955669 214
fe2caefc
PP
215static enum rdma_link_layer ocrdma_link_layer(struct ib_device *device,
216 u8 port_num)
217{
218 return IB_LINK_LAYER_ETHERNET;
219}
220
7738613e
IW
221static 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;
f9b22e35 233 immutable->core_cap_flags = RDMA_CORE_PORT_IBA_ROCE;
337877a4 234 immutable->max_mad_size = IB_MGMT_MAD_SIZE;
7738613e
IW
235
236 return 0;
237}
238
abe3afac 239static int ocrdma_register_device(struct ocrdma_dev *dev)
fe2caefc
PP
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;
38754397 246 dev->ibdev.uverbs_abi_ver = OCRDMA_ABI_VERSION;
fe2caefc
PP
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;
0c0eacdc 276 dev->ibdev.num_comp_vectors = dev->eq_cnt;
fe2caefc
PP
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;
cffce990 308 dev->ibdev.reg_phys_mr = ocrdma_reg_kernel_mr;
fe2caefc
PP
309 dev->ibdev.dereg_mr = ocrdma_dereg_mr;
310 dev->ibdev.reg_user_mr = ocrdma_reg_user_mr;
311
7c33880c
NG
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
fe2caefc
PP
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;
7738613e 323 dev->ibdev.get_port_immutable = ocrdma_port_immutable;
fe2caefc 324
21c3391a 325 if (ocrdma_get_asic_type(dev) == OCRDMA_ASIC_GEN_SKH_R) {
fe2caefc
PP
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
342static 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 }
4f1df844
SX
362
363 dev->stag_arr = kzalloc(sizeof(u64) * OCRDMA_MAX_STAG, GFP_KERNEL);
364 if (dev->stag_arr == NULL)
365 goto alloc_err;
366
9ba1377d
MA
367 ocrdma_alloc_pd_pool(dev);
368
fe2caefc
PP
369 spin_lock_init(&dev->av_tbl.lock);
370 spin_lock_init(&dev->flush_q_lock);
371 return 0;
372alloc_err:
ef99c4c2 373 pr_err("%s(%d) error.\n", __func__, dev->id);
fe2caefc
PP
374 return -ENOMEM;
375}
376
377static void ocrdma_free_resources(struct ocrdma_dev *dev)
378{
4f1df844 379 kfree(dev->stag_arr);
fe2caefc
PP
380 kfree(dev->qp_tbl);
381 kfree(dev->cq_tbl);
382 kfree(dev->sgid_tbl);
383}
384
334b8db3
SX
385/* OCRDMA sysfs interface */
386static 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
394static 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
4808b184
SX
399 return scnprintf(buf, PAGE_SIZE, "%s\n", &dev->attr.fw_ver[0]);
400}
401
402static 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]);
334b8db3
SX
408}
409
410static DEVICE_ATTR(hw_rev, S_IRUGO, show_rev, NULL);
411static DEVICE_ATTR(fw_ver, S_IRUGO, show_fw_ver, NULL);
4808b184 412static DEVICE_ATTR(hca_type, S_IRUGO, show_hca_type, NULL);
334b8db3
SX
413
414static struct device_attribute *ocrdma_attributes[] = {
415 &dev_attr_hw_rev,
4808b184
SX
416 &dev_attr_fw_ver,
417 &dev_attr_hca_type
334b8db3
SX
418};
419
420static 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
7ec11e0a
DS
428static 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
b8806324
SX
437static 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
454static 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
474static 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) {
7ec11e0a 483 ocrdma_add_default_sgid(dev);
b8806324
SX
484 ocrdma_init_ipv4_gids(dev, net_dev);
485 ocrdma_init_ipv6_gids(dev, net_dev);
486 }
487 }
488}
489
fe2caefc
PP
490static struct ocrdma_dev *ocrdma_add(struct be_dev_info *dev_info)
491{
334b8db3 492 int status = 0, i;
fe2caefc
PP
493 struct ocrdma_dev *dev;
494
495 dev = (struct ocrdma_dev *)ib_alloc_device(sizeof(struct ocrdma_dev));
496 if (!dev) {
ef99c4c2 497 pr_err("Unable to allocate ib device\n");
fe2caefc
PP
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));
cffcd59f 505 dev->id = idr_alloc(&ocrdma_dev_id, NULL, 0, 0, GFP_KERNEL);
fe2caefc
PP
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
31dbdd9a 517 ocrdma_init_service_level(dev);
b8806324 518 ocrdma_init_gid_table(dev);
fe2caefc
PP
519 status = ocrdma_register_device(dev);
520 if (status)
521 goto alloc_err;
522
334b8db3
SX
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;
3e4d60a8
SL
526 spin_lock(&ocrdma_devlist_lock);
527 list_add_tail_rcu(&dev->entry, &ocrdma_dev_list);
528 spin_unlock(&ocrdma_devlist_lock);
a51f06e1
SX
529 /* Init stats */
530 ocrdma_add_port_stats(dev);
b4dbe8d5
MA
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));
a51f06e1
SX
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);
fe2caefc
PP
541 return dev;
542
334b8db3
SX
543sysfs_err:
544 ocrdma_remove_sysfiles(dev);
fe2caefc
PP
545alloc_err:
546 ocrdma_free_resources(dev);
547 ocrdma_cleanup_hw(dev);
548init_err:
549 idr_remove(&ocrdma_dev_id, dev->id);
550idr_err:
551 kfree(dev->mbx_cmd);
552 ib_dealloc_device(&dev->ibdev);
ef99c4c2 553 pr_err("%s() leaving. ret=%d\n", __func__, status);
fe2caefc
PP
554 return NULL;
555}
556
3e4d60a8 557static void ocrdma_remove_free(struct rcu_head *rcu)
fe2caefc 558{
3e4d60a8 559 struct ocrdma_dev *dev = container_of(rcu, struct ocrdma_dev, rcu);
fe2caefc 560
fe2caefc
PP
561 idr_remove(&ocrdma_dev_id, dev->id);
562 kfree(dev->mbx_cmd);
563 ib_dealloc_device(&dev->ibdev);
564}
565
3e4d60a8
SL
566static 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 */
b4dbe8d5 571 cancel_delayed_work_sync(&dev->eqd_work);
334b8db3 572 ocrdma_remove_sysfiles(dev);
3e4d60a8 573 ib_unregister_device(&dev->ibdev);
4b8180aa
MA
574
575 ocrdma_rem_port_stats(dev);
3e4d60a8
SL
576
577 spin_lock(&ocrdma_devlist_lock);
578 list_del_rcu(&dev->entry);
579 spin_unlock(&ocrdma_devlist_lock);
1852d1da
NG
580
581 ocrdma_free_resources(dev);
582 ocrdma_cleanup_hw(dev);
583
3e4d60a8
SL
584 call_rcu(&dev->rcu, ocrdma_remove_free);
585}
586
fe2caefc
PP
587static 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
598static 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];
fad51b7d 612 if (qp && qp->ibqp.qp_type != IB_QPT_GSI) {
fe2caefc
PP
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
efe45937
DS
632static void ocrdma_shutdown(struct ocrdma_dev *dev)
633{
634 ocrdma_close(dev);
635 ocrdma_remove(dev);
636}
637
fe2caefc
PP
638/* event handling via NIC driver ensures that all the NIC specific
639 * initialization done before RoCE driver notifies
640 * event to stack.
641 */
642static 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;
efe45937
DS
651 case BE_DEV_SHUTDOWN:
652 ocrdma_shutdown(dev);
653 break;
2b50176d 654 }
fe2caefc
PP
655}
656
abe3afac 657static struct ocrdma_driver ocrdma_drv = {
fe2caefc
PP
658 .name = "ocrdma_driver",
659 .add = ocrdma_add,
660 .remove = ocrdma_remove,
661 .state_change_handler = ocrdma_event_handler,
b6b87d2e 662 .be_abi_version = OCRDMA_BE_ROCE_ABI_VERSION,
fe2caefc
PP
663};
664
34955669
RD
665static void ocrdma_unregister_inet6addr_notifier(void)
666{
d90f9b35 667#if IS_ENABLED(CONFIG_IPV6)
34955669
RD
668 unregister_inet6addr_notifier(&ocrdma_inet6addr_notifier);
669#endif
670}
671
2d8f57d5
SX
672static void ocrdma_unregister_inetaddr_notifier(void)
673{
674 unregister_inetaddr_notifier(&ocrdma_inetaddr_notifier);
675}
676
fe2caefc
PP
677static int __init ocrdma_init_module(void)
678{
679 int status;
680
a51f06e1
SX
681 ocrdma_init_debugfs();
682
37721d85
MS
683 status = register_inetaddr_notifier(&ocrdma_inetaddr_notifier);
684 if (status)
685 return status;
686
d90f9b35 687#if IS_ENABLED(CONFIG_IPV6)
fe2caefc
PP
688 status = register_inet6addr_notifier(&ocrdma_inet6addr_notifier);
689 if (status)
2d8f57d5 690 goto err_notifier6;
34955669
RD
691#endif
692
fe2caefc
PP
693 status = be_roce_register_driver(&ocrdma_drv);
694 if (status)
2d8f57d5 695 goto err_be_reg;
34955669 696
2d8f57d5
SX
697 return 0;
698
699err_be_reg:
e5dc9409 700#if IS_ENABLED(CONFIG_IPV6)
2d8f57d5
SX
701 ocrdma_unregister_inet6addr_notifier();
702err_notifier6:
e5dc9409 703#endif
2d8f57d5 704 ocrdma_unregister_inetaddr_notifier();
fe2caefc
PP
705 return status;
706}
707
708static void __exit ocrdma_exit_module(void)
709{
710 be_roce_unregister_driver(&ocrdma_drv);
34955669 711 ocrdma_unregister_inet6addr_notifier();
2d8f57d5 712 ocrdma_unregister_inetaddr_notifier();
a51f06e1 713 ocrdma_rem_debugfs();
d8b2ba7c 714 idr_destroy(&ocrdma_dev_id);
fe2caefc
PP
715}
716
717module_init(ocrdma_init_module);
718module_exit(ocrdma_exit_module);