1 /* SPDX-License-Identifier: GPL-2.0+ */ 2 3 #ifndef __LAN966X_MAIN_H__ 4 #define __LAN966X_MAIN_H__ 5 6 #include <linux/etherdevice.h> 7 #include <linux/if_vlan.h> 8 #include <linux/jiffies.h> 9 #include <linux/phy.h> 10 #include <linux/phylink.h> 11 #include <linux/ptp_clock_kernel.h> 12 #include <net/pkt_cls.h> 13 #include <net/pkt_sched.h> 14 #include <net/switchdev.h> 15 16 #include "lan966x_regs.h" 17 #include "lan966x_ifh.h" 18 19 #define TABLE_UPDATE_SLEEP_US 10 20 #define TABLE_UPDATE_TIMEOUT_US 100000 21 22 #define READL_SLEEP_US 10 23 #define READL_TIMEOUT_US 100000000 24 25 #define LAN966X_BUFFER_CELL_SZ 64 26 #define LAN966X_BUFFER_MEMORY (160 * 1024) 27 #define LAN966X_BUFFER_MIN_SZ 60 28 29 #define PGID_AGGR 64 30 #define PGID_SRC 80 31 #define PGID_ENTRIES 89 32 33 #define UNAWARE_PVID 0 34 #define HOST_PVID 4095 35 36 /* Reserved amount for (SRC, PRIO) at index 8*SRC + PRIO */ 37 #define QSYS_Q_RSRV 95 38 39 #define NUM_PHYS_PORTS 8 40 #define CPU_PORT 8 41 #define NUM_PRIO_QUEUES 8 42 43 /* Reserved PGIDs */ 44 #define PGID_CPU (PGID_AGGR - 6) 45 #define PGID_UC (PGID_AGGR - 5) 46 #define PGID_BC (PGID_AGGR - 4) 47 #define PGID_MC (PGID_AGGR - 3) 48 #define PGID_MCIPV4 (PGID_AGGR - 2) 49 #define PGID_MCIPV6 (PGID_AGGR - 1) 50 51 /* Non-reserved PGIDs, used for general purpose */ 52 #define PGID_GP_START (CPU_PORT + 1) 53 #define PGID_GP_END PGID_CPU 54 55 #define LAN966X_SPEED_NONE 0 56 #define LAN966X_SPEED_2500 1 57 #define LAN966X_SPEED_1000 1 58 #define LAN966X_SPEED_100 2 59 #define LAN966X_SPEED_10 3 60 61 #define LAN966X_PHC_COUNT 3 62 #define LAN966X_PHC_PORT 0 63 #define LAN966X_PHC_PINS_NUM 7 64 65 #define IFH_REW_OP_NOOP 0x0 66 #define IFH_REW_OP_ONE_STEP_PTP 0x3 67 #define IFH_REW_OP_TWO_STEP_PTP 0x4 68 69 #define FDMA_RX_DCB_MAX_DBS 1 70 #define FDMA_TX_DCB_MAX_DBS 1 71 #define FDMA_DCB_INFO_DATAL(x) ((x) & GENMASK(15, 0)) 72 73 #define FDMA_DCB_STATUS_BLOCKL(x) ((x) & GENMASK(15, 0)) 74 #define FDMA_DCB_STATUS_SOF BIT(16) 75 #define FDMA_DCB_STATUS_EOF BIT(17) 76 #define FDMA_DCB_STATUS_INTR BIT(18) 77 #define FDMA_DCB_STATUS_DONE BIT(19) 78 #define FDMA_DCB_STATUS_BLOCKO(x) (((x) << 20) & GENMASK(31, 20)) 79 #define FDMA_DCB_INVALID_DATA 0x1 80 81 #define FDMA_XTR_CHANNEL 6 82 #define FDMA_INJ_CHANNEL 0 83 #define FDMA_DCB_MAX 512 84 85 #define SE_IDX_QUEUE 0 /* 0-79 : Queue scheduler elements */ 86 #define SE_IDX_PORT 80 /* 80-89 : Port schedular elements */ 87 88 /* MAC table entry types. 89 * ENTRYTYPE_NORMAL is subject to aging. 90 * ENTRYTYPE_LOCKED is not subject to aging. 91 * ENTRYTYPE_MACv4 is not subject to aging. For IPv4 multicast. 92 * ENTRYTYPE_MACv6 is not subject to aging. For IPv6 multicast. 93 */ 94 enum macaccess_entry_type { 95 ENTRYTYPE_NORMAL = 0, 96 ENTRYTYPE_LOCKED, 97 ENTRYTYPE_MACV4, 98 ENTRYTYPE_MACV6, 99 }; 100 101 struct lan966x_port; 102 103 struct lan966x_db { 104 u64 dataptr; 105 u64 status; 106 }; 107 108 struct lan966x_rx_dcb { 109 u64 nextptr; 110 u64 info; 111 struct lan966x_db db[FDMA_RX_DCB_MAX_DBS]; 112 }; 113 114 struct lan966x_tx_dcb { 115 u64 nextptr; 116 u64 info; 117 struct lan966x_db db[FDMA_TX_DCB_MAX_DBS]; 118 }; 119 120 struct lan966x_rx { 121 struct lan966x *lan966x; 122 123 /* Pointer to the array of hardware dcbs. */ 124 struct lan966x_rx_dcb *dcbs; 125 126 /* Pointer to the last address in the dcbs. */ 127 struct lan966x_rx_dcb *last_entry; 128 129 /* For each DB, there is a page */ 130 struct page *page[FDMA_DCB_MAX][FDMA_RX_DCB_MAX_DBS]; 131 132 /* Represents the db_index, it can have a value between 0 and 133 * FDMA_RX_DCB_MAX_DBS, once it reaches the value of FDMA_RX_DCB_MAX_DBS 134 * it means that the DCB can be reused. 135 */ 136 int db_index; 137 138 /* Represents the index in the dcbs. It has a value between 0 and 139 * FDMA_DCB_MAX 140 */ 141 int dcb_index; 142 143 /* Represents the dma address to the dcbs array */ 144 dma_addr_t dma; 145 146 /* Represents the page order that is used to allocate the pages for the 147 * RX buffers. This value is calculated based on max MTU of the devices. 148 */ 149 u8 page_order; 150 151 u8 channel_id; 152 }; 153 154 struct lan966x_tx_dcb_buf { 155 struct net_device *dev; 156 struct sk_buff *skb; 157 dma_addr_t dma_addr; 158 bool used; 159 bool ptp; 160 }; 161 162 struct lan966x_tx { 163 struct lan966x *lan966x; 164 165 /* Pointer to the dcb list */ 166 struct lan966x_tx_dcb *dcbs; 167 u16 last_in_use; 168 169 /* Represents the DMA address to the first entry of the dcb entries. */ 170 dma_addr_t dma; 171 172 /* Array of dcbs that are given to the HW */ 173 struct lan966x_tx_dcb_buf *dcbs_buf; 174 175 u8 channel_id; 176 177 bool activated; 178 }; 179 180 struct lan966x_stat_layout { 181 u32 offset; 182 char name[ETH_GSTRING_LEN]; 183 }; 184 185 struct lan966x_phc { 186 struct ptp_clock *clock; 187 struct ptp_clock_info info; 188 struct ptp_pin_desc pins[LAN966X_PHC_PINS_NUM]; 189 struct hwtstamp_config hwtstamp_config; 190 struct lan966x *lan966x; 191 u8 index; 192 }; 193 194 struct lan966x_skb_cb { 195 u8 rew_op; 196 u16 ts_id; 197 unsigned long jiffies; 198 }; 199 200 #define LAN966X_PTP_TIMEOUT msecs_to_jiffies(10) 201 #define LAN966X_SKB_CB(skb) \ 202 ((struct lan966x_skb_cb *)((skb)->cb)) 203 204 struct lan966x { 205 struct device *dev; 206 207 u8 num_phys_ports; 208 struct lan966x_port **ports; 209 210 void __iomem *regs[NUM_TARGETS]; 211 212 int shared_queue_sz; 213 214 u8 base_mac[ETH_ALEN]; 215 216 spinlock_t tx_lock; /* lock for frame transmition */ 217 218 struct net_device *bridge; 219 u16 bridge_mask; 220 u16 bridge_fwd_mask; 221 222 struct list_head mac_entries; 223 spinlock_t mac_lock; /* lock for mac_entries list */ 224 225 u16 vlan_mask[VLAN_N_VID]; 226 DECLARE_BITMAP(cpu_vlan_mask, VLAN_N_VID); 227 228 /* stats */ 229 const struct lan966x_stat_layout *stats_layout; 230 u32 num_stats; 231 232 /* workqueue for reading stats */ 233 struct mutex stats_lock; 234 u64 *stats; 235 struct delayed_work stats_work; 236 struct workqueue_struct *stats_queue; 237 238 /* interrupts */ 239 int xtr_irq; 240 int ana_irq; 241 int ptp_irq; 242 int fdma_irq; 243 int ptp_ext_irq; 244 245 /* worqueue for fdb */ 246 struct workqueue_struct *fdb_work; 247 struct list_head fdb_entries; 248 249 /* mdb */ 250 struct list_head mdb_entries; 251 struct list_head pgid_entries; 252 253 /* ptp */ 254 bool ptp; 255 struct lan966x_phc phc[LAN966X_PHC_COUNT]; 256 spinlock_t ptp_clock_lock; /* lock for phc */ 257 spinlock_t ptp_ts_id_lock; /* lock for ts_id */ 258 struct mutex ptp_lock; /* lock for ptp interface state */ 259 u16 ptp_skbs; 260 261 /* fdma */ 262 bool fdma; 263 struct net_device *fdma_ndev; 264 struct lan966x_rx rx; 265 struct lan966x_tx tx; 266 struct napi_struct napi; 267 268 /* Mirror */ 269 struct lan966x_port *mirror_monitor; 270 u32 mirror_mask[2]; 271 u32 mirror_count; 272 }; 273 274 struct lan966x_port_config { 275 phy_interface_t portmode; 276 const unsigned long *advertising; 277 int speed; 278 int duplex; 279 u32 pause; 280 bool inband; 281 bool autoneg; 282 }; 283 284 struct lan966x_port_tc { 285 bool ingress_shared_block; 286 unsigned long police_id; 287 unsigned long ingress_mirror_id; 288 unsigned long egress_mirror_id; 289 struct flow_stats police_stat; 290 struct flow_stats mirror_stat; 291 }; 292 293 struct lan966x_port { 294 struct net_device *dev; 295 struct lan966x *lan966x; 296 297 u8 chip_port; 298 u16 pvid; 299 u16 vid; 300 bool vlan_aware; 301 302 bool learn_ena; 303 bool mcast_ena; 304 305 struct phylink_config phylink_config; 306 struct phylink_pcs phylink_pcs; 307 struct lan966x_port_config config; 308 struct phylink *phylink; 309 struct phy *serdes; 310 struct fwnode_handle *fwnode; 311 312 u8 ptp_cmd; 313 u16 ts_id; 314 struct sk_buff_head tx_skbs; 315 316 struct net_device *bond; 317 bool lag_tx_active; 318 enum netdev_lag_hash hash_type; 319 320 struct lan966x_port_tc tc; 321 }; 322 323 extern const struct phylink_mac_ops lan966x_phylink_mac_ops; 324 extern const struct phylink_pcs_ops lan966x_phylink_pcs_ops; 325 extern const struct ethtool_ops lan966x_ethtool_ops; 326 extern struct notifier_block lan966x_switchdev_nb __read_mostly; 327 extern struct notifier_block lan966x_switchdev_blocking_nb __read_mostly; 328 329 bool lan966x_netdevice_check(const struct net_device *dev); 330 331 void lan966x_register_notifier_blocks(void); 332 void lan966x_unregister_notifier_blocks(void); 333 334 bool lan966x_hw_offload(struct lan966x *lan966x, u32 port, struct sk_buff *skb); 335 336 void lan966x_ifh_get_src_port(void *ifh, u64 *src_port); 337 void lan966x_ifh_get_timestamp(void *ifh, u64 *timestamp); 338 339 void lan966x_stats_get(struct net_device *dev, 340 struct rtnl_link_stats64 *stats); 341 int lan966x_stats_init(struct lan966x *lan966x); 342 343 void lan966x_port_config_down(struct lan966x_port *port); 344 void lan966x_port_config_up(struct lan966x_port *port); 345 void lan966x_port_status_get(struct lan966x_port *port, 346 struct phylink_link_state *state); 347 int lan966x_port_pcs_set(struct lan966x_port *port, 348 struct lan966x_port_config *config); 349 void lan966x_port_init(struct lan966x_port *port); 350 351 int lan966x_mac_ip_learn(struct lan966x *lan966x, 352 bool cpu_copy, 353 const unsigned char mac[ETH_ALEN], 354 unsigned int vid, 355 enum macaccess_entry_type type); 356 int lan966x_mac_learn(struct lan966x *lan966x, int port, 357 const unsigned char mac[ETH_ALEN], 358 unsigned int vid, 359 enum macaccess_entry_type type); 360 int lan966x_mac_forget(struct lan966x *lan966x, 361 const unsigned char mac[ETH_ALEN], 362 unsigned int vid, 363 enum macaccess_entry_type type); 364 int lan966x_mac_cpu_learn(struct lan966x *lan966x, const char *addr, u16 vid); 365 int lan966x_mac_cpu_forget(struct lan966x *lan966x, const char *addr, u16 vid); 366 void lan966x_mac_init(struct lan966x *lan966x); 367 void lan966x_mac_set_ageing(struct lan966x *lan966x, 368 u32 ageing); 369 int lan966x_mac_del_entry(struct lan966x *lan966x, 370 const unsigned char *addr, 371 u16 vid); 372 int lan966x_mac_add_entry(struct lan966x *lan966x, 373 struct lan966x_port *port, 374 const unsigned char *addr, 375 u16 vid); 376 void lan966x_mac_lag_replace_port_entry(struct lan966x *lan966x, 377 struct lan966x_port *src, 378 struct lan966x_port *dst); 379 void lan966x_mac_lag_remove_port_entry(struct lan966x *lan966x, 380 struct lan966x_port *src); 381 void lan966x_mac_purge_entries(struct lan966x *lan966x); 382 irqreturn_t lan966x_mac_irq_handler(struct lan966x *lan966x); 383 384 void lan966x_vlan_init(struct lan966x *lan966x); 385 void lan966x_vlan_port_apply(struct lan966x_port *port); 386 bool lan966x_vlan_cpu_member_cpu_vlan_mask(struct lan966x *lan966x, u16 vid); 387 void lan966x_vlan_port_set_vlan_aware(struct lan966x_port *port, 388 bool vlan_aware); 389 int lan966x_vlan_port_set_vid(struct lan966x_port *port, 390 u16 vid, 391 bool pvid, 392 bool untagged); 393 void lan966x_vlan_port_add_vlan(struct lan966x_port *port, 394 u16 vid, 395 bool pvid, 396 bool untagged); 397 void lan966x_vlan_port_del_vlan(struct lan966x_port *port, u16 vid); 398 void lan966x_vlan_cpu_add_vlan(struct lan966x *lan966x, u16 vid); 399 void lan966x_vlan_cpu_del_vlan(struct lan966x *lan966x, u16 vid); 400 401 void lan966x_fdb_write_entries(struct lan966x *lan966x, u16 vid); 402 void lan966x_fdb_erase_entries(struct lan966x *lan966x, u16 vid); 403 int lan966x_fdb_init(struct lan966x *lan966x); 404 void lan966x_fdb_deinit(struct lan966x *lan966x); 405 void lan966x_fdb_flush_workqueue(struct lan966x *lan966x); 406 int lan966x_handle_fdb(struct net_device *dev, 407 struct net_device *orig_dev, 408 unsigned long event, const void *ctx, 409 const struct switchdev_notifier_fdb_info *fdb_info); 410 411 void lan966x_mdb_init(struct lan966x *lan966x); 412 void lan966x_mdb_deinit(struct lan966x *lan966x); 413 int lan966x_handle_port_mdb_add(struct lan966x_port *port, 414 const struct switchdev_obj *obj); 415 int lan966x_handle_port_mdb_del(struct lan966x_port *port, 416 const struct switchdev_obj *obj); 417 void lan966x_mdb_erase_entries(struct lan966x *lan966x, u16 vid); 418 void lan966x_mdb_write_entries(struct lan966x *lan966x, u16 vid); 419 void lan966x_mdb_clear_entries(struct lan966x *lan966x); 420 void lan966x_mdb_restore_entries(struct lan966x *lan966x); 421 422 int lan966x_ptp_init(struct lan966x *lan966x); 423 void lan966x_ptp_deinit(struct lan966x *lan966x); 424 int lan966x_ptp_hwtstamp_set(struct lan966x_port *port, struct ifreq *ifr); 425 int lan966x_ptp_hwtstamp_get(struct lan966x_port *port, struct ifreq *ifr); 426 void lan966x_ptp_rxtstamp(struct lan966x *lan966x, struct sk_buff *skb, 427 u64 timestamp); 428 int lan966x_ptp_txtstamp_request(struct lan966x_port *port, 429 struct sk_buff *skb); 430 void lan966x_ptp_txtstamp_release(struct lan966x_port *port, 431 struct sk_buff *skb); 432 irqreturn_t lan966x_ptp_irq_handler(int irq, void *args); 433 irqreturn_t lan966x_ptp_ext_irq_handler(int irq, void *args); 434 u32 lan966x_ptp_get_period_ps(void); 435 int lan966x_ptp_gettime64(struct ptp_clock_info *ptp, struct timespec64 *ts); 436 437 int lan966x_fdma_xmit(struct sk_buff *skb, __be32 *ifh, struct net_device *dev); 438 int lan966x_fdma_change_mtu(struct lan966x *lan966x); 439 void lan966x_fdma_netdev_init(struct lan966x *lan966x, struct net_device *dev); 440 void lan966x_fdma_netdev_deinit(struct lan966x *lan966x, struct net_device *dev); 441 int lan966x_fdma_init(struct lan966x *lan966x); 442 void lan966x_fdma_deinit(struct lan966x *lan966x); 443 irqreturn_t lan966x_fdma_irq_handler(int irq, void *args); 444 445 int lan966x_lag_port_join(struct lan966x_port *port, 446 struct net_device *brport_dev, 447 struct net_device *bond, 448 struct netlink_ext_ack *extack); 449 void lan966x_lag_port_leave(struct lan966x_port *port, struct net_device *bond); 450 int lan966x_lag_port_prechangeupper(struct net_device *dev, 451 struct netdev_notifier_changeupper_info *info); 452 int lan966x_lag_port_changelowerstate(struct net_device *dev, 453 struct netdev_notifier_changelowerstate_info *info); 454 int lan966x_lag_netdev_prechangeupper(struct net_device *dev, 455 struct netdev_notifier_changeupper_info *info); 456 int lan966x_lag_netdev_changeupper(struct net_device *dev, 457 struct netdev_notifier_changeupper_info *info); 458 bool lan966x_lag_first_port(struct net_device *lag, struct net_device *dev); 459 u32 lan966x_lag_get_mask(struct lan966x *lan966x, struct net_device *bond); 460 461 int lan966x_port_changeupper(struct net_device *dev, 462 struct net_device *brport_dev, 463 struct netdev_notifier_changeupper_info *info); 464 int lan966x_port_prechangeupper(struct net_device *dev, 465 struct net_device *brport_dev, 466 struct netdev_notifier_changeupper_info *info); 467 void lan966x_port_stp_state_set(struct lan966x_port *port, u8 state); 468 void lan966x_port_ageing_set(struct lan966x_port *port, 469 unsigned long ageing_clock_t); 470 void lan966x_update_fwd_mask(struct lan966x *lan966x); 471 472 int lan966x_tc_setup(struct net_device *dev, enum tc_setup_type type, 473 void *type_data); 474 475 int lan966x_mqprio_add(struct lan966x_port *port, u8 num_tc); 476 int lan966x_mqprio_del(struct lan966x_port *port); 477 478 void lan966x_taprio_init(struct lan966x *lan966x); 479 void lan966x_taprio_deinit(struct lan966x *lan966x); 480 int lan966x_taprio_add(struct lan966x_port *port, 481 struct tc_taprio_qopt_offload *qopt); 482 int lan966x_taprio_del(struct lan966x_port *port); 483 int lan966x_taprio_speed_set(struct lan966x_port *port, int speed); 484 485 int lan966x_tbf_add(struct lan966x_port *port, 486 struct tc_tbf_qopt_offload *qopt); 487 int lan966x_tbf_del(struct lan966x_port *port, 488 struct tc_tbf_qopt_offload *qopt); 489 490 int lan966x_cbs_add(struct lan966x_port *port, 491 struct tc_cbs_qopt_offload *qopt); 492 int lan966x_cbs_del(struct lan966x_port *port, 493 struct tc_cbs_qopt_offload *qopt); 494 495 int lan966x_ets_add(struct lan966x_port *port, 496 struct tc_ets_qopt_offload *qopt); 497 int lan966x_ets_del(struct lan966x_port *port, 498 struct tc_ets_qopt_offload *qopt); 499 500 int lan966x_tc_matchall(struct lan966x_port *port, 501 struct tc_cls_matchall_offload *f, 502 bool ingress); 503 504 int lan966x_police_port_add(struct lan966x_port *port, 505 struct flow_action *action, 506 struct flow_action_entry *act, 507 unsigned long police_id, 508 bool ingress, 509 struct netlink_ext_ack *extack); 510 int lan966x_police_port_del(struct lan966x_port *port, 511 unsigned long police_id, 512 struct netlink_ext_ack *extack); 513 void lan966x_police_port_stats(struct lan966x_port *port, 514 struct flow_stats *stats); 515 516 int lan966x_mirror_port_add(struct lan966x_port *port, 517 struct flow_action_entry *action, 518 unsigned long mirror_id, 519 bool ingress, 520 struct netlink_ext_ack *extack); 521 int lan966x_mirror_port_del(struct lan966x_port *port, 522 bool ingress, 523 struct netlink_ext_ack *extack); 524 void lan966x_mirror_port_stats(struct lan966x_port *port, 525 struct flow_stats *stats, 526 bool ingress); 527 528 static inline void __iomem *lan_addr(void __iomem *base[], 529 int id, int tinst, int tcnt, 530 int gbase, int ginst, 531 int gcnt, int gwidth, 532 int raddr, int rinst, 533 int rcnt, int rwidth) 534 { 535 WARN_ON((tinst) >= tcnt); 536 WARN_ON((ginst) >= gcnt); 537 WARN_ON((rinst) >= rcnt); 538 return base[id + (tinst)] + 539 gbase + ((ginst) * gwidth) + 540 raddr + ((rinst) * rwidth); 541 } 542 543 static inline u32 lan_rd(struct lan966x *lan966x, int id, int tinst, int tcnt, 544 int gbase, int ginst, int gcnt, int gwidth, 545 int raddr, int rinst, int rcnt, int rwidth) 546 { 547 return readl(lan_addr(lan966x->regs, id, tinst, tcnt, gbase, ginst, 548 gcnt, gwidth, raddr, rinst, rcnt, rwidth)); 549 } 550 551 static inline void lan_wr(u32 val, struct lan966x *lan966x, 552 int id, int tinst, int tcnt, 553 int gbase, int ginst, int gcnt, int gwidth, 554 int raddr, int rinst, int rcnt, int rwidth) 555 { 556 writel(val, lan_addr(lan966x->regs, id, tinst, tcnt, 557 gbase, ginst, gcnt, gwidth, 558 raddr, rinst, rcnt, rwidth)); 559 } 560 561 static inline void lan_rmw(u32 val, u32 mask, struct lan966x *lan966x, 562 int id, int tinst, int tcnt, 563 int gbase, int ginst, int gcnt, int gwidth, 564 int raddr, int rinst, int rcnt, int rwidth) 565 { 566 u32 nval; 567 568 nval = readl(lan_addr(lan966x->regs, id, tinst, tcnt, gbase, ginst, 569 gcnt, gwidth, raddr, rinst, rcnt, rwidth)); 570 nval = (nval & ~mask) | (val & mask); 571 writel(nval, lan_addr(lan966x->regs, id, tinst, tcnt, gbase, ginst, 572 gcnt, gwidth, raddr, rinst, rcnt, rwidth)); 573 } 574 575 #endif /* __LAN966X_MAIN_H__ */ 576