1
2
3
4
5
6
7
8
9
10#ifndef __HNAE_H
11#define __HNAE_H
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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/notifier.h>
36#include <linux/phy.h>
37#include <linux/types.h>
38
39#define HNAE_DRIVER_VERSION "2.0"
40#define HNAE_DRIVER_NAME "hns"
41#define HNAE_COPYRIGHT "Copyright(c) 2015 Huawei Corporation."
42#define HNAE_DRIVER_STRING "Hisilicon Network Subsystem Driver"
43#define HNAE_DEFAULT_DEVICE_DESCR "Hisilicon Network Subsystem"
44
45#ifdef DEBUG
46
47#ifndef assert
48#define assert(expr) \
49do { \
50 if (!(expr)) { \
51 pr_err("Assertion failed! %s, %s, %s, line %d\n", \
52 #expr, __FILE__, __func__, __LINE__); \
53 } \
54} while (0)
55#endif
56
57#else
58
59#ifndef assert
60#define assert(expr)
61#endif
62
63#endif
64
65#define AE_VERSION_1 ('6' << 16 | '6' << 8 | '0')
66#define AE_VERSION_2 ('1' << 24 | '6' << 16 | '1' << 8 | '0')
67#define AE_IS_VER1(ver) ((ver) == AE_VERSION_1)
68#define AE_NAME_SIZE 16
69
70
71
72
73#define RCB_REG_BASEADDR_L 0x00
74#define RCB_REG_BASEADDR_H 0x04
75#define RCB_REG_BD_NUM 0x08
76#define RCB_REG_BD_LEN 0x0C
77#define RCB_REG_PKTLINE 0x10
78#define RCB_REG_TAIL 0x18
79#define RCB_REG_HEAD 0x1C
80#define RCB_REG_FBDNUM 0x20
81#define RCB_REG_OFFSET 0x24
82#define RCB_REG_PKTNUM_RECORD 0x2C
83
84#define HNS_RX_HEAD_SIZE 256
85
86#define HNAE_AE_REGISTER 0x1
87
88#define RCB_RING_NAME_LEN 16
89
90enum hnae_led_state {
91 HNAE_LED_INACTIVE,
92 HNAE_LED_ACTIVE,
93 HNAE_LED_ON,
94 HNAE_LED_OFF
95};
96
97#define HNS_RX_FLAG_VLAN_PRESENT 0x1
98#define HNS_RX_FLAG_L3ID_IPV4 0x0
99#define HNS_RX_FLAG_L3ID_IPV6 0x1
100#define HNS_RX_FLAG_L4ID_UDP 0x0
101#define HNS_RX_FLAG_L4ID_TCP 0x1
102
103#define HNS_TXD_ASID_S 0
104#define HNS_TXD_ASID_M (0xff << HNS_TXD_ASID_S)
105#define HNS_TXD_BUFNUM_S 8
106#define HNS_TXD_BUFNUM_M (0x3 << HNS_TXD_BUFNUM_S)
107#define HNS_TXD_PORTID_S 10
108#define HNS_TXD_PORTID_M (0x7 << HNS_TXD_PORTID_S)
109
110#define HNS_TXD_RA_B 8
111#define HNS_TXD_RI_B 9
112#define HNS_TXD_L4CS_B 10
113#define HNS_TXD_L3CS_B 11
114#define HNS_TXD_FE_B 12
115#define HNS_TXD_VLD_B 13
116#define HNS_TXD_IPOFFSET_S 14
117#define HNS_TXD_IPOFFSET_M (0xff << HNS_TXD_IPOFFSET_S)
118
119#define HNS_RXD_IPOFFSET_S 0
120#define HNS_RXD_IPOFFSET_M (0xff << HNS_TXD_IPOFFSET_S)
121#define HNS_RXD_BUFNUM_S 8
122#define HNS_RXD_BUFNUM_M (0x3 << HNS_RXD_BUFNUM_S)
123#define HNS_RXD_PORTID_S 10
124#define HNS_RXD_PORTID_M (0x7 << HNS_RXD_PORTID_S)
125#define HNS_RXD_DMAC_S 13
126#define HNS_RXD_DMAC_M (0x3 << HNS_RXD_DMAC_S)
127#define HNS_RXD_VLAN_S 15
128#define HNS_RXD_VLAN_M (0x3 << HNS_RXD_VLAN_S)
129#define HNS_RXD_L3ID_S 17
130#define HNS_RXD_L3ID_M (0xf << HNS_RXD_L3ID_S)
131#define HNS_RXD_L4ID_S 21
132#define HNS_RXD_L4ID_M (0xf << HNS_RXD_L4ID_S)
133#define HNS_RXD_FE_B 25
134#define HNS_RXD_FRAG_B 26
135#define HNS_RXD_VLD_B 27
136#define HNS_RXD_L2E_B 28
137#define HNS_RXD_L3E_B 29
138#define HNS_RXD_L4E_B 30
139#define HNS_RXD_DROP_B 31
140
141#define HNS_RXD_VLANID_S 8
142#define HNS_RXD_VLANID_M (0xfff << HNS_RXD_VLANID_S)
143#define HNS_RXD_CFI_B 20
144#define HNS_RXD_PRI_S 21
145#define HNS_RXD_PRI_M (0x7 << HNS_RXD_PRI_S)
146#define HNS_RXD_ASID_S 24
147#define HNS_RXD_ASID_M (0xff << HNS_RXD_ASID_S)
148
149#define HNSV2_TXD_BUFNUM_S 0
150#define HNSV2_TXD_BUFNUM_M (0x7 << HNSV2_TXD_BUFNUM_S)
151#define HNSV2_TXD_PORTID_S 4
152#define HNSV2_TXD_PORTID_M (0X7 << HNSV2_TXD_PORTID_S)
153#define HNSV2_TXD_RI_B 1
154#define HNSV2_TXD_L4CS_B 2
155#define HNSV2_TXD_L3CS_B 3
156#define HNSV2_TXD_FE_B 4
157#define HNSV2_TXD_VLD_B 5
158
159#define HNSV2_TXD_TSE_B 0
160#define HNSV2_TXD_VLAN_EN_B 1
161#define HNSV2_TXD_SNAP_B 2
162#define HNSV2_TXD_IPV6_B 3
163#define HNSV2_TXD_SCTP_B 4
164
165
166struct __packed hnae_desc {
167 __le64 addr;
168 union {
169 struct {
170 union {
171 __le16 asid_bufnum_pid;
172 __le16 asid;
173 };
174 __le16 send_size;
175 union {
176 __le32 flag_ipoffset;
177 struct {
178 __u8 bn_pid;
179 __u8 ra_ri_cs_fe_vld;
180 __u8 ip_offset;
181 __u8 tse_vlan_snap_v6_sctp_nth;
182 };
183 };
184 __le16 mss;
185 __u8 l4_len;
186 __u8 reserved1;
187 __le16 paylen;
188 __u8 vmid;
189 __u8 qid;
190 __le32 reserved2[2];
191 } tx;
192
193 struct {
194 __le32 ipoff_bnum_pid_flag;
195 __le16 pkt_len;
196 __le16 size;
197 union {
198 __le32 vlan_pri_asid;
199 struct {
200 __le16 asid;
201 __le16 vlan_cfi_pri;
202 };
203 };
204 __le32 rss_hash;
205 __le32 reserved_1[2];
206 } rx;
207 };
208};
209
210struct hnae_desc_cb {
211 dma_addr_t dma;
212 void *buf;
213
214
215 void *priv;
216 u16 page_offset;
217 u16 reuse_flag;
218
219 u16 length;
220
221
222 u16 type;
223};
224
225#define setflags(flags, bits) ((flags) |= (bits))
226#define unsetflags(flags, bits) ((flags) &= ~(bits))
227
228
229#define RINGF_DIR 0x1
230#define is_tx_ring(ring) ((ring)->flags & RINGF_DIR)
231#define is_rx_ring(ring) (!is_tx_ring(ring))
232#define ring_to_dma_dir(ring) (is_tx_ring(ring) ? \
233 DMA_TO_DEVICE : DMA_FROM_DEVICE)
234
235struct ring_stats {
236 u64 io_err_cnt;
237 u64 sw_err_cnt;
238 u64 seg_pkt_cnt;
239 union {
240 struct {
241 u64 tx_pkts;
242 u64 tx_bytes;
243 u64 tx_err_cnt;
244 u64 restart_queue;
245 u64 tx_busy;
246 };
247 struct {
248 u64 rx_pkts;
249 u64 rx_bytes;
250 u64 rx_err_cnt;
251 u64 reuse_pg_cnt;
252 u64 err_pkt_len;
253 u64 non_vld_descs;
254 u64 err_bd_num;
255 u64 l2_err;
256 u64 l3l4_csum_err;
257 };
258 };
259};
260
261struct hnae_queue;
262
263struct hnae_ring {
264 u8 __iomem *io_base;
265 struct hnae_desc *desc;
266 struct hnae_desc_cb *desc_cb;
267 struct hnae_queue *q;
268 int irq;
269 char ring_name[RCB_RING_NAME_LEN];
270
271
272 struct ring_stats stats;
273
274 dma_addr_t desc_dma_addr;
275 u32 buf_size;
276 u16 desc_num;
277 u16 max_desc_num_per_pkt;
278 u16 max_raw_data_sz_per_desc;
279 u16 max_pkt_size;
280 int next_to_use;
281
282
283
284
285 int next_to_clean;
286
287 int flags;
288 int irq_init_flag;
289};
290
291#define ring_ptr_move_fw(ring, p) \
292 ((ring)->p = ((ring)->p + 1) % (ring)->desc_num)
293#define ring_ptr_move_bw(ring, p) \
294 ((ring)->p = ((ring)->p - 1 + (ring)->desc_num) % (ring)->desc_num)
295
296enum hns_desc_type {
297 DESC_TYPE_SKB,
298 DESC_TYPE_PAGE,
299};
300
301#define assert_is_ring_idx(ring, idx) \
302 assert((idx) >= 0 && (idx) < (ring)->desc_num)
303
304
305
306
307static inline int ring_dist(struct hnae_ring *ring, int begin, int end)
308{
309 assert_is_ring_idx(ring, begin);
310 assert_is_ring_idx(ring, end);
311
312 return (end - begin + ring->desc_num) % ring->desc_num;
313}
314
315static inline int ring_space(struct hnae_ring *ring)
316{
317 return ring->desc_num -
318 ring_dist(ring, ring->next_to_clean, ring->next_to_use) - 1;
319}
320
321static inline int is_ring_empty(struct hnae_ring *ring)
322{
323 assert_is_ring_idx(ring, ring->next_to_use);
324 assert_is_ring_idx(ring, ring->next_to_clean);
325
326 return ring->next_to_use == ring->next_to_clean;
327}
328
329#define hnae_buf_size(_ring) ((_ring)->buf_size)
330#define hnae_page_order(_ring) (get_order(hnae_buf_size(_ring)))
331#define hnae_page_size(_ring) (PAGE_SIZE << hnae_page_order(_ring))
332
333struct hnae_handle;
334
335
336struct hnae_buf_ops {
337 int (*alloc_buffer)(struct hnae_ring *ring, struct hnae_desc_cb *cb);
338 void (*free_buffer)(struct hnae_ring *ring, struct hnae_desc_cb *cb);
339 int (*map_buffer)(struct hnae_ring *ring, struct hnae_desc_cb *cb);
340 void (*unmap_buffer)(struct hnae_ring *ring, struct hnae_desc_cb *cb);
341};
342
343struct hnae_queue {
344 void __iomem *io_base;
345 phys_addr_t phy_base;
346 struct hnae_ae_dev *dev;
347 struct hnae_ring rx_ring ____cacheline_internodealigned_in_smp;
348 struct hnae_ring tx_ring ____cacheline_internodealigned_in_smp;
349 struct hnae_handle *handle;
350};
351
352
353enum hnae_loop {
354 MAC_INTERNALLOOP_MAC = 0,
355 MAC_INTERNALLOOP_SERDES,
356 MAC_INTERNALLOOP_PHY,
357 MAC_LOOP_NONE,
358};
359
360
361enum hnae_port_type {
362 HNAE_PORT_SERVICE = 0,
363 HNAE_PORT_DEBUG
364};
365
366
367enum hnae_media_type {
368 HNAE_MEDIA_TYPE_UNKNOWN = 0,
369 HNAE_MEDIA_TYPE_FIBER,
370 HNAE_MEDIA_TYPE_COPPER,
371 HNAE_MEDIA_TYPE_BACKPLANE,
372};
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450struct hnae_ae_ops {
451 struct hnae_handle *(*get_handle)(struct hnae_ae_dev *dev,
452 u32 port_id);
453 void (*put_handle)(struct hnae_handle *handle);
454 void (*init_queue)(struct hnae_queue *q);
455 void (*fini_queue)(struct hnae_queue *q);
456 int (*start)(struct hnae_handle *handle);
457 void (*stop)(struct hnae_handle *handle);
458 void (*reset)(struct hnae_handle *handle);
459 int (*set_opts)(struct hnae_handle *handle, int type, void *opts);
460 int (*get_opts)(struct hnae_handle *handle, int type, void **opts);
461 int (*get_status)(struct hnae_handle *handle);
462 int (*get_info)(struct hnae_handle *handle,
463 u8 *auto_neg, u16 *speed, u8 *duplex);
464 void (*toggle_ring_irq)(struct hnae_ring *ring, u32 val);
465 void (*adjust_link)(struct hnae_handle *handle, int speed, int duplex);
466 int (*set_loopback)(struct hnae_handle *handle,
467 enum hnae_loop loop_mode, int en);
468 void (*get_ring_bdnum_limit)(struct hnae_queue *queue,
469 u32 *uplimit);
470 void (*get_pauseparam)(struct hnae_handle *handle,
471 u32 *auto_neg, u32 *rx_en, u32 *tx_en);
472 int (*set_autoneg)(struct hnae_handle *handle, u8 enable);
473 int (*get_autoneg)(struct hnae_handle *handle);
474 int (*set_pauseparam)(struct hnae_handle *handle,
475 u32 auto_neg, u32 rx_en, u32 tx_en);
476 void (*get_coalesce_usecs)(struct hnae_handle *handle,
477 u32 *tx_usecs, u32 *rx_usecs);
478 void (*get_rx_max_coalesced_frames)(struct hnae_handle *handle,
479 u32 *tx_frames, u32 *rx_frames);
480 int (*set_coalesce_usecs)(struct hnae_handle *handle, u32 timeout);
481 int (*set_coalesce_frames)(struct hnae_handle *handle,
482 u32 coalesce_frames);
483 void (*get_coalesce_range)(struct hnae_handle *handle,
484 u32 *tx_frames_low, u32 *rx_frames_low,
485 u32 *tx_frames_high, u32 *rx_frames_high,
486 u32 *tx_usecs_low, u32 *rx_usecs_low,
487 u32 *tx_usecs_high, u32 *rx_usecs_high);
488 void (*set_promisc_mode)(struct hnae_handle *handle, u32 en);
489 int (*get_mac_addr)(struct hnae_handle *handle, void **p);
490 int (*set_mac_addr)(struct hnae_handle *handle, void *p);
491 int (*set_mc_addr)(struct hnae_handle *handle, void *addr);
492 int (*set_mtu)(struct hnae_handle *handle, int new_mtu);
493 void (*set_tso_stats)(struct hnae_handle *handle, int enable);
494 void (*update_stats)(struct hnae_handle *handle,
495 struct net_device_stats *net_stats);
496 void (*get_stats)(struct hnae_handle *handle, u64 *data);
497 void (*get_strings)(struct hnae_handle *handle,
498 u32 stringset, u8 *data);
499 int (*get_sset_count)(struct hnae_handle *handle, int stringset);
500 void (*update_led_status)(struct hnae_handle *handle);
501 int (*set_led_id)(struct hnae_handle *handle,
502 enum hnae_led_state status);
503 void (*get_regs)(struct hnae_handle *handle, void *data);
504 int (*get_regs_len)(struct hnae_handle *handle);
505 u32 (*get_rss_key_size)(struct hnae_handle *handle);
506 u32 (*get_rss_indir_size)(struct hnae_handle *handle);
507 int (*get_rss)(struct hnae_handle *handle, u32 *indir, u8 *key,
508 u8 *hfunc);
509 int (*set_rss)(struct hnae_handle *handle, const u32 *indir,
510 const u8 *key, const u8 hfunc);
511};
512
513struct hnae_ae_dev {
514 struct device cls_dev;
515 struct device *dev;
516 struct hnae_ae_ops *ops;
517 struct list_head node;
518 struct module *owner;
519 int id;
520 char name[AE_NAME_SIZE];
521 struct list_head handle_list;
522 spinlock_t lock;
523};
524
525struct hnae_handle {
526 struct device *owner_dev;
527 struct hnae_ae_dev *dev;
528 struct phy_device *phy_dev;
529 phy_interface_t phy_if;
530 u32 if_support;
531 int q_num;
532 int vf_id;
533 u32 eport_id;
534 u32 dport_id;
535 enum hnae_port_type port_type;
536 enum hnae_media_type media_type;
537 struct list_head node;
538 struct hnae_buf_ops *bops;
539 struct hnae_queue **qs;
540};
541
542#define ring_to_dev(ring) ((ring)->q->dev->dev)
543
544struct hnae_handle *hnae_get_handle(struct device *owner_dev,
545 const struct fwnode_handle *fwnode,
546 u32 port_id,
547 struct hnae_buf_ops *bops);
548
549void hnae_put_handle(struct hnae_handle *handle);
550int hnae_ae_register(struct hnae_ae_dev *dev, struct module *owner);
551void hnae_ae_unregister(struct hnae_ae_dev *dev);
552
553int hnae_register_notifier(struct notifier_block *nb);
554void hnae_unregister_notifier(struct notifier_block *nb);
555int hnae_reinit_handle(struct hnae_handle *handle);
556
557#define hnae_queue_xmit(q, buf_num) writel_relaxed(buf_num, \
558 (q)->tx_ring.io_base + RCB_REG_TAIL)
559
560#ifndef assert
561#define assert(cond)
562#endif
563
564static inline int hnae_reserve_buffer_map(struct hnae_ring *ring,
565 struct hnae_desc_cb *cb)
566{
567 struct hnae_buf_ops *bops = ring->q->handle->bops;
568 int ret;
569
570 ret = bops->alloc_buffer(ring, cb);
571 if (ret)
572 goto out;
573
574 ret = bops->map_buffer(ring, cb);
575 if (ret)
576 goto out_with_buf;
577
578 return 0;
579
580out_with_buf:
581 bops->free_buffer(ring, cb);
582out:
583 return ret;
584}
585
586static inline int hnae_alloc_buffer_attach(struct hnae_ring *ring, int i)
587{
588 int ret = hnae_reserve_buffer_map(ring, &ring->desc_cb[i]);
589
590 if (ret)
591 return ret;
592
593 ring->desc[i].addr = (__le64)ring->desc_cb[i].dma;
594
595 return 0;
596}
597
598static inline void hnae_buffer_detach(struct hnae_ring *ring, int i)
599{
600 ring->q->handle->bops->unmap_buffer(ring, &ring->desc_cb[i]);
601 ring->desc[i].addr = 0;
602}
603
604static inline void hnae_free_buffer_detach(struct hnae_ring *ring, int i)
605{
606 struct hnae_buf_ops *bops = ring->q->handle->bops;
607 struct hnae_desc_cb *cb = &ring->desc_cb[i];
608
609 if (!ring->desc_cb[i].dma)
610 return;
611
612 hnae_buffer_detach(ring, i);
613 bops->free_buffer(ring, cb);
614}
615
616
617static inline void hnae_replace_buffer(struct hnae_ring *ring, int i,
618 struct hnae_desc_cb *res_cb)
619{
620 struct hnae_buf_ops *bops = ring->q->handle->bops;
621
622 bops->unmap_buffer(ring, &ring->desc_cb[i]);
623 ring->desc_cb[i] = *res_cb;
624 ring->desc[i].addr = (__le64)ring->desc_cb[i].dma;
625 ring->desc[i].rx.ipoff_bnum_pid_flag = 0;
626}
627
628static inline void hnae_reuse_buffer(struct hnae_ring *ring, int i)
629{
630 ring->desc_cb[i].reuse_flag = 0;
631 ring->desc[i].addr = (__le64)(ring->desc_cb[i].dma
632 + ring->desc_cb[i].page_offset);
633 ring->desc[i].rx.ipoff_bnum_pid_flag = 0;
634}
635
636#define hnae_set_field(origin, mask, shift, val) \
637 do { \
638 (origin) &= (~(mask)); \
639 (origin) |= ((val) << (shift)) & (mask); \
640 } while (0)
641
642#define hnae_set_bit(origin, shift, val) \
643 hnae_set_field((origin), (0x1 << (shift)), (shift), (val))
644
645#define hnae_get_field(origin, mask, shift) (((origin) & (mask)) >> (shift))
646
647#define hnae_get_bit(origin, shift) \
648 hnae_get_field((origin), (0x1 << (shift)), (shift))
649
650#endif
651