1 /* Broadcom NetXtreme-C/E network driver. 2 * 3 * Copyright (c) 2016 Broadcom Limited 4 * 5 * This program is free software; you can redistribute it and/or modify 6 * it under the terms of the GNU General Public License as published by 7 * the Free Software Foundation. 8 */ 9 10 #include <linux/module.h> 11 12 #include <linux/kernel.h> 13 #include <linux/errno.h> 14 #include <linux/interrupt.h> 15 #include <linux/pci.h> 16 #include <linux/netdevice.h> 17 #include <linux/rtnetlink.h> 18 #include <linux/bitops.h> 19 #include <linux/irq.h> 20 #include <asm/byteorder.h> 21 #include <linux/bitmap.h> 22 23 #include "bnxt_hsi.h" 24 #include "bnxt.h" 25 #include "bnxt_ulp.h" 26 27 static int bnxt_register_dev(struct bnxt_en_dev *edev, int ulp_id, 28 struct bnxt_ulp_ops *ulp_ops, void *handle) 29 { 30 struct net_device *dev = edev->net; 31 struct bnxt *bp = netdev_priv(dev); 32 struct bnxt_ulp *ulp; 33 34 ASSERT_RTNL(); 35 if (ulp_id >= BNXT_MAX_ULP) 36 return -EINVAL; 37 38 ulp = &edev->ulp_tbl[ulp_id]; 39 if (rcu_access_pointer(ulp->ulp_ops)) { 40 netdev_err(bp->dev, "ulp id %d already registered\n", ulp_id); 41 return -EBUSY; 42 } 43 if (ulp_id == BNXT_ROCE_ULP) { 44 unsigned int max_stat_ctxs; 45 46 max_stat_ctxs = bnxt_get_max_func_stat_ctxs(bp); 47 if (max_stat_ctxs <= BNXT_MIN_ROCE_STAT_CTXS || 48 bp->num_stat_ctxs == max_stat_ctxs) 49 return -ENOMEM; 50 bnxt_set_max_func_stat_ctxs(bp, max_stat_ctxs - 51 BNXT_MIN_ROCE_STAT_CTXS); 52 } 53 54 atomic_set(&ulp->ref_count, 0); 55 ulp->handle = handle; 56 rcu_assign_pointer(ulp->ulp_ops, ulp_ops); 57 58 if (ulp_id == BNXT_ROCE_ULP) { 59 if (test_bit(BNXT_STATE_OPEN, &bp->state)) 60 bnxt_hwrm_vnic_cfg(bp, 0); 61 } 62 63 return 0; 64 } 65 66 static int bnxt_unregister_dev(struct bnxt_en_dev *edev, int ulp_id) 67 { 68 struct net_device *dev = edev->net; 69 struct bnxt *bp = netdev_priv(dev); 70 struct bnxt_ulp *ulp; 71 int i = 0; 72 73 ASSERT_RTNL(); 74 if (ulp_id >= BNXT_MAX_ULP) 75 return -EINVAL; 76 77 ulp = &edev->ulp_tbl[ulp_id]; 78 if (!rcu_access_pointer(ulp->ulp_ops)) { 79 netdev_err(bp->dev, "ulp id %d not registered\n", ulp_id); 80 return -EINVAL; 81 } 82 if (ulp_id == BNXT_ROCE_ULP) { 83 unsigned int max_stat_ctxs; 84 85 max_stat_ctxs = bnxt_get_max_func_stat_ctxs(bp); 86 bnxt_set_max_func_stat_ctxs(bp, max_stat_ctxs + 1); 87 } 88 if (ulp->max_async_event_id) 89 bnxt_hwrm_func_rgtr_async_events(bp, NULL, 0); 90 91 RCU_INIT_POINTER(ulp->ulp_ops, NULL); 92 synchronize_rcu(); 93 ulp->max_async_event_id = 0; 94 ulp->async_events_bmap = NULL; 95 while (atomic_read(&ulp->ref_count) != 0 && i < 10) { 96 msleep(100); 97 i++; 98 } 99 return 0; 100 } 101 102 static int bnxt_req_msix_vecs(struct bnxt_en_dev *edev, int ulp_id, 103 struct bnxt_msix_entry *ent, int num_msix) 104 { 105 struct net_device *dev = edev->net; 106 struct bnxt *bp = netdev_priv(dev); 107 int max_idx, max_cp_rings; 108 int avail_msix, i, idx; 109 110 ASSERT_RTNL(); 111 if (ulp_id != BNXT_ROCE_ULP) 112 return -EINVAL; 113 114 if (!(bp->flags & BNXT_FLAG_USING_MSIX)) 115 return -ENODEV; 116 117 max_cp_rings = bnxt_get_max_func_cp_rings(bp); 118 max_idx = min_t(int, bp->total_irqs, max_cp_rings); 119 avail_msix = max_idx - bp->cp_nr_rings; 120 if (!avail_msix) 121 return -ENOMEM; 122 if (avail_msix > num_msix) 123 avail_msix = num_msix; 124 125 idx = max_idx - avail_msix; 126 for (i = 0; i < avail_msix; i++) { 127 ent[i].vector = bp->irq_tbl[idx + i].vector; 128 ent[i].ring_idx = idx + i; 129 ent[i].db_offset = (idx + i) * 0x80; 130 } 131 bnxt_set_max_func_irqs(bp, max_idx - avail_msix); 132 bnxt_set_max_func_cp_rings(bp, max_cp_rings - avail_msix); 133 edev->ulp_tbl[ulp_id].msix_requested = avail_msix; 134 return avail_msix; 135 } 136 137 static int bnxt_free_msix_vecs(struct bnxt_en_dev *edev, int ulp_id) 138 { 139 struct net_device *dev = edev->net; 140 struct bnxt *bp = netdev_priv(dev); 141 int max_cp_rings, msix_requested; 142 143 ASSERT_RTNL(); 144 if (ulp_id != BNXT_ROCE_ULP) 145 return -EINVAL; 146 147 max_cp_rings = bnxt_get_max_func_cp_rings(bp); 148 msix_requested = edev->ulp_tbl[ulp_id].msix_requested; 149 bnxt_set_max_func_cp_rings(bp, max_cp_rings + msix_requested); 150 edev->ulp_tbl[ulp_id].msix_requested = 0; 151 bnxt_set_max_func_irqs(bp, bp->total_irqs); 152 return 0; 153 } 154 155 void bnxt_subtract_ulp_resources(struct bnxt *bp, int ulp_id) 156 { 157 ASSERT_RTNL(); 158 if (bnxt_ulp_registered(bp->edev, ulp_id)) { 159 struct bnxt_en_dev *edev = bp->edev; 160 unsigned int msix_req, max; 161 162 msix_req = edev->ulp_tbl[ulp_id].msix_requested; 163 max = bnxt_get_max_func_cp_rings(bp); 164 bnxt_set_max_func_cp_rings(bp, max - msix_req); 165 max = bnxt_get_max_func_stat_ctxs(bp); 166 bnxt_set_max_func_stat_ctxs(bp, max - 1); 167 } 168 } 169 170 static int bnxt_send_msg(struct bnxt_en_dev *edev, int ulp_id, 171 struct bnxt_fw_msg *fw_msg) 172 { 173 struct net_device *dev = edev->net; 174 struct bnxt *bp = netdev_priv(dev); 175 struct input *req; 176 int rc; 177 178 mutex_lock(&bp->hwrm_cmd_lock); 179 req = fw_msg->msg; 180 req->resp_addr = cpu_to_le64(bp->hwrm_cmd_resp_dma_addr); 181 rc = _hwrm_send_message(bp, fw_msg->msg, fw_msg->msg_len, 182 fw_msg->timeout); 183 if (!rc) { 184 struct output *resp = bp->hwrm_cmd_resp_addr; 185 u32 len = le16_to_cpu(resp->resp_len); 186 187 if (fw_msg->resp_max_len < len) 188 len = fw_msg->resp_max_len; 189 190 memcpy(fw_msg->resp, resp, len); 191 } 192 mutex_unlock(&bp->hwrm_cmd_lock); 193 return rc; 194 } 195 196 static void bnxt_ulp_get(struct bnxt_ulp *ulp) 197 { 198 atomic_inc(&ulp->ref_count); 199 } 200 201 static void bnxt_ulp_put(struct bnxt_ulp *ulp) 202 { 203 atomic_dec(&ulp->ref_count); 204 } 205 206 void bnxt_ulp_stop(struct bnxt *bp) 207 { 208 struct bnxt_en_dev *edev = bp->edev; 209 struct bnxt_ulp_ops *ops; 210 int i; 211 212 if (!edev) 213 return; 214 215 for (i = 0; i < BNXT_MAX_ULP; i++) { 216 struct bnxt_ulp *ulp = &edev->ulp_tbl[i]; 217 218 ops = rtnl_dereference(ulp->ulp_ops); 219 if (!ops || !ops->ulp_stop) 220 continue; 221 ops->ulp_stop(ulp->handle); 222 } 223 } 224 225 void bnxt_ulp_start(struct bnxt *bp) 226 { 227 struct bnxt_en_dev *edev = bp->edev; 228 struct bnxt_ulp_ops *ops; 229 int i; 230 231 if (!edev) 232 return; 233 234 for (i = 0; i < BNXT_MAX_ULP; i++) { 235 struct bnxt_ulp *ulp = &edev->ulp_tbl[i]; 236 237 ops = rtnl_dereference(ulp->ulp_ops); 238 if (!ops || !ops->ulp_start) 239 continue; 240 ops->ulp_start(ulp->handle); 241 } 242 } 243 244 void bnxt_ulp_sriov_cfg(struct bnxt *bp, int num_vfs) 245 { 246 struct bnxt_en_dev *edev = bp->edev; 247 struct bnxt_ulp_ops *ops; 248 int i; 249 250 if (!edev) 251 return; 252 253 for (i = 0; i < BNXT_MAX_ULP; i++) { 254 struct bnxt_ulp *ulp = &edev->ulp_tbl[i]; 255 256 rcu_read_lock(); 257 ops = rcu_dereference(ulp->ulp_ops); 258 if (!ops || !ops->ulp_sriov_config) { 259 rcu_read_unlock(); 260 continue; 261 } 262 bnxt_ulp_get(ulp); 263 rcu_read_unlock(); 264 ops->ulp_sriov_config(ulp->handle, num_vfs); 265 bnxt_ulp_put(ulp); 266 } 267 } 268 269 void bnxt_ulp_async_events(struct bnxt *bp, struct hwrm_async_event_cmpl *cmpl) 270 { 271 u16 event_id = le16_to_cpu(cmpl->event_id); 272 struct bnxt_en_dev *edev = bp->edev; 273 struct bnxt_ulp_ops *ops; 274 int i; 275 276 if (!edev) 277 return; 278 279 rcu_read_lock(); 280 for (i = 0; i < BNXT_MAX_ULP; i++) { 281 struct bnxt_ulp *ulp = &edev->ulp_tbl[i]; 282 283 ops = rcu_dereference(ulp->ulp_ops); 284 if (!ops || !ops->ulp_async_notifier) 285 continue; 286 if (!ulp->async_events_bmap || 287 event_id > ulp->max_async_event_id) 288 continue; 289 290 /* Read max_async_event_id first before testing the bitmap. */ 291 smp_rmb(); 292 if (test_bit(event_id, ulp->async_events_bmap)) 293 ops->ulp_async_notifier(ulp->handle, cmpl); 294 } 295 rcu_read_unlock(); 296 } 297 298 static int bnxt_register_async_events(struct bnxt_en_dev *edev, int ulp_id, 299 unsigned long *events_bmap, u16 max_id) 300 { 301 struct net_device *dev = edev->net; 302 struct bnxt *bp = netdev_priv(dev); 303 struct bnxt_ulp *ulp; 304 305 if (ulp_id >= BNXT_MAX_ULP) 306 return -EINVAL; 307 308 ulp = &edev->ulp_tbl[ulp_id]; 309 ulp->async_events_bmap = events_bmap; 310 /* Make sure bnxt_ulp_async_events() sees this order */ 311 smp_wmb(); 312 ulp->max_async_event_id = max_id; 313 bnxt_hwrm_func_rgtr_async_events(bp, events_bmap, max_id + 1); 314 return 0; 315 } 316 317 static const struct bnxt_en_ops bnxt_en_ops_tbl = { 318 .bnxt_register_device = bnxt_register_dev, 319 .bnxt_unregister_device = bnxt_unregister_dev, 320 .bnxt_request_msix = bnxt_req_msix_vecs, 321 .bnxt_free_msix = bnxt_free_msix_vecs, 322 .bnxt_send_fw_msg = bnxt_send_msg, 323 .bnxt_register_fw_async_events = bnxt_register_async_events, 324 }; 325 326 struct bnxt_en_dev *bnxt_ulp_probe(struct net_device *dev) 327 { 328 struct bnxt *bp = netdev_priv(dev); 329 struct bnxt_en_dev *edev; 330 331 edev = bp->edev; 332 if (!edev) { 333 edev = kzalloc(sizeof(*edev), GFP_KERNEL); 334 if (!edev) 335 return ERR_PTR(-ENOMEM); 336 edev->en_ops = &bnxt_en_ops_tbl; 337 if (bp->flags & BNXT_FLAG_ROCEV1_CAP) 338 edev->flags |= BNXT_EN_FLAG_ROCEV1_CAP; 339 if (bp->flags & BNXT_FLAG_ROCEV2_CAP) 340 edev->flags |= BNXT_EN_FLAG_ROCEV2_CAP; 341 edev->net = dev; 342 edev->pdev = bp->pdev; 343 bp->edev = edev; 344 } 345 return bp->edev; 346 } 347