GNU Linux-libre 4.19.245-gnu1
[releases.git] / drivers / infiniband / hw / usnic / usnic_ib_verbs.c
1 /*
2  * Copyright (c) 2013, Cisco Systems, Inc. All rights reserved.
3  *
4  * This software is available to you under a choice of one of two
5  * licenses.  You may choose to be licensed under the terms of the GNU
6  * General Public License (GPL) Version 2, available from the file
7  * COPYING in the main directory of this source tree, or the
8  * BSD license below:
9  *
10  *     Redistribution and use in source and binary forms, with or
11  *     without modification, are permitted provided that the following
12  *     conditions are met:
13  *
14  *      - Redistributions of source code must retain the above
15  *        copyright notice, this list of conditions and the following
16  *        disclaimer.
17  *
18  *      - Redistributions in binary form must reproduce the above
19  *        copyright notice, this list of conditions and the following
20  *        disclaimer in the documentation and/or other materials
21  *        provided with the distribution.
22  *
23  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30  * SOFTWARE.
31  *
32  */
33 #include <linux/module.h>
34 #include <linux/init.h>
35 #include <linux/slab.h>
36 #include <linux/errno.h>
37
38 #include <rdma/ib_user_verbs.h>
39 #include <rdma/ib_addr.h>
40
41 #include "usnic_abi.h"
42 #include "usnic_ib.h"
43 #include "usnic_common_util.h"
44 #include "usnic_ib_qp_grp.h"
45 #include "usnic_ib_verbs.h"
46 #include "usnic_fwd.h"
47 #include "usnic_log.h"
48 #include "usnic_uiom.h"
49 #include "usnic_transport.h"
50
51 #define USNIC_DEFAULT_TRANSPORT USNIC_TRANSPORT_ROCE_CUSTOM
52
53 const struct usnic_vnic_res_spec min_transport_spec[USNIC_TRANSPORT_MAX] = {
54         { /*USNIC_TRANSPORT_UNKNOWN*/
55                 .resources = {
56                         {.type = USNIC_VNIC_RES_TYPE_EOL,       .cnt = 0,},
57                 },
58         },
59         { /*USNIC_TRANSPORT_ROCE_CUSTOM*/
60                 .resources = {
61                         {.type = USNIC_VNIC_RES_TYPE_WQ,        .cnt = 1,},
62                         {.type = USNIC_VNIC_RES_TYPE_RQ,        .cnt = 1,},
63                         {.type = USNIC_VNIC_RES_TYPE_CQ,        .cnt = 1,},
64                         {.type = USNIC_VNIC_RES_TYPE_EOL,       .cnt = 0,},
65                 },
66         },
67         { /*USNIC_TRANSPORT_IPV4_UDP*/
68                 .resources = {
69                         {.type = USNIC_VNIC_RES_TYPE_WQ,        .cnt = 1,},
70                         {.type = USNIC_VNIC_RES_TYPE_RQ,        .cnt = 1,},
71                         {.type = USNIC_VNIC_RES_TYPE_CQ,        .cnt = 1,},
72                         {.type = USNIC_VNIC_RES_TYPE_EOL,       .cnt = 0,},
73                 },
74         },
75 };
76
77 static void usnic_ib_fw_string_to_u64(char *fw_ver_str, u64 *fw_ver)
78 {
79         *fw_ver = *((u64 *)fw_ver_str);
80 }
81
82 static int usnic_ib_fill_create_qp_resp(struct usnic_ib_qp_grp *qp_grp,
83                                         struct ib_udata *udata)
84 {
85         struct usnic_ib_dev *us_ibdev;
86         struct usnic_ib_create_qp_resp resp;
87         struct pci_dev *pdev;
88         struct vnic_dev_bar *bar;
89         struct usnic_vnic_res_chunk *chunk;
90         struct usnic_ib_qp_grp_flow *default_flow;
91         int i, err;
92
93         memset(&resp, 0, sizeof(resp));
94
95         us_ibdev = qp_grp->vf->pf;
96         pdev = usnic_vnic_get_pdev(qp_grp->vf->vnic);
97         if (!pdev) {
98                 usnic_err("Failed to get pdev of qp_grp %d\n",
99                                 qp_grp->grp_id);
100                 return -EFAULT;
101         }
102
103         bar = usnic_vnic_get_bar(qp_grp->vf->vnic, 0);
104         if (!bar) {
105                 usnic_err("Failed to get bar0 of qp_grp %d vf %s",
106                                 qp_grp->grp_id, pci_name(pdev));
107                 return -EFAULT;
108         }
109
110         resp.vfid = usnic_vnic_get_index(qp_grp->vf->vnic);
111         resp.bar_bus_addr = bar->bus_addr;
112         resp.bar_len = bar->len;
113
114         chunk = usnic_ib_qp_grp_get_chunk(qp_grp, USNIC_VNIC_RES_TYPE_RQ);
115         if (IS_ERR(chunk)) {
116                 usnic_err("Failed to get chunk %s for qp_grp %d with err %ld\n",
117                         usnic_vnic_res_type_to_str(USNIC_VNIC_RES_TYPE_RQ),
118                         qp_grp->grp_id,
119                         PTR_ERR(chunk));
120                 return PTR_ERR(chunk);
121         }
122
123         WARN_ON(chunk->type != USNIC_VNIC_RES_TYPE_RQ);
124         resp.rq_cnt = chunk->cnt;
125         for (i = 0; i < chunk->cnt; i++)
126                 resp.rq_idx[i] = chunk->res[i]->vnic_idx;
127
128         chunk = usnic_ib_qp_grp_get_chunk(qp_grp, USNIC_VNIC_RES_TYPE_WQ);
129         if (IS_ERR(chunk)) {
130                 usnic_err("Failed to get chunk %s for qp_grp %d with err %ld\n",
131                         usnic_vnic_res_type_to_str(USNIC_VNIC_RES_TYPE_WQ),
132                         qp_grp->grp_id,
133                         PTR_ERR(chunk));
134                 return PTR_ERR(chunk);
135         }
136
137         WARN_ON(chunk->type != USNIC_VNIC_RES_TYPE_WQ);
138         resp.wq_cnt = chunk->cnt;
139         for (i = 0; i < chunk->cnt; i++)
140                 resp.wq_idx[i] = chunk->res[i]->vnic_idx;
141
142         chunk = usnic_ib_qp_grp_get_chunk(qp_grp, USNIC_VNIC_RES_TYPE_CQ);
143         if (IS_ERR(chunk)) {
144                 usnic_err("Failed to get chunk %s for qp_grp %d with err %ld\n",
145                         usnic_vnic_res_type_to_str(USNIC_VNIC_RES_TYPE_CQ),
146                         qp_grp->grp_id,
147                         PTR_ERR(chunk));
148                 return PTR_ERR(chunk);
149         }
150
151         WARN_ON(chunk->type != USNIC_VNIC_RES_TYPE_CQ);
152         resp.cq_cnt = chunk->cnt;
153         for (i = 0; i < chunk->cnt; i++)
154                 resp.cq_idx[i] = chunk->res[i]->vnic_idx;
155
156         default_flow = list_first_entry(&qp_grp->flows_lst,
157                                         struct usnic_ib_qp_grp_flow, link);
158         resp.transport = default_flow->trans_type;
159
160         err = ib_copy_to_udata(udata, &resp, sizeof(resp));
161         if (err) {
162                 usnic_err("Failed to copy udata for %s", us_ibdev->ib_dev.name);
163                 return err;
164         }
165
166         return 0;
167 }
168
169 static struct usnic_ib_qp_grp*
170 find_free_vf_and_create_qp_grp(struct usnic_ib_dev *us_ibdev,
171                                 struct usnic_ib_pd *pd,
172                                 struct usnic_transport_spec *trans_spec,
173                                 struct usnic_vnic_res_spec *res_spec)
174 {
175         struct usnic_ib_vf *vf;
176         struct usnic_vnic *vnic;
177         struct usnic_ib_qp_grp *qp_grp;
178         struct device *dev, **dev_list;
179         int i;
180
181         BUG_ON(!mutex_is_locked(&us_ibdev->usdev_lock));
182
183         if (list_empty(&us_ibdev->vf_dev_list)) {
184                 usnic_info("No vfs to allocate\n");
185                 return NULL;
186         }
187
188         if (usnic_ib_share_vf) {
189                 /* Try to find resouces on a used vf which is in pd */
190                 dev_list = usnic_uiom_get_dev_list(pd->umem_pd);
191                 if (IS_ERR(dev_list))
192                         return ERR_CAST(dev_list);
193                 for (i = 0; dev_list[i]; i++) {
194                         dev = dev_list[i];
195                         vf = pci_get_drvdata(to_pci_dev(dev));
196                         spin_lock(&vf->lock);
197                         vnic = vf->vnic;
198                         if (!usnic_vnic_check_room(vnic, res_spec)) {
199                                 usnic_dbg("Found used vnic %s from %s\n",
200                                                 us_ibdev->ib_dev.name,
201                                                 pci_name(usnic_vnic_get_pdev(
202                                                                         vnic)));
203                                 qp_grp = usnic_ib_qp_grp_create(us_ibdev->ufdev,
204                                                                 vf, pd,
205                                                                 res_spec,
206                                                                 trans_spec);
207
208                                 spin_unlock(&vf->lock);
209                                 goto qp_grp_check;
210                         }
211                         spin_unlock(&vf->lock);
212
213                 }
214                 usnic_uiom_free_dev_list(dev_list);
215                 dev_list = NULL;
216         }
217
218         /* Try to find resources on an unused vf */
219         list_for_each_entry(vf, &us_ibdev->vf_dev_list, link) {
220                 spin_lock(&vf->lock);
221                 vnic = vf->vnic;
222                 if (vf->qp_grp_ref_cnt == 0 &&
223                     usnic_vnic_check_room(vnic, res_spec) == 0) {
224                         qp_grp = usnic_ib_qp_grp_create(us_ibdev->ufdev, vf,
225                                                         pd, res_spec,
226                                                         trans_spec);
227
228                         spin_unlock(&vf->lock);
229                         goto qp_grp_check;
230                 }
231                 spin_unlock(&vf->lock);
232         }
233
234         usnic_info("No free qp grp found on %s\n", us_ibdev->ib_dev.name);
235         return ERR_PTR(-ENOMEM);
236
237 qp_grp_check:
238         if (IS_ERR_OR_NULL(qp_grp)) {
239                 usnic_err("Failed to allocate qp_grp\n");
240                 if (usnic_ib_share_vf)
241                         usnic_uiom_free_dev_list(dev_list);
242                 return ERR_PTR(qp_grp ? PTR_ERR(qp_grp) : -ENOMEM);
243         }
244         return qp_grp;
245 }
246
247 static void qp_grp_destroy(struct usnic_ib_qp_grp *qp_grp)
248 {
249         struct usnic_ib_vf *vf = qp_grp->vf;
250
251         WARN_ON(qp_grp->state != IB_QPS_RESET);
252
253         spin_lock(&vf->lock);
254         usnic_ib_qp_grp_destroy(qp_grp);
255         spin_unlock(&vf->lock);
256 }
257
258 static int create_qp_validate_user_data(struct usnic_ib_create_qp_cmd cmd)
259 {
260         if (cmd.spec.trans_type <= USNIC_TRANSPORT_UNKNOWN ||
261                         cmd.spec.trans_type >= USNIC_TRANSPORT_MAX)
262                 return -EINVAL;
263
264         return 0;
265 }
266
267 /* Start of ib callback functions */
268
269 enum rdma_link_layer usnic_ib_port_link_layer(struct ib_device *device,
270                                                 u8 port_num)
271 {
272         return IB_LINK_LAYER_ETHERNET;
273 }
274
275 int usnic_ib_query_device(struct ib_device *ibdev,
276                           struct ib_device_attr *props,
277                           struct ib_udata *uhw)
278 {
279         struct usnic_ib_dev *us_ibdev = to_usdev(ibdev);
280         union ib_gid gid;
281         struct ethtool_drvinfo info;
282         int qp_per_vf;
283
284         usnic_dbg("\n");
285         if (uhw->inlen || uhw->outlen)
286                 return -EINVAL;
287
288         mutex_lock(&us_ibdev->usdev_lock);
289         us_ibdev->netdev->ethtool_ops->get_drvinfo(us_ibdev->netdev, &info);
290         memset(props, 0, sizeof(*props));
291         usnic_mac_ip_to_gid(us_ibdev->ufdev->mac, us_ibdev->ufdev->inaddr,
292                         &gid.raw[0]);
293         memcpy(&props->sys_image_guid, &gid.global.interface_id,
294                 sizeof(gid.global.interface_id));
295         usnic_ib_fw_string_to_u64(&info.fw_version[0], &props->fw_ver);
296         props->max_mr_size = USNIC_UIOM_MAX_MR_SIZE;
297         props->page_size_cap = USNIC_UIOM_PAGE_SIZE;
298         props->vendor_id = PCI_VENDOR_ID_CISCO;
299         props->vendor_part_id = PCI_DEVICE_ID_CISCO_VIC_USPACE_NIC;
300         props->hw_ver = us_ibdev->pdev->subsystem_device;
301         qp_per_vf = max(us_ibdev->vf_res_cnt[USNIC_VNIC_RES_TYPE_WQ],
302                         us_ibdev->vf_res_cnt[USNIC_VNIC_RES_TYPE_RQ]);
303         props->max_qp = qp_per_vf *
304                 kref_read(&us_ibdev->vf_cnt);
305         props->device_cap_flags = IB_DEVICE_PORT_ACTIVE_EVENT |
306                 IB_DEVICE_SYS_IMAGE_GUID | IB_DEVICE_BLOCK_MULTICAST_LOOPBACK;
307         props->max_cq = us_ibdev->vf_res_cnt[USNIC_VNIC_RES_TYPE_CQ] *
308                 kref_read(&us_ibdev->vf_cnt);
309         props->max_pd = USNIC_UIOM_MAX_PD_CNT;
310         props->max_mr = USNIC_UIOM_MAX_MR_CNT;
311         props->local_ca_ack_delay = 0;
312         props->max_pkeys = 0;
313         props->atomic_cap = IB_ATOMIC_NONE;
314         props->masked_atomic_cap = props->atomic_cap;
315         props->max_qp_rd_atom = 0;
316         props->max_qp_init_rd_atom = 0;
317         props->max_res_rd_atom = 0;
318         props->max_srq = 0;
319         props->max_srq_wr = 0;
320         props->max_srq_sge = 0;
321         props->max_fast_reg_page_list_len = 0;
322         props->max_mcast_grp = 0;
323         props->max_mcast_qp_attach = 0;
324         props->max_total_mcast_qp_attach = 0;
325         props->max_map_per_fmr = 0;
326         /* Owned by Userspace
327          * max_qp_wr, max_sge, max_sge_rd, max_cqe */
328         mutex_unlock(&us_ibdev->usdev_lock);
329
330         return 0;
331 }
332
333 int usnic_ib_query_port(struct ib_device *ibdev, u8 port,
334                                 struct ib_port_attr *props)
335 {
336         struct usnic_ib_dev *us_ibdev = to_usdev(ibdev);
337
338         usnic_dbg("\n");
339
340         if (ib_get_eth_speed(ibdev, port, &props->active_speed,
341                              &props->active_width))
342                 return -EINVAL;
343
344         /*
345          * usdev_lock is acquired after (and not before) ib_get_eth_speed call
346          * because acquiring rtnl_lock in ib_get_eth_speed, while holding
347          * usdev_lock could lead to a deadlock.
348          */
349         mutex_lock(&us_ibdev->usdev_lock);
350         /* props being zeroed by the caller, avoid zeroing it here */
351
352         props->lid = 0;
353         props->lmc = 1;
354         props->sm_lid = 0;
355         props->sm_sl = 0;
356
357         if (!us_ibdev->ufdev->link_up) {
358                 props->state = IB_PORT_DOWN;
359                 props->phys_state = 3;
360         } else if (!us_ibdev->ufdev->inaddr) {
361                 props->state = IB_PORT_INIT;
362                 props->phys_state = 4;
363         } else {
364                 props->state = IB_PORT_ACTIVE;
365                 props->phys_state = 5;
366         }
367
368         props->port_cap_flags = 0;
369         props->gid_tbl_len = 1;
370         props->pkey_tbl_len = 1;
371         props->bad_pkey_cntr = 0;
372         props->qkey_viol_cntr = 0;
373         props->max_mtu = IB_MTU_4096;
374         props->active_mtu = iboe_get_mtu(us_ibdev->ufdev->mtu);
375         /* Userspace will adjust for hdrs */
376         props->max_msg_sz = us_ibdev->ufdev->mtu;
377         props->max_vl_num = 1;
378         mutex_unlock(&us_ibdev->usdev_lock);
379
380         return 0;
381 }
382
383 int usnic_ib_query_qp(struct ib_qp *qp, struct ib_qp_attr *qp_attr,
384                                 int qp_attr_mask,
385                                 struct ib_qp_init_attr *qp_init_attr)
386 {
387         struct usnic_ib_qp_grp *qp_grp;
388         struct usnic_ib_vf *vf;
389         int err;
390
391         usnic_dbg("\n");
392
393         memset(qp_attr, 0, sizeof(*qp_attr));
394         memset(qp_init_attr, 0, sizeof(*qp_init_attr));
395
396         qp_grp = to_uqp_grp(qp);
397         vf = qp_grp->vf;
398         mutex_lock(&vf->pf->usdev_lock);
399         usnic_dbg("\n");
400         qp_attr->qp_state = qp_grp->state;
401         qp_attr->cur_qp_state = qp_grp->state;
402
403         switch (qp_grp->ibqp.qp_type) {
404         case IB_QPT_UD:
405                 qp_attr->qkey = 0;
406                 break;
407         default:
408                 usnic_err("Unexpected qp_type %d\n", qp_grp->ibqp.qp_type);
409                 err = -EINVAL;
410                 goto err_out;
411         }
412
413         mutex_unlock(&vf->pf->usdev_lock);
414         return 0;
415
416 err_out:
417         mutex_unlock(&vf->pf->usdev_lock);
418         return err;
419 }
420
421 int usnic_ib_query_gid(struct ib_device *ibdev, u8 port, int index,
422                                 union ib_gid *gid)
423 {
424
425         struct usnic_ib_dev *us_ibdev = to_usdev(ibdev);
426         usnic_dbg("\n");
427
428         if (index > 1)
429                 return -EINVAL;
430
431         mutex_lock(&us_ibdev->usdev_lock);
432         memset(&(gid->raw[0]), 0, sizeof(gid->raw));
433         usnic_mac_ip_to_gid(us_ibdev->ufdev->mac, us_ibdev->ufdev->inaddr,
434                         &gid->raw[0]);
435         mutex_unlock(&us_ibdev->usdev_lock);
436
437         return 0;
438 }
439
440 struct net_device *usnic_get_netdev(struct ib_device *device, u8 port_num)
441 {
442         struct usnic_ib_dev *us_ibdev = to_usdev(device);
443
444         if (us_ibdev->netdev)
445                 dev_hold(us_ibdev->netdev);
446
447         return us_ibdev->netdev;
448 }
449
450 int usnic_ib_query_pkey(struct ib_device *ibdev, u8 port, u16 index,
451                                 u16 *pkey)
452 {
453         if (index > 0)
454                 return -EINVAL;
455
456         *pkey = 0xffff;
457         return 0;
458 }
459
460 struct ib_pd *usnic_ib_alloc_pd(struct ib_device *ibdev,
461                                         struct ib_ucontext *context,
462                                         struct ib_udata *udata)
463 {
464         struct usnic_ib_pd *pd;
465         void *umem_pd;
466
467         usnic_dbg("\n");
468
469         pd = kzalloc(sizeof(*pd), GFP_KERNEL);
470         if (!pd)
471                 return ERR_PTR(-ENOMEM);
472
473         umem_pd = pd->umem_pd = usnic_uiom_alloc_pd();
474         if (IS_ERR_OR_NULL(umem_pd)) {
475                 kfree(pd);
476                 return ERR_PTR(umem_pd ? PTR_ERR(umem_pd) : -ENOMEM);
477         }
478
479         usnic_info("domain 0x%p allocated for context 0x%p and device %s\n",
480                         pd, context, ibdev->name);
481         return &pd->ibpd;
482 }
483
484 int usnic_ib_dealloc_pd(struct ib_pd *pd)
485 {
486         usnic_info("freeing domain 0x%p\n", pd);
487
488         usnic_uiom_dealloc_pd((to_upd(pd))->umem_pd);
489         kfree(pd);
490         return 0;
491 }
492
493 struct ib_qp *usnic_ib_create_qp(struct ib_pd *pd,
494                                         struct ib_qp_init_attr *init_attr,
495                                         struct ib_udata *udata)
496 {
497         int err;
498         struct usnic_ib_dev *us_ibdev;
499         struct usnic_ib_qp_grp *qp_grp;
500         struct usnic_ib_ucontext *ucontext;
501         int cq_cnt;
502         struct usnic_vnic_res_spec res_spec;
503         struct usnic_ib_create_qp_cmd cmd;
504         struct usnic_transport_spec trans_spec;
505
506         usnic_dbg("\n");
507
508         ucontext = to_uucontext(pd->uobject->context);
509         us_ibdev = to_usdev(pd->device);
510
511         if (init_attr->create_flags)
512                 return ERR_PTR(-EINVAL);
513
514         err = ib_copy_from_udata(&cmd, udata, sizeof(cmd));
515         if (err) {
516                 usnic_err("%s: cannot copy udata for create_qp\n",
517                                 us_ibdev->ib_dev.name);
518                 return ERR_PTR(-EINVAL);
519         }
520
521         err = create_qp_validate_user_data(cmd);
522         if (err) {
523                 usnic_err("%s: Failed to validate user data\n",
524                                 us_ibdev->ib_dev.name);
525                 return ERR_PTR(-EINVAL);
526         }
527
528         if (init_attr->qp_type != IB_QPT_UD) {
529                 usnic_err("%s asked to make a non-UD QP: %d\n",
530                                 us_ibdev->ib_dev.name, init_attr->qp_type);
531                 return ERR_PTR(-EINVAL);
532         }
533
534         trans_spec = cmd.spec;
535         mutex_lock(&us_ibdev->usdev_lock);
536         cq_cnt = (init_attr->send_cq == init_attr->recv_cq) ? 1 : 2;
537         res_spec = min_transport_spec[trans_spec.trans_type];
538         usnic_vnic_res_spec_update(&res_spec, USNIC_VNIC_RES_TYPE_CQ, cq_cnt);
539         qp_grp = find_free_vf_and_create_qp_grp(us_ibdev, to_upd(pd),
540                                                 &trans_spec,
541                                                 &res_spec);
542         if (IS_ERR_OR_NULL(qp_grp)) {
543                 err = qp_grp ? PTR_ERR(qp_grp) : -ENOMEM;
544                 goto out_release_mutex;
545         }
546
547         err = usnic_ib_fill_create_qp_resp(qp_grp, udata);
548         if (err) {
549                 err = -EBUSY;
550                 goto out_release_qp_grp;
551         }
552
553         qp_grp->ctx = ucontext;
554         list_add_tail(&qp_grp->link, &ucontext->qp_grp_list);
555         usnic_ib_log_vf(qp_grp->vf);
556         mutex_unlock(&us_ibdev->usdev_lock);
557         return &qp_grp->ibqp;
558
559 out_release_qp_grp:
560         qp_grp_destroy(qp_grp);
561 out_release_mutex:
562         mutex_unlock(&us_ibdev->usdev_lock);
563         return ERR_PTR(err);
564 }
565
566 int usnic_ib_destroy_qp(struct ib_qp *qp)
567 {
568         struct usnic_ib_qp_grp *qp_grp;
569         struct usnic_ib_vf *vf;
570
571         usnic_dbg("\n");
572
573         qp_grp = to_uqp_grp(qp);
574         vf = qp_grp->vf;
575         mutex_lock(&vf->pf->usdev_lock);
576         if (usnic_ib_qp_grp_modify(qp_grp, IB_QPS_RESET, NULL)) {
577                 usnic_err("Failed to move qp grp %u to reset\n",
578                                 qp_grp->grp_id);
579         }
580
581         list_del(&qp_grp->link);
582         qp_grp_destroy(qp_grp);
583         mutex_unlock(&vf->pf->usdev_lock);
584
585         return 0;
586 }
587
588 int usnic_ib_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
589                                 int attr_mask, struct ib_udata *udata)
590 {
591         struct usnic_ib_qp_grp *qp_grp;
592         int status;
593         usnic_dbg("\n");
594
595         qp_grp = to_uqp_grp(ibqp);
596
597         mutex_lock(&qp_grp->vf->pf->usdev_lock);
598         if ((attr_mask & IB_QP_PORT) && attr->port_num != 1) {
599                 /* usnic devices only have one port */
600                 status = -EINVAL;
601                 goto out_unlock;
602         }
603         if (attr_mask & IB_QP_STATE) {
604                 status = usnic_ib_qp_grp_modify(qp_grp, attr->qp_state, NULL);
605         } else {
606                 usnic_err("Unhandled request, attr_mask=0x%x\n", attr_mask);
607                 status = -EINVAL;
608         }
609
610 out_unlock:
611         mutex_unlock(&qp_grp->vf->pf->usdev_lock);
612         return status;
613 }
614
615 struct ib_cq *usnic_ib_create_cq(struct ib_device *ibdev,
616                                  const struct ib_cq_init_attr *attr,
617                                  struct ib_ucontext *context,
618                                  struct ib_udata *udata)
619 {
620         struct ib_cq *cq;
621
622         usnic_dbg("\n");
623         if (attr->flags)
624                 return ERR_PTR(-EINVAL);
625
626         cq = kzalloc(sizeof(*cq), GFP_KERNEL);
627         if (!cq)
628                 return ERR_PTR(-EBUSY);
629
630         return cq;
631 }
632
633 int usnic_ib_destroy_cq(struct ib_cq *cq)
634 {
635         usnic_dbg("\n");
636         kfree(cq);
637         return 0;
638 }
639
640 struct ib_mr *usnic_ib_reg_mr(struct ib_pd *pd, u64 start, u64 length,
641                                         u64 virt_addr, int access_flags,
642                                         struct ib_udata *udata)
643 {
644         struct usnic_ib_mr *mr;
645         int err;
646
647         usnic_dbg("start 0x%llx va 0x%llx length 0x%llx\n", start,
648                         virt_addr, length);
649
650         mr = kzalloc(sizeof(*mr), GFP_KERNEL);
651         if (!mr)
652                 return ERR_PTR(-ENOMEM);
653
654         mr->umem = usnic_uiom_reg_get(to_upd(pd)->umem_pd, start, length,
655                                         access_flags, 0);
656         if (IS_ERR_OR_NULL(mr->umem)) {
657                 err = mr->umem ? PTR_ERR(mr->umem) : -EFAULT;
658                 goto err_free;
659         }
660
661         mr->ibmr.lkey = mr->ibmr.rkey = 0;
662         return &mr->ibmr;
663
664 err_free:
665         kfree(mr);
666         return ERR_PTR(err);
667 }
668
669 int usnic_ib_dereg_mr(struct ib_mr *ibmr)
670 {
671         struct usnic_ib_mr *mr = to_umr(ibmr);
672
673         usnic_dbg("va 0x%lx length 0x%zx\n", mr->umem->va, mr->umem->length);
674
675         usnic_uiom_reg_release(mr->umem, ibmr->uobject->context);
676         kfree(mr);
677         return 0;
678 }
679
680 struct ib_ucontext *usnic_ib_alloc_ucontext(struct ib_device *ibdev,
681                                                         struct ib_udata *udata)
682 {
683         struct usnic_ib_ucontext *context;
684         struct usnic_ib_dev *us_ibdev = to_usdev(ibdev);
685         usnic_dbg("\n");
686
687         context = kmalloc(sizeof(*context), GFP_KERNEL);
688         if (!context)
689                 return ERR_PTR(-ENOMEM);
690
691         INIT_LIST_HEAD(&context->qp_grp_list);
692         mutex_lock(&us_ibdev->usdev_lock);
693         list_add_tail(&context->link, &us_ibdev->ctx_list);
694         mutex_unlock(&us_ibdev->usdev_lock);
695
696         return &context->ibucontext;
697 }
698
699 int usnic_ib_dealloc_ucontext(struct ib_ucontext *ibcontext)
700 {
701         struct usnic_ib_ucontext *context = to_uucontext(ibcontext);
702         struct usnic_ib_dev *us_ibdev = to_usdev(ibcontext->device);
703         usnic_dbg("\n");
704
705         mutex_lock(&us_ibdev->usdev_lock);
706         BUG_ON(!list_empty(&context->qp_grp_list));
707         list_del(&context->link);
708         mutex_unlock(&us_ibdev->usdev_lock);
709         kfree(context);
710         return 0;
711 }
712
713 int usnic_ib_mmap(struct ib_ucontext *context,
714                                 struct vm_area_struct *vma)
715 {
716         struct usnic_ib_ucontext *uctx = to_ucontext(context);
717         struct usnic_ib_dev *us_ibdev;
718         struct usnic_ib_qp_grp *qp_grp;
719         struct usnic_ib_vf *vf;
720         struct vnic_dev_bar *bar;
721         dma_addr_t bus_addr;
722         unsigned int len;
723         unsigned int vfid;
724
725         usnic_dbg("\n");
726
727         us_ibdev = to_usdev(context->device);
728         vma->vm_flags |= VM_IO;
729         vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
730         vfid = vma->vm_pgoff;
731         usnic_dbg("Page Offset %lu PAGE_SHIFT %u VFID %u\n",
732                         vma->vm_pgoff, PAGE_SHIFT, vfid);
733
734         mutex_lock(&us_ibdev->usdev_lock);
735         list_for_each_entry(qp_grp, &uctx->qp_grp_list, link) {
736                 vf = qp_grp->vf;
737                 if (usnic_vnic_get_index(vf->vnic) == vfid) {
738                         bar = usnic_vnic_get_bar(vf->vnic, 0);
739                         if ((vma->vm_end - vma->vm_start) != bar->len) {
740                                 usnic_err("Bar0 Len %lu - Request map %lu\n",
741                                                 bar->len,
742                                                 vma->vm_end - vma->vm_start);
743                                 mutex_unlock(&us_ibdev->usdev_lock);
744                                 return -EINVAL;
745                         }
746                         bus_addr = bar->bus_addr;
747                         len = bar->len;
748                         usnic_dbg("bus: %pa vaddr: %p size: %ld\n",
749                                         &bus_addr, bar->vaddr, bar->len);
750                         mutex_unlock(&us_ibdev->usdev_lock);
751
752                         return remap_pfn_range(vma,
753                                                 vma->vm_start,
754                                                 bus_addr >> PAGE_SHIFT,
755                                                 len, vma->vm_page_prot);
756                 }
757         }
758
759         mutex_unlock(&us_ibdev->usdev_lock);
760         usnic_err("No VF %u found\n", vfid);
761         return -EINVAL;
762 }
763
764 /* In ib callbacks section -  Start of stub funcs */
765 struct ib_ah *usnic_ib_create_ah(struct ib_pd *pd,
766                                  struct rdma_ah_attr *ah_attr,
767                                  struct ib_udata *udata)
768
769 {
770         usnic_dbg("\n");
771         return ERR_PTR(-EPERM);
772 }
773
774 int usnic_ib_destroy_ah(struct ib_ah *ah)
775 {
776         usnic_dbg("\n");
777         return -EINVAL;
778 }
779
780 int usnic_ib_post_send(struct ib_qp *ibqp, const struct ib_send_wr *wr,
781                        const struct ib_send_wr **bad_wr)
782 {
783         usnic_dbg("\n");
784         return -EINVAL;
785 }
786
787 int usnic_ib_post_recv(struct ib_qp *ibqp, const struct ib_recv_wr *wr,
788                        const struct ib_recv_wr **bad_wr)
789 {
790         usnic_dbg("\n");
791         return -EINVAL;
792 }
793
794 int usnic_ib_poll_cq(struct ib_cq *ibcq, int num_entries,
795                                 struct ib_wc *wc)
796 {
797         usnic_dbg("\n");
798         return -EINVAL;
799 }
800
801 int usnic_ib_req_notify_cq(struct ib_cq *cq,
802                                         enum ib_cq_notify_flags flags)
803 {
804         usnic_dbg("\n");
805         return -EINVAL;
806 }
807
808 struct ib_mr *usnic_ib_get_dma_mr(struct ib_pd *pd, int acc)
809 {
810         usnic_dbg("\n");
811         return ERR_PTR(-ENOMEM);
812 }
813
814
815 /* In ib callbacks section - End of stub funcs */
816 /* End of ib callbacks section */