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