hns_roce_main.c 30.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
/*
 * Copyright (c) 2016 Hisilicon Limited.
 * Copyright (c) 2007, 2008 Mellanox Technologies. All rights reserved.
 *
 * This software is available to you under a choice of one of two
 * licenses.  You may choose to be licensed under the terms of the GNU
 * General Public License (GPL) Version 2, available from the file
 * COPYING in the main directory of this source tree, or the
 * OpenIB.org BSD license below:
 *
 *     Redistribution and use in source and binary forms, with or
 *     without modification, are permitted provided that the following
 *     conditions are met:
 *
 *      - Redistributions of source code must retain the above
 *        copyright notice, this list of conditions and the following
 *        disclaimer.
 *
 *      - Redistributions in binary form must reproduce the above
 *        copyright notice, this list of conditions and the following
 *        disclaimer in the documentation and/or other materials
 *        provided with the distribution.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
33
#include <linux/acpi.h>
A
Arnd Bergmann 已提交
34
#include <linux/module.h>
35
#include <linux/pci.h>
36 37 38
#include <rdma/ib_addr.h>
#include <rdma/ib_smi.h>
#include <rdma/ib_user_verbs.h>
39
#include <rdma/ib_cache.h>
J
Junxian Huang 已提交
40 41

#include "hnae3.h"
42 43 44
#include "hns_roce_common.h"
#include "hns_roce_device.h"
#include "hns_roce_hem.h"
J
Junxian Huang 已提交
45
#include "hns_roce_hw_v2.h"
46

47 48
static int hns_roce_set_mac(struct hns_roce_dev *hr_dev, u32 port,
			    const u8 *addr)
49 50
{
	u8 phy_port;
51 52 53 54
	u32 i;

	if (hr_dev->pci_dev->revision >= PCI_REVISION_ID_HIP09)
		return 0;
55

56
	if (!memcmp(hr_dev->dev_addr[port], addr, ETH_ALEN))
57
		return 0;
58

59
	for (i = 0; i < ETH_ALEN; i++)
60 61 62
		hr_dev->dev_addr[port][i] = addr[i];

	phy_port = hr_dev->iboe.phy_port[port];
63
	return hr_dev->hw->set_mac(hr_dev, phy_port, addr);
64 65
}

66
static int hns_roce_add_gid(const struct ib_gid_attr *attr, void **context)
67
{
68 69
	struct hns_roce_dev *hr_dev = to_hr_dev(attr->device);
	u8 port = attr->port_num - 1;
70
	int ret;
71 72 73

	if (port >= hr_dev->caps.num_ports)
		return -EINVAL;
74

75
	ret = hr_dev->hw->set_gid(hr_dev, attr->index, &attr->gid, attr);
76

77
	return ret;
78 79
}

80
static int hns_roce_del_gid(const struct ib_gid_attr *attr, void **context)
81
{
82 83
	struct hns_roce_dev *hr_dev = to_hr_dev(attr->device);
	u8 port = attr->port_num - 1;
84
	int ret;
85 86 87 88

	if (port >= hr_dev->caps.num_ports)
		return -EINVAL;

89
	ret = hr_dev->hw->set_gid(hr_dev, attr->index, NULL, NULL);
90

91
	return ret;
92 93 94
}

static int handle_en_event(struct hns_roce_dev *hr_dev, u8 port,
95
			   unsigned long dev_event)
96
{
97
	struct device *dev = hr_dev->dev;
98
	enum ib_port_state port_state;
99
	struct net_device *netdev;
100 101
	struct ib_event event;
	unsigned long flags;
102
	int ret = 0;
103 104 105

	netdev = hr_dev->iboe.netdevs[port];
	if (!netdev) {
106
		dev_err(dev, "Can't find netdev on port(%u)!\n", port);
107 108 109
		return -ENODEV;
	}

110
	switch (dev_event) {
111 112
	case NETDEV_REGISTER:
	case NETDEV_CHANGEADDR:
113
		ret = hns_roce_set_mac(hr_dev, port, netdev->dev_addr);
114
		break;
115 116 117 118 119 120
	case NETDEV_UP:
	case NETDEV_CHANGE:
		ret = hns_roce_set_mac(hr_dev, port, netdev->dev_addr);
		if (ret)
			return ret;
		fallthrough;
121
	case NETDEV_DOWN:
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
		port_state = get_port_state(netdev);

		spin_lock_irqsave(&hr_dev->iboe.lock, flags);
		if (hr_dev->iboe.port_state[port] == port_state) {
			spin_unlock_irqrestore(&hr_dev->iboe.lock, flags);
			return NOTIFY_DONE;
		}
		hr_dev->iboe.port_state[port] = port_state;
		spin_unlock_irqrestore(&hr_dev->iboe.lock, flags);

		event.device = &hr_dev->ib_dev;
		event.event = (port_state == IB_PORT_ACTIVE) ?
			      IB_EVENT_PORT_ACTIVE : IB_EVENT_PORT_ERR;
		event.element.port_num = to_rdma_port_num(port);
		ib_dispatch_event(&event);
		break;
	case NETDEV_UNREGISTER:
139 140
		break;
	default:
141
		dev_dbg(dev, "NETDEV event = 0x%x!\n", (u32)(dev_event));
142 143 144
		break;
	}

145
	return ret;
146 147 148 149 150 151 152 153
}

static int hns_roce_netdev_event(struct notifier_block *self,
				 unsigned long event, void *ptr)
{
	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
	struct hns_roce_ib_iboe *iboe = NULL;
	struct hns_roce_dev *hr_dev = NULL;
154 155
	int ret;
	u8 port;
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171

	hr_dev = container_of(self, struct hns_roce_dev, iboe.nb);
	iboe = &hr_dev->iboe;

	for (port = 0; port < hr_dev->caps.num_ports; port++) {
		if (dev == iboe->netdevs[port]) {
			ret = handle_en_event(hr_dev, port, event);
			if (ret)
				return NOTIFY_DONE;
			break;
		}
	}

	return NOTIFY_DONE;
}

172
static int hns_roce_setup_mtu_mac(struct hns_roce_dev *hr_dev)
173
{
174
	int ret;
175
	u8 i;
176 177

	for (i = 0; i < hr_dev->caps.num_ports; i++) {
178 179
		hr_dev->iboe.port_state[i] = IB_PORT_DOWN;

180 181 182 183
		ret = hns_roce_set_mac(hr_dev, i,
				       hr_dev->iboe.netdevs[i]->dev_addr);
		if (ret)
			return ret;
184 185
	}

186
	return 0;
187 188 189 190 191 192 193 194 195 196
}

static int hns_roce_query_device(struct ib_device *ib_dev,
				 struct ib_device_attr *props,
				 struct ib_udata *uhw)
{
	struct hns_roce_dev *hr_dev = to_hr_dev(ib_dev);

	memset(props, 0, sizeof(*props));

197
	props->fw_ver = hr_dev->caps.fw_ver;
198
	props->sys_image_guid = cpu_to_be64(hr_dev->sys_image_guid);
199 200 201 202 203 204 205 206
	props->max_mr_size = (u64)(~(0ULL));
	props->page_size_cap = hr_dev->caps.page_size_cap;
	props->vendor_id = hr_dev->vendor_id;
	props->vendor_part_id = hr_dev->vendor_part_id;
	props->hw_ver = hr_dev->hw_rev;
	props->max_qp = hr_dev->caps.num_qps;
	props->max_qp_wr = hr_dev->caps.max_wqes;
	props->device_cap_flags = IB_DEVICE_PORT_ACTIVE_EVENT |
207
				  IB_DEVICE_RC_RNR_NAK_GEN;
208 209
	props->max_send_sge = hr_dev->caps.max_sq_sg;
	props->max_recv_sge = hr_dev->caps.max_rq_sg;
210 211 212 213 214 215 216
	props->max_sge_rd = 1;
	props->max_cq = hr_dev->caps.num_cqs;
	props->max_cqe = hr_dev->caps.max_cqes;
	props->max_mr = hr_dev->caps.num_mtpts;
	props->max_pd = hr_dev->caps.num_pds;
	props->max_qp_rd_atom = hr_dev->caps.max_qp_dest_rdma;
	props->max_qp_init_rd_atom = hr_dev->caps.max_qp_init_rdma;
L
Lijun Ou 已提交
217 218
	props->atomic_cap = hr_dev->caps.flags & HNS_ROCE_CAP_FLAG_ATOMIC ?
			    IB_ATOMIC_HCA : IB_ATOMIC_NONE;
219 220
	props->max_pkeys = 1;
	props->local_ca_ack_delay = hr_dev->caps.local_ca_ack_delay;
L
Lijun Ou 已提交
221
	if (hr_dev->caps.flags & HNS_ROCE_CAP_FLAG_SRQ) {
222
		props->max_srq = hr_dev->caps.num_srqs;
L
Lijun Ou 已提交
223 224 225
		props->max_srq_wr = hr_dev->caps.max_srq_wrs;
		props->max_srq_sge = hr_dev->caps.max_srq_sges;
	}
226

227 228
	if (hr_dev->caps.flags & HNS_ROCE_CAP_FLAG_FRMR &&
	    hr_dev->pci_dev->revision >= PCI_REVISION_ID_HIP09) {
229 230 231 232
		props->device_cap_flags |= IB_DEVICE_MEM_MGT_EXTENSIONS;
		props->max_fast_reg_page_list_len = HNS_ROCE_FRMR_MAX_PA;
	}

233 234 235
	if (hr_dev->caps.flags & HNS_ROCE_CAP_FLAG_XRC)
		props->device_cap_flags |= IB_DEVICE_XRC;

236 237 238 239 240 241 242
	return 0;
}

static int hns_roce_query_port(struct ib_device *ib_dev, u8 port_num,
			       struct ib_port_attr *props)
{
	struct hns_roce_dev *hr_dev = to_hr_dev(ib_dev);
243
	struct device *dev = hr_dev->dev;
244 245 246 247 248 249 250
	struct net_device *net_dev;
	unsigned long flags;
	enum ib_mtu mtu;
	u8 port;

	port = port_num - 1;

251
	/* props being zeroed by the caller, avoid zeroing it here */
252 253 254 255 256 257 258 259 260 261 262 263 264

	props->max_mtu = hr_dev->caps.max_mtu;
	props->gid_tbl_len = hr_dev->caps.gid_table_len[port];
	props->port_cap_flags = IB_PORT_CM_SUP | IB_PORT_REINIT_SUP |
				IB_PORT_VENDOR_CLASS_SUP |
				IB_PORT_BOOT_MGMT_SUP;
	props->max_msg_sz = HNS_ROCE_MAX_MSG_LEN;
	props->pkey_tbl_len = 1;
	props->active_width = IB_WIDTH_4X;
	props->active_speed = 1;

	spin_lock_irqsave(&hr_dev->iboe.lock, flags);

J
Junxian Huang 已提交
265 266 267
	net_dev = hr_dev->hw->get_bond_netdev(hr_dev);
	if (!net_dev)
		net_dev = hr_dev->iboe.netdevs[port];
268 269
	if (!net_dev) {
		spin_unlock_irqrestore(&hr_dev->iboe.lock, flags);
270
		dev_err(dev, "Find netdev %u failed!\n", port);
271 272 273 274 275
		return -EINVAL;
	}

	mtu = iboe_get_mtu(net_dev->mtu);
	props->active_mtu = mtu ? min(props->max_mtu, mtu) : IB_MTU_256;
276
	props->state = get_port_state(net_dev);
L
Lijun Ou 已提交
277 278 279
	props->phys_state = props->state == IB_PORT_ACTIVE ?
				    IB_PORT_PHYS_STATE_LINK_UP :
				    IB_PORT_PHYS_STATE_DISABLED;
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294

	spin_unlock_irqrestore(&hr_dev->iboe.lock, flags);

	return 0;
}

static enum rdma_link_layer hns_roce_get_link_layer(struct ib_device *device,
						    u8 port_num)
{
	return IB_LINK_LAYER_ETHERNET;
}

static int hns_roce_query_pkey(struct ib_device *ib_dev, u8 port, u16 index,
			       u16 *pkey)
{
K
Kamal Heib 已提交
295 296 297
	if (index > 0)
		return -EINVAL;

298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
	*pkey = PKEY_ID;

	return 0;
}

static int hns_roce_modify_device(struct ib_device *ib_dev, int mask,
				  struct ib_device_modify *props)
{
	unsigned long flags;

	if (mask & ~IB_DEVICE_MODIFY_NODE_DESC)
		return -EOPNOTSUPP;

	if (mask & IB_DEVICE_MODIFY_NODE_DESC) {
		spin_lock_irqsave(&to_hr_dev(ib_dev)->sm_lock, flags);
		memcpy(ib_dev->node_desc, props->node_desc, NODE_DESC_SIZE);
		spin_unlock_irqrestore(&to_hr_dev(ib_dev)->sm_lock, flags);
	}

	return 0;
}

320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
struct hns_user_mmap_entry *
hns_roce_user_mmap_entry_insert(struct ib_ucontext *ucontext, u64 address,
				size_t length,
				enum hns_roce_mmap_type mmap_type)
{
	struct hns_user_mmap_entry *entry;
	int ret;

	entry = kzalloc(sizeof(*entry), GFP_KERNEL);
	if (!entry)
		return NULL;

	entry->address = address;
	entry->mmap_type = mmap_type;

335
	switch (mmap_type) {
336
	/* pgoff 0 must be used by DB for compatibility */
337 338 339 340 341 342
	case HNS_ROCE_MMAP_TYPE_DB:
		ret = rdma_user_mmap_entry_insert_exact(
				ucontext, &entry->rdma_entry, length, 0);
		break;
	case HNS_ROCE_MMAP_TYPE_DWQE:
		ret = rdma_user_mmap_entry_insert_range(
343
				ucontext, &entry->rdma_entry, length, 1,
344 345 346 347 348 349 350
				U32_MAX);
		break;
	default:
		ret = -EINVAL;
		break;
	}

351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379
	if (ret) {
		kfree(entry);
		return NULL;
	}

	return entry;
}

static void hns_roce_dealloc_uar_entry(struct hns_roce_ucontext *context)
{
	if (context->db_mmap_entry)
		rdma_user_mmap_entry_remove(
			&context->db_mmap_entry->rdma_entry);
}

static int hns_roce_alloc_uar_entry(struct ib_ucontext *uctx)
{
	struct hns_roce_ucontext *context = to_hr_ucontext(uctx);
	u64 address;

	address = context->uar.pfn << PAGE_SHIFT;
	context->db_mmap_entry = hns_roce_user_mmap_entry_insert(
		uctx, address, PAGE_SIZE, HNS_ROCE_MMAP_TYPE_DB);
	if (!context->db_mmap_entry)
		return -ENOMEM;

	return 0;
}

380 381
static int hns_roce_alloc_ucontext(struct ib_ucontext *uctx,
				   struct ib_udata *udata)
382
{
383 384
	struct hns_roce_ucontext *context = to_hr_ucontext(uctx);
	struct hns_roce_dev *hr_dev = to_hr_dev(uctx->device);
L
Luoyouming 已提交
385 386 387
	struct hns_roce_ib_alloc_ucontext_resp resp = {};
	struct hns_roce_ib_alloc_ucontext ucmd = {};
	int ret;
388

389
	if (!hr_dev->active)
390
		return -EAGAIN;
391

392
	resp.qp_tab_size = hr_dev->caps.num_qps;
393
	resp.srq_tab_size = hr_dev->caps.num_srqs;
394

L
Luoyouming 已提交
395 396 397 398 399 400 401 402 403 404 405 406 407
	ret = ib_copy_from_udata(&ucmd, udata,
				 min(udata->inlen, sizeof(ucmd)));
	if (ret)
		return ret;

	if (hr_dev->pci_dev->revision >= PCI_REVISION_ID_HIP09)
		context->config = ucmd.config & HNS_ROCE_EXSGE_FLAGS;

	if (context->config & HNS_ROCE_EXSGE_FLAGS) {
		resp.config |= HNS_ROCE_RSP_EXSGE_FLAGS;
		resp.max_inline_data = hr_dev->caps.max_sq_inline;
	}

408 409 410 411 412
	if (hr_dev->caps.flags & HNS_ROCE_CAP_FLAG_RQ_INLINE) {
		context->config |= ucmd.config & HNS_ROCE_RQ_INLINE_FLAGS;
		resp.config |= HNS_ROCE_RSP_RQ_INLINE_FLAGS;
	}

413 414 415 416 417
	if (hr_dev->caps.flags & HNS_ROCE_CAP_FLAG_CQE_INLINE) {
		context->config |= ucmd.config & HNS_ROCE_CQE_INLINE_FLAGS;
		resp.config |= HNS_ROCE_RSP_CQE_INLINE_FLAGS;
	}

418 419 420 421
	ret = hns_roce_uar_alloc(hr_dev, &context->uar);
	if (ret)
		goto error_fail_uar_alloc;

422 423 424 425
	ret = hns_roce_alloc_uar_entry(uctx);
	if (ret)
		goto error_fail_uar_entry;

426 427
	if (hr_dev->caps.flags & HNS_ROCE_CAP_FLAG_CQ_RECORD_DB ||
	    hr_dev->caps.flags & HNS_ROCE_CAP_FLAG_QP_RECORD_DB) {
428 429 430 431
		INIT_LIST_HEAD(&context->page_list);
		mutex_init(&context->page_mutex);
	}

432
	resp.cqe_size = hr_dev->caps.cqe_sz;
433
	resp.mac_type = hr_dev->mac_type;
434

435 436
	ret = ib_copy_to_udata(udata, &resp,
			       min(udata->outlen, sizeof(resp)));
437 438 439
	if (ret)
		goto error_fail_copy_to_udata;

440
	return 0;
441 442

error_fail_copy_to_udata:
443 444 445
	hns_roce_dealloc_uar_entry(context);

error_fail_uar_entry:
446
	ida_free(&hr_dev->uar_ida.ida, (int)context->uar.logic_idx);
447 448

error_fail_uar_alloc:
449
	return ret;
450 451
}

452
static void hns_roce_dealloc_ucontext(struct ib_ucontext *ibcontext)
453 454
{
	struct hns_roce_ucontext *context = to_hr_ucontext(ibcontext);
455
	struct hns_roce_dev *hr_dev = to_hr_dev(ibcontext->device);
456

457 458
	hns_roce_dealloc_uar_entry(context);

459
	ida_free(&hr_dev->uar_ida.ida, (int)context->uar.logic_idx);
460 461
}

462
static int hns_roce_mmap(struct ib_ucontext *uctx, struct vm_area_struct *vma)
463
{
464 465 466 467 468
	struct rdma_user_mmap_entry *rdma_entry;
	struct hns_user_mmap_entry *entry;
	phys_addr_t pfn;
	pgprot_t prot;
	int ret;
469

470 471 472
	rdma_entry = rdma_user_mmap_entry_get_pgoff(uctx, vma->vm_pgoff);
	if (!rdma_entry)
 		return -EINVAL;
473

474 475 476
	entry = to_hns_mmap(rdma_entry);
	pfn = entry->address >> PAGE_SHIFT;

477 478 479 480 481 482 483 484
	switch (entry->mmap_type) {
	case HNS_ROCE_MMAP_TYPE_DB:
	case HNS_ROCE_MMAP_TYPE_DWQE:
		prot = pgprot_device(vma->vm_page_prot);
		break;
	default:
		return -EINVAL;
	}
485 486 487 488 489 490 491 492 493 494 495 496 497 498

	ret = rdma_user_mmap_io(uctx, vma, pfn, rdma_entry->npages * PAGE_SIZE,
				prot, rdma_entry);

	rdma_user_mmap_entry_put(rdma_entry);

	return ret;
}

static void hns_roce_free_mmap(struct rdma_user_mmap_entry *rdma_entry)
{
	struct hns_user_mmap_entry *entry = to_hns_mmap(rdma_entry);

	kfree(entry);
499 500 501 502 503 504 505 506
}

static int hns_roce_port_immutable(struct ib_device *ib_dev, u8 port_num,
				   struct ib_port_immutable *immutable)
{
	struct ib_port_attr attr;
	int ret;

507
	ret = ib_query_port(ib_dev, port_num, &attr);
508 509 510 511 512 513 514
	if (ret)
		return ret;

	immutable->pkey_tbl_len = attr.pkey_tbl_len;
	immutable->gid_tbl_len = attr.gid_tbl_len;

	immutable->max_mad_size = IB_MGMT_MAD_SIZE;
515 516 517 518 519 520 521 522

	if (to_hr_dev(ib_dev)->mac_type == HNAE3_MAC_ROH)
		immutable->core_cap_flags = RDMA_CORE_PORT_IBA_ROCE_UDP_ENCAP;
	else if (to_hr_dev(ib_dev)->caps.flags & HNS_ROCE_CAP_FLAG_ROCE_V1_V2)
		immutable->core_cap_flags = RDMA_CORE_PORT_IBA_ROCE |
					    RDMA_CORE_PORT_IBA_ROCE_UDP_ENCAP;
	else
		immutable->core_cap_flags = RDMA_CORE_PORT_IBA_ROCE;
523 524 525 526

	return 0;
}

527 528 529 530
static void hns_roce_disassociate_ucontext(struct ib_ucontext *ibcontext)
{
}

531 532 533 534 535 536 537 538 539 540 541 542 543
static void hns_roce_get_fw_ver(struct ib_device *device, char *str)
{
	u64 fw_ver = to_hr_dev(device)->caps.fw_ver;
	unsigned int major, minor, sub_minor;

	major = upper_32_bits(fw_ver);
	minor = high_16_bits(lower_32_bits(fw_ver));
	sub_minor = low_16_bits(fw_ver);

	snprintf(str, IB_FW_VERSION_NAME_MAX, "%u.%u.%04u", major, minor,
		 sub_minor);
}

544 545 546 547
static void hns_roce_unregister_device(struct hns_roce_dev *hr_dev)
{
	struct hns_roce_ib_iboe *iboe = &hr_dev->iboe;

J
Junxian Huang 已提交
548 549 550
	if (hr_dev->caps.flags & HNS_ROCE_CAP_FLAG_BOND)
		hns_roce_cleanup_bond(hr_dev);

551
	hr_dev->active = false;
552 553 554 555
	unregister_netdevice_notifier(&iboe->nb);
	ib_unregister_device(&hr_dev->ib_dev);
}

556
static const struct ib_device_ops hns_roce_dev_ops = {
557
	.owner = THIS_MODULE,
558
	.driver_id = RDMA_DRIVER_HNS,
559
	.uverbs_abi_ver = 1,
560
	.uverbs_no_driver_id_binding = 1,
561

562
	.get_dev_fw_str = hns_roce_get_fw_ver,
563 564 565 566
	.add_gid = hns_roce_add_gid,
	.alloc_pd = hns_roce_alloc_pd,
	.alloc_ucontext = hns_roce_alloc_ucontext,
	.create_ah = hns_roce_create_ah,
567
	.create_cq = hns_roce_create_cq,
568 569 570 571 572 573
	.create_qp = hns_roce_create_qp,
	.dealloc_pd = hns_roce_dealloc_pd,
	.dealloc_ucontext = hns_roce_dealloc_ucontext,
	.del_gid = hns_roce_del_gid,
	.dereg_mr = hns_roce_dereg_mr,
	.destroy_ah = hns_roce_destroy_ah,
574
	.destroy_cq = hns_roce_destroy_cq,
575 576 577 578 579
	.disassociate_ucontext = hns_roce_disassociate_ucontext,
	.get_dma_mr = hns_roce_get_dma_mr,
	.get_link_layer = hns_roce_get_link_layer,
	.get_port_immutable = hns_roce_port_immutable,
	.mmap = hns_roce_mmap,
580
	.mmap_free = hns_roce_free_mmap,
581 582 583 584 585 586 587
	.modify_device = hns_roce_modify_device,
	.modify_qp = hns_roce_modify_qp,
	.query_ah = hns_roce_query_ah,
	.query_device = hns_roce_query_device,
	.query_pkey = hns_roce_query_pkey,
	.query_port = hns_roce_query_port,
	.reg_user_mr = hns_roce_reg_user_mr,
588 589

	INIT_RDMA_OBJ_SIZE(ib_ah, hns_roce_ah, ibah),
590
	INIT_RDMA_OBJ_SIZE(ib_cq, hns_roce_cq, ib_cq),
591
	INIT_RDMA_OBJ_SIZE(ib_pd, hns_roce_pd, ibpd),
592
	INIT_RDMA_OBJ_SIZE(ib_ucontext, hns_roce_ucontext, ibucontext),
593 594 595 596 597 598 599 600 601
};

static const struct ib_device_ops hns_roce_dev_mr_ops = {
	.rereg_user_mr = hns_roce_rereg_user_mr,
};

static const struct ib_device_ops hns_roce_dev_mw_ops = {
	.alloc_mw = hns_roce_alloc_mw,
	.dealloc_mw = hns_roce_dealloc_mw,
602 603

	INIT_RDMA_OBJ_SIZE(ib_mw, hns_roce_mw, ibmw),
604 605 606 607 608 609 610 611 612 613
};

static const struct ib_device_ops hns_roce_dev_frmr_ops = {
	.alloc_mr = hns_roce_alloc_mr,
	.map_mr_sg = hns_roce_map_mr_sg,
};

static const struct ib_device_ops hns_roce_dev_srq_ops = {
	.create_srq = hns_roce_create_srq,
	.destroy_srq = hns_roce_destroy_srq,
614 615

	INIT_RDMA_OBJ_SIZE(ib_srq, hns_roce_srq, ibsrq),
616 617
};

618 619 620 621 622 623 624
static const struct ib_device_ops hns_roce_dev_xrcd_ops = {
	.alloc_xrcd = hns_roce_alloc_xrcd,
	.dealloc_xrcd = hns_roce_dealloc_xrcd,

	INIT_RDMA_OBJ_SIZE(ib_xrcd, hns_roce_xrcd, ibxrcd),
};

625 626
static const struct ib_device_ops hns_roce_dev_restrack_ops = {
	.fill_res_cq_entry = hns_roce_fill_res_cq_entry,
627
	.fill_res_cq_entry_raw = hns_roce_fill_res_cq_entry_raw,
628
	.fill_res_qp_entry = hns_roce_fill_res_qp_entry,
629
	.fill_res_qp_entry_raw = hns_roce_fill_res_qp_entry_raw,
630
	.fill_res_mr_entry = hns_roce_fill_res_mr_entry,
631
	.fill_res_mr_entry_raw = hns_roce_fill_res_mr_entry_raw,
632 633
};

634 635 636 637 638
static int hns_roce_register_device(struct hns_roce_dev *hr_dev)
{
	int ret;
	struct hns_roce_ib_iboe *iboe = NULL;
	struct ib_device *ib_dev = NULL;
639
	struct device *dev = hr_dev->dev;
640
	unsigned int i;
641 642

	iboe = &hr_dev->iboe;
L
Lijun Ou 已提交
643
	spin_lock_init(&iboe->lock);
644 645 646

	ib_dev = &hr_dev->ib_dev;

L
Lijun Ou 已提交
647 648
	ib_dev->node_type = RDMA_NODE_IB_CA;
	ib_dev->dev.parent = dev;
649

L
Lijun Ou 已提交
650 651 652 653
	ib_dev->phys_port_cnt = hr_dev->caps.num_ports;
	ib_dev->local_dma_lkey = hr_dev->caps.reserved_lkey;
	ib_dev->num_comp_vectors = hr_dev->caps.num_comp_vectors;
	ib_dev->uverbs_cmd_mask =
654 655 656 657 658 659 660 661 662 663 664 665 666
		(1ULL << IB_USER_VERBS_CMD_GET_CONTEXT) |
		(1ULL << IB_USER_VERBS_CMD_QUERY_DEVICE) |
		(1ULL << IB_USER_VERBS_CMD_QUERY_PORT) |
		(1ULL << IB_USER_VERBS_CMD_ALLOC_PD) |
		(1ULL << IB_USER_VERBS_CMD_DEALLOC_PD) |
		(1ULL << IB_USER_VERBS_CMD_REG_MR) |
		(1ULL << IB_USER_VERBS_CMD_DEREG_MR) |
		(1ULL << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL) |
		(1ULL << IB_USER_VERBS_CMD_CREATE_CQ) |
		(1ULL << IB_USER_VERBS_CMD_DESTROY_CQ) |
		(1ULL << IB_USER_VERBS_CMD_CREATE_QP) |
		(1ULL << IB_USER_VERBS_CMD_MODIFY_QP) |
		(1ULL << IB_USER_VERBS_CMD_QUERY_QP) |
667 668 669
		(1ULL << IB_USER_VERBS_CMD_DESTROY_QP) |
		(1ULL << IB_USER_VERBS_CMD_CREATE_AH) |
		(1ULL << IB_USER_VERBS_CMD_DESTROY_AH);
670

Y
Yixing Liu 已提交
671 672 673 674 675 676
	ib_dev->uverbs_ex_cmd_mask |=
				(1ULL << IB_USER_VERBS_EX_CMD_QUERY_DEVICE) |
				(1ULL << IB_USER_VERBS_EX_CMD_MODIFY_CQ) |
				(1ULL << IB_USER_VERBS_EX_CMD_CREATE_CQ) |
				(1ULL << IB_USER_VERBS_EX_CMD_MODIFY_QP) |
				(1ULL << IB_USER_VERBS_EX_CMD_CREATE_QP);
677

678 679
	if (hr_dev->caps.flags & HNS_ROCE_CAP_FLAG_REREG_MR) {
		ib_dev->uverbs_cmd_mask |= (1ULL << IB_USER_VERBS_CMD_REREG_MR);
680
		ib_set_device_ops(ib_dev, &hns_roce_dev_mr_ops);
681
	}
682

Y
Yixian Liu 已提交
683 684 685 686 687
	/* MW */
	if (hr_dev->caps.flags & HNS_ROCE_CAP_FLAG_MW) {
		ib_dev->uverbs_cmd_mask |=
					(1ULL << IB_USER_VERBS_CMD_ALLOC_MW) |
					(1ULL << IB_USER_VERBS_CMD_DEALLOC_MW);
688
		ib_set_device_ops(ib_dev, &hns_roce_dev_mw_ops);
Y
Yixian Liu 已提交
689 690
	}

Y
Yixian Liu 已提交
691
	/* FRMR */
692 693
	if (hr_dev->caps.flags & HNS_ROCE_CAP_FLAG_FRMR)
		ib_set_device_ops(ib_dev, &hns_roce_dev_frmr_ops);
Y
Yixian Liu 已提交
694

695 696 697 698 699 700 701 702
	/* SRQ */
	if (hr_dev->caps.flags & HNS_ROCE_CAP_FLAG_SRQ) {
		ib_dev->uverbs_cmd_mask |=
				(1ULL << IB_USER_VERBS_CMD_CREATE_SRQ) |
				(1ULL << IB_USER_VERBS_CMD_MODIFY_SRQ) |
				(1ULL << IB_USER_VERBS_CMD_QUERY_SRQ) |
				(1ULL << IB_USER_VERBS_CMD_DESTROY_SRQ) |
				(1ULL << IB_USER_VERBS_CMD_POST_SRQ_RECV);
703 704
		ib_set_device_ops(ib_dev, &hns_roce_dev_srq_ops);
		ib_set_device_ops(ib_dev, hr_dev->hw->hns_roce_dev_srq_ops);
705 706
	}

707 708 709 710 711 712 713 714
	if (hr_dev->caps.flags & HNS_ROCE_CAP_FLAG_XRC) {
		ib_dev->uverbs_cmd_mask |=
				(1ULL << IB_USER_VERBS_CMD_OPEN_XRCD) |
				(1ULL << IB_USER_VERBS_CMD_CLOSE_XRCD) |
				(1ULL << IB_USER_VERBS_CMD_CREATE_XSRQ);
		ib_set_device_ops(ib_dev, &hns_roce_dev_xrcd_ops);
	}

715 716
	ib_set_device_ops(ib_dev, hr_dev->hw->hns_roce_dev_ops);
	ib_set_device_ops(ib_dev, &hns_roce_dev_ops);
717
	ib_set_device_ops(ib_dev, &hns_roce_dev_restrack_ops);
718 719 720 721 722 723 724 725 726
	for (i = 0; i < hr_dev->caps.num_ports; i++) {
		if (!hr_dev->iboe.netdevs[i])
			continue;

		ret = ib_device_set_netdev(ib_dev, hr_dev->iboe.netdevs[i],
					   i + 1);
		if (ret)
			return ret;
	}
727
	dma_set_max_seg_size(dev, UINT_MAX);
J
Junxian Huang 已提交
728 729 730 731 732 733

	if ((hr_dev->caps.flags & HNS_ROCE_CAP_FLAG_BOND) &&
	    (hr_dev->hw->bond_is_active(hr_dev)))
		ret = ib_register_device(ib_dev, "hns_bond_%d", dev);
	else
		ret = ib_register_device(ib_dev, "hns_%d", dev);
734 735 736 737 738
	if (ret) {
		dev_err(dev, "ib_register_device failed!\n");
		return ret;
	}

739
	ret = hns_roce_setup_mtu_mac(hr_dev);
740
	if (ret) {
741 742
		dev_err(dev, "setup_mtu_mac failed!\n");
		goto error_failed_setup_mtu_mac;
743 744 745 746 747 748
	}

	iboe->nb.notifier_call = hns_roce_netdev_event;
	ret = register_netdevice_notifier(&iboe->nb);
	if (ret) {
		dev_err(dev, "register_netdevice_notifier failed!\n");
749
		goto error_failed_setup_mtu_mac;
750 751
	}

J
Junxian Huang 已提交
752 753 754 755 756 757
	if (hr_dev->caps.flags & HNS_ROCE_CAP_FLAG_BOND) {
		ret = hr_dev->hw->bond_init(hr_dev);
		if (ret)
			dev_err(dev, "roce bond init failed, ret = %d\n", ret);
	}

758
	hr_dev->active = true;
J
Junxian Huang 已提交
759 760

	return ret;
761

762
error_failed_setup_mtu_mac:
763 764 765 766 767 768 769
	ib_unregister_device(ib_dev);

	return ret;
}

static int hns_roce_init_hem(struct hns_roce_dev *hr_dev)
{
770
	struct device *dev = hr_dev->dev;
L
Lang Cheng 已提交
771
	int ret;
772 773 774 775 776 777

	ret = hns_roce_init_hem_table(hr_dev, &hr_dev->mr_table.mtpt_table,
				      HEM_TYPE_MTPT, hr_dev->caps.mtpt_entry_sz,
				      hr_dev->caps.num_mtpts, 1);
	if (ret) {
		dev_err(dev, "Failed to init MTPT context memory, aborting.\n");
X
Xi Wang 已提交
778
		return ret;
779 780 781
	}

	ret = hns_roce_init_hem_table(hr_dev, &hr_dev->qp_table.qp_table,
782
				      HEM_TYPE_QPC, hr_dev->caps.qpc_sz,
783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798
				      hr_dev->caps.num_qps, 1);
	if (ret) {
		dev_err(dev, "Failed to init QP context memory, aborting.\n");
		goto err_unmap_dmpt;
	}

	ret = hns_roce_init_hem_table(hr_dev, &hr_dev->qp_table.irrl_table,
				      HEM_TYPE_IRRL,
				      hr_dev->caps.irrl_entry_sz *
				      hr_dev->caps.max_qp_init_rdma,
				      hr_dev->caps.num_qps, 1);
	if (ret) {
		dev_err(dev, "Failed to init irrl_table memory, aborting.\n");
		goto err_unmap_qp;
	}

799 800 801 802 803 804 805 806 807
	if (hr_dev->caps.trrl_entry_sz) {
		ret = hns_roce_init_hem_table(hr_dev,
					      &hr_dev->qp_table.trrl_table,
					      HEM_TYPE_TRRL,
					      hr_dev->caps.trrl_entry_sz *
					      hr_dev->caps.max_qp_dest_rdma,
					      hr_dev->caps.num_qps, 1);
		if (ret) {
			dev_err(dev,
L
Lijun Ou 已提交
808
				"Failed to init trrl_table memory, aborting.\n");
809 810 811 812
			goto err_unmap_irrl;
		}
	}

813 814 815 816 817
	ret = hns_roce_init_hem_table(hr_dev, &hr_dev->cq_table.table,
				      HEM_TYPE_CQC, hr_dev->caps.cqc_entry_sz,
				      hr_dev->caps.num_cqs, 1);
	if (ret) {
		dev_err(dev, "Failed to init CQ context memory, aborting.\n");
818
		goto err_unmap_trrl;
819 820
	}

821
	if (hr_dev->caps.flags & HNS_ROCE_CAP_FLAG_SRQ) {
L
Lijun Ou 已提交
822 823 824 825 826 827
		ret = hns_roce_init_hem_table(hr_dev, &hr_dev->srq_table.table,
					      HEM_TYPE_SRQC,
					      hr_dev->caps.srqc_entry_sz,
					      hr_dev->caps.num_srqs, 1);
		if (ret) {
			dev_err(dev,
L
Lijun Ou 已提交
828
				"Failed to init SRQ context memory, aborting.\n");
L
Lijun Ou 已提交
829 830 831 832
			goto err_unmap_cq;
		}
	}

833
	if (hr_dev->caps.flags & HNS_ROCE_CAP_FLAG_QP_FLOW_CTRL) {
834 835 836
		ret = hns_roce_init_hem_table(hr_dev,
					      &hr_dev->qp_table.sccc_table,
					      HEM_TYPE_SCCC,
837
					      hr_dev->caps.sccc_sz,
838 839 840
					      hr_dev->caps.num_qps, 1);
		if (ret) {
			dev_err(dev,
L
Lijun Ou 已提交
841
				"Failed to init SCC context memory, aborting.\n");
X
Xi Wang 已提交
842
			goto err_unmap_srq;
843 844 845
		}
	}

846
	if (hr_dev->caps.qpc_timer_entry_sz) {
L
Lijun Ou 已提交
847
		ret = hns_roce_init_hem_table(hr_dev, &hr_dev->qpc_timer_table,
848 849 850 851 852
					      HEM_TYPE_QPC_TIMER,
					      hr_dev->caps.qpc_timer_entry_sz,
					      hr_dev->caps.num_qpc_timer, 1);
		if (ret) {
			dev_err(dev,
L
Lijun Ou 已提交
853
				"Failed to init QPC timer memory, aborting.\n");
854 855 856 857 858
			goto err_unmap_ctx;
		}
	}

	if (hr_dev->caps.cqc_timer_entry_sz) {
L
Lijun Ou 已提交
859
		ret = hns_roce_init_hem_table(hr_dev, &hr_dev->cqc_timer_table,
860 861
					      HEM_TYPE_CQC_TIMER,
					      hr_dev->caps.cqc_timer_entry_sz,
862
					      hr_dev->caps.cqc_timer_bt_num, 1);
863 864
		if (ret) {
			dev_err(dev,
L
Lijun Ou 已提交
865
				"Failed to init CQC timer memory, aborting.\n");
866 867 868 869
			goto err_unmap_qpc_timer;
		}
	}

870 871 872 873 874 875 876 877 878 879 880 881 882
	if (hr_dev->caps.gmv_entry_sz) {
		ret = hns_roce_init_hem_table(hr_dev, &hr_dev->gmv_table,
					      HEM_TYPE_GMV,
					      hr_dev->caps.gmv_entry_sz,
					      hr_dev->caps.gmv_entry_num, 1);
		if (ret) {
			dev_err(dev,
				"failed to init gmv table memory, ret = %d\n",
				ret);
			goto err_unmap_cqc_timer;
		}
	}

883 884
	return 0;

885 886 887 888
err_unmap_cqc_timer:
	if (hr_dev->caps.cqc_timer_entry_sz)
		hns_roce_cleanup_hem_table(hr_dev, &hr_dev->cqc_timer_table);

889 890
err_unmap_qpc_timer:
	if (hr_dev->caps.qpc_timer_entry_sz)
L
Lijun Ou 已提交
891
		hns_roce_cleanup_hem_table(hr_dev, &hr_dev->qpc_timer_table);
892 893

err_unmap_ctx:
894
	if (hr_dev->caps.flags & HNS_ROCE_CAP_FLAG_QP_FLOW_CTRL)
895 896
		hns_roce_cleanup_hem_table(hr_dev,
					   &hr_dev->qp_table.sccc_table);
L
Lijun Ou 已提交
897
err_unmap_srq:
898
	if (hr_dev->caps.flags & HNS_ROCE_CAP_FLAG_SRQ)
L
Lijun Ou 已提交
899 900 901 902 903
		hns_roce_cleanup_hem_table(hr_dev, &hr_dev->srq_table.table);

err_unmap_cq:
	hns_roce_cleanup_hem_table(hr_dev, &hr_dev->cq_table.table);

904 905 906 907 908
err_unmap_trrl:
	if (hr_dev->caps.trrl_entry_sz)
		hns_roce_cleanup_hem_table(hr_dev,
					   &hr_dev->qp_table.trrl_table);

909 910 911 912 913 914 915 916 917 918 919 920 921
err_unmap_irrl:
	hns_roce_cleanup_hem_table(hr_dev, &hr_dev->qp_table.irrl_table);

err_unmap_qp:
	hns_roce_cleanup_hem_table(hr_dev, &hr_dev->qp_table.qp_table);

err_unmap_dmpt:
	hns_roce_cleanup_hem_table(hr_dev, &hr_dev->mr_table.mtpt_table);

	return ret;
}

/**
922 923 924 925
 * hns_roce_setup_hca - setup host channel adapter
 * @hr_dev: pointer to hns roce device
 * Return : int
 */
926 927
static int hns_roce_setup_hca(struct hns_roce_dev *hr_dev)
{
928
	struct device *dev = hr_dev->dev;
L
Lang Cheng 已提交
929
	int ret;
930 931 932

	spin_lock_init(&hr_dev->sm_lock);

933 934
	if (hr_dev->caps.flags & HNS_ROCE_CAP_FLAG_CQ_RECORD_DB ||
	    hr_dev->caps.flags & HNS_ROCE_CAP_FLAG_QP_RECORD_DB) {
935 936 937 938
		INIT_LIST_HEAD(&hr_dev->pgdir_list);
		mutex_init(&hr_dev->pgdir_mutex);
	}

939
	hns_roce_init_uar_table(hr_dev);
940 941 942 943 944 945 946

	ret = hns_roce_uar_alloc(hr_dev, &hr_dev->priv_uar);
	if (ret) {
		dev_err(dev, "Failed to allocate priv_uar.\n");
		goto err_uar_table_free;
	}

947 948 949 950 951 952
	ret = hns_roce_init_qp_table(hr_dev);
	if (ret) {
		dev_err(dev, "Failed to init qp_table.\n");
		goto err_uar_table_free;
	}

953
	hns_roce_init_pd_table(hr_dev);
954

955 956
	if (hr_dev->caps.flags & HNS_ROCE_CAP_FLAG_XRC)
		hns_roce_init_xrcd_table(hr_dev);
957

958
	hns_roce_init_mr_table(hr_dev);
959

960
	hns_roce_init_cq_table(hr_dev);
961

L
Lijun Ou 已提交
962
	if (hr_dev->caps.flags & HNS_ROCE_CAP_FLAG_SRQ) {
963
		hns_roce_init_srq_table(hr_dev);
L
Lijun Ou 已提交
964 965
	}

966 967 968
	return 0;

err_uar_table_free:
969
	ida_destroy(&hr_dev->uar_ida.ida);
970 971 972
	return ret;
}

973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016
static void check_and_get_armed_cq(struct list_head *cq_list, struct ib_cq *cq)
{
	struct hns_roce_cq *hr_cq = to_hr_cq(cq);
	unsigned long flags;

	spin_lock_irqsave(&hr_cq->lock, flags);
	if (cq->comp_handler) {
		if (!hr_cq->is_armed) {
			hr_cq->is_armed = 1;
			list_add_tail(&hr_cq->node, cq_list);
		}
	}
	spin_unlock_irqrestore(&hr_cq->lock, flags);
}

void hns_roce_handle_device_err(struct hns_roce_dev *hr_dev)
{
	struct hns_roce_qp *hr_qp;
	struct hns_roce_cq *hr_cq;
	struct list_head cq_list;
	unsigned long flags_qp;
	unsigned long flags;

	INIT_LIST_HEAD(&cq_list);

	spin_lock_irqsave(&hr_dev->qp_list_lock, flags);
	list_for_each_entry(hr_qp, &hr_dev->qp_list, node) {
		spin_lock_irqsave(&hr_qp->sq.lock, flags_qp);
		if (hr_qp->sq.tail != hr_qp->sq.head)
			check_and_get_armed_cq(&cq_list, hr_qp->ibqp.send_cq);
		spin_unlock_irqrestore(&hr_qp->sq.lock, flags_qp);

		spin_lock_irqsave(&hr_qp->rq.lock, flags_qp);
		if ((!hr_qp->ibqp.srq) && (hr_qp->rq.tail != hr_qp->rq.head))
			check_and_get_armed_cq(&cq_list, hr_qp->ibqp.recv_cq);
		spin_unlock_irqrestore(&hr_qp->rq.lock, flags_qp);
	}

	list_for_each_entry(hr_cq, &cq_list, node)
		hns_roce_cq_completion(hr_dev, hr_cq->cqn);

	spin_unlock_irqrestore(&hr_dev->qp_list_lock, flags);
}

1017
int hns_roce_init(struct hns_roce_dev *hr_dev)
1018
{
1019
	struct device *dev = hr_dev->dev;
L
Lang Cheng 已提交
1020
	int ret;
1021

1022
	hr_dev->is_reset = false;
1023

1024 1025 1026 1027
	if (hr_dev->hw->cmq_init) {
		ret = hr_dev->hw->cmq_init(hr_dev);
		if (ret) {
			dev_err(dev, "Init RoCE Command Queue failed!\n");
1028
			return ret;
1029 1030 1031
		}
	}

1032 1033 1034 1035 1036
	ret = hr_dev->hw->hw_profile(hr_dev);
	if (ret) {
		dev_err(dev, "Get RoCE engine profile failed!\n");
		goto error_failed_cmd_init;
	}
1037 1038 1039 1040 1041 1042 1043

	ret = hns_roce_cmd_init(hr_dev);
	if (ret) {
		dev_err(dev, "cmd init failed!\n");
		goto error_failed_cmd_init;
	}

1044
	/* EQ depends on poll mode, event mode depends on EQ */
Y
Yixian Liu 已提交
1045 1046 1047 1048
	ret = hr_dev->hw->init_eq(hr_dev);
	if (ret) {
		dev_err(dev, "eq init failed!\n");
		goto error_failed_eq_table;
1049 1050 1051 1052
	}

	if (hr_dev->cmd_mod) {
		ret = hns_roce_cmd_use_events(hr_dev);
1053
		if (ret)
1054 1055
			dev_warn(dev,
				 "Cmd event  mode failed, set back to poll!\n");
1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069
	}

	ret = hns_roce_init_hem(hr_dev);
	if (ret) {
		dev_err(dev, "init HEM(Hardware Entry Memory) failed!\n");
		goto error_failed_init_hem;
	}

	ret = hns_roce_setup_hca(hr_dev);
	if (ret) {
		dev_err(dev, "setup hca failed!\n");
		goto error_failed_setup_hca;
	}

1070 1071 1072 1073 1074 1075
	if (hr_dev->hw->hw_init) {
		ret = hr_dev->hw->hw_init(hr_dev);
		if (ret) {
			dev_err(dev, "hw_init failed!\n");
			goto error_failed_engine_init;
		}
1076 1077
	}

1078 1079
	INIT_LIST_HEAD(&hr_dev->qp_list);
	spin_lock_init(&hr_dev->qp_list_lock);
1080 1081
	INIT_LIST_HEAD(&hr_dev->dip_list);
	spin_lock_init(&hr_dev->dip_list_lock);
1082

1083 1084 1085 1086 1087 1088 1089
	ret = hns_roce_register_device(hr_dev);
	if (ret)
		goto error_failed_register_device;

	return 0;

error_failed_register_device:
1090 1091
	if (hr_dev->hw->hw_exit)
		hr_dev->hw->hw_exit(hr_dev);
1092 1093 1094 1095 1096 1097 1098 1099 1100 1101

error_failed_engine_init:
	hns_roce_cleanup_bitmap(hr_dev);

error_failed_setup_hca:
	hns_roce_cleanup_hem(hr_dev);

error_failed_init_hem:
	if (hr_dev->cmd_mod)
		hns_roce_cmd_use_polling(hr_dev);
Y
Yixian Liu 已提交
1102
	hr_dev->hw->cleanup_eq(hr_dev);
1103 1104 1105 1106 1107

error_failed_eq_table:
	hns_roce_cmd_cleanup(hr_dev);

error_failed_cmd_init:
1108 1109 1110
	if (hr_dev->hw->cmq_exit)
		hr_dev->hw->cmq_exit(hr_dev);

1111 1112 1113
	return ret;
}

1114
void hns_roce_exit(struct hns_roce_dev *hr_dev)
1115 1116
{
	hns_roce_unregister_device(hr_dev);
1117

1118 1119
	if (hr_dev->hw->hw_exit)
		hr_dev->hw->hw_exit(hr_dev);
1120 1121 1122 1123 1124 1125
	hns_roce_cleanup_bitmap(hr_dev);
	hns_roce_cleanup_hem(hr_dev);

	if (hr_dev->cmd_mod)
		hns_roce_cmd_use_polling(hr_dev);

Y
Yixian Liu 已提交
1126
	hr_dev->hw->cleanup_eq(hr_dev);
1127
	hns_roce_cmd_cleanup(hr_dev);
1128 1129
	if (hr_dev->hw->cmq_exit)
		hr_dev->hw->cmq_exit(hr_dev);
1130 1131 1132 1133 1134 1135 1136
}

MODULE_LICENSE("Dual BSD/GPL");
MODULE_AUTHOR("Wei Hu <xavier.huwei@huawei.com>");
MODULE_AUTHOR("Nenglong Zhao <zhaonenglong@hisilicon.com>");
MODULE_AUTHOR("Lijun Ou <oulijun@huawei.com>");
MODULE_DESCRIPTION("HNS RoCE Driver");