2 * Copyright (c) 2016-2017 Hisilicon Limited.
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.
13 /* Names used in this framework:
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
19 * a tx or rx channel within a rbq
20 * ring description (desc):
21 * an element in the ring with packet information
23 * a memory region referred by desc with the full packet payload
25 * "num" means a static number set as a parameter, "count" mean a dynamic
26 * number set while running
27 * "cb" means control block
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>
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
49 #define HNAE3_CLASS_NAME_SIZE 16
51 #define HNAE3_DEV_INITED_B 0x0
52 #define HNAE3_DEV_SUPPORT_ROCE_B 0x1
53 #define HNAE3_DEV_SUPPORT_DCB_B 0x2
55 #define HNAE3_DEV_SUPPORT_ROCE_DCB_BITS (BIT(HNAE3_DEV_SUPPORT_DCB_B) |\
56 BIT(HNAE3_DEV_SUPPORT_ROCE_B))
58 #define hnae3_dev_roce_supported(hdev) \
59 hnae_get_bit(hdev->ae_dev->flag, HNAE3_DEV_SUPPORT_ROCE_B)
61 #define hnae3_dev_dcb_supported(hdev) \
62 hnae_get_bit(hdev->ae_dev->flag, HNAE3_DEV_SUPPORT_DCB_B)
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)
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 */
87 HNAE3_MAC_INTER_LOOP_MAC
,
88 HNAE3_MAC_INTER_LOOP_SERDES
,
89 HNAE3_MAC_INTER_LOOP_PHY
,
93 enum hnae3_client_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
,
112 struct hnae3_vector_info
{
117 #define HNAE3_RING_TYPE_B 0
118 #define HNAE3_RING_TYPE_TX 0
119 #define HNAE3_RING_TYPE_RX 1
121 struct hnae3_ring_chain_node
{
122 struct hnae3_ring_chain_node
*next
;
127 #define HNAE3_IS_TX_RING(node) \
128 (((node)->flag & (1 << HNAE3_RING_TYPE_B)) == HNAE3_RING_TYPE_TX)
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
);
136 #define HNAE3_CLIENT_NAME_LENGTH 16
137 struct hnae3_client
{
138 char name
[HNAE3_CLIENT_NAME_LENGTH
];
141 enum hnae3_client_type type
;
142 const struct hnae3_client_ops
*ops
;
143 struct list_head node
;
146 struct hnae3_ae_dev
{
147 struct pci_dev
*pdev
;
148 const struct hnae3_ae_ops
*ops
;
149 struct list_head node
;
151 enum hnae3_dev_type dev_type
;
155 /* This struct defines the operation on the handle.
157 * init_ae_dev(): (mandatory)
158 * Get PF configure from pci_dev and initialize PF hardware
160 * Disable PF device and release PF resource
162 * Register client to ae_dev
163 * unregister_client()
164 * Unregister client from ae_dev
166 * Enable the hardware
168 * Disable the hardware
170 * Get the carrier state of the back channel of the handle, 1 for ok, 0 for
172 * get_ksettings_an_result()
173 * Get negotiation status,speed and duplex
174 * update_speed_duplex_h()
175 * Update hardware speed and duplex
177 * Get media type of MAC
187 * get tx and rx of pause frame use
189 * set tx and rx of pause frame use
191 * set auto autonegotiation of pause frame use
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.
207 * Add unicast addr to mac table
209 * Remove unicast addr from mac table
211 * Set multicast address
213 * Add multicast address to mac table
215 * Remove multicast address from mac table
217 * Update Old network device statistics
218 * get_ethtool_stats()
219 * Get ethtool network device statistics
221 * Get a set of strings that describe the requested objects
223 * Get number of strings that @get_strings will write
224 * update_led_status()
225 * Update the led status
231 * Get the len of the regs dump
234 * get_rss_indir_size()
235 * Get rss indirection table size
241 * Get tc size of handle
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
249 * Add tunnel information to hardware
251 * Delete tunnel information from hardware
255 * Get firmware version
257 * Get media typr of phy
259 * Set vlan filter config of Ports
260 * set_vf_vlan_filter()
261 * Set vlan filter config of vf
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
);
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
);
277 int (*update_speed_duplex_h
)(struct hnae3_handle
*handle
);
278 int (*cfg_mac_speed_dup_h
)(struct hnae3_handle
*handle
, int speed
,
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
);
286 void (*set_promisc_mode
)(struct hnae3_handle
*handle
, u32 en
);
287 int (*set_mtu
)(struct hnae3_handle
*handle
, int new_mtu
);
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
);
294 int (*set_autoneg
)(struct hnae3_handle
*handle
, bool enable
);
295 int (*get_autoneg
)(struct hnae3_handle
*handle
);
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
);
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
);
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
);
327 void (*get_strings
)(struct hnae3_handle
*handle
,
328 u32 stringset
, u8
*data
);
329 int (*get_sset_count
)(struct hnae3_handle
*handle
, int stringset
);
331 void (*get_regs
)(struct hnae3_handle
*handle
, void *data
);
332 int (*get_regs_len
)(struct hnae3_handle
*handle
);
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
,
338 int (*set_rss
)(struct hnae3_handle
*handle
, const u32
*indir
,
339 const u8
*key
, const u8 hfunc
);
341 int (*get_tc_size
)(struct hnae3_handle
*handle
);
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
,
347 struct hnae3_ring_chain_node
*vr_chain
);
348 int (*unmap_ring_from_vector
)(struct hnae3_handle
*handle
,
350 struct hnae3_ring_chain_node
*vr_chain
);
352 int (*add_tunnel_udp
)(struct hnae3_handle
*handle
, u16 port_num
);
353 int (*del_tunnel_udp
)(struct hnae3_handle
*handle
, u16 port_num
);
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
);
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
);
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
;
373 #define HNAE3_INT_NAME_LEN (IFNAMSIZ + 16)
374 #define HNAE3_ITR_COUNTDOWN_START 100
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 */
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 */
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 */
395 u16 num_tqps
; /* total number of TQPs in this handle */
396 struct hnae3_queue
**tqp
; /* array base of all TQPs in this instance */
399 struct hnae3_roce_private_info
{
400 struct net_device
*netdev
;
401 void __iomem
*roce_io_base
;
406 struct hnae3_unic_private_info
{
407 struct net_device
*netdev
;
410 u16 num_tqps
; /* total number of tqps in this handle */
411 struct hnae3_queue
**tqp
; /* array base of all TQPs of this instance */
414 #define HNAE3_SUPPORT_MAC_LOOPBACK 1
415 #define HNAE3_SUPPORT_PHY_LOOPBACK 2
416 #define HNAE3_SUPPORT_SERDES_LOOPBACK 4
418 struct hnae3_handle
{
419 struct hnae3_client
*client
;
420 struct pci_dev
*pdev
;
422 struct hnae3_ae_algo
*ae_algo
; /* the class who provides this handle */
423 u64 flags
; /* Indicate the capabilities for this handle*/
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
;
432 u32 numa_node_mask
; /* for multi-chip support */
435 #define hnae_set_field(origin, mask, shift, val) \
437 (origin) &= (~(mask)); \
438 (origin) |= ((val) << (shift)) & (mask); \
440 #define hnae_get_field(origin, mask, shift) (((origin) & (mask)) >> (shift))
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))
447 int hnae3_register_ae_dev(struct hnae3_ae_dev
*ae_dev
);
448 void hnae3_unregister_ae_dev(struct hnae3_ae_dev
*ae_dev
);
450 void hnae3_unregister_ae_algo(struct hnae3_ae_algo
*ae_algo
);
451 int hnae3_register_ae_algo(struct hnae3_ae_algo
*ae_algo
);
453 void hnae3_unregister_client(struct hnae3_client
*client
);
454 int hnae3_register_client(struct hnae3_client
*client
);