| 1 | |
| 2 |
|
| 3 |
|
| 4 | #include <linux/module.h>
|
| 5 | #include <linux/printk.h>
|
| 6 | #include <linux/pci.h>
|
| 7 | #include <linux/irq.h>
|
| 8 | #include <net/addrconf.h>
|
| 9 | #include <rdma/ib_addr.h>
|
| 10 | #include <rdma/ib_mad.h>
|
| 11 |
|
| 12 | #include "ionic_ibdev.h"
|
| 13 |
|
| 14 | #define DRIVER_DESCRIPTION "AMD Pensando RoCE HCA driver"
|
| 15 | #define DEVICE_DESCRIPTION "AMD Pensando RoCE HCA"
|
| 16 |
|
| 17 | MODULE_AUTHOR("Allen Hubbe <[email protected]>");
|
| 18 | MODULE_DESCRIPTION(DRIVER_DESCRIPTION);
|
| 19 | MODULE_LICENSE("GPL");
|
| 20 | MODULE_IMPORT_NS("NET_IONIC");
|
| 21 |
|
| 22 | static int ionic_query_device(struct ib_device *ibdev,
|
| 23 | struct ib_device_attr *attr,
|
| 24 | struct ib_udata *udata)
|
| 25 | {
|
| 26 | struct ionic_ibdev *dev = to_ionic_ibdev(ibdev);
|
| 27 | struct net_device *ndev;
|
| 28 | int err;
|
| 29 |
|
| 30 | err = ib_no_udata_io(udata);
|
| 31 | if (err)
|
| 32 | return err;
|
| 33 |
|
| 34 | ndev = ib_device_get_netdev(ibdev, 1);
|
| 35 | addrconf_ifid_eui48((u8 *)&attr->sys_image_guid, ndev);
|
| 36 | dev_put(ndev);
|
| 37 | attr->max_mr_size = dev->lif_cfg.npts_per_lif * PAGE_SIZE / 2;
|
| 38 | attr->page_size_cap = dev->lif_cfg.page_size_supported;
|
| 39 |
|
| 40 | attr->vendor_id = to_pci_dev(dev->lif_cfg.hwdev)->vendor;
|
| 41 | attr->vendor_part_id = to_pci_dev(dev->lif_cfg.hwdev)->device;
|
| 42 |
|
| 43 | attr->hw_ver = ionic_lif_asic_rev(dev->lif_cfg.lif);
|
| 44 | attr->fw_ver = 0;
|
| 45 | attr->max_qp = dev->lif_cfg.qp_count;
|
| 46 | attr->max_qp_wr = IONIC_MAX_DEPTH;
|
| 47 | attr->device_cap_flags =
|
| 48 | IB_DEVICE_MEM_WINDOW |
|
| 49 | IB_DEVICE_MEM_MGT_EXTENSIONS |
|
| 50 | IB_DEVICE_MEM_WINDOW_TYPE_2B |
|
| 51 | 0;
|
| 52 | attr->max_send_sge =
|
| 53 | min(ionic_v1_send_wqe_max_sge(dev->lif_cfg.max_stride, 0, false),
|
| 54 | IONIC_SPEC_HIGH);
|
| 55 | attr->max_recv_sge =
|
| 56 | min(ionic_v1_recv_wqe_max_sge(dev->lif_cfg.max_stride, 0, false),
|
| 57 | IONIC_SPEC_HIGH);
|
| 58 | attr->max_sge_rd = attr->max_send_sge;
|
| 59 | attr->max_cq = dev->lif_cfg.cq_count / dev->lif_cfg.udma_count;
|
| 60 | attr->max_cqe = IONIC_MAX_CQ_DEPTH - IONIC_CQ_GRACE;
|
| 61 | attr->max_mr = dev->lif_cfg.nmrs_per_lif;
|
| 62 | attr->max_pd = IONIC_MAX_PD;
|
| 63 | attr->max_qp_rd_atom = IONIC_MAX_RD_ATOM;
|
| 64 | attr->max_ee_rd_atom = 0;
|
| 65 | attr->max_res_rd_atom = IONIC_MAX_RD_ATOM;
|
| 66 | attr->max_qp_init_rd_atom = IONIC_MAX_RD_ATOM;
|
| 67 | attr->max_ee_init_rd_atom = 0;
|
| 68 | attr->atomic_cap = IB_ATOMIC_GLOB;
|
| 69 | attr->masked_atomic_cap = IB_ATOMIC_GLOB;
|
| 70 | attr->max_mw = dev->lif_cfg.nmrs_per_lif;
|
| 71 | attr->max_mcast_grp = 0;
|
| 72 | attr->max_mcast_qp_attach = 0;
|
| 73 | attr->max_ah = dev->lif_cfg.nahs_per_lif;
|
| 74 | attr->max_fast_reg_page_list_len = dev->lif_cfg.npts_per_lif / 2;
|
| 75 | attr->max_pkeys = IONIC_PKEY_TBL_LEN;
|
| 76 |
|
| 77 | return 0;
|
| 78 | }
|
| 79 |
|
| 80 | static int ionic_query_port(struct ib_device *ibdev, u32 port,
|
| 81 | struct ib_port_attr *attr)
|
| 82 | {
|
| 83 | struct net_device *ndev;
|
| 84 |
|
| 85 | if (port != 1)
|
| 86 | return -EINVAL;
|
| 87 |
|
| 88 | ndev = ib_device_get_netdev(ibdev, port);
|
| 89 | if (!ndev)
|
| 90 | return -ENODEV;
|
| 91 |
|
| 92 | if (netif_running(ndev) && netif_carrier_ok(ndev)) {
|
| 93 | attr->state = IB_PORT_ACTIVE;
|
| 94 | attr->phys_state = IB_PORT_PHYS_STATE_LINK_UP;
|
| 95 | } else if (netif_running(ndev)) {
|
| 96 | attr->state = IB_PORT_DOWN;
|
| 97 | attr->phys_state = IB_PORT_PHYS_STATE_POLLING;
|
| 98 | } else {
|
| 99 | attr->state = IB_PORT_DOWN;
|
| 100 | attr->phys_state = IB_PORT_PHYS_STATE_DISABLED;
|
| 101 | }
|
| 102 |
|
| 103 | attr->max_mtu = iboe_get_mtu(ndev->max_mtu);
|
| 104 | attr->active_mtu = min(attr->max_mtu, iboe_get_mtu(ndev->mtu));
|
| 105 | attr->gid_tbl_len = IONIC_GID_TBL_LEN;
|
| 106 | attr->ip_gids = true;
|
| 107 | attr->port_cap_flags = 0;
|
| 108 | attr->max_msg_sz = 0x80000000;
|
| 109 | attr->pkey_tbl_len = IONIC_PKEY_TBL_LEN;
|
| 110 | attr->max_vl_num = 1;
|
| 111 | attr->subnet_prefix = 0xfe80000000000000ull;
|
| 112 |
|
| 113 | dev_put(ndev);
|
| 114 |
|
| 115 | return ib_get_eth_speed(ibdev, port,
|
| 116 | &attr->active_speed,
|
| 117 | &attr->active_width);
|
| 118 | }
|
| 119 |
|
| 120 | static enum rdma_link_layer ionic_get_link_layer(struct ib_device *ibdev,
|
| 121 | u32 port)
|
| 122 | {
|
| 123 | return IB_LINK_LAYER_ETHERNET;
|
| 124 | }
|
| 125 |
|
| 126 | static int ionic_query_pkey(struct ib_device *ibdev, u32 port, u16 index,
|
| 127 | u16 *pkey)
|
| 128 | {
|
| 129 | if (port != 1)
|
| 130 | return -EINVAL;
|
| 131 |
|
| 132 | if (index != 0)
|
| 133 | return -EINVAL;
|
| 134 |
|
| 135 | *pkey = IB_DEFAULT_PKEY_FULL;
|
| 136 |
|
| 137 | return 0;
|
| 138 | }
|
| 139 |
|
| 140 | static int ionic_modify_device(struct ib_device *ibdev, int mask,
|
| 141 | struct ib_device_modify *attr)
|
| 142 | {
|
| 143 | struct ionic_ibdev *dev = to_ionic_ibdev(ibdev);
|
| 144 |
|
| 145 | if (mask & ~IB_DEVICE_MODIFY_NODE_DESC)
|
| 146 | return -EOPNOTSUPP;
|
| 147 |
|
| 148 | if (mask & IB_DEVICE_MODIFY_NODE_DESC)
|
| 149 | memcpy(dev->ibdev.node_desc, attr->node_desc,
|
| 150 | IB_DEVICE_NODE_DESC_MAX);
|
| 151 |
|
| 152 | return 0;
|
| 153 | }
|
| 154 |
|
| 155 | static int ionic_get_port_immutable(struct ib_device *ibdev, u32 port,
|
| 156 | struct ib_port_immutable *attr)
|
| 157 | {
|
| 158 | if (port != 1)
|
| 159 | return -EINVAL;
|
| 160 |
|
| 161 | attr->core_cap_flags = RDMA_CORE_PORT_IBA_ROCE_UDP_ENCAP;
|
| 162 |
|
| 163 | attr->pkey_tbl_len = IONIC_PKEY_TBL_LEN;
|
| 164 | attr->gid_tbl_len = IONIC_GID_TBL_LEN;
|
| 165 | attr->max_mad_size = IB_MGMT_MAD_SIZE;
|
| 166 |
|
| 167 | return 0;
|
| 168 | }
|
| 169 |
|
| 170 | static void ionic_get_dev_fw_str(struct ib_device *ibdev, char *str)
|
| 171 | {
|
| 172 | struct ionic_ibdev *dev = to_ionic_ibdev(ibdev);
|
| 173 |
|
| 174 | ionic_lif_fw_version(dev->lif_cfg.lif, str, IB_FW_VERSION_NAME_MAX);
|
| 175 | }
|
| 176 |
|
| 177 | static ssize_t hw_rev_show(struct device *device, struct device_attribute *attr,
|
| 178 | char *buf)
|
| 179 | {
|
| 180 | struct ionic_ibdev *dev =
|
| 181 | rdma_device_to_drv_device(device, struct ionic_ibdev, ibdev);
|
| 182 |
|
| 183 | return sysfs_emit(buf, "0x%x\n", ionic_lif_asic_rev(dev->lif_cfg.lif));
|
| 184 | }
|
| 185 | static DEVICE_ATTR_RO(hw_rev);
|
| 186 |
|
| 187 | static ssize_t hca_type_show(struct device *device,
|
| 188 | struct device_attribute *attr, char *buf)
|
| 189 | {
|
| 190 | struct ionic_ibdev *dev =
|
| 191 | rdma_device_to_drv_device(device, struct ionic_ibdev, ibdev);
|
| 192 |
|
| 193 | return sysfs_emit(buf, "%.64s\n", dev->ibdev.node_desc);
|
| 194 | }
|
| 195 | static DEVICE_ATTR_RO(hca_type);
|
| 196 |
|
| 197 | static struct attribute *ionic_rdma_attributes[] = {
|
| 198 | &dev_attr_hw_rev.attr,
|
| 199 | &dev_attr_hca_type.attr,
|
| 200 | NULL
|
| 201 | };
|
| 202 |
|
| 203 | static const struct attribute_group ionic_rdma_attr_group = {
|
| 204 | .attrs = ionic_rdma_attributes,
|
| 205 | };
|
| 206 |
|
| 207 | static void ionic_disassociate_ucontext(struct ib_ucontext *ibcontext)
|
| 208 | {
|
| 209 | |
| 210 | * Dummy define disassociate_ucontext so that it does not
|
| 211 | * wait for user context before cleaning up hw resources.
|
| 212 |
|
| 213 | }
|
| 214 |
|
| 215 | static const struct ib_device_ops ionic_dev_ops = {
|
| 216 | .owner = THIS_MODULE,
|
| 217 | .driver_id = RDMA_DRIVER_IONIC,
|
| 218 | .uverbs_abi_ver = IONIC_ABI_VERSION,
|
| 219 | .uverbs_robust_udata = true,
|
| 220 |
|
| 221 | .alloc_ucontext = ionic_alloc_ucontext,
|
| 222 | .dealloc_ucontext = ionic_dealloc_ucontext,
|
| 223 | .mmap = ionic_mmap,
|
| 224 | .mmap_free = ionic_mmap_free,
|
| 225 | .alloc_pd = ionic_alloc_pd,
|
| 226 | .dealloc_pd = ionic_dealloc_pd,
|
| 227 | .create_ah = ionic_create_ah,
|
| 228 | .query_ah = ionic_query_ah,
|
| 229 | .destroy_ah = ionic_destroy_ah,
|
| 230 | .create_user_ah = ionic_create_ah,
|
| 231 | .get_dma_mr = ionic_get_dma_mr,
|
| 232 | .reg_user_mr = ionic_reg_user_mr,
|
| 233 | .reg_user_mr_dmabuf = ionic_reg_user_mr_dmabuf,
|
| 234 | .dereg_mr = ionic_dereg_mr,
|
| 235 | .alloc_mr = ionic_alloc_mr,
|
| 236 | .map_mr_sg = ionic_map_mr_sg,
|
| 237 | .alloc_mw = ionic_alloc_mw,
|
| 238 | .dealloc_mw = ionic_dealloc_mw,
|
| 239 | .create_cq = ionic_create_cq,
|
| 240 | .destroy_cq = ionic_destroy_cq,
|
| 241 | .create_qp = ionic_create_qp,
|
| 242 | .modify_qp = ionic_modify_qp,
|
| 243 | .query_qp = ionic_query_qp,
|
| 244 | .destroy_qp = ionic_destroy_qp,
|
| 245 |
|
| 246 | .post_send = ionic_post_send,
|
| 247 | .post_recv = ionic_post_recv,
|
| 248 | .poll_cq = ionic_poll_cq,
|
| 249 | .req_notify_cq = ionic_req_notify_cq,
|
| 250 |
|
| 251 | .query_device = ionic_query_device,
|
| 252 | .query_port = ionic_query_port,
|
| 253 | .get_link_layer = ionic_get_link_layer,
|
| 254 | .query_pkey = ionic_query_pkey,
|
| 255 | .modify_device = ionic_modify_device,
|
| 256 | .get_port_immutable = ionic_get_port_immutable,
|
| 257 | .get_dev_fw_str = ionic_get_dev_fw_str,
|
| 258 | .device_group = &ionic_rdma_attr_group,
|
| 259 | .disassociate_ucontext = ionic_disassociate_ucontext,
|
| 260 |
|
| 261 | INIT_RDMA_OBJ_SIZE(ib_ucontext, ionic_ctx, ibctx),
|
| 262 | INIT_RDMA_OBJ_SIZE(ib_pd, ionic_pd, ibpd),
|
| 263 | INIT_RDMA_OBJ_SIZE(ib_ah, ionic_ah, ibah),
|
| 264 | INIT_RDMA_OBJ_SIZE(ib_cq, ionic_vcq, ibcq),
|
| 265 | INIT_RDMA_OBJ_SIZE(ib_qp, ionic_qp, ibqp),
|
| 266 | INIT_RDMA_OBJ_SIZE(ib_mw, ionic_mr, ibmw),
|
| 267 | };
|
| 268 |
|
| 269 | static void ionic_init_resids(struct ionic_ibdev *dev)
|
| 270 | {
|
| 271 | ionic_resid_init(&dev->inuse_cqid, dev->lif_cfg.cq_count);
|
| 272 | dev->half_cqid_udma_shift =
|
| 273 | order_base_2(dev->lif_cfg.cq_count / dev->lif_cfg.udma_count);
|
| 274 | ionic_resid_init(&dev->inuse_pdid, IONIC_MAX_PD);
|
| 275 | ionic_resid_init(&dev->inuse_ahid, dev->lif_cfg.nahs_per_lif);
|
| 276 | ionic_resid_init(&dev->inuse_mrid, dev->lif_cfg.nmrs_per_lif);
|
| 277 |
|
| 278 | dev->next_mrkey = 1;
|
| 279 | ionic_resid_init(&dev->inuse_qpid, dev->lif_cfg.qp_count);
|
| 280 |
|
| 281 | dev->half_qpid_udma_shift =
|
| 282 | order_base_2(dev->lif_cfg.qp_count / dev->lif_cfg.udma_count);
|
| 283 | ionic_resid_init(&dev->inuse_dbid, dev->lif_cfg.dbid_count);
|
| 284 | }
|
| 285 |
|
| 286 | static void ionic_destroy_resids(struct ionic_ibdev *dev)
|
| 287 | {
|
| 288 | ionic_resid_destroy(&dev->inuse_cqid);
|
| 289 | ionic_resid_destroy(&dev->inuse_pdid);
|
| 290 | ionic_resid_destroy(&dev->inuse_ahid);
|
| 291 | ionic_resid_destroy(&dev->inuse_mrid);
|
| 292 | ionic_resid_destroy(&dev->inuse_qpid);
|
| 293 | ionic_resid_destroy(&dev->inuse_dbid);
|
| 294 | }
|
| 295 |
|
| 296 | static void ionic_destroy_ibdev(struct ionic_ibdev *dev)
|
| 297 | {
|
| 298 | ionic_kill_rdma_admin(dev, false);
|
| 299 | ib_unregister_device(&dev->ibdev);
|
| 300 | ionic_stats_cleanup(dev);
|
| 301 | ionic_destroy_rdma_admin(dev);
|
| 302 | ionic_destroy_resids(dev);
|
| 303 | WARN_ON(!xa_empty(&dev->qp_tbl));
|
| 304 | xa_destroy(&dev->qp_tbl);
|
| 305 | WARN_ON(!xa_empty(&dev->cq_tbl));
|
| 306 | xa_destroy(&dev->cq_tbl);
|
| 307 | ib_dealloc_device(&dev->ibdev);
|
| 308 | }
|
| 309 |
|
| 310 | static struct ionic_ibdev *ionic_create_ibdev(struct ionic_aux_dev *ionic_adev)
|
| 311 | {
|
| 312 | struct ib_device *ibdev;
|
| 313 | struct ionic_ibdev *dev;
|
| 314 | struct net_device *ndev;
|
| 315 | int rc;
|
| 316 |
|
| 317 | dev = ib_alloc_device(ionic_ibdev, ibdev);
|
| 318 | if (!dev)
|
| 319 | return ERR_PTR(-EINVAL);
|
| 320 |
|
| 321 | ionic_fill_lif_cfg(ionic_adev->lif, &dev->lif_cfg);
|
| 322 |
|
| 323 | xa_init_flags(&dev->qp_tbl, GFP_ATOMIC);
|
| 324 | xa_init_flags(&dev->cq_tbl, GFP_ATOMIC);
|
| 325 |
|
| 326 | ionic_init_resids(dev);
|
| 327 |
|
| 328 | rc = ionic_rdma_reset_devcmd(dev);
|
| 329 | if (rc)
|
| 330 | goto err_reset;
|
| 331 |
|
| 332 | rc = ionic_create_rdma_admin(dev);
|
| 333 | if (rc)
|
| 334 | goto err_admin;
|
| 335 |
|
| 336 | ibdev = &dev->ibdev;
|
| 337 | ibdev->dev.parent = dev->lif_cfg.hwdev;
|
| 338 |
|
| 339 | strscpy(ibdev->name, "ionic_%d", IB_DEVICE_NAME_MAX);
|
| 340 | strscpy(ibdev->node_desc, DEVICE_DESCRIPTION, IB_DEVICE_NODE_DESC_MAX);
|
| 341 |
|
| 342 | ibdev->node_type = RDMA_NODE_IB_CA;
|
| 343 | ibdev->phys_port_cnt = 1;
|
| 344 |
|
| 345 |
|
| 346 | ibdev->num_comp_vectors = dev->lif_cfg.eq_count - 2;
|
| 347 |
|
| 348 | ndev = ionic_lif_netdev(ionic_adev->lif);
|
| 349 | addrconf_ifid_eui48((u8 *)&ibdev->node_guid, ndev);
|
| 350 | rc = ib_device_set_netdev(ibdev, ndev, 1);
|
| 351 |
|
| 352 | dev_put(ndev);
|
| 353 | if (rc)
|
| 354 | goto err_admin;
|
| 355 |
|
| 356 | ib_set_device_ops(&dev->ibdev, &ionic_dev_ops);
|
| 357 |
|
| 358 | ionic_stats_init(dev);
|
| 359 |
|
| 360 | rc = ib_register_device(ibdev, "ionic_%d", ibdev->dev.parent);
|
| 361 | if (rc)
|
| 362 | goto err_register;
|
| 363 |
|
| 364 | return dev;
|
| 365 |
|
| 366 | err_register:
|
| 367 | ionic_stats_cleanup(dev);
|
| 368 | err_admin:
|
| 369 | ionic_kill_rdma_admin(dev, false);
|
| 370 | ionic_destroy_rdma_admin(dev);
|
| 371 | err_reset:
|
| 372 | ionic_destroy_resids(dev);
|
| 373 | xa_destroy(&dev->qp_tbl);
|
| 374 | xa_destroy(&dev->cq_tbl);
|
| 375 | ib_dealloc_device(&dev->ibdev);
|
| 376 |
|
| 377 | return ERR_PTR(rc);
|
| 378 | }
|
| 379 |
|
| 380 | static int ionic_aux_probe(struct auxiliary_device *adev,
|
| 381 | const struct auxiliary_device_id *id)
|
| 382 | {
|
| 383 | struct ionic_aux_dev *ionic_adev;
|
| 384 | struct ionic_ibdev *dev;
|
| 385 |
|
| 386 | ionic_adev = container_of(adev, struct ionic_aux_dev, adev);
|
| 387 | dev = ionic_create_ibdev(ionic_adev);
|
| 388 | if (IS_ERR(dev))
|
| 389 | return dev_err_probe(&adev->dev, PTR_ERR(dev),
|
| 390 | "Failed to register ibdev\n");
|
| 391 |
|
| 392 | auxiliary_set_drvdata(adev, dev);
|
| 393 | ibdev_dbg(&dev->ibdev, "registered\n");
|
| 394 |
|
| 395 | return 0;
|
| 396 | }
|
| 397 |
|
| 398 | static void ionic_aux_remove(struct auxiliary_device *adev)
|
| 399 | {
|
| 400 | struct ionic_ibdev *dev = auxiliary_get_drvdata(adev);
|
| 401 |
|
| 402 | dev_dbg(&adev->dev, "unregister ibdev\n");
|
| 403 | ionic_destroy_ibdev(dev);
|
| 404 | dev_dbg(&adev->dev, "unregistered\n");
|
| 405 | }
|
| 406 |
|
| 407 | static const struct auxiliary_device_id ionic_aux_id_table[] = {
|
| 408 | { .name = "ionic.rdma", },
|
| 409 | {},
|
| 410 | };
|
| 411 |
|
| 412 | MODULE_DEVICE_TABLE(auxiliary, ionic_aux_id_table);
|
| 413 |
|
| 414 | static struct auxiliary_driver ionic_aux_r_driver = {
|
| 415 | .name = "rdma",
|
| 416 | .probe = ionic_aux_probe,
|
| 417 | .remove = ionic_aux_remove,
|
| 418 | .id_table = ionic_aux_id_table,
|
| 419 | };
|
| 420 |
|
| 421 | static int __init ionic_mod_init(void)
|
| 422 | {
|
| 423 | int rc;
|
| 424 |
|
| 425 | ionic_evt_workq = create_workqueue(KBUILD_MODNAME "-evt");
|
| 426 | if (!ionic_evt_workq)
|
| 427 | return -ENOMEM;
|
| 428 |
|
| 429 | rc = auxiliary_driver_register(&ionic_aux_r_driver);
|
| 430 | if (rc)
|
| 431 | goto err_aux;
|
| 432 |
|
| 433 | return 0;
|
| 434 |
|
| 435 | err_aux:
|
| 436 | destroy_workqueue(ionic_evt_workq);
|
| 437 |
|
| 438 | return rc;
|
| 439 | }
|
| 440 |
|
| 441 | static void __exit ionic_mod_exit(void)
|
| 442 | {
|
| 443 | auxiliary_driver_unregister(&ionic_aux_r_driver);
|
| 444 | destroy_workqueue(ionic_evt_workq);
|
| 445 | }
|
| 446 |
|
| 447 | module_init(ionic_mod_init);
|
| 448 | module_exit(ionic_mod_exit);
|