GNU Linux-libre 4.14.290-gnu1
[releases.git] / drivers / infiniband / hw / bnxt_re / main.c
1 /*
2  * Broadcom NetXtreme-E RoCE driver.
3  *
4  * Copyright (c) 2016 - 2017, Broadcom. All rights reserved.  The term
5  * Broadcom refers to Broadcom Limited and/or its subsidiaries.
6  *
7  * This software is available to you under a choice of one of two
8  * licenses.  You may choose to be licensed under the terms of the GNU
9  * General Public License (GPL) Version 2, available from the file
10  * COPYING in the main directory of this source tree, or the
11  * BSD license below:
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  *
17  * 1. Redistributions of source code must retain the above copyright
18  *    notice, this list of conditions and the following disclaimer.
19  * 2. 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
22  *    distribution.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS''
25  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
26  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
27  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS
28  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
31  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
32  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
33  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
34  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35  *
36  * Description: Main component of the bnxt_re driver
37  */
38
39 #include <linux/module.h>
40 #include <linux/netdevice.h>
41 #include <linux/ethtool.h>
42 #include <linux/mutex.h>
43 #include <linux/list.h>
44 #include <linux/rculist.h>
45 #include <linux/spinlock.h>
46 #include <linux/pci.h>
47 #include <net/dcbnl.h>
48 #include <net/ipv6.h>
49 #include <net/addrconf.h>
50 #include <linux/if_ether.h>
51
52 #include <rdma/ib_verbs.h>
53 #include <rdma/ib_user_verbs.h>
54 #include <rdma/ib_umem.h>
55 #include <rdma/ib_addr.h>
56
57 #include "bnxt_ulp.h"
58 #include "roce_hsi.h"
59 #include "qplib_res.h"
60 #include "qplib_sp.h"
61 #include "qplib_fp.h"
62 #include "qplib_rcfw.h"
63 #include "bnxt_re.h"
64 #include "ib_verbs.h"
65 #include <rdma/bnxt_re-abi.h>
66 #include "bnxt.h"
67 #include "hw_counters.h"
68
69 static char version[] =
70                 BNXT_RE_DESC " v" ROCE_DRV_MODULE_VERSION "\n";
71
72 MODULE_AUTHOR("Eddie Wai <eddie.wai@broadcom.com>");
73 MODULE_DESCRIPTION(BNXT_RE_DESC " Driver");
74 MODULE_LICENSE("Dual BSD/GPL");
75
76 /* globals */
77 static struct list_head bnxt_re_dev_list = LIST_HEAD_INIT(bnxt_re_dev_list);
78 /* Mutex to protect the list of bnxt_re devices added */
79 static DEFINE_MUTEX(bnxt_re_dev_lock);
80 static struct workqueue_struct *bnxt_re_wq;
81
82 /* for handling bnxt_en callbacks later */
83 static void bnxt_re_stop(void *p)
84 {
85 }
86
87 static void bnxt_re_start(void *p)
88 {
89 }
90
91 static void bnxt_re_sriov_config(void *p, int num_vfs)
92 {
93 }
94
95 static struct bnxt_ulp_ops bnxt_re_ulp_ops = {
96         .ulp_async_notifier = NULL,
97         .ulp_stop = bnxt_re_stop,
98         .ulp_start = bnxt_re_start,
99         .ulp_sriov_config = bnxt_re_sriov_config
100 };
101
102 /* RoCE -> Net driver */
103
104 /* Driver registration routines used to let the networking driver (bnxt_en)
105  * to know that the RoCE driver is now installed
106  */
107 static int bnxt_re_unregister_netdev(struct bnxt_re_dev *rdev, bool lock_wait)
108 {
109         struct bnxt_en_dev *en_dev;
110         int rc;
111
112         if (!rdev)
113                 return -EINVAL;
114
115         en_dev = rdev->en_dev;
116         /* Acquire rtnl lock if it is not invokded from netdev event */
117         if (lock_wait)
118                 rtnl_lock();
119
120         rc = en_dev->en_ops->bnxt_unregister_device(rdev->en_dev,
121                                                     BNXT_ROCE_ULP);
122         if (lock_wait)
123                 rtnl_unlock();
124         return rc;
125 }
126
127 static int bnxt_re_register_netdev(struct bnxt_re_dev *rdev)
128 {
129         struct bnxt_en_dev *en_dev;
130         int rc = 0;
131
132         if (!rdev)
133                 return -EINVAL;
134
135         en_dev = rdev->en_dev;
136
137         rtnl_lock();
138         rc = en_dev->en_ops->bnxt_register_device(en_dev, BNXT_ROCE_ULP,
139                                                   &bnxt_re_ulp_ops, rdev);
140         rtnl_unlock();
141         return rc;
142 }
143
144 static int bnxt_re_free_msix(struct bnxt_re_dev *rdev, bool lock_wait)
145 {
146         struct bnxt_en_dev *en_dev;
147         int rc;
148
149         if (!rdev)
150                 return -EINVAL;
151
152         en_dev = rdev->en_dev;
153
154         if (lock_wait)
155                 rtnl_lock();
156
157         rc = en_dev->en_ops->bnxt_free_msix(rdev->en_dev, BNXT_ROCE_ULP);
158
159         if (lock_wait)
160                 rtnl_unlock();
161         return rc;
162 }
163
164 static int bnxt_re_request_msix(struct bnxt_re_dev *rdev)
165 {
166         int rc = 0, num_msix_want = BNXT_RE_MAX_MSIX, num_msix_got;
167         struct bnxt_en_dev *en_dev;
168
169         if (!rdev)
170                 return -EINVAL;
171
172         en_dev = rdev->en_dev;
173
174         num_msix_want = min_t(u32, BNXT_RE_MAX_MSIX, num_online_cpus());
175
176         rtnl_lock();
177         num_msix_got = en_dev->en_ops->bnxt_request_msix(en_dev, BNXT_ROCE_ULP,
178                                                          rdev->msix_entries,
179                                                          num_msix_want);
180         if (num_msix_got < BNXT_RE_MIN_MSIX) {
181                 rc = -EINVAL;
182                 goto done;
183         }
184         if (num_msix_got != num_msix_want) {
185                 dev_warn(rdev_to_dev(rdev),
186                          "Requested %d MSI-X vectors, got %d\n",
187                          num_msix_want, num_msix_got);
188         }
189         rdev->num_msix = num_msix_got;
190 done:
191         rtnl_unlock();
192         return rc;
193 }
194
195 static void bnxt_re_init_hwrm_hdr(struct bnxt_re_dev *rdev, struct input *hdr,
196                                   u16 opcd, u16 crid, u16 trid)
197 {
198         hdr->req_type = cpu_to_le16(opcd);
199         hdr->cmpl_ring = cpu_to_le16(crid);
200         hdr->target_id = cpu_to_le16(trid);
201 }
202
203 static void bnxt_re_fill_fw_msg(struct bnxt_fw_msg *fw_msg, void *msg,
204                                 int msg_len, void *resp, int resp_max_len,
205                                 int timeout)
206 {
207         fw_msg->msg = msg;
208         fw_msg->msg_len = msg_len;
209         fw_msg->resp = resp;
210         fw_msg->resp_max_len = resp_max_len;
211         fw_msg->timeout = timeout;
212 }
213
214 static int bnxt_re_net_ring_free(struct bnxt_re_dev *rdev, u16 fw_ring_id,
215                                  bool lock_wait)
216 {
217         struct bnxt_en_dev *en_dev = rdev->en_dev;
218         struct hwrm_ring_free_input req = {0};
219         struct hwrm_ring_free_output resp;
220         struct bnxt_fw_msg fw_msg;
221         bool do_unlock = false;
222         int rc = -EINVAL;
223
224         if (!en_dev)
225                 return rc;
226
227         memset(&fw_msg, 0, sizeof(fw_msg));
228         if (lock_wait) {
229                 rtnl_lock();
230                 do_unlock = true;
231         }
232
233         bnxt_re_init_hwrm_hdr(rdev, (void *)&req, HWRM_RING_FREE, -1, -1);
234         req.ring_type = RING_ALLOC_REQ_RING_TYPE_L2_CMPL;
235         req.ring_id = cpu_to_le16(fw_ring_id);
236         bnxt_re_fill_fw_msg(&fw_msg, (void *)&req, sizeof(req), (void *)&resp,
237                             sizeof(resp), DFLT_HWRM_CMD_TIMEOUT);
238         rc = en_dev->en_ops->bnxt_send_fw_msg(en_dev, BNXT_ROCE_ULP, &fw_msg);
239         if (rc)
240                 dev_err(rdev_to_dev(rdev),
241                         "Failed to free HW ring:%d :%#x", req.ring_id, rc);
242         if (do_unlock)
243                 rtnl_unlock();
244         return rc;
245 }
246
247 static int bnxt_re_net_ring_alloc(struct bnxt_re_dev *rdev, dma_addr_t *dma_arr,
248                                   int pages, int type, u32 ring_mask,
249                                   u32 map_index, u16 *fw_ring_id)
250 {
251         struct bnxt_en_dev *en_dev = rdev->en_dev;
252         struct hwrm_ring_alloc_input req = {0};
253         struct hwrm_ring_alloc_output resp;
254         struct bnxt_fw_msg fw_msg;
255         int rc = -EINVAL;
256
257         if (!en_dev)
258                 return rc;
259
260         memset(&fw_msg, 0, sizeof(fw_msg));
261         rtnl_lock();
262         bnxt_re_init_hwrm_hdr(rdev, (void *)&req, HWRM_RING_ALLOC, -1, -1);
263         req.enables = 0;
264         req.page_tbl_addr =  cpu_to_le64(dma_arr[0]);
265         if (pages > 1) {
266                 /* Page size is in log2 units */
267                 req.page_size = BNXT_PAGE_SHIFT;
268                 req.page_tbl_depth = 1;
269         }
270         req.fbo = 0;
271         /* Association of ring index with doorbell index and MSIX number */
272         req.logical_id = cpu_to_le16(map_index);
273         req.length = cpu_to_le32(ring_mask + 1);
274         req.ring_type = RING_ALLOC_REQ_RING_TYPE_L2_CMPL;
275         req.int_mode = RING_ALLOC_REQ_INT_MODE_MSIX;
276         bnxt_re_fill_fw_msg(&fw_msg, (void *)&req, sizeof(req), (void *)&resp,
277                             sizeof(resp), DFLT_HWRM_CMD_TIMEOUT);
278         rc = en_dev->en_ops->bnxt_send_fw_msg(en_dev, BNXT_ROCE_ULP, &fw_msg);
279         if (!rc)
280                 *fw_ring_id = le16_to_cpu(resp.ring_id);
281
282         rtnl_unlock();
283         return rc;
284 }
285
286 static int bnxt_re_net_stats_ctx_free(struct bnxt_re_dev *rdev,
287                                       u32 fw_stats_ctx_id, bool lock_wait)
288 {
289         struct bnxt_en_dev *en_dev = rdev->en_dev;
290         struct hwrm_stat_ctx_free_input req = {0};
291         struct bnxt_fw_msg fw_msg;
292         bool do_unlock = false;
293         int rc = -EINVAL;
294
295         if (!en_dev)
296                 return rc;
297
298         memset(&fw_msg, 0, sizeof(fw_msg));
299         if (lock_wait) {
300                 rtnl_lock();
301                 do_unlock = true;
302         }
303
304         bnxt_re_init_hwrm_hdr(rdev, (void *)&req, HWRM_STAT_CTX_FREE, -1, -1);
305         req.stat_ctx_id = cpu_to_le32(fw_stats_ctx_id);
306         bnxt_re_fill_fw_msg(&fw_msg, (void *)&req, sizeof(req), (void *)&req,
307                             sizeof(req), DFLT_HWRM_CMD_TIMEOUT);
308         rc = en_dev->en_ops->bnxt_send_fw_msg(en_dev, BNXT_ROCE_ULP, &fw_msg);
309         if (rc)
310                 dev_err(rdev_to_dev(rdev),
311                         "Failed to free HW stats context %#x", rc);
312
313         if (do_unlock)
314                 rtnl_unlock();
315         return rc;
316 }
317
318 static int bnxt_re_net_stats_ctx_alloc(struct bnxt_re_dev *rdev,
319                                        dma_addr_t dma_map,
320                                        u32 *fw_stats_ctx_id)
321 {
322         struct hwrm_stat_ctx_alloc_output resp = {0};
323         struct hwrm_stat_ctx_alloc_input req = {0};
324         struct bnxt_en_dev *en_dev = rdev->en_dev;
325         struct bnxt_fw_msg fw_msg;
326         int rc = -EINVAL;
327
328         *fw_stats_ctx_id = INVALID_STATS_CTX_ID;
329
330         if (!en_dev)
331                 return rc;
332
333         memset(&fw_msg, 0, sizeof(fw_msg));
334         rtnl_lock();
335
336         bnxt_re_init_hwrm_hdr(rdev, (void *)&req, HWRM_STAT_CTX_ALLOC, -1, -1);
337         req.update_period_ms = cpu_to_le32(1000);
338         req.stats_dma_addr = cpu_to_le64(dma_map);
339         req.stat_ctx_flags = STAT_CTX_ALLOC_REQ_STAT_CTX_FLAGS_ROCE;
340         bnxt_re_fill_fw_msg(&fw_msg, (void *)&req, sizeof(req), (void *)&resp,
341                             sizeof(resp), DFLT_HWRM_CMD_TIMEOUT);
342         rc = en_dev->en_ops->bnxt_send_fw_msg(en_dev, BNXT_ROCE_ULP, &fw_msg);
343         if (!rc)
344                 *fw_stats_ctx_id = le32_to_cpu(resp.stat_ctx_id);
345
346         rtnl_unlock();
347         return rc;
348 }
349
350 /* Device */
351
352 static bool is_bnxt_re_dev(struct net_device *netdev)
353 {
354         struct ethtool_drvinfo drvinfo;
355
356         if (netdev->ethtool_ops && netdev->ethtool_ops->get_drvinfo) {
357                 memset(&drvinfo, 0, sizeof(drvinfo));
358                 netdev->ethtool_ops->get_drvinfo(netdev, &drvinfo);
359
360                 if (strcmp(drvinfo.driver, "bnxt_en"))
361                         return false;
362                 return true;
363         }
364         return false;
365 }
366
367 static struct bnxt_re_dev *bnxt_re_from_netdev(struct net_device *netdev)
368 {
369         struct bnxt_re_dev *rdev;
370
371         rcu_read_lock();
372         list_for_each_entry_rcu(rdev, &bnxt_re_dev_list, list) {
373                 if (rdev->netdev == netdev) {
374                         rcu_read_unlock();
375                         return rdev;
376                 }
377         }
378         rcu_read_unlock();
379         return NULL;
380 }
381
382 static void bnxt_re_dev_unprobe(struct net_device *netdev,
383                                 struct bnxt_en_dev *en_dev)
384 {
385         dev_put(netdev);
386         module_put(en_dev->pdev->driver->driver.owner);
387 }
388
389 static struct bnxt_en_dev *bnxt_re_dev_probe(struct net_device *netdev)
390 {
391         struct bnxt *bp = netdev_priv(netdev);
392         struct bnxt_en_dev *en_dev;
393         struct pci_dev *pdev;
394
395         /* Call bnxt_en's RoCE probe via indirect API */
396         if (!bp->ulp_probe)
397                 return ERR_PTR(-EINVAL);
398
399         en_dev = bp->ulp_probe(netdev);
400         if (IS_ERR(en_dev))
401                 return en_dev;
402
403         pdev = en_dev->pdev;
404         if (!pdev)
405                 return ERR_PTR(-EINVAL);
406
407         if (!(en_dev->flags & BNXT_EN_FLAG_ROCE_CAP)) {
408                 dev_dbg(&pdev->dev,
409                         "%s: probe error: RoCE is not supported on this device",
410                         ROCE_DRV_MODULE_NAME);
411                 return ERR_PTR(-ENODEV);
412         }
413
414         /* Bump net device reference count */
415         if (!try_module_get(pdev->driver->driver.owner))
416                 return ERR_PTR(-ENODEV);
417
418         dev_hold(netdev);
419
420         return en_dev;
421 }
422
423 static void bnxt_re_unregister_ib(struct bnxt_re_dev *rdev)
424 {
425         ib_unregister_device(&rdev->ibdev);
426 }
427
428 static int bnxt_re_register_ib(struct bnxt_re_dev *rdev)
429 {
430         struct ib_device *ibdev = &rdev->ibdev;
431
432         /* ib device init */
433         ibdev->owner = THIS_MODULE;
434         ibdev->node_type = RDMA_NODE_IB_CA;
435         strlcpy(ibdev->name, "bnxt_re%d", IB_DEVICE_NAME_MAX);
436         strlcpy(ibdev->node_desc, BNXT_RE_DESC " HCA",
437                 strlen(BNXT_RE_DESC) + 5);
438         ibdev->phys_port_cnt = 1;
439
440         bnxt_qplib_get_guid(rdev->netdev->dev_addr, (u8 *)&ibdev->node_guid);
441
442         ibdev->num_comp_vectors = 1;
443         ibdev->dev.parent = &rdev->en_dev->pdev->dev;
444         ibdev->local_dma_lkey = BNXT_QPLIB_RSVD_LKEY;
445
446         /* User space */
447         ibdev->uverbs_abi_ver = BNXT_RE_ABI_VERSION;
448         ibdev->uverbs_cmd_mask =
449                         (1ull << IB_USER_VERBS_CMD_GET_CONTEXT)         |
450                         (1ull << IB_USER_VERBS_CMD_QUERY_DEVICE)        |
451                         (1ull << IB_USER_VERBS_CMD_QUERY_PORT)          |
452                         (1ull << IB_USER_VERBS_CMD_ALLOC_PD)            |
453                         (1ull << IB_USER_VERBS_CMD_DEALLOC_PD)          |
454                         (1ull << IB_USER_VERBS_CMD_REG_MR)              |
455                         (1ull << IB_USER_VERBS_CMD_REREG_MR)            |
456                         (1ull << IB_USER_VERBS_CMD_DEREG_MR)            |
457                         (1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL) |
458                         (1ull << IB_USER_VERBS_CMD_CREATE_CQ)           |
459                         (1ull << IB_USER_VERBS_CMD_RESIZE_CQ)           |
460                         (1ull << IB_USER_VERBS_CMD_DESTROY_CQ)          |
461                         (1ull << IB_USER_VERBS_CMD_CREATE_QP)           |
462                         (1ull << IB_USER_VERBS_CMD_MODIFY_QP)           |
463                         (1ull << IB_USER_VERBS_CMD_QUERY_QP)            |
464                         (1ull << IB_USER_VERBS_CMD_DESTROY_QP)          |
465                         (1ull << IB_USER_VERBS_CMD_CREATE_SRQ)          |
466                         (1ull << IB_USER_VERBS_CMD_MODIFY_SRQ)          |
467                         (1ull << IB_USER_VERBS_CMD_QUERY_SRQ)           |
468                         (1ull << IB_USER_VERBS_CMD_DESTROY_SRQ)         |
469                         (1ull << IB_USER_VERBS_CMD_CREATE_AH)           |
470                         (1ull << IB_USER_VERBS_CMD_MODIFY_AH)           |
471                         (1ull << IB_USER_VERBS_CMD_QUERY_AH)            |
472                         (1ull << IB_USER_VERBS_CMD_DESTROY_AH);
473         /* POLL_CQ and REQ_NOTIFY_CQ is directly handled in libbnxt_re */
474
475         /* Kernel verbs */
476         ibdev->query_device             = bnxt_re_query_device;
477         ibdev->modify_device            = bnxt_re_modify_device;
478
479         ibdev->query_port               = bnxt_re_query_port;
480         ibdev->get_port_immutable       = bnxt_re_get_port_immutable;
481         ibdev->query_pkey               = bnxt_re_query_pkey;
482         ibdev->query_gid                = bnxt_re_query_gid;
483         ibdev->get_netdev               = bnxt_re_get_netdev;
484         ibdev->add_gid                  = bnxt_re_add_gid;
485         ibdev->del_gid                  = bnxt_re_del_gid;
486         ibdev->get_link_layer           = bnxt_re_get_link_layer;
487
488         ibdev->alloc_pd                 = bnxt_re_alloc_pd;
489         ibdev->dealloc_pd               = bnxt_re_dealloc_pd;
490
491         ibdev->create_ah                = bnxt_re_create_ah;
492         ibdev->modify_ah                = bnxt_re_modify_ah;
493         ibdev->query_ah                 = bnxt_re_query_ah;
494         ibdev->destroy_ah               = bnxt_re_destroy_ah;
495
496         ibdev->create_qp                = bnxt_re_create_qp;
497         ibdev->modify_qp                = bnxt_re_modify_qp;
498         ibdev->query_qp                 = bnxt_re_query_qp;
499         ibdev->destroy_qp               = bnxt_re_destroy_qp;
500
501         ibdev->post_send                = bnxt_re_post_send;
502         ibdev->post_recv                = bnxt_re_post_recv;
503
504         ibdev->create_cq                = bnxt_re_create_cq;
505         ibdev->destroy_cq               = bnxt_re_destroy_cq;
506         ibdev->poll_cq                  = bnxt_re_poll_cq;
507         ibdev->req_notify_cq            = bnxt_re_req_notify_cq;
508
509         ibdev->get_dma_mr               = bnxt_re_get_dma_mr;
510         ibdev->dereg_mr                 = bnxt_re_dereg_mr;
511         ibdev->alloc_mr                 = bnxt_re_alloc_mr;
512         ibdev->map_mr_sg                = bnxt_re_map_mr_sg;
513
514         ibdev->reg_user_mr              = bnxt_re_reg_user_mr;
515         ibdev->alloc_ucontext           = bnxt_re_alloc_ucontext;
516         ibdev->dealloc_ucontext         = bnxt_re_dealloc_ucontext;
517         ibdev->mmap                     = bnxt_re_mmap;
518         ibdev->get_hw_stats             = bnxt_re_ib_get_hw_stats;
519         ibdev->alloc_hw_stats           = bnxt_re_ib_alloc_hw_stats;
520
521         return ib_register_device(ibdev, NULL);
522 }
523
524 static ssize_t show_rev(struct device *device, struct device_attribute *attr,
525                         char *buf)
526 {
527         struct bnxt_re_dev *rdev = to_bnxt_re_dev(device, ibdev.dev);
528
529         return scnprintf(buf, PAGE_SIZE, "0x%x\n", rdev->en_dev->pdev->vendor);
530 }
531
532 static ssize_t show_fw_ver(struct device *device, struct device_attribute *attr,
533                            char *buf)
534 {
535         struct bnxt_re_dev *rdev = to_bnxt_re_dev(device, ibdev.dev);
536
537         return scnprintf(buf, PAGE_SIZE, "%s\n", rdev->dev_attr.fw_ver);
538 }
539
540 static ssize_t show_hca(struct device *device, struct device_attribute *attr,
541                         char *buf)
542 {
543         struct bnxt_re_dev *rdev = to_bnxt_re_dev(device, ibdev.dev);
544
545         return scnprintf(buf, PAGE_SIZE, "%s\n", rdev->ibdev.node_desc);
546 }
547
548 static DEVICE_ATTR(hw_rev, 0444, show_rev, NULL);
549 static DEVICE_ATTR(fw_rev, 0444, show_fw_ver, NULL);
550 static DEVICE_ATTR(hca_type, 0444, show_hca, NULL);
551
552 static struct device_attribute *bnxt_re_attributes[] = {
553         &dev_attr_hw_rev,
554         &dev_attr_fw_rev,
555         &dev_attr_hca_type
556 };
557
558 static void bnxt_re_dev_remove(struct bnxt_re_dev *rdev)
559 {
560         dev_put(rdev->netdev);
561         rdev->netdev = NULL;
562
563         mutex_lock(&bnxt_re_dev_lock);
564         list_del_rcu(&rdev->list);
565         mutex_unlock(&bnxt_re_dev_lock);
566
567         synchronize_rcu();
568         flush_workqueue(bnxt_re_wq);
569
570         ib_dealloc_device(&rdev->ibdev);
571         /* rdev is gone */
572 }
573
574 static struct bnxt_re_dev *bnxt_re_dev_add(struct net_device *netdev,
575                                            struct bnxt_en_dev *en_dev)
576 {
577         struct bnxt_re_dev *rdev;
578
579         /* Allocate bnxt_re_dev instance here */
580         rdev = (struct bnxt_re_dev *)ib_alloc_device(sizeof(*rdev));
581         if (!rdev) {
582                 dev_err(NULL, "%s: bnxt_re_dev allocation failure!",
583                         ROCE_DRV_MODULE_NAME);
584                 return NULL;
585         }
586         /* Default values */
587         rdev->netdev = netdev;
588         dev_hold(rdev->netdev);
589         rdev->en_dev = en_dev;
590         rdev->id = rdev->en_dev->pdev->devfn;
591         INIT_LIST_HEAD(&rdev->qp_list);
592         mutex_init(&rdev->qp_lock);
593         atomic_set(&rdev->qp_count, 0);
594         atomic_set(&rdev->cq_count, 0);
595         atomic_set(&rdev->srq_count, 0);
596         atomic_set(&rdev->mr_count, 0);
597         atomic_set(&rdev->mw_count, 0);
598         rdev->cosq[0] = 0xFFFF;
599         rdev->cosq[1] = 0xFFFF;
600
601         mutex_lock(&bnxt_re_dev_lock);
602         list_add_tail_rcu(&rdev->list, &bnxt_re_dev_list);
603         mutex_unlock(&bnxt_re_dev_lock);
604         return rdev;
605 }
606
607 static int bnxt_re_aeq_handler(struct bnxt_qplib_rcfw *rcfw,
608                                struct creq_func_event *aeqe)
609 {
610         switch (aeqe->event) {
611         case CREQ_FUNC_EVENT_EVENT_TX_WQE_ERROR:
612                 break;
613         case CREQ_FUNC_EVENT_EVENT_TX_DATA_ERROR:
614                 break;
615         case CREQ_FUNC_EVENT_EVENT_RX_WQE_ERROR:
616                 break;
617         case CREQ_FUNC_EVENT_EVENT_RX_DATA_ERROR:
618                 break;
619         case CREQ_FUNC_EVENT_EVENT_CQ_ERROR:
620                 break;
621         case CREQ_FUNC_EVENT_EVENT_TQM_ERROR:
622                 break;
623         case CREQ_FUNC_EVENT_EVENT_CFCQ_ERROR:
624                 break;
625         case CREQ_FUNC_EVENT_EVENT_CFCS_ERROR:
626                 break;
627         case CREQ_FUNC_EVENT_EVENT_CFCC_ERROR:
628                 break;
629         case CREQ_FUNC_EVENT_EVENT_CFCM_ERROR:
630                 break;
631         case CREQ_FUNC_EVENT_EVENT_TIM_ERROR:
632                 break;
633         default:
634                 return -EINVAL;
635         }
636         return 0;
637 }
638
639 static int bnxt_re_cqn_handler(struct bnxt_qplib_nq *nq,
640                                struct bnxt_qplib_cq *handle)
641 {
642         struct bnxt_re_cq *cq = container_of(handle, struct bnxt_re_cq,
643                                              qplib_cq);
644
645         if (!cq) {
646                 dev_err(NULL, "%s: CQ is NULL, CQN not handled",
647                         ROCE_DRV_MODULE_NAME);
648                 return -EINVAL;
649         }
650         if (cq->ib_cq.comp_handler) {
651                 /* Lock comp_handler? */
652                 (*cq->ib_cq.comp_handler)(&cq->ib_cq, cq->ib_cq.cq_context);
653         }
654
655         return 0;
656 }
657
658 static void bnxt_re_cleanup_res(struct bnxt_re_dev *rdev)
659 {
660         int i;
661
662         if (rdev->nq[0].hwq.max_elements) {
663                 for (i = 1; i < rdev->num_msix; i++)
664                         bnxt_qplib_disable_nq(&rdev->nq[i - 1]);
665         }
666
667         if (rdev->qplib_res.rcfw)
668                 bnxt_qplib_cleanup_res(&rdev->qplib_res);
669 }
670
671 static int bnxt_re_init_res(struct bnxt_re_dev *rdev)
672 {
673         int rc = 0, i;
674
675         bnxt_qplib_init_res(&rdev->qplib_res);
676
677         for (i = 1; i < rdev->num_msix ; i++) {
678                 rc = bnxt_qplib_enable_nq(rdev->en_dev->pdev, &rdev->nq[i - 1],
679                                           i - 1, rdev->msix_entries[i].vector,
680                                           rdev->msix_entries[i].db_offset,
681                                           &bnxt_re_cqn_handler, NULL);
682
683                 if (rc) {
684                         dev_err(rdev_to_dev(rdev),
685                                 "Failed to enable NQ with rc = 0x%x", rc);
686                         goto fail;
687                 }
688         }
689         return 0;
690 fail:
691         return rc;
692 }
693
694 static void bnxt_re_free_nq_res(struct bnxt_re_dev *rdev, bool lock_wait)
695 {
696         int i;
697
698         for (i = 0; i < rdev->num_msix - 1; i++) {
699                 bnxt_re_net_ring_free(rdev, rdev->nq[i].ring_id, lock_wait);
700                 bnxt_qplib_free_nq(&rdev->nq[i]);
701         }
702 }
703
704 static void bnxt_re_free_res(struct bnxt_re_dev *rdev, bool lock_wait)
705 {
706         bnxt_re_free_nq_res(rdev, lock_wait);
707
708         if (rdev->qplib_res.dpi_tbl.max) {
709                 bnxt_qplib_dealloc_dpi(&rdev->qplib_res,
710                                        &rdev->qplib_res.dpi_tbl,
711                                        &rdev->dpi_privileged);
712         }
713         if (rdev->qplib_res.rcfw) {
714                 bnxt_qplib_free_res(&rdev->qplib_res);
715                 rdev->qplib_res.rcfw = NULL;
716         }
717 }
718
719 static int bnxt_re_alloc_res(struct bnxt_re_dev *rdev)
720 {
721         int rc = 0, i;
722
723         /* Configure and allocate resources for qplib */
724         rdev->qplib_res.rcfw = &rdev->rcfw;
725         rc = bnxt_qplib_get_dev_attr(&rdev->rcfw, &rdev->dev_attr);
726         if (rc)
727                 goto fail;
728
729         rc = bnxt_qplib_alloc_res(&rdev->qplib_res, rdev->en_dev->pdev,
730                                   rdev->netdev, &rdev->dev_attr);
731         if (rc)
732                 goto fail;
733
734         rc = bnxt_qplib_alloc_dpi(&rdev->qplib_res.dpi_tbl,
735                                   &rdev->dpi_privileged,
736                                   rdev);
737         if (rc)
738                 goto dealloc_res;
739
740         for (i = 0; i < rdev->num_msix - 1; i++) {
741                 rdev->nq[i].hwq.max_elements = BNXT_RE_MAX_CQ_COUNT +
742                         BNXT_RE_MAX_SRQC_COUNT + 2;
743                 rc = bnxt_qplib_alloc_nq(rdev->en_dev->pdev, &rdev->nq[i]);
744                 if (rc) {
745                         dev_err(rdev_to_dev(rdev), "Alloc Failed NQ%d rc:%#x",
746                                 i, rc);
747                         goto dealloc_dpi;
748                 }
749                 rc = bnxt_re_net_ring_alloc
750                         (rdev, rdev->nq[i].hwq.pbl[PBL_LVL_0].pg_map_arr,
751                          rdev->nq[i].hwq.pbl[rdev->nq[i].hwq.level].pg_count,
752                          HWRM_RING_ALLOC_CMPL,
753                          BNXT_QPLIB_NQE_MAX_CNT - 1,
754                          rdev->msix_entries[i + 1].ring_idx,
755                          &rdev->nq[i].ring_id);
756                 if (rc) {
757                         dev_err(rdev_to_dev(rdev),
758                                 "Failed to allocate NQ fw id with rc = 0x%x",
759                                 rc);
760                         goto free_nq;
761                 }
762         }
763         return 0;
764 free_nq:
765         for (i = 0; i < rdev->num_msix - 1; i++)
766                 bnxt_qplib_free_nq(&rdev->nq[i]);
767 dealloc_dpi:
768         bnxt_qplib_dealloc_dpi(&rdev->qplib_res,
769                                &rdev->qplib_res.dpi_tbl,
770                                &rdev->dpi_privileged);
771 dealloc_res:
772         bnxt_qplib_free_res(&rdev->qplib_res);
773
774 fail:
775         rdev->qplib_res.rcfw = NULL;
776         return rc;
777 }
778
779 static void bnxt_re_dispatch_event(struct ib_device *ibdev, struct ib_qp *qp,
780                                    u8 port_num, enum ib_event_type event)
781 {
782         struct ib_event ib_event;
783
784         ib_event.device = ibdev;
785         if (qp) {
786                 ib_event.element.qp = qp;
787                 ib_event.event = event;
788                 if (qp->event_handler)
789                         qp->event_handler(&ib_event, qp->qp_context);
790
791         } else {
792                 ib_event.element.port_num = port_num;
793                 ib_event.event = event;
794                 ib_dispatch_event(&ib_event);
795         }
796 }
797
798 #define HWRM_QUEUE_PRI2COS_QCFG_INPUT_FLAGS_IVLAN      0x02
799 static int bnxt_re_query_hwrm_pri2cos(struct bnxt_re_dev *rdev, u8 dir,
800                                       u64 *cid_map)
801 {
802         struct hwrm_queue_pri2cos_qcfg_input req = {0};
803         struct bnxt *bp = netdev_priv(rdev->netdev);
804         struct hwrm_queue_pri2cos_qcfg_output resp;
805         struct bnxt_en_dev *en_dev = rdev->en_dev;
806         struct bnxt_fw_msg fw_msg;
807         u32 flags = 0;
808         u8 *qcfgmap, *tmp_map;
809         int rc = 0, i;
810
811         if (!cid_map)
812                 return -EINVAL;
813
814         memset(&fw_msg, 0, sizeof(fw_msg));
815         bnxt_re_init_hwrm_hdr(rdev, (void *)&req,
816                               HWRM_QUEUE_PRI2COS_QCFG, -1, -1);
817         flags |= (dir & 0x01);
818         flags |= HWRM_QUEUE_PRI2COS_QCFG_INPUT_FLAGS_IVLAN;
819         req.flags = cpu_to_le32(flags);
820         req.port_id = bp->pf.port_id;
821
822         bnxt_re_fill_fw_msg(&fw_msg, (void *)&req, sizeof(req), (void *)&resp,
823                             sizeof(resp), DFLT_HWRM_CMD_TIMEOUT);
824         rc = en_dev->en_ops->bnxt_send_fw_msg(en_dev, BNXT_ROCE_ULP, &fw_msg);
825         if (rc)
826                 return rc;
827
828         if (resp.queue_cfg_info) {
829                 dev_warn(rdev_to_dev(rdev),
830                          "Asymmetric cos queue configuration detected");
831                 dev_warn(rdev_to_dev(rdev),
832                          " on device, QoS may not be fully functional\n");
833         }
834         qcfgmap = &resp.pri0_cos_queue_id;
835         tmp_map = (u8 *)cid_map;
836         for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++)
837                 tmp_map[i] = qcfgmap[i];
838
839         return rc;
840 }
841
842 static bool bnxt_re_is_qp1_or_shadow_qp(struct bnxt_re_dev *rdev,
843                                         struct bnxt_re_qp *qp)
844 {
845         return (qp->ib_qp.qp_type == IB_QPT_GSI) || (qp == rdev->qp1_sqp);
846 }
847
848 static void bnxt_re_dev_stop(struct bnxt_re_dev *rdev)
849 {
850         int mask = IB_QP_STATE;
851         struct ib_qp_attr qp_attr;
852         struct bnxt_re_qp *qp;
853
854         qp_attr.qp_state = IB_QPS_ERR;
855         mutex_lock(&rdev->qp_lock);
856         list_for_each_entry(qp, &rdev->qp_list, list) {
857                 /* Modify the state of all QPs except QP1/Shadow QP */
858                 if (!bnxt_re_is_qp1_or_shadow_qp(rdev, qp)) {
859                         if (qp->qplib_qp.state !=
860                             CMDQ_MODIFY_QP_NEW_STATE_RESET &&
861                             qp->qplib_qp.state !=
862                             CMDQ_MODIFY_QP_NEW_STATE_ERR) {
863                                 bnxt_re_dispatch_event(&rdev->ibdev, &qp->ib_qp,
864                                                        1, IB_EVENT_QP_FATAL);
865                                 bnxt_re_modify_qp(&qp->ib_qp, &qp_attr, mask,
866                                                   NULL);
867                         }
868                 }
869         }
870         mutex_unlock(&rdev->qp_lock);
871 }
872
873 static int bnxt_re_update_gid(struct bnxt_re_dev *rdev)
874 {
875         struct bnxt_qplib_sgid_tbl *sgid_tbl = &rdev->qplib_res.sgid_tbl;
876         struct bnxt_qplib_gid gid;
877         u16 gid_idx, index;
878         int rc = 0;
879
880         if (!test_bit(BNXT_RE_FLAG_IBDEV_REGISTERED, &rdev->flags))
881                 return 0;
882
883         if (!sgid_tbl) {
884                 dev_err(rdev_to_dev(rdev), "QPLIB: SGID table not allocated");
885                 return -EINVAL;
886         }
887
888         for (index = 0; index < sgid_tbl->active; index++) {
889                 gid_idx = sgid_tbl->hw_id[index];
890
891                 if (!memcmp(&sgid_tbl->tbl[index], &bnxt_qplib_gid_zero,
892                             sizeof(bnxt_qplib_gid_zero)))
893                         continue;
894                 /* need to modify the VLAN enable setting of non VLAN GID only
895                  * as setting is done for VLAN GID while adding GID
896                  */
897                 if (sgid_tbl->vlan[index])
898                         continue;
899
900                 memcpy(&gid, &sgid_tbl->tbl[index], sizeof(gid));
901
902                 rc = bnxt_qplib_update_sgid(sgid_tbl, &gid, gid_idx,
903                                             rdev->qplib_res.netdev->dev_addr);
904         }
905
906         return rc;
907 }
908
909 static u32 bnxt_re_get_priority_mask(struct bnxt_re_dev *rdev)
910 {
911         u32 prio_map = 0, tmp_map = 0;
912         struct net_device *netdev;
913         struct dcb_app app;
914
915         netdev = rdev->netdev;
916
917         memset(&app, 0, sizeof(app));
918         app.selector = IEEE_8021QAZ_APP_SEL_ETHERTYPE;
919         app.protocol = ETH_P_IBOE;
920         tmp_map = dcb_ieee_getapp_mask(netdev, &app);
921         prio_map = tmp_map;
922
923         app.selector = IEEE_8021QAZ_APP_SEL_DGRAM;
924         app.protocol = ROCE_V2_UDP_DPORT;
925         tmp_map = dcb_ieee_getapp_mask(netdev, &app);
926         prio_map |= tmp_map;
927
928         return prio_map;
929 }
930
931 static void bnxt_re_parse_cid_map(u8 prio_map, u8 *cid_map, u16 *cosq)
932 {
933         u16 prio;
934         u8 id;
935
936         for (prio = 0, id = 0; prio < 8; prio++) {
937                 if (prio_map & (1 << prio)) {
938                         cosq[id] = cid_map[prio];
939                         id++;
940                         if (id == 2) /* Max 2 tcs supported */
941                                 break;
942                 }
943         }
944 }
945
946 static int bnxt_re_setup_qos(struct bnxt_re_dev *rdev)
947 {
948         u8 prio_map = 0;
949         u64 cid_map;
950         int rc;
951
952         /* Get priority for roce */
953         prio_map = bnxt_re_get_priority_mask(rdev);
954
955         if (prio_map == rdev->cur_prio_map)
956                 return 0;
957         rdev->cur_prio_map = prio_map;
958         /* Get cosq id for this priority */
959         rc = bnxt_re_query_hwrm_pri2cos(rdev, 0, &cid_map);
960         if (rc) {
961                 dev_warn(rdev_to_dev(rdev), "no cos for p_mask %x\n", prio_map);
962                 return rc;
963         }
964         /* Parse CoS IDs for app priority */
965         bnxt_re_parse_cid_map(prio_map, (u8 *)&cid_map, rdev->cosq);
966
967         /* Config BONO. */
968         rc = bnxt_qplib_map_tc2cos(&rdev->qplib_res, rdev->cosq);
969         if (rc) {
970                 dev_warn(rdev_to_dev(rdev), "no tc for cos{%x, %x}\n",
971                          rdev->cosq[0], rdev->cosq[1]);
972                 return rc;
973         }
974
975         /* Actual priorities are not programmed as they are already
976          * done by L2 driver; just enable or disable priority vlan tagging
977          */
978         if ((prio_map == 0 && rdev->qplib_res.prio) ||
979             (prio_map != 0 && !rdev->qplib_res.prio)) {
980                 rdev->qplib_res.prio = prio_map ? true : false;
981
982                 bnxt_re_update_gid(rdev);
983         }
984
985         return 0;
986 }
987
988 static void bnxt_re_ib_unreg(struct bnxt_re_dev *rdev, bool lock_wait)
989 {
990         int i, rc;
991
992         if (test_and_clear_bit(BNXT_RE_FLAG_IBDEV_REGISTERED, &rdev->flags)) {
993                 for (i = 0; i < ARRAY_SIZE(bnxt_re_attributes); i++)
994                         device_remove_file(&rdev->ibdev.dev,
995                                            bnxt_re_attributes[i]);
996                 /* Cleanup ib dev */
997                 bnxt_re_unregister_ib(rdev);
998         }
999         if (test_and_clear_bit(BNXT_RE_FLAG_QOS_WORK_REG, &rdev->flags))
1000                 cancel_delayed_work(&rdev->worker);
1001
1002         bnxt_re_cleanup_res(rdev);
1003         bnxt_re_free_res(rdev, lock_wait);
1004
1005         if (test_and_clear_bit(BNXT_RE_FLAG_RCFW_CHANNEL_EN, &rdev->flags)) {
1006                 rc = bnxt_qplib_deinit_rcfw(&rdev->rcfw);
1007                 if (rc)
1008                         dev_warn(rdev_to_dev(rdev),
1009                                  "Failed to deinitialize RCFW: %#x", rc);
1010                 bnxt_re_net_stats_ctx_free(rdev, rdev->qplib_ctx.stats.fw_id,
1011                                            lock_wait);
1012                 bnxt_qplib_free_ctx(rdev->en_dev->pdev, &rdev->qplib_ctx);
1013                 bnxt_qplib_disable_rcfw_channel(&rdev->rcfw);
1014                 bnxt_re_net_ring_free(rdev, rdev->rcfw.creq_ring_id, lock_wait);
1015                 bnxt_qplib_free_rcfw_channel(&rdev->rcfw);
1016         }
1017         if (test_and_clear_bit(BNXT_RE_FLAG_GOT_MSIX, &rdev->flags)) {
1018                 rc = bnxt_re_free_msix(rdev, lock_wait);
1019                 if (rc)
1020                         dev_warn(rdev_to_dev(rdev),
1021                                  "Failed to free MSI-X vectors: %#x", rc);
1022         }
1023         if (test_and_clear_bit(BNXT_RE_FLAG_NETDEV_REGISTERED, &rdev->flags)) {
1024                 rc = bnxt_re_unregister_netdev(rdev, lock_wait);
1025                 if (rc)
1026                         dev_warn(rdev_to_dev(rdev),
1027                                  "Failed to unregister with netdev: %#x", rc);
1028         }
1029 }
1030
1031 static void bnxt_re_set_resource_limits(struct bnxt_re_dev *rdev)
1032 {
1033         u32 i;
1034
1035         rdev->qplib_ctx.qpc_count = BNXT_RE_MAX_QPC_COUNT;
1036         rdev->qplib_ctx.mrw_count = BNXT_RE_MAX_MRW_COUNT;
1037         rdev->qplib_ctx.srqc_count = BNXT_RE_MAX_SRQC_COUNT;
1038         rdev->qplib_ctx.cq_count = BNXT_RE_MAX_CQ_COUNT;
1039         for (i = 0; i < MAX_TQM_ALLOC_REQ; i++)
1040                 rdev->qplib_ctx.tqm_count[i] =
1041                 rdev->dev_attr.tqm_alloc_reqs[i];
1042 }
1043
1044 /* worker thread for polling periodic events. Now used for QoS programming*/
1045 static void bnxt_re_worker(struct work_struct *work)
1046 {
1047         struct bnxt_re_dev *rdev = container_of(work, struct bnxt_re_dev,
1048                                                 worker.work);
1049
1050         bnxt_re_setup_qos(rdev);
1051         schedule_delayed_work(&rdev->worker, msecs_to_jiffies(30000));
1052 }
1053
1054 static int bnxt_re_ib_reg(struct bnxt_re_dev *rdev)
1055 {
1056         int i, j, rc;
1057
1058         /* Registered a new RoCE device instance to netdev */
1059         rc = bnxt_re_register_netdev(rdev);
1060         if (rc) {
1061                 pr_err("Failed to register with netedev: %#x\n", rc);
1062                 return -EINVAL;
1063         }
1064         set_bit(BNXT_RE_FLAG_NETDEV_REGISTERED, &rdev->flags);
1065
1066         rc = bnxt_re_request_msix(rdev);
1067         if (rc) {
1068                 pr_err("Failed to get MSI-X vectors: %#x\n", rc);
1069                 rc = -EINVAL;
1070                 goto fail;
1071         }
1072         set_bit(BNXT_RE_FLAG_GOT_MSIX, &rdev->flags);
1073
1074         /* Establish RCFW Communication Channel to initialize the context
1075          * memory for the function and all child VFs
1076          */
1077         rc = bnxt_qplib_alloc_rcfw_channel(rdev->en_dev->pdev, &rdev->rcfw,
1078                                            BNXT_RE_MAX_QPC_COUNT);
1079         if (rc)
1080                 goto fail;
1081
1082         rc = bnxt_re_net_ring_alloc
1083                         (rdev, rdev->rcfw.creq.pbl[PBL_LVL_0].pg_map_arr,
1084                          rdev->rcfw.creq.pbl[rdev->rcfw.creq.level].pg_count,
1085                          HWRM_RING_ALLOC_CMPL, BNXT_QPLIB_CREQE_MAX_CNT - 1,
1086                          rdev->msix_entries[BNXT_RE_AEQ_IDX].ring_idx,
1087                          &rdev->rcfw.creq_ring_id);
1088         if (rc) {
1089                 pr_err("Failed to allocate CREQ: %#x\n", rc);
1090                 goto free_rcfw;
1091         }
1092         rc = bnxt_qplib_enable_rcfw_channel
1093                                 (rdev->en_dev->pdev, &rdev->rcfw,
1094                                  rdev->msix_entries[BNXT_RE_AEQ_IDX].vector,
1095                                  rdev->msix_entries[BNXT_RE_AEQ_IDX].db_offset,
1096                                  0, &bnxt_re_aeq_handler);
1097         if (rc) {
1098                 pr_err("Failed to enable RCFW channel: %#x\n", rc);
1099                 goto free_ring;
1100         }
1101
1102         rc = bnxt_qplib_get_dev_attr(&rdev->rcfw, &rdev->dev_attr);
1103         if (rc)
1104                 goto disable_rcfw;
1105         bnxt_re_set_resource_limits(rdev);
1106
1107         rc = bnxt_qplib_alloc_ctx(rdev->en_dev->pdev, &rdev->qplib_ctx, 0);
1108         if (rc) {
1109                 pr_err("Failed to allocate QPLIB context: %#x\n", rc);
1110                 goto disable_rcfw;
1111         }
1112         rc = bnxt_re_net_stats_ctx_alloc(rdev,
1113                                          rdev->qplib_ctx.stats.dma_map,
1114                                          &rdev->qplib_ctx.stats.fw_id);
1115         if (rc) {
1116                 pr_err("Failed to allocate stats context: %#x\n", rc);
1117                 goto free_ctx;
1118         }
1119
1120         rc = bnxt_qplib_init_rcfw(&rdev->rcfw, &rdev->qplib_ctx, 0);
1121         if (rc) {
1122                 pr_err("Failed to initialize RCFW: %#x\n", rc);
1123                 goto free_sctx;
1124         }
1125         set_bit(BNXT_RE_FLAG_RCFW_CHANNEL_EN, &rdev->flags);
1126
1127         /* Resources based on the 'new' device caps */
1128         rc = bnxt_re_alloc_res(rdev);
1129         if (rc) {
1130                 pr_err("Failed to allocate resources: %#x\n", rc);
1131                 goto fail;
1132         }
1133         rc = bnxt_re_init_res(rdev);
1134         if (rc) {
1135                 pr_err("Failed to initialize resources: %#x\n", rc);
1136                 goto fail;
1137         }
1138
1139         rc = bnxt_re_setup_qos(rdev);
1140         if (rc)
1141                 pr_info("RoCE priority not yet configured\n");
1142
1143         INIT_DELAYED_WORK(&rdev->worker, bnxt_re_worker);
1144         set_bit(BNXT_RE_FLAG_QOS_WORK_REG, &rdev->flags);
1145         schedule_delayed_work(&rdev->worker, msecs_to_jiffies(30000));
1146
1147         /* Register ib dev */
1148         rc = bnxt_re_register_ib(rdev);
1149         if (rc) {
1150                 pr_err("Failed to register with IB: %#x\n", rc);
1151                 goto fail;
1152         }
1153         dev_info(rdev_to_dev(rdev), "Device registered successfully");
1154         for (i = 0; i < ARRAY_SIZE(bnxt_re_attributes); i++) {
1155                 rc = device_create_file(&rdev->ibdev.dev,
1156                                         bnxt_re_attributes[i]);
1157                 if (rc) {
1158                         dev_err(rdev_to_dev(rdev),
1159                                 "Failed to create IB sysfs: %#x", rc);
1160                         /* Must clean up all created device files */
1161                         for (j = 0; j < i; j++)
1162                                 device_remove_file(&rdev->ibdev.dev,
1163                                                    bnxt_re_attributes[j]);
1164                         bnxt_re_unregister_ib(rdev);
1165                         goto fail;
1166                 }
1167         }
1168         set_bit(BNXT_RE_FLAG_IBDEV_REGISTERED, &rdev->flags);
1169         ib_get_eth_speed(&rdev->ibdev, 1, &rdev->active_speed,
1170                          &rdev->active_width);
1171         bnxt_re_dispatch_event(&rdev->ibdev, NULL, 1, IB_EVENT_PORT_ACTIVE);
1172         bnxt_re_dispatch_event(&rdev->ibdev, NULL, 1, IB_EVENT_GID_CHANGE);
1173
1174         return 0;
1175 free_sctx:
1176         bnxt_re_net_stats_ctx_free(rdev, rdev->qplib_ctx.stats.fw_id, true);
1177 free_ctx:
1178         bnxt_qplib_free_ctx(rdev->en_dev->pdev, &rdev->qplib_ctx);
1179 disable_rcfw:
1180         bnxt_qplib_disable_rcfw_channel(&rdev->rcfw);
1181 free_ring:
1182         bnxt_re_net_ring_free(rdev, rdev->rcfw.creq_ring_id, true);
1183 free_rcfw:
1184         bnxt_qplib_free_rcfw_channel(&rdev->rcfw);
1185 fail:
1186         bnxt_re_ib_unreg(rdev, true);
1187         return rc;
1188 }
1189
1190 static void bnxt_re_dev_unreg(struct bnxt_re_dev *rdev)
1191 {
1192         struct bnxt_en_dev *en_dev = rdev->en_dev;
1193         struct net_device *netdev = rdev->netdev;
1194
1195         bnxt_re_dev_remove(rdev);
1196
1197         if (netdev)
1198                 bnxt_re_dev_unprobe(netdev, en_dev);
1199 }
1200
1201 static int bnxt_re_dev_reg(struct bnxt_re_dev **rdev, struct net_device *netdev)
1202 {
1203         struct bnxt_en_dev *en_dev;
1204         int rc = 0;
1205
1206         if (!is_bnxt_re_dev(netdev))
1207                 return -ENODEV;
1208
1209         en_dev = bnxt_re_dev_probe(netdev);
1210         if (IS_ERR(en_dev)) {
1211                 if (en_dev != ERR_PTR(-ENODEV))
1212                         pr_err("%s: Failed to probe\n", ROCE_DRV_MODULE_NAME);
1213                 rc = PTR_ERR(en_dev);
1214                 goto exit;
1215         }
1216         *rdev = bnxt_re_dev_add(netdev, en_dev);
1217         if (!*rdev) {
1218                 rc = -ENOMEM;
1219                 bnxt_re_dev_unprobe(netdev, en_dev);
1220                 goto exit;
1221         }
1222 exit:
1223         return rc;
1224 }
1225
1226 static void bnxt_re_remove_one(struct bnxt_re_dev *rdev)
1227 {
1228         pci_dev_put(rdev->en_dev->pdev);
1229 }
1230
1231 /* Handle all deferred netevents tasks */
1232 static void bnxt_re_task(struct work_struct *work)
1233 {
1234         struct bnxt_re_work *re_work;
1235         struct bnxt_re_dev *rdev;
1236         int rc = 0;
1237
1238         re_work = container_of(work, struct bnxt_re_work, work);
1239         rdev = re_work->rdev;
1240
1241         if (re_work->event != NETDEV_REGISTER &&
1242             !test_bit(BNXT_RE_FLAG_IBDEV_REGISTERED, &rdev->flags))
1243                 return;
1244
1245         switch (re_work->event) {
1246         case NETDEV_REGISTER:
1247                 rc = bnxt_re_ib_reg(rdev);
1248                 if (rc) {
1249                         dev_err(rdev_to_dev(rdev),
1250                                 "Failed to register with IB: %#x", rc);
1251                         bnxt_re_remove_one(rdev);
1252                         bnxt_re_dev_unreg(rdev);
1253                 }
1254                 break;
1255         case NETDEV_UP:
1256                 bnxt_re_dispatch_event(&rdev->ibdev, NULL, 1,
1257                                        IB_EVENT_PORT_ACTIVE);
1258                 break;
1259         case NETDEV_DOWN:
1260                 bnxt_re_dev_stop(rdev);
1261                 break;
1262         case NETDEV_CHANGE:
1263                 if (!netif_carrier_ok(rdev->netdev))
1264                         bnxt_re_dev_stop(rdev);
1265                 else if (netif_carrier_ok(rdev->netdev))
1266                         bnxt_re_dispatch_event(&rdev->ibdev, NULL, 1,
1267                                                IB_EVENT_PORT_ACTIVE);
1268                 ib_get_eth_speed(&rdev->ibdev, 1, &rdev->active_speed,
1269                                  &rdev->active_width);
1270                 break;
1271         default:
1272                 break;
1273         }
1274         smp_mb__before_atomic();
1275         clear_bit(BNXT_RE_FLAG_TASK_IN_PROG, &rdev->flags);
1276         kfree(re_work);
1277 }
1278
1279 static void bnxt_re_init_one(struct bnxt_re_dev *rdev)
1280 {
1281         pci_dev_get(rdev->en_dev->pdev);
1282 }
1283
1284 /*
1285  * "Notifier chain callback can be invoked for the same chain from
1286  * different CPUs at the same time".
1287  *
1288  * For cases when the netdev is already present, our call to the
1289  * register_netdevice_notifier() will actually get the rtnl_lock()
1290  * before sending NETDEV_REGISTER and (if up) NETDEV_UP
1291  * events.
1292  *
1293  * But for cases when the netdev is not already present, the notifier
1294  * chain is subjected to be invoked from different CPUs simultaneously.
1295  *
1296  * This is protected by the netdev_mutex.
1297  */
1298 static int bnxt_re_netdev_event(struct notifier_block *notifier,
1299                                 unsigned long event, void *ptr)
1300 {
1301         struct net_device *real_dev, *netdev = netdev_notifier_info_to_dev(ptr);
1302         struct bnxt_re_work *re_work;
1303         struct bnxt_re_dev *rdev;
1304         int rc = 0;
1305         bool sch_work = false;
1306
1307         real_dev = rdma_vlan_dev_real_dev(netdev);
1308         if (!real_dev)
1309                 real_dev = netdev;
1310
1311         rdev = bnxt_re_from_netdev(real_dev);
1312         if (!rdev && event != NETDEV_REGISTER)
1313                 goto exit;
1314         if (real_dev != netdev)
1315                 goto exit;
1316
1317         switch (event) {
1318         case NETDEV_REGISTER:
1319                 if (rdev)
1320                         break;
1321                 rc = bnxt_re_dev_reg(&rdev, real_dev);
1322                 if (rc == -ENODEV)
1323                         break;
1324                 if (rc) {
1325                         pr_err("Failed to register with the device %s: %#x\n",
1326                                real_dev->name, rc);
1327                         break;
1328                 }
1329                 bnxt_re_init_one(rdev);
1330                 sch_work = true;
1331                 break;
1332
1333         case NETDEV_UNREGISTER:
1334                 /* netdev notifier will call NETDEV_UNREGISTER again later since
1335                  * we are still holding the reference to the netdev
1336                  */
1337                 if (test_bit(BNXT_RE_FLAG_TASK_IN_PROG, &rdev->flags))
1338                         goto exit;
1339                 bnxt_re_ib_unreg(rdev, false);
1340                 bnxt_re_remove_one(rdev);
1341                 bnxt_re_dev_unreg(rdev);
1342                 break;
1343
1344         default:
1345                 sch_work = true;
1346                 break;
1347         }
1348         if (sch_work) {
1349                 /* Allocate for the deferred task */
1350                 re_work = kzalloc(sizeof(*re_work), GFP_ATOMIC);
1351                 if (re_work) {
1352                         re_work->rdev = rdev;
1353                         re_work->event = event;
1354                         re_work->vlan_dev = (real_dev == netdev ?
1355                                              NULL : netdev);
1356                         INIT_WORK(&re_work->work, bnxt_re_task);
1357                         set_bit(BNXT_RE_FLAG_TASK_IN_PROG, &rdev->flags);
1358                         queue_work(bnxt_re_wq, &re_work->work);
1359                 }
1360         }
1361
1362 exit:
1363         return NOTIFY_DONE;
1364 }
1365
1366 static struct notifier_block bnxt_re_netdev_notifier = {
1367         .notifier_call = bnxt_re_netdev_event
1368 };
1369
1370 static int __init bnxt_re_mod_init(void)
1371 {
1372         int rc = 0;
1373
1374         pr_info("%s: %s", ROCE_DRV_MODULE_NAME, version);
1375
1376         bnxt_re_wq = create_singlethread_workqueue("bnxt_re");
1377         if (!bnxt_re_wq)
1378                 return -ENOMEM;
1379
1380         INIT_LIST_HEAD(&bnxt_re_dev_list);
1381
1382         rc = register_netdevice_notifier(&bnxt_re_netdev_notifier);
1383         if (rc) {
1384                 pr_err("%s: Cannot register to netdevice_notifier",
1385                        ROCE_DRV_MODULE_NAME);
1386                 goto err_netdev;
1387         }
1388         return 0;
1389
1390 err_netdev:
1391         destroy_workqueue(bnxt_re_wq);
1392
1393         return rc;
1394 }
1395
1396 static void __exit bnxt_re_mod_exit(void)
1397 {
1398         struct bnxt_re_dev *rdev;
1399         LIST_HEAD(to_be_deleted);
1400
1401         mutex_lock(&bnxt_re_dev_lock);
1402         /* Free all adapter allocated resources */
1403         if (!list_empty(&bnxt_re_dev_list))
1404                 list_splice_init(&bnxt_re_dev_list, &to_be_deleted);
1405         mutex_unlock(&bnxt_re_dev_lock);
1406
1407         list_for_each_entry(rdev, &to_be_deleted, list) {
1408                 dev_info(rdev_to_dev(rdev), "Unregistering Device");
1409                 /*
1410                  * Flush out any scheduled tasks before destroying the
1411                  * resources
1412                  */
1413                 flush_workqueue(bnxt_re_wq);
1414                 bnxt_re_dev_stop(rdev);
1415                 bnxt_re_ib_unreg(rdev, true);
1416                 bnxt_re_remove_one(rdev);
1417                 bnxt_re_dev_unreg(rdev);
1418         }
1419         unregister_netdevice_notifier(&bnxt_re_netdev_notifier);
1420         if (bnxt_re_wq)
1421                 destroy_workqueue(bnxt_re_wq);
1422 }
1423
1424 module_init(bnxt_re_mod_init);
1425 module_exit(bnxt_re_mod_exit);