Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/net
[linux-2.6-block.git] / drivers / infiniband / hw / ocrdma / ocrdma_main.c
CommitLineData
71ee6730
DS
1/* This file is part of the Emulex RoCE Device Driver for
2 * RoCE (RDMA over Converged Ethernet) adapters.
3 * Copyright (C) 2012-2015 Emulex. All rights reserved.
4 * EMULEX and SLI are trademarks of Emulex.
5 * www.emulex.com
6 *
7 * This software is available to you under a choice of one of two licenses.
8 * You may choose to be licensed under the terms of the GNU General Public
9 * License (GPL) Version 2, available from the file COPYING in the main
10 * directory of this source tree, or the BSD license below:
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 *
16 * - Redistributions of source code must retain the above copyright notice,
17 * this list of conditions and the following disclaimer.
18 *
19 * - Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in
21 * the documentation and/or other materials provided with the distribution.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
27 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
30 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
31 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
32 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
33 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
fe2caefc
PP
34 *
35 * Contact Information:
36 * linux-drivers@emulex.com
37 *
38 * Emulex
39 * 3333 Susan Street
40 * Costa Mesa, CA 92626
71ee6730 41 */
fe2caefc
PP
42
43#include <linux/module.h>
fe2caefc
PP
44#include <linux/idr.h>
45#include <rdma/ib_verbs.h>
46#include <rdma/ib_user_verbs.h>
47#include <rdma/ib_addr.h>
337877a4 48#include <rdma/ib_mad.h>
fe2caefc
PP
49
50#include <linux/netdevice.h>
51#include <net/addrconf.h>
52
53#include "ocrdma.h"
54#include "ocrdma_verbs.h"
55#include "ocrdma_ah.h"
56#include "be_roce.h"
57#include "ocrdma_hw.h"
a51f06e1 58#include "ocrdma_stats.h"
38754397 59#include "ocrdma_abi.h"
fe2caefc 60
0154410b
DS
61MODULE_VERSION(OCRDMA_ROCE_DRV_VERSION);
62MODULE_DESCRIPTION(OCRDMA_ROCE_DRV_DESC " " OCRDMA_ROCE_DRV_VERSION);
fe2caefc 63MODULE_AUTHOR("Emulex Corporation");
b8f5595e 64MODULE_LICENSE("Dual BSD/GPL");
fe2caefc 65
fe2caefc
PP
66static DEFINE_IDR(ocrdma_dev_id);
67
fe2caefc
PP
68void ocrdma_get_guid(struct ocrdma_dev *dev, u8 *guid)
69{
70 u8 mac_addr[6];
71
72 memcpy(&mac_addr[0], &dev->nic_info.mac_addr[0], ETH_ALEN);
73 guid[0] = mac_addr[0] ^ 2;
74 guid[1] = mac_addr[1];
75 guid[2] = mac_addr[2];
76 guid[3] = 0xff;
77 guid[4] = 0xfe;
78 guid[5] = mac_addr[3];
79 guid[6] = mac_addr[4];
80 guid[7] = mac_addr[5];
81}
fe2caefc
PP
82static enum rdma_link_layer ocrdma_link_layer(struct ib_device *device,
83 u8 port_num)
84{
85 return IB_LINK_LAYER_ETHERNET;
86}
87
7738613e
IW
88static int ocrdma_port_immutable(struct ib_device *ibdev, u8 port_num,
89 struct ib_port_immutable *immutable)
90{
91 struct ib_port_attr attr;
92 int err;
93
94 err = ocrdma_query_port(ibdev, port_num, &attr);
95 if (err)
96 return err;
97
98 immutable->pkey_tbl_len = attr.pkey_tbl_len;
99 immutable->gid_tbl_len = attr.gid_tbl_len;
f9b22e35 100 immutable->core_cap_flags = RDMA_CORE_PORT_IBA_ROCE;
337877a4 101 immutable->max_mad_size = IB_MGMT_MAD_SIZE;
7738613e
IW
102
103 return 0;
104}
105
abe3afac 106static int ocrdma_register_device(struct ocrdma_dev *dev)
fe2caefc
PP
107{
108 strlcpy(dev->ibdev.name, "ocrdma%d", IB_DEVICE_NAME_MAX);
109 ocrdma_get_guid(dev, (u8 *)&dev->ibdev.node_guid);
110 memcpy(dev->ibdev.node_desc, OCRDMA_NODE_DESC,
111 sizeof(OCRDMA_NODE_DESC));
112 dev->ibdev.owner = THIS_MODULE;
38754397 113 dev->ibdev.uverbs_abi_ver = OCRDMA_ABI_VERSION;
fe2caefc
PP
114 dev->ibdev.uverbs_cmd_mask =
115 OCRDMA_UVERBS(GET_CONTEXT) |
116 OCRDMA_UVERBS(QUERY_DEVICE) |
117 OCRDMA_UVERBS(QUERY_PORT) |
118 OCRDMA_UVERBS(ALLOC_PD) |
119 OCRDMA_UVERBS(DEALLOC_PD) |
120 OCRDMA_UVERBS(REG_MR) |
121 OCRDMA_UVERBS(DEREG_MR) |
122 OCRDMA_UVERBS(CREATE_COMP_CHANNEL) |
123 OCRDMA_UVERBS(CREATE_CQ) |
124 OCRDMA_UVERBS(RESIZE_CQ) |
125 OCRDMA_UVERBS(DESTROY_CQ) |
126 OCRDMA_UVERBS(REQ_NOTIFY_CQ) |
127 OCRDMA_UVERBS(CREATE_QP) |
128 OCRDMA_UVERBS(MODIFY_QP) |
129 OCRDMA_UVERBS(QUERY_QP) |
130 OCRDMA_UVERBS(DESTROY_QP) |
131 OCRDMA_UVERBS(POLL_CQ) |
132 OCRDMA_UVERBS(POST_SEND) |
133 OCRDMA_UVERBS(POST_RECV);
134
135 dev->ibdev.uverbs_cmd_mask |=
136 OCRDMA_UVERBS(CREATE_AH) |
137 OCRDMA_UVERBS(MODIFY_AH) |
138 OCRDMA_UVERBS(QUERY_AH) |
139 OCRDMA_UVERBS(DESTROY_AH);
140
141 dev->ibdev.node_type = RDMA_NODE_IB_CA;
142 dev->ibdev.phys_port_cnt = 1;
0c0eacdc 143 dev->ibdev.num_comp_vectors = dev->eq_cnt;
fe2caefc
PP
144
145 /* mandatory verbs. */
146 dev->ibdev.query_device = ocrdma_query_device;
147 dev->ibdev.query_port = ocrdma_query_port;
148 dev->ibdev.modify_port = ocrdma_modify_port;
149 dev->ibdev.query_gid = ocrdma_query_gid;
cc36929e
SK
150 dev->ibdev.get_netdev = ocrdma_get_netdev;
151 dev->ibdev.add_gid = ocrdma_add_gid;
152 dev->ibdev.del_gid = ocrdma_del_gid;
fe2caefc
PP
153 dev->ibdev.get_link_layer = ocrdma_link_layer;
154 dev->ibdev.alloc_pd = ocrdma_alloc_pd;
155 dev->ibdev.dealloc_pd = ocrdma_dealloc_pd;
156
157 dev->ibdev.create_cq = ocrdma_create_cq;
158 dev->ibdev.destroy_cq = ocrdma_destroy_cq;
159 dev->ibdev.resize_cq = ocrdma_resize_cq;
160
161 dev->ibdev.create_qp = ocrdma_create_qp;
162 dev->ibdev.modify_qp = ocrdma_modify_qp;
163 dev->ibdev.query_qp = ocrdma_query_qp;
164 dev->ibdev.destroy_qp = ocrdma_destroy_qp;
165
166 dev->ibdev.query_pkey = ocrdma_query_pkey;
167 dev->ibdev.create_ah = ocrdma_create_ah;
168 dev->ibdev.destroy_ah = ocrdma_destroy_ah;
169 dev->ibdev.query_ah = ocrdma_query_ah;
170 dev->ibdev.modify_ah = ocrdma_modify_ah;
171
172 dev->ibdev.poll_cq = ocrdma_poll_cq;
173 dev->ibdev.post_send = ocrdma_post_send;
174 dev->ibdev.post_recv = ocrdma_post_recv;
175 dev->ibdev.req_notify_cq = ocrdma_arm_cq;
176
177 dev->ibdev.get_dma_mr = ocrdma_get_dma_mr;
178 dev->ibdev.dereg_mr = ocrdma_dereg_mr;
179 dev->ibdev.reg_user_mr = ocrdma_reg_user_mr;
180
cacb7d59 181 dev->ibdev.alloc_mr = ocrdma_alloc_mr;
2eaa1c56 182 dev->ibdev.map_mr_sg = ocrdma_map_mr_sg;
7c33880c 183
fe2caefc
PP
184 /* mandatory to support user space verbs consumer. */
185 dev->ibdev.alloc_ucontext = ocrdma_alloc_ucontext;
186 dev->ibdev.dealloc_ucontext = ocrdma_dealloc_ucontext;
187 dev->ibdev.mmap = ocrdma_mmap;
188 dev->ibdev.dma_device = &dev->nic_info.pdev->dev;
189
190 dev->ibdev.process_mad = ocrdma_process_mad;
7738613e 191 dev->ibdev.get_port_immutable = ocrdma_port_immutable;
fe2caefc 192
21c3391a 193 if (ocrdma_get_asic_type(dev) == OCRDMA_ASIC_GEN_SKH_R) {
fe2caefc
PP
194 dev->ibdev.uverbs_cmd_mask |=
195 OCRDMA_UVERBS(CREATE_SRQ) |
196 OCRDMA_UVERBS(MODIFY_SRQ) |
197 OCRDMA_UVERBS(QUERY_SRQ) |
198 OCRDMA_UVERBS(DESTROY_SRQ) |
199 OCRDMA_UVERBS(POST_SRQ_RECV);
200
201 dev->ibdev.create_srq = ocrdma_create_srq;
202 dev->ibdev.modify_srq = ocrdma_modify_srq;
203 dev->ibdev.query_srq = ocrdma_query_srq;
204 dev->ibdev.destroy_srq = ocrdma_destroy_srq;
205 dev->ibdev.post_srq_recv = ocrdma_post_srq_recv;
206 }
207 return ib_register_device(&dev->ibdev, NULL);
208}
209
210static int ocrdma_alloc_resources(struct ocrdma_dev *dev)
211{
212 mutex_init(&dev->dev_lock);
fe2caefc
PP
213 dev->cq_tbl = kzalloc(sizeof(struct ocrdma_cq *) *
214 OCRDMA_MAX_CQ, GFP_KERNEL);
215 if (!dev->cq_tbl)
216 goto alloc_err;
217
218 if (dev->attr.max_qp) {
219 dev->qp_tbl = kzalloc(sizeof(struct ocrdma_qp *) *
220 OCRDMA_MAX_QP, GFP_KERNEL);
221 if (!dev->qp_tbl)
222 goto alloc_err;
223 }
4f1df844
SX
224
225 dev->stag_arr = kzalloc(sizeof(u64) * OCRDMA_MAX_STAG, GFP_KERNEL);
226 if (dev->stag_arr == NULL)
227 goto alloc_err;
228
9ba1377d
MA
229 ocrdma_alloc_pd_pool(dev);
230
fd98d896
SX
231 if (!ocrdma_alloc_stats_resources(dev)) {
232 pr_err("%s: stats resource allocation failed\n", __func__);
233 goto alloc_err;
234 }
235
fe2caefc
PP
236 spin_lock_init(&dev->av_tbl.lock);
237 spin_lock_init(&dev->flush_q_lock);
238 return 0;
239alloc_err:
ef99c4c2 240 pr_err("%s(%d) error.\n", __func__, dev->id);
fe2caefc
PP
241 return -ENOMEM;
242}
243
244static void ocrdma_free_resources(struct ocrdma_dev *dev)
245{
fd98d896 246 ocrdma_release_stats_resources(dev);
4f1df844 247 kfree(dev->stag_arr);
fe2caefc
PP
248 kfree(dev->qp_tbl);
249 kfree(dev->cq_tbl);
fe2caefc
PP
250}
251
334b8db3
SX
252/* OCRDMA sysfs interface */
253static ssize_t show_rev(struct device *device, struct device_attribute *attr,
254 char *buf)
255{
256 struct ocrdma_dev *dev = dev_get_drvdata(device);
257
258 return scnprintf(buf, PAGE_SIZE, "0x%x\n", dev->nic_info.pdev->vendor);
259}
260
261static ssize_t show_fw_ver(struct device *device, struct device_attribute *attr,
262 char *buf)
263{
264 struct ocrdma_dev *dev = dev_get_drvdata(device);
265
4808b184
SX
266 return scnprintf(buf, PAGE_SIZE, "%s\n", &dev->attr.fw_ver[0]);
267}
268
269static ssize_t show_hca_type(struct device *device,
270 struct device_attribute *attr, char *buf)
271{
272 struct ocrdma_dev *dev = dev_get_drvdata(device);
273
274 return scnprintf(buf, PAGE_SIZE, "%s\n", &dev->model_number[0]);
334b8db3
SX
275}
276
277static DEVICE_ATTR(hw_rev, S_IRUGO, show_rev, NULL);
278static DEVICE_ATTR(fw_ver, S_IRUGO, show_fw_ver, NULL);
4808b184 279static DEVICE_ATTR(hca_type, S_IRUGO, show_hca_type, NULL);
334b8db3
SX
280
281static struct device_attribute *ocrdma_attributes[] = {
282 &dev_attr_hw_rev,
4808b184
SX
283 &dev_attr_fw_ver,
284 &dev_attr_hca_type
334b8db3
SX
285};
286
287static void ocrdma_remove_sysfiles(struct ocrdma_dev *dev)
288{
289 int i;
290
291 for (i = 0; i < ARRAY_SIZE(ocrdma_attributes); i++)
292 device_remove_file(&dev->ibdev.dev, ocrdma_attributes[i]);
293}
294
fe2caefc
PP
295static struct ocrdma_dev *ocrdma_add(struct be_dev_info *dev_info)
296{
334b8db3 297 int status = 0, i;
3b1ea430 298 u8 lstate = 0;
fe2caefc
PP
299 struct ocrdma_dev *dev;
300
301 dev = (struct ocrdma_dev *)ib_alloc_device(sizeof(struct ocrdma_dev));
302 if (!dev) {
ef99c4c2 303 pr_err("Unable to allocate ib device\n");
fe2caefc
PP
304 return NULL;
305 }
306 dev->mbx_cmd = kzalloc(sizeof(struct ocrdma_mqe_emb_cmd), GFP_KERNEL);
307 if (!dev->mbx_cmd)
308 goto idr_err;
309
310 memcpy(&dev->nic_info, dev_info, sizeof(*dev_info));
cffcd59f 311 dev->id = idr_alloc(&ocrdma_dev_id, NULL, 0, 0, GFP_KERNEL);
fe2caefc
PP
312 if (dev->id < 0)
313 goto idr_err;
314
315 status = ocrdma_init_hw(dev);
316 if (status)
317 goto init_err;
318
319 status = ocrdma_alloc_resources(dev);
320 if (status)
321 goto alloc_err;
322
31dbdd9a 323 ocrdma_init_service_level(dev);
fe2caefc
PP
324 status = ocrdma_register_device(dev);
325 if (status)
326 goto alloc_err;
327
3b1ea430
DS
328 /* Query Link state and update */
329 status = ocrdma_mbx_get_link_speed(dev, NULL, &lstate);
330 if (!status)
331 ocrdma_update_link_state(dev, lstate);
332
334b8db3
SX
333 for (i = 0; i < ARRAY_SIZE(ocrdma_attributes); i++)
334 if (device_create_file(&dev->ibdev.dev, ocrdma_attributes[i]))
335 goto sysfs_err;
a51f06e1
SX
336 /* Init stats */
337 ocrdma_add_port_stats(dev);
b4dbe8d5
MA
338 /* Interrupt Moderation */
339 INIT_DELAYED_WORK(&dev->eqd_work, ocrdma_eqd_set_task);
340 schedule_delayed_work(&dev->eqd_work, msecs_to_jiffies(1000));
a51f06e1
SX
341
342 pr_info("%s %s: %s \"%s\" port %d\n",
343 dev_name(&dev->nic_info.pdev->dev), hca_name(dev),
344 port_speed_string(dev), dev->model_number,
345 dev->hba_port_num);
346 pr_info("%s ocrdma%d driver loaded successfully\n",
347 dev_name(&dev->nic_info.pdev->dev), dev->id);
fe2caefc
PP
348 return dev;
349
334b8db3
SX
350sysfs_err:
351 ocrdma_remove_sysfiles(dev);
fe2caefc
PP
352alloc_err:
353 ocrdma_free_resources(dev);
354 ocrdma_cleanup_hw(dev);
355init_err:
356 idr_remove(&ocrdma_dev_id, dev->id);
357idr_err:
358 kfree(dev->mbx_cmd);
359 ib_dealloc_device(&dev->ibdev);
ef99c4c2 360 pr_err("%s() leaving. ret=%d\n", __func__, status);
fe2caefc
PP
361 return NULL;
362}
363
5a85f5e9 364static void ocrdma_remove_free(struct ocrdma_dev *dev)
fe2caefc 365{
fe2caefc 366
fe2caefc
PP
367 idr_remove(&ocrdma_dev_id, dev->id);
368 kfree(dev->mbx_cmd);
369 ib_dealloc_device(&dev->ibdev);
370}
371
3e4d60a8
SL
372static void ocrdma_remove(struct ocrdma_dev *dev)
373{
374 /* first unregister with stack to stop all the active traffic
375 * of the registered clients.
376 */
b4dbe8d5 377 cancel_delayed_work_sync(&dev->eqd_work);
334b8db3 378 ocrdma_remove_sysfiles(dev);
3e4d60a8 379 ib_unregister_device(&dev->ibdev);
4b8180aa
MA
380
381 ocrdma_rem_port_stats(dev);
1852d1da
NG
382 ocrdma_free_resources(dev);
383 ocrdma_cleanup_hw(dev);
5a85f5e9 384 ocrdma_remove_free(dev);
3e4d60a8
SL
385}
386
3b1ea430 387static int ocrdma_dispatch_port_active(struct ocrdma_dev *dev)
fe2caefc
PP
388{
389 struct ib_event port_event;
390
391 port_event.event = IB_EVENT_PORT_ACTIVE;
392 port_event.element.port_num = 1;
393 port_event.device = &dev->ibdev;
394 ib_dispatch_event(&port_event);
395 return 0;
396}
397
3b1ea430 398static int ocrdma_dispatch_port_error(struct ocrdma_dev *dev)
fe2caefc 399{
fe2caefc 400 struct ib_event err_event;
fe2caefc
PP
401
402 err_event.event = IB_EVENT_PORT_ERR;
403 err_event.element.port_num = 1;
404 err_event.device = &dev->ibdev;
405 ib_dispatch_event(&err_event);
406 return 0;
407}
408
efe45937
DS
409static void ocrdma_shutdown(struct ocrdma_dev *dev)
410{
3b1ea430 411 ocrdma_dispatch_port_error(dev);
efe45937
DS
412 ocrdma_remove(dev);
413}
414
fe2caefc
PP
415/* event handling via NIC driver ensures that all the NIC specific
416 * initialization done before RoCE driver notifies
417 * event to stack.
418 */
419static void ocrdma_event_handler(struct ocrdma_dev *dev, u32 event)
420{
421 switch (event) {
efe45937
DS
422 case BE_DEV_SHUTDOWN:
423 ocrdma_shutdown(dev);
424 break;
3b1ea430
DS
425 default:
426 break;
2b50176d 427 }
fe2caefc
PP
428}
429
3b1ea430
DS
430void ocrdma_update_link_state(struct ocrdma_dev *dev, u8 lstate)
431{
432 if (!(dev->flags & OCRDMA_FLAGS_LINK_STATUS_INIT)) {
433 dev->flags |= OCRDMA_FLAGS_LINK_STATUS_INIT;
434 if (!lstate)
435 return;
436 }
437
438 if (!lstate)
439 ocrdma_dispatch_port_error(dev);
440 else
441 ocrdma_dispatch_port_active(dev);
442}
443
abe3afac 444static struct ocrdma_driver ocrdma_drv = {
fe2caefc
PP
445 .name = "ocrdma_driver",
446 .add = ocrdma_add,
447 .remove = ocrdma_remove,
448 .state_change_handler = ocrdma_event_handler,
b6b87d2e 449 .be_abi_version = OCRDMA_BE_ROCE_ABI_VERSION,
fe2caefc
PP
450};
451
452static int __init ocrdma_init_module(void)
453{
454 int status;
455
a51f06e1
SX
456 ocrdma_init_debugfs();
457
fe2caefc
PP
458 status = be_roce_register_driver(&ocrdma_drv);
459 if (status)
2d8f57d5 460 goto err_be_reg;
34955669 461
2d8f57d5
SX
462 return 0;
463
464err_be_reg:
cc36929e 465
fe2caefc
PP
466 return status;
467}
468
469static void __exit ocrdma_exit_module(void)
470{
471 be_roce_unregister_driver(&ocrdma_drv);
a51f06e1 472 ocrdma_rem_debugfs();
d8b2ba7c 473 idr_destroy(&ocrdma_dev_id);
fe2caefc
PP
474}
475
476module_init(ocrdma_init_module);
477module_exit(ocrdma_exit_module);