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