2 * Copyright (c) 2016 Hisilicon Limited.
3 * Copyright (c) 2007, 2008 Mellanox Technologies. All rights reserved.
5 * This software is available to you under a choice of one of two
6 * licenses. You may choose to be licensed under the terms of the GNU
7 * General Public License (GPL) Version 2, available from the file
8 * COPYING in the main directory of this source tree, or the
9 * OpenIB.org BSD license below:
11 * Redistribution and use in source and binary forms, with or
12 * without modification, are permitted provided that the following
15 * - Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
19 * - Redistributions in binary form must reproduce the above
20 * copyright notice, this list of conditions and the following
21 * disclaimer in the documentation and/or other materials
22 * provided with the distribution.
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
29 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
33 #include <linux/acpi.h>
34 #include <linux/of_platform.h>
35 #include <linux/module.h>
36 #include <rdma/ib_addr.h>
37 #include <rdma/ib_smi.h>
38 #include <rdma/ib_user_verbs.h>
39 #include <rdma/ib_cache.h>
40 #include "hns_roce_common.h"
41 #include "hns_roce_device.h"
42 #include <rdma/hns-abi.h>
43 #include "hns_roce_hem.h"
46 * hns_get_gid_index - Get gid index.
47 * @hr_dev: pointer to structure hns_roce_dev.
48 * @port: port, value range: 0 ~ MAX
49 * @gid_index: gid_index, value range: 0 ~ MAX
51 * N ports shared gids, allocation method as follow:
52 * GID[0][0], GID[1][0],.....GID[N - 1][0],
53 * GID[0][0], GID[1][0],.....GID[N - 1][0],
56 int hns_get_gid_index(struct hns_roce_dev *hr_dev, u8 port, int gid_index)
58 return gid_index * hr_dev->caps.num_ports + port;
60 EXPORT_SYMBOL_GPL(hns_get_gid_index);
62 static int hns_roce_set_mac(struct hns_roce_dev *hr_dev, u8 port, u8 *addr)
67 if (!memcmp(hr_dev->dev_addr[port], addr, ETH_ALEN))
70 for (i = 0; i < ETH_ALEN; i++)
71 hr_dev->dev_addr[port][i] = addr[i];
73 phy_port = hr_dev->iboe.phy_port[port];
74 return hr_dev->hw->set_mac(hr_dev, phy_port, addr);
77 static int hns_roce_add_gid(const struct ib_gid_attr *attr, void **context)
79 struct hns_roce_dev *hr_dev = to_hr_dev(attr->device);
80 u8 port = attr->port_num - 1;
83 if (port >= hr_dev->caps.num_ports)
86 ret = hr_dev->hw->set_gid(hr_dev, port, attr->index, &attr->gid, attr);
91 static int hns_roce_del_gid(const struct ib_gid_attr *attr, void **context)
93 struct hns_roce_dev *hr_dev = to_hr_dev(attr->device);
94 struct ib_gid_attr zattr = { };
95 u8 port = attr->port_num - 1;
98 if (port >= hr_dev->caps.num_ports)
101 ret = hr_dev->hw->set_gid(hr_dev, port, attr->index, &zgid, &zattr);
106 static int handle_en_event(struct hns_roce_dev *hr_dev, u8 port,
109 struct device *dev = hr_dev->dev;
110 struct net_device *netdev;
113 netdev = hr_dev->iboe.netdevs[port];
115 dev_err(dev, "port(%d) can't find netdev\n", port);
122 case NETDEV_REGISTER:
123 case NETDEV_CHANGEADDR:
124 ret = hns_roce_set_mac(hr_dev, port, netdev->dev_addr);
128 * In v1 engine, only support all ports closed together.
132 dev_dbg(dev, "NETDEV event = 0x%x!\n", (u32)(event));
139 static int hns_roce_netdev_event(struct notifier_block *self,
140 unsigned long event, void *ptr)
142 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
143 struct hns_roce_ib_iboe *iboe = NULL;
144 struct hns_roce_dev *hr_dev = NULL;
148 hr_dev = container_of(self, struct hns_roce_dev, iboe.nb);
149 iboe = &hr_dev->iboe;
151 for (port = 0; port < hr_dev->caps.num_ports; port++) {
152 if (dev == iboe->netdevs[port]) {
153 ret = handle_en_event(hr_dev, port, event);
163 static int hns_roce_setup_mtu_mac(struct hns_roce_dev *hr_dev)
168 for (i = 0; i < hr_dev->caps.num_ports; i++) {
169 if (hr_dev->hw->set_mtu)
170 hr_dev->hw->set_mtu(hr_dev, hr_dev->iboe.phy_port[i],
171 hr_dev->caps.max_mtu);
172 ret = hns_roce_set_mac(hr_dev, i,
173 hr_dev->iboe.netdevs[i]->dev_addr);
181 static int hns_roce_query_device(struct ib_device *ib_dev,
182 struct ib_device_attr *props,
183 struct ib_udata *uhw)
185 struct hns_roce_dev *hr_dev = to_hr_dev(ib_dev);
187 memset(props, 0, sizeof(*props));
189 props->fw_ver = hr_dev->caps.fw_ver;
190 props->sys_image_guid = cpu_to_be64(hr_dev->sys_image_guid);
191 props->max_mr_size = (u64)(~(0ULL));
192 props->page_size_cap = hr_dev->caps.page_size_cap;
193 props->vendor_id = hr_dev->vendor_id;
194 props->vendor_part_id = hr_dev->vendor_part_id;
195 props->hw_ver = hr_dev->hw_rev;
196 props->max_qp = hr_dev->caps.num_qps;
197 props->max_qp_wr = hr_dev->caps.max_wqes;
198 props->device_cap_flags = IB_DEVICE_PORT_ACTIVE_EVENT |
199 IB_DEVICE_RC_RNR_NAK_GEN;
200 props->max_send_sge = hr_dev->caps.max_sq_sg;
201 props->max_recv_sge = hr_dev->caps.max_rq_sg;
202 props->max_sge_rd = 1;
203 props->max_cq = hr_dev->caps.num_cqs;
204 props->max_cqe = hr_dev->caps.max_cqes;
205 props->max_mr = hr_dev->caps.num_mtpts;
206 props->max_pd = hr_dev->caps.num_pds;
207 props->max_qp_rd_atom = hr_dev->caps.max_qp_dest_rdma;
208 props->max_qp_init_rd_atom = hr_dev->caps.max_qp_init_rdma;
209 props->atomic_cap = hr_dev->caps.flags & HNS_ROCE_CAP_FLAG_ATOMIC ?
210 IB_ATOMIC_HCA : IB_ATOMIC_NONE;
211 props->max_pkeys = 1;
212 props->local_ca_ack_delay = hr_dev->caps.local_ca_ack_delay;
213 if (hr_dev->caps.flags & HNS_ROCE_CAP_FLAG_SRQ) {
214 props->max_srq = hr_dev->caps.max_srqs;
215 props->max_srq_wr = hr_dev->caps.max_srq_wrs;
216 props->max_srq_sge = hr_dev->caps.max_srq_sges;
219 if (hr_dev->caps.flags & HNS_ROCE_CAP_FLAG_FRMR) {
220 props->device_cap_flags |= IB_DEVICE_MEM_MGT_EXTENSIONS;
221 props->max_fast_reg_page_list_len = HNS_ROCE_FRMR_MAX_PA;
227 static int hns_roce_query_port(struct ib_device *ib_dev, u8 port_num,
228 struct ib_port_attr *props)
230 struct hns_roce_dev *hr_dev = to_hr_dev(ib_dev);
231 struct device *dev = hr_dev->dev;
232 struct net_device *net_dev;
237 assert(port_num > 0);
240 /* props being zeroed by the caller, avoid zeroing it here */
242 props->max_mtu = hr_dev->caps.max_mtu;
243 props->gid_tbl_len = hr_dev->caps.gid_table_len[port];
244 props->port_cap_flags = IB_PORT_CM_SUP | IB_PORT_REINIT_SUP |
245 IB_PORT_VENDOR_CLASS_SUP |
246 IB_PORT_BOOT_MGMT_SUP;
247 props->max_msg_sz = HNS_ROCE_MAX_MSG_LEN;
248 props->pkey_tbl_len = 1;
249 props->active_width = IB_WIDTH_4X;
250 props->active_speed = 1;
252 spin_lock_irqsave(&hr_dev->iboe.lock, flags);
254 net_dev = hr_dev->iboe.netdevs[port];
256 spin_unlock_irqrestore(&hr_dev->iboe.lock, flags);
257 dev_err(dev, "find netdev %d failed!\r\n", port);
261 mtu = iboe_get_mtu(net_dev->mtu);
262 props->active_mtu = mtu ? min(props->max_mtu, mtu) : IB_MTU_256;
263 props->state = (netif_running(net_dev) && netif_carrier_ok(net_dev)) ?
264 IB_PORT_ACTIVE : IB_PORT_DOWN;
265 props->phys_state = (props->state == IB_PORT_ACTIVE) ?
266 HNS_ROCE_PHY_LINKUP : HNS_ROCE_PHY_DISABLED;
268 spin_unlock_irqrestore(&hr_dev->iboe.lock, flags);
273 static enum rdma_link_layer hns_roce_get_link_layer(struct ib_device *device,
276 return IB_LINK_LAYER_ETHERNET;
279 static int hns_roce_query_pkey(struct ib_device *ib_dev, u8 port, u16 index,
287 static int hns_roce_modify_device(struct ib_device *ib_dev, int mask,
288 struct ib_device_modify *props)
292 if (mask & ~IB_DEVICE_MODIFY_NODE_DESC)
295 if (mask & IB_DEVICE_MODIFY_NODE_DESC) {
296 spin_lock_irqsave(&to_hr_dev(ib_dev)->sm_lock, flags);
297 memcpy(ib_dev->node_desc, props->node_desc, NODE_DESC_SIZE);
298 spin_unlock_irqrestore(&to_hr_dev(ib_dev)->sm_lock, flags);
304 static int hns_roce_modify_port(struct ib_device *ib_dev, u8 port_num, int mask,
305 struct ib_port_modify *props)
310 static int hns_roce_alloc_ucontext(struct ib_ucontext *uctx,
311 struct ib_udata *udata)
314 struct hns_roce_ucontext *context = to_hr_ucontext(uctx);
315 struct hns_roce_ib_alloc_ucontext_resp resp = {};
316 struct hns_roce_dev *hr_dev = to_hr_dev(uctx->device);
321 resp.qp_tab_size = hr_dev->caps.num_qps;
323 ret = hns_roce_uar_alloc(hr_dev, &context->uar);
325 goto error_fail_uar_alloc;
327 if (hr_dev->caps.flags & HNS_ROCE_CAP_FLAG_RECORD_DB) {
328 INIT_LIST_HEAD(&context->page_list);
329 mutex_init(&context->page_mutex);
332 ret = ib_copy_to_udata(udata, &resp, sizeof(resp));
334 goto error_fail_copy_to_udata;
338 error_fail_copy_to_udata:
339 hns_roce_uar_free(hr_dev, &context->uar);
341 error_fail_uar_alloc:
345 static void hns_roce_dealloc_ucontext(struct ib_ucontext *ibcontext)
347 struct hns_roce_ucontext *context = to_hr_ucontext(ibcontext);
349 hns_roce_uar_free(to_hr_dev(ibcontext->device), &context->uar);
352 static int hns_roce_mmap(struct ib_ucontext *context,
353 struct vm_area_struct *vma)
355 struct hns_roce_dev *hr_dev = to_hr_dev(context->device);
357 switch (vma->vm_pgoff) {
359 return rdma_user_mmap_io(context, vma,
360 to_hr_ucontext(context)->uar.pfn,
362 pgprot_noncached(vma->vm_page_prot));
364 /* vm_pgoff: 1 -- TPTR */
366 if (!hr_dev->tptr_dma_addr || !hr_dev->tptr_size)
369 * FIXME: using io_remap_pfn_range on the dma address returned
370 * by dma_alloc_coherent is totally wrong.
372 return rdma_user_mmap_io(context, vma,
373 hr_dev->tptr_dma_addr >> PAGE_SHIFT,
382 static int hns_roce_port_immutable(struct ib_device *ib_dev, u8 port_num,
383 struct ib_port_immutable *immutable)
385 struct ib_port_attr attr;
388 ret = ib_query_port(ib_dev, port_num, &attr);
392 immutable->pkey_tbl_len = attr.pkey_tbl_len;
393 immutable->gid_tbl_len = attr.gid_tbl_len;
395 immutable->max_mad_size = IB_MGMT_MAD_SIZE;
396 immutable->core_cap_flags = RDMA_CORE_PORT_IBA_ROCE;
397 if (to_hr_dev(ib_dev)->caps.flags & HNS_ROCE_CAP_FLAG_ROCE_V1_V2)
398 immutable->core_cap_flags |= RDMA_CORE_PORT_IBA_ROCE_UDP_ENCAP;
403 static void hns_roce_disassociate_ucontext(struct ib_ucontext *ibcontext)
407 static void hns_roce_unregister_device(struct hns_roce_dev *hr_dev)
409 struct hns_roce_ib_iboe *iboe = &hr_dev->iboe;
411 hr_dev->active = false;
412 unregister_netdevice_notifier(&iboe->nb);
413 ib_unregister_device(&hr_dev->ib_dev);
416 static const struct ib_device_ops hns_roce_dev_ops = {
417 .driver_id = RDMA_DRIVER_HNS,
420 .add_gid = hns_roce_add_gid,
421 .alloc_pd = hns_roce_alloc_pd,
422 .alloc_ucontext = hns_roce_alloc_ucontext,
423 .create_ah = hns_roce_create_ah,
424 .create_cq = hns_roce_ib_create_cq,
425 .create_qp = hns_roce_create_qp,
426 .dealloc_pd = hns_roce_dealloc_pd,
427 .dealloc_ucontext = hns_roce_dealloc_ucontext,
428 .del_gid = hns_roce_del_gid,
429 .dereg_mr = hns_roce_dereg_mr,
430 .destroy_ah = hns_roce_destroy_ah,
431 .destroy_cq = hns_roce_ib_destroy_cq,
432 .disassociate_ucontext = hns_roce_disassociate_ucontext,
433 .fill_res_entry = hns_roce_fill_res_entry,
434 .get_dma_mr = hns_roce_get_dma_mr,
435 .get_link_layer = hns_roce_get_link_layer,
436 .get_port_immutable = hns_roce_port_immutable,
437 .mmap = hns_roce_mmap,
438 .modify_device = hns_roce_modify_device,
439 .modify_port = hns_roce_modify_port,
440 .modify_qp = hns_roce_modify_qp,
441 .query_ah = hns_roce_query_ah,
442 .query_device = hns_roce_query_device,
443 .query_pkey = hns_roce_query_pkey,
444 .query_port = hns_roce_query_port,
445 .reg_user_mr = hns_roce_reg_user_mr,
447 INIT_RDMA_OBJ_SIZE(ib_ah, hns_roce_ah, ibah),
448 INIT_RDMA_OBJ_SIZE(ib_pd, hns_roce_pd, ibpd),
449 INIT_RDMA_OBJ_SIZE(ib_ucontext, hns_roce_ucontext, ibucontext),
452 static const struct ib_device_ops hns_roce_dev_mr_ops = {
453 .rereg_user_mr = hns_roce_rereg_user_mr,
456 static const struct ib_device_ops hns_roce_dev_mw_ops = {
457 .alloc_mw = hns_roce_alloc_mw,
458 .dealloc_mw = hns_roce_dealloc_mw,
461 static const struct ib_device_ops hns_roce_dev_frmr_ops = {
462 .alloc_mr = hns_roce_alloc_mr,
463 .map_mr_sg = hns_roce_map_mr_sg,
466 static const struct ib_device_ops hns_roce_dev_srq_ops = {
467 .create_srq = hns_roce_create_srq,
468 .destroy_srq = hns_roce_destroy_srq,
470 INIT_RDMA_OBJ_SIZE(ib_srq, hns_roce_srq, ibsrq),
473 static int hns_roce_register_device(struct hns_roce_dev *hr_dev)
476 struct hns_roce_ib_iboe *iboe = NULL;
477 struct ib_device *ib_dev = NULL;
478 struct device *dev = hr_dev->dev;
481 iboe = &hr_dev->iboe;
482 spin_lock_init(&iboe->lock);
484 ib_dev = &hr_dev->ib_dev;
486 ib_dev->owner = THIS_MODULE;
487 ib_dev->node_type = RDMA_NODE_IB_CA;
488 ib_dev->dev.parent = dev;
490 ib_dev->phys_port_cnt = hr_dev->caps.num_ports;
491 ib_dev->local_dma_lkey = hr_dev->caps.reserved_lkey;
492 ib_dev->num_comp_vectors = hr_dev->caps.num_comp_vectors;
493 ib_dev->uverbs_cmd_mask =
494 (1ULL << IB_USER_VERBS_CMD_GET_CONTEXT) |
495 (1ULL << IB_USER_VERBS_CMD_QUERY_DEVICE) |
496 (1ULL << IB_USER_VERBS_CMD_QUERY_PORT) |
497 (1ULL << IB_USER_VERBS_CMD_ALLOC_PD) |
498 (1ULL << IB_USER_VERBS_CMD_DEALLOC_PD) |
499 (1ULL << IB_USER_VERBS_CMD_REG_MR) |
500 (1ULL << IB_USER_VERBS_CMD_DEREG_MR) |
501 (1ULL << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL) |
502 (1ULL << IB_USER_VERBS_CMD_CREATE_CQ) |
503 (1ULL << IB_USER_VERBS_CMD_DESTROY_CQ) |
504 (1ULL << IB_USER_VERBS_CMD_CREATE_QP) |
505 (1ULL << IB_USER_VERBS_CMD_MODIFY_QP) |
506 (1ULL << IB_USER_VERBS_CMD_QUERY_QP) |
507 (1ULL << IB_USER_VERBS_CMD_DESTROY_QP);
509 ib_dev->uverbs_ex_cmd_mask |=
510 (1ULL << IB_USER_VERBS_EX_CMD_MODIFY_CQ);
512 if (hr_dev->caps.flags & HNS_ROCE_CAP_FLAG_REREG_MR) {
513 ib_dev->uverbs_cmd_mask |= (1ULL << IB_USER_VERBS_CMD_REREG_MR);
514 ib_set_device_ops(ib_dev, &hns_roce_dev_mr_ops);
518 if (hr_dev->caps.flags & HNS_ROCE_CAP_FLAG_MW) {
519 ib_dev->uverbs_cmd_mask |=
520 (1ULL << IB_USER_VERBS_CMD_ALLOC_MW) |
521 (1ULL << IB_USER_VERBS_CMD_DEALLOC_MW);
522 ib_set_device_ops(ib_dev, &hns_roce_dev_mw_ops);
526 if (hr_dev->caps.flags & HNS_ROCE_CAP_FLAG_FRMR)
527 ib_set_device_ops(ib_dev, &hns_roce_dev_frmr_ops);
530 if (hr_dev->caps.flags & HNS_ROCE_CAP_FLAG_SRQ) {
531 ib_dev->uverbs_cmd_mask |=
532 (1ULL << IB_USER_VERBS_CMD_CREATE_SRQ) |
533 (1ULL << IB_USER_VERBS_CMD_MODIFY_SRQ) |
534 (1ULL << IB_USER_VERBS_CMD_QUERY_SRQ) |
535 (1ULL << IB_USER_VERBS_CMD_DESTROY_SRQ) |
536 (1ULL << IB_USER_VERBS_CMD_POST_SRQ_RECV);
537 ib_set_device_ops(ib_dev, &hns_roce_dev_srq_ops);
538 ib_set_device_ops(ib_dev, hr_dev->hw->hns_roce_dev_srq_ops);
541 ib_set_device_ops(ib_dev, hr_dev->hw->hns_roce_dev_ops);
542 ib_set_device_ops(ib_dev, &hns_roce_dev_ops);
543 for (i = 0; i < hr_dev->caps.num_ports; i++) {
544 if (!hr_dev->iboe.netdevs[i])
547 ret = ib_device_set_netdev(ib_dev, hr_dev->iboe.netdevs[i],
552 ret = ib_register_device(ib_dev, "hns_%d");
554 dev_err(dev, "ib_register_device failed!\n");
558 ret = hns_roce_setup_mtu_mac(hr_dev);
560 dev_err(dev, "setup_mtu_mac failed!\n");
561 goto error_failed_setup_mtu_mac;
564 iboe->nb.notifier_call = hns_roce_netdev_event;
565 ret = register_netdevice_notifier(&iboe->nb);
567 dev_err(dev, "register_netdevice_notifier failed!\n");
568 goto error_failed_setup_mtu_mac;
571 hr_dev->active = true;
574 error_failed_setup_mtu_mac:
575 ib_unregister_device(ib_dev);
580 static int hns_roce_init_hem(struct hns_roce_dev *hr_dev)
583 struct device *dev = hr_dev->dev;
585 ret = hns_roce_init_hem_table(hr_dev, &hr_dev->mr_table.mtt_table,
586 HEM_TYPE_MTT, hr_dev->caps.mtt_entry_sz,
587 hr_dev->caps.num_mtt_segs, 1);
589 dev_err(dev, "Failed to init MTT context memory, aborting.\n");
593 if (hns_roce_check_whether_mhop(hr_dev, HEM_TYPE_CQE)) {
594 ret = hns_roce_init_hem_table(hr_dev,
595 &hr_dev->mr_table.mtt_cqe_table,
596 HEM_TYPE_CQE, hr_dev->caps.mtt_entry_sz,
597 hr_dev->caps.num_cqe_segs, 1);
599 dev_err(dev, "Failed to init MTT CQE context memory, aborting.\n");
604 ret = hns_roce_init_hem_table(hr_dev, &hr_dev->mr_table.mtpt_table,
605 HEM_TYPE_MTPT, hr_dev->caps.mtpt_entry_sz,
606 hr_dev->caps.num_mtpts, 1);
608 dev_err(dev, "Failed to init MTPT context memory, aborting.\n");
612 ret = hns_roce_init_hem_table(hr_dev, &hr_dev->qp_table.qp_table,
613 HEM_TYPE_QPC, hr_dev->caps.qpc_entry_sz,
614 hr_dev->caps.num_qps, 1);
616 dev_err(dev, "Failed to init QP context memory, aborting.\n");
620 ret = hns_roce_init_hem_table(hr_dev, &hr_dev->qp_table.irrl_table,
622 hr_dev->caps.irrl_entry_sz *
623 hr_dev->caps.max_qp_init_rdma,
624 hr_dev->caps.num_qps, 1);
626 dev_err(dev, "Failed to init irrl_table memory, aborting.\n");
630 if (hr_dev->caps.trrl_entry_sz) {
631 ret = hns_roce_init_hem_table(hr_dev,
632 &hr_dev->qp_table.trrl_table,
634 hr_dev->caps.trrl_entry_sz *
635 hr_dev->caps.max_qp_dest_rdma,
636 hr_dev->caps.num_qps, 1);
639 "Failed to init trrl_table memory, aborting.\n");
644 ret = hns_roce_init_hem_table(hr_dev, &hr_dev->cq_table.table,
645 HEM_TYPE_CQC, hr_dev->caps.cqc_entry_sz,
646 hr_dev->caps.num_cqs, 1);
648 dev_err(dev, "Failed to init CQ context memory, aborting.\n");
652 if (hr_dev->caps.srqc_entry_sz) {
653 ret = hns_roce_init_hem_table(hr_dev, &hr_dev->srq_table.table,
655 hr_dev->caps.srqc_entry_sz,
656 hr_dev->caps.num_srqs, 1);
659 "Failed to init SRQ context memory, aborting.\n");
664 if (hr_dev->caps.num_srqwqe_segs) {
665 ret = hns_roce_init_hem_table(hr_dev,
666 &hr_dev->mr_table.mtt_srqwqe_table,
668 hr_dev->caps.mtt_entry_sz,
669 hr_dev->caps.num_srqwqe_segs, 1);
672 "Failed to init MTT srqwqe memory, aborting.\n");
677 if (hr_dev->caps.num_idx_segs) {
678 ret = hns_roce_init_hem_table(hr_dev,
679 &hr_dev->mr_table.mtt_idx_table,
681 hr_dev->caps.idx_entry_sz,
682 hr_dev->caps.num_idx_segs, 1);
685 "Failed to init MTT idx memory, aborting.\n");
686 goto err_unmap_srqwqe;
690 if (hr_dev->caps.sccc_entry_sz) {
691 ret = hns_roce_init_hem_table(hr_dev,
692 &hr_dev->qp_table.sccc_table,
694 hr_dev->caps.sccc_entry_sz,
695 hr_dev->caps.num_qps, 1);
698 "Failed to init SCC context memory, aborting.\n");
703 if (hr_dev->caps.qpc_timer_entry_sz) {
704 ret = hns_roce_init_hem_table(hr_dev,
705 &hr_dev->qpc_timer_table,
707 hr_dev->caps.qpc_timer_entry_sz,
708 hr_dev->caps.num_qpc_timer, 1);
711 "Failed to init QPC timer memory, aborting.\n");
716 if (hr_dev->caps.cqc_timer_entry_sz) {
717 ret = hns_roce_init_hem_table(hr_dev,
718 &hr_dev->cqc_timer_table,
720 hr_dev->caps.cqc_timer_entry_sz,
721 hr_dev->caps.num_cqc_timer, 1);
724 "Failed to init CQC timer memory, aborting.\n");
725 goto err_unmap_qpc_timer;
732 if (hr_dev->caps.qpc_timer_entry_sz)
733 hns_roce_cleanup_hem_table(hr_dev,
734 &hr_dev->qpc_timer_table);
737 if (hr_dev->caps.sccc_entry_sz)
738 hns_roce_cleanup_hem_table(hr_dev,
739 &hr_dev->qp_table.sccc_table);
742 if (hr_dev->caps.num_idx_segs)
743 hns_roce_cleanup_hem_table(hr_dev,
744 &hr_dev->mr_table.mtt_idx_table);
747 if (hr_dev->caps.num_srqwqe_segs)
748 hns_roce_cleanup_hem_table(hr_dev,
749 &hr_dev->mr_table.mtt_srqwqe_table);
752 if (hr_dev->caps.srqc_entry_sz)
753 hns_roce_cleanup_hem_table(hr_dev, &hr_dev->srq_table.table);
756 hns_roce_cleanup_hem_table(hr_dev, &hr_dev->cq_table.table);
759 if (hr_dev->caps.trrl_entry_sz)
760 hns_roce_cleanup_hem_table(hr_dev,
761 &hr_dev->qp_table.trrl_table);
764 hns_roce_cleanup_hem_table(hr_dev, &hr_dev->qp_table.irrl_table);
767 hns_roce_cleanup_hem_table(hr_dev, &hr_dev->qp_table.qp_table);
770 hns_roce_cleanup_hem_table(hr_dev, &hr_dev->mr_table.mtpt_table);
773 if (hns_roce_check_whether_mhop(hr_dev, HEM_TYPE_CQE))
774 hns_roce_cleanup_hem_table(hr_dev,
775 &hr_dev->mr_table.mtt_cqe_table);
778 hns_roce_cleanup_hem_table(hr_dev, &hr_dev->mr_table.mtt_table);
784 * hns_roce_setup_hca - setup host channel adapter
785 * @hr_dev: pointer to hns roce device
788 static int hns_roce_setup_hca(struct hns_roce_dev *hr_dev)
791 struct device *dev = hr_dev->dev;
793 spin_lock_init(&hr_dev->sm_lock);
794 spin_lock_init(&hr_dev->bt_cmd_lock);
796 if (hr_dev->caps.flags & HNS_ROCE_CAP_FLAG_RECORD_DB) {
797 INIT_LIST_HEAD(&hr_dev->pgdir_list);
798 mutex_init(&hr_dev->pgdir_mutex);
801 ret = hns_roce_init_uar_table(hr_dev);
803 dev_err(dev, "Failed to initialize uar table. aborting\n");
807 ret = hns_roce_uar_alloc(hr_dev, &hr_dev->priv_uar);
809 dev_err(dev, "Failed to allocate priv_uar.\n");
810 goto err_uar_table_free;
813 ret = hns_roce_init_pd_table(hr_dev);
815 dev_err(dev, "Failed to init protected domain table.\n");
816 goto err_uar_alloc_free;
819 ret = hns_roce_init_mr_table(hr_dev);
821 dev_err(dev, "Failed to init memory region table.\n");
822 goto err_pd_table_free;
825 ret = hns_roce_init_cq_table(hr_dev);
827 dev_err(dev, "Failed to init completion queue table.\n");
828 goto err_mr_table_free;
831 ret = hns_roce_init_qp_table(hr_dev);
833 dev_err(dev, "Failed to init queue pair table.\n");
834 goto err_cq_table_free;
837 if (hr_dev->caps.flags & HNS_ROCE_CAP_FLAG_SRQ) {
838 ret = hns_roce_init_srq_table(hr_dev);
841 "Failed to init share receive queue table.\n");
842 goto err_qp_table_free;
849 if (hr_dev->caps.flags & HNS_ROCE_CAP_FLAG_SRQ)
850 hns_roce_cleanup_qp_table(hr_dev);
853 hns_roce_cleanup_cq_table(hr_dev);
856 hns_roce_cleanup_mr_table(hr_dev);
859 hns_roce_cleanup_pd_table(hr_dev);
862 hns_roce_uar_free(hr_dev, &hr_dev->priv_uar);
865 hns_roce_cleanup_uar_table(hr_dev);
869 int hns_roce_init(struct hns_roce_dev *hr_dev)
872 struct device *dev = hr_dev->dev;
874 if (hr_dev->hw->reset) {
875 ret = hr_dev->hw->reset(hr_dev, true);
877 dev_err(dev, "Reset RoCE engine failed!\n");
881 hr_dev->is_reset = false;
883 if (hr_dev->hw->cmq_init) {
884 ret = hr_dev->hw->cmq_init(hr_dev);
886 dev_err(dev, "Init RoCE Command Queue failed!\n");
887 goto error_failed_cmq_init;
891 ret = hr_dev->hw->hw_profile(hr_dev);
893 dev_err(dev, "Get RoCE engine profile failed!\n");
894 goto error_failed_cmd_init;
897 ret = hns_roce_cmd_init(hr_dev);
899 dev_err(dev, "cmd init failed!\n");
900 goto error_failed_cmd_init;
903 ret = hr_dev->hw->init_eq(hr_dev);
905 dev_err(dev, "eq init failed!\n");
906 goto error_failed_eq_table;
909 if (hr_dev->cmd_mod) {
910 ret = hns_roce_cmd_use_events(hr_dev);
912 dev_err(dev, "Switch to event-driven cmd failed!\n");
913 goto error_failed_use_event;
917 ret = hns_roce_init_hem(hr_dev);
919 dev_err(dev, "init HEM(Hardware Entry Memory) failed!\n");
920 goto error_failed_init_hem;
923 ret = hns_roce_setup_hca(hr_dev);
925 dev_err(dev, "setup hca failed!\n");
926 goto error_failed_setup_hca;
929 if (hr_dev->hw->hw_init) {
930 ret = hr_dev->hw->hw_init(hr_dev);
932 dev_err(dev, "hw_init failed!\n");
933 goto error_failed_engine_init;
937 ret = hns_roce_register_device(hr_dev);
939 goto error_failed_register_device;
943 error_failed_register_device:
944 if (hr_dev->hw->hw_exit)
945 hr_dev->hw->hw_exit(hr_dev);
947 error_failed_engine_init:
948 hns_roce_cleanup_bitmap(hr_dev);
950 error_failed_setup_hca:
951 hns_roce_cleanup_hem(hr_dev);
953 error_failed_init_hem:
955 hns_roce_cmd_use_polling(hr_dev);
957 error_failed_use_event:
958 hr_dev->hw->cleanup_eq(hr_dev);
960 error_failed_eq_table:
961 hns_roce_cmd_cleanup(hr_dev);
963 error_failed_cmd_init:
964 if (hr_dev->hw->cmq_exit)
965 hr_dev->hw->cmq_exit(hr_dev);
967 error_failed_cmq_init:
968 if (hr_dev->hw->reset) {
969 if (hr_dev->hw->reset(hr_dev, false))
970 dev_err(dev, "Dereset RoCE engine failed!\n");
975 EXPORT_SYMBOL_GPL(hns_roce_init);
977 void hns_roce_exit(struct hns_roce_dev *hr_dev)
979 hns_roce_unregister_device(hr_dev);
981 if (hr_dev->hw->hw_exit)
982 hr_dev->hw->hw_exit(hr_dev);
983 hns_roce_cleanup_bitmap(hr_dev);
984 hns_roce_cleanup_hem(hr_dev);
987 hns_roce_cmd_use_polling(hr_dev);
989 hr_dev->hw->cleanup_eq(hr_dev);
990 hns_roce_cmd_cleanup(hr_dev);
991 if (hr_dev->hw->cmq_exit)
992 hr_dev->hw->cmq_exit(hr_dev);
993 if (hr_dev->hw->reset)
994 hr_dev->hw->reset(hr_dev, false);
996 EXPORT_SYMBOL_GPL(hns_roce_exit);
998 MODULE_LICENSE("Dual BSD/GPL");
999 MODULE_AUTHOR("Wei Hu <xavier.huwei@huawei.com>");
1000 MODULE_AUTHOR("Nenglong Zhao <zhaonenglong@hisilicon.com>");
1001 MODULE_AUTHOR("Lijun Ou <oulijun@huawei.com>");
1002 MODULE_DESCRIPTION("HNS RoCE Driver");