mlx4: Clean up to make it compile
[akaros.git] / kern / drivers / net / mlx4 / en_tx.c
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 #include <linux/mlx4/cq.h>
35 #include <linux/mlx4/qp.h>
36 #include "mlx4_en.h"
37
38 int mlx4_en_create_tx_ring(struct mlx4_en_priv *priv,
39                            struct mlx4_en_tx_ring **pring, uint32_t size,
40                            uint16_t stride, int node, int queue_index)
41 {
42         struct mlx4_en_dev *mdev = priv->mdev;
43         struct mlx4_en_tx_ring *ring;
44         int tmp;
45         int err;
46
47         ring = kzalloc_node(sizeof(*ring), KMALLOC_WAIT, node);
48         if (!ring) {
49                 ring = kzmalloc(sizeof(*ring), KMALLOC_WAIT);
50                 if (!ring) {
51                         en_err(priv, "Failed allocating TX ring\n");
52                         return -ENOMEM;
53                 }
54         }
55
56         ring->size = size;
57         ring->size_mask = size - 1;
58         ring->stride = stride;
59
60         tmp = size * sizeof(struct mlx4_en_tx_info);
61         ring->tx_info = kmalloc_node(tmp, KMALLOC_WAIT | __GFP_NOWARN, node);
62         if (!ring->tx_info) {
63                 ring->tx_info = vmalloc(tmp);
64                 if (!ring->tx_info) {
65                         err = -ENOMEM;
66                         goto err_ring;
67                 }
68         }
69
70         en_dbg(DRV, priv, "Allocated tx_info ring at addr:%p size:%d\n",
71                  ring->tx_info, tmp);
72
73         ring->bounce_buf = kmalloc_node(MAX_DESC_SIZE, KMALLOC_WAIT, node);
74         if (!ring->bounce_buf) {
75                 ring->bounce_buf = kmalloc(MAX_DESC_SIZE, KMALLOC_WAIT);
76                 if (!ring->bounce_buf) {
77                         err = -ENOMEM;
78                         goto err_info;
79                 }
80         }
81         ring->buf_size = ALIGN(size * ring->stride, MLX4_EN_PAGE_SIZE);
82
83         /* Allocate HW buffers on provided NUMA node */
84         set_dev_node(&mdev->dev->persist->pdev->dev, node);
85         err = mlx4_alloc_hwq_res(mdev->dev, &ring->wqres, ring->buf_size,
86                                  2 * PAGE_SIZE);
87         set_dev_node(&mdev->dev->persist->pdev->dev, mdev->dev->numa_node);
88         if (err) {
89                 en_err(priv, "Failed allocating hwq resources\n");
90                 goto err_bounce;
91         }
92
93         err = mlx4_en_map_buffer(&ring->wqres.buf);
94         if (err) {
95                 en_err(priv, "Failed to map TX buffer\n");
96                 goto err_hwq_res;
97         }
98
99         ring->buf = ring->wqres.buf.direct.buf;
100
101         en_dbg(DRV, priv, "Allocated TX ring (addr:%p) - buf:%p size:%d buf_size:%d dma:%llx\n",
102                ring, ring->buf, ring->size, ring->buf_size,
103                (unsigned long long) ring->wqres.buf.direct.map);
104
105         err = mlx4_qp_reserve_range(mdev->dev, 1, 1, &ring->qpn,
106                                     MLX4_RESERVE_ETH_BF_QP);
107         if (err) {
108                 en_err(priv, "failed reserving qp for TX ring\n");
109                 goto err_map;
110         }
111
112         err = mlx4_qp_alloc(mdev->dev, ring->qpn, &ring->qp, KMALLOC_WAIT);
113         if (err) {
114                 en_err(priv, "Failed allocating qp %d\n", ring->qpn);
115                 goto err_reserve;
116         }
117         ring->qp.event = mlx4_en_sqp_event;
118
119 #if 0 // AKAROS_PORT
120         err = mlx4_bf_alloc(mdev->dev, &ring->bf, node);
121         if (err) {
122 #else
123         if (true) {
124 #endif
125                 en_dbg(DRV, priv, "working without blueflame (%d)\n", err);
126                 ring->bf.uar = &mdev->priv_uar;
127                 ring->bf.uar->map = mdev->uar_map;
128                 ring->bf_enabled = false;
129                 ring->bf_alloced = false;
130                 priv->pflags &= ~MLX4_EN_PRIV_FLAGS_BLUEFLAME;
131         } else {
132                 ring->bf_alloced = true;
133                 ring->bf_enabled = !!(priv->pflags &
134                                       MLX4_EN_PRIV_FLAGS_BLUEFLAME);
135         }
136
137         ring->hwtstamp_tx_type = priv->hwtstamp_config.tx_type;
138         ring->queue_index = queue_index;
139
140         if (queue_index < priv->num_tx_rings_p_up)
141                 cpumask_set_cpu(cpumask_local_spread(queue_index,
142                                                      priv->mdev->dev->numa_node),
143                                 &ring->affinity_mask);
144
145         *pring = ring;
146         return 0;
147
148 err_reserve:
149         mlx4_qp_release_range(mdev->dev, ring->qpn, 1);
150 err_map:
151         mlx4_en_unmap_buffer(&ring->wqres.buf);
152 err_hwq_res:
153         mlx4_free_hwq_res(mdev->dev, &ring->wqres, ring->buf_size);
154 err_bounce:
155         kfree(ring->bounce_buf);
156         ring->bounce_buf = NULL;
157 err_info:
158 #if 0 // AKAROS_PORT
159         kvfree(ring->tx_info);
160 #endif
161         ring->tx_info = NULL;
162 err_ring:
163         kfree(ring);
164         *pring = NULL;
165         return err;
166 }
167
168 void mlx4_en_destroy_tx_ring(struct mlx4_en_priv *priv,
169                              struct mlx4_en_tx_ring **pring)
170 {
171         panic("Disabled");
172 #if 0 // AKAROS_PORT
173         struct mlx4_en_dev *mdev = priv->mdev;
174         struct mlx4_en_tx_ring *ring = *pring;
175         en_dbg(DRV, priv, "Destroying tx ring, qpn: %d\n", ring->qpn);
176
177         if (ring->bf_alloced)
178                 mlx4_bf_free(mdev->dev, &ring->bf);
179         mlx4_qp_remove(mdev->dev, &ring->qp);
180         mlx4_qp_free(mdev->dev, &ring->qp);
181         mlx4_en_unmap_buffer(&ring->wqres.buf);
182         mlx4_free_hwq_res(mdev->dev, &ring->wqres, ring->buf_size);
183         kfree(ring->bounce_buf);
184         ring->bounce_buf = NULL;
185         kvfree(ring->tx_info);
186         ring->tx_info = NULL;
187         kfree(ring);
188         *pring = NULL;
189 #endif
190 }
191
192 int mlx4_en_activate_tx_ring(struct mlx4_en_priv *priv,
193                              struct mlx4_en_tx_ring *ring,
194                              int cq, int user_prio)
195 {
196         struct mlx4_en_dev *mdev = priv->mdev;
197         int err;
198
199         ring->cqn = cq;
200         ring->prod = 0;
201         ring->cons = 0xffffffff;
202         ring->last_nr_txbb = 1;
203         memset(ring->tx_info, 0, ring->size * sizeof(struct mlx4_en_tx_info));
204         memset(ring->buf, 0, ring->buf_size);
205
206         ring->qp_state = MLX4_QP_STATE_RST;
207         ring->doorbell_qpn = cpu_to_be32(ring->qp.qpn << 8);
208         ring->mr_key = cpu_to_be32(mdev->mr.key);
209
210         mlx4_en_fill_qp_context(priv, ring->size, ring->stride, 1, 0, ring->qpn,
211                                 ring->cqn, user_prio, &ring->context);
212         if (ring->bf_alloced)
213                 ring->context.usr_page = cpu_to_be32(ring->bf.uar->index);
214
215         err = mlx4_qp_to_ready(mdev->dev, &ring->wqres.mtt, &ring->context,
216                                &ring->qp, &ring->qp_state);
217 #if 0 // AKAROS_PORT
218         if (!cpumask_empty(&ring->affinity_mask))
219                 netif_set_xps_queue(priv->dev, &ring->affinity_mask,
220                                     ring->queue_index);
221 #endif
222
223         return err;
224 }
225
226 void mlx4_en_deactivate_tx_ring(struct mlx4_en_priv *priv,
227                                 struct mlx4_en_tx_ring *ring)
228 {
229         struct mlx4_en_dev *mdev = priv->mdev;
230
231         mlx4_qp_modify(mdev->dev, NULL, ring->qp_state,
232                        MLX4_QP_STATE_RST, NULL, 0, 0, &ring->qp);
233 }
234
235 static void mlx4_en_stamp_wqe(struct mlx4_en_priv *priv,
236                               struct mlx4_en_tx_ring *ring, int index,
237                               uint8_t owner)
238 {
239         __be32 stamp = cpu_to_be32(STAMP_VAL | (!!owner << STAMP_SHIFT));
240         struct mlx4_en_tx_desc *tx_desc = ring->buf + index * TXBB_SIZE;
241         struct mlx4_en_tx_info *tx_info = &ring->tx_info[index];
242         void *end = ring->buf + ring->buf_size;
243         __be32 *ptr = (__be32 *)tx_desc;
244         int i;
245
246         /* Optimize the common case when there are no wraparounds */
247         if (likely((void *)tx_desc + tx_info->nr_txbb * TXBB_SIZE <= end)) {
248                 /* Stamp the freed descriptor */
249                 for (i = 0; i < tx_info->nr_txbb * TXBB_SIZE;
250                      i += STAMP_STRIDE) {
251                         *ptr = stamp;
252                         ptr += STAMP_DWORDS;
253                 }
254         } else {
255                 /* Stamp the freed descriptor */
256                 for (i = 0; i < tx_info->nr_txbb * TXBB_SIZE;
257                      i += STAMP_STRIDE) {
258                         *ptr = stamp;
259                         ptr += STAMP_DWORDS;
260                         if ((void *)ptr >= end) {
261                                 ptr = ring->buf;
262                                 stamp ^= cpu_to_be32(0x80000000);
263                         }
264                 }
265         }
266 }
267
268
269 static uint32_t mlx4_en_free_tx_desc(struct mlx4_en_priv *priv,
270                                 struct mlx4_en_tx_ring *ring,
271                                 int index, uint8_t owner, uint64_t timestamp)
272 {
273         struct mlx4_en_tx_info *tx_info = &ring->tx_info[index];
274         struct mlx4_en_tx_desc *tx_desc = ring->buf + index * TXBB_SIZE;
275         struct mlx4_wqe_data_seg *data = (void *) tx_desc + tx_info->data_offset;
276         void *end = ring->buf + ring->buf_size;
277         struct block *block = tx_info->block;
278         int nr_maps = tx_info->nr_maps;
279         int i;
280
281 #if 0 // AKAROS_PORT
282         /* We do not touch skb here, so prefetch skb->users location
283          * to speedup consume_skb()
284          */
285         prefetchw(&skb->users);
286
287         if (unlikely(timestamp)) {
288                 struct skb_shared_hwtstamps hwts;
289
290                 mlx4_en_fill_hwtstamps(priv->mdev, &hwts, timestamp);
291                 skb_tstamp_tx(skb, &hwts);
292         }
293 #endif
294
295         /* Optimize the common case when there are no wraparounds */
296         if (likely((void *) tx_desc + tx_info->nr_txbb * TXBB_SIZE <= end)) {
297                 if (!tx_info->inl) {
298                         if (tx_info->linear)
299                                 dma_unmap_single(priv->ddev,
300                                                 tx_info->map0_dma,
301                                                 tx_info->map0_byte_count,
302                                                 PCI_DMA_TODEVICE);
303                         else
304                                 dma_unmap_page(priv->ddev,
305                                                tx_info->map0_dma,
306                                                tx_info->map0_byte_count,
307                                                PCI_DMA_TODEVICE);
308                         for (i = 1; i < nr_maps; i++) {
309                                 data++;
310                                 dma_unmap_page(priv->ddev,
311                                         (dma_addr_t)be64_to_cpu(data->addr),
312                                         be32_to_cpu(data->byte_count),
313                                         PCI_DMA_TODEVICE);
314                         }
315                 }
316         } else {
317                 if (!tx_info->inl) {
318                         if ((void *) data >= end) {
319                                 data = ring->buf + ((void *)data - end);
320                         }
321
322                         if (tx_info->linear)
323                                 dma_unmap_single(priv->ddev,
324                                                 tx_info->map0_dma,
325                                                 tx_info->map0_byte_count,
326                                                 PCI_DMA_TODEVICE);
327                         else
328                                 dma_unmap_page(priv->ddev,
329                                                tx_info->map0_dma,
330                                                tx_info->map0_byte_count,
331                                                PCI_DMA_TODEVICE);
332                         for (i = 1; i < nr_maps; i++) {
333                                 data++;
334                                 /* Check for wraparound before unmapping */
335                                 if ((void *) data >= end)
336                                         data = ring->buf;
337                                 dma_unmap_page(priv->ddev,
338                                         (dma_addr_t)be64_to_cpu(data->addr),
339                                         be32_to_cpu(data->byte_count),
340                                         PCI_DMA_TODEVICE);
341                         }
342                 }
343         }
344         freeb(block);
345         return tx_info->nr_txbb;
346 }
347
348
349 int mlx4_en_free_tx_buf(struct ether *dev, struct mlx4_en_tx_ring *ring)
350 {
351         panic("Disabled");
352 #if 0 // AKAROS_PORT
353         struct mlx4_en_priv *priv = netdev_priv(dev);
354         int cnt = 0;
355
356         /* Skip last polled descriptor */
357         ring->cons += ring->last_nr_txbb;
358         en_dbg(DRV, priv, "Freeing Tx buf - cons:0x%x prod:0x%x\n",
359                  ring->cons, ring->prod);
360
361         if ((uint32_t) (ring->prod - ring->cons) > ring->size) {
362                 if (netif_msg_tx_err(priv))
363                         en_warn(priv, "Tx consumer passed producer!\n");
364                 return 0;
365         }
366
367         while (ring->cons != ring->prod) {
368                 ring->last_nr_txbb = mlx4_en_free_tx_desc(priv, ring,
369                                                 ring->cons & ring->size_mask,
370                                                 !!(ring->cons & ring->size), 0);
371                 ring->cons += ring->last_nr_txbb;
372                 cnt++;
373         }
374
375         netdev_tx_reset_queue(ring->tx_queue);
376
377         if (cnt)
378                 en_dbg(DRV, priv, "Freed %d uncompleted tx descriptors\n", cnt);
379
380         return cnt;
381 #endif
382 }
383
384 static bool mlx4_en_process_tx_cq(struct ether *dev,
385                                   struct mlx4_en_cq *cq)
386 {
387         struct mlx4_en_priv *priv = netdev_priv(dev);
388         struct mlx4_cq *mcq = &cq->mcq;
389         struct mlx4_en_tx_ring *ring = priv->tx_ring[cq->ring];
390         struct mlx4_cqe *cqe;
391         uint16_t index;
392         uint16_t new_index, ring_index, stamp_index;
393         uint32_t txbbs_skipped = 0;
394         uint32_t txbbs_stamp = 0;
395         uint32_t cons_index = mcq->cons_index;
396         int size = cq->size;
397         uint32_t size_mask = ring->size_mask;
398         struct mlx4_cqe *buf = cq->buf;
399         uint32_t packets = 0;
400         uint32_t bytes = 0;
401         int factor = priv->cqe_factor;
402         uint64_t timestamp = 0;
403         int done = 0;
404         int budget = priv->tx_work_limit;
405         uint32_t last_nr_txbb;
406         uint32_t ring_cons;
407
408         if (!priv->port_up)
409                 return true;
410
411 #if 0 // AKAROS_PORT
412         netdev_txq_bql_complete_prefetchw(ring->tx_queue);
413 #endif
414
415         index = cons_index & size_mask;
416         cqe = mlx4_en_get_cqe(buf, index, priv->cqe_size) + factor;
417         last_nr_txbb = ACCESS_ONCE(ring->last_nr_txbb);
418         ring_cons = ACCESS_ONCE(ring->cons);
419         ring_index = ring_cons & size_mask;
420         stamp_index = ring_index;
421
422         /* Process all completed CQEs */
423         while (XNOR(cqe->owner_sr_opcode & MLX4_CQE_OWNER_MASK,
424                         cons_index & size) && (done < budget)) {
425                 /*
426                  * make sure we read the CQE after we read the
427                  * ownership bit
428                  */
429                 bus_rmb();
430
431                 if (unlikely((cqe->owner_sr_opcode & MLX4_CQE_OPCODE_MASK) ==
432                              MLX4_CQE_OPCODE_ERROR)) {
433                         struct mlx4_err_cqe *cqe_err = (struct mlx4_err_cqe *)cqe;
434
435                         en_err(priv, "CQE error - vendor syndrome: 0x%x syndrome: 0x%x\n",
436                                cqe_err->vendor_err_syndrome,
437                                cqe_err->syndrome);
438                 }
439
440                 /* Skip over last polled CQE */
441                 new_index = be16_to_cpu(cqe->wqe_index) & size_mask;
442
443                 do {
444                         txbbs_skipped += last_nr_txbb;
445                         ring_index = (ring_index + last_nr_txbb) & size_mask;
446                         if (ring->tx_info[ring_index].ts_requested)
447                                 timestamp = mlx4_en_get_cqe_ts(cqe);
448
449                         /* free next descriptor */
450                         last_nr_txbb = mlx4_en_free_tx_desc(
451                                         priv, ring, ring_index,
452                                         !!((ring_cons + txbbs_skipped) &
453                                         ring->size), timestamp);
454
455                         mlx4_en_stamp_wqe(priv, ring, stamp_index,
456                                           !!((ring_cons + txbbs_stamp) &
457                                                 ring->size));
458                         stamp_index = ring_index;
459                         txbbs_stamp = txbbs_skipped;
460                         packets++;
461                         bytes += ring->tx_info[ring_index].nr_bytes;
462                 } while ((++done < budget) && (ring_index != new_index));
463
464                 ++cons_index;
465                 index = cons_index & size_mask;
466                 cqe = mlx4_en_get_cqe(buf, index, priv->cqe_size) + factor;
467         }
468
469
470         /*
471          * To prevent CQ overflow we first update CQ consumer and only then
472          * the ring consumer.
473          */
474         mcq->cons_index = cons_index;
475         mlx4_cq_set_ci(mcq);
476         wmb();
477
478         /* we want to dirty this cache line once */
479         ACCESS_ONCE(ring->last_nr_txbb) = last_nr_txbb;
480         ACCESS_ONCE(ring->cons) = ring_cons + txbbs_skipped;
481
482 #if 0 // AKAROS_PORT
483         netdev_tx_completed_queue(ring->tx_queue, packets, bytes);
484
485         /*
486          * Wakeup Tx queue if this stopped, and at least 1 packet
487          * was completed
488          */
489         if (netif_tx_queue_stopped(ring->tx_queue) && txbbs_skipped > 0) {
490                 netif_tx_wake_queue(ring->tx_queue);
491                 ring->wake_queue++;
492         }
493 #endif
494         return done < budget;
495 }
496
497 void mlx4_en_tx_irq(struct mlx4_cq *mcq)
498 {
499         struct mlx4_en_cq *cq = container_of(mcq, struct mlx4_en_cq, mcq);
500         struct mlx4_en_priv *priv = netdev_priv(cq->dev);
501
502         if (likely(priv->port_up))
503 #if 0 // AKAROS_PORT
504                 napi_schedule_irqoff(&cq->napi);
505 #else
506                 { /* TODO */ }
507 #endif
508         else
509                 mlx4_en_arm_cq(priv, cq);
510 }
511
512 /* TX CQ polling - called by NAPI */
513 int mlx4_en_poll_tx_cq(struct napi_struct *napi, int budget)
514 {
515         struct mlx4_en_cq *cq = container_of(napi, struct mlx4_en_cq, napi);
516         struct ether *dev = cq->dev;
517         struct mlx4_en_priv *priv = netdev_priv(dev);
518         int clean_complete;
519
520         clean_complete = mlx4_en_process_tx_cq(dev, cq);
521         if (!clean_complete)
522                 return budget;
523
524         napi_complete(napi);
525         mlx4_en_arm_cq(priv, cq);
526
527         return 0;
528 }
529
530 static struct mlx4_en_tx_desc *mlx4_en_bounce_to_desc(struct mlx4_en_priv *priv,
531                                                       struct mlx4_en_tx_ring *ring,
532                                                       uint32_t index,
533                                                       unsigned int desc_size)
534 {
535         uint32_t copy = (ring->size - index) * TXBB_SIZE;
536         int i;
537
538         for (i = desc_size - copy - 4; i >= 0; i -= 4) {
539                 if ((i & (TXBB_SIZE - 1)) == 0)
540                         wmb();
541
542                 *((uint32_t *) (ring->buf + i)) =
543                         *((uint32_t *) (ring->bounce_buf + copy + i));
544         }
545
546         for (i = copy - 4; i >= 4 ; i -= 4) {
547                 if ((i & (TXBB_SIZE - 1)) == 0)
548                         wmb();
549
550                 *((uint32_t *) (ring->buf + index * TXBB_SIZE + i)) =
551                         *((uint32_t *) (ring->bounce_buf + i));
552         }
553
554         /* Return real descriptor location */
555         return ring->buf + index * TXBB_SIZE;
556 }
557
558 #if 0 // AKAROS_PORT
559 /* Decide if skb can be inlined in tx descriptor to avoid dma mapping
560  *
561  * It seems strange we do not simply use skb_copy_bits().
562  * This would allow to inline all skbs iff skb->len <= inline_thold
563  *
564  * Note that caller already checked skb was not a gso packet
565  */
566 static bool is_inline(int inline_thold, const struct sk_buff *skb,
567                       const struct skb_shared_info *shinfo,
568                       void **pfrag)
569 {
570         void *ptr;
571
572         if (skb->len > inline_thold || !inline_thold)
573                 return false;
574
575         if (shinfo->nr_frags == 1) {
576                 ptr = skb_frag_address_safe(&shinfo->frags[0]);
577                 if (unlikely(!ptr))
578                         return false;
579                 *pfrag = ptr;
580                 return true;
581         }
582         if (shinfo->nr_frags)
583                 return false;
584         return true;
585 }
586
587 static int inline_size(const struct sk_buff *skb)
588 {
589         if (skb->len + CTRL_SIZE + sizeof(struct mlx4_wqe_inline_seg)
590             <= MLX4_INLINE_ALIGN)
591                 return ALIGN(skb->len + CTRL_SIZE +
592                              sizeof(struct mlx4_wqe_inline_seg), 16);
593         else
594                 return ALIGN(skb->len + CTRL_SIZE + 2 *
595                              sizeof(struct mlx4_wqe_inline_seg), 16);
596 }
597
598 static int get_real_size(const struct sk_buff *skb,
599                          const struct skb_shared_info *shinfo,
600                          struct ether *dev,
601                          int *lso_header_size,
602                          bool *inline_ok,
603                          void **pfrag)
604 {
605         struct mlx4_en_priv *priv = netdev_priv(dev);
606         int real_size;
607
608         if (shinfo->gso_size) {
609                 *inline_ok = false;
610                 if (skb->encapsulation)
611                         *lso_header_size = (skb_inner_transport_header(skb) - skb->data) + inner_tcp_hdrlen(skb);
612                 else
613                         *lso_header_size = skb_transport_offset(skb) + tcp_hdrlen(skb);
614                 real_size = CTRL_SIZE + shinfo->nr_frags * DS_SIZE +
615                         ALIGN(*lso_header_size + 4, DS_SIZE);
616                 if (unlikely(*lso_header_size != skb_headlen(skb))) {
617                         /* We add a segment for the skb linear buffer only if
618                          * it contains data */
619                         if (*lso_header_size < skb_headlen(skb))
620                                 real_size += DS_SIZE;
621                         else {
622                                 if (netif_msg_tx_err(priv))
623                                         en_warn(priv, "Non-linear headers\n");
624                                 return 0;
625                         }
626                 }
627         } else {
628                 *lso_header_size = 0;
629                 *inline_ok = is_inline(priv->prof->inline_thold, skb,
630                                        shinfo, pfrag);
631
632                 if (*inline_ok)
633                         real_size = inline_size(skb);
634                 else
635                         real_size = CTRL_SIZE +
636                                     (shinfo->nr_frags + 1) * DS_SIZE;
637         }
638
639         return real_size;
640 }
641
642 static void build_inline_wqe(struct mlx4_en_tx_desc *tx_desc,
643                              const struct sk_buff *skb,
644                              const struct skb_shared_info *shinfo,
645                              int real_size, uint16_t *vlan_tag,
646                              int tx_ind, void *fragptr)
647 {
648         struct mlx4_wqe_inline_seg *inl = &tx_desc->inl;
649         int spc = MLX4_INLINE_ALIGN - CTRL_SIZE - sizeof *inl;
650         unsigned int hlen = skb_headlen(skb);
651
652         if (skb->len <= spc) {
653                 if (likely(skb->len >= MIN_PKT_LEN)) {
654                         inl->byte_count = cpu_to_be32(1 << 31 | skb->len);
655                 } else {
656                         inl->byte_count = cpu_to_be32(1 << 31 | MIN_PKT_LEN);
657                         memset(((void *)(inl + 1)) + skb->len, 0,
658                                MIN_PKT_LEN - skb->len);
659                 }
660                 skb_copy_from_linear_data(skb, inl + 1, hlen);
661                 if (shinfo->nr_frags)
662                         memcpy(((void *)(inl + 1)) + hlen, fragptr,
663                                skb_frag_size(&shinfo->frags[0]));
664
665         } else {
666                 inl->byte_count = cpu_to_be32(1 << 31 | spc);
667                 if (hlen <= spc) {
668                         skb_copy_from_linear_data(skb, inl + 1, hlen);
669                         if (hlen < spc) {
670                                 memcpy(((void *)(inl + 1)) + hlen,
671                                        fragptr, spc - hlen);
672                                 fragptr +=  spc - hlen;
673                         }
674                         inl = (void *) (inl + 1) + spc;
675                         memcpy(((void *)(inl + 1)), fragptr, skb->len - spc);
676                 } else {
677                         skb_copy_from_linear_data(skb, inl + 1, spc);
678                         inl = (void *) (inl + 1) + spc;
679                         skb_copy_from_linear_data_offset(skb, spc, inl + 1,
680                                                          hlen - spc);
681                         if (shinfo->nr_frags)
682                                 memcpy(((void *)(inl + 1)) + hlen - spc,
683                                        fragptr,
684                                        skb_frag_size(&shinfo->frags[0]));
685                 }
686
687                 bus_wmb();
688                 inl->byte_count = cpu_to_be32(1 << 31 | (skb->len - spc));
689         }
690 }
691 #endif
692
693 uint16_t mlx4_en_select_queue(struct ether *dev, struct sk_buff *skb,
694                               void *accel_priv,
695                               select_queue_fallback_t fallback)
696 {
697         panic("Disabled");
698 #if 0 // AKAROS_PORT
699         struct mlx4_en_priv *priv = netdev_priv(dev);
700         uint16_t rings_p_up = priv->num_tx_rings_p_up;
701         uint8_t up = 0;
702
703         if (dev->num_tc)
704                 return skb_tx_hash(dev, skb);
705
706         if (skb_vlan_tag_present(skb))
707                 up = skb_vlan_tag_get(skb) >> VLAN_PRIO_SHIFT;
708
709         return fallback(dev, skb) % rings_p_up + up * rings_p_up;
710 #endif
711 }
712
713 #if 0 // AKAROS_PORT
714 static void mlx4_bf_copy(void __iomem *dst, const void *src,
715                          unsigned int bytecnt)
716 {
717         __iowrite64_copy(dst, src, bytecnt / 8);
718 }
719 #endif
720
721 #if 0 // AKAROS_PORT
722 netdev_tx_t mlx4_en_xmit(struct sk_buff *skb, struct ether *dev)
723 {
724         struct skb_shared_info *shinfo = skb_shinfo(skb);
725         struct mlx4_en_priv *priv = netdev_priv(dev);
726         struct device *ddev = priv->ddev;
727         struct mlx4_en_tx_ring *ring;
728         struct mlx4_en_tx_desc *tx_desc;
729         struct mlx4_wqe_data_seg *data;
730         struct mlx4_en_tx_info *tx_info;
731         int tx_ind = 0;
732         int nr_txbb;
733         int desc_size;
734         int real_size;
735         uint32_t index, bf_index;
736         __be32 op_own;
737         uint16_t vlan_tag = 0;
738         int i_frag;
739         int lso_header_size;
740         void *fragptr = NULL;
741         bool bounce = false;
742         bool send_doorbell;
743         bool stop_queue;
744         bool inline_ok;
745         uint32_t ring_cons;
746
747         if (!priv->port_up)
748                 goto tx_drop;
749
750         tx_ind = skb_get_queue_mapping(skb);
751         ring = priv->tx_ring[tx_ind];
752
753         /* fetch ring->cons far ahead before needing it to avoid stall */
754         ring_cons = ACCESS_ONCE(ring->cons);
755
756         real_size = get_real_size(skb, shinfo, dev, &lso_header_size,
757                                   &inline_ok, &fragptr);
758         if (unlikely(!real_size))
759                 goto tx_drop;
760
761         /* Align descriptor to TXBB size */
762         desc_size = ALIGN(real_size, TXBB_SIZE);
763         nr_txbb = desc_size / TXBB_SIZE;
764         if (unlikely(nr_txbb > MAX_DESC_TXBBS)) {
765                 if (netif_msg_tx_err(priv))
766                         en_warn(priv, "Oversized header or SG list\n");
767                 goto tx_drop;
768         }
769
770         if (skb_vlan_tag_present(skb))
771                 vlan_tag = skb_vlan_tag_get(skb);
772
773
774         netdev_txq_bql_enqueue_prefetchw(ring->tx_queue);
775
776         /* Track current inflight packets for performance analysis */
777         AVG_PERF_COUNTER(priv->pstats.inflight_avg,
778                          (uint32_t)(ring->prod - ring_cons - 1));
779
780         /* Packet is good - grab an index and transmit it */
781         index = ring->prod & ring->size_mask;
782         bf_index = ring->prod;
783
784         /* See if we have enough space for whole descriptor TXBB for setting
785          * SW ownership on next descriptor; if not, use a bounce buffer. */
786         if (likely(index + nr_txbb <= ring->size))
787                 tx_desc = ring->buf + index * TXBB_SIZE;
788         else {
789                 tx_desc = (struct mlx4_en_tx_desc *) ring->bounce_buf;
790                 bounce = true;
791         }
792
793         /* Save skb in tx_info ring */
794         tx_info = &ring->tx_info[index];
795         tx_info->skb = skb;
796         tx_info->nr_txbb = nr_txbb;
797
798         data = &tx_desc->data;
799         if (lso_header_size)
800                 data = ((void *)&tx_desc->lso + ALIGN(lso_header_size + 4,
801                                                       DS_SIZE));
802
803         /* valid only for none inline segments */
804         tx_info->data_offset = (void *)data - (void *)tx_desc;
805
806         tx_info->inl = inline_ok;
807
808         tx_info->linear = (lso_header_size < skb_headlen(skb) &&
809                            !inline_ok) ? 1 : 0;
810
811         tx_info->nr_maps = shinfo->nr_frags + tx_info->linear;
812         data += tx_info->nr_maps - 1;
813
814         if (!tx_info->inl) {
815                 dma_addr_t dma = 0;
816                 uint32_t byte_count = 0;
817
818                 /* Map fragments if any */
819                 for (i_frag = shinfo->nr_frags - 1; i_frag >= 0; i_frag--) {
820                         const struct skb_frag_struct *frag;
821
822                         frag = &shinfo->frags[i_frag];
823                         byte_count = skb_frag_size(frag);
824                         dma = skb_frag_dma_map(ddev, frag,
825                                                0, byte_count,
826                                                DMA_TO_DEVICE);
827                         if (dma_mapping_error(ddev, dma))
828                                 goto tx_drop_unmap;
829
830                         data->addr = cpu_to_be64(dma);
831                         data->lkey = ring->mr_key;
832                         bus_wmb();
833                         data->byte_count = cpu_to_be32(byte_count);
834                         --data;
835                 }
836
837                 /* Map linear part if needed */
838                 if (tx_info->linear) {
839                         byte_count = skb_headlen(skb) - lso_header_size;
840
841                         dma = dma_map_single(ddev, skb->data +
842                                              lso_header_size, byte_count,
843                                              PCI_DMA_TODEVICE);
844                         if (dma_mapping_error(ddev, dma))
845                                 goto tx_drop_unmap;
846
847                         data->addr = cpu_to_be64(dma);
848                         data->lkey = ring->mr_key;
849                         bus_wmb();
850                         data->byte_count = cpu_to_be32(byte_count);
851                 }
852                 /* tx completion can avoid cache line miss for common cases */
853                 tx_info->map0_dma = dma;
854                 tx_info->map0_byte_count = byte_count;
855         }
856
857         /*
858          * For timestamping add flag to skb_shinfo and
859          * set flag for further reference
860          */
861         tx_info->ts_requested = 0;
862         if (unlikely(ring->hwtstamp_tx_type == HWTSTAMP_TX_ON &&
863                      shinfo->tx_flags & SKBTX_HW_TSTAMP)) {
864                 shinfo->tx_flags |= SKBTX_IN_PROGRESS;
865                 tx_info->ts_requested = 1;
866         }
867
868         /* Prepare ctrl segement apart opcode+ownership, which depends on
869          * whether LSO is used */
870         tx_desc->ctrl.srcrb_flags = priv->ctrl_flags;
871         if (likely(skb->ip_summed == CHECKSUM_PARTIAL)) {
872                 if (!skb->encapsulation)
873                         tx_desc->ctrl.srcrb_flags |= cpu_to_be32(MLX4_WQE_CTRL_IP_CSUM |
874                                                                  MLX4_WQE_CTRL_TCP_UDP_CSUM);
875                 else
876                         tx_desc->ctrl.srcrb_flags |= cpu_to_be32(MLX4_WQE_CTRL_IP_CSUM);
877                 ring->tx_csum++;
878         }
879
880         if (priv->flags & MLX4_EN_FLAG_ENABLE_HW_LOOPBACK) {
881                 struct ethhdr *ethh;
882
883                 /* Copy dst mac address to wqe. This allows loopback in eSwitch,
884                  * so that VFs and PF can communicate with each other
885                  */
886                 ethh = (struct ethhdr *)skb->data;
887                 tx_desc->ctrl.srcrb_flags16[0] = get_unaligned((__be16 *)ethh->h_dest);
888                 tx_desc->ctrl.imm = get_unaligned((__be32 *)(ethh->h_dest + 2));
889         }
890
891         /* Handle LSO (TSO) packets */
892         if (lso_header_size) {
893                 int i;
894
895                 /* Mark opcode as LSO */
896                 op_own = cpu_to_be32(MLX4_OPCODE_LSO | (1 << 6)) |
897                         ((ring->prod & ring->size) ?
898                                 cpu_to_be32(MLX4_EN_BIT_DESC_OWN) : 0);
899
900                 /* Fill in the LSO prefix */
901                 tx_desc->lso.mss_hdr_size = cpu_to_be32(
902                         shinfo->gso_size << 16 | lso_header_size);
903
904                 /* Copy headers;
905                  * note that we already verified that it is linear */
906                 memcpy(tx_desc->lso.header, skb->data, lso_header_size);
907
908                 ring->tso_packets++;
909
910                 i = ((skb->len - lso_header_size) / shinfo->gso_size) +
911                         !!((skb->len - lso_header_size) % shinfo->gso_size);
912                 tx_info->nr_bytes = skb->len + (i - 1) * lso_header_size;
913                 ring->packets += i;
914         } else {
915                 /* Normal (Non LSO) packet */
916                 op_own = cpu_to_be32(MLX4_OPCODE_SEND) |
917                         ((ring->prod & ring->size) ?
918                          cpu_to_be32(MLX4_EN_BIT_DESC_OWN) : 0);
919                 tx_info->nr_bytes = MAX_T(unsigned int, skb->len, ETH_ZLEN);
920                 ring->packets++;
921         }
922         ring->bytes += tx_info->nr_bytes;
923         netdev_tx_sent_queue(ring->tx_queue, tx_info->nr_bytes);
924         AVG_PERF_COUNTER(priv->pstats.tx_pktsz_avg, skb->len);
925
926         if (tx_info->inl)
927                 build_inline_wqe(tx_desc, skb, shinfo, real_size, &vlan_tag,
928                                  tx_ind, fragptr);
929
930         if (skb->encapsulation) {
931                 struct iphdr *ipv4 = (struct iphdr *)skb_inner_network_header(skb);
932                 if (ipv4->protocol == IPPROTO_TCP || ipv4->protocol == IPPROTO_UDP)
933                         op_own |= cpu_to_be32(MLX4_WQE_CTRL_IIP | MLX4_WQE_CTRL_ILP);
934                 else
935                         op_own |= cpu_to_be32(MLX4_WQE_CTRL_IIP);
936         }
937
938         ring->prod += nr_txbb;
939
940         /* If we used a bounce buffer then copy descriptor back into place */
941         if (unlikely(bounce))
942                 tx_desc = mlx4_en_bounce_to_desc(priv, ring, index, desc_size);
943
944         skb_tx_timestamp(skb);
945
946         /* Check available TXBBs And 2K spare for prefetch */
947         stop_queue = (int)(ring->prod - ring_cons) >
948                       ring->size - HEADROOM - MAX_DESC_TXBBS;
949         if (unlikely(stop_queue)) {
950                 netif_tx_stop_queue(ring->tx_queue);
951                 ring->queue_stopped++;
952         }
953         send_doorbell = !skb->xmit_more || netif_xmit_stopped(ring->tx_queue);
954
955         real_size = (real_size / 16) & 0x3f;
956
957         if (ring->bf_enabled && desc_size <= MAX_BF && !bounce &&
958             !skb_vlan_tag_present(skb) && send_doorbell) {
959                 tx_desc->ctrl.bf_qpn = ring->doorbell_qpn |
960                                        cpu_to_be32(real_size);
961
962                 op_own |= cpu_to_be32((bf_index & 0xffff) << 8);
963                 /* Ensure new descriptor hits memory
964                  * before setting ownership of this descriptor to HW
965                  */
966                 bus_wmb();
967                 tx_desc->ctrl.owner_opcode = op_own;
968
969                 wmb();
970
971                 mlx4_bf_copy(ring->bf.reg + ring->bf.offset, &tx_desc->ctrl,
972                              desc_size);
973
974                 wmb();
975
976                 ring->bf.offset ^= ring->bf.buf_size;
977         } else {
978                 tx_desc->ctrl.vlan_tag = cpu_to_be16(vlan_tag);
979                 tx_desc->ctrl.ins_vlan = MLX4_WQE_CTRL_INS_VLAN *
980                         !!skb_vlan_tag_present(skb);
981                 tx_desc->ctrl.fence_size = real_size;
982
983                 /* Ensure new descriptor hits memory
984                  * before setting ownership of this descriptor to HW
985                  */
986                 bus_wmb();
987                 tx_desc->ctrl.owner_opcode = op_own;
988                 if (send_doorbell) {
989                         wmb();
990                         /* Since there is no iowrite*_native() that writes the
991                          * value as is, without byteswapping - using the one
992                          * the doesn't do byteswapping in the relevant arch
993                          * endianness.
994                          */
995 #if defined(__LITTLE_ENDIAN)
996                         iowrite32(
997 #else
998                         iowrite32be(
999 #endif
1000                                   ring->doorbell_qpn,
1001                                   ring->bf.uar->map + MLX4_SEND_DOORBELL);
1002                 } else {
1003                         ring->xmit_more++;
1004                 }
1005         }
1006
1007         if (unlikely(stop_queue)) {
1008                 /* If queue was emptied after the if (stop_queue) , and before
1009                  * the netif_tx_stop_queue() - need to wake the queue,
1010                  * or else it will remain stopped forever.
1011                  * Need a memory barrier to make sure ring->cons was not
1012                  * updated before queue was stopped.
1013                  */
1014                 rmb();
1015
1016                 ring_cons = ACCESS_ONCE(ring->cons);
1017                 if (unlikely(((int)(ring->prod - ring_cons)) <=
1018                              ring->size - HEADROOM - MAX_DESC_TXBBS)) {
1019                         netif_tx_wake_queue(ring->tx_queue);
1020                         ring->wake_queue++;
1021                 }
1022         }
1023         return NETDEV_TX_OK;
1024
1025 tx_drop_unmap:
1026         en_err(priv, "DMA mapping error\n");
1027
1028         while (++i_frag < shinfo->nr_frags) {
1029                 ++data;
1030                 dma_unmap_page(ddev, (dma_addr_t) be64_to_cpu(data->addr),
1031                                be32_to_cpu(data->byte_count),
1032                                PCI_DMA_TODEVICE);
1033         }
1034
1035 tx_drop:
1036         dev_kfree_skb_any(skb);
1037         priv->stats.tx_dropped++;
1038         return NETDEV_TX_OK;
1039 }
1040 #endif
1041