linux/drivers/infiniband/hw/hns/hns_roce_main.c
<<
>>
Prefs
   1/*
   2 * Copyright (c) 2016 Hisilicon Limited.
   3 * Copyright (c) 2007, 2008 Mellanox Technologies. All rights reserved.
   4 *
   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:
  10 *
  11 *     Redistribution and use in source and binary forms, with or
  12 *     without modification, are permitted provided that the following
  13 *     conditions are met:
  14 *
  15 *      - Redistributions of source code must retain the above
  16 *        copyright notice, this list of conditions and the following
  17 *        disclaimer.
  18 *
  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.
  23 *
  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
  31 * SOFTWARE.
  32 */
  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"
  42
  43/**
  44 * hns_roce_addrconf_ifid_eui48 - Get default gid.
  45 * @eui: eui.
  46 * @vlan_id:  gid
  47 * @dev:  net device
  48 * Description:
  49 *    MAC convert to GID
  50 *        gid[0..7] = fe80 0000 0000 0000
  51 *        gid[8] = mac[0] ^ 2
  52 *        gid[9] = mac[1]
  53 *        gid[10] = mac[2]
  54 *        gid[11] = ff        (VLAN ID high byte (4 MS bits))
  55 *        gid[12] = fe        (VLAN ID low byte)
  56 *        gid[13] = mac[3]
  57 *        gid[14] = mac[4]
  58 *        gid[15] = mac[5]
  59 */
  60static void hns_roce_addrconf_ifid_eui48(u8 *eui, u16 vlan_id,
  61                                         struct net_device *dev)
  62{
  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;
  68        } else {
  69                eui[3] = 0xff;
  70                eui[4] = 0xfe;
  71        }
  72        eui[0] ^= 2;
  73}
  74
  75static void hns_roce_make_default_gid(struct net_device *dev, union ib_gid *gid)
  76{
  77        memset(gid, 0, sizeof(*gid));
  78        gid->raw[0] = 0xFE;
  79        gid->raw[1] = 0x80;
  80        hns_roce_addrconf_ifid_eui48(&gid->raw[8], 0xffff, dev);
  81}
  82
  83/**
  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
  88 * Description:
  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],
  92 *              And so on
  93 */
  94int hns_get_gid_index(struct hns_roce_dev *hr_dev, u8 port, int gid_index)
  95{
  96        return gid_index * hr_dev->caps.num_ports + port;
  97}
  98
  99static int hns_roce_set_gid(struct hns_roce_dev *hr_dev, u8 port, int gid_index,
 100                     union ib_gid *gid)
 101{
 102        struct device *dev = &hr_dev->pdev->dev;
 103        u8 gid_idx = 0;
 104
 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);
 108                return -EINVAL;
 109        }
 110
 111        gid_idx = hns_get_gid_index(hr_dev, port, gid_index);
 112
 113        if (!memcmp(gid, &hr_dev->iboe.gid_table[gid_idx], sizeof(*gid)))
 114                return -EINVAL;
 115
 116        memcpy(&hr_dev->iboe.gid_table[gid_idx], gid, sizeof(*gid));
 117
 118        hr_dev->hw->set_gid(hr_dev, port, gid_index, gid);
 119
 120        return 0;
 121}
 122
 123static void hns_roce_set_mac(struct hns_roce_dev *hr_dev, u8 port, u8 *addr)
 124{
 125        u8 phy_port;
 126        u32 i = 0;
 127
 128        if (!memcmp(hr_dev->dev_addr[port], addr, MAC_ADDR_OCTET_NUM))
 129                return;
 130
 131        for (i = 0; i < MAC_ADDR_OCTET_NUM; i++)
 132                hr_dev->dev_addr[port][i] = addr[i];
 133
 134        phy_port = hr_dev->iboe.phy_port[port];
 135        hr_dev->hw->set_mac(hr_dev, phy_port, addr);
 136}
 137
 138static void hns_roce_set_mtu(struct hns_roce_dev *hr_dev, u8 port, int mtu)
 139{
 140        u8 phy_port = hr_dev->iboe.phy_port[port];
 141        enum ib_mtu tmp;
 142
 143        tmp = iboe_get_mtu(mtu);
 144        if (!tmp)
 145                tmp = IB_MTU_256;
 146
 147        hr_dev->hw->set_mtu(hr_dev, phy_port, tmp);
 148}
 149
 150static void hns_roce_update_gids(struct hns_roce_dev *hr_dev, int port)
 151{
 152        struct ib_event event;
 153
 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);
 159}
 160
 161static int handle_en_event(struct hns_roce_dev *hr_dev, u8 port,
 162                           unsigned long event)
 163{
 164        struct device *dev = &hr_dev->pdev->dev;
 165        struct net_device *netdev;
 166        unsigned long flags;
 167        union ib_gid gid;
 168        int ret = 0;
 169
 170        netdev = hr_dev->iboe.netdevs[port];
 171        if (!netdev) {
 172                dev_err(dev, "port(%d) can't find netdev\n", port);
 173                return -ENODEV;
 174        }
 175
 176        spin_lock_irqsave(&hr_dev->iboe.lock, flags);
 177
 178        switch (event) {
 179        case NETDEV_UP:
 180        case NETDEV_CHANGE:
 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);
 186                if (!ret)
 187                        hns_roce_update_gids(hr_dev, port);
 188                break;
 189        case NETDEV_DOWN:
 190                /*
 191                * In v1 engine, only support all ports closed together.
 192                */
 193                break;
 194        default:
 195                dev_dbg(dev, "NETDEV event = 0x%x!\n", (u32)(event));
 196                break;
 197        }
 198
 199        spin_unlock_irqrestore(&hr_dev->iboe.lock, flags);
 200        return ret;
 201}
 202
 203static int hns_roce_netdev_event(struct notifier_block *self,
 204                                 unsigned long event, void *ptr)
 205{
 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;
 209        u8 port = 0;
 210        int ret = 0;
 211
 212        hr_dev = container_of(self, struct hns_roce_dev, iboe.nb);
 213        iboe = &hr_dev->iboe;
 214
 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);
 218                        if (ret)
 219                                return NOTIFY_DONE;
 220                        break;
 221                }
 222        }
 223
 224        return NOTIFY_DONE;
 225}
 226
 227static void hns_roce_addr_event(int event, struct net_device *event_netdev,
 228                                struct hns_roce_dev *hr_dev, union ib_gid *gid)
 229{
 230        struct hns_roce_ib_iboe *iboe = NULL;
 231        int gid_table_len = 0;
 232        unsigned long flags;
 233        union ib_gid zgid;
 234        u8 gid_idx = 0;
 235        u8 port = 0;
 236        int i = 0;
 237        int free;
 238        struct net_device *real_dev = rdma_vlan_dev_real_dev(event_netdev) ?
 239                                      rdma_vlan_dev_real_dev(event_netdev) :
 240                                      event_netdev;
 241
 242        if (event != NETDEV_UP && event != NETDEV_DOWN)
 243                return;
 244
 245        iboe = &hr_dev->iboe;
 246        while (port < hr_dev->caps.num_ports) {
 247                if (real_dev == iboe->netdevs[port])
 248                        break;
 249                port++;
 250        }
 251
 252        if (port >= hr_dev->caps.num_ports) {
 253                dev_dbg(&hr_dev->pdev->dev, "can't find netdev\n");
 254                return;
 255        }
 256
 257        memset(zgid.raw, 0, sizeof(zgid.raw));
 258        free = -1;
 259        gid_table_len = hr_dev->caps.gid_table_len[port];
 260
 261        spin_lock_irqsave(&hr_dev->iboe.lock, flags);
 262
 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,
 266                            sizeof(gid->raw)))
 267                        break;
 268                if (free < 0 && !memcmp(zgid.raw,
 269                        iboe->gid_table[gid_idx].raw, sizeof(zgid.raw)))
 270                        free = i;
 271        }
 272
 273        if (i >= gid_table_len) {
 274                if (free < 0) {
 275                        spin_unlock_irqrestore(&hr_dev->iboe.lock, flags);
 276                        dev_dbg(&hr_dev->pdev->dev,
 277                                "gid_index overflow, port(%d)\n", port);
 278                        return;
 279                }
 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);
 285        }
 286
 287        spin_unlock_irqrestore(&hr_dev->iboe.lock, flags);
 288}
 289
 290static int hns_roce_inet_event(struct notifier_block *self, unsigned long event,
 291                               void *ptr)
 292{
 293        struct in_ifaddr *ifa = ptr;
 294        struct hns_roce_dev *hr_dev;
 295        struct net_device *dev = ifa->ifa_dev->dev;
 296        union ib_gid gid;
 297
 298        ipv6_addr_set_v4mapped(ifa->ifa_address, (struct in6_addr *)&gid);
 299
 300        hr_dev = container_of(self, struct hns_roce_dev, iboe.nb_inet);
 301
 302        hns_roce_addr_event(event, dev, hr_dev, &gid);
 303
 304        return NOTIFY_DONE;
 305}
 306
 307static int hns_roce_setup_mtu_gids(struct hns_roce_dev *hr_dev)
 308{
 309        struct in_ifaddr *ifa_list = NULL;
 310        union ib_gid gid = {{0} };
 311        u32 ipaddr = 0;
 312        int index = 0;
 313        int ret = 0;
 314        u8 i = 0;
 315
 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);
 320
 321                if (hr_dev->iboe.netdevs[i]->ip_ptr) {
 322                        ifa_list = hr_dev->iboe.netdevs[i]->ip_ptr->ifa_list;
 323                        index = 1;
 324                        while (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);
 329                                if (ret)
 330                                        break;
 331                                index++;
 332                                ifa_list = ifa_list->ifa_next;
 333                        }
 334                        hns_roce_update_gids(hr_dev, i);
 335                }
 336        }
 337
 338        return ret;
 339}
 340
 341static int hns_roce_query_device(struct ib_device *ib_dev,
 342                                 struct ib_device_attr *props,
 343                                 struct ib_udata *uhw)
 344{
 345        struct hns_roce_dev *hr_dev = to_hr_dev(ib_dev);
 346
 347        memset(props, 0, sizeof(*props));
 348
 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        props->max_sge = hr_dev->caps.max_sq_sg;
 360        props->max_sge_rd = 1;
 361        props->max_cq = hr_dev->caps.num_cqs;
 362        props->max_cqe = hr_dev->caps.max_cqes;
 363        props->max_mr = hr_dev->caps.num_mtpts;
 364        props->max_pd = hr_dev->caps.num_pds;
 365        props->max_qp_rd_atom = hr_dev->caps.max_qp_dest_rdma;
 366        props->max_qp_init_rd_atom = hr_dev->caps.max_qp_init_rdma;
 367        props->atomic_cap = IB_ATOMIC_NONE;
 368        props->max_pkeys = 1;
 369        props->local_ca_ack_delay = hr_dev->caps.local_ca_ack_delay;
 370
 371        return 0;
 372}
 373
 374static struct net_device *hns_roce_get_netdev(struct ib_device *ib_dev,
 375                                              u8 port_num)
 376{
 377        struct hns_roce_dev *hr_dev = to_hr_dev(ib_dev);
 378        struct net_device *ndev;
 379
 380        if (port_num < 1 || port_num > hr_dev->caps.num_ports)
 381                return NULL;
 382
 383        rcu_read_lock();
 384
 385        ndev = hr_dev->iboe.netdevs[port_num - 1];
 386        if (ndev)
 387                dev_hold(ndev);
 388
 389        rcu_read_unlock();
 390        return ndev;
 391}
 392
 393static int hns_roce_query_port(struct ib_device *ib_dev, u8 port_num,
 394                               struct ib_port_attr *props)
 395{
 396        struct hns_roce_dev *hr_dev = to_hr_dev(ib_dev);
 397        struct device *dev = &hr_dev->pdev->dev;
 398        struct net_device *net_dev;
 399        unsigned long flags;
 400        enum ib_mtu mtu;
 401        u8 port;
 402
 403        assert(port_num > 0);
 404        port = port_num - 1;
 405
 406        memset(props, 0, sizeof(*props));
 407
 408        props->max_mtu = hr_dev->caps.max_mtu;
 409        props->gid_tbl_len = hr_dev->caps.gid_table_len[port];
 410        props->port_cap_flags = IB_PORT_CM_SUP | IB_PORT_REINIT_SUP |
 411                                IB_PORT_VENDOR_CLASS_SUP |
 412                                IB_PORT_BOOT_MGMT_SUP;
 413        props->max_msg_sz = HNS_ROCE_MAX_MSG_LEN;
 414        props->pkey_tbl_len = 1;
 415        props->active_width = IB_WIDTH_4X;
 416        props->active_speed = 1;
 417
 418        spin_lock_irqsave(&hr_dev->iboe.lock, flags);
 419
 420        net_dev = hr_dev->iboe.netdevs[port];
 421        if (!net_dev) {
 422                spin_unlock_irqrestore(&hr_dev->iboe.lock, flags);
 423                dev_err(dev, "find netdev %d failed!\r\n", port);
 424                return -EINVAL;
 425        }
 426
 427        mtu = iboe_get_mtu(net_dev->mtu);
 428        props->active_mtu = mtu ? min(props->max_mtu, mtu) : IB_MTU_256;
 429        props->state = (netif_running(net_dev) && netif_carrier_ok(net_dev)) ?
 430                        IB_PORT_ACTIVE : IB_PORT_DOWN;
 431        props->phys_state = (props->state == IB_PORT_ACTIVE) ? 5 : 3;
 432
 433        spin_unlock_irqrestore(&hr_dev->iboe.lock, flags);
 434
 435        return 0;
 436}
 437
 438static enum rdma_link_layer hns_roce_get_link_layer(struct ib_device *device,
 439                                                    u8 port_num)
 440{
 441        return IB_LINK_LAYER_ETHERNET;
 442}
 443
 444static int hns_roce_query_gid(struct ib_device *ib_dev, u8 port_num, int index,
 445                              union ib_gid *gid)
 446{
 447        struct hns_roce_dev *hr_dev = to_hr_dev(ib_dev);
 448        struct device *dev = &hr_dev->pdev->dev;
 449        u8 gid_idx = 0;
 450        u8 port;
 451
 452        if (port_num < 1 || port_num > hr_dev->caps.num_ports ||
 453            index >= hr_dev->caps.gid_table_len[port_num - 1]) {
 454                dev_err(dev,
 455                        "port_num %d index %d illegal! correct range: port_num 1~%d index 0~%d!\n",
 456                        port_num, index, hr_dev->caps.num_ports,
 457                        hr_dev->caps.gid_table_len[port_num - 1] - 1);
 458                return -EINVAL;
 459        }
 460
 461        port = port_num - 1;
 462        gid_idx = hns_get_gid_index(hr_dev, port, index);
 463        if (gid_idx >= HNS_ROCE_MAX_GID_NUM) {
 464                dev_err(dev, "port_num %d index %d illegal! total gid num %d!\n",
 465                        port_num, index, HNS_ROCE_MAX_GID_NUM);
 466                return -EINVAL;
 467        }
 468
 469        memcpy(gid->raw, hr_dev->iboe.gid_table[gid_idx].raw,
 470               HNS_ROCE_GID_SIZE);
 471
 472        return 0;
 473}
 474
 475static int hns_roce_query_pkey(struct ib_device *ib_dev, u8 port, u16 index,
 476                               u16 *pkey)
 477{
 478        *pkey = PKEY_ID;
 479
 480        return 0;
 481}
 482
 483static int hns_roce_modify_device(struct ib_device *ib_dev, int mask,
 484                                  struct ib_device_modify *props)
 485{
 486        unsigned long flags;
 487
 488        if (mask & ~IB_DEVICE_MODIFY_NODE_DESC)
 489                return -EOPNOTSUPP;
 490
 491        if (mask & IB_DEVICE_MODIFY_NODE_DESC) {
 492                spin_lock_irqsave(&to_hr_dev(ib_dev)->sm_lock, flags);
 493                memcpy(ib_dev->node_desc, props->node_desc, NODE_DESC_SIZE);
 494                spin_unlock_irqrestore(&to_hr_dev(ib_dev)->sm_lock, flags);
 495        }
 496
 497        return 0;
 498}
 499
 500static int hns_roce_modify_port(struct ib_device *ib_dev, u8 port_num, int mask,
 501                                struct ib_port_modify *props)
 502{
 503        return 0;
 504}
 505
 506static struct ib_ucontext *hns_roce_alloc_ucontext(struct ib_device *ib_dev,
 507                                                   struct ib_udata *udata)
 508{
 509        int ret = 0;
 510        struct hns_roce_ucontext *context;
 511        struct hns_roce_ib_alloc_ucontext_resp resp;
 512        struct hns_roce_dev *hr_dev = to_hr_dev(ib_dev);
 513
 514        resp.qp_tab_size = hr_dev->caps.num_qps;
 515
 516        context = kmalloc(sizeof(*context), GFP_KERNEL);
 517        if (!context)
 518                return ERR_PTR(-ENOMEM);
 519
 520        ret = hns_roce_uar_alloc(hr_dev, &context->uar);
 521        if (ret)
 522                goto error_fail_uar_alloc;
 523
 524        ret = ib_copy_to_udata(udata, &resp, sizeof(resp));
 525        if (ret)
 526                goto error_fail_copy_to_udata;
 527
 528        return &context->ibucontext;
 529
 530error_fail_copy_to_udata:
 531        hns_roce_uar_free(hr_dev, &context->uar);
 532
 533error_fail_uar_alloc:
 534        kfree(context);
 535
 536        return ERR_PTR(ret);
 537}
 538
 539static int hns_roce_dealloc_ucontext(struct ib_ucontext *ibcontext)
 540{
 541        struct hns_roce_ucontext *context = to_hr_ucontext(ibcontext);
 542
 543        hns_roce_uar_free(to_hr_dev(ibcontext->device), &context->uar);
 544        kfree(context);
 545
 546        return 0;
 547}
 548
 549static int hns_roce_mmap(struct ib_ucontext *context,
 550                         struct vm_area_struct *vma)
 551{
 552        if (((vma->vm_end - vma->vm_start) % PAGE_SIZE) != 0)
 553                return -EINVAL;
 554
 555        if (vma->vm_pgoff == 0) {
 556                vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
 557                if (io_remap_pfn_range(vma, vma->vm_start,
 558                                       to_hr_ucontext(context)->uar.pfn,
 559                                       PAGE_SIZE, vma->vm_page_prot))
 560                        return -EAGAIN;
 561
 562        } else {
 563                return -EINVAL;
 564        }
 565
 566        return 0;
 567}
 568
 569static int hns_roce_port_immutable(struct ib_device *ib_dev, u8 port_num,
 570                                   struct ib_port_immutable *immutable)
 571{
 572        struct ib_port_attr attr;
 573        int ret;
 574
 575        ret = hns_roce_query_port(ib_dev, port_num, &attr);
 576        if (ret)
 577                return ret;
 578
 579        immutable->pkey_tbl_len = attr.pkey_tbl_len;
 580        immutable->gid_tbl_len = attr.gid_tbl_len;
 581
 582        immutable->core_cap_flags = RDMA_CORE_PORT_IBA_ROCE;
 583        immutable->max_mad_size = IB_MGMT_MAD_SIZE;
 584
 585        return 0;
 586}
 587
 588static void hns_roce_unregister_device(struct hns_roce_dev *hr_dev)
 589{
 590        struct hns_roce_ib_iboe *iboe = &hr_dev->iboe;
 591
 592        unregister_inetaddr_notifier(&iboe->nb_inet);
 593        unregister_netdevice_notifier(&iboe->nb);
 594        ib_unregister_device(&hr_dev->ib_dev);
 595}
 596
 597static int hns_roce_register_device(struct hns_roce_dev *hr_dev)
 598{
 599        int ret;
 600        struct hns_roce_ib_iboe *iboe = NULL;
 601        struct ib_device *ib_dev = NULL;
 602        struct device *dev = &hr_dev->pdev->dev;
 603
 604        iboe = &hr_dev->iboe;
 605        spin_lock_init(&iboe->lock);
 606
 607        ib_dev = &hr_dev->ib_dev;
 608        strlcpy(ib_dev->name, "hisi_%d", IB_DEVICE_NAME_MAX);
 609
 610        ib_dev->owner                   = THIS_MODULE;
 611        ib_dev->node_type               = RDMA_NODE_IB_CA;
 612        ib_dev->dma_device              = dev;
 613
 614        ib_dev->phys_port_cnt           = hr_dev->caps.num_ports;
 615        ib_dev->local_dma_lkey          = hr_dev->caps.reserved_lkey;
 616        ib_dev->num_comp_vectors        = hr_dev->caps.num_comp_vectors;
 617        ib_dev->uverbs_abi_ver          = 1;
 618        ib_dev->uverbs_cmd_mask         =
 619                (1ULL << IB_USER_VERBS_CMD_GET_CONTEXT) |
 620                (1ULL << IB_USER_VERBS_CMD_QUERY_DEVICE) |
 621                (1ULL << IB_USER_VERBS_CMD_QUERY_PORT) |
 622                (1ULL << IB_USER_VERBS_CMD_ALLOC_PD) |
 623                (1ULL << IB_USER_VERBS_CMD_DEALLOC_PD) |
 624                (1ULL << IB_USER_VERBS_CMD_REG_MR) |
 625                (1ULL << IB_USER_VERBS_CMD_DEREG_MR) |
 626                (1ULL << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL) |
 627                (1ULL << IB_USER_VERBS_CMD_CREATE_CQ) |
 628                (1ULL << IB_USER_VERBS_CMD_DESTROY_CQ) |
 629                (1ULL << IB_USER_VERBS_CMD_CREATE_QP) |
 630                (1ULL << IB_USER_VERBS_CMD_MODIFY_QP) |
 631                (1ULL << IB_USER_VERBS_CMD_QUERY_QP) |
 632                (1ULL << IB_USER_VERBS_CMD_DESTROY_QP);
 633
 634        /* HCA||device||port */
 635        ib_dev->modify_device           = hns_roce_modify_device;
 636        ib_dev->query_device            = hns_roce_query_device;
 637        ib_dev->query_port              = hns_roce_query_port;
 638        ib_dev->modify_port             = hns_roce_modify_port;
 639        ib_dev->get_link_layer          = hns_roce_get_link_layer;
 640        ib_dev->get_netdev              = hns_roce_get_netdev;
 641        ib_dev->query_gid               = hns_roce_query_gid;
 642        ib_dev->query_pkey              = hns_roce_query_pkey;
 643        ib_dev->alloc_ucontext          = hns_roce_alloc_ucontext;
 644        ib_dev->dealloc_ucontext        = hns_roce_dealloc_ucontext;
 645        ib_dev->mmap                    = hns_roce_mmap;
 646
 647        /* PD */
 648        ib_dev->alloc_pd                = hns_roce_alloc_pd;
 649        ib_dev->dealloc_pd              = hns_roce_dealloc_pd;
 650
 651        /* AH */
 652        ib_dev->create_ah               = hns_roce_create_ah;
 653        ib_dev->query_ah                = hns_roce_query_ah;
 654        ib_dev->destroy_ah              = hns_roce_destroy_ah;
 655
 656        /* QP */
 657        ib_dev->create_qp               = hns_roce_create_qp;
 658        ib_dev->modify_qp               = hns_roce_modify_qp;
 659        ib_dev->query_qp                = hr_dev->hw->query_qp;
 660        ib_dev->destroy_qp              = hr_dev->hw->destroy_qp;
 661        ib_dev->post_send               = hr_dev->hw->post_send;
 662        ib_dev->post_recv               = hr_dev->hw->post_recv;
 663
 664        /* CQ */
 665        ib_dev->create_cq               = hns_roce_ib_create_cq;
 666        ib_dev->destroy_cq              = hns_roce_ib_destroy_cq;
 667        ib_dev->req_notify_cq           = hr_dev->hw->req_notify_cq;
 668        ib_dev->poll_cq                 = hr_dev->hw->poll_cq;
 669
 670        /* MR */
 671        ib_dev->get_dma_mr              = hns_roce_get_dma_mr;
 672        ib_dev->reg_user_mr             = hns_roce_reg_user_mr;
 673        ib_dev->dereg_mr                = hns_roce_dereg_mr;
 674
 675        /* OTHERS */
 676        ib_dev->get_port_immutable      = hns_roce_port_immutable;
 677
 678        ret = ib_register_device(ib_dev, NULL);
 679        if (ret) {
 680                dev_err(dev, "ib_register_device failed!\n");
 681                return ret;
 682        }
 683
 684        ret = hns_roce_setup_mtu_gids(hr_dev);
 685        if (ret) {
 686                dev_err(dev, "roce_setup_mtu_gids failed!\n");
 687                goto error_failed_setup_mtu_gids;
 688        }
 689
 690        iboe->nb.notifier_call = hns_roce_netdev_event;
 691        ret = register_netdevice_notifier(&iboe->nb);
 692        if (ret) {
 693                dev_err(dev, "register_netdevice_notifier failed!\n");
 694                goto error_failed_setup_mtu_gids;
 695        }
 696
 697        iboe->nb_inet.notifier_call = hns_roce_inet_event;
 698        ret = register_inetaddr_notifier(&iboe->nb_inet);
 699        if (ret) {
 700                dev_err(dev, "register inet addr notifier failed!\n");
 701                goto error_failed_register_inetaddr_notifier;
 702        }
 703
 704        return 0;
 705
 706error_failed_register_inetaddr_notifier:
 707        unregister_netdevice_notifier(&iboe->nb);
 708
 709error_failed_setup_mtu_gids:
 710        ib_unregister_device(ib_dev);
 711
 712        return ret;
 713}
 714
 715static const struct of_device_id hns_roce_of_match[] = {
 716        { .compatible = "hisilicon,hns-roce-v1", .data = &hns_roce_hw_v1, },
 717        {},
 718};
 719MODULE_DEVICE_TABLE(of, hns_roce_of_match);
 720
 721static const struct acpi_device_id hns_roce_acpi_match[] = {
 722        { "HISI00D1", (kernel_ulong_t)&hns_roce_hw_v1 },
 723        {},
 724};
 725MODULE_DEVICE_TABLE(acpi, hns_roce_acpi_match);
 726
 727static int hns_roce_node_match(struct device *dev, void *fwnode)
 728{
 729        return dev->fwnode == fwnode;
 730}
 731
 732static struct
 733platform_device *hns_roce_find_pdev(struct fwnode_handle *fwnode)
 734{
 735        struct device *dev;
 736
 737        /* get the 'device'corresponding to matching 'fwnode' */
 738        dev = bus_find_device(&platform_bus_type, NULL,
 739                              fwnode, hns_roce_node_match);
 740        /* get the platform device */
 741        return dev ? to_platform_device(dev) : NULL;
 742}
 743
 744static int hns_roce_get_cfg(struct hns_roce_dev *hr_dev)
 745{
 746        int i;
 747        int ret;
 748        u8 phy_port;
 749        int port_cnt = 0;
 750        struct device *dev = &hr_dev->pdev->dev;
 751        struct device_node *net_node;
 752        struct net_device *netdev = NULL;
 753        struct platform_device *pdev = NULL;
 754        struct resource *res;
 755
 756        /* check if we are compatible with the underlying SoC */
 757        if (dev_of_node(dev)) {
 758                const struct of_device_id *of_id;
 759
 760                of_id = of_match_node(hns_roce_of_match, dev->of_node);
 761                if (!of_id) {
 762                        dev_err(dev, "device is not compatible!\n");
 763                        return -ENXIO;
 764                }
 765                hr_dev->hw = (struct hns_roce_hw *)of_id->data;
 766                if (!hr_dev->hw) {
 767                        dev_err(dev, "couldn't get H/W specific DT data!\n");
 768                        return -ENXIO;
 769                }
 770        } else if (is_acpi_device_node(dev->fwnode)) {
 771                const struct acpi_device_id *acpi_id;
 772
 773                acpi_id = acpi_match_device(hns_roce_acpi_match, dev);
 774                if (!acpi_id) {
 775                        dev_err(dev, "device is not compatible!\n");
 776                        return -ENXIO;
 777                }
 778                hr_dev->hw = (struct hns_roce_hw *) acpi_id->driver_data;
 779                if (!hr_dev->hw) {
 780                        dev_err(dev, "couldn't get H/W specific ACPI data!\n");
 781                        return -ENXIO;
 782                }
 783        } else {
 784                dev_err(dev, "can't read compatibility data from DT or ACPI\n");
 785                return -ENXIO;
 786        }
 787
 788        /* get the mapped register base address */
 789        res = platform_get_resource(hr_dev->pdev, IORESOURCE_MEM, 0);
 790        if (!res) {
 791                dev_err(dev, "memory resource not found!\n");
 792                return -EINVAL;
 793        }
 794        hr_dev->reg_base = devm_ioremap_resource(dev, res);
 795        if (IS_ERR(hr_dev->reg_base))
 796                return PTR_ERR(hr_dev->reg_base);
 797
 798        /* read the node_guid of IB device from the DT or ACPI */
 799        ret = device_property_read_u8_array(dev, "node-guid",
 800                                            (u8 *)&hr_dev->ib_dev.node_guid,
 801                                            GUID_LEN);
 802        if (ret) {
 803                dev_err(dev, "couldn't get node_guid from DT or ACPI!\n");
 804                return ret;
 805        }
 806
 807        /* get the RoCE associated ethernet ports or netdevices */
 808        for (i = 0; i < HNS_ROCE_MAX_PORTS; i++) {
 809                if (dev_of_node(dev)) {
 810                        net_node = of_parse_phandle(dev->of_node, "eth-handle",
 811                                                    i);
 812                        if (!net_node)
 813                                continue;
 814                        pdev = of_find_device_by_node(net_node);
 815                } else if (is_acpi_device_node(dev->fwnode)) {
 816                        struct acpi_reference_args args;
 817                        struct fwnode_handle *fwnode;
 818
 819                        ret = acpi_node_get_property_reference(dev->fwnode,
 820                                                               "eth-handle",
 821                                                               i, &args);
 822                        if (ret)
 823                                continue;
 824                        fwnode = acpi_fwnode_handle(args.adev);
 825                        pdev = hns_roce_find_pdev(fwnode);
 826                } else {
 827                        dev_err(dev, "cannot read data from DT or ACPI\n");
 828                        return -ENXIO;
 829                }
 830
 831                if (pdev) {
 832                        netdev = platform_get_drvdata(pdev);
 833                        phy_port = (u8)i;
 834                        if (netdev) {
 835                                hr_dev->iboe.netdevs[port_cnt] = netdev;
 836                                hr_dev->iboe.phy_port[port_cnt] = phy_port;
 837                        } else {
 838                                dev_err(dev, "no netdev found with pdev %s\n",
 839                                        pdev->name);
 840                                return -ENODEV;
 841                        }
 842                        port_cnt++;
 843                }
 844        }
 845
 846        if (port_cnt == 0) {
 847                dev_err(dev, "unable to get eth-handle for available ports!\n");
 848                return -EINVAL;
 849        }
 850
 851        hr_dev->caps.num_ports = port_cnt;
 852
 853        /* cmd issue mode: 0 is poll, 1 is event */
 854        hr_dev->cmd_mod = 1;
 855        hr_dev->loop_idc = 0;
 856
 857        /* read the interrupt names from the DT or ACPI */
 858        ret = device_property_read_string_array(dev, "interrupt-names",
 859                                                hr_dev->irq_names,
 860                                                HNS_ROCE_MAX_IRQ_NUM);
 861        if (ret < 0) {
 862                dev_err(dev, "couldn't get interrupt names from DT or ACPI!\n");
 863                return ret;
 864        }
 865
 866        /* fetch the interrupt numbers */
 867        for (i = 0; i < HNS_ROCE_MAX_IRQ_NUM; i++) {
 868                hr_dev->irq[i] = platform_get_irq(hr_dev->pdev, i);
 869                if (hr_dev->irq[i] <= 0) {
 870                        dev_err(dev, "platform get of irq[=%d] failed!\n", i);
 871                        return -EINVAL;
 872                }
 873        }
 874
 875        return 0;
 876}
 877
 878static int hns_roce_init_hem(struct hns_roce_dev *hr_dev)
 879{
 880        int ret;
 881        struct device *dev = &hr_dev->pdev->dev;
 882
 883        ret = hns_roce_init_hem_table(hr_dev, &hr_dev->mr_table.mtt_table,
 884                                      HEM_TYPE_MTT, hr_dev->caps.mtt_entry_sz,
 885                                      hr_dev->caps.num_mtt_segs, 1);
 886        if (ret) {
 887                dev_err(dev, "Failed to init MTT context memory, aborting.\n");
 888                return ret;
 889        }
 890
 891        ret = hns_roce_init_hem_table(hr_dev, &hr_dev->mr_table.mtpt_table,
 892                                      HEM_TYPE_MTPT, hr_dev->caps.mtpt_entry_sz,
 893                                      hr_dev->caps.num_mtpts, 1);
 894        if (ret) {
 895                dev_err(dev, "Failed to init MTPT context memory, aborting.\n");
 896                goto err_unmap_mtt;
 897        }
 898
 899        ret = hns_roce_init_hem_table(hr_dev, &hr_dev->qp_table.qp_table,
 900                                      HEM_TYPE_QPC, hr_dev->caps.qpc_entry_sz,
 901                                      hr_dev->caps.num_qps, 1);
 902        if (ret) {
 903                dev_err(dev, "Failed to init QP context memory, aborting.\n");
 904                goto err_unmap_dmpt;
 905        }
 906
 907        ret = hns_roce_init_hem_table(hr_dev, &hr_dev->qp_table.irrl_table,
 908                                      HEM_TYPE_IRRL,
 909                                      hr_dev->caps.irrl_entry_sz *
 910                                      hr_dev->caps.max_qp_init_rdma,
 911                                      hr_dev->caps.num_qps, 1);
 912        if (ret) {
 913                dev_err(dev, "Failed to init irrl_table memory, aborting.\n");
 914                goto err_unmap_qp;
 915        }
 916
 917        ret = hns_roce_init_hem_table(hr_dev, &hr_dev->cq_table.table,
 918                                      HEM_TYPE_CQC, hr_dev->caps.cqc_entry_sz,
 919                                      hr_dev->caps.num_cqs, 1);
 920        if (ret) {
 921                dev_err(dev, "Failed to init CQ context memory, aborting.\n");
 922                goto err_unmap_irrl;
 923        }
 924
 925        return 0;
 926
 927err_unmap_irrl:
 928        hns_roce_cleanup_hem_table(hr_dev, &hr_dev->qp_table.irrl_table);
 929
 930err_unmap_qp:
 931        hns_roce_cleanup_hem_table(hr_dev, &hr_dev->qp_table.qp_table);
 932
 933err_unmap_dmpt:
 934        hns_roce_cleanup_hem_table(hr_dev, &hr_dev->mr_table.mtpt_table);
 935
 936err_unmap_mtt:
 937        hns_roce_cleanup_hem_table(hr_dev, &hr_dev->mr_table.mtt_table);
 938
 939        return ret;
 940}
 941
 942/**
 943* hns_roce_setup_hca - setup host channel adapter
 944* @hr_dev: pointer to hns roce device
 945* Return : int
 946*/
 947static int hns_roce_setup_hca(struct hns_roce_dev *hr_dev)
 948{
 949        int ret;
 950        struct device *dev = &hr_dev->pdev->dev;
 951
 952        spin_lock_init(&hr_dev->sm_lock);
 953        spin_lock_init(&hr_dev->bt_cmd_lock);
 954
 955        ret = hns_roce_init_uar_table(hr_dev);
 956        if (ret) {
 957                dev_err(dev, "Failed to initialize uar table. aborting\n");
 958                return ret;
 959        }
 960
 961        ret = hns_roce_uar_alloc(hr_dev, &hr_dev->priv_uar);
 962        if (ret) {
 963                dev_err(dev, "Failed to allocate priv_uar.\n");
 964                goto err_uar_table_free;
 965        }
 966
 967        ret = hns_roce_init_pd_table(hr_dev);
 968        if (ret) {
 969                dev_err(dev, "Failed to init protected domain table.\n");
 970                goto err_uar_alloc_free;
 971        }
 972
 973        ret = hns_roce_init_mr_table(hr_dev);
 974        if (ret) {
 975                dev_err(dev, "Failed to init memory region table.\n");
 976                goto err_pd_table_free;
 977        }
 978
 979        ret = hns_roce_init_cq_table(hr_dev);
 980        if (ret) {
 981                dev_err(dev, "Failed to init completion queue table.\n");
 982                goto err_mr_table_free;
 983        }
 984
 985        ret = hns_roce_init_qp_table(hr_dev);
 986        if (ret) {
 987                dev_err(dev, "Failed to init queue pair table.\n");
 988                goto err_cq_table_free;
 989        }
 990
 991        return 0;
 992
 993err_cq_table_free:
 994        hns_roce_cleanup_cq_table(hr_dev);
 995
 996err_mr_table_free:
 997        hns_roce_cleanup_mr_table(hr_dev);
 998
 999err_pd_table_free:
1000        hns_roce_cleanup_pd_table(hr_dev);
1001
1002err_uar_alloc_free:
1003        hns_roce_uar_free(hr_dev, &hr_dev->priv_uar);
1004
1005err_uar_table_free:
1006        hns_roce_cleanup_uar_table(hr_dev);
1007        return ret;
1008}
1009
1010/**
1011* hns_roce_probe - RoCE driver entrance
1012* @pdev: pointer to platform device
1013* Return : int
1014*
1015*/
1016static int hns_roce_probe(struct platform_device *pdev)
1017{
1018        int ret;
1019        struct hns_roce_dev *hr_dev;
1020        struct device *dev = &pdev->dev;
1021
1022        hr_dev = (struct hns_roce_dev *)ib_alloc_device(sizeof(*hr_dev));
1023        if (!hr_dev)
1024                return -ENOMEM;
1025
1026        memset((u8 *)hr_dev + sizeof(struct ib_device), 0,
1027                sizeof(struct hns_roce_dev) - sizeof(struct ib_device));
1028
1029        hr_dev->pdev = pdev;
1030        platform_set_drvdata(pdev, hr_dev);
1031
1032        if (dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64ULL)) &&
1033            dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32ULL))) {
1034                dev_err(dev, "Not usable DMA addressing mode\n");
1035                ret = -EIO;
1036                goto error_failed_get_cfg;
1037        }
1038
1039        ret = hns_roce_get_cfg(hr_dev);
1040        if (ret) {
1041                dev_err(dev, "Get Configuration failed!\n");
1042                goto error_failed_get_cfg;
1043        }
1044
1045        ret = hr_dev->hw->reset(hr_dev, true);
1046        if (ret) {
1047                dev_err(dev, "Reset RoCE engine failed!\n");
1048                goto error_failed_get_cfg;
1049        }
1050
1051        hr_dev->hw->hw_profile(hr_dev);
1052
1053        ret = hns_roce_cmd_init(hr_dev);
1054        if (ret) {
1055                dev_err(dev, "cmd init failed!\n");
1056                goto error_failed_cmd_init;
1057        }
1058
1059        ret = hns_roce_init_eq_table(hr_dev);
1060        if (ret) {
1061                dev_err(dev, "eq init failed!\n");
1062                goto error_failed_eq_table;
1063        }
1064
1065        if (hr_dev->cmd_mod) {
1066                ret = hns_roce_cmd_use_events(hr_dev);
1067                if (ret) {
1068                        dev_err(dev, "Switch to event-driven cmd failed!\n");
1069                        goto error_failed_use_event;
1070                }
1071        }
1072
1073        ret = hns_roce_init_hem(hr_dev);
1074        if (ret) {
1075                dev_err(dev, "init HEM(Hardware Entry Memory) failed!\n");
1076                goto error_failed_init_hem;
1077        }
1078
1079        ret = hns_roce_setup_hca(hr_dev);
1080        if (ret) {
1081                dev_err(dev, "setup hca failed!\n");
1082                goto error_failed_setup_hca;
1083        }
1084
1085        ret = hr_dev->hw->hw_init(hr_dev);
1086        if (ret) {
1087                dev_err(dev, "hw_init failed!\n");
1088                goto error_failed_engine_init;
1089        }
1090
1091        ret = hns_roce_register_device(hr_dev);
1092        if (ret)
1093                goto error_failed_register_device;
1094
1095        return 0;
1096
1097error_failed_register_device:
1098        hr_dev->hw->hw_exit(hr_dev);
1099
1100error_failed_engine_init:
1101        hns_roce_cleanup_bitmap(hr_dev);
1102
1103error_failed_setup_hca:
1104        hns_roce_cleanup_hem(hr_dev);
1105
1106error_failed_init_hem:
1107        if (hr_dev->cmd_mod)
1108                hns_roce_cmd_use_polling(hr_dev);
1109
1110error_failed_use_event:
1111        hns_roce_cleanup_eq_table(hr_dev);
1112
1113error_failed_eq_table:
1114        hns_roce_cmd_cleanup(hr_dev);
1115
1116error_failed_cmd_init:
1117        ret = hr_dev->hw->reset(hr_dev, false);
1118        if (ret)
1119                dev_err(&hr_dev->pdev->dev, "roce_engine reset fail\n");
1120
1121error_failed_get_cfg:
1122        ib_dealloc_device(&hr_dev->ib_dev);
1123
1124        return ret;
1125}
1126
1127/**
1128* hns_roce_remove - remove RoCE device
1129* @pdev: pointer to platform device
1130*/
1131static int hns_roce_remove(struct platform_device *pdev)
1132{
1133        struct hns_roce_dev *hr_dev = platform_get_drvdata(pdev);
1134
1135        hns_roce_unregister_device(hr_dev);
1136        hr_dev->hw->hw_exit(hr_dev);
1137        hns_roce_cleanup_bitmap(hr_dev);
1138        hns_roce_cleanup_hem(hr_dev);
1139
1140        if (hr_dev->cmd_mod)
1141                hns_roce_cmd_use_polling(hr_dev);
1142
1143        hns_roce_cleanup_eq_table(hr_dev);
1144        hns_roce_cmd_cleanup(hr_dev);
1145        hr_dev->hw->reset(hr_dev, false);
1146
1147        ib_dealloc_device(&hr_dev->ib_dev);
1148
1149        return 0;
1150}
1151
1152static struct platform_driver hns_roce_driver = {
1153        .probe = hns_roce_probe,
1154        .remove = hns_roce_remove,
1155        .driver = {
1156                .name = DRV_NAME,
1157                .of_match_table = hns_roce_of_match,
1158                .acpi_match_table = ACPI_PTR(hns_roce_acpi_match),
1159        },
1160};
1161
1162module_platform_driver(hns_roce_driver);
1163
1164MODULE_LICENSE("Dual BSD/GPL");
1165MODULE_AUTHOR("Wei Hu <xavier.huwei@huawei.com>");
1166MODULE_AUTHOR("Nenglong Zhao <zhaonenglong@hisilicon.com>");
1167MODULE_AUTHOR("Lijun Ou <oulijun@huawei.com>");
1168MODULE_DESCRIPTION("HNS RoCE Driver");
1169