d36d8c170af4a88ccd62706362a69e2fece045be
[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_iobuf.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 struct pmd_internals {
42         struct rte_eth_dev *dev;
43         uint16_t max_rx_queues;
44         uint16_t max_tx_queues;
45         char sze_dev[PATH_MAX];
46         struct rte_mem_resource *pci_rsc;
47 };
48
49 struct szedata2_rx_queue {
50         struct pmd_internals *priv;
51         struct szedata *sze;
52         uint8_t rx_channel;
53         uint16_t in_port;
54         struct rte_mempool *mb_pool;
55         volatile uint64_t rx_pkts;
56         volatile uint64_t rx_bytes;
57         volatile uint64_t err_pkts;
58 };
59
60 struct szedata2_tx_queue {
61         struct pmd_internals *priv;
62         struct szedata *sze;
63         uint8_t tx_channel;
64         volatile uint64_t tx_pkts;
65         volatile uint64_t tx_bytes;
66         volatile uint64_t err_pkts;
67 };
68
69 static struct ether_addr eth_addr = {
70         .addr_bytes = { 0x00, 0x11, 0x17, 0x00, 0x00, 0x00 }
71 };
72
73 static uint16_t
74 eth_szedata2_rx(void *queue,
75                 struct rte_mbuf **bufs,
76                 uint16_t nb_pkts)
77 {
78         unsigned int i;
79         struct rte_mbuf *mbuf;
80         struct szedata2_rx_queue *sze_q = queue;
81         struct rte_pktmbuf_pool_private *mbp_priv;
82         uint16_t num_rx = 0;
83         uint16_t buf_size;
84         uint16_t sg_size;
85         uint16_t hw_size;
86         uint16_t packet_size;
87         uint64_t num_bytes = 0;
88         struct szedata *sze = sze_q->sze;
89         uint8_t *header_ptr = NULL; /* header of packet */
90         uint8_t *packet_ptr1 = NULL;
91         uint8_t *packet_ptr2 = NULL;
92         uint16_t packet_len1 = 0;
93         uint16_t packet_len2 = 0;
94         uint16_t hw_data_align;
95
96         if (unlikely(sze_q->sze == NULL || nb_pkts == 0))
97                 return 0;
98
99         /*
100          * Reads the given number of packets from szedata2 channel given
101          * by queue and copies the packet data into a newly allocated mbuf
102          * to return.
103          */
104         for (i = 0; i < nb_pkts; i++) {
105                 mbuf = rte_pktmbuf_alloc(sze_q->mb_pool);
106
107                 if (unlikely(mbuf == NULL)) {
108                         sze_q->priv->dev->data->rx_mbuf_alloc_failed++;
109                         break;
110                 }
111
112                 /* get the next sze packet */
113                 if (sze->ct_rx_lck != NULL && !sze->ct_rx_rem_bytes &&
114                                 sze->ct_rx_lck->next == NULL) {
115                         /* unlock old data */
116                         szedata_rx_unlock_data(sze_q->sze, sze->ct_rx_lck_orig);
117                         sze->ct_rx_lck_orig = NULL;
118                         sze->ct_rx_lck = NULL;
119                 }
120
121                 if (!sze->ct_rx_rem_bytes && sze->ct_rx_lck_orig == NULL) {
122                         /* nothing to read, lock new data */
123                         sze->ct_rx_lck = szedata_rx_lock_data(sze_q->sze, ~0U);
124                         sze->ct_rx_lck_orig = sze->ct_rx_lck;
125
126                         if (sze->ct_rx_lck == NULL) {
127                                 /* nothing to lock */
128                                 rte_pktmbuf_free(mbuf);
129                                 break;
130                         }
131
132                         sze->ct_rx_cur_ptr = sze->ct_rx_lck->start;
133                         sze->ct_rx_rem_bytes = sze->ct_rx_lck->len;
134
135                         if (!sze->ct_rx_rem_bytes) {
136                                 rte_pktmbuf_free(mbuf);
137                                 break;
138                         }
139                 }
140
141                 if (sze->ct_rx_rem_bytes < RTE_SZE2_PACKET_HEADER_SIZE) {
142                         /*
143                          * cut in header
144                          * copy parts of header to merge buffer
145                          */
146                         if (sze->ct_rx_lck->next == NULL) {
147                                 rte_pktmbuf_free(mbuf);
148                                 break;
149                         }
150
151                         /* copy first part of header */
152                         rte_memcpy(sze->ct_rx_buffer, sze->ct_rx_cur_ptr,
153                                         sze->ct_rx_rem_bytes);
154
155                         /* copy second part of header */
156                         sze->ct_rx_lck = sze->ct_rx_lck->next;
157                         sze->ct_rx_cur_ptr = sze->ct_rx_lck->start;
158                         rte_memcpy(sze->ct_rx_buffer + sze->ct_rx_rem_bytes,
159                                 sze->ct_rx_cur_ptr,
160                                 RTE_SZE2_PACKET_HEADER_SIZE -
161                                 sze->ct_rx_rem_bytes);
162
163                         sze->ct_rx_cur_ptr += RTE_SZE2_PACKET_HEADER_SIZE -
164                                 sze->ct_rx_rem_bytes;
165                         sze->ct_rx_rem_bytes = sze->ct_rx_lck->len -
166                                 RTE_SZE2_PACKET_HEADER_SIZE +
167                                 sze->ct_rx_rem_bytes;
168
169                         header_ptr = (uint8_t *)sze->ct_rx_buffer;
170                 } else {
171                         /* not cut */
172                         header_ptr = (uint8_t *)sze->ct_rx_cur_ptr;
173                         sze->ct_rx_cur_ptr += RTE_SZE2_PACKET_HEADER_SIZE;
174                         sze->ct_rx_rem_bytes -= RTE_SZE2_PACKET_HEADER_SIZE;
175                 }
176
177                 sg_size = le16toh(*((uint16_t *)header_ptr));
178                 hw_size = le16toh(*(((uint16_t *)header_ptr) + 1));
179                 packet_size = sg_size -
180                         RTE_SZE2_ALIGN8(RTE_SZE2_PACKET_HEADER_SIZE + hw_size);
181
182
183                 /* checks if packet all right */
184                 if (!sg_size)
185                         errx(5, "Zero segsize");
186
187                 /* check sg_size and hwsize */
188                 if (hw_size > sg_size - RTE_SZE2_PACKET_HEADER_SIZE) {
189                         errx(10, "Hwsize bigger than expected. Segsize: %d, "
190                                 "hwsize: %d", sg_size, hw_size);
191                 }
192
193                 hw_data_align =
194                         RTE_SZE2_ALIGN8(RTE_SZE2_PACKET_HEADER_SIZE + hw_size) -
195                         RTE_SZE2_PACKET_HEADER_SIZE;
196
197                 if (sze->ct_rx_rem_bytes >=
198                                 (uint16_t)(sg_size -
199                                 RTE_SZE2_PACKET_HEADER_SIZE)) {
200                         /* no cut */
201                         /* one packet ready - go to another */
202                         packet_ptr1 = sze->ct_rx_cur_ptr + hw_data_align;
203                         packet_len1 = packet_size;
204                         packet_ptr2 = NULL;
205                         packet_len2 = 0;
206
207                         sze->ct_rx_cur_ptr += RTE_SZE2_ALIGN8(sg_size) -
208                                 RTE_SZE2_PACKET_HEADER_SIZE;
209                         sze->ct_rx_rem_bytes -= RTE_SZE2_ALIGN8(sg_size) -
210                                 RTE_SZE2_PACKET_HEADER_SIZE;
211                 } else {
212                         /* cut in data */
213                         if (sze->ct_rx_lck->next == NULL) {
214                                 errx(6, "Need \"next\" lock, "
215                                         "but it is missing: %u",
216                                         sze->ct_rx_rem_bytes);
217                         }
218
219                         /* skip hw data */
220                         if (sze->ct_rx_rem_bytes <= hw_data_align) {
221                                 uint16_t rem_size = hw_data_align -
222                                         sze->ct_rx_rem_bytes;
223
224                                 /* MOVE to next lock */
225                                 sze->ct_rx_lck = sze->ct_rx_lck->next;
226                                 sze->ct_rx_cur_ptr =
227                                         (void *)(((uint8_t *)
228                                         (sze->ct_rx_lck->start)) + rem_size);
229
230                                 packet_ptr1 = sze->ct_rx_cur_ptr;
231                                 packet_len1 = packet_size;
232                                 packet_ptr2 = NULL;
233                                 packet_len2 = 0;
234
235                                 sze->ct_rx_cur_ptr +=
236                                         RTE_SZE2_ALIGN8(packet_size);
237                                 sze->ct_rx_rem_bytes = sze->ct_rx_lck->len -
238                                         rem_size - RTE_SZE2_ALIGN8(packet_size);
239                         } else {
240                                 /* get pointer and length from first part */
241                                 packet_ptr1 = sze->ct_rx_cur_ptr +
242                                         hw_data_align;
243                                 packet_len1 = sze->ct_rx_rem_bytes -
244                                         hw_data_align;
245
246                                 /* MOVE to next lock */
247                                 sze->ct_rx_lck = sze->ct_rx_lck->next;
248                                 sze->ct_rx_cur_ptr = sze->ct_rx_lck->start;
249
250                                 /* get pointer and length from second part */
251                                 packet_ptr2 = sze->ct_rx_cur_ptr;
252                                 packet_len2 = packet_size - packet_len1;
253
254                                 sze->ct_rx_cur_ptr +=
255                                         RTE_SZE2_ALIGN8(packet_size) -
256                                         packet_len1;
257                                 sze->ct_rx_rem_bytes = sze->ct_rx_lck->len -
258                                         (RTE_SZE2_ALIGN8(packet_size) -
259                                          packet_len1);
260                         }
261                 }
262
263                 if (unlikely(packet_ptr1 == NULL)) {
264                         rte_pktmbuf_free(mbuf);
265                         break;
266                 }
267
268                 /* get the space available for data in the mbuf */
269                 mbp_priv = rte_mempool_get_priv(sze_q->mb_pool);
270                 buf_size = (uint16_t)(mbp_priv->mbuf_data_room_size -
271                                 RTE_PKTMBUF_HEADROOM);
272
273                 if (packet_size <= buf_size) {
274                         /* sze packet will fit in one mbuf, go ahead and copy */
275                         rte_memcpy(rte_pktmbuf_mtod(mbuf, void *),
276                                         packet_ptr1, packet_len1);
277                         if (packet_ptr2 != NULL) {
278                                 rte_memcpy((void *)(rte_pktmbuf_mtod(mbuf,
279                                         uint8_t *) + packet_len1),
280                                         packet_ptr2, packet_len2);
281                         }
282                         mbuf->data_len = (uint16_t)packet_size;
283
284                         mbuf->pkt_len = packet_size;
285                         mbuf->port = sze_q->in_port;
286                         bufs[num_rx] = mbuf;
287                         num_rx++;
288                         num_bytes += packet_size;
289                 } else {
290                         /*
291                          * sze packet will not fit in one mbuf,
292                          * scattered mode is not enabled, drop packet
293                          */
294                         RTE_LOG(ERR, PMD,
295                                 "SZE segment %d bytes will not fit in one mbuf "
296                                 "(%d bytes), scattered mode is not enabled, "
297                                 "drop packet!!\n",
298                                 packet_size, buf_size);
299                         rte_pktmbuf_free(mbuf);
300                 }
301         }
302
303         sze_q->rx_pkts += num_rx;
304         sze_q->rx_bytes += num_bytes;
305         return num_rx;
306 }
307
308 static uint16_t
309 eth_szedata2_rx_scattered(void *queue,
310                 struct rte_mbuf **bufs,
311                 uint16_t nb_pkts)
312 {
313         unsigned int i;
314         struct rte_mbuf *mbuf;
315         struct szedata2_rx_queue *sze_q = queue;
316         struct rte_pktmbuf_pool_private *mbp_priv;
317         uint16_t num_rx = 0;
318         uint16_t buf_size;
319         uint16_t sg_size;
320         uint16_t hw_size;
321         uint16_t packet_size;
322         uint64_t num_bytes = 0;
323         struct szedata *sze = sze_q->sze;
324         uint8_t *header_ptr = NULL; /* header of packet */
325         uint8_t *packet_ptr1 = NULL;
326         uint8_t *packet_ptr2 = NULL;
327         uint16_t packet_len1 = 0;
328         uint16_t packet_len2 = 0;
329         uint16_t hw_data_align;
330         uint64_t *mbuf_failed_ptr =
331                 &sze_q->priv->dev->data->rx_mbuf_alloc_failed;
332
333         if (unlikely(sze_q->sze == NULL || nb_pkts == 0))
334                 return 0;
335
336         /*
337          * Reads the given number of packets from szedata2 channel given
338          * by queue and copies the packet data into a newly allocated mbuf
339          * to return.
340          */
341         for (i = 0; i < nb_pkts; i++) {
342                 const struct szedata_lock *ct_rx_lck_backup;
343                 unsigned int ct_rx_rem_bytes_backup;
344                 unsigned char *ct_rx_cur_ptr_backup;
345
346                 /* get the next sze packet */
347                 if (sze->ct_rx_lck != NULL && !sze->ct_rx_rem_bytes &&
348                                 sze->ct_rx_lck->next == NULL) {
349                         /* unlock old data */
350                         szedata_rx_unlock_data(sze_q->sze, sze->ct_rx_lck_orig);
351                         sze->ct_rx_lck_orig = NULL;
352                         sze->ct_rx_lck = NULL;
353                 }
354
355                 /*
356                  * Store items from sze structure which can be changed
357                  * before mbuf allocating. Use these items in case of mbuf
358                  * allocating failure.
359                  */
360                 ct_rx_lck_backup = sze->ct_rx_lck;
361                 ct_rx_rem_bytes_backup = sze->ct_rx_rem_bytes;
362                 ct_rx_cur_ptr_backup = sze->ct_rx_cur_ptr;
363
364                 if (!sze->ct_rx_rem_bytes && sze->ct_rx_lck_orig == NULL) {
365                         /* nothing to read, lock new data */
366                         sze->ct_rx_lck = szedata_rx_lock_data(sze_q->sze, ~0U);
367                         sze->ct_rx_lck_orig = sze->ct_rx_lck;
368
369                         /*
370                          * Backup items from sze structure must be updated
371                          * after locking to contain pointers to new locks.
372                          */
373                         ct_rx_lck_backup = sze->ct_rx_lck;
374                         ct_rx_rem_bytes_backup = sze->ct_rx_rem_bytes;
375                         ct_rx_cur_ptr_backup = sze->ct_rx_cur_ptr;
376
377                         if (sze->ct_rx_lck == NULL)
378                                 /* nothing to lock */
379                                 break;
380
381                         sze->ct_rx_cur_ptr = sze->ct_rx_lck->start;
382                         sze->ct_rx_rem_bytes = sze->ct_rx_lck->len;
383
384                         if (!sze->ct_rx_rem_bytes)
385                                 break;
386                 }
387
388                 if (sze->ct_rx_rem_bytes < RTE_SZE2_PACKET_HEADER_SIZE) {
389                         /*
390                          * cut in header - copy parts of header to merge buffer
391                          */
392                         if (sze->ct_rx_lck->next == NULL)
393                                 break;
394
395                         /* copy first part of header */
396                         rte_memcpy(sze->ct_rx_buffer, sze->ct_rx_cur_ptr,
397                                         sze->ct_rx_rem_bytes);
398
399                         /* copy second part of header */
400                         sze->ct_rx_lck = sze->ct_rx_lck->next;
401                         sze->ct_rx_cur_ptr = sze->ct_rx_lck->start;
402                         rte_memcpy(sze->ct_rx_buffer + sze->ct_rx_rem_bytes,
403                                 sze->ct_rx_cur_ptr,
404                                 RTE_SZE2_PACKET_HEADER_SIZE -
405                                 sze->ct_rx_rem_bytes);
406
407                         sze->ct_rx_cur_ptr += RTE_SZE2_PACKET_HEADER_SIZE -
408                                 sze->ct_rx_rem_bytes;
409                         sze->ct_rx_rem_bytes = sze->ct_rx_lck->len -
410                                 RTE_SZE2_PACKET_HEADER_SIZE +
411                                 sze->ct_rx_rem_bytes;
412
413                         header_ptr = (uint8_t *)sze->ct_rx_buffer;
414                 } else {
415                         /* not cut */
416                         header_ptr = (uint8_t *)sze->ct_rx_cur_ptr;
417                         sze->ct_rx_cur_ptr += RTE_SZE2_PACKET_HEADER_SIZE;
418                         sze->ct_rx_rem_bytes -= RTE_SZE2_PACKET_HEADER_SIZE;
419                 }
420
421                 sg_size = le16toh(*((uint16_t *)header_ptr));
422                 hw_size = le16toh(*(((uint16_t *)header_ptr) + 1));
423                 packet_size = sg_size -
424                         RTE_SZE2_ALIGN8(RTE_SZE2_PACKET_HEADER_SIZE + hw_size);
425
426
427                 /* checks if packet all right */
428                 if (!sg_size)
429                         errx(5, "Zero segsize");
430
431                 /* check sg_size and hwsize */
432                 if (hw_size > sg_size - RTE_SZE2_PACKET_HEADER_SIZE) {
433                         errx(10, "Hwsize bigger than expected. Segsize: %d, "
434                                         "hwsize: %d", sg_size, hw_size);
435                 }
436
437                 hw_data_align =
438                         RTE_SZE2_ALIGN8((RTE_SZE2_PACKET_HEADER_SIZE +
439                         hw_size)) - RTE_SZE2_PACKET_HEADER_SIZE;
440
441                 if (sze->ct_rx_rem_bytes >=
442                                 (uint16_t)(sg_size -
443                                 RTE_SZE2_PACKET_HEADER_SIZE)) {
444                         /* no cut */
445                         /* one packet ready - go to another */
446                         packet_ptr1 = sze->ct_rx_cur_ptr + hw_data_align;
447                         packet_len1 = packet_size;
448                         packet_ptr2 = NULL;
449                         packet_len2 = 0;
450
451                         sze->ct_rx_cur_ptr += RTE_SZE2_ALIGN8(sg_size) -
452                                 RTE_SZE2_PACKET_HEADER_SIZE;
453                         sze->ct_rx_rem_bytes -= RTE_SZE2_ALIGN8(sg_size) -
454                                 RTE_SZE2_PACKET_HEADER_SIZE;
455                 } else {
456                         /* cut in data */
457                         if (sze->ct_rx_lck->next == NULL) {
458                                 errx(6, "Need \"next\" lock, but it is "
459                                         "missing: %u", sze->ct_rx_rem_bytes);
460                         }
461
462                         /* skip hw data */
463                         if (sze->ct_rx_rem_bytes <= hw_data_align) {
464                                 uint16_t rem_size = hw_data_align -
465                                         sze->ct_rx_rem_bytes;
466
467                                 /* MOVE to next lock */
468                                 sze->ct_rx_lck = sze->ct_rx_lck->next;
469                                 sze->ct_rx_cur_ptr =
470                                         (void *)(((uint8_t *)
471                                         (sze->ct_rx_lck->start)) + rem_size);
472
473                                 packet_ptr1 = sze->ct_rx_cur_ptr;
474                                 packet_len1 = packet_size;
475                                 packet_ptr2 = NULL;
476                                 packet_len2 = 0;
477
478                                 sze->ct_rx_cur_ptr +=
479                                         RTE_SZE2_ALIGN8(packet_size);
480                                 sze->ct_rx_rem_bytes = sze->ct_rx_lck->len -
481                                         rem_size - RTE_SZE2_ALIGN8(packet_size);
482                         } else {
483                                 /* get pointer and length from first part */
484                                 packet_ptr1 = sze->ct_rx_cur_ptr +
485                                         hw_data_align;
486                                 packet_len1 = sze->ct_rx_rem_bytes -
487                                         hw_data_align;
488
489                                 /* MOVE to next lock */
490                                 sze->ct_rx_lck = sze->ct_rx_lck->next;
491                                 sze->ct_rx_cur_ptr = sze->ct_rx_lck->start;
492
493                                 /* get pointer and length from second part */
494                                 packet_ptr2 = sze->ct_rx_cur_ptr;
495                                 packet_len2 = packet_size - packet_len1;
496
497                                 sze->ct_rx_cur_ptr +=
498                                         RTE_SZE2_ALIGN8(packet_size) -
499                                         packet_len1;
500                                 sze->ct_rx_rem_bytes = sze->ct_rx_lck->len -
501                                         (RTE_SZE2_ALIGN8(packet_size) -
502                                          packet_len1);
503                         }
504                 }
505
506                 if (unlikely(packet_ptr1 == NULL))
507                         break;
508
509                 mbuf = rte_pktmbuf_alloc(sze_q->mb_pool);
510
511                 if (unlikely(mbuf == NULL)) {
512                         /*
513                          * Restore items from sze structure to state after
514                          * unlocking (eventually locking).
515                          */
516                         sze->ct_rx_lck = ct_rx_lck_backup;
517                         sze->ct_rx_rem_bytes = ct_rx_rem_bytes_backup;
518                         sze->ct_rx_cur_ptr = ct_rx_cur_ptr_backup;
519                         sze_q->priv->dev->data->rx_mbuf_alloc_failed++;
520                         break;
521                 }
522
523                 /* get the space available for data in the mbuf */
524                 mbp_priv = rte_mempool_get_priv(sze_q->mb_pool);
525                 buf_size = (uint16_t)(mbp_priv->mbuf_data_room_size -
526                                 RTE_PKTMBUF_HEADROOM);
527
528                 if (packet_size <= buf_size) {
529                         /* sze packet will fit in one mbuf, go ahead and copy */
530                         rte_memcpy(rte_pktmbuf_mtod(mbuf, void *),
531                                         packet_ptr1, packet_len1);
532                         if (packet_ptr2 != NULL) {
533                                 rte_memcpy((void *)
534                                         (rte_pktmbuf_mtod(mbuf, uint8_t *) +
535                                         packet_len1), packet_ptr2, packet_len2);
536                         }
537                         mbuf->data_len = (uint16_t)packet_size;
538                 } else {
539                         /*
540                          * sze packet will not fit in one mbuf,
541                          * scatter packet into more mbufs
542                          */
543                         struct rte_mbuf *m = mbuf;
544                         uint16_t len = rte_pktmbuf_tailroom(mbuf);
545
546                         /* copy first part of packet */
547                         /* fill first mbuf */
548                         rte_memcpy(rte_pktmbuf_append(mbuf, len), packet_ptr1,
549                                 len);
550                         packet_len1 -= len;
551                         packet_ptr1 = ((uint8_t *)packet_ptr1) + len;
552
553                         while (packet_len1 > 0) {
554                                 /* fill new mbufs */
555                                 m->next = rte_pktmbuf_alloc(sze_q->mb_pool);
556
557                                 if (unlikely(m->next == NULL)) {
558                                         rte_pktmbuf_free(mbuf);
559                                         /*
560                                          * Restore items from sze structure
561                                          * to state after unlocking (eventually
562                                          * locking).
563                                          */
564                                         sze->ct_rx_lck = ct_rx_lck_backup;
565                                         sze->ct_rx_rem_bytes =
566                                                 ct_rx_rem_bytes_backup;
567                                         sze->ct_rx_cur_ptr =
568                                                 ct_rx_cur_ptr_backup;
569                                         (*mbuf_failed_ptr)++;
570                                         goto finish;
571                                 }
572
573                                 m = m->next;
574
575                                 len = RTE_MIN(rte_pktmbuf_tailroom(m),
576                                         packet_len1);
577                                 rte_memcpy(rte_pktmbuf_append(mbuf, len),
578                                         packet_ptr1, len);
579
580                                 (mbuf->nb_segs)++;
581                                 packet_len1 -= len;
582                                 packet_ptr1 = ((uint8_t *)packet_ptr1) + len;
583                         }
584
585                         if (packet_ptr2 != NULL) {
586                                 /* copy second part of packet, if exists */
587                                 /* fill the rest of currently last mbuf */
588                                 len = rte_pktmbuf_tailroom(m);
589                                 rte_memcpy(rte_pktmbuf_append(mbuf, len),
590                                         packet_ptr2, len);
591                                 packet_len2 -= len;
592                                 packet_ptr2 = ((uint8_t *)packet_ptr2) + len;
593
594                                 while (packet_len2 > 0) {
595                                         /* fill new mbufs */
596                                         m->next = rte_pktmbuf_alloc(
597                                                         sze_q->mb_pool);
598
599                                         if (unlikely(m->next == NULL)) {
600                                                 rte_pktmbuf_free(mbuf);
601                                                 /*
602                                                  * Restore items from sze
603                                                  * structure to state after
604                                                  * unlocking (eventually
605                                                  * locking).
606                                                  */
607                                                 sze->ct_rx_lck =
608                                                         ct_rx_lck_backup;
609                                                 sze->ct_rx_rem_bytes =
610                                                         ct_rx_rem_bytes_backup;
611                                                 sze->ct_rx_cur_ptr =
612                                                         ct_rx_cur_ptr_backup;
613                                                 (*mbuf_failed_ptr)++;
614                                                 goto finish;
615                                         }
616
617                                         m = m->next;
618
619                                         len = RTE_MIN(rte_pktmbuf_tailroom(m),
620                                                 packet_len2);
621                                         rte_memcpy(
622                                                 rte_pktmbuf_append(mbuf, len),
623                                                 packet_ptr2, len);
624
625                                         (mbuf->nb_segs)++;
626                                         packet_len2 -= len;
627                                         packet_ptr2 = ((uint8_t *)packet_ptr2) +
628                                                 len;
629                                 }
630                         }
631                 }
632                 mbuf->pkt_len = packet_size;
633                 mbuf->port = sze_q->in_port;
634                 bufs[num_rx] = mbuf;
635                 num_rx++;
636                 num_bytes += packet_size;
637         }
638
639 finish:
640         sze_q->rx_pkts += num_rx;
641         sze_q->rx_bytes += num_bytes;
642         return num_rx;
643 }
644
645 static uint16_t
646 eth_szedata2_tx(void *queue,
647                 struct rte_mbuf **bufs,
648                 uint16_t nb_pkts)
649 {
650         struct rte_mbuf *mbuf;
651         struct szedata2_tx_queue *sze_q = queue;
652         uint16_t num_tx = 0;
653         uint64_t num_bytes = 0;
654
655         const struct szedata_lock *lck;
656         uint32_t lock_size;
657         uint32_t lock_size2;
658         void *dst;
659         uint32_t pkt_len;
660         uint32_t hwpkt_len;
661         uint32_t unlock_size;
662         uint32_t rem_len;
663         uint16_t mbuf_segs;
664         uint16_t pkt_left = nb_pkts;
665
666         if (sze_q->sze == NULL || nb_pkts == 0)
667                 return 0;
668
669         while (pkt_left > 0) {
670                 unlock_size = 0;
671                 lck = szedata_tx_lock_data(sze_q->sze,
672                         RTE_ETH_SZEDATA2_TX_LOCK_SIZE,
673                         sze_q->tx_channel);
674                 if (lck == NULL)
675                         continue;
676
677                 dst = lck->start;
678                 lock_size = lck->len;
679                 lock_size2 = lck->next ? lck->next->len : 0;
680
681 next_packet:
682                 mbuf = bufs[nb_pkts - pkt_left];
683
684                 pkt_len = mbuf->pkt_len;
685                 mbuf_segs = mbuf->nb_segs;
686
687                 hwpkt_len = RTE_SZE2_PACKET_HEADER_SIZE_ALIGNED +
688                         RTE_SZE2_ALIGN8(pkt_len);
689
690                 if (lock_size + lock_size2 < hwpkt_len) {
691                         szedata_tx_unlock_data(sze_q->sze, lck, unlock_size);
692                         continue;
693                 }
694
695                 num_bytes += pkt_len;
696
697                 if (lock_size > hwpkt_len) {
698                         void *tmp_dst;
699
700                         rem_len = 0;
701
702                         /* write packet length at first 2 bytes in 8B header */
703                         *((uint16_t *)dst) = htole16(
704                                         RTE_SZE2_PACKET_HEADER_SIZE_ALIGNED +
705                                         pkt_len);
706                         *(((uint16_t *)dst) + 1) = htole16(0);
707
708                         /* copy packet from mbuf */
709                         tmp_dst = ((uint8_t *)(dst)) +
710                                 RTE_SZE2_PACKET_HEADER_SIZE_ALIGNED;
711                         if (mbuf_segs == 1) {
712                                 /*
713                                  * non-scattered packet,
714                                  * transmit from one mbuf
715                                  */
716                                 rte_memcpy(tmp_dst,
717                                         rte_pktmbuf_mtod(mbuf, const void *),
718                                         pkt_len);
719                         } else {
720                                 /* scattered packet, transmit from more mbufs */
721                                 struct rte_mbuf *m = mbuf;
722                                 while (m) {
723                                         rte_memcpy(tmp_dst,
724                                                 rte_pktmbuf_mtod(m,
725                                                 const void *),
726                                                 m->data_len);
727                                         tmp_dst = ((uint8_t *)(tmp_dst)) +
728                                                 m->data_len;
729                                         m = m->next;
730                                 }
731                         }
732
733
734                         dst = ((uint8_t *)dst) + hwpkt_len;
735                         unlock_size += hwpkt_len;
736                         lock_size -= hwpkt_len;
737
738                         rte_pktmbuf_free(mbuf);
739                         num_tx++;
740                         pkt_left--;
741                         if (pkt_left == 0) {
742                                 szedata_tx_unlock_data(sze_q->sze, lck,
743                                         unlock_size);
744                                 break;
745                         }
746                         goto next_packet;
747                 } else if (lock_size + lock_size2 >= hwpkt_len) {
748                         void *tmp_dst;
749                         uint16_t write_len;
750
751                         /* write packet length at first 2 bytes in 8B header */
752                         *((uint16_t *)dst) =
753                                 htole16(RTE_SZE2_PACKET_HEADER_SIZE_ALIGNED +
754                                         pkt_len);
755                         *(((uint16_t *)dst) + 1) = htole16(0);
756
757                         /*
758                          * If the raw packet (pkt_len) is smaller than lock_size
759                          * get the correct length for memcpy
760                          */
761                         write_len =
762                                 pkt_len < lock_size -
763                                 RTE_SZE2_PACKET_HEADER_SIZE_ALIGNED ?
764                                 pkt_len :
765                                 lock_size - RTE_SZE2_PACKET_HEADER_SIZE_ALIGNED;
766
767                         rem_len = hwpkt_len - lock_size;
768
769                         tmp_dst = ((uint8_t *)(dst)) +
770                                 RTE_SZE2_PACKET_HEADER_SIZE_ALIGNED;
771                         if (mbuf_segs == 1) {
772                                 /*
773                                  * non-scattered packet,
774                                  * transmit from one mbuf
775                                  */
776                                 /* copy part of packet to first area */
777                                 rte_memcpy(tmp_dst,
778                                         rte_pktmbuf_mtod(mbuf, const void *),
779                                         write_len);
780
781                                 if (lck->next)
782                                         dst = lck->next->start;
783
784                                 /* copy part of packet to second area */
785                                 rte_memcpy(dst,
786                                         (const void *)(rte_pktmbuf_mtod(mbuf,
787                                                         const uint8_t *) +
788                                         write_len), pkt_len - write_len);
789                         } else {
790                                 /* scattered packet, transmit from more mbufs */
791                                 struct rte_mbuf *m = mbuf;
792                                 uint16_t written = 0;
793                                 uint16_t to_write = 0;
794                                 bool new_mbuf = true;
795                                 uint16_t write_off = 0;
796
797                                 /* copy part of packet to first area */
798                                 while (m && written < write_len) {
799                                         to_write = RTE_MIN(m->data_len,
800                                                         write_len - written);
801                                         rte_memcpy(tmp_dst,
802                                                 rte_pktmbuf_mtod(m,
803                                                         const void *),
804                                                 to_write);
805
806                                         tmp_dst = ((uint8_t *)(tmp_dst)) +
807                                                 to_write;
808                                         if (m->data_len <= write_len -
809                                                         written) {
810                                                 m = m->next;
811                                                 new_mbuf = true;
812                                         } else {
813                                                 new_mbuf = false;
814                                         }
815                                         written += to_write;
816                                 }
817
818                                 if (lck->next)
819                                         dst = lck->next->start;
820
821                                 tmp_dst = dst;
822                                 written = 0;
823                                 write_off = new_mbuf ? 0 : to_write;
824
825                                 /* copy part of packet to second area */
826                                 while (m && written < pkt_len - write_len) {
827                                         rte_memcpy(tmp_dst, (const void *)
828                                                 (rte_pktmbuf_mtod(m,
829                                                 uint8_t *) + write_off),
830                                                 m->data_len - write_off);
831
832                                         tmp_dst = ((uint8_t *)(tmp_dst)) +
833                                                 (m->data_len - write_off);
834                                         written += m->data_len - write_off;
835                                         m = m->next;
836                                         write_off = 0;
837                                 }
838                         }
839
840                         dst = ((uint8_t *)dst) + rem_len;
841                         unlock_size += hwpkt_len;
842                         lock_size = lock_size2 - rem_len;
843                         lock_size2 = 0;
844
845                         rte_pktmbuf_free(mbuf);
846                         num_tx++;
847                 }
848
849                 szedata_tx_unlock_data(sze_q->sze, lck, unlock_size);
850                 pkt_left--;
851         }
852
853         sze_q->tx_pkts += num_tx;
854         sze_q->err_pkts += nb_pkts - num_tx;
855         sze_q->tx_bytes += num_bytes;
856         return num_tx;
857 }
858
859 static int
860 eth_rx_queue_start(struct rte_eth_dev *dev, uint16_t rxq_id)
861 {
862         struct szedata2_rx_queue *rxq = dev->data->rx_queues[rxq_id];
863         int ret;
864         struct pmd_internals *internals = (struct pmd_internals *)
865                 dev->data->dev_private;
866
867         if (rxq->sze == NULL) {
868                 uint32_t rx = 1 << rxq->rx_channel;
869                 uint32_t tx = 0;
870                 rxq->sze = szedata_open(internals->sze_dev);
871                 if (rxq->sze == NULL)
872                         return -EINVAL;
873                 ret = szedata_subscribe3(rxq->sze, &rx, &tx);
874                 if (ret != 0 || rx == 0)
875                         goto err;
876         }
877
878         ret = szedata_start(rxq->sze);
879         if (ret != 0)
880                 goto err;
881         dev->data->rx_queue_state[rxq_id] = RTE_ETH_QUEUE_STATE_STARTED;
882         return 0;
883
884 err:
885         szedata_close(rxq->sze);
886         rxq->sze = NULL;
887         return -EINVAL;
888 }
889
890 static int
891 eth_rx_queue_stop(struct rte_eth_dev *dev, uint16_t rxq_id)
892 {
893         struct szedata2_rx_queue *rxq = dev->data->rx_queues[rxq_id];
894
895         if (rxq->sze != NULL) {
896                 szedata_close(rxq->sze);
897                 rxq->sze = NULL;
898         }
899
900         dev->data->rx_queue_state[rxq_id] = RTE_ETH_QUEUE_STATE_STOPPED;
901         return 0;
902 }
903
904 static int
905 eth_tx_queue_start(struct rte_eth_dev *dev, uint16_t txq_id)
906 {
907         struct szedata2_tx_queue *txq = dev->data->tx_queues[txq_id];
908         int ret;
909         struct pmd_internals *internals = (struct pmd_internals *)
910                 dev->data->dev_private;
911
912         if (txq->sze == NULL) {
913                 uint32_t rx = 0;
914                 uint32_t tx = 1 << txq->tx_channel;
915                 txq->sze = szedata_open(internals->sze_dev);
916                 if (txq->sze == NULL)
917                         return -EINVAL;
918                 ret = szedata_subscribe3(txq->sze, &rx, &tx);
919                 if (ret != 0 || tx == 0)
920                         goto err;
921         }
922
923         ret = szedata_start(txq->sze);
924         if (ret != 0)
925                 goto err;
926         dev->data->tx_queue_state[txq_id] = RTE_ETH_QUEUE_STATE_STARTED;
927         return 0;
928
929 err:
930         szedata_close(txq->sze);
931         txq->sze = NULL;
932         return -EINVAL;
933 }
934
935 static int
936 eth_tx_queue_stop(struct rte_eth_dev *dev, uint16_t txq_id)
937 {
938         struct szedata2_tx_queue *txq = dev->data->tx_queues[txq_id];
939
940         if (txq->sze != NULL) {
941                 szedata_close(txq->sze);
942                 txq->sze = NULL;
943         }
944
945         dev->data->tx_queue_state[txq_id] = RTE_ETH_QUEUE_STATE_STOPPED;
946         return 0;
947 }
948
949 static int
950 eth_dev_start(struct rte_eth_dev *dev)
951 {
952         int ret;
953         uint16_t i;
954         uint16_t nb_rx = dev->data->nb_rx_queues;
955         uint16_t nb_tx = dev->data->nb_tx_queues;
956
957         for (i = 0; i < nb_rx; i++) {
958                 ret = eth_rx_queue_start(dev, i);
959                 if (ret != 0)
960                         goto err_rx;
961         }
962
963         for (i = 0; i < nb_tx; i++) {
964                 ret = eth_tx_queue_start(dev, i);
965                 if (ret != 0)
966                         goto err_tx;
967         }
968
969         return 0;
970
971 err_tx:
972         for (i = 0; i < nb_tx; i++)
973                 eth_tx_queue_stop(dev, i);
974 err_rx:
975         for (i = 0; i < nb_rx; i++)
976                 eth_rx_queue_stop(dev, i);
977         return ret;
978 }
979
980 static void
981 eth_dev_stop(struct rte_eth_dev *dev)
982 {
983         uint16_t i;
984         uint16_t nb_rx = dev->data->nb_rx_queues;
985         uint16_t nb_tx = dev->data->nb_tx_queues;
986
987         for (i = 0; i < nb_tx; i++)
988                 eth_tx_queue_stop(dev, i);
989
990         for (i = 0; i < nb_rx; i++)
991                 eth_rx_queue_stop(dev, i);
992 }
993
994 static int
995 eth_dev_configure(struct rte_eth_dev *dev)
996 {
997         struct rte_eth_dev_data *data = dev->data;
998         if (data->dev_conf.rxmode.offloads & DEV_RX_OFFLOAD_SCATTER) {
999                 dev->rx_pkt_burst = eth_szedata2_rx_scattered;
1000                 data->scattered_rx = 1;
1001         } else {
1002                 dev->rx_pkt_burst = eth_szedata2_rx;
1003                 data->scattered_rx = 0;
1004         }
1005         return 0;
1006 }
1007
1008 static void
1009 eth_dev_info(struct rte_eth_dev *dev,
1010                 struct rte_eth_dev_info *dev_info)
1011 {
1012         struct pmd_internals *internals = dev->data->dev_private;
1013
1014         dev_info->pci_dev = RTE_ETH_DEV_TO_PCI(dev);
1015         dev_info->if_index = 0;
1016         dev_info->max_mac_addrs = 1;
1017         dev_info->max_rx_pktlen = (uint32_t)-1;
1018         dev_info->max_rx_queues = internals->max_rx_queues;
1019         dev_info->max_tx_queues = internals->max_tx_queues;
1020         dev_info->min_rx_bufsize = 0;
1021         dev_info->rx_offload_capa = DEV_RX_OFFLOAD_SCATTER;
1022         dev_info->tx_offload_capa = 0;
1023         dev_info->rx_queue_offload_capa = 0;
1024         dev_info->tx_queue_offload_capa = 0;
1025         dev_info->speed_capa = ETH_LINK_SPEED_100G;
1026 }
1027
1028 static int
1029 eth_stats_get(struct rte_eth_dev *dev,
1030                 struct rte_eth_stats *stats)
1031 {
1032         uint16_t i;
1033         uint16_t nb_rx = dev->data->nb_rx_queues;
1034         uint16_t nb_tx = dev->data->nb_tx_queues;
1035         uint64_t rx_total = 0;
1036         uint64_t tx_total = 0;
1037         uint64_t tx_err_total = 0;
1038         uint64_t rx_total_bytes = 0;
1039         uint64_t tx_total_bytes = 0;
1040
1041         for (i = 0; i < nb_rx; i++) {
1042                 struct szedata2_rx_queue *rxq = dev->data->rx_queues[i];
1043
1044                 if (i < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
1045                         stats->q_ipackets[i] = rxq->rx_pkts;
1046                         stats->q_ibytes[i] = rxq->rx_bytes;
1047                 }
1048                 rx_total += rxq->rx_pkts;
1049                 rx_total_bytes += rxq->rx_bytes;
1050         }
1051
1052         for (i = 0; i < nb_tx; i++) {
1053                 struct szedata2_tx_queue *txq = dev->data->tx_queues[i];
1054
1055                 if (i < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
1056                         stats->q_opackets[i] = txq->tx_pkts;
1057                         stats->q_obytes[i] = txq->tx_bytes;
1058                         stats->q_errors[i] = txq->err_pkts;
1059                 }
1060                 tx_total += txq->tx_pkts;
1061                 tx_total_bytes += txq->tx_bytes;
1062                 tx_err_total += txq->err_pkts;
1063         }
1064
1065         stats->ipackets = rx_total;
1066         stats->opackets = tx_total;
1067         stats->ibytes = rx_total_bytes;
1068         stats->obytes = tx_total_bytes;
1069         stats->oerrors = tx_err_total;
1070         stats->rx_nombuf = dev->data->rx_mbuf_alloc_failed;
1071
1072         return 0;
1073 }
1074
1075 static void
1076 eth_stats_reset(struct rte_eth_dev *dev)
1077 {
1078         uint16_t i;
1079         uint16_t nb_rx = dev->data->nb_rx_queues;
1080         uint16_t nb_tx = dev->data->nb_tx_queues;
1081
1082         for (i = 0; i < nb_rx; i++) {
1083                 struct szedata2_rx_queue *rxq = dev->data->rx_queues[i];
1084                 rxq->rx_pkts = 0;
1085                 rxq->rx_bytes = 0;
1086                 rxq->err_pkts = 0;
1087         }
1088         for (i = 0; i < nb_tx; i++) {
1089                 struct szedata2_tx_queue *txq = dev->data->tx_queues[i];
1090                 txq->tx_pkts = 0;
1091                 txq->tx_bytes = 0;
1092                 txq->err_pkts = 0;
1093         }
1094 }
1095
1096 static void
1097 eth_rx_queue_release(void *q)
1098 {
1099         struct szedata2_rx_queue *rxq = (struct szedata2_rx_queue *)q;
1100
1101         if (rxq != NULL) {
1102                 if (rxq->sze != NULL)
1103                         szedata_close(rxq->sze);
1104                 rte_free(rxq);
1105         }
1106 }
1107
1108 static void
1109 eth_tx_queue_release(void *q)
1110 {
1111         struct szedata2_tx_queue *txq = (struct szedata2_tx_queue *)q;
1112
1113         if (txq != NULL) {
1114                 if (txq->sze != NULL)
1115                         szedata_close(txq->sze);
1116                 rte_free(txq);
1117         }
1118 }
1119
1120 static void
1121 eth_dev_close(struct rte_eth_dev *dev)
1122 {
1123         uint16_t i;
1124         uint16_t nb_rx = dev->data->nb_rx_queues;
1125         uint16_t nb_tx = dev->data->nb_tx_queues;
1126
1127         eth_dev_stop(dev);
1128
1129         for (i = 0; i < nb_rx; i++) {
1130                 eth_rx_queue_release(dev->data->rx_queues[i]);
1131                 dev->data->rx_queues[i] = NULL;
1132         }
1133         dev->data->nb_rx_queues = 0;
1134         for (i = 0; i < nb_tx; i++) {
1135                 eth_tx_queue_release(dev->data->tx_queues[i]);
1136                 dev->data->tx_queues[i] = NULL;
1137         }
1138         dev->data->nb_tx_queues = 0;
1139 }
1140
1141 /**
1142  * Function takes value from first IBUF status register.
1143  * Values in IBUF and OBUF should be same.
1144  *
1145  * @param internals
1146  *     Pointer to device private structure.
1147  * @return
1148  *     Link speed constant.
1149  */
1150 static inline enum szedata2_link_speed
1151 get_link_speed(const struct pmd_internals *internals)
1152 {
1153         const volatile struct szedata2_ibuf *ibuf =
1154                 ibuf_ptr_by_index(internals->pci_rsc, 0);
1155         uint32_t speed = (szedata2_read32(&ibuf->ibuf_st) & 0x70) >> 4;
1156         switch (speed) {
1157         case 0x03:
1158                 return SZEDATA2_LINK_SPEED_10G;
1159         case 0x04:
1160                 return SZEDATA2_LINK_SPEED_40G;
1161         case 0x05:
1162                 return SZEDATA2_LINK_SPEED_100G;
1163         default:
1164                 return SZEDATA2_LINK_SPEED_DEFAULT;
1165         }
1166 }
1167
1168 static int
1169 eth_link_update(struct rte_eth_dev *dev,
1170                 int wait_to_complete __rte_unused)
1171 {
1172         struct rte_eth_link link;
1173         struct pmd_internals *internals = (struct pmd_internals *)
1174                 dev->data->dev_private;
1175         const volatile struct szedata2_ibuf *ibuf;
1176         uint32_t i;
1177         bool link_is_up = false;
1178
1179         memset(&link, 0, sizeof(link));
1180
1181         switch (get_link_speed(internals)) {
1182         case SZEDATA2_LINK_SPEED_10G:
1183                 link.link_speed = ETH_SPEED_NUM_10G;
1184                 break;
1185         case SZEDATA2_LINK_SPEED_40G:
1186                 link.link_speed = ETH_SPEED_NUM_40G;
1187                 break;
1188         case SZEDATA2_LINK_SPEED_100G:
1189                 link.link_speed = ETH_SPEED_NUM_100G;
1190                 break;
1191         default:
1192                 link.link_speed = ETH_SPEED_NUM_10G;
1193                 break;
1194         }
1195
1196         /* szedata2 uses only full duplex */
1197         link.link_duplex = ETH_LINK_FULL_DUPLEX;
1198
1199         for (i = 0; i < szedata2_ibuf_count; i++) {
1200                 ibuf = ibuf_ptr_by_index(internals->pci_rsc, i);
1201                 /*
1202                  * Link is considered up if at least one ibuf is enabled
1203                  * and up.
1204                  */
1205                 if (ibuf_is_enabled(ibuf) && ibuf_is_link_up(ibuf)) {
1206                         link_is_up = true;
1207                         break;
1208                 }
1209         }
1210
1211         link.link_status = link_is_up ? ETH_LINK_UP : ETH_LINK_DOWN;
1212
1213         link.link_autoneg = ETH_LINK_FIXED;
1214
1215         rte_eth_linkstatus_set(dev, &link);
1216         return 0;
1217 }
1218
1219 static int
1220 eth_dev_set_link_up(struct rte_eth_dev *dev)
1221 {
1222         struct pmd_internals *internals = (struct pmd_internals *)
1223                 dev->data->dev_private;
1224         uint32_t i;
1225
1226         for (i = 0; i < szedata2_ibuf_count; i++)
1227                 ibuf_enable(ibuf_ptr_by_index(internals->pci_rsc, i));
1228         for (i = 0; i < szedata2_obuf_count; i++)
1229                 obuf_enable(obuf_ptr_by_index(internals->pci_rsc, i));
1230         return 0;
1231 }
1232
1233 static int
1234 eth_dev_set_link_down(struct rte_eth_dev *dev)
1235 {
1236         struct pmd_internals *internals = (struct pmd_internals *)
1237                 dev->data->dev_private;
1238         uint32_t i;
1239
1240         for (i = 0; i < szedata2_ibuf_count; i++)
1241                 ibuf_disable(ibuf_ptr_by_index(internals->pci_rsc, i));
1242         for (i = 0; i < szedata2_obuf_count; i++)
1243                 obuf_disable(obuf_ptr_by_index(internals->pci_rsc, i));
1244         return 0;
1245 }
1246
1247 static int
1248 eth_rx_queue_setup(struct rte_eth_dev *dev,
1249                 uint16_t rx_queue_id,
1250                 uint16_t nb_rx_desc __rte_unused,
1251                 unsigned int socket_id,
1252                 const struct rte_eth_rxconf *rx_conf __rte_unused,
1253                 struct rte_mempool *mb_pool)
1254 {
1255         struct pmd_internals *internals = dev->data->dev_private;
1256         struct szedata2_rx_queue *rxq;
1257         int ret;
1258         uint32_t rx = 1 << rx_queue_id;
1259         uint32_t tx = 0;
1260
1261         if (dev->data->rx_queues[rx_queue_id] != NULL) {
1262                 eth_rx_queue_release(dev->data->rx_queues[rx_queue_id]);
1263                 dev->data->rx_queues[rx_queue_id] = NULL;
1264         }
1265
1266         rxq = rte_zmalloc_socket("szedata2 rx queue",
1267                         sizeof(struct szedata2_rx_queue),
1268                         RTE_CACHE_LINE_SIZE, socket_id);
1269         if (rxq == NULL) {
1270                 RTE_LOG(ERR, PMD, "rte_zmalloc_socket() failed for rx queue id "
1271                                 "%" PRIu16 "!\n", rx_queue_id);
1272                 return -ENOMEM;
1273         }
1274
1275         rxq->priv = internals;
1276         rxq->sze = szedata_open(internals->sze_dev);
1277         if (rxq->sze == NULL) {
1278                 RTE_LOG(ERR, PMD, "szedata_open() failed for rx queue id "
1279                                 "%" PRIu16 "!\n", rx_queue_id);
1280                 eth_rx_queue_release(rxq);
1281                 return -EINVAL;
1282         }
1283         ret = szedata_subscribe3(rxq->sze, &rx, &tx);
1284         if (ret != 0 || rx == 0) {
1285                 RTE_LOG(ERR, PMD, "szedata_subscribe3() failed for rx queue id "
1286                                 "%" PRIu16 "!\n", rx_queue_id);
1287                 eth_rx_queue_release(rxq);
1288                 return -EINVAL;
1289         }
1290         rxq->rx_channel = rx_queue_id;
1291         rxq->in_port = dev->data->port_id;
1292         rxq->mb_pool = mb_pool;
1293         rxq->rx_pkts = 0;
1294         rxq->rx_bytes = 0;
1295         rxq->err_pkts = 0;
1296
1297         dev->data->rx_queues[rx_queue_id] = rxq;
1298
1299         RTE_LOG(DEBUG, PMD, "Configured rx queue id %" PRIu16 " on socket "
1300                         "%u.\n", rx_queue_id, socket_id);
1301
1302         return 0;
1303 }
1304
1305 static int
1306 eth_tx_queue_setup(struct rte_eth_dev *dev,
1307                 uint16_t tx_queue_id,
1308                 uint16_t nb_tx_desc __rte_unused,
1309                 unsigned int socket_id,
1310                 const struct rte_eth_txconf *tx_conf __rte_unused)
1311 {
1312         struct pmd_internals *internals = dev->data->dev_private;
1313         struct szedata2_tx_queue *txq;
1314         int ret;
1315         uint32_t rx = 0;
1316         uint32_t tx = 1 << tx_queue_id;
1317
1318         if (dev->data->tx_queues[tx_queue_id] != NULL) {
1319                 eth_tx_queue_release(dev->data->tx_queues[tx_queue_id]);
1320                 dev->data->tx_queues[tx_queue_id] = NULL;
1321         }
1322
1323         txq = rte_zmalloc_socket("szedata2 tx queue",
1324                         sizeof(struct szedata2_tx_queue),
1325                         RTE_CACHE_LINE_SIZE, socket_id);
1326         if (txq == NULL) {
1327                 RTE_LOG(ERR, PMD, "rte_zmalloc_socket() failed for tx queue id "
1328                                 "%" PRIu16 "!\n", tx_queue_id);
1329                 return -ENOMEM;
1330         }
1331
1332         txq->priv = internals;
1333         txq->sze = szedata_open(internals->sze_dev);
1334         if (txq->sze == NULL) {
1335                 RTE_LOG(ERR, PMD, "szedata_open() failed for tx queue id "
1336                                 "%" PRIu16 "!\n", tx_queue_id);
1337                 eth_tx_queue_release(txq);
1338                 return -EINVAL;
1339         }
1340         ret = szedata_subscribe3(txq->sze, &rx, &tx);
1341         if (ret != 0 || tx == 0) {
1342                 RTE_LOG(ERR, PMD, "szedata_subscribe3() failed for tx queue id "
1343                                 "%" PRIu16 "!\n", tx_queue_id);
1344                 eth_tx_queue_release(txq);
1345                 return -EINVAL;
1346         }
1347         txq->tx_channel = tx_queue_id;
1348         txq->tx_pkts = 0;
1349         txq->tx_bytes = 0;
1350         txq->err_pkts = 0;
1351
1352         dev->data->tx_queues[tx_queue_id] = txq;
1353
1354         RTE_LOG(DEBUG, PMD, "Configured tx queue id %" PRIu16 " on socket "
1355                         "%u.\n", tx_queue_id, socket_id);
1356
1357         return 0;
1358 }
1359
1360 static void
1361 eth_mac_addr_set(struct rte_eth_dev *dev __rte_unused,
1362                 struct ether_addr *mac_addr __rte_unused)
1363 {
1364 }
1365
1366 static void
1367 eth_promiscuous_enable(struct rte_eth_dev *dev)
1368 {
1369         struct pmd_internals *internals = (struct pmd_internals *)
1370                 dev->data->dev_private;
1371         uint32_t i;
1372
1373         for (i = 0; i < szedata2_ibuf_count; i++) {
1374                 ibuf_mac_mode_write(ibuf_ptr_by_index(internals->pci_rsc, i),
1375                                 SZEDATA2_MAC_CHMODE_PROMISC);
1376         }
1377 }
1378
1379 static void
1380 eth_promiscuous_disable(struct rte_eth_dev *dev)
1381 {
1382         struct pmd_internals *internals = (struct pmd_internals *)
1383                 dev->data->dev_private;
1384         uint32_t i;
1385
1386         for (i = 0; i < szedata2_ibuf_count; i++) {
1387                 ibuf_mac_mode_write(ibuf_ptr_by_index(internals->pci_rsc, i),
1388                                 SZEDATA2_MAC_CHMODE_ONLY_VALID);
1389         }
1390 }
1391
1392 static void
1393 eth_allmulticast_enable(struct rte_eth_dev *dev)
1394 {
1395         struct pmd_internals *internals = (struct pmd_internals *)
1396                 dev->data->dev_private;
1397         uint32_t i;
1398
1399         for (i = 0; i < szedata2_ibuf_count; i++) {
1400                 ibuf_mac_mode_write(ibuf_ptr_by_index(internals->pci_rsc, i),
1401                                 SZEDATA2_MAC_CHMODE_ALL_MULTICAST);
1402         }
1403 }
1404
1405 static void
1406 eth_allmulticast_disable(struct rte_eth_dev *dev)
1407 {
1408         struct pmd_internals *internals = (struct pmd_internals *)
1409                 dev->data->dev_private;
1410         uint32_t i;
1411
1412         for (i = 0; i < szedata2_ibuf_count; i++) {
1413                 ibuf_mac_mode_write(ibuf_ptr_by_index(internals->pci_rsc, i),
1414                                 SZEDATA2_MAC_CHMODE_ONLY_VALID);
1415         }
1416 }
1417
1418 static const struct eth_dev_ops ops = {
1419         .dev_start          = eth_dev_start,
1420         .dev_stop           = eth_dev_stop,
1421         .dev_set_link_up    = eth_dev_set_link_up,
1422         .dev_set_link_down  = eth_dev_set_link_down,
1423         .dev_close          = eth_dev_close,
1424         .dev_configure      = eth_dev_configure,
1425         .dev_infos_get      = eth_dev_info,
1426         .promiscuous_enable   = eth_promiscuous_enable,
1427         .promiscuous_disable  = eth_promiscuous_disable,
1428         .allmulticast_enable  = eth_allmulticast_enable,
1429         .allmulticast_disable = eth_allmulticast_disable,
1430         .rx_queue_start     = eth_rx_queue_start,
1431         .rx_queue_stop      = eth_rx_queue_stop,
1432         .tx_queue_start     = eth_tx_queue_start,
1433         .tx_queue_stop      = eth_tx_queue_stop,
1434         .rx_queue_setup     = eth_rx_queue_setup,
1435         .tx_queue_setup     = eth_tx_queue_setup,
1436         .rx_queue_release   = eth_rx_queue_release,
1437         .tx_queue_release   = eth_tx_queue_release,
1438         .link_update        = eth_link_update,
1439         .stats_get          = eth_stats_get,
1440         .stats_reset        = eth_stats_reset,
1441         .mac_addr_set       = eth_mac_addr_set,
1442 };
1443
1444 /*
1445  * This function goes through sysfs and looks for an index of szedata2
1446  * device file (/dev/szedataIIX, where X is the index).
1447  *
1448  * @return
1449  *           0 on success
1450  *          -1 on error
1451  */
1452 static int
1453 get_szedata2_index(const struct rte_pci_addr *pcislot_addr, uint32_t *index)
1454 {
1455         DIR *dir;
1456         struct dirent *entry;
1457         int ret;
1458         uint32_t tmp_index;
1459         FILE *fd;
1460         char pcislot_path[PATH_MAX];
1461         uint32_t domain;
1462         uint8_t bus;
1463         uint8_t devid;
1464         uint8_t function;
1465
1466         dir = opendir("/sys/class/combo");
1467         if (dir == NULL)
1468                 return -1;
1469
1470         /*
1471          * Iterate through all combosixX directories.
1472          * When the value in /sys/class/combo/combosixX/device/pcislot
1473          * file is the location of the ethernet device dev, "X" is the
1474          * index of the device.
1475          */
1476         while ((entry = readdir(dir)) != NULL) {
1477                 ret = sscanf(entry->d_name, "combosix%u", &tmp_index);
1478                 if (ret != 1)
1479                         continue;
1480
1481                 snprintf(pcislot_path, PATH_MAX,
1482                         "/sys/class/combo/combosix%u/device/pcislot",
1483                         tmp_index);
1484
1485                 fd = fopen(pcislot_path, "r");
1486                 if (fd == NULL)
1487                         continue;
1488
1489                 ret = fscanf(fd, "%8" SCNx32 ":%2" SCNx8 ":%2" SCNx8 ".%" SCNx8,
1490                                 &domain, &bus, &devid, &function);
1491                 fclose(fd);
1492                 if (ret != 4)
1493                         continue;
1494
1495                 if (pcislot_addr->domain == domain &&
1496                                 pcislot_addr->bus == bus &&
1497                                 pcislot_addr->devid == devid &&
1498                                 pcislot_addr->function == function) {
1499                         *index = tmp_index;
1500                         closedir(dir);
1501                         return 0;
1502                 }
1503         }
1504
1505         closedir(dir);
1506         return -1;
1507 }
1508
1509 static int
1510 rte_szedata2_eth_dev_init(struct rte_eth_dev *dev)
1511 {
1512         struct rte_eth_dev_data *data = dev->data;
1513         struct pmd_internals *internals = (struct pmd_internals *)
1514                 data->dev_private;
1515         struct szedata *szedata_temp;
1516         int ret;
1517         uint32_t szedata2_index;
1518         struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
1519         struct rte_pci_addr *pci_addr = &pci_dev->addr;
1520         struct rte_mem_resource *pci_rsc =
1521                 &pci_dev->mem_resource[PCI_RESOURCE_NUMBER];
1522         char rsc_filename[PATH_MAX];
1523         void *pci_resource_ptr = NULL;
1524         int fd;
1525
1526         RTE_LOG(INFO, PMD, "Initializing szedata2 device (" PCI_PRI_FMT ")\n",
1527                         pci_addr->domain, pci_addr->bus, pci_addr->devid,
1528                         pci_addr->function);
1529
1530         internals->dev = dev;
1531
1532         /* Get index of szedata2 device file and create path to device file */
1533         ret = get_szedata2_index(pci_addr, &szedata2_index);
1534         if (ret != 0) {
1535                 RTE_LOG(ERR, PMD, "Failed to get szedata2 device index!\n");
1536                 return -ENODEV;
1537         }
1538         snprintf(internals->sze_dev, PATH_MAX, SZEDATA2_DEV_PATH_FMT,
1539                         szedata2_index);
1540
1541         RTE_LOG(INFO, PMD, "SZEDATA2 path: %s\n", internals->sze_dev);
1542
1543         /*
1544          * Get number of available DMA RX and TX channels, which is maximum
1545          * number of queues that can be created and store it in private device
1546          * data structure.
1547          */
1548         szedata_temp = szedata_open(internals->sze_dev);
1549         if (szedata_temp == NULL) {
1550                 RTE_LOG(ERR, PMD, "szedata_open(): failed to open %s",
1551                                 internals->sze_dev);
1552                 return -EINVAL;
1553         }
1554         internals->max_rx_queues = szedata_ifaces_available(szedata_temp,
1555                         SZE2_DIR_RX);
1556         internals->max_tx_queues = szedata_ifaces_available(szedata_temp,
1557                         SZE2_DIR_TX);
1558         szedata_close(szedata_temp);
1559
1560         RTE_LOG(INFO, PMD, "Available DMA channels RX: %u TX: %u\n",
1561                         internals->max_rx_queues, internals->max_tx_queues);
1562
1563         /* Set rx, tx burst functions */
1564         if (data->scattered_rx == 1)
1565                 dev->rx_pkt_burst = eth_szedata2_rx_scattered;
1566         else
1567                 dev->rx_pkt_burst = eth_szedata2_rx;
1568         dev->tx_pkt_burst = eth_szedata2_tx;
1569
1570         /* Set function callbacks for Ethernet API */
1571         dev->dev_ops = &ops;
1572
1573         rte_eth_copy_pci_info(dev, pci_dev);
1574
1575         /* mmap pci resource0 file to rte_mem_resource structure */
1576         if (pci_dev->mem_resource[PCI_RESOURCE_NUMBER].phys_addr ==
1577                         0) {
1578                 RTE_LOG(ERR, PMD, "Missing resource%u file\n",
1579                                 PCI_RESOURCE_NUMBER);
1580                 return -EINVAL;
1581         }
1582         snprintf(rsc_filename, PATH_MAX,
1583                 "%s/" PCI_PRI_FMT "/resource%u", rte_pci_get_sysfs_path(),
1584                 pci_addr->domain, pci_addr->bus,
1585                 pci_addr->devid, pci_addr->function, PCI_RESOURCE_NUMBER);
1586         fd = open(rsc_filename, O_RDWR);
1587         if (fd < 0) {
1588                 RTE_LOG(ERR, PMD, "Could not open file %s\n", rsc_filename);
1589                 return -EINVAL;
1590         }
1591
1592         pci_resource_ptr = mmap(0,
1593                         pci_dev->mem_resource[PCI_RESOURCE_NUMBER].len,
1594                         PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
1595         close(fd);
1596         if (pci_resource_ptr == MAP_FAILED) {
1597                 RTE_LOG(ERR, PMD, "Could not mmap file %s (fd = %d)\n",
1598                                 rsc_filename, fd);
1599                 return -EINVAL;
1600         }
1601         pci_dev->mem_resource[PCI_RESOURCE_NUMBER].addr = pci_resource_ptr;
1602         internals->pci_rsc = pci_rsc;
1603
1604         RTE_LOG(DEBUG, PMD, "resource%u phys_addr = 0x%llx len = %llu "
1605                         "virt addr = %llx\n", PCI_RESOURCE_NUMBER,
1606                         (unsigned long long)pci_rsc->phys_addr,
1607                         (unsigned long long)pci_rsc->len,
1608                         (unsigned long long)pci_rsc->addr);
1609
1610         /* Get link state */
1611         eth_link_update(dev, 0);
1612
1613         /* Allocate space for one mac address */
1614         data->mac_addrs = rte_zmalloc(data->name, sizeof(struct ether_addr),
1615                         RTE_CACHE_LINE_SIZE);
1616         if (data->mac_addrs == NULL) {
1617                 RTE_LOG(ERR, PMD, "Could not alloc space for MAC address!\n");
1618                 munmap(pci_dev->mem_resource[PCI_RESOURCE_NUMBER].addr,
1619                        pci_dev->mem_resource[PCI_RESOURCE_NUMBER].len);
1620                 return -EINVAL;
1621         }
1622
1623         ether_addr_copy(&eth_addr, data->mac_addrs);
1624
1625         /* At initial state COMBO card is in promiscuous mode so disable it */
1626         eth_promiscuous_disable(dev);
1627
1628         RTE_LOG(INFO, PMD, "szedata2 device ("
1629                         PCI_PRI_FMT ") successfully initialized\n",
1630                         pci_addr->domain, pci_addr->bus, pci_addr->devid,
1631                         pci_addr->function);
1632
1633         return 0;
1634 }
1635
1636 static int
1637 rte_szedata2_eth_dev_uninit(struct rte_eth_dev *dev)
1638 {
1639         struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
1640         struct rte_pci_addr *pci_addr = &pci_dev->addr;
1641
1642         rte_free(dev->data->mac_addrs);
1643         dev->data->mac_addrs = NULL;
1644         munmap(pci_dev->mem_resource[PCI_RESOURCE_NUMBER].addr,
1645                pci_dev->mem_resource[PCI_RESOURCE_NUMBER].len);
1646
1647         RTE_LOG(INFO, PMD, "szedata2 device ("
1648                         PCI_PRI_FMT ") successfully uninitialized\n",
1649                         pci_addr->domain, pci_addr->bus, pci_addr->devid,
1650                         pci_addr->function);
1651
1652         return 0;
1653 }
1654
1655 static const struct rte_pci_id rte_szedata2_pci_id_table[] = {
1656         {
1657                 RTE_PCI_DEVICE(PCI_VENDOR_ID_NETCOPE,
1658                                 PCI_DEVICE_ID_NETCOPE_COMBO80G)
1659         },
1660         {
1661                 RTE_PCI_DEVICE(PCI_VENDOR_ID_NETCOPE,
1662                                 PCI_DEVICE_ID_NETCOPE_COMBO100G)
1663         },
1664         {
1665                 RTE_PCI_DEVICE(PCI_VENDOR_ID_NETCOPE,
1666                                 PCI_DEVICE_ID_NETCOPE_COMBO100G2)
1667         },
1668         {
1669                 .vendor_id = 0,
1670         }
1671 };
1672
1673 static int szedata2_eth_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
1674         struct rte_pci_device *pci_dev)
1675 {
1676         return rte_eth_dev_pci_generic_probe(pci_dev,
1677                 sizeof(struct pmd_internals), rte_szedata2_eth_dev_init);
1678 }
1679
1680 static int szedata2_eth_pci_remove(struct rte_pci_device *pci_dev)
1681 {
1682         return rte_eth_dev_pci_generic_remove(pci_dev,
1683                 rte_szedata2_eth_dev_uninit);
1684 }
1685
1686 static struct rte_pci_driver szedata2_eth_driver = {
1687         .id_table = rte_szedata2_pci_id_table,
1688         .probe = szedata2_eth_pci_probe,
1689         .remove = szedata2_eth_pci_remove,
1690 };
1691
1692 RTE_PMD_REGISTER_PCI(RTE_SZEDATA2_DRIVER_NAME, szedata2_eth_driver);
1693 RTE_PMD_REGISTER_PCI_TABLE(RTE_SZEDATA2_DRIVER_NAME, rte_szedata2_pci_id_table);
1694 RTE_PMD_REGISTER_KMOD_DEP(RTE_SZEDATA2_DRIVER_NAME,
1695         "* combo6core & combov3 & szedata2 & szedata2_cv3");