1/* SPDX-License-Identifier: GPL-2.0-or-later */
2/*
3 * Copyright Gavin Shan, IBM Corporation 2016.
4 */
5
6#ifndef __NCSI_INTERNAL_H__
7#define __NCSI_INTERNAL_H__
8
9enum {
10	NCSI_CAP_BASE		= 0,
11	NCSI_CAP_GENERIC	= 0,
12	NCSI_CAP_BC,
13	NCSI_CAP_MC,
14	NCSI_CAP_BUFFER,
15	NCSI_CAP_AEN,
16	NCSI_CAP_VLAN,
17	NCSI_CAP_MAX
18};
19
20enum {
21	NCSI_CAP_GENERIC_HWA             = 0x01, /* HW arbitration           */
22	NCSI_CAP_GENERIC_HDS             = 0x02, /* HNC driver status change */
23	NCSI_CAP_GENERIC_FC              = 0x04, /* HNC to MC flow control   */
24	NCSI_CAP_GENERIC_FC1             = 0x08, /* MC to HNC flow control   */
25	NCSI_CAP_GENERIC_MC              = 0x10, /* Global MC filtering      */
26	NCSI_CAP_GENERIC_HWA_UNKNOWN     = 0x00, /* Unknown HW arbitration   */
27	NCSI_CAP_GENERIC_HWA_SUPPORT     = 0x20, /* Supported HW arbitration */
28	NCSI_CAP_GENERIC_HWA_NOT_SUPPORT = 0x40, /* No HW arbitration        */
29	NCSI_CAP_GENERIC_HWA_RESERVED    = 0x60, /* Reserved HW arbitration  */
30	NCSI_CAP_GENERIC_HWA_MASK        = 0x60, /* Mask for HW arbitration  */
31	NCSI_CAP_GENERIC_MASK            = 0x7f,
32	NCSI_CAP_BC_ARP                  = 0x01, /* ARP packet filtering     */
33	NCSI_CAP_BC_DHCPC                = 0x02, /* DHCP client filtering    */
34	NCSI_CAP_BC_DHCPS                = 0x04, /* DHCP server filtering    */
35	NCSI_CAP_BC_NETBIOS              = 0x08, /* NetBIOS packet filtering */
36	NCSI_CAP_BC_MASK                 = 0x0f,
37	NCSI_CAP_MC_IPV6_NEIGHBOR        = 0x01, /* IPv6 neighbor filtering  */
38	NCSI_CAP_MC_IPV6_ROUTER          = 0x02, /* IPv6 router filering     */
39	NCSI_CAP_MC_DHCPV6_RELAY         = 0x04, /* DHCPv6 relay / server MC */
40	NCSI_CAP_MC_DHCPV6_WELL_KNOWN    = 0x08, /* DHCPv6 well-known MC     */
41	NCSI_CAP_MC_IPV6_MLD             = 0x10, /* IPv6 MLD filtering       */
42	NCSI_CAP_MC_IPV6_NEIGHBOR_S      = 0x20, /* IPv6 neighbour filtering */
43	NCSI_CAP_MC_MASK                 = 0x3f,
44	NCSI_CAP_AEN_LSC                 = 0x01, /* Link status change       */
45	NCSI_CAP_AEN_CR                  = 0x02, /* Configuration required   */
46	NCSI_CAP_AEN_HDS                 = 0x04, /* HNC driver status        */
47	NCSI_CAP_AEN_MASK                = 0x07,
48	NCSI_CAP_VLAN_ONLY               = 0x01, /* Filter VLAN packet only  */
49	NCSI_CAP_VLAN_NO                 = 0x02, /* Filter VLAN and non-VLAN */
50	NCSI_CAP_VLAN_ANY                = 0x04, /* Filter Any-and-non-VLAN  */
51	NCSI_CAP_VLAN_MASK               = 0x07
52};
53
54enum {
55	NCSI_MODE_BASE		= 0,
56	NCSI_MODE_ENABLE	= 0,
57	NCSI_MODE_TX_ENABLE,
58	NCSI_MODE_LINK,
59	NCSI_MODE_VLAN,
60	NCSI_MODE_BC,
61	NCSI_MODE_MC,
62	NCSI_MODE_AEN,
63	NCSI_MODE_FC,
64	NCSI_MODE_MAX
65};
66
67/* Supported media status bits for Mellanox Mac affinity command.
68 * Bit (0-2) for different protocol support; Bit 1 for RBT support,
69 * bit 1 for SMBUS support and bit 2 for PCIE support. Bit (3-5)
70 * for different protocol availability. Bit 4 for RBT, bit 4 for
71 * SMBUS and bit 5 for PCIE.
72 */
73enum {
74	MLX_MC_RBT_SUPPORT  = 0x01, /* MC supports RBT         */
75	MLX_MC_RBT_AVL      = 0x08, /* RBT medium is available */
76};
77
78/* OEM Vendor Manufacture ID */
79#define NCSI_OEM_MFR_MLX_ID             0x8119
80#define NCSI_OEM_MFR_BCM_ID             0x113d
81#define NCSI_OEM_MFR_INTEL_ID           0x157
82/* Intel specific OEM command */
83#define NCSI_OEM_INTEL_CMD_GMA          0x06   /* CMD ID for Get MAC */
84#define NCSI_OEM_INTEL_CMD_KEEP_PHY     0x20   /* CMD ID for Keep PHY up */
85/* Broadcom specific OEM Command */
86#define NCSI_OEM_BCM_CMD_GMA            0x01   /* CMD ID for Get MAC */
87/* Mellanox specific OEM Command */
88#define NCSI_OEM_MLX_CMD_GMA            0x00   /* CMD ID for Get MAC */
89#define NCSI_OEM_MLX_CMD_GMA_PARAM      0x1b   /* Parameter for GMA  */
90#define NCSI_OEM_MLX_CMD_SMAF           0x01   /* CMD ID for Set MC Affinity */
91#define NCSI_OEM_MLX_CMD_SMAF_PARAM     0x07   /* Parameter for SMAF         */
92/* OEM Command payload lengths*/
93#define NCSI_OEM_INTEL_CMD_GMA_LEN      5
94#define NCSI_OEM_INTEL_CMD_KEEP_PHY_LEN 7
95#define NCSI_OEM_BCM_CMD_GMA_LEN        12
96#define NCSI_OEM_MLX_CMD_GMA_LEN        8
97#define NCSI_OEM_MLX_CMD_SMAF_LEN        60
98/* Offset in OEM request */
99#define MLX_SMAF_MAC_ADDR_OFFSET         8     /* Offset for MAC in SMAF    */
100#define MLX_SMAF_MED_SUPPORT_OFFSET      14    /* Offset for medium in SMAF */
101/* Mac address offset in OEM response */
102#define BCM_MAC_ADDR_OFFSET             28
103#define MLX_MAC_ADDR_OFFSET             8
104#define INTEL_MAC_ADDR_OFFSET           1
105
106
107struct ncsi_channel_version {
108	u8   major;		/* NCSI version major */
109	u8   minor;		/* NCSI version minor */
110	u8   update;		/* NCSI version update */
111	char alpha1;		/* NCSI version alpha1 */
112	char alpha2;		/* NCSI version alpha2 */
113	u8  fw_name[12];	/* Firmware name string                */
114	u32 fw_version;		/* Firmware version                   */
115	u16 pci_ids[4];		/* PCI identification                 */
116	u32 mf_id;		/* Manufacture ID                     */
117};
118
119struct ncsi_channel_cap {
120	u32 index;	/* Index of channel capabilities */
121	u32 cap;	/* NCSI channel capability       */
122};
123
124struct ncsi_channel_mode {
125	u32 index;	/* Index of channel modes      */
126	u32 enable;	/* Enabled or disabled         */
127	u32 size;	/* Valid entries in ncm_data[] */
128	u32 data[8];	/* Data entries                */
129};
130
131struct ncsi_channel_mac_filter {
132	u8	n_uc;
133	u8	n_mc;
134	u8	n_mixed;
135	u64	bitmap;
136	unsigned char	*addrs;
137};
138
139struct ncsi_channel_vlan_filter {
140	u8	n_vids;
141	u64	bitmap;
142	u16	*vids;
143};
144
145struct ncsi_channel_stats {
146	u32 hnc_cnt_hi;		/* Counter cleared            */
147	u32 hnc_cnt_lo;		/* Counter cleared            */
148	u32 hnc_rx_bytes;	/* Rx bytes                   */
149	u32 hnc_tx_bytes;	/* Tx bytes                   */
150	u32 hnc_rx_uc_pkts;	/* Rx UC packets              */
151	u32 hnc_rx_mc_pkts;     /* Rx MC packets              */
152	u32 hnc_rx_bc_pkts;	/* Rx BC packets              */
153	u32 hnc_tx_uc_pkts;	/* Tx UC packets              */
154	u32 hnc_tx_mc_pkts;	/* Tx MC packets              */
155	u32 hnc_tx_bc_pkts;	/* Tx BC packets              */
156	u32 hnc_fcs_err;	/* FCS errors                 */
157	u32 hnc_align_err;	/* Alignment errors           */
158	u32 hnc_false_carrier;	/* False carrier detection    */
159	u32 hnc_runt_pkts;	/* Rx runt packets            */
160	u32 hnc_jabber_pkts;	/* Rx jabber packets          */
161	u32 hnc_rx_pause_xon;	/* Rx pause XON frames        */
162	u32 hnc_rx_pause_xoff;	/* Rx XOFF frames             */
163	u32 hnc_tx_pause_xon;	/* Tx XON frames              */
164	u32 hnc_tx_pause_xoff;	/* Tx XOFF frames             */
165	u32 hnc_tx_s_collision;	/* Single collision frames    */
166	u32 hnc_tx_m_collision;	/* Multiple collision frames  */
167	u32 hnc_l_collision;	/* Late collision frames      */
168	u32 hnc_e_collision;	/* Excessive collision frames */
169	u32 hnc_rx_ctl_frames;	/* Rx control frames          */
170	u32 hnc_rx_64_frames;	/* Rx 64-bytes frames         */
171	u32 hnc_rx_127_frames;	/* Rx 65-127 bytes frames     */
172	u32 hnc_rx_255_frames;	/* Rx 128-255 bytes frames    */
173	u32 hnc_rx_511_frames;	/* Rx 256-511 bytes frames    */
174	u32 hnc_rx_1023_frames;	/* Rx 512-1023 bytes frames   */
175	u32 hnc_rx_1522_frames;	/* Rx 1024-1522 bytes frames  */
176	u32 hnc_rx_9022_frames;	/* Rx 1523-9022 bytes frames  */
177	u32 hnc_tx_64_frames;	/* Tx 64-bytes frames         */
178	u32 hnc_tx_127_frames;	/* Tx 65-127 bytes frames     */
179	u32 hnc_tx_255_frames;	/* Tx 128-255 bytes frames    */
180	u32 hnc_tx_511_frames;	/* Tx 256-511 bytes frames    */
181	u32 hnc_tx_1023_frames;	/* Tx 512-1023 bytes frames   */
182	u32 hnc_tx_1522_frames;	/* Tx 1024-1522 bytes frames  */
183	u32 hnc_tx_9022_frames;	/* Tx 1523-9022 bytes frames  */
184	u32 hnc_rx_valid_bytes;	/* Rx valid bytes             */
185	u32 hnc_rx_runt_pkts;	/* Rx error runt packets      */
186	u32 hnc_rx_jabber_pkts;	/* Rx error jabber packets    */
187	u32 ncsi_rx_cmds;	/* Rx NCSI commands           */
188	u32 ncsi_dropped_cmds;	/* Dropped commands           */
189	u32 ncsi_cmd_type_errs;	/* Command type errors        */
190	u32 ncsi_cmd_csum_errs;	/* Command checksum errors    */
191	u32 ncsi_rx_pkts;	/* Rx NCSI packets            */
192	u32 ncsi_tx_pkts;	/* Tx NCSI packets            */
193	u32 ncsi_tx_aen_pkts;	/* Tx AEN packets             */
194	u32 pt_tx_pkts;		/* Tx packets                 */
195	u32 pt_tx_dropped;	/* Tx dropped packets         */
196	u32 pt_tx_channel_err;	/* Tx channel errors          */
197	u32 pt_tx_us_err;	/* Tx undersize errors        */
198	u32 pt_rx_pkts;		/* Rx packets                 */
199	u32 pt_rx_dropped;	/* Rx dropped packets         */
200	u32 pt_rx_channel_err;	/* Rx channel errors          */
201	u32 pt_rx_us_err;	/* Rx undersize errors        */
202	u32 pt_rx_os_err;	/* Rx oversize errors         */
203};
204
205struct ncsi_dev_priv;
206struct ncsi_package;
207
208#define NCSI_PACKAGE_SHIFT	5
209#define NCSI_PACKAGE_INDEX(c)	(((c) >> NCSI_PACKAGE_SHIFT) & 0x7)
210#define NCSI_RESERVED_CHANNEL	0x1f
211#define NCSI_CHANNEL_INDEX(c)	((c) & ((1 << NCSI_PACKAGE_SHIFT) - 1))
212#define NCSI_TO_CHANNEL(p, c)	(((p) << NCSI_PACKAGE_SHIFT) | (c))
213#define NCSI_MAX_PACKAGE	8
214#define NCSI_MAX_CHANNEL	32
215
216struct ncsi_channel {
217	unsigned char               id;
218	int                         state;
219#define NCSI_CHANNEL_INACTIVE		1
220#define NCSI_CHANNEL_ACTIVE		2
221#define NCSI_CHANNEL_INVISIBLE		3
222	bool                        reconfigure_needed;
223	spinlock_t                  lock;	/* Protect filters etc */
224	struct ncsi_package         *package;
225	struct ncsi_channel_version version;
226	struct ncsi_channel_cap	    caps[NCSI_CAP_MAX];
227	struct ncsi_channel_mode    modes[NCSI_MODE_MAX];
228	/* Filtering Settings */
229	struct ncsi_channel_mac_filter	mac_filter;
230	struct ncsi_channel_vlan_filter	vlan_filter;
231	struct ncsi_channel_stats   stats;
232	struct {
233		struct timer_list   timer;
234		bool                enabled;
235		unsigned int        state;
236#define NCSI_CHANNEL_MONITOR_START	0
237#define NCSI_CHANNEL_MONITOR_RETRY	1
238#define NCSI_CHANNEL_MONITOR_WAIT	2
239#define NCSI_CHANNEL_MONITOR_WAIT_MAX	5
240	} monitor;
241	struct list_head            node;
242	struct list_head            link;
243};
244
245struct ncsi_package {
246	unsigned char        id;          /* NCSI 3-bits package ID */
247	unsigned char        uuid[16];    /* UUID                   */
248	struct ncsi_dev_priv *ndp;        /* NCSI device            */
249	spinlock_t           lock;        /* Protect the package    */
250	unsigned int         channel_num; /* Number of channels     */
251	struct list_head     channels;    /* List of channels        */
252	struct list_head     node;        /* Form list of packages  */
253
254	bool                 multi_channel; /* Enable multiple channels  */
255	u32                  channel_whitelist; /* Channels to configure */
256	struct ncsi_channel  *preferred_channel; /* Primary channel      */
257};
258
259struct ncsi_request {
260	unsigned char        id;      /* Request ID - 0 to 255           */
261	bool                 used;    /* Request that has been assigned  */
262	unsigned int         flags;   /* NCSI request property           */
263#define NCSI_REQ_FLAG_EVENT_DRIVEN	1
264#define NCSI_REQ_FLAG_NETLINK_DRIVEN	2
265	struct ncsi_dev_priv *ndp;    /* Associated NCSI device          */
266	struct sk_buff       *cmd;    /* Associated NCSI command packet  */
267	struct sk_buff       *rsp;    /* Associated NCSI response packet */
268	struct timer_list    timer;   /* Timer on waiting for response   */
269	bool                 enabled; /* Time has been enabled or not    */
270	u32                  snd_seq;     /* netlink sending sequence number */
271	u32                  snd_portid;  /* netlink portid of sender        */
272	struct nlmsghdr      nlhdr;       /* netlink message header          */
273};
274
275enum {
276	ncsi_dev_state_major		= 0xff00,
277	ncsi_dev_state_minor		= 0x00ff,
278	ncsi_dev_state_probe_deselect	= 0x0201,
279	ncsi_dev_state_probe_package,
280	ncsi_dev_state_probe_channel,
281	ncsi_dev_state_probe_mlx_gma,
282	ncsi_dev_state_probe_mlx_smaf,
283	ncsi_dev_state_probe_cis,
284	ncsi_dev_state_probe_keep_phy,
285	ncsi_dev_state_probe_gvi,
286	ncsi_dev_state_probe_gc,
287	ncsi_dev_state_probe_gls,
288	ncsi_dev_state_probe_dp,
289	ncsi_dev_state_config_sp	= 0x0301,
290	ncsi_dev_state_config_cis,
291	ncsi_dev_state_config_oem_gma,
292	ncsi_dev_state_config_clear_vids,
293	ncsi_dev_state_config_svf,
294	ncsi_dev_state_config_ev,
295	ncsi_dev_state_config_sma,
296	ncsi_dev_state_config_ebf,
297	ncsi_dev_state_config_dgmf,
298	ncsi_dev_state_config_ecnt,
299	ncsi_dev_state_config_ec,
300	ncsi_dev_state_config_ae,
301	ncsi_dev_state_config_gls,
302	ncsi_dev_state_config_done,
303	ncsi_dev_state_suspend_select	= 0x0401,
304	ncsi_dev_state_suspend_gls,
305	ncsi_dev_state_suspend_dcnt,
306	ncsi_dev_state_suspend_dc,
307	ncsi_dev_state_suspend_deselect,
308	ncsi_dev_state_suspend_done
309};
310
311struct vlan_vid {
312	struct list_head list;
313	__be16 proto;
314	u16 vid;
315};
316
317struct ncsi_dev_priv {
318	struct ncsi_dev     ndev;            /* Associated NCSI device     */
319	unsigned int        flags;           /* NCSI device flags          */
320#define NCSI_DEV_PROBED		1            /* Finalized NCSI topology    */
321#define NCSI_DEV_HWA		2            /* Enabled HW arbitration     */
322#define NCSI_DEV_RESHUFFLE	4
323#define NCSI_DEV_RESET		8            /* Reset state of NC          */
324	unsigned int        gma_flag;        /* OEM GMA flag               */
325	spinlock_t          lock;            /* Protect the NCSI device    */
326	unsigned int        package_probe_id;/* Current ID during probe    */
327	unsigned int        package_num;     /* Number of packages         */
328	struct list_head    packages;        /* List of packages           */
329	struct ncsi_channel *hot_channel;    /* Channel was ever active    */
330	struct ncsi_request requests[256];   /* Request table              */
331	unsigned int        request_id;      /* Last used request ID       */
332#define NCSI_REQ_START_IDX	1
333	unsigned int        pending_req_num; /* Number of pending requests */
334	struct ncsi_package *active_package; /* Currently handled package  */
335	struct ncsi_channel *active_channel; /* Currently handled channel  */
336	struct list_head    channel_queue;   /* Config queue of channels   */
337	struct work_struct  work;            /* For channel management     */
338	struct packet_type  ptype;           /* NCSI packet Rx handler     */
339	struct list_head    node;            /* Form NCSI device list      */
340#define NCSI_MAX_VLAN_VIDS	15
341	struct list_head    vlan_vids;       /* List of active VLAN IDs */
342
343	bool                multi_package;   /* Enable multiple packages   */
344	bool                mlx_multi_host;  /* Enable multi host Mellanox */
345	u32                 package_whitelist; /* Packages to configure    */
346};
347
348struct ncsi_cmd_arg {
349	struct ncsi_dev_priv *ndp;        /* Associated NCSI device        */
350	unsigned char        type;        /* Command in the NCSI packet    */
351	unsigned char        id;          /* Request ID (sequence number)  */
352	unsigned char        package;     /* Destination package ID        */
353	unsigned char        channel;     /* Destination channel ID or 0x1f */
354	unsigned short       payload;     /* Command packet payload length */
355	unsigned int         req_flags;   /* NCSI request properties       */
356	union {
357		unsigned char  bytes[16]; /* Command packet specific data  */
358		unsigned short words[8];
359		unsigned int   dwords[4];
360	};
361	unsigned char        *data;       /* NCSI OEM data                 */
362	struct genl_info     *info;       /* Netlink information           */
363};
364
365extern struct list_head ncsi_dev_list;
366extern spinlock_t ncsi_dev_lock;
367
368#define TO_NCSI_DEV_PRIV(nd) \
369	container_of(nd, struct ncsi_dev_priv, ndev)
370#define NCSI_FOR_EACH_DEV(ndp) \
371	list_for_each_entry_rcu(ndp, &ncsi_dev_list, node)
372#define NCSI_FOR_EACH_PACKAGE(ndp, np) \
373	list_for_each_entry_rcu(np, &ndp->packages, node)
374#define NCSI_FOR_EACH_CHANNEL(np, nc) \
375	list_for_each_entry_rcu(nc, &np->channels, node)
376
377/* Resources */
378int ncsi_reset_dev(struct ncsi_dev *nd);
379void ncsi_start_channel_monitor(struct ncsi_channel *nc);
380void ncsi_stop_channel_monitor(struct ncsi_channel *nc);
381struct ncsi_channel *ncsi_find_channel(struct ncsi_package *np,
382				       unsigned char id);
383struct ncsi_channel *ncsi_add_channel(struct ncsi_package *np,
384				      unsigned char id);
385struct ncsi_package *ncsi_find_package(struct ncsi_dev_priv *ndp,
386				       unsigned char id);
387struct ncsi_package *ncsi_add_package(struct ncsi_dev_priv *ndp,
388				      unsigned char id);
389void ncsi_remove_package(struct ncsi_package *np);
390void ncsi_find_package_and_channel(struct ncsi_dev_priv *ndp,
391				   unsigned char id,
392				   struct ncsi_package **np,
393				   struct ncsi_channel **nc);
394struct ncsi_request *ncsi_alloc_request(struct ncsi_dev_priv *ndp,
395					unsigned int req_flags);
396void ncsi_free_request(struct ncsi_request *nr);
397struct ncsi_dev *ncsi_find_dev(struct net_device *dev);
398int ncsi_process_next_channel(struct ncsi_dev_priv *ndp);
399bool ncsi_channel_has_link(struct ncsi_channel *channel);
400bool ncsi_channel_is_last(struct ncsi_dev_priv *ndp,
401			  struct ncsi_channel *channel);
402int ncsi_update_tx_channel(struct ncsi_dev_priv *ndp,
403			   struct ncsi_package *np,
404			   struct ncsi_channel *disable,
405			   struct ncsi_channel *enable);
406
407/* Packet handlers */
408u32 ncsi_calculate_checksum(unsigned char *data, int len);
409int ncsi_xmit_cmd(struct ncsi_cmd_arg *nca);
410int ncsi_rcv_rsp(struct sk_buff *skb, struct net_device *dev,
411		 struct packet_type *pt, struct net_device *orig_dev);
412int ncsi_aen_handler(struct ncsi_dev_priv *ndp, struct sk_buff *skb);
413
414#endif /* __NCSI_INTERNAL_H__ */
415