20
20
#include <linux/netdevice.h>
21
21
#include <linux/if_bridge.h>
22
22
#include <linux/if_ether.h>
23
+ #include <linux/dsa/8021q.h>
23
24
#include "sja1105.h"
24
25
25
26
static void sja1105_hw_reset (struct gpio_desc * gpio , unsigned int pulse_len ,
@@ -406,11 +407,14 @@ static int sja1105_init_general_params(struct sja1105_private *priv)
406
407
.tpid2 = ETH_P_SJA1105 ,
407
408
};
408
409
struct sja1105_table * table ;
409
- int i ;
410
+ int i , k = 0 ;
410
411
411
- for (i = 0 ; i < SJA1105_NUM_PORTS ; i ++ )
412
+ for (i = 0 ; i < SJA1105_NUM_PORTS ; i ++ ) {
412
413
if (dsa_is_dsa_port (priv -> ds , i ))
413
414
default_general_params .casc_port = i ;
415
+ else if (dsa_is_user_port (priv -> ds , i ))
416
+ priv -> ports [i ].mgmt_slot = k ++ ;
417
+ }
414
418
415
419
table = & priv -> static_config .tables [BLK_IDX_GENERAL_PARAMS ];
416
420
@@ -1146,10 +1150,27 @@ static int sja1105_vlan_apply(struct sja1105_private *priv, int port, u16 vid,
1146
1150
return 0 ;
1147
1151
}
1148
1152
1153
+ static int sja1105_setup_8021q_tagging (struct dsa_switch * ds , bool enabled )
1154
+ {
1155
+ int rc , i ;
1156
+
1157
+ for (i = 0 ; i < SJA1105_NUM_PORTS ; i ++ ) {
1158
+ rc = dsa_port_setup_8021q_tagging (ds , i , enabled );
1159
+ if (rc < 0 ) {
1160
+ dev_err (ds -> dev , "Failed to setup VLAN tagging for port %d: %d\n" ,
1161
+ i , rc );
1162
+ return rc ;
1163
+ }
1164
+ }
1165
+ dev_info (ds -> dev , "%s switch tagging\n" ,
1166
+ enabled ? "Enabled" : "Disabled" );
1167
+ return 0 ;
1168
+ }
1169
+
1149
1170
static enum dsa_tag_protocol
1150
1171
sja1105_get_tag_protocol (struct dsa_switch * ds , int port )
1151
1172
{
1152
- return DSA_TAG_PROTO_NONE ;
1173
+ return DSA_TAG_PROTO_SJA1105 ;
1153
1174
}
1154
1175
1155
1176
/* This callback needs to be present */
@@ -1173,7 +1194,11 @@ static int sja1105_vlan_filtering(struct dsa_switch *ds, int port, bool enabled)
1173
1194
if (rc )
1174
1195
dev_err (ds -> dev , "Failed to change VLAN Ethertype\n" );
1175
1196
1176
- return rc ;
1197
+ /* Switch port identification based on 802.1Q is only passable
1198
+ * if we are not under a vlan_filtering bridge. So make sure
1199
+ * the two configurations are mutually exclusive.
1200
+ */
1201
+ return sja1105_setup_8021q_tagging (ds , !enabled );
1177
1202
}
1178
1203
1179
1204
static void sja1105_vlan_add (struct dsa_switch * ds , int port ,
@@ -1276,7 +1301,98 @@ static int sja1105_setup(struct dsa_switch *ds)
1276
1301
*/
1277
1302
ds -> vlan_filtering_is_global = true;
1278
1303
1279
- return 0 ;
1304
+ /* The DSA/switchdev model brings up switch ports in standalone mode by
1305
+ * default, and that means vlan_filtering is 0 since they're not under
1306
+ * a bridge, so it's safe to set up switch tagging at this time.
1307
+ */
1308
+ return sja1105_setup_8021q_tagging (ds , true);
1309
+ }
1310
+
1311
+ static int sja1105_mgmt_xmit (struct dsa_switch * ds , int port , int slot ,
1312
+ struct sk_buff * skb )
1313
+ {
1314
+ struct sja1105_mgmt_entry mgmt_route = {0 };
1315
+ struct sja1105_private * priv = ds -> priv ;
1316
+ struct ethhdr * hdr ;
1317
+ int timeout = 10 ;
1318
+ int rc ;
1319
+
1320
+ hdr = eth_hdr (skb );
1321
+
1322
+ mgmt_route .macaddr = ether_addr_to_u64 (hdr -> h_dest );
1323
+ mgmt_route .destports = BIT (port );
1324
+ mgmt_route .enfport = 1 ;
1325
+
1326
+ rc = sja1105_dynamic_config_write (priv , BLK_IDX_MGMT_ROUTE ,
1327
+ slot , & mgmt_route , true);
1328
+ if (rc < 0 ) {
1329
+ kfree_skb (skb );
1330
+ return rc ;
1331
+ }
1332
+
1333
+ /* Transfer skb to the host port. */
1334
+ dsa_enqueue_skb (skb , ds -> ports [port ].slave );
1335
+
1336
+ /* Wait until the switch has processed the frame */
1337
+ do {
1338
+ rc = sja1105_dynamic_config_read (priv , BLK_IDX_MGMT_ROUTE ,
1339
+ slot , & mgmt_route );
1340
+ if (rc < 0 ) {
1341
+ dev_err_ratelimited (priv -> ds -> dev ,
1342
+ "failed to poll for mgmt route\n" );
1343
+ continue ;
1344
+ }
1345
+
1346
+ /* UM10944: The ENFPORT flag of the respective entry is
1347
+ * cleared when a match is found. The host can use this
1348
+ * flag as an acknowledgment.
1349
+ */
1350
+ cpu_relax ();
1351
+ } while (mgmt_route .enfport && -- timeout );
1352
+
1353
+ if (!timeout ) {
1354
+ /* Clean up the management route so that a follow-up
1355
+ * frame may not match on it by mistake.
1356
+ */
1357
+ sja1105_dynamic_config_write (priv , BLK_IDX_MGMT_ROUTE ,
1358
+ slot , & mgmt_route , false);
1359
+ dev_err_ratelimited (priv -> ds -> dev , "xmit timed out\n" );
1360
+ }
1361
+
1362
+ return NETDEV_TX_OK ;
1363
+ }
1364
+
1365
+ /* Deferred work is unfortunately necessary because setting up the management
1366
+ * route cannot be done from atomit context (SPI transfer takes a sleepable
1367
+ * lock on the bus)
1368
+ */
1369
+ static netdev_tx_t sja1105_port_deferred_xmit (struct dsa_switch * ds , int port ,
1370
+ struct sk_buff * skb )
1371
+ {
1372
+ struct sja1105_private * priv = ds -> priv ;
1373
+ struct sja1105_port * sp = & priv -> ports [port ];
1374
+ int slot = sp -> mgmt_slot ;
1375
+
1376
+ /* The tragic fact about the switch having 4x2 slots for installing
1377
+ * management routes is that all of them except one are actually
1378
+ * useless.
1379
+ * If 2 slots are simultaneously configured for two BPDUs sent to the
1380
+ * same (multicast) DMAC but on different egress ports, the switch
1381
+ * would confuse them and redirect first frame it receives on the CPU
1382
+ * port towards the port configured on the numerically first slot
1383
+ * (therefore wrong port), then second received frame on second slot
1384
+ * (also wrong port).
1385
+ * So for all practical purposes, there needs to be a lock that
1386
+ * prevents that from happening. The slot used here is utterly useless
1387
+ * (could have simply been 0 just as fine), but we are doing it
1388
+ * nonetheless, in case a smarter idea ever comes up in the future.
1389
+ */
1390
+ mutex_lock (& priv -> mgmt_lock );
1391
+
1392
+ sja1105_mgmt_xmit (ds , port , slot , skb );
1393
+
1394
+ mutex_unlock (& priv -> mgmt_lock );
1395
+ return NETDEV_TX_OK ;
1280
1396
}
1281
1397
1282
1398
/* The MAXAGE setting belongs to the L2 Forwarding Parameters table,
@@ -1324,6 +1440,7 @@ static const struct dsa_switch_ops sja1105_switch_ops = {
1324
1440
.port_mdb_prepare = sja1105_mdb_prepare ,
1325
1441
.port_mdb_add = sja1105_mdb_add ,
1326
1442
.port_mdb_del = sja1105_mdb_del ,
1443
+ .port_deferred_xmit = sja1105_port_deferred_xmit ,
1327
1444
};
1328
1445
1329
1446
static int sja1105_check_device_id (struct sja1105_private * priv )
@@ -1367,7 +1484,7 @@ static int sja1105_probe(struct spi_device *spi)
1367
1484
struct device * dev = & spi -> dev ;
1368
1485
struct sja1105_private * priv ;
1369
1486
struct dsa_switch * ds ;
1370
- int rc ;
1487
+ int rc , i ;
1371
1488
1372
1489
if (!dev -> of_node ) {
1373
1490
dev_err (dev , "No DTS bindings for SJA1105 driver\n" );
@@ -1418,6 +1535,15 @@ static int sja1105_probe(struct spi_device *spi)
1418
1535
ds -> priv = priv ;
1419
1536
priv -> ds = ds ;
1420
1537
1538
+ /* Connections between dsa_port and sja1105_port */
1539
+ for (i = 0 ; i < SJA1105_NUM_PORTS ; i ++ ) {
1540
+ struct sja1105_port * sp = & priv -> ports [i ];
1541
+
1542
+ ds -> ports [i ].priv = sp ;
1543
+ sp -> dp = & ds -> ports [i ];
1544
+ }
1545
+ mutex_init (& priv -> mgmt_lock );
1546
+
1421
1547
return dsa_register_switch (priv -> ds );
1422
1548
}
1423
1549
0 commit comments