12e0cbc4dSRam Amrani /* QLogic qedr NIC Driver 22e0cbc4dSRam Amrani * Copyright (c) 2015-2016 QLogic Corporation 32e0cbc4dSRam Amrani * 42e0cbc4dSRam Amrani * This software is available to you under a choice of one of two 52e0cbc4dSRam Amrani * licenses. You may choose to be licensed under the terms of the GNU 62e0cbc4dSRam Amrani * General Public License (GPL) Version 2, available from the file 72e0cbc4dSRam Amrani * COPYING in the main directory of this source tree, or the 82e0cbc4dSRam Amrani * OpenIB.org BSD license below: 92e0cbc4dSRam Amrani * 102e0cbc4dSRam Amrani * Redistribution and use in source and binary forms, with or 112e0cbc4dSRam Amrani * without modification, are permitted provided that the following 122e0cbc4dSRam Amrani * conditions are met: 132e0cbc4dSRam Amrani * 142e0cbc4dSRam Amrani * - Redistributions of source code must retain the above 152e0cbc4dSRam Amrani * copyright notice, this list of conditions and the following 162e0cbc4dSRam Amrani * disclaimer. 172e0cbc4dSRam Amrani * 182e0cbc4dSRam Amrani * - Redistributions in binary form must reproduce the above 192e0cbc4dSRam Amrani * copyright notice, this list of conditions and the following 202e0cbc4dSRam Amrani * disclaimer in the documentation and /or other materials 212e0cbc4dSRam Amrani * provided with the distribution. 222e0cbc4dSRam Amrani * 232e0cbc4dSRam Amrani * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 242e0cbc4dSRam Amrani * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 252e0cbc4dSRam Amrani * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 262e0cbc4dSRam Amrani * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 272e0cbc4dSRam Amrani * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 282e0cbc4dSRam Amrani * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 292e0cbc4dSRam Amrani * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 302e0cbc4dSRam Amrani * SOFTWARE. 312e0cbc4dSRam Amrani */ 322e0cbc4dSRam Amrani #include <linux/module.h> 332e0cbc4dSRam Amrani #include <rdma/ib_verbs.h> 342e0cbc4dSRam Amrani #include <rdma/ib_addr.h> 35ac1b36e5SRam Amrani #include <rdma/ib_user_verbs.h> 36e6a38c54SKalderon, Michal #include <rdma/iw_cm.h> 37e6a38c54SKalderon, Michal #include <rdma/ib_mad.h> 382e0cbc4dSRam Amrani #include <linux/netdevice.h> 392e0cbc4dSRam Amrani #include <linux/iommu.h> 40461a6946SJoerg Roedel #include <linux/pci.h> 412e0cbc4dSRam Amrani #include <net/addrconf.h> 42de0089e6SKalderon, Michal #include <linux/idr.h> 43b262a06eSMichal Kalderon 44ec72fce4SRam Amrani #include <linux/qed/qed_chain.h> 45ec72fce4SRam Amrani #include <linux/qed/qed_if.h> 462e0cbc4dSRam Amrani #include "qedr.h" 47ac1b36e5SRam Amrani #include "verbs.h" 48ac1b36e5SRam Amrani #include <rdma/qedr-abi.h> 49de0089e6SKalderon, Michal #include "qedr_iw_cm.h" 502e0cbc4dSRam Amrani 512e0cbc4dSRam Amrani MODULE_DESCRIPTION("QLogic 40G/100G ROCE Driver"); 522e0cbc4dSRam Amrani MODULE_AUTHOR("QLogic Corporation"); 532e0cbc4dSRam Amrani MODULE_LICENSE("Dual BSD/GPL"); 542e0cbc4dSRam Amrani MODULE_VERSION(QEDR_MODULE_VERSION); 552e0cbc4dSRam Amrani 56cecbcddfSRam Amrani #define QEDR_WQ_MULTIPLIER_DFT (3) 57cecbcddfSRam Amrani 582e0cbc4dSRam Amrani void qedr_ib_dispatch_event(struct qedr_dev *dev, u8 port_num, 592e0cbc4dSRam Amrani enum ib_event_type type) 602e0cbc4dSRam Amrani { 612e0cbc4dSRam Amrani struct ib_event ibev; 622e0cbc4dSRam Amrani 632e0cbc4dSRam Amrani ibev.device = &dev->ibdev; 642e0cbc4dSRam Amrani ibev.element.port_num = port_num; 652e0cbc4dSRam Amrani ibev.event = type; 662e0cbc4dSRam Amrani 672e0cbc4dSRam Amrani ib_dispatch_event(&ibev); 682e0cbc4dSRam Amrani } 692e0cbc4dSRam Amrani 702e0cbc4dSRam Amrani static enum rdma_link_layer qedr_link_layer(struct ib_device *device, 712e0cbc4dSRam Amrani u8 port_num) 722e0cbc4dSRam Amrani { 732e0cbc4dSRam Amrani return IB_LINK_LAYER_ETHERNET; 742e0cbc4dSRam Amrani } 752e0cbc4dSRam Amrani 76ec72fce4SRam Amrani static void qedr_get_dev_fw_str(struct ib_device *ibdev, char *str, 77ec72fce4SRam Amrani size_t str_len) 78ec72fce4SRam Amrani { 79ec72fce4SRam Amrani struct qedr_dev *qedr = get_qedr_dev(ibdev); 80ec72fce4SRam Amrani u32 fw_ver = (u32)qedr->attr.fw_ver; 81ec72fce4SRam Amrani 82ec72fce4SRam Amrani snprintf(str, str_len, "%d. %d. %d. %d", 83ec72fce4SRam Amrani (fw_ver >> 24) & 0xFF, (fw_ver >> 16) & 0xFF, 84ec72fce4SRam Amrani (fw_ver >> 8) & 0xFF, fw_ver & 0xFF); 85ec72fce4SRam Amrani } 86ec72fce4SRam Amrani 87993d1b52SRam Amrani static struct net_device *qedr_get_netdev(struct ib_device *dev, u8 port_num) 88993d1b52SRam Amrani { 89993d1b52SRam Amrani struct qedr_dev *qdev; 90993d1b52SRam Amrani 91993d1b52SRam Amrani qdev = get_qedr_dev(dev); 92993d1b52SRam Amrani dev_hold(qdev->ndev); 93993d1b52SRam Amrani 94993d1b52SRam Amrani /* The HW vendor's device driver must guarantee 95993d1b52SRam Amrani * that this function returns NULL before the net device reaches 96993d1b52SRam Amrani * NETDEV_UNREGISTER_FINAL state. 97993d1b52SRam Amrani */ 98993d1b52SRam Amrani return qdev->ndev; 99993d1b52SRam Amrani } 100993d1b52SRam Amrani 101e6a38c54SKalderon, Michal int qedr_roce_port_immutable(struct ib_device *ibdev, u8 port_num, 102e6a38c54SKalderon, Michal struct ib_port_immutable *immutable) 103e6a38c54SKalderon, Michal { 104e6a38c54SKalderon, Michal struct ib_port_attr attr; 105e6a38c54SKalderon, Michal int err; 106e6a38c54SKalderon, Michal 107e6a38c54SKalderon, Michal err = qedr_query_port(ibdev, port_num, &attr); 108e6a38c54SKalderon, Michal if (err) 109e6a38c54SKalderon, Michal return err; 110e6a38c54SKalderon, Michal 111e6a38c54SKalderon, Michal immutable->pkey_tbl_len = attr.pkey_tbl_len; 112e6a38c54SKalderon, Michal immutable->gid_tbl_len = attr.gid_tbl_len; 113e6a38c54SKalderon, Michal immutable->core_cap_flags = RDMA_CORE_PORT_IBA_ROCE | 114e6a38c54SKalderon, Michal RDMA_CORE_PORT_IBA_ROCE_UDP_ENCAP; 115e6a38c54SKalderon, Michal immutable->max_mad_size = IB_MGMT_MAD_SIZE; 116e6a38c54SKalderon, Michal 117e6a38c54SKalderon, Michal return 0; 118e6a38c54SKalderon, Michal } 119e6a38c54SKalderon, Michal 120e6a38c54SKalderon, Michal int qedr_iw_port_immutable(struct ib_device *ibdev, u8 port_num, 121e6a38c54SKalderon, Michal struct ib_port_immutable *immutable) 122e6a38c54SKalderon, Michal { 123e6a38c54SKalderon, Michal struct ib_port_attr attr; 124e6a38c54SKalderon, Michal int err; 125e6a38c54SKalderon, Michal 126e6a38c54SKalderon, Michal err = qedr_query_port(ibdev, port_num, &attr); 127e6a38c54SKalderon, Michal if (err) 128e6a38c54SKalderon, Michal return err; 129e6a38c54SKalderon, Michal 130e6a38c54SKalderon, Michal immutable->pkey_tbl_len = 1; 131e6a38c54SKalderon, Michal immutable->gid_tbl_len = 1; 132e6a38c54SKalderon, Michal immutable->core_cap_flags = RDMA_CORE_PORT_IWARP; 133e6a38c54SKalderon, Michal immutable->max_mad_size = 0; 134e6a38c54SKalderon, Michal 135e6a38c54SKalderon, Michal return 0; 136e6a38c54SKalderon, Michal } 137e6a38c54SKalderon, Michal 138e6a38c54SKalderon, Michal int qedr_iw_register_device(struct qedr_dev *dev) 139e6a38c54SKalderon, Michal { 140e6a38c54SKalderon, Michal dev->ibdev.node_type = RDMA_NODE_RNIC; 141e6a38c54SKalderon, Michal dev->ibdev.query_gid = qedr_iw_query_gid; 142e6a38c54SKalderon, Michal 143e6a38c54SKalderon, Michal dev->ibdev.get_port_immutable = qedr_iw_port_immutable; 144e6a38c54SKalderon, Michal 145e6a38c54SKalderon, Michal dev->ibdev.iwcm = kzalloc(sizeof(*dev->ibdev.iwcm), GFP_KERNEL); 146e6a38c54SKalderon, Michal if (!dev->ibdev.iwcm) 147e6a38c54SKalderon, Michal return -ENOMEM; 148*e411e058SKalderon, Michal 149*e411e058SKalderon, Michal dev->ibdev.iwcm->connect = qedr_iw_connect; 150*e411e058SKalderon, Michal dev->ibdev.iwcm->accept = qedr_iw_accept; 151*e411e058SKalderon, Michal dev->ibdev.iwcm->reject = qedr_iw_reject; 152*e411e058SKalderon, Michal dev->ibdev.iwcm->create_listen = qedr_iw_create_listen; 153*e411e058SKalderon, Michal dev->ibdev.iwcm->destroy_listen = qedr_iw_destroy_listen; 154de0089e6SKalderon, Michal dev->ibdev.iwcm->add_ref = qedr_iw_qp_add_ref; 155de0089e6SKalderon, Michal dev->ibdev.iwcm->rem_ref = qedr_iw_qp_rem_ref; 156de0089e6SKalderon, Michal dev->ibdev.iwcm->get_qp = qedr_iw_get_qp; 157e6a38c54SKalderon, Michal 158e6a38c54SKalderon, Michal memcpy(dev->ibdev.iwcm->ifname, 159e6a38c54SKalderon, Michal dev->ndev->name, sizeof(dev->ibdev.iwcm->ifname)); 160e6a38c54SKalderon, Michal 161e6a38c54SKalderon, Michal return 0; 162e6a38c54SKalderon, Michal } 163e6a38c54SKalderon, Michal 164e6a38c54SKalderon, Michal void qedr_roce_register_device(struct qedr_dev *dev) 165e6a38c54SKalderon, Michal { 166e6a38c54SKalderon, Michal dev->ibdev.node_type = RDMA_NODE_IB_CA; 167e6a38c54SKalderon, Michal dev->ibdev.query_gid = qedr_query_gid; 168e6a38c54SKalderon, Michal 169e6a38c54SKalderon, Michal dev->ibdev.add_gid = qedr_add_gid; 170e6a38c54SKalderon, Michal dev->ibdev.del_gid = qedr_del_gid; 171e6a38c54SKalderon, Michal 172e6a38c54SKalderon, Michal dev->ibdev.get_port_immutable = qedr_roce_port_immutable; 173e6a38c54SKalderon, Michal } 174e6a38c54SKalderon, Michal 1752e0cbc4dSRam Amrani static int qedr_register_device(struct qedr_dev *dev) 1762e0cbc4dSRam Amrani { 177e6a38c54SKalderon, Michal int rc; 178e6a38c54SKalderon, Michal 1792e0cbc4dSRam Amrani strlcpy(dev->ibdev.name, "qedr%d", IB_DEVICE_NAME_MAX); 1802e0cbc4dSRam Amrani 181993d1b52SRam Amrani dev->ibdev.node_guid = dev->attr.node_guid; 1822e0cbc4dSRam Amrani memcpy(dev->ibdev.node_desc, QEDR_NODE_DESC, sizeof(QEDR_NODE_DESC)); 1832e0cbc4dSRam Amrani dev->ibdev.owner = THIS_MODULE; 184ac1b36e5SRam Amrani dev->ibdev.uverbs_abi_ver = QEDR_ABI_VERSION; 185ac1b36e5SRam Amrani 186ac1b36e5SRam Amrani dev->ibdev.uverbs_cmd_mask = QEDR_UVERBS(GET_CONTEXT) | 187ac1b36e5SRam Amrani QEDR_UVERBS(QUERY_DEVICE) | 188a7efd777SRam Amrani QEDR_UVERBS(QUERY_PORT) | 189a7efd777SRam Amrani QEDR_UVERBS(ALLOC_PD) | 190a7efd777SRam Amrani QEDR_UVERBS(DEALLOC_PD) | 191a7efd777SRam Amrani QEDR_UVERBS(CREATE_COMP_CHANNEL) | 192a7efd777SRam Amrani QEDR_UVERBS(CREATE_CQ) | 193a7efd777SRam Amrani QEDR_UVERBS(RESIZE_CQ) | 194a7efd777SRam Amrani QEDR_UVERBS(DESTROY_CQ) | 195cecbcddfSRam Amrani QEDR_UVERBS(REQ_NOTIFY_CQ) | 196cecbcddfSRam Amrani QEDR_UVERBS(CREATE_QP) | 197cecbcddfSRam Amrani QEDR_UVERBS(MODIFY_QP) | 198cecbcddfSRam Amrani QEDR_UVERBS(QUERY_QP) | 199e0290cceSRam Amrani QEDR_UVERBS(DESTROY_QP) | 200e0290cceSRam Amrani QEDR_UVERBS(REG_MR) | 201afa0e13bSRam Amrani QEDR_UVERBS(DEREG_MR) | 202afa0e13bSRam Amrani QEDR_UVERBS(POLL_CQ) | 203afa0e13bSRam Amrani QEDR_UVERBS(POST_SEND) | 204afa0e13bSRam Amrani QEDR_UVERBS(POST_RECV); 205ac1b36e5SRam Amrani 206e6a38c54SKalderon, Michal if (IS_IWARP(dev)) { 207e6a38c54SKalderon, Michal rc = qedr_iw_register_device(dev); 208e6a38c54SKalderon, Michal if (rc) 209e6a38c54SKalderon, Michal return rc; 210e6a38c54SKalderon, Michal } else { 211e6a38c54SKalderon, Michal qedr_roce_register_device(dev); 212e6a38c54SKalderon, Michal } 213e6a38c54SKalderon, Michal 214ac1b36e5SRam Amrani dev->ibdev.phys_port_cnt = 1; 215ac1b36e5SRam Amrani dev->ibdev.num_comp_vectors = dev->num_cnq; 216ac1b36e5SRam Amrani 217ac1b36e5SRam Amrani dev->ibdev.query_device = qedr_query_device; 218ac1b36e5SRam Amrani dev->ibdev.query_port = qedr_query_port; 219ac1b36e5SRam Amrani dev->ibdev.modify_port = qedr_modify_port; 220ac1b36e5SRam Amrani 221ac1b36e5SRam Amrani dev->ibdev.alloc_ucontext = qedr_alloc_ucontext; 222ac1b36e5SRam Amrani dev->ibdev.dealloc_ucontext = qedr_dealloc_ucontext; 223ac1b36e5SRam Amrani dev->ibdev.mmap = qedr_mmap; 224ac1b36e5SRam Amrani 225a7efd777SRam Amrani dev->ibdev.alloc_pd = qedr_alloc_pd; 226a7efd777SRam Amrani dev->ibdev.dealloc_pd = qedr_dealloc_pd; 227a7efd777SRam Amrani 228a7efd777SRam Amrani dev->ibdev.create_cq = qedr_create_cq; 229a7efd777SRam Amrani dev->ibdev.destroy_cq = qedr_destroy_cq; 230a7efd777SRam Amrani dev->ibdev.resize_cq = qedr_resize_cq; 231a7efd777SRam Amrani dev->ibdev.req_notify_cq = qedr_arm_cq; 232a7efd777SRam Amrani 233cecbcddfSRam Amrani dev->ibdev.create_qp = qedr_create_qp; 234cecbcddfSRam Amrani dev->ibdev.modify_qp = qedr_modify_qp; 235cecbcddfSRam Amrani dev->ibdev.query_qp = qedr_query_qp; 236cecbcddfSRam Amrani dev->ibdev.destroy_qp = qedr_destroy_qp; 237cecbcddfSRam Amrani 238a7efd777SRam Amrani dev->ibdev.query_pkey = qedr_query_pkey; 239a7efd777SRam Amrani 24004886779SRam Amrani dev->ibdev.create_ah = qedr_create_ah; 24104886779SRam Amrani dev->ibdev.destroy_ah = qedr_destroy_ah; 24204886779SRam Amrani 243e0290cceSRam Amrani dev->ibdev.get_dma_mr = qedr_get_dma_mr; 244e0290cceSRam Amrani dev->ibdev.dereg_mr = qedr_dereg_mr; 245e0290cceSRam Amrani dev->ibdev.reg_user_mr = qedr_reg_user_mr; 246e0290cceSRam Amrani dev->ibdev.alloc_mr = qedr_alloc_mr; 247e0290cceSRam Amrani dev->ibdev.map_mr_sg = qedr_map_mr_sg; 248e0290cceSRam Amrani 249afa0e13bSRam Amrani dev->ibdev.poll_cq = qedr_poll_cq; 250afa0e13bSRam Amrani dev->ibdev.post_send = qedr_post_send; 251afa0e13bSRam Amrani dev->ibdev.post_recv = qedr_post_recv; 252afa0e13bSRam Amrani 253993d1b52SRam Amrani dev->ibdev.process_mad = qedr_process_mad; 254e6a38c54SKalderon, Michal 255993d1b52SRam Amrani dev->ibdev.get_netdev = qedr_get_netdev; 256993d1b52SRam Amrani 25769117101SBart Van Assche dev->ibdev.dev.parent = &dev->pdev->dev; 2582e0cbc4dSRam Amrani 2592e0cbc4dSRam Amrani dev->ibdev.get_link_layer = qedr_link_layer; 260ec72fce4SRam Amrani dev->ibdev.get_dev_fw_str = qedr_get_dev_fw_str; 2612e0cbc4dSRam Amrani 262993d1b52SRam Amrani return ib_register_device(&dev->ibdev, NULL); 2632e0cbc4dSRam Amrani } 2642e0cbc4dSRam Amrani 265ec72fce4SRam Amrani /* This function allocates fast-path status block memory */ 266ec72fce4SRam Amrani static int qedr_alloc_mem_sb(struct qedr_dev *dev, 267ec72fce4SRam Amrani struct qed_sb_info *sb_info, u16 sb_id) 268ec72fce4SRam Amrani { 269ec72fce4SRam Amrani struct status_block *sb_virt; 270ec72fce4SRam Amrani dma_addr_t sb_phys; 271ec72fce4SRam Amrani int rc; 272ec72fce4SRam Amrani 273ec72fce4SRam Amrani sb_virt = dma_alloc_coherent(&dev->pdev->dev, 274ec72fce4SRam Amrani sizeof(*sb_virt), &sb_phys, GFP_KERNEL); 275ec72fce4SRam Amrani if (!sb_virt) 276ec72fce4SRam Amrani return -ENOMEM; 277ec72fce4SRam Amrani 278ec72fce4SRam Amrani rc = dev->ops->common->sb_init(dev->cdev, sb_info, 279ec72fce4SRam Amrani sb_virt, sb_phys, sb_id, 280ec72fce4SRam Amrani QED_SB_TYPE_CNQ); 281ec72fce4SRam Amrani if (rc) { 282ec72fce4SRam Amrani pr_err("Status block initialization failed\n"); 283ec72fce4SRam Amrani dma_free_coherent(&dev->pdev->dev, sizeof(*sb_virt), 284ec72fce4SRam Amrani sb_virt, sb_phys); 285ec72fce4SRam Amrani return rc; 286ec72fce4SRam Amrani } 287ec72fce4SRam Amrani 288ec72fce4SRam Amrani return 0; 289ec72fce4SRam Amrani } 290ec72fce4SRam Amrani 291ec72fce4SRam Amrani static void qedr_free_mem_sb(struct qedr_dev *dev, 292ec72fce4SRam Amrani struct qed_sb_info *sb_info, int sb_id) 293ec72fce4SRam Amrani { 294ec72fce4SRam Amrani if (sb_info->sb_virt) { 295ec72fce4SRam Amrani dev->ops->common->sb_release(dev->cdev, sb_info, sb_id); 296ec72fce4SRam Amrani dma_free_coherent(&dev->pdev->dev, sizeof(*sb_info->sb_virt), 297ec72fce4SRam Amrani (void *)sb_info->sb_virt, sb_info->sb_phys); 298ec72fce4SRam Amrani } 299ec72fce4SRam Amrani } 300ec72fce4SRam Amrani 301ec72fce4SRam Amrani static void qedr_free_resources(struct qedr_dev *dev) 302ec72fce4SRam Amrani { 303ec72fce4SRam Amrani int i; 304ec72fce4SRam Amrani 305*e411e058SKalderon, Michal if (IS_IWARP(dev)) 306*e411e058SKalderon, Michal destroy_workqueue(dev->iwarp_wq); 307*e411e058SKalderon, Michal 308ec72fce4SRam Amrani for (i = 0; i < dev->num_cnq; i++) { 309ec72fce4SRam Amrani qedr_free_mem_sb(dev, &dev->sb_array[i], dev->sb_start + i); 310ec72fce4SRam Amrani dev->ops->common->chain_free(dev->cdev, &dev->cnq_array[i].pbl); 311ec72fce4SRam Amrani } 312ec72fce4SRam Amrani 313ec72fce4SRam Amrani kfree(dev->cnq_array); 314ec72fce4SRam Amrani kfree(dev->sb_array); 315ec72fce4SRam Amrani kfree(dev->sgid_tbl); 316ec72fce4SRam Amrani } 317ec72fce4SRam Amrani 318ec72fce4SRam Amrani static int qedr_alloc_resources(struct qedr_dev *dev) 319ec72fce4SRam Amrani { 320ec72fce4SRam Amrani struct qedr_cnq *cnq; 321ec72fce4SRam Amrani __le16 *cons_pi; 322ec72fce4SRam Amrani u16 n_entries; 323ec72fce4SRam Amrani int i, rc; 324ec72fce4SRam Amrani 325ec72fce4SRam Amrani dev->sgid_tbl = kzalloc(sizeof(union ib_gid) * 326ec72fce4SRam Amrani QEDR_MAX_SGID, GFP_KERNEL); 327ec72fce4SRam Amrani if (!dev->sgid_tbl) 328ec72fce4SRam Amrani return -ENOMEM; 329ec72fce4SRam Amrani 330ec72fce4SRam Amrani spin_lock_init(&dev->sgid_lock); 331ec72fce4SRam Amrani 332de0089e6SKalderon, Michal if (IS_IWARP(dev)) { 333de0089e6SKalderon, Michal spin_lock_init(&dev->idr_lock); 334de0089e6SKalderon, Michal idr_init(&dev->qpidr); 335*e411e058SKalderon, Michal dev->iwarp_wq = create_singlethread_workqueue("qedr_iwarpq"); 336de0089e6SKalderon, Michal } 337de0089e6SKalderon, Michal 338ec72fce4SRam Amrani /* Allocate Status blocks for CNQ */ 339ec72fce4SRam Amrani dev->sb_array = kcalloc(dev->num_cnq, sizeof(*dev->sb_array), 340ec72fce4SRam Amrani GFP_KERNEL); 341ec72fce4SRam Amrani if (!dev->sb_array) { 342ec72fce4SRam Amrani rc = -ENOMEM; 343ec72fce4SRam Amrani goto err1; 344ec72fce4SRam Amrani } 345ec72fce4SRam Amrani 346ec72fce4SRam Amrani dev->cnq_array = kcalloc(dev->num_cnq, 347ec72fce4SRam Amrani sizeof(*dev->cnq_array), GFP_KERNEL); 348ec72fce4SRam Amrani if (!dev->cnq_array) { 349ec72fce4SRam Amrani rc = -ENOMEM; 350ec72fce4SRam Amrani goto err2; 351ec72fce4SRam Amrani } 352ec72fce4SRam Amrani 353ec72fce4SRam Amrani dev->sb_start = dev->ops->rdma_get_start_sb(dev->cdev); 354ec72fce4SRam Amrani 355ec72fce4SRam Amrani /* Allocate CNQ PBLs */ 356ec72fce4SRam Amrani n_entries = min_t(u32, QED_RDMA_MAX_CNQ_SIZE, QEDR_ROCE_MAX_CNQ_SIZE); 357ec72fce4SRam Amrani for (i = 0; i < dev->num_cnq; i++) { 358ec72fce4SRam Amrani cnq = &dev->cnq_array[i]; 359ec72fce4SRam Amrani 360ec72fce4SRam Amrani rc = qedr_alloc_mem_sb(dev, &dev->sb_array[i], 361ec72fce4SRam Amrani dev->sb_start + i); 362ec72fce4SRam Amrani if (rc) 363ec72fce4SRam Amrani goto err3; 364ec72fce4SRam Amrani 365ec72fce4SRam Amrani rc = dev->ops->common->chain_alloc(dev->cdev, 366ec72fce4SRam Amrani QED_CHAIN_USE_TO_CONSUME, 367ec72fce4SRam Amrani QED_CHAIN_MODE_PBL, 368ec72fce4SRam Amrani QED_CHAIN_CNT_TYPE_U16, 369ec72fce4SRam Amrani n_entries, 370ec72fce4SRam Amrani sizeof(struct regpair *), 3711a4a6975SMintz, Yuval &cnq->pbl, NULL); 372ec72fce4SRam Amrani if (rc) 373ec72fce4SRam Amrani goto err4; 374ec72fce4SRam Amrani 375ec72fce4SRam Amrani cnq->dev = dev; 376ec72fce4SRam Amrani cnq->sb = &dev->sb_array[i]; 377ec72fce4SRam Amrani cons_pi = dev->sb_array[i].sb_virt->pi_array; 378ec72fce4SRam Amrani cnq->hw_cons_ptr = &cons_pi[QED_ROCE_PROTOCOL_INDEX]; 379ec72fce4SRam Amrani cnq->index = i; 380ec72fce4SRam Amrani sprintf(cnq->name, "qedr%d@pci:%s", i, pci_name(dev->pdev)); 381ec72fce4SRam Amrani 382ec72fce4SRam Amrani DP_DEBUG(dev, QEDR_MSG_INIT, "cnq[%d].cons=%d\n", 383ec72fce4SRam Amrani i, qed_chain_get_cons_idx(&cnq->pbl)); 384ec72fce4SRam Amrani } 385ec72fce4SRam Amrani 386ec72fce4SRam Amrani return 0; 387ec72fce4SRam Amrani err4: 388ec72fce4SRam Amrani qedr_free_mem_sb(dev, &dev->sb_array[i], dev->sb_start + i); 389ec72fce4SRam Amrani err3: 390ec72fce4SRam Amrani for (--i; i >= 0; i--) { 391ec72fce4SRam Amrani dev->ops->common->chain_free(dev->cdev, &dev->cnq_array[i].pbl); 392ec72fce4SRam Amrani qedr_free_mem_sb(dev, &dev->sb_array[i], dev->sb_start + i); 393ec72fce4SRam Amrani } 394ec72fce4SRam Amrani kfree(dev->cnq_array); 395ec72fce4SRam Amrani err2: 396ec72fce4SRam Amrani kfree(dev->sb_array); 397ec72fce4SRam Amrani err1: 398ec72fce4SRam Amrani kfree(dev->sgid_tbl); 399ec72fce4SRam Amrani return rc; 400ec72fce4SRam Amrani } 401ec72fce4SRam Amrani 4022e0cbc4dSRam Amrani /* QEDR sysfs interface */ 4032e0cbc4dSRam Amrani static ssize_t show_rev(struct device *device, struct device_attribute *attr, 4042e0cbc4dSRam Amrani char *buf) 4052e0cbc4dSRam Amrani { 4062e0cbc4dSRam Amrani struct qedr_dev *dev = dev_get_drvdata(device); 4072e0cbc4dSRam Amrani 4082e0cbc4dSRam Amrani return scnprintf(buf, PAGE_SIZE, "0x%x\n", dev->pdev->vendor); 4092e0cbc4dSRam Amrani } 4102e0cbc4dSRam Amrani 4112e0cbc4dSRam Amrani static ssize_t show_hca_type(struct device *device, 4122e0cbc4dSRam Amrani struct device_attribute *attr, char *buf) 4132e0cbc4dSRam Amrani { 4142e0cbc4dSRam Amrani return scnprintf(buf, PAGE_SIZE, "%s\n", "HCA_TYPE_TO_SET"); 4152e0cbc4dSRam Amrani } 4162e0cbc4dSRam Amrani 4172e0cbc4dSRam Amrani static DEVICE_ATTR(hw_rev, S_IRUGO, show_rev, NULL); 4182e0cbc4dSRam Amrani static DEVICE_ATTR(hca_type, S_IRUGO, show_hca_type, NULL); 4192e0cbc4dSRam Amrani 4202e0cbc4dSRam Amrani static struct device_attribute *qedr_attributes[] = { 4212e0cbc4dSRam Amrani &dev_attr_hw_rev, 4222e0cbc4dSRam Amrani &dev_attr_hca_type 4232e0cbc4dSRam Amrani }; 4242e0cbc4dSRam Amrani 4252e0cbc4dSRam Amrani static void qedr_remove_sysfiles(struct qedr_dev *dev) 4262e0cbc4dSRam Amrani { 4272e0cbc4dSRam Amrani int i; 4282e0cbc4dSRam Amrani 4292e0cbc4dSRam Amrani for (i = 0; i < ARRAY_SIZE(qedr_attributes); i++) 4302e0cbc4dSRam Amrani device_remove_file(&dev->ibdev.dev, qedr_attributes[i]); 4312e0cbc4dSRam Amrani } 4322e0cbc4dSRam Amrani 4332e0cbc4dSRam Amrani static void qedr_pci_set_atomic(struct qedr_dev *dev, struct pci_dev *pdev) 4342e0cbc4dSRam Amrani { 4352e0cbc4dSRam Amrani struct pci_dev *bridge; 436f92faabaSAmrani, Ram u32 ctl2, cap2; 437f92faabaSAmrani, Ram u16 flags; 438f92faabaSAmrani, Ram int rc; 4392e0cbc4dSRam Amrani 4402e0cbc4dSRam Amrani bridge = pdev->bus->self; 4412e0cbc4dSRam Amrani if (!bridge) 442f92faabaSAmrani, Ram goto disable; 4432e0cbc4dSRam Amrani 444f92faabaSAmrani, Ram /* Check atomic routing support all the way to root complex */ 445f92faabaSAmrani, Ram while (bridge->bus->parent) { 446f92faabaSAmrani, Ram rc = pcie_capability_read_word(bridge, PCI_EXP_FLAGS, &flags); 447f92faabaSAmrani, Ram if (rc || ((flags & PCI_EXP_FLAGS_VERS) < 2)) 448f92faabaSAmrani, Ram goto disable; 449f92faabaSAmrani, Ram 450f92faabaSAmrani, Ram rc = pcie_capability_read_dword(bridge, PCI_EXP_DEVCAP2, &cap2); 451f92faabaSAmrani, Ram if (rc) 452f92faabaSAmrani, Ram goto disable; 453f92faabaSAmrani, Ram 454f92faabaSAmrani, Ram rc = pcie_capability_read_dword(bridge, PCI_EXP_DEVCTL2, &ctl2); 455f92faabaSAmrani, Ram if (rc) 456f92faabaSAmrani, Ram goto disable; 457f92faabaSAmrani, Ram 458f92faabaSAmrani, Ram if (!(cap2 & PCI_EXP_DEVCAP2_ATOMIC_ROUTE) || 459f92faabaSAmrani, Ram (ctl2 & PCI_EXP_DEVCTL2_ATOMIC_EGRESS_BLOCK)) 460f92faabaSAmrani, Ram goto disable; 4612e0cbc4dSRam Amrani bridge = bridge->bus->parent->self; 4622e0cbc4dSRam Amrani } 4632e0cbc4dSRam Amrani 464f92faabaSAmrani, Ram rc = pcie_capability_read_word(bridge, PCI_EXP_FLAGS, &flags); 465f92faabaSAmrani, Ram if (rc || ((flags & PCI_EXP_FLAGS_VERS) < 2)) 466f92faabaSAmrani, Ram goto disable; 467f92faabaSAmrani, Ram 468f92faabaSAmrani, Ram rc = pcie_capability_read_dword(bridge, PCI_EXP_DEVCAP2, &cap2); 469f92faabaSAmrani, Ram if (rc || !(cap2 & PCI_EXP_DEVCAP2_ATOMIC_COMP64)) 470f92faabaSAmrani, Ram goto disable; 471f92faabaSAmrani, Ram 472f92faabaSAmrani, Ram /* Set atomic operations */ 4732e0cbc4dSRam Amrani pcie_capability_set_word(pdev, PCI_EXP_DEVCTL2, 4742e0cbc4dSRam Amrani PCI_EXP_DEVCTL2_ATOMIC_REQ); 4752e0cbc4dSRam Amrani dev->atomic_cap = IB_ATOMIC_GLOB; 476f92faabaSAmrani, Ram 477f92faabaSAmrani, Ram DP_DEBUG(dev, QEDR_MSG_INIT, "Atomic capability enabled\n"); 478f92faabaSAmrani, Ram 479f92faabaSAmrani, Ram return; 480f92faabaSAmrani, Ram 481f92faabaSAmrani, Ram disable: 4822e0cbc4dSRam Amrani pcie_capability_clear_word(pdev, PCI_EXP_DEVCTL2, 4832e0cbc4dSRam Amrani PCI_EXP_DEVCTL2_ATOMIC_REQ); 484f92faabaSAmrani, Ram dev->atomic_cap = IB_ATOMIC_NONE; 485f92faabaSAmrani, Ram 486f92faabaSAmrani, Ram DP_DEBUG(dev, QEDR_MSG_INIT, "Atomic capability disabled\n"); 487f92faabaSAmrani, Ram 4882e0cbc4dSRam Amrani } 4892e0cbc4dSRam Amrani 490ec72fce4SRam Amrani static const struct qed_rdma_ops *qed_ops; 491ec72fce4SRam Amrani 492ec72fce4SRam Amrani #define HILO_U64(hi, lo) ((((u64)(hi)) << 32) + (lo)) 493ec72fce4SRam Amrani 494ec72fce4SRam Amrani static irqreturn_t qedr_irq_handler(int irq, void *handle) 495ec72fce4SRam Amrani { 496ec72fce4SRam Amrani u16 hw_comp_cons, sw_comp_cons; 497ec72fce4SRam Amrani struct qedr_cnq *cnq = handle; 498a7efd777SRam Amrani struct regpair *cq_handle; 499a7efd777SRam Amrani struct qedr_cq *cq; 500ec72fce4SRam Amrani 501ec72fce4SRam Amrani qed_sb_ack(cnq->sb, IGU_INT_DISABLE, 0); 502ec72fce4SRam Amrani 503ec72fce4SRam Amrani qed_sb_update_sb_idx(cnq->sb); 504ec72fce4SRam Amrani 505ec72fce4SRam Amrani hw_comp_cons = le16_to_cpu(*cnq->hw_cons_ptr); 506ec72fce4SRam Amrani sw_comp_cons = qed_chain_get_cons_idx(&cnq->pbl); 507ec72fce4SRam Amrani 508ec72fce4SRam Amrani /* Align protocol-index and chain reads */ 509ec72fce4SRam Amrani rmb(); 510ec72fce4SRam Amrani 511ec72fce4SRam Amrani while (sw_comp_cons != hw_comp_cons) { 512a7efd777SRam Amrani cq_handle = (struct regpair *)qed_chain_consume(&cnq->pbl); 513a7efd777SRam Amrani cq = (struct qedr_cq *)(uintptr_t)HILO_U64(cq_handle->hi, 514a7efd777SRam Amrani cq_handle->lo); 515a7efd777SRam Amrani 516a7efd777SRam Amrani if (cq == NULL) { 517a7efd777SRam Amrani DP_ERR(cnq->dev, 518a7efd777SRam Amrani "Received NULL CQ cq_handle->hi=%d cq_handle->lo=%d sw_comp_cons=%d hw_comp_cons=%d\n", 519a7efd777SRam Amrani cq_handle->hi, cq_handle->lo, sw_comp_cons, 520a7efd777SRam Amrani hw_comp_cons); 521a7efd777SRam Amrani 522a7efd777SRam Amrani break; 523a7efd777SRam Amrani } 524a7efd777SRam Amrani 525a7efd777SRam Amrani if (cq->sig != QEDR_CQ_MAGIC_NUMBER) { 526a7efd777SRam Amrani DP_ERR(cnq->dev, 527a7efd777SRam Amrani "Problem with cq signature, cq_handle->hi=%d ch_handle->lo=%d cq=%p\n", 528a7efd777SRam Amrani cq_handle->hi, cq_handle->lo, cq); 529a7efd777SRam Amrani break; 530a7efd777SRam Amrani } 531a7efd777SRam Amrani 532a7efd777SRam Amrani cq->arm_flags = 0; 533a7efd777SRam Amrani 5344dd72636SAmrani, Ram if (!cq->destroyed && cq->ibcq.comp_handler) 535a7efd777SRam Amrani (*cq->ibcq.comp_handler) 536a7efd777SRam Amrani (&cq->ibcq, cq->ibcq.cq_context); 537a7efd777SRam Amrani 5384dd72636SAmrani, Ram /* The CQ's CNQ notification counter is checked before 5394dd72636SAmrani, Ram * destroying the CQ in a busy-wait loop that waits for all of 5404dd72636SAmrani, Ram * the CQ's CNQ interrupts to be processed. It is increased 5414dd72636SAmrani, Ram * here, only after the completion handler, to ensure that the 5424dd72636SAmrani, Ram * the handler is not running when the CQ is destroyed. 5434dd72636SAmrani, Ram */ 5444dd72636SAmrani, Ram cq->cnq_notif++; 5454dd72636SAmrani, Ram 546ec72fce4SRam Amrani sw_comp_cons = qed_chain_get_cons_idx(&cnq->pbl); 547a7efd777SRam Amrani 548ec72fce4SRam Amrani cnq->n_comp++; 549ec72fce4SRam Amrani } 550ec72fce4SRam Amrani 551ec72fce4SRam Amrani qed_ops->rdma_cnq_prod_update(cnq->dev->rdma_ctx, cnq->index, 552ec72fce4SRam Amrani sw_comp_cons); 553ec72fce4SRam Amrani 554ec72fce4SRam Amrani qed_sb_ack(cnq->sb, IGU_INT_ENABLE, 1); 555ec72fce4SRam Amrani 556ec72fce4SRam Amrani return IRQ_HANDLED; 557ec72fce4SRam Amrani } 558ec72fce4SRam Amrani 559ec72fce4SRam Amrani static void qedr_sync_free_irqs(struct qedr_dev *dev) 560ec72fce4SRam Amrani { 561ec72fce4SRam Amrani u32 vector; 562ec72fce4SRam Amrani int i; 563ec72fce4SRam Amrani 564ec72fce4SRam Amrani for (i = 0; i < dev->int_info.used_cnt; i++) { 565ec72fce4SRam Amrani if (dev->int_info.msix_cnt) { 566ec72fce4SRam Amrani vector = dev->int_info.msix[i * dev->num_hwfns].vector; 567ec72fce4SRam Amrani synchronize_irq(vector); 568ec72fce4SRam Amrani free_irq(vector, &dev->cnq_array[i]); 569ec72fce4SRam Amrani } 570ec72fce4SRam Amrani } 571ec72fce4SRam Amrani 572ec72fce4SRam Amrani dev->int_info.used_cnt = 0; 573ec72fce4SRam Amrani } 574ec72fce4SRam Amrani 575ec72fce4SRam Amrani static int qedr_req_msix_irqs(struct qedr_dev *dev) 576ec72fce4SRam Amrani { 577ec72fce4SRam Amrani int i, rc = 0; 578ec72fce4SRam Amrani 579ec72fce4SRam Amrani if (dev->num_cnq > dev->int_info.msix_cnt) { 580ec72fce4SRam Amrani DP_ERR(dev, 581ec72fce4SRam Amrani "Interrupt mismatch: %d CNQ queues > %d MSI-x vectors\n", 582ec72fce4SRam Amrani dev->num_cnq, dev->int_info.msix_cnt); 583ec72fce4SRam Amrani return -EINVAL; 584ec72fce4SRam Amrani } 585ec72fce4SRam Amrani 586ec72fce4SRam Amrani for (i = 0; i < dev->num_cnq; i++) { 587ec72fce4SRam Amrani rc = request_irq(dev->int_info.msix[i * dev->num_hwfns].vector, 588ec72fce4SRam Amrani qedr_irq_handler, 0, dev->cnq_array[i].name, 589ec72fce4SRam Amrani &dev->cnq_array[i]); 590ec72fce4SRam Amrani if (rc) { 591ec72fce4SRam Amrani DP_ERR(dev, "Request cnq %d irq failed\n", i); 592ec72fce4SRam Amrani qedr_sync_free_irqs(dev); 593ec72fce4SRam Amrani } else { 594ec72fce4SRam Amrani DP_DEBUG(dev, QEDR_MSG_INIT, 595ec72fce4SRam Amrani "Requested cnq irq for %s [entry %d]. Cookie is at %p\n", 596ec72fce4SRam Amrani dev->cnq_array[i].name, i, 597ec72fce4SRam Amrani &dev->cnq_array[i]); 598ec72fce4SRam Amrani dev->int_info.used_cnt++; 599ec72fce4SRam Amrani } 600ec72fce4SRam Amrani } 601ec72fce4SRam Amrani 602ec72fce4SRam Amrani return rc; 603ec72fce4SRam Amrani } 604ec72fce4SRam Amrani 605ec72fce4SRam Amrani static int qedr_setup_irqs(struct qedr_dev *dev) 606ec72fce4SRam Amrani { 607ec72fce4SRam Amrani int rc; 608ec72fce4SRam Amrani 609ec72fce4SRam Amrani DP_DEBUG(dev, QEDR_MSG_INIT, "qedr_setup_irqs\n"); 610ec72fce4SRam Amrani 611ec72fce4SRam Amrani /* Learn Interrupt configuration */ 612ec72fce4SRam Amrani rc = dev->ops->rdma_set_rdma_int(dev->cdev, dev->num_cnq); 613ec72fce4SRam Amrani if (rc < 0) 614ec72fce4SRam Amrani return rc; 615ec72fce4SRam Amrani 616ec72fce4SRam Amrani rc = dev->ops->rdma_get_rdma_int(dev->cdev, &dev->int_info); 617ec72fce4SRam Amrani if (rc) { 618ec72fce4SRam Amrani DP_DEBUG(dev, QEDR_MSG_INIT, "get_rdma_int failed\n"); 619ec72fce4SRam Amrani return rc; 620ec72fce4SRam Amrani } 621ec72fce4SRam Amrani 622ec72fce4SRam Amrani if (dev->int_info.msix_cnt) { 623ec72fce4SRam Amrani DP_DEBUG(dev, QEDR_MSG_INIT, "rdma msix_cnt = %d\n", 624ec72fce4SRam Amrani dev->int_info.msix_cnt); 625ec72fce4SRam Amrani rc = qedr_req_msix_irqs(dev); 626ec72fce4SRam Amrani if (rc) 627ec72fce4SRam Amrani return rc; 628ec72fce4SRam Amrani } 629ec72fce4SRam Amrani 630ec72fce4SRam Amrani DP_DEBUG(dev, QEDR_MSG_INIT, "qedr_setup_irqs succeeded\n"); 631ec72fce4SRam Amrani 632ec72fce4SRam Amrani return 0; 633ec72fce4SRam Amrani } 634ec72fce4SRam Amrani 635ec72fce4SRam Amrani static int qedr_set_device_attr(struct qedr_dev *dev) 636ec72fce4SRam Amrani { 637ec72fce4SRam Amrani struct qed_rdma_device *qed_attr; 638ec72fce4SRam Amrani struct qedr_device_attr *attr; 639ec72fce4SRam Amrani u32 page_size; 640ec72fce4SRam Amrani 641ec72fce4SRam Amrani /* Part 1 - query core capabilities */ 642ec72fce4SRam Amrani qed_attr = dev->ops->rdma_query_device(dev->rdma_ctx); 643ec72fce4SRam Amrani 644ec72fce4SRam Amrani /* Part 2 - check capabilities */ 645ec72fce4SRam Amrani page_size = ~dev->attr.page_size_caps + 1; 646ec72fce4SRam Amrani if (page_size > PAGE_SIZE) { 647ec72fce4SRam Amrani DP_ERR(dev, 648ec72fce4SRam Amrani "Kernel PAGE_SIZE is %ld which is smaller than minimum page size (%d) required by qedr\n", 649ec72fce4SRam Amrani PAGE_SIZE, page_size); 650ec72fce4SRam Amrani return -ENODEV; 651ec72fce4SRam Amrani } 652ec72fce4SRam Amrani 653ec72fce4SRam Amrani /* Part 3 - copy and update capabilities */ 654ec72fce4SRam Amrani attr = &dev->attr; 655ec72fce4SRam Amrani attr->vendor_id = qed_attr->vendor_id; 656ec72fce4SRam Amrani attr->vendor_part_id = qed_attr->vendor_part_id; 657ec72fce4SRam Amrani attr->hw_ver = qed_attr->hw_ver; 658ec72fce4SRam Amrani attr->fw_ver = qed_attr->fw_ver; 659ec72fce4SRam Amrani attr->node_guid = qed_attr->node_guid; 660ec72fce4SRam Amrani attr->sys_image_guid = qed_attr->sys_image_guid; 661ec72fce4SRam Amrani attr->max_cnq = qed_attr->max_cnq; 662ec72fce4SRam Amrani attr->max_sge = qed_attr->max_sge; 663ec72fce4SRam Amrani attr->max_inline = qed_attr->max_inline; 664ec72fce4SRam Amrani attr->max_sqe = min_t(u32, qed_attr->max_wqe, QEDR_MAX_SQE); 665ec72fce4SRam Amrani attr->max_rqe = min_t(u32, qed_attr->max_wqe, QEDR_MAX_RQE); 666ec72fce4SRam Amrani attr->max_qp_resp_rd_atomic_resc = qed_attr->max_qp_resp_rd_atomic_resc; 667ec72fce4SRam Amrani attr->max_qp_req_rd_atomic_resc = qed_attr->max_qp_req_rd_atomic_resc; 668ec72fce4SRam Amrani attr->max_dev_resp_rd_atomic_resc = 669ec72fce4SRam Amrani qed_attr->max_dev_resp_rd_atomic_resc; 670ec72fce4SRam Amrani attr->max_cq = qed_attr->max_cq; 671ec72fce4SRam Amrani attr->max_qp = qed_attr->max_qp; 672ec72fce4SRam Amrani attr->max_mr = qed_attr->max_mr; 673ec72fce4SRam Amrani attr->max_mr_size = qed_attr->max_mr_size; 674ec72fce4SRam Amrani attr->max_cqe = min_t(u64, qed_attr->max_cqe, QEDR_MAX_CQES); 675ec72fce4SRam Amrani attr->max_mw = qed_attr->max_mw; 676ec72fce4SRam Amrani attr->max_fmr = qed_attr->max_fmr; 677ec72fce4SRam Amrani attr->max_mr_mw_fmr_pbl = qed_attr->max_mr_mw_fmr_pbl; 678ec72fce4SRam Amrani attr->max_mr_mw_fmr_size = qed_attr->max_mr_mw_fmr_size; 679ec72fce4SRam Amrani attr->max_pd = qed_attr->max_pd; 680ec72fce4SRam Amrani attr->max_ah = qed_attr->max_ah; 681ec72fce4SRam Amrani attr->max_pkey = qed_attr->max_pkey; 682ec72fce4SRam Amrani attr->max_srq = qed_attr->max_srq; 683ec72fce4SRam Amrani attr->max_srq_wr = qed_attr->max_srq_wr; 684ec72fce4SRam Amrani attr->dev_caps = qed_attr->dev_caps; 685ec72fce4SRam Amrani attr->page_size_caps = qed_attr->page_size_caps; 686ec72fce4SRam Amrani attr->dev_ack_delay = qed_attr->dev_ack_delay; 687ec72fce4SRam Amrani attr->reserved_lkey = qed_attr->reserved_lkey; 688ec72fce4SRam Amrani attr->bad_pkey_counter = qed_attr->bad_pkey_counter; 689ec72fce4SRam Amrani attr->max_stats_queues = qed_attr->max_stats_queues; 690ec72fce4SRam Amrani 691ec72fce4SRam Amrani return 0; 692ec72fce4SRam Amrani } 693ec72fce4SRam Amrani 6941a590751SRam Amrani void qedr_unaffiliated_event(void *context, u8 event_code) 695993d1b52SRam Amrani { 696993d1b52SRam Amrani pr_err("unaffiliated event not implemented yet\n"); 697993d1b52SRam Amrani } 698993d1b52SRam Amrani 699993d1b52SRam Amrani void qedr_affiliated_event(void *context, u8 e_code, void *fw_handle) 700993d1b52SRam Amrani { 701993d1b52SRam Amrani #define EVENT_TYPE_NOT_DEFINED 0 702993d1b52SRam Amrani #define EVENT_TYPE_CQ 1 703993d1b52SRam Amrani #define EVENT_TYPE_QP 2 704993d1b52SRam Amrani struct qedr_dev *dev = (struct qedr_dev *)context; 705be086e7cSMintz, Yuval struct regpair *async_handle = (struct regpair *)fw_handle; 706be086e7cSMintz, Yuval u64 roce_handle64 = ((u64) async_handle->hi << 32) + async_handle->lo; 707993d1b52SRam Amrani u8 event_type = EVENT_TYPE_NOT_DEFINED; 708993d1b52SRam Amrani struct ib_event event; 709993d1b52SRam Amrani struct ib_cq *ibcq; 710993d1b52SRam Amrani struct ib_qp *ibqp; 711993d1b52SRam Amrani struct qedr_cq *cq; 712993d1b52SRam Amrani struct qedr_qp *qp; 713993d1b52SRam Amrani 714993d1b52SRam Amrani switch (e_code) { 715993d1b52SRam Amrani case ROCE_ASYNC_EVENT_CQ_OVERFLOW_ERR: 716993d1b52SRam Amrani event.event = IB_EVENT_CQ_ERR; 717993d1b52SRam Amrani event_type = EVENT_TYPE_CQ; 718993d1b52SRam Amrani break; 719993d1b52SRam Amrani case ROCE_ASYNC_EVENT_SQ_DRAINED: 720993d1b52SRam Amrani event.event = IB_EVENT_SQ_DRAINED; 721993d1b52SRam Amrani event_type = EVENT_TYPE_QP; 722993d1b52SRam Amrani break; 723993d1b52SRam Amrani case ROCE_ASYNC_EVENT_QP_CATASTROPHIC_ERR: 724993d1b52SRam Amrani event.event = IB_EVENT_QP_FATAL; 725993d1b52SRam Amrani event_type = EVENT_TYPE_QP; 726993d1b52SRam Amrani break; 727993d1b52SRam Amrani case ROCE_ASYNC_EVENT_LOCAL_INVALID_REQUEST_ERR: 728993d1b52SRam Amrani event.event = IB_EVENT_QP_REQ_ERR; 729993d1b52SRam Amrani event_type = EVENT_TYPE_QP; 730993d1b52SRam Amrani break; 731993d1b52SRam Amrani case ROCE_ASYNC_EVENT_LOCAL_ACCESS_ERR: 732993d1b52SRam Amrani event.event = IB_EVENT_QP_ACCESS_ERR; 733993d1b52SRam Amrani event_type = EVENT_TYPE_QP; 734993d1b52SRam Amrani break; 735993d1b52SRam Amrani default: 736993d1b52SRam Amrani DP_ERR(dev, "unsupported event %d on handle=%llx\n", e_code, 737993d1b52SRam Amrani roce_handle64); 738993d1b52SRam Amrani } 739993d1b52SRam Amrani 740993d1b52SRam Amrani switch (event_type) { 741993d1b52SRam Amrani case EVENT_TYPE_CQ: 742993d1b52SRam Amrani cq = (struct qedr_cq *)(uintptr_t)roce_handle64; 743993d1b52SRam Amrani if (cq) { 744993d1b52SRam Amrani ibcq = &cq->ibcq; 745993d1b52SRam Amrani if (ibcq->event_handler) { 746993d1b52SRam Amrani event.device = ibcq->device; 747993d1b52SRam Amrani event.element.cq = ibcq; 748993d1b52SRam Amrani ibcq->event_handler(&event, ibcq->cq_context); 749993d1b52SRam Amrani } 750993d1b52SRam Amrani } else { 751993d1b52SRam Amrani WARN(1, 752993d1b52SRam Amrani "Error: CQ event with NULL pointer ibcq. Handle=%llx\n", 753993d1b52SRam Amrani roce_handle64); 754993d1b52SRam Amrani } 755993d1b52SRam Amrani DP_ERR(dev, "CQ event %d on hanlde %p\n", e_code, cq); 756993d1b52SRam Amrani break; 757993d1b52SRam Amrani case EVENT_TYPE_QP: 758993d1b52SRam Amrani qp = (struct qedr_qp *)(uintptr_t)roce_handle64; 759993d1b52SRam Amrani if (qp) { 760993d1b52SRam Amrani ibqp = &qp->ibqp; 761993d1b52SRam Amrani if (ibqp->event_handler) { 762993d1b52SRam Amrani event.device = ibqp->device; 763993d1b52SRam Amrani event.element.qp = ibqp; 764993d1b52SRam Amrani ibqp->event_handler(&event, ibqp->qp_context); 765993d1b52SRam Amrani } 766993d1b52SRam Amrani } else { 767993d1b52SRam Amrani WARN(1, 768993d1b52SRam Amrani "Error: QP event with NULL pointer ibqp. Handle=%llx\n", 769993d1b52SRam Amrani roce_handle64); 770993d1b52SRam Amrani } 771993d1b52SRam Amrani DP_ERR(dev, "QP event %d on hanlde %p\n", e_code, qp); 772993d1b52SRam Amrani break; 773993d1b52SRam Amrani default: 774993d1b52SRam Amrani break; 775993d1b52SRam Amrani } 776993d1b52SRam Amrani } 777993d1b52SRam Amrani 778ec72fce4SRam Amrani static int qedr_init_hw(struct qedr_dev *dev) 779ec72fce4SRam Amrani { 780ec72fce4SRam Amrani struct qed_rdma_add_user_out_params out_params; 781ec72fce4SRam Amrani struct qed_rdma_start_in_params *in_params; 782ec72fce4SRam Amrani struct qed_rdma_cnq_params *cur_pbl; 783ec72fce4SRam Amrani struct qed_rdma_events events; 784ec72fce4SRam Amrani dma_addr_t p_phys_table; 785ec72fce4SRam Amrani u32 page_cnt; 786ec72fce4SRam Amrani int rc = 0; 787ec72fce4SRam Amrani int i; 788ec72fce4SRam Amrani 789ec72fce4SRam Amrani in_params = kzalloc(sizeof(*in_params), GFP_KERNEL); 790ec72fce4SRam Amrani if (!in_params) { 791ec72fce4SRam Amrani rc = -ENOMEM; 792ec72fce4SRam Amrani goto out; 793ec72fce4SRam Amrani } 794ec72fce4SRam Amrani 795ec72fce4SRam Amrani in_params->desired_cnq = dev->num_cnq; 796ec72fce4SRam Amrani for (i = 0; i < dev->num_cnq; i++) { 797ec72fce4SRam Amrani cur_pbl = &in_params->cnq_pbl_list[i]; 798ec72fce4SRam Amrani 799ec72fce4SRam Amrani page_cnt = qed_chain_get_page_cnt(&dev->cnq_array[i].pbl); 800ec72fce4SRam Amrani cur_pbl->num_pbl_pages = page_cnt; 801ec72fce4SRam Amrani 802ec72fce4SRam Amrani p_phys_table = qed_chain_get_pbl_phys(&dev->cnq_array[i].pbl); 803ec72fce4SRam Amrani cur_pbl->pbl_ptr = (u64)p_phys_table; 804ec72fce4SRam Amrani } 805ec72fce4SRam Amrani 806993d1b52SRam Amrani events.affiliated_event = qedr_affiliated_event; 807993d1b52SRam Amrani events.unaffiliated_event = qedr_unaffiliated_event; 808ec72fce4SRam Amrani events.context = dev; 809ec72fce4SRam Amrani 810ec72fce4SRam Amrani in_params->events = &events; 811ec72fce4SRam Amrani in_params->cq_mode = QED_RDMA_CQ_MODE_32_BITS; 812ec72fce4SRam Amrani in_params->max_mtu = dev->ndev->mtu; 813*e411e058SKalderon, Michal dev->iwarp_max_mtu = dev->ndev->mtu; 814ec72fce4SRam Amrani ether_addr_copy(&in_params->mac_addr[0], dev->ndev->dev_addr); 815ec72fce4SRam Amrani 816ec72fce4SRam Amrani rc = dev->ops->rdma_init(dev->cdev, in_params); 817ec72fce4SRam Amrani if (rc) 818ec72fce4SRam Amrani goto out; 819ec72fce4SRam Amrani 820ec72fce4SRam Amrani rc = dev->ops->rdma_add_user(dev->rdma_ctx, &out_params); 821ec72fce4SRam Amrani if (rc) 822ec72fce4SRam Amrani goto out; 823ec72fce4SRam Amrani 824ec72fce4SRam Amrani dev->db_addr = (void *)(uintptr_t)out_params.dpi_addr; 825ec72fce4SRam Amrani dev->db_phys_addr = out_params.dpi_phys_addr; 826ec72fce4SRam Amrani dev->db_size = out_params.dpi_size; 827ec72fce4SRam Amrani dev->dpi = out_params.dpi; 828ec72fce4SRam Amrani 829ec72fce4SRam Amrani rc = qedr_set_device_attr(dev); 830ec72fce4SRam Amrani out: 831ec72fce4SRam Amrani kfree(in_params); 832ec72fce4SRam Amrani if (rc) 833ec72fce4SRam Amrani DP_ERR(dev, "Init HW Failed rc = %d\n", rc); 834ec72fce4SRam Amrani 835ec72fce4SRam Amrani return rc; 836ec72fce4SRam Amrani } 837ec72fce4SRam Amrani 838ec72fce4SRam Amrani void qedr_stop_hw(struct qedr_dev *dev) 839ec72fce4SRam Amrani { 840ec72fce4SRam Amrani dev->ops->rdma_remove_user(dev->rdma_ctx, dev->dpi); 841ec72fce4SRam Amrani dev->ops->rdma_stop(dev->rdma_ctx); 842ec72fce4SRam Amrani } 843ec72fce4SRam Amrani 8442e0cbc4dSRam Amrani static struct qedr_dev *qedr_add(struct qed_dev *cdev, struct pci_dev *pdev, 8452e0cbc4dSRam Amrani struct net_device *ndev) 8462e0cbc4dSRam Amrani { 847ec72fce4SRam Amrani struct qed_dev_rdma_info dev_info; 8482e0cbc4dSRam Amrani struct qedr_dev *dev; 8492e0cbc4dSRam Amrani int rc = 0, i; 8502e0cbc4dSRam Amrani 8512e0cbc4dSRam Amrani dev = (struct qedr_dev *)ib_alloc_device(sizeof(*dev)); 8522e0cbc4dSRam Amrani if (!dev) { 8532e0cbc4dSRam Amrani pr_err("Unable to allocate ib device\n"); 8542e0cbc4dSRam Amrani return NULL; 8552e0cbc4dSRam Amrani } 8562e0cbc4dSRam Amrani 8572e0cbc4dSRam Amrani DP_DEBUG(dev, QEDR_MSG_INIT, "qedr add device called\n"); 8582e0cbc4dSRam Amrani 8592e0cbc4dSRam Amrani dev->pdev = pdev; 8602e0cbc4dSRam Amrani dev->ndev = ndev; 8612e0cbc4dSRam Amrani dev->cdev = cdev; 8622e0cbc4dSRam Amrani 863ec72fce4SRam Amrani qed_ops = qed_get_rdma_ops(); 864ec72fce4SRam Amrani if (!qed_ops) { 865ec72fce4SRam Amrani DP_ERR(dev, "Failed to get qed roce operations\n"); 866ec72fce4SRam Amrani goto init_err; 867ec72fce4SRam Amrani } 868ec72fce4SRam Amrani 869ec72fce4SRam Amrani dev->ops = qed_ops; 870ec72fce4SRam Amrani rc = qed_ops->fill_dev_info(cdev, &dev_info); 871ec72fce4SRam Amrani if (rc) 872ec72fce4SRam Amrani goto init_err; 873ec72fce4SRam Amrani 874ec72fce4SRam Amrani dev->num_hwfns = dev_info.common.num_hwfns; 875ec72fce4SRam Amrani dev->rdma_ctx = dev->ops->rdma_get_rdma_ctx(cdev); 876ec72fce4SRam Amrani 877ec72fce4SRam Amrani dev->num_cnq = dev->ops->rdma_get_min_cnq_msix(cdev); 878ec72fce4SRam Amrani if (!dev->num_cnq) { 879ec72fce4SRam Amrani DP_ERR(dev, "not enough CNQ resources.\n"); 880ec72fce4SRam Amrani goto init_err; 881ec72fce4SRam Amrani } 882ec72fce4SRam Amrani 883cecbcddfSRam Amrani dev->wq_multiplier = QEDR_WQ_MULTIPLIER_DFT; 884cecbcddfSRam Amrani 8852e0cbc4dSRam Amrani qedr_pci_set_atomic(dev, pdev); 8862e0cbc4dSRam Amrani 887ec72fce4SRam Amrani rc = qedr_alloc_resources(dev); 888ec72fce4SRam Amrani if (rc) 889ec72fce4SRam Amrani goto init_err; 890ec72fce4SRam Amrani 891ec72fce4SRam Amrani rc = qedr_init_hw(dev); 892ec72fce4SRam Amrani if (rc) 893ec72fce4SRam Amrani goto alloc_err; 894ec72fce4SRam Amrani 895ec72fce4SRam Amrani rc = qedr_setup_irqs(dev); 896ec72fce4SRam Amrani if (rc) 897ec72fce4SRam Amrani goto irq_err; 898ec72fce4SRam Amrani 8992e0cbc4dSRam Amrani rc = qedr_register_device(dev); 9002e0cbc4dSRam Amrani if (rc) { 9012e0cbc4dSRam Amrani DP_ERR(dev, "Unable to allocate register device\n"); 902ec72fce4SRam Amrani goto reg_err; 9032e0cbc4dSRam Amrani } 9042e0cbc4dSRam Amrani 9052e0cbc4dSRam Amrani for (i = 0; i < ARRAY_SIZE(qedr_attributes); i++) 9062e0cbc4dSRam Amrani if (device_create_file(&dev->ibdev.dev, qedr_attributes[i])) 907993d1b52SRam Amrani goto sysfs_err; 9082e0cbc4dSRam Amrani 909f449c7a2SRam Amrani if (!test_and_set_bit(QEDR_ENET_STATE_BIT, &dev->enet_state)) 910f449c7a2SRam Amrani qedr_ib_dispatch_event(dev, QEDR_PORT, IB_EVENT_PORT_ACTIVE); 911f449c7a2SRam Amrani 9122e0cbc4dSRam Amrani DP_DEBUG(dev, QEDR_MSG_INIT, "qedr driver loaded successfully\n"); 9132e0cbc4dSRam Amrani return dev; 9142e0cbc4dSRam Amrani 915993d1b52SRam Amrani sysfs_err: 916993d1b52SRam Amrani ib_unregister_device(&dev->ibdev); 917ec72fce4SRam Amrani reg_err: 918ec72fce4SRam Amrani qedr_sync_free_irqs(dev); 919ec72fce4SRam Amrani irq_err: 920ec72fce4SRam Amrani qedr_stop_hw(dev); 921ec72fce4SRam Amrani alloc_err: 922ec72fce4SRam Amrani qedr_free_resources(dev); 9232e0cbc4dSRam Amrani init_err: 9242e0cbc4dSRam Amrani ib_dealloc_device(&dev->ibdev); 9252e0cbc4dSRam Amrani DP_ERR(dev, "qedr driver load failed rc=%d\n", rc); 9262e0cbc4dSRam Amrani 9272e0cbc4dSRam Amrani return NULL; 9282e0cbc4dSRam Amrani } 9292e0cbc4dSRam Amrani 9302e0cbc4dSRam Amrani static void qedr_remove(struct qedr_dev *dev) 9312e0cbc4dSRam Amrani { 9322e0cbc4dSRam Amrani /* First unregister with stack to stop all the active traffic 9332e0cbc4dSRam Amrani * of the registered clients. 9342e0cbc4dSRam Amrani */ 9352e0cbc4dSRam Amrani qedr_remove_sysfiles(dev); 936993d1b52SRam Amrani ib_unregister_device(&dev->ibdev); 9372e0cbc4dSRam Amrani 938ec72fce4SRam Amrani qedr_stop_hw(dev); 939ec72fce4SRam Amrani qedr_sync_free_irqs(dev); 940ec72fce4SRam Amrani qedr_free_resources(dev); 9412e0cbc4dSRam Amrani ib_dealloc_device(&dev->ibdev); 9422e0cbc4dSRam Amrani } 9432e0cbc4dSRam Amrani 944f449c7a2SRam Amrani static void qedr_close(struct qedr_dev *dev) 9452e0cbc4dSRam Amrani { 946f449c7a2SRam Amrani if (test_and_clear_bit(QEDR_ENET_STATE_BIT, &dev->enet_state)) 947f449c7a2SRam Amrani qedr_ib_dispatch_event(dev, QEDR_PORT, IB_EVENT_PORT_ERR); 9482e0cbc4dSRam Amrani } 9492e0cbc4dSRam Amrani 9502e0cbc4dSRam Amrani static void qedr_shutdown(struct qedr_dev *dev) 9512e0cbc4dSRam Amrani { 9522e0cbc4dSRam Amrani qedr_close(dev); 9532e0cbc4dSRam Amrani qedr_remove(dev); 9542e0cbc4dSRam Amrani } 9552e0cbc4dSRam Amrani 956f449c7a2SRam Amrani static void qedr_open(struct qedr_dev *dev) 957f449c7a2SRam Amrani { 958f449c7a2SRam Amrani if (!test_and_set_bit(QEDR_ENET_STATE_BIT, &dev->enet_state)) 959f449c7a2SRam Amrani qedr_ib_dispatch_event(dev, QEDR_PORT, IB_EVENT_PORT_ACTIVE); 960f449c7a2SRam Amrani } 961f449c7a2SRam Amrani 9621d1424c8SRam Amrani static void qedr_mac_address_change(struct qedr_dev *dev) 9631d1424c8SRam Amrani { 9641d1424c8SRam Amrani union ib_gid *sgid = &dev->sgid_tbl[0]; 9651d1424c8SRam Amrani u8 guid[8], mac_addr[6]; 9661d1424c8SRam Amrani int rc; 9671d1424c8SRam Amrani 9681d1424c8SRam Amrani /* Update SGID */ 9691d1424c8SRam Amrani ether_addr_copy(&mac_addr[0], dev->ndev->dev_addr); 9701d1424c8SRam Amrani guid[0] = mac_addr[0] ^ 2; 9711d1424c8SRam Amrani guid[1] = mac_addr[1]; 9721d1424c8SRam Amrani guid[2] = mac_addr[2]; 9731d1424c8SRam Amrani guid[3] = 0xff; 9741d1424c8SRam Amrani guid[4] = 0xfe; 9751d1424c8SRam Amrani guid[5] = mac_addr[3]; 9761d1424c8SRam Amrani guid[6] = mac_addr[4]; 9771d1424c8SRam Amrani guid[7] = mac_addr[5]; 9781d1424c8SRam Amrani sgid->global.subnet_prefix = cpu_to_be64(0xfe80000000000000LL); 9791d1424c8SRam Amrani memcpy(&sgid->raw[8], guid, sizeof(guid)); 9801d1424c8SRam Amrani 9811d1424c8SRam Amrani /* Update LL2 */ 9820518c12fSMichal Kalderon rc = dev->ops->ll2_set_mac_filter(dev->cdev, 9831d1424c8SRam Amrani dev->gsi_ll2_mac_address, 9841d1424c8SRam Amrani dev->ndev->dev_addr); 9851d1424c8SRam Amrani 9861d1424c8SRam Amrani ether_addr_copy(dev->gsi_ll2_mac_address, dev->ndev->dev_addr); 9871d1424c8SRam Amrani 988f449c7a2SRam Amrani qedr_ib_dispatch_event(dev, QEDR_PORT, IB_EVENT_GID_CHANGE); 9891d1424c8SRam Amrani 9901d1424c8SRam Amrani if (rc) 9911d1424c8SRam Amrani DP_ERR(dev, "Error updating mac filter\n"); 9921d1424c8SRam Amrani } 9931d1424c8SRam Amrani 9942e0cbc4dSRam Amrani /* event handling via NIC driver ensures that all the NIC specific 9952e0cbc4dSRam Amrani * initialization done before RoCE driver notifies 9962e0cbc4dSRam Amrani * event to stack. 9972e0cbc4dSRam Amrani */ 998bbfcd1e8SMichal Kalderon static void qedr_notify(struct qedr_dev *dev, enum qede_rdma_event event) 9992e0cbc4dSRam Amrani { 10002e0cbc4dSRam Amrani switch (event) { 10012e0cbc4dSRam Amrani case QEDE_UP: 1002f449c7a2SRam Amrani qedr_open(dev); 10032e0cbc4dSRam Amrani break; 10042e0cbc4dSRam Amrani case QEDE_DOWN: 10052e0cbc4dSRam Amrani qedr_close(dev); 10062e0cbc4dSRam Amrani break; 10072e0cbc4dSRam Amrani case QEDE_CLOSE: 10082e0cbc4dSRam Amrani qedr_shutdown(dev); 10092e0cbc4dSRam Amrani break; 10102e0cbc4dSRam Amrani case QEDE_CHANGE_ADDR: 10111d1424c8SRam Amrani qedr_mac_address_change(dev); 10122e0cbc4dSRam Amrani break; 10132e0cbc4dSRam Amrani default: 10142e0cbc4dSRam Amrani pr_err("Event not supported\n"); 10152e0cbc4dSRam Amrani } 10162e0cbc4dSRam Amrani } 10172e0cbc4dSRam Amrani 10182e0cbc4dSRam Amrani static struct qedr_driver qedr_drv = { 10192e0cbc4dSRam Amrani .name = "qedr_driver", 10202e0cbc4dSRam Amrani .add = qedr_add, 10212e0cbc4dSRam Amrani .remove = qedr_remove, 10222e0cbc4dSRam Amrani .notify = qedr_notify, 10232e0cbc4dSRam Amrani }; 10242e0cbc4dSRam Amrani 10252e0cbc4dSRam Amrani static int __init qedr_init_module(void) 10262e0cbc4dSRam Amrani { 1027bbfcd1e8SMichal Kalderon return qede_rdma_register_driver(&qedr_drv); 10282e0cbc4dSRam Amrani } 10292e0cbc4dSRam Amrani 10302e0cbc4dSRam Amrani static void __exit qedr_exit_module(void) 10312e0cbc4dSRam Amrani { 1032bbfcd1e8SMichal Kalderon qede_rdma_unregister_driver(&qedr_drv); 10332e0cbc4dSRam Amrani } 10342e0cbc4dSRam Amrani 10352e0cbc4dSRam Amrani module_init(qedr_init_module); 10362e0cbc4dSRam Amrani module_exit(qedr_exit_module); 1037