Linux Audio

Check our new training course

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