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