treewide: kzalloc() -> kcalloc()
[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"
a7fe7380 59#include <rdma/ocrdma-abi.h>
fe2caefc 60
0154410b 61MODULE_DESCRIPTION(OCRDMA_ROCE_DRV_DESC " " OCRDMA_ROCE_DRV_VERSION);
fe2caefc 62MODULE_AUTHOR("Emulex Corporation");
b8f5595e 63MODULE_LICENSE("Dual BSD/GPL");
fe2caefc 64
fe2caefc
PP
65static DEFINE_IDR(ocrdma_dev_id);
66
fe2caefc
PP
67void ocrdma_get_guid(struct ocrdma_dev *dev, u8 *guid)
68{
69 u8 mac_addr[6];
70
71 memcpy(&mac_addr[0], &dev->nic_info.mac_addr[0], ETH_ALEN);
72 guid[0] = mac_addr[0] ^ 2;
73 guid[1] = mac_addr[1];
74 guid[2] = mac_addr[2];
75 guid[3] = 0xff;
76 guid[4] = 0xfe;
77 guid[5] = mac_addr[3];
78 guid[6] = mac_addr[4];
79 guid[7] = mac_addr[5];
80}
fe2caefc
PP
81static enum rdma_link_layer ocrdma_link_layer(struct ib_device *device,
82 u8 port_num)
83{
84 return IB_LINK_LAYER_ETHERNET;
85}
86
7738613e
IW
87static int ocrdma_port_immutable(struct ib_device *ibdev, u8 port_num,
88 struct ib_port_immutable *immutable)
89{
90 struct ib_port_attr attr;
e1614869 91 struct ocrdma_dev *dev;
7738613e
IW
92 int err;
93
e1614869 94 dev = get_ocrdma_dev(ibdev);
c4550c63
OG
95 immutable->core_cap_flags = RDMA_CORE_PORT_IBA_ROCE;
96 if (ocrdma_is_udp_encap_supported(dev))
97 immutable->core_cap_flags |= RDMA_CORE_CAP_PROT_ROCE_UDP_ENCAP;
98
99 err = ib_query_port(ibdev, port_num, &attr);
7738613e
IW
100 if (err)
101 return err;
102
103 immutable->pkey_tbl_len = attr.pkey_tbl_len;
104 immutable->gid_tbl_len = attr.gid_tbl_len;
337877a4 105 immutable->max_mad_size = IB_MGMT_MAD_SIZE;
7738613e
IW
106
107 return 0;
108}
109
9abb0d1b 110static void get_dev_fw_str(struct ib_device *device, char *str)
bd395005
IW
111{
112 struct ocrdma_dev *dev = get_ocrdma_dev(device);
113
9abb0d1b 114 snprintf(str, IB_FW_VERSION_NAME_MAX, "%s", &dev->attr.fw_ver[0]);
bd395005
IW
115}
116
abe3afac 117static int ocrdma_register_device(struct ocrdma_dev *dev)
fe2caefc
PP
118{
119 strlcpy(dev->ibdev.name, "ocrdma%d", IB_DEVICE_NAME_MAX);
120 ocrdma_get_guid(dev, (u8 *)&dev->ibdev.node_guid);
bd99fdea 121 BUILD_BUG_ON(sizeof(OCRDMA_NODE_DESC) > IB_DEVICE_NODE_DESC_MAX);
fe2caefc
PP
122 memcpy(dev->ibdev.node_desc, OCRDMA_NODE_DESC,
123 sizeof(OCRDMA_NODE_DESC));
124 dev->ibdev.owner = THIS_MODULE;
38754397 125 dev->ibdev.uverbs_abi_ver = OCRDMA_ABI_VERSION;
fe2caefc
PP
126 dev->ibdev.uverbs_cmd_mask =
127 OCRDMA_UVERBS(GET_CONTEXT) |
128 OCRDMA_UVERBS(QUERY_DEVICE) |
129 OCRDMA_UVERBS(QUERY_PORT) |
130 OCRDMA_UVERBS(ALLOC_PD) |
131 OCRDMA_UVERBS(DEALLOC_PD) |
132 OCRDMA_UVERBS(REG_MR) |
133 OCRDMA_UVERBS(DEREG_MR) |
134 OCRDMA_UVERBS(CREATE_COMP_CHANNEL) |
135 OCRDMA_UVERBS(CREATE_CQ) |
136 OCRDMA_UVERBS(RESIZE_CQ) |
137 OCRDMA_UVERBS(DESTROY_CQ) |
138 OCRDMA_UVERBS(REQ_NOTIFY_CQ) |
139 OCRDMA_UVERBS(CREATE_QP) |
140 OCRDMA_UVERBS(MODIFY_QP) |
141 OCRDMA_UVERBS(QUERY_QP) |
142 OCRDMA_UVERBS(DESTROY_QP) |
143 OCRDMA_UVERBS(POLL_CQ) |
144 OCRDMA_UVERBS(POST_SEND) |
145 OCRDMA_UVERBS(POST_RECV);
146
147 dev->ibdev.uverbs_cmd_mask |=
148 OCRDMA_UVERBS(CREATE_AH) |
149 OCRDMA_UVERBS(MODIFY_AH) |
150 OCRDMA_UVERBS(QUERY_AH) |
151 OCRDMA_UVERBS(DESTROY_AH);
152
153 dev->ibdev.node_type = RDMA_NODE_IB_CA;
154 dev->ibdev.phys_port_cnt = 1;
0c0eacdc 155 dev->ibdev.num_comp_vectors = dev->eq_cnt;
fe2caefc
PP
156
157 /* mandatory verbs. */
158 dev->ibdev.query_device = ocrdma_query_device;
159 dev->ibdev.query_port = ocrdma_query_port;
160 dev->ibdev.modify_port = ocrdma_modify_port;
cc36929e 161 dev->ibdev.get_netdev = ocrdma_get_netdev;
fe2caefc
PP
162 dev->ibdev.get_link_layer = ocrdma_link_layer;
163 dev->ibdev.alloc_pd = ocrdma_alloc_pd;
164 dev->ibdev.dealloc_pd = ocrdma_dealloc_pd;
165
166 dev->ibdev.create_cq = ocrdma_create_cq;
167 dev->ibdev.destroy_cq = ocrdma_destroy_cq;
168 dev->ibdev.resize_cq = ocrdma_resize_cq;
169
170 dev->ibdev.create_qp = ocrdma_create_qp;
171 dev->ibdev.modify_qp = ocrdma_modify_qp;
172 dev->ibdev.query_qp = ocrdma_query_qp;
173 dev->ibdev.destroy_qp = ocrdma_destroy_qp;
174
175 dev->ibdev.query_pkey = ocrdma_query_pkey;
176 dev->ibdev.create_ah = ocrdma_create_ah;
177 dev->ibdev.destroy_ah = ocrdma_destroy_ah;
178 dev->ibdev.query_ah = ocrdma_query_ah;
179 dev->ibdev.modify_ah = ocrdma_modify_ah;
180
181 dev->ibdev.poll_cq = ocrdma_poll_cq;
182 dev->ibdev.post_send = ocrdma_post_send;
183 dev->ibdev.post_recv = ocrdma_post_recv;
184 dev->ibdev.req_notify_cq = ocrdma_arm_cq;
185
186 dev->ibdev.get_dma_mr = ocrdma_get_dma_mr;
187 dev->ibdev.dereg_mr = ocrdma_dereg_mr;
188 dev->ibdev.reg_user_mr = ocrdma_reg_user_mr;
189
cacb7d59 190 dev->ibdev.alloc_mr = ocrdma_alloc_mr;
2eaa1c56 191 dev->ibdev.map_mr_sg = ocrdma_map_mr_sg;
7c33880c 192
fe2caefc
PP
193 /* mandatory to support user space verbs consumer. */
194 dev->ibdev.alloc_ucontext = ocrdma_alloc_ucontext;
195 dev->ibdev.dealloc_ucontext = ocrdma_dealloc_ucontext;
196 dev->ibdev.mmap = ocrdma_mmap;
e6a73f26 197 dev->ibdev.dev.parent = &dev->nic_info.pdev->dev;
fe2caefc
PP
198
199 dev->ibdev.process_mad = ocrdma_process_mad;
7738613e 200 dev->ibdev.get_port_immutable = ocrdma_port_immutable;
bd395005 201 dev->ibdev.get_dev_fw_str = get_dev_fw_str;
fe2caefc 202
21c3391a 203 if (ocrdma_get_asic_type(dev) == OCRDMA_ASIC_GEN_SKH_R) {
fe2caefc
PP
204 dev->ibdev.uverbs_cmd_mask |=
205 OCRDMA_UVERBS(CREATE_SRQ) |
206 OCRDMA_UVERBS(MODIFY_SRQ) |
207 OCRDMA_UVERBS(QUERY_SRQ) |
208 OCRDMA_UVERBS(DESTROY_SRQ) |
209 OCRDMA_UVERBS(POST_SRQ_RECV);
210
211 dev->ibdev.create_srq = ocrdma_create_srq;
212 dev->ibdev.modify_srq = ocrdma_modify_srq;
213 dev->ibdev.query_srq = ocrdma_query_srq;
214 dev->ibdev.destroy_srq = ocrdma_destroy_srq;
215 dev->ibdev.post_srq_recv = ocrdma_post_srq_recv;
216 }
0ede73bc 217 dev->ibdev.driver_id = RDMA_DRIVER_OCRDMA;
fe2caefc
PP
218 return ib_register_device(&dev->ibdev, NULL);
219}
220
221static int ocrdma_alloc_resources(struct ocrdma_dev *dev)
222{
223 mutex_init(&dev->dev_lock);
6396bb22
KC
224 dev->cq_tbl = kcalloc(OCRDMA_MAX_CQ, sizeof(struct ocrdma_cq *),
225 GFP_KERNEL);
fe2caefc
PP
226 if (!dev->cq_tbl)
227 goto alloc_err;
228
229 if (dev->attr.max_qp) {
6396bb22
KC
230 dev->qp_tbl = kcalloc(OCRDMA_MAX_QP,
231 sizeof(struct ocrdma_qp *),
232 GFP_KERNEL);
fe2caefc
PP
233 if (!dev->qp_tbl)
234 goto alloc_err;
235 }
4f1df844 236
6396bb22 237 dev->stag_arr = kcalloc(OCRDMA_MAX_STAG, sizeof(u64), GFP_KERNEL);
4f1df844
SX
238 if (dev->stag_arr == NULL)
239 goto alloc_err;
240
9ba1377d
MA
241 ocrdma_alloc_pd_pool(dev);
242
fd98d896
SX
243 if (!ocrdma_alloc_stats_resources(dev)) {
244 pr_err("%s: stats resource allocation failed\n", __func__);
245 goto alloc_err;
246 }
247
fe2caefc
PP
248 spin_lock_init(&dev->av_tbl.lock);
249 spin_lock_init(&dev->flush_q_lock);
250 return 0;
251alloc_err:
ef99c4c2 252 pr_err("%s(%d) error.\n", __func__, dev->id);
fe2caefc
PP
253 return -ENOMEM;
254}
255
256static void ocrdma_free_resources(struct ocrdma_dev *dev)
257{
fd98d896 258 ocrdma_release_stats_resources(dev);
4f1df844 259 kfree(dev->stag_arr);
fe2caefc
PP
260 kfree(dev->qp_tbl);
261 kfree(dev->cq_tbl);
fe2caefc
PP
262}
263
334b8db3
SX
264/* OCRDMA sysfs interface */
265static ssize_t show_rev(struct device *device, struct device_attribute *attr,
266 char *buf)
267{
268 struct ocrdma_dev *dev = dev_get_drvdata(device);
269
270 return scnprintf(buf, PAGE_SIZE, "0x%x\n", dev->nic_info.pdev->vendor);
271}
272
4808b184
SX
273static ssize_t show_hca_type(struct device *device,
274 struct device_attribute *attr, char *buf)
275{
276 struct ocrdma_dev *dev = dev_get_drvdata(device);
277
278 return scnprintf(buf, PAGE_SIZE, "%s\n", &dev->model_number[0]);
334b8db3
SX
279}
280
281static DEVICE_ATTR(hw_rev, S_IRUGO, show_rev, NULL);
4808b184 282static DEVICE_ATTR(hca_type, S_IRUGO, show_hca_type, NULL);
334b8db3
SX
283
284static struct device_attribute *ocrdma_attributes[] = {
285 &dev_attr_hw_rev,
4808b184 286 &dev_attr_hca_type
334b8db3
SX
287};
288
289static void ocrdma_remove_sysfiles(struct ocrdma_dev *dev)
290{
291 int i;
292
293 for (i = 0; i < ARRAY_SIZE(ocrdma_attributes); i++)
294 device_remove_file(&dev->ibdev.dev, ocrdma_attributes[i]);
295}
296
fe2caefc
PP
297static struct ocrdma_dev *ocrdma_add(struct be_dev_info *dev_info)
298{
334b8db3 299 int status = 0, i;
3b1ea430 300 u8 lstate = 0;
fe2caefc
PP
301 struct ocrdma_dev *dev;
302
303 dev = (struct ocrdma_dev *)ib_alloc_device(sizeof(struct ocrdma_dev));
304 if (!dev) {
ef99c4c2 305 pr_err("Unable to allocate ib device\n");
fe2caefc
PP
306 return NULL;
307 }
308 dev->mbx_cmd = kzalloc(sizeof(struct ocrdma_mqe_emb_cmd), GFP_KERNEL);
309 if (!dev->mbx_cmd)
310 goto idr_err;
311
312 memcpy(&dev->nic_info, dev_info, sizeof(*dev_info));
cffcd59f 313 dev->id = idr_alloc(&ocrdma_dev_id, NULL, 0, 0, GFP_KERNEL);
fe2caefc
PP
314 if (dev->id < 0)
315 goto idr_err;
316
317 status = ocrdma_init_hw(dev);
318 if (status)
319 goto init_err;
320
321 status = ocrdma_alloc_resources(dev);
322 if (status)
323 goto alloc_err;
324
31dbdd9a 325 ocrdma_init_service_level(dev);
fe2caefc
PP
326 status = ocrdma_register_device(dev);
327 if (status)
328 goto alloc_err;
329
3b1ea430
DS
330 /* Query Link state and update */
331 status = ocrdma_mbx_get_link_speed(dev, NULL, &lstate);
332 if (!status)
333 ocrdma_update_link_state(dev, lstate);
334
334b8db3
SX
335 for (i = 0; i < ARRAY_SIZE(ocrdma_attributes); i++)
336 if (device_create_file(&dev->ibdev.dev, ocrdma_attributes[i]))
337 goto sysfs_err;
a51f06e1
SX
338 /* Init stats */
339 ocrdma_add_port_stats(dev);
b4dbe8d5
MA
340 /* Interrupt Moderation */
341 INIT_DELAYED_WORK(&dev->eqd_work, ocrdma_eqd_set_task);
342 schedule_delayed_work(&dev->eqd_work, msecs_to_jiffies(1000));
a51f06e1
SX
343
344 pr_info("%s %s: %s \"%s\" port %d\n",
345 dev_name(&dev->nic_info.pdev->dev), hca_name(dev),
346 port_speed_string(dev), dev->model_number,
347 dev->hba_port_num);
348 pr_info("%s ocrdma%d driver loaded successfully\n",
349 dev_name(&dev->nic_info.pdev->dev), dev->id);
fe2caefc
PP
350 return dev;
351
334b8db3
SX
352sysfs_err:
353 ocrdma_remove_sysfiles(dev);
fe2caefc
PP
354alloc_err:
355 ocrdma_free_resources(dev);
356 ocrdma_cleanup_hw(dev);
357init_err:
358 idr_remove(&ocrdma_dev_id, dev->id);
359idr_err:
360 kfree(dev->mbx_cmd);
361 ib_dealloc_device(&dev->ibdev);
ef99c4c2 362 pr_err("%s() leaving. ret=%d\n", __func__, status);
fe2caefc
PP
363 return NULL;
364}
365
5a85f5e9 366static void ocrdma_remove_free(struct ocrdma_dev *dev)
fe2caefc 367{
fe2caefc 368
fe2caefc
PP
369 idr_remove(&ocrdma_dev_id, dev->id);
370 kfree(dev->mbx_cmd);
371 ib_dealloc_device(&dev->ibdev);
372}
373
3e4d60a8
SL
374static void ocrdma_remove(struct ocrdma_dev *dev)
375{
376 /* first unregister with stack to stop all the active traffic
377 * of the registered clients.
378 */
b4dbe8d5 379 cancel_delayed_work_sync(&dev->eqd_work);
334b8db3 380 ocrdma_remove_sysfiles(dev);
3e4d60a8 381 ib_unregister_device(&dev->ibdev);
4b8180aa
MA
382
383 ocrdma_rem_port_stats(dev);
1852d1da
NG
384 ocrdma_free_resources(dev);
385 ocrdma_cleanup_hw(dev);
5a85f5e9 386 ocrdma_remove_free(dev);
3e4d60a8
SL
387}
388
3b1ea430 389static int ocrdma_dispatch_port_active(struct ocrdma_dev *dev)
fe2caefc
PP
390{
391 struct ib_event port_event;
392
393 port_event.event = IB_EVENT_PORT_ACTIVE;
394 port_event.element.port_num = 1;
395 port_event.device = &dev->ibdev;
396 ib_dispatch_event(&port_event);
397 return 0;
398}
399
3b1ea430 400static int ocrdma_dispatch_port_error(struct ocrdma_dev *dev)
fe2caefc 401{
fe2caefc 402 struct ib_event err_event;
fe2caefc
PP
403
404 err_event.event = IB_EVENT_PORT_ERR;
405 err_event.element.port_num = 1;
406 err_event.device = &dev->ibdev;
407 ib_dispatch_event(&err_event);
408 return 0;
409}
410
efe45937
DS
411static void ocrdma_shutdown(struct ocrdma_dev *dev)
412{
3b1ea430 413 ocrdma_dispatch_port_error(dev);
efe45937
DS
414 ocrdma_remove(dev);
415}
416
fe2caefc
PP
417/* event handling via NIC driver ensures that all the NIC specific
418 * initialization done before RoCE driver notifies
419 * event to stack.
420 */
421static void ocrdma_event_handler(struct ocrdma_dev *dev, u32 event)
422{
423 switch (event) {
efe45937
DS
424 case BE_DEV_SHUTDOWN:
425 ocrdma_shutdown(dev);
426 break;
3b1ea430
DS
427 default:
428 break;
2b50176d 429 }
fe2caefc
PP
430}
431
3b1ea430
DS
432void ocrdma_update_link_state(struct ocrdma_dev *dev, u8 lstate)
433{
434 if (!(dev->flags & OCRDMA_FLAGS_LINK_STATUS_INIT)) {
435 dev->flags |= OCRDMA_FLAGS_LINK_STATUS_INIT;
436 if (!lstate)
437 return;
438 }
439
440 if (!lstate)
441 ocrdma_dispatch_port_error(dev);
442 else
443 ocrdma_dispatch_port_active(dev);
444}
445
abe3afac 446static struct ocrdma_driver ocrdma_drv = {
fe2caefc
PP
447 .name = "ocrdma_driver",
448 .add = ocrdma_add,
449 .remove = ocrdma_remove,
450 .state_change_handler = ocrdma_event_handler,
b6b87d2e 451 .be_abi_version = OCRDMA_BE_ROCE_ABI_VERSION,
fe2caefc
PP
452};
453
454static int __init ocrdma_init_module(void)
455{
456 int status;
457
a51f06e1
SX
458 ocrdma_init_debugfs();
459
fe2caefc
PP
460 status = be_roce_register_driver(&ocrdma_drv);
461 if (status)
2d8f57d5 462 goto err_be_reg;
34955669 463
2d8f57d5
SX
464 return 0;
465
466err_be_reg:
cc36929e 467
fe2caefc
PP
468 return status;
469}
470
471static void __exit ocrdma_exit_module(void)
472{
473 be_roce_unregister_driver(&ocrdma_drv);
a51f06e1 474 ocrdma_rem_debugfs();
d8b2ba7c 475 idr_destroy(&ocrdma_dev_id);
fe2caefc
PP
476}
477
478module_init(ocrdma_init_module);
479module_exit(ocrdma_exit_module);