oulijun | 9a44353 | 2016-07-21 19:06:38 +0800 | [diff] [blame] | 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 | */ |
Salil | 528f1de | 2016-08-24 04:44:50 +0800 | [diff] [blame] | 33 | #include <linux/acpi.h> |
oulijun | 9a44353 | 2016-07-21 19:06:38 +0800 | [diff] [blame] | 34 | #include <linux/of_platform.h> |
Arnd Bergmann | 3ecc16c | 2017-02-17 15:38:26 +0100 | [diff] [blame] | 35 | #include <linux/module.h> |
oulijun | 9a44353 | 2016-07-21 19:06:38 +0800 | [diff] [blame] | 36 | #include <rdma/ib_addr.h> |
| 37 | #include <rdma/ib_smi.h> |
| 38 | #include <rdma/ib_user_verbs.h> |
Shaobo Xu | 8254746 | 2016-11-23 19:41:08 +0000 | [diff] [blame] | 39 | #include <rdma/ib_cache.h> |
oulijun | 9a44353 | 2016-07-21 19:06:38 +0800 | [diff] [blame] | 40 | #include "hns_roce_common.h" |
| 41 | #include "hns_roce_device.h" |
Leon Romanovsky | 4d40995 | 2016-10-19 20:13:07 +0300 | [diff] [blame] | 42 | #include <rdma/hns-abi.h> |
oulijun | 9a44353 | 2016-07-21 19:06:38 +0800 | [diff] [blame] | 43 | #include "hns_roce_hem.h" |
| 44 | |
| 45 | /** |
oulijun | 9a44353 | 2016-07-21 19:06:38 +0800 | [diff] [blame] | 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 |
| 50 | * Description: |
| 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], |
| 54 | * And so on |
| 55 | */ |
| 56 | int hns_get_gid_index(struct hns_roce_dev *hr_dev, u8 port, int gid_index) |
| 57 | { |
| 58 | return gid_index * hr_dev->caps.num_ports + port; |
| 59 | } |
Wei Hu(Xavier) | 08805fd | 2017-08-30 17:22:59 +0800 | [diff] [blame] | 60 | EXPORT_SYMBOL_GPL(hns_get_gid_index); |
oulijun | 9a44353 | 2016-07-21 19:06:38 +0800 | [diff] [blame] | 61 | |
Wei Hu(Xavier) | a74dc41 | 2017-09-29 23:10:09 +0800 | [diff] [blame] | 62 | static int hns_roce_set_mac(struct hns_roce_dev *hr_dev, u8 port, u8 *addr) |
oulijun | 9a44353 | 2016-07-21 19:06:38 +0800 | [diff] [blame] | 63 | { |
| 64 | u8 phy_port; |
| 65 | u32 i = 0; |
| 66 | |
| 67 | if (!memcmp(hr_dev->dev_addr[port], addr, MAC_ADDR_OCTET_NUM)) |
Wei Hu(Xavier) | a74dc41 | 2017-09-29 23:10:09 +0800 | [diff] [blame] | 68 | return 0; |
oulijun | 9a44353 | 2016-07-21 19:06:38 +0800 | [diff] [blame] | 69 | |
| 70 | for (i = 0; i < MAC_ADDR_OCTET_NUM; i++) |
| 71 | hr_dev->dev_addr[port][i] = addr[i]; |
| 72 | |
| 73 | phy_port = hr_dev->iboe.phy_port[port]; |
Wei Hu(Xavier) | a74dc41 | 2017-09-29 23:10:09 +0800 | [diff] [blame] | 74 | return hr_dev->hw->set_mac(hr_dev, phy_port, addr); |
oulijun | 9a44353 | 2016-07-21 19:06:38 +0800 | [diff] [blame] | 75 | } |
| 76 | |
Shaobo Xu | 8254746 | 2016-11-23 19:41:08 +0000 | [diff] [blame] | 77 | static int hns_roce_add_gid(struct ib_device *device, u8 port_num, |
| 78 | unsigned int index, const union ib_gid *gid, |
| 79 | const struct ib_gid_attr *attr, void **context) |
oulijun | 9a44353 | 2016-07-21 19:06:38 +0800 | [diff] [blame] | 80 | { |
Shaobo Xu | 8254746 | 2016-11-23 19:41:08 +0000 | [diff] [blame] | 81 | struct hns_roce_dev *hr_dev = to_hr_dev(device); |
| 82 | u8 port = port_num - 1; |
| 83 | unsigned long flags; |
Wei Hu(Xavier) | b5ff0f6 | 2017-10-26 17:10:25 +0800 | [diff] [blame] | 84 | int ret; |
oulijun | 9a44353 | 2016-07-21 19:06:38 +0800 | [diff] [blame] | 85 | |
Shaobo Xu | 8254746 | 2016-11-23 19:41:08 +0000 | [diff] [blame] | 86 | if (port >= hr_dev->caps.num_ports) |
| 87 | return -EINVAL; |
| 88 | |
| 89 | spin_lock_irqsave(&hr_dev->iboe.lock, flags); |
| 90 | |
Wei Hu(Xavier) | b5ff0f6 | 2017-10-26 17:10:25 +0800 | [diff] [blame] | 91 | ret = hr_dev->hw->set_gid(hr_dev, port, index, (union ib_gid *)gid, |
| 92 | attr); |
Shaobo Xu | 8254746 | 2016-11-23 19:41:08 +0000 | [diff] [blame] | 93 | |
| 94 | spin_unlock_irqrestore(&hr_dev->iboe.lock, flags); |
| 95 | |
Wei Hu(Xavier) | b5ff0f6 | 2017-10-26 17:10:25 +0800 | [diff] [blame] | 96 | return ret; |
Shaobo Xu | 8254746 | 2016-11-23 19:41:08 +0000 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | static int hns_roce_del_gid(struct ib_device *device, u8 port_num, |
| 100 | unsigned int index, void **context) |
| 101 | { |
| 102 | struct hns_roce_dev *hr_dev = to_hr_dev(device); |
oulijun | 173bc6b | 2018-02-07 17:49:28 +0800 | [diff] [blame] | 103 | struct ib_gid_attr zattr = { }; |
Shaobo Xu | 8254746 | 2016-11-23 19:41:08 +0000 | [diff] [blame] | 104 | union ib_gid zgid = { {0} }; |
| 105 | u8 port = port_num - 1; |
| 106 | unsigned long flags; |
Wei Hu(Xavier) | b5ff0f6 | 2017-10-26 17:10:25 +0800 | [diff] [blame] | 107 | int ret; |
Shaobo Xu | 8254746 | 2016-11-23 19:41:08 +0000 | [diff] [blame] | 108 | |
| 109 | if (port >= hr_dev->caps.num_ports) |
| 110 | return -EINVAL; |
| 111 | |
| 112 | spin_lock_irqsave(&hr_dev->iboe.lock, flags); |
| 113 | |
oulijun | 173bc6b | 2018-02-07 17:49:28 +0800 | [diff] [blame] | 114 | ret = hr_dev->hw->set_gid(hr_dev, port, index, &zgid, &zattr); |
Shaobo Xu | 8254746 | 2016-11-23 19:41:08 +0000 | [diff] [blame] | 115 | |
| 116 | spin_unlock_irqrestore(&hr_dev->iboe.lock, flags); |
| 117 | |
Wei Hu(Xavier) | b5ff0f6 | 2017-10-26 17:10:25 +0800 | [diff] [blame] | 118 | return ret; |
oulijun | 9a44353 | 2016-07-21 19:06:38 +0800 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | static int handle_en_event(struct hns_roce_dev *hr_dev, u8 port, |
| 122 | unsigned long event) |
| 123 | { |
Wei Hu(Xavier) | 13ca970 | 2017-08-30 17:23:02 +0800 | [diff] [blame] | 124 | struct device *dev = hr_dev->dev; |
oulijun | 9a44353 | 2016-07-21 19:06:38 +0800 | [diff] [blame] | 125 | struct net_device *netdev; |
Wei Hu(Xavier) | a74dc41 | 2017-09-29 23:10:09 +0800 | [diff] [blame] | 126 | int ret = 0; |
oulijun | 9a44353 | 2016-07-21 19:06:38 +0800 | [diff] [blame] | 127 | |
| 128 | netdev = hr_dev->iboe.netdevs[port]; |
| 129 | if (!netdev) { |
| 130 | dev_err(dev, "port(%d) can't find netdev\n", port); |
| 131 | return -ENODEV; |
| 132 | } |
| 133 | |
oulijun | 9a44353 | 2016-07-21 19:06:38 +0800 | [diff] [blame] | 134 | switch (event) { |
| 135 | case NETDEV_UP: |
| 136 | case NETDEV_CHANGE: |
| 137 | case NETDEV_REGISTER: |
| 138 | case NETDEV_CHANGEADDR: |
Wei Hu(Xavier) | a74dc41 | 2017-09-29 23:10:09 +0800 | [diff] [blame] | 139 | ret = hns_roce_set_mac(hr_dev, port, netdev->dev_addr); |
oulijun | 9a44353 | 2016-07-21 19:06:38 +0800 | [diff] [blame] | 140 | break; |
| 141 | case NETDEV_DOWN: |
| 142 | /* |
Salil | e84e40be | 2016-11-23 19:41:09 +0000 | [diff] [blame] | 143 | * In v1 engine, only support all ports closed together. |
| 144 | */ |
oulijun | 9a44353 | 2016-07-21 19:06:38 +0800 | [diff] [blame] | 145 | break; |
| 146 | default: |
| 147 | dev_dbg(dev, "NETDEV event = 0x%x!\n", (u32)(event)); |
| 148 | break; |
| 149 | } |
| 150 | |
Wei Hu(Xavier) | a74dc41 | 2017-09-29 23:10:09 +0800 | [diff] [blame] | 151 | return ret; |
oulijun | 9a44353 | 2016-07-21 19:06:38 +0800 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | static int hns_roce_netdev_event(struct notifier_block *self, |
| 155 | unsigned long event, void *ptr) |
| 156 | { |
| 157 | struct net_device *dev = netdev_notifier_info_to_dev(ptr); |
| 158 | struct hns_roce_ib_iboe *iboe = NULL; |
| 159 | struct hns_roce_dev *hr_dev = NULL; |
| 160 | u8 port = 0; |
| 161 | int ret = 0; |
| 162 | |
| 163 | hr_dev = container_of(self, struct hns_roce_dev, iboe.nb); |
| 164 | iboe = &hr_dev->iboe; |
| 165 | |
| 166 | for (port = 0; port < hr_dev->caps.num_ports; port++) { |
| 167 | if (dev == iboe->netdevs[port]) { |
| 168 | ret = handle_en_event(hr_dev, port, event); |
| 169 | if (ret) |
| 170 | return NOTIFY_DONE; |
| 171 | break; |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | return NOTIFY_DONE; |
| 176 | } |
| 177 | |
Shaobo Xu | 8254746 | 2016-11-23 19:41:08 +0000 | [diff] [blame] | 178 | static int hns_roce_setup_mtu_mac(struct hns_roce_dev *hr_dev) |
oulijun | 9a44353 | 2016-07-21 19:06:38 +0800 | [diff] [blame] | 179 | { |
Wei Hu(Xavier) | a74dc41 | 2017-09-29 23:10:09 +0800 | [diff] [blame] | 180 | int ret; |
Shaobo Xu | 8254746 | 2016-11-23 19:41:08 +0000 | [diff] [blame] | 181 | u8 i; |
oulijun | 9a44353 | 2016-07-21 19:06:38 +0800 | [diff] [blame] | 182 | |
| 183 | for (i = 0; i < hr_dev->caps.num_ports; i++) { |
Wei Hu(Xavier) | 08805fd | 2017-08-30 17:22:59 +0800 | [diff] [blame] | 184 | if (hr_dev->hw->set_mtu) |
| 185 | hr_dev->hw->set_mtu(hr_dev, hr_dev->iboe.phy_port[i], |
| 186 | hr_dev->caps.max_mtu); |
Wei Hu(Xavier) | a74dc41 | 2017-09-29 23:10:09 +0800 | [diff] [blame] | 187 | ret = hns_roce_set_mac(hr_dev, i, |
| 188 | hr_dev->iboe.netdevs[i]->dev_addr); |
| 189 | if (ret) |
| 190 | return ret; |
oulijun | 9a44353 | 2016-07-21 19:06:38 +0800 | [diff] [blame] | 191 | } |
| 192 | |
Shaobo Xu | 8254746 | 2016-11-23 19:41:08 +0000 | [diff] [blame] | 193 | return 0; |
oulijun | 9a44353 | 2016-07-21 19:06:38 +0800 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | static int hns_roce_query_device(struct ib_device *ib_dev, |
| 197 | struct ib_device_attr *props, |
| 198 | struct ib_udata *uhw) |
| 199 | { |
| 200 | struct hns_roce_dev *hr_dev = to_hr_dev(ib_dev); |
| 201 | |
| 202 | memset(props, 0, sizeof(*props)); |
| 203 | |
oulijun | 8b9b8d1 | 2018-02-05 21:14:00 +0800 | [diff] [blame] | 204 | props->sys_image_guid = cpu_to_be32(hr_dev->sys_image_guid); |
oulijun | 9a44353 | 2016-07-21 19:06:38 +0800 | [diff] [blame] | 205 | props->max_mr_size = (u64)(~(0ULL)); |
| 206 | props->page_size_cap = hr_dev->caps.page_size_cap; |
| 207 | props->vendor_id = hr_dev->vendor_id; |
| 208 | props->vendor_part_id = hr_dev->vendor_part_id; |
| 209 | props->hw_ver = hr_dev->hw_rev; |
| 210 | props->max_qp = hr_dev->caps.num_qps; |
| 211 | props->max_qp_wr = hr_dev->caps.max_wqes; |
| 212 | props->device_cap_flags = IB_DEVICE_PORT_ACTIVE_EVENT | |
Lijun Ou | a74aab6 | 2016-09-15 23:48:08 +0100 | [diff] [blame] | 213 | IB_DEVICE_RC_RNR_NAK_GEN; |
Wei Hu(Xavier) | cfc85f3 | 2017-08-30 17:23:04 +0800 | [diff] [blame] | 214 | props->max_sge = max(hr_dev->caps.max_sq_sg, hr_dev->caps.max_rq_sg); |
oulijun | 9a44353 | 2016-07-21 19:06:38 +0800 | [diff] [blame] | 215 | props->max_sge_rd = 1; |
| 216 | props->max_cq = hr_dev->caps.num_cqs; |
| 217 | props->max_cqe = hr_dev->caps.max_cqes; |
| 218 | props->max_mr = hr_dev->caps.num_mtpts; |
| 219 | props->max_pd = hr_dev->caps.num_pds; |
| 220 | props->max_qp_rd_atom = hr_dev->caps.max_qp_dest_rdma; |
| 221 | props->max_qp_init_rd_atom = hr_dev->caps.max_qp_init_rdma; |
| 222 | props->atomic_cap = IB_ATOMIC_NONE; |
| 223 | props->max_pkeys = 1; |
| 224 | props->local_ca_ack_delay = hr_dev->caps.local_ca_ack_delay; |
| 225 | |
| 226 | return 0; |
| 227 | } |
| 228 | |
Lijun Ou | 2eefca2 | 2016-09-15 23:48:06 +0100 | [diff] [blame] | 229 | static struct net_device *hns_roce_get_netdev(struct ib_device *ib_dev, |
| 230 | u8 port_num) |
| 231 | { |
| 232 | struct hns_roce_dev *hr_dev = to_hr_dev(ib_dev); |
| 233 | struct net_device *ndev; |
| 234 | |
| 235 | if (port_num < 1 || port_num > hr_dev->caps.num_ports) |
| 236 | return NULL; |
| 237 | |
| 238 | rcu_read_lock(); |
| 239 | |
| 240 | ndev = hr_dev->iboe.netdevs[port_num - 1]; |
| 241 | if (ndev) |
| 242 | dev_hold(ndev); |
| 243 | |
| 244 | rcu_read_unlock(); |
| 245 | return ndev; |
| 246 | } |
| 247 | |
oulijun | 9a44353 | 2016-07-21 19:06:38 +0800 | [diff] [blame] | 248 | static int hns_roce_query_port(struct ib_device *ib_dev, u8 port_num, |
| 249 | struct ib_port_attr *props) |
| 250 | { |
| 251 | struct hns_roce_dev *hr_dev = to_hr_dev(ib_dev); |
Wei Hu(Xavier) | 13ca970 | 2017-08-30 17:23:02 +0800 | [diff] [blame] | 252 | struct device *dev = hr_dev->dev; |
oulijun | 9a44353 | 2016-07-21 19:06:38 +0800 | [diff] [blame] | 253 | struct net_device *net_dev; |
| 254 | unsigned long flags; |
| 255 | enum ib_mtu mtu; |
| 256 | u8 port; |
| 257 | |
| 258 | assert(port_num > 0); |
| 259 | port = port_num - 1; |
| 260 | |
Or Gerlitz | c4550c6 | 2017-01-24 13:02:39 +0200 | [diff] [blame] | 261 | /* props being zeroed by the caller, avoid zeroing it here */ |
oulijun | 9a44353 | 2016-07-21 19:06:38 +0800 | [diff] [blame] | 262 | |
| 263 | props->max_mtu = hr_dev->caps.max_mtu; |
| 264 | props->gid_tbl_len = hr_dev->caps.gid_table_len[port]; |
| 265 | props->port_cap_flags = IB_PORT_CM_SUP | IB_PORT_REINIT_SUP | |
| 266 | IB_PORT_VENDOR_CLASS_SUP | |
| 267 | IB_PORT_BOOT_MGMT_SUP; |
| 268 | props->max_msg_sz = HNS_ROCE_MAX_MSG_LEN; |
| 269 | props->pkey_tbl_len = 1; |
| 270 | props->active_width = IB_WIDTH_4X; |
| 271 | props->active_speed = 1; |
| 272 | |
| 273 | spin_lock_irqsave(&hr_dev->iboe.lock, flags); |
| 274 | |
| 275 | net_dev = hr_dev->iboe.netdevs[port]; |
| 276 | if (!net_dev) { |
| 277 | spin_unlock_irqrestore(&hr_dev->iboe.lock, flags); |
| 278 | dev_err(dev, "find netdev %d failed!\r\n", port); |
| 279 | return -EINVAL; |
| 280 | } |
| 281 | |
| 282 | mtu = iboe_get_mtu(net_dev->mtu); |
| 283 | props->active_mtu = mtu ? min(props->max_mtu, mtu) : IB_MTU_256; |
| 284 | props->state = (netif_running(net_dev) && netif_carrier_ok(net_dev)) ? |
| 285 | IB_PORT_ACTIVE : IB_PORT_DOWN; |
| 286 | props->phys_state = (props->state == IB_PORT_ACTIVE) ? 5 : 3; |
| 287 | |
| 288 | spin_unlock_irqrestore(&hr_dev->iboe.lock, flags); |
| 289 | |
| 290 | return 0; |
| 291 | } |
| 292 | |
| 293 | static enum rdma_link_layer hns_roce_get_link_layer(struct ib_device *device, |
| 294 | u8 port_num) |
| 295 | { |
| 296 | return IB_LINK_LAYER_ETHERNET; |
| 297 | } |
| 298 | |
| 299 | static int hns_roce_query_gid(struct ib_device *ib_dev, u8 port_num, int index, |
| 300 | union ib_gid *gid) |
| 301 | { |
oulijun | 9a44353 | 2016-07-21 19:06:38 +0800 | [diff] [blame] | 302 | return 0; |
| 303 | } |
| 304 | |
| 305 | static int hns_roce_query_pkey(struct ib_device *ib_dev, u8 port, u16 index, |
| 306 | u16 *pkey) |
| 307 | { |
| 308 | *pkey = PKEY_ID; |
| 309 | |
| 310 | return 0; |
| 311 | } |
| 312 | |
| 313 | static int hns_roce_modify_device(struct ib_device *ib_dev, int mask, |
| 314 | struct ib_device_modify *props) |
| 315 | { |
| 316 | unsigned long flags; |
| 317 | |
| 318 | if (mask & ~IB_DEVICE_MODIFY_NODE_DESC) |
| 319 | return -EOPNOTSUPP; |
| 320 | |
| 321 | if (mask & IB_DEVICE_MODIFY_NODE_DESC) { |
| 322 | spin_lock_irqsave(&to_hr_dev(ib_dev)->sm_lock, flags); |
| 323 | memcpy(ib_dev->node_desc, props->node_desc, NODE_DESC_SIZE); |
| 324 | spin_unlock_irqrestore(&to_hr_dev(ib_dev)->sm_lock, flags); |
| 325 | } |
| 326 | |
| 327 | return 0; |
| 328 | } |
| 329 | |
| 330 | static int hns_roce_modify_port(struct ib_device *ib_dev, u8 port_num, int mask, |
| 331 | struct ib_port_modify *props) |
| 332 | { |
| 333 | return 0; |
| 334 | } |
| 335 | |
| 336 | static struct ib_ucontext *hns_roce_alloc_ucontext(struct ib_device *ib_dev, |
| 337 | struct ib_udata *udata) |
| 338 | { |
| 339 | int ret = 0; |
| 340 | struct hns_roce_ucontext *context; |
| 341 | struct hns_roce_ib_alloc_ucontext_resp resp; |
| 342 | struct hns_roce_dev *hr_dev = to_hr_dev(ib_dev); |
| 343 | |
| 344 | resp.qp_tab_size = hr_dev->caps.num_qps; |
| 345 | |
| 346 | context = kmalloc(sizeof(*context), GFP_KERNEL); |
| 347 | if (!context) |
| 348 | return ERR_PTR(-ENOMEM); |
| 349 | |
| 350 | ret = hns_roce_uar_alloc(hr_dev, &context->uar); |
| 351 | if (ret) |
| 352 | goto error_fail_uar_alloc; |
| 353 | |
Yixian Liu | e088a68 | 2018-03-09 18:36:29 +0800 | [diff] [blame^] | 354 | if (hr_dev->caps.flags & HNS_ROCE_CAP_FLAG_RECORD_DB) { |
| 355 | INIT_LIST_HEAD(&context->page_list); |
| 356 | mutex_init(&context->page_mutex); |
| 357 | } |
| 358 | |
oulijun | 9a44353 | 2016-07-21 19:06:38 +0800 | [diff] [blame] | 359 | ret = ib_copy_to_udata(udata, &resp, sizeof(resp)); |
| 360 | if (ret) |
| 361 | goto error_fail_copy_to_udata; |
| 362 | |
| 363 | return &context->ibucontext; |
| 364 | |
| 365 | error_fail_copy_to_udata: |
| 366 | hns_roce_uar_free(hr_dev, &context->uar); |
| 367 | |
| 368 | error_fail_uar_alloc: |
| 369 | kfree(context); |
| 370 | |
| 371 | return ERR_PTR(ret); |
| 372 | } |
| 373 | |
| 374 | static int hns_roce_dealloc_ucontext(struct ib_ucontext *ibcontext) |
| 375 | { |
| 376 | struct hns_roce_ucontext *context = to_hr_ucontext(ibcontext); |
| 377 | |
| 378 | hns_roce_uar_free(to_hr_dev(ibcontext->device), &context->uar); |
| 379 | kfree(context); |
| 380 | |
| 381 | return 0; |
| 382 | } |
| 383 | |
| 384 | static int hns_roce_mmap(struct ib_ucontext *context, |
| 385 | struct vm_area_struct *vma) |
| 386 | { |
Wei Hu (Xavier) | 8f3e9f3 | 2016-11-23 19:41:00 +0000 | [diff] [blame] | 387 | struct hns_roce_dev *hr_dev = to_hr_dev(context->device); |
| 388 | |
oulijun | 9a44353 | 2016-07-21 19:06:38 +0800 | [diff] [blame] | 389 | if (((vma->vm_end - vma->vm_start) % PAGE_SIZE) != 0) |
| 390 | return -EINVAL; |
| 391 | |
| 392 | if (vma->vm_pgoff == 0) { |
| 393 | vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot); |
| 394 | if (io_remap_pfn_range(vma, vma->vm_start, |
| 395 | to_hr_ucontext(context)->uar.pfn, |
| 396 | PAGE_SIZE, vma->vm_page_prot)) |
| 397 | return -EAGAIN; |
Wei Hu(Xavier) | 5caad67 | 2017-08-30 17:23:17 +0800 | [diff] [blame] | 398 | } else if (vma->vm_pgoff == 1 && hr_dev->tptr_dma_addr && |
| 399 | hr_dev->tptr_size) { |
Wei Hu (Xavier) | 8f3e9f3 | 2016-11-23 19:41:00 +0000 | [diff] [blame] | 400 | /* vm_pgoff: 1 -- TPTR */ |
| 401 | if (io_remap_pfn_range(vma, vma->vm_start, |
| 402 | hr_dev->tptr_dma_addr >> PAGE_SHIFT, |
| 403 | hr_dev->tptr_size, |
| 404 | vma->vm_page_prot)) |
| 405 | return -EAGAIN; |
| 406 | } else |
oulijun | 9a44353 | 2016-07-21 19:06:38 +0800 | [diff] [blame] | 407 | return -EINVAL; |
oulijun | 9a44353 | 2016-07-21 19:06:38 +0800 | [diff] [blame] | 408 | |
| 409 | return 0; |
| 410 | } |
| 411 | |
| 412 | static int hns_roce_port_immutable(struct ib_device *ib_dev, u8 port_num, |
| 413 | struct ib_port_immutable *immutable) |
| 414 | { |
| 415 | struct ib_port_attr attr; |
| 416 | int ret; |
| 417 | |
Or Gerlitz | c4550c6 | 2017-01-24 13:02:39 +0200 | [diff] [blame] | 418 | ret = ib_query_port(ib_dev, port_num, &attr); |
oulijun | 9a44353 | 2016-07-21 19:06:38 +0800 | [diff] [blame] | 419 | if (ret) |
| 420 | return ret; |
| 421 | |
| 422 | immutable->pkey_tbl_len = attr.pkey_tbl_len; |
| 423 | immutable->gid_tbl_len = attr.gid_tbl_len; |
| 424 | |
oulijun | 9a44353 | 2016-07-21 19:06:38 +0800 | [diff] [blame] | 425 | immutable->max_mad_size = IB_MGMT_MAD_SIZE; |
Wei Hu(Xavier) | 023c147 | 2017-10-26 17:10:24 +0800 | [diff] [blame] | 426 | immutable->core_cap_flags = RDMA_CORE_PORT_IBA_ROCE; |
| 427 | if (to_hr_dev(ib_dev)->caps.flags & HNS_ROCE_CAP_FLAG_ROCE_V1_V2) |
| 428 | immutable->core_cap_flags |= RDMA_CORE_PORT_IBA_ROCE_UDP_ENCAP; |
oulijun | 9a44353 | 2016-07-21 19:06:38 +0800 | [diff] [blame] | 429 | |
| 430 | return 0; |
| 431 | } |
| 432 | |
| 433 | static void hns_roce_unregister_device(struct hns_roce_dev *hr_dev) |
| 434 | { |
| 435 | struct hns_roce_ib_iboe *iboe = &hr_dev->iboe; |
| 436 | |
oulijun | 9a44353 | 2016-07-21 19:06:38 +0800 | [diff] [blame] | 437 | unregister_netdevice_notifier(&iboe->nb); |
| 438 | ib_unregister_device(&hr_dev->ib_dev); |
| 439 | } |
| 440 | |
| 441 | static int hns_roce_register_device(struct hns_roce_dev *hr_dev) |
| 442 | { |
| 443 | int ret; |
| 444 | struct hns_roce_ib_iboe *iboe = NULL; |
| 445 | struct ib_device *ib_dev = NULL; |
Wei Hu(Xavier) | 13ca970 | 2017-08-30 17:23:02 +0800 | [diff] [blame] | 446 | struct device *dev = hr_dev->dev; |
oulijun | 9a44353 | 2016-07-21 19:06:38 +0800 | [diff] [blame] | 447 | |
| 448 | iboe = &hr_dev->iboe; |
Lijun Ou | 49fdf6b | 2016-09-20 17:07:02 +0100 | [diff] [blame] | 449 | spin_lock_init(&iboe->lock); |
oulijun | 9a44353 | 2016-07-21 19:06:38 +0800 | [diff] [blame] | 450 | |
| 451 | ib_dev = &hr_dev->ib_dev; |
Lijun Ou | 3b5184b | 2016-11-29 23:10:30 +0000 | [diff] [blame] | 452 | strlcpy(ib_dev->name, "hns_%d", IB_DEVICE_NAME_MAX); |
oulijun | 9a44353 | 2016-07-21 19:06:38 +0800 | [diff] [blame] | 453 | |
| 454 | ib_dev->owner = THIS_MODULE; |
| 455 | ib_dev->node_type = RDMA_NODE_IB_CA; |
Bart Van Assche | fecd02e | 2017-01-20 13:04:18 -0800 | [diff] [blame] | 456 | ib_dev->dev.parent = dev; |
oulijun | 9a44353 | 2016-07-21 19:06:38 +0800 | [diff] [blame] | 457 | |
| 458 | ib_dev->phys_port_cnt = hr_dev->caps.num_ports; |
| 459 | ib_dev->local_dma_lkey = hr_dev->caps.reserved_lkey; |
| 460 | ib_dev->num_comp_vectors = hr_dev->caps.num_comp_vectors; |
| 461 | ib_dev->uverbs_abi_ver = 1; |
| 462 | ib_dev->uverbs_cmd_mask = |
| 463 | (1ULL << IB_USER_VERBS_CMD_GET_CONTEXT) | |
| 464 | (1ULL << IB_USER_VERBS_CMD_QUERY_DEVICE) | |
| 465 | (1ULL << IB_USER_VERBS_CMD_QUERY_PORT) | |
| 466 | (1ULL << IB_USER_VERBS_CMD_ALLOC_PD) | |
| 467 | (1ULL << IB_USER_VERBS_CMD_DEALLOC_PD) | |
| 468 | (1ULL << IB_USER_VERBS_CMD_REG_MR) | |
| 469 | (1ULL << IB_USER_VERBS_CMD_DEREG_MR) | |
| 470 | (1ULL << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL) | |
| 471 | (1ULL << IB_USER_VERBS_CMD_CREATE_CQ) | |
| 472 | (1ULL << IB_USER_VERBS_CMD_DESTROY_CQ) | |
| 473 | (1ULL << IB_USER_VERBS_CMD_CREATE_QP) | |
| 474 | (1ULL << IB_USER_VERBS_CMD_MODIFY_QP) | |
| 475 | (1ULL << IB_USER_VERBS_CMD_QUERY_QP) | |
| 476 | (1ULL << IB_USER_VERBS_CMD_DESTROY_QP); |
| 477 | |
| 478 | /* HCA||device||port */ |
| 479 | ib_dev->modify_device = hns_roce_modify_device; |
| 480 | ib_dev->query_device = hns_roce_query_device; |
| 481 | ib_dev->query_port = hns_roce_query_port; |
| 482 | ib_dev->modify_port = hns_roce_modify_port; |
| 483 | ib_dev->get_link_layer = hns_roce_get_link_layer; |
Lijun Ou | 2eefca2 | 2016-09-15 23:48:06 +0100 | [diff] [blame] | 484 | ib_dev->get_netdev = hns_roce_get_netdev; |
oulijun | 9a44353 | 2016-07-21 19:06:38 +0800 | [diff] [blame] | 485 | ib_dev->query_gid = hns_roce_query_gid; |
Shaobo Xu | 8254746 | 2016-11-23 19:41:08 +0000 | [diff] [blame] | 486 | ib_dev->add_gid = hns_roce_add_gid; |
| 487 | ib_dev->del_gid = hns_roce_del_gid; |
oulijun | 9a44353 | 2016-07-21 19:06:38 +0800 | [diff] [blame] | 488 | ib_dev->query_pkey = hns_roce_query_pkey; |
| 489 | ib_dev->alloc_ucontext = hns_roce_alloc_ucontext; |
| 490 | ib_dev->dealloc_ucontext = hns_roce_dealloc_ucontext; |
| 491 | ib_dev->mmap = hns_roce_mmap; |
| 492 | |
| 493 | /* PD */ |
| 494 | ib_dev->alloc_pd = hns_roce_alloc_pd; |
| 495 | ib_dev->dealloc_pd = hns_roce_dealloc_pd; |
| 496 | |
| 497 | /* AH */ |
| 498 | ib_dev->create_ah = hns_roce_create_ah; |
| 499 | ib_dev->query_ah = hns_roce_query_ah; |
| 500 | ib_dev->destroy_ah = hns_roce_destroy_ah; |
| 501 | |
| 502 | /* QP */ |
| 503 | ib_dev->create_qp = hns_roce_create_qp; |
| 504 | ib_dev->modify_qp = hns_roce_modify_qp; |
| 505 | ib_dev->query_qp = hr_dev->hw->query_qp; |
| 506 | ib_dev->destroy_qp = hr_dev->hw->destroy_qp; |
| 507 | ib_dev->post_send = hr_dev->hw->post_send; |
| 508 | ib_dev->post_recv = hr_dev->hw->post_recv; |
| 509 | |
| 510 | /* CQ */ |
| 511 | ib_dev->create_cq = hns_roce_ib_create_cq; |
oulijun | b156269 | 2017-10-19 11:52:40 +0800 | [diff] [blame] | 512 | ib_dev->modify_cq = hr_dev->hw->modify_cq; |
oulijun | 9a44353 | 2016-07-21 19:06:38 +0800 | [diff] [blame] | 513 | ib_dev->destroy_cq = hns_roce_ib_destroy_cq; |
| 514 | ib_dev->req_notify_cq = hr_dev->hw->req_notify_cq; |
| 515 | ib_dev->poll_cq = hr_dev->hw->poll_cq; |
| 516 | |
| 517 | /* MR */ |
| 518 | ib_dev->get_dma_mr = hns_roce_get_dma_mr; |
| 519 | ib_dev->reg_user_mr = hns_roce_reg_user_mr; |
| 520 | ib_dev->dereg_mr = hns_roce_dereg_mr; |
Wei Hu(Xavier) | a2c80b7 | 2017-10-26 17:10:23 +0800 | [diff] [blame] | 521 | if (hr_dev->caps.flags & HNS_ROCE_CAP_FLAG_REREG_MR) { |
| 522 | ib_dev->rereg_user_mr = hns_roce_rereg_user_mr; |
| 523 | ib_dev->uverbs_cmd_mask |= (1ULL << IB_USER_VERBS_CMD_REREG_MR); |
| 524 | } |
oulijun | 9a44353 | 2016-07-21 19:06:38 +0800 | [diff] [blame] | 525 | |
| 526 | /* OTHERS */ |
| 527 | ib_dev->get_port_immutable = hns_roce_port_immutable; |
| 528 | |
| 529 | ret = ib_register_device(ib_dev, NULL); |
| 530 | if (ret) { |
| 531 | dev_err(dev, "ib_register_device failed!\n"); |
| 532 | return ret; |
| 533 | } |
| 534 | |
Shaobo Xu | 8254746 | 2016-11-23 19:41:08 +0000 | [diff] [blame] | 535 | ret = hns_roce_setup_mtu_mac(hr_dev); |
oulijun | 9a44353 | 2016-07-21 19:06:38 +0800 | [diff] [blame] | 536 | if (ret) { |
Shaobo Xu | 8254746 | 2016-11-23 19:41:08 +0000 | [diff] [blame] | 537 | dev_err(dev, "setup_mtu_mac failed!\n"); |
| 538 | goto error_failed_setup_mtu_mac; |
oulijun | 9a44353 | 2016-07-21 19:06:38 +0800 | [diff] [blame] | 539 | } |
| 540 | |
oulijun | 9a44353 | 2016-07-21 19:06:38 +0800 | [diff] [blame] | 541 | iboe->nb.notifier_call = hns_roce_netdev_event; |
| 542 | ret = register_netdevice_notifier(&iboe->nb); |
| 543 | if (ret) { |
| 544 | dev_err(dev, "register_netdevice_notifier failed!\n"); |
Shaobo Xu | 8254746 | 2016-11-23 19:41:08 +0000 | [diff] [blame] | 545 | goto error_failed_setup_mtu_mac; |
oulijun | 9a44353 | 2016-07-21 19:06:38 +0800 | [diff] [blame] | 546 | } |
| 547 | |
| 548 | return 0; |
| 549 | |
Shaobo Xu | 8254746 | 2016-11-23 19:41:08 +0000 | [diff] [blame] | 550 | error_failed_setup_mtu_mac: |
oulijun | 9a44353 | 2016-07-21 19:06:38 +0800 | [diff] [blame] | 551 | ib_unregister_device(ib_dev); |
| 552 | |
| 553 | return ret; |
| 554 | } |
| 555 | |
oulijun | 9a44353 | 2016-07-21 19:06:38 +0800 | [diff] [blame] | 556 | static int hns_roce_init_hem(struct hns_roce_dev *hr_dev) |
| 557 | { |
| 558 | int ret; |
Wei Hu(Xavier) | 13ca970 | 2017-08-30 17:23:02 +0800 | [diff] [blame] | 559 | struct device *dev = hr_dev->dev; |
oulijun | 9a44353 | 2016-07-21 19:06:38 +0800 | [diff] [blame] | 560 | |
| 561 | ret = hns_roce_init_hem_table(hr_dev, &hr_dev->mr_table.mtt_table, |
| 562 | HEM_TYPE_MTT, hr_dev->caps.mtt_entry_sz, |
| 563 | hr_dev->caps.num_mtt_segs, 1); |
| 564 | if (ret) { |
| 565 | dev_err(dev, "Failed to init MTT context memory, aborting.\n"); |
| 566 | return ret; |
| 567 | } |
| 568 | |
Shaobo Xu | 9766edc | 2017-08-30 17:23:09 +0800 | [diff] [blame] | 569 | if (hns_roce_check_whether_mhop(hr_dev, HEM_TYPE_CQE)) { |
| 570 | ret = hns_roce_init_hem_table(hr_dev, |
| 571 | &hr_dev->mr_table.mtt_cqe_table, |
| 572 | HEM_TYPE_CQE, hr_dev->caps.mtt_entry_sz, |
| 573 | hr_dev->caps.num_cqe_segs, 1); |
| 574 | if (ret) { |
| 575 | dev_err(dev, "Failed to init MTT CQE context memory, aborting.\n"); |
| 576 | goto err_unmap_cqe; |
| 577 | } |
| 578 | } |
| 579 | |
oulijun | 9a44353 | 2016-07-21 19:06:38 +0800 | [diff] [blame] | 580 | ret = hns_roce_init_hem_table(hr_dev, &hr_dev->mr_table.mtpt_table, |
| 581 | HEM_TYPE_MTPT, hr_dev->caps.mtpt_entry_sz, |
| 582 | hr_dev->caps.num_mtpts, 1); |
| 583 | if (ret) { |
| 584 | dev_err(dev, "Failed to init MTPT context memory, aborting.\n"); |
| 585 | goto err_unmap_mtt; |
| 586 | } |
| 587 | |
| 588 | ret = hns_roce_init_hem_table(hr_dev, &hr_dev->qp_table.qp_table, |
| 589 | HEM_TYPE_QPC, hr_dev->caps.qpc_entry_sz, |
| 590 | hr_dev->caps.num_qps, 1); |
| 591 | if (ret) { |
| 592 | dev_err(dev, "Failed to init QP context memory, aborting.\n"); |
| 593 | goto err_unmap_dmpt; |
| 594 | } |
| 595 | |
| 596 | ret = hns_roce_init_hem_table(hr_dev, &hr_dev->qp_table.irrl_table, |
| 597 | HEM_TYPE_IRRL, |
| 598 | hr_dev->caps.irrl_entry_sz * |
| 599 | hr_dev->caps.max_qp_init_rdma, |
| 600 | hr_dev->caps.num_qps, 1); |
| 601 | if (ret) { |
| 602 | dev_err(dev, "Failed to init irrl_table memory, aborting.\n"); |
| 603 | goto err_unmap_qp; |
| 604 | } |
| 605 | |
oulijun | e92f2c1 | 2017-11-10 16:55:44 +0800 | [diff] [blame] | 606 | if (hr_dev->caps.trrl_entry_sz) { |
| 607 | ret = hns_roce_init_hem_table(hr_dev, |
| 608 | &hr_dev->qp_table.trrl_table, |
| 609 | HEM_TYPE_TRRL, |
| 610 | hr_dev->caps.trrl_entry_sz * |
| 611 | hr_dev->caps.max_qp_dest_rdma, |
| 612 | hr_dev->caps.num_qps, 1); |
| 613 | if (ret) { |
| 614 | dev_err(dev, |
| 615 | "Failed to init trrl_table memory, aborting.\n"); |
| 616 | goto err_unmap_irrl; |
| 617 | } |
| 618 | } |
| 619 | |
oulijun | 9a44353 | 2016-07-21 19:06:38 +0800 | [diff] [blame] | 620 | ret = hns_roce_init_hem_table(hr_dev, &hr_dev->cq_table.table, |
| 621 | HEM_TYPE_CQC, hr_dev->caps.cqc_entry_sz, |
| 622 | hr_dev->caps.num_cqs, 1); |
| 623 | if (ret) { |
| 624 | dev_err(dev, "Failed to init CQ context memory, aborting.\n"); |
oulijun | e92f2c1 | 2017-11-10 16:55:44 +0800 | [diff] [blame] | 625 | goto err_unmap_trrl; |
oulijun | 9a44353 | 2016-07-21 19:06:38 +0800 | [diff] [blame] | 626 | } |
| 627 | |
| 628 | return 0; |
| 629 | |
oulijun | e92f2c1 | 2017-11-10 16:55:44 +0800 | [diff] [blame] | 630 | err_unmap_trrl: |
| 631 | if (hr_dev->caps.trrl_entry_sz) |
| 632 | hns_roce_cleanup_hem_table(hr_dev, |
| 633 | &hr_dev->qp_table.trrl_table); |
| 634 | |
oulijun | 9a44353 | 2016-07-21 19:06:38 +0800 | [diff] [blame] | 635 | err_unmap_irrl: |
| 636 | hns_roce_cleanup_hem_table(hr_dev, &hr_dev->qp_table.irrl_table); |
| 637 | |
| 638 | err_unmap_qp: |
| 639 | hns_roce_cleanup_hem_table(hr_dev, &hr_dev->qp_table.qp_table); |
| 640 | |
| 641 | err_unmap_dmpt: |
| 642 | hns_roce_cleanup_hem_table(hr_dev, &hr_dev->mr_table.mtpt_table); |
| 643 | |
| 644 | err_unmap_mtt: |
Shaobo Xu | 9766edc | 2017-08-30 17:23:09 +0800 | [diff] [blame] | 645 | if (hns_roce_check_whether_mhop(hr_dev, HEM_TYPE_CQE)) |
| 646 | hns_roce_cleanup_hem_table(hr_dev, |
| 647 | &hr_dev->mr_table.mtt_cqe_table); |
| 648 | |
| 649 | err_unmap_cqe: |
| 650 | hns_roce_cleanup_hem_table(hr_dev, &hr_dev->mr_table.mtt_table); |
oulijun | 9a44353 | 2016-07-21 19:06:38 +0800 | [diff] [blame] | 651 | |
| 652 | return ret; |
| 653 | } |
| 654 | |
| 655 | /** |
Salil | e84e40be | 2016-11-23 19:41:09 +0000 | [diff] [blame] | 656 | * hns_roce_setup_hca - setup host channel adapter |
| 657 | * @hr_dev: pointer to hns roce device |
| 658 | * Return : int |
| 659 | */ |
oulijun | 9a44353 | 2016-07-21 19:06:38 +0800 | [diff] [blame] | 660 | static int hns_roce_setup_hca(struct hns_roce_dev *hr_dev) |
| 661 | { |
| 662 | int ret; |
Wei Hu(Xavier) | 13ca970 | 2017-08-30 17:23:02 +0800 | [diff] [blame] | 663 | struct device *dev = hr_dev->dev; |
oulijun | 9a44353 | 2016-07-21 19:06:38 +0800 | [diff] [blame] | 664 | |
| 665 | spin_lock_init(&hr_dev->sm_lock); |
oulijun | 9a44353 | 2016-07-21 19:06:38 +0800 | [diff] [blame] | 666 | spin_lock_init(&hr_dev->bt_cmd_lock); |
| 667 | |
| 668 | ret = hns_roce_init_uar_table(hr_dev); |
| 669 | if (ret) { |
| 670 | dev_err(dev, "Failed to initialize uar table. aborting\n"); |
| 671 | return ret; |
| 672 | } |
| 673 | |
| 674 | ret = hns_roce_uar_alloc(hr_dev, &hr_dev->priv_uar); |
| 675 | if (ret) { |
| 676 | dev_err(dev, "Failed to allocate priv_uar.\n"); |
| 677 | goto err_uar_table_free; |
| 678 | } |
| 679 | |
| 680 | ret = hns_roce_init_pd_table(hr_dev); |
| 681 | if (ret) { |
| 682 | dev_err(dev, "Failed to init protected domain table.\n"); |
| 683 | goto err_uar_alloc_free; |
| 684 | } |
| 685 | |
| 686 | ret = hns_roce_init_mr_table(hr_dev); |
| 687 | if (ret) { |
| 688 | dev_err(dev, "Failed to init memory region table.\n"); |
| 689 | goto err_pd_table_free; |
| 690 | } |
| 691 | |
| 692 | ret = hns_roce_init_cq_table(hr_dev); |
| 693 | if (ret) { |
| 694 | dev_err(dev, "Failed to init completion queue table.\n"); |
| 695 | goto err_mr_table_free; |
| 696 | } |
| 697 | |
| 698 | ret = hns_roce_init_qp_table(hr_dev); |
| 699 | if (ret) { |
| 700 | dev_err(dev, "Failed to init queue pair table.\n"); |
| 701 | goto err_cq_table_free; |
| 702 | } |
| 703 | |
| 704 | return 0; |
| 705 | |
| 706 | err_cq_table_free: |
| 707 | hns_roce_cleanup_cq_table(hr_dev); |
| 708 | |
| 709 | err_mr_table_free: |
| 710 | hns_roce_cleanup_mr_table(hr_dev); |
| 711 | |
| 712 | err_pd_table_free: |
| 713 | hns_roce_cleanup_pd_table(hr_dev); |
| 714 | |
| 715 | err_uar_alloc_free: |
| 716 | hns_roce_uar_free(hr_dev, &hr_dev->priv_uar); |
| 717 | |
| 718 | err_uar_table_free: |
| 719 | hns_roce_cleanup_uar_table(hr_dev); |
| 720 | return ret; |
| 721 | } |
| 722 | |
Wei Hu(Xavier) | 08805fd | 2017-08-30 17:22:59 +0800 | [diff] [blame] | 723 | int hns_roce_init(struct hns_roce_dev *hr_dev) |
oulijun | 9a44353 | 2016-07-21 19:06:38 +0800 | [diff] [blame] | 724 | { |
| 725 | int ret; |
Wei Hu(Xavier) | 13ca970 | 2017-08-30 17:23:02 +0800 | [diff] [blame] | 726 | struct device *dev = hr_dev->dev; |
oulijun | 9a44353 | 2016-07-21 19:06:38 +0800 | [diff] [blame] | 727 | |
Wei Hu(Xavier) | 08805fd | 2017-08-30 17:22:59 +0800 | [diff] [blame] | 728 | if (hr_dev->hw->reset) { |
| 729 | ret = hr_dev->hw->reset(hr_dev, true); |
| 730 | if (ret) { |
| 731 | dev_err(dev, "Reset RoCE engine failed!\n"); |
| 732 | return ret; |
| 733 | } |
oulijun | 9a44353 | 2016-07-21 19:06:38 +0800 | [diff] [blame] | 734 | } |
| 735 | |
Wei Hu(Xavier) | a04ff73 | 2017-08-30 17:23:03 +0800 | [diff] [blame] | 736 | if (hr_dev->hw->cmq_init) { |
| 737 | ret = hr_dev->hw->cmq_init(hr_dev); |
| 738 | if (ret) { |
| 739 | dev_err(dev, "Init RoCE Command Queue failed!\n"); |
| 740 | goto error_failed_cmq_init; |
| 741 | } |
| 742 | } |
| 743 | |
Wei Hu(Xavier) | cfc85f3 | 2017-08-30 17:23:04 +0800 | [diff] [blame] | 744 | ret = hr_dev->hw->hw_profile(hr_dev); |
| 745 | if (ret) { |
| 746 | dev_err(dev, "Get RoCE engine profile failed!\n"); |
| 747 | goto error_failed_cmd_init; |
| 748 | } |
oulijun | 9a44353 | 2016-07-21 19:06:38 +0800 | [diff] [blame] | 749 | |
| 750 | ret = hns_roce_cmd_init(hr_dev); |
| 751 | if (ret) { |
| 752 | dev_err(dev, "cmd init failed!\n"); |
| 753 | goto error_failed_cmd_init; |
| 754 | } |
| 755 | |
Yixian Liu | b16f818 | 2017-11-14 17:26:16 +0800 | [diff] [blame] | 756 | ret = hr_dev->hw->init_eq(hr_dev); |
| 757 | if (ret) { |
| 758 | dev_err(dev, "eq init failed!\n"); |
| 759 | goto error_failed_eq_table; |
oulijun | 9a44353 | 2016-07-21 19:06:38 +0800 | [diff] [blame] | 760 | } |
| 761 | |
| 762 | if (hr_dev->cmd_mod) { |
| 763 | ret = hns_roce_cmd_use_events(hr_dev); |
| 764 | if (ret) { |
| 765 | dev_err(dev, "Switch to event-driven cmd failed!\n"); |
| 766 | goto error_failed_use_event; |
| 767 | } |
| 768 | } |
| 769 | |
| 770 | ret = hns_roce_init_hem(hr_dev); |
| 771 | if (ret) { |
| 772 | dev_err(dev, "init HEM(Hardware Entry Memory) failed!\n"); |
| 773 | goto error_failed_init_hem; |
| 774 | } |
| 775 | |
| 776 | ret = hns_roce_setup_hca(hr_dev); |
| 777 | if (ret) { |
| 778 | dev_err(dev, "setup hca failed!\n"); |
| 779 | goto error_failed_setup_hca; |
| 780 | } |
| 781 | |
Wei Hu(Xavier) | 08805fd | 2017-08-30 17:22:59 +0800 | [diff] [blame] | 782 | if (hr_dev->hw->hw_init) { |
| 783 | ret = hr_dev->hw->hw_init(hr_dev); |
| 784 | if (ret) { |
| 785 | dev_err(dev, "hw_init failed!\n"); |
| 786 | goto error_failed_engine_init; |
| 787 | } |
oulijun | 9a44353 | 2016-07-21 19:06:38 +0800 | [diff] [blame] | 788 | } |
| 789 | |
| 790 | ret = hns_roce_register_device(hr_dev); |
| 791 | if (ret) |
| 792 | goto error_failed_register_device; |
| 793 | |
| 794 | return 0; |
| 795 | |
| 796 | error_failed_register_device: |
Wei Hu(Xavier) | 08805fd | 2017-08-30 17:22:59 +0800 | [diff] [blame] | 797 | if (hr_dev->hw->hw_exit) |
| 798 | hr_dev->hw->hw_exit(hr_dev); |
oulijun | 9a44353 | 2016-07-21 19:06:38 +0800 | [diff] [blame] | 799 | |
| 800 | error_failed_engine_init: |
| 801 | hns_roce_cleanup_bitmap(hr_dev); |
| 802 | |
| 803 | error_failed_setup_hca: |
| 804 | hns_roce_cleanup_hem(hr_dev); |
| 805 | |
| 806 | error_failed_init_hem: |
| 807 | if (hr_dev->cmd_mod) |
| 808 | hns_roce_cmd_use_polling(hr_dev); |
| 809 | |
| 810 | error_failed_use_event: |
Yixian Liu | b16f818 | 2017-11-14 17:26:16 +0800 | [diff] [blame] | 811 | hr_dev->hw->cleanup_eq(hr_dev); |
oulijun | 9a44353 | 2016-07-21 19:06:38 +0800 | [diff] [blame] | 812 | |
| 813 | error_failed_eq_table: |
| 814 | hns_roce_cmd_cleanup(hr_dev); |
| 815 | |
| 816 | error_failed_cmd_init: |
Wei Hu(Xavier) | a04ff73 | 2017-08-30 17:23:03 +0800 | [diff] [blame] | 817 | if (hr_dev->hw->cmq_exit) |
| 818 | hr_dev->hw->cmq_exit(hr_dev); |
| 819 | |
| 820 | error_failed_cmq_init: |
Wei Hu(Xavier) | 08805fd | 2017-08-30 17:22:59 +0800 | [diff] [blame] | 821 | if (hr_dev->hw->reset) { |
| 822 | ret = hr_dev->hw->reset(hr_dev, false); |
| 823 | if (ret) |
| 824 | dev_err(dev, "Dereset RoCE engine failed!\n"); |
| 825 | } |
oulijun | 9a44353 | 2016-07-21 19:06:38 +0800 | [diff] [blame] | 826 | |
| 827 | return ret; |
| 828 | } |
Wei Hu(Xavier) | 08805fd | 2017-08-30 17:22:59 +0800 | [diff] [blame] | 829 | EXPORT_SYMBOL_GPL(hns_roce_init); |
oulijun | 9a44353 | 2016-07-21 19:06:38 +0800 | [diff] [blame] | 830 | |
Wei Hu(Xavier) | 08805fd | 2017-08-30 17:22:59 +0800 | [diff] [blame] | 831 | void hns_roce_exit(struct hns_roce_dev *hr_dev) |
oulijun | 9a44353 | 2016-07-21 19:06:38 +0800 | [diff] [blame] | 832 | { |
oulijun | 9a44353 | 2016-07-21 19:06:38 +0800 | [diff] [blame] | 833 | hns_roce_unregister_device(hr_dev); |
Wei Hu(Xavier) | 08805fd | 2017-08-30 17:22:59 +0800 | [diff] [blame] | 834 | if (hr_dev->hw->hw_exit) |
| 835 | hr_dev->hw->hw_exit(hr_dev); |
oulijun | 9a44353 | 2016-07-21 19:06:38 +0800 | [diff] [blame] | 836 | hns_roce_cleanup_bitmap(hr_dev); |
| 837 | hns_roce_cleanup_hem(hr_dev); |
| 838 | |
| 839 | if (hr_dev->cmd_mod) |
| 840 | hns_roce_cmd_use_polling(hr_dev); |
| 841 | |
Yixian Liu | b16f818 | 2017-11-14 17:26:16 +0800 | [diff] [blame] | 842 | hr_dev->hw->cleanup_eq(hr_dev); |
oulijun | 9a44353 | 2016-07-21 19:06:38 +0800 | [diff] [blame] | 843 | hns_roce_cmd_cleanup(hr_dev); |
Wei Hu(Xavier) | a04ff73 | 2017-08-30 17:23:03 +0800 | [diff] [blame] | 844 | if (hr_dev->hw->cmq_exit) |
| 845 | hr_dev->hw->cmq_exit(hr_dev); |
Wei Hu(Xavier) | 08805fd | 2017-08-30 17:22:59 +0800 | [diff] [blame] | 846 | if (hr_dev->hw->reset) |
| 847 | hr_dev->hw->reset(hr_dev, false); |
oulijun | 9a44353 | 2016-07-21 19:06:38 +0800 | [diff] [blame] | 848 | } |
Wei Hu(Xavier) | 08805fd | 2017-08-30 17:22:59 +0800 | [diff] [blame] | 849 | EXPORT_SYMBOL_GPL(hns_roce_exit); |
oulijun | 9a44353 | 2016-07-21 19:06:38 +0800 | [diff] [blame] | 850 | |
| 851 | MODULE_LICENSE("Dual BSD/GPL"); |
| 852 | MODULE_AUTHOR("Wei Hu <xavier.huwei@huawei.com>"); |
| 853 | MODULE_AUTHOR("Nenglong Zhao <zhaonenglong@hisilicon.com>"); |
| 854 | MODULE_AUTHOR("Lijun Ou <oulijun@huawei.com>"); |
| 855 | MODULE_DESCRIPTION("HNS RoCE Driver"); |