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> 352e0cbc4dSRam Amrani #include <linux/netdevice.h> 362e0cbc4dSRam Amrani #include <linux/iommu.h> 372e0cbc4dSRam Amrani #include <net/addrconf.h> 382e0cbc4dSRam Amrani #include <linux/qed/qede_roce.h> 39*ec72fce4SRam Amrani #include <linux/qed/qed_chain.h> 40*ec72fce4SRam Amrani #include <linux/qed/qed_if.h> 412e0cbc4dSRam Amrani #include "qedr.h" 422e0cbc4dSRam Amrani 432e0cbc4dSRam Amrani MODULE_DESCRIPTION("QLogic 40G/100G ROCE Driver"); 442e0cbc4dSRam Amrani MODULE_AUTHOR("QLogic Corporation"); 452e0cbc4dSRam Amrani MODULE_LICENSE("Dual BSD/GPL"); 462e0cbc4dSRam Amrani MODULE_VERSION(QEDR_MODULE_VERSION); 472e0cbc4dSRam Amrani 482e0cbc4dSRam Amrani void qedr_ib_dispatch_event(struct qedr_dev *dev, u8 port_num, 492e0cbc4dSRam Amrani enum ib_event_type type) 502e0cbc4dSRam Amrani { 512e0cbc4dSRam Amrani struct ib_event ibev; 522e0cbc4dSRam Amrani 532e0cbc4dSRam Amrani ibev.device = &dev->ibdev; 542e0cbc4dSRam Amrani ibev.element.port_num = port_num; 552e0cbc4dSRam Amrani ibev.event = type; 562e0cbc4dSRam Amrani 572e0cbc4dSRam Amrani ib_dispatch_event(&ibev); 582e0cbc4dSRam Amrani } 592e0cbc4dSRam Amrani 602e0cbc4dSRam Amrani static enum rdma_link_layer qedr_link_layer(struct ib_device *device, 612e0cbc4dSRam Amrani u8 port_num) 622e0cbc4dSRam Amrani { 632e0cbc4dSRam Amrani return IB_LINK_LAYER_ETHERNET; 642e0cbc4dSRam Amrani } 652e0cbc4dSRam Amrani 66*ec72fce4SRam Amrani static void qedr_get_dev_fw_str(struct ib_device *ibdev, char *str, 67*ec72fce4SRam Amrani size_t str_len) 68*ec72fce4SRam Amrani { 69*ec72fce4SRam Amrani struct qedr_dev *qedr = get_qedr_dev(ibdev); 70*ec72fce4SRam Amrani u32 fw_ver = (u32)qedr->attr.fw_ver; 71*ec72fce4SRam Amrani 72*ec72fce4SRam Amrani snprintf(str, str_len, "%d. %d. %d. %d", 73*ec72fce4SRam Amrani (fw_ver >> 24) & 0xFF, (fw_ver >> 16) & 0xFF, 74*ec72fce4SRam Amrani (fw_ver >> 8) & 0xFF, fw_ver & 0xFF); 75*ec72fce4SRam Amrani } 76*ec72fce4SRam Amrani 772e0cbc4dSRam Amrani static int qedr_register_device(struct qedr_dev *dev) 782e0cbc4dSRam Amrani { 792e0cbc4dSRam Amrani strlcpy(dev->ibdev.name, "qedr%d", IB_DEVICE_NAME_MAX); 802e0cbc4dSRam Amrani 812e0cbc4dSRam Amrani memcpy(dev->ibdev.node_desc, QEDR_NODE_DESC, sizeof(QEDR_NODE_DESC)); 822e0cbc4dSRam Amrani dev->ibdev.owner = THIS_MODULE; 832e0cbc4dSRam Amrani 842e0cbc4dSRam Amrani dev->ibdev.get_link_layer = qedr_link_layer; 85*ec72fce4SRam Amrani dev->ibdev.get_dev_fw_str = qedr_get_dev_fw_str; 862e0cbc4dSRam Amrani 872e0cbc4dSRam Amrani return 0; 882e0cbc4dSRam Amrani } 892e0cbc4dSRam Amrani 90*ec72fce4SRam Amrani /* This function allocates fast-path status block memory */ 91*ec72fce4SRam Amrani static int qedr_alloc_mem_sb(struct qedr_dev *dev, 92*ec72fce4SRam Amrani struct qed_sb_info *sb_info, u16 sb_id) 93*ec72fce4SRam Amrani { 94*ec72fce4SRam Amrani struct status_block *sb_virt; 95*ec72fce4SRam Amrani dma_addr_t sb_phys; 96*ec72fce4SRam Amrani int rc; 97*ec72fce4SRam Amrani 98*ec72fce4SRam Amrani sb_virt = dma_alloc_coherent(&dev->pdev->dev, 99*ec72fce4SRam Amrani sizeof(*sb_virt), &sb_phys, GFP_KERNEL); 100*ec72fce4SRam Amrani if (!sb_virt) 101*ec72fce4SRam Amrani return -ENOMEM; 102*ec72fce4SRam Amrani 103*ec72fce4SRam Amrani rc = dev->ops->common->sb_init(dev->cdev, sb_info, 104*ec72fce4SRam Amrani sb_virt, sb_phys, sb_id, 105*ec72fce4SRam Amrani QED_SB_TYPE_CNQ); 106*ec72fce4SRam Amrani if (rc) { 107*ec72fce4SRam Amrani pr_err("Status block initialization failed\n"); 108*ec72fce4SRam Amrani dma_free_coherent(&dev->pdev->dev, sizeof(*sb_virt), 109*ec72fce4SRam Amrani sb_virt, sb_phys); 110*ec72fce4SRam Amrani return rc; 111*ec72fce4SRam Amrani } 112*ec72fce4SRam Amrani 113*ec72fce4SRam Amrani return 0; 114*ec72fce4SRam Amrani } 115*ec72fce4SRam Amrani 116*ec72fce4SRam Amrani static void qedr_free_mem_sb(struct qedr_dev *dev, 117*ec72fce4SRam Amrani struct qed_sb_info *sb_info, int sb_id) 118*ec72fce4SRam Amrani { 119*ec72fce4SRam Amrani if (sb_info->sb_virt) { 120*ec72fce4SRam Amrani dev->ops->common->sb_release(dev->cdev, sb_info, sb_id); 121*ec72fce4SRam Amrani dma_free_coherent(&dev->pdev->dev, sizeof(*sb_info->sb_virt), 122*ec72fce4SRam Amrani (void *)sb_info->sb_virt, sb_info->sb_phys); 123*ec72fce4SRam Amrani } 124*ec72fce4SRam Amrani } 125*ec72fce4SRam Amrani 126*ec72fce4SRam Amrani static void qedr_free_resources(struct qedr_dev *dev) 127*ec72fce4SRam Amrani { 128*ec72fce4SRam Amrani int i; 129*ec72fce4SRam Amrani 130*ec72fce4SRam Amrani for (i = 0; i < dev->num_cnq; i++) { 131*ec72fce4SRam Amrani qedr_free_mem_sb(dev, &dev->sb_array[i], dev->sb_start + i); 132*ec72fce4SRam Amrani dev->ops->common->chain_free(dev->cdev, &dev->cnq_array[i].pbl); 133*ec72fce4SRam Amrani } 134*ec72fce4SRam Amrani 135*ec72fce4SRam Amrani kfree(dev->cnq_array); 136*ec72fce4SRam Amrani kfree(dev->sb_array); 137*ec72fce4SRam Amrani kfree(dev->sgid_tbl); 138*ec72fce4SRam Amrani } 139*ec72fce4SRam Amrani 140*ec72fce4SRam Amrani static int qedr_alloc_resources(struct qedr_dev *dev) 141*ec72fce4SRam Amrani { 142*ec72fce4SRam Amrani struct qedr_cnq *cnq; 143*ec72fce4SRam Amrani __le16 *cons_pi; 144*ec72fce4SRam Amrani u16 n_entries; 145*ec72fce4SRam Amrani int i, rc; 146*ec72fce4SRam Amrani 147*ec72fce4SRam Amrani dev->sgid_tbl = kzalloc(sizeof(union ib_gid) * 148*ec72fce4SRam Amrani QEDR_MAX_SGID, GFP_KERNEL); 149*ec72fce4SRam Amrani if (!dev->sgid_tbl) 150*ec72fce4SRam Amrani return -ENOMEM; 151*ec72fce4SRam Amrani 152*ec72fce4SRam Amrani spin_lock_init(&dev->sgid_lock); 153*ec72fce4SRam Amrani 154*ec72fce4SRam Amrani /* Allocate Status blocks for CNQ */ 155*ec72fce4SRam Amrani dev->sb_array = kcalloc(dev->num_cnq, sizeof(*dev->sb_array), 156*ec72fce4SRam Amrani GFP_KERNEL); 157*ec72fce4SRam Amrani if (!dev->sb_array) { 158*ec72fce4SRam Amrani rc = -ENOMEM; 159*ec72fce4SRam Amrani goto err1; 160*ec72fce4SRam Amrani } 161*ec72fce4SRam Amrani 162*ec72fce4SRam Amrani dev->cnq_array = kcalloc(dev->num_cnq, 163*ec72fce4SRam Amrani sizeof(*dev->cnq_array), GFP_KERNEL); 164*ec72fce4SRam Amrani if (!dev->cnq_array) { 165*ec72fce4SRam Amrani rc = -ENOMEM; 166*ec72fce4SRam Amrani goto err2; 167*ec72fce4SRam Amrani } 168*ec72fce4SRam Amrani 169*ec72fce4SRam Amrani dev->sb_start = dev->ops->rdma_get_start_sb(dev->cdev); 170*ec72fce4SRam Amrani 171*ec72fce4SRam Amrani /* Allocate CNQ PBLs */ 172*ec72fce4SRam Amrani n_entries = min_t(u32, QED_RDMA_MAX_CNQ_SIZE, QEDR_ROCE_MAX_CNQ_SIZE); 173*ec72fce4SRam Amrani for (i = 0; i < dev->num_cnq; i++) { 174*ec72fce4SRam Amrani cnq = &dev->cnq_array[i]; 175*ec72fce4SRam Amrani 176*ec72fce4SRam Amrani rc = qedr_alloc_mem_sb(dev, &dev->sb_array[i], 177*ec72fce4SRam Amrani dev->sb_start + i); 178*ec72fce4SRam Amrani if (rc) 179*ec72fce4SRam Amrani goto err3; 180*ec72fce4SRam Amrani 181*ec72fce4SRam Amrani rc = dev->ops->common->chain_alloc(dev->cdev, 182*ec72fce4SRam Amrani QED_CHAIN_USE_TO_CONSUME, 183*ec72fce4SRam Amrani QED_CHAIN_MODE_PBL, 184*ec72fce4SRam Amrani QED_CHAIN_CNT_TYPE_U16, 185*ec72fce4SRam Amrani n_entries, 186*ec72fce4SRam Amrani sizeof(struct regpair *), 187*ec72fce4SRam Amrani &cnq->pbl); 188*ec72fce4SRam Amrani if (rc) 189*ec72fce4SRam Amrani goto err4; 190*ec72fce4SRam Amrani 191*ec72fce4SRam Amrani cnq->dev = dev; 192*ec72fce4SRam Amrani cnq->sb = &dev->sb_array[i]; 193*ec72fce4SRam Amrani cons_pi = dev->sb_array[i].sb_virt->pi_array; 194*ec72fce4SRam Amrani cnq->hw_cons_ptr = &cons_pi[QED_ROCE_PROTOCOL_INDEX]; 195*ec72fce4SRam Amrani cnq->index = i; 196*ec72fce4SRam Amrani sprintf(cnq->name, "qedr%d@pci:%s", i, pci_name(dev->pdev)); 197*ec72fce4SRam Amrani 198*ec72fce4SRam Amrani DP_DEBUG(dev, QEDR_MSG_INIT, "cnq[%d].cons=%d\n", 199*ec72fce4SRam Amrani i, qed_chain_get_cons_idx(&cnq->pbl)); 200*ec72fce4SRam Amrani } 201*ec72fce4SRam Amrani 202*ec72fce4SRam Amrani return 0; 203*ec72fce4SRam Amrani err4: 204*ec72fce4SRam Amrani qedr_free_mem_sb(dev, &dev->sb_array[i], dev->sb_start + i); 205*ec72fce4SRam Amrani err3: 206*ec72fce4SRam Amrani for (--i; i >= 0; i--) { 207*ec72fce4SRam Amrani dev->ops->common->chain_free(dev->cdev, &dev->cnq_array[i].pbl); 208*ec72fce4SRam Amrani qedr_free_mem_sb(dev, &dev->sb_array[i], dev->sb_start + i); 209*ec72fce4SRam Amrani } 210*ec72fce4SRam Amrani kfree(dev->cnq_array); 211*ec72fce4SRam Amrani err2: 212*ec72fce4SRam Amrani kfree(dev->sb_array); 213*ec72fce4SRam Amrani err1: 214*ec72fce4SRam Amrani kfree(dev->sgid_tbl); 215*ec72fce4SRam Amrani return rc; 216*ec72fce4SRam Amrani } 217*ec72fce4SRam Amrani 2182e0cbc4dSRam Amrani /* QEDR sysfs interface */ 2192e0cbc4dSRam Amrani static ssize_t show_rev(struct device *device, struct device_attribute *attr, 2202e0cbc4dSRam Amrani char *buf) 2212e0cbc4dSRam Amrani { 2222e0cbc4dSRam Amrani struct qedr_dev *dev = dev_get_drvdata(device); 2232e0cbc4dSRam Amrani 2242e0cbc4dSRam Amrani return scnprintf(buf, PAGE_SIZE, "0x%x\n", dev->pdev->vendor); 2252e0cbc4dSRam Amrani } 2262e0cbc4dSRam Amrani 2272e0cbc4dSRam Amrani static ssize_t show_hca_type(struct device *device, 2282e0cbc4dSRam Amrani struct device_attribute *attr, char *buf) 2292e0cbc4dSRam Amrani { 2302e0cbc4dSRam Amrani return scnprintf(buf, PAGE_SIZE, "%s\n", "HCA_TYPE_TO_SET"); 2312e0cbc4dSRam Amrani } 2322e0cbc4dSRam Amrani 2332e0cbc4dSRam Amrani static DEVICE_ATTR(hw_rev, S_IRUGO, show_rev, NULL); 2342e0cbc4dSRam Amrani static DEVICE_ATTR(hca_type, S_IRUGO, show_hca_type, NULL); 2352e0cbc4dSRam Amrani 2362e0cbc4dSRam Amrani static struct device_attribute *qedr_attributes[] = { 2372e0cbc4dSRam Amrani &dev_attr_hw_rev, 2382e0cbc4dSRam Amrani &dev_attr_hca_type 2392e0cbc4dSRam Amrani }; 2402e0cbc4dSRam Amrani 2412e0cbc4dSRam Amrani static void qedr_remove_sysfiles(struct qedr_dev *dev) 2422e0cbc4dSRam Amrani { 2432e0cbc4dSRam Amrani int i; 2442e0cbc4dSRam Amrani 2452e0cbc4dSRam Amrani for (i = 0; i < ARRAY_SIZE(qedr_attributes); i++) 2462e0cbc4dSRam Amrani device_remove_file(&dev->ibdev.dev, qedr_attributes[i]); 2472e0cbc4dSRam Amrani } 2482e0cbc4dSRam Amrani 2492e0cbc4dSRam Amrani static void qedr_pci_set_atomic(struct qedr_dev *dev, struct pci_dev *pdev) 2502e0cbc4dSRam Amrani { 2512e0cbc4dSRam Amrani struct pci_dev *bridge; 2522e0cbc4dSRam Amrani u32 val; 2532e0cbc4dSRam Amrani 2542e0cbc4dSRam Amrani dev->atomic_cap = IB_ATOMIC_NONE; 2552e0cbc4dSRam Amrani 2562e0cbc4dSRam Amrani bridge = pdev->bus->self; 2572e0cbc4dSRam Amrani if (!bridge) 2582e0cbc4dSRam Amrani return; 2592e0cbc4dSRam Amrani 2602e0cbc4dSRam Amrani /* Check whether we are connected directly or via a switch */ 2612e0cbc4dSRam Amrani while (bridge && bridge->bus->parent) { 2622e0cbc4dSRam Amrani DP_DEBUG(dev, QEDR_MSG_INIT, 2632e0cbc4dSRam Amrani "Device is not connected directly to root. bridge->bus->number=%d primary=%d\n", 2642e0cbc4dSRam Amrani bridge->bus->number, bridge->bus->primary); 2652e0cbc4dSRam Amrani /* Need to check Atomic Op Routing Supported all the way to 2662e0cbc4dSRam Amrani * root complex. 2672e0cbc4dSRam Amrani */ 2682e0cbc4dSRam Amrani pcie_capability_read_dword(bridge, PCI_EXP_DEVCAP2, &val); 2692e0cbc4dSRam Amrani if (!(val & PCI_EXP_DEVCAP2_ATOMIC_ROUTE)) { 2702e0cbc4dSRam Amrani pcie_capability_clear_word(pdev, 2712e0cbc4dSRam Amrani PCI_EXP_DEVCTL2, 2722e0cbc4dSRam Amrani PCI_EXP_DEVCTL2_ATOMIC_REQ); 2732e0cbc4dSRam Amrani return; 2742e0cbc4dSRam Amrani } 2752e0cbc4dSRam Amrani bridge = bridge->bus->parent->self; 2762e0cbc4dSRam Amrani } 2772e0cbc4dSRam Amrani bridge = pdev->bus->self; 2782e0cbc4dSRam Amrani 2792e0cbc4dSRam Amrani /* according to bridge capability */ 2802e0cbc4dSRam Amrani pcie_capability_read_dword(bridge, PCI_EXP_DEVCAP2, &val); 2812e0cbc4dSRam Amrani if (val & PCI_EXP_DEVCAP2_ATOMIC_COMP64) { 2822e0cbc4dSRam Amrani pcie_capability_set_word(pdev, PCI_EXP_DEVCTL2, 2832e0cbc4dSRam Amrani PCI_EXP_DEVCTL2_ATOMIC_REQ); 2842e0cbc4dSRam Amrani dev->atomic_cap = IB_ATOMIC_GLOB; 2852e0cbc4dSRam Amrani } else { 2862e0cbc4dSRam Amrani pcie_capability_clear_word(pdev, PCI_EXP_DEVCTL2, 2872e0cbc4dSRam Amrani PCI_EXP_DEVCTL2_ATOMIC_REQ); 2882e0cbc4dSRam Amrani } 2892e0cbc4dSRam Amrani } 2902e0cbc4dSRam Amrani 291*ec72fce4SRam Amrani static const struct qed_rdma_ops *qed_ops; 292*ec72fce4SRam Amrani 293*ec72fce4SRam Amrani #define HILO_U64(hi, lo) ((((u64)(hi)) << 32) + (lo)) 294*ec72fce4SRam Amrani 295*ec72fce4SRam Amrani static irqreturn_t qedr_irq_handler(int irq, void *handle) 296*ec72fce4SRam Amrani { 297*ec72fce4SRam Amrani u16 hw_comp_cons, sw_comp_cons; 298*ec72fce4SRam Amrani struct qedr_cnq *cnq = handle; 299*ec72fce4SRam Amrani 300*ec72fce4SRam Amrani qed_sb_ack(cnq->sb, IGU_INT_DISABLE, 0); 301*ec72fce4SRam Amrani 302*ec72fce4SRam Amrani qed_sb_update_sb_idx(cnq->sb); 303*ec72fce4SRam Amrani 304*ec72fce4SRam Amrani hw_comp_cons = le16_to_cpu(*cnq->hw_cons_ptr); 305*ec72fce4SRam Amrani sw_comp_cons = qed_chain_get_cons_idx(&cnq->pbl); 306*ec72fce4SRam Amrani 307*ec72fce4SRam Amrani /* Align protocol-index and chain reads */ 308*ec72fce4SRam Amrani rmb(); 309*ec72fce4SRam Amrani 310*ec72fce4SRam Amrani while (sw_comp_cons != hw_comp_cons) { 311*ec72fce4SRam Amrani sw_comp_cons = qed_chain_get_cons_idx(&cnq->pbl); 312*ec72fce4SRam Amrani cnq->n_comp++; 313*ec72fce4SRam Amrani } 314*ec72fce4SRam Amrani 315*ec72fce4SRam Amrani qed_ops->rdma_cnq_prod_update(cnq->dev->rdma_ctx, cnq->index, 316*ec72fce4SRam Amrani sw_comp_cons); 317*ec72fce4SRam Amrani 318*ec72fce4SRam Amrani qed_sb_ack(cnq->sb, IGU_INT_ENABLE, 1); 319*ec72fce4SRam Amrani 320*ec72fce4SRam Amrani return IRQ_HANDLED; 321*ec72fce4SRam Amrani } 322*ec72fce4SRam Amrani 323*ec72fce4SRam Amrani static void qedr_sync_free_irqs(struct qedr_dev *dev) 324*ec72fce4SRam Amrani { 325*ec72fce4SRam Amrani u32 vector; 326*ec72fce4SRam Amrani int i; 327*ec72fce4SRam Amrani 328*ec72fce4SRam Amrani for (i = 0; i < dev->int_info.used_cnt; i++) { 329*ec72fce4SRam Amrani if (dev->int_info.msix_cnt) { 330*ec72fce4SRam Amrani vector = dev->int_info.msix[i * dev->num_hwfns].vector; 331*ec72fce4SRam Amrani synchronize_irq(vector); 332*ec72fce4SRam Amrani free_irq(vector, &dev->cnq_array[i]); 333*ec72fce4SRam Amrani } 334*ec72fce4SRam Amrani } 335*ec72fce4SRam Amrani 336*ec72fce4SRam Amrani dev->int_info.used_cnt = 0; 337*ec72fce4SRam Amrani } 338*ec72fce4SRam Amrani 339*ec72fce4SRam Amrani static int qedr_req_msix_irqs(struct qedr_dev *dev) 340*ec72fce4SRam Amrani { 341*ec72fce4SRam Amrani int i, rc = 0; 342*ec72fce4SRam Amrani 343*ec72fce4SRam Amrani if (dev->num_cnq > dev->int_info.msix_cnt) { 344*ec72fce4SRam Amrani DP_ERR(dev, 345*ec72fce4SRam Amrani "Interrupt mismatch: %d CNQ queues > %d MSI-x vectors\n", 346*ec72fce4SRam Amrani dev->num_cnq, dev->int_info.msix_cnt); 347*ec72fce4SRam Amrani return -EINVAL; 348*ec72fce4SRam Amrani } 349*ec72fce4SRam Amrani 350*ec72fce4SRam Amrani for (i = 0; i < dev->num_cnq; i++) { 351*ec72fce4SRam Amrani rc = request_irq(dev->int_info.msix[i * dev->num_hwfns].vector, 352*ec72fce4SRam Amrani qedr_irq_handler, 0, dev->cnq_array[i].name, 353*ec72fce4SRam Amrani &dev->cnq_array[i]); 354*ec72fce4SRam Amrani if (rc) { 355*ec72fce4SRam Amrani DP_ERR(dev, "Request cnq %d irq failed\n", i); 356*ec72fce4SRam Amrani qedr_sync_free_irqs(dev); 357*ec72fce4SRam Amrani } else { 358*ec72fce4SRam Amrani DP_DEBUG(dev, QEDR_MSG_INIT, 359*ec72fce4SRam Amrani "Requested cnq irq for %s [entry %d]. Cookie is at %p\n", 360*ec72fce4SRam Amrani dev->cnq_array[i].name, i, 361*ec72fce4SRam Amrani &dev->cnq_array[i]); 362*ec72fce4SRam Amrani dev->int_info.used_cnt++; 363*ec72fce4SRam Amrani } 364*ec72fce4SRam Amrani } 365*ec72fce4SRam Amrani 366*ec72fce4SRam Amrani return rc; 367*ec72fce4SRam Amrani } 368*ec72fce4SRam Amrani 369*ec72fce4SRam Amrani static int qedr_setup_irqs(struct qedr_dev *dev) 370*ec72fce4SRam Amrani { 371*ec72fce4SRam Amrani int rc; 372*ec72fce4SRam Amrani 373*ec72fce4SRam Amrani DP_DEBUG(dev, QEDR_MSG_INIT, "qedr_setup_irqs\n"); 374*ec72fce4SRam Amrani 375*ec72fce4SRam Amrani /* Learn Interrupt configuration */ 376*ec72fce4SRam Amrani rc = dev->ops->rdma_set_rdma_int(dev->cdev, dev->num_cnq); 377*ec72fce4SRam Amrani if (rc < 0) 378*ec72fce4SRam Amrani return rc; 379*ec72fce4SRam Amrani 380*ec72fce4SRam Amrani rc = dev->ops->rdma_get_rdma_int(dev->cdev, &dev->int_info); 381*ec72fce4SRam Amrani if (rc) { 382*ec72fce4SRam Amrani DP_DEBUG(dev, QEDR_MSG_INIT, "get_rdma_int failed\n"); 383*ec72fce4SRam Amrani return rc; 384*ec72fce4SRam Amrani } 385*ec72fce4SRam Amrani 386*ec72fce4SRam Amrani if (dev->int_info.msix_cnt) { 387*ec72fce4SRam Amrani DP_DEBUG(dev, QEDR_MSG_INIT, "rdma msix_cnt = %d\n", 388*ec72fce4SRam Amrani dev->int_info.msix_cnt); 389*ec72fce4SRam Amrani rc = qedr_req_msix_irqs(dev); 390*ec72fce4SRam Amrani if (rc) 391*ec72fce4SRam Amrani return rc; 392*ec72fce4SRam Amrani } 393*ec72fce4SRam Amrani 394*ec72fce4SRam Amrani DP_DEBUG(dev, QEDR_MSG_INIT, "qedr_setup_irqs succeeded\n"); 395*ec72fce4SRam Amrani 396*ec72fce4SRam Amrani return 0; 397*ec72fce4SRam Amrani } 398*ec72fce4SRam Amrani 399*ec72fce4SRam Amrani static int qedr_set_device_attr(struct qedr_dev *dev) 400*ec72fce4SRam Amrani { 401*ec72fce4SRam Amrani struct qed_rdma_device *qed_attr; 402*ec72fce4SRam Amrani struct qedr_device_attr *attr; 403*ec72fce4SRam Amrani u32 page_size; 404*ec72fce4SRam Amrani 405*ec72fce4SRam Amrani /* Part 1 - query core capabilities */ 406*ec72fce4SRam Amrani qed_attr = dev->ops->rdma_query_device(dev->rdma_ctx); 407*ec72fce4SRam Amrani 408*ec72fce4SRam Amrani /* Part 2 - check capabilities */ 409*ec72fce4SRam Amrani page_size = ~dev->attr.page_size_caps + 1; 410*ec72fce4SRam Amrani if (page_size > PAGE_SIZE) { 411*ec72fce4SRam Amrani DP_ERR(dev, 412*ec72fce4SRam Amrani "Kernel PAGE_SIZE is %ld which is smaller than minimum page size (%d) required by qedr\n", 413*ec72fce4SRam Amrani PAGE_SIZE, page_size); 414*ec72fce4SRam Amrani return -ENODEV; 415*ec72fce4SRam Amrani } 416*ec72fce4SRam Amrani 417*ec72fce4SRam Amrani /* Part 3 - copy and update capabilities */ 418*ec72fce4SRam Amrani attr = &dev->attr; 419*ec72fce4SRam Amrani attr->vendor_id = qed_attr->vendor_id; 420*ec72fce4SRam Amrani attr->vendor_part_id = qed_attr->vendor_part_id; 421*ec72fce4SRam Amrani attr->hw_ver = qed_attr->hw_ver; 422*ec72fce4SRam Amrani attr->fw_ver = qed_attr->fw_ver; 423*ec72fce4SRam Amrani attr->node_guid = qed_attr->node_guid; 424*ec72fce4SRam Amrani attr->sys_image_guid = qed_attr->sys_image_guid; 425*ec72fce4SRam Amrani attr->max_cnq = qed_attr->max_cnq; 426*ec72fce4SRam Amrani attr->max_sge = qed_attr->max_sge; 427*ec72fce4SRam Amrani attr->max_inline = qed_attr->max_inline; 428*ec72fce4SRam Amrani attr->max_sqe = min_t(u32, qed_attr->max_wqe, QEDR_MAX_SQE); 429*ec72fce4SRam Amrani attr->max_rqe = min_t(u32, qed_attr->max_wqe, QEDR_MAX_RQE); 430*ec72fce4SRam Amrani attr->max_qp_resp_rd_atomic_resc = qed_attr->max_qp_resp_rd_atomic_resc; 431*ec72fce4SRam Amrani attr->max_qp_req_rd_atomic_resc = qed_attr->max_qp_req_rd_atomic_resc; 432*ec72fce4SRam Amrani attr->max_dev_resp_rd_atomic_resc = 433*ec72fce4SRam Amrani qed_attr->max_dev_resp_rd_atomic_resc; 434*ec72fce4SRam Amrani attr->max_cq = qed_attr->max_cq; 435*ec72fce4SRam Amrani attr->max_qp = qed_attr->max_qp; 436*ec72fce4SRam Amrani attr->max_mr = qed_attr->max_mr; 437*ec72fce4SRam Amrani attr->max_mr_size = qed_attr->max_mr_size; 438*ec72fce4SRam Amrani attr->max_cqe = min_t(u64, qed_attr->max_cqe, QEDR_MAX_CQES); 439*ec72fce4SRam Amrani attr->max_mw = qed_attr->max_mw; 440*ec72fce4SRam Amrani attr->max_fmr = qed_attr->max_fmr; 441*ec72fce4SRam Amrani attr->max_mr_mw_fmr_pbl = qed_attr->max_mr_mw_fmr_pbl; 442*ec72fce4SRam Amrani attr->max_mr_mw_fmr_size = qed_attr->max_mr_mw_fmr_size; 443*ec72fce4SRam Amrani attr->max_pd = qed_attr->max_pd; 444*ec72fce4SRam Amrani attr->max_ah = qed_attr->max_ah; 445*ec72fce4SRam Amrani attr->max_pkey = qed_attr->max_pkey; 446*ec72fce4SRam Amrani attr->max_srq = qed_attr->max_srq; 447*ec72fce4SRam Amrani attr->max_srq_wr = qed_attr->max_srq_wr; 448*ec72fce4SRam Amrani attr->dev_caps = qed_attr->dev_caps; 449*ec72fce4SRam Amrani attr->page_size_caps = qed_attr->page_size_caps; 450*ec72fce4SRam Amrani attr->dev_ack_delay = qed_attr->dev_ack_delay; 451*ec72fce4SRam Amrani attr->reserved_lkey = qed_attr->reserved_lkey; 452*ec72fce4SRam Amrani attr->bad_pkey_counter = qed_attr->bad_pkey_counter; 453*ec72fce4SRam Amrani attr->max_stats_queues = qed_attr->max_stats_queues; 454*ec72fce4SRam Amrani 455*ec72fce4SRam Amrani return 0; 456*ec72fce4SRam Amrani } 457*ec72fce4SRam Amrani 458*ec72fce4SRam Amrani static int qedr_init_hw(struct qedr_dev *dev) 459*ec72fce4SRam Amrani { 460*ec72fce4SRam Amrani struct qed_rdma_add_user_out_params out_params; 461*ec72fce4SRam Amrani struct qed_rdma_start_in_params *in_params; 462*ec72fce4SRam Amrani struct qed_rdma_cnq_params *cur_pbl; 463*ec72fce4SRam Amrani struct qed_rdma_events events; 464*ec72fce4SRam Amrani dma_addr_t p_phys_table; 465*ec72fce4SRam Amrani u32 page_cnt; 466*ec72fce4SRam Amrani int rc = 0; 467*ec72fce4SRam Amrani int i; 468*ec72fce4SRam Amrani 469*ec72fce4SRam Amrani in_params = kzalloc(sizeof(*in_params), GFP_KERNEL); 470*ec72fce4SRam Amrani if (!in_params) { 471*ec72fce4SRam Amrani rc = -ENOMEM; 472*ec72fce4SRam Amrani goto out; 473*ec72fce4SRam Amrani } 474*ec72fce4SRam Amrani 475*ec72fce4SRam Amrani in_params->desired_cnq = dev->num_cnq; 476*ec72fce4SRam Amrani for (i = 0; i < dev->num_cnq; i++) { 477*ec72fce4SRam Amrani cur_pbl = &in_params->cnq_pbl_list[i]; 478*ec72fce4SRam Amrani 479*ec72fce4SRam Amrani page_cnt = qed_chain_get_page_cnt(&dev->cnq_array[i].pbl); 480*ec72fce4SRam Amrani cur_pbl->num_pbl_pages = page_cnt; 481*ec72fce4SRam Amrani 482*ec72fce4SRam Amrani p_phys_table = qed_chain_get_pbl_phys(&dev->cnq_array[i].pbl); 483*ec72fce4SRam Amrani cur_pbl->pbl_ptr = (u64)p_phys_table; 484*ec72fce4SRam Amrani } 485*ec72fce4SRam Amrani 486*ec72fce4SRam Amrani events.context = dev; 487*ec72fce4SRam Amrani 488*ec72fce4SRam Amrani in_params->events = &events; 489*ec72fce4SRam Amrani in_params->cq_mode = QED_RDMA_CQ_MODE_32_BITS; 490*ec72fce4SRam Amrani in_params->max_mtu = dev->ndev->mtu; 491*ec72fce4SRam Amrani ether_addr_copy(&in_params->mac_addr[0], dev->ndev->dev_addr); 492*ec72fce4SRam Amrani 493*ec72fce4SRam Amrani rc = dev->ops->rdma_init(dev->cdev, in_params); 494*ec72fce4SRam Amrani if (rc) 495*ec72fce4SRam Amrani goto out; 496*ec72fce4SRam Amrani 497*ec72fce4SRam Amrani rc = dev->ops->rdma_add_user(dev->rdma_ctx, &out_params); 498*ec72fce4SRam Amrani if (rc) 499*ec72fce4SRam Amrani goto out; 500*ec72fce4SRam Amrani 501*ec72fce4SRam Amrani dev->db_addr = (void *)(uintptr_t)out_params.dpi_addr; 502*ec72fce4SRam Amrani dev->db_phys_addr = out_params.dpi_phys_addr; 503*ec72fce4SRam Amrani dev->db_size = out_params.dpi_size; 504*ec72fce4SRam Amrani dev->dpi = out_params.dpi; 505*ec72fce4SRam Amrani 506*ec72fce4SRam Amrani rc = qedr_set_device_attr(dev); 507*ec72fce4SRam Amrani out: 508*ec72fce4SRam Amrani kfree(in_params); 509*ec72fce4SRam Amrani if (rc) 510*ec72fce4SRam Amrani DP_ERR(dev, "Init HW Failed rc = %d\n", rc); 511*ec72fce4SRam Amrani 512*ec72fce4SRam Amrani return rc; 513*ec72fce4SRam Amrani } 514*ec72fce4SRam Amrani 515*ec72fce4SRam Amrani void qedr_stop_hw(struct qedr_dev *dev) 516*ec72fce4SRam Amrani { 517*ec72fce4SRam Amrani dev->ops->rdma_remove_user(dev->rdma_ctx, dev->dpi); 518*ec72fce4SRam Amrani dev->ops->rdma_stop(dev->rdma_ctx); 519*ec72fce4SRam Amrani } 520*ec72fce4SRam Amrani 5212e0cbc4dSRam Amrani static struct qedr_dev *qedr_add(struct qed_dev *cdev, struct pci_dev *pdev, 5222e0cbc4dSRam Amrani struct net_device *ndev) 5232e0cbc4dSRam Amrani { 524*ec72fce4SRam Amrani struct qed_dev_rdma_info dev_info; 5252e0cbc4dSRam Amrani struct qedr_dev *dev; 5262e0cbc4dSRam Amrani int rc = 0, i; 5272e0cbc4dSRam Amrani 5282e0cbc4dSRam Amrani dev = (struct qedr_dev *)ib_alloc_device(sizeof(*dev)); 5292e0cbc4dSRam Amrani if (!dev) { 5302e0cbc4dSRam Amrani pr_err("Unable to allocate ib device\n"); 5312e0cbc4dSRam Amrani return NULL; 5322e0cbc4dSRam Amrani } 5332e0cbc4dSRam Amrani 5342e0cbc4dSRam Amrani DP_DEBUG(dev, QEDR_MSG_INIT, "qedr add device called\n"); 5352e0cbc4dSRam Amrani 5362e0cbc4dSRam Amrani dev->pdev = pdev; 5372e0cbc4dSRam Amrani dev->ndev = ndev; 5382e0cbc4dSRam Amrani dev->cdev = cdev; 5392e0cbc4dSRam Amrani 540*ec72fce4SRam Amrani qed_ops = qed_get_rdma_ops(); 541*ec72fce4SRam Amrani if (!qed_ops) { 542*ec72fce4SRam Amrani DP_ERR(dev, "Failed to get qed roce operations\n"); 543*ec72fce4SRam Amrani goto init_err; 544*ec72fce4SRam Amrani } 545*ec72fce4SRam Amrani 546*ec72fce4SRam Amrani dev->ops = qed_ops; 547*ec72fce4SRam Amrani rc = qed_ops->fill_dev_info(cdev, &dev_info); 548*ec72fce4SRam Amrani if (rc) 549*ec72fce4SRam Amrani goto init_err; 550*ec72fce4SRam Amrani 551*ec72fce4SRam Amrani dev->num_hwfns = dev_info.common.num_hwfns; 552*ec72fce4SRam Amrani dev->rdma_ctx = dev->ops->rdma_get_rdma_ctx(cdev); 553*ec72fce4SRam Amrani 554*ec72fce4SRam Amrani dev->num_cnq = dev->ops->rdma_get_min_cnq_msix(cdev); 555*ec72fce4SRam Amrani if (!dev->num_cnq) { 556*ec72fce4SRam Amrani DP_ERR(dev, "not enough CNQ resources.\n"); 557*ec72fce4SRam Amrani goto init_err; 558*ec72fce4SRam Amrani } 559*ec72fce4SRam Amrani 5602e0cbc4dSRam Amrani qedr_pci_set_atomic(dev, pdev); 5612e0cbc4dSRam Amrani 562*ec72fce4SRam Amrani rc = qedr_alloc_resources(dev); 563*ec72fce4SRam Amrani if (rc) 564*ec72fce4SRam Amrani goto init_err; 565*ec72fce4SRam Amrani 566*ec72fce4SRam Amrani rc = qedr_init_hw(dev); 567*ec72fce4SRam Amrani if (rc) 568*ec72fce4SRam Amrani goto alloc_err; 569*ec72fce4SRam Amrani 570*ec72fce4SRam Amrani rc = qedr_setup_irqs(dev); 571*ec72fce4SRam Amrani if (rc) 572*ec72fce4SRam Amrani goto irq_err; 573*ec72fce4SRam Amrani 5742e0cbc4dSRam Amrani rc = qedr_register_device(dev); 5752e0cbc4dSRam Amrani if (rc) { 5762e0cbc4dSRam Amrani DP_ERR(dev, "Unable to allocate register device\n"); 577*ec72fce4SRam Amrani goto reg_err; 5782e0cbc4dSRam Amrani } 5792e0cbc4dSRam Amrani 5802e0cbc4dSRam Amrani for (i = 0; i < ARRAY_SIZE(qedr_attributes); i++) 5812e0cbc4dSRam Amrani if (device_create_file(&dev->ibdev.dev, qedr_attributes[i])) 582*ec72fce4SRam Amrani goto reg_err; 5832e0cbc4dSRam Amrani 5842e0cbc4dSRam Amrani DP_DEBUG(dev, QEDR_MSG_INIT, "qedr driver loaded successfully\n"); 5852e0cbc4dSRam Amrani return dev; 5862e0cbc4dSRam Amrani 587*ec72fce4SRam Amrani reg_err: 588*ec72fce4SRam Amrani qedr_sync_free_irqs(dev); 589*ec72fce4SRam Amrani irq_err: 590*ec72fce4SRam Amrani qedr_stop_hw(dev); 591*ec72fce4SRam Amrani alloc_err: 592*ec72fce4SRam Amrani qedr_free_resources(dev); 5932e0cbc4dSRam Amrani init_err: 5942e0cbc4dSRam Amrani ib_dealloc_device(&dev->ibdev); 5952e0cbc4dSRam Amrani DP_ERR(dev, "qedr driver load failed rc=%d\n", rc); 5962e0cbc4dSRam Amrani 5972e0cbc4dSRam Amrani return NULL; 5982e0cbc4dSRam Amrani } 5992e0cbc4dSRam Amrani 6002e0cbc4dSRam Amrani static void qedr_remove(struct qedr_dev *dev) 6012e0cbc4dSRam Amrani { 6022e0cbc4dSRam Amrani /* First unregister with stack to stop all the active traffic 6032e0cbc4dSRam Amrani * of the registered clients. 6042e0cbc4dSRam Amrani */ 6052e0cbc4dSRam Amrani qedr_remove_sysfiles(dev); 6062e0cbc4dSRam Amrani 607*ec72fce4SRam Amrani qedr_stop_hw(dev); 608*ec72fce4SRam Amrani qedr_sync_free_irqs(dev); 609*ec72fce4SRam Amrani qedr_free_resources(dev); 6102e0cbc4dSRam Amrani ib_dealloc_device(&dev->ibdev); 6112e0cbc4dSRam Amrani } 6122e0cbc4dSRam Amrani 6132e0cbc4dSRam Amrani static int qedr_close(struct qedr_dev *dev) 6142e0cbc4dSRam Amrani { 6152e0cbc4dSRam Amrani qedr_ib_dispatch_event(dev, 1, IB_EVENT_PORT_ERR); 6162e0cbc4dSRam Amrani 6172e0cbc4dSRam Amrani return 0; 6182e0cbc4dSRam Amrani } 6192e0cbc4dSRam Amrani 6202e0cbc4dSRam Amrani static void qedr_shutdown(struct qedr_dev *dev) 6212e0cbc4dSRam Amrani { 6222e0cbc4dSRam Amrani qedr_close(dev); 6232e0cbc4dSRam Amrani qedr_remove(dev); 6242e0cbc4dSRam Amrani } 6252e0cbc4dSRam Amrani 6262e0cbc4dSRam Amrani /* event handling via NIC driver ensures that all the NIC specific 6272e0cbc4dSRam Amrani * initialization done before RoCE driver notifies 6282e0cbc4dSRam Amrani * event to stack. 6292e0cbc4dSRam Amrani */ 6302e0cbc4dSRam Amrani static void qedr_notify(struct qedr_dev *dev, enum qede_roce_event event) 6312e0cbc4dSRam Amrani { 6322e0cbc4dSRam Amrani switch (event) { 6332e0cbc4dSRam Amrani case QEDE_UP: 6342e0cbc4dSRam Amrani qedr_ib_dispatch_event(dev, 1, IB_EVENT_PORT_ACTIVE); 6352e0cbc4dSRam Amrani break; 6362e0cbc4dSRam Amrani case QEDE_DOWN: 6372e0cbc4dSRam Amrani qedr_close(dev); 6382e0cbc4dSRam Amrani break; 6392e0cbc4dSRam Amrani case QEDE_CLOSE: 6402e0cbc4dSRam Amrani qedr_shutdown(dev); 6412e0cbc4dSRam Amrani break; 6422e0cbc4dSRam Amrani case QEDE_CHANGE_ADDR: 6432e0cbc4dSRam Amrani qedr_ib_dispatch_event(dev, 1, IB_EVENT_GID_CHANGE); 6442e0cbc4dSRam Amrani break; 6452e0cbc4dSRam Amrani default: 6462e0cbc4dSRam Amrani pr_err("Event not supported\n"); 6472e0cbc4dSRam Amrani } 6482e0cbc4dSRam Amrani } 6492e0cbc4dSRam Amrani 6502e0cbc4dSRam Amrani static struct qedr_driver qedr_drv = { 6512e0cbc4dSRam Amrani .name = "qedr_driver", 6522e0cbc4dSRam Amrani .add = qedr_add, 6532e0cbc4dSRam Amrani .remove = qedr_remove, 6542e0cbc4dSRam Amrani .notify = qedr_notify, 6552e0cbc4dSRam Amrani }; 6562e0cbc4dSRam Amrani 6572e0cbc4dSRam Amrani static int __init qedr_init_module(void) 6582e0cbc4dSRam Amrani { 6592e0cbc4dSRam Amrani return qede_roce_register_driver(&qedr_drv); 6602e0cbc4dSRam Amrani } 6612e0cbc4dSRam Amrani 6622e0cbc4dSRam Amrani static void __exit qedr_exit_module(void) 6632e0cbc4dSRam Amrani { 6642e0cbc4dSRam Amrani qede_roce_unregister_driver(&qedr_drv); 6652e0cbc4dSRam Amrani } 6662e0cbc4dSRam Amrani 6672e0cbc4dSRam Amrani module_init(qedr_init_module); 6682e0cbc4dSRam Amrani module_exit(qedr_exit_module); 669