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