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 HNAE_DEV_SUPPORT_ROCE_B 0x1
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)
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 */
77 HNAE3_MAC_INTER_LOOP_MAC,
78 HNAE3_MAC_INTER_LOOP_SERDES,
79 HNAE3_MAC_INTER_LOOP_PHY,
83 enum hnae3_client_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,
102 struct hnae3_vector_info {
107 #define HNAE3_RING_TYPE_B 0
108 #define HNAE3_RING_TYPE_TX 0
109 #define HNAE3_RING_TYPE_RX 1
111 struct hnae3_ring_chain_node {
112 struct hnae3_ring_chain_node *next;
117 #define HNAE3_IS_TX_RING(node) \
118 (((node)->flag & (1 << HNAE3_RING_TYPE_B)) == HNAE3_RING_TYPE_TX)
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);
126 #define HNAE3_CLIENT_NAME_LENGTH 16
127 struct hnae3_client {
128 char name[HNAE3_CLIENT_NAME_LENGTH];
131 enum hnae3_client_type type;
132 const struct hnae3_client_ops *ops;
133 struct list_head node;
136 struct hnae3_ae_dev {
137 struct pci_dev *pdev;
138 const struct hnae3_ae_ops *ops;
139 struct list_head node;
141 enum hnae3_dev_type dev_type;
145 /* This struct defines the operation on the handle.
147 * init_ae_dev(): (mandatory)
148 * Get PF configure from pci_dev and initialize PF hardware
150 * Disable PF device and release PF resource
152 * Register client to ae_dev
153 * unregister_client()
154 * Unregister client from ae_dev
156 * Enable the hardware
158 * Disable the hardware
160 * Get the carrier state of the back channel of the handle, 1 for ok, 0 for
162 * get_ksettings_an_result()
163 * Get negotiation status,speed and duplex
164 * update_speed_duplex_h()
165 * Update hardware speed and duplex
167 * Get media type of MAC
177 * get tx and rx of pause frame use
179 * set tx and rx of pause frame use
181 * set auto autonegotiation of pause frame use
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.
197 * Add unicast addr to mac table
199 * Remove unicast addr from mac table
201 * Set multicast address
203 * Add multicast address to mac table
205 * Remove multicast address from mac table
207 * Update Old network device statistics
208 * get_ethtool_stats()
209 * Get ethtool network device statistics
211 * Get a set of strings that describe the requested objects
213 * Get number of strings that @get_strings will write
214 * update_led_status()
215 * Update the led status
221 * Get the len of the regs dump
224 * get_rss_indir_size()
225 * Get rss indirection table size
231 * Get tc size of handle
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
239 * Add tunnel information to hardware
241 * Delete tunnel information from hardware
245 * Get firmware version
247 * Get media typr of phy
249 * Set vlan filter config of Ports
250 * set_vf_vlan_filter()
251 * Set vlan filter config of vf
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);
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);
267 int (*update_speed_duplex_h)(struct hnae3_handle *handle);
268 int (*cfg_mac_speed_dup_h)(struct hnae3_handle *handle, int speed,
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);
276 void (*set_promisc_mode)(struct hnae3_handle *handle, u32 en);
277 int (*set_mtu)(struct hnae3_handle *handle, int new_mtu);
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);
284 int (*set_autoneg)(struct hnae3_handle *handle, bool enable);
285 int (*get_autoneg)(struct hnae3_handle *handle);
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);
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);
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);
317 void (*get_strings)(struct hnae3_handle *handle,
318 u32 stringset, u8 *data);
319 int (*get_sset_count)(struct hnae3_handle *handle, int stringset);
321 void (*get_regs)(struct hnae3_handle *handle, void *data);
322 int (*get_regs_len)(struct hnae3_handle *handle);
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,
328 int (*set_rss)(struct hnae3_handle *handle, const u32 *indir,
329 const u8 *key, const u8 hfunc);
331 int (*get_tc_size)(struct hnae3_handle *handle);
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,
337 struct hnae3_ring_chain_node *vr_chain);
338 int (*unmap_ring_from_vector)(struct hnae3_handle *handle,
340 struct hnae3_ring_chain_node *vr_chain);
342 int (*add_tunnel_udp)(struct hnae3_handle *handle, u16 port_num);
343 int (*del_tunnel_udp)(struct hnae3_handle *handle, u16 port_num);
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);
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);
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;
363 #define HNAE3_INT_NAME_LEN (IFNAMSIZ + 16)
364 #define HNAE3_ITR_COUNTDOWN_START 100
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 */
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 */
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 */
384 u16 num_tqps; /* total number of TQPs in this handle */
385 struct hnae3_queue **tqp; /* array base of all TQPs in this instance */
388 struct hnae3_roce_private_info {
389 struct net_device *netdev;
390 void __iomem *roce_io_base;
395 struct hnae3_unic_private_info {
396 struct net_device *netdev;
399 u16 num_tqps; /* total number of tqps in this handle */
400 struct hnae3_queue **tqp; /* array base of all TQPs of this instance */
403 #define HNAE3_SUPPORT_MAC_LOOPBACK 1
404 #define HNAE3_SUPPORT_PHY_LOOPBACK 2
405 #define HNAE3_SUPPORT_SERDES_LOOPBACK 4
407 struct hnae3_handle {
408 struct hnae3_client *client;
409 struct pci_dev *pdev;
411 struct hnae3_ae_algo *ae_algo; /* the class who provides this handle */
412 u64 flags; /* Indicate the capabilities for this handle*/
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;
421 u32 numa_node_mask; /* for multi-chip support */
424 #define hnae_set_field(origin, mask, shift, val) \
426 (origin) &= (~(mask)); \
427 (origin) |= ((val) << (shift)) & (mask); \
429 #define hnae_get_field(origin, mask, shift) (((origin) & (mask)) >> (shift))
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))
436 int hnae3_register_ae_dev(struct hnae3_ae_dev *ae_dev);
437 void hnae3_unregister_ae_dev(struct hnae3_ae_dev *ae_dev);
439 void hnae3_unregister_ae_algo(struct hnae3_ae_algo *ae_algo);
440 int hnae3_register_ae_algo(struct hnae3_ae_algo *ae_algo);
442 void hnae3_unregister_client(struct hnae3_client *client);
443 int hnae3_register_client(struct hnae3_client *client);