1 // SPDX-License-Identifier: GPL-2.0 2 /* Copyright 2019 NXP Semiconductors 3 */ 4 #include <uapi/linux/if_bridge.h> 5 #include <soc/mscc/ocelot.h> 6 #include <linux/packing.h> 7 #include <linux/module.h> 8 #include <linux/pci.h> 9 #include <linux/of.h> 10 #include <net/dsa.h> 11 #include "felix.h" 12 13 static enum dsa_tag_protocol felix_get_tag_protocol(struct dsa_switch *ds, 14 int port) 15 { 16 return DSA_TAG_PROTO_OCELOT; 17 } 18 19 static int felix_set_ageing_time(struct dsa_switch *ds, 20 unsigned int ageing_time) 21 { 22 struct ocelot *ocelot = ds->priv; 23 24 ocelot_set_ageing_time(ocelot, ageing_time); 25 26 return 0; 27 } 28 29 static void felix_adjust_link(struct dsa_switch *ds, int port, 30 struct phy_device *phydev) 31 { 32 struct ocelot *ocelot = ds->priv; 33 34 ocelot_adjust_link(ocelot, port, phydev); 35 } 36 37 static int felix_fdb_dump(struct dsa_switch *ds, int port, 38 dsa_fdb_dump_cb_t *cb, void *data) 39 { 40 struct ocelot *ocelot = ds->priv; 41 42 return ocelot_fdb_dump(ocelot, port, cb, data); 43 } 44 45 static int felix_fdb_add(struct dsa_switch *ds, int port, 46 const unsigned char *addr, u16 vid) 47 { 48 struct ocelot *ocelot = ds->priv; 49 bool vlan_aware; 50 51 vlan_aware = dsa_port_is_vlan_filtering(dsa_to_port(ds, port)); 52 53 return ocelot_fdb_add(ocelot, port, addr, vid, vlan_aware); 54 } 55 56 static int felix_fdb_del(struct dsa_switch *ds, int port, 57 const unsigned char *addr, u16 vid) 58 { 59 struct ocelot *ocelot = ds->priv; 60 61 return ocelot_fdb_del(ocelot, port, addr, vid); 62 } 63 64 static void felix_bridge_stp_state_set(struct dsa_switch *ds, int port, 65 u8 state) 66 { 67 struct ocelot *ocelot = ds->priv; 68 69 return ocelot_bridge_stp_state_set(ocelot, port, state); 70 } 71 72 static int felix_bridge_join(struct dsa_switch *ds, int port, 73 struct net_device *br) 74 { 75 struct ocelot *ocelot = ds->priv; 76 77 return ocelot_port_bridge_join(ocelot, port, br); 78 } 79 80 static void felix_bridge_leave(struct dsa_switch *ds, int port, 81 struct net_device *br) 82 { 83 struct ocelot *ocelot = ds->priv; 84 85 ocelot_port_bridge_leave(ocelot, port, br); 86 } 87 88 /* This callback needs to be present */ 89 static int felix_vlan_prepare(struct dsa_switch *ds, int port, 90 const struct switchdev_obj_port_vlan *vlan) 91 { 92 return 0; 93 } 94 95 static int felix_vlan_filtering(struct dsa_switch *ds, int port, bool enabled) 96 { 97 struct ocelot *ocelot = ds->priv; 98 99 ocelot_port_vlan_filtering(ocelot, port, enabled); 100 101 return 0; 102 } 103 104 static void felix_vlan_add(struct dsa_switch *ds, int port, 105 const struct switchdev_obj_port_vlan *vlan) 106 { 107 struct ocelot *ocelot = ds->priv; 108 u16 vid; 109 int err; 110 111 for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++) { 112 err = ocelot_vlan_add(ocelot, port, vid, 113 vlan->flags & BRIDGE_VLAN_INFO_PVID, 114 vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED); 115 if (err) { 116 dev_err(ds->dev, "Failed to add VLAN %d to port %d: %d\n", 117 vid, port, err); 118 return; 119 } 120 } 121 } 122 123 static int felix_vlan_del(struct dsa_switch *ds, int port, 124 const struct switchdev_obj_port_vlan *vlan) 125 { 126 struct ocelot *ocelot = ds->priv; 127 u16 vid; 128 int err; 129 130 for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++) { 131 err = ocelot_vlan_del(ocelot, port, vid); 132 if (err) { 133 dev_err(ds->dev, "Failed to remove VLAN %d from port %d: %d\n", 134 vid, port, err); 135 return err; 136 } 137 } 138 return 0; 139 } 140 141 static int felix_port_enable(struct dsa_switch *ds, int port, 142 struct phy_device *phy) 143 { 144 struct ocelot *ocelot = ds->priv; 145 146 ocelot_port_enable(ocelot, port, phy); 147 148 return 0; 149 } 150 151 static void felix_port_disable(struct dsa_switch *ds, int port) 152 { 153 struct ocelot *ocelot = ds->priv; 154 155 return ocelot_port_disable(ocelot, port); 156 } 157 158 static void felix_get_strings(struct dsa_switch *ds, int port, 159 u32 stringset, u8 *data) 160 { 161 struct ocelot *ocelot = ds->priv; 162 163 return ocelot_get_strings(ocelot, port, stringset, data); 164 } 165 166 static void felix_get_ethtool_stats(struct dsa_switch *ds, int port, u64 *data) 167 { 168 struct ocelot *ocelot = ds->priv; 169 170 ocelot_get_ethtool_stats(ocelot, port, data); 171 } 172 173 static int felix_get_sset_count(struct dsa_switch *ds, int port, int sset) 174 { 175 struct ocelot *ocelot = ds->priv; 176 177 return ocelot_get_sset_count(ocelot, port, sset); 178 } 179 180 static int felix_get_ts_info(struct dsa_switch *ds, int port, 181 struct ethtool_ts_info *info) 182 { 183 struct ocelot *ocelot = ds->priv; 184 185 return ocelot_get_ts_info(ocelot, port, info); 186 } 187 188 static int felix_init_structs(struct felix *felix, int num_phys_ports) 189 { 190 struct ocelot *ocelot = &felix->ocelot; 191 resource_size_t base; 192 int port, i, err; 193 194 ocelot->num_phys_ports = num_phys_ports; 195 ocelot->ports = devm_kcalloc(ocelot->dev, num_phys_ports, 196 sizeof(struct ocelot_port *), GFP_KERNEL); 197 if (!ocelot->ports) 198 return -ENOMEM; 199 200 ocelot->map = felix->info->map; 201 ocelot->stats_layout = felix->info->stats_layout; 202 ocelot->num_stats = felix->info->num_stats; 203 ocelot->shared_queue_sz = felix->info->shared_queue_sz; 204 ocelot->ops = felix->info->ops; 205 206 base = pci_resource_start(felix->pdev, felix->info->pci_bar); 207 208 for (i = 0; i < TARGET_MAX; i++) { 209 struct regmap *target; 210 struct resource *res; 211 212 if (!felix->info->target_io_res[i].name) 213 continue; 214 215 res = &felix->info->target_io_res[i]; 216 res->flags = IORESOURCE_MEM; 217 res->start += base; 218 res->end += base; 219 220 target = ocelot_regmap_init(ocelot, res); 221 if (IS_ERR(target)) { 222 dev_err(ocelot->dev, 223 "Failed to map device memory space\n"); 224 return PTR_ERR(target); 225 } 226 227 ocelot->targets[i] = target; 228 } 229 230 err = ocelot_regfields_init(ocelot, felix->info->regfields); 231 if (err) { 232 dev_err(ocelot->dev, "failed to init reg fields map\n"); 233 return err; 234 } 235 236 for (port = 0; port < num_phys_ports; port++) { 237 struct ocelot_port *ocelot_port; 238 void __iomem *port_regs; 239 struct resource *res; 240 241 ocelot_port = devm_kzalloc(ocelot->dev, 242 sizeof(struct ocelot_port), 243 GFP_KERNEL); 244 if (!ocelot_port) { 245 dev_err(ocelot->dev, 246 "failed to allocate port memory\n"); 247 return -ENOMEM; 248 } 249 250 res = &felix->info->port_io_res[port]; 251 res->flags = IORESOURCE_MEM; 252 res->start += base; 253 res->end += base; 254 255 port_regs = devm_ioremap_resource(ocelot->dev, res); 256 if (IS_ERR(port_regs)) { 257 dev_err(ocelot->dev, 258 "failed to map registers for port %d\n", port); 259 return PTR_ERR(port_regs); 260 } 261 262 ocelot_port->ocelot = ocelot; 263 ocelot_port->regs = port_regs; 264 ocelot->ports[port] = ocelot_port; 265 } 266 267 return 0; 268 } 269 270 /* Hardware initialization done here so that we can allocate structures with 271 * devm without fear of dsa_register_switch returning -EPROBE_DEFER and causing 272 * us to allocate structures twice (leak memory) and map PCI memory twice 273 * (which will not work). 274 */ 275 static int felix_setup(struct dsa_switch *ds) 276 { 277 struct ocelot *ocelot = ds->priv; 278 struct felix *felix = ocelot_to_felix(ocelot); 279 int port, err; 280 281 err = felix_init_structs(felix, ds->num_ports); 282 if (err) 283 return err; 284 285 ocelot_init(ocelot); 286 287 for (port = 0; port < ds->num_ports; port++) { 288 ocelot_init_port(ocelot, port); 289 290 if (dsa_is_cpu_port(ds, port)) 291 ocelot_set_cpu_port(ocelot, port, 292 OCELOT_TAG_PREFIX_NONE, 293 OCELOT_TAG_PREFIX_LONG); 294 } 295 296 return 0; 297 } 298 299 static void felix_teardown(struct dsa_switch *ds) 300 { 301 struct ocelot *ocelot = ds->priv; 302 303 /* stop workqueue thread */ 304 ocelot_deinit(ocelot); 305 } 306 307 static int felix_hwtstamp_get(struct dsa_switch *ds, int port, 308 struct ifreq *ifr) 309 { 310 struct ocelot *ocelot = ds->priv; 311 312 return ocelot_hwstamp_get(ocelot, port, ifr); 313 } 314 315 static int felix_hwtstamp_set(struct dsa_switch *ds, int port, 316 struct ifreq *ifr) 317 { 318 struct ocelot *ocelot = ds->priv; 319 320 return ocelot_hwstamp_set(ocelot, port, ifr); 321 } 322 323 static bool felix_rxtstamp(struct dsa_switch *ds, int port, 324 struct sk_buff *skb, unsigned int type) 325 { 326 struct skb_shared_hwtstamps *shhwtstamps; 327 struct ocelot *ocelot = ds->priv; 328 u8 *extraction = skb->data - ETH_HLEN - OCELOT_TAG_LEN; 329 u32 tstamp_lo, tstamp_hi; 330 struct timespec64 ts; 331 u64 tstamp, val; 332 333 ocelot_ptp_gettime64(&ocelot->ptp_info, &ts); 334 tstamp = ktime_set(ts.tv_sec, ts.tv_nsec); 335 336 packing(extraction, &val, 116, 85, OCELOT_TAG_LEN, UNPACK, 0); 337 tstamp_lo = (u32)val; 338 339 tstamp_hi = tstamp >> 32; 340 if ((tstamp & 0xffffffff) < tstamp_lo) 341 tstamp_hi--; 342 343 tstamp = ((u64)tstamp_hi << 32) | tstamp_lo; 344 345 shhwtstamps = skb_hwtstamps(skb); 346 memset(shhwtstamps, 0, sizeof(struct skb_shared_hwtstamps)); 347 shhwtstamps->hwtstamp = tstamp; 348 return false; 349 } 350 351 static bool felix_txtstamp(struct dsa_switch *ds, int port, 352 struct sk_buff *clone, unsigned int type) 353 { 354 struct ocelot *ocelot = ds->priv; 355 struct ocelot_port *ocelot_port = ocelot->ports[port]; 356 357 if (!ocelot_port_add_txtstamp_skb(ocelot_port, clone)) 358 return true; 359 360 return false; 361 } 362 363 static const struct dsa_switch_ops felix_switch_ops = { 364 .get_tag_protocol = felix_get_tag_protocol, 365 .setup = felix_setup, 366 .teardown = felix_teardown, 367 .set_ageing_time = felix_set_ageing_time, 368 .get_strings = felix_get_strings, 369 .get_ethtool_stats = felix_get_ethtool_stats, 370 .get_sset_count = felix_get_sset_count, 371 .get_ts_info = felix_get_ts_info, 372 .adjust_link = felix_adjust_link, 373 .port_enable = felix_port_enable, 374 .port_disable = felix_port_disable, 375 .port_fdb_dump = felix_fdb_dump, 376 .port_fdb_add = felix_fdb_add, 377 .port_fdb_del = felix_fdb_del, 378 .port_bridge_join = felix_bridge_join, 379 .port_bridge_leave = felix_bridge_leave, 380 .port_stp_state_set = felix_bridge_stp_state_set, 381 .port_vlan_prepare = felix_vlan_prepare, 382 .port_vlan_filtering = felix_vlan_filtering, 383 .port_vlan_add = felix_vlan_add, 384 .port_vlan_del = felix_vlan_del, 385 .port_hwtstamp_get = felix_hwtstamp_get, 386 .port_hwtstamp_set = felix_hwtstamp_set, 387 .port_rxtstamp = felix_rxtstamp, 388 .port_txtstamp = felix_txtstamp, 389 }; 390 391 static struct felix_info *felix_instance_tbl[] = { 392 [FELIX_INSTANCE_VSC9959] = &felix_info_vsc9959, 393 }; 394 395 static irqreturn_t felix_irq_handler(int irq, void *data) 396 { 397 struct ocelot *ocelot = (struct ocelot *)data; 398 399 /* The INTB interrupt is used for both PTP TX timestamp interrupt 400 * and preemption status change interrupt on each port. 401 * 402 * - Get txtstamp if have 403 * - TODO: handle preemption. Without handling it, driver may get 404 * interrupt storm. 405 */ 406 407 ocelot_get_txtstamp(ocelot); 408 409 return IRQ_HANDLED; 410 } 411 412 static int felix_pci_probe(struct pci_dev *pdev, 413 const struct pci_device_id *id) 414 { 415 enum felix_instance instance = id->driver_data; 416 struct dsa_switch *ds; 417 struct ocelot *ocelot; 418 struct felix *felix; 419 int err; 420 421 err = pci_enable_device(pdev); 422 if (err) { 423 dev_err(&pdev->dev, "device enable failed\n"); 424 goto err_pci_enable; 425 } 426 427 /* set up for high or low dma */ 428 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)); 429 if (err) { 430 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)); 431 if (err) { 432 dev_err(&pdev->dev, 433 "DMA configuration failed: 0x%x\n", err); 434 goto err_dma; 435 } 436 } 437 438 felix = kzalloc(sizeof(struct felix), GFP_KERNEL); 439 if (!felix) { 440 err = -ENOMEM; 441 dev_err(&pdev->dev, "Failed to allocate driver memory\n"); 442 goto err_alloc_felix; 443 } 444 445 pci_set_drvdata(pdev, felix); 446 ocelot = &felix->ocelot; 447 ocelot->dev = &pdev->dev; 448 felix->pdev = pdev; 449 felix->info = felix_instance_tbl[instance]; 450 451 pci_set_master(pdev); 452 453 err = devm_request_threaded_irq(&pdev->dev, pdev->irq, NULL, 454 &felix_irq_handler, IRQF_ONESHOT, 455 "felix-intb", ocelot); 456 if (err) { 457 dev_err(&pdev->dev, "Failed to request irq\n"); 458 goto err_alloc_irq; 459 } 460 461 ocelot->ptp = 1; 462 463 ds = kzalloc(sizeof(struct dsa_switch), GFP_KERNEL); 464 if (!ds) { 465 err = -ENOMEM; 466 dev_err(&pdev->dev, "Failed to allocate DSA switch\n"); 467 goto err_alloc_ds; 468 } 469 470 ds->dev = &pdev->dev; 471 ds->num_ports = felix->info->num_ports; 472 ds->ops = &felix_switch_ops; 473 ds->priv = ocelot; 474 felix->ds = ds; 475 476 err = dsa_register_switch(ds); 477 if (err) { 478 dev_err(&pdev->dev, "Failed to register DSA switch: %d\n", err); 479 goto err_register_ds; 480 } 481 482 return 0; 483 484 err_register_ds: 485 kfree(ds); 486 err_alloc_ds: 487 err_alloc_irq: 488 err_alloc_felix: 489 kfree(felix); 490 err_dma: 491 pci_disable_device(pdev); 492 err_pci_enable: 493 return err; 494 } 495 496 static void felix_pci_remove(struct pci_dev *pdev) 497 { 498 struct felix *felix; 499 500 felix = pci_get_drvdata(pdev); 501 502 dsa_unregister_switch(felix->ds); 503 504 kfree(felix->ds); 505 kfree(felix); 506 507 pci_disable_device(pdev); 508 } 509 510 static struct pci_device_id felix_ids[] = { 511 { 512 /* NXP LS1028A */ 513 PCI_DEVICE(PCI_VENDOR_ID_FREESCALE, 0xEEF0), 514 .driver_data = FELIX_INSTANCE_VSC9959, 515 }, 516 { 0, } 517 }; 518 MODULE_DEVICE_TABLE(pci, felix_ids); 519 520 static struct pci_driver felix_pci_driver = { 521 .name = KBUILD_MODNAME, 522 .id_table = felix_ids, 523 .probe = felix_pci_probe, 524 .remove = felix_pci_remove, 525 }; 526 527 module_pci_driver(felix_pci_driver); 528 529 MODULE_DESCRIPTION("Felix Switch driver"); 530 MODULE_LICENSE("GPL v2"); 531