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.
34  *
35  * Contact Information:
36  * linux-drivers@emulex.com
37  *
38  * Emulex
39  * 3333 Susan Street
40  * Costa Mesa, CA 92626
41  */
42 
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>
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"
58 #include "ocrdma_stats.h"
59 #include <rdma/ocrdma-abi.h>
60 
61 MODULE_DESCRIPTION(OCRDMA_ROCE_DRV_DESC " " OCRDMA_ROCE_DRV_VERSION);
62 MODULE_AUTHOR("Emulex Corporation");
63 MODULE_LICENSE("Dual BSD/GPL");
64 
65 void ocrdma_get_guid(struct ocrdma_dev *dev, u8 *guid)
66 {
67 	u8 mac_addr[6];
68 
69 	memcpy(&mac_addr[0], &dev->nic_info.mac_addr[0], ETH_ALEN);
70 	guid[0] = mac_addr[0] ^ 2;
71 	guid[1] = mac_addr[1];
72 	guid[2] = mac_addr[2];
73 	guid[3] = 0xff;
74 	guid[4] = 0xfe;
75 	guid[5] = mac_addr[3];
76 	guid[6] = mac_addr[4];
77 	guid[7] = mac_addr[5];
78 }
79 static enum rdma_link_layer ocrdma_link_layer(struct ib_device *device,
80 					      u32 port_num)
81 {
82 	return IB_LINK_LAYER_ETHERNET;
83 }
84 
85 static int ocrdma_port_immutable(struct ib_device *ibdev, u32 port_num,
86 			         struct ib_port_immutable *immutable)
87 {
88 	struct ib_port_attr attr;
89 	struct ocrdma_dev *dev;
90 	int err;
91 
92 	dev = get_ocrdma_dev(ibdev);
93 	immutable->core_cap_flags = RDMA_CORE_PORT_IBA_ROCE;
94 	if (ocrdma_is_udp_encap_supported(dev))
95 		immutable->core_cap_flags |= RDMA_CORE_CAP_PROT_ROCE_UDP_ENCAP;
96 
97 	err = ib_query_port(ibdev, port_num, &attr);
98 	if (err)
99 		return err;
100 
101 	immutable->pkey_tbl_len = attr.pkey_tbl_len;
102 	immutable->gid_tbl_len = attr.gid_tbl_len;
103 	immutable->max_mad_size = IB_MGMT_MAD_SIZE;
104 
105 	return 0;
106 }
107 
108 static void get_dev_fw_str(struct ib_device *device, char *str)
109 {
110 	struct ocrdma_dev *dev = get_ocrdma_dev(device);
111 
112 	snprintf(str, IB_FW_VERSION_NAME_MAX, "%s", &dev->attr.fw_ver[0]);
113 }
114 
115 /* OCRDMA sysfs interface */
116 static ssize_t hw_rev_show(struct device *device,
117 			   struct device_attribute *attr, char *buf)
118 {
119 	struct ocrdma_dev *dev =
120 		rdma_device_to_drv_device(device, struct ocrdma_dev, ibdev);
121 
122 	return sysfs_emit(buf, "0x%x\n", dev->nic_info.pdev->vendor);
123 }
124 static DEVICE_ATTR_RO(hw_rev);
125 
126 static ssize_t hca_type_show(struct device *device,
127 			     struct device_attribute *attr, char *buf)
128 {
129 	struct ocrdma_dev *dev =
130 		rdma_device_to_drv_device(device, struct ocrdma_dev, ibdev);
131 
132 	return sysfs_emit(buf, "%s\n", &dev->model_number[0]);
133 }
134 static DEVICE_ATTR_RO(hca_type);
135 
136 static struct attribute *ocrdma_attributes[] = {
137 	&dev_attr_hw_rev.attr,
138 	&dev_attr_hca_type.attr,
139 	NULL
140 };
141 
142 static const struct attribute_group ocrdma_attr_group = {
143 	.attrs = ocrdma_attributes,
144 };
145 
146 static const struct ib_device_ops ocrdma_dev_ops = {
147 	.owner = THIS_MODULE,
148 	.driver_id = RDMA_DRIVER_OCRDMA,
149 	.uverbs_abi_ver = OCRDMA_ABI_VERSION,
150 
151 	.alloc_mr = ocrdma_alloc_mr,
152 	.alloc_pd = ocrdma_alloc_pd,
153 	.alloc_ucontext = ocrdma_alloc_ucontext,
154 	.create_ah = ocrdma_create_ah,
155 	.create_cq = ocrdma_create_cq,
156 	.create_qp = ocrdma_create_qp,
157 	.create_user_ah = ocrdma_create_ah,
158 	.dealloc_pd = ocrdma_dealloc_pd,
159 	.dealloc_ucontext = ocrdma_dealloc_ucontext,
160 	.dereg_mr = ocrdma_dereg_mr,
161 	.destroy_ah = ocrdma_destroy_ah,
162 	.destroy_cq = ocrdma_destroy_cq,
163 	.destroy_qp = ocrdma_destroy_qp,
164 	.device_group = &ocrdma_attr_group,
165 	.get_dev_fw_str = get_dev_fw_str,
166 	.get_dma_mr = ocrdma_get_dma_mr,
167 	.get_link_layer = ocrdma_link_layer,
168 	.get_port_immutable = ocrdma_port_immutable,
169 	.map_mr_sg = ocrdma_map_mr_sg,
170 	.mmap = ocrdma_mmap,
171 	.modify_qp = ocrdma_modify_qp,
172 	.poll_cq = ocrdma_poll_cq,
173 	.post_recv = ocrdma_post_recv,
174 	.post_send = ocrdma_post_send,
175 	.process_mad = ocrdma_process_mad,
176 	.query_ah = ocrdma_query_ah,
177 	.query_device = ocrdma_query_device,
178 	.query_pkey = ocrdma_query_pkey,
179 	.query_port = ocrdma_query_port,
180 	.query_qp = ocrdma_query_qp,
181 	.reg_user_mr = ocrdma_reg_user_mr,
182 	.req_notify_cq = ocrdma_arm_cq,
183 	.resize_cq = ocrdma_resize_cq,
184 
185 	INIT_RDMA_OBJ_SIZE(ib_ah, ocrdma_ah, ibah),
186 	INIT_RDMA_OBJ_SIZE(ib_cq, ocrdma_cq, ibcq),
187 	INIT_RDMA_OBJ_SIZE(ib_pd, ocrdma_pd, ibpd),
188 	INIT_RDMA_OBJ_SIZE(ib_qp, ocrdma_qp, ibqp),
189 	INIT_RDMA_OBJ_SIZE(ib_ucontext, ocrdma_ucontext, ibucontext),
190 };
191 
192 static const struct ib_device_ops ocrdma_dev_srq_ops = {
193 	.create_srq = ocrdma_create_srq,
194 	.destroy_srq = ocrdma_destroy_srq,
195 	.modify_srq = ocrdma_modify_srq,
196 	.post_srq_recv = ocrdma_post_srq_recv,
197 	.query_srq = ocrdma_query_srq,
198 
199 	INIT_RDMA_OBJ_SIZE(ib_srq, ocrdma_srq, ibsrq),
200 };
201 
202 static int ocrdma_register_device(struct ocrdma_dev *dev)
203 {
204 	int ret;
205 
206 	ocrdma_get_guid(dev, (u8 *)&dev->ibdev.node_guid);
207 	BUILD_BUG_ON(sizeof(OCRDMA_NODE_DESC) > IB_DEVICE_NODE_DESC_MAX);
208 	memcpy(dev->ibdev.node_desc, OCRDMA_NODE_DESC,
209 	       sizeof(OCRDMA_NODE_DESC));
210 
211 	dev->ibdev.node_type = RDMA_NODE_IB_CA;
212 	dev->ibdev.phys_port_cnt = 1;
213 	dev->ibdev.num_comp_vectors = dev->eq_cnt;
214 
215 	/* mandatory to support user space verbs consumer. */
216 	dev->ibdev.dev.parent = &dev->nic_info.pdev->dev;
217 
218 	ib_set_device_ops(&dev->ibdev, &ocrdma_dev_ops);
219 
220 	if (ocrdma_get_asic_type(dev) == OCRDMA_ASIC_GEN_SKH_R)
221 		ib_set_device_ops(&dev->ibdev, &ocrdma_dev_srq_ops);
222 
223 	ret = ib_device_set_netdev(&dev->ibdev, dev->nic_info.netdev, 1);
224 	if (ret)
225 		return ret;
226 
227 	dma_set_max_seg_size(&dev->nic_info.pdev->dev, UINT_MAX);
228 	return ib_register_device(&dev->ibdev, "ocrdma%d",
229 				  &dev->nic_info.pdev->dev);
230 }
231 
232 static int ocrdma_alloc_resources(struct ocrdma_dev *dev)
233 {
234 	mutex_init(&dev->dev_lock);
235 	dev->cq_tbl = kcalloc(OCRDMA_MAX_CQ, sizeof(struct ocrdma_cq *),
236 			      GFP_KERNEL);
237 	if (!dev->cq_tbl)
238 		goto alloc_err;
239 
240 	if (dev->attr.max_qp) {
241 		dev->qp_tbl = kcalloc(OCRDMA_MAX_QP,
242 				      sizeof(struct ocrdma_qp *),
243 				      GFP_KERNEL);
244 		if (!dev->qp_tbl)
245 			goto alloc_err;
246 	}
247 
248 	dev->stag_arr = kcalloc(OCRDMA_MAX_STAG, sizeof(u64), GFP_KERNEL);
249 	if (dev->stag_arr == NULL)
250 		goto alloc_err;
251 
252 	ocrdma_alloc_pd_pool(dev);
253 
254 	if (!ocrdma_alloc_stats_resources(dev)) {
255 		pr_err("%s: stats resource allocation failed\n", __func__);
256 		goto alloc_err;
257 	}
258 
259 	spin_lock_init(&dev->av_tbl.lock);
260 	spin_lock_init(&dev->flush_q_lock);
261 	return 0;
262 alloc_err:
263 	pr_err("%s(%d) error.\n", __func__, dev->id);
264 	return -ENOMEM;
265 }
266 
267 static void ocrdma_free_resources(struct ocrdma_dev *dev)
268 {
269 	ocrdma_release_stats_resources(dev);
270 	kfree(dev->stag_arr);
271 	kfree(dev->qp_tbl);
272 	kfree(dev->cq_tbl);
273 }
274 
275 static struct ocrdma_dev *ocrdma_add(struct be_dev_info *dev_info)
276 {
277 	int status = 0;
278 	u8 lstate = 0;
279 	struct ocrdma_dev *dev;
280 
281 	dev = ib_alloc_device(ocrdma_dev, ibdev);
282 	if (!dev) {
283 		pr_err("Unable to allocate ib device\n");
284 		return NULL;
285 	}
286 
287 	dev->mbx_cmd = kzalloc(sizeof(struct ocrdma_mqe_emb_cmd), GFP_KERNEL);
288 	if (!dev->mbx_cmd)
289 		goto init_err;
290 
291 	memcpy(&dev->nic_info, dev_info, sizeof(*dev_info));
292 	dev->id = PCI_FUNC(dev->nic_info.pdev->devfn);
293 	status = ocrdma_init_hw(dev);
294 	if (status)
295 		goto init_err;
296 
297 	status = ocrdma_alloc_resources(dev);
298 	if (status)
299 		goto alloc_err;
300 
301 	ocrdma_init_service_level(dev);
302 	status = ocrdma_register_device(dev);
303 	if (status)
304 		goto alloc_err;
305 
306 	/* Query Link state and update */
307 	status = ocrdma_mbx_get_link_speed(dev, NULL, &lstate);
308 	if (!status)
309 		ocrdma_update_link_state(dev, lstate);
310 
311 	/* Init stats */
312 	ocrdma_add_port_stats(dev);
313 	/* Interrupt Moderation */
314 	INIT_DELAYED_WORK(&dev->eqd_work, ocrdma_eqd_set_task);
315 	schedule_delayed_work(&dev->eqd_work, msecs_to_jiffies(1000));
316 
317 	pr_info("%s %s: %s \"%s\" port %d\n",
318 		dev_name(&dev->nic_info.pdev->dev), hca_name(dev),
319 		port_speed_string(dev), dev->model_number,
320 		dev->hba_port_num);
321 	pr_info("%s ocrdma%d driver loaded successfully\n",
322 		dev_name(&dev->nic_info.pdev->dev), dev->id);
323 	return dev;
324 
325 alloc_err:
326 	ocrdma_free_resources(dev);
327 	ocrdma_cleanup_hw(dev);
328 init_err:
329 	kfree(dev->mbx_cmd);
330 	ib_dealloc_device(&dev->ibdev);
331 	pr_err("%s() leaving. ret=%d\n", __func__, status);
332 	return NULL;
333 }
334 
335 static void ocrdma_remove_free(struct ocrdma_dev *dev)
336 {
337 
338 	kfree(dev->mbx_cmd);
339 	ib_dealloc_device(&dev->ibdev);
340 }
341 
342 static void ocrdma_remove(struct ocrdma_dev *dev)
343 {
344 	/* first unregister with stack to stop all the active traffic
345 	 * of the registered clients.
346 	 */
347 	cancel_delayed_work_sync(&dev->eqd_work);
348 	ib_unregister_device(&dev->ibdev);
349 
350 	ocrdma_rem_port_stats(dev);
351 	ocrdma_free_resources(dev);
352 	ocrdma_cleanup_hw(dev);
353 	ocrdma_remove_free(dev);
354 }
355 
356 static int ocrdma_dispatch_port_active(struct ocrdma_dev *dev)
357 {
358 	struct ib_event port_event;
359 
360 	port_event.event = IB_EVENT_PORT_ACTIVE;
361 	port_event.element.port_num = 1;
362 	port_event.device = &dev->ibdev;
363 	ib_dispatch_event(&port_event);
364 	return 0;
365 }
366 
367 static int ocrdma_dispatch_port_error(struct ocrdma_dev *dev)
368 {
369 	struct ib_event err_event;
370 
371 	err_event.event = IB_EVENT_PORT_ERR;
372 	err_event.element.port_num = 1;
373 	err_event.device = &dev->ibdev;
374 	ib_dispatch_event(&err_event);
375 	return 0;
376 }
377 
378 static void ocrdma_shutdown(struct ocrdma_dev *dev)
379 {
380 	ocrdma_dispatch_port_error(dev);
381 	ocrdma_remove(dev);
382 }
383 
384 /* event handling via NIC driver ensures that all the NIC specific
385  * initialization done before RoCE driver notifies
386  * event to stack.
387  */
388 static void ocrdma_event_handler(struct ocrdma_dev *dev, u32 event)
389 {
390 	switch (event) {
391 	case BE_DEV_SHUTDOWN:
392 		ocrdma_shutdown(dev);
393 		break;
394 	default:
395 		break;
396 	}
397 }
398 
399 void ocrdma_update_link_state(struct ocrdma_dev *dev, u8 lstate)
400 {
401 	if (!(dev->flags & OCRDMA_FLAGS_LINK_STATUS_INIT)) {
402 		dev->flags |= OCRDMA_FLAGS_LINK_STATUS_INIT;
403 		if (!lstate)
404 			return;
405 	}
406 
407 	if (!lstate)
408 		ocrdma_dispatch_port_error(dev);
409 	else
410 		ocrdma_dispatch_port_active(dev);
411 }
412 
413 static struct ocrdma_driver ocrdma_drv = {
414 	.name			= "ocrdma_driver",
415 	.add			= ocrdma_add,
416 	.remove			= ocrdma_remove,
417 	.state_change_handler	= ocrdma_event_handler,
418 	.be_abi_version		= OCRDMA_BE_ROCE_ABI_VERSION,
419 };
420 
421 static int __init ocrdma_init_module(void)
422 {
423 	int status;
424 
425 	ocrdma_init_debugfs();
426 
427 	status = be_roce_register_driver(&ocrdma_drv);
428 	if (status)
429 		goto err_be_reg;
430 
431 	return 0;
432 
433 err_be_reg:
434 
435 	return status;
436 }
437 
438 static void __exit ocrdma_exit_module(void)
439 {
440 	be_roce_unregister_driver(&ocrdma_drv);
441 	ocrdma_rem_debugfs();
442 }
443 
444 module_init(ocrdma_init_module);
445 module_exit(ocrdma_exit_module);
446