]> bbs.cooldavid.org Git - net-next-2.6.git/blob - drivers/net/mlx4/mlx4_en.h
Merge branch 'master' of master.kernel.org:/pub/scm/linux/kernel/git/davem/net-2.6
[net-next-2.6.git] / drivers / net / mlx4 / mlx4_en.h
1 /*
2  * Copyright (c) 2007 Mellanox Technologies. All rights reserved.
3  *
4  * This software is available to you under a choice of one of two
5  * licenses.  You may choose to be licensed under the terms of the GNU
6  * General Public License (GPL) Version 2, available from the file
7  * COPYING in the main directory of this source tree, or the
8  * OpenIB.org BSD license below:
9  *
10  *     Redistribution and use in source and binary forms, with or
11  *     without modification, are permitted provided that the following
12  *     conditions are met:
13  *
14  *      - Redistributions of source code must retain the above
15  *        copyright notice, this list of conditions and the following
16  *        disclaimer.
17  *
18  *      - Redistributions in binary form must reproduce the above
19  *        copyright notice, this list of conditions and the following
20  *        disclaimer in the documentation and/or other materials
21  *        provided with the distribution.
22  *
23  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30  * SOFTWARE.
31  *
32  */
33
34 #ifndef _MLX4_EN_H_
35 #define _MLX4_EN_H_
36
37 #include <linux/compiler.h>
38 #include <linux/list.h>
39 #include <linux/mutex.h>
40 #include <linux/netdevice.h>
41 #include <linux/inet_lro.h>
42
43 #include <linux/mlx4/device.h>
44 #include <linux/mlx4/qp.h>
45 #include <linux/mlx4/cq.h>
46 #include <linux/mlx4/srq.h>
47 #include <linux/mlx4/doorbell.h>
48
49 #include "en_port.h"
50
51 #define DRV_NAME        "mlx4_en"
52 #define DRV_VERSION     "1.4.1.1"
53 #define DRV_RELDATE     "June 2009"
54
55 #define MLX4_EN_MSG_LEVEL       (NETIF_MSG_LINK | NETIF_MSG_IFDOWN)
56
57 /*
58  * Device constants
59  */
60
61
62 #define MLX4_EN_PAGE_SHIFT      12
63 #define MLX4_EN_PAGE_SIZE       (1 << MLX4_EN_PAGE_SHIFT)
64 #define MAX_TX_RINGS            16
65 #define MAX_RX_RINGS            16
66 #define TXBB_SIZE               64
67 #define HEADROOM                (2048 / TXBB_SIZE + 1)
68 #define STAMP_STRIDE            64
69 #define STAMP_DWORDS            (STAMP_STRIDE / 4)
70 #define STAMP_SHIFT             31
71 #define STAMP_VAL               0x7fffffff
72 #define STATS_DELAY             (HZ / 4)
73
74 /* Typical TSO descriptor with 16 gather entries is 352 bytes... */
75 #define MAX_DESC_SIZE           512
76 #define MAX_DESC_TXBBS          (MAX_DESC_SIZE / TXBB_SIZE)
77
78 /*
79  * OS related constants and tunables
80  */
81
82 #define MLX4_EN_WATCHDOG_TIMEOUT        (15 * HZ)
83
84 #define MLX4_EN_ALLOC_ORDER     2
85 #define MLX4_EN_ALLOC_SIZE      (PAGE_SIZE << MLX4_EN_ALLOC_ORDER)
86
87 #define MLX4_EN_MAX_LRO_DESCRIPTORS     32
88
89 /* Receive fragment sizes; we use at most 4 fragments (for 9600 byte MTU
90  * and 4K allocations) */
91 enum {
92         FRAG_SZ0 = 512 - NET_IP_ALIGN,
93         FRAG_SZ1 = 1024,
94         FRAG_SZ2 = 4096,
95         FRAG_SZ3 = MLX4_EN_ALLOC_SIZE
96 };
97 #define MLX4_EN_MAX_RX_FRAGS    4
98
99 /* Maximum ring sizes */
100 #define MLX4_EN_MAX_TX_SIZE     8192
101 #define MLX4_EN_MAX_RX_SIZE     8192
102
103 /* Minimum ring size for our page-allocation sceme to work */
104 #define MLX4_EN_MIN_RX_SIZE     (MLX4_EN_ALLOC_SIZE / SMP_CACHE_BYTES)
105 #define MLX4_EN_MIN_TX_SIZE     (4096 / TXBB_SIZE)
106
107 #define MLX4_EN_SMALL_PKT_SIZE          64
108 #define MLX4_EN_NUM_TX_RINGS            8
109 #define MLX4_EN_NUM_PPP_RINGS           8
110 #define MLX4_EN_DEF_TX_RING_SIZE        512
111 #define MLX4_EN_DEF_RX_RING_SIZE        1024
112
113 /* Target number of packets to coalesce with interrupt moderation */
114 #define MLX4_EN_RX_COAL_TARGET  44
115 #define MLX4_EN_RX_COAL_TIME    0x10
116
117 #define MLX4_EN_TX_COAL_PKTS    5
118 #define MLX4_EN_TX_COAL_TIME    0x80
119
120 #define MLX4_EN_RX_RATE_LOW             400000
121 #define MLX4_EN_RX_COAL_TIME_LOW        0
122 #define MLX4_EN_RX_RATE_HIGH            450000
123 #define MLX4_EN_RX_COAL_TIME_HIGH       128
124 #define MLX4_EN_RX_SIZE_THRESH          1024
125 #define MLX4_EN_RX_RATE_THRESH          (1000000 / MLX4_EN_RX_COAL_TIME_HIGH)
126 #define MLX4_EN_SAMPLE_INTERVAL         0
127
128 #define MLX4_EN_AUTO_CONF       0xffff
129
130 #define MLX4_EN_DEF_RX_PAUSE    1
131 #define MLX4_EN_DEF_TX_PAUSE    1
132
133 /* Interval between successive polls in the Tx routine when polling is used
134    instead of interrupts (in per-core Tx rings) - should be power of 2 */
135 #define MLX4_EN_TX_POLL_MODER   16
136 #define MLX4_EN_TX_POLL_TIMEOUT (HZ / 4)
137
138 #define ETH_LLC_SNAP_SIZE       8
139
140 #define SMALL_PACKET_SIZE      (256 - NET_IP_ALIGN)
141 #define HEADER_COPY_SIZE       (128 - NET_IP_ALIGN)
142
143 #define MLX4_EN_MIN_MTU         46
144 #define ETH_BCAST               0xffffffffffffULL
145
146 #ifdef MLX4_EN_PERF_STAT
147 /* Number of samples to 'average' */
148 #define AVG_SIZE                        128
149 #define AVG_FACTOR                      1024
150 #define NUM_PERF_STATS                  NUM_PERF_COUNTERS
151
152 #define INC_PERF_COUNTER(cnt)           (++(cnt))
153 #define ADD_PERF_COUNTER(cnt, add)      ((cnt) += (add))
154 #define AVG_PERF_COUNTER(cnt, sample) \
155         ((cnt) = ((cnt) * (AVG_SIZE - 1) + (sample) * AVG_FACTOR) / AVG_SIZE)
156 #define GET_PERF_COUNTER(cnt)           (cnt)
157 #define GET_AVG_PERF_COUNTER(cnt)       ((cnt) / AVG_FACTOR)
158
159 #else
160
161 #define NUM_PERF_STATS                  0
162 #define INC_PERF_COUNTER(cnt)           do {} while (0)
163 #define ADD_PERF_COUNTER(cnt, add)      do {} while (0)
164 #define AVG_PERF_COUNTER(cnt, sample)   do {} while (0)
165 #define GET_PERF_COUNTER(cnt)           (0)
166 #define GET_AVG_PERF_COUNTER(cnt)       (0)
167 #endif /* MLX4_EN_PERF_STAT */
168
169 /*
170  * Configurables
171  */
172
173 enum cq_type {
174         RX = 0,
175         TX = 1,
176 };
177
178
179 /*
180  * Useful macros
181  */
182 #define ROUNDUP_LOG2(x)         ilog2(roundup_pow_of_two(x))
183 #define XNOR(x, y)              (!(x) == !(y))
184 #define ILLEGAL_MAC(addr)       (addr == 0xffffffffffffULL || addr == 0x0)
185
186
187 struct mlx4_en_tx_info {
188         struct sk_buff *skb;
189         u32 nr_txbb;
190         u8 linear;
191         u8 data_offset;
192         u8 inl;
193 };
194
195
196 #define MLX4_EN_BIT_DESC_OWN    0x80000000
197 #define CTRL_SIZE       sizeof(struct mlx4_wqe_ctrl_seg)
198 #define MLX4_EN_MEMTYPE_PAD     0x100
199 #define DS_SIZE         sizeof(struct mlx4_wqe_data_seg)
200
201
202 struct mlx4_en_tx_desc {
203         struct mlx4_wqe_ctrl_seg ctrl;
204         union {
205                 struct mlx4_wqe_data_seg data; /* at least one data segment */
206                 struct mlx4_wqe_lso_seg lso;
207                 struct mlx4_wqe_inline_seg inl;
208         };
209 };
210
211 #define MLX4_EN_USE_SRQ         0x01000000
212
213 struct mlx4_en_rx_alloc {
214         struct page *page;
215         u16 offset;
216 };
217
218 struct mlx4_en_tx_ring {
219         struct mlx4_hwq_resources wqres;
220         u32 size ; /* number of TXBBs */
221         u32 size_mask;
222         u16 stride;
223         u16 cqn;        /* index of port CQ associated with this ring */
224         u32 prod;
225         u32 cons;
226         u32 buf_size;
227         u32 doorbell_qpn;
228         void *buf;
229         u16 poll_cnt;
230         int blocked;
231         struct mlx4_en_tx_info *tx_info;
232         u8 *bounce_buf;
233         u32 last_nr_txbb;
234         struct mlx4_qp qp;
235         struct mlx4_qp_context context;
236         int qpn;
237         enum mlx4_qp_state qp_state;
238         struct mlx4_srq dummy;
239         unsigned long bytes;
240         unsigned long packets;
241         spinlock_t comp_lock;
242 };
243
244 struct mlx4_en_rx_desc {
245         /* actual number of entries depends on rx ring stride */
246         struct mlx4_wqe_data_seg data[0];
247 };
248
249 struct mlx4_en_rx_ring {
250         struct mlx4_hwq_resources wqres;
251         struct mlx4_en_rx_alloc page_alloc[MLX4_EN_MAX_RX_FRAGS];
252         struct net_lro_mgr lro;
253         u32 size ;      /* number of Rx descs*/
254         u32 actual_size;
255         u32 size_mask;
256         u16 stride;
257         u16 log_stride;
258         u16 cqn;        /* index of port CQ associated with this ring */
259         u32 prod;
260         u32 cons;
261         u32 buf_size;
262         void *buf;
263         void *rx_info;
264         unsigned long bytes;
265         unsigned long packets;
266 };
267
268
269 static inline int mlx4_en_can_lro(__be16 status)
270 {
271         return (status & cpu_to_be16(MLX4_CQE_STATUS_IPV4       |
272                                      MLX4_CQE_STATUS_IPV4F      |
273                                      MLX4_CQE_STATUS_IPV6       |
274                                      MLX4_CQE_STATUS_IPV4OPT    |
275                                      MLX4_CQE_STATUS_TCP        |
276                                      MLX4_CQE_STATUS_UDP        |
277                                      MLX4_CQE_STATUS_IPOK)) ==
278                 cpu_to_be16(MLX4_CQE_STATUS_IPV4 |
279                             MLX4_CQE_STATUS_IPOK |
280                             MLX4_CQE_STATUS_TCP);
281 }
282
283 struct mlx4_en_cq {
284         struct mlx4_cq          mcq;
285         struct mlx4_hwq_resources wqres;
286         int                     ring;
287         spinlock_t              lock;
288         struct net_device      *dev;
289         struct napi_struct      napi;
290         /* Per-core Tx cq processing support */
291         struct timer_list timer;
292         int size;
293         int buf_size;
294         unsigned vector;
295         enum cq_type is_tx;
296         u16 moder_time;
297         u16 moder_cnt;
298         struct mlx4_cqe *buf;
299 #define MLX4_EN_OPCODE_ERROR    0x1e
300 };
301
302 struct mlx4_en_port_profile {
303         u32 flags;
304         u32 tx_ring_num;
305         u32 rx_ring_num;
306         u32 tx_ring_size;
307         u32 rx_ring_size;
308         u8 rx_pause;
309         u8 rx_ppp;
310         u8 tx_pause;
311         u8 tx_ppp;
312 };
313
314 struct mlx4_en_profile {
315         int rss_xor;
316         u8 rss_mask;
317         u32 active_ports;
318         u32 small_pkt_int;
319         u8 no_reset;
320         struct mlx4_en_port_profile prof[MLX4_MAX_PORTS + 1];
321 };
322
323 struct mlx4_en_dev {
324         struct mlx4_dev         *dev;
325         struct pci_dev          *pdev;
326         struct mutex            state_lock;
327         struct net_device       *pndev[MLX4_MAX_PORTS + 1];
328         u32                     port_cnt;
329         bool                    device_up;
330         struct mlx4_en_profile  profile;
331         u32                     LSO_support;
332         struct workqueue_struct *workqueue;
333         struct device           *dma_device;
334         void __iomem            *uar_map;
335         struct mlx4_uar         priv_uar;
336         struct mlx4_mr          mr;
337         u32                     priv_pdn;
338         spinlock_t              uar_lock;
339 };
340
341
342 struct mlx4_en_rss_map {
343         int base_qpn;
344         struct mlx4_qp qps[MAX_RX_RINGS];
345         enum mlx4_qp_state state[MAX_RX_RINGS];
346         struct mlx4_qp indir_qp;
347         enum mlx4_qp_state indir_state;
348 };
349
350 struct mlx4_en_rss_context {
351         __be32 base_qpn;
352         __be32 default_qpn;
353         u16 reserved;
354         u8 hash_fn;
355         u8 flags;
356         __be32 rss_key[10];
357 };
358
359 struct mlx4_en_pkt_stats {
360         unsigned long broadcast;
361         unsigned long rx_prio[8];
362         unsigned long tx_prio[8];
363 #define NUM_PKT_STATS           17
364 };
365
366 struct mlx4_en_port_stats {
367         unsigned long lro_aggregated;
368         unsigned long lro_flushed;
369         unsigned long lro_no_desc;
370         unsigned long tso_packets;
371         unsigned long queue_stopped;
372         unsigned long wake_queue;
373         unsigned long tx_timeout;
374         unsigned long rx_alloc_failed;
375         unsigned long rx_chksum_good;
376         unsigned long rx_chksum_none;
377         unsigned long tx_chksum_offload;
378 #define NUM_PORT_STATS          11
379 };
380
381 struct mlx4_en_perf_stats {
382         u32 tx_poll;
383         u64 tx_pktsz_avg;
384         u32 inflight_avg;
385         u16 tx_coal_avg;
386         u16 rx_coal_avg;
387         u32 napi_quota;
388 #define NUM_PERF_COUNTERS               6
389 };
390
391 struct mlx4_en_frag_info {
392         u16 frag_size;
393         u16 frag_prefix_size;
394         u16 frag_stride;
395         u16 frag_align;
396         u16 last_offset;
397
398 };
399
400 struct mlx4_en_priv {
401         struct mlx4_en_dev *mdev;
402         struct mlx4_en_port_profile *prof;
403         struct net_device *dev;
404         struct vlan_group *vlgrp;
405         struct net_device_stats stats;
406         struct net_device_stats ret_stats;
407         spinlock_t stats_lock;
408
409         unsigned long last_moder_packets;
410         unsigned long last_moder_tx_packets;
411         unsigned long last_moder_bytes;
412         unsigned long last_moder_jiffies;
413         int last_moder_time;
414         u16 rx_usecs;
415         u16 rx_frames;
416         u16 tx_usecs;
417         u16 tx_frames;
418         u32 pkt_rate_low;
419         u16 rx_usecs_low;
420         u32 pkt_rate_high;
421         u16 rx_usecs_high;
422         u16 sample_interval;
423         u16 adaptive_rx_coal;
424         u32 msg_enable;
425
426         struct mlx4_hwq_resources res;
427         int link_state;
428         int last_link_state;
429         bool port_up;
430         int port;
431         int registered;
432         int allocated;
433         int stride;
434         int rx_csum;
435         u64 mac;
436         int mac_index;
437         unsigned max_mtu;
438         int base_qpn;
439
440         struct mlx4_en_rss_map rss_map;
441         u32 flags;
442 #define MLX4_EN_FLAG_PROMISC    0x1
443         u32 tx_ring_num;
444         u32 rx_ring_num;
445         u32 rx_skb_size;
446         struct mlx4_en_frag_info frag_info[MLX4_EN_MAX_RX_FRAGS];
447         u16 num_frags;
448         u16 log_rx_info;
449
450         struct mlx4_en_tx_ring tx_ring[MAX_TX_RINGS];
451         struct mlx4_en_rx_ring rx_ring[MAX_RX_RINGS];
452         struct mlx4_en_cq tx_cq[MAX_TX_RINGS];
453         struct mlx4_en_cq rx_cq[MAX_RX_RINGS];
454         struct work_struct mcast_task;
455         struct work_struct mac_task;
456         struct work_struct watchdog_task;
457         struct work_struct linkstate_task;
458         struct delayed_work stats_task;
459         struct mlx4_en_perf_stats pstats;
460         struct mlx4_en_pkt_stats pkstats;
461         struct mlx4_en_port_stats port_stats;
462         char *mc_addrs;
463         int mc_addrs_cnt;
464         struct mlx4_en_stat_out_mbox hw_stats;
465 };
466
467
468 void mlx4_en_destroy_netdev(struct net_device *dev);
469 int mlx4_en_init_netdev(struct mlx4_en_dev *mdev, int port,
470                         struct mlx4_en_port_profile *prof);
471
472 int mlx4_en_start_port(struct net_device *dev);
473 void mlx4_en_stop_port(struct net_device *dev);
474
475 void mlx4_en_free_resources(struct mlx4_en_priv *priv);
476 int mlx4_en_alloc_resources(struct mlx4_en_priv *priv);
477
478 int mlx4_en_create_cq(struct mlx4_en_priv *priv, struct mlx4_en_cq *cq,
479                       int entries, int ring, enum cq_type mode);
480 void mlx4_en_destroy_cq(struct mlx4_en_priv *priv, struct mlx4_en_cq *cq);
481 int mlx4_en_activate_cq(struct mlx4_en_priv *priv, struct mlx4_en_cq *cq);
482 void mlx4_en_deactivate_cq(struct mlx4_en_priv *priv, struct mlx4_en_cq *cq);
483 int mlx4_en_set_cq_moder(struct mlx4_en_priv *priv, struct mlx4_en_cq *cq);
484 int mlx4_en_arm_cq(struct mlx4_en_priv *priv, struct mlx4_en_cq *cq);
485
486 void mlx4_en_poll_tx_cq(unsigned long data);
487 void mlx4_en_tx_irq(struct mlx4_cq *mcq);
488 u16 mlx4_en_select_queue(struct net_device *dev, struct sk_buff *skb);
489 netdev_tx_t mlx4_en_xmit(struct sk_buff *skb, struct net_device *dev);
490
491 int mlx4_en_create_tx_ring(struct mlx4_en_priv *priv, struct mlx4_en_tx_ring *ring,
492                            u32 size, u16 stride);
493 void mlx4_en_destroy_tx_ring(struct mlx4_en_priv *priv, struct mlx4_en_tx_ring *ring);
494 int mlx4_en_activate_tx_ring(struct mlx4_en_priv *priv,
495                              struct mlx4_en_tx_ring *ring,
496                              int cq);
497 void mlx4_en_deactivate_tx_ring(struct mlx4_en_priv *priv,
498                                 struct mlx4_en_tx_ring *ring);
499
500 int mlx4_en_create_rx_ring(struct mlx4_en_priv *priv,
501                            struct mlx4_en_rx_ring *ring,
502                            u32 size, u16 stride);
503 void mlx4_en_destroy_rx_ring(struct mlx4_en_priv *priv,
504                              struct mlx4_en_rx_ring *ring);
505 int mlx4_en_activate_rx_rings(struct mlx4_en_priv *priv);
506 void mlx4_en_deactivate_rx_ring(struct mlx4_en_priv *priv,
507                                 struct mlx4_en_rx_ring *ring);
508 int mlx4_en_process_rx_cq(struct net_device *dev,
509                           struct mlx4_en_cq *cq,
510                           int budget);
511 int mlx4_en_poll_rx_cq(struct napi_struct *napi, int budget);
512 void mlx4_en_fill_qp_context(struct mlx4_en_priv *priv, int size, int stride,
513                              int is_tx, int rss, int qpn, int cqn,
514                              struct mlx4_qp_context *context);
515 void mlx4_en_sqp_event(struct mlx4_qp *qp, enum mlx4_event event);
516 int mlx4_en_map_buffer(struct mlx4_buf *buf);
517 void mlx4_en_unmap_buffer(struct mlx4_buf *buf);
518
519 void mlx4_en_calc_rx_buf(struct net_device *dev);
520 int mlx4_en_config_rss_steer(struct mlx4_en_priv *priv);
521 void mlx4_en_release_rss_steer(struct mlx4_en_priv *priv);
522 int mlx4_en_free_tx_buf(struct net_device *dev, struct mlx4_en_tx_ring *ring);
523 void mlx4_en_rx_irq(struct mlx4_cq *mcq);
524
525 int mlx4_SET_MCAST_FLTR(struct mlx4_dev *dev, u8 port, u64 mac, u64 clear, u8 mode);
526 int mlx4_SET_VLAN_FLTR(struct mlx4_dev *dev, u8 port, struct vlan_group *grp);
527 int mlx4_SET_PORT_general(struct mlx4_dev *dev, u8 port, int mtu,
528                           u8 pptx, u8 pfctx, u8 pprx, u8 pfcrx);
529 int mlx4_SET_PORT_qpn_calc(struct mlx4_dev *dev, u8 port, u32 base_qpn,
530                            u8 promisc);
531
532 int mlx4_en_DUMP_ETH_STATS(struct mlx4_en_dev *mdev, u8 port, u8 reset);
533
534 /*
535  * Globals
536  */
537 extern const struct ethtool_ops mlx4_en_ethtool_ops;
538
539
540
541 /*
542  * printk / logging functions
543  */
544
545 int en_print(const char *level, const struct mlx4_en_priv *priv,
546              const char *format, ...) __attribute__ ((format (printf, 3, 4)));
547
548 #define en_dbg(mlevel, priv, format, arg...)                    \
549 do {                                                            \
550         if (NETIF_MSG_##mlevel & priv->msg_enable)              \
551                 en_print(KERN_DEBUG, priv, format, ##arg);      \
552 } while (0)
553 #define en_warn(priv, format, arg...)                   \
554         en_print(KERN_WARNING, priv, format, ##arg)
555 #define en_err(priv, format, arg...)                    \
556         en_print(KERN_ERR, priv, format, ##arg)
557
558 #define mlx4_err(mdev, format, arg...)                  \
559         pr_err("%s %s: " format, DRV_NAME,              \
560                dev_name(&mdev->pdev->dev), ##arg)
561 #define mlx4_info(mdev, format, arg...)                 \
562         pr_info("%s %s: " format, DRV_NAME,             \
563                 dev_name(&mdev->pdev->dev), ##arg)
564 #define mlx4_warn(mdev, format, arg...)                 \
565         pr_warning("%s %s: " format, DRV_NAME,          \
566                    dev_name(&mdev->pdev->dev), ##arg)
567
568 #endif