blob: 6ded95a667255a1f870654ffec729242be6f3883 [file] [log] [blame]
Devesh Sharma71ee6732015-07-24 05:03:59 +05301/* 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.
Parav Panditfe2caef2012-03-21 04:09:06 +053034 *
35 * Contact Information:
36 * linux-drivers@emulex.com
37 *
38 * Emulex
39 * 3333 Susan Street
40 * Costa Mesa, CA 92626
Devesh Sharma71ee6732015-07-24 05:03:59 +053041 */
Parav Panditfe2caef2012-03-21 04:09:06 +053042
43#include <linux/module.h>
Parav Panditfe2caef2012-03-21 04:09:06 +053044#include <linux/idr.h>
45#include <rdma/ib_verbs.h>
46#include <rdma/ib_user_verbs.h>
47#include <rdma/ib_addr.h>
Ira Weiny337877a2015-06-06 14:38:29 -040048#include <rdma/ib_mad.h>
Parav Panditfe2caef2012-03-21 04:09:06 +053049
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"
Selvin Xaviera51f06e2014-02-04 11:57:07 +053058#include "ocrdma_stats.h"
Naresh Gottumukkala38754392013-08-26 15:27:49 +053059#include "ocrdma_abi.h"
Parav Panditfe2caef2012-03-21 04:09:06 +053060
Devesh Sharma01544102014-02-04 11:57:00 +053061MODULE_VERSION(OCRDMA_ROCE_DRV_VERSION);
62MODULE_DESCRIPTION(OCRDMA_ROCE_DRV_DESC " " OCRDMA_ROCE_DRV_VERSION);
Parav Panditfe2caef2012-03-21 04:09:06 +053063MODULE_AUTHOR("Emulex Corporation");
64MODULE_LICENSE("GPL");
65
66static LIST_HEAD(ocrdma_dev_list);
Sasha Levin3e4d60a2012-04-28 07:40:01 +020067static DEFINE_SPINLOCK(ocrdma_devlist_lock);
Parav Panditfe2caef2012-03-21 04:09:06 +053068static DEFINE_IDR(ocrdma_dev_id);
69
70static union ib_gid ocrdma_zero_sgid;
Parav Panditfe2caef2012-03-21 04:09:06 +053071
Parav Panditfe2caef2012-03-21 04:09:06 +053072void 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
Moni Shoua37721d82013-12-12 18:03:16 +020087static bool ocrdma_add_sgid(struct ocrdma_dev *dev, union ib_gid *new_sgid)
Parav Panditfe2caef2012-03-21 04:09:06 +053088{
89 int i;
Parav Panditfe2caef2012-03-21 04:09:06 +053090 unsigned long flags;
91
92 memset(&ocrdma_zero_sgid, 0, sizeof(union ib_gid));
93
Parav Panditfe2caef2012-03-21 04:09:06 +053094
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 */
Moni Shoua37721d82013-12-12 18:03:16 +0200100 memcpy(&dev->sgid_tbl[i], new_sgid,
Parav Pandit6ab68272012-06-08 21:25:25 +0530101 sizeof(union ib_gid));
102 spin_unlock_irqrestore(&dev->sgid_lock, flags);
103 return true;
Moni Shoua37721d82013-12-12 18:03:16 +0200104 } else if (!memcmp(&dev->sgid_tbl[i], new_sgid,
Parav Panditfe2caef2012-03-21 04:09:06 +0530105 sizeof(union ib_gid))) {
106 /* entry already present, no addition is required. */
107 spin_unlock_irqrestore(&dev->sgid_lock, flags);
Parav Pandit6ab68272012-06-08 21:25:25 +0530108 return false;
Parav Panditfe2caef2012-03-21 04:09:06 +0530109 }
110 }
Parav Panditfe2caef2012-03-21 04:09:06 +0530111 spin_unlock_irqrestore(&dev->sgid_lock, flags);
Parav Pandit6ab68272012-06-08 21:25:25 +0530112 return false;
Parav Panditfe2caef2012-03-21 04:09:06 +0530113}
114
Moni Shoua37721d82013-12-12 18:03:16 +0200115static bool ocrdma_del_sgid(struct ocrdma_dev *dev, union ib_gid *sgid)
Parav Panditfe2caef2012-03-21 04:09:06 +0530116{
117 int found = false;
118 int i;
Parav Panditfe2caef2012-03-21 04:09:06 +0530119 unsigned long flags;
120
Parav Panditfe2caef2012-03-21 04:09:06 +0530121
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++) {
Moni Shoua37721d82013-12-12 18:03:16 +0200125 if (!memcmp(&dev->sgid_tbl[i], sgid, sizeof(union ib_gid))) {
Parav Panditfe2caef2012-03-21 04:09:06 +0530126 /* 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
Moni Shoua37721d82013-12-12 18:03:16 +0200136static int ocrdma_addr_event(unsigned long event, struct net_device *netdev,
137 union ib_gid *gid)
Parav Panditfe2caef2012-03-21 04:09:06 +0530138{
Parav Panditfe2caef2012-03-21 04:09:06 +0530139 struct ib_event gid_event;
140 struct ocrdma_dev *dev;
141 bool found = false;
Parav Pandit6ab68272012-06-08 21:25:25 +0530142 bool updated = false;
Parav Panditfe2caef2012-03-21 04:09:06 +0530143 bool is_vlan = false;
Parav Panditfe2caef2012-03-21 04:09:06 +0530144
Roland Dreierd549f552012-08-10 16:52:13 -0700145 is_vlan = netdev->priv_flags & IFF_802_1Q_VLAN;
Moni Shoua37721d82013-12-12 18:03:16 +0200146 if (is_vlan)
Devesh Sharma09de3f12014-02-04 12:10:48 +0530147 netdev = rdma_vlan_dev_real_dev(netdev);
Roland Dreierd549f552012-08-10 16:52:13 -0700148
Sasha Levin3e4d60a2012-04-28 07:40:01 +0200149 rcu_read_lock();
150 list_for_each_entry_rcu(dev, &ocrdma_dev_list, entry) {
Parav Panditfe2caef2012-03-21 04:09:06 +0530151 if (dev->nic_info.netdev == netdev) {
152 found = true;
153 break;
154 }
155 }
Sasha Levin3e4d60a2012-04-28 07:40:01 +0200156 rcu_read_unlock();
Parav Panditfe2caef2012-03-21 04:09:06 +0530157
158 if (!found)
159 return NOTIFY_DONE;
Parav Panditfe2caef2012-03-21 04:09:06 +0530160
161 mutex_lock(&dev->dev_lock);
162 switch (event) {
163 case NETDEV_UP:
Moni Shoua37721d82013-12-12 18:03:16 +0200164 updated = ocrdma_add_sgid(dev, gid);
Parav Panditfe2caef2012-03-21 04:09:06 +0530165 break;
166 case NETDEV_DOWN:
Moni Shoua37721d82013-12-12 18:03:16 +0200167 updated = ocrdma_del_sgid(dev, gid);
Parav Panditfe2caef2012-03-21 04:09:06 +0530168 break;
169 default:
170 break;
171 }
Parav Pandit6ab68272012-06-08 21:25:25 +0530172 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 }
Parav Panditfe2caef2012-03-21 04:09:06 +0530179 mutex_unlock(&dev->dev_lock);
180 return NOTIFY_OK;
181}
182
Moni Shoua37721d82013-12-12 18:03:16 +0200183static 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
Roland Dreier31ab8ac2014-01-19 15:13:44 -0800194static struct notifier_block ocrdma_inetaddr_notifier = {
195 .notifier_call = ocrdma_inetaddr_event
196};
197
Moni Shoua37721d82013-12-12 18:03:16 +0200198#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
Roland Dreier34955662012-05-02 15:42:33 -0700209static struct notifier_block ocrdma_inet6addr_notifier = {
210 .notifier_call = ocrdma_inet6addr_event
211};
212
Parav Pandit6ab68272012-06-08 21:25:25 +0530213#endif /* IPV6 and VLAN */
Roland Dreier34955662012-05-02 15:42:33 -0700214
Parav Panditfe2caef2012-03-21 04:09:06 +0530215static enum rdma_link_layer ocrdma_link_layer(struct ib_device *device,
216 u8 port_num)
217{
218 return IB_LINK_LAYER_ETHERNET;
219}
220
Ira Weiny77386132015-05-13 20:02:58 -0400221static 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;
Ira Weinyf9b22e32015-05-13 20:02:59 -0400233 immutable->core_cap_flags = RDMA_CORE_PORT_IBA_ROCE;
Ira Weiny337877a2015-06-06 14:38:29 -0400234 immutable->max_mad_size = IB_MGMT_MAD_SIZE;
Ira Weiny77386132015-05-13 20:02:58 -0400235
236 return 0;
237}
238
Roland Dreierabe3afa2012-04-16 11:36:29 -0700239static int ocrdma_register_device(struct ocrdma_dev *dev)
Parav Panditfe2caef2012-03-21 04:09:06 +0530240{
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;
Naresh Gottumukkala38754392013-08-26 15:27:49 +0530246 dev->ibdev.uverbs_abi_ver = OCRDMA_ABI_VERSION;
Parav Panditfe2caef2012-03-21 04:09:06 +0530247 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;
Devesh Sharma0c0eacd2014-12-18 14:12:58 +0530276 dev->ibdev.num_comp_vectors = dev->eq_cnt;
Parav Panditfe2caef2012-03-21 04:09:06 +0530277
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;
Naresh Gottumukkalacffce992013-08-26 15:27:44 +0530308 dev->ibdev.reg_phys_mr = ocrdma_reg_kernel_mr;
Parav Panditfe2caef2012-03-21 04:09:06 +0530309 dev->ibdev.dereg_mr = ocrdma_dereg_mr;
310 dev->ibdev.reg_user_mr = ocrdma_reg_user_mr;
311
Naresh Gottumukkala7c338802013-08-26 15:27:39 +0530312 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
Parav Panditfe2caef2012-03-21 04:09:06 +0530316 /* 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;
Ira Weiny77386132015-05-13 20:02:58 -0400323 dev->ibdev.get_port_immutable = ocrdma_port_immutable;
Parav Panditfe2caef2012-03-21 04:09:06 +0530324
Devesh Sharma21c33912014-02-04 11:56:56 +0530325 if (ocrdma_get_asic_type(dev) == OCRDMA_ASIC_GEN_SKH_R) {
Parav Panditfe2caef2012-03-21 04:09:06 +0530326 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 }
Selvin Xavier4f1df842014-06-10 19:32:24 +0530362
363 dev->stag_arr = kzalloc(sizeof(u64) * OCRDMA_MAX_STAG, GFP_KERNEL);
364 if (dev->stag_arr == NULL)
365 goto alloc_err;
366
Mitesh Ahuja9ba13772014-12-18 14:12:57 +0530367 ocrdma_alloc_pd_pool(dev);
368
Parav Panditfe2caef2012-03-21 04:09:06 +0530369 spin_lock_init(&dev->av_tbl.lock);
370 spin_lock_init(&dev->flush_q_lock);
371 return 0;
372alloc_err:
Naresh Gottumukkalaef99c4c2013-06-10 04:42:39 +0000373 pr_err("%s(%d) error.\n", __func__, dev->id);
Parav Panditfe2caef2012-03-21 04:09:06 +0530374 return -ENOMEM;
375}
376
377static void ocrdma_free_resources(struct ocrdma_dev *dev)
378{
Selvin Xavier4f1df842014-06-10 19:32:24 +0530379 kfree(dev->stag_arr);
Parav Panditfe2caef2012-03-21 04:09:06 +0530380 kfree(dev->qp_tbl);
381 kfree(dev->cq_tbl);
382 kfree(dev->sgid_tbl);
383}
384
Selvin Xavier334b8db2014-02-04 11:57:09 +0530385/* 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
Selvin Xavier4808b182014-06-10 19:32:14 +0530399 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]);
Selvin Xavier334b8db2014-02-04 11:57:09 +0530408}
409
410static DEVICE_ATTR(hw_rev, S_IRUGO, show_rev, NULL);
411static DEVICE_ATTR(fw_ver, S_IRUGO, show_fw_ver, NULL);
Selvin Xavier4808b182014-06-10 19:32:14 +0530412static DEVICE_ATTR(hca_type, S_IRUGO, show_hca_type, NULL);
Selvin Xavier334b8db2014-02-04 11:57:09 +0530413
414static struct device_attribute *ocrdma_attributes[] = {
415 &dev_attr_hw_rev,
Selvin Xavier4808b182014-06-10 19:32:14 +0530416 &dev_attr_fw_ver,
417 &dev_attr_hca_type
Selvin Xavier334b8db2014-02-04 11:57:09 +0530418};
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
Devesh Sharma7ec11e02014-08-22 16:57:19 +0530428static 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
Selvin Xavierb8806322014-06-10 19:32:25 +0530437static 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) {
Devesh Sharma7ec11e02014-08-22 16:57:19 +0530483 ocrdma_add_default_sgid(dev);
Selvin Xavierb8806322014-06-10 19:32:25 +0530484 ocrdma_init_ipv4_gids(dev, net_dev);
485 ocrdma_init_ipv6_gids(dev, net_dev);
486 }
487 }
488}
489
Parav Panditfe2caef2012-03-21 04:09:06 +0530490static struct ocrdma_dev *ocrdma_add(struct be_dev_info *dev_info)
491{
Selvin Xavier334b8db2014-02-04 11:57:09 +0530492 int status = 0, i;
Parav Panditfe2caef2012-03-21 04:09:06 +0530493 struct ocrdma_dev *dev;
494
495 dev = (struct ocrdma_dev *)ib_alloc_device(sizeof(struct ocrdma_dev));
496 if (!dev) {
Naresh Gottumukkalaef99c4c2013-06-10 04:42:39 +0000497 pr_err("Unable to allocate ib device\n");
Parav Panditfe2caef2012-03-21 04:09:06 +0530498 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));
Tejun Heocffcd592013-02-27 17:04:24 -0800505 dev->id = idr_alloc(&ocrdma_dev_id, NULL, 0, 0, GFP_KERNEL);
Parav Panditfe2caef2012-03-21 04:09:06 +0530506 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
Selvin Xavier31dbdd92014-06-10 19:32:13 +0530517 ocrdma_init_service_level(dev);
Selvin Xavierb8806322014-06-10 19:32:25 +0530518 ocrdma_init_gid_table(dev);
Parav Panditfe2caef2012-03-21 04:09:06 +0530519 status = ocrdma_register_device(dev);
520 if (status)
521 goto alloc_err;
522
Selvin Xavier334b8db2014-02-04 11:57:09 +0530523 for (i = 0; i < ARRAY_SIZE(ocrdma_attributes); i++)
524 if (device_create_file(&dev->ibdev.dev, ocrdma_attributes[i]))
525 goto sysfs_err;
Sasha Levin3e4d60a2012-04-28 07:40:01 +0200526 spin_lock(&ocrdma_devlist_lock);
527 list_add_tail_rcu(&dev->entry, &ocrdma_dev_list);
528 spin_unlock(&ocrdma_devlist_lock);
Selvin Xaviera51f06e2014-02-04 11:57:07 +0530529 /* Init stats */
530 ocrdma_add_port_stats(dev);
Mitesh Ahujab4dbe8d2014-12-18 14:13:05 +0530531 /* Interrupt Moderation */
532 INIT_DELAYED_WORK(&dev->eqd_work, ocrdma_eqd_set_task);
533 schedule_delayed_work(&dev->eqd_work, msecs_to_jiffies(1000));
Selvin Xaviera51f06e2014-02-04 11:57:07 +0530534
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);
Parav Panditfe2caef2012-03-21 04:09:06 +0530541 return dev;
542
Selvin Xavier334b8db2014-02-04 11:57:09 +0530543sysfs_err:
544 ocrdma_remove_sysfiles(dev);
Parav Panditfe2caef2012-03-21 04:09:06 +0530545alloc_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);
Naresh Gottumukkalaef99c4c2013-06-10 04:42:39 +0000553 pr_err("%s() leaving. ret=%d\n", __func__, status);
Parav Panditfe2caef2012-03-21 04:09:06 +0530554 return NULL;
555}
556
Sasha Levin3e4d60a2012-04-28 07:40:01 +0200557static void ocrdma_remove_free(struct rcu_head *rcu)
Parav Panditfe2caef2012-03-21 04:09:06 +0530558{
Sasha Levin3e4d60a2012-04-28 07:40:01 +0200559 struct ocrdma_dev *dev = container_of(rcu, struct ocrdma_dev, rcu);
Parav Panditfe2caef2012-03-21 04:09:06 +0530560
Parav Panditfe2caef2012-03-21 04:09:06 +0530561 idr_remove(&ocrdma_dev_id, dev->id);
562 kfree(dev->mbx_cmd);
563 ib_dealloc_device(&dev->ibdev);
564}
565
Sasha Levin3e4d60a2012-04-28 07:40:01 +0200566static 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 */
Mitesh Ahujab4dbe8d2014-12-18 14:13:05 +0530571 cancel_delayed_work_sync(&dev->eqd_work);
Selvin Xavier334b8db2014-02-04 11:57:09 +0530572 ocrdma_remove_sysfiles(dev);
Sasha Levin3e4d60a2012-04-28 07:40:01 +0200573 ib_unregister_device(&dev->ibdev);
574
Mitesh Ahuja4b8180a2014-12-18 14:13:01 +0530575 ocrdma_rem_port_stats(dev);
576
Sasha Levin3e4d60a2012-04-28 07:40:01 +0200577 spin_lock(&ocrdma_devlist_lock);
578 list_del_rcu(&dev->entry);
579 spin_unlock(&ocrdma_devlist_lock);
Naresh Gottumukkala1852d1d2013-09-06 15:02:47 +0530580
581 ocrdma_free_resources(dev);
582 ocrdma_cleanup_hw(dev);
583
Sasha Levin3e4d60a2012-04-28 07:40:01 +0200584 call_rcu(&dev->rcu, ocrdma_remove_free);
585}
586
Parav Panditfe2caef2012-03-21 04:09:06 +0530587static 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];
Devesh Sharmafad51b72014-02-04 11:57:10 +0530612 if (qp && qp->ibqp.qp_type != IB_QPT_GSI) {
Parav Panditfe2caef2012-03-21 04:09:06 +0530613 /* 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
Devesh Sharmaefe45932014-06-10 19:32:16 +0530632static void ocrdma_shutdown(struct ocrdma_dev *dev)
633{
634 ocrdma_close(dev);
635 ocrdma_remove(dev);
636}
637
Parav Panditfe2caef2012-03-21 04:09:06 +0530638/* 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;
Devesh Sharmaefe45932014-06-10 19:32:16 +0530651 case BE_DEV_SHUTDOWN:
652 ocrdma_shutdown(dev);
653 break;
Joe Perches2b50176d2013-10-08 16:07:22 -0700654 }
Parav Panditfe2caef2012-03-21 04:09:06 +0530655}
656
Roland Dreierabe3afa2012-04-16 11:36:29 -0700657static struct ocrdma_driver ocrdma_drv = {
Parav Panditfe2caef2012-03-21 04:09:06 +0530658 .name = "ocrdma_driver",
659 .add = ocrdma_add,
660 .remove = ocrdma_remove,
661 .state_change_handler = ocrdma_event_handler,
Devesh Sharmab6b87d22014-02-04 11:56:58 +0530662 .be_abi_version = OCRDMA_BE_ROCE_ABI_VERSION,
Parav Panditfe2caef2012-03-21 04:09:06 +0530663};
664
Roland Dreier34955662012-05-02 15:42:33 -0700665static void ocrdma_unregister_inet6addr_notifier(void)
666{
Roland Dreierd90f9b32012-07-05 22:39:34 -0700667#if IS_ENABLED(CONFIG_IPV6)
Roland Dreier34955662012-05-02 15:42:33 -0700668 unregister_inet6addr_notifier(&ocrdma_inet6addr_notifier);
669#endif
670}
671
Selvin Xavier2d8f57d2014-03-18 14:54:56 +0530672static void ocrdma_unregister_inetaddr_notifier(void)
673{
674 unregister_inetaddr_notifier(&ocrdma_inetaddr_notifier);
675}
676
Parav Panditfe2caef2012-03-21 04:09:06 +0530677static int __init ocrdma_init_module(void)
678{
679 int status;
680
Selvin Xaviera51f06e2014-02-04 11:57:07 +0530681 ocrdma_init_debugfs();
682
Moni Shoua37721d82013-12-12 18:03:16 +0200683 status = register_inetaddr_notifier(&ocrdma_inetaddr_notifier);
684 if (status)
685 return status;
686
Roland Dreierd90f9b32012-07-05 22:39:34 -0700687#if IS_ENABLED(CONFIG_IPV6)
Parav Panditfe2caef2012-03-21 04:09:06 +0530688 status = register_inet6addr_notifier(&ocrdma_inet6addr_notifier);
689 if (status)
Selvin Xavier2d8f57d2014-03-18 14:54:56 +0530690 goto err_notifier6;
Roland Dreier34955662012-05-02 15:42:33 -0700691#endif
692
Parav Panditfe2caef2012-03-21 04:09:06 +0530693 status = be_roce_register_driver(&ocrdma_drv);
694 if (status)
Selvin Xavier2d8f57d2014-03-18 14:54:56 +0530695 goto err_be_reg;
Roland Dreier34955662012-05-02 15:42:33 -0700696
Selvin Xavier2d8f57d2014-03-18 14:54:56 +0530697 return 0;
698
699err_be_reg:
Li RongQinge5dc9402014-09-29 14:59:43 +0800700#if IS_ENABLED(CONFIG_IPV6)
Selvin Xavier2d8f57d2014-03-18 14:54:56 +0530701 ocrdma_unregister_inet6addr_notifier();
702err_notifier6:
Li RongQinge5dc9402014-09-29 14:59:43 +0800703#endif
Selvin Xavier2d8f57d2014-03-18 14:54:56 +0530704 ocrdma_unregister_inetaddr_notifier();
Parav Panditfe2caef2012-03-21 04:09:06 +0530705 return status;
706}
707
708static void __exit ocrdma_exit_module(void)
709{
710 be_roce_unregister_driver(&ocrdma_drv);
Roland Dreier34955662012-05-02 15:42:33 -0700711 ocrdma_unregister_inet6addr_notifier();
Selvin Xavier2d8f57d2014-03-18 14:54:56 +0530712 ocrdma_unregister_inetaddr_notifier();
Selvin Xaviera51f06e2014-02-04 11:57:07 +0530713 ocrdma_rem_debugfs();
Johannes Thumshirnd8b2ba72015-07-08 17:23:00 +0200714 idr_destroy(&ocrdma_dev_id);
Parav Panditfe2caef2012-03-21 04:09:06 +0530715}
716
717module_init(ocrdma_init_module);
718module_exit(ocrdma_exit_module);