blob: 8a1398b253a2bec42f2d0032e0aa915ce747bfd9 [file] [log] [blame]
Parav Panditfe2caef2012-03-21 04:09:06 +05301/*******************************************************************
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>
Parav Panditfe2caef2012-03-21 04:09:06 +053029#include <linux/idr.h>
30#include <rdma/ib_verbs.h>
31#include <rdma/ib_user_verbs.h>
32#include <rdma/ib_addr.h>
Ira Weiny337877a2015-06-06 14:38:29 -040033#include <rdma/ib_mad.h>
Parav Panditfe2caef2012-03-21 04:09:06 +053034
35#include <linux/netdevice.h>
36#include <net/addrconf.h>
37
38#include "ocrdma.h"
39#include "ocrdma_verbs.h"
40#include "ocrdma_ah.h"
41#include "be_roce.h"
42#include "ocrdma_hw.h"
Selvin Xaviera51f06e2014-02-04 11:57:07 +053043#include "ocrdma_stats.h"
Naresh Gottumukkala38754392013-08-26 15:27:49 +053044#include "ocrdma_abi.h"
Parav Panditfe2caef2012-03-21 04:09:06 +053045
Devesh Sharma01544102014-02-04 11:57:00 +053046MODULE_VERSION(OCRDMA_ROCE_DRV_VERSION);
47MODULE_DESCRIPTION(OCRDMA_ROCE_DRV_DESC " " OCRDMA_ROCE_DRV_VERSION);
Parav Panditfe2caef2012-03-21 04:09:06 +053048MODULE_AUTHOR("Emulex Corporation");
49MODULE_LICENSE("GPL");
50
51static LIST_HEAD(ocrdma_dev_list);
Sasha Levin3e4d60a2012-04-28 07:40:01 +020052static DEFINE_SPINLOCK(ocrdma_devlist_lock);
Parav Panditfe2caef2012-03-21 04:09:06 +053053static DEFINE_IDR(ocrdma_dev_id);
54
55static union ib_gid ocrdma_zero_sgid;
Parav Panditfe2caef2012-03-21 04:09:06 +053056
Parav Panditfe2caef2012-03-21 04:09:06 +053057void ocrdma_get_guid(struct ocrdma_dev *dev, u8 *guid)
58{
59 u8 mac_addr[6];
60
61 memcpy(&mac_addr[0], &dev->nic_info.mac_addr[0], ETH_ALEN);
62 guid[0] = mac_addr[0] ^ 2;
63 guid[1] = mac_addr[1];
64 guid[2] = mac_addr[2];
65 guid[3] = 0xff;
66 guid[4] = 0xfe;
67 guid[5] = mac_addr[3];
68 guid[6] = mac_addr[4];
69 guid[7] = mac_addr[5];
70}
71
Moni Shoua37721d82013-12-12 18:03:16 +020072static bool ocrdma_add_sgid(struct ocrdma_dev *dev, union ib_gid *new_sgid)
Parav Panditfe2caef2012-03-21 04:09:06 +053073{
74 int i;
Parav Panditfe2caef2012-03-21 04:09:06 +053075 unsigned long flags;
76
77 memset(&ocrdma_zero_sgid, 0, sizeof(union ib_gid));
78
Parav Panditfe2caef2012-03-21 04:09:06 +053079
80 spin_lock_irqsave(&dev->sgid_lock, flags);
81 for (i = 0; i < OCRDMA_MAX_SGID; i++) {
82 if (!memcmp(&dev->sgid_tbl[i], &ocrdma_zero_sgid,
83 sizeof(union ib_gid))) {
84 /* found free entry */
Moni Shoua37721d82013-12-12 18:03:16 +020085 memcpy(&dev->sgid_tbl[i], new_sgid,
Parav Pandit6ab68272012-06-08 21:25:25 +053086 sizeof(union ib_gid));
87 spin_unlock_irqrestore(&dev->sgid_lock, flags);
88 return true;
Moni Shoua37721d82013-12-12 18:03:16 +020089 } else if (!memcmp(&dev->sgid_tbl[i], new_sgid,
Parav Panditfe2caef2012-03-21 04:09:06 +053090 sizeof(union ib_gid))) {
91 /* entry already present, no addition is required. */
92 spin_unlock_irqrestore(&dev->sgid_lock, flags);
Parav Pandit6ab68272012-06-08 21:25:25 +053093 return false;
Parav Panditfe2caef2012-03-21 04:09:06 +053094 }
95 }
Parav Panditfe2caef2012-03-21 04:09:06 +053096 spin_unlock_irqrestore(&dev->sgid_lock, flags);
Parav Pandit6ab68272012-06-08 21:25:25 +053097 return false;
Parav Panditfe2caef2012-03-21 04:09:06 +053098}
99
Moni Shoua37721d82013-12-12 18:03:16 +0200100static bool ocrdma_del_sgid(struct ocrdma_dev *dev, union ib_gid *sgid)
Parav Panditfe2caef2012-03-21 04:09:06 +0530101{
102 int found = false;
103 int i;
Parav Panditfe2caef2012-03-21 04:09:06 +0530104 unsigned long flags;
105
Parav Panditfe2caef2012-03-21 04:09:06 +0530106
107 spin_lock_irqsave(&dev->sgid_lock, flags);
108 /* first is default sgid, which cannot be deleted. */
109 for (i = 1; i < OCRDMA_MAX_SGID; i++) {
Moni Shoua37721d82013-12-12 18:03:16 +0200110 if (!memcmp(&dev->sgid_tbl[i], sgid, sizeof(union ib_gid))) {
Parav Panditfe2caef2012-03-21 04:09:06 +0530111 /* found matching entry */
112 memset(&dev->sgid_tbl[i], 0, sizeof(union ib_gid));
113 found = true;
114 break;
115 }
116 }
117 spin_unlock_irqrestore(&dev->sgid_lock, flags);
118 return found;
119}
120
Moni Shoua37721d82013-12-12 18:03:16 +0200121static int ocrdma_addr_event(unsigned long event, struct net_device *netdev,
122 union ib_gid *gid)
Parav Panditfe2caef2012-03-21 04:09:06 +0530123{
Parav Panditfe2caef2012-03-21 04:09:06 +0530124 struct ib_event gid_event;
125 struct ocrdma_dev *dev;
126 bool found = false;
Parav Pandit6ab68272012-06-08 21:25:25 +0530127 bool updated = false;
Parav Panditfe2caef2012-03-21 04:09:06 +0530128 bool is_vlan = false;
Parav Panditfe2caef2012-03-21 04:09:06 +0530129
Roland Dreierd549f552012-08-10 16:52:13 -0700130 is_vlan = netdev->priv_flags & IFF_802_1Q_VLAN;
Moni Shoua37721d82013-12-12 18:03:16 +0200131 if (is_vlan)
Devesh Sharma09de3f12014-02-04 12:10:48 +0530132 netdev = rdma_vlan_dev_real_dev(netdev);
Roland Dreierd549f552012-08-10 16:52:13 -0700133
Sasha Levin3e4d60a2012-04-28 07:40:01 +0200134 rcu_read_lock();
135 list_for_each_entry_rcu(dev, &ocrdma_dev_list, entry) {
Parav Panditfe2caef2012-03-21 04:09:06 +0530136 if (dev->nic_info.netdev == netdev) {
137 found = true;
138 break;
139 }
140 }
Sasha Levin3e4d60a2012-04-28 07:40:01 +0200141 rcu_read_unlock();
Parav Panditfe2caef2012-03-21 04:09:06 +0530142
143 if (!found)
144 return NOTIFY_DONE;
Parav Panditfe2caef2012-03-21 04:09:06 +0530145
146 mutex_lock(&dev->dev_lock);
147 switch (event) {
148 case NETDEV_UP:
Moni Shoua37721d82013-12-12 18:03:16 +0200149 updated = ocrdma_add_sgid(dev, gid);
Parav Panditfe2caef2012-03-21 04:09:06 +0530150 break;
151 case NETDEV_DOWN:
Moni Shoua37721d82013-12-12 18:03:16 +0200152 updated = ocrdma_del_sgid(dev, gid);
Parav Panditfe2caef2012-03-21 04:09:06 +0530153 break;
154 default:
155 break;
156 }
Parav Pandit6ab68272012-06-08 21:25:25 +0530157 if (updated) {
158 /* GID table updated, notify the consumers about it */
159 gid_event.device = &dev->ibdev;
160 gid_event.element.port_num = 1;
161 gid_event.event = IB_EVENT_GID_CHANGE;
162 ib_dispatch_event(&gid_event);
163 }
Parav Panditfe2caef2012-03-21 04:09:06 +0530164 mutex_unlock(&dev->dev_lock);
165 return NOTIFY_OK;
166}
167
Moni Shoua37721d82013-12-12 18:03:16 +0200168static int ocrdma_inetaddr_event(struct notifier_block *notifier,
169 unsigned long event, void *ptr)
170{
171 struct in_ifaddr *ifa = ptr;
172 union ib_gid gid;
173 struct net_device *netdev = ifa->ifa_dev->dev;
174
175 ipv6_addr_set_v4mapped(ifa->ifa_address, (struct in6_addr *)&gid);
176 return ocrdma_addr_event(event, netdev, &gid);
177}
178
Roland Dreier31ab8ac2014-01-19 15:13:44 -0800179static struct notifier_block ocrdma_inetaddr_notifier = {
180 .notifier_call = ocrdma_inetaddr_event
181};
182
Moni Shoua37721d82013-12-12 18:03:16 +0200183#if IS_ENABLED(CONFIG_IPV6)
184
185static int ocrdma_inet6addr_event(struct notifier_block *notifier,
186 unsigned long event, void *ptr)
187{
188 struct inet6_ifaddr *ifa = (struct inet6_ifaddr *)ptr;
189 union ib_gid *gid = (union ib_gid *)&ifa->addr;
190 struct net_device *netdev = ifa->idev->dev;
191 return ocrdma_addr_event(event, netdev, gid);
192}
193
Roland Dreier34955662012-05-02 15:42:33 -0700194static struct notifier_block ocrdma_inet6addr_notifier = {
195 .notifier_call = ocrdma_inet6addr_event
196};
197
Parav Pandit6ab68272012-06-08 21:25:25 +0530198#endif /* IPV6 and VLAN */
Roland Dreier34955662012-05-02 15:42:33 -0700199
Parav Panditfe2caef2012-03-21 04:09:06 +0530200static enum rdma_link_layer ocrdma_link_layer(struct ib_device *device,
201 u8 port_num)
202{
203 return IB_LINK_LAYER_ETHERNET;
204}
205
Ira Weiny77386132015-05-13 20:02:58 -0400206static int ocrdma_port_immutable(struct ib_device *ibdev, u8 port_num,
207 struct ib_port_immutable *immutable)
208{
209 struct ib_port_attr attr;
210 int err;
211
212 err = ocrdma_query_port(ibdev, port_num, &attr);
213 if (err)
214 return err;
215
216 immutable->pkey_tbl_len = attr.pkey_tbl_len;
217 immutable->gid_tbl_len = attr.gid_tbl_len;
Ira Weinyf9b22e32015-05-13 20:02:59 -0400218 immutable->core_cap_flags = RDMA_CORE_PORT_IBA_ROCE;
Ira Weiny337877a2015-06-06 14:38:29 -0400219 immutable->max_mad_size = IB_MGMT_MAD_SIZE;
Ira Weiny77386132015-05-13 20:02:58 -0400220
221 return 0;
222}
223
Roland Dreierabe3afa2012-04-16 11:36:29 -0700224static int ocrdma_register_device(struct ocrdma_dev *dev)
Parav Panditfe2caef2012-03-21 04:09:06 +0530225{
226 strlcpy(dev->ibdev.name, "ocrdma%d", IB_DEVICE_NAME_MAX);
227 ocrdma_get_guid(dev, (u8 *)&dev->ibdev.node_guid);
228 memcpy(dev->ibdev.node_desc, OCRDMA_NODE_DESC,
229 sizeof(OCRDMA_NODE_DESC));
230 dev->ibdev.owner = THIS_MODULE;
Naresh Gottumukkala38754392013-08-26 15:27:49 +0530231 dev->ibdev.uverbs_abi_ver = OCRDMA_ABI_VERSION;
Parav Panditfe2caef2012-03-21 04:09:06 +0530232 dev->ibdev.uverbs_cmd_mask =
233 OCRDMA_UVERBS(GET_CONTEXT) |
234 OCRDMA_UVERBS(QUERY_DEVICE) |
235 OCRDMA_UVERBS(QUERY_PORT) |
236 OCRDMA_UVERBS(ALLOC_PD) |
237 OCRDMA_UVERBS(DEALLOC_PD) |
238 OCRDMA_UVERBS(REG_MR) |
239 OCRDMA_UVERBS(DEREG_MR) |
240 OCRDMA_UVERBS(CREATE_COMP_CHANNEL) |
241 OCRDMA_UVERBS(CREATE_CQ) |
242 OCRDMA_UVERBS(RESIZE_CQ) |
243 OCRDMA_UVERBS(DESTROY_CQ) |
244 OCRDMA_UVERBS(REQ_NOTIFY_CQ) |
245 OCRDMA_UVERBS(CREATE_QP) |
246 OCRDMA_UVERBS(MODIFY_QP) |
247 OCRDMA_UVERBS(QUERY_QP) |
248 OCRDMA_UVERBS(DESTROY_QP) |
249 OCRDMA_UVERBS(POLL_CQ) |
250 OCRDMA_UVERBS(POST_SEND) |
251 OCRDMA_UVERBS(POST_RECV);
252
253 dev->ibdev.uverbs_cmd_mask |=
254 OCRDMA_UVERBS(CREATE_AH) |
255 OCRDMA_UVERBS(MODIFY_AH) |
256 OCRDMA_UVERBS(QUERY_AH) |
257 OCRDMA_UVERBS(DESTROY_AH);
258
259 dev->ibdev.node_type = RDMA_NODE_IB_CA;
260 dev->ibdev.phys_port_cnt = 1;
Devesh Sharma0c0eacd2014-12-18 14:12:58 +0530261 dev->ibdev.num_comp_vectors = dev->eq_cnt;
Parav Panditfe2caef2012-03-21 04:09:06 +0530262
263 /* mandatory verbs. */
264 dev->ibdev.query_device = ocrdma_query_device;
265 dev->ibdev.query_port = ocrdma_query_port;
266 dev->ibdev.modify_port = ocrdma_modify_port;
267 dev->ibdev.query_gid = ocrdma_query_gid;
268 dev->ibdev.get_link_layer = ocrdma_link_layer;
269 dev->ibdev.alloc_pd = ocrdma_alloc_pd;
270 dev->ibdev.dealloc_pd = ocrdma_dealloc_pd;
271
272 dev->ibdev.create_cq = ocrdma_create_cq;
273 dev->ibdev.destroy_cq = ocrdma_destroy_cq;
274 dev->ibdev.resize_cq = ocrdma_resize_cq;
275
276 dev->ibdev.create_qp = ocrdma_create_qp;
277 dev->ibdev.modify_qp = ocrdma_modify_qp;
278 dev->ibdev.query_qp = ocrdma_query_qp;
279 dev->ibdev.destroy_qp = ocrdma_destroy_qp;
280
281 dev->ibdev.query_pkey = ocrdma_query_pkey;
282 dev->ibdev.create_ah = ocrdma_create_ah;
283 dev->ibdev.destroy_ah = ocrdma_destroy_ah;
284 dev->ibdev.query_ah = ocrdma_query_ah;
285 dev->ibdev.modify_ah = ocrdma_modify_ah;
286
287 dev->ibdev.poll_cq = ocrdma_poll_cq;
288 dev->ibdev.post_send = ocrdma_post_send;
289 dev->ibdev.post_recv = ocrdma_post_recv;
290 dev->ibdev.req_notify_cq = ocrdma_arm_cq;
291
292 dev->ibdev.get_dma_mr = ocrdma_get_dma_mr;
Naresh Gottumukkalacffce992013-08-26 15:27:44 +0530293 dev->ibdev.reg_phys_mr = ocrdma_reg_kernel_mr;
Parav Panditfe2caef2012-03-21 04:09:06 +0530294 dev->ibdev.dereg_mr = ocrdma_dereg_mr;
295 dev->ibdev.reg_user_mr = ocrdma_reg_user_mr;
296
Naresh Gottumukkala7c338802013-08-26 15:27:39 +0530297 dev->ibdev.alloc_fast_reg_mr = ocrdma_alloc_frmr;
298 dev->ibdev.alloc_fast_reg_page_list = ocrdma_alloc_frmr_page_list;
299 dev->ibdev.free_fast_reg_page_list = ocrdma_free_frmr_page_list;
300
Parav Panditfe2caef2012-03-21 04:09:06 +0530301 /* mandatory to support user space verbs consumer. */
302 dev->ibdev.alloc_ucontext = ocrdma_alloc_ucontext;
303 dev->ibdev.dealloc_ucontext = ocrdma_dealloc_ucontext;
304 dev->ibdev.mmap = ocrdma_mmap;
305 dev->ibdev.dma_device = &dev->nic_info.pdev->dev;
306
307 dev->ibdev.process_mad = ocrdma_process_mad;
Ira Weiny77386132015-05-13 20:02:58 -0400308 dev->ibdev.get_port_immutable = ocrdma_port_immutable;
Parav Panditfe2caef2012-03-21 04:09:06 +0530309
Devesh Sharma21c33912014-02-04 11:56:56 +0530310 if (ocrdma_get_asic_type(dev) == OCRDMA_ASIC_GEN_SKH_R) {
Parav Panditfe2caef2012-03-21 04:09:06 +0530311 dev->ibdev.uverbs_cmd_mask |=
312 OCRDMA_UVERBS(CREATE_SRQ) |
313 OCRDMA_UVERBS(MODIFY_SRQ) |
314 OCRDMA_UVERBS(QUERY_SRQ) |
315 OCRDMA_UVERBS(DESTROY_SRQ) |
316 OCRDMA_UVERBS(POST_SRQ_RECV);
317
318 dev->ibdev.create_srq = ocrdma_create_srq;
319 dev->ibdev.modify_srq = ocrdma_modify_srq;
320 dev->ibdev.query_srq = ocrdma_query_srq;
321 dev->ibdev.destroy_srq = ocrdma_destroy_srq;
322 dev->ibdev.post_srq_recv = ocrdma_post_srq_recv;
323 }
324 return ib_register_device(&dev->ibdev, NULL);
325}
326
327static int ocrdma_alloc_resources(struct ocrdma_dev *dev)
328{
329 mutex_init(&dev->dev_lock);
330 dev->sgid_tbl = kzalloc(sizeof(union ib_gid) *
331 OCRDMA_MAX_SGID, GFP_KERNEL);
332 if (!dev->sgid_tbl)
333 goto alloc_err;
334 spin_lock_init(&dev->sgid_lock);
335
336 dev->cq_tbl = kzalloc(sizeof(struct ocrdma_cq *) *
337 OCRDMA_MAX_CQ, GFP_KERNEL);
338 if (!dev->cq_tbl)
339 goto alloc_err;
340
341 if (dev->attr.max_qp) {
342 dev->qp_tbl = kzalloc(sizeof(struct ocrdma_qp *) *
343 OCRDMA_MAX_QP, GFP_KERNEL);
344 if (!dev->qp_tbl)
345 goto alloc_err;
346 }
Selvin Xavier4f1df842014-06-10 19:32:24 +0530347
348 dev->stag_arr = kzalloc(sizeof(u64) * OCRDMA_MAX_STAG, GFP_KERNEL);
349 if (dev->stag_arr == NULL)
350 goto alloc_err;
351
Mitesh Ahuja9ba13772014-12-18 14:12:57 +0530352 ocrdma_alloc_pd_pool(dev);
353
Parav Panditfe2caef2012-03-21 04:09:06 +0530354 spin_lock_init(&dev->av_tbl.lock);
355 spin_lock_init(&dev->flush_q_lock);
356 return 0;
357alloc_err:
Naresh Gottumukkalaef99c4c2013-06-10 04:42:39 +0000358 pr_err("%s(%d) error.\n", __func__, dev->id);
Parav Panditfe2caef2012-03-21 04:09:06 +0530359 return -ENOMEM;
360}
361
362static void ocrdma_free_resources(struct ocrdma_dev *dev)
363{
Selvin Xavier4f1df842014-06-10 19:32:24 +0530364 kfree(dev->stag_arr);
Parav Panditfe2caef2012-03-21 04:09:06 +0530365 kfree(dev->qp_tbl);
366 kfree(dev->cq_tbl);
367 kfree(dev->sgid_tbl);
368}
369
Selvin Xavier334b8db2014-02-04 11:57:09 +0530370/* OCRDMA sysfs interface */
371static ssize_t show_rev(struct device *device, struct device_attribute *attr,
372 char *buf)
373{
374 struct ocrdma_dev *dev = dev_get_drvdata(device);
375
376 return scnprintf(buf, PAGE_SIZE, "0x%x\n", dev->nic_info.pdev->vendor);
377}
378
379static ssize_t show_fw_ver(struct device *device, struct device_attribute *attr,
380 char *buf)
381{
382 struct ocrdma_dev *dev = dev_get_drvdata(device);
383
Selvin Xavier4808b182014-06-10 19:32:14 +0530384 return scnprintf(buf, PAGE_SIZE, "%s\n", &dev->attr.fw_ver[0]);
385}
386
387static ssize_t show_hca_type(struct device *device,
388 struct device_attribute *attr, char *buf)
389{
390 struct ocrdma_dev *dev = dev_get_drvdata(device);
391
392 return scnprintf(buf, PAGE_SIZE, "%s\n", &dev->model_number[0]);
Selvin Xavier334b8db2014-02-04 11:57:09 +0530393}
394
395static DEVICE_ATTR(hw_rev, S_IRUGO, show_rev, NULL);
396static DEVICE_ATTR(fw_ver, S_IRUGO, show_fw_ver, NULL);
Selvin Xavier4808b182014-06-10 19:32:14 +0530397static DEVICE_ATTR(hca_type, S_IRUGO, show_hca_type, NULL);
Selvin Xavier334b8db2014-02-04 11:57:09 +0530398
399static struct device_attribute *ocrdma_attributes[] = {
400 &dev_attr_hw_rev,
Selvin Xavier4808b182014-06-10 19:32:14 +0530401 &dev_attr_fw_ver,
402 &dev_attr_hca_type
Selvin Xavier334b8db2014-02-04 11:57:09 +0530403};
404
405static void ocrdma_remove_sysfiles(struct ocrdma_dev *dev)
406{
407 int i;
408
409 for (i = 0; i < ARRAY_SIZE(ocrdma_attributes); i++)
410 device_remove_file(&dev->ibdev.dev, ocrdma_attributes[i]);
411}
412
Devesh Sharma7ec11e02014-08-22 16:57:19 +0530413static void ocrdma_add_default_sgid(struct ocrdma_dev *dev)
414{
415 /* GID Index 0 - Invariant manufacturer-assigned EUI-64 */
416 union ib_gid *sgid = &dev->sgid_tbl[0];
417
418 sgid->global.subnet_prefix = cpu_to_be64(0xfe80000000000000LL);
419 ocrdma_get_guid(dev, &sgid->raw[8]);
420}
421
Selvin Xavierb8806322014-06-10 19:32:25 +0530422static void ocrdma_init_ipv4_gids(struct ocrdma_dev *dev,
423 struct net_device *net)
424{
425 struct in_device *in_dev;
426 union ib_gid gid;
427 in_dev = in_dev_get(net);
428 if (in_dev) {
429 for_ifa(in_dev) {
430 ipv6_addr_set_v4mapped(ifa->ifa_address,
431 (struct in6_addr *)&gid);
432 ocrdma_add_sgid(dev, &gid);
433 }
434 endfor_ifa(in_dev);
435 in_dev_put(in_dev);
436 }
437}
438
439static void ocrdma_init_ipv6_gids(struct ocrdma_dev *dev,
440 struct net_device *net)
441{
442#if IS_ENABLED(CONFIG_IPV6)
443 struct inet6_dev *in6_dev;
444 union ib_gid *pgid;
445 struct inet6_ifaddr *ifp;
446 in6_dev = in6_dev_get(net);
447 if (in6_dev) {
448 read_lock_bh(&in6_dev->lock);
449 list_for_each_entry(ifp, &in6_dev->addr_list, if_list) {
450 pgid = (union ib_gid *)&ifp->addr;
451 ocrdma_add_sgid(dev, pgid);
452 }
453 read_unlock_bh(&in6_dev->lock);
454 in6_dev_put(in6_dev);
455 }
456#endif
457}
458
459static void ocrdma_init_gid_table(struct ocrdma_dev *dev)
460{
461 struct net_device *net_dev;
462
463 for_each_netdev(&init_net, net_dev) {
464 struct net_device *real_dev = rdma_vlan_dev_real_dev(net_dev) ?
465 rdma_vlan_dev_real_dev(net_dev) : net_dev;
466
467 if (real_dev == dev->nic_info.netdev) {
Devesh Sharma7ec11e02014-08-22 16:57:19 +0530468 ocrdma_add_default_sgid(dev);
Selvin Xavierb8806322014-06-10 19:32:25 +0530469 ocrdma_init_ipv4_gids(dev, net_dev);
470 ocrdma_init_ipv6_gids(dev, net_dev);
471 }
472 }
473}
474
Parav Panditfe2caef2012-03-21 04:09:06 +0530475static struct ocrdma_dev *ocrdma_add(struct be_dev_info *dev_info)
476{
Selvin Xavier334b8db2014-02-04 11:57:09 +0530477 int status = 0, i;
Parav Panditfe2caef2012-03-21 04:09:06 +0530478 struct ocrdma_dev *dev;
479
480 dev = (struct ocrdma_dev *)ib_alloc_device(sizeof(struct ocrdma_dev));
481 if (!dev) {
Naresh Gottumukkalaef99c4c2013-06-10 04:42:39 +0000482 pr_err("Unable to allocate ib device\n");
Parav Panditfe2caef2012-03-21 04:09:06 +0530483 return NULL;
484 }
485 dev->mbx_cmd = kzalloc(sizeof(struct ocrdma_mqe_emb_cmd), GFP_KERNEL);
486 if (!dev->mbx_cmd)
487 goto idr_err;
488
489 memcpy(&dev->nic_info, dev_info, sizeof(*dev_info));
Tejun Heocffcd592013-02-27 17:04:24 -0800490 dev->id = idr_alloc(&ocrdma_dev_id, NULL, 0, 0, GFP_KERNEL);
Parav Panditfe2caef2012-03-21 04:09:06 +0530491 if (dev->id < 0)
492 goto idr_err;
493
494 status = ocrdma_init_hw(dev);
495 if (status)
496 goto init_err;
497
498 status = ocrdma_alloc_resources(dev);
499 if (status)
500 goto alloc_err;
501
Selvin Xavier31dbdd92014-06-10 19:32:13 +0530502 ocrdma_init_service_level(dev);
Selvin Xavierb8806322014-06-10 19:32:25 +0530503 ocrdma_init_gid_table(dev);
Parav Panditfe2caef2012-03-21 04:09:06 +0530504 status = ocrdma_register_device(dev);
505 if (status)
506 goto alloc_err;
507
Selvin Xavier334b8db2014-02-04 11:57:09 +0530508 for (i = 0; i < ARRAY_SIZE(ocrdma_attributes); i++)
509 if (device_create_file(&dev->ibdev.dev, ocrdma_attributes[i]))
510 goto sysfs_err;
Sasha Levin3e4d60a2012-04-28 07:40:01 +0200511 spin_lock(&ocrdma_devlist_lock);
512 list_add_tail_rcu(&dev->entry, &ocrdma_dev_list);
513 spin_unlock(&ocrdma_devlist_lock);
Selvin Xaviera51f06e2014-02-04 11:57:07 +0530514 /* Init stats */
515 ocrdma_add_port_stats(dev);
Mitesh Ahujab4dbe8d2014-12-18 14:13:05 +0530516 /* Interrupt Moderation */
517 INIT_DELAYED_WORK(&dev->eqd_work, ocrdma_eqd_set_task);
518 schedule_delayed_work(&dev->eqd_work, msecs_to_jiffies(1000));
Selvin Xaviera51f06e2014-02-04 11:57:07 +0530519
520 pr_info("%s %s: %s \"%s\" port %d\n",
521 dev_name(&dev->nic_info.pdev->dev), hca_name(dev),
522 port_speed_string(dev), dev->model_number,
523 dev->hba_port_num);
524 pr_info("%s ocrdma%d driver loaded successfully\n",
525 dev_name(&dev->nic_info.pdev->dev), dev->id);
Parav Panditfe2caef2012-03-21 04:09:06 +0530526 return dev;
527
Selvin Xavier334b8db2014-02-04 11:57:09 +0530528sysfs_err:
529 ocrdma_remove_sysfiles(dev);
Parav Panditfe2caef2012-03-21 04:09:06 +0530530alloc_err:
531 ocrdma_free_resources(dev);
532 ocrdma_cleanup_hw(dev);
533init_err:
534 idr_remove(&ocrdma_dev_id, dev->id);
535idr_err:
536 kfree(dev->mbx_cmd);
537 ib_dealloc_device(&dev->ibdev);
Naresh Gottumukkalaef99c4c2013-06-10 04:42:39 +0000538 pr_err("%s() leaving. ret=%d\n", __func__, status);
Parav Panditfe2caef2012-03-21 04:09:06 +0530539 return NULL;
540}
541
Sasha Levin3e4d60a2012-04-28 07:40:01 +0200542static void ocrdma_remove_free(struct rcu_head *rcu)
Parav Panditfe2caef2012-03-21 04:09:06 +0530543{
Sasha Levin3e4d60a2012-04-28 07:40:01 +0200544 struct ocrdma_dev *dev = container_of(rcu, struct ocrdma_dev, rcu);
Parav Panditfe2caef2012-03-21 04:09:06 +0530545
Parav Panditfe2caef2012-03-21 04:09:06 +0530546 idr_remove(&ocrdma_dev_id, dev->id);
547 kfree(dev->mbx_cmd);
548 ib_dealloc_device(&dev->ibdev);
549}
550
Sasha Levin3e4d60a2012-04-28 07:40:01 +0200551static void ocrdma_remove(struct ocrdma_dev *dev)
552{
553 /* first unregister with stack to stop all the active traffic
554 * of the registered clients.
555 */
Mitesh Ahujab4dbe8d2014-12-18 14:13:05 +0530556 cancel_delayed_work_sync(&dev->eqd_work);
Selvin Xavier334b8db2014-02-04 11:57:09 +0530557 ocrdma_remove_sysfiles(dev);
Sasha Levin3e4d60a2012-04-28 07:40:01 +0200558 ib_unregister_device(&dev->ibdev);
559
Mitesh Ahuja4b8180a2014-12-18 14:13:01 +0530560 ocrdma_rem_port_stats(dev);
561
Sasha Levin3e4d60a2012-04-28 07:40:01 +0200562 spin_lock(&ocrdma_devlist_lock);
563 list_del_rcu(&dev->entry);
564 spin_unlock(&ocrdma_devlist_lock);
Naresh Gottumukkala1852d1d2013-09-06 15:02:47 +0530565
566 ocrdma_free_resources(dev);
567 ocrdma_cleanup_hw(dev);
568
Sasha Levin3e4d60a2012-04-28 07:40:01 +0200569 call_rcu(&dev->rcu, ocrdma_remove_free);
570}
571
Parav Panditfe2caef2012-03-21 04:09:06 +0530572static int ocrdma_open(struct ocrdma_dev *dev)
573{
574 struct ib_event port_event;
575
576 port_event.event = IB_EVENT_PORT_ACTIVE;
577 port_event.element.port_num = 1;
578 port_event.device = &dev->ibdev;
579 ib_dispatch_event(&port_event);
580 return 0;
581}
582
583static int ocrdma_close(struct ocrdma_dev *dev)
584{
585 int i;
586 struct ocrdma_qp *qp, **cur_qp;
587 struct ib_event err_event;
588 struct ib_qp_attr attrs;
589 int attr_mask = IB_QP_STATE;
590
591 attrs.qp_state = IB_QPS_ERR;
592 mutex_lock(&dev->dev_lock);
593 if (dev->qp_tbl) {
594 cur_qp = dev->qp_tbl;
595 for (i = 0; i < OCRDMA_MAX_QP; i++) {
596 qp = cur_qp[i];
Devesh Sharmafad51b72014-02-04 11:57:10 +0530597 if (qp && qp->ibqp.qp_type != IB_QPT_GSI) {
Parav Panditfe2caef2012-03-21 04:09:06 +0530598 /* change the QP state to ERROR */
599 _ocrdma_modify_qp(&qp->ibqp, &attrs, attr_mask);
600
601 err_event.event = IB_EVENT_QP_FATAL;
602 err_event.element.qp = &qp->ibqp;
603 err_event.device = &dev->ibdev;
604 ib_dispatch_event(&err_event);
605 }
606 }
607 }
608 mutex_unlock(&dev->dev_lock);
609
610 err_event.event = IB_EVENT_PORT_ERR;
611 err_event.element.port_num = 1;
612 err_event.device = &dev->ibdev;
613 ib_dispatch_event(&err_event);
614 return 0;
615}
616
Devesh Sharmaefe45932014-06-10 19:32:16 +0530617static void ocrdma_shutdown(struct ocrdma_dev *dev)
618{
619 ocrdma_close(dev);
620 ocrdma_remove(dev);
621}
622
Parav Panditfe2caef2012-03-21 04:09:06 +0530623/* event handling via NIC driver ensures that all the NIC specific
624 * initialization done before RoCE driver notifies
625 * event to stack.
626 */
627static void ocrdma_event_handler(struct ocrdma_dev *dev, u32 event)
628{
629 switch (event) {
630 case BE_DEV_UP:
631 ocrdma_open(dev);
632 break;
633 case BE_DEV_DOWN:
634 ocrdma_close(dev);
635 break;
Devesh Sharmaefe45932014-06-10 19:32:16 +0530636 case BE_DEV_SHUTDOWN:
637 ocrdma_shutdown(dev);
638 break;
Joe Perches2b50176d2013-10-08 16:07:22 -0700639 }
Parav Panditfe2caef2012-03-21 04:09:06 +0530640}
641
Roland Dreierabe3afa2012-04-16 11:36:29 -0700642static struct ocrdma_driver ocrdma_drv = {
Parav Panditfe2caef2012-03-21 04:09:06 +0530643 .name = "ocrdma_driver",
644 .add = ocrdma_add,
645 .remove = ocrdma_remove,
646 .state_change_handler = ocrdma_event_handler,
Devesh Sharmab6b87d22014-02-04 11:56:58 +0530647 .be_abi_version = OCRDMA_BE_ROCE_ABI_VERSION,
Parav Panditfe2caef2012-03-21 04:09:06 +0530648};
649
Roland Dreier34955662012-05-02 15:42:33 -0700650static void ocrdma_unregister_inet6addr_notifier(void)
651{
Roland Dreierd90f9b32012-07-05 22:39:34 -0700652#if IS_ENABLED(CONFIG_IPV6)
Roland Dreier34955662012-05-02 15:42:33 -0700653 unregister_inet6addr_notifier(&ocrdma_inet6addr_notifier);
654#endif
655}
656
Selvin Xavier2d8f57d2014-03-18 14:54:56 +0530657static void ocrdma_unregister_inetaddr_notifier(void)
658{
659 unregister_inetaddr_notifier(&ocrdma_inetaddr_notifier);
660}
661
Parav Panditfe2caef2012-03-21 04:09:06 +0530662static int __init ocrdma_init_module(void)
663{
664 int status;
665
Selvin Xaviera51f06e2014-02-04 11:57:07 +0530666 ocrdma_init_debugfs();
667
Moni Shoua37721d82013-12-12 18:03:16 +0200668 status = register_inetaddr_notifier(&ocrdma_inetaddr_notifier);
669 if (status)
670 return status;
671
Roland Dreierd90f9b32012-07-05 22:39:34 -0700672#if IS_ENABLED(CONFIG_IPV6)
Parav Panditfe2caef2012-03-21 04:09:06 +0530673 status = register_inet6addr_notifier(&ocrdma_inet6addr_notifier);
674 if (status)
Selvin Xavier2d8f57d2014-03-18 14:54:56 +0530675 goto err_notifier6;
Roland Dreier34955662012-05-02 15:42:33 -0700676#endif
677
Parav Panditfe2caef2012-03-21 04:09:06 +0530678 status = be_roce_register_driver(&ocrdma_drv);
679 if (status)
Selvin Xavier2d8f57d2014-03-18 14:54:56 +0530680 goto err_be_reg;
Roland Dreier34955662012-05-02 15:42:33 -0700681
Selvin Xavier2d8f57d2014-03-18 14:54:56 +0530682 return 0;
683
684err_be_reg:
Li RongQinge5dc9402014-09-29 14:59:43 +0800685#if IS_ENABLED(CONFIG_IPV6)
Selvin Xavier2d8f57d2014-03-18 14:54:56 +0530686 ocrdma_unregister_inet6addr_notifier();
687err_notifier6:
Li RongQinge5dc9402014-09-29 14:59:43 +0800688#endif
Selvin Xavier2d8f57d2014-03-18 14:54:56 +0530689 ocrdma_unregister_inetaddr_notifier();
Parav Panditfe2caef2012-03-21 04:09:06 +0530690 return status;
691}
692
693static void __exit ocrdma_exit_module(void)
694{
695 be_roce_unregister_driver(&ocrdma_drv);
Roland Dreier34955662012-05-02 15:42:33 -0700696 ocrdma_unregister_inet6addr_notifier();
Selvin Xavier2d8f57d2014-03-18 14:54:56 +0530697 ocrdma_unregister_inetaddr_notifier();
Selvin Xaviera51f06e2014-02-04 11:57:07 +0530698 ocrdma_rem_debugfs();
Parav Panditfe2caef2012-03-21 04:09:06 +0530699}
700
701module_init(ocrdma_init_module);
702module_exit(ocrdma_exit_module);