net/sfc/base: import 5xxx/6xxx family support
[dpdk.git] / drivers / net / sfc / base / efx_tx.c
1 /*
2  * Copyright (c) 2007-2016 Solarflare Communications Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice,
9  *    this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright notice,
11  *    this list of conditions and the following disclaimer in the documentation
12  *    and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
16  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
18  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
21  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
24  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  *
26  * The views and conclusions contained in the software and documentation are
27  * those of the authors and should not be interpreted as representing official
28  * policies, either expressed or implied, of the FreeBSD Project.
29  */
30
31 #include "efx.h"
32 #include "efx_impl.h"
33
34 #define EFX_TX_QSTAT_INCR(_etp, _stat)
35
36 #if EFSYS_OPT_SIENA
37
38 static  __checkReturn   efx_rc_t
39 siena_tx_init(
40         __in            efx_nic_t *enp);
41
42 static                  void
43 siena_tx_fini(
44         __in            efx_nic_t *enp);
45
46 static  __checkReturn   efx_rc_t
47 siena_tx_qcreate(
48         __in            efx_nic_t *enp,
49         __in            unsigned int index,
50         __in            unsigned int label,
51         __in            efsys_mem_t *esmp,
52         __in            size_t n,
53         __in            uint32_t id,
54         __in            uint16_t flags,
55         __in            efx_evq_t *eep,
56         __in            efx_txq_t *etp,
57         __out           unsigned int *addedp);
58
59 static          void
60 siena_tx_qdestroy(
61         __in    efx_txq_t *etp);
62
63 static  __checkReturn   efx_rc_t
64 siena_tx_qpost(
65         __in            efx_txq_t *etp,
66         __in_ecount(n)  efx_buffer_t *eb,
67         __in            unsigned int n,
68         __in            unsigned int completed,
69         __inout         unsigned int *addedp);
70
71 static                  void
72 siena_tx_qpush(
73         __in    efx_txq_t *etp,
74         __in    unsigned int added,
75         __in    unsigned int pushed);
76
77 static  __checkReturn   efx_rc_t
78 siena_tx_qpace(
79         __in            efx_txq_t *etp,
80         __in            unsigned int ns);
81
82 static  __checkReturn   efx_rc_t
83 siena_tx_qflush(
84         __in            efx_txq_t *etp);
85
86 static                  void
87 siena_tx_qenable(
88         __in    efx_txq_t *etp);
89
90         __checkReturn   efx_rc_t
91 siena_tx_qdesc_post(
92         __in            efx_txq_t *etp,
93         __in_ecount(n)  efx_desc_t *ed,
94         __in            unsigned int n,
95         __in            unsigned int completed,
96         __inout         unsigned int *addedp);
97
98         void
99 siena_tx_qdesc_dma_create(
100         __in    efx_txq_t *etp,
101         __in    efsys_dma_addr_t addr,
102         __in    size_t size,
103         __in    boolean_t eop,
104         __out   efx_desc_t *edp);
105
106 #endif /* EFSYS_OPT_SIENA */
107
108
109 #if EFSYS_OPT_SIENA
110 static const efx_tx_ops_t       __efx_tx_siena_ops = {
111         siena_tx_init,                          /* etxo_init */
112         siena_tx_fini,                          /* etxo_fini */
113         siena_tx_qcreate,                       /* etxo_qcreate */
114         siena_tx_qdestroy,                      /* etxo_qdestroy */
115         siena_tx_qpost,                         /* etxo_qpost */
116         siena_tx_qpush,                         /* etxo_qpush */
117         siena_tx_qpace,                         /* etxo_qpace */
118         siena_tx_qflush,                        /* etxo_qflush */
119         siena_tx_qenable,                       /* etxo_qenable */
120         NULL,                                   /* etxo_qpio_enable */
121         NULL,                                   /* etxo_qpio_disable */
122         NULL,                                   /* etxo_qpio_write */
123         NULL,                                   /* etxo_qpio_post */
124         siena_tx_qdesc_post,                    /* etxo_qdesc_post */
125         siena_tx_qdesc_dma_create,              /* etxo_qdesc_dma_create */
126         NULL,                                   /* etxo_qdesc_tso_create */
127         NULL,                                   /* etxo_qdesc_tso2_create */
128         NULL,                                   /* etxo_qdesc_vlantci_create */
129 };
130 #endif /* EFSYS_OPT_SIENA */
131
132         __checkReturn   efx_rc_t
133 efx_tx_init(
134         __in            efx_nic_t *enp)
135 {
136         const efx_tx_ops_t *etxop;
137         efx_rc_t rc;
138
139         EFSYS_ASSERT3U(enp->en_magic, ==, EFX_NIC_MAGIC);
140         EFSYS_ASSERT3U(enp->en_mod_flags, &, EFX_MOD_NIC);
141
142         if (!(enp->en_mod_flags & EFX_MOD_EV)) {
143                 rc = EINVAL;
144                 goto fail1;
145         }
146
147         if (enp->en_mod_flags & EFX_MOD_TX) {
148                 rc = EINVAL;
149                 goto fail2;
150         }
151
152         switch (enp->en_family) {
153 #if EFSYS_OPT_SIENA
154         case EFX_FAMILY_SIENA:
155                 etxop = &__efx_tx_siena_ops;
156                 break;
157 #endif /* EFSYS_OPT_SIENA */
158
159         default:
160                 EFSYS_ASSERT(0);
161                 rc = ENOTSUP;
162                 goto fail3;
163         }
164
165         EFSYS_ASSERT3U(enp->en_tx_qcount, ==, 0);
166
167         if ((rc = etxop->etxo_init(enp)) != 0)
168                 goto fail4;
169
170         enp->en_etxop = etxop;
171         enp->en_mod_flags |= EFX_MOD_TX;
172         return (0);
173
174 fail4:
175         EFSYS_PROBE(fail4);
176 fail3:
177         EFSYS_PROBE(fail3);
178 fail2:
179         EFSYS_PROBE(fail2);
180 fail1:
181         EFSYS_PROBE1(fail1, efx_rc_t, rc);
182
183         enp->en_etxop = NULL;
184         enp->en_mod_flags &= ~EFX_MOD_TX;
185         return (rc);
186 }
187
188                         void
189 efx_tx_fini(
190         __in    efx_nic_t *enp)
191 {
192         const efx_tx_ops_t *etxop = enp->en_etxop;
193
194         EFSYS_ASSERT3U(enp->en_magic, ==, EFX_NIC_MAGIC);
195         EFSYS_ASSERT3U(enp->en_mod_flags, &, EFX_MOD_NIC);
196         EFSYS_ASSERT3U(enp->en_mod_flags, &, EFX_MOD_TX);
197         EFSYS_ASSERT3U(enp->en_tx_qcount, ==, 0);
198
199         etxop->etxo_fini(enp);
200
201         enp->en_etxop = NULL;
202         enp->en_mod_flags &= ~EFX_MOD_TX;
203 }
204
205         __checkReturn   efx_rc_t
206 efx_tx_qcreate(
207         __in            efx_nic_t *enp,
208         __in            unsigned int index,
209         __in            unsigned int label,
210         __in            efsys_mem_t *esmp,
211         __in            size_t n,
212         __in            uint32_t id,
213         __in            uint16_t flags,
214         __in            efx_evq_t *eep,
215         __deref_out     efx_txq_t **etpp,
216         __out           unsigned int *addedp)
217 {
218         const efx_tx_ops_t *etxop = enp->en_etxop;
219         efx_nic_cfg_t *encp = &(enp->en_nic_cfg);
220         efx_txq_t *etp;
221         efx_rc_t rc;
222
223         EFSYS_ASSERT3U(enp->en_magic, ==, EFX_NIC_MAGIC);
224         EFSYS_ASSERT3U(enp->en_mod_flags, &, EFX_MOD_TX);
225
226         EFSYS_ASSERT3U(enp->en_tx_qcount + 1, <, encp->enc_txq_limit);
227
228         /* Allocate an TXQ object */
229         EFSYS_KMEM_ALLOC(enp->en_esip, sizeof (efx_txq_t), etp);
230
231         if (etp == NULL) {
232                 rc = ENOMEM;
233                 goto fail1;
234         }
235
236         etp->et_magic = EFX_TXQ_MAGIC;
237         etp->et_enp = enp;
238         etp->et_index = index;
239         etp->et_mask = n - 1;
240         etp->et_esmp = esmp;
241
242         /* Initial descriptor index may be modified by etxo_qcreate */
243         *addedp = 0;
244
245         if ((rc = etxop->etxo_qcreate(enp, index, label, esmp,
246             n, id, flags, eep, etp, addedp)) != 0)
247                 goto fail2;
248
249         enp->en_tx_qcount++;
250         *etpp = etp;
251
252         return (0);
253
254 fail2:
255         EFSYS_PROBE(fail2);
256         EFSYS_KMEM_FREE(enp->en_esip, sizeof (efx_txq_t), etp);
257 fail1:
258         EFSYS_PROBE1(fail1, efx_rc_t, rc);
259         return (rc);
260 }
261
262                 void
263 efx_tx_qdestroy(
264         __in    efx_txq_t *etp)
265 {
266         efx_nic_t *enp = etp->et_enp;
267         const efx_tx_ops_t *etxop = enp->en_etxop;
268
269         EFSYS_ASSERT3U(etp->et_magic, ==, EFX_TXQ_MAGIC);
270
271         EFSYS_ASSERT(enp->en_tx_qcount != 0);
272         --enp->en_tx_qcount;
273
274         etxop->etxo_qdestroy(etp);
275
276         /* Free the TXQ object */
277         EFSYS_KMEM_FREE(enp->en_esip, sizeof (efx_txq_t), etp);
278 }
279
280         __checkReturn   efx_rc_t
281 efx_tx_qpost(
282         __in            efx_txq_t *etp,
283         __in_ecount(n)  efx_buffer_t *eb,
284         __in            unsigned int n,
285         __in            unsigned int completed,
286         __inout         unsigned int *addedp)
287 {
288         efx_nic_t *enp = etp->et_enp;
289         const efx_tx_ops_t *etxop = enp->en_etxop;
290         efx_rc_t rc;
291
292         EFSYS_ASSERT3U(etp->et_magic, ==, EFX_TXQ_MAGIC);
293
294         if ((rc = etxop->etxo_qpost(etp, eb,
295             n, completed, addedp)) != 0)
296                 goto fail1;
297
298         return (0);
299
300 fail1:
301         EFSYS_PROBE1(fail1, efx_rc_t, rc);
302         return (rc);
303 }
304
305                         void
306 efx_tx_qpush(
307         __in    efx_txq_t *etp,
308         __in    unsigned int added,
309         __in    unsigned int pushed)
310 {
311         efx_nic_t *enp = etp->et_enp;
312         const efx_tx_ops_t *etxop = enp->en_etxop;
313
314         EFSYS_ASSERT3U(etp->et_magic, ==, EFX_TXQ_MAGIC);
315
316         etxop->etxo_qpush(etp, added, pushed);
317 }
318
319         __checkReturn   efx_rc_t
320 efx_tx_qpace(
321         __in            efx_txq_t *etp,
322         __in            unsigned int ns)
323 {
324         efx_nic_t *enp = etp->et_enp;
325         const efx_tx_ops_t *etxop = enp->en_etxop;
326         efx_rc_t rc;
327
328         EFSYS_ASSERT3U(etp->et_magic, ==, EFX_TXQ_MAGIC);
329
330         if ((rc = etxop->etxo_qpace(etp, ns)) != 0)
331                 goto fail1;
332
333         return (0);
334
335 fail1:
336         EFSYS_PROBE1(fail1, efx_rc_t, rc);
337         return (rc);
338 }
339
340         __checkReturn   efx_rc_t
341 efx_tx_qflush(
342         __in    efx_txq_t *etp)
343 {
344         efx_nic_t *enp = etp->et_enp;
345         const efx_tx_ops_t *etxop = enp->en_etxop;
346         efx_rc_t rc;
347
348         EFSYS_ASSERT3U(etp->et_magic, ==, EFX_TXQ_MAGIC);
349
350         if ((rc = etxop->etxo_qflush(etp)) != 0)
351                 goto fail1;
352
353         return (0);
354
355 fail1:
356         EFSYS_PROBE1(fail1, efx_rc_t, rc);
357         return (rc);
358 }
359
360                         void
361 efx_tx_qenable(
362         __in    efx_txq_t *etp)
363 {
364         efx_nic_t *enp = etp->et_enp;
365         const efx_tx_ops_t *etxop = enp->en_etxop;
366
367         EFSYS_ASSERT3U(etp->et_magic, ==, EFX_TXQ_MAGIC);
368
369         etxop->etxo_qenable(etp);
370 }
371
372         __checkReturn   efx_rc_t
373 efx_tx_qpio_enable(
374         __in    efx_txq_t *etp)
375 {
376         efx_nic_t *enp = etp->et_enp;
377         const efx_tx_ops_t *etxop = enp->en_etxop;
378         efx_rc_t rc;
379
380         EFSYS_ASSERT3U(etp->et_magic, ==, EFX_TXQ_MAGIC);
381
382         if (~enp->en_features & EFX_FEATURE_PIO_BUFFERS) {
383                 rc = ENOTSUP;
384                 goto fail1;
385         }
386         if (etxop->etxo_qpio_enable == NULL) {
387                 rc = ENOTSUP;
388                 goto fail2;
389         }
390         if ((rc = etxop->etxo_qpio_enable(etp)) != 0)
391                 goto fail3;
392
393         return (0);
394
395 fail3:
396         EFSYS_PROBE(fail3);
397 fail2:
398         EFSYS_PROBE(fail2);
399 fail1:
400         EFSYS_PROBE1(fail1, efx_rc_t, rc);
401         return (rc);
402 }
403
404                 void
405 efx_tx_qpio_disable(
406         __in    efx_txq_t *etp)
407 {
408         efx_nic_t *enp = etp->et_enp;
409         const efx_tx_ops_t *etxop = enp->en_etxop;
410
411         EFSYS_ASSERT3U(etp->et_magic, ==, EFX_TXQ_MAGIC);
412
413         if (etxop->etxo_qpio_disable != NULL)
414                 etxop->etxo_qpio_disable(etp);
415 }
416
417         __checkReturn   efx_rc_t
418 efx_tx_qpio_write(
419         __in                    efx_txq_t *etp,
420         __in_ecount(buf_length) uint8_t *buffer,
421         __in                    size_t buf_length,
422         __in                    size_t pio_buf_offset)
423 {
424         efx_nic_t *enp = etp->et_enp;
425         const efx_tx_ops_t *etxop = enp->en_etxop;
426         efx_rc_t rc;
427
428         EFSYS_ASSERT3U(etp->et_magic, ==, EFX_TXQ_MAGIC);
429
430         if (etxop->etxo_qpio_write != NULL) {
431                 if ((rc = etxop->etxo_qpio_write(etp, buffer, buf_length,
432                                                 pio_buf_offset)) != 0)
433                         goto fail1;
434                 return (0);
435         }
436
437         return (ENOTSUP);
438
439 fail1:
440         EFSYS_PROBE1(fail1, efx_rc_t, rc);
441         return (rc);
442 }
443
444         __checkReturn   efx_rc_t
445 efx_tx_qpio_post(
446         __in                    efx_txq_t *etp,
447         __in                    size_t pkt_length,
448         __in                    unsigned int completed,
449         __inout                 unsigned int *addedp)
450 {
451         efx_nic_t *enp = etp->et_enp;
452         const efx_tx_ops_t *etxop = enp->en_etxop;
453         efx_rc_t rc;
454
455         EFSYS_ASSERT3U(etp->et_magic, ==, EFX_TXQ_MAGIC);
456
457         if (etxop->etxo_qpio_post != NULL) {
458                 if ((rc = etxop->etxo_qpio_post(etp, pkt_length, completed,
459                                                 addedp)) != 0)
460                         goto fail1;
461                 return (0);
462         }
463
464         return (ENOTSUP);
465
466 fail1:
467         EFSYS_PROBE1(fail1, efx_rc_t, rc);
468         return (rc);
469 }
470
471         __checkReturn   efx_rc_t
472 efx_tx_qdesc_post(
473         __in            efx_txq_t *etp,
474         __in_ecount(n)  efx_desc_t *ed,
475         __in            unsigned int n,
476         __in            unsigned int completed,
477         __inout         unsigned int *addedp)
478 {
479         efx_nic_t *enp = etp->et_enp;
480         const efx_tx_ops_t *etxop = enp->en_etxop;
481         efx_rc_t rc;
482
483         EFSYS_ASSERT3U(etp->et_magic, ==, EFX_TXQ_MAGIC);
484
485         if ((rc = etxop->etxo_qdesc_post(etp, ed,
486             n, completed, addedp)) != 0)
487                 goto fail1;
488
489         return (0);
490
491 fail1:
492         EFSYS_PROBE1(fail1, efx_rc_t, rc);
493         return (rc);
494 }
495
496         void
497 efx_tx_qdesc_dma_create(
498         __in    efx_txq_t *etp,
499         __in    efsys_dma_addr_t addr,
500         __in    size_t size,
501         __in    boolean_t eop,
502         __out   efx_desc_t *edp)
503 {
504         efx_nic_t *enp = etp->et_enp;
505         const efx_tx_ops_t *etxop = enp->en_etxop;
506
507         EFSYS_ASSERT3U(etp->et_magic, ==, EFX_TXQ_MAGIC);
508         EFSYS_ASSERT(etxop->etxo_qdesc_dma_create != NULL);
509
510         etxop->etxo_qdesc_dma_create(etp, addr, size, eop, edp);
511 }
512
513         void
514 efx_tx_qdesc_tso_create(
515         __in    efx_txq_t *etp,
516         __in    uint16_t ipv4_id,
517         __in    uint32_t tcp_seq,
518         __in    uint8_t  tcp_flags,
519         __out   efx_desc_t *edp)
520 {
521         efx_nic_t *enp = etp->et_enp;
522         const efx_tx_ops_t *etxop = enp->en_etxop;
523
524         EFSYS_ASSERT3U(etp->et_magic, ==, EFX_TXQ_MAGIC);
525         EFSYS_ASSERT(etxop->etxo_qdesc_tso_create != NULL);
526
527         etxop->etxo_qdesc_tso_create(etp, ipv4_id, tcp_seq, tcp_flags, edp);
528 }
529
530         void
531 efx_tx_qdesc_tso2_create(
532         __in                    efx_txq_t *etp,
533         __in                    uint16_t ipv4_id,
534         __in                    uint32_t tcp_seq,
535         __in                    uint16_t mss,
536         __out_ecount(count)     efx_desc_t *edp,
537         __in                    int count)
538 {
539         efx_nic_t *enp = etp->et_enp;
540         const efx_tx_ops_t *etxop = enp->en_etxop;
541
542         EFSYS_ASSERT3U(etp->et_magic, ==, EFX_TXQ_MAGIC);
543         EFSYS_ASSERT(etxop->etxo_qdesc_tso2_create != NULL);
544
545         etxop->etxo_qdesc_tso2_create(etp, ipv4_id, tcp_seq, mss, edp, count);
546 }
547
548         void
549 efx_tx_qdesc_vlantci_create(
550         __in    efx_txq_t *etp,
551         __in    uint16_t tci,
552         __out   efx_desc_t *edp)
553 {
554         efx_nic_t *enp = etp->et_enp;
555         const efx_tx_ops_t *etxop = enp->en_etxop;
556
557         EFSYS_ASSERT3U(etp->et_magic, ==, EFX_TXQ_MAGIC);
558         EFSYS_ASSERT(etxop->etxo_qdesc_vlantci_create != NULL);
559
560         etxop->etxo_qdesc_vlantci_create(etp, tci, edp);
561 }
562
563
564 #if EFSYS_OPT_SIENA
565
566 static  __checkReturn   efx_rc_t
567 siena_tx_init(
568         __in            efx_nic_t *enp)
569 {
570         efx_oword_t oword;
571
572         /*
573          * Disable the timer-based TX DMA backoff and allow TX DMA to be
574          * controlled by the RX FIFO fill level (although always allow a
575          * minimal trickle).
576          */
577         EFX_BAR_READO(enp, FR_AZ_TX_RESERVED_REG, &oword);
578         EFX_SET_OWORD_FIELD(oword, FRF_AZ_TX_RX_SPACER, 0xfe);
579         EFX_SET_OWORD_FIELD(oword, FRF_AZ_TX_RX_SPACER_EN, 1);
580         EFX_SET_OWORD_FIELD(oword, FRF_AZ_TX_ONE_PKT_PER_Q, 1);
581         EFX_SET_OWORD_FIELD(oword, FRF_AZ_TX_PUSH_EN, 0);
582         EFX_SET_OWORD_FIELD(oword, FRF_AZ_TX_DIS_NON_IP_EV, 1);
583         EFX_SET_OWORD_FIELD(oword, FRF_AZ_TX_PREF_THRESHOLD, 2);
584         EFX_SET_OWORD_FIELD(oword, FRF_AZ_TX_PREF_WD_TMR, 0x3fffff);
585
586         /*
587          * Filter all packets less than 14 bytes to avoid parsing
588          * errors.
589          */
590         EFX_SET_OWORD_FIELD(oword, FRF_BZ_TX_FLUSH_MIN_LEN_EN, 1);
591         EFX_BAR_WRITEO(enp, FR_AZ_TX_RESERVED_REG, &oword);
592
593         /*
594          * Do not set TX_NO_EOP_DISC_EN, since it limits packets to 16
595          * descriptors (which is bad).
596          */
597         EFX_BAR_READO(enp, FR_AZ_TX_CFG_REG, &oword);
598         EFX_SET_OWORD_FIELD(oword, FRF_AZ_TX_NO_EOP_DISC_EN, 0);
599         EFX_BAR_WRITEO(enp, FR_AZ_TX_CFG_REG, &oword);
600
601         return (0);
602 }
603
604 #define EFX_TX_DESC(_etp, _addr, _size, _eop, _added)                   \
605         do {                                                            \
606                 unsigned int id;                                        \
607                 size_t offset;                                          \
608                 efx_qword_t qword;                                      \
609                                                                         \
610                 id = (_added)++ & (_etp)->et_mask;                      \
611                 offset = id * sizeof (efx_qword_t);                     \
612                                                                         \
613                 EFSYS_PROBE5(tx_post, unsigned int, (_etp)->et_index,   \
614                     unsigned int, id, efsys_dma_addr_t, (_addr),        \
615                     size_t, (_size), boolean_t, (_eop));                \
616                                                                         \
617                 EFX_POPULATE_QWORD_4(qword,                             \
618                     FSF_AZ_TX_KER_CONT, (_eop) ? 0 : 1,                 \
619                     FSF_AZ_TX_KER_BYTE_COUNT, (uint32_t)(_size),        \
620                     FSF_AZ_TX_KER_BUF_ADDR_DW0,                         \
621                     (uint32_t)((_addr) & 0xffffffff),                   \
622                     FSF_AZ_TX_KER_BUF_ADDR_DW1,                         \
623                     (uint32_t)((_addr) >> 32));                         \
624                 EFSYS_MEM_WRITEQ((_etp)->et_esmp, offset, &qword);      \
625                                                                         \
626                 _NOTE(CONSTANTCONDITION)                                \
627         } while (B_FALSE)
628
629 static  __checkReturn   efx_rc_t
630 siena_tx_qpost(
631         __in            efx_txq_t *etp,
632         __in_ecount(n)  efx_buffer_t *eb,
633         __in            unsigned int n,
634         __in            unsigned int completed,
635         __inout         unsigned int *addedp)
636 {
637         unsigned int added = *addedp;
638         unsigned int i;
639         int rc = ENOSPC;
640
641         if (added - completed + n > EFX_TXQ_LIMIT(etp->et_mask + 1))
642                 goto fail1;
643
644         for (i = 0; i < n; i++) {
645                 efx_buffer_t *ebp = &eb[i];
646                 efsys_dma_addr_t start = ebp->eb_addr;
647                 size_t size = ebp->eb_size;
648                 efsys_dma_addr_t end = start + size;
649
650                 /* Fragments must not span 4k boundaries. */
651                 EFSYS_ASSERT(P2ROUNDUP(start + 1, 4096) >= end);
652
653                 EFX_TX_DESC(etp, start, size, ebp->eb_eop, added);
654         }
655
656         EFX_TX_QSTAT_INCR(etp, TX_POST);
657
658         *addedp = added;
659         return (0);
660
661 fail1:
662         EFSYS_PROBE1(fail1, efx_rc_t, rc);
663
664         return (rc);
665 }
666
667 static          void
668 siena_tx_qpush(
669         __in    efx_txq_t *etp,
670         __in    unsigned int added,
671         __in    unsigned int pushed)
672 {
673         efx_nic_t *enp = etp->et_enp;
674         uint32_t wptr;
675         efx_dword_t dword;
676         efx_oword_t oword;
677
678         /* Push the populated descriptors out */
679         wptr = added & etp->et_mask;
680
681         EFX_POPULATE_OWORD_1(oword, FRF_AZ_TX_DESC_WPTR, wptr);
682
683         /* Only write the third DWORD */
684         EFX_POPULATE_DWORD_1(dword,
685             EFX_DWORD_0, EFX_OWORD_FIELD(oword, EFX_DWORD_3));
686
687         /* Guarantee ordering of memory (descriptors) and PIO (doorbell) */
688         EFX_DMA_SYNC_QUEUE_FOR_DEVICE(etp->et_esmp, etp->et_mask + 1,
689             wptr, pushed & etp->et_mask);
690         EFSYS_PIO_WRITE_BARRIER();
691         EFX_BAR_TBL_WRITED3(enp, FR_BZ_TX_DESC_UPD_REGP0,
692                             etp->et_index, &dword, B_FALSE);
693 }
694
695 #define EFX_MAX_PACE_VALUE 20
696
697 static  __checkReturn   efx_rc_t
698 siena_tx_qpace(
699         __in            efx_txq_t *etp,
700         __in            unsigned int ns)
701 {
702         efx_nic_t *enp = etp->et_enp;
703         efx_nic_cfg_t *encp = &(enp->en_nic_cfg);
704         efx_oword_t oword;
705         unsigned int pace_val;
706         unsigned int timer_period;
707         efx_rc_t rc;
708
709         if (ns == 0) {
710                 pace_val = 0;
711         } else {
712                 /*
713                  * The pace_val to write into the table is s.t
714                  * ns <= timer_period * (2 ^ pace_val)
715                  */
716                 timer_period = 104 / encp->enc_clk_mult;
717                 for (pace_val = 1; pace_val <= EFX_MAX_PACE_VALUE; pace_val++) {
718                         if ((timer_period << pace_val) >= ns)
719                                 break;
720                 }
721         }
722         if (pace_val > EFX_MAX_PACE_VALUE) {
723                 rc = EINVAL;
724                 goto fail1;
725         }
726
727         /* Update the pacing table */
728         EFX_POPULATE_OWORD_1(oword, FRF_AZ_TX_PACE, pace_val);
729         EFX_BAR_TBL_WRITEO(enp, FR_AZ_TX_PACE_TBL, etp->et_index,
730             &oword, B_TRUE);
731
732         return (0);
733
734 fail1:
735         EFSYS_PROBE1(fail1, efx_rc_t, rc);
736
737         return (rc);
738 }
739
740 static  __checkReturn   efx_rc_t
741 siena_tx_qflush(
742         __in            efx_txq_t *etp)
743 {
744         efx_nic_t *enp = etp->et_enp;
745         efx_oword_t oword;
746         uint32_t label;
747
748         efx_tx_qpace(etp, 0);
749
750         label = etp->et_index;
751
752         /* Flush the queue */
753         EFX_POPULATE_OWORD_2(oword, FRF_AZ_TX_FLUSH_DESCQ_CMD, 1,
754             FRF_AZ_TX_FLUSH_DESCQ, label);
755         EFX_BAR_WRITEO(enp, FR_AZ_TX_FLUSH_DESCQ_REG, &oword);
756
757         return (0);
758 }
759
760 static          void
761 siena_tx_qenable(
762         __in    efx_txq_t *etp)
763 {
764         efx_nic_t *enp = etp->et_enp;
765         efx_oword_t oword;
766
767         EFX_BAR_TBL_READO(enp, FR_AZ_TX_DESC_PTR_TBL,
768                             etp->et_index, &oword, B_TRUE);
769
770         EFSYS_PROBE5(tx_descq_ptr, unsigned int, etp->et_index,
771             uint32_t, EFX_OWORD_FIELD(oword, EFX_DWORD_3),
772             uint32_t, EFX_OWORD_FIELD(oword, EFX_DWORD_2),
773             uint32_t, EFX_OWORD_FIELD(oword, EFX_DWORD_1),
774             uint32_t, EFX_OWORD_FIELD(oword, EFX_DWORD_0));
775
776         EFX_SET_OWORD_FIELD(oword, FRF_AZ_TX_DC_HW_RPTR, 0);
777         EFX_SET_OWORD_FIELD(oword, FRF_AZ_TX_DESCQ_HW_RPTR, 0);
778         EFX_SET_OWORD_FIELD(oword, FRF_AZ_TX_DESCQ_EN, 1);
779
780         EFX_BAR_TBL_WRITEO(enp, FR_AZ_TX_DESC_PTR_TBL,
781                             etp->et_index, &oword, B_TRUE);
782 }
783
784 static  __checkReturn   efx_rc_t
785 siena_tx_qcreate(
786         __in            efx_nic_t *enp,
787         __in            unsigned int index,
788         __in            unsigned int label,
789         __in            efsys_mem_t *esmp,
790         __in            size_t n,
791         __in            uint32_t id,
792         __in            uint16_t flags,
793         __in            efx_evq_t *eep,
794         __in            efx_txq_t *etp,
795         __out           unsigned int *addedp)
796 {
797         efx_nic_cfg_t *encp = &(enp->en_nic_cfg);
798         efx_oword_t oword;
799         uint32_t size;
800         efx_rc_t rc;
801
802         _NOTE(ARGUNUSED(esmp))
803
804         EFX_STATIC_ASSERT(EFX_EV_TX_NLABELS ==
805             (1 << FRF_AZ_TX_DESCQ_LABEL_WIDTH));
806         EFSYS_ASSERT3U(label, <, EFX_EV_TX_NLABELS);
807
808         EFSYS_ASSERT(ISP2(encp->enc_txq_max_ndescs));
809         EFX_STATIC_ASSERT(ISP2(EFX_TXQ_MINNDESCS));
810
811         if (!ISP2(n) || (n < EFX_TXQ_MINNDESCS) || (n > EFX_EVQ_MAXNEVS)) {
812                 rc = EINVAL;
813                 goto fail1;
814         }
815         if (index >= encp->enc_txq_limit) {
816                 rc = EINVAL;
817                 goto fail2;
818         }
819         for (size = 0;
820             (1 << size) <= (int)(encp->enc_txq_max_ndescs / EFX_TXQ_MINNDESCS);
821             size++)
822                 if ((1 << size) == (int)(n / EFX_TXQ_MINNDESCS))
823                         break;
824         if (id + (1 << size) >= encp->enc_buftbl_limit) {
825                 rc = EINVAL;
826                 goto fail3;
827         }
828
829         /* Set up the new descriptor queue */
830         *addedp = 0;
831
832         EFX_POPULATE_OWORD_6(oword,
833             FRF_AZ_TX_DESCQ_BUF_BASE_ID, id,
834             FRF_AZ_TX_DESCQ_EVQ_ID, eep->ee_index,
835             FRF_AZ_TX_DESCQ_OWNER_ID, 0,
836             FRF_AZ_TX_DESCQ_LABEL, label,
837             FRF_AZ_TX_DESCQ_SIZE, size,
838             FRF_AZ_TX_DESCQ_TYPE, 0);
839
840         EFX_SET_OWORD_FIELD(oword, FRF_BZ_TX_NON_IP_DROP_DIS, 1);
841         EFX_SET_OWORD_FIELD(oword, FRF_BZ_TX_IP_CHKSM_DIS,
842             (flags & EFX_TXQ_CKSUM_IPV4) ? 0 : 1);
843         EFX_SET_OWORD_FIELD(oword, FRF_BZ_TX_TCP_CHKSM_DIS,
844             (flags & EFX_TXQ_CKSUM_TCPUDP) ? 0 : 1);
845
846         EFX_BAR_TBL_WRITEO(enp, FR_AZ_TX_DESC_PTR_TBL,
847             etp->et_index, &oword, B_TRUE);
848
849         return (0);
850
851 fail3:
852         EFSYS_PROBE(fail3);
853 fail2:
854         EFSYS_PROBE(fail2);
855 fail1:
856         EFSYS_PROBE1(fail1, efx_rc_t, rc);
857
858         return (rc);
859 }
860
861         __checkReturn   efx_rc_t
862 siena_tx_qdesc_post(
863         __in            efx_txq_t *etp,
864         __in_ecount(n)  efx_desc_t *ed,
865         __in            unsigned int n,
866         __in            unsigned int completed,
867         __inout         unsigned int *addedp)
868 {
869         unsigned int added = *addedp;
870         unsigned int i;
871         efx_rc_t rc;
872
873         if (added - completed + n > EFX_TXQ_LIMIT(etp->et_mask + 1)) {
874                 rc = ENOSPC;
875                 goto fail1;
876         }
877
878         for (i = 0; i < n; i++) {
879                 efx_desc_t *edp = &ed[i];
880                 unsigned int id;
881                 size_t offset;
882
883                 id = added++ & etp->et_mask;
884                 offset = id * sizeof (efx_desc_t);
885
886                 EFSYS_MEM_WRITEQ(etp->et_esmp, offset, &edp->ed_eq);
887         }
888
889         EFSYS_PROBE3(tx_desc_post, unsigned int, etp->et_index,
890                     unsigned int, added, unsigned int, n);
891
892         EFX_TX_QSTAT_INCR(etp, TX_POST);
893
894         *addedp = added;
895         return (0);
896
897 fail1:
898         EFSYS_PROBE1(fail1, efx_rc_t, rc);
899         return (rc);
900 }
901
902         void
903 siena_tx_qdesc_dma_create(
904         __in    efx_txq_t *etp,
905         __in    efsys_dma_addr_t addr,
906         __in    size_t size,
907         __in    boolean_t eop,
908         __out   efx_desc_t *edp)
909 {
910         /* Fragments must not span 4k boundaries. */
911         EFSYS_ASSERT(P2ROUNDUP(addr + 1, 4096) >= addr + size);
912
913         EFSYS_PROBE4(tx_desc_dma_create, unsigned int, etp->et_index,
914                     efsys_dma_addr_t, addr,
915                     size_t, size, boolean_t, eop);
916
917         EFX_POPULATE_QWORD_4(edp->ed_eq,
918                             FSF_AZ_TX_KER_CONT, eop ? 0 : 1,
919                             FSF_AZ_TX_KER_BYTE_COUNT, (uint32_t)size,
920                             FSF_AZ_TX_KER_BUF_ADDR_DW0,
921                             (uint32_t)(addr & 0xffffffff),
922                             FSF_AZ_TX_KER_BUF_ADDR_DW1,
923                             (uint32_t)(addr >> 32));
924 }
925
926 #endif /* EFSYS_OPT_SIENA */
927
928 #if EFSYS_OPT_SIENA
929
930 static          void
931 siena_tx_qdestroy(
932         __in    efx_txq_t *etp)
933 {
934         efx_nic_t *enp = etp->et_enp;
935         efx_oword_t oword;
936
937         /* Purge descriptor queue */
938         EFX_ZERO_OWORD(oword);
939
940         EFX_BAR_TBL_WRITEO(enp, FR_AZ_TX_DESC_PTR_TBL,
941                             etp->et_index, &oword, B_TRUE);
942 }
943
944 static          void
945 siena_tx_fini(
946         __in    efx_nic_t *enp)
947 {
948         _NOTE(ARGUNUSED(enp))
949 }
950
951 #endif /* EFSYS_OPT_SIENA */