hns_roce_main.c 40.4 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>
40
#include <rdma/uverbs_ioctl.h>
J
Junxian Huang 已提交
41 42

#include "hnae3.h"
43 44 45
#include "hns_roce_common.h"
#include "hns_roce_device.h"
#include "hns_roce_hem.h"
J
Junxian Huang 已提交
46
#include "hns_roce_hw_v2.h"
47
#include "hns_roce_dca.h"
48
#include "hns_roce_debugfs.h"
49

50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
static struct net_device *hns_roce_get_netdev(struct ib_device *ib_dev,
					      u8 port_num)
{
	struct hns_roce_dev *hr_dev = to_hr_dev(ib_dev);
	struct net_device *ndev;

	if (port_num < 1 || port_num > hr_dev->caps.num_ports)
		return NULL;

	ndev = hr_dev->hw->get_bond_netdev(hr_dev);

	rcu_read_lock();

	if (!ndev)
		ndev = hr_dev->iboe.netdevs[port_num - 1];

	if (ndev)
		dev_hold(ndev);

	rcu_read_unlock();

	return ndev;
}

74 75
static int hns_roce_set_mac(struct hns_roce_dev *hr_dev, u32 port,
			    const u8 *addr)
76 77
{
	u8 phy_port;
78 79 80 81
	u32 i;

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

83
	if (!memcmp(hr_dev->dev_addr[port], addr, ETH_ALEN))
84
		return 0;
85

86
	for (i = 0; i < ETH_ALEN; i++)
87 88 89
		hr_dev->dev_addr[port][i] = addr[i];

	phy_port = hr_dev->iboe.phy_port[port];
90
	return hr_dev->hw->set_mac(hr_dev, phy_port, addr);
91 92
}

93
static int hns_roce_add_gid(const struct ib_gid_attr *attr, void **context)
94
{
95 96
	struct hns_roce_dev *hr_dev = to_hr_dev(attr->device);
	u8 port = attr->port_num - 1;
97
	int ret;
98 99 100

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

102
	ret = hr_dev->hw->set_gid(hr_dev, attr->index, &attr->gid, attr);
103

104
	return ret;
105 106
}

107
static int hns_roce_del_gid(const struct ib_gid_attr *attr, void **context)
108
{
109 110
	struct hns_roce_dev *hr_dev = to_hr_dev(attr->device);
	u8 port = attr->port_num - 1;
111
	int ret;
112 113 114 115

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

116
	ret = hr_dev->hw->set_gid(hr_dev, attr->index, NULL, NULL);
117

118
	return ret;
119 120
}

121 122 123 124 125 126 127 128 129 130 131 132 133
static enum ib_port_state get_upper_port_state(struct hns_roce_dev *hr_dev)
{
	struct hns_roce_bond_group *bond_grp;
	struct net_device *upper;

	bond_grp = hns_roce_get_bond_grp(hr_dev);
	upper = bond_grp ? bond_grp->upper_dev : NULL;
	if (upper)
		return get_port_state(upper);

	return IB_PORT_ACTIVE;
}

134 135 136
static int handle_en_event(struct net_device *netdev,
			   struct hns_roce_dev *hr_dev,
			   u32 port, unsigned long dev_event)
137
{
138
	struct device *dev = hr_dev->dev;
139 140 141
	enum ib_port_state port_state;
	struct ib_event event;
	unsigned long flags;
142
	int ret = 0;
143 144

	if (!netdev) {
145
		dev_err(dev, "Can't find netdev on port(%u)!\n", port);
146 147 148
		return -ENODEV;
	}

149
	switch (dev_event) {
150 151
	case NETDEV_REGISTER:
	case NETDEV_CHANGEADDR:
152
		ret = hns_roce_set_mac(hr_dev, port, netdev->dev_addr);
153
		break;
154 155 156 157 158 159
	case NETDEV_UP:
	case NETDEV_CHANGE:
		ret = hns_roce_set_mac(hr_dev, port, netdev->dev_addr);
		if (ret)
			return ret;
		fallthrough;
160
	case NETDEV_DOWN:
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
		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:
178 179
		break;
	default:
180
		dev_dbg(dev, "NETDEV event = 0x%x!\n", (u32)(dev_event));
181 182 183
		break;
	}

184
	return ret;
185 186 187 188 189 190
}

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);
191
	struct hns_roce_bond_group *bond_grp;
192 193
	struct hns_roce_ib_iboe *iboe = NULL;
	struct hns_roce_dev *hr_dev = NULL;
194
	struct net_device *upper = NULL;
195 196
	int ret;
	u8 port;
197 198 199

	hr_dev = container_of(self, struct hns_roce_dev, iboe.nb);
	iboe = &hr_dev->iboe;
200 201 202 203
	if (hr_dev->caps.flags & HNS_ROCE_CAP_FLAG_BOND) {
		bond_grp = hns_roce_get_bond_grp(hr_dev);
		upper = bond_grp ? bond_grp->upper_dev : NULL;
	}
204 205

	for (port = 0; port < hr_dev->caps.num_ports; port++) {
206 207 208
		if ((!upper && dev == iboe->netdevs[port]) ||
		    (upper && dev == upper)) {
			ret = handle_en_event(dev, hr_dev, port, event);
209 210 211 212 213 214 215 216 217
			if (ret)
				return NOTIFY_DONE;
			break;
		}
	}

	return NOTIFY_DONE;
}

218
static int hns_roce_setup_mtu_mac(struct hns_roce_dev *hr_dev)
219
{
220
	int ret;
221
	u8 i;
222 223

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

226 227 228 229
		ret = hns_roce_set_mac(hr_dev, i,
				       hr_dev->iboe.netdevs[i]->dev_addr);
		if (ret)
			return ret;
230 231
	}

232
	return 0;
233 234 235 236 237 238 239 240 241 242
}

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));

243
	props->fw_ver = hr_dev->caps.fw_ver;
244
	props->sys_image_guid = cpu_to_be64(hr_dev->sys_image_guid);
245 246 247 248 249 250 251 252
	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 |
253
				  IB_DEVICE_RC_RNR_NAK_GEN;
254 255
	props->max_send_sge = hr_dev->caps.max_sq_sg;
	props->max_recv_sge = hr_dev->caps.max_rq_sg;
256 257 258 259 260 261 262
	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 已提交
263 264
	props->atomic_cap = hr_dev->caps.flags & HNS_ROCE_CAP_FLAG_ATOMIC ?
			    IB_ATOMIC_HCA : IB_ATOMIC_NONE;
265 266
	props->max_pkeys = 1;
	props->local_ca_ack_delay = hr_dev->caps.local_ca_ack_delay;
L
Lijun Ou 已提交
267
	if (hr_dev->caps.flags & HNS_ROCE_CAP_FLAG_SRQ) {
268
		props->max_srq = hr_dev->caps.num_srqs;
L
Lijun Ou 已提交
269 270 271
		props->max_srq_wr = hr_dev->caps.max_srq_wrs;
		props->max_srq_sge = hr_dev->caps.max_srq_sges;
	}
272

273 274
	if (hr_dev->caps.flags & HNS_ROCE_CAP_FLAG_FRMR &&
	    hr_dev->pci_dev->revision >= PCI_REVISION_ID_HIP09) {
275 276 277 278
		props->device_cap_flags |= IB_DEVICE_MEM_MGT_EXTENSIONS;
		props->max_fast_reg_page_list_len = HNS_ROCE_FRMR_MAX_PA;
	}

279 280 281
	if (hr_dev->caps.flags & HNS_ROCE_CAP_FLAG_XRC)
		props->device_cap_flags |= IB_DEVICE_XRC;

282 283 284 285 286 287 288
	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);
289
	struct device *dev = hr_dev->dev;
290 291 292 293 294 295 296
	struct net_device *net_dev;
	unsigned long flags;
	enum ib_mtu mtu;
	u8 port;

	port = port_num - 1;

297
	/* props being zeroed by the caller, avoid zeroing it here */
298 299 300 301 302 303 304 305 306 307 308 309 310

	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 已提交
311 312 313
	net_dev = hr_dev->hw->get_bond_netdev(hr_dev);
	if (!net_dev)
		net_dev = hr_dev->iboe.netdevs[port];
314 315
	if (!net_dev) {
		spin_unlock_irqrestore(&hr_dev->iboe.lock, flags);
316
		dev_err(dev, "Find netdev %u failed!\n", port);
317 318 319 320 321
		return -EINVAL;
	}

	mtu = iboe_get_mtu(net_dev->mtu);
	props->active_mtu = mtu ? min(props->max_mtu, mtu) : IB_MTU_256;
322
	props->state = get_port_state(net_dev);
323 324 325 326 327

	if (hr_dev->caps.flags & HNS_ROCE_CAP_FLAG_BOND &&
	    props->state == IB_PORT_ACTIVE)
		props->state = get_upper_port_state(hr_dev);

L
Lijun Ou 已提交
328 329 330
	props->phys_state = props->state == IB_PORT_ACTIVE ?
				    IB_PORT_PHYS_STATE_LINK_UP :
				    IB_PORT_PHYS_STATE_DISABLED;
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345

	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 已提交
346 347 348
	if (index > 0)
		return -EINVAL;

349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
	*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;
}

371 372 373 374 375 376 377 378 379 380 381 382 383 384 385
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;

386
	switch (mmap_type) {
387
	/* pgoff 0 must be used by DB for compatibility */
388 389 390 391 392
	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:
393
	case HNS_ROCE_MMAP_TYPE_DCA:
394
	case HNS_ROCE_MMAP_TYPE_RESET:
395
		ret = rdma_user_mmap_entry_insert_range(
396
				ucontext, &entry->rdma_entry, length, 1,
397 398 399 400 401 402 403
				U32_MAX);
		break;
	default:
		ret = -EINVAL;
		break;
	}

404 405 406 407 408 409 410 411 412 413 414 415 416
	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);
417 418 419
	if (context->dca_ctx.dca_mmap_entry)
		rdma_user_mmap_entry_remove(
			&context->dca_ctx.dca_mmap_entry->rdma_entry);
420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435
}

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;
}

436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455
static void hns_roce_dealloc_reset_entry(struct hns_roce_ucontext *context)
{
	if (context->reset_mmap_entry)
		rdma_user_mmap_entry_remove(&context->reset_mmap_entry->rdma_entry);
}

static int hns_roce_alloc_reset_entry(struct ib_ucontext *uctx)
{
	struct hns_roce_ucontext *context = to_hr_ucontext(uctx);
	struct hns_roce_dev *hr_dev = to_hr_dev(uctx->device);

	context->reset_mmap_entry = hns_roce_user_mmap_entry_insert(uctx,
		(u64)hr_dev->reset_kaddr, PAGE_SIZE, HNS_ROCE_MMAP_TYPE_RESET);

	if (!context->reset_mmap_entry)
		return -ENOMEM;

	return 0;
}

456 457 458
static void ucontext_set_resp(struct ib_ucontext *uctx,
			      struct hns_roce_ib_alloc_ucontext_resp *resp)
{
459
	struct hns_roce_ucontext *context = to_hr_ucontext(uctx);
460
	struct hns_roce_dev *hr_dev = to_hr_dev(uctx->device);
461
	struct rdma_user_mmap_entry *rdma_entry;
462 463 464 465 466

	resp->qp_tab_size = hr_dev->caps.num_qps;
	resp->srq_tab_size = hr_dev->caps.num_srqs;
	resp->cqe_size = hr_dev->caps.cqe_sz;
	resp->mac_type = hr_dev->mac_type;
467 468 469 470

	if (hr_dev->pci_dev->revision >= PCI_REVISION_ID_HIP09)
		resp->congest_type = hr_dev->caps.congest_type;

471 472 473 474 475 476
	if (context->dca_ctx.dca_mmap_entry) {
		resp->dca_qps = context->dca_ctx.max_qps;
		resp->dca_mmap_size = PAGE_SIZE * context->dca_ctx.status_npage;
		rdma_entry = &context->dca_ctx.dca_mmap_entry->rdma_entry;
		resp->dca_mmap_key = rdma_user_mmap_get_offset(rdma_entry);
	}
477 478 479 480 481

	if (context->reset_mmap_entry) {
		rdma_entry = &context->reset_mmap_entry->rdma_entry;
		resp->reset_mmap_key = rdma_user_mmap_get_offset(rdma_entry);
	}
482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497
}

static u32 get_udca_max_qps(struct hns_roce_dev *hr_dev,
			    struct hns_roce_ib_alloc_ucontext *ucmd)
{
	u32 qp_num;

	if (ucmd->comp & HNS_ROCE_ALLOC_UCTX_COMP_DCA_MAX_QPS) {
		qp_num = ucmd->dca_max_qps;
		if (!qp_num)
			qp_num = hr_dev->caps.num_qps;
	} else {
		qp_num = 0;
	}

	return qp_num;
498 499
}

500 501
static int hns_roce_alloc_ucontext(struct ib_ucontext *uctx,
				   struct ib_udata *udata)
502
{
503 504
	struct hns_roce_ucontext *context = to_hr_ucontext(uctx);
	struct hns_roce_dev *hr_dev = to_hr_dev(uctx->device);
L
Luoyouming 已提交
505 506
	struct hns_roce_ib_alloc_ucontext_resp resp = {};
	struct hns_roce_ib_alloc_ucontext ucmd = {};
C
Chengchang Tang 已提交
507
	int ret = -EAGAIN;
508

509
	if (!hr_dev->active)
C
Chengchang Tang 已提交
510
		goto error_fail_uar_alloc;
511

512 513 514
	context->pid = current->pid;
	INIT_LIST_HEAD(&context->list);

L
Luoyouming 已提交
515 516 517
	ret = ib_copy_from_udata(&ucmd, udata,
				 min(udata->inlen, sizeof(ucmd)));
	if (ret)
C
Chengchang Tang 已提交
518
		goto error_fail_uar_alloc;
L
Luoyouming 已提交
519 520 521 522 523 524 525 526 527

	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;
	}

528 529 530 531 532
	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;
	}

533 534 535 536 537
	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;
	}

538 539 540 541 542
	if (hr_dev->caps.flags & HNS_ROCE_CAP_FLAG_DCA_MODE) {
		context->config |= ucmd.config & HNS_ROCE_UCTX_CONFIG_DCA;
		resp.config |= HNS_ROCE_UCTX_RSP_DCA_FLAGS;
	}

543 544 545 546
	ret = hns_roce_uar_alloc(hr_dev, &context->uar);
	if (ret)
		goto error_fail_uar_alloc;

547 548 549 550
	ret = hns_roce_alloc_uar_entry(uctx);
	if (ret)
		goto error_fail_uar_entry;

551 552
	if (hr_dev->caps.flags & HNS_ROCE_CAP_FLAG_CQ_RECORD_DB ||
	    hr_dev->caps.flags & HNS_ROCE_CAP_FLAG_QP_RECORD_DB) {
553 554 555 556
		INIT_LIST_HEAD(&context->page_list);
		mutex_init(&context->page_mutex);
	}

557 558
	hns_roce_register_udca(hr_dev, get_udca_max_qps(hr_dev, &ucmd),
			       context);
559

560 561 562 563
	ret = hns_roce_alloc_reset_entry(uctx);
	if (ret)
		goto error_fail_reset_entry;

564 565
	ucontext_set_resp(uctx, &resp);
	ret = ib_copy_to_udata(udata, &resp, min(udata->outlen, sizeof(resp)));
566 567 568
	if (ret)
		goto error_fail_copy_to_udata;

569 570 571 572 573 574
	spin_lock(&hr_dev->uctx_list_lock);
	list_add(&context->list, &hr_dev->uctx_list);
	spin_unlock(&hr_dev->uctx_list_lock);

	hns_roce_register_uctx_debugfs(hr_dev, context);

575
	return 0;
576 577

error_fail_copy_to_udata:
578
	hns_roce_unregister_udca(hr_dev, context);
579
	hns_roce_dealloc_reset_entry(context);
580

581
error_fail_reset_entry:
582 583 584
	hns_roce_dealloc_uar_entry(context);

error_fail_uar_entry:
585
	ida_free(&hr_dev->uar_ida.ida, (int)context->uar.logic_idx);
586 587

error_fail_uar_alloc:
C
Chengchang Tang 已提交
588 589
	atomic64_inc(&hr_dev->dfx_cnt[HNS_ROCE_DFX_UCTX_ALLOC_ERR_CNT]);

590
	return ret;
591 592
}

593
static void hns_roce_dealloc_ucontext(struct ib_ucontext *ibcontext)
594 595
{
	struct hns_roce_ucontext *context = to_hr_ucontext(ibcontext);
596
	struct hns_roce_dev *hr_dev = to_hr_dev(ibcontext->device);
597

598 599 600 601 602 603
	spin_lock(&hr_dev->uctx_list_lock);
	list_del(&context->list);
	spin_unlock(&hr_dev->uctx_list_lock);

	hns_roce_unregister_uctx_debugfs(hr_dev, context);

604 605
	hns_roce_unregister_udca(hr_dev, context);

606
	hns_roce_dealloc_uar_entry(context);
607
	hns_roce_dealloc_reset_entry(context);
608

609
	ida_free(&hr_dev->uar_ida.ida, (int)context->uar.logic_idx);
610 611
}

612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641
static int mmap_dca(struct ib_ucontext *context, struct vm_area_struct *vma)
{
	struct hns_roce_ucontext *uctx = to_hr_ucontext(context);
	struct hns_roce_dca_ctx *ctx = &uctx->dca_ctx;
	struct page **pages;
	unsigned long num;
	int ret;

	if ((vma->vm_end - vma->vm_start != (ctx->status_npage * PAGE_SIZE) ||
	     !(vma->vm_flags & VM_SHARED)))
		return -EINVAL;

	if (!(vma->vm_flags & VM_WRITE) || (vma->vm_flags & VM_EXEC))
		return -EPERM;

	if (!ctx->buf_status)
		return -EOPNOTSUPP;

	pages = kcalloc(ctx->status_npage, sizeof(struct page *), GFP_KERNEL);
	if (!pages)
		return -ENOMEM;

	for (num = 0; num < ctx->status_npage; num++)
		pages[num] = virt_to_page(ctx->buf_status + num * PAGE_SIZE);

	ret = vm_insert_pages(vma, vma->vm_start, pages, &num);
	kfree(pages);
	return ret;
}

642
static int hns_roce_mmap(struct ib_ucontext *uctx, struct vm_area_struct *vma)
643
{
644
	struct hns_roce_dev *hr_dev = to_hr_dev(uctx->device);
645 646 647 648 649
	struct rdma_user_mmap_entry *rdma_entry;
	struct hns_user_mmap_entry *entry;
	phys_addr_t pfn;
	pgprot_t prot;
	int ret;
650

651
	rdma_entry = rdma_user_mmap_entry_get_pgoff(uctx, vma->vm_pgoff);
C
Chengchang Tang 已提交
652 653 654 655
	if (!rdma_entry) {
		atomic64_inc(&hr_dev->dfx_cnt[HNS_ROCE_DFX_MMAP_ERR_CNT]);
		return -EINVAL;
	}
656

657 658 659
	entry = to_hns_mmap(rdma_entry);
	pfn = entry->address >> PAGE_SHIFT;

660 661 662 663 664
	switch (entry->mmap_type) {
	case HNS_ROCE_MMAP_TYPE_DB:
	case HNS_ROCE_MMAP_TYPE_DWQE:
		prot = pgprot_device(vma->vm_page_prot);
		break;
665 666 667
	case HNS_ROCE_MMAP_TYPE_DCA:
		ret = mmap_dca(uctx, vma);
		goto out;
668 669 670 671 672 673 674 675 676 677
	case HNS_ROCE_MMAP_TYPE_RESET:
		if (vma->vm_flags & (VM_WRITE | VM_EXEC)) {
			ret = -EINVAL;
			goto out;
		}

		ret = remap_pfn_range(vma, vma->vm_start,
				      page_to_pfn(hr_dev->reset_page),
				      PAGE_SIZE, vma->vm_page_prot);
		goto out;
678
	default:
679 680
		ret = -EINVAL;
		goto out;
681
	}
682 683 684 685

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

686
out:
687
	rdma_user_mmap_entry_put(rdma_entry);
C
Chengchang Tang 已提交
688 689
	if (ret)
		atomic64_inc(&hr_dev->dfx_cnt[HNS_ROCE_DFX_MMAP_ERR_CNT]);
690 691 692 693 694 695 696 697 698

	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);
699 700 701 702 703 704 705 706
}

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;

707
	ret = ib_query_port(ib_dev, port_num, &attr);
708 709 710 711 712 713 714
	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;
715 716 717 718 719 720 721 722

	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;
723 724 725 726

	return 0;
}

727 728 729 730
static void hns_roce_disassociate_ucontext(struct ib_ucontext *ibcontext)
{
}

731 732 733 734 735 736 737 738 739 740 741 742 743
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);
}

C
Chengchang Tang 已提交
744 745 746
#define HNS_ROCE_DFX_STATS(ename, cname) \
	[HNS_ROCE_DFX_##ename##_CNT] = cname

C
Chengchang Tang 已提交
747
#define HNS_ROCE_HW_CNT(ename, cname) \
C
Chengchang Tang 已提交
748
	[HNS_ROCE_DFX_CNT_TOTAL + HNS_ROCE_HW_##ename##_CNT] = cname
C
Chengchang Tang 已提交
749 750

static const char *const hns_roce_port_stats_descs[] = {
C
Chengchang Tang 已提交
751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767
	HNS_ROCE_DFX_STATS(AEQE, "aeqe"),
	HNS_ROCE_DFX_STATS(CEQE, "ceqe"),
	HNS_ROCE_DFX_STATS(CMDS, "cmds"),
	HNS_ROCE_DFX_STATS(CMDS_ERR, "cmds_err"),
	HNS_ROCE_DFX_STATS(MBX_POSTED, "posted_mbx"),
	HNS_ROCE_DFX_STATS(MBX_POLLED, "polled_mbx"),
	HNS_ROCE_DFX_STATS(MBX_EVENT, "mbx_event"),
	HNS_ROCE_DFX_STATS(QP_CREATE_ERR, "qp_create_err"),
	HNS_ROCE_DFX_STATS(QP_MODIFY_ERR, "qp_modify_err"),
	HNS_ROCE_DFX_STATS(CQ_CREATE_ERR, "cq_create_err"),
	HNS_ROCE_DFX_STATS(SRQ_CREATE_ERR, "srq_create_err"),
	HNS_ROCE_DFX_STATS(XRCD_ALLOC_ERR, "xrcd_alloc_err"),
	HNS_ROCE_DFX_STATS(MR_REG_ERR, "mr_reg_err"),
	HNS_ROCE_DFX_STATS(MR_REREG_ERR, "mr_rereg_err"),
	HNS_ROCE_DFX_STATS(AH_CREATE_ERR, "ah_create_err"),
	HNS_ROCE_DFX_STATS(MMAP_ERR, "mmap_err"),
	HNS_ROCE_DFX_STATS(UCTX_ALLOC_ERR, "uctx_alloc_err"),
C
Chengchang Tang 已提交
768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795
	HNS_ROCE_HW_CNT(RX_RC_PKT, "rx_rc_pkt"),
	HNS_ROCE_HW_CNT(RX_UC_PKT, "rx_uc_pkt"),
	HNS_ROCE_HW_CNT(RX_UD_PKT, "rx_ud_pkt"),
	HNS_ROCE_HW_CNT(RX_XRC_PKT, "rx_xrc_pkt"),
	HNS_ROCE_HW_CNT(RX_PKT, "rx_pkt"),
	HNS_ROCE_HW_CNT(RX_ERR_PKT, "rx_err_pkt"),
	HNS_ROCE_HW_CNT(RX_CNP_PKT, "rx_cnp_pkt"),
	HNS_ROCE_HW_CNT(TX_RC_PKT, "tx_rc_pkt"),
	HNS_ROCE_HW_CNT(TX_UC_PKT, "tx_uc_pkt"),
	HNS_ROCE_HW_CNT(TX_UD_PKT, "tx_ud_pkt"),
	HNS_ROCE_HW_CNT(TX_XRC_PKT, "tx_xrc_pkt"),
	HNS_ROCE_HW_CNT(TX_PKT, "tx_pkt"),
	HNS_ROCE_HW_CNT(TX_ERR_PKT, "tx_err_pkt"),
	HNS_ROCE_HW_CNT(TX_CNP_PKT, "tx_cnp_pkt"),
	HNS_ROCE_HW_CNT(TRP_GET_MPT_ERR_PKT, "trp_get_mpt_err_pkt"),
	HNS_ROCE_HW_CNT(TRP_GET_IRRL_ERR_PKT, "trp_get_irrl_err_pkt"),
	HNS_ROCE_HW_CNT(ECN_DB, "ecn_doorbell"),
	HNS_ROCE_HW_CNT(RX_BUF, "rx_buffer"),
	HNS_ROCE_HW_CNT(TRP_RX_SOF, "trp_rx_sof"),
	HNS_ROCE_HW_CNT(CQ_CQE, "cq_cqe"),
	HNS_ROCE_HW_CNT(CQ_POE, "cq_poe"),
	HNS_ROCE_HW_CNT(CQ_NOTIFY, "cq_notify"),
};

static struct rdma_hw_stats *hns_roce_alloc_hw_port_stats(struct ib_device *device,
							  u8 port_num)
{
	struct hns_roce_dev *hr_dev = to_hr_dev(device);
C
Chengchang Tang 已提交
796
	int num_counters;
C
Chengchang Tang 已提交
797

C
Chengchang Tang 已提交
798
	if (port_num > hr_dev->caps.num_ports) {
C
Chengchang Tang 已提交
799 800 801 802
		ibdev_err(device, "invalid port num.\n");
		return NULL;
	}

C
Chengchang Tang 已提交
803 804 805 806 807 808
	if (hr_dev->pci_dev->revision >= PCI_REVISION_ID_HIP09 &&
	    !hr_dev->is_vf)
		num_counters = ARRAY_SIZE(hns_roce_port_stats_descs);
	else
		num_counters = HNS_ROCE_DFX_CNT_TOTAL;

C
Chengchang Tang 已提交
809
	return rdma_alloc_hw_stats_struct(hns_roce_port_stats_descs,
C
Chengchang Tang 已提交
810
					  num_counters,
C
Chengchang Tang 已提交
811 812 813 814 815 816 817 818
					  RDMA_HW_STATS_DEFAULT_LIFESPAN);
}

static int hns_roce_get_hw_stats(struct ib_device *device,
				 struct rdma_hw_stats *stats,
				 u8 port, int index)
{
	struct hns_roce_dev *hr_dev = to_hr_dev(device);
C
Chengchang Tang 已提交
819
	int hw_counters = HNS_ROCE_HW_CNT_TOTAL;
C
Chengchang Tang 已提交
820
	int ret;
C
Chengchang Tang 已提交
821
	int i;
C
Chengchang Tang 已提交
822 823 824 825 826 827 828

	if (port == 0)
		return 0;

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

C
Chengchang Tang 已提交
829 830 831
	for (i = 0; i < HNS_ROCE_DFX_CNT_TOTAL; i++)
		stats->value[i] = atomic64_read(&hr_dev->dfx_cnt[i]);

C
Chengchang Tang 已提交
832 833
	if (hr_dev->pci_dev->revision <= PCI_REVISION_ID_HIP08 ||
	    hr_dev->is_vf)
C
Chengchang Tang 已提交
834
		return HNS_ROCE_DFX_CNT_TOTAL;
C
Chengchang Tang 已提交
835

C
Chengchang Tang 已提交
836 837 838 839
	hw_counters = HNS_ROCE_HW_CNT_TOTAL;
	ret = hr_dev->hw->query_hw_counter(hr_dev,
					&stats->value[HNS_ROCE_DFX_CNT_TOTAL],
					port, &hw_counters);
C
Chengchang Tang 已提交
840 841 842 843 844 845
	if (ret) {
		ibdev_err(device, "failed to query hw counter, ret = %d.\n",
			  ret);
		return ret;
	}

C
Chengchang Tang 已提交
846
	return hw_counters + HNS_ROCE_DFX_CNT_TOTAL;
C
Chengchang Tang 已提交
847 848
}

849 850
static void hns_roce_unregister_device(struct hns_roce_dev *hr_dev,
				       bool bond_cleanup)
851 852
{
	struct hns_roce_ib_iboe *iboe = &hr_dev->iboe;
853
	struct hns_roce_v2_priv *priv = hr_dev->priv;
854
	struct hns_roce_bond_group *bond_grp;
855

856
	if (hr_dev->caps.flags & HNS_ROCE_CAP_FLAG_BOND) {
857
		unregister_netdevice_notifier(&hr_dev->bond_nb);
858
		bond_grp = hns_roce_get_bond_grp(hr_dev);
859 860 861 862 863 864 865
		if (bond_grp) {
			if (bond_cleanup)
				hns_roce_cleanup_bond(bond_grp);
			else if (priv->handle->rinfo.reset_state ==
				 HNS_ROCE_STATE_RST_UNINIT)
				bond_grp->main_hr_dev = NULL;
		}
866
	}
J
Junxian Huang 已提交
867

868
	hr_dev->active = false;
869 870 871 872
	unregister_netdevice_notifier(&iboe->nb);
	ib_unregister_device(&hr_dev->ib_dev);
}

873 874 875 876 877
const struct uapi_definition hns_roce_uapi_defs[] = {
	UAPI_DEF_CHAIN(hns_roce_dca_uapi_defs),
	{}
};

878
static const struct ib_device_ops hns_roce_dev_ops = {
879
	.owner = THIS_MODULE,
880
	.driver_id = RDMA_DRIVER_HNS,
881
	.uverbs_abi_ver = 1,
882
	.uverbs_no_driver_id_binding = 1,
883

884
	.get_dev_fw_str = hns_roce_get_fw_ver,
885 886 887 888
	.add_gid = hns_roce_add_gid,
	.alloc_pd = hns_roce_alloc_pd,
	.alloc_ucontext = hns_roce_alloc_ucontext,
	.create_ah = hns_roce_create_ah,
889
	.create_cq = hns_roce_create_cq,
890 891 892 893 894 895
	.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,
896
	.destroy_cq = hns_roce_destroy_cq,
897 898 899
	.disassociate_ucontext = hns_roce_disassociate_ucontext,
	.get_dma_mr = hns_roce_get_dma_mr,
	.get_link_layer = hns_roce_get_link_layer,
900
	.get_netdev = hns_roce_get_netdev,
901 902
	.get_port_immutable = hns_roce_port_immutable,
	.mmap = hns_roce_mmap,
903
	.mmap_free = hns_roce_free_mmap,
904 905 906 907 908 909 910
	.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,
C
Chengchang Tang 已提交
911 912
	.alloc_hw_stats = hns_roce_alloc_hw_port_stats,
	.get_hw_stats = hns_roce_get_hw_stats,
913
	.init_port = hns_roce_create_port_files,
914 915

	INIT_RDMA_OBJ_SIZE(ib_ah, hns_roce_ah, ibah),
916
	INIT_RDMA_OBJ_SIZE(ib_cq, hns_roce_cq, ib_cq),
917
	INIT_RDMA_OBJ_SIZE(ib_pd, hns_roce_pd, ibpd),
918
	INIT_RDMA_OBJ_SIZE(ib_ucontext, hns_roce_ucontext, ibucontext),
919 920 921 922 923 924 925 926 927
};

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,
928 929

	INIT_RDMA_OBJ_SIZE(ib_mw, hns_roce_mw, ibmw),
930 931 932 933 934 935 936 937 938 939
};

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,
940 941

	INIT_RDMA_OBJ_SIZE(ib_srq, hns_roce_srq, ibsrq),
942 943
};

944 945 946 947 948 949 950
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),
};

951 952
static const struct ib_device_ops hns_roce_dev_restrack_ops = {
	.fill_res_cq_entry = hns_roce_fill_res_cq_entry,
953
	.fill_res_cq_entry_raw = hns_roce_fill_res_cq_entry_raw,
954
	.fill_res_qp_entry = hns_roce_fill_res_qp_entry,
955
	.fill_res_qp_entry_raw = hns_roce_fill_res_qp_entry_raw,
956
	.fill_res_mr_entry = hns_roce_fill_res_mr_entry,
957
	.fill_res_mr_entry_raw = hns_roce_fill_res_mr_entry_raw,
958 959
};

960 961 962 963 964
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;
965
	struct device *dev = hr_dev->dev;
966
	unsigned int i;
967 968

	iboe = &hr_dev->iboe;
L
Lijun Ou 已提交
969
	spin_lock_init(&iboe->lock);
970 971 972

	ib_dev = &hr_dev->ib_dev;

L
Lijun Ou 已提交
973 974
	ib_dev->node_type = RDMA_NODE_IB_CA;
	ib_dev->dev.parent = dev;
975

L
Lijun Ou 已提交
976 977 978 979
	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 =
980 981 982 983 984 985 986 987 988 989 990 991 992
		(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) |
993 994 995
		(1ULL << IB_USER_VERBS_CMD_DESTROY_QP) |
		(1ULL << IB_USER_VERBS_CMD_CREATE_AH) |
		(1ULL << IB_USER_VERBS_CMD_DESTROY_AH);
996

Y
Yixing Liu 已提交
997 998 999 1000 1001 1002
	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);
1003

1004 1005
	if (hr_dev->caps.flags & HNS_ROCE_CAP_FLAG_REREG_MR) {
		ib_dev->uverbs_cmd_mask |= (1ULL << IB_USER_VERBS_CMD_REREG_MR);
1006
		ib_set_device_ops(ib_dev, &hns_roce_dev_mr_ops);
1007
	}
1008

Y
Yixian Liu 已提交
1009 1010 1011 1012 1013
	/* 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);
1014
		ib_set_device_ops(ib_dev, &hns_roce_dev_mw_ops);
Y
Yixian Liu 已提交
1015 1016
	}

Y
Yixian Liu 已提交
1017
	/* FRMR */
1018 1019
	if (hr_dev->caps.flags & HNS_ROCE_CAP_FLAG_FRMR)
		ib_set_device_ops(ib_dev, &hns_roce_dev_frmr_ops);
Y
Yixian Liu 已提交
1020

1021 1022 1023 1024 1025 1026 1027 1028
	/* 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);
1029 1030
		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);
1031 1032
	}

1033 1034 1035 1036
	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) |
1037 1038
				(1ULL << IB_USER_VERBS_CMD_CREATE_XSRQ) |
				(1ULL << IB_USER_VERBS_CMD_OPEN_QP);
1039 1040 1041
		ib_set_device_ops(ib_dev, &hns_roce_dev_xrcd_ops);
	}

1042 1043
	ib_set_device_ops(ib_dev, hr_dev->hw->hns_roce_dev_ops);
	ib_set_device_ops(ib_dev, &hns_roce_dev_ops);
1044
	ib_set_device_ops(ib_dev, &hns_roce_dev_restrack_ops);
1045 1046 1047 1048

	if (IS_ENABLED(CONFIG_INFINIBAND_USER_ACCESS))
		ib_dev->driver_def = hns_roce_uapi_defs;

1049 1050 1051 1052 1053 1054 1055 1056 1057
	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;
	}
1058
	dma_set_max_seg_size(dev, UINT_MAX);
J
Junxian Huang 已提交
1059 1060 1061 1062 1063 1064

	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);
1065 1066 1067 1068 1069
	if (ret) {
		dev_err(dev, "ib_register_device failed!\n");
		return ret;
	}

1070
	ret = hns_roce_setup_mtu_mac(hr_dev);
1071
	if (ret) {
1072 1073
		dev_err(dev, "setup_mtu_mac failed!\n");
		goto error_failed_setup_mtu_mac;
1074 1075 1076 1077 1078 1079
	}

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

J
Junxian Huang 已提交
1083 1084 1085 1086 1087 1088
	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);
	}

1089
	hr_dev->active = true;
J
Junxian Huang 已提交
1090 1091

	return ret;
1092

1093
error_failed_setup_mtu_mac:
1094 1095 1096 1097 1098 1099 1100
	ib_unregister_device(ib_dev);

	return ret;
}

static int hns_roce_init_hem(struct hns_roce_dev *hr_dev)
{
1101
	struct device *dev = hr_dev->dev;
L
Lang Cheng 已提交
1102
	int ret;
1103 1104 1105 1106 1107 1108

	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 已提交
1109
		return ret;
1110 1111 1112
	}

	ret = hns_roce_init_hem_table(hr_dev, &hr_dev->qp_table.qp_table,
1113
				      HEM_TYPE_QPC, hr_dev->caps.qpc_sz,
1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129
				      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;
	}

1130 1131 1132 1133 1134 1135 1136 1137 1138
	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 已提交
1139
				"Failed to init trrl_table memory, aborting.\n");
1140 1141 1142 1143
			goto err_unmap_irrl;
		}
	}

1144 1145 1146 1147 1148
	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");
1149
		goto err_unmap_trrl;
1150 1151
	}

1152
	if (hr_dev->caps.flags & HNS_ROCE_CAP_FLAG_SRQ) {
L
Lijun Ou 已提交
1153 1154 1155 1156 1157 1158
		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 已提交
1159
				"Failed to init SRQ context memory, aborting.\n");
L
Lijun Ou 已提交
1160 1161 1162 1163
			goto err_unmap_cq;
		}
	}

1164
	if (hr_dev->caps.flags & HNS_ROCE_CAP_FLAG_QP_FLOW_CTRL) {
1165 1166 1167
		ret = hns_roce_init_hem_table(hr_dev,
					      &hr_dev->qp_table.sccc_table,
					      HEM_TYPE_SCCC,
1168
					      hr_dev->caps.sccc_sz,
1169 1170 1171
					      hr_dev->caps.num_qps, 1);
		if (ret) {
			dev_err(dev,
L
Lijun Ou 已提交
1172
				"Failed to init SCC context memory, aborting.\n");
X
Xi Wang 已提交
1173
			goto err_unmap_srq;
1174 1175 1176
		}
	}

1177
	if (hr_dev->caps.qpc_timer_entry_sz) {
L
Lijun Ou 已提交
1178
		ret = hns_roce_init_hem_table(hr_dev, &hr_dev->qpc_timer_table,
1179 1180 1181 1182 1183
					      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 已提交
1184
				"Failed to init QPC timer memory, aborting.\n");
1185 1186 1187 1188 1189
			goto err_unmap_ctx;
		}
	}

	if (hr_dev->caps.cqc_timer_entry_sz) {
L
Lijun Ou 已提交
1190
		ret = hns_roce_init_hem_table(hr_dev, &hr_dev->cqc_timer_table,
1191 1192
					      HEM_TYPE_CQC_TIMER,
					      hr_dev->caps.cqc_timer_entry_sz,
1193
					      hr_dev->caps.cqc_timer_bt_num, 1);
1194 1195
		if (ret) {
			dev_err(dev,
L
Lijun Ou 已提交
1196
				"Failed to init CQC timer memory, aborting.\n");
1197 1198 1199 1200
			goto err_unmap_qpc_timer;
		}
	}

1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213
	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;
		}
	}

1214 1215
	return 0;

1216 1217 1218 1219
err_unmap_cqc_timer:
	if (hr_dev->caps.cqc_timer_entry_sz)
		hns_roce_cleanup_hem_table(hr_dev, &hr_dev->cqc_timer_table);

1220 1221
err_unmap_qpc_timer:
	if (hr_dev->caps.qpc_timer_entry_sz)
L
Lijun Ou 已提交
1222
		hns_roce_cleanup_hem_table(hr_dev, &hr_dev->qpc_timer_table);
1223 1224

err_unmap_ctx:
1225
	if (hr_dev->caps.flags & HNS_ROCE_CAP_FLAG_QP_FLOW_CTRL)
1226 1227
		hns_roce_cleanup_hem_table(hr_dev,
					   &hr_dev->qp_table.sccc_table);
L
Lijun Ou 已提交
1228
err_unmap_srq:
1229
	if (hr_dev->caps.flags & HNS_ROCE_CAP_FLAG_SRQ)
L
Lijun Ou 已提交
1230 1231 1232 1233 1234
		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);

1235 1236 1237 1238 1239
err_unmap_trrl:
	if (hr_dev->caps.trrl_entry_sz)
		hns_roce_cleanup_hem_table(hr_dev,
					   &hr_dev->qp_table.trrl_table);

1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251
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;
}

1252 1253 1254 1255 1256 1257 1258 1259
static void hns_roce_teardown_hca(struct hns_roce_dev *hr_dev)
{
	if (hr_dev->caps.flags & HNS_ROCE_CAP_FLAG_DCA_MODE)
		hns_roce_cleanup_dca(hr_dev);

	hns_roce_cleanup_bitmap(hr_dev);
}

1260
/**
1261 1262 1263 1264
 * hns_roce_setup_hca - setup host channel adapter
 * @hr_dev: pointer to hns roce device
 * Return : int
 */
1265 1266
static int hns_roce_setup_hca(struct hns_roce_dev *hr_dev)
{
1267
	struct device *dev = hr_dev->dev;
L
Lang Cheng 已提交
1268
	int ret;
1269 1270 1271

	spin_lock_init(&hr_dev->sm_lock);

1272 1273 1274 1275 1276 1277 1278 1279
	INIT_LIST_HEAD(&hr_dev->qp_list);
	spin_lock_init(&hr_dev->qp_list_lock);
	INIT_LIST_HEAD(&hr_dev->dip_list);
	spin_lock_init(&hr_dev->dip_list_lock);

	INIT_LIST_HEAD(&hr_dev->uctx_list);
	spin_lock_init(&hr_dev->uctx_list_lock);

1280 1281
	if (hr_dev->caps.flags & HNS_ROCE_CAP_FLAG_CQ_RECORD_DB ||
	    hr_dev->caps.flags & HNS_ROCE_CAP_FLAG_QP_RECORD_DB) {
1282 1283 1284 1285
		INIT_LIST_HEAD(&hr_dev->pgdir_list);
		mutex_init(&hr_dev->pgdir_mutex);
	}

1286
	hns_roce_init_uar_table(hr_dev);
1287 1288 1289 1290 1291 1292 1293

	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;
	}

1294 1295 1296 1297 1298 1299
	ret = hns_roce_init_qp_table(hr_dev);
	if (ret) {
		dev_err(dev, "Failed to init qp_table.\n");
		goto err_uar_table_free;
	}

1300
	hns_roce_init_pd_table(hr_dev);
1301

1302 1303
	if (hr_dev->caps.flags & HNS_ROCE_CAP_FLAG_XRC)
		hns_roce_init_xrcd_table(hr_dev);
1304

1305
	hns_roce_init_mr_table(hr_dev);
1306

1307
	hns_roce_init_cq_table(hr_dev);
1308

L
Lijun Ou 已提交
1309
	if (hr_dev->caps.flags & HNS_ROCE_CAP_FLAG_SRQ) {
1310
		hns_roce_init_srq_table(hr_dev);
L
Lijun Ou 已提交
1311 1312
	}

1313 1314 1315
	if (hr_dev->caps.flags & HNS_ROCE_CAP_FLAG_DCA_MODE)
		hns_roce_init_dca(hr_dev);

1316 1317 1318
	return 0;

err_uar_table_free:
1319
	ida_destroy(&hr_dev->uar_ida.ida);
1320 1321 1322
	return ret;
}

1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339
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)
{
1340
	struct hns_roce_qp *hr_qp, *hr_qp_next;
1341 1342 1343 1344 1345 1346 1347 1348
	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);
1349
	list_for_each_entry_safe(hr_qp, hr_qp_next, &hr_dev->qp_list, node) {
1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366
		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);
}

C
Chengchang Tang 已提交
1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381
static int hns_roce_alloc_dfx_cnt(struct hns_roce_dev *hr_dev)
{
	hr_dev->dfx_cnt = kcalloc(HNS_ROCE_DFX_CNT_TOTAL, sizeof(atomic64_t),
				  GFP_KERNEL);
	if (!hr_dev->dfx_cnt)
		return -ENOMEM;

	return 0;
}

static void hns_roce_dealloc_dfx_cnt(struct hns_roce_dev *hr_dev)
{
	kfree(hr_dev->dfx_cnt);
}

1382
int hns_roce_init(struct hns_roce_dev *hr_dev)
1383
{
1384
	struct device *dev = hr_dev->dev;
L
Lang Cheng 已提交
1385
	int ret;
1386

1387
	hr_dev->is_reset = false;
1388

C
Chengchang Tang 已提交
1389 1390 1391 1392
	ret = hns_roce_alloc_dfx_cnt(hr_dev);
	if (ret)
		return ret;

1393 1394 1395 1396
	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");
C
Chengchang Tang 已提交
1397
			goto error_failed_alloc_dfx_cnt;
1398 1399 1400
		}
	}

1401 1402 1403 1404 1405
	ret = hr_dev->hw->hw_profile(hr_dev);
	if (ret) {
		dev_err(dev, "Get RoCE engine profile failed!\n");
		goto error_failed_cmd_init;
	}
1406 1407 1408 1409 1410 1411 1412

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

1413
	/* EQ depends on poll mode, event mode depends on EQ */
Y
Yixian Liu 已提交
1414 1415 1416 1417
	ret = hr_dev->hw->init_eq(hr_dev);
	if (ret) {
		dev_err(dev, "eq init failed!\n");
		goto error_failed_eq_table;
1418 1419 1420 1421
	}

	if (hr_dev->cmd_mod) {
		ret = hns_roce_cmd_use_events(hr_dev);
1422
		if (ret)
1423 1424
			dev_warn(dev,
				 "Cmd event  mode failed, set back to poll!\n");
1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438
	}

	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;
	}

1439 1440 1441 1442 1443 1444
	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;
		}
1445 1446 1447 1448 1449 1450
	}

	ret = hns_roce_register_device(hr_dev);
	if (ret)
		goto error_failed_register_device;

1451 1452
	hns_roce_register_debugfs(hr_dev);

1453 1454 1455
	return 0;

error_failed_register_device:
1456 1457
	if (hr_dev->hw->hw_exit)
		hr_dev->hw->hw_exit(hr_dev);
1458 1459

error_failed_engine_init:
1460
	hns_roce_teardown_hca(hr_dev);
1461 1462 1463 1464 1465 1466 1467

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 已提交
1468
	hr_dev->hw->cleanup_eq(hr_dev);
1469 1470 1471 1472 1473

error_failed_eq_table:
	hns_roce_cmd_cleanup(hr_dev);

error_failed_cmd_init:
1474 1475 1476
	if (hr_dev->hw->cmq_exit)
		hr_dev->hw->cmq_exit(hr_dev);

C
Chengchang Tang 已提交
1477 1478 1479
error_failed_alloc_dfx_cnt:
	hns_roce_dealloc_dfx_cnt(hr_dev);

1480 1481 1482
	return ret;
}

1483
void hns_roce_exit(struct hns_roce_dev *hr_dev, bool bond_cleanup)
1484
{
1485
	hns_roce_unregister_sysfs(hr_dev);
1486
	hns_roce_unregister_device(hr_dev, bond_cleanup);
1487
	hns_roce_unregister_debugfs(hr_dev);
1488

1489 1490
	if (hr_dev->hw->hw_exit)
		hr_dev->hw->hw_exit(hr_dev);
1491
	hns_roce_teardown_hca(hr_dev);
1492 1493 1494 1495 1496
	hns_roce_cleanup_hem(hr_dev);

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

Y
Yixian Liu 已提交
1497
	hr_dev->hw->cleanup_eq(hr_dev);
1498
	hns_roce_cmd_cleanup(hr_dev);
1499 1500
	if (hr_dev->hw->cmq_exit)
		hr_dev->hw->cmq_exit(hr_dev);
1501 1502 1503 1504 1505 1506 1507
}

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");