|
| 1 | +// SPDX-License-Identifier: GPL-2.0 |
| 2 | +/* Marvell MCS driver |
| 3 | + * |
| 4 | + * Copyright (C) 2022 Marvell. |
| 5 | + */ |
| 6 | + |
| 7 | +#include <linux/bitfield.h> |
| 8 | +#include <linux/delay.h> |
| 9 | +#include <linux/device.h> |
| 10 | +#include <linux/module.h> |
| 11 | +#include <linux/pci.h> |
| 12 | + |
| 13 | +#include "mcs.h" |
| 14 | +#include "mcs_reg.h" |
| 15 | + |
| 16 | +#define DRV_NAME "Marvell MCS Driver" |
| 17 | + |
| 18 | +#define PCI_CFG_REG_BAR_NUM 0 |
| 19 | + |
| 20 | +static const struct pci_device_id mcs_id_table[] = { |
| 21 | + { PCI_DEVICE(PCI_VENDOR_ID_CAVIUM, PCI_DEVID_CN10K_MCS) }, |
| 22 | + { 0, } /* end of table */ |
| 23 | +}; |
| 24 | + |
| 25 | +static LIST_HEAD(mcs_list); |
| 26 | + |
| 27 | +static void *alloc_mem(struct mcs *mcs, int n) |
| 28 | +{ |
| 29 | + return devm_kcalloc(mcs->dev, n, sizeof(u16), GFP_KERNEL); |
| 30 | +} |
| 31 | + |
| 32 | +static int mcs_alloc_struct_mem(struct mcs *mcs, struct mcs_rsrc_map *res) |
| 33 | +{ |
| 34 | + struct hwinfo *hw = mcs->hw; |
| 35 | + int err; |
| 36 | + |
| 37 | + res->flowid2pf_map = alloc_mem(mcs, hw->tcam_entries); |
| 38 | + if (!res->flowid2pf_map) |
| 39 | + return -ENOMEM; |
| 40 | + |
| 41 | + res->secy2pf_map = alloc_mem(mcs, hw->secy_entries); |
| 42 | + if (!res->secy2pf_map) |
| 43 | + return -ENOMEM; |
| 44 | + |
| 45 | + res->sc2pf_map = alloc_mem(mcs, hw->sc_entries); |
| 46 | + if (!res->sc2pf_map) |
| 47 | + return -ENOMEM; |
| 48 | + |
| 49 | + res->sa2pf_map = alloc_mem(mcs, hw->sa_entries); |
| 50 | + if (!res->sa2pf_map) |
| 51 | + return -ENOMEM; |
| 52 | + |
| 53 | + res->flowid2secy_map = alloc_mem(mcs, hw->tcam_entries); |
| 54 | + if (!res->flowid2secy_map) |
| 55 | + return -ENOMEM; |
| 56 | + |
| 57 | + res->flow_ids.max = hw->tcam_entries - MCS_RSRC_RSVD_CNT; |
| 58 | + err = rvu_alloc_bitmap(&res->flow_ids); |
| 59 | + if (err) |
| 60 | + return err; |
| 61 | + |
| 62 | + res->secy.max = hw->secy_entries - MCS_RSRC_RSVD_CNT; |
| 63 | + err = rvu_alloc_bitmap(&res->secy); |
| 64 | + if (err) |
| 65 | + return err; |
| 66 | + |
| 67 | + res->sc.max = hw->sc_entries; |
| 68 | + err = rvu_alloc_bitmap(&res->sc); |
| 69 | + if (err) |
| 70 | + return err; |
| 71 | + |
| 72 | + res->sa.max = hw->sa_entries; |
| 73 | + err = rvu_alloc_bitmap(&res->sa); |
| 74 | + if (err) |
| 75 | + return err; |
| 76 | + |
| 77 | + return 0; |
| 78 | +} |
| 79 | + |
| 80 | +int mcs_get_blkcnt(void) |
| 81 | +{ |
| 82 | + struct mcs *mcs; |
| 83 | + int idmax = -ENODEV; |
| 84 | + |
| 85 | + /* Check MCS block is present in hardware */ |
| 86 | + if (!pci_dev_present(mcs_id_table)) |
| 87 | + return 0; |
| 88 | + |
| 89 | + list_for_each_entry(mcs, &mcs_list, mcs_list) |
| 90 | + if (mcs->mcs_id > idmax) |
| 91 | + idmax = mcs->mcs_id; |
| 92 | + |
| 93 | + if (idmax < 0) |
| 94 | + return 0; |
| 95 | + |
| 96 | + return idmax + 1; |
| 97 | +} |
| 98 | + |
| 99 | +struct mcs *mcs_get_pdata(int mcs_id) |
| 100 | +{ |
| 101 | + struct mcs *mcs_dev; |
| 102 | + |
| 103 | + list_for_each_entry(mcs_dev, &mcs_list, mcs_list) { |
| 104 | + if (mcs_dev->mcs_id == mcs_id) |
| 105 | + return mcs_dev; |
| 106 | + } |
| 107 | + return NULL; |
| 108 | +} |
| 109 | + |
| 110 | +/* Set lmac to bypass/operational mode */ |
| 111 | +void mcs_set_lmac_mode(struct mcs *mcs, int lmac_id, u8 mode) |
| 112 | +{ |
| 113 | + u64 reg; |
| 114 | + |
| 115 | + reg = MCSX_MCS_TOP_SLAVE_CHANNEL_CFG(lmac_id * 2); |
| 116 | + mcs_reg_write(mcs, reg, (u64)mode); |
| 117 | +} |
| 118 | + |
| 119 | +void cn10kb_mcs_parser_cfg(struct mcs *mcs) |
| 120 | +{ |
| 121 | + u64 reg, val; |
| 122 | + |
| 123 | + /* VLAN CTag */ |
| 124 | + val = BIT_ULL(0) | (0x8100ull & 0xFFFF) << 1 | BIT_ULL(17); |
| 125 | + /* RX */ |
| 126 | + reg = MCSX_PEX_RX_SLAVE_VLAN_CFGX(0); |
| 127 | + mcs_reg_write(mcs, reg, val); |
| 128 | + |
| 129 | + /* TX */ |
| 130 | + reg = MCSX_PEX_TX_SLAVE_VLAN_CFGX(0); |
| 131 | + mcs_reg_write(mcs, reg, val); |
| 132 | + |
| 133 | + /* VLAN STag */ |
| 134 | + val = BIT_ULL(0) | (0x88a8ull & 0xFFFF) << 1 | BIT_ULL(18); |
| 135 | + /* RX */ |
| 136 | + reg = MCSX_PEX_RX_SLAVE_VLAN_CFGX(1); |
| 137 | + mcs_reg_write(mcs, reg, val); |
| 138 | + |
| 139 | + /* TX */ |
| 140 | + reg = MCSX_PEX_TX_SLAVE_VLAN_CFGX(1); |
| 141 | + mcs_reg_write(mcs, reg, val); |
| 142 | +} |
| 143 | + |
| 144 | +static void mcs_lmac_init(struct mcs *mcs, int lmac_id) |
| 145 | +{ |
| 146 | + u64 reg; |
| 147 | + |
| 148 | + /* Port mode 25GB */ |
| 149 | + reg = MCSX_PAB_RX_SLAVE_PORT_CFGX(lmac_id); |
| 150 | + mcs_reg_write(mcs, reg, 0); |
| 151 | + |
| 152 | + if (mcs->hw->mcs_blks > 1) { |
| 153 | + reg = MCSX_PAB_RX_SLAVE_FIFO_SKID_CFGX(lmac_id); |
| 154 | + mcs_reg_write(mcs, reg, 0xe000e); |
| 155 | + return; |
| 156 | + } |
| 157 | + |
| 158 | + reg = MCSX_PAB_TX_SLAVE_PORT_CFGX(lmac_id); |
| 159 | + mcs_reg_write(mcs, reg, 0); |
| 160 | +} |
| 161 | + |
| 162 | +int mcs_set_lmac_channels(int mcs_id, u16 base) |
| 163 | +{ |
| 164 | + struct mcs *mcs; |
| 165 | + int lmac; |
| 166 | + u64 cfg; |
| 167 | + |
| 168 | + mcs = mcs_get_pdata(mcs_id); |
| 169 | + if (!mcs) |
| 170 | + return -ENODEV; |
| 171 | + for (lmac = 0; lmac < mcs->hw->lmac_cnt; lmac++) { |
| 172 | + cfg = mcs_reg_read(mcs, MCSX_LINK_LMACX_CFG(lmac)); |
| 173 | + cfg &= ~(MCSX_LINK_LMAC_BASE_MASK | MCSX_LINK_LMAC_RANGE_MASK); |
| 174 | + cfg |= FIELD_PREP(MCSX_LINK_LMAC_RANGE_MASK, ilog2(16)); |
| 175 | + cfg |= FIELD_PREP(MCSX_LINK_LMAC_BASE_MASK, base); |
| 176 | + mcs_reg_write(mcs, MCSX_LINK_LMACX_CFG(lmac), cfg); |
| 177 | + base += 16; |
| 178 | + } |
| 179 | + return 0; |
| 180 | +} |
| 181 | + |
| 182 | +static int mcs_x2p_calibration(struct mcs *mcs) |
| 183 | +{ |
| 184 | + unsigned long timeout = jiffies + usecs_to_jiffies(20000); |
| 185 | + int i, err = 0; |
| 186 | + u64 val; |
| 187 | + |
| 188 | + /* set X2P calibration */ |
| 189 | + val = mcs_reg_read(mcs, MCSX_MIL_GLOBAL); |
| 190 | + val |= BIT_ULL(5); |
| 191 | + mcs_reg_write(mcs, MCSX_MIL_GLOBAL, val); |
| 192 | + |
| 193 | + /* Wait for calibration to complete */ |
| 194 | + while (!(mcs_reg_read(mcs, MCSX_MIL_RX_GBL_STATUS) & BIT_ULL(0))) { |
| 195 | + if (time_before(jiffies, timeout)) { |
| 196 | + usleep_range(80, 100); |
| 197 | + continue; |
| 198 | + } else { |
| 199 | + err = -EBUSY; |
| 200 | + dev_err(mcs->dev, "MCS X2P calibration failed..ignoring\n"); |
| 201 | + return err; |
| 202 | + } |
| 203 | + } |
| 204 | + |
| 205 | + val = mcs_reg_read(mcs, MCSX_MIL_RX_GBL_STATUS); |
| 206 | + for (i = 0; i < mcs->hw->mcs_x2p_intf; i++) { |
| 207 | + if (val & BIT_ULL(1 + i)) |
| 208 | + continue; |
| 209 | + err = -EBUSY; |
| 210 | + dev_err(mcs->dev, "MCS:%d didn't respond to X2P calibration\n", i); |
| 211 | + } |
| 212 | + /* Clear X2P calibrate */ |
| 213 | + mcs_reg_write(mcs, MCSX_MIL_GLOBAL, mcs_reg_read(mcs, MCSX_MIL_GLOBAL) & ~BIT_ULL(5)); |
| 214 | + |
| 215 | + return err; |
| 216 | +} |
| 217 | + |
| 218 | +static void mcs_set_external_bypass(struct mcs *mcs, u8 bypass) |
| 219 | +{ |
| 220 | + u64 val; |
| 221 | + |
| 222 | + /* Set MCS to external bypass */ |
| 223 | + val = mcs_reg_read(mcs, MCSX_MIL_GLOBAL); |
| 224 | + if (bypass) |
| 225 | + val |= BIT_ULL(6); |
| 226 | + else |
| 227 | + val &= ~BIT_ULL(6); |
| 228 | + mcs_reg_write(mcs, MCSX_MIL_GLOBAL, val); |
| 229 | +} |
| 230 | + |
| 231 | +static void mcs_global_cfg(struct mcs *mcs) |
| 232 | +{ |
| 233 | + /* Disable external bypass */ |
| 234 | + mcs_set_external_bypass(mcs, false); |
| 235 | + |
| 236 | + /* Set MCS to perform standard IEEE802.1AE macsec processing */ |
| 237 | + if (mcs->hw->mcs_blks == 1) { |
| 238 | + mcs_reg_write(mcs, MCSX_IP_MODE, BIT_ULL(3)); |
| 239 | + return; |
| 240 | + } |
| 241 | + |
| 242 | + mcs_reg_write(mcs, MCSX_BBE_RX_SLAVE_CAL_ENTRY, 0xe4); |
| 243 | + mcs_reg_write(mcs, MCSX_BBE_RX_SLAVE_CAL_LEN, 4); |
| 244 | +} |
| 245 | + |
| 246 | +void cn10kb_mcs_set_hw_capabilities(struct mcs *mcs) |
| 247 | +{ |
| 248 | + struct hwinfo *hw = mcs->hw; |
| 249 | + |
| 250 | + hw->tcam_entries = 128; /* TCAM entries */ |
| 251 | + hw->secy_entries = 128; /* SecY entries */ |
| 252 | + hw->sc_entries = 128; /* SC CAM entries */ |
| 253 | + hw->sa_entries = 256; /* SA entries */ |
| 254 | + hw->lmac_cnt = 20; /* lmacs/ports per mcs block */ |
| 255 | + hw->mcs_x2p_intf = 5; /* x2p clabration intf */ |
| 256 | + hw->mcs_blks = 1; /* MCS blocks */ |
| 257 | +} |
| 258 | + |
| 259 | +static struct mcs_ops cn10kb_mcs_ops = { |
| 260 | + .mcs_set_hw_capabilities = cn10kb_mcs_set_hw_capabilities, |
| 261 | + .mcs_parser_cfg = cn10kb_mcs_parser_cfg, |
| 262 | +}; |
| 263 | + |
| 264 | +static int mcs_probe(struct pci_dev *pdev, const struct pci_device_id *id) |
| 265 | +{ |
| 266 | + struct device *dev = &pdev->dev; |
| 267 | + int lmac, err = 0; |
| 268 | + struct mcs *mcs; |
| 269 | + |
| 270 | + mcs = devm_kzalloc(dev, sizeof(*mcs), GFP_KERNEL); |
| 271 | + if (!mcs) |
| 272 | + return -ENOMEM; |
| 273 | + |
| 274 | + mcs->hw = devm_kzalloc(dev, sizeof(struct hwinfo), GFP_KERNEL); |
| 275 | + if (!mcs->hw) |
| 276 | + return -ENOMEM; |
| 277 | + |
| 278 | + err = pci_enable_device(pdev); |
| 279 | + if (err) { |
| 280 | + dev_err(dev, "Failed to enable PCI device\n"); |
| 281 | + pci_set_drvdata(pdev, NULL); |
| 282 | + return err; |
| 283 | + } |
| 284 | + |
| 285 | + err = pci_request_regions(pdev, DRV_NAME); |
| 286 | + if (err) { |
| 287 | + dev_err(dev, "PCI request regions failed 0x%x\n", err); |
| 288 | + goto exit; |
| 289 | + } |
| 290 | + |
| 291 | + mcs->reg_base = pcim_iomap(pdev, PCI_CFG_REG_BAR_NUM, 0); |
| 292 | + if (!mcs->reg_base) { |
| 293 | + dev_err(dev, "mcs: Cannot map CSR memory space, aborting\n"); |
| 294 | + err = -ENOMEM; |
| 295 | + goto exit; |
| 296 | + } |
| 297 | + |
| 298 | + pci_set_drvdata(pdev, mcs); |
| 299 | + mcs->pdev = pdev; |
| 300 | + mcs->dev = &pdev->dev; |
| 301 | + |
| 302 | + if (pdev->subsystem_device == PCI_SUBSYS_DEVID_CN10K_B) |
| 303 | + mcs->mcs_ops = &cn10kb_mcs_ops; |
| 304 | + else |
| 305 | + mcs->mcs_ops = cnf10kb_get_mac_ops(); |
| 306 | + |
| 307 | + /* Set hardware capabilities */ |
| 308 | + mcs->mcs_ops->mcs_set_hw_capabilities(mcs); |
| 309 | + |
| 310 | + mcs_global_cfg(mcs); |
| 311 | + |
| 312 | + /* Perform X2P clibration */ |
| 313 | + err = mcs_x2p_calibration(mcs); |
| 314 | + if (err) |
| 315 | + goto err_x2p; |
| 316 | + |
| 317 | + mcs->mcs_id = (pci_resource_start(pdev, PCI_CFG_REG_BAR_NUM) >> 24) |
| 318 | + & MCS_ID_MASK; |
| 319 | + |
| 320 | + /* Set mcs tx side resources */ |
| 321 | + err = mcs_alloc_struct_mem(mcs, &mcs->tx); |
| 322 | + if (err) |
| 323 | + goto err_x2p; |
| 324 | + |
| 325 | + /* Set mcs rx side resources */ |
| 326 | + err = mcs_alloc_struct_mem(mcs, &mcs->rx); |
| 327 | + if (err) |
| 328 | + goto err_x2p; |
| 329 | + |
| 330 | + /* per port config */ |
| 331 | + for (lmac = 0; lmac < mcs->hw->lmac_cnt; lmac++) |
| 332 | + mcs_lmac_init(mcs, lmac); |
| 333 | + |
| 334 | + /* Parser configuration */ |
| 335 | + mcs->mcs_ops->mcs_parser_cfg(mcs); |
| 336 | + |
| 337 | + list_add(&mcs->mcs_list, &mcs_list); |
| 338 | + |
| 339 | + return 0; |
| 340 | + |
| 341 | +err_x2p: |
| 342 | + /* Enable external bypass */ |
| 343 | + mcs_set_external_bypass(mcs, true); |
| 344 | +exit: |
| 345 | + pci_release_regions(pdev); |
| 346 | + pci_disable_device(pdev); |
| 347 | + pci_set_drvdata(pdev, NULL); |
| 348 | + return err; |
| 349 | +} |
| 350 | + |
| 351 | +static void mcs_remove(struct pci_dev *pdev) |
| 352 | +{ |
| 353 | + struct mcs *mcs = pci_get_drvdata(pdev); |
| 354 | + |
| 355 | + /* Set MCS to external bypass */ |
| 356 | + mcs_set_external_bypass(mcs, true); |
| 357 | + pci_free_irq_vectors(pdev); |
| 358 | + pci_release_regions(pdev); |
| 359 | + pci_disable_device(pdev); |
| 360 | + pci_set_drvdata(pdev, NULL); |
| 361 | +} |
| 362 | + |
| 363 | +struct pci_driver mcs_driver = { |
| 364 | + .name = DRV_NAME, |
| 365 | + .id_table = mcs_id_table, |
| 366 | + .probe = mcs_probe, |
| 367 | + .remove = mcs_remove, |
| 368 | +}; |
0 commit comments