blob: cb7b52a791db691eb2584fb96750d131d9240a57 [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>
33
34#include <linux/netdevice.h>
35#include <net/addrconf.h>
36
37#include "ocrdma.h"
38#include "ocrdma_verbs.h"
39#include "ocrdma_ah.h"
40#include "be_roce.h"
41#include "ocrdma_hw.h"
Selvin Xaviera51f06e2014-02-04 11:57:07 +053042#include "ocrdma_stats.h"
Naresh Gottumukkala38754392013-08-26 15:27:49 +053043#include "ocrdma_abi.h"
Parav Panditfe2caef2012-03-21 04:09:06 +053044
Devesh Sharma01544102014-02-04 11:57:00 +053045MODULE_VERSION(OCRDMA_ROCE_DRV_VERSION);
46MODULE_DESCRIPTION(OCRDMA_ROCE_DRV_DESC " " OCRDMA_ROCE_DRV_VERSION);
Parav Panditfe2caef2012-03-21 04:09:06 +053047MODULE_AUTHOR("Emulex Corporation");
48MODULE_LICENSE("GPL");
49
50static LIST_HEAD(ocrdma_dev_list);
Sasha Levin3e4d60a2012-04-28 07:40:01 +020051static DEFINE_SPINLOCK(ocrdma_devlist_lock);
Parav Panditfe2caef2012-03-21 04:09:06 +053052static DEFINE_IDR(ocrdma_dev_id);
53
54static union ib_gid ocrdma_zero_sgid;
Parav Panditfe2caef2012-03-21 04:09:06 +053055
Parav Panditfe2caef2012-03-21 04:09:06 +053056void ocrdma_get_guid(struct ocrdma_dev *dev, u8 *guid)
57{
58 u8 mac_addr[6];
59
60 memcpy(&mac_addr[0], &dev->nic_info.mac_addr[0], ETH_ALEN);
61 guid[0] = mac_addr[0] ^ 2;
62 guid[1] = mac_addr[1];
63 guid[2] = mac_addr[2];
64 guid[3] = 0xff;
65 guid[4] = 0xfe;
66 guid[5] = mac_addr[3];
67 guid[6] = mac_addr[4];
68 guid[7] = mac_addr[5];
69}
70
Moni Shoua37721d82013-12-12 18:03:16 +020071static bool ocrdma_add_sgid(struct ocrdma_dev *dev, union ib_gid *new_sgid)
Parav Panditfe2caef2012-03-21 04:09:06 +053072{
73 int i;
Parav Panditfe2caef2012-03-21 04:09:06 +053074 unsigned long flags;
75
76 memset(&ocrdma_zero_sgid, 0, sizeof(union ib_gid));
77
Parav Panditfe2caef2012-03-21 04:09:06 +053078
79 spin_lock_irqsave(&dev->sgid_lock, flags);
80 for (i = 0; i < OCRDMA_MAX_SGID; i++) {
81 if (!memcmp(&dev->sgid_tbl[i], &ocrdma_zero_sgid,
82 sizeof(union ib_gid))) {
83 /* found free entry */
Moni Shoua37721d82013-12-12 18:03:16 +020084 memcpy(&dev->sgid_tbl[i], new_sgid,
Parav Pandit6ab68272012-06-08 21:25:25 +053085 sizeof(union ib_gid));
86 spin_unlock_irqrestore(&dev->sgid_lock, flags);
87 return true;
Moni Shoua37721d82013-12-12 18:03:16 +020088 } else if (!memcmp(&dev->sgid_tbl[i], new_sgid,
Parav Panditfe2caef2012-03-21 04:09:06 +053089 sizeof(union ib_gid))) {
90 /* entry already present, no addition is required. */
91 spin_unlock_irqrestore(&dev->sgid_lock, flags);
Parav Pandit6ab68272012-06-08 21:25:25 +053092 return false;
Parav Panditfe2caef2012-03-21 04:09:06 +053093 }
94 }
Parav Panditfe2caef2012-03-21 04:09:06 +053095 spin_unlock_irqrestore(&dev->sgid_lock, flags);
Parav Pandit6ab68272012-06-08 21:25:25 +053096 return false;
Parav Panditfe2caef2012-03-21 04:09:06 +053097}
98
Moni Shoua37721d82013-12-12 18:03:16 +020099static bool ocrdma_del_sgid(struct ocrdma_dev *dev, union ib_gid *sgid)
Parav Panditfe2caef2012-03-21 04:09:06 +0530100{
101 int found = false;
102 int i;
Parav Panditfe2caef2012-03-21 04:09:06 +0530103 unsigned long flags;
104
Parav Panditfe2caef2012-03-21 04:09:06 +0530105
106 spin_lock_irqsave(&dev->sgid_lock, flags);
107 /* first is default sgid, which cannot be deleted. */
108 for (i = 1; i < OCRDMA_MAX_SGID; i++) {
Moni Shoua37721d82013-12-12 18:03:16 +0200109 if (!memcmp(&dev->sgid_tbl[i], sgid, sizeof(union ib_gid))) {
Parav Panditfe2caef2012-03-21 04:09:06 +0530110 /* found matching entry */
111 memset(&dev->sgid_tbl[i], 0, sizeof(union ib_gid));
112 found = true;
113 break;
114 }
115 }
116 spin_unlock_irqrestore(&dev->sgid_lock, flags);
117 return found;
118}
119
Moni Shoua37721d82013-12-12 18:03:16 +0200120static int ocrdma_addr_event(unsigned long event, struct net_device *netdev,
121 union ib_gid *gid)
Parav Panditfe2caef2012-03-21 04:09:06 +0530122{
Parav Panditfe2caef2012-03-21 04:09:06 +0530123 struct ib_event gid_event;
124 struct ocrdma_dev *dev;
125 bool found = false;
Parav Pandit6ab68272012-06-08 21:25:25 +0530126 bool updated = false;
Parav Panditfe2caef2012-03-21 04:09:06 +0530127 bool is_vlan = false;
Parav Panditfe2caef2012-03-21 04:09:06 +0530128
Roland Dreierd549f552012-08-10 16:52:13 -0700129 is_vlan = netdev->priv_flags & IFF_802_1Q_VLAN;
Moni Shoua37721d82013-12-12 18:03:16 +0200130 if (is_vlan)
Devesh Sharma09de3f12014-02-04 12:10:48 +0530131 netdev = rdma_vlan_dev_real_dev(netdev);
Roland Dreierd549f552012-08-10 16:52:13 -0700132
Sasha Levin3e4d60a2012-04-28 07:40:01 +0200133 rcu_read_lock();
134 list_for_each_entry_rcu(dev, &ocrdma_dev_list, entry) {
Parav Panditfe2caef2012-03-21 04:09:06 +0530135 if (dev->nic_info.netdev == netdev) {
136 found = true;
137 break;
138 }
139 }
Sasha Levin3e4d60a2012-04-28 07:40:01 +0200140 rcu_read_unlock();
Parav Panditfe2caef2012-03-21 04:09:06 +0530141
142 if (!found)
143 return NOTIFY_DONE;
Parav Panditfe2caef2012-03-21 04:09:06 +0530144
145 mutex_lock(&dev->dev_lock);
146 switch (event) {
147 case NETDEV_UP:
Moni Shoua37721d82013-12-12 18:03:16 +0200148 updated = ocrdma_add_sgid(dev, gid);
Parav Panditfe2caef2012-03-21 04:09:06 +0530149 break;
150 case NETDEV_DOWN:
Moni Shoua37721d82013-12-12 18:03:16 +0200151 updated = ocrdma_del_sgid(dev, gid);
Parav Panditfe2caef2012-03-21 04:09:06 +0530152 break;
153 default:
154 break;
155 }
Parav Pandit6ab68272012-06-08 21:25:25 +0530156 if (updated) {
157 /* GID table updated, notify the consumers about it */
158 gid_event.device = &dev->ibdev;
159 gid_event.element.port_num = 1;
160 gid_event.event = IB_EVENT_GID_CHANGE;
161 ib_dispatch_event(&gid_event);
162 }
Parav Panditfe2caef2012-03-21 04:09:06 +0530163 mutex_unlock(&dev->dev_lock);
164 return NOTIFY_OK;
165}
166
Moni Shoua37721d82013-12-12 18:03:16 +0200167static int ocrdma_inetaddr_event(struct notifier_block *notifier,
168 unsigned long event, void *ptr)
169{
170 struct in_ifaddr *ifa = ptr;
171 union ib_gid gid;
172 struct net_device *netdev = ifa->ifa_dev->dev;
173
174 ipv6_addr_set_v4mapped(ifa->ifa_address, (struct in6_addr *)&gid);
175 return ocrdma_addr_event(event, netdev, &gid);
176}
177
Roland Dreier31ab8ac2014-01-19 15:13:44 -0800178static struct notifier_block ocrdma_inetaddr_notifier = {
179 .notifier_call = ocrdma_inetaddr_event
180};
181
Moni Shoua37721d82013-12-12 18:03:16 +0200182#if IS_ENABLED(CONFIG_IPV6)
183
184static int ocrdma_inet6addr_event(struct notifier_block *notifier,
185 unsigned long event, void *ptr)
186{
187 struct inet6_ifaddr *ifa = (struct inet6_ifaddr *)ptr;
188 union ib_gid *gid = (union ib_gid *)&ifa->addr;
189 struct net_device *netdev = ifa->idev->dev;
190 return ocrdma_addr_event(event, netdev, gid);
191}
192
Roland Dreier34955662012-05-02 15:42:33 -0700193static struct notifier_block ocrdma_inet6addr_notifier = {
194 .notifier_call = ocrdma_inet6addr_event
195};
196
Parav Pandit6ab68272012-06-08 21:25:25 +0530197#endif /* IPV6 and VLAN */
Roland Dreier34955662012-05-02 15:42:33 -0700198
Parav Panditfe2caef2012-03-21 04:09:06 +0530199static enum rdma_link_layer ocrdma_link_layer(struct ib_device *device,
200 u8 port_num)
201{
202 return IB_LINK_LAYER_ETHERNET;
203}
204
Roland Dreierabe3afa2012-04-16 11:36:29 -0700205static int ocrdma_register_device(struct ocrdma_dev *dev)
Parav Panditfe2caef2012-03-21 04:09:06 +0530206{
207 strlcpy(dev->ibdev.name, "ocrdma%d", IB_DEVICE_NAME_MAX);
208 ocrdma_get_guid(dev, (u8 *)&dev->ibdev.node_guid);
209 memcpy(dev->ibdev.node_desc, OCRDMA_NODE_DESC,
210 sizeof(OCRDMA_NODE_DESC));
211 dev->ibdev.owner = THIS_MODULE;
Naresh Gottumukkala38754392013-08-26 15:27:49 +0530212 dev->ibdev.uverbs_abi_ver = OCRDMA_ABI_VERSION;
Parav Panditfe2caef2012-03-21 04:09:06 +0530213 dev->ibdev.uverbs_cmd_mask =
214 OCRDMA_UVERBS(GET_CONTEXT) |
215 OCRDMA_UVERBS(QUERY_DEVICE) |
216 OCRDMA_UVERBS(QUERY_PORT) |
217 OCRDMA_UVERBS(ALLOC_PD) |
218 OCRDMA_UVERBS(DEALLOC_PD) |
219 OCRDMA_UVERBS(REG_MR) |
220 OCRDMA_UVERBS(DEREG_MR) |
221 OCRDMA_UVERBS(CREATE_COMP_CHANNEL) |
222 OCRDMA_UVERBS(CREATE_CQ) |
223 OCRDMA_UVERBS(RESIZE_CQ) |
224 OCRDMA_UVERBS(DESTROY_CQ) |
225 OCRDMA_UVERBS(REQ_NOTIFY_CQ) |
226 OCRDMA_UVERBS(CREATE_QP) |
227 OCRDMA_UVERBS(MODIFY_QP) |
228 OCRDMA_UVERBS(QUERY_QP) |
229 OCRDMA_UVERBS(DESTROY_QP) |
230 OCRDMA_UVERBS(POLL_CQ) |
231 OCRDMA_UVERBS(POST_SEND) |
232 OCRDMA_UVERBS(POST_RECV);
233
234 dev->ibdev.uverbs_cmd_mask |=
235 OCRDMA_UVERBS(CREATE_AH) |
236 OCRDMA_UVERBS(MODIFY_AH) |
237 OCRDMA_UVERBS(QUERY_AH) |
238 OCRDMA_UVERBS(DESTROY_AH);
239
240 dev->ibdev.node_type = RDMA_NODE_IB_CA;
241 dev->ibdev.phys_port_cnt = 1;
242 dev->ibdev.num_comp_vectors = 1;
243
244 /* mandatory verbs. */
245 dev->ibdev.query_device = ocrdma_query_device;
246 dev->ibdev.query_port = ocrdma_query_port;
247 dev->ibdev.modify_port = ocrdma_modify_port;
248 dev->ibdev.query_gid = ocrdma_query_gid;
249 dev->ibdev.get_link_layer = ocrdma_link_layer;
250 dev->ibdev.alloc_pd = ocrdma_alloc_pd;
251 dev->ibdev.dealloc_pd = ocrdma_dealloc_pd;
252
253 dev->ibdev.create_cq = ocrdma_create_cq;
254 dev->ibdev.destroy_cq = ocrdma_destroy_cq;
255 dev->ibdev.resize_cq = ocrdma_resize_cq;
256
257 dev->ibdev.create_qp = ocrdma_create_qp;
258 dev->ibdev.modify_qp = ocrdma_modify_qp;
259 dev->ibdev.query_qp = ocrdma_query_qp;
260 dev->ibdev.destroy_qp = ocrdma_destroy_qp;
261
262 dev->ibdev.query_pkey = ocrdma_query_pkey;
263 dev->ibdev.create_ah = ocrdma_create_ah;
264 dev->ibdev.destroy_ah = ocrdma_destroy_ah;
265 dev->ibdev.query_ah = ocrdma_query_ah;
266 dev->ibdev.modify_ah = ocrdma_modify_ah;
267
268 dev->ibdev.poll_cq = ocrdma_poll_cq;
269 dev->ibdev.post_send = ocrdma_post_send;
270 dev->ibdev.post_recv = ocrdma_post_recv;
271 dev->ibdev.req_notify_cq = ocrdma_arm_cq;
272
273 dev->ibdev.get_dma_mr = ocrdma_get_dma_mr;
Naresh Gottumukkalacffce992013-08-26 15:27:44 +0530274 dev->ibdev.reg_phys_mr = ocrdma_reg_kernel_mr;
Parav Panditfe2caef2012-03-21 04:09:06 +0530275 dev->ibdev.dereg_mr = ocrdma_dereg_mr;
276 dev->ibdev.reg_user_mr = ocrdma_reg_user_mr;
277
Naresh Gottumukkala7c338802013-08-26 15:27:39 +0530278 dev->ibdev.alloc_fast_reg_mr = ocrdma_alloc_frmr;
279 dev->ibdev.alloc_fast_reg_page_list = ocrdma_alloc_frmr_page_list;
280 dev->ibdev.free_fast_reg_page_list = ocrdma_free_frmr_page_list;
281
Parav Panditfe2caef2012-03-21 04:09:06 +0530282 /* mandatory to support user space verbs consumer. */
283 dev->ibdev.alloc_ucontext = ocrdma_alloc_ucontext;
284 dev->ibdev.dealloc_ucontext = ocrdma_dealloc_ucontext;
285 dev->ibdev.mmap = ocrdma_mmap;
286 dev->ibdev.dma_device = &dev->nic_info.pdev->dev;
287
288 dev->ibdev.process_mad = ocrdma_process_mad;
289
Devesh Sharma21c33912014-02-04 11:56:56 +0530290 if (ocrdma_get_asic_type(dev) == OCRDMA_ASIC_GEN_SKH_R) {
Parav Panditfe2caef2012-03-21 04:09:06 +0530291 dev->ibdev.uverbs_cmd_mask |=
292 OCRDMA_UVERBS(CREATE_SRQ) |
293 OCRDMA_UVERBS(MODIFY_SRQ) |
294 OCRDMA_UVERBS(QUERY_SRQ) |
295 OCRDMA_UVERBS(DESTROY_SRQ) |
296 OCRDMA_UVERBS(POST_SRQ_RECV);
297
298 dev->ibdev.create_srq = ocrdma_create_srq;
299 dev->ibdev.modify_srq = ocrdma_modify_srq;
300 dev->ibdev.query_srq = ocrdma_query_srq;
301 dev->ibdev.destroy_srq = ocrdma_destroy_srq;
302 dev->ibdev.post_srq_recv = ocrdma_post_srq_recv;
303 }
304 return ib_register_device(&dev->ibdev, NULL);
305}
306
307static int ocrdma_alloc_resources(struct ocrdma_dev *dev)
308{
309 mutex_init(&dev->dev_lock);
310 dev->sgid_tbl = kzalloc(sizeof(union ib_gid) *
311 OCRDMA_MAX_SGID, GFP_KERNEL);
312 if (!dev->sgid_tbl)
313 goto alloc_err;
314 spin_lock_init(&dev->sgid_lock);
315
316 dev->cq_tbl = kzalloc(sizeof(struct ocrdma_cq *) *
317 OCRDMA_MAX_CQ, GFP_KERNEL);
318 if (!dev->cq_tbl)
319 goto alloc_err;
320
321 if (dev->attr.max_qp) {
322 dev->qp_tbl = kzalloc(sizeof(struct ocrdma_qp *) *
323 OCRDMA_MAX_QP, GFP_KERNEL);
324 if (!dev->qp_tbl)
325 goto alloc_err;
326 }
Selvin Xavier4f1df842014-06-10 19:32:24 +0530327
328 dev->stag_arr = kzalloc(sizeof(u64) * OCRDMA_MAX_STAG, GFP_KERNEL);
329 if (dev->stag_arr == NULL)
330 goto alloc_err;
331
Parav Panditfe2caef2012-03-21 04:09:06 +0530332 spin_lock_init(&dev->av_tbl.lock);
333 spin_lock_init(&dev->flush_q_lock);
334 return 0;
335alloc_err:
Naresh Gottumukkalaef99c4c2013-06-10 04:42:39 +0000336 pr_err("%s(%d) error.\n", __func__, dev->id);
Parav Panditfe2caef2012-03-21 04:09:06 +0530337 return -ENOMEM;
338}
339
340static void ocrdma_free_resources(struct ocrdma_dev *dev)
341{
Selvin Xavier4f1df842014-06-10 19:32:24 +0530342 kfree(dev->stag_arr);
Parav Panditfe2caef2012-03-21 04:09:06 +0530343 kfree(dev->qp_tbl);
344 kfree(dev->cq_tbl);
345 kfree(dev->sgid_tbl);
346}
347
Selvin Xavier334b8db2014-02-04 11:57:09 +0530348/* OCRDMA sysfs interface */
349static ssize_t show_rev(struct device *device, struct device_attribute *attr,
350 char *buf)
351{
352 struct ocrdma_dev *dev = dev_get_drvdata(device);
353
354 return scnprintf(buf, PAGE_SIZE, "0x%x\n", dev->nic_info.pdev->vendor);
355}
356
357static ssize_t show_fw_ver(struct device *device, struct device_attribute *attr,
358 char *buf)
359{
360 struct ocrdma_dev *dev = dev_get_drvdata(device);
361
Selvin Xavier4808b182014-06-10 19:32:14 +0530362 return scnprintf(buf, PAGE_SIZE, "%s\n", &dev->attr.fw_ver[0]);
363}
364
365static ssize_t show_hca_type(struct device *device,
366 struct device_attribute *attr, char *buf)
367{
368 struct ocrdma_dev *dev = dev_get_drvdata(device);
369
370 return scnprintf(buf, PAGE_SIZE, "%s\n", &dev->model_number[0]);
Selvin Xavier334b8db2014-02-04 11:57:09 +0530371}
372
373static DEVICE_ATTR(hw_rev, S_IRUGO, show_rev, NULL);
374static DEVICE_ATTR(fw_ver, S_IRUGO, show_fw_ver, NULL);
Selvin Xavier4808b182014-06-10 19:32:14 +0530375static DEVICE_ATTR(hca_type, S_IRUGO, show_hca_type, NULL);
Selvin Xavier334b8db2014-02-04 11:57:09 +0530376
377static struct device_attribute *ocrdma_attributes[] = {
378 &dev_attr_hw_rev,
Selvin Xavier4808b182014-06-10 19:32:14 +0530379 &dev_attr_fw_ver,
380 &dev_attr_hca_type
Selvin Xavier334b8db2014-02-04 11:57:09 +0530381};
382
383static void ocrdma_remove_sysfiles(struct ocrdma_dev *dev)
384{
385 int i;
386
387 for (i = 0; i < ARRAY_SIZE(ocrdma_attributes); i++)
388 device_remove_file(&dev->ibdev.dev, ocrdma_attributes[i]);
389}
390
Devesh Sharma7ec11e02014-08-22 16:57:19 +0530391static void ocrdma_add_default_sgid(struct ocrdma_dev *dev)
392{
393 /* GID Index 0 - Invariant manufacturer-assigned EUI-64 */
394 union ib_gid *sgid = &dev->sgid_tbl[0];
395
396 sgid->global.subnet_prefix = cpu_to_be64(0xfe80000000000000LL);
397 ocrdma_get_guid(dev, &sgid->raw[8]);
398}
399
Selvin Xavierb8806322014-06-10 19:32:25 +0530400static void ocrdma_init_ipv4_gids(struct ocrdma_dev *dev,
401 struct net_device *net)
402{
403 struct in_device *in_dev;
404 union ib_gid gid;
405 in_dev = in_dev_get(net);
406 if (in_dev) {
407 for_ifa(in_dev) {
408 ipv6_addr_set_v4mapped(ifa->ifa_address,
409 (struct in6_addr *)&gid);
410 ocrdma_add_sgid(dev, &gid);
411 }
412 endfor_ifa(in_dev);
413 in_dev_put(in_dev);
414 }
415}
416
417static void ocrdma_init_ipv6_gids(struct ocrdma_dev *dev,
418 struct net_device *net)
419{
420#if IS_ENABLED(CONFIG_IPV6)
421 struct inet6_dev *in6_dev;
422 union ib_gid *pgid;
423 struct inet6_ifaddr *ifp;
424 in6_dev = in6_dev_get(net);
425 if (in6_dev) {
426 read_lock_bh(&in6_dev->lock);
427 list_for_each_entry(ifp, &in6_dev->addr_list, if_list) {
428 pgid = (union ib_gid *)&ifp->addr;
429 ocrdma_add_sgid(dev, pgid);
430 }
431 read_unlock_bh(&in6_dev->lock);
432 in6_dev_put(in6_dev);
433 }
434#endif
435}
436
437static void ocrdma_init_gid_table(struct ocrdma_dev *dev)
438{
439 struct net_device *net_dev;
440
441 for_each_netdev(&init_net, net_dev) {
442 struct net_device *real_dev = rdma_vlan_dev_real_dev(net_dev) ?
443 rdma_vlan_dev_real_dev(net_dev) : net_dev;
444
445 if (real_dev == dev->nic_info.netdev) {
Devesh Sharma7ec11e02014-08-22 16:57:19 +0530446 ocrdma_add_default_sgid(dev);
Selvin Xavierb8806322014-06-10 19:32:25 +0530447 ocrdma_init_ipv4_gids(dev, net_dev);
448 ocrdma_init_ipv6_gids(dev, net_dev);
449 }
450 }
451}
452
Parav Panditfe2caef2012-03-21 04:09:06 +0530453static struct ocrdma_dev *ocrdma_add(struct be_dev_info *dev_info)
454{
Selvin Xavier334b8db2014-02-04 11:57:09 +0530455 int status = 0, i;
Parav Panditfe2caef2012-03-21 04:09:06 +0530456 struct ocrdma_dev *dev;
457
458 dev = (struct ocrdma_dev *)ib_alloc_device(sizeof(struct ocrdma_dev));
459 if (!dev) {
Naresh Gottumukkalaef99c4c2013-06-10 04:42:39 +0000460 pr_err("Unable to allocate ib device\n");
Parav Panditfe2caef2012-03-21 04:09:06 +0530461 return NULL;
462 }
463 dev->mbx_cmd = kzalloc(sizeof(struct ocrdma_mqe_emb_cmd), GFP_KERNEL);
464 if (!dev->mbx_cmd)
465 goto idr_err;
466
467 memcpy(&dev->nic_info, dev_info, sizeof(*dev_info));
Tejun Heocffcd592013-02-27 17:04:24 -0800468 dev->id = idr_alloc(&ocrdma_dev_id, NULL, 0, 0, GFP_KERNEL);
Parav Panditfe2caef2012-03-21 04:09:06 +0530469 if (dev->id < 0)
470 goto idr_err;
471
472 status = ocrdma_init_hw(dev);
473 if (status)
474 goto init_err;
475
476 status = ocrdma_alloc_resources(dev);
477 if (status)
478 goto alloc_err;
479
Selvin Xavier31dbdd92014-06-10 19:32:13 +0530480 ocrdma_init_service_level(dev);
Selvin Xavierb8806322014-06-10 19:32:25 +0530481 ocrdma_init_gid_table(dev);
Parav Panditfe2caef2012-03-21 04:09:06 +0530482 status = ocrdma_register_device(dev);
483 if (status)
484 goto alloc_err;
485
Selvin Xavier334b8db2014-02-04 11:57:09 +0530486 for (i = 0; i < ARRAY_SIZE(ocrdma_attributes); i++)
487 if (device_create_file(&dev->ibdev.dev, ocrdma_attributes[i]))
488 goto sysfs_err;
Sasha Levin3e4d60a2012-04-28 07:40:01 +0200489 spin_lock(&ocrdma_devlist_lock);
490 list_add_tail_rcu(&dev->entry, &ocrdma_dev_list);
491 spin_unlock(&ocrdma_devlist_lock);
Selvin Xaviera51f06e2014-02-04 11:57:07 +0530492 /* Init stats */
493 ocrdma_add_port_stats(dev);
494
495 pr_info("%s %s: %s \"%s\" port %d\n",
496 dev_name(&dev->nic_info.pdev->dev), hca_name(dev),
497 port_speed_string(dev), dev->model_number,
498 dev->hba_port_num);
499 pr_info("%s ocrdma%d driver loaded successfully\n",
500 dev_name(&dev->nic_info.pdev->dev), dev->id);
Parav Panditfe2caef2012-03-21 04:09:06 +0530501 return dev;
502
Selvin Xavier334b8db2014-02-04 11:57:09 +0530503sysfs_err:
504 ocrdma_remove_sysfiles(dev);
Parav Panditfe2caef2012-03-21 04:09:06 +0530505alloc_err:
506 ocrdma_free_resources(dev);
507 ocrdma_cleanup_hw(dev);
508init_err:
509 idr_remove(&ocrdma_dev_id, dev->id);
510idr_err:
511 kfree(dev->mbx_cmd);
512 ib_dealloc_device(&dev->ibdev);
Naresh Gottumukkalaef99c4c2013-06-10 04:42:39 +0000513 pr_err("%s() leaving. ret=%d\n", __func__, status);
Parav Panditfe2caef2012-03-21 04:09:06 +0530514 return NULL;
515}
516
Sasha Levin3e4d60a2012-04-28 07:40:01 +0200517static void ocrdma_remove_free(struct rcu_head *rcu)
Parav Panditfe2caef2012-03-21 04:09:06 +0530518{
Sasha Levin3e4d60a2012-04-28 07:40:01 +0200519 struct ocrdma_dev *dev = container_of(rcu, struct ocrdma_dev, rcu);
Parav Panditfe2caef2012-03-21 04:09:06 +0530520
Parav Panditfe2caef2012-03-21 04:09:06 +0530521 idr_remove(&ocrdma_dev_id, dev->id);
522 kfree(dev->mbx_cmd);
523 ib_dealloc_device(&dev->ibdev);
524}
525
Sasha Levin3e4d60a2012-04-28 07:40:01 +0200526static void ocrdma_remove(struct ocrdma_dev *dev)
527{
528 /* first unregister with stack to stop all the active traffic
529 * of the registered clients.
530 */
Selvin Xaviera51f06e2014-02-04 11:57:07 +0530531 ocrdma_rem_port_stats(dev);
Selvin Xavier334b8db2014-02-04 11:57:09 +0530532 ocrdma_remove_sysfiles(dev);
533
Sasha Levin3e4d60a2012-04-28 07:40:01 +0200534 ib_unregister_device(&dev->ibdev);
535
536 spin_lock(&ocrdma_devlist_lock);
537 list_del_rcu(&dev->entry);
538 spin_unlock(&ocrdma_devlist_lock);
Naresh Gottumukkala1852d1d2013-09-06 15:02:47 +0530539
540 ocrdma_free_resources(dev);
541 ocrdma_cleanup_hw(dev);
542
Sasha Levin3e4d60a2012-04-28 07:40:01 +0200543 call_rcu(&dev->rcu, ocrdma_remove_free);
544}
545
Parav Panditfe2caef2012-03-21 04:09:06 +0530546static int ocrdma_open(struct ocrdma_dev *dev)
547{
548 struct ib_event port_event;
549
550 port_event.event = IB_EVENT_PORT_ACTIVE;
551 port_event.element.port_num = 1;
552 port_event.device = &dev->ibdev;
553 ib_dispatch_event(&port_event);
554 return 0;
555}
556
557static int ocrdma_close(struct ocrdma_dev *dev)
558{
559 int i;
560 struct ocrdma_qp *qp, **cur_qp;
561 struct ib_event err_event;
562 struct ib_qp_attr attrs;
563 int attr_mask = IB_QP_STATE;
564
565 attrs.qp_state = IB_QPS_ERR;
566 mutex_lock(&dev->dev_lock);
567 if (dev->qp_tbl) {
568 cur_qp = dev->qp_tbl;
569 for (i = 0; i < OCRDMA_MAX_QP; i++) {
570 qp = cur_qp[i];
Devesh Sharmafad51b72014-02-04 11:57:10 +0530571 if (qp && qp->ibqp.qp_type != IB_QPT_GSI) {
Parav Panditfe2caef2012-03-21 04:09:06 +0530572 /* change the QP state to ERROR */
573 _ocrdma_modify_qp(&qp->ibqp, &attrs, attr_mask);
574
575 err_event.event = IB_EVENT_QP_FATAL;
576 err_event.element.qp = &qp->ibqp;
577 err_event.device = &dev->ibdev;
578 ib_dispatch_event(&err_event);
579 }
580 }
581 }
582 mutex_unlock(&dev->dev_lock);
583
584 err_event.event = IB_EVENT_PORT_ERR;
585 err_event.element.port_num = 1;
586 err_event.device = &dev->ibdev;
587 ib_dispatch_event(&err_event);
588 return 0;
589}
590
Devesh Sharmaefe45932014-06-10 19:32:16 +0530591static void ocrdma_shutdown(struct ocrdma_dev *dev)
592{
593 ocrdma_close(dev);
594 ocrdma_remove(dev);
595}
596
Parav Panditfe2caef2012-03-21 04:09:06 +0530597/* event handling via NIC driver ensures that all the NIC specific
598 * initialization done before RoCE driver notifies
599 * event to stack.
600 */
601static void ocrdma_event_handler(struct ocrdma_dev *dev, u32 event)
602{
603 switch (event) {
604 case BE_DEV_UP:
605 ocrdma_open(dev);
606 break;
607 case BE_DEV_DOWN:
608 ocrdma_close(dev);
609 break;
Devesh Sharmaefe45932014-06-10 19:32:16 +0530610 case BE_DEV_SHUTDOWN:
611 ocrdma_shutdown(dev);
612 break;
Joe Perches2b50176d2013-10-08 16:07:22 -0700613 }
Parav Panditfe2caef2012-03-21 04:09:06 +0530614}
615
Roland Dreierabe3afa2012-04-16 11:36:29 -0700616static struct ocrdma_driver ocrdma_drv = {
Parav Panditfe2caef2012-03-21 04:09:06 +0530617 .name = "ocrdma_driver",
618 .add = ocrdma_add,
619 .remove = ocrdma_remove,
620 .state_change_handler = ocrdma_event_handler,
Devesh Sharmab6b87d22014-02-04 11:56:58 +0530621 .be_abi_version = OCRDMA_BE_ROCE_ABI_VERSION,
Parav Panditfe2caef2012-03-21 04:09:06 +0530622};
623
Roland Dreier34955662012-05-02 15:42:33 -0700624static void ocrdma_unregister_inet6addr_notifier(void)
625{
Roland Dreierd90f9b32012-07-05 22:39:34 -0700626#if IS_ENABLED(CONFIG_IPV6)
Roland Dreier34955662012-05-02 15:42:33 -0700627 unregister_inet6addr_notifier(&ocrdma_inet6addr_notifier);
628#endif
629}
630
Selvin Xavier2d8f57d2014-03-18 14:54:56 +0530631static void ocrdma_unregister_inetaddr_notifier(void)
632{
633 unregister_inetaddr_notifier(&ocrdma_inetaddr_notifier);
634}
635
Parav Panditfe2caef2012-03-21 04:09:06 +0530636static int __init ocrdma_init_module(void)
637{
638 int status;
639
Selvin Xaviera51f06e2014-02-04 11:57:07 +0530640 ocrdma_init_debugfs();
641
Moni Shoua37721d82013-12-12 18:03:16 +0200642 status = register_inetaddr_notifier(&ocrdma_inetaddr_notifier);
643 if (status)
644 return status;
645
Roland Dreierd90f9b32012-07-05 22:39:34 -0700646#if IS_ENABLED(CONFIG_IPV6)
Parav Panditfe2caef2012-03-21 04:09:06 +0530647 status = register_inet6addr_notifier(&ocrdma_inet6addr_notifier);
648 if (status)
Selvin Xavier2d8f57d2014-03-18 14:54:56 +0530649 goto err_notifier6;
Roland Dreier34955662012-05-02 15:42:33 -0700650#endif
651
Parav Panditfe2caef2012-03-21 04:09:06 +0530652 status = be_roce_register_driver(&ocrdma_drv);
653 if (status)
Selvin Xavier2d8f57d2014-03-18 14:54:56 +0530654 goto err_be_reg;
Roland Dreier34955662012-05-02 15:42:33 -0700655
Selvin Xavier2d8f57d2014-03-18 14:54:56 +0530656 return 0;
657
658err_be_reg:
659 ocrdma_unregister_inet6addr_notifier();
660err_notifier6:
661 ocrdma_unregister_inetaddr_notifier();
Parav Panditfe2caef2012-03-21 04:09:06 +0530662 return status;
663}
664
665static void __exit ocrdma_exit_module(void)
666{
667 be_roce_unregister_driver(&ocrdma_drv);
Roland Dreier34955662012-05-02 15:42:33 -0700668 ocrdma_unregister_inet6addr_notifier();
Selvin Xavier2d8f57d2014-03-18 14:54:56 +0530669 ocrdma_unregister_inetaddr_notifier();
Selvin Xaviera51f06e2014-02-04 11:57:07 +0530670 ocrdma_rem_debugfs();
Parav Panditfe2caef2012-03-21 04:09:06 +0530671}
672
673module_init(ocrdma_init_module);
674module_exit(ocrdma_exit_module);