1 /*
2  * Copyright (c) 2016-2017 Hisilicon Limited.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  */
9 
10 #ifndef __HNAE3_H
11 #define __HNAE3_H
12 
13 /* Names used in this framework:
14  *      ae handle (handle):
15  *        a set of queues provided by AE
16  *      ring buffer queue (rbq):
17  *        the channel between upper layer and the AE, can do tx and rx
18  *      ring:
19  *        a tx or rx channel within a rbq
20  *      ring description (desc):
21  *        an element in the ring with packet information
22  *      buffer:
23  *        a memory region referred by desc with the full packet payload
24  *
25  * "num" means a static number set as a parameter, "count" mean a dynamic
26  *   number set while running
27  * "cb" means control block
28  */
29 
30 #include <linux/acpi.h>
31 #include <linux/delay.h>
32 #include <linux/device.h>
33 #include <linux/module.h>
34 #include <linux/netdevice.h>
35 #include <linux/pci.h>
36 #include <linux/types.h>
37 
38 /* Device IDs */
39 #define HNAE3_DEV_ID_GE				0xA220
40 #define HNAE3_DEV_ID_25GE			0xA221
41 #define HNAE3_DEV_ID_25GE_RDMA			0xA222
42 #define HNAE3_DEV_ID_25GE_RDMA_MACSEC		0xA223
43 #define HNAE3_DEV_ID_50GE_RDMA			0xA224
44 #define HNAE3_DEV_ID_50GE_RDMA_MACSEC		0xA225
45 #define HNAE3_DEV_ID_100G_RDMA_MACSEC		0xA226
46 #define HNAE3_DEV_ID_100G_VF			0xA22E
47 #define HNAE3_DEV_ID_100G_RDMA_DCB_PFC_VF	0xA22F
48 
49 #define HNAE3_CLASS_NAME_SIZE 16
50 
51 #define HNAE3_DEV_INITED_B			0x0
52 #define HNAE_DEV_SUPPORT_ROCE_B			0x1
53 
54 #define ring_ptr_move_fw(ring, p) \
55 	((ring)->p = ((ring)->p + 1) % (ring)->desc_num)
56 #define ring_ptr_move_bw(ring, p) \
57 	((ring)->p = ((ring)->p - 1 + (ring)->desc_num) % (ring)->desc_num)
58 
59 enum hns_desc_type {
60 	DESC_TYPE_SKB,
61 	DESC_TYPE_PAGE,
62 };
63 
64 struct hnae3_handle;
65 
66 struct hnae3_queue {
67 	void __iomem *io_base;
68 	struct hnae3_ae_algo *ae_algo;
69 	struct hnae3_handle *handle;
70 	int tqp_index;	/* index in a handle */
71 	u32 buf_size;	/* size for hnae_desc->addr, preset by AE */
72 	u16 desc_num;	/* total number of desc */
73 };
74 
75 /*hnae3 loop mode*/
76 enum hnae3_loop {
77 	HNAE3_MAC_INTER_LOOP_MAC,
78 	HNAE3_MAC_INTER_LOOP_SERDES,
79 	HNAE3_MAC_INTER_LOOP_PHY,
80 	HNAE3_MAC_LOOP_NONE,
81 };
82 
83 enum hnae3_client_type {
84 	HNAE3_CLIENT_KNIC,
85 	HNAE3_CLIENT_UNIC,
86 	HNAE3_CLIENT_ROCE,
87 };
88 
89 enum hnae3_dev_type {
90 	HNAE3_DEV_KNIC,
91 	HNAE3_DEV_UNIC,
92 };
93 
94 /* mac media type */
95 enum hnae3_media_type {
96 	HNAE3_MEDIA_TYPE_UNKNOWN,
97 	HNAE3_MEDIA_TYPE_FIBER,
98 	HNAE3_MEDIA_TYPE_COPPER,
99 	HNAE3_MEDIA_TYPE_BACKPLANE,
100 };
101 
102 struct hnae3_vector_info {
103 	u8 __iomem *io_addr;
104 	int vector;
105 };
106 
107 #define HNAE3_RING_TYPE_B 0
108 #define HNAE3_RING_TYPE_TX 0
109 #define HNAE3_RING_TYPE_RX 1
110 
111 struct hnae3_ring_chain_node {
112 	struct hnae3_ring_chain_node *next;
113 	u32 tqp_index;
114 	u32 flag;
115 };
116 
117 #define HNAE3_IS_TX_RING(node) \
118 	(((node)->flag & (1 << HNAE3_RING_TYPE_B)) == HNAE3_RING_TYPE_TX)
119 
120 struct hnae3_client_ops {
121 	int (*init_instance)(struct hnae3_handle *handle);
122 	void (*uninit_instance)(struct hnae3_handle *handle, bool reset);
123 	void (*link_status_change)(struct hnae3_handle *handle, bool state);
124 };
125 
126 #define HNAE3_CLIENT_NAME_LENGTH 16
127 struct hnae3_client {
128 	char name[HNAE3_CLIENT_NAME_LENGTH];
129 	u16 version;
130 	unsigned long state;
131 	enum hnae3_client_type type;
132 	const struct hnae3_client_ops *ops;
133 	struct list_head node;
134 };
135 
136 struct hnae3_ae_dev {
137 	struct pci_dev *pdev;
138 	const struct hnae3_ae_ops *ops;
139 	struct list_head node;
140 	u32 flag;
141 	enum hnae3_dev_type dev_type;
142 	void *priv;
143 };
144 
145 /* This struct defines the operation on the handle.
146  *
147  * init_ae_dev(): (mandatory)
148  *   Get PF configure from pci_dev and initialize PF hardware
149  * uninit_ae_dev()
150  *   Disable PF device and release PF resource
151  * register_client
152  *   Register client to ae_dev
153  * unregister_client()
154  *   Unregister client from ae_dev
155  * start()
156  *   Enable the hardware
157  * stop()
158  *   Disable the hardware
159  * get_status()
160  *   Get the carrier state of the back channel of the handle, 1 for ok, 0 for
161  *   non-ok
162  * get_ksettings_an_result()
163  *   Get negotiation status,speed and duplex
164  * update_speed_duplex_h()
165  *   Update hardware speed and duplex
166  * get_media_type()
167  *   Get media type of MAC
168  * adjust_link()
169  *   Adjust link status
170  * set_loopback()
171  *   Set loopback
172  * set_promisc_mode
173  *   Set promisc mode
174  * set_mtu()
175  *   set mtu
176  * get_pauseparam()
177  *   get tx and rx of pause frame use
178  * set_pauseparam()
179  *   set tx and rx of pause frame use
180  * set_autoneg()
181  *   set auto autonegotiation of pause frame use
182  * get_autoneg()
183  *   get auto autonegotiation of pause frame use
184  * get_coalesce_usecs()
185  *   get usecs to delay a TX interrupt after a packet is sent
186  * get_rx_max_coalesced_frames()
187  *   get Maximum number of packets to be sent before a TX interrupt.
188  * set_coalesce_usecs()
189  *   set usecs to delay a TX interrupt after a packet is sent
190  * set_coalesce_frames()
191  *   set Maximum number of packets to be sent before a TX interrupt.
192  * get_mac_addr()
193  *   get mac address
194  * set_mac_addr()
195  *   set mac address
196  * add_uc_addr
197  *   Add unicast addr to mac table
198  * rm_uc_addr
199  *   Remove unicast addr from mac table
200  * set_mc_addr()
201  *   Set multicast address
202  * add_mc_addr
203  *   Add multicast address to mac table
204  * rm_mc_addr
205  *   Remove multicast address from mac table
206  * update_stats()
207  *   Update Old network device statistics
208  * get_ethtool_stats()
209  *   Get ethtool network device statistics
210  * get_strings()
211  *   Get a set of strings that describe the requested objects
212  * get_sset_count()
213  *   Get number of strings that @get_strings will write
214  * update_led_status()
215  *   Update the led status
216  * set_led_id()
217  *   Set led id
218  * get_regs()
219  *   Get regs dump
220  * get_regs_len()
221  *   Get the len of the regs dump
222  * get_rss_key_size()
223  *   Get rss key size
224  * get_rss_indir_size()
225  *   Get rss indirection table size
226  * get_rss()
227  *   Get rss table
228  * set_rss()
229  *   Set rss table
230  * get_tc_size()
231  *   Get tc size of handle
232  * get_vector()
233  *   Get vector number and vector information
234  * map_ring_to_vector()
235  *   Map rings to vector
236  * unmap_ring_from_vector()
237  *   Unmap rings from vector
238  * add_tunnel_udp()
239  *   Add tunnel information to hardware
240  * del_tunnel_udp()
241  *   Delete tunnel information from hardware
242  * reset_queue()
243  *   Reset queue
244  * get_fw_version()
245  *   Get firmware version
246  * get_mdix_mode()
247  *   Get media typr of phy
248  * set_vlan_filter()
249  *   Set vlan filter config of Ports
250  * set_vf_vlan_filter()
251  *   Set vlan filter config of vf
252  */
253 struct hnae3_ae_ops {
254 	int (*init_ae_dev)(struct hnae3_ae_dev *ae_dev);
255 	void (*uninit_ae_dev)(struct hnae3_ae_dev *ae_dev);
256 
257 	int (*init_client_instance)(struct hnae3_client *client,
258 				    struct hnae3_ae_dev *ae_dev);
259 	void (*uninit_client_instance)(struct hnae3_client *client,
260 				       struct hnae3_ae_dev *ae_dev);
261 	int (*start)(struct hnae3_handle *handle);
262 	void (*stop)(struct hnae3_handle *handle);
263 	int (*get_status)(struct hnae3_handle *handle);
264 	void (*get_ksettings_an_result)(struct hnae3_handle *handle,
265 					u8 *auto_neg, u32 *speed, u8 *duplex);
266 
267 	int (*update_speed_duplex_h)(struct hnae3_handle *handle);
268 	int (*cfg_mac_speed_dup_h)(struct hnae3_handle *handle, int speed,
269 				   u8 duplex);
270 
271 	void (*get_media_type)(struct hnae3_handle *handle, u8 *media_type);
272 	void (*adjust_link)(struct hnae3_handle *handle, int speed, int duplex);
273 	int (*set_loopback)(struct hnae3_handle *handle,
274 			    enum hnae3_loop loop_mode, bool en);
275 
276 	void (*set_promisc_mode)(struct hnae3_handle *handle, u32 en);
277 	int (*set_mtu)(struct hnae3_handle *handle, int new_mtu);
278 
279 	void (*get_pauseparam)(struct hnae3_handle *handle,
280 			       u32 *auto_neg, u32 *rx_en, u32 *tx_en);
281 	int (*set_pauseparam)(struct hnae3_handle *handle,
282 			      u32 auto_neg, u32 rx_en, u32 tx_en);
283 
284 	int (*set_autoneg)(struct hnae3_handle *handle, bool enable);
285 	int (*get_autoneg)(struct hnae3_handle *handle);
286 
287 	void (*get_coalesce_usecs)(struct hnae3_handle *handle,
288 				   u32 *tx_usecs, u32 *rx_usecs);
289 	void (*get_rx_max_coalesced_frames)(struct hnae3_handle *handle,
290 					    u32 *tx_frames, u32 *rx_frames);
291 	int (*set_coalesce_usecs)(struct hnae3_handle *handle, u32 timeout);
292 	int (*set_coalesce_frames)(struct hnae3_handle *handle,
293 				   u32 coalesce_frames);
294 	void (*get_coalesce_range)(struct hnae3_handle *handle,
295 				   u32 *tx_frames_low, u32 *rx_frames_low,
296 				   u32 *tx_frames_high, u32 *rx_frames_high,
297 				   u32 *tx_usecs_low, u32 *rx_usecs_low,
298 				   u32 *tx_usecs_high, u32 *rx_usecs_high);
299 
300 	void (*get_mac_addr)(struct hnae3_handle *handle, u8 *p);
301 	int (*set_mac_addr)(struct hnae3_handle *handle, void *p);
302 	int (*add_uc_addr)(struct hnae3_handle *handle,
303 			   const unsigned char *addr);
304 	int (*rm_uc_addr)(struct hnae3_handle *handle,
305 			  const unsigned char *addr);
306 	int (*set_mc_addr)(struct hnae3_handle *handle, void *addr);
307 	int (*add_mc_addr)(struct hnae3_handle *handle,
308 			   const unsigned char *addr);
309 	int (*rm_mc_addr)(struct hnae3_handle *handle,
310 			  const unsigned char *addr);
311 
312 	void (*set_tso_stats)(struct hnae3_handle *handle, int enable);
313 	void (*update_stats)(struct hnae3_handle *handle,
314 			     struct net_device_stats *net_stats);
315 	void (*get_stats)(struct hnae3_handle *handle, u64 *data);
316 
317 	void (*get_strings)(struct hnae3_handle *handle,
318 			    u32 stringset, u8 *data);
319 	int (*get_sset_count)(struct hnae3_handle *handle, int stringset);
320 
321 	void (*get_regs)(struct hnae3_handle *handle, void *data);
322 	int (*get_regs_len)(struct hnae3_handle *handle);
323 
324 	u32 (*get_rss_key_size)(struct hnae3_handle *handle);
325 	u32 (*get_rss_indir_size)(struct hnae3_handle *handle);
326 	int (*get_rss)(struct hnae3_handle *handle, u32 *indir, u8 *key,
327 		       u8 *hfunc);
328 	int (*set_rss)(struct hnae3_handle *handle, const u32 *indir,
329 		       const u8 *key, const u8 hfunc);
330 
331 	int (*get_tc_size)(struct hnae3_handle *handle);
332 
333 	int (*get_vector)(struct hnae3_handle *handle, u16 vector_num,
334 			  struct hnae3_vector_info *vector_info);
335 	int (*map_ring_to_vector)(struct hnae3_handle *handle,
336 				  int vector_num,
337 				  struct hnae3_ring_chain_node *vr_chain);
338 	int (*unmap_ring_from_vector)(struct hnae3_handle *handle,
339 				      int vector_num,
340 				      struct hnae3_ring_chain_node *vr_chain);
341 
342 	int (*add_tunnel_udp)(struct hnae3_handle *handle, u16 port_num);
343 	int (*del_tunnel_udp)(struct hnae3_handle *handle, u16 port_num);
344 
345 	void (*reset_queue)(struct hnae3_handle *handle, u16 queue_id);
346 	u32 (*get_fw_version)(struct hnae3_handle *handle);
347 	void (*get_mdix_mode)(struct hnae3_handle *handle,
348 			      u8 *tp_mdix_ctrl, u8 *tp_mdix);
349 
350 	int (*set_vlan_filter)(struct hnae3_handle *handle, __be16 proto,
351 			       u16 vlan_id, bool is_kill);
352 	int (*set_vf_vlan_filter)(struct hnae3_handle *handle, int vfid,
353 				  u16 vlan, u8 qos, __be16 proto);
354 };
355 
356 struct hnae3_ae_algo {
357 	const struct hnae3_ae_ops *ops;
358 	struct list_head node;
359 	char name[HNAE3_CLASS_NAME_SIZE];
360 	const struct pci_device_id *pdev_id_table;
361 };
362 
363 #define HNAE3_INT_NAME_LEN        (IFNAMSIZ + 16)
364 #define HNAE3_ITR_COUNTDOWN_START 100
365 
366 struct hnae3_tc_info {
367 	u16	tqp_offset;	/* TQP offset from base TQP */
368 	u16	tqp_count;	/* Total TQPs */
369 	u8	up;		/* user priority */
370 	u8	tc;		/* TC index */
371 	bool	enable;		/* If this TC is enable or not */
372 };
373 
374 #define HNAE3_MAX_TC		8
375 struct hnae3_knic_private_info {
376 	struct net_device *netdev; /* Set by KNIC client when init instance */
377 	u16 rss_size;		   /* Allocated RSS queues */
378 	u16 rx_buf_len;
379 	u16 num_desc;
380 
381 	u8 num_tc;		   /* Total number of enabled TCs */
382 	struct hnae3_tc_info tc_info[HNAE3_MAX_TC]; /* Idx of array is HW TC */
383 
384 	u16 num_tqps;		  /* total number of TQPs in this handle */
385 	struct hnae3_queue **tqp;  /* array base of all TQPs in this instance */
386 };
387 
388 struct hnae3_roce_private_info {
389 	struct net_device *netdev;
390 	void __iomem *roce_io_base;
391 	int base_vector;
392 	int num_vectors;
393 };
394 
395 struct hnae3_unic_private_info {
396 	struct net_device *netdev;
397 	u16 rx_buf_len;
398 	u16 num_desc;
399 	u16 num_tqps;	/* total number of tqps in this handle */
400 	struct hnae3_queue **tqp;  /* array base of all TQPs of this instance */
401 };
402 
403 #define HNAE3_SUPPORT_MAC_LOOPBACK    1
404 #define HNAE3_SUPPORT_PHY_LOOPBACK    2
405 #define HNAE3_SUPPORT_SERDES_LOOPBACK 4
406 
407 struct hnae3_handle {
408 	struct hnae3_client *client;
409 	struct pci_dev *pdev;
410 	void *priv;
411 	struct hnae3_ae_algo *ae_algo;  /* the class who provides this handle */
412 	u64 flags; /* Indicate the capabilities for this handle*/
413 
414 	union {
415 		struct net_device *netdev; /* first member */
416 		struct hnae3_knic_private_info kinfo;
417 		struct hnae3_unic_private_info uinfo;
418 		struct hnae3_roce_private_info rinfo;
419 	};
420 
421 	u32 numa_node_mask;	/* for multi-chip support */
422 };
423 
424 #define hnae_set_field(origin, mask, shift, val) \
425 	do { \
426 		(origin) &= (~(mask)); \
427 		(origin) |= ((val) << (shift)) & (mask); \
428 	} while (0)
429 #define hnae_get_field(origin, mask, shift) (((origin) & (mask)) >> (shift))
430 
431 #define hnae_set_bit(origin, shift, val) \
432 	hnae_set_field((origin), (0x1 << (shift)), (shift), (val))
433 #define hnae_get_bit(origin, shift) \
434 	hnae_get_field((origin), (0x1 << (shift)), (shift))
435 
436 int hnae3_register_ae_dev(struct hnae3_ae_dev *ae_dev);
437 void hnae3_unregister_ae_dev(struct hnae3_ae_dev *ae_dev);
438 
439 void hnae3_unregister_ae_algo(struct hnae3_ae_algo *ae_algo);
440 int hnae3_register_ae_algo(struct hnae3_ae_algo *ae_algo);
441 
442 void hnae3_unregister_client(struct hnae3_client *client);
443 int hnae3_register_client(struct hnae3_client *client);
444 #endif
445