1 // SPDX-License-Identifier: (GPL-2.0 OR MIT)
3 * Driver for Microsemi VSC85xx PHYs
5 * Author: Nagaraju Lakkaraju
6 * License: Dual MIT/GPL
7 * Copyright (c) 2016 Microsemi Corporation
10 #include <linux/phy.h>
11 #include <dt-bindings/net/mscc-phy-vsc8531.h>
13 #include <crypto/skcipher.h>
15 #include <net/macsec.h>
19 #include "mscc_macsec.h"
20 #include "mscc_fc_buffer.h"
22 static u32 vsc8584_macsec_phy_read(struct phy_device *phydev,
23 enum macsec_bank bank, u32 reg)
25 u32 val, val_l = 0, val_h = 0;
26 unsigned long deadline;
29 rc = phy_select_page(phydev, MSCC_PHY_PAGE_MACSEC);
33 __phy_write(phydev, MSCC_EXT_PAGE_MACSEC_20,
34 MSCC_PHY_MACSEC_20_TARGET(bank >> 2));
37 /* non-MACsec access */
42 __phy_write(phydev, MSCC_EXT_PAGE_MACSEC_19,
43 MSCC_PHY_MACSEC_19_CMD | MSCC_PHY_MACSEC_19_READ |
44 MSCC_PHY_MACSEC_19_REG_ADDR(reg) |
45 MSCC_PHY_MACSEC_19_TARGET(bank));
47 deadline = jiffies + msecs_to_jiffies(PROC_CMD_NCOMPLETED_TIMEOUT_MS);
49 val = __phy_read(phydev, MSCC_EXT_PAGE_MACSEC_19);
50 } while (time_before(jiffies, deadline) && !(val & MSCC_PHY_MACSEC_19_CMD));
52 val_l = __phy_read(phydev, MSCC_EXT_PAGE_MACSEC_17);
53 val_h = __phy_read(phydev, MSCC_EXT_PAGE_MACSEC_18);
56 phy_restore_page(phydev, rc, rc);
58 return (val_h << 16) | val_l;
61 static void vsc8584_macsec_phy_write(struct phy_device *phydev,
62 enum macsec_bank bank, u32 reg, u32 val)
64 unsigned long deadline;
67 rc = phy_select_page(phydev, MSCC_PHY_PAGE_MACSEC);
71 __phy_write(phydev, MSCC_EXT_PAGE_MACSEC_20,
72 MSCC_PHY_MACSEC_20_TARGET(bank >> 2));
74 if ((bank >> 2 == 0x1) || (bank >> 2 == 0x3))
80 __phy_write(phydev, MSCC_EXT_PAGE_MACSEC_17, (u16)val);
81 __phy_write(phydev, MSCC_EXT_PAGE_MACSEC_18, (u16)(val >> 16));
83 __phy_write(phydev, MSCC_EXT_PAGE_MACSEC_19,
84 MSCC_PHY_MACSEC_19_CMD | MSCC_PHY_MACSEC_19_REG_ADDR(reg) |
85 MSCC_PHY_MACSEC_19_TARGET(bank));
87 deadline = jiffies + msecs_to_jiffies(PROC_CMD_NCOMPLETED_TIMEOUT_MS);
89 val = __phy_read(phydev, MSCC_EXT_PAGE_MACSEC_19);
90 } while (time_before(jiffies, deadline) && !(val & MSCC_PHY_MACSEC_19_CMD));
93 phy_restore_page(phydev, rc, rc);
96 static void vsc8584_macsec_classification(struct phy_device *phydev,
97 enum macsec_bank bank)
99 /* enable VLAN tag parsing */
100 vsc8584_macsec_phy_write(phydev, bank, MSCC_MS_SAM_CP_TAG,
101 MSCC_MS_SAM_CP_TAG_PARSE_STAG |
102 MSCC_MS_SAM_CP_TAG_PARSE_QTAG |
103 MSCC_MS_SAM_CP_TAG_PARSE_QINQ);
106 static void vsc8584_macsec_flow_default_action(struct phy_device *phydev,
107 enum macsec_bank bank,
110 u32 port = (bank == MACSEC_INGR) ?
111 MSCC_MS_PORT_UNCONTROLLED : MSCC_MS_PORT_COMMON;
112 u32 action = MSCC_MS_FLOW_BYPASS;
115 action = MSCC_MS_FLOW_DROP;
117 vsc8584_macsec_phy_write(phydev, bank, MSCC_MS_SAM_NM_FLOW_NCP,
118 /* MACsec untagged */
119 MSCC_MS_SAM_NM_FLOW_NCP_UNTAGGED_FLOW_TYPE(action) |
120 MSCC_MS_SAM_NM_FLOW_NCP_UNTAGGED_DROP_ACTION(MSCC_MS_ACTION_DROP) |
121 MSCC_MS_SAM_NM_FLOW_NCP_UNTAGGED_DEST_PORT(port) |
123 MSCC_MS_SAM_NM_FLOW_NCP_TAGGED_FLOW_TYPE(action) |
124 MSCC_MS_SAM_NM_FLOW_NCP_TAGGED_DROP_ACTION(MSCC_MS_ACTION_DROP) |
125 MSCC_MS_SAM_NM_FLOW_NCP_TAGGED_DEST_PORT(port) |
127 MSCC_MS_SAM_NM_FLOW_NCP_BADTAG_FLOW_TYPE(action) |
128 MSCC_MS_SAM_NM_FLOW_NCP_BADTAG_DROP_ACTION(MSCC_MS_ACTION_DROP) |
129 MSCC_MS_SAM_NM_FLOW_NCP_BADTAG_DEST_PORT(port) |
131 MSCC_MS_SAM_NM_FLOW_NCP_KAY_FLOW_TYPE(action) |
132 MSCC_MS_SAM_NM_FLOW_NCP_KAY_DROP_ACTION(MSCC_MS_ACTION_DROP) |
133 MSCC_MS_SAM_NM_FLOW_NCP_KAY_DEST_PORT(port));
134 vsc8584_macsec_phy_write(phydev, bank, MSCC_MS_SAM_NM_FLOW_CP,
135 /* MACsec untagged */
136 MSCC_MS_SAM_NM_FLOW_NCP_UNTAGGED_FLOW_TYPE(action) |
137 MSCC_MS_SAM_NM_FLOW_CP_UNTAGGED_DROP_ACTION(MSCC_MS_ACTION_DROP) |
138 MSCC_MS_SAM_NM_FLOW_CP_UNTAGGED_DEST_PORT(port) |
140 MSCC_MS_SAM_NM_FLOW_NCP_TAGGED_FLOW_TYPE(action) |
141 MSCC_MS_SAM_NM_FLOW_CP_TAGGED_DROP_ACTION(MSCC_MS_ACTION_DROP) |
142 MSCC_MS_SAM_NM_FLOW_CP_TAGGED_DEST_PORT(port) |
144 MSCC_MS_SAM_NM_FLOW_NCP_BADTAG_FLOW_TYPE(action) |
145 MSCC_MS_SAM_NM_FLOW_CP_BADTAG_DROP_ACTION(MSCC_MS_ACTION_DROP) |
146 MSCC_MS_SAM_NM_FLOW_CP_BADTAG_DEST_PORT(port) |
148 MSCC_MS_SAM_NM_FLOW_NCP_KAY_FLOW_TYPE(action) |
149 MSCC_MS_SAM_NM_FLOW_CP_KAY_DROP_ACTION(MSCC_MS_ACTION_DROP) |
150 MSCC_MS_SAM_NM_FLOW_CP_KAY_DEST_PORT(port));
153 static void vsc8584_macsec_integrity_checks(struct phy_device *phydev,
154 enum macsec_bank bank)
158 if (bank != MACSEC_INGR)
161 /* Set default rules to pass unmatched frames */
162 val = vsc8584_macsec_phy_read(phydev, bank,
163 MSCC_MS_PARAMS2_IG_CC_CONTROL);
164 val |= MSCC_MS_PARAMS2_IG_CC_CONTROL_NON_MATCH_CTRL_ACT |
165 MSCC_MS_PARAMS2_IG_CC_CONTROL_NON_MATCH_ACT;
166 vsc8584_macsec_phy_write(phydev, bank, MSCC_MS_PARAMS2_IG_CC_CONTROL,
169 vsc8584_macsec_phy_write(phydev, bank, MSCC_MS_PARAMS2_IG_CP_TAG,
170 MSCC_MS_PARAMS2_IG_CP_TAG_PARSE_STAG |
171 MSCC_MS_PARAMS2_IG_CP_TAG_PARSE_QTAG |
172 MSCC_MS_PARAMS2_IG_CP_TAG_PARSE_QINQ);
175 static void vsc8584_macsec_block_init(struct phy_device *phydev,
176 enum macsec_bank bank)
181 vsc8584_macsec_phy_write(phydev, bank, MSCC_MS_ENA_CFG,
182 MSCC_MS_ENA_CFG_SW_RST |
183 MSCC_MS_ENA_CFG_MACSEC_BYPASS_ENA);
185 /* Set the MACsec block out of s/w reset and enable clocks */
186 vsc8584_macsec_phy_write(phydev, bank, MSCC_MS_ENA_CFG,
187 MSCC_MS_ENA_CFG_CLK_ENA);
189 vsc8584_macsec_phy_write(phydev, bank, MSCC_MS_STATUS_CONTEXT_CTRL,
190 bank == MACSEC_INGR ? 0xe5880214 : 0xe5880218);
191 vsc8584_macsec_phy_write(phydev, bank, MSCC_MS_MISC_CONTROL,
192 MSCC_MS_MISC_CONTROL_MC_LATENCY_FIX(bank == MACSEC_INGR ? 57 : 40) |
193 MSCC_MS_MISC_CONTROL_XFORM_REC_SIZE(bank == MACSEC_INGR ? 1 : 2));
195 /* Clear the counters */
196 val = vsc8584_macsec_phy_read(phydev, bank, MSCC_MS_COUNT_CONTROL);
197 val |= MSCC_MS_COUNT_CONTROL_AUTO_CNTR_RESET;
198 vsc8584_macsec_phy_write(phydev, bank, MSCC_MS_COUNT_CONTROL, val);
200 /* Enable octet increment mode */
201 vsc8584_macsec_phy_write(phydev, bank, MSCC_MS_PP_CTRL,
202 MSCC_MS_PP_CTRL_MACSEC_OCTET_INCR_MODE);
204 vsc8584_macsec_phy_write(phydev, bank, MSCC_MS_BLOCK_CTX_UPDATE, 0x3);
206 val = vsc8584_macsec_phy_read(phydev, bank, MSCC_MS_COUNT_CONTROL);
207 val |= MSCC_MS_COUNT_CONTROL_RESET_ALL;
208 vsc8584_macsec_phy_write(phydev, bank, MSCC_MS_COUNT_CONTROL, val);
211 vsc8584_macsec_phy_write(phydev, bank, MSCC_MS_NON_VLAN_MTU_CHECK,
212 MSCC_MS_NON_VLAN_MTU_CHECK_NV_MTU_COMPARE(32761) |
213 MSCC_MS_NON_VLAN_MTU_CHECK_NV_MTU_COMP_DROP);
215 for (i = 0; i < 8; i++)
216 vsc8584_macsec_phy_write(phydev, bank, MSCC_MS_VLAN_MTU_CHECK(i),
217 MSCC_MS_VLAN_MTU_CHECK_MTU_COMPARE(32761) |
218 MSCC_MS_VLAN_MTU_CHECK_MTU_COMP_DROP);
220 if (bank == MACSEC_EGR) {
221 val = vsc8584_macsec_phy_read(phydev, bank, MSCC_MS_INTR_CTRL_STATUS);
222 val &= ~MSCC_MS_INTR_CTRL_STATUS_INTR_ENABLE_M;
223 vsc8584_macsec_phy_write(phydev, bank, MSCC_MS_INTR_CTRL_STATUS, val);
225 vsc8584_macsec_phy_write(phydev, bank, MSCC_MS_FC_CFG,
226 MSCC_MS_FC_CFG_FCBUF_ENA |
227 MSCC_MS_FC_CFG_LOW_THRESH(0x1) |
228 MSCC_MS_FC_CFG_HIGH_THRESH(0x4) |
229 MSCC_MS_FC_CFG_LOW_BYTES_VAL(0x4) |
230 MSCC_MS_FC_CFG_HIGH_BYTES_VAL(0x6));
233 vsc8584_macsec_classification(phydev, bank);
234 vsc8584_macsec_flow_default_action(phydev, bank, false);
235 vsc8584_macsec_integrity_checks(phydev, bank);
237 /* Enable the MACsec block */
238 vsc8584_macsec_phy_write(phydev, bank, MSCC_MS_ENA_CFG,
239 MSCC_MS_ENA_CFG_CLK_ENA |
240 MSCC_MS_ENA_CFG_MACSEC_ENA |
241 MSCC_MS_ENA_CFG_MACSEC_SPEED_MODE(0x5));
244 static void vsc8584_macsec_mac_init(struct phy_device *phydev,
245 enum macsec_bank bank)
250 /* Clear host & line stats */
251 for (i = 0; i < 36; i++)
252 vsc8584_macsec_phy_write(phydev, bank, 0x1c + i, 0);
254 val = vsc8584_macsec_phy_read(phydev, bank,
255 MSCC_MAC_PAUSE_CFG_TX_FRAME_CTRL);
256 val &= ~MSCC_MAC_PAUSE_CFG_TX_FRAME_CTRL_PAUSE_MODE_M;
257 val |= MSCC_MAC_PAUSE_CFG_TX_FRAME_CTRL_PAUSE_MODE(2) |
258 MSCC_MAC_PAUSE_CFG_TX_FRAME_CTRL_PAUSE_VALUE(0xffff);
259 vsc8584_macsec_phy_write(phydev, bank,
260 MSCC_MAC_PAUSE_CFG_TX_FRAME_CTRL, val);
262 val = vsc8584_macsec_phy_read(phydev, bank,
263 MSCC_MAC_PAUSE_CFG_TX_FRAME_CTRL_2);
265 vsc8584_macsec_phy_write(phydev, bank,
266 MSCC_MAC_PAUSE_CFG_TX_FRAME_CTRL_2, val);
268 val = vsc8584_macsec_phy_read(phydev, bank,
269 MSCC_MAC_PAUSE_CFG_RX_FRAME_CTRL);
270 if (bank == HOST_MAC)
271 val |= MSCC_MAC_PAUSE_CFG_RX_FRAME_CTRL_PAUSE_TIMER_ENA |
272 MSCC_MAC_PAUSE_CFG_RX_FRAME_CTRL_PAUSE_FRAME_DROP_ENA;
274 val |= MSCC_MAC_PAUSE_CFG_RX_FRAME_CTRL_PAUSE_REACT_ENA |
275 MSCC_MAC_PAUSE_CFG_RX_FRAME_CTRL_PAUSE_FRAME_DROP_ENA |
276 MSCC_MAC_PAUSE_CFG_RX_FRAME_CTRL_PAUSE_MODE |
277 MSCC_MAC_PAUSE_CFG_RX_FRAME_CTRL_EARLY_PAUSE_DETECT_ENA;
278 vsc8584_macsec_phy_write(phydev, bank,
279 MSCC_MAC_PAUSE_CFG_RX_FRAME_CTRL, val);
281 vsc8584_macsec_phy_write(phydev, bank, MSCC_MAC_CFG_PKTINF_CFG,
282 MSCC_MAC_CFG_PKTINF_CFG_STRIP_FCS_ENA |
283 MSCC_MAC_CFG_PKTINF_CFG_INSERT_FCS_ENA |
284 MSCC_MAC_CFG_PKTINF_CFG_LPI_RELAY_ENA |
285 MSCC_MAC_CFG_PKTINF_CFG_STRIP_PREAMBLE_ENA |
286 MSCC_MAC_CFG_PKTINF_CFG_INSERT_PREAMBLE_ENA |
288 MSCC_MAC_CFG_PKTINF_CFG_ENABLE_TX_PADDING : 0));
290 val = vsc8584_macsec_phy_read(phydev, bank, MSCC_MAC_CFG_MODE_CFG);
291 val &= ~MSCC_MAC_CFG_MODE_CFG_DISABLE_DIC;
292 vsc8584_macsec_phy_write(phydev, bank, MSCC_MAC_CFG_MODE_CFG, val);
294 val = vsc8584_macsec_phy_read(phydev, bank, MSCC_MAC_CFG_MAXLEN_CFG);
295 val &= ~MSCC_MAC_CFG_MAXLEN_CFG_MAX_LEN_M;
296 val |= MSCC_MAC_CFG_MAXLEN_CFG_MAX_LEN(10240);
297 vsc8584_macsec_phy_write(phydev, bank, MSCC_MAC_CFG_MAXLEN_CFG, val);
299 vsc8584_macsec_phy_write(phydev, bank, MSCC_MAC_CFG_ADV_CHK_CFG,
300 MSCC_MAC_CFG_ADV_CHK_CFG_SFD_CHK_ENA |
301 MSCC_MAC_CFG_ADV_CHK_CFG_PRM_CHK_ENA |
302 MSCC_MAC_CFG_ADV_CHK_CFG_OOR_ERR_ENA |
303 MSCC_MAC_CFG_ADV_CHK_CFG_INR_ERR_ENA);
305 val = vsc8584_macsec_phy_read(phydev, bank, MSCC_MAC_CFG_LFS_CFG);
306 val &= ~MSCC_MAC_CFG_LFS_CFG_LFS_MODE_ENA;
307 vsc8584_macsec_phy_write(phydev, bank, MSCC_MAC_CFG_LFS_CFG, val);
309 vsc8584_macsec_phy_write(phydev, bank, MSCC_MAC_CFG_ENA_CFG,
310 MSCC_MAC_CFG_ENA_CFG_RX_CLK_ENA |
311 MSCC_MAC_CFG_ENA_CFG_TX_CLK_ENA |
312 MSCC_MAC_CFG_ENA_CFG_RX_ENA |
313 MSCC_MAC_CFG_ENA_CFG_TX_ENA);
316 /* Must be called with mdio_lock taken */
317 static int __vsc8584_macsec_init(struct phy_device *phydev)
319 struct vsc8531_private *priv = phydev->priv;
320 enum macsec_bank proc_bank;
323 vsc8584_macsec_block_init(phydev, MACSEC_INGR);
324 vsc8584_macsec_block_init(phydev, MACSEC_EGR);
325 vsc8584_macsec_mac_init(phydev, HOST_MAC);
326 vsc8584_macsec_mac_init(phydev, LINE_MAC);
328 vsc8584_macsec_phy_write(phydev, FC_BUFFER,
329 MSCC_FCBUF_FC_READ_THRESH_CFG,
330 MSCC_FCBUF_FC_READ_THRESH_CFG_TX_THRESH(4) |
331 MSCC_FCBUF_FC_READ_THRESH_CFG_RX_THRESH(5));
333 val = vsc8584_macsec_phy_read(phydev, FC_BUFFER, MSCC_FCBUF_MODE_CFG);
334 val |= MSCC_FCBUF_MODE_CFG_PAUSE_GEN_ENA |
335 MSCC_FCBUF_MODE_CFG_RX_PPM_RATE_ADAPT_ENA |
336 MSCC_FCBUF_MODE_CFG_TX_PPM_RATE_ADAPT_ENA;
337 vsc8584_macsec_phy_write(phydev, FC_BUFFER, MSCC_FCBUF_MODE_CFG, val);
339 vsc8584_macsec_phy_write(phydev, FC_BUFFER, MSCC_FCBUF_PPM_RATE_ADAPT_THRESH_CFG,
340 MSCC_FCBUF_PPM_RATE_ADAPT_THRESH_CFG_TX_THRESH(8) |
341 MSCC_FCBUF_PPM_RATE_ADAPT_THRESH_CFG_TX_OFFSET(9));
343 val = vsc8584_macsec_phy_read(phydev, FC_BUFFER,
344 MSCC_FCBUF_TX_DATA_QUEUE_CFG);
345 val &= ~(MSCC_FCBUF_TX_DATA_QUEUE_CFG_START_M |
346 MSCC_FCBUF_TX_DATA_QUEUE_CFG_END_M);
347 val |= MSCC_FCBUF_TX_DATA_QUEUE_CFG_START(0) |
348 MSCC_FCBUF_TX_DATA_QUEUE_CFG_END(5119);
349 vsc8584_macsec_phy_write(phydev, FC_BUFFER,
350 MSCC_FCBUF_TX_DATA_QUEUE_CFG, val);
352 val = vsc8584_macsec_phy_read(phydev, FC_BUFFER, MSCC_FCBUF_ENA_CFG);
353 val |= MSCC_FCBUF_ENA_CFG_TX_ENA | MSCC_FCBUF_ENA_CFG_RX_ENA;
354 vsc8584_macsec_phy_write(phydev, FC_BUFFER, MSCC_FCBUF_ENA_CFG, val);
356 proc_bank = (priv->addr < 2) ? PROC_0 : PROC_2;
358 val = vsc8584_macsec_phy_read(phydev, proc_bank,
359 MSCC_PROC_IP_1588_TOP_CFG_STAT_MODE_CTL);
360 val &= ~MSCC_PROC_IP_1588_TOP_CFG_STAT_MODE_CTL_PROTOCOL_MODE_M;
361 val |= MSCC_PROC_IP_1588_TOP_CFG_STAT_MODE_CTL_PROTOCOL_MODE(4);
362 vsc8584_macsec_phy_write(phydev, proc_bank,
363 MSCC_PROC_IP_1588_TOP_CFG_STAT_MODE_CTL, val);
368 static void vsc8584_macsec_flow(struct phy_device *phydev,
369 struct macsec_flow *flow)
371 struct vsc8531_private *priv = phydev->priv;
372 enum macsec_bank bank = flow->bank;
373 u32 val, match = 0, mask = 0, action = 0, idx = flow->index;
375 if (flow->match.tagged)
376 match |= MSCC_MS_SAM_MISC_MATCH_TAGGED;
377 if (flow->match.untagged)
378 match |= MSCC_MS_SAM_MISC_MATCH_UNTAGGED;
380 if (bank == MACSEC_INGR && flow->assoc_num >= 0) {
381 match |= MSCC_MS_SAM_MISC_MATCH_AN(flow->assoc_num);
382 mask |= MSCC_MS_SAM_MASK_AN_MASK(0x3);
385 if (bank == MACSEC_INGR && flow->match.sci && flow->rx_sa->sc->sci) {
386 match |= MSCC_MS_SAM_MISC_MATCH_TCI(BIT(3));
387 mask |= MSCC_MS_SAM_MASK_TCI_MASK(BIT(3)) |
388 MSCC_MS_SAM_MASK_SCI_MASK;
390 vsc8584_macsec_phy_write(phydev, bank, MSCC_MS_SAM_MATCH_SCI_LO(idx),
391 lower_32_bits(flow->rx_sa->sc->sci));
392 vsc8584_macsec_phy_write(phydev, bank, MSCC_MS_SAM_MATCH_SCI_HI(idx),
393 upper_32_bits(flow->rx_sa->sc->sci));
396 if (flow->match.etype) {
397 mask |= MSCC_MS_SAM_MASK_MAC_ETYPE_MASK;
399 vsc8584_macsec_phy_write(phydev, bank, MSCC_MS_SAM_MAC_SA_MATCH_HI(idx),
400 MSCC_MS_SAM_MAC_SA_MATCH_HI_ETYPE(htons(flow->etype)));
403 match |= MSCC_MS_SAM_MISC_MATCH_PRIORITY(flow->priority);
405 vsc8584_macsec_phy_write(phydev, bank, MSCC_MS_SAM_MISC_MATCH(idx), match);
406 vsc8584_macsec_phy_write(phydev, bank, MSCC_MS_SAM_MASK(idx), mask);
408 /* Action for matching packets */
409 if (flow->action.drop)
410 action = MSCC_MS_FLOW_DROP;
411 else if (flow->action.bypass || flow->port == MSCC_MS_PORT_UNCONTROLLED)
412 action = MSCC_MS_FLOW_BYPASS;
414 action = (bank == MACSEC_INGR) ?
415 MSCC_MS_FLOW_INGRESS : MSCC_MS_FLOW_EGRESS;
417 val = MSCC_MS_SAM_FLOW_CTRL_FLOW_TYPE(action) |
418 MSCC_MS_SAM_FLOW_CTRL_DROP_ACTION(MSCC_MS_ACTION_DROP) |
419 MSCC_MS_SAM_FLOW_CTRL_DEST_PORT(flow->port);
421 if (action == MSCC_MS_FLOW_BYPASS)
424 if (bank == MACSEC_INGR) {
425 if (priv->secy->replay_protect)
426 val |= MSCC_MS_SAM_FLOW_CTRL_REPLAY_PROTECT;
427 if (priv->secy->validate_frames == MACSEC_VALIDATE_STRICT)
428 val |= MSCC_MS_SAM_FLOW_CTRL_VALIDATE_FRAMES(MSCC_MS_VALIDATE_STRICT);
429 else if (priv->secy->validate_frames == MACSEC_VALIDATE_CHECK)
430 val |= MSCC_MS_SAM_FLOW_CTRL_VALIDATE_FRAMES(MSCC_MS_VALIDATE_CHECK);
431 } else if (bank == MACSEC_EGR) {
432 if (priv->secy->protect_frames)
433 val |= MSCC_MS_SAM_FLOW_CTRL_PROTECT_FRAME;
434 if (priv->secy->tx_sc.encrypt)
435 val |= MSCC_MS_SAM_FLOW_CTRL_CONF_PROTECT;
436 if (priv->secy->tx_sc.send_sci)
437 val |= MSCC_MS_SAM_FLOW_CTRL_INCLUDE_SCI;
441 vsc8584_macsec_phy_write(phydev, bank, MSCC_MS_SAM_FLOW_CTRL(idx), val);
444 static struct macsec_flow *vsc8584_macsec_find_flow(struct macsec_context *ctx,
445 enum macsec_bank bank)
447 struct vsc8531_private *priv = ctx->phydev->priv;
448 struct macsec_flow *pos, *tmp;
450 list_for_each_entry_safe(pos, tmp, &priv->macsec_flows, list)
451 if (pos->assoc_num == ctx->sa.assoc_num && pos->bank == bank)
454 return ERR_PTR(-ENOENT);
457 static void vsc8584_macsec_flow_enable(struct phy_device *phydev,
458 struct macsec_flow *flow)
460 enum macsec_bank bank = flow->bank;
461 u32 val, idx = flow->index;
463 if ((flow->bank == MACSEC_INGR && flow->rx_sa && !flow->rx_sa->active) ||
464 (flow->bank == MACSEC_EGR && flow->tx_sa && !flow->tx_sa->active))
468 vsc8584_macsec_phy_write(phydev, bank, MSCC_MS_SAM_ENTRY_SET1, BIT(idx));
471 val = vsc8584_macsec_phy_read(phydev, bank, MSCC_MS_SAM_FLOW_CTRL(idx));
472 val |= MSCC_MS_SAM_FLOW_CTRL_SA_IN_USE;
473 vsc8584_macsec_phy_write(phydev, bank, MSCC_MS_SAM_FLOW_CTRL(idx), val);
476 static void vsc8584_macsec_flow_disable(struct phy_device *phydev,
477 struct macsec_flow *flow)
479 enum macsec_bank bank = flow->bank;
480 u32 val, idx = flow->index;
483 vsc8584_macsec_phy_write(phydev, bank, MSCC_MS_SAM_ENTRY_CLEAR1, BIT(idx));
486 val = vsc8584_macsec_phy_read(phydev, bank, MSCC_MS_SAM_FLOW_CTRL(idx));
487 val &= ~MSCC_MS_SAM_FLOW_CTRL_SA_IN_USE;
488 vsc8584_macsec_phy_write(phydev, bank, MSCC_MS_SAM_FLOW_CTRL(idx), val);
491 static u32 vsc8584_macsec_flow_context_id(struct macsec_flow *flow)
493 if (flow->bank == MACSEC_INGR)
494 return flow->index + MSCC_MS_MAX_FLOWS;
499 /* Derive the AES key to get a key for the hash autentication */
500 static int vsc8584_macsec_derive_key(const u8 key[MACSEC_KEYID_LEN],
501 u16 key_len, u8 hkey[16])
503 struct crypto_skcipher *tfm = crypto_alloc_skcipher("ecb(aes)", 0, 0);
504 struct skcipher_request *req = NULL;
505 struct scatterlist src, dst;
506 DECLARE_CRYPTO_WAIT(wait);
513 req = skcipher_request_alloc(tfm, GFP_KERNEL);
519 skcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG |
520 CRYPTO_TFM_REQ_MAY_SLEEP, crypto_req_done,
522 ret = crypto_skcipher_setkey(tfm, key, key_len);
526 sg_init_one(&src, input, 16);
527 sg_init_one(&dst, hkey, 16);
528 skcipher_request_set_crypt(req, &src, &dst, 16, NULL);
530 ret = crypto_wait_req(crypto_skcipher_encrypt(req), &wait);
533 skcipher_request_free(req);
534 crypto_free_skcipher(tfm);
538 static int vsc8584_macsec_transformation(struct phy_device *phydev,
539 struct macsec_flow *flow)
541 struct vsc8531_private *priv = phydev->priv;
542 enum macsec_bank bank = flow->bank;
543 int i, ret, index = flow->index;
544 u32 rec = 0, control = 0;
548 ret = vsc8584_macsec_derive_key(flow->key, priv->secy->key_len, hkey);
552 switch (priv->secy->key_len) {
554 control |= CONTROL_CRYPTO_ALG(CTRYPTO_ALG_AES_CTR_128);
557 control |= CONTROL_CRYPTO_ALG(CTRYPTO_ALG_AES_CTR_256);
563 control |= (bank == MACSEC_EGR) ?
564 (CONTROL_TYPE_EGRESS | CONTROL_AN(priv->secy->tx_sc.encoding_sa)) :
565 (CONTROL_TYPE_INGRESS | CONTROL_SEQ_MASK);
567 control |= CONTROL_UPDATE_SEQ | CONTROL_ENCRYPT_AUTH | CONTROL_KEY_IN_CTX |
568 CONTROL_IV0 | CONTROL_IV1 | CONTROL_IV_IN_SEQ |
569 CONTROL_DIGEST_TYPE(0x2) | CONTROL_SEQ_TYPE(0x1) |
570 CONTROL_AUTH_ALG(AUTH_ALG_AES_GHAS) | CONTROL_CONTEXT_ID;
572 /* Set the control word */
573 vsc8584_macsec_phy_write(phydev, bank, MSCC_MS_XFORM_REC(index, rec++),
576 /* Set the context ID. Must be unique. */
577 vsc8584_macsec_phy_write(phydev, bank, MSCC_MS_XFORM_REC(index, rec++),
578 vsc8584_macsec_flow_context_id(flow));
580 /* Set the encryption/decryption key */
581 for (i = 0; i < priv->secy->key_len / sizeof(u32); i++)
582 vsc8584_macsec_phy_write(phydev, bank,
583 MSCC_MS_XFORM_REC(index, rec++),
584 ((u32 *)flow->key)[i]);
586 /* Set the authentication key */
587 for (i = 0; i < 4; i++)
588 vsc8584_macsec_phy_write(phydev, bank,
589 MSCC_MS_XFORM_REC(index, rec++),
592 /* Initial sequence number */
593 vsc8584_macsec_phy_write(phydev, bank, MSCC_MS_XFORM_REC(index, rec++),
594 bank == MACSEC_INGR ?
595 flow->rx_sa->next_pn : flow->tx_sa->next_pn);
597 if (bank == MACSEC_INGR)
598 /* Set the mask (replay window size) */
599 vsc8584_macsec_phy_write(phydev, bank,
600 MSCC_MS_XFORM_REC(index, rec++),
601 priv->secy->replay_window);
603 /* Set the input vectors */
604 sci = bank == MACSEC_INGR ? flow->rx_sa->sc->sci : priv->secy->sci;
605 vsc8584_macsec_phy_write(phydev, bank, MSCC_MS_XFORM_REC(index, rec++),
607 vsc8584_macsec_phy_write(phydev, bank, MSCC_MS_XFORM_REC(index, rec++),
611 vsc8584_macsec_phy_write(phydev, bank, MSCC_MS_XFORM_REC(index, rec++),
614 flow->has_transformation = true;
618 static struct macsec_flow *vsc8584_macsec_alloc_flow(struct vsc8531_private *priv,
619 enum macsec_bank bank)
621 unsigned long *bitmap = bank == MACSEC_INGR ?
622 &priv->ingr_flows : &priv->egr_flows;
623 struct macsec_flow *flow;
626 index = find_first_zero_bit(bitmap, MSCC_MS_MAX_FLOWS);
628 if (index == MSCC_MS_MAX_FLOWS)
629 return ERR_PTR(-ENOMEM);
631 flow = kzalloc(sizeof(*flow), GFP_KERNEL);
633 return ERR_PTR(-ENOMEM);
635 set_bit(index, bitmap);
639 flow->assoc_num = -1;
641 list_add_tail(&flow->list, &priv->macsec_flows);
645 static void vsc8584_macsec_free_flow(struct vsc8531_private *priv,
646 struct macsec_flow *flow)
648 unsigned long *bitmap = flow->bank == MACSEC_INGR ?
649 &priv->ingr_flows : &priv->egr_flows;
651 list_del(&flow->list);
652 clear_bit(flow->index, bitmap);
656 static int vsc8584_macsec_add_flow(struct phy_device *phydev,
657 struct macsec_flow *flow, bool update)
661 flow->port = MSCC_MS_PORT_CONTROLLED;
662 vsc8584_macsec_flow(phydev, flow);
667 ret = vsc8584_macsec_transformation(phydev, flow);
669 vsc8584_macsec_free_flow(phydev->priv, flow);
676 static int vsc8584_macsec_default_flows(struct phy_device *phydev)
678 struct macsec_flow *flow;
680 /* Add a rule to let the MKA traffic go through, ingress */
681 flow = vsc8584_macsec_alloc_flow(phydev->priv, MACSEC_INGR);
683 return PTR_ERR(flow);
686 flow->port = MSCC_MS_PORT_UNCONTROLLED;
687 flow->match.tagged = 1;
688 flow->match.untagged = 1;
689 flow->match.etype = 1;
690 flow->etype = ETH_P_PAE;
691 flow->action.bypass = 1;
693 vsc8584_macsec_flow(phydev, flow);
694 vsc8584_macsec_flow_enable(phydev, flow);
696 /* Add a rule to let the MKA traffic go through, egress */
697 flow = vsc8584_macsec_alloc_flow(phydev->priv, MACSEC_EGR);
699 return PTR_ERR(flow);
702 flow->port = MSCC_MS_PORT_COMMON;
703 flow->match.untagged = 1;
704 flow->match.etype = 1;
705 flow->etype = ETH_P_PAE;
706 flow->action.bypass = 1;
708 vsc8584_macsec_flow(phydev, flow);
709 vsc8584_macsec_flow_enable(phydev, flow);
714 static void vsc8584_macsec_del_flow(struct phy_device *phydev,
715 struct macsec_flow *flow)
717 vsc8584_macsec_flow_disable(phydev, flow);
718 vsc8584_macsec_free_flow(phydev->priv, flow);
721 static int __vsc8584_macsec_add_rxsa(struct macsec_context *ctx,
722 struct macsec_flow *flow, bool update)
724 struct phy_device *phydev = ctx->phydev;
725 struct vsc8531_private *priv = phydev->priv;
728 flow = vsc8584_macsec_alloc_flow(priv, MACSEC_INGR);
730 return PTR_ERR(flow);
732 memcpy(flow->key, ctx->sa.key, priv->secy->key_len);
735 flow->assoc_num = ctx->sa.assoc_num;
736 flow->rx_sa = ctx->sa.rx_sa;
738 /* Always match tagged packets on ingress */
739 flow->match.tagged = 1;
742 if (priv->secy->validate_frames != MACSEC_VALIDATE_DISABLED)
743 flow->match.untagged = 1;
745 return vsc8584_macsec_add_flow(phydev, flow, update);
748 static int __vsc8584_macsec_add_txsa(struct macsec_context *ctx,
749 struct macsec_flow *flow, bool update)
751 struct phy_device *phydev = ctx->phydev;
752 struct vsc8531_private *priv = phydev->priv;
755 flow = vsc8584_macsec_alloc_flow(priv, MACSEC_EGR);
757 return PTR_ERR(flow);
759 memcpy(flow->key, ctx->sa.key, priv->secy->key_len);
762 flow->assoc_num = ctx->sa.assoc_num;
763 flow->tx_sa = ctx->sa.tx_sa;
765 /* Always match untagged packets on egress */
766 flow->match.untagged = 1;
768 return vsc8584_macsec_add_flow(phydev, flow, update);
771 static int vsc8584_macsec_dev_open(struct macsec_context *ctx)
773 struct vsc8531_private *priv = ctx->phydev->priv;
774 struct macsec_flow *flow, *tmp;
776 /* No operation to perform before the commit step */
780 list_for_each_entry_safe(flow, tmp, &priv->macsec_flows, list)
781 vsc8584_macsec_flow_enable(ctx->phydev, flow);
786 static int vsc8584_macsec_dev_stop(struct macsec_context *ctx)
788 struct vsc8531_private *priv = ctx->phydev->priv;
789 struct macsec_flow *flow, *tmp;
791 /* No operation to perform before the commit step */
795 list_for_each_entry_safe(flow, tmp, &priv->macsec_flows, list)
796 vsc8584_macsec_flow_disable(ctx->phydev, flow);
801 static int vsc8584_macsec_add_secy(struct macsec_context *ctx)
803 struct vsc8531_private *priv = ctx->phydev->priv;
804 struct macsec_secy *secy = ctx->secy;
815 vsc8584_macsec_flow_default_action(ctx->phydev, MACSEC_EGR,
816 secy->validate_frames != MACSEC_VALIDATE_DISABLED);
817 vsc8584_macsec_flow_default_action(ctx->phydev, MACSEC_INGR,
818 secy->validate_frames != MACSEC_VALIDATE_DISABLED);
820 return vsc8584_macsec_default_flows(ctx->phydev);
823 static int vsc8584_macsec_del_secy(struct macsec_context *ctx)
825 struct vsc8531_private *priv = ctx->phydev->priv;
826 struct macsec_flow *flow, *tmp;
828 /* No operation to perform before the commit step */
832 list_for_each_entry_safe(flow, tmp, &priv->macsec_flows, list)
833 vsc8584_macsec_del_flow(ctx->phydev, flow);
835 vsc8584_macsec_flow_default_action(ctx->phydev, MACSEC_EGR, false);
836 vsc8584_macsec_flow_default_action(ctx->phydev, MACSEC_INGR, false);
842 static int vsc8584_macsec_upd_secy(struct macsec_context *ctx)
844 /* No operation to perform before the commit step */
848 vsc8584_macsec_del_secy(ctx);
849 return vsc8584_macsec_add_secy(ctx);
852 static int vsc8584_macsec_add_rxsc(struct macsec_context *ctx)
858 static int vsc8584_macsec_upd_rxsc(struct macsec_context *ctx)
863 static int vsc8584_macsec_del_rxsc(struct macsec_context *ctx)
865 struct vsc8531_private *priv = ctx->phydev->priv;
866 struct macsec_flow *flow, *tmp;
868 /* No operation to perform before the commit step */
872 list_for_each_entry_safe(flow, tmp, &priv->macsec_flows, list) {
873 if (flow->bank == MACSEC_INGR && flow->rx_sa &&
874 flow->rx_sa->sc->sci == ctx->rx_sc->sci)
875 vsc8584_macsec_del_flow(ctx->phydev, flow);
881 static int vsc8584_macsec_add_rxsa(struct macsec_context *ctx)
883 struct macsec_flow *flow = NULL;
886 return __vsc8584_macsec_add_rxsa(ctx, flow, false);
888 flow = vsc8584_macsec_find_flow(ctx, MACSEC_INGR);
890 return PTR_ERR(flow);
892 vsc8584_macsec_flow_enable(ctx->phydev, flow);
896 static int vsc8584_macsec_upd_rxsa(struct macsec_context *ctx)
898 struct macsec_flow *flow;
900 flow = vsc8584_macsec_find_flow(ctx, MACSEC_INGR);
902 return PTR_ERR(flow);
905 /* Make sure the flow is disabled before updating it */
906 vsc8584_macsec_flow_disable(ctx->phydev, flow);
908 return __vsc8584_macsec_add_rxsa(ctx, flow, true);
911 vsc8584_macsec_flow_enable(ctx->phydev, flow);
915 static int vsc8584_macsec_del_rxsa(struct macsec_context *ctx)
917 struct macsec_flow *flow;
919 flow = vsc8584_macsec_find_flow(ctx, MACSEC_INGR);
922 return PTR_ERR(flow);
926 vsc8584_macsec_del_flow(ctx->phydev, flow);
930 static int vsc8584_macsec_add_txsa(struct macsec_context *ctx)
932 struct macsec_flow *flow = NULL;
935 return __vsc8584_macsec_add_txsa(ctx, flow, false);
937 flow = vsc8584_macsec_find_flow(ctx, MACSEC_EGR);
939 return PTR_ERR(flow);
941 vsc8584_macsec_flow_enable(ctx->phydev, flow);
945 static int vsc8584_macsec_upd_txsa(struct macsec_context *ctx)
947 struct macsec_flow *flow;
949 flow = vsc8584_macsec_find_flow(ctx, MACSEC_EGR);
951 return PTR_ERR(flow);
954 /* Make sure the flow is disabled before updating it */
955 vsc8584_macsec_flow_disable(ctx->phydev, flow);
957 return __vsc8584_macsec_add_txsa(ctx, flow, true);
960 vsc8584_macsec_flow_enable(ctx->phydev, flow);
964 static int vsc8584_macsec_del_txsa(struct macsec_context *ctx)
966 struct macsec_flow *flow;
968 flow = vsc8584_macsec_find_flow(ctx, MACSEC_EGR);
971 return PTR_ERR(flow);
975 vsc8584_macsec_del_flow(ctx->phydev, flow);
979 static struct macsec_ops vsc8584_macsec_ops = {
980 .mdo_dev_open = vsc8584_macsec_dev_open,
981 .mdo_dev_stop = vsc8584_macsec_dev_stop,
982 .mdo_add_secy = vsc8584_macsec_add_secy,
983 .mdo_upd_secy = vsc8584_macsec_upd_secy,
984 .mdo_del_secy = vsc8584_macsec_del_secy,
985 .mdo_add_rxsc = vsc8584_macsec_add_rxsc,
986 .mdo_upd_rxsc = vsc8584_macsec_upd_rxsc,
987 .mdo_del_rxsc = vsc8584_macsec_del_rxsc,
988 .mdo_add_rxsa = vsc8584_macsec_add_rxsa,
989 .mdo_upd_rxsa = vsc8584_macsec_upd_rxsa,
990 .mdo_del_rxsa = vsc8584_macsec_del_rxsa,
991 .mdo_add_txsa = vsc8584_macsec_add_txsa,
992 .mdo_upd_txsa = vsc8584_macsec_upd_txsa,
993 .mdo_del_txsa = vsc8584_macsec_del_txsa,
996 int vsc8584_macsec_init(struct phy_device *phydev)
998 struct vsc8531_private *vsc8531 = phydev->priv;
1000 switch (phydev->phy_id & phydev->drv->phy_id_mask) {
1001 case PHY_ID_VSC856X:
1002 case PHY_ID_VSC8575:
1003 case PHY_ID_VSC8582:
1004 case PHY_ID_VSC8584:
1005 INIT_LIST_HEAD(&vsc8531->macsec_flows);
1006 vsc8531->secy = NULL;
1008 phydev->macsec_ops = &vsc8584_macsec_ops;
1010 return __vsc8584_macsec_init(phydev);
1016 void vsc8584_handle_macsec_interrupt(struct phy_device *phydev)
1018 struct vsc8531_private *priv = phydev->priv;
1019 struct macsec_flow *flow, *tmp;
1022 /* Check MACsec PN rollover */
1023 cause = vsc8584_macsec_phy_read(phydev, MACSEC_EGR,
1024 MSCC_MS_INTR_CTRL_STATUS);
1025 cause &= MSCC_MS_INTR_CTRL_STATUS_INTR_CLR_STATUS_M;
1026 if (!(cause & MACSEC_INTR_CTRL_STATUS_ROLLOVER))
1029 rec = 6 + priv->secy->key_len / sizeof(u32);
1030 list_for_each_entry_safe(flow, tmp, &priv->macsec_flows, list) {
1033 if (flow->bank != MACSEC_EGR || !flow->has_transformation)
1036 val = vsc8584_macsec_phy_read(phydev, MACSEC_EGR,
1037 MSCC_MS_XFORM_REC(flow->index, rec));
1038 if (val == 0xffffffff) {
1039 vsc8584_macsec_flow_disable(phydev, flow);
1040 macsec_pn_wrapped(priv->secy, flow->tx_sa);
1046 void vsc8584_config_macsec_intr(struct phy_device *phydev)
1048 phy_write(phydev, MSCC_EXT_PAGE_ACCESS, MSCC_PHY_PAGE_EXTENDED_2);
1049 phy_write(phydev, MSCC_PHY_EXTENDED_INT, MSCC_PHY_EXTENDED_INT_MS_EGR);
1050 phy_write(phydev, MSCC_EXT_PAGE_ACCESS, MSCC_PHY_PAGE_STANDARD);
1052 vsc8584_macsec_phy_write(phydev, MACSEC_EGR, MSCC_MS_AIC_CTRL, 0xf);
1053 vsc8584_macsec_phy_write(phydev, MACSEC_EGR, MSCC_MS_INTR_CTRL_STATUS,
1054 MSCC_MS_INTR_CTRL_STATUS_INTR_ENABLE(MACSEC_INTR_CTRL_STATUS_ROLLOVER));