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.
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:
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
16 * - Redistributions of source code must retain the above copyright notice,
17 * this list of conditions and the following disclaimer.
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.
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.
35 * Contact Information:
36 * linux-drivers@emulex.com
40 * Costa Mesa, CA 92626
43 #include <linux/module.h>
44 #include <linux/idr.h>
45 #include <rdma/ib_verbs.h>
46 #include <rdma/ib_user_verbs.h>
47 #include <rdma/ib_addr.h>
48 #include <rdma/ib_mad.h>
50 #include <linux/netdevice.h>
51 #include <net/addrconf.h>
54 #include "ocrdma_verbs.h"
55 #include "ocrdma_ah.h"
57 #include "ocrdma_hw.h"
58 #include "ocrdma_stats.h"
59 #include "ocrdma_abi.h"
61 MODULE_VERSION(OCRDMA_ROCE_DRV_VERSION
);
62 MODULE_DESCRIPTION(OCRDMA_ROCE_DRV_DESC
" " OCRDMA_ROCE_DRV_VERSION
);
63 MODULE_AUTHOR("Emulex Corporation");
64 MODULE_LICENSE("Dual BSD/GPL");
66 static DEFINE_IDR(ocrdma_dev_id
);
68 void ocrdma_get_guid(struct ocrdma_dev
*dev
, u8
*guid
)
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];
78 guid
[5] = mac_addr
[3];
79 guid
[6] = mac_addr
[4];
80 guid
[7] = mac_addr
[5];
82 static enum rdma_link_layer
ocrdma_link_layer(struct ib_device
*device
,
85 return IB_LINK_LAYER_ETHERNET
;
88 static int ocrdma_port_immutable(struct ib_device
*ibdev
, u8 port_num
,
89 struct ib_port_immutable
*immutable
)
91 struct ib_port_attr attr
;
92 struct ocrdma_dev
*dev
;
95 dev
= get_ocrdma_dev(ibdev
);
96 err
= ocrdma_query_port(ibdev
, port_num
, &attr
);
100 immutable
->pkey_tbl_len
= attr
.pkey_tbl_len
;
101 immutable
->gid_tbl_len
= attr
.gid_tbl_len
;
102 immutable
->core_cap_flags
= RDMA_CORE_PORT_IBA_ROCE
;
103 if (ocrdma_is_udp_encap_supported(dev
))
104 immutable
->core_cap_flags
|= RDMA_CORE_CAP_PROT_ROCE_UDP_ENCAP
;
105 immutable
->max_mad_size
= IB_MGMT_MAD_SIZE
;
110 static int ocrdma_register_device(struct ocrdma_dev
*dev
)
112 strlcpy(dev
->ibdev
.name
, "ocrdma%d", IB_DEVICE_NAME_MAX
);
113 ocrdma_get_guid(dev
, (u8
*)&dev
->ibdev
.node_guid
);
114 memcpy(dev
->ibdev
.node_desc
, OCRDMA_NODE_DESC
,
115 sizeof(OCRDMA_NODE_DESC
));
116 dev
->ibdev
.owner
= THIS_MODULE
;
117 dev
->ibdev
.uverbs_abi_ver
= OCRDMA_ABI_VERSION
;
118 dev
->ibdev
.uverbs_cmd_mask
=
119 OCRDMA_UVERBS(GET_CONTEXT
) |
120 OCRDMA_UVERBS(QUERY_DEVICE
) |
121 OCRDMA_UVERBS(QUERY_PORT
) |
122 OCRDMA_UVERBS(ALLOC_PD
) |
123 OCRDMA_UVERBS(DEALLOC_PD
) |
124 OCRDMA_UVERBS(REG_MR
) |
125 OCRDMA_UVERBS(DEREG_MR
) |
126 OCRDMA_UVERBS(CREATE_COMP_CHANNEL
) |
127 OCRDMA_UVERBS(CREATE_CQ
) |
128 OCRDMA_UVERBS(RESIZE_CQ
) |
129 OCRDMA_UVERBS(DESTROY_CQ
) |
130 OCRDMA_UVERBS(REQ_NOTIFY_CQ
) |
131 OCRDMA_UVERBS(CREATE_QP
) |
132 OCRDMA_UVERBS(MODIFY_QP
) |
133 OCRDMA_UVERBS(QUERY_QP
) |
134 OCRDMA_UVERBS(DESTROY_QP
) |
135 OCRDMA_UVERBS(POLL_CQ
) |
136 OCRDMA_UVERBS(POST_SEND
) |
137 OCRDMA_UVERBS(POST_RECV
);
139 dev
->ibdev
.uverbs_cmd_mask
|=
140 OCRDMA_UVERBS(CREATE_AH
) |
141 OCRDMA_UVERBS(MODIFY_AH
) |
142 OCRDMA_UVERBS(QUERY_AH
) |
143 OCRDMA_UVERBS(DESTROY_AH
);
145 dev
->ibdev
.node_type
= RDMA_NODE_IB_CA
;
146 dev
->ibdev
.phys_port_cnt
= 1;
147 dev
->ibdev
.num_comp_vectors
= dev
->eq_cnt
;
149 /* mandatory verbs. */
150 dev
->ibdev
.query_device
= ocrdma_query_device
;
151 dev
->ibdev
.query_port
= ocrdma_query_port
;
152 dev
->ibdev
.modify_port
= ocrdma_modify_port
;
153 dev
->ibdev
.query_gid
= ocrdma_query_gid
;
154 dev
->ibdev
.get_netdev
= ocrdma_get_netdev
;
155 dev
->ibdev
.add_gid
= ocrdma_add_gid
;
156 dev
->ibdev
.del_gid
= ocrdma_del_gid
;
157 dev
->ibdev
.get_link_layer
= ocrdma_link_layer
;
158 dev
->ibdev
.alloc_pd
= ocrdma_alloc_pd
;
159 dev
->ibdev
.dealloc_pd
= ocrdma_dealloc_pd
;
161 dev
->ibdev
.create_cq
= ocrdma_create_cq
;
162 dev
->ibdev
.destroy_cq
= ocrdma_destroy_cq
;
163 dev
->ibdev
.resize_cq
= ocrdma_resize_cq
;
165 dev
->ibdev
.create_qp
= ocrdma_create_qp
;
166 dev
->ibdev
.modify_qp
= ocrdma_modify_qp
;
167 dev
->ibdev
.query_qp
= ocrdma_query_qp
;
168 dev
->ibdev
.destroy_qp
= ocrdma_destroy_qp
;
170 dev
->ibdev
.query_pkey
= ocrdma_query_pkey
;
171 dev
->ibdev
.create_ah
= ocrdma_create_ah
;
172 dev
->ibdev
.destroy_ah
= ocrdma_destroy_ah
;
173 dev
->ibdev
.query_ah
= ocrdma_query_ah
;
174 dev
->ibdev
.modify_ah
= ocrdma_modify_ah
;
176 dev
->ibdev
.poll_cq
= ocrdma_poll_cq
;
177 dev
->ibdev
.post_send
= ocrdma_post_send
;
178 dev
->ibdev
.post_recv
= ocrdma_post_recv
;
179 dev
->ibdev
.req_notify_cq
= ocrdma_arm_cq
;
181 dev
->ibdev
.get_dma_mr
= ocrdma_get_dma_mr
;
182 dev
->ibdev
.dereg_mr
= ocrdma_dereg_mr
;
183 dev
->ibdev
.reg_user_mr
= ocrdma_reg_user_mr
;
185 dev
->ibdev
.alloc_mr
= ocrdma_alloc_mr
;
186 dev
->ibdev
.map_mr_sg
= ocrdma_map_mr_sg
;
188 /* mandatory to support user space verbs consumer. */
189 dev
->ibdev
.alloc_ucontext
= ocrdma_alloc_ucontext
;
190 dev
->ibdev
.dealloc_ucontext
= ocrdma_dealloc_ucontext
;
191 dev
->ibdev
.mmap
= ocrdma_mmap
;
192 dev
->ibdev
.dma_device
= &dev
->nic_info
.pdev
->dev
;
194 dev
->ibdev
.process_mad
= ocrdma_process_mad
;
195 dev
->ibdev
.get_port_immutable
= ocrdma_port_immutable
;
197 if (ocrdma_get_asic_type(dev
) == OCRDMA_ASIC_GEN_SKH_R
) {
198 dev
->ibdev
.uverbs_cmd_mask
|=
199 OCRDMA_UVERBS(CREATE_SRQ
) |
200 OCRDMA_UVERBS(MODIFY_SRQ
) |
201 OCRDMA_UVERBS(QUERY_SRQ
) |
202 OCRDMA_UVERBS(DESTROY_SRQ
) |
203 OCRDMA_UVERBS(POST_SRQ_RECV
);
205 dev
->ibdev
.create_srq
= ocrdma_create_srq
;
206 dev
->ibdev
.modify_srq
= ocrdma_modify_srq
;
207 dev
->ibdev
.query_srq
= ocrdma_query_srq
;
208 dev
->ibdev
.destroy_srq
= ocrdma_destroy_srq
;
209 dev
->ibdev
.post_srq_recv
= ocrdma_post_srq_recv
;
211 return ib_register_device(&dev
->ibdev
, NULL
);
214 static int ocrdma_alloc_resources(struct ocrdma_dev
*dev
)
216 mutex_init(&dev
->dev_lock
);
217 dev
->cq_tbl
= kzalloc(sizeof(struct ocrdma_cq
*) *
218 OCRDMA_MAX_CQ
, GFP_KERNEL
);
222 if (dev
->attr
.max_qp
) {
223 dev
->qp_tbl
= kzalloc(sizeof(struct ocrdma_qp
*) *
224 OCRDMA_MAX_QP
, GFP_KERNEL
);
229 dev
->stag_arr
= kzalloc(sizeof(u64
) * OCRDMA_MAX_STAG
, GFP_KERNEL
);
230 if (dev
->stag_arr
== NULL
)
233 ocrdma_alloc_pd_pool(dev
);
235 if (!ocrdma_alloc_stats_resources(dev
)) {
236 pr_err("%s: stats resource allocation failed\n", __func__
);
240 spin_lock_init(&dev
->av_tbl
.lock
);
241 spin_lock_init(&dev
->flush_q_lock
);
244 pr_err("%s(%d) error.\n", __func__
, dev
->id
);
248 static void ocrdma_free_resources(struct ocrdma_dev
*dev
)
250 ocrdma_release_stats_resources(dev
);
251 kfree(dev
->stag_arr
);
256 /* OCRDMA sysfs interface */
257 static ssize_t
show_rev(struct device
*device
, struct device_attribute
*attr
,
260 struct ocrdma_dev
*dev
= dev_get_drvdata(device
);
262 return scnprintf(buf
, PAGE_SIZE
, "0x%x\n", dev
->nic_info
.pdev
->vendor
);
265 static ssize_t
show_fw_ver(struct device
*device
, struct device_attribute
*attr
,
268 struct ocrdma_dev
*dev
= dev_get_drvdata(device
);
270 return scnprintf(buf
, PAGE_SIZE
, "%s\n", &dev
->attr
.fw_ver
[0]);
273 static ssize_t
show_hca_type(struct device
*device
,
274 struct device_attribute
*attr
, char *buf
)
276 struct ocrdma_dev
*dev
= dev_get_drvdata(device
);
278 return scnprintf(buf
, PAGE_SIZE
, "%s\n", &dev
->model_number
[0]);
281 static DEVICE_ATTR(hw_rev
, S_IRUGO
, show_rev
, NULL
);
282 static DEVICE_ATTR(fw_ver
, S_IRUGO
, show_fw_ver
, NULL
);
283 static DEVICE_ATTR(hca_type
, S_IRUGO
, show_hca_type
, NULL
);
285 static struct device_attribute
*ocrdma_attributes
[] = {
291 static void ocrdma_remove_sysfiles(struct ocrdma_dev
*dev
)
295 for (i
= 0; i
< ARRAY_SIZE(ocrdma_attributes
); i
++)
296 device_remove_file(&dev
->ibdev
.dev
, ocrdma_attributes
[i
]);
299 static struct ocrdma_dev
*ocrdma_add(struct be_dev_info
*dev_info
)
303 struct ocrdma_dev
*dev
;
305 dev
= (struct ocrdma_dev
*)ib_alloc_device(sizeof(struct ocrdma_dev
));
307 pr_err("Unable to allocate ib device\n");
310 dev
->mbx_cmd
= kzalloc(sizeof(struct ocrdma_mqe_emb_cmd
), GFP_KERNEL
);
314 memcpy(&dev
->nic_info
, dev_info
, sizeof(*dev_info
));
315 dev
->id
= idr_alloc(&ocrdma_dev_id
, NULL
, 0, 0, GFP_KERNEL
);
319 status
= ocrdma_init_hw(dev
);
323 status
= ocrdma_alloc_resources(dev
);
327 ocrdma_init_service_level(dev
);
328 status
= ocrdma_register_device(dev
);
332 /* Query Link state and update */
333 status
= ocrdma_mbx_get_link_speed(dev
, NULL
, &lstate
);
335 ocrdma_update_link_state(dev
, lstate
);
337 for (i
= 0; i
< ARRAY_SIZE(ocrdma_attributes
); i
++)
338 if (device_create_file(&dev
->ibdev
.dev
, ocrdma_attributes
[i
]))
341 ocrdma_add_port_stats(dev
);
342 /* Interrupt Moderation */
343 INIT_DELAYED_WORK(&dev
->eqd_work
, ocrdma_eqd_set_task
);
344 schedule_delayed_work(&dev
->eqd_work
, msecs_to_jiffies(1000));
346 pr_info("%s %s: %s \"%s\" port %d\n",
347 dev_name(&dev
->nic_info
.pdev
->dev
), hca_name(dev
),
348 port_speed_string(dev
), dev
->model_number
,
350 pr_info("%s ocrdma%d driver loaded successfully\n",
351 dev_name(&dev
->nic_info
.pdev
->dev
), dev
->id
);
355 ocrdma_remove_sysfiles(dev
);
357 ocrdma_free_resources(dev
);
358 ocrdma_cleanup_hw(dev
);
360 idr_remove(&ocrdma_dev_id
, dev
->id
);
363 ib_dealloc_device(&dev
->ibdev
);
364 pr_err("%s() leaving. ret=%d\n", __func__
, status
);
368 static void ocrdma_remove_free(struct ocrdma_dev
*dev
)
371 idr_remove(&ocrdma_dev_id
, dev
->id
);
373 ib_dealloc_device(&dev
->ibdev
);
376 static void ocrdma_remove(struct ocrdma_dev
*dev
)
378 /* first unregister with stack to stop all the active traffic
379 * of the registered clients.
381 cancel_delayed_work_sync(&dev
->eqd_work
);
382 ocrdma_remove_sysfiles(dev
);
383 ib_unregister_device(&dev
->ibdev
);
385 ocrdma_rem_port_stats(dev
);
386 ocrdma_free_resources(dev
);
387 ocrdma_cleanup_hw(dev
);
388 ocrdma_remove_free(dev
);
391 static int ocrdma_dispatch_port_active(struct ocrdma_dev
*dev
)
393 struct ib_event port_event
;
395 port_event
.event
= IB_EVENT_PORT_ACTIVE
;
396 port_event
.element
.port_num
= 1;
397 port_event
.device
= &dev
->ibdev
;
398 ib_dispatch_event(&port_event
);
402 static int ocrdma_dispatch_port_error(struct ocrdma_dev
*dev
)
404 struct ib_event err_event
;
406 err_event
.event
= IB_EVENT_PORT_ERR
;
407 err_event
.element
.port_num
= 1;
408 err_event
.device
= &dev
->ibdev
;
409 ib_dispatch_event(&err_event
);
413 static void ocrdma_shutdown(struct ocrdma_dev
*dev
)
415 ocrdma_dispatch_port_error(dev
);
419 /* event handling via NIC driver ensures that all the NIC specific
420 * initialization done before RoCE driver notifies
423 static void ocrdma_event_handler(struct ocrdma_dev
*dev
, u32 event
)
426 case BE_DEV_SHUTDOWN
:
427 ocrdma_shutdown(dev
);
434 void ocrdma_update_link_state(struct ocrdma_dev
*dev
, u8 lstate
)
436 if (!(dev
->flags
& OCRDMA_FLAGS_LINK_STATUS_INIT
)) {
437 dev
->flags
|= OCRDMA_FLAGS_LINK_STATUS_INIT
;
443 ocrdma_dispatch_port_error(dev
);
445 ocrdma_dispatch_port_active(dev
);
448 static struct ocrdma_driver ocrdma_drv
= {
449 .name
= "ocrdma_driver",
451 .remove
= ocrdma_remove
,
452 .state_change_handler
= ocrdma_event_handler
,
453 .be_abi_version
= OCRDMA_BE_ROCE_ABI_VERSION
,
456 static int __init
ocrdma_init_module(void)
460 ocrdma_init_debugfs();
462 status
= be_roce_register_driver(&ocrdma_drv
);
473 static void __exit
ocrdma_exit_module(void)
475 be_roce_unregister_driver(&ocrdma_drv
);
476 ocrdma_rem_debugfs();
477 idr_destroy(&ocrdma_dev_id
);
480 module_init(ocrdma_init_module
);
481 module_exit(ocrdma_exit_module
);