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 <rdma/ib_addr.h>
36 #include <rdma/ib_smi.h>
37 #include <rdma/ib_user_verbs.h>
38 #include "hns_roce_common.h"
39 #include "hns_roce_device.h"
40 #include "hns_roce_user.h"
41 #include "hns_roce_hem.h"
44 * hns_roce_addrconf_ifid_eui48 - Get default gid.
50 * gid[0..7] = fe80 0000 0000 0000
54 * gid[11] = ff (VLAN ID high byte (4 MS bits))
55 * gid[12] = fe (VLAN ID low byte)
60 static void hns_roce_addrconf_ifid_eui48(u8 *eui, u16 vlan_id,
61 struct net_device *dev)
63 memcpy(eui, dev->dev_addr, 3);
64 memcpy(eui + 5, dev->dev_addr + 3, 3);
65 if (vlan_id < 0x1000) {
66 eui[3] = vlan_id >> 8;
67 eui[4] = vlan_id & 0xff;
75 static void hns_roce_make_default_gid(struct net_device *dev, union ib_gid *gid)
77 memset(gid, 0, sizeof(*gid));
80 hns_roce_addrconf_ifid_eui48(&gid->raw[8], 0xffff, dev);
84 * hns_get_gid_index - Get gid index.
85 * @hr_dev: pointer to structure hns_roce_dev.
86 * @port: port, value range: 0 ~ MAX
87 * @gid_index: gid_index, value range: 0 ~ MAX
89 * N ports shared gids, allocation method as follow:
90 * GID[0][0], GID[1][0],.....GID[N - 1][0],
91 * GID[0][0], GID[1][0],.....GID[N - 1][0],
94 int hns_get_gid_index(struct hns_roce_dev *hr_dev, u8 port, int gid_index)
96 return gid_index * hr_dev->caps.num_ports + port;
99 static int hns_roce_set_gid(struct hns_roce_dev *hr_dev, u8 port, int gid_index,
102 struct device *dev = &hr_dev->pdev->dev;
105 if (gid_index >= hr_dev->caps.gid_table_len[port]) {
106 dev_err(dev, "gid_index %d illegal, port %d gid range: 0~%d\n",
107 gid_index, port, hr_dev->caps.gid_table_len[port] - 1);
111 gid_idx = hns_get_gid_index(hr_dev, port, gid_index);
113 if (!memcmp(gid, &hr_dev->iboe.gid_table[gid_idx], sizeof(*gid)))
116 memcpy(&hr_dev->iboe.gid_table[gid_idx], gid, sizeof(*gid));
118 hr_dev->hw->set_gid(hr_dev, port, gid_index, gid);
123 static void hns_roce_set_mac(struct hns_roce_dev *hr_dev, u8 port, u8 *addr)
128 if (!memcmp(hr_dev->dev_addr[port], addr, MAC_ADDR_OCTET_NUM))
131 for (i = 0; i < MAC_ADDR_OCTET_NUM; i++)
132 hr_dev->dev_addr[port][i] = addr[i];
134 phy_port = hr_dev->iboe.phy_port[port];
135 hr_dev->hw->set_mac(hr_dev, phy_port, addr);
138 static void hns_roce_set_mtu(struct hns_roce_dev *hr_dev, u8 port, int mtu)
140 u8 phy_port = hr_dev->iboe.phy_port[port];
143 tmp = iboe_get_mtu(mtu);
147 hr_dev->hw->set_mtu(hr_dev, phy_port, tmp);
150 static void hns_roce_update_gids(struct hns_roce_dev *hr_dev, int port)
152 struct ib_event event;
154 /* Refresh gid in ib_cache */
155 event.device = &hr_dev->ib_dev;
156 event.element.port_num = port + 1;
157 event.event = IB_EVENT_GID_CHANGE;
158 ib_dispatch_event(&event);
161 static int handle_en_event(struct hns_roce_dev *hr_dev, u8 port,
164 struct device *dev = &hr_dev->pdev->dev;
165 struct net_device *netdev;
170 netdev = hr_dev->iboe.netdevs[port];
172 dev_err(dev, "port(%d) can't find netdev\n", port);
176 spin_lock_irqsave(&hr_dev->iboe.lock, flags);
181 case NETDEV_REGISTER:
182 case NETDEV_CHANGEADDR:
183 hns_roce_set_mac(hr_dev, port, netdev->dev_addr);
184 hns_roce_make_default_gid(netdev, &gid);
185 ret = hns_roce_set_gid(hr_dev, port, 0, &gid);
187 hns_roce_update_gids(hr_dev, port);
191 * In v1 engine, only support all ports closed together.
195 dev_dbg(dev, "NETDEV event = 0x%x!\n", (u32)(event));
199 spin_unlock_irqrestore(&hr_dev->iboe.lock, flags);
203 static int hns_roce_netdev_event(struct notifier_block *self,
204 unsigned long event, void *ptr)
206 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
207 struct hns_roce_ib_iboe *iboe = NULL;
208 struct hns_roce_dev *hr_dev = NULL;
212 hr_dev = container_of(self, struct hns_roce_dev, iboe.nb);
213 iboe = &hr_dev->iboe;
215 for (port = 0; port < hr_dev->caps.num_ports; port++) {
216 if (dev == iboe->netdevs[port]) {
217 ret = handle_en_event(hr_dev, port, event);
227 static void hns_roce_addr_event(int event, struct net_device *event_netdev,
228 struct hns_roce_dev *hr_dev, union ib_gid *gid)
230 struct hns_roce_ib_iboe *iboe = NULL;
231 int gid_table_len = 0;
238 struct net_device *real_dev = rdma_vlan_dev_real_dev(event_netdev) ?
239 rdma_vlan_dev_real_dev(event_netdev) :
242 if (event != NETDEV_UP && event != NETDEV_DOWN)
245 iboe = &hr_dev->iboe;
246 while (port < hr_dev->caps.num_ports) {
247 if (real_dev == iboe->netdevs[port])
252 if (port >= hr_dev->caps.num_ports) {
253 dev_dbg(&hr_dev->pdev->dev, "can't find netdev\n");
257 memset(zgid.raw, 0, sizeof(zgid.raw));
259 gid_table_len = hr_dev->caps.gid_table_len[port];
261 spin_lock_irqsave(&hr_dev->iboe.lock, flags);
263 for (i = 0; i < gid_table_len; i++) {
264 gid_idx = hns_get_gid_index(hr_dev, port, i);
265 if (!memcmp(gid->raw, iboe->gid_table[gid_idx].raw,
268 if (free < 0 && !memcmp(zgid.raw,
269 iboe->gid_table[gid_idx].raw, sizeof(zgid.raw)))
273 if (i >= gid_table_len) {
275 spin_unlock_irqrestore(&hr_dev->iboe.lock, flags);
276 dev_dbg(&hr_dev->pdev->dev,
277 "gid_index overflow, port(%d)\n", port);
280 if (!hns_roce_set_gid(hr_dev, port, free, gid))
281 hns_roce_update_gids(hr_dev, port);
282 } else if (event == NETDEV_DOWN) {
283 if (!hns_roce_set_gid(hr_dev, port, i, &zgid))
284 hns_roce_update_gids(hr_dev, port);
287 spin_unlock_irqrestore(&hr_dev->iboe.lock, flags);
290 static int hns_roce_inet_event(struct notifier_block *self, unsigned long event,
293 struct in_ifaddr *ifa = ptr;
294 struct hns_roce_dev *hr_dev;
295 struct net_device *dev = ifa->ifa_dev->dev;
298 ipv6_addr_set_v4mapped(ifa->ifa_address, (struct in6_addr *)&gid);
300 hr_dev = container_of(self, struct hns_roce_dev, iboe.nb_inet);
302 hns_roce_addr_event(event, dev, hr_dev, &gid);
307 static int hns_roce_setup_mtu_gids(struct hns_roce_dev *hr_dev)
309 struct in_ifaddr *ifa_list = NULL;
310 union ib_gid gid = {{0} };
316 for (i = 0; i < hr_dev->caps.num_ports; i++) {
317 hns_roce_set_mtu(hr_dev, i,
318 ib_mtu_enum_to_int(hr_dev->caps.max_mtu));
319 hns_roce_set_mac(hr_dev, i, hr_dev->iboe.netdevs[i]->dev_addr);
321 if (hr_dev->iboe.netdevs[i]->ip_ptr) {
322 ifa_list = hr_dev->iboe.netdevs[i]->ip_ptr->ifa_list;
325 ipaddr = ifa_list->ifa_address;
326 ipv6_addr_set_v4mapped(ipaddr,
327 (struct in6_addr *)&gid);
328 ret = hns_roce_set_gid(hr_dev, i, index, &gid);
332 ifa_list = ifa_list->ifa_next;
334 hns_roce_update_gids(hr_dev, i);
341 static int hns_roce_query_device(struct ib_device *ib_dev,
342 struct ib_device_attr *props,
343 struct ib_udata *uhw)
345 struct hns_roce_dev *hr_dev = to_hr_dev(ib_dev);
347 memset(props, 0, sizeof(*props));
349 props->sys_image_guid = hr_dev->sys_image_guid;
350 props->max_mr_size = (u64)(~(0ULL));
351 props->page_size_cap = hr_dev->caps.page_size_cap;
352 props->vendor_id = hr_dev->vendor_id;
353 props->vendor_part_id = hr_dev->vendor_part_id;
354 props->hw_ver = hr_dev->hw_rev;
355 props->max_qp = hr_dev->caps.num_qps;
356 props->max_qp_wr = hr_dev->caps.max_wqes;
357 props->device_cap_flags = IB_DEVICE_PORT_ACTIVE_EVENT |
358 IB_DEVICE_RC_RNR_NAK_GEN |
359 IB_DEVICE_LOCAL_DMA_LKEY;
360 props->max_sge = hr_dev->caps.max_sq_sg;
361 props->max_sge_rd = 1;
362 props->max_cq = hr_dev->caps.num_cqs;
363 props->max_cqe = hr_dev->caps.max_cqes;
364 props->max_mr = hr_dev->caps.num_mtpts;
365 props->max_pd = hr_dev->caps.num_pds;
366 props->max_qp_rd_atom = hr_dev->caps.max_qp_dest_rdma;
367 props->max_qp_init_rd_atom = hr_dev->caps.max_qp_init_rdma;
368 props->atomic_cap = IB_ATOMIC_NONE;
369 props->max_pkeys = 1;
370 props->local_ca_ack_delay = hr_dev->caps.local_ca_ack_delay;
375 static int hns_roce_query_port(struct ib_device *ib_dev, u8 port_num,
376 struct ib_port_attr *props)
378 struct hns_roce_dev *hr_dev = to_hr_dev(ib_dev);
379 struct device *dev = &hr_dev->pdev->dev;
380 struct net_device *net_dev;
385 assert(port_num > 0);
388 memset(props, 0, sizeof(*props));
390 props->max_mtu = hr_dev->caps.max_mtu;
391 props->gid_tbl_len = hr_dev->caps.gid_table_len[port];
392 props->port_cap_flags = IB_PORT_CM_SUP | IB_PORT_REINIT_SUP |
393 IB_PORT_VENDOR_CLASS_SUP |
394 IB_PORT_BOOT_MGMT_SUP;
395 props->max_msg_sz = HNS_ROCE_MAX_MSG_LEN;
396 props->pkey_tbl_len = 1;
397 props->active_width = IB_WIDTH_4X;
398 props->active_speed = 1;
400 spin_lock_irqsave(&hr_dev->iboe.lock, flags);
402 net_dev = hr_dev->iboe.netdevs[port];
404 spin_unlock_irqrestore(&hr_dev->iboe.lock, flags);
405 dev_err(dev, "find netdev %d failed!\r\n", port);
409 mtu = iboe_get_mtu(net_dev->mtu);
410 props->active_mtu = mtu ? min(props->max_mtu, mtu) : IB_MTU_256;
411 props->state = (netif_running(net_dev) && netif_carrier_ok(net_dev)) ?
412 IB_PORT_ACTIVE : IB_PORT_DOWN;
413 props->phys_state = (props->state == IB_PORT_ACTIVE) ? 5 : 3;
415 spin_unlock_irqrestore(&hr_dev->iboe.lock, flags);
420 static enum rdma_link_layer hns_roce_get_link_layer(struct ib_device *device,
423 return IB_LINK_LAYER_ETHERNET;
426 static int hns_roce_query_gid(struct ib_device *ib_dev, u8 port_num, int index,
429 struct hns_roce_dev *hr_dev = to_hr_dev(ib_dev);
430 struct device *dev = &hr_dev->pdev->dev;
434 if (port_num < 1 || port_num > hr_dev->caps.num_ports ||
435 index >= hr_dev->caps.gid_table_len[port_num - 1]) {
437 "port_num %d index %d illegal! correct range: port_num 1~%d index 0~%d!\n",
438 port_num, index, hr_dev->caps.num_ports,
439 hr_dev->caps.gid_table_len[port_num - 1] - 1);
444 gid_idx = hns_get_gid_index(hr_dev, port, index);
445 if (gid_idx >= HNS_ROCE_MAX_GID_NUM) {
446 dev_err(dev, "port_num %d index %d illegal! total gid num %d!\n",
447 port_num, index, HNS_ROCE_MAX_GID_NUM);
451 memcpy(gid->raw, hr_dev->iboe.gid_table[gid_idx].raw,
457 static int hns_roce_query_pkey(struct ib_device *ib_dev, u8 port, u16 index,
465 static int hns_roce_modify_device(struct ib_device *ib_dev, int mask,
466 struct ib_device_modify *props)
470 if (mask & ~IB_DEVICE_MODIFY_NODE_DESC)
473 if (mask & IB_DEVICE_MODIFY_NODE_DESC) {
474 spin_lock_irqsave(&to_hr_dev(ib_dev)->sm_lock, flags);
475 memcpy(ib_dev->node_desc, props->node_desc, NODE_DESC_SIZE);
476 spin_unlock_irqrestore(&to_hr_dev(ib_dev)->sm_lock, flags);
482 static int hns_roce_modify_port(struct ib_device *ib_dev, u8 port_num, int mask,
483 struct ib_port_modify *props)
488 static struct ib_ucontext *hns_roce_alloc_ucontext(struct ib_device *ib_dev,
489 struct ib_udata *udata)
492 struct hns_roce_ucontext *context;
493 struct hns_roce_ib_alloc_ucontext_resp resp;
494 struct hns_roce_dev *hr_dev = to_hr_dev(ib_dev);
496 resp.qp_tab_size = hr_dev->caps.num_qps;
498 context = kmalloc(sizeof(*context), GFP_KERNEL);
500 return ERR_PTR(-ENOMEM);
502 ret = hns_roce_uar_alloc(hr_dev, &context->uar);
504 goto error_fail_uar_alloc;
506 ret = ib_copy_to_udata(udata, &resp, sizeof(resp));
508 goto error_fail_copy_to_udata;
510 return &context->ibucontext;
512 error_fail_copy_to_udata:
513 hns_roce_uar_free(hr_dev, &context->uar);
515 error_fail_uar_alloc:
521 static int hns_roce_dealloc_ucontext(struct ib_ucontext *ibcontext)
523 struct hns_roce_ucontext *context = to_hr_ucontext(ibcontext);
525 hns_roce_uar_free(to_hr_dev(ibcontext->device), &context->uar);
531 static int hns_roce_mmap(struct ib_ucontext *context,
532 struct vm_area_struct *vma)
534 if (((vma->vm_end - vma->vm_start) % PAGE_SIZE) != 0)
537 if (vma->vm_pgoff == 0) {
538 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
539 if (io_remap_pfn_range(vma, vma->vm_start,
540 to_hr_ucontext(context)->uar.pfn,
541 PAGE_SIZE, vma->vm_page_prot))
551 static int hns_roce_port_immutable(struct ib_device *ib_dev, u8 port_num,
552 struct ib_port_immutable *immutable)
554 struct ib_port_attr attr;
557 ret = hns_roce_query_port(ib_dev, port_num, &attr);
561 immutable->pkey_tbl_len = attr.pkey_tbl_len;
562 immutable->gid_tbl_len = attr.gid_tbl_len;
564 immutable->core_cap_flags = RDMA_CORE_PORT_IBA_ROCE;
565 immutable->max_mad_size = IB_MGMT_MAD_SIZE;
570 static void hns_roce_unregister_device(struct hns_roce_dev *hr_dev)
572 struct hns_roce_ib_iboe *iboe = &hr_dev->iboe;
574 unregister_inetaddr_notifier(&iboe->nb_inet);
575 unregister_netdevice_notifier(&iboe->nb);
576 ib_unregister_device(&hr_dev->ib_dev);
579 static int hns_roce_register_device(struct hns_roce_dev *hr_dev)
582 struct hns_roce_ib_iboe *iboe = NULL;
583 struct ib_device *ib_dev = NULL;
584 struct device *dev = &hr_dev->pdev->dev;
586 iboe = &hr_dev->iboe;
588 ib_dev = &hr_dev->ib_dev;
589 strlcpy(ib_dev->name, "hisi_%d", IB_DEVICE_NAME_MAX);
591 ib_dev->owner = THIS_MODULE;
592 ib_dev->node_type = RDMA_NODE_IB_CA;
593 ib_dev->dma_device = dev;
595 ib_dev->phys_port_cnt = hr_dev->caps.num_ports;
596 ib_dev->local_dma_lkey = hr_dev->caps.reserved_lkey;
597 ib_dev->num_comp_vectors = hr_dev->caps.num_comp_vectors;
598 ib_dev->uverbs_abi_ver = 1;
599 ib_dev->uverbs_cmd_mask =
600 (1ULL << IB_USER_VERBS_CMD_GET_CONTEXT) |
601 (1ULL << IB_USER_VERBS_CMD_QUERY_DEVICE) |
602 (1ULL << IB_USER_VERBS_CMD_QUERY_PORT) |
603 (1ULL << IB_USER_VERBS_CMD_ALLOC_PD) |
604 (1ULL << IB_USER_VERBS_CMD_DEALLOC_PD) |
605 (1ULL << IB_USER_VERBS_CMD_REG_MR) |
606 (1ULL << IB_USER_VERBS_CMD_DEREG_MR) |
607 (1ULL << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL) |
608 (1ULL << IB_USER_VERBS_CMD_CREATE_CQ) |
609 (1ULL << IB_USER_VERBS_CMD_DESTROY_CQ) |
610 (1ULL << IB_USER_VERBS_CMD_CREATE_QP) |
611 (1ULL << IB_USER_VERBS_CMD_MODIFY_QP) |
612 (1ULL << IB_USER_VERBS_CMD_QUERY_QP) |
613 (1ULL << IB_USER_VERBS_CMD_DESTROY_QP);
615 /* HCA||device||port */
616 ib_dev->modify_device = hns_roce_modify_device;
617 ib_dev->query_device = hns_roce_query_device;
618 ib_dev->query_port = hns_roce_query_port;
619 ib_dev->modify_port = hns_roce_modify_port;
620 ib_dev->get_link_layer = hns_roce_get_link_layer;
621 ib_dev->query_gid = hns_roce_query_gid;
622 ib_dev->query_pkey = hns_roce_query_pkey;
623 ib_dev->alloc_ucontext = hns_roce_alloc_ucontext;
624 ib_dev->dealloc_ucontext = hns_roce_dealloc_ucontext;
625 ib_dev->mmap = hns_roce_mmap;
628 ib_dev->alloc_pd = hns_roce_alloc_pd;
629 ib_dev->dealloc_pd = hns_roce_dealloc_pd;
632 ib_dev->create_ah = hns_roce_create_ah;
633 ib_dev->query_ah = hns_roce_query_ah;
634 ib_dev->destroy_ah = hns_roce_destroy_ah;
637 ib_dev->create_qp = hns_roce_create_qp;
638 ib_dev->modify_qp = hns_roce_modify_qp;
639 ib_dev->query_qp = hr_dev->hw->query_qp;
640 ib_dev->destroy_qp = hr_dev->hw->destroy_qp;
641 ib_dev->post_send = hr_dev->hw->post_send;
642 ib_dev->post_recv = hr_dev->hw->post_recv;
645 ib_dev->create_cq = hns_roce_ib_create_cq;
646 ib_dev->destroy_cq = hns_roce_ib_destroy_cq;
647 ib_dev->req_notify_cq = hr_dev->hw->req_notify_cq;
648 ib_dev->poll_cq = hr_dev->hw->poll_cq;
651 ib_dev->get_dma_mr = hns_roce_get_dma_mr;
652 ib_dev->reg_user_mr = hns_roce_reg_user_mr;
653 ib_dev->dereg_mr = hns_roce_dereg_mr;
656 ib_dev->get_port_immutable = hns_roce_port_immutable;
658 ret = ib_register_device(ib_dev, NULL);
660 dev_err(dev, "ib_register_device failed!\n");
664 ret = hns_roce_setup_mtu_gids(hr_dev);
666 dev_err(dev, "roce_setup_mtu_gids failed!\n");
667 goto error_failed_setup_mtu_gids;
670 spin_lock_init(&iboe->lock);
672 iboe->nb.notifier_call = hns_roce_netdev_event;
673 ret = register_netdevice_notifier(&iboe->nb);
675 dev_err(dev, "register_netdevice_notifier failed!\n");
676 goto error_failed_setup_mtu_gids;
679 iboe->nb_inet.notifier_call = hns_roce_inet_event;
680 ret = register_inetaddr_notifier(&iboe->nb_inet);
682 dev_err(dev, "register inet addr notifier failed!\n");
683 goto error_failed_register_inetaddr_notifier;
688 error_failed_register_inetaddr_notifier:
689 unregister_netdevice_notifier(&iboe->nb);
691 error_failed_setup_mtu_gids:
692 ib_unregister_device(ib_dev);
697 static const struct of_device_id hns_roce_of_match[] = {
698 { .compatible = "hisilicon,hns-roce-v1", .data = &hns_roce_hw_v1, },
701 MODULE_DEVICE_TABLE(of, hns_roce_of_match);
703 static const struct acpi_device_id hns_roce_acpi_match[] = {
704 { "HISI00D1", (kernel_ulong_t)&hns_roce_hw_v1 },
707 MODULE_DEVICE_TABLE(acpi, hns_roce_acpi_match);
709 static int hns_roce_node_match(struct device *dev, void *fwnode)
711 return dev->fwnode == fwnode;
715 platform_device *hns_roce_find_pdev(struct fwnode_handle *fwnode)
719 /* get the 'device'corresponding to matching 'fwnode' */
720 dev = bus_find_device(&platform_bus_type, NULL,
721 fwnode, hns_roce_node_match);
722 /* get the platform device */
723 return dev ? to_platform_device(dev) : NULL;
726 static int hns_roce_get_cfg(struct hns_roce_dev *hr_dev)
732 struct device *dev = &hr_dev->pdev->dev;
733 struct device_node *net_node;
734 struct net_device *netdev = NULL;
735 struct platform_device *pdev = NULL;
736 struct resource *res;
738 /* check if we are compatible with the underlying SoC */
739 if (dev_of_node(dev)) {
740 const struct of_device_id *of_id;
742 of_id = of_match_node(hns_roce_of_match, dev->of_node);
744 dev_err(dev, "device is not compatible!\n");
747 hr_dev->hw = (struct hns_roce_hw *)of_id->data;
749 dev_err(dev, "couldn't get H/W specific DT data!\n");
752 } else if (is_acpi_device_node(dev->fwnode)) {
753 const struct acpi_device_id *acpi_id;
755 acpi_id = acpi_match_device(hns_roce_acpi_match, dev);
757 dev_err(dev, "device is not compatible!\n");
760 hr_dev->hw = (struct hns_roce_hw *) acpi_id->driver_data;
762 dev_err(dev, "couldn't get H/W specific ACPI data!\n");
766 dev_err(dev, "can't read compatibility data from DT or ACPI\n");
770 /* get the mapped register base address */
771 res = platform_get_resource(hr_dev->pdev, IORESOURCE_MEM, 0);
773 dev_err(dev, "memory resource not found!\n");
776 hr_dev->reg_base = devm_ioremap_resource(dev, res);
777 if (IS_ERR(hr_dev->reg_base))
778 return PTR_ERR(hr_dev->reg_base);
780 /* get the RoCE associated ethernet ports or netdevices */
781 for (i = 0; i < HNS_ROCE_MAX_PORTS; i++) {
782 if (dev_of_node(dev)) {
783 net_node = of_parse_phandle(dev->of_node, "eth-handle",
787 pdev = of_find_device_by_node(net_node);
788 } else if (is_acpi_device_node(dev->fwnode)) {
789 struct acpi_reference_args args;
790 struct fwnode_handle *fwnode;
792 ret = acpi_node_get_property_reference(dev->fwnode,
797 fwnode = acpi_fwnode_handle(args.adev);
798 pdev = hns_roce_find_pdev(fwnode);
800 dev_err(dev, "cannot read data from DT or ACPI\n");
805 netdev = platform_get_drvdata(pdev);
808 hr_dev->iboe.netdevs[port_cnt] = netdev;
809 hr_dev->iboe.phy_port[port_cnt] = phy_port;
811 dev_err(dev, "no netdev found with pdev %s\n",
820 dev_err(dev, "unable to get eth-handle for available ports!\n");
824 hr_dev->caps.num_ports = port_cnt;
826 /* cmd issue mode: 0 is poll, 1 is event */
828 hr_dev->loop_idc = 0;
830 /* read the interrupt names from the DT or ACPI */
831 ret = device_property_read_string_array(dev, "interrupt-names",
833 HNS_ROCE_MAX_IRQ_NUM);
835 dev_err(dev, "couldn't get interrupt names from DT or ACPI!\n");
839 /* fetch the interrupt numbers */
840 for (i = 0; i < HNS_ROCE_MAX_IRQ_NUM; i++) {
841 hr_dev->irq[i] = platform_get_irq(hr_dev->pdev, i);
842 if (hr_dev->irq[i] <= 0) {
843 dev_err(dev, "platform get of irq[=%d] failed!\n", i);
851 static int hns_roce_init_hem(struct hns_roce_dev *hr_dev)
854 struct device *dev = &hr_dev->pdev->dev;
856 ret = hns_roce_init_hem_table(hr_dev, &hr_dev->mr_table.mtt_table,
857 HEM_TYPE_MTT, hr_dev->caps.mtt_entry_sz,
858 hr_dev->caps.num_mtt_segs, 1);
860 dev_err(dev, "Failed to init MTT context memory, aborting.\n");
864 ret = hns_roce_init_hem_table(hr_dev, &hr_dev->mr_table.mtpt_table,
865 HEM_TYPE_MTPT, hr_dev->caps.mtpt_entry_sz,
866 hr_dev->caps.num_mtpts, 1);
868 dev_err(dev, "Failed to init MTPT context memory, aborting.\n");
872 ret = hns_roce_init_hem_table(hr_dev, &hr_dev->qp_table.qp_table,
873 HEM_TYPE_QPC, hr_dev->caps.qpc_entry_sz,
874 hr_dev->caps.num_qps, 1);
876 dev_err(dev, "Failed to init QP context memory, aborting.\n");
880 ret = hns_roce_init_hem_table(hr_dev, &hr_dev->qp_table.irrl_table,
882 hr_dev->caps.irrl_entry_sz *
883 hr_dev->caps.max_qp_init_rdma,
884 hr_dev->caps.num_qps, 1);
886 dev_err(dev, "Failed to init irrl_table memory, aborting.\n");
890 ret = hns_roce_init_hem_table(hr_dev, &hr_dev->cq_table.table,
891 HEM_TYPE_CQC, hr_dev->caps.cqc_entry_sz,
892 hr_dev->caps.num_cqs, 1);
894 dev_err(dev, "Failed to init CQ context memory, aborting.\n");
901 hns_roce_cleanup_hem_table(hr_dev, &hr_dev->qp_table.irrl_table);
904 hns_roce_cleanup_hem_table(hr_dev, &hr_dev->qp_table.qp_table);
907 hns_roce_cleanup_hem_table(hr_dev, &hr_dev->mr_table.mtpt_table);
910 hns_roce_cleanup_hem_table(hr_dev, &hr_dev->mr_table.mtt_table);
916 * hns_roce_setup_hca - setup host channel adapter
917 * @hr_dev: pointer to hns roce device
920 static int hns_roce_setup_hca(struct hns_roce_dev *hr_dev)
923 struct device *dev = &hr_dev->pdev->dev;
925 spin_lock_init(&hr_dev->sm_lock);
926 spin_lock_init(&hr_dev->cq_db_lock);
927 spin_lock_init(&hr_dev->bt_cmd_lock);
929 ret = hns_roce_init_uar_table(hr_dev);
931 dev_err(dev, "Failed to initialize uar table. aborting\n");
935 ret = hns_roce_uar_alloc(hr_dev, &hr_dev->priv_uar);
937 dev_err(dev, "Failed to allocate priv_uar.\n");
938 goto err_uar_table_free;
941 ret = hns_roce_init_pd_table(hr_dev);
943 dev_err(dev, "Failed to init protected domain table.\n");
944 goto err_uar_alloc_free;
947 ret = hns_roce_init_mr_table(hr_dev);
949 dev_err(dev, "Failed to init memory region table.\n");
950 goto err_pd_table_free;
953 ret = hns_roce_init_cq_table(hr_dev);
955 dev_err(dev, "Failed to init completion queue table.\n");
956 goto err_mr_table_free;
959 ret = hns_roce_init_qp_table(hr_dev);
961 dev_err(dev, "Failed to init queue pair table.\n");
962 goto err_cq_table_free;
968 hns_roce_cleanup_cq_table(hr_dev);
971 hns_roce_cleanup_mr_table(hr_dev);
974 hns_roce_cleanup_pd_table(hr_dev);
977 hns_roce_uar_free(hr_dev, &hr_dev->priv_uar);
980 hns_roce_cleanup_uar_table(hr_dev);
985 * hns_roce_probe - RoCE driver entrance
986 * @pdev: pointer to platform device
990 static int hns_roce_probe(struct platform_device *pdev)
993 struct hns_roce_dev *hr_dev;
994 struct device *dev = &pdev->dev;
996 hr_dev = (struct hns_roce_dev *)ib_alloc_device(sizeof(*hr_dev));
1000 memset((u8 *)hr_dev + sizeof(struct ib_device), 0,
1001 sizeof(struct hns_roce_dev) - sizeof(struct ib_device));
1003 hr_dev->pdev = pdev;
1004 platform_set_drvdata(pdev, hr_dev);
1006 if (dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64ULL)) &&
1007 dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32ULL))) {
1008 dev_err(dev, "Not usable DMA addressing mode\n");
1010 goto error_failed_get_cfg;
1013 ret = hns_roce_get_cfg(hr_dev);
1015 dev_err(dev, "Get Configuration failed!\n");
1016 goto error_failed_get_cfg;
1019 ret = hr_dev->hw->reset(hr_dev, true);
1021 dev_err(dev, "Reset RoCE engine failed!\n");
1022 goto error_failed_get_cfg;
1025 hr_dev->hw->hw_profile(hr_dev);
1027 ret = hns_roce_cmd_init(hr_dev);
1029 dev_err(dev, "cmd init failed!\n");
1030 goto error_failed_cmd_init;
1033 ret = hns_roce_init_eq_table(hr_dev);
1035 dev_err(dev, "eq init failed!\n");
1036 goto error_failed_eq_table;
1039 if (hr_dev->cmd_mod) {
1040 ret = hns_roce_cmd_use_events(hr_dev);
1042 dev_err(dev, "Switch to event-driven cmd failed!\n");
1043 goto error_failed_use_event;
1047 ret = hns_roce_init_hem(hr_dev);
1049 dev_err(dev, "init HEM(Hardware Entry Memory) failed!\n");
1050 goto error_failed_init_hem;
1053 ret = hns_roce_setup_hca(hr_dev);
1055 dev_err(dev, "setup hca failed!\n");
1056 goto error_failed_setup_hca;
1059 ret = hr_dev->hw->hw_init(hr_dev);
1061 dev_err(dev, "hw_init failed!\n");
1062 goto error_failed_engine_init;
1065 ret = hns_roce_register_device(hr_dev);
1067 goto error_failed_register_device;
1071 error_failed_register_device:
1072 hr_dev->hw->hw_exit(hr_dev);
1074 error_failed_engine_init:
1075 hns_roce_cleanup_bitmap(hr_dev);
1077 error_failed_setup_hca:
1078 hns_roce_cleanup_hem(hr_dev);
1080 error_failed_init_hem:
1081 if (hr_dev->cmd_mod)
1082 hns_roce_cmd_use_polling(hr_dev);
1084 error_failed_use_event:
1085 hns_roce_cleanup_eq_table(hr_dev);
1087 error_failed_eq_table:
1088 hns_roce_cmd_cleanup(hr_dev);
1090 error_failed_cmd_init:
1091 ret = hr_dev->hw->reset(hr_dev, false);
1093 dev_err(&hr_dev->pdev->dev, "roce_engine reset fail\n");
1095 error_failed_get_cfg:
1096 ib_dealloc_device(&hr_dev->ib_dev);
1102 * hns_roce_remove - remove RoCE device
1103 * @pdev: pointer to platform device
1105 static int hns_roce_remove(struct platform_device *pdev)
1107 struct hns_roce_dev *hr_dev = platform_get_drvdata(pdev);
1109 hns_roce_unregister_device(hr_dev);
1110 hr_dev->hw->hw_exit(hr_dev);
1111 hns_roce_cleanup_bitmap(hr_dev);
1112 hns_roce_cleanup_hem(hr_dev);
1114 if (hr_dev->cmd_mod)
1115 hns_roce_cmd_use_polling(hr_dev);
1117 hns_roce_cleanup_eq_table(hr_dev);
1118 hns_roce_cmd_cleanup(hr_dev);
1119 hr_dev->hw->reset(hr_dev, false);
1121 ib_dealloc_device(&hr_dev->ib_dev);
1126 static struct platform_driver hns_roce_driver = {
1127 .probe = hns_roce_probe,
1128 .remove = hns_roce_remove,
1131 .of_match_table = hns_roce_of_match,
1132 .acpi_match_table = ACPI_PTR(hns_roce_acpi_match),
1136 module_platform_driver(hns_roce_driver);
1138 MODULE_LICENSE("Dual BSD/GPL");
1139 MODULE_AUTHOR("Wei Hu <xavier.huwei@huawei.com>");
1140 MODULE_AUTHOR("Nenglong Zhao <zhaonenglong@hisilicon.com>");
1141 MODULE_AUTHOR("Lijun Ou <oulijun@huawei.com>");
1142 MODULE_DESCRIPTION("HNS RoCE Driver");