drivers/net: fix exposing internal headers
[dpdk.git] / drivers / net / szedata2 / rte_eth_szedata2.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2015 - 2016 CESNET
3  */
4
5 #include <stdint.h>
6 #include <unistd.h>
7 #include <stdbool.h>
8 #include <err.h>
9 #include <sys/types.h>
10 #include <dirent.h>
11 #include <sys/stat.h>
12 #include <fcntl.h>
13 #include <sys/mman.h>
14
15 #include <libsze2.h>
16
17 #include <rte_mbuf.h>
18 #include <rte_ethdev_driver.h>
19 #include <rte_ethdev_pci.h>
20 #include <rte_malloc.h>
21 #include <rte_memcpy.h>
22 #include <rte_kvargs.h>
23 #include <rte_dev.h>
24
25 #include "rte_eth_szedata2.h"
26 #include "szedata2_logs.h"
27
28 #define RTE_ETH_SZEDATA2_MAX_RX_QUEUES 32
29 #define RTE_ETH_SZEDATA2_MAX_TX_QUEUES 32
30 #define RTE_ETH_SZEDATA2_TX_LOCK_SIZE (32 * 1024 * 1024)
31
32 /**
33  * size of szedata2_packet header with alignment
34  */
35 #define RTE_SZE2_PACKET_HEADER_SIZE_ALIGNED 8
36
37 #define RTE_SZEDATA2_DRIVER_NAME net_szedata2
38
39 #define SZEDATA2_DEV_PATH_FMT "/dev/szedataII%u"
40
41 /**
42  * Format string for suffix used to differentiate between Ethernet ports
43  * on the same PCI device.
44  */
45 #define SZEDATA2_ETH_DEV_NAME_SUFFIX_FMT "-port%u"
46
47 /**
48  * Maximum number of ports for one device.
49  */
50 #define SZEDATA2_MAX_PORTS 2
51
52 /**
53  * Entry in list of PCI devices for this driver.
54  */
55 struct pci_dev_list_entry;
56 struct pci_dev_list_entry {
57         LIST_ENTRY(pci_dev_list_entry) next;
58         struct rte_pci_device *pci_dev;
59         unsigned int port_count;
60 };
61
62 /* List of PCI devices with number of ports for this driver. */
63 LIST_HEAD(pci_dev_list, pci_dev_list_entry) szedata2_pci_dev_list =
64         LIST_HEAD_INITIALIZER(szedata2_pci_dev_list);
65
66 struct port_info {
67         unsigned int rx_base_id;
68         unsigned int tx_base_id;
69         unsigned int rx_count;
70         unsigned int tx_count;
71         int numa_node;
72 };
73
74 struct pmd_internals {
75         struct rte_eth_dev *dev;
76         uint16_t max_rx_queues;
77         uint16_t max_tx_queues;
78         unsigned int rxq_base_id;
79         unsigned int txq_base_id;
80         char *sze_dev_path;
81 };
82
83 struct szedata2_rx_queue {
84         struct pmd_internals *priv;
85         struct szedata *sze;
86         uint8_t rx_channel;
87         uint16_t qid;
88         uint16_t in_port;
89         struct rte_mempool *mb_pool;
90         volatile uint64_t rx_pkts;
91         volatile uint64_t rx_bytes;
92         volatile uint64_t err_pkts;
93 };
94
95 struct szedata2_tx_queue {
96         struct pmd_internals *priv;
97         struct szedata *sze;
98         uint8_t tx_channel;
99         uint16_t qid;
100         volatile uint64_t tx_pkts;
101         volatile uint64_t tx_bytes;
102         volatile uint64_t err_pkts;
103 };
104
105 static struct rte_ether_addr eth_addr = {
106         .addr_bytes = { 0x00, 0x11, 0x17, 0x00, 0x00, 0x00 }
107 };
108
109 static uint16_t
110 eth_szedata2_rx(void *queue,
111                 struct rte_mbuf **bufs,
112                 uint16_t nb_pkts)
113 {
114         unsigned int i;
115         struct rte_mbuf *mbuf;
116         struct szedata2_rx_queue *sze_q = queue;
117         struct rte_pktmbuf_pool_private *mbp_priv;
118         uint16_t num_rx = 0;
119         uint16_t buf_size;
120         uint16_t sg_size;
121         uint16_t hw_size;
122         uint16_t packet_size;
123         uint64_t num_bytes = 0;
124         struct szedata *sze = sze_q->sze;
125         uint8_t *header_ptr = NULL; /* header of packet */
126         uint8_t *packet_ptr1 = NULL;
127         uint8_t *packet_ptr2 = NULL;
128         uint16_t packet_len1 = 0;
129         uint16_t packet_len2 = 0;
130         uint16_t hw_data_align;
131
132         if (unlikely(sze_q->sze == NULL || nb_pkts == 0))
133                 return 0;
134
135         /*
136          * Reads the given number of packets from szedata2 channel given
137          * by queue and copies the packet data into a newly allocated mbuf
138          * to return.
139          */
140         for (i = 0; i < nb_pkts; i++) {
141                 mbuf = rte_pktmbuf_alloc(sze_q->mb_pool);
142
143                 if (unlikely(mbuf == NULL)) {
144                         sze_q->priv->dev->data->rx_mbuf_alloc_failed++;
145                         break;
146                 }
147
148                 /* get the next sze packet */
149                 if (sze->ct_rx_lck != NULL && !sze->ct_rx_rem_bytes &&
150                                 sze->ct_rx_lck->next == NULL) {
151                         /* unlock old data */
152                         szedata_rx_unlock_data(sze_q->sze, sze->ct_rx_lck_orig);
153                         sze->ct_rx_lck_orig = NULL;
154                         sze->ct_rx_lck = NULL;
155                 }
156
157                 if (!sze->ct_rx_rem_bytes && sze->ct_rx_lck_orig == NULL) {
158                         /* nothing to read, lock new data */
159                         sze->ct_rx_lck = szedata_rx_lock_data(sze_q->sze, ~0U);
160                         sze->ct_rx_lck_orig = sze->ct_rx_lck;
161
162                         if (sze->ct_rx_lck == NULL) {
163                                 /* nothing to lock */
164                                 rte_pktmbuf_free(mbuf);
165                                 break;
166                         }
167
168                         sze->ct_rx_cur_ptr = sze->ct_rx_lck->start;
169                         sze->ct_rx_rem_bytes = sze->ct_rx_lck->len;
170
171                         if (!sze->ct_rx_rem_bytes) {
172                                 rte_pktmbuf_free(mbuf);
173                                 break;
174                         }
175                 }
176
177                 if (sze->ct_rx_rem_bytes < RTE_SZE2_PACKET_HEADER_SIZE) {
178                         /*
179                          * cut in header
180                          * copy parts of header to merge buffer
181                          */
182                         if (sze->ct_rx_lck->next == NULL) {
183                                 rte_pktmbuf_free(mbuf);
184                                 break;
185                         }
186
187                         /* copy first part of header */
188                         rte_memcpy(sze->ct_rx_buffer, sze->ct_rx_cur_ptr,
189                                         sze->ct_rx_rem_bytes);
190
191                         /* copy second part of header */
192                         sze->ct_rx_lck = sze->ct_rx_lck->next;
193                         sze->ct_rx_cur_ptr = sze->ct_rx_lck->start;
194                         rte_memcpy(sze->ct_rx_buffer + sze->ct_rx_rem_bytes,
195                                 sze->ct_rx_cur_ptr,
196                                 RTE_SZE2_PACKET_HEADER_SIZE -
197                                 sze->ct_rx_rem_bytes);
198
199                         sze->ct_rx_cur_ptr += RTE_SZE2_PACKET_HEADER_SIZE -
200                                 sze->ct_rx_rem_bytes;
201                         sze->ct_rx_rem_bytes = sze->ct_rx_lck->len -
202                                 RTE_SZE2_PACKET_HEADER_SIZE +
203                                 sze->ct_rx_rem_bytes;
204
205                         header_ptr = (uint8_t *)sze->ct_rx_buffer;
206                 } else {
207                         /* not cut */
208                         header_ptr = (uint8_t *)sze->ct_rx_cur_ptr;
209                         sze->ct_rx_cur_ptr += RTE_SZE2_PACKET_HEADER_SIZE;
210                         sze->ct_rx_rem_bytes -= RTE_SZE2_PACKET_HEADER_SIZE;
211                 }
212
213                 sg_size = le16toh(*((uint16_t *)header_ptr));
214                 hw_size = le16toh(*(((uint16_t *)header_ptr) + 1));
215                 packet_size = sg_size -
216                         RTE_SZE2_ALIGN8(RTE_SZE2_PACKET_HEADER_SIZE + hw_size);
217
218
219                 /* checks if packet all right */
220                 if (!sg_size)
221                         errx(5, "Zero segsize");
222
223                 /* check sg_size and hwsize */
224                 if (hw_size > sg_size - RTE_SZE2_PACKET_HEADER_SIZE) {
225                         errx(10, "Hwsize bigger than expected. Segsize: %d, "
226                                 "hwsize: %d", sg_size, hw_size);
227                 }
228
229                 hw_data_align =
230                         RTE_SZE2_ALIGN8(RTE_SZE2_PACKET_HEADER_SIZE + hw_size) -
231                         RTE_SZE2_PACKET_HEADER_SIZE;
232
233                 if (sze->ct_rx_rem_bytes >=
234                                 (uint16_t)(sg_size -
235                                 RTE_SZE2_PACKET_HEADER_SIZE)) {
236                         /* no cut */
237                         /* one packet ready - go to another */
238                         packet_ptr1 = sze->ct_rx_cur_ptr + hw_data_align;
239                         packet_len1 = packet_size;
240                         packet_ptr2 = NULL;
241                         packet_len2 = 0;
242
243                         sze->ct_rx_cur_ptr += RTE_SZE2_ALIGN8(sg_size) -
244                                 RTE_SZE2_PACKET_HEADER_SIZE;
245                         sze->ct_rx_rem_bytes -= RTE_SZE2_ALIGN8(sg_size) -
246                                 RTE_SZE2_PACKET_HEADER_SIZE;
247                 } else {
248                         /* cut in data */
249                         if (sze->ct_rx_lck->next == NULL) {
250                                 errx(6, "Need \"next\" lock, "
251                                         "but it is missing: %u",
252                                         sze->ct_rx_rem_bytes);
253                         }
254
255                         /* skip hw data */
256                         if (sze->ct_rx_rem_bytes <= hw_data_align) {
257                                 uint16_t rem_size = hw_data_align -
258                                         sze->ct_rx_rem_bytes;
259
260                                 /* MOVE to next lock */
261                                 sze->ct_rx_lck = sze->ct_rx_lck->next;
262                                 sze->ct_rx_cur_ptr =
263                                         (void *)(((uint8_t *)
264                                         (sze->ct_rx_lck->start)) + rem_size);
265
266                                 packet_ptr1 = sze->ct_rx_cur_ptr;
267                                 packet_len1 = packet_size;
268                                 packet_ptr2 = NULL;
269                                 packet_len2 = 0;
270
271                                 sze->ct_rx_cur_ptr +=
272                                         RTE_SZE2_ALIGN8(packet_size);
273                                 sze->ct_rx_rem_bytes = sze->ct_rx_lck->len -
274                                         rem_size - RTE_SZE2_ALIGN8(packet_size);
275                         } else {
276                                 /* get pointer and length from first part */
277                                 packet_ptr1 = sze->ct_rx_cur_ptr +
278                                         hw_data_align;
279                                 packet_len1 = sze->ct_rx_rem_bytes -
280                                         hw_data_align;
281
282                                 /* MOVE to next lock */
283                                 sze->ct_rx_lck = sze->ct_rx_lck->next;
284                                 sze->ct_rx_cur_ptr = sze->ct_rx_lck->start;
285
286                                 /* get pointer and length from second part */
287                                 packet_ptr2 = sze->ct_rx_cur_ptr;
288                                 packet_len2 = packet_size - packet_len1;
289
290                                 sze->ct_rx_cur_ptr +=
291                                         RTE_SZE2_ALIGN8(packet_size) -
292                                         packet_len1;
293                                 sze->ct_rx_rem_bytes = sze->ct_rx_lck->len -
294                                         (RTE_SZE2_ALIGN8(packet_size) -
295                                          packet_len1);
296                         }
297                 }
298
299                 if (unlikely(packet_ptr1 == NULL)) {
300                         rte_pktmbuf_free(mbuf);
301                         break;
302                 }
303
304                 /* get the space available for data in the mbuf */
305                 mbp_priv = rte_mempool_get_priv(sze_q->mb_pool);
306                 buf_size = (uint16_t)(mbp_priv->mbuf_data_room_size -
307                                 RTE_PKTMBUF_HEADROOM);
308
309                 if (packet_size <= buf_size) {
310                         /* sze packet will fit in one mbuf, go ahead and copy */
311                         rte_memcpy(rte_pktmbuf_mtod(mbuf, void *),
312                                         packet_ptr1, packet_len1);
313                         if (packet_ptr2 != NULL) {
314                                 rte_memcpy((void *)(rte_pktmbuf_mtod(mbuf,
315                                         uint8_t *) + packet_len1),
316                                         packet_ptr2, packet_len2);
317                         }
318                         mbuf->data_len = (uint16_t)packet_size;
319
320                         mbuf->pkt_len = packet_size;
321                         mbuf->port = sze_q->in_port;
322                         bufs[num_rx] = mbuf;
323                         num_rx++;
324                         num_bytes += packet_size;
325                 } else {
326                         /*
327                          * sze packet will not fit in one mbuf,
328                          * scattered mode is not enabled, drop packet
329                          */
330                         PMD_DRV_LOG(ERR,
331                                 "SZE segment %d bytes will not fit in one mbuf "
332                                 "(%d bytes), scattered mode is not enabled, "
333                                 "drop packet!!",
334                                 packet_size, buf_size);
335                         rte_pktmbuf_free(mbuf);
336                 }
337         }
338
339         sze_q->rx_pkts += num_rx;
340         sze_q->rx_bytes += num_bytes;
341         return num_rx;
342 }
343
344 static uint16_t
345 eth_szedata2_rx_scattered(void *queue,
346                 struct rte_mbuf **bufs,
347                 uint16_t nb_pkts)
348 {
349         unsigned int i;
350         struct rte_mbuf *mbuf;
351         struct szedata2_rx_queue *sze_q = queue;
352         struct rte_pktmbuf_pool_private *mbp_priv;
353         uint16_t num_rx = 0;
354         uint16_t buf_size;
355         uint16_t sg_size;
356         uint16_t hw_size;
357         uint16_t packet_size;
358         uint64_t num_bytes = 0;
359         struct szedata *sze = sze_q->sze;
360         uint8_t *header_ptr = NULL; /* header of packet */
361         uint8_t *packet_ptr1 = NULL;
362         uint8_t *packet_ptr2 = NULL;
363         uint16_t packet_len1 = 0;
364         uint16_t packet_len2 = 0;
365         uint16_t hw_data_align;
366         uint64_t *mbuf_failed_ptr =
367                 &sze_q->priv->dev->data->rx_mbuf_alloc_failed;
368
369         if (unlikely(sze_q->sze == NULL || nb_pkts == 0))
370                 return 0;
371
372         /*
373          * Reads the given number of packets from szedata2 channel given
374          * by queue and copies the packet data into a newly allocated mbuf
375          * to return.
376          */
377         for (i = 0; i < nb_pkts; i++) {
378                 const struct szedata_lock *ct_rx_lck_backup;
379                 unsigned int ct_rx_rem_bytes_backup;
380                 unsigned char *ct_rx_cur_ptr_backup;
381
382                 /* get the next sze packet */
383                 if (sze->ct_rx_lck != NULL && !sze->ct_rx_rem_bytes &&
384                                 sze->ct_rx_lck->next == NULL) {
385                         /* unlock old data */
386                         szedata_rx_unlock_data(sze_q->sze, sze->ct_rx_lck_orig);
387                         sze->ct_rx_lck_orig = NULL;
388                         sze->ct_rx_lck = NULL;
389                 }
390
391                 /*
392                  * Store items from sze structure which can be changed
393                  * before mbuf allocating. Use these items in case of mbuf
394                  * allocating failure.
395                  */
396                 ct_rx_lck_backup = sze->ct_rx_lck;
397                 ct_rx_rem_bytes_backup = sze->ct_rx_rem_bytes;
398                 ct_rx_cur_ptr_backup = sze->ct_rx_cur_ptr;
399
400                 if (!sze->ct_rx_rem_bytes && sze->ct_rx_lck_orig == NULL) {
401                         /* nothing to read, lock new data */
402                         sze->ct_rx_lck = szedata_rx_lock_data(sze_q->sze, ~0U);
403                         sze->ct_rx_lck_orig = sze->ct_rx_lck;
404
405                         /*
406                          * Backup items from sze structure must be updated
407                          * after locking to contain pointers to new locks.
408                          */
409                         ct_rx_lck_backup = sze->ct_rx_lck;
410                         ct_rx_rem_bytes_backup = sze->ct_rx_rem_bytes;
411                         ct_rx_cur_ptr_backup = sze->ct_rx_cur_ptr;
412
413                         if (sze->ct_rx_lck == NULL)
414                                 /* nothing to lock */
415                                 break;
416
417                         sze->ct_rx_cur_ptr = sze->ct_rx_lck->start;
418                         sze->ct_rx_rem_bytes = sze->ct_rx_lck->len;
419
420                         if (!sze->ct_rx_rem_bytes)
421                                 break;
422                 }
423
424                 if (sze->ct_rx_rem_bytes < RTE_SZE2_PACKET_HEADER_SIZE) {
425                         /*
426                          * cut in header - copy parts of header to merge buffer
427                          */
428                         if (sze->ct_rx_lck->next == NULL)
429                                 break;
430
431                         /* copy first part of header */
432                         rte_memcpy(sze->ct_rx_buffer, sze->ct_rx_cur_ptr,
433                                         sze->ct_rx_rem_bytes);
434
435                         /* copy second part of header */
436                         sze->ct_rx_lck = sze->ct_rx_lck->next;
437                         sze->ct_rx_cur_ptr = sze->ct_rx_lck->start;
438                         rte_memcpy(sze->ct_rx_buffer + sze->ct_rx_rem_bytes,
439                                 sze->ct_rx_cur_ptr,
440                                 RTE_SZE2_PACKET_HEADER_SIZE -
441                                 sze->ct_rx_rem_bytes);
442
443                         sze->ct_rx_cur_ptr += RTE_SZE2_PACKET_HEADER_SIZE -
444                                 sze->ct_rx_rem_bytes;
445                         sze->ct_rx_rem_bytes = sze->ct_rx_lck->len -
446                                 RTE_SZE2_PACKET_HEADER_SIZE +
447                                 sze->ct_rx_rem_bytes;
448
449                         header_ptr = (uint8_t *)sze->ct_rx_buffer;
450                 } else {
451                         /* not cut */
452                         header_ptr = (uint8_t *)sze->ct_rx_cur_ptr;
453                         sze->ct_rx_cur_ptr += RTE_SZE2_PACKET_HEADER_SIZE;
454                         sze->ct_rx_rem_bytes -= RTE_SZE2_PACKET_HEADER_SIZE;
455                 }
456
457                 sg_size = le16toh(*((uint16_t *)header_ptr));
458                 hw_size = le16toh(*(((uint16_t *)header_ptr) + 1));
459                 packet_size = sg_size -
460                         RTE_SZE2_ALIGN8(RTE_SZE2_PACKET_HEADER_SIZE + hw_size);
461
462
463                 /* checks if packet all right */
464                 if (!sg_size)
465                         errx(5, "Zero segsize");
466
467                 /* check sg_size and hwsize */
468                 if (hw_size > sg_size - RTE_SZE2_PACKET_HEADER_SIZE) {
469                         errx(10, "Hwsize bigger than expected. Segsize: %d, "
470                                         "hwsize: %d", sg_size, hw_size);
471                 }
472
473                 hw_data_align =
474                         RTE_SZE2_ALIGN8((RTE_SZE2_PACKET_HEADER_SIZE +
475                         hw_size)) - RTE_SZE2_PACKET_HEADER_SIZE;
476
477                 if (sze->ct_rx_rem_bytes >=
478                                 (uint16_t)(sg_size -
479                                 RTE_SZE2_PACKET_HEADER_SIZE)) {
480                         /* no cut */
481                         /* one packet ready - go to another */
482                         packet_ptr1 = sze->ct_rx_cur_ptr + hw_data_align;
483                         packet_len1 = packet_size;
484                         packet_ptr2 = NULL;
485                         packet_len2 = 0;
486
487                         sze->ct_rx_cur_ptr += RTE_SZE2_ALIGN8(sg_size) -
488                                 RTE_SZE2_PACKET_HEADER_SIZE;
489                         sze->ct_rx_rem_bytes -= RTE_SZE2_ALIGN8(sg_size) -
490                                 RTE_SZE2_PACKET_HEADER_SIZE;
491                 } else {
492                         /* cut in data */
493                         if (sze->ct_rx_lck->next == NULL) {
494                                 errx(6, "Need \"next\" lock, but it is "
495                                         "missing: %u", sze->ct_rx_rem_bytes);
496                         }
497
498                         /* skip hw data */
499                         if (sze->ct_rx_rem_bytes <= hw_data_align) {
500                                 uint16_t rem_size = hw_data_align -
501                                         sze->ct_rx_rem_bytes;
502
503                                 /* MOVE to next lock */
504                                 sze->ct_rx_lck = sze->ct_rx_lck->next;
505                                 sze->ct_rx_cur_ptr =
506                                         (void *)(((uint8_t *)
507                                         (sze->ct_rx_lck->start)) + rem_size);
508
509                                 packet_ptr1 = sze->ct_rx_cur_ptr;
510                                 packet_len1 = packet_size;
511                                 packet_ptr2 = NULL;
512                                 packet_len2 = 0;
513
514                                 sze->ct_rx_cur_ptr +=
515                                         RTE_SZE2_ALIGN8(packet_size);
516                                 sze->ct_rx_rem_bytes = sze->ct_rx_lck->len -
517                                         rem_size - RTE_SZE2_ALIGN8(packet_size);
518                         } else {
519                                 /* get pointer and length from first part */
520                                 packet_ptr1 = sze->ct_rx_cur_ptr +
521                                         hw_data_align;
522                                 packet_len1 = sze->ct_rx_rem_bytes -
523                                         hw_data_align;
524
525                                 /* MOVE to next lock */
526                                 sze->ct_rx_lck = sze->ct_rx_lck->next;
527                                 sze->ct_rx_cur_ptr = sze->ct_rx_lck->start;
528
529                                 /* get pointer and length from second part */
530                                 packet_ptr2 = sze->ct_rx_cur_ptr;
531                                 packet_len2 = packet_size - packet_len1;
532
533                                 sze->ct_rx_cur_ptr +=
534                                         RTE_SZE2_ALIGN8(packet_size) -
535                                         packet_len1;
536                                 sze->ct_rx_rem_bytes = sze->ct_rx_lck->len -
537                                         (RTE_SZE2_ALIGN8(packet_size) -
538                                          packet_len1);
539                         }
540                 }
541
542                 if (unlikely(packet_ptr1 == NULL))
543                         break;
544
545                 mbuf = rte_pktmbuf_alloc(sze_q->mb_pool);
546
547                 if (unlikely(mbuf == NULL)) {
548                         /*
549                          * Restore items from sze structure to state after
550                          * unlocking (eventually locking).
551                          */
552                         sze->ct_rx_lck = ct_rx_lck_backup;
553                         sze->ct_rx_rem_bytes = ct_rx_rem_bytes_backup;
554                         sze->ct_rx_cur_ptr = ct_rx_cur_ptr_backup;
555                         sze_q->priv->dev->data->rx_mbuf_alloc_failed++;
556                         break;
557                 }
558
559                 /* get the space available for data in the mbuf */
560                 mbp_priv = rte_mempool_get_priv(sze_q->mb_pool);
561                 buf_size = (uint16_t)(mbp_priv->mbuf_data_room_size -
562                                 RTE_PKTMBUF_HEADROOM);
563
564                 if (packet_size <= buf_size) {
565                         /* sze packet will fit in one mbuf, go ahead and copy */
566                         rte_memcpy(rte_pktmbuf_mtod(mbuf, void *),
567                                         packet_ptr1, packet_len1);
568                         if (packet_ptr2 != NULL) {
569                                 rte_memcpy((void *)
570                                         (rte_pktmbuf_mtod(mbuf, uint8_t *) +
571                                         packet_len1), packet_ptr2, packet_len2);
572                         }
573                         mbuf->data_len = (uint16_t)packet_size;
574                 } else {
575                         /*
576                          * sze packet will not fit in one mbuf,
577                          * scatter packet into more mbufs
578                          */
579                         struct rte_mbuf *m = mbuf;
580                         uint16_t len = rte_pktmbuf_tailroom(mbuf);
581
582                         /* copy first part of packet */
583                         /* fill first mbuf */
584                         rte_memcpy(rte_pktmbuf_append(mbuf, len), packet_ptr1,
585                                 len);
586                         packet_len1 -= len;
587                         packet_ptr1 = ((uint8_t *)packet_ptr1) + len;
588
589                         while (packet_len1 > 0) {
590                                 /* fill new mbufs */
591                                 m->next = rte_pktmbuf_alloc(sze_q->mb_pool);
592
593                                 if (unlikely(m->next == NULL)) {
594                                         rte_pktmbuf_free(mbuf);
595                                         /*
596                                          * Restore items from sze structure
597                                          * to state after unlocking (eventually
598                                          * locking).
599                                          */
600                                         sze->ct_rx_lck = ct_rx_lck_backup;
601                                         sze->ct_rx_rem_bytes =
602                                                 ct_rx_rem_bytes_backup;
603                                         sze->ct_rx_cur_ptr =
604                                                 ct_rx_cur_ptr_backup;
605                                         (*mbuf_failed_ptr)++;
606                                         goto finish;
607                                 }
608
609                                 m = m->next;
610
611                                 len = RTE_MIN(rte_pktmbuf_tailroom(m),
612                                         packet_len1);
613                                 rte_memcpy(rte_pktmbuf_append(mbuf, len),
614                                         packet_ptr1, len);
615
616                                 (mbuf->nb_segs)++;
617                                 packet_len1 -= len;
618                                 packet_ptr1 = ((uint8_t *)packet_ptr1) + len;
619                         }
620
621                         if (packet_ptr2 != NULL) {
622                                 /* copy second part of packet, if exists */
623                                 /* fill the rest of currently last mbuf */
624                                 len = rte_pktmbuf_tailroom(m);
625                                 rte_memcpy(rte_pktmbuf_append(mbuf, len),
626                                         packet_ptr2, len);
627                                 packet_len2 -= len;
628                                 packet_ptr2 = ((uint8_t *)packet_ptr2) + len;
629
630                                 while (packet_len2 > 0) {
631                                         /* fill new mbufs */
632                                         m->next = rte_pktmbuf_alloc(
633                                                         sze_q->mb_pool);
634
635                                         if (unlikely(m->next == NULL)) {
636                                                 rte_pktmbuf_free(mbuf);
637                                                 /*
638                                                  * Restore items from sze
639                                                  * structure to state after
640                                                  * unlocking (eventually
641                                                  * locking).
642                                                  */
643                                                 sze->ct_rx_lck =
644                                                         ct_rx_lck_backup;
645                                                 sze->ct_rx_rem_bytes =
646                                                         ct_rx_rem_bytes_backup;
647                                                 sze->ct_rx_cur_ptr =
648                                                         ct_rx_cur_ptr_backup;
649                                                 (*mbuf_failed_ptr)++;
650                                                 goto finish;
651                                         }
652
653                                         m = m->next;
654
655                                         len = RTE_MIN(rte_pktmbuf_tailroom(m),
656                                                 packet_len2);
657                                         rte_memcpy(
658                                                 rte_pktmbuf_append(mbuf, len),
659                                                 packet_ptr2, len);
660
661                                         (mbuf->nb_segs)++;
662                                         packet_len2 -= len;
663                                         packet_ptr2 = ((uint8_t *)packet_ptr2) +
664                                                 len;
665                                 }
666                         }
667                 }
668                 mbuf->pkt_len = packet_size;
669                 mbuf->port = sze_q->in_port;
670                 bufs[num_rx] = mbuf;
671                 num_rx++;
672                 num_bytes += packet_size;
673         }
674
675 finish:
676         sze_q->rx_pkts += num_rx;
677         sze_q->rx_bytes += num_bytes;
678         return num_rx;
679 }
680
681 static uint16_t
682 eth_szedata2_tx(void *queue,
683                 struct rte_mbuf **bufs,
684                 uint16_t nb_pkts)
685 {
686         struct rte_mbuf *mbuf;
687         struct szedata2_tx_queue *sze_q = queue;
688         uint16_t num_tx = 0;
689         uint64_t num_bytes = 0;
690
691         const struct szedata_lock *lck;
692         uint32_t lock_size;
693         uint32_t lock_size2;
694         void *dst;
695         uint32_t pkt_len;
696         uint32_t hwpkt_len;
697         uint32_t unlock_size;
698         uint32_t rem_len;
699         uint16_t mbuf_segs;
700         uint16_t pkt_left = nb_pkts;
701
702         if (sze_q->sze == NULL || nb_pkts == 0)
703                 return 0;
704
705         while (pkt_left > 0) {
706                 unlock_size = 0;
707                 lck = szedata_tx_lock_data(sze_q->sze,
708                         RTE_ETH_SZEDATA2_TX_LOCK_SIZE,
709                         sze_q->tx_channel);
710                 if (lck == NULL)
711                         continue;
712
713                 dst = lck->start;
714                 lock_size = lck->len;
715                 lock_size2 = lck->next ? lck->next->len : 0;
716
717 next_packet:
718                 mbuf = bufs[nb_pkts - pkt_left];
719
720                 pkt_len = mbuf->pkt_len;
721                 mbuf_segs = mbuf->nb_segs;
722
723                 hwpkt_len = RTE_SZE2_PACKET_HEADER_SIZE_ALIGNED +
724                         RTE_SZE2_ALIGN8(pkt_len);
725
726                 if (lock_size + lock_size2 < hwpkt_len) {
727                         szedata_tx_unlock_data(sze_q->sze, lck, unlock_size);
728                         continue;
729                 }
730
731                 num_bytes += pkt_len;
732
733                 if (lock_size > hwpkt_len) {
734                         void *tmp_dst;
735
736                         rem_len = 0;
737
738                         /* write packet length at first 2 bytes in 8B header */
739                         *((uint16_t *)dst) = htole16(
740                                         RTE_SZE2_PACKET_HEADER_SIZE_ALIGNED +
741                                         pkt_len);
742                         *(((uint16_t *)dst) + 1) = htole16(0);
743
744                         /* copy packet from mbuf */
745                         tmp_dst = ((uint8_t *)(dst)) +
746                                 RTE_SZE2_PACKET_HEADER_SIZE_ALIGNED;
747                         if (mbuf_segs == 1) {
748                                 /*
749                                  * non-scattered packet,
750                                  * transmit from one mbuf
751                                  */
752                                 rte_memcpy(tmp_dst,
753                                         rte_pktmbuf_mtod(mbuf, const void *),
754                                         pkt_len);
755                         } else {
756                                 /* scattered packet, transmit from more mbufs */
757                                 struct rte_mbuf *m = mbuf;
758                                 while (m) {
759                                         rte_memcpy(tmp_dst,
760                                                 rte_pktmbuf_mtod(m,
761                                                 const void *),
762                                                 m->data_len);
763                                         tmp_dst = ((uint8_t *)(tmp_dst)) +
764                                                 m->data_len;
765                                         m = m->next;
766                                 }
767                         }
768
769
770                         dst = ((uint8_t *)dst) + hwpkt_len;
771                         unlock_size += hwpkt_len;
772                         lock_size -= hwpkt_len;
773
774                         rte_pktmbuf_free(mbuf);
775                         num_tx++;
776                         pkt_left--;
777                         if (pkt_left == 0) {
778                                 szedata_tx_unlock_data(sze_q->sze, lck,
779                                         unlock_size);
780                                 break;
781                         }
782                         goto next_packet;
783                 } else if (lock_size + lock_size2 >= hwpkt_len) {
784                         void *tmp_dst;
785                         uint16_t write_len;
786
787                         /* write packet length at first 2 bytes in 8B header */
788                         *((uint16_t *)dst) =
789                                 htole16(RTE_SZE2_PACKET_HEADER_SIZE_ALIGNED +
790                                         pkt_len);
791                         *(((uint16_t *)dst) + 1) = htole16(0);
792
793                         /*
794                          * If the raw packet (pkt_len) is smaller than lock_size
795                          * get the correct length for memcpy
796                          */
797                         write_len =
798                                 pkt_len < lock_size -
799                                 RTE_SZE2_PACKET_HEADER_SIZE_ALIGNED ?
800                                 pkt_len :
801                                 lock_size - RTE_SZE2_PACKET_HEADER_SIZE_ALIGNED;
802
803                         rem_len = hwpkt_len - lock_size;
804
805                         tmp_dst = ((uint8_t *)(dst)) +
806                                 RTE_SZE2_PACKET_HEADER_SIZE_ALIGNED;
807                         if (mbuf_segs == 1) {
808                                 /*
809                                  * non-scattered packet,
810                                  * transmit from one mbuf
811                                  */
812                                 /* copy part of packet to first area */
813                                 rte_memcpy(tmp_dst,
814                                         rte_pktmbuf_mtod(mbuf, const void *),
815                                         write_len);
816
817                                 if (lck->next)
818                                         dst = lck->next->start;
819
820                                 /* copy part of packet to second area */
821                                 rte_memcpy(dst,
822                                         (const void *)(rte_pktmbuf_mtod(mbuf,
823                                                         const uint8_t *) +
824                                         write_len), pkt_len - write_len);
825                         } else {
826                                 /* scattered packet, transmit from more mbufs */
827                                 struct rte_mbuf *m = mbuf;
828                                 uint16_t written = 0;
829                                 uint16_t to_write = 0;
830                                 bool new_mbuf = true;
831                                 uint16_t write_off = 0;
832
833                                 /* copy part of packet to first area */
834                                 while (m && written < write_len) {
835                                         to_write = RTE_MIN(m->data_len,
836                                                         write_len - written);
837                                         rte_memcpy(tmp_dst,
838                                                 rte_pktmbuf_mtod(m,
839                                                         const void *),
840                                                 to_write);
841
842                                         tmp_dst = ((uint8_t *)(tmp_dst)) +
843                                                 to_write;
844                                         if (m->data_len <= write_len -
845                                                         written) {
846                                                 m = m->next;
847                                                 new_mbuf = true;
848                                         } else {
849                                                 new_mbuf = false;
850                                         }
851                                         written += to_write;
852                                 }
853
854                                 if (lck->next)
855                                         dst = lck->next->start;
856
857                                 tmp_dst = dst;
858                                 written = 0;
859                                 write_off = new_mbuf ? 0 : to_write;
860
861                                 /* copy part of packet to second area */
862                                 while (m && written < pkt_len - write_len) {
863                                         rte_memcpy(tmp_dst, (const void *)
864                                                 (rte_pktmbuf_mtod(m,
865                                                 uint8_t *) + write_off),
866                                                 m->data_len - write_off);
867
868                                         tmp_dst = ((uint8_t *)(tmp_dst)) +
869                                                 (m->data_len - write_off);
870                                         written += m->data_len - write_off;
871                                         m = m->next;
872                                         write_off = 0;
873                                 }
874                         }
875
876                         dst = ((uint8_t *)dst) + rem_len;
877                         unlock_size += hwpkt_len;
878                         lock_size = lock_size2 - rem_len;
879                         lock_size2 = 0;
880
881                         rte_pktmbuf_free(mbuf);
882                         num_tx++;
883                 }
884
885                 szedata_tx_unlock_data(sze_q->sze, lck, unlock_size);
886                 pkt_left--;
887         }
888
889         sze_q->tx_pkts += num_tx;
890         sze_q->err_pkts += nb_pkts - num_tx;
891         sze_q->tx_bytes += num_bytes;
892         return num_tx;
893 }
894
895 static int
896 eth_rx_queue_start(struct rte_eth_dev *dev, uint16_t rxq_id)
897 {
898         struct szedata2_rx_queue *rxq = dev->data->rx_queues[rxq_id];
899         int ret;
900         struct pmd_internals *internals = (struct pmd_internals *)
901                 dev->data->dev_private;
902
903         if (rxq->sze == NULL) {
904                 uint32_t rx = 1 << rxq->rx_channel;
905                 uint32_t tx = 0;
906                 rxq->sze = szedata_open(internals->sze_dev_path);
907                 if (rxq->sze == NULL)
908                         return -EINVAL;
909                 ret = szedata_subscribe3(rxq->sze, &rx, &tx);
910                 if (ret != 0 || rx == 0)
911                         goto err;
912         }
913
914         ret = szedata_start(rxq->sze);
915         if (ret != 0)
916                 goto err;
917         dev->data->rx_queue_state[rxq_id] = RTE_ETH_QUEUE_STATE_STARTED;
918         return 0;
919
920 err:
921         szedata_close(rxq->sze);
922         rxq->sze = NULL;
923         return -EINVAL;
924 }
925
926 static int
927 eth_rx_queue_stop(struct rte_eth_dev *dev, uint16_t rxq_id)
928 {
929         struct szedata2_rx_queue *rxq = dev->data->rx_queues[rxq_id];
930
931         if (rxq->sze != NULL) {
932                 szedata_close(rxq->sze);
933                 rxq->sze = NULL;
934         }
935
936         dev->data->rx_queue_state[rxq_id] = RTE_ETH_QUEUE_STATE_STOPPED;
937         return 0;
938 }
939
940 static int
941 eth_tx_queue_start(struct rte_eth_dev *dev, uint16_t txq_id)
942 {
943         struct szedata2_tx_queue *txq = dev->data->tx_queues[txq_id];
944         int ret;
945         struct pmd_internals *internals = (struct pmd_internals *)
946                 dev->data->dev_private;
947
948         if (txq->sze == NULL) {
949                 uint32_t rx = 0;
950                 uint32_t tx = 1 << txq->tx_channel;
951                 txq->sze = szedata_open(internals->sze_dev_path);
952                 if (txq->sze == NULL)
953                         return -EINVAL;
954                 ret = szedata_subscribe3(txq->sze, &rx, &tx);
955                 if (ret != 0 || tx == 0)
956                         goto err;
957         }
958
959         ret = szedata_start(txq->sze);
960         if (ret != 0)
961                 goto err;
962         dev->data->tx_queue_state[txq_id] = RTE_ETH_QUEUE_STATE_STARTED;
963         return 0;
964
965 err:
966         szedata_close(txq->sze);
967         txq->sze = NULL;
968         return -EINVAL;
969 }
970
971 static int
972 eth_tx_queue_stop(struct rte_eth_dev *dev, uint16_t txq_id)
973 {
974         struct szedata2_tx_queue *txq = dev->data->tx_queues[txq_id];
975
976         if (txq->sze != NULL) {
977                 szedata_close(txq->sze);
978                 txq->sze = NULL;
979         }
980
981         dev->data->tx_queue_state[txq_id] = RTE_ETH_QUEUE_STATE_STOPPED;
982         return 0;
983 }
984
985 static int
986 eth_dev_start(struct rte_eth_dev *dev)
987 {
988         int ret;
989         uint16_t i;
990         uint16_t nb_rx = dev->data->nb_rx_queues;
991         uint16_t nb_tx = dev->data->nb_tx_queues;
992
993         for (i = 0; i < nb_rx; i++) {
994                 ret = eth_rx_queue_start(dev, i);
995                 if (ret != 0)
996                         goto err_rx;
997         }
998
999         for (i = 0; i < nb_tx; i++) {
1000                 ret = eth_tx_queue_start(dev, i);
1001                 if (ret != 0)
1002                         goto err_tx;
1003         }
1004
1005         return 0;
1006
1007 err_tx:
1008         for (i = 0; i < nb_tx; i++)
1009                 eth_tx_queue_stop(dev, i);
1010 err_rx:
1011         for (i = 0; i < nb_rx; i++)
1012                 eth_rx_queue_stop(dev, i);
1013         return ret;
1014 }
1015
1016 static void
1017 eth_dev_stop(struct rte_eth_dev *dev)
1018 {
1019         uint16_t i;
1020         uint16_t nb_rx = dev->data->nb_rx_queues;
1021         uint16_t nb_tx = dev->data->nb_tx_queues;
1022
1023         for (i = 0; i < nb_tx; i++)
1024                 eth_tx_queue_stop(dev, i);
1025
1026         for (i = 0; i < nb_rx; i++)
1027                 eth_rx_queue_stop(dev, i);
1028 }
1029
1030 static int
1031 eth_dev_configure(struct rte_eth_dev *dev)
1032 {
1033         struct rte_eth_dev_data *data = dev->data;
1034         if (data->dev_conf.rxmode.offloads & DEV_RX_OFFLOAD_SCATTER) {
1035                 dev->rx_pkt_burst = eth_szedata2_rx_scattered;
1036                 data->scattered_rx = 1;
1037         } else {
1038                 dev->rx_pkt_burst = eth_szedata2_rx;
1039                 data->scattered_rx = 0;
1040         }
1041         return 0;
1042 }
1043
1044 static int
1045 eth_dev_info(struct rte_eth_dev *dev,
1046                 struct rte_eth_dev_info *dev_info)
1047 {
1048         struct pmd_internals *internals = dev->data->dev_private;
1049
1050         dev_info->if_index = 0;
1051         dev_info->max_mac_addrs = 1;
1052         dev_info->max_rx_pktlen = (uint32_t)-1;
1053         dev_info->max_rx_queues = internals->max_rx_queues;
1054         dev_info->max_tx_queues = internals->max_tx_queues;
1055         dev_info->min_rx_bufsize = 0;
1056         dev_info->rx_offload_capa = DEV_RX_OFFLOAD_SCATTER;
1057         dev_info->tx_offload_capa = 0;
1058         dev_info->rx_queue_offload_capa = 0;
1059         dev_info->tx_queue_offload_capa = 0;
1060         dev_info->speed_capa = ETH_LINK_SPEED_100G;
1061
1062         return 0;
1063 }
1064
1065 static int
1066 eth_stats_get(struct rte_eth_dev *dev,
1067                 struct rte_eth_stats *stats)
1068 {
1069         uint16_t i;
1070         uint16_t nb_rx = dev->data->nb_rx_queues;
1071         uint16_t nb_tx = dev->data->nb_tx_queues;
1072         uint64_t rx_total = 0;
1073         uint64_t tx_total = 0;
1074         uint64_t tx_err_total = 0;
1075         uint64_t rx_total_bytes = 0;
1076         uint64_t tx_total_bytes = 0;
1077
1078         for (i = 0; i < nb_rx; i++) {
1079                 struct szedata2_rx_queue *rxq = dev->data->rx_queues[i];
1080
1081                 if (i < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
1082                         stats->q_ipackets[i] = rxq->rx_pkts;
1083                         stats->q_ibytes[i] = rxq->rx_bytes;
1084                 }
1085                 rx_total += rxq->rx_pkts;
1086                 rx_total_bytes += rxq->rx_bytes;
1087         }
1088
1089         for (i = 0; i < nb_tx; i++) {
1090                 struct szedata2_tx_queue *txq = dev->data->tx_queues[i];
1091
1092                 if (i < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
1093                         stats->q_opackets[i] = txq->tx_pkts;
1094                         stats->q_obytes[i] = txq->tx_bytes;
1095                 }
1096                 tx_total += txq->tx_pkts;
1097                 tx_total_bytes += txq->tx_bytes;
1098                 tx_err_total += txq->err_pkts;
1099         }
1100
1101         stats->ipackets = rx_total;
1102         stats->opackets = tx_total;
1103         stats->ibytes = rx_total_bytes;
1104         stats->obytes = tx_total_bytes;
1105         stats->oerrors = tx_err_total;
1106         stats->rx_nombuf = dev->data->rx_mbuf_alloc_failed;
1107
1108         return 0;
1109 }
1110
1111 static int
1112 eth_stats_reset(struct rte_eth_dev *dev)
1113 {
1114         uint16_t i;
1115         uint16_t nb_rx = dev->data->nb_rx_queues;
1116         uint16_t nb_tx = dev->data->nb_tx_queues;
1117
1118         for (i = 0; i < nb_rx; i++) {
1119                 struct szedata2_rx_queue *rxq = dev->data->rx_queues[i];
1120                 rxq->rx_pkts = 0;
1121                 rxq->rx_bytes = 0;
1122                 rxq->err_pkts = 0;
1123         }
1124         for (i = 0; i < nb_tx; i++) {
1125                 struct szedata2_tx_queue *txq = dev->data->tx_queues[i];
1126                 txq->tx_pkts = 0;
1127                 txq->tx_bytes = 0;
1128                 txq->err_pkts = 0;
1129         }
1130
1131         return 0;
1132 }
1133
1134 static void
1135 eth_rx_queue_release(void *q)
1136 {
1137         struct szedata2_rx_queue *rxq = (struct szedata2_rx_queue *)q;
1138
1139         if (rxq != NULL) {
1140                 if (rxq->sze != NULL)
1141                         szedata_close(rxq->sze);
1142                 rte_free(rxq);
1143         }
1144 }
1145
1146 static void
1147 eth_tx_queue_release(void *q)
1148 {
1149         struct szedata2_tx_queue *txq = (struct szedata2_tx_queue *)q;
1150
1151         if (txq != NULL) {
1152                 if (txq->sze != NULL)
1153                         szedata_close(txq->sze);
1154                 rte_free(txq);
1155         }
1156 }
1157
1158 static void
1159 eth_dev_close(struct rte_eth_dev *dev)
1160 {
1161         struct pmd_internals *internals = dev->data->dev_private;
1162         uint16_t i;
1163         uint16_t nb_rx = dev->data->nb_rx_queues;
1164         uint16_t nb_tx = dev->data->nb_tx_queues;
1165
1166         eth_dev_stop(dev);
1167
1168         free(internals->sze_dev_path);
1169
1170         for (i = 0; i < nb_rx; i++) {
1171                 eth_rx_queue_release(dev->data->rx_queues[i]);
1172                 dev->data->rx_queues[i] = NULL;
1173         }
1174         dev->data->nb_rx_queues = 0;
1175         for (i = 0; i < nb_tx; i++) {
1176                 eth_tx_queue_release(dev->data->tx_queues[i]);
1177                 dev->data->tx_queues[i] = NULL;
1178         }
1179         dev->data->nb_tx_queues = 0;
1180
1181         rte_free(dev->data->mac_addrs);
1182         dev->data->mac_addrs = NULL;
1183 }
1184
1185 static int
1186 eth_link_update(struct rte_eth_dev *dev,
1187                 int wait_to_complete __rte_unused)
1188 {
1189         struct rte_eth_link link;
1190
1191         memset(&link, 0, sizeof(link));
1192
1193         link.link_speed = ETH_SPEED_NUM_100G;
1194         link.link_duplex = ETH_LINK_FULL_DUPLEX;
1195         link.link_status = ETH_LINK_UP;
1196         link.link_autoneg = ETH_LINK_FIXED;
1197
1198         rte_eth_linkstatus_set(dev, &link);
1199         return 0;
1200 }
1201
1202 static int
1203 eth_dev_set_link_up(struct rte_eth_dev *dev __rte_unused)
1204 {
1205         PMD_DRV_LOG(WARNING, "Setting link up is not supported.");
1206         return 0;
1207 }
1208
1209 static int
1210 eth_dev_set_link_down(struct rte_eth_dev *dev __rte_unused)
1211 {
1212         PMD_DRV_LOG(WARNING, "Setting link down is not supported.");
1213         return 0;
1214 }
1215
1216 static int
1217 eth_rx_queue_setup(struct rte_eth_dev *dev,
1218                 uint16_t rx_queue_id,
1219                 uint16_t nb_rx_desc __rte_unused,
1220                 unsigned int socket_id,
1221                 const struct rte_eth_rxconf *rx_conf __rte_unused,
1222                 struct rte_mempool *mb_pool)
1223 {
1224         struct szedata2_rx_queue *rxq;
1225         int ret;
1226         struct pmd_internals *internals = dev->data->dev_private;
1227         uint8_t rx_channel = internals->rxq_base_id + rx_queue_id;
1228         uint32_t rx = 1 << rx_channel;
1229         uint32_t tx = 0;
1230
1231         PMD_INIT_FUNC_TRACE();
1232
1233         if (dev->data->rx_queues[rx_queue_id] != NULL) {
1234                 eth_rx_queue_release(dev->data->rx_queues[rx_queue_id]);
1235                 dev->data->rx_queues[rx_queue_id] = NULL;
1236         }
1237
1238         rxq = rte_zmalloc_socket("szedata2 rx queue",
1239                         sizeof(struct szedata2_rx_queue),
1240                         RTE_CACHE_LINE_SIZE, socket_id);
1241         if (rxq == NULL) {
1242                 PMD_INIT_LOG(ERR, "rte_zmalloc_socket() failed for rx queue id "
1243                                 "%" PRIu16 "!", rx_queue_id);
1244                 return -ENOMEM;
1245         }
1246
1247         rxq->priv = internals;
1248         rxq->sze = szedata_open(internals->sze_dev_path);
1249         if (rxq->sze == NULL) {
1250                 PMD_INIT_LOG(ERR, "szedata_open() failed for rx queue id "
1251                                 "%" PRIu16 "!", rx_queue_id);
1252                 eth_rx_queue_release(rxq);
1253                 return -EINVAL;
1254         }
1255         ret = szedata_subscribe3(rxq->sze, &rx, &tx);
1256         if (ret != 0 || rx == 0) {
1257                 PMD_INIT_LOG(ERR, "szedata_subscribe3() failed for rx queue id "
1258                                 "%" PRIu16 "!", rx_queue_id);
1259                 eth_rx_queue_release(rxq);
1260                 return -EINVAL;
1261         }
1262         rxq->rx_channel = rx_channel;
1263         rxq->qid = rx_queue_id;
1264         rxq->in_port = dev->data->port_id;
1265         rxq->mb_pool = mb_pool;
1266         rxq->rx_pkts = 0;
1267         rxq->rx_bytes = 0;
1268         rxq->err_pkts = 0;
1269
1270         dev->data->rx_queues[rx_queue_id] = rxq;
1271
1272         PMD_INIT_LOG(DEBUG, "Configured rx queue id %" PRIu16 " on socket "
1273                         "%u (channel id %u).", rxq->qid, socket_id,
1274                         rxq->rx_channel);
1275
1276         return 0;
1277 }
1278
1279 static int
1280 eth_tx_queue_setup(struct rte_eth_dev *dev,
1281                 uint16_t tx_queue_id,
1282                 uint16_t nb_tx_desc __rte_unused,
1283                 unsigned int socket_id,
1284                 const struct rte_eth_txconf *tx_conf __rte_unused)
1285 {
1286         struct szedata2_tx_queue *txq;
1287         int ret;
1288         struct pmd_internals *internals = dev->data->dev_private;
1289         uint8_t tx_channel = internals->txq_base_id + tx_queue_id;
1290         uint32_t rx = 0;
1291         uint32_t tx = 1 << tx_channel;
1292
1293         PMD_INIT_FUNC_TRACE();
1294
1295         if (dev->data->tx_queues[tx_queue_id] != NULL) {
1296                 eth_tx_queue_release(dev->data->tx_queues[tx_queue_id]);
1297                 dev->data->tx_queues[tx_queue_id] = NULL;
1298         }
1299
1300         txq = rte_zmalloc_socket("szedata2 tx queue",
1301                         sizeof(struct szedata2_tx_queue),
1302                         RTE_CACHE_LINE_SIZE, socket_id);
1303         if (txq == NULL) {
1304                 PMD_INIT_LOG(ERR, "rte_zmalloc_socket() failed for tx queue id "
1305                                 "%" PRIu16 "!", tx_queue_id);
1306                 return -ENOMEM;
1307         }
1308
1309         txq->priv = internals;
1310         txq->sze = szedata_open(internals->sze_dev_path);
1311         if (txq->sze == NULL) {
1312                 PMD_INIT_LOG(ERR, "szedata_open() failed for tx queue id "
1313                                 "%" PRIu16 "!", tx_queue_id);
1314                 eth_tx_queue_release(txq);
1315                 return -EINVAL;
1316         }
1317         ret = szedata_subscribe3(txq->sze, &rx, &tx);
1318         if (ret != 0 || tx == 0) {
1319                 PMD_INIT_LOG(ERR, "szedata_subscribe3() failed for tx queue id "
1320                                 "%" PRIu16 "!", tx_queue_id);
1321                 eth_tx_queue_release(txq);
1322                 return -EINVAL;
1323         }
1324         txq->tx_channel = tx_channel;
1325         txq->qid = tx_queue_id;
1326         txq->tx_pkts = 0;
1327         txq->tx_bytes = 0;
1328         txq->err_pkts = 0;
1329
1330         dev->data->tx_queues[tx_queue_id] = txq;
1331
1332         PMD_INIT_LOG(DEBUG, "Configured tx queue id %" PRIu16 " on socket "
1333                         "%u (channel id %u).", txq->qid, socket_id,
1334                         txq->tx_channel);
1335
1336         return 0;
1337 }
1338
1339 static int
1340 eth_mac_addr_set(struct rte_eth_dev *dev __rte_unused,
1341                 struct rte_ether_addr *mac_addr __rte_unused)
1342 {
1343         return 0;
1344 }
1345
1346 static int
1347 eth_promiscuous_enable(struct rte_eth_dev *dev __rte_unused)
1348 {
1349         PMD_DRV_LOG(WARNING, "Enabling promiscuous mode is not supported. "
1350                         "The card is always in promiscuous mode.");
1351         return 0;
1352 }
1353
1354 static int
1355 eth_promiscuous_disable(struct rte_eth_dev *dev __rte_unused)
1356 {
1357         PMD_DRV_LOG(WARNING, "Disabling promiscuous mode is not supported. "
1358                         "The card is always in promiscuous mode.");
1359         return -ENOTSUP;
1360 }
1361
1362 static int
1363 eth_allmulticast_enable(struct rte_eth_dev *dev __rte_unused)
1364 {
1365         PMD_DRV_LOG(WARNING, "Enabling allmulticast mode is not supported.");
1366         return -ENOTSUP;
1367 }
1368
1369 static int
1370 eth_allmulticast_disable(struct rte_eth_dev *dev __rte_unused)
1371 {
1372         PMD_DRV_LOG(WARNING, "Disabling allmulticast mode is not supported.");
1373         return -ENOTSUP;
1374 }
1375
1376 static const struct eth_dev_ops ops = {
1377         .dev_start          = eth_dev_start,
1378         .dev_stop           = eth_dev_stop,
1379         .dev_set_link_up    = eth_dev_set_link_up,
1380         .dev_set_link_down  = eth_dev_set_link_down,
1381         .dev_close          = eth_dev_close,
1382         .dev_configure      = eth_dev_configure,
1383         .dev_infos_get      = eth_dev_info,
1384         .promiscuous_enable   = eth_promiscuous_enable,
1385         .promiscuous_disable  = eth_promiscuous_disable,
1386         .allmulticast_enable  = eth_allmulticast_enable,
1387         .allmulticast_disable = eth_allmulticast_disable,
1388         .rx_queue_start     = eth_rx_queue_start,
1389         .rx_queue_stop      = eth_rx_queue_stop,
1390         .tx_queue_start     = eth_tx_queue_start,
1391         .tx_queue_stop      = eth_tx_queue_stop,
1392         .rx_queue_setup     = eth_rx_queue_setup,
1393         .tx_queue_setup     = eth_tx_queue_setup,
1394         .rx_queue_release   = eth_rx_queue_release,
1395         .tx_queue_release   = eth_tx_queue_release,
1396         .link_update        = eth_link_update,
1397         .stats_get          = eth_stats_get,
1398         .stats_reset        = eth_stats_reset,
1399         .mac_addr_set       = eth_mac_addr_set,
1400 };
1401
1402 /*
1403  * This function goes through sysfs and looks for an index of szedata2
1404  * device file (/dev/szedataIIX, where X is the index).
1405  *
1406  * @return
1407  *           0 on success
1408  *          -1 on error
1409  */
1410 static int
1411 get_szedata2_index(const struct rte_pci_addr *pcislot_addr, uint32_t *index)
1412 {
1413         DIR *dir;
1414         struct dirent *entry;
1415         int ret;
1416         uint32_t tmp_index;
1417         FILE *fd;
1418         char pcislot_path[PATH_MAX];
1419         uint32_t domain;
1420         uint8_t bus;
1421         uint8_t devid;
1422         uint8_t function;
1423
1424         dir = opendir("/sys/class/combo");
1425         if (dir == NULL)
1426                 return -1;
1427
1428         /*
1429          * Iterate through all combosixX directories.
1430          * When the value in /sys/class/combo/combosixX/device/pcislot
1431          * file is the location of the ethernet device dev, "X" is the
1432          * index of the device.
1433          */
1434         while ((entry = readdir(dir)) != NULL) {
1435                 ret = sscanf(entry->d_name, "combosix%u", &tmp_index);
1436                 if (ret != 1)
1437                         continue;
1438
1439                 snprintf(pcislot_path, PATH_MAX,
1440                         "/sys/class/combo/combosix%u/device/pcislot",
1441                         tmp_index);
1442
1443                 fd = fopen(pcislot_path, "r");
1444                 if (fd == NULL)
1445                         continue;
1446
1447                 ret = fscanf(fd, "%8" SCNx32 ":%2" SCNx8 ":%2" SCNx8 ".%" SCNx8,
1448                                 &domain, &bus, &devid, &function);
1449                 fclose(fd);
1450                 if (ret != 4)
1451                         continue;
1452
1453                 if (pcislot_addr->domain == domain &&
1454                                 pcislot_addr->bus == bus &&
1455                                 pcislot_addr->devid == devid &&
1456                                 pcislot_addr->function == function) {
1457                         *index = tmp_index;
1458                         closedir(dir);
1459                         return 0;
1460                 }
1461         }
1462
1463         closedir(dir);
1464         return -1;
1465 }
1466
1467 /**
1468  * @brief Initializes rte_eth_dev device.
1469  * @param dev Device to initialize.
1470  * @param pi Structure with info about DMA queues.
1471  * @return 0 on success, negative error code on error.
1472  */
1473 static int
1474 rte_szedata2_eth_dev_init(struct rte_eth_dev *dev, struct port_info *pi)
1475 {
1476         int ret;
1477         uint32_t szedata2_index;
1478         char name[PATH_MAX];
1479         struct rte_eth_dev_data *data = dev->data;
1480         struct pmd_internals *internals = (struct pmd_internals *)
1481                 data->dev_private;
1482         struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
1483
1484         PMD_INIT_FUNC_TRACE();
1485
1486         PMD_INIT_LOG(INFO, "Initializing eth_dev %s (driver %s)", data->name,
1487                         RTE_STR(RTE_SZEDATA2_DRIVER_NAME));
1488
1489         /* Let rte_eth_dev_close() release the port resources */
1490         dev->data->dev_flags |= RTE_ETH_DEV_CLOSE_REMOVE;
1491
1492         /* Fill internal private structure. */
1493         internals->dev = dev;
1494         /* Get index of szedata2 device file and create path to device file */
1495         ret = get_szedata2_index(&pci_dev->addr, &szedata2_index);
1496         if (ret != 0) {
1497                 PMD_INIT_LOG(ERR, "Failed to get szedata2 device index!");
1498                 return -ENODEV;
1499         }
1500         snprintf(name, PATH_MAX, SZEDATA2_DEV_PATH_FMT, szedata2_index);
1501         internals->sze_dev_path = strdup(name);
1502         if (internals->sze_dev_path == NULL) {
1503                 PMD_INIT_LOG(ERR, "strdup() failed!");
1504                 return -ENOMEM;
1505         }
1506         PMD_INIT_LOG(INFO, "SZEDATA2 path: %s", internals->sze_dev_path);
1507         internals->max_rx_queues = pi->rx_count;
1508         internals->max_tx_queues = pi->tx_count;
1509         internals->rxq_base_id = pi->rx_base_id;
1510         internals->txq_base_id = pi->tx_base_id;
1511         PMD_INIT_LOG(INFO, "%u RX DMA channels from id %u",
1512                         internals->max_rx_queues, internals->rxq_base_id);
1513         PMD_INIT_LOG(INFO, "%u TX DMA channels from id %u",
1514                         internals->max_tx_queues, internals->txq_base_id);
1515
1516         /* Set rx, tx burst functions */
1517         if (data->scattered_rx == 1)
1518                 dev->rx_pkt_burst = eth_szedata2_rx_scattered;
1519         else
1520                 dev->rx_pkt_burst = eth_szedata2_rx;
1521         dev->tx_pkt_burst = eth_szedata2_tx;
1522
1523         /* Set function callbacks for Ethernet API */
1524         dev->dev_ops = &ops;
1525
1526         /* Get link state */
1527         eth_link_update(dev, 0);
1528
1529         /* Allocate space for one mac address */
1530         data->mac_addrs = rte_zmalloc(data->name, sizeof(struct rte_ether_addr),
1531                         RTE_CACHE_LINE_SIZE);
1532         if (data->mac_addrs == NULL) {
1533                 PMD_INIT_LOG(ERR, "Could not alloc space for MAC address!");
1534                 free(internals->sze_dev_path);
1535                 return -ENOMEM;
1536         }
1537
1538         rte_ether_addr_copy(&eth_addr, data->mac_addrs);
1539
1540         PMD_INIT_LOG(INFO, "%s device %s successfully initialized",
1541                         RTE_STR(RTE_SZEDATA2_DRIVER_NAME), data->name);
1542
1543         return 0;
1544 }
1545
1546 /**
1547  * @brief Unitializes rte_eth_dev device.
1548  * @param dev Device to uninitialize.
1549  * @return 0 on success, negative error code on error.
1550  */
1551 static int
1552 rte_szedata2_eth_dev_uninit(struct rte_eth_dev *dev)
1553 {
1554         PMD_INIT_FUNC_TRACE();
1555
1556         eth_dev_close(dev);
1557
1558         PMD_DRV_LOG(INFO, "%s device %s successfully uninitialized",
1559                         RTE_STR(RTE_SZEDATA2_DRIVER_NAME), dev->data->name);
1560
1561         return 0;
1562 }
1563
1564 static const struct rte_pci_id rte_szedata2_pci_id_table[] = {
1565         {
1566                 RTE_PCI_DEVICE(PCI_VENDOR_ID_NETCOPE,
1567                                 PCI_DEVICE_ID_NETCOPE_COMBO80G)
1568         },
1569         {
1570                 RTE_PCI_DEVICE(PCI_VENDOR_ID_NETCOPE,
1571                                 PCI_DEVICE_ID_NETCOPE_COMBO100G)
1572         },
1573         {
1574                 RTE_PCI_DEVICE(PCI_VENDOR_ID_NETCOPE,
1575                                 PCI_DEVICE_ID_NETCOPE_COMBO100G2)
1576         },
1577         {
1578                 RTE_PCI_DEVICE(PCI_VENDOR_ID_NETCOPE,
1579                                 PCI_DEVICE_ID_NETCOPE_NFB200G2QL)
1580         },
1581         {
1582                 RTE_PCI_DEVICE(PCI_VENDOR_ID_SILICOM,
1583                                 PCI_DEVICE_ID_FB2CGG3)
1584         },
1585         {
1586                 RTE_PCI_DEVICE(PCI_VENDOR_ID_SILICOM,
1587                                 PCI_DEVICE_ID_FB2CGG3D)
1588         },
1589         {
1590                 .vendor_id = 0,
1591         }
1592 };
1593
1594 /**
1595  * @brief Gets info about DMA queues for ports.
1596  * @param pci_dev PCI device structure.
1597  * @param port_count Pointer to variable set with number of ports.
1598  * @param pi Pointer to array of structures with info about DMA queues
1599  *           for ports.
1600  * @param max_ports Maximum number of ports.
1601  * @return 0 on success, negative error code on error.
1602  */
1603 static int
1604 get_port_info(struct rte_pci_device *pci_dev, unsigned int *port_count,
1605                 struct port_info *pi, unsigned int max_ports)
1606 {
1607         struct szedata *szedata_temp;
1608         char sze_dev_path[PATH_MAX];
1609         uint32_t szedata2_index;
1610         int ret;
1611         uint16_t max_rx_queues;
1612         uint16_t max_tx_queues;
1613
1614         if (max_ports == 0)
1615                 return -EINVAL;
1616
1617         memset(pi, 0, max_ports * sizeof(struct port_info));
1618         *port_count = 0;
1619
1620         /* Get index of szedata2 device file and create path to device file */
1621         ret = get_szedata2_index(&pci_dev->addr, &szedata2_index);
1622         if (ret != 0) {
1623                 PMD_INIT_LOG(ERR, "Failed to get szedata2 device index!");
1624                 return -ENODEV;
1625         }
1626         snprintf(sze_dev_path, PATH_MAX, SZEDATA2_DEV_PATH_FMT, szedata2_index);
1627
1628         /*
1629          * Get number of available DMA RX and TX channels, which is maximum
1630          * number of queues that can be created.
1631          */
1632         szedata_temp = szedata_open(sze_dev_path);
1633         if (szedata_temp == NULL) {
1634                 PMD_INIT_LOG(ERR, "szedata_open(%s) failed", sze_dev_path);
1635                 return -EINVAL;
1636         }
1637         max_rx_queues = szedata_ifaces_available(szedata_temp, SZE2_DIR_RX);
1638         max_tx_queues = szedata_ifaces_available(szedata_temp, SZE2_DIR_TX);
1639         PMD_INIT_LOG(INFO, "Available DMA channels RX: %u TX: %u",
1640                         max_rx_queues, max_tx_queues);
1641         if (max_rx_queues > RTE_ETH_SZEDATA2_MAX_RX_QUEUES) {
1642                 PMD_INIT_LOG(ERR, "%u RX queues exceeds supported number %u",
1643                                 max_rx_queues, RTE_ETH_SZEDATA2_MAX_RX_QUEUES);
1644                 szedata_close(szedata_temp);
1645                 return -EINVAL;
1646         }
1647         if (max_tx_queues > RTE_ETH_SZEDATA2_MAX_TX_QUEUES) {
1648                 PMD_INIT_LOG(ERR, "%u TX queues exceeds supported number %u",
1649                                 max_tx_queues, RTE_ETH_SZEDATA2_MAX_TX_QUEUES);
1650                 szedata_close(szedata_temp);
1651                 return -EINVAL;
1652         }
1653
1654         if (pci_dev->id.device_id == PCI_DEVICE_ID_NETCOPE_NFB200G2QL) {
1655                 unsigned int i;
1656                 unsigned int rx_queues = max_rx_queues / max_ports;
1657                 unsigned int tx_queues = max_tx_queues / max_ports;
1658
1659                 /*
1660                  * Number of queues reported by szedata_ifaces_available()
1661                  * is the number of all queues from all DMA controllers which
1662                  * may reside at different numa locations.
1663                  * All queues from the same DMA controller have the same numa
1664                  * node.
1665                  * Numa node from the first queue of each DMA controller is
1666                  * retrieved.
1667                  * If the numa node differs from the numa node of the queues
1668                  * from the previous DMA controller the queues are assigned
1669                  * to the next port.
1670                  */
1671
1672                 for (i = 0; i < max_ports; i++) {
1673                         int numa_rx = szedata_get_area_numa_node(szedata_temp,
1674                                 SZE2_DIR_RX, rx_queues * i);
1675                         int numa_tx = szedata_get_area_numa_node(szedata_temp,
1676                                 SZE2_DIR_TX, tx_queues * i);
1677                         unsigned int port_rx_queues = numa_rx != -1 ?
1678                                 rx_queues : 0;
1679                         unsigned int port_tx_queues = numa_tx != -1 ?
1680                                 tx_queues : 0;
1681                         PMD_INIT_LOG(DEBUG, "%u rx queues from id %u, numa %d",
1682                                         rx_queues, rx_queues * i, numa_rx);
1683                         PMD_INIT_LOG(DEBUG, "%u tx queues from id %u, numa %d",
1684                                         tx_queues, tx_queues * i, numa_tx);
1685
1686                         if (port_rx_queues != 0 && port_tx_queues != 0 &&
1687                                         numa_rx != numa_tx) {
1688                                 PMD_INIT_LOG(ERR, "RX queue %u numa %d differs "
1689                                                 "from TX queue %u numa %d "
1690                                                 "unexpectedly",
1691                                                 rx_queues * i, numa_rx,
1692                                                 tx_queues * i, numa_tx);
1693                                 szedata_close(szedata_temp);
1694                                 return -EINVAL;
1695                         } else if (port_rx_queues == 0 && port_tx_queues == 0) {
1696                                 continue;
1697                         } else {
1698                                 unsigned int j;
1699                                 unsigned int current = *port_count;
1700                                 int port_numa = port_rx_queues != 0 ?
1701                                         numa_rx : numa_tx;
1702
1703                                 for (j = 0; j < *port_count; j++) {
1704                                         if (pi[j].numa_node ==
1705                                                         port_numa) {
1706                                                 current = j;
1707                                                 break;
1708                                         }
1709                                 }
1710                                 if (pi[current].rx_count == 0 &&
1711                                                 pi[current].tx_count == 0) {
1712                                         pi[current].rx_base_id = rx_queues * i;
1713                                         pi[current].tx_base_id = tx_queues * i;
1714                                         (*port_count)++;
1715                                 } else if ((rx_queues * i !=
1716                                                 pi[current].rx_base_id +
1717                                                 pi[current].rx_count) ||
1718                                                 (tx_queues * i !=
1719                                                  pi[current].tx_base_id +
1720                                                  pi[current].tx_count)) {
1721                                         PMD_INIT_LOG(ERR, "Queue ids does not "
1722                                                         "fulfill constraints");
1723                                         szedata_close(szedata_temp);
1724                                         return -EINVAL;
1725                                 }
1726                                 pi[current].rx_count += port_rx_queues;
1727                                 pi[current].tx_count += port_tx_queues;
1728                                 pi[current].numa_node = port_numa;
1729                         }
1730                 }
1731         } else {
1732                 pi[0].rx_count = max_rx_queues;
1733                 pi[0].tx_count = max_tx_queues;
1734                 pi[0].numa_node = pci_dev->device.numa_node;
1735                 *port_count = 1;
1736         }
1737
1738         szedata_close(szedata_temp);
1739         return 0;
1740 }
1741
1742 /**
1743  * @brief Allocates rte_eth_dev device.
1744  * @param pci_dev Corresponding PCI device.
1745  * @param numa_node NUMA node on which device is allocated.
1746  * @param port_no Id of rte_eth_device created on PCI device pci_dev.
1747  * @return Pointer to allocated device or NULL on error.
1748  */
1749 static struct rte_eth_dev *
1750 szedata2_eth_dev_allocate(struct rte_pci_device *pci_dev, int numa_node,
1751                 unsigned int port_no)
1752 {
1753         struct rte_eth_dev *eth_dev;
1754         char name[RTE_ETH_NAME_MAX_LEN];
1755
1756         PMD_INIT_FUNC_TRACE();
1757
1758         snprintf(name, RTE_ETH_NAME_MAX_LEN, "%s"
1759                         SZEDATA2_ETH_DEV_NAME_SUFFIX_FMT,
1760                         pci_dev->device.name, port_no);
1761         PMD_INIT_LOG(DEBUG, "Allocating eth_dev %s", name);
1762
1763         if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
1764                 eth_dev = rte_eth_dev_allocate(name);
1765                 if (!eth_dev)
1766                         return NULL;
1767
1768                 eth_dev->data->dev_private = rte_zmalloc_socket(name,
1769                         sizeof(struct pmd_internals), RTE_CACHE_LINE_SIZE,
1770                         numa_node);
1771                 if (!eth_dev->data->dev_private) {
1772                         rte_eth_dev_release_port(eth_dev);
1773                         return NULL;
1774                 }
1775         } else {
1776                 eth_dev = rte_eth_dev_attach_secondary(name);
1777                 if (!eth_dev)
1778                         return NULL;
1779         }
1780
1781         eth_dev->device = &pci_dev->device;
1782         rte_eth_copy_pci_info(eth_dev, pci_dev);
1783         eth_dev->data->numa_node = numa_node;
1784         return eth_dev;
1785 }
1786
1787 /**
1788  * @brief Releases interval of rte_eth_dev devices from array.
1789  * @param eth_devs Array of pointers to rte_eth_dev devices.
1790  * @param from Index in array eth_devs to start with.
1791  * @param to Index in array right after the last element to release.
1792  *
1793  * Used for releasing at failed initialization.
1794  */
1795 static void
1796 szedata2_eth_dev_release_interval(struct rte_eth_dev **eth_devs,
1797                 unsigned int from, unsigned int to)
1798 {
1799         unsigned int i;
1800
1801         PMD_INIT_FUNC_TRACE();
1802
1803         for (i = from; i < to; i++) {
1804                 rte_szedata2_eth_dev_uninit(eth_devs[i]);
1805                 rte_eth_dev_pci_release(eth_devs[i]);
1806         }
1807 }
1808
1809 /**
1810  * @brief Callback .probe for struct rte_pci_driver.
1811  */
1812 static int szedata2_eth_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
1813         struct rte_pci_device *pci_dev)
1814 {
1815         struct port_info port_info[SZEDATA2_MAX_PORTS];
1816         unsigned int port_count;
1817         int ret;
1818         unsigned int i;
1819         struct pci_dev_list_entry *list_entry;
1820         struct rte_eth_dev *eth_devs[SZEDATA2_MAX_PORTS] = {NULL,};
1821
1822         PMD_INIT_FUNC_TRACE();
1823
1824         ret = get_port_info(pci_dev, &port_count, port_info,
1825                         SZEDATA2_MAX_PORTS);
1826         if (ret != 0)
1827                 return ret;
1828
1829         if (port_count == 0) {
1830                 PMD_INIT_LOG(ERR, "No available ports!");
1831                 return -ENODEV;
1832         }
1833
1834         list_entry = rte_zmalloc(NULL, sizeof(struct pci_dev_list_entry),
1835                         RTE_CACHE_LINE_SIZE);
1836         if (list_entry == NULL) {
1837                 PMD_INIT_LOG(ERR, "rte_zmalloc() failed!");
1838                 return -ENOMEM;
1839         }
1840
1841         for (i = 0; i < port_count; i++) {
1842                 eth_devs[i] = szedata2_eth_dev_allocate(pci_dev,
1843                                 port_info[i].numa_node, i);
1844                 if (eth_devs[i] == NULL) {
1845                         PMD_INIT_LOG(ERR, "Failed to alloc eth_dev for port %u",
1846                                         i);
1847                         szedata2_eth_dev_release_interval(eth_devs, 0, i);
1848                         rte_free(list_entry);
1849                         return -ENOMEM;
1850                 }
1851
1852                 ret = rte_szedata2_eth_dev_init(eth_devs[i], &port_info[i]);
1853                 if (ret != 0) {
1854                         PMD_INIT_LOG(ERR, "Failed to init eth_dev for port %u",
1855                                         i);
1856                         rte_eth_dev_pci_release(eth_devs[i]);
1857                         szedata2_eth_dev_release_interval(eth_devs, 0, i);
1858                         rte_free(list_entry);
1859                         return ret;
1860                 }
1861
1862                 rte_eth_dev_probing_finish(eth_devs[i]);
1863         }
1864
1865         /*
1866          * Add pci_dev to list of PCI devices for this driver
1867          * which is used at remove callback to release all created eth_devs.
1868          */
1869         list_entry->pci_dev = pci_dev;
1870         list_entry->port_count = port_count;
1871         LIST_INSERT_HEAD(&szedata2_pci_dev_list, list_entry, next);
1872         return 0;
1873 }
1874
1875 /**
1876  * @brief Callback .remove for struct rte_pci_driver.
1877  */
1878 static int szedata2_eth_pci_remove(struct rte_pci_device *pci_dev)
1879 {
1880         unsigned int i;
1881         unsigned int port_count;
1882         char name[RTE_ETH_NAME_MAX_LEN];
1883         struct rte_eth_dev *eth_dev;
1884         int ret;
1885         int retval = 0;
1886         bool found = false;
1887         struct pci_dev_list_entry *list_entry = NULL;
1888
1889         PMD_INIT_FUNC_TRACE();
1890
1891         LIST_FOREACH(list_entry, &szedata2_pci_dev_list, next) {
1892                 if (list_entry->pci_dev == pci_dev) {
1893                         port_count = list_entry->port_count;
1894                         found = true;
1895                         break;
1896                 }
1897         }
1898         LIST_REMOVE(list_entry, next);
1899         rte_free(list_entry);
1900
1901         if (!found) {
1902                 PMD_DRV_LOG(ERR, "PCI device " PCI_PRI_FMT " not found",
1903                                 pci_dev->addr.domain, pci_dev->addr.bus,
1904                                 pci_dev->addr.devid, pci_dev->addr.function);
1905                 return -ENODEV;
1906         }
1907
1908         for (i = 0; i < port_count; i++) {
1909                 snprintf(name, RTE_ETH_NAME_MAX_LEN, "%s"
1910                                 SZEDATA2_ETH_DEV_NAME_SUFFIX_FMT,
1911                                 pci_dev->device.name, i);
1912                 PMD_DRV_LOG(DEBUG, "Removing eth_dev %s", name);
1913                 eth_dev = rte_eth_dev_allocated(name);
1914                 if (!eth_dev) {
1915                         PMD_DRV_LOG(ERR, "eth_dev %s not found", name);
1916                         retval = retval ? retval : -ENODEV;
1917                 }
1918
1919                 ret = rte_szedata2_eth_dev_uninit(eth_dev);
1920                 if (ret != 0) {
1921                         PMD_DRV_LOG(ERR, "eth_dev %s uninit failed", name);
1922                         retval = retval ? retval : ret;
1923                 }
1924
1925                 rte_eth_dev_pci_release(eth_dev);
1926         }
1927
1928         return retval;
1929 }
1930
1931 static struct rte_pci_driver szedata2_eth_driver = {
1932         .id_table = rte_szedata2_pci_id_table,
1933         .probe = szedata2_eth_pci_probe,
1934         .remove = szedata2_eth_pci_remove,
1935 };
1936
1937 RTE_PMD_REGISTER_PCI(RTE_SZEDATA2_DRIVER_NAME, szedata2_eth_driver);
1938 RTE_PMD_REGISTER_PCI_TABLE(RTE_SZEDATA2_DRIVER_NAME, rte_szedata2_pci_id_table);
1939 RTE_PMD_REGISTER_KMOD_DEP(RTE_SZEDATA2_DRIVER_NAME,
1940         "* combo6core & combov3 & szedata2 & ( szedata2_cv3 | szedata2_cv3_fdt )");
1941 RTE_LOG_REGISTER(szedata2_logtype_init, pmd.net.szedata2.init, NOTICE);
1942 RTE_LOG_REGISTER(szedata2_logtype_driver, pmd.net.szedata2.driver, NOTICE);