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