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 "ocrdma_abi.h"
60 
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");
65 
66 static LIST_HEAD(ocrdma_dev_list);
67 static DEFINE_SPINLOCK(ocrdma_devlist_lock);
68 static DEFINE_IDR(ocrdma_dev_id);
69 
70 void ocrdma_get_guid(struct ocrdma_dev *dev, u8 *guid)
71 {
72 	u8 mac_addr[6];
73 
74 	memcpy(&mac_addr[0], &dev->nic_info.mac_addr[0], ETH_ALEN);
75 	guid[0] = mac_addr[0] ^ 2;
76 	guid[1] = mac_addr[1];
77 	guid[2] = mac_addr[2];
78 	guid[3] = 0xff;
79 	guid[4] = 0xfe;
80 	guid[5] = mac_addr[3];
81 	guid[6] = mac_addr[4];
82 	guid[7] = mac_addr[5];
83 }
84 static enum rdma_link_layer ocrdma_link_layer(struct ib_device *device,
85 					      u8 port_num)
86 {
87 	return IB_LINK_LAYER_ETHERNET;
88 }
89 
90 static int ocrdma_port_immutable(struct ib_device *ibdev, u8 port_num,
91 			         struct ib_port_immutable *immutable)
92 {
93 	struct ib_port_attr attr;
94 	int err;
95 
96 	err = ocrdma_query_port(ibdev, port_num, &attr);
97 	if (err)
98 		return err;
99 
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 	immutable->max_mad_size = IB_MGMT_MAD_SIZE;
104 
105 	return 0;
106 }
107 
108 static int ocrdma_register_device(struct ocrdma_dev *dev)
109 {
110 	strlcpy(dev->ibdev.name, "ocrdma%d", IB_DEVICE_NAME_MAX);
111 	ocrdma_get_guid(dev, (u8 *)&dev->ibdev.node_guid);
112 	memcpy(dev->ibdev.node_desc, OCRDMA_NODE_DESC,
113 	       sizeof(OCRDMA_NODE_DESC));
114 	dev->ibdev.owner = THIS_MODULE;
115 	dev->ibdev.uverbs_abi_ver = OCRDMA_ABI_VERSION;
116 	dev->ibdev.uverbs_cmd_mask =
117 	    OCRDMA_UVERBS(GET_CONTEXT) |
118 	    OCRDMA_UVERBS(QUERY_DEVICE) |
119 	    OCRDMA_UVERBS(QUERY_PORT) |
120 	    OCRDMA_UVERBS(ALLOC_PD) |
121 	    OCRDMA_UVERBS(DEALLOC_PD) |
122 	    OCRDMA_UVERBS(REG_MR) |
123 	    OCRDMA_UVERBS(DEREG_MR) |
124 	    OCRDMA_UVERBS(CREATE_COMP_CHANNEL) |
125 	    OCRDMA_UVERBS(CREATE_CQ) |
126 	    OCRDMA_UVERBS(RESIZE_CQ) |
127 	    OCRDMA_UVERBS(DESTROY_CQ) |
128 	    OCRDMA_UVERBS(REQ_NOTIFY_CQ) |
129 	    OCRDMA_UVERBS(CREATE_QP) |
130 	    OCRDMA_UVERBS(MODIFY_QP) |
131 	    OCRDMA_UVERBS(QUERY_QP) |
132 	    OCRDMA_UVERBS(DESTROY_QP) |
133 	    OCRDMA_UVERBS(POLL_CQ) |
134 	    OCRDMA_UVERBS(POST_SEND) |
135 	    OCRDMA_UVERBS(POST_RECV);
136 
137 	dev->ibdev.uverbs_cmd_mask |=
138 	    OCRDMA_UVERBS(CREATE_AH) |
139 	     OCRDMA_UVERBS(MODIFY_AH) |
140 	     OCRDMA_UVERBS(QUERY_AH) |
141 	     OCRDMA_UVERBS(DESTROY_AH);
142 
143 	dev->ibdev.node_type = RDMA_NODE_IB_CA;
144 	dev->ibdev.phys_port_cnt = 1;
145 	dev->ibdev.num_comp_vectors = dev->eq_cnt;
146 
147 	/* mandatory verbs. */
148 	dev->ibdev.query_device = ocrdma_query_device;
149 	dev->ibdev.query_port = ocrdma_query_port;
150 	dev->ibdev.modify_port = ocrdma_modify_port;
151 	dev->ibdev.query_gid = ocrdma_query_gid;
152 	dev->ibdev.get_netdev = ocrdma_get_netdev;
153 	dev->ibdev.add_gid = ocrdma_add_gid;
154 	dev->ibdev.del_gid = ocrdma_del_gid;
155 	dev->ibdev.get_link_layer = ocrdma_link_layer;
156 	dev->ibdev.alloc_pd = ocrdma_alloc_pd;
157 	dev->ibdev.dealloc_pd = ocrdma_dealloc_pd;
158 
159 	dev->ibdev.create_cq = ocrdma_create_cq;
160 	dev->ibdev.destroy_cq = ocrdma_destroy_cq;
161 	dev->ibdev.resize_cq = ocrdma_resize_cq;
162 
163 	dev->ibdev.create_qp = ocrdma_create_qp;
164 	dev->ibdev.modify_qp = ocrdma_modify_qp;
165 	dev->ibdev.query_qp = ocrdma_query_qp;
166 	dev->ibdev.destroy_qp = ocrdma_destroy_qp;
167 
168 	dev->ibdev.query_pkey = ocrdma_query_pkey;
169 	dev->ibdev.create_ah = ocrdma_create_ah;
170 	dev->ibdev.destroy_ah = ocrdma_destroy_ah;
171 	dev->ibdev.query_ah = ocrdma_query_ah;
172 	dev->ibdev.modify_ah = ocrdma_modify_ah;
173 
174 	dev->ibdev.poll_cq = ocrdma_poll_cq;
175 	dev->ibdev.post_send = ocrdma_post_send;
176 	dev->ibdev.post_recv = ocrdma_post_recv;
177 	dev->ibdev.req_notify_cq = ocrdma_arm_cq;
178 
179 	dev->ibdev.get_dma_mr = ocrdma_get_dma_mr;
180 	dev->ibdev.reg_phys_mr = ocrdma_reg_kernel_mr;
181 	dev->ibdev.dereg_mr = ocrdma_dereg_mr;
182 	dev->ibdev.reg_user_mr = ocrdma_reg_user_mr;
183 
184 	dev->ibdev.alloc_mr = ocrdma_alloc_mr;
185 	dev->ibdev.alloc_fast_reg_page_list = ocrdma_alloc_frmr_page_list;
186 	dev->ibdev.free_fast_reg_page_list = ocrdma_free_frmr_page_list;
187 
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;
193 
194 	dev->ibdev.process_mad = ocrdma_process_mad;
195 	dev->ibdev.get_port_immutable = ocrdma_port_immutable;
196 
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);
204 
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;
210 	}
211 	return ib_register_device(&dev->ibdev, NULL);
212 }
213 
214 static int ocrdma_alloc_resources(struct ocrdma_dev *dev)
215 {
216 	mutex_init(&dev->dev_lock);
217 	dev->cq_tbl = kzalloc(sizeof(struct ocrdma_cq *) *
218 			      OCRDMA_MAX_CQ, GFP_KERNEL);
219 	if (!dev->cq_tbl)
220 		goto alloc_err;
221 
222 	if (dev->attr.max_qp) {
223 		dev->qp_tbl = kzalloc(sizeof(struct ocrdma_qp *) *
224 				      OCRDMA_MAX_QP, GFP_KERNEL);
225 		if (!dev->qp_tbl)
226 			goto alloc_err;
227 	}
228 
229 	dev->stag_arr = kzalloc(sizeof(u64) * OCRDMA_MAX_STAG, GFP_KERNEL);
230 	if (dev->stag_arr == NULL)
231 		goto alloc_err;
232 
233 	ocrdma_alloc_pd_pool(dev);
234 
235 	spin_lock_init(&dev->av_tbl.lock);
236 	spin_lock_init(&dev->flush_q_lock);
237 	return 0;
238 alloc_err:
239 	pr_err("%s(%d) error.\n", __func__, dev->id);
240 	return -ENOMEM;
241 }
242 
243 static void ocrdma_free_resources(struct ocrdma_dev *dev)
244 {
245 	kfree(dev->stag_arr);
246 	kfree(dev->qp_tbl);
247 	kfree(dev->cq_tbl);
248 }
249 
250 /* OCRDMA sysfs interface */
251 static ssize_t show_rev(struct device *device, struct device_attribute *attr,
252 			char *buf)
253 {
254 	struct ocrdma_dev *dev = dev_get_drvdata(device);
255 
256 	return scnprintf(buf, PAGE_SIZE, "0x%x\n", dev->nic_info.pdev->vendor);
257 }
258 
259 static ssize_t show_fw_ver(struct device *device, struct device_attribute *attr,
260 			char *buf)
261 {
262 	struct ocrdma_dev *dev = dev_get_drvdata(device);
263 
264 	return scnprintf(buf, PAGE_SIZE, "%s\n", &dev->attr.fw_ver[0]);
265 }
266 
267 static ssize_t show_hca_type(struct device *device,
268 			     struct device_attribute *attr, char *buf)
269 {
270 	struct ocrdma_dev *dev = dev_get_drvdata(device);
271 
272 	return scnprintf(buf, PAGE_SIZE, "%s\n", &dev->model_number[0]);
273 }
274 
275 static DEVICE_ATTR(hw_rev, S_IRUGO, show_rev, NULL);
276 static DEVICE_ATTR(fw_ver, S_IRUGO, show_fw_ver, NULL);
277 static DEVICE_ATTR(hca_type, S_IRUGO, show_hca_type, NULL);
278 
279 static struct device_attribute *ocrdma_attributes[] = {
280 	&dev_attr_hw_rev,
281 	&dev_attr_fw_ver,
282 	&dev_attr_hca_type
283 };
284 
285 static void ocrdma_remove_sysfiles(struct ocrdma_dev *dev)
286 {
287 	int i;
288 
289 	for (i = 0; i < ARRAY_SIZE(ocrdma_attributes); i++)
290 		device_remove_file(&dev->ibdev.dev, ocrdma_attributes[i]);
291 }
292 
293 static struct ocrdma_dev *ocrdma_add(struct be_dev_info *dev_info)
294 {
295 	int status = 0, i;
296 	struct ocrdma_dev *dev;
297 
298 	dev = (struct ocrdma_dev *)ib_alloc_device(sizeof(struct ocrdma_dev));
299 	if (!dev) {
300 		pr_err("Unable to allocate ib device\n");
301 		return NULL;
302 	}
303 	dev->mbx_cmd = kzalloc(sizeof(struct ocrdma_mqe_emb_cmd), GFP_KERNEL);
304 	if (!dev->mbx_cmd)
305 		goto idr_err;
306 
307 	memcpy(&dev->nic_info, dev_info, sizeof(*dev_info));
308 	dev->id = idr_alloc(&ocrdma_dev_id, NULL, 0, 0, GFP_KERNEL);
309 	if (dev->id < 0)
310 		goto idr_err;
311 
312 	status = ocrdma_init_hw(dev);
313 	if (status)
314 		goto init_err;
315 
316 	status = ocrdma_alloc_resources(dev);
317 	if (status)
318 		goto alloc_err;
319 
320 	ocrdma_init_service_level(dev);
321 	status = ocrdma_register_device(dev);
322 	if (status)
323 		goto alloc_err;
324 
325 	for (i = 0; i < ARRAY_SIZE(ocrdma_attributes); i++)
326 		if (device_create_file(&dev->ibdev.dev, ocrdma_attributes[i]))
327 			goto sysfs_err;
328 	spin_lock(&ocrdma_devlist_lock);
329 	list_add_tail_rcu(&dev->entry, &ocrdma_dev_list);
330 	spin_unlock(&ocrdma_devlist_lock);
331 	/* Init stats */
332 	ocrdma_add_port_stats(dev);
333 	/* Interrupt Moderation */
334 	INIT_DELAYED_WORK(&dev->eqd_work, ocrdma_eqd_set_task);
335 	schedule_delayed_work(&dev->eqd_work, msecs_to_jiffies(1000));
336 
337 	pr_info("%s %s: %s \"%s\" port %d\n",
338 		dev_name(&dev->nic_info.pdev->dev), hca_name(dev),
339 		port_speed_string(dev), dev->model_number,
340 		dev->hba_port_num);
341 	pr_info("%s ocrdma%d driver loaded successfully\n",
342 		dev_name(&dev->nic_info.pdev->dev), dev->id);
343 	return dev;
344 
345 sysfs_err:
346 	ocrdma_remove_sysfiles(dev);
347 alloc_err:
348 	ocrdma_free_resources(dev);
349 	ocrdma_cleanup_hw(dev);
350 init_err:
351 	idr_remove(&ocrdma_dev_id, dev->id);
352 idr_err:
353 	kfree(dev->mbx_cmd);
354 	ib_dealloc_device(&dev->ibdev);
355 	pr_err("%s() leaving. ret=%d\n", __func__, status);
356 	return NULL;
357 }
358 
359 static void ocrdma_remove_free(struct rcu_head *rcu)
360 {
361 	struct ocrdma_dev *dev = container_of(rcu, struct ocrdma_dev, rcu);
362 
363 	idr_remove(&ocrdma_dev_id, dev->id);
364 	kfree(dev->mbx_cmd);
365 	ib_dealloc_device(&dev->ibdev);
366 }
367 
368 static void ocrdma_remove(struct ocrdma_dev *dev)
369 {
370 	/* first unregister with stack to stop all the active traffic
371 	 * of the registered clients.
372 	 */
373 	cancel_delayed_work_sync(&dev->eqd_work);
374 	ocrdma_remove_sysfiles(dev);
375 	ib_unregister_device(&dev->ibdev);
376 
377 	ocrdma_rem_port_stats(dev);
378 
379 	spin_lock(&ocrdma_devlist_lock);
380 	list_del_rcu(&dev->entry);
381 	spin_unlock(&ocrdma_devlist_lock);
382 
383 	ocrdma_free_resources(dev);
384 	ocrdma_cleanup_hw(dev);
385 
386 	call_rcu(&dev->rcu, ocrdma_remove_free);
387 }
388 
389 static int ocrdma_open(struct ocrdma_dev *dev)
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 
400 static int ocrdma_close(struct ocrdma_dev *dev)
401 {
402 	int i;
403 	struct ocrdma_qp *qp, **cur_qp;
404 	struct ib_event err_event;
405 	struct ib_qp_attr attrs;
406 	int attr_mask = IB_QP_STATE;
407 
408 	attrs.qp_state = IB_QPS_ERR;
409 	mutex_lock(&dev->dev_lock);
410 	if (dev->qp_tbl) {
411 		cur_qp = dev->qp_tbl;
412 		for (i = 0; i < OCRDMA_MAX_QP; i++) {
413 			qp = cur_qp[i];
414 			if (qp && qp->ibqp.qp_type != IB_QPT_GSI) {
415 				/* change the QP state to ERROR */
416 				_ocrdma_modify_qp(&qp->ibqp, &attrs, attr_mask);
417 
418 				err_event.event = IB_EVENT_QP_FATAL;
419 				err_event.element.qp = &qp->ibqp;
420 				err_event.device = &dev->ibdev;
421 				ib_dispatch_event(&err_event);
422 			}
423 		}
424 	}
425 	mutex_unlock(&dev->dev_lock);
426 
427 	err_event.event = IB_EVENT_PORT_ERR;
428 	err_event.element.port_num = 1;
429 	err_event.device = &dev->ibdev;
430 	ib_dispatch_event(&err_event);
431 	return 0;
432 }
433 
434 static void ocrdma_shutdown(struct ocrdma_dev *dev)
435 {
436 	ocrdma_close(dev);
437 	ocrdma_remove(dev);
438 }
439 
440 /* event handling via NIC driver ensures that all the NIC specific
441  * initialization done before RoCE driver notifies
442  * event to stack.
443  */
444 static void ocrdma_event_handler(struct ocrdma_dev *dev, u32 event)
445 {
446 	switch (event) {
447 	case BE_DEV_UP:
448 		ocrdma_open(dev);
449 		break;
450 	case BE_DEV_DOWN:
451 		ocrdma_close(dev);
452 		break;
453 	case BE_DEV_SHUTDOWN:
454 		ocrdma_shutdown(dev);
455 		break;
456 	}
457 }
458 
459 static struct ocrdma_driver ocrdma_drv = {
460 	.name			= "ocrdma_driver",
461 	.add			= ocrdma_add,
462 	.remove			= ocrdma_remove,
463 	.state_change_handler	= ocrdma_event_handler,
464 	.be_abi_version		= OCRDMA_BE_ROCE_ABI_VERSION,
465 };
466 
467 static int __init ocrdma_init_module(void)
468 {
469 	int status;
470 
471 	ocrdma_init_debugfs();
472 
473 	status = be_roce_register_driver(&ocrdma_drv);
474 	if (status)
475 		goto err_be_reg;
476 
477 	return 0;
478 
479 err_be_reg:
480 
481 	return status;
482 }
483 
484 static void __exit ocrdma_exit_module(void)
485 {
486 	be_roce_unregister_driver(&ocrdma_drv);
487 	ocrdma_rem_debugfs();
488 	idr_destroy(&ocrdma_dev_id);
489 }
490 
491 module_init(ocrdma_init_module);
492 module_exit(ocrdma_exit_module);
493