fm10k: add Tx queue setup/release
[dpdk.git] / lib / librte_pmd_fm10k / fm10k_ethdev.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2013-2015 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #include <rte_ethdev.h>
35 #include <rte_malloc.h>
36 #include <rte_memzone.h>
37 #include <rte_string_fns.h>
38 #include <rte_dev.h>
39 #include <rte_spinlock.h>
40
41 #include "fm10k.h"
42 #include "base/fm10k_api.h"
43
44 #define FM10K_RX_BUFF_ALIGN 512
45 /* Default delay to acquire mailbox lock */
46 #define FM10K_MBXLOCK_DELAY_US 20
47
48 /* Number of chars per uint32 type */
49 #define CHARS_PER_UINT32 (sizeof(uint32_t))
50 #define BIT_MASK_PER_UINT32 ((1 << CHARS_PER_UINT32) - 1)
51
52 static void
53 fm10k_mbx_initlock(struct fm10k_hw *hw)
54 {
55         rte_spinlock_init(FM10K_DEV_PRIVATE_TO_MBXLOCK(hw->back));
56 }
57
58 static void
59 fm10k_mbx_lock(struct fm10k_hw *hw)
60 {
61         while (!rte_spinlock_trylock(FM10K_DEV_PRIVATE_TO_MBXLOCK(hw->back)))
62                 rte_delay_us(FM10K_MBXLOCK_DELAY_US);
63 }
64
65 static void
66 fm10k_mbx_unlock(struct fm10k_hw *hw)
67 {
68         rte_spinlock_unlock(FM10K_DEV_PRIVATE_TO_MBXLOCK(hw->back));
69 }
70
71 /*
72  * clean queue, descriptor rings, free software buffers used when stopping
73  * device.
74  */
75 static inline void
76 rx_queue_clean(struct fm10k_rx_queue *q)
77 {
78         union fm10k_rx_desc zero = {.q = {0, 0, 0, 0} };
79         uint32_t i;
80         PMD_INIT_FUNC_TRACE();
81
82         /* zero descriptor rings */
83         for (i = 0; i < q->nb_desc; ++i)
84                 q->hw_ring[i] = zero;
85
86         /* free software buffers */
87         for (i = 0; i < q->nb_desc; ++i) {
88                 if (q->sw_ring[i]) {
89                         rte_pktmbuf_free_seg(q->sw_ring[i]);
90                         q->sw_ring[i] = NULL;
91                 }
92         }
93 }
94
95 /*
96  * free all queue memory used when releasing the queue (i.e. configure)
97  */
98 static inline void
99 rx_queue_free(struct fm10k_rx_queue *q)
100 {
101         PMD_INIT_FUNC_TRACE();
102         if (q) {
103                 PMD_INIT_LOG(DEBUG, "Freeing rx queue %p", q);
104                 rx_queue_clean(q);
105                 if (q->sw_ring)
106                         rte_free(q->sw_ring);
107                 rte_free(q);
108         }
109 }
110
111 /*
112  * clean queue, descriptor rings, free software buffers used when stopping
113  * device
114  */
115 static inline void
116 tx_queue_clean(struct fm10k_tx_queue *q)
117 {
118         struct fm10k_tx_desc zero = {0, 0, 0, 0, 0, 0};
119         uint32_t i;
120         PMD_INIT_FUNC_TRACE();
121
122         /* zero descriptor rings */
123         for (i = 0; i < q->nb_desc; ++i)
124                 q->hw_ring[i] = zero;
125
126         /* free software buffers */
127         for (i = 0; i < q->nb_desc; ++i) {
128                 if (q->sw_ring[i]) {
129                         rte_pktmbuf_free_seg(q->sw_ring[i]);
130                         q->sw_ring[i] = NULL;
131                 }
132         }
133 }
134
135 /*
136  * free all queue memory used when releasing the queue (i.e. configure)
137  */
138 static inline void
139 tx_queue_free(struct fm10k_tx_queue *q)
140 {
141         PMD_INIT_FUNC_TRACE();
142         if (q) {
143                 PMD_INIT_LOG(DEBUG, "Freeing tx queue %p", q);
144                 tx_queue_clean(q);
145                 if (q->rs_tracker.list)
146                         rte_free(q->rs_tracker.list);
147                 if (q->sw_ring)
148                         rte_free(q->sw_ring);
149                 rte_free(q);
150         }
151 }
152
153 static int
154 fm10k_dev_configure(struct rte_eth_dev *dev)
155 {
156         PMD_INIT_FUNC_TRACE();
157
158         if (dev->data->dev_conf.rxmode.hw_strip_crc == 0)
159                 PMD_INIT_LOG(WARNING, "fm10k always strip CRC");
160
161         return 0;
162 }
163
164 static int
165 fm10k_link_update(struct rte_eth_dev *dev,
166         __rte_unused int wait_to_complete)
167 {
168         PMD_INIT_FUNC_TRACE();
169
170         /* The host-interface link is always up.  The speed is ~50Gbps per Gen3
171          * x8 PCIe interface. For now, we leave the speed undefined since there
172          * is no 50Gbps Ethernet. */
173         dev->data->dev_link.link_speed  = 0;
174         dev->data->dev_link.link_duplex = ETH_LINK_FULL_DUPLEX;
175         dev->data->dev_link.link_status = 1;
176
177         return 0;
178 }
179
180 static void
181 fm10k_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
182 {
183         uint64_t ipackets, opackets, ibytes, obytes;
184         struct fm10k_hw *hw =
185                 FM10K_DEV_PRIVATE_TO_HW(dev->data->dev_private);
186         struct fm10k_hw_stats *hw_stats =
187                 FM10K_DEV_PRIVATE_TO_STATS(dev->data->dev_private);
188         int i;
189
190         PMD_INIT_FUNC_TRACE();
191
192         fm10k_update_hw_stats(hw, hw_stats);
193
194         ipackets = opackets = ibytes = obytes = 0;
195         for (i = 0; (i < RTE_ETHDEV_QUEUE_STAT_CNTRS) &&
196                 (i < FM10K_MAX_QUEUES_PF); ++i) {
197                 stats->q_ipackets[i] = hw_stats->q[i].rx_packets.count;
198                 stats->q_opackets[i] = hw_stats->q[i].tx_packets.count;
199                 stats->q_ibytes[i]   = hw_stats->q[i].rx_bytes.count;
200                 stats->q_obytes[i]   = hw_stats->q[i].tx_bytes.count;
201                 ipackets += stats->q_ipackets[i];
202                 opackets += stats->q_opackets[i];
203                 ibytes   += stats->q_ibytes[i];
204                 obytes   += stats->q_obytes[i];
205         }
206         stats->ipackets = ipackets;
207         stats->opackets = opackets;
208         stats->ibytes = ibytes;
209         stats->obytes = obytes;
210 }
211
212 static void
213 fm10k_stats_reset(struct rte_eth_dev *dev)
214 {
215         struct fm10k_hw *hw = FM10K_DEV_PRIVATE_TO_HW(dev->data->dev_private);
216         struct fm10k_hw_stats *hw_stats =
217                 FM10K_DEV_PRIVATE_TO_STATS(dev->data->dev_private);
218
219         PMD_INIT_FUNC_TRACE();
220
221         memset(hw_stats, 0, sizeof(*hw_stats));
222         fm10k_rebind_hw_stats(hw, hw_stats);
223 }
224
225 static void
226 fm10k_dev_infos_get(struct rte_eth_dev *dev,
227         struct rte_eth_dev_info *dev_info)
228 {
229         struct fm10k_hw *hw = FM10K_DEV_PRIVATE_TO_HW(dev->data->dev_private);
230
231         PMD_INIT_FUNC_TRACE();
232
233         dev_info->min_rx_bufsize     = FM10K_MIN_RX_BUF_SIZE;
234         dev_info->max_rx_pktlen      = FM10K_MAX_PKT_SIZE;
235         dev_info->max_rx_queues      = hw->mac.max_queues;
236         dev_info->max_tx_queues      = hw->mac.max_queues;
237         dev_info->max_mac_addrs      = 1;
238         dev_info->max_hash_mac_addrs = 0;
239         dev_info->max_vfs            = FM10K_MAX_VF_NUM;
240         dev_info->max_vmdq_pools     = ETH_64_POOLS;
241         dev_info->rx_offload_capa =
242                 DEV_RX_OFFLOAD_IPV4_CKSUM |
243                 DEV_RX_OFFLOAD_UDP_CKSUM  |
244                 DEV_RX_OFFLOAD_TCP_CKSUM;
245         dev_info->tx_offload_capa    = 0;
246         dev_info->reta_size = FM10K_MAX_RSS_INDICES;
247
248         dev_info->default_rxconf = (struct rte_eth_rxconf) {
249                 .rx_thresh = {
250                         .pthresh = FM10K_DEFAULT_RX_PTHRESH,
251                         .hthresh = FM10K_DEFAULT_RX_HTHRESH,
252                         .wthresh = FM10K_DEFAULT_RX_WTHRESH,
253                 },
254                 .rx_free_thresh = FM10K_RX_FREE_THRESH_DEFAULT(0),
255                 .rx_drop_en = 0,
256         };
257
258         dev_info->default_txconf = (struct rte_eth_txconf) {
259                 .tx_thresh = {
260                         .pthresh = FM10K_DEFAULT_TX_PTHRESH,
261                         .hthresh = FM10K_DEFAULT_TX_HTHRESH,
262                         .wthresh = FM10K_DEFAULT_TX_WTHRESH,
263                 },
264                 .tx_free_thresh = FM10K_TX_FREE_THRESH_DEFAULT(0),
265                 .tx_rs_thresh = FM10K_TX_RS_THRESH_DEFAULT(0),
266                 .txq_flags = ETH_TXQ_FLAGS_NOMULTSEGS |
267                                 ETH_TXQ_FLAGS_NOOFFLOADS,
268         };
269
270 }
271
272 static inline int
273 check_nb_desc(uint16_t min, uint16_t max, uint16_t mult, uint16_t request)
274 {
275         if ((request < min) || (request > max) || ((request % mult) != 0))
276                 return -1;
277         else
278                 return 0;
279 }
280
281 /*
282  * Create a memzone for hardware descriptor rings. Malloc cannot be used since
283  * the physical address is required. If the memzone is already created, then
284  * this function returns a pointer to the existing memzone.
285  */
286 static inline const struct rte_memzone *
287 allocate_hw_ring(const char *driver_name, const char *ring_name,
288         uint8_t port_id, uint16_t queue_id, int socket_id,
289         uint32_t size, uint32_t align)
290 {
291         char name[RTE_MEMZONE_NAMESIZE];
292         const struct rte_memzone *mz;
293
294         snprintf(name, sizeof(name), "%s_%s_%d_%d_%d",
295                  driver_name, ring_name, port_id, queue_id, socket_id);
296
297         /* return the memzone if it already exists */
298         mz = rte_memzone_lookup(name);
299         if (mz)
300                 return mz;
301
302 #ifdef RTE_LIBRTE_XEN_DOM0
303         return rte_memzone_reserve_bounded(name, size, socket_id, 0, align,
304                                            RTE_PGSIZE_2M);
305 #else
306         return rte_memzone_reserve_aligned(name, size, socket_id, 0, align);
307 #endif
308 }
309
310 static inline int
311 check_thresh(uint16_t min, uint16_t max, uint16_t div, uint16_t request)
312 {
313         if ((request < min) || (request > max) || ((div % request) != 0))
314                 return -1;
315         else
316                 return 0;
317 }
318
319 static inline int
320 handle_rxconf(struct fm10k_rx_queue *q, const struct rte_eth_rxconf *conf)
321 {
322         uint16_t rx_free_thresh;
323
324         if (conf->rx_free_thresh == 0)
325                 rx_free_thresh = FM10K_RX_FREE_THRESH_DEFAULT(q);
326         else
327                 rx_free_thresh = conf->rx_free_thresh;
328
329         /* make sure the requested threshold satisfies the constraints */
330         if (check_thresh(FM10K_RX_FREE_THRESH_MIN(q),
331                         FM10K_RX_FREE_THRESH_MAX(q),
332                         FM10K_RX_FREE_THRESH_DIV(q),
333                         rx_free_thresh)) {
334                 PMD_INIT_LOG(ERR, "rx_free_thresh (%u) must be "
335                         "less than or equal to %u, "
336                         "greater than or equal to %u, "
337                         "and a divisor of %u",
338                         rx_free_thresh, FM10K_RX_FREE_THRESH_MAX(q),
339                         FM10K_RX_FREE_THRESH_MIN(q),
340                         FM10K_RX_FREE_THRESH_DIV(q));
341                 return (-EINVAL);
342         }
343
344         q->alloc_thresh = rx_free_thresh;
345         q->drop_en = conf->rx_drop_en;
346         q->rx_deferred_start = conf->rx_deferred_start;
347
348         return 0;
349 }
350
351 /*
352  * Hardware requires specific alignment for Rx packet buffers. At
353  * least one of the following two conditions must be satisfied.
354  *  1. Address is 512B aligned
355  *  2. Address is 8B aligned and buffer does not cross 4K boundary.
356  *
357  * As such, the driver may need to adjust the DMA address within the
358  * buffer by up to 512B. The mempool element size is checked here
359  * to make sure a maximally sized Ethernet frame can still be wholly
360  * contained within the buffer after 512B alignment.
361  *
362  * return 1 if the element size is valid, otherwise return 0.
363  */
364 static int
365 mempool_element_size_valid(struct rte_mempool *mp)
366 {
367         uint32_t min_size;
368
369         /* elt_size includes mbuf header and headroom */
370         min_size = mp->elt_size - sizeof(struct rte_mbuf) -
371                         RTE_PKTMBUF_HEADROOM;
372
373         /* account for up to 512B of alignment */
374         min_size -= FM10K_RX_BUFF_ALIGN;
375
376         /* sanity check for overflow */
377         if (min_size > mp->elt_size)
378                 return 0;
379
380         if (min_size < ETHER_MAX_VLAN_FRAME_LEN)
381                 return 0;
382
383         /* size is valid */
384         return 1;
385 }
386
387 static int
388 fm10k_rx_queue_setup(struct rte_eth_dev *dev, uint16_t queue_id,
389         uint16_t nb_desc, unsigned int socket_id,
390         const struct rte_eth_rxconf *conf, struct rte_mempool *mp)
391 {
392         struct fm10k_hw *hw = FM10K_DEV_PRIVATE_TO_HW(dev->data->dev_private);
393         struct fm10k_rx_queue *q;
394         const struct rte_memzone *mz;
395
396         PMD_INIT_FUNC_TRACE();
397
398         /* make sure the mempool element size can account for alignment. */
399         if (!mempool_element_size_valid(mp)) {
400                 PMD_INIT_LOG(ERR, "Error : Mempool element size is too small");
401                 return (-EINVAL);
402         }
403
404         /* make sure a valid number of descriptors have been requested */
405         if (check_nb_desc(FM10K_MIN_RX_DESC, FM10K_MAX_RX_DESC,
406                                 FM10K_MULT_RX_DESC, nb_desc)) {
407                 PMD_INIT_LOG(ERR, "Number of Rx descriptors (%u) must be "
408                         "less than or equal to %"PRIu32", "
409                         "greater than or equal to %u, "
410                         "and a multiple of %u",
411                         nb_desc, (uint32_t)FM10K_MAX_RX_DESC, FM10K_MIN_RX_DESC,
412                         FM10K_MULT_RX_DESC);
413                 return (-EINVAL);
414         }
415
416         /*
417          * if this queue existed already, free the associated memory. The
418          * queue cannot be reused in case we need to allocate memory on
419          * different socket than was previously used.
420          */
421         if (dev->data->rx_queues[queue_id] != NULL) {
422                 rx_queue_free(dev->data->rx_queues[queue_id]);
423                 dev->data->rx_queues[queue_id] = NULL;
424         }
425
426         /* allocate memory for the queue structure */
427         q = rte_zmalloc_socket("fm10k", sizeof(*q), RTE_CACHE_LINE_SIZE,
428                                 socket_id);
429         if (q == NULL) {
430                 PMD_INIT_LOG(ERR, "Cannot allocate queue structure");
431                 return (-ENOMEM);
432         }
433
434         /* setup queue */
435         q->mp = mp;
436         q->nb_desc = nb_desc;
437         q->port_id = dev->data->port_id;
438         q->queue_id = queue_id;
439         q->tail_ptr = (volatile uint32_t *)
440                 &((uint32_t *)hw->hw_addr)[FM10K_RDT(queue_id)];
441         if (handle_rxconf(q, conf))
442                 return (-EINVAL);
443
444         /* allocate memory for the software ring */
445         q->sw_ring = rte_zmalloc_socket("fm10k sw ring",
446                                         nb_desc * sizeof(struct rte_mbuf *),
447                                         RTE_CACHE_LINE_SIZE, socket_id);
448         if (q->sw_ring == NULL) {
449                 PMD_INIT_LOG(ERR, "Cannot allocate software ring");
450                 rte_free(q);
451                 return (-ENOMEM);
452         }
453
454         /*
455          * allocate memory for the hardware descriptor ring. A memzone large
456          * enough to hold the maximum ring size is requested to allow for
457          * resizing in later calls to the queue setup function.
458          */
459         mz = allocate_hw_ring(dev->driver->pci_drv.name, "rx_ring",
460                                 dev->data->port_id, queue_id, socket_id,
461                                 FM10K_MAX_RX_RING_SZ, FM10K_ALIGN_RX_DESC);
462         if (mz == NULL) {
463                 PMD_INIT_LOG(ERR, "Cannot allocate hardware ring");
464                 rte_free(q->sw_ring);
465                 rte_free(q);
466                 return (-ENOMEM);
467         }
468         q->hw_ring = mz->addr;
469         q->hw_ring_phys_addr = mz->phys_addr;
470
471         dev->data->rx_queues[queue_id] = q;
472         return 0;
473 }
474
475 static void
476 fm10k_rx_queue_release(void *queue)
477 {
478         PMD_INIT_FUNC_TRACE();
479
480         rx_queue_free(queue);
481 }
482
483 static inline int
484 handle_txconf(struct fm10k_tx_queue *q, const struct rte_eth_txconf *conf)
485 {
486         uint16_t tx_free_thresh;
487         uint16_t tx_rs_thresh;
488
489         /* constraint MACROs require that tx_free_thresh is configured
490          * before tx_rs_thresh */
491         if (conf->tx_free_thresh == 0)
492                 tx_free_thresh = FM10K_TX_FREE_THRESH_DEFAULT(q);
493         else
494                 tx_free_thresh = conf->tx_free_thresh;
495
496         /* make sure the requested threshold satisfies the constraints */
497         if (check_thresh(FM10K_TX_FREE_THRESH_MIN(q),
498                         FM10K_TX_FREE_THRESH_MAX(q),
499                         FM10K_TX_FREE_THRESH_DIV(q),
500                         tx_free_thresh)) {
501                 PMD_INIT_LOG(ERR, "tx_free_thresh (%u) must be "
502                         "less than or equal to %u, "
503                         "greater than or equal to %u, "
504                         "and a divisor of %u",
505                         tx_free_thresh, FM10K_TX_FREE_THRESH_MAX(q),
506                         FM10K_TX_FREE_THRESH_MIN(q),
507                         FM10K_TX_FREE_THRESH_DIV(q));
508                 return (-EINVAL);
509         }
510
511         q->free_thresh = tx_free_thresh;
512
513         if (conf->tx_rs_thresh == 0)
514                 tx_rs_thresh = FM10K_TX_RS_THRESH_DEFAULT(q);
515         else
516                 tx_rs_thresh = conf->tx_rs_thresh;
517
518         q->tx_deferred_start = conf->tx_deferred_start;
519
520         /* make sure the requested threshold satisfies the constraints */
521         if (check_thresh(FM10K_TX_RS_THRESH_MIN(q),
522                         FM10K_TX_RS_THRESH_MAX(q),
523                         FM10K_TX_RS_THRESH_DIV(q),
524                         tx_rs_thresh)) {
525                 PMD_INIT_LOG(ERR, "tx_rs_thresh (%u) must be "
526                         "less than or equal to %u, "
527                         "greater than or equal to %u, "
528                         "and a divisor of %u",
529                         tx_rs_thresh, FM10K_TX_RS_THRESH_MAX(q),
530                         FM10K_TX_RS_THRESH_MIN(q),
531                         FM10K_TX_RS_THRESH_DIV(q));
532                 return (-EINVAL);
533         }
534
535         q->rs_thresh = tx_rs_thresh;
536
537         return 0;
538 }
539
540 static int
541 fm10k_tx_queue_setup(struct rte_eth_dev *dev, uint16_t queue_id,
542         uint16_t nb_desc, unsigned int socket_id,
543         const struct rte_eth_txconf *conf)
544 {
545         struct fm10k_hw *hw = FM10K_DEV_PRIVATE_TO_HW(dev->data->dev_private);
546         struct fm10k_tx_queue *q;
547         const struct rte_memzone *mz;
548
549         PMD_INIT_FUNC_TRACE();
550
551         /* make sure a valid number of descriptors have been requested */
552         if (check_nb_desc(FM10K_MIN_TX_DESC, FM10K_MAX_TX_DESC,
553                                 FM10K_MULT_TX_DESC, nb_desc)) {
554                 PMD_INIT_LOG(ERR, "Number of Tx descriptors (%u) must be "
555                         "less than or equal to %"PRIu32", "
556                         "greater than or equal to %u, "
557                         "and a multiple of %u",
558                         nb_desc, (uint32_t)FM10K_MAX_TX_DESC, FM10K_MIN_TX_DESC,
559                         FM10K_MULT_TX_DESC);
560                 return (-EINVAL);
561         }
562
563         /*
564          * if this queue existed already, free the associated memory. The
565          * queue cannot be reused in case we need to allocate memory on
566          * different socket than was previously used.
567          */
568         if (dev->data->tx_queues[queue_id] != NULL) {
569                 tx_queue_free(dev->data->tx_queues[queue_id]);
570                 dev->data->tx_queues[queue_id] = NULL;
571         }
572
573         /* allocate memory for the queue structure */
574         q = rte_zmalloc_socket("fm10k", sizeof(*q), RTE_CACHE_LINE_SIZE,
575                                 socket_id);
576         if (q == NULL) {
577                 PMD_INIT_LOG(ERR, "Cannot allocate queue structure");
578                 return (-ENOMEM);
579         }
580
581         /* setup queue */
582         q->nb_desc = nb_desc;
583         q->port_id = dev->data->port_id;
584         q->queue_id = queue_id;
585         q->tail_ptr = (volatile uint32_t *)
586                 &((uint32_t *)hw->hw_addr)[FM10K_TDT(queue_id)];
587         if (handle_txconf(q, conf))
588                 return (-EINVAL);
589
590         /* allocate memory for the software ring */
591         q->sw_ring = rte_zmalloc_socket("fm10k sw ring",
592                                         nb_desc * sizeof(struct rte_mbuf *),
593                                         RTE_CACHE_LINE_SIZE, socket_id);
594         if (q->sw_ring == NULL) {
595                 PMD_INIT_LOG(ERR, "Cannot allocate software ring");
596                 rte_free(q);
597                 return (-ENOMEM);
598         }
599
600         /*
601          * allocate memory for the hardware descriptor ring. A memzone large
602          * enough to hold the maximum ring size is requested to allow for
603          * resizing in later calls to the queue setup function.
604          */
605         mz = allocate_hw_ring(dev->driver->pci_drv.name, "tx_ring",
606                                 dev->data->port_id, queue_id, socket_id,
607                                 FM10K_MAX_TX_RING_SZ, FM10K_ALIGN_TX_DESC);
608         if (mz == NULL) {
609                 PMD_INIT_LOG(ERR, "Cannot allocate hardware ring");
610                 rte_free(q->sw_ring);
611                 rte_free(q);
612                 return (-ENOMEM);
613         }
614         q->hw_ring = mz->addr;
615         q->hw_ring_phys_addr = mz->phys_addr;
616
617         /*
618          * allocate memory for the RS bit tracker. Enough slots to hold the
619          * descriptor index for each RS bit needing to be set are required.
620          */
621         q->rs_tracker.list = rte_zmalloc_socket("fm10k rs tracker",
622                                 ((nb_desc + 1) / q->rs_thresh) *
623                                 sizeof(uint16_t),
624                                 RTE_CACHE_LINE_SIZE, socket_id);
625         if (q->rs_tracker.list == NULL) {
626                 PMD_INIT_LOG(ERR, "Cannot allocate RS bit tracker");
627                 rte_free(q->sw_ring);
628                 rte_free(q);
629                 return (-ENOMEM);
630         }
631
632         dev->data->tx_queues[queue_id] = q;
633         return 0;
634 }
635
636 static void
637 fm10k_tx_queue_release(void *queue)
638 {
639         PMD_INIT_FUNC_TRACE();
640
641         tx_queue_free(queue);
642 }
643
644 static int
645 fm10k_reta_update(struct rte_eth_dev *dev,
646                         struct rte_eth_rss_reta_entry64 *reta_conf,
647                         uint16_t reta_size)
648 {
649         struct fm10k_hw *hw = FM10K_DEV_PRIVATE_TO_HW(dev->data->dev_private);
650         uint16_t i, j, idx, shift;
651         uint8_t mask;
652         uint32_t reta;
653
654         PMD_INIT_FUNC_TRACE();
655
656         if (reta_size > FM10K_MAX_RSS_INDICES) {
657                 PMD_INIT_LOG(ERR, "The size of hash lookup table configured "
658                         "(%d) doesn't match the number hardware can supported "
659                         "(%d)", reta_size, FM10K_MAX_RSS_INDICES);
660                 return -EINVAL;
661         }
662
663         /*
664          * Update Redirection Table RETA[n], n=0..31. The redirection table has
665          * 128-entries in 32 registers
666          */
667         for (i = 0; i < FM10K_MAX_RSS_INDICES; i += CHARS_PER_UINT32) {
668                 idx = i / RTE_RETA_GROUP_SIZE;
669                 shift = i % RTE_RETA_GROUP_SIZE;
670                 mask = (uint8_t)((reta_conf[idx].mask >> shift) &
671                                 BIT_MASK_PER_UINT32);
672                 if (mask == 0)
673                         continue;
674
675                 reta = 0;
676                 if (mask != BIT_MASK_PER_UINT32)
677                         reta = FM10K_READ_REG(hw, FM10K_RETA(0, i >> 2));
678
679                 for (j = 0; j < CHARS_PER_UINT32; j++) {
680                         if (mask & (0x1 << j)) {
681                                 if (mask != 0xF)
682                                         reta &= ~(UINT8_MAX << CHAR_BIT * j);
683                                 reta |= reta_conf[idx].reta[shift + j] <<
684                                                 (CHAR_BIT * j);
685                         }
686                 }
687                 FM10K_WRITE_REG(hw, FM10K_RETA(0, i >> 2), reta);
688         }
689
690         return 0;
691 }
692
693 static int
694 fm10k_reta_query(struct rte_eth_dev *dev,
695                         struct rte_eth_rss_reta_entry64 *reta_conf,
696                         uint16_t reta_size)
697 {
698         struct fm10k_hw *hw = FM10K_DEV_PRIVATE_TO_HW(dev->data->dev_private);
699         uint16_t i, j, idx, shift;
700         uint8_t mask;
701         uint32_t reta;
702
703         PMD_INIT_FUNC_TRACE();
704
705         if (reta_size < FM10K_MAX_RSS_INDICES) {
706                 PMD_INIT_LOG(ERR, "The size of hash lookup table configured "
707                         "(%d) doesn't match the number hardware can supported "
708                         "(%d)", reta_size, FM10K_MAX_RSS_INDICES);
709                 return -EINVAL;
710         }
711
712         /*
713          * Read Redirection Table RETA[n], n=0..31. The redirection table has
714          * 128-entries in 32 registers
715          */
716         for (i = 0; i < FM10K_MAX_RSS_INDICES; i += CHARS_PER_UINT32) {
717                 idx = i / RTE_RETA_GROUP_SIZE;
718                 shift = i % RTE_RETA_GROUP_SIZE;
719                 mask = (uint8_t)((reta_conf[idx].mask >> shift) &
720                                 BIT_MASK_PER_UINT32);
721                 if (mask == 0)
722                         continue;
723
724                 reta = FM10K_READ_REG(hw, FM10K_RETA(0, i >> 2));
725                 for (j = 0; j < CHARS_PER_UINT32; j++) {
726                         if (mask & (0x1 << j))
727                                 reta_conf[idx].reta[shift + j] = ((reta >>
728                                         CHAR_BIT * j) & UINT8_MAX);
729                 }
730         }
731
732         return 0;
733 }
734
735 /* Mailbox message handler in VF */
736 static const struct fm10k_msg_data fm10k_msgdata_vf[] = {
737         FM10K_TLV_MSG_TEST_HANDLER(fm10k_tlv_msg_test),
738         FM10K_VF_MSG_MAC_VLAN_HANDLER(fm10k_msg_mac_vlan_vf),
739         FM10K_VF_MSG_LPORT_STATE_HANDLER(fm10k_msg_lport_state_vf),
740         FM10K_TLV_MSG_ERROR_HANDLER(fm10k_tlv_msg_error),
741 };
742
743 /* Mailbox message handler in PF */
744 static const struct fm10k_msg_data fm10k_msgdata_pf[] = {
745         FM10K_PF_MSG_ERR_HANDLER(XCAST_MODES, fm10k_msg_err_pf),
746         FM10K_PF_MSG_ERR_HANDLER(UPDATE_MAC_FWD_RULE, fm10k_msg_err_pf),
747         FM10K_PF_MSG_LPORT_MAP_HANDLER(fm10k_msg_lport_map_pf),
748         FM10K_PF_MSG_ERR_HANDLER(LPORT_CREATE, fm10k_msg_err_pf),
749         FM10K_PF_MSG_ERR_HANDLER(LPORT_DELETE, fm10k_msg_err_pf),
750         FM10K_PF_MSG_UPDATE_PVID_HANDLER(fm10k_msg_update_pvid_pf),
751         FM10K_TLV_MSG_ERROR_HANDLER(fm10k_tlv_msg_error),
752 };
753
754 static int
755 fm10k_setup_mbx_service(struct fm10k_hw *hw)
756 {
757         int err;
758
759         /* Initialize mailbox lock */
760         fm10k_mbx_initlock(hw);
761
762         /* Replace default message handler with new ones */
763         if (hw->mac.type == fm10k_mac_pf)
764                 err = hw->mbx.ops.register_handlers(&hw->mbx, fm10k_msgdata_pf);
765         else
766                 err = hw->mbx.ops.register_handlers(&hw->mbx, fm10k_msgdata_vf);
767
768         if (err) {
769                 PMD_INIT_LOG(ERR, "Failed to register mailbox handler.err:%d",
770                                 err);
771                 return err;
772         }
773         /* Connect to SM for PF device or PF for VF device */
774         return hw->mbx.ops.connect(hw, &hw->mbx);
775 }
776
777 static struct eth_dev_ops fm10k_eth_dev_ops = {
778         .dev_configure          = fm10k_dev_configure,
779         .stats_get              = fm10k_stats_get,
780         .stats_reset            = fm10k_stats_reset,
781         .link_update            = fm10k_link_update,
782         .dev_infos_get          = fm10k_dev_infos_get,
783         .rx_queue_setup         = fm10k_rx_queue_setup,
784         .rx_queue_release       = fm10k_rx_queue_release,
785         .tx_queue_setup         = fm10k_tx_queue_setup,
786         .tx_queue_release       = fm10k_tx_queue_release,
787         .reta_update            = fm10k_reta_update,
788         .reta_query             = fm10k_reta_query,
789 };
790
791 static int
792 eth_fm10k_dev_init(__rte_unused struct eth_driver *eth_drv,
793         struct rte_eth_dev *dev)
794 {
795         struct fm10k_hw *hw = FM10K_DEV_PRIVATE_TO_HW(dev->data->dev_private);
796         int diag;
797
798         PMD_INIT_FUNC_TRACE();
799
800         dev->dev_ops = &fm10k_eth_dev_ops;
801
802         /* only initialize in the primary process */
803         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
804                 return 0;
805
806         /* Vendor and Device ID need to be set before init of shared code */
807         memset(hw, 0, sizeof(*hw));
808         hw->device_id = dev->pci_dev->id.device_id;
809         hw->vendor_id = dev->pci_dev->id.vendor_id;
810         hw->subsystem_device_id = dev->pci_dev->id.subsystem_device_id;
811         hw->subsystem_vendor_id = dev->pci_dev->id.subsystem_vendor_id;
812         hw->revision_id = 0;
813         hw->hw_addr = (void *)dev->pci_dev->mem_resource[0].addr;
814         if (hw->hw_addr == NULL) {
815                 PMD_INIT_LOG(ERR, "Bad mem resource."
816                         " Try to blacklist unused devices.");
817                 return -EIO;
818         }
819
820         /* Store fm10k_adapter pointer */
821         hw->back = dev->data->dev_private;
822
823         /* Initialize the shared code */
824         diag = fm10k_init_shared_code(hw);
825         if (diag != FM10K_SUCCESS) {
826                 PMD_INIT_LOG(ERR, "Shared code init failed: %d", diag);
827                 return -EIO;
828         }
829
830         /*
831          * Inialize bus info. Normally we would call fm10k_get_bus_info(), but
832          * there is no way to get link status without reading BAR4.  Until this
833          * works, assume we have maximum bandwidth.
834          * @todo - fix bus info
835          */
836         hw->bus_caps.speed = fm10k_bus_speed_8000;
837         hw->bus_caps.width = fm10k_bus_width_pcie_x8;
838         hw->bus_caps.payload = fm10k_bus_payload_512;
839         hw->bus.speed = fm10k_bus_speed_8000;
840         hw->bus.width = fm10k_bus_width_pcie_x8;
841         hw->bus.payload = fm10k_bus_payload_256;
842
843         /* Initialize the hw */
844         diag = fm10k_init_hw(hw);
845         if (diag != FM10K_SUCCESS) {
846                 PMD_INIT_LOG(ERR, "Hardware init failed: %d", diag);
847                 return -EIO;
848         }
849
850         /* Initialize MAC address(es) */
851         dev->data->mac_addrs = rte_zmalloc("fm10k", ETHER_ADDR_LEN, 0);
852         if (dev->data->mac_addrs == NULL) {
853                 PMD_INIT_LOG(ERR, "Cannot allocate memory for MAC addresses");
854                 return -ENOMEM;
855         }
856
857         diag = fm10k_read_mac_addr(hw);
858         if (diag != FM10K_SUCCESS) {
859                 /*
860                  * TODO: remove special handling on VF. Need shared code to
861                  * fix first.
862                  */
863                 if (hw->mac.type == fm10k_mac_pf) {
864                         PMD_INIT_LOG(ERR, "Read MAC addr failed: %d", diag);
865                         return -EIO;
866                 } else {
867                         /* Generate a random addr */
868                         eth_random_addr(hw->mac.addr);
869                         memcpy(hw->mac.perm_addr, hw->mac.addr, ETH_ALEN);
870                 }
871         }
872
873         ether_addr_copy((const struct ether_addr *)hw->mac.addr,
874                         &dev->data->mac_addrs[0]);
875
876         /* Reset the hw statistics */
877         fm10k_stats_reset(dev);
878
879         /* Reset the hw */
880         diag = fm10k_reset_hw(hw);
881         if (diag != FM10K_SUCCESS) {
882                 PMD_INIT_LOG(ERR, "Hardware reset failed: %d", diag);
883                 return -EIO;
884         }
885
886         /* Setup mailbox service */
887         diag = fm10k_setup_mbx_service(hw);
888         if (diag != FM10K_SUCCESS) {
889                 PMD_INIT_LOG(ERR, "Failed to setup mailbox: %d", diag);
890                 return -EIO;
891         }
892
893         /*
894          * Below function will trigger operations on mailbox, acquire lock to
895          * avoid race condition from interrupt handler. Operations on mailbox
896          * FIFO will trigger interrupt to PF/SM, in which interrupt handler
897          * will handle and generate an interrupt to our side. Then,  FIFO in
898          * mailbox will be touched.
899          */
900         fm10k_mbx_lock(hw);
901         /* Enable port first */
902         hw->mac.ops.update_lport_state(hw, 0, 0, 1);
903
904         /* Update default vlan */
905         hw->mac.ops.update_vlan(hw, hw->mac.default_vid, 0, true);
906
907         /*
908          * Add default mac/vlan filter. glort is assigned by SM for PF, while is
909          * unused for VF. PF will assign correct glort for VF.
910          */
911         hw->mac.ops.update_uc_addr(hw, hw->mac.dglort_map, hw->mac.addr,
912                               hw->mac.default_vid, 1, 0);
913
914         /* Set unicast mode by default. App can change to other mode in other
915          * API func.
916          */
917         hw->mac.ops.update_xcast_mode(hw, hw->mac.dglort_map,
918                                         FM10K_XCAST_MODE_MULTI);
919
920         fm10k_mbx_unlock(hw);
921
922         return 0;
923 }
924
925 /*
926  * The set of PCI devices this driver supports. This driver will enable both PF
927  * and SRIOV-VF devices.
928  */
929 static struct rte_pci_id pci_id_fm10k_map[] = {
930 #define RTE_PCI_DEV_ID_DECL_FM10K(vend, dev) { RTE_PCI_DEVICE(vend, dev) },
931 #include "rte_pci_dev_ids.h"
932         { .vendor_id = 0, /* sentinel */ },
933 };
934
935 static struct eth_driver rte_pmd_fm10k = {
936         {
937                 .name = "rte_pmd_fm10k",
938                 .id_table = pci_id_fm10k_map,
939                 .drv_flags = RTE_PCI_DRV_NEED_MAPPING,
940         },
941         .eth_dev_init = eth_fm10k_dev_init,
942         .dev_private_size = sizeof(struct fm10k_adapter),
943 };
944
945 /*
946  * Driver initialization routine.
947  * Invoked once at EAL init time.
948  * Register itself as the [Poll Mode] Driver of PCI FM10K devices.
949  */
950 static int
951 rte_pmd_fm10k_init(__rte_unused const char *name,
952         __rte_unused const char *params)
953 {
954         PMD_INIT_FUNC_TRACE();
955         rte_eth_driver_register(&rte_pmd_fm10k);
956         return 0;
957 }
958
959 static struct rte_driver rte_fm10k_driver = {
960         .type = PMD_PDEV,
961         .init = rte_pmd_fm10k_init,
962 };
963
964 PMD_REGISTER_DRIVER(rte_fm10k_driver);