blob: 795ef97bfcbf3e697b70c9c3f7824fdefc59a8a8 [file] [log] [blame]
oulijun9a443532016-07-21 19:06:38 +08001/*
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 */
Salil528f1de2016-08-24 04:44:50 +080033#include <linux/acpi.h>
oulijun9a443532016-07-21 19:06:38 +080034#include <linux/of_platform.h>
35#include <rdma/ib_addr.h>
36#include <rdma/ib_smi.h>
37#include <rdma/ib_user_verbs.h>
Shaobo Xu82547462016-11-23 19:41:08 +000038#include <rdma/ib_cache.h>
oulijun9a443532016-07-21 19:06:38 +080039#include "hns_roce_common.h"
40#include "hns_roce_device.h"
41#include "hns_roce_user.h"
42#include "hns_roce_hem.h"
43
44/**
oulijun9a443532016-07-21 19:06:38 +080045 * hns_get_gid_index - Get gid index.
46 * @hr_dev: pointer to structure hns_roce_dev.
47 * @port: port, value range: 0 ~ MAX
48 * @gid_index: gid_index, value range: 0 ~ MAX
49 * Description:
50 * N ports shared gids, allocation method as follow:
51 * GID[0][0], GID[1][0],.....GID[N - 1][0],
52 * GID[0][0], GID[1][0],.....GID[N - 1][0],
53 * And so on
54 */
55int hns_get_gid_index(struct hns_roce_dev *hr_dev, u8 port, int gid_index)
56{
57 return gid_index * hr_dev->caps.num_ports + port;
58}
59
oulijun9a443532016-07-21 19:06:38 +080060static void hns_roce_set_mac(struct hns_roce_dev *hr_dev, u8 port, u8 *addr)
61{
62 u8 phy_port;
63 u32 i = 0;
64
65 if (!memcmp(hr_dev->dev_addr[port], addr, MAC_ADDR_OCTET_NUM))
66 return;
67
68 for (i = 0; i < MAC_ADDR_OCTET_NUM; i++)
69 hr_dev->dev_addr[port][i] = addr[i];
70
71 phy_port = hr_dev->iboe.phy_port[port];
72 hr_dev->hw->set_mac(hr_dev, phy_port, addr);
73}
74
75static void hns_roce_set_mtu(struct hns_roce_dev *hr_dev, u8 port, int mtu)
76{
77 u8 phy_port = hr_dev->iboe.phy_port[port];
78 enum ib_mtu tmp;
79
80 tmp = iboe_get_mtu(mtu);
81 if (!tmp)
82 tmp = IB_MTU_256;
83
84 hr_dev->hw->set_mtu(hr_dev, phy_port, tmp);
85}
86
Shaobo Xu82547462016-11-23 19:41:08 +000087static int hns_roce_add_gid(struct ib_device *device, u8 port_num,
88 unsigned int index, const union ib_gid *gid,
89 const struct ib_gid_attr *attr, void **context)
oulijun9a443532016-07-21 19:06:38 +080090{
Shaobo Xu82547462016-11-23 19:41:08 +000091 struct hns_roce_dev *hr_dev = to_hr_dev(device);
92 u8 port = port_num - 1;
93 unsigned long flags;
oulijun9a443532016-07-21 19:06:38 +080094
Shaobo Xu82547462016-11-23 19:41:08 +000095 if (port >= hr_dev->caps.num_ports)
96 return -EINVAL;
97
98 spin_lock_irqsave(&hr_dev->iboe.lock, flags);
99
100 hr_dev->hw->set_gid(hr_dev, port, index, (union ib_gid *)gid);
101
102 spin_unlock_irqrestore(&hr_dev->iboe.lock, flags);
103
104 return 0;
105}
106
107static int hns_roce_del_gid(struct ib_device *device, u8 port_num,
108 unsigned int index, void **context)
109{
110 struct hns_roce_dev *hr_dev = to_hr_dev(device);
111 union ib_gid zgid = { {0} };
112 u8 port = port_num - 1;
113 unsigned long flags;
114
115 if (port >= hr_dev->caps.num_ports)
116 return -EINVAL;
117
118 spin_lock_irqsave(&hr_dev->iboe.lock, flags);
119
120 hr_dev->hw->set_gid(hr_dev, port, index, &zgid);
121
122 spin_unlock_irqrestore(&hr_dev->iboe.lock, flags);
123
124 return 0;
oulijun9a443532016-07-21 19:06:38 +0800125}
126
127static int handle_en_event(struct hns_roce_dev *hr_dev, u8 port,
128 unsigned long event)
129{
130 struct device *dev = &hr_dev->pdev->dev;
131 struct net_device *netdev;
132 unsigned long flags;
oulijun9a443532016-07-21 19:06:38 +0800133
134 netdev = hr_dev->iboe.netdevs[port];
135 if (!netdev) {
136 dev_err(dev, "port(%d) can't find netdev\n", port);
137 return -ENODEV;
138 }
139
140 spin_lock_irqsave(&hr_dev->iboe.lock, flags);
141
142 switch (event) {
143 case NETDEV_UP:
144 case NETDEV_CHANGE:
145 case NETDEV_REGISTER:
146 case NETDEV_CHANGEADDR:
147 hns_roce_set_mac(hr_dev, port, netdev->dev_addr);
oulijun9a443532016-07-21 19:06:38 +0800148 break;
149 case NETDEV_DOWN:
150 /*
151 * In v1 engine, only support all ports closed together.
152 */
153 break;
154 default:
155 dev_dbg(dev, "NETDEV event = 0x%x!\n", (u32)(event));
156 break;
157 }
158
159 spin_unlock_irqrestore(&hr_dev->iboe.lock, flags);
Shaobo Xu82547462016-11-23 19:41:08 +0000160 return 0;
oulijun9a443532016-07-21 19:06:38 +0800161}
162
163static int hns_roce_netdev_event(struct notifier_block *self,
164 unsigned long event, void *ptr)
165{
166 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
167 struct hns_roce_ib_iboe *iboe = NULL;
168 struct hns_roce_dev *hr_dev = NULL;
169 u8 port = 0;
170 int ret = 0;
171
172 hr_dev = container_of(self, struct hns_roce_dev, iboe.nb);
173 iboe = &hr_dev->iboe;
174
175 for (port = 0; port < hr_dev->caps.num_ports; port++) {
176 if (dev == iboe->netdevs[port]) {
177 ret = handle_en_event(hr_dev, port, event);
178 if (ret)
179 return NOTIFY_DONE;
180 break;
181 }
182 }
183
184 return NOTIFY_DONE;
185}
186
Shaobo Xu82547462016-11-23 19:41:08 +0000187static int hns_roce_setup_mtu_mac(struct hns_roce_dev *hr_dev)
oulijun9a443532016-07-21 19:06:38 +0800188{
Shaobo Xu82547462016-11-23 19:41:08 +0000189 u8 i;
oulijun9a443532016-07-21 19:06:38 +0800190
191 for (i = 0; i < hr_dev->caps.num_ports; i++) {
192 hns_roce_set_mtu(hr_dev, i,
193 ib_mtu_enum_to_int(hr_dev->caps.max_mtu));
194 hns_roce_set_mac(hr_dev, i, hr_dev->iboe.netdevs[i]->dev_addr);
oulijun9a443532016-07-21 19:06:38 +0800195 }
196
Shaobo Xu82547462016-11-23 19:41:08 +0000197 return 0;
oulijun9a443532016-07-21 19:06:38 +0800198}
199
200static int hns_roce_query_device(struct ib_device *ib_dev,
201 struct ib_device_attr *props,
202 struct ib_udata *uhw)
203{
204 struct hns_roce_dev *hr_dev = to_hr_dev(ib_dev);
205
206 memset(props, 0, sizeof(*props));
207
208 props->sys_image_guid = hr_dev->sys_image_guid;
209 props->max_mr_size = (u64)(~(0ULL));
210 props->page_size_cap = hr_dev->caps.page_size_cap;
211 props->vendor_id = hr_dev->vendor_id;
212 props->vendor_part_id = hr_dev->vendor_part_id;
213 props->hw_ver = hr_dev->hw_rev;
214 props->max_qp = hr_dev->caps.num_qps;
215 props->max_qp_wr = hr_dev->caps.max_wqes;
216 props->device_cap_flags = IB_DEVICE_PORT_ACTIVE_EVENT |
Lijun Oua74aab62016-09-15 23:48:08 +0100217 IB_DEVICE_RC_RNR_NAK_GEN;
oulijun9a443532016-07-21 19:06:38 +0800218 props->max_sge = hr_dev->caps.max_sq_sg;
219 props->max_sge_rd = 1;
220 props->max_cq = hr_dev->caps.num_cqs;
221 props->max_cqe = hr_dev->caps.max_cqes;
222 props->max_mr = hr_dev->caps.num_mtpts;
223 props->max_pd = hr_dev->caps.num_pds;
224 props->max_qp_rd_atom = hr_dev->caps.max_qp_dest_rdma;
225 props->max_qp_init_rd_atom = hr_dev->caps.max_qp_init_rdma;
226 props->atomic_cap = IB_ATOMIC_NONE;
227 props->max_pkeys = 1;
228 props->local_ca_ack_delay = hr_dev->caps.local_ca_ack_delay;
229
230 return 0;
231}
232
Lijun Ou2eefca22016-09-15 23:48:06 +0100233static struct net_device *hns_roce_get_netdev(struct ib_device *ib_dev,
234 u8 port_num)
235{
236 struct hns_roce_dev *hr_dev = to_hr_dev(ib_dev);
237 struct net_device *ndev;
238
239 if (port_num < 1 || port_num > hr_dev->caps.num_ports)
240 return NULL;
241
242 rcu_read_lock();
243
244 ndev = hr_dev->iboe.netdevs[port_num - 1];
245 if (ndev)
246 dev_hold(ndev);
247
248 rcu_read_unlock();
249 return ndev;
250}
251
oulijun9a443532016-07-21 19:06:38 +0800252static int hns_roce_query_port(struct ib_device *ib_dev, u8 port_num,
253 struct ib_port_attr *props)
254{
255 struct hns_roce_dev *hr_dev = to_hr_dev(ib_dev);
256 struct device *dev = &hr_dev->pdev->dev;
257 struct net_device *net_dev;
258 unsigned long flags;
259 enum ib_mtu mtu;
260 u8 port;
261
262 assert(port_num > 0);
263 port = port_num - 1;
264
265 memset(props, 0, sizeof(*props));
266
267 props->max_mtu = hr_dev->caps.max_mtu;
268 props->gid_tbl_len = hr_dev->caps.gid_table_len[port];
269 props->port_cap_flags = IB_PORT_CM_SUP | IB_PORT_REINIT_SUP |
270 IB_PORT_VENDOR_CLASS_SUP |
271 IB_PORT_BOOT_MGMT_SUP;
272 props->max_msg_sz = HNS_ROCE_MAX_MSG_LEN;
273 props->pkey_tbl_len = 1;
274 props->active_width = IB_WIDTH_4X;
275 props->active_speed = 1;
276
277 spin_lock_irqsave(&hr_dev->iboe.lock, flags);
278
279 net_dev = hr_dev->iboe.netdevs[port];
280 if (!net_dev) {
281 spin_unlock_irqrestore(&hr_dev->iboe.lock, flags);
282 dev_err(dev, "find netdev %d failed!\r\n", port);
283 return -EINVAL;
284 }
285
286 mtu = iboe_get_mtu(net_dev->mtu);
287 props->active_mtu = mtu ? min(props->max_mtu, mtu) : IB_MTU_256;
288 props->state = (netif_running(net_dev) && netif_carrier_ok(net_dev)) ?
289 IB_PORT_ACTIVE : IB_PORT_DOWN;
290 props->phys_state = (props->state == IB_PORT_ACTIVE) ? 5 : 3;
291
292 spin_unlock_irqrestore(&hr_dev->iboe.lock, flags);
293
294 return 0;
295}
296
297static enum rdma_link_layer hns_roce_get_link_layer(struct ib_device *device,
298 u8 port_num)
299{
300 return IB_LINK_LAYER_ETHERNET;
301}
302
303static int hns_roce_query_gid(struct ib_device *ib_dev, u8 port_num, int index,
304 union ib_gid *gid)
305{
oulijun9a443532016-07-21 19:06:38 +0800306 return 0;
307}
308
309static int hns_roce_query_pkey(struct ib_device *ib_dev, u8 port, u16 index,
310 u16 *pkey)
311{
312 *pkey = PKEY_ID;
313
314 return 0;
315}
316
317static int hns_roce_modify_device(struct ib_device *ib_dev, int mask,
318 struct ib_device_modify *props)
319{
320 unsigned long flags;
321
322 if (mask & ~IB_DEVICE_MODIFY_NODE_DESC)
323 return -EOPNOTSUPP;
324
325 if (mask & IB_DEVICE_MODIFY_NODE_DESC) {
326 spin_lock_irqsave(&to_hr_dev(ib_dev)->sm_lock, flags);
327 memcpy(ib_dev->node_desc, props->node_desc, NODE_DESC_SIZE);
328 spin_unlock_irqrestore(&to_hr_dev(ib_dev)->sm_lock, flags);
329 }
330
331 return 0;
332}
333
334static int hns_roce_modify_port(struct ib_device *ib_dev, u8 port_num, int mask,
335 struct ib_port_modify *props)
336{
337 return 0;
338}
339
340static struct ib_ucontext *hns_roce_alloc_ucontext(struct ib_device *ib_dev,
341 struct ib_udata *udata)
342{
343 int ret = 0;
344 struct hns_roce_ucontext *context;
345 struct hns_roce_ib_alloc_ucontext_resp resp;
346 struct hns_roce_dev *hr_dev = to_hr_dev(ib_dev);
347
348 resp.qp_tab_size = hr_dev->caps.num_qps;
349
350 context = kmalloc(sizeof(*context), GFP_KERNEL);
351 if (!context)
352 return ERR_PTR(-ENOMEM);
353
354 ret = hns_roce_uar_alloc(hr_dev, &context->uar);
355 if (ret)
356 goto error_fail_uar_alloc;
357
358 ret = ib_copy_to_udata(udata, &resp, sizeof(resp));
359 if (ret)
360 goto error_fail_copy_to_udata;
361
362 return &context->ibucontext;
363
364error_fail_copy_to_udata:
365 hns_roce_uar_free(hr_dev, &context->uar);
366
367error_fail_uar_alloc:
368 kfree(context);
369
370 return ERR_PTR(ret);
371}
372
373static int hns_roce_dealloc_ucontext(struct ib_ucontext *ibcontext)
374{
375 struct hns_roce_ucontext *context = to_hr_ucontext(ibcontext);
376
377 hns_roce_uar_free(to_hr_dev(ibcontext->device), &context->uar);
378 kfree(context);
379
380 return 0;
381}
382
383static int hns_roce_mmap(struct ib_ucontext *context,
384 struct vm_area_struct *vma)
385{
Wei Hu (Xavier)8f3e9f32016-11-23 19:41:00 +0000386 struct hns_roce_dev *hr_dev = to_hr_dev(context->device);
387
oulijun9a443532016-07-21 19:06:38 +0800388 if (((vma->vm_end - vma->vm_start) % PAGE_SIZE) != 0)
389 return -EINVAL;
390
391 if (vma->vm_pgoff == 0) {
392 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
393 if (io_remap_pfn_range(vma, vma->vm_start,
394 to_hr_ucontext(context)->uar.pfn,
395 PAGE_SIZE, vma->vm_page_prot))
396 return -EAGAIN;
Wei Hu (Xavier)8f3e9f32016-11-23 19:41:00 +0000397 } else if (vma->vm_pgoff == 1 && hr_dev->hw_rev == HNS_ROCE_HW_VER1) {
398 /* vm_pgoff: 1 -- TPTR */
399 if (io_remap_pfn_range(vma, vma->vm_start,
400 hr_dev->tptr_dma_addr >> PAGE_SHIFT,
401 hr_dev->tptr_size,
402 vma->vm_page_prot))
403 return -EAGAIN;
404 } else
oulijun9a443532016-07-21 19:06:38 +0800405 return -EINVAL;
oulijun9a443532016-07-21 19:06:38 +0800406
407 return 0;
408}
409
410static int hns_roce_port_immutable(struct ib_device *ib_dev, u8 port_num,
411 struct ib_port_immutable *immutable)
412{
413 struct ib_port_attr attr;
414 int ret;
415
416 ret = hns_roce_query_port(ib_dev, port_num, &attr);
417 if (ret)
418 return ret;
419
420 immutable->pkey_tbl_len = attr.pkey_tbl_len;
421 immutable->gid_tbl_len = attr.gid_tbl_len;
422
423 immutable->core_cap_flags = RDMA_CORE_PORT_IBA_ROCE;
424 immutable->max_mad_size = IB_MGMT_MAD_SIZE;
425
426 return 0;
427}
428
429static void hns_roce_unregister_device(struct hns_roce_dev *hr_dev)
430{
431 struct hns_roce_ib_iboe *iboe = &hr_dev->iboe;
432
433 unregister_inetaddr_notifier(&iboe->nb_inet);
434 unregister_netdevice_notifier(&iboe->nb);
435 ib_unregister_device(&hr_dev->ib_dev);
436}
437
438static int hns_roce_register_device(struct hns_roce_dev *hr_dev)
439{
440 int ret;
441 struct hns_roce_ib_iboe *iboe = NULL;
442 struct ib_device *ib_dev = NULL;
443 struct device *dev = &hr_dev->pdev->dev;
444
445 iboe = &hr_dev->iboe;
Lijun Ou49fdf6b2016-09-20 17:07:02 +0100446 spin_lock_init(&iboe->lock);
oulijun9a443532016-07-21 19:06:38 +0800447
448 ib_dev = &hr_dev->ib_dev;
449 strlcpy(ib_dev->name, "hisi_%d", IB_DEVICE_NAME_MAX);
450
451 ib_dev->owner = THIS_MODULE;
452 ib_dev->node_type = RDMA_NODE_IB_CA;
453 ib_dev->dma_device = dev;
454
455 ib_dev->phys_port_cnt = hr_dev->caps.num_ports;
456 ib_dev->local_dma_lkey = hr_dev->caps.reserved_lkey;
457 ib_dev->num_comp_vectors = hr_dev->caps.num_comp_vectors;
458 ib_dev->uverbs_abi_ver = 1;
459 ib_dev->uverbs_cmd_mask =
460 (1ULL << IB_USER_VERBS_CMD_GET_CONTEXT) |
461 (1ULL << IB_USER_VERBS_CMD_QUERY_DEVICE) |
462 (1ULL << IB_USER_VERBS_CMD_QUERY_PORT) |
463 (1ULL << IB_USER_VERBS_CMD_ALLOC_PD) |
464 (1ULL << IB_USER_VERBS_CMD_DEALLOC_PD) |
465 (1ULL << IB_USER_VERBS_CMD_REG_MR) |
466 (1ULL << IB_USER_VERBS_CMD_DEREG_MR) |
467 (1ULL << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL) |
468 (1ULL << IB_USER_VERBS_CMD_CREATE_CQ) |
469 (1ULL << IB_USER_VERBS_CMD_DESTROY_CQ) |
470 (1ULL << IB_USER_VERBS_CMD_CREATE_QP) |
471 (1ULL << IB_USER_VERBS_CMD_MODIFY_QP) |
472 (1ULL << IB_USER_VERBS_CMD_QUERY_QP) |
473 (1ULL << IB_USER_VERBS_CMD_DESTROY_QP);
474
475 /* HCA||device||port */
476 ib_dev->modify_device = hns_roce_modify_device;
477 ib_dev->query_device = hns_roce_query_device;
478 ib_dev->query_port = hns_roce_query_port;
479 ib_dev->modify_port = hns_roce_modify_port;
480 ib_dev->get_link_layer = hns_roce_get_link_layer;
Lijun Ou2eefca22016-09-15 23:48:06 +0100481 ib_dev->get_netdev = hns_roce_get_netdev;
oulijun9a443532016-07-21 19:06:38 +0800482 ib_dev->query_gid = hns_roce_query_gid;
Shaobo Xu82547462016-11-23 19:41:08 +0000483 ib_dev->add_gid = hns_roce_add_gid;
484 ib_dev->del_gid = hns_roce_del_gid;
oulijun9a443532016-07-21 19:06:38 +0800485 ib_dev->query_pkey = hns_roce_query_pkey;
486 ib_dev->alloc_ucontext = hns_roce_alloc_ucontext;
487 ib_dev->dealloc_ucontext = hns_roce_dealloc_ucontext;
488 ib_dev->mmap = hns_roce_mmap;
489
490 /* PD */
491 ib_dev->alloc_pd = hns_roce_alloc_pd;
492 ib_dev->dealloc_pd = hns_roce_dealloc_pd;
493
494 /* AH */
495 ib_dev->create_ah = hns_roce_create_ah;
496 ib_dev->query_ah = hns_roce_query_ah;
497 ib_dev->destroy_ah = hns_roce_destroy_ah;
498
499 /* QP */
500 ib_dev->create_qp = hns_roce_create_qp;
501 ib_dev->modify_qp = hns_roce_modify_qp;
502 ib_dev->query_qp = hr_dev->hw->query_qp;
503 ib_dev->destroy_qp = hr_dev->hw->destroy_qp;
504 ib_dev->post_send = hr_dev->hw->post_send;
505 ib_dev->post_recv = hr_dev->hw->post_recv;
506
507 /* CQ */
508 ib_dev->create_cq = hns_roce_ib_create_cq;
509 ib_dev->destroy_cq = hns_roce_ib_destroy_cq;
510 ib_dev->req_notify_cq = hr_dev->hw->req_notify_cq;
511 ib_dev->poll_cq = hr_dev->hw->poll_cq;
512
513 /* MR */
514 ib_dev->get_dma_mr = hns_roce_get_dma_mr;
515 ib_dev->reg_user_mr = hns_roce_reg_user_mr;
516 ib_dev->dereg_mr = hns_roce_dereg_mr;
517
518 /* OTHERS */
519 ib_dev->get_port_immutable = hns_roce_port_immutable;
520
521 ret = ib_register_device(ib_dev, NULL);
522 if (ret) {
523 dev_err(dev, "ib_register_device failed!\n");
524 return ret;
525 }
526
Shaobo Xu82547462016-11-23 19:41:08 +0000527 ret = hns_roce_setup_mtu_mac(hr_dev);
oulijun9a443532016-07-21 19:06:38 +0800528 if (ret) {
Shaobo Xu82547462016-11-23 19:41:08 +0000529 dev_err(dev, "setup_mtu_mac failed!\n");
530 goto error_failed_setup_mtu_mac;
oulijun9a443532016-07-21 19:06:38 +0800531 }
532
oulijun9a443532016-07-21 19:06:38 +0800533 iboe->nb.notifier_call = hns_roce_netdev_event;
534 ret = register_netdevice_notifier(&iboe->nb);
535 if (ret) {
536 dev_err(dev, "register_netdevice_notifier failed!\n");
Shaobo Xu82547462016-11-23 19:41:08 +0000537 goto error_failed_setup_mtu_mac;
oulijun9a443532016-07-21 19:06:38 +0800538 }
539
540 return 0;
541
Shaobo Xu82547462016-11-23 19:41:08 +0000542error_failed_setup_mtu_mac:
oulijun9a443532016-07-21 19:06:38 +0800543 ib_unregister_device(ib_dev);
544
545 return ret;
546}
547
Salil528f1de2016-08-24 04:44:50 +0800548static const struct of_device_id hns_roce_of_match[] = {
549 { .compatible = "hisilicon,hns-roce-v1", .data = &hns_roce_hw_v1, },
550 {},
551};
552MODULE_DEVICE_TABLE(of, hns_roce_of_match);
553
554static const struct acpi_device_id hns_roce_acpi_match[] = {
555 { "HISI00D1", (kernel_ulong_t)&hns_roce_hw_v1 },
556 {},
557};
558MODULE_DEVICE_TABLE(acpi, hns_roce_acpi_match);
559
560static int hns_roce_node_match(struct device *dev, void *fwnode)
561{
562 return dev->fwnode == fwnode;
563}
564
565static struct
566platform_device *hns_roce_find_pdev(struct fwnode_handle *fwnode)
567{
568 struct device *dev;
569
570 /* get the 'device'corresponding to matching 'fwnode' */
571 dev = bus_find_device(&platform_bus_type, NULL,
572 fwnode, hns_roce_node_match);
573 /* get the platform device */
574 return dev ? to_platform_device(dev) : NULL;
575}
576
oulijun9a443532016-07-21 19:06:38 +0800577static int hns_roce_get_cfg(struct hns_roce_dev *hr_dev)
578{
579 int i;
Salil528f1de2016-08-24 04:44:50 +0800580 int ret;
oulijun9a443532016-07-21 19:06:38 +0800581 u8 phy_port;
582 int port_cnt = 0;
583 struct device *dev = &hr_dev->pdev->dev;
oulijun9a443532016-07-21 19:06:38 +0800584 struct device_node *net_node;
585 struct net_device *netdev = NULL;
586 struct platform_device *pdev = NULL;
587 struct resource *res;
588
Salil528f1de2016-08-24 04:44:50 +0800589 /* check if we are compatible with the underlying SoC */
590 if (dev_of_node(dev)) {
591 const struct of_device_id *of_id;
592
593 of_id = of_match_node(hns_roce_of_match, dev->of_node);
594 if (!of_id) {
595 dev_err(dev, "device is not compatible!\n");
596 return -ENXIO;
597 }
598 hr_dev->hw = (struct hns_roce_hw *)of_id->data;
599 if (!hr_dev->hw) {
600 dev_err(dev, "couldn't get H/W specific DT data!\n");
601 return -ENXIO;
602 }
603 } else if (is_acpi_device_node(dev->fwnode)) {
604 const struct acpi_device_id *acpi_id;
605
606 acpi_id = acpi_match_device(hns_roce_acpi_match, dev);
607 if (!acpi_id) {
608 dev_err(dev, "device is not compatible!\n");
609 return -ENXIO;
610 }
611 hr_dev->hw = (struct hns_roce_hw *) acpi_id->driver_data;
612 if (!hr_dev->hw) {
613 dev_err(dev, "couldn't get H/W specific ACPI data!\n");
614 return -ENXIO;
615 }
oulijun9a443532016-07-21 19:06:38 +0800616 } else {
Salil528f1de2016-08-24 04:44:50 +0800617 dev_err(dev, "can't read compatibility data from DT or ACPI\n");
618 return -ENXIO;
oulijun9a443532016-07-21 19:06:38 +0800619 }
620
Salil528f1de2016-08-24 04:44:50 +0800621 /* get the mapped register base address */
oulijun9a443532016-07-21 19:06:38 +0800622 res = platform_get_resource(hr_dev->pdev, IORESOURCE_MEM, 0);
Salil528f1de2016-08-24 04:44:50 +0800623 if (!res) {
624 dev_err(dev, "memory resource not found!\n");
625 return -EINVAL;
626 }
oulijun9a443532016-07-21 19:06:38 +0800627 hr_dev->reg_base = devm_ioremap_resource(dev, res);
Wei Yongjun204f69b2016-08-06 01:01:24 +0000628 if (IS_ERR(hr_dev->reg_base))
629 return PTR_ERR(hr_dev->reg_base);
oulijun9a443532016-07-21 19:06:38 +0800630
Lijun Ou31644662016-09-15 23:48:07 +0100631 /* read the node_guid of IB device from the DT or ACPI */
632 ret = device_property_read_u8_array(dev, "node-guid",
633 (u8 *)&hr_dev->ib_dev.node_guid,
634 GUID_LEN);
635 if (ret) {
636 dev_err(dev, "couldn't get node_guid from DT or ACPI!\n");
637 return ret;
638 }
639
Salil528f1de2016-08-24 04:44:50 +0800640 /* get the RoCE associated ethernet ports or netdevices */
oulijun9a443532016-07-21 19:06:38 +0800641 for (i = 0; i < HNS_ROCE_MAX_PORTS; i++) {
Salil528f1de2016-08-24 04:44:50 +0800642 if (dev_of_node(dev)) {
643 net_node = of_parse_phandle(dev->of_node, "eth-handle",
644 i);
645 if (!net_node)
646 continue;
oulijun9a443532016-07-21 19:06:38 +0800647 pdev = of_find_device_by_node(net_node);
Salil528f1de2016-08-24 04:44:50 +0800648 } else if (is_acpi_device_node(dev->fwnode)) {
649 struct acpi_reference_args args;
650 struct fwnode_handle *fwnode;
651
652 ret = acpi_node_get_property_reference(dev->fwnode,
653 "eth-handle",
654 i, &args);
655 if (ret)
656 continue;
657 fwnode = acpi_fwnode_handle(args.adev);
658 pdev = hns_roce_find_pdev(fwnode);
659 } else {
660 dev_err(dev, "cannot read data from DT or ACPI\n");
661 return -ENXIO;
662 }
663
664 if (pdev) {
oulijun9a443532016-07-21 19:06:38 +0800665 netdev = platform_get_drvdata(pdev);
666 phy_port = (u8)i;
667 if (netdev) {
668 hr_dev->iboe.netdevs[port_cnt] = netdev;
669 hr_dev->iboe.phy_port[port_cnt] = phy_port;
670 } else {
Salil528f1de2016-08-24 04:44:50 +0800671 dev_err(dev, "no netdev found with pdev %s\n",
672 pdev->name);
oulijun9a443532016-07-21 19:06:38 +0800673 return -ENODEV;
674 }
675 port_cnt++;
676 }
677 }
678
679 if (port_cnt == 0) {
Salil528f1de2016-08-24 04:44:50 +0800680 dev_err(dev, "unable to get eth-handle for available ports!\n");
oulijun9a443532016-07-21 19:06:38 +0800681 return -EINVAL;
682 }
683
684 hr_dev->caps.num_ports = port_cnt;
685
Salil528f1de2016-08-24 04:44:50 +0800686 /* cmd issue mode: 0 is poll, 1 is event */
oulijun9a443532016-07-21 19:06:38 +0800687 hr_dev->cmd_mod = 1;
688 hr_dev->loop_idc = 0;
689
Salil528f1de2016-08-24 04:44:50 +0800690 /* read the interrupt names from the DT or ACPI */
691 ret = device_property_read_string_array(dev, "interrupt-names",
692 hr_dev->irq_names,
693 HNS_ROCE_MAX_IRQ_NUM);
694 if (ret < 0) {
695 dev_err(dev, "couldn't get interrupt names from DT or ACPI!\n");
696 return ret;
697 }
698
699 /* fetch the interrupt numbers */
oulijun9a443532016-07-21 19:06:38 +0800700 for (i = 0; i < HNS_ROCE_MAX_IRQ_NUM; i++) {
701 hr_dev->irq[i] = platform_get_irq(hr_dev->pdev, i);
702 if (hr_dev->irq[i] <= 0) {
Salil528f1de2016-08-24 04:44:50 +0800703 dev_err(dev, "platform get of irq[=%d] failed!\n", i);
oulijun9a443532016-07-21 19:06:38 +0800704 return -EINVAL;
705 }
oulijun9a443532016-07-21 19:06:38 +0800706 }
707
708 return 0;
709}
710
711static int hns_roce_init_hem(struct hns_roce_dev *hr_dev)
712{
713 int ret;
714 struct device *dev = &hr_dev->pdev->dev;
715
716 ret = hns_roce_init_hem_table(hr_dev, &hr_dev->mr_table.mtt_table,
717 HEM_TYPE_MTT, hr_dev->caps.mtt_entry_sz,
718 hr_dev->caps.num_mtt_segs, 1);
719 if (ret) {
720 dev_err(dev, "Failed to init MTT context memory, aborting.\n");
721 return ret;
722 }
723
724 ret = hns_roce_init_hem_table(hr_dev, &hr_dev->mr_table.mtpt_table,
725 HEM_TYPE_MTPT, hr_dev->caps.mtpt_entry_sz,
726 hr_dev->caps.num_mtpts, 1);
727 if (ret) {
728 dev_err(dev, "Failed to init MTPT context memory, aborting.\n");
729 goto err_unmap_mtt;
730 }
731
732 ret = hns_roce_init_hem_table(hr_dev, &hr_dev->qp_table.qp_table,
733 HEM_TYPE_QPC, hr_dev->caps.qpc_entry_sz,
734 hr_dev->caps.num_qps, 1);
735 if (ret) {
736 dev_err(dev, "Failed to init QP context memory, aborting.\n");
737 goto err_unmap_dmpt;
738 }
739
740 ret = hns_roce_init_hem_table(hr_dev, &hr_dev->qp_table.irrl_table,
741 HEM_TYPE_IRRL,
742 hr_dev->caps.irrl_entry_sz *
743 hr_dev->caps.max_qp_init_rdma,
744 hr_dev->caps.num_qps, 1);
745 if (ret) {
746 dev_err(dev, "Failed to init irrl_table memory, aborting.\n");
747 goto err_unmap_qp;
748 }
749
750 ret = hns_roce_init_hem_table(hr_dev, &hr_dev->cq_table.table,
751 HEM_TYPE_CQC, hr_dev->caps.cqc_entry_sz,
752 hr_dev->caps.num_cqs, 1);
753 if (ret) {
754 dev_err(dev, "Failed to init CQ context memory, aborting.\n");
755 goto err_unmap_irrl;
756 }
757
758 return 0;
759
760err_unmap_irrl:
761 hns_roce_cleanup_hem_table(hr_dev, &hr_dev->qp_table.irrl_table);
762
763err_unmap_qp:
764 hns_roce_cleanup_hem_table(hr_dev, &hr_dev->qp_table.qp_table);
765
766err_unmap_dmpt:
767 hns_roce_cleanup_hem_table(hr_dev, &hr_dev->mr_table.mtpt_table);
768
769err_unmap_mtt:
770 hns_roce_cleanup_hem_table(hr_dev, &hr_dev->mr_table.mtt_table);
771
772 return ret;
773}
774
775/**
776* hns_roce_setup_hca - setup host channel adapter
777* @hr_dev: pointer to hns roce device
778* Return : int
779*/
780static int hns_roce_setup_hca(struct hns_roce_dev *hr_dev)
781{
782 int ret;
783 struct device *dev = &hr_dev->pdev->dev;
784
785 spin_lock_init(&hr_dev->sm_lock);
oulijun9a443532016-07-21 19:06:38 +0800786 spin_lock_init(&hr_dev->bt_cmd_lock);
787
788 ret = hns_roce_init_uar_table(hr_dev);
789 if (ret) {
790 dev_err(dev, "Failed to initialize uar table. aborting\n");
791 return ret;
792 }
793
794 ret = hns_roce_uar_alloc(hr_dev, &hr_dev->priv_uar);
795 if (ret) {
796 dev_err(dev, "Failed to allocate priv_uar.\n");
797 goto err_uar_table_free;
798 }
799
800 ret = hns_roce_init_pd_table(hr_dev);
801 if (ret) {
802 dev_err(dev, "Failed to init protected domain table.\n");
803 goto err_uar_alloc_free;
804 }
805
806 ret = hns_roce_init_mr_table(hr_dev);
807 if (ret) {
808 dev_err(dev, "Failed to init memory region table.\n");
809 goto err_pd_table_free;
810 }
811
812 ret = hns_roce_init_cq_table(hr_dev);
813 if (ret) {
814 dev_err(dev, "Failed to init completion queue table.\n");
815 goto err_mr_table_free;
816 }
817
818 ret = hns_roce_init_qp_table(hr_dev);
819 if (ret) {
820 dev_err(dev, "Failed to init queue pair table.\n");
821 goto err_cq_table_free;
822 }
823
824 return 0;
825
826err_cq_table_free:
827 hns_roce_cleanup_cq_table(hr_dev);
828
829err_mr_table_free:
830 hns_roce_cleanup_mr_table(hr_dev);
831
832err_pd_table_free:
833 hns_roce_cleanup_pd_table(hr_dev);
834
835err_uar_alloc_free:
836 hns_roce_uar_free(hr_dev, &hr_dev->priv_uar);
837
838err_uar_table_free:
839 hns_roce_cleanup_uar_table(hr_dev);
840 return ret;
841}
842
843/**
844* hns_roce_probe - RoCE driver entrance
845* @pdev: pointer to platform device
846* Return : int
847*
848*/
849static int hns_roce_probe(struct platform_device *pdev)
850{
851 int ret;
852 struct hns_roce_dev *hr_dev;
853 struct device *dev = &pdev->dev;
854
855 hr_dev = (struct hns_roce_dev *)ib_alloc_device(sizeof(*hr_dev));
856 if (!hr_dev)
857 return -ENOMEM;
858
859 memset((u8 *)hr_dev + sizeof(struct ib_device), 0,
860 sizeof(struct hns_roce_dev) - sizeof(struct ib_device));
861
862 hr_dev->pdev = pdev;
863 platform_set_drvdata(pdev, hr_dev);
864
865 if (dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64ULL)) &&
866 dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32ULL))) {
Salil528f1de2016-08-24 04:44:50 +0800867 dev_err(dev, "Not usable DMA addressing mode\n");
oulijun9a443532016-07-21 19:06:38 +0800868 ret = -EIO;
869 goto error_failed_get_cfg;
870 }
871
872 ret = hns_roce_get_cfg(hr_dev);
873 if (ret) {
874 dev_err(dev, "Get Configuration failed!\n");
875 goto error_failed_get_cfg;
876 }
877
878 ret = hr_dev->hw->reset(hr_dev, true);
879 if (ret) {
880 dev_err(dev, "Reset RoCE engine failed!\n");
881 goto error_failed_get_cfg;
882 }
883
884 hr_dev->hw->hw_profile(hr_dev);
885
886 ret = hns_roce_cmd_init(hr_dev);
887 if (ret) {
888 dev_err(dev, "cmd init failed!\n");
889 goto error_failed_cmd_init;
890 }
891
892 ret = hns_roce_init_eq_table(hr_dev);
893 if (ret) {
894 dev_err(dev, "eq init failed!\n");
895 goto error_failed_eq_table;
896 }
897
898 if (hr_dev->cmd_mod) {
899 ret = hns_roce_cmd_use_events(hr_dev);
900 if (ret) {
901 dev_err(dev, "Switch to event-driven cmd failed!\n");
902 goto error_failed_use_event;
903 }
904 }
905
906 ret = hns_roce_init_hem(hr_dev);
907 if (ret) {
908 dev_err(dev, "init HEM(Hardware Entry Memory) failed!\n");
909 goto error_failed_init_hem;
910 }
911
912 ret = hns_roce_setup_hca(hr_dev);
913 if (ret) {
914 dev_err(dev, "setup hca failed!\n");
915 goto error_failed_setup_hca;
916 }
917
918 ret = hr_dev->hw->hw_init(hr_dev);
919 if (ret) {
920 dev_err(dev, "hw_init failed!\n");
921 goto error_failed_engine_init;
922 }
923
924 ret = hns_roce_register_device(hr_dev);
925 if (ret)
926 goto error_failed_register_device;
927
928 return 0;
929
930error_failed_register_device:
931 hr_dev->hw->hw_exit(hr_dev);
932
933error_failed_engine_init:
934 hns_roce_cleanup_bitmap(hr_dev);
935
936error_failed_setup_hca:
937 hns_roce_cleanup_hem(hr_dev);
938
939error_failed_init_hem:
940 if (hr_dev->cmd_mod)
941 hns_roce_cmd_use_polling(hr_dev);
942
943error_failed_use_event:
944 hns_roce_cleanup_eq_table(hr_dev);
945
946error_failed_eq_table:
947 hns_roce_cmd_cleanup(hr_dev);
948
949error_failed_cmd_init:
950 ret = hr_dev->hw->reset(hr_dev, false);
951 if (ret)
952 dev_err(&hr_dev->pdev->dev, "roce_engine reset fail\n");
953
954error_failed_get_cfg:
955 ib_dealloc_device(&hr_dev->ib_dev);
956
957 return ret;
958}
959
960/**
961* hns_roce_remove - remove RoCE device
962* @pdev: pointer to platform device
963*/
964static int hns_roce_remove(struct platform_device *pdev)
965{
966 struct hns_roce_dev *hr_dev = platform_get_drvdata(pdev);
967
968 hns_roce_unregister_device(hr_dev);
969 hr_dev->hw->hw_exit(hr_dev);
970 hns_roce_cleanup_bitmap(hr_dev);
971 hns_roce_cleanup_hem(hr_dev);
972
973 if (hr_dev->cmd_mod)
974 hns_roce_cmd_use_polling(hr_dev);
975
976 hns_roce_cleanup_eq_table(hr_dev);
977 hns_roce_cmd_cleanup(hr_dev);
978 hr_dev->hw->reset(hr_dev, false);
979
980 ib_dealloc_device(&hr_dev->ib_dev);
981
982 return 0;
983}
984
oulijun9a443532016-07-21 19:06:38 +0800985static struct platform_driver hns_roce_driver = {
986 .probe = hns_roce_probe,
987 .remove = hns_roce_remove,
988 .driver = {
989 .name = DRV_NAME,
990 .of_match_table = hns_roce_of_match,
Salil528f1de2016-08-24 04:44:50 +0800991 .acpi_match_table = ACPI_PTR(hns_roce_acpi_match),
oulijun9a443532016-07-21 19:06:38 +0800992 },
993};
994
995module_platform_driver(hns_roce_driver);
996
997MODULE_LICENSE("Dual BSD/GPL");
998MODULE_AUTHOR("Wei Hu <xavier.huwei@huawei.com>");
999MODULE_AUTHOR("Nenglong Zhao <zhaonenglong@hisilicon.com>");
1000MODULE_AUTHOR("Lijun Ou <oulijun@huawei.com>");
1001MODULE_DESCRIPTION("HNS RoCE Driver");