4701518a64866d588fb62303565dbb32442692be
[dpdk.git] / drivers / net / cxgbe / cxgbe_main.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2014-2018 Chelsio Communications.
3  * All rights reserved.
4  */
5
6 #include <sys/queue.h>
7 #include <stdio.h>
8 #include <errno.h>
9 #include <stdint.h>
10 #include <string.h>
11 #include <unistd.h>
12 #include <stdarg.h>
13 #include <inttypes.h>
14 #include <netinet/in.h>
15
16 #include <rte_byteorder.h>
17 #include <rte_common.h>
18 #include <rte_cycles.h>
19 #include <rte_interrupts.h>
20 #include <rte_log.h>
21 #include <rte_debug.h>
22 #include <rte_pci.h>
23 #include <rte_atomic.h>
24 #include <rte_branch_prediction.h>
25 #include <rte_memory.h>
26 #include <rte_tailq.h>
27 #include <rte_eal.h>
28 #include <rte_alarm.h>
29 #include <rte_ether.h>
30 #include <rte_ethdev_driver.h>
31 #include <rte_ethdev_pci.h>
32 #include <rte_random.h>
33 #include <rte_dev.h>
34 #include <rte_kvargs.h>
35
36 #include "base/common.h"
37 #include "base/t4_regs.h"
38 #include "base/t4_msg.h"
39 #include "cxgbe.h"
40 #include "cxgbe_pfvf.h"
41 #include "clip_tbl.h"
42 #include "l2t.h"
43 #include "mps_tcam.h"
44
45 /**
46  * Allocate a chunk of memory. The allocated memory is cleared.
47  */
48 void *t4_alloc_mem(size_t size)
49 {
50         return rte_zmalloc(NULL, size, 0);
51 }
52
53 /**
54  * Free memory allocated through t4_alloc_mem().
55  */
56 void t4_free_mem(void *addr)
57 {
58         rte_free(addr);
59 }
60
61 /*
62  * Response queue handler for the FW event queue.
63  */
64 static int fwevtq_handler(struct sge_rspq *q, const __be64 *rsp,
65                           __rte_unused const struct pkt_gl *gl)
66 {
67         u8 opcode = ((const struct rss_header *)rsp)->opcode;
68
69         rsp++;                                          /* skip RSS header */
70
71         /*
72          * FW can send EGR_UPDATEs encapsulated in a CPL_FW4_MSG.
73          */
74         if (unlikely(opcode == CPL_FW4_MSG &&
75                      ((const struct cpl_fw4_msg *)rsp)->type ==
76                       FW_TYPE_RSSCPL)) {
77                 rsp++;
78                 opcode = ((const struct rss_header *)rsp)->opcode;
79                 rsp++;
80                 if (opcode != CPL_SGE_EGR_UPDATE) {
81                         dev_err(q->adapter, "unexpected FW4/CPL %#x on FW event queue\n",
82                                 opcode);
83                         goto out;
84                 }
85         }
86
87         if (likely(opcode == CPL_SGE_EGR_UPDATE)) {
88                 /* do nothing */
89         } else if (opcode == CPL_FW6_MSG || opcode == CPL_FW4_MSG) {
90                 const struct cpl_fw6_msg *msg = (const void *)rsp;
91
92                 t4_handle_fw_rpl(q->adapter, msg->data);
93         } else if (opcode == CPL_ABORT_RPL_RSS) {
94                 const struct cpl_abort_rpl_rss *p = (const void *)rsp;
95
96                 cxgbe_hash_del_filter_rpl(q->adapter, p);
97         } else if (opcode == CPL_SET_TCB_RPL) {
98                 const struct cpl_set_tcb_rpl *p = (const void *)rsp;
99
100                 cxgbe_filter_rpl(q->adapter, p);
101         } else if (opcode == CPL_ACT_OPEN_RPL) {
102                 const struct cpl_act_open_rpl *p = (const void *)rsp;
103
104                 cxgbe_hash_filter_rpl(q->adapter, p);
105         } else if (opcode == CPL_L2T_WRITE_RPL) {
106                 const struct cpl_l2t_write_rpl *p = (const void *)rsp;
107
108                 cxgbe_do_l2t_write_rpl(q->adapter, p);
109         } else {
110                 dev_err(adapter, "unexpected CPL %#x on FW event queue\n",
111                         opcode);
112         }
113 out:
114         return 0;
115 }
116
117 /**
118  * Setup sge control queues to pass control information.
119  */
120 int cxgbe_setup_sge_ctrl_txq(struct adapter *adapter)
121 {
122         struct sge *s = &adapter->sge;
123         int err = 0, i = 0;
124
125         for_each_port(adapter, i) {
126                 struct port_info *pi = adap2pinfo(adapter, i);
127                 char name[RTE_ETH_NAME_MAX_LEN];
128                 struct sge_ctrl_txq *q = &s->ctrlq[i];
129
130                 q->q.size = 1024;
131                 err = t4_sge_alloc_ctrl_txq(adapter, q,
132                                             adapter->eth_dev,  i,
133                                             s->fw_evtq.cntxt_id,
134                                             rte_socket_id());
135                 if (err) {
136                         dev_err(adapter, "Failed to alloc ctrl txq. Err: %d",
137                                 err);
138                         goto out;
139                 }
140                 snprintf(name, sizeof(name), "%s_ctrl_pool_%d",
141                          pi->eth_dev->device->driver->name,
142                          pi->eth_dev->data->port_id);
143                 q->mb_pool = rte_pktmbuf_pool_create(name, s->ctrlq[i].q.size,
144                                                      RTE_CACHE_LINE_SIZE,
145                                                      RTE_MBUF_PRIV_ALIGN,
146                                                      RTE_MBUF_DEFAULT_BUF_SIZE,
147                                                      SOCKET_ID_ANY);
148                 if (!q->mb_pool) {
149                         err = -rte_errno;
150                         dev_err(adapter,
151                                 "Can't create ctrl pool for port %d. Err: %d\n",
152                                 pi->eth_dev->data->port_id, err);
153                         goto out;
154                 }
155         }
156         return 0;
157 out:
158         t4_free_sge_resources(adapter);
159         return err;
160 }
161
162 /**
163  * cxgbe_poll_for_completion: Poll rxq for completion
164  * @q: rxq to poll
165  * @ms: milliseconds to delay
166  * @cnt: number of times to poll
167  * @c: completion to check for 'done' status
168  *
169  * Polls the rxq for reples until completion is done or the count
170  * expires.
171  */
172 int cxgbe_poll_for_completion(struct sge_rspq *q, unsigned int ms,
173                               unsigned int cnt, struct t4_completion *c)
174 {
175         unsigned int i;
176         unsigned int work_done, budget = 32;
177
178         if (!c)
179                 return -EINVAL;
180
181         for (i = 0; i < cnt; i++) {
182                 cxgbe_poll(q, NULL, budget, &work_done);
183                 t4_os_lock(&c->lock);
184                 if (c->done) {
185                         t4_os_unlock(&c->lock);
186                         return 0;
187                 }
188                 t4_os_unlock(&c->lock);
189                 rte_delay_ms(ms);
190         }
191         return -ETIMEDOUT;
192 }
193
194 int cxgbe_setup_sge_fwevtq(struct adapter *adapter)
195 {
196         struct sge *s = &adapter->sge;
197         int err = 0;
198         int msi_idx = 0;
199
200         err = t4_sge_alloc_rxq(adapter, &s->fw_evtq, true, adapter->eth_dev,
201                                msi_idx, NULL, fwevtq_handler, -1, NULL, 0,
202                                rte_socket_id());
203         return err;
204 }
205
206 static int closest_timer(const struct sge *s, int time)
207 {
208         unsigned int i, match = 0;
209         int delta, min_delta = INT_MAX;
210
211         for (i = 0; i < ARRAY_SIZE(s->timer_val); i++) {
212                 delta = time - s->timer_val[i];
213                 if (delta < 0)
214                         delta = -delta;
215                 if (delta < min_delta) {
216                         min_delta = delta;
217                         match = i;
218                 }
219         }
220         return match;
221 }
222
223 static int closest_thres(const struct sge *s, int thres)
224 {
225         unsigned int i, match = 0;
226         int delta, min_delta = INT_MAX;
227
228         for (i = 0; i < ARRAY_SIZE(s->counter_val); i++) {
229                 delta = thres - s->counter_val[i];
230                 if (delta < 0)
231                         delta = -delta;
232                 if (delta < min_delta) {
233                         min_delta = delta;
234                         match = i;
235                 }
236         }
237         return match;
238 }
239
240 /**
241  * cxgb4_set_rspq_intr_params - set a queue's interrupt holdoff parameters
242  * @q: the Rx queue
243  * @us: the hold-off time in us, or 0 to disable timer
244  * @cnt: the hold-off packet count, or 0 to disable counter
245  *
246  * Sets an Rx queue's interrupt hold-off time and packet count.  At least
247  * one of the two needs to be enabled for the queue to generate interrupts.
248  */
249 int cxgb4_set_rspq_intr_params(struct sge_rspq *q, unsigned int us,
250                                unsigned int cnt)
251 {
252         struct adapter *adap = q->adapter;
253         unsigned int timer_val;
254
255         if (cnt) {
256                 int err;
257                 u32 v, new_idx;
258
259                 new_idx = closest_thres(&adap->sge, cnt);
260                 if (q->desc && q->pktcnt_idx != new_idx) {
261                         /* the queue has already been created, update it */
262                         v = V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_DMAQ) |
263                             V_FW_PARAMS_PARAM_X(
264                             FW_PARAMS_PARAM_DMAQ_IQ_INTCNTTHRESH) |
265                             V_FW_PARAMS_PARAM_YZ(q->cntxt_id);
266                         err = t4_set_params(adap, adap->mbox, adap->pf, 0, 1,
267                                             &v, &new_idx);
268                         if (err)
269                                 return err;
270                 }
271                 q->pktcnt_idx = new_idx;
272         }
273
274         timer_val = (us == 0) ? X_TIMERREG_RESTART_COUNTER :
275                                 closest_timer(&adap->sge, us);
276
277         if ((us | cnt) == 0)
278                 q->intr_params = V_QINTR_TIMER_IDX(X_TIMERREG_UPDATE_CIDX);
279         else
280                 q->intr_params = V_QINTR_TIMER_IDX(timer_val) |
281                                  V_QINTR_CNT_EN(cnt > 0);
282         return 0;
283 }
284
285 /**
286  * Allocate an active-open TID and set it to the supplied value.
287  */
288 int cxgbe_alloc_atid(struct tid_info *t, void *data)
289 {
290         int atid = -1;
291
292         t4_os_lock(&t->atid_lock);
293         if (t->afree) {
294                 union aopen_entry *p = t->afree;
295
296                 atid = p - t->atid_tab;
297                 t->afree = p->next;
298                 p->data = data;
299                 t->atids_in_use++;
300         }
301         t4_os_unlock(&t->atid_lock);
302         return atid;
303 }
304
305 /**
306  * Release an active-open TID.
307  */
308 void cxgbe_free_atid(struct tid_info *t, unsigned int atid)
309 {
310         union aopen_entry *p = &t->atid_tab[atid];
311
312         t4_os_lock(&t->atid_lock);
313         p->next = t->afree;
314         t->afree = p;
315         t->atids_in_use--;
316         t4_os_unlock(&t->atid_lock);
317 }
318
319 /**
320  * Populate a TID_RELEASE WR.  Caller must properly size the skb.
321  */
322 static void mk_tid_release(struct rte_mbuf *mbuf, unsigned int tid)
323 {
324         struct cpl_tid_release *req;
325
326         req = rte_pktmbuf_mtod(mbuf, struct cpl_tid_release *);
327         INIT_TP_WR_MIT_CPL(req, CPL_TID_RELEASE, tid);
328 }
329
330 /**
331  * Release a TID and inform HW.  If we are unable to allocate the release
332  * message we defer to a work queue.
333  */
334 void cxgbe_remove_tid(struct tid_info *t, unsigned int chan, unsigned int tid,
335                       unsigned short family)
336 {
337         struct rte_mbuf *mbuf;
338         struct adapter *adap = container_of(t, struct adapter, tids);
339
340         WARN_ON(tid >= t->ntids);
341
342         if (t->tid_tab[tid]) {
343                 t->tid_tab[tid] = NULL;
344                 rte_atomic32_dec(&t->conns_in_use);
345                 if (t->hash_base && tid >= t->hash_base) {
346                         if (family == FILTER_TYPE_IPV4)
347                                 rte_atomic32_dec(&t->hash_tids_in_use);
348                 } else {
349                         if (family == FILTER_TYPE_IPV4)
350                                 rte_atomic32_dec(&t->tids_in_use);
351                 }
352         }
353
354         mbuf = rte_pktmbuf_alloc((&adap->sge.ctrlq[chan])->mb_pool);
355         if (mbuf) {
356                 mbuf->data_len = sizeof(struct cpl_tid_release);
357                 mbuf->pkt_len = mbuf->data_len;
358                 mk_tid_release(mbuf, tid);
359                 t4_mgmt_tx(&adap->sge.ctrlq[chan], mbuf);
360         }
361 }
362
363 /**
364  * Insert a TID.
365  */
366 void cxgbe_insert_tid(struct tid_info *t, void *data, unsigned int tid,
367                       unsigned short family)
368 {
369         t->tid_tab[tid] = data;
370         if (t->hash_base && tid >= t->hash_base) {
371                 if (family == FILTER_TYPE_IPV4)
372                         rte_atomic32_inc(&t->hash_tids_in_use);
373         } else {
374                 if (family == FILTER_TYPE_IPV4)
375                         rte_atomic32_inc(&t->tids_in_use);
376         }
377
378         rte_atomic32_inc(&t->conns_in_use);
379 }
380
381 /**
382  * Free TID tables.
383  */
384 static void tid_free(struct tid_info *t)
385 {
386         if (t->tid_tab) {
387                 if (t->ftid_bmap)
388                         rte_bitmap_free(t->ftid_bmap);
389
390                 if (t->ftid_bmap_array)
391                         t4_os_free(t->ftid_bmap_array);
392
393                 t4_os_free(t->tid_tab);
394         }
395
396         memset(t, 0, sizeof(struct tid_info));
397 }
398
399 /**
400  * Allocate and initialize the TID tables.  Returns 0 on success.
401  */
402 static int tid_init(struct tid_info *t)
403 {
404         size_t size;
405         unsigned int ftid_bmap_size;
406         unsigned int natids = t->natids;
407         unsigned int max_ftids = t->nftids;
408
409         ftid_bmap_size = rte_bitmap_get_memory_footprint(t->nftids);
410         size = t->ntids * sizeof(*t->tid_tab) +
411                 max_ftids * sizeof(*t->ftid_tab) +
412                 natids * sizeof(*t->atid_tab);
413
414         t->tid_tab = t4_os_alloc(size);
415         if (!t->tid_tab)
416                 return -ENOMEM;
417
418         t->atid_tab = (union aopen_entry *)&t->tid_tab[t->ntids];
419         t->ftid_tab = (struct filter_entry *)&t->atid_tab[t->natids];
420         t->ftid_bmap_array = t4_os_alloc(ftid_bmap_size);
421         if (!t->ftid_bmap_array) {
422                 tid_free(t);
423                 return -ENOMEM;
424         }
425
426         t4_os_lock_init(&t->atid_lock);
427         t4_os_lock_init(&t->ftid_lock);
428
429         t->afree = NULL;
430         t->atids_in_use = 0;
431         rte_atomic32_init(&t->tids_in_use);
432         rte_atomic32_set(&t->tids_in_use, 0);
433         rte_atomic32_init(&t->conns_in_use);
434         rte_atomic32_set(&t->conns_in_use, 0);
435
436         /* Setup the free list for atid_tab and clear the stid bitmap. */
437         if (natids) {
438                 while (--natids)
439                         t->atid_tab[natids - 1].next = &t->atid_tab[natids];
440                 t->afree = t->atid_tab;
441         }
442
443         t->ftid_bmap = rte_bitmap_init(t->nftids, t->ftid_bmap_array,
444                                        ftid_bmap_size);
445         if (!t->ftid_bmap) {
446                 tid_free(t);
447                 return -ENOMEM;
448         }
449
450         return 0;
451 }
452
453 static inline bool is_x_1g_port(const struct link_config *lc)
454 {
455         return (lc->pcaps & FW_PORT_CAP32_SPEED_1G) != 0;
456 }
457
458 static inline bool is_x_10g_port(const struct link_config *lc)
459 {
460         unsigned int speeds, high_speeds;
461
462         speeds = V_FW_PORT_CAP32_SPEED(G_FW_PORT_CAP32_SPEED(lc->pcaps));
463         high_speeds = speeds &
464                       ~(FW_PORT_CAP32_SPEED_100M | FW_PORT_CAP32_SPEED_1G);
465
466         return high_speeds != 0;
467 }
468
469 static inline void init_rspq(struct adapter *adap, struct sge_rspq *q,
470                       unsigned int us, unsigned int cnt,
471                       unsigned int size, unsigned int iqe_size)
472 {
473         q->adapter = adap;
474         cxgb4_set_rspq_intr_params(q, us, cnt);
475         q->iqe_len = iqe_size;
476         q->size = size;
477 }
478
479 int cxgbe_cfg_queue_count(struct rte_eth_dev *eth_dev)
480 {
481         struct port_info *pi = eth_dev->data->dev_private;
482         struct adapter *adap = pi->adapter;
483         struct sge *s = &adap->sge;
484         unsigned int max_queues = s->max_ethqsets / adap->params.nports;
485
486         if ((eth_dev->data->nb_rx_queues < 1) ||
487             (eth_dev->data->nb_tx_queues < 1))
488                 return -EINVAL;
489
490         if ((eth_dev->data->nb_rx_queues > max_queues) ||
491             (eth_dev->data->nb_tx_queues > max_queues))
492                 return -EINVAL;
493
494         if (eth_dev->data->nb_rx_queues > pi->rss_size)
495                 return -EINVAL;
496
497         /* We must configure RSS, since config has changed*/
498         pi->flags &= ~PORT_RSS_DONE;
499
500         pi->n_rx_qsets = eth_dev->data->nb_rx_queues;
501         pi->n_tx_qsets = eth_dev->data->nb_tx_queues;
502
503         return 0;
504 }
505
506 void cxgbe_cfg_queues(struct rte_eth_dev *eth_dev)
507 {
508         struct port_info *pi = eth_dev->data->dev_private;
509         struct adapter *adap = pi->adapter;
510         struct sge *s = &adap->sge;
511         unsigned int i, nb_ports = 0, qidx = 0;
512         unsigned int q_per_port = 0;
513
514         if (!(adap->flags & CFG_QUEUES)) {
515                 for_each_port(adap, i) {
516                         struct port_info *tpi = adap2pinfo(adap, i);
517
518                         nb_ports += (is_x_10g_port(&tpi->link_cfg)) ||
519                                      is_x_1g_port(&tpi->link_cfg) ? 1 : 0;
520                 }
521
522                 /*
523                  * We default up to # of cores queues per 1G/10G port.
524                  */
525                 if (nb_ports)
526                         q_per_port = (s->max_ethqsets -
527                                      (adap->params.nports - nb_ports)) /
528                                      nb_ports;
529
530                 if (q_per_port > rte_lcore_count())
531                         q_per_port = rte_lcore_count();
532
533                 for_each_port(adap, i) {
534                         struct port_info *pi = adap2pinfo(adap, i);
535
536                         pi->first_qset = qidx;
537
538                         /* Initially n_rx_qsets == n_tx_qsets */
539                         pi->n_rx_qsets = (is_x_10g_port(&pi->link_cfg) ||
540                                           is_x_1g_port(&pi->link_cfg)) ?
541                                           q_per_port : 1;
542                         pi->n_tx_qsets = pi->n_rx_qsets;
543
544                         if (pi->n_rx_qsets > pi->rss_size)
545                                 pi->n_rx_qsets = pi->rss_size;
546
547                         qidx += pi->n_rx_qsets;
548                 }
549
550                 for (i = 0; i < ARRAY_SIZE(s->ethrxq); i++) {
551                         struct sge_eth_rxq *r = &s->ethrxq[i];
552
553                         init_rspq(adap, &r->rspq, 5, 32, 1024, 64);
554                         r->usembufs = 1;
555                         r->fl.size = (r->usembufs ? 1024 : 72);
556                 }
557
558                 for (i = 0; i < ARRAY_SIZE(s->ethtxq); i++)
559                         s->ethtxq[i].q.size = 1024;
560
561                 init_rspq(adap, &adap->sge.fw_evtq, 0, 0, 1024, 64);
562                 adap->flags |= CFG_QUEUES;
563         }
564 }
565
566 void cxgbe_stats_get(struct port_info *pi, struct port_stats *stats)
567 {
568         t4_get_port_stats_offset(pi->adapter, pi->tx_chan, stats,
569                                  &pi->stats_base);
570 }
571
572 void cxgbe_stats_reset(struct port_info *pi)
573 {
574         t4_clr_port_stats(pi->adapter, pi->tx_chan);
575 }
576
577 static void setup_memwin(struct adapter *adap)
578 {
579         u32 mem_win0_base;
580
581         /* For T5, only relative offset inside the PCIe BAR is passed */
582         mem_win0_base = MEMWIN0_BASE;
583
584         /*
585          * Set up memory window for accessing adapter memory ranges.  (Read
586          * back MA register to ensure that changes propagate before we attempt
587          * to use the new values.)
588          */
589         t4_write_reg(adap,
590                      PCIE_MEM_ACCESS_REG(A_PCIE_MEM_ACCESS_BASE_WIN,
591                                          MEMWIN_NIC),
592                      mem_win0_base | V_BIR(0) |
593                      V_WINDOW(ilog2(MEMWIN0_APERTURE) - X_WINDOW_SHIFT));
594         t4_read_reg(adap,
595                     PCIE_MEM_ACCESS_REG(A_PCIE_MEM_ACCESS_BASE_WIN,
596                                         MEMWIN_NIC));
597 }
598
599 int cxgbe_init_rss(struct adapter *adap)
600 {
601         unsigned int i;
602
603         if (is_pf4(adap)) {
604                 int err;
605
606                 err = t4_init_rss_mode(adap, adap->mbox);
607                 if (err)
608                         return err;
609         }
610
611         for_each_port(adap, i) {
612                 struct port_info *pi = adap2pinfo(adap, i);
613
614                 pi->rss = rte_zmalloc(NULL, pi->rss_size * sizeof(u16), 0);
615                 if (!pi->rss)
616                         return -ENOMEM;
617
618                 pi->rss_hf = CXGBE_RSS_HF_ALL;
619         }
620         return 0;
621 }
622
623 /**
624  * Dump basic information about the adapter.
625  */
626 void cxgbe_print_adapter_info(struct adapter *adap)
627 {
628         /**
629          * Hardware/Firmware/etc. Version/Revision IDs.
630          */
631         t4_dump_version_info(adap);
632 }
633
634 void cxgbe_print_port_info(struct adapter *adap)
635 {
636         int i;
637         char buf[80];
638         struct rte_pci_addr *loc = &adap->pdev->addr;
639
640         for_each_port(adap, i) {
641                 const struct port_info *pi = adap2pinfo(adap, i);
642                 char *bufp = buf;
643
644                 if (pi->link_cfg.pcaps & FW_PORT_CAP32_SPEED_100M)
645                         bufp += sprintf(bufp, "100M/");
646                 if (pi->link_cfg.pcaps & FW_PORT_CAP32_SPEED_1G)
647                         bufp += sprintf(bufp, "1G/");
648                 if (pi->link_cfg.pcaps & FW_PORT_CAP32_SPEED_10G)
649                         bufp += sprintf(bufp, "10G/");
650                 if (pi->link_cfg.pcaps & FW_PORT_CAP32_SPEED_25G)
651                         bufp += sprintf(bufp, "25G/");
652                 if (pi->link_cfg.pcaps & FW_PORT_CAP32_SPEED_40G)
653                         bufp += sprintf(bufp, "40G/");
654                 if (pi->link_cfg.pcaps & FW_PORT_CAP32_SPEED_50G)
655                         bufp += sprintf(bufp, "50G/");
656                 if (pi->link_cfg.pcaps & FW_PORT_CAP32_SPEED_100G)
657                         bufp += sprintf(bufp, "100G/");
658                 if (bufp != buf)
659                         --bufp;
660                 sprintf(bufp, "BASE-%s",
661                         t4_get_port_type_description(
662                                         (enum fw_port_type)pi->port_type));
663
664                 dev_info(adap,
665                          " " PCI_PRI_FMT " Chelsio rev %d %s %s\n",
666                          loc->domain, loc->bus, loc->devid, loc->function,
667                          CHELSIO_CHIP_RELEASE(adap->params.chip), buf,
668                          (adap->flags & USING_MSIX) ? " MSI-X" :
669                          (adap->flags & USING_MSI) ? " MSI" : "");
670         }
671 }
672
673 static int check_devargs_handler(const char *key, const char *value, void *p)
674 {
675         if (!strncmp(key, CXGBE_DEVARG_CMN_KEEP_OVLAN, strlen(key)) ||
676             !strncmp(key, CXGBE_DEVARG_CMN_TX_MODE_LATENCY, strlen(key)) ||
677             !strncmp(key, CXGBE_DEVARG_VF_FORCE_LINK_UP, strlen(key))) {
678                 if (!strncmp(value, "1", 1)) {
679                         bool *dst_val = (bool *)p;
680
681                         *dst_val = true;
682                 }
683         }
684
685         return 0;
686 }
687
688 static int cxgbe_get_devargs(struct rte_devargs *devargs, const char *key,
689                              void *p)
690 {
691         struct rte_kvargs *kvlist;
692         int ret = 0;
693
694         if (!devargs)
695                 return 0;
696
697         kvlist = rte_kvargs_parse(devargs->args, NULL);
698         if (!kvlist)
699                 return 0;
700
701         if (!rte_kvargs_count(kvlist, key))
702                 goto out;
703
704         ret = rte_kvargs_process(kvlist, key, check_devargs_handler, p);
705
706 out:
707         rte_kvargs_free(kvlist);
708
709         return ret;
710 }
711
712 static void cxgbe_get_devargs_int(struct adapter *adap, int *dst,
713                                   const char *key, int default_value)
714 {
715         struct rte_pci_device *pdev = adap->pdev;
716         int ret, devarg_value = default_value;
717
718         *dst = default_value;
719         if (!pdev)
720                 return;
721
722         ret = cxgbe_get_devargs(pdev->device.devargs, key, &devarg_value);
723         if (ret)
724                 return;
725
726         *dst = devarg_value;
727 }
728
729 void cxgbe_process_devargs(struct adapter *adap)
730 {
731         cxgbe_get_devargs_int(adap, &adap->devargs.keep_ovlan,
732                               CXGBE_DEVARG_CMN_KEEP_OVLAN, 0);
733         cxgbe_get_devargs_int(adap, &adap->devargs.tx_mode_latency,
734                               CXGBE_DEVARG_CMN_TX_MODE_LATENCY, 0);
735         cxgbe_get_devargs_int(adap, &adap->devargs.force_link_up,
736                               CXGBE_DEVARG_VF_FORCE_LINK_UP, 0);
737 }
738
739 static void configure_vlan_types(struct adapter *adapter)
740 {
741         int i;
742
743         for_each_port(adapter, i) {
744                 /* OVLAN Type 0x88a8 */
745                 t4_set_reg_field(adapter, MPS_PORT_RX_OVLAN_REG(i, A_RX_OVLAN0),
746                                  V_OVLAN_MASK(M_OVLAN_MASK) |
747                                  V_OVLAN_ETYPE(M_OVLAN_ETYPE),
748                                  V_OVLAN_MASK(M_OVLAN_MASK) |
749                                  V_OVLAN_ETYPE(0x88a8));
750                 /* OVLAN Type 0x9100 */
751                 t4_set_reg_field(adapter, MPS_PORT_RX_OVLAN_REG(i, A_RX_OVLAN1),
752                                  V_OVLAN_MASK(M_OVLAN_MASK) |
753                                  V_OVLAN_ETYPE(M_OVLAN_ETYPE),
754                                  V_OVLAN_MASK(M_OVLAN_MASK) |
755                                  V_OVLAN_ETYPE(0x9100));
756                 /* OVLAN Type 0x8100 */
757                 t4_set_reg_field(adapter, MPS_PORT_RX_OVLAN_REG(i, A_RX_OVLAN2),
758                                  V_OVLAN_MASK(M_OVLAN_MASK) |
759                                  V_OVLAN_ETYPE(M_OVLAN_ETYPE),
760                                  V_OVLAN_MASK(M_OVLAN_MASK) |
761                                  V_OVLAN_ETYPE(0x8100));
762
763                 /* IVLAN 0X8100 */
764                 t4_set_reg_field(adapter, MPS_PORT_RX_IVLAN(i),
765                                  V_IVLAN_ETYPE(M_IVLAN_ETYPE),
766                                  V_IVLAN_ETYPE(0x8100));
767
768                 t4_set_reg_field(adapter, MPS_PORT_RX_CTL(i),
769                                  F_OVLAN_EN0 | F_OVLAN_EN1 |
770                                  F_OVLAN_EN2 | F_IVLAN_EN,
771                                  F_OVLAN_EN0 | F_OVLAN_EN1 |
772                                  F_OVLAN_EN2 | F_IVLAN_EN);
773         }
774
775         t4_tp_wr_bits_indirect(adapter, A_TP_INGRESS_CONFIG, V_RM_OVLAN(1),
776                                V_RM_OVLAN(!adapter->devargs.keep_ovlan));
777 }
778
779 static void configure_pcie_ext_tag(struct adapter *adapter)
780 {
781         u16 v;
782         int pos = t4_os_find_pci_capability(adapter, PCI_CAP_ID_EXP);
783
784         if (!pos)
785                 return;
786
787         if (pos > 0) {
788                 t4_os_pci_read_cfg2(adapter, pos + PCI_EXP_DEVCTL, &v);
789                 v |= PCI_EXP_DEVCTL_EXT_TAG;
790                 t4_os_pci_write_cfg2(adapter, pos + PCI_EXP_DEVCTL, v);
791                 if (is_t6(adapter->params.chip)) {
792                         t4_set_reg_field(adapter, A_PCIE_CFG2,
793                                          V_T6_TOTMAXTAG(M_T6_TOTMAXTAG),
794                                          V_T6_TOTMAXTAG(7));
795                         t4_set_reg_field(adapter, A_PCIE_CMD_CFG,
796                                          V_T6_MINTAG(M_T6_MINTAG),
797                                          V_T6_MINTAG(8));
798                 } else {
799                         t4_set_reg_field(adapter, A_PCIE_CFG2,
800                                          V_TOTMAXTAG(M_TOTMAXTAG),
801                                          V_TOTMAXTAG(3));
802                         t4_set_reg_field(adapter, A_PCIE_CMD_CFG,
803                                          V_MINTAG(M_MINTAG),
804                                          V_MINTAG(8));
805                 }
806         }
807 }
808
809 /* Figure out how many Queue Sets we can support */
810 void cxgbe_configure_max_ethqsets(struct adapter *adapter)
811 {
812         unsigned int ethqsets;
813
814         /*
815          * We need to reserve an Ingress Queue for the Asynchronous Firmware
816          * Event Queue.
817          *
818          * For each Queue Set, we'll need the ability to allocate two Egress
819          * Contexts -- one for the Ingress Queue Free List and one for the TX
820          * Ethernet Queue.
821          */
822         if (is_pf4(adapter)) {
823                 struct pf_resources *pfres = &adapter->params.pfres;
824
825                 ethqsets = pfres->niqflint - 1;
826                 if (pfres->neq < ethqsets * 2)
827                         ethqsets = pfres->neq / 2;
828         } else {
829                 struct vf_resources *vfres = &adapter->params.vfres;
830
831                 ethqsets = vfres->niqflint - 1;
832                 if (vfres->nethctrl != ethqsets)
833                         ethqsets = min(vfres->nethctrl, ethqsets);
834                 if (vfres->neq < ethqsets * 2)
835                         ethqsets = vfres->neq / 2;
836         }
837
838         if (ethqsets > MAX_ETH_QSETS)
839                 ethqsets = MAX_ETH_QSETS;
840         adapter->sge.max_ethqsets = ethqsets;
841 }
842
843 /*
844  * Tweak configuration based on system architecture, etc.  Most of these have
845  * defaults assigned to them by Firmware Configuration Files (if we're using
846  * them) but need to be explicitly set if we're using hard-coded
847  * initialization. So these are essentially common tweaks/settings for
848  * Configuration Files and hard-coded initialization ...
849  */
850 static int adap_init0_tweaks(struct adapter *adapter)
851 {
852         u8 rx_dma_offset;
853
854         /*
855          * Fix up various Host-Dependent Parameters like Page Size, Cache
856          * Line Size, etc.  The firmware default is for a 4KB Page Size and
857          * 64B Cache Line Size ...
858          */
859         t4_fixup_host_params_compat(adapter, CXGBE_PAGE_SIZE, L1_CACHE_BYTES,
860                                     T5_LAST_REV);
861
862         /*
863          * Keep the chip default offset to deliver Ingress packets into our
864          * DMA buffers to zero
865          */
866         rx_dma_offset = 0;
867         t4_set_reg_field(adapter, A_SGE_CONTROL, V_PKTSHIFT(M_PKTSHIFT),
868                          V_PKTSHIFT(rx_dma_offset));
869
870         t4_set_reg_field(adapter, A_SGE_FLM_CFG,
871                          V_CREDITCNT(M_CREDITCNT) | M_CREDITCNTPACKING,
872                          V_CREDITCNT(3) | V_CREDITCNTPACKING(1));
873
874         t4_set_reg_field(adapter, A_SGE_INGRESS_RX_THRESHOLD,
875                          V_THRESHOLD_3(M_THRESHOLD_3), V_THRESHOLD_3(32U));
876
877         t4_set_reg_field(adapter, A_SGE_CONTROL2, V_IDMAARBROUNDROBIN(1U),
878                          V_IDMAARBROUNDROBIN(1U));
879
880         /*
881          * Don't include the "IP Pseudo Header" in CPL_RX_PKT checksums: Linux
882          * adds the pseudo header itself.
883          */
884         t4_tp_wr_bits_indirect(adapter, A_TP_INGRESS_CONFIG,
885                                F_CSUM_HAS_PSEUDO_HDR, 0);
886
887         return 0;
888 }
889
890 /*
891  * Attempt to initialize the adapter via a Firmware Configuration File.
892  */
893 static int adap_init0_config(struct adapter *adapter, int reset)
894 {
895         struct fw_caps_config_cmd caps_cmd;
896         unsigned long mtype = 0, maddr = 0;
897         u32 finiver, finicsum, cfcsum;
898         int ret;
899         int config_issued = 0;
900         int cfg_addr;
901         char config_name[20];
902
903         /*
904          * Reset device if necessary.
905          */
906         if (reset) {
907                 ret = t4_fw_reset(adapter, adapter->mbox,
908                                   F_PIORSTMODE | F_PIORST);
909                 if (ret < 0) {
910                         dev_warn(adapter, "Firmware reset failed, error %d\n",
911                                  -ret);
912                         goto bye;
913                 }
914         }
915
916         cfg_addr = t4_flash_cfg_addr(adapter);
917         if (cfg_addr < 0) {
918                 ret = cfg_addr;
919                 dev_warn(adapter, "Finding address for firmware config file in flash failed, error %d\n",
920                          -ret);
921                 goto bye;
922         }
923
924         strcpy(config_name, "On Flash");
925         mtype = FW_MEMTYPE_CF_FLASH;
926         maddr = cfg_addr;
927
928         /*
929          * Issue a Capability Configuration command to the firmware to get it
930          * to parse the Configuration File.  We don't use t4_fw_config_file()
931          * because we want the ability to modify various features after we've
932          * processed the configuration file ...
933          */
934         memset(&caps_cmd, 0, sizeof(caps_cmd));
935         caps_cmd.op_to_write = cpu_to_be32(V_FW_CMD_OP(FW_CAPS_CONFIG_CMD) |
936                                            F_FW_CMD_REQUEST | F_FW_CMD_READ);
937         caps_cmd.cfvalid_to_len16 =
938                 cpu_to_be32(F_FW_CAPS_CONFIG_CMD_CFVALID |
939                             V_FW_CAPS_CONFIG_CMD_MEMTYPE_CF(mtype) |
940                             V_FW_CAPS_CONFIG_CMD_MEMADDR64K_CF(maddr >> 16) |
941                             FW_LEN16(caps_cmd));
942         ret = t4_wr_mbox(adapter, adapter->mbox, &caps_cmd, sizeof(caps_cmd),
943                          &caps_cmd);
944         /*
945          * If the CAPS_CONFIG failed with an ENOENT (for a Firmware
946          * Configuration File in FLASH), our last gasp effort is to use the
947          * Firmware Configuration File which is embedded in the firmware.  A
948          * very few early versions of the firmware didn't have one embedded
949          * but we can ignore those.
950          */
951         if (ret == -ENOENT) {
952                 dev_info(adapter, "%s: Going for embedded config in firmware..\n",
953                          __func__);
954
955                 memset(&caps_cmd, 0, sizeof(caps_cmd));
956                 caps_cmd.op_to_write =
957                         cpu_to_be32(V_FW_CMD_OP(FW_CAPS_CONFIG_CMD) |
958                                     F_FW_CMD_REQUEST | F_FW_CMD_READ);
959                 caps_cmd.cfvalid_to_len16 = cpu_to_be32(FW_LEN16(caps_cmd));
960                 ret = t4_wr_mbox(adapter, adapter->mbox, &caps_cmd,
961                                  sizeof(caps_cmd), &caps_cmd);
962                 strcpy(config_name, "Firmware Default");
963         }
964
965         config_issued = 1;
966         if (ret < 0)
967                 goto bye;
968
969         finiver = be32_to_cpu(caps_cmd.finiver);
970         finicsum = be32_to_cpu(caps_cmd.finicsum);
971         cfcsum = be32_to_cpu(caps_cmd.cfcsum);
972         if (finicsum != cfcsum)
973                 dev_warn(adapter, "Configuration File checksum mismatch: [fini] csum=%#x, computed csum=%#x\n",
974                          finicsum, cfcsum);
975
976         /*
977          * If we're a pure NIC driver then disable all offloading facilities.
978          * This will allow the firmware to optimize aspects of the hardware
979          * configuration which will result in improved performance.
980          */
981         caps_cmd.niccaps &= cpu_to_be16(~FW_CAPS_CONFIG_NIC_ETHOFLD);
982         caps_cmd.toecaps = 0;
983         caps_cmd.iscsicaps = 0;
984         caps_cmd.rdmacaps = 0;
985         caps_cmd.fcoecaps = 0;
986
987         /*
988          * And now tell the firmware to use the configuration we just loaded.
989          */
990         caps_cmd.op_to_write = cpu_to_be32(V_FW_CMD_OP(FW_CAPS_CONFIG_CMD) |
991                                            F_FW_CMD_REQUEST | F_FW_CMD_WRITE);
992         caps_cmd.cfvalid_to_len16 = htonl(FW_LEN16(caps_cmd));
993         ret = t4_wr_mbox(adapter, adapter->mbox, &caps_cmd, sizeof(caps_cmd),
994                          NULL);
995         if (ret < 0) {
996                 dev_warn(adapter, "Unable to finalize Firmware Capabilities %d\n",
997                          -ret);
998                 goto bye;
999         }
1000
1001         /*
1002          * Tweak configuration based on system architecture, etc.
1003          */
1004         ret = adap_init0_tweaks(adapter);
1005         if (ret < 0) {
1006                 dev_warn(adapter, "Unable to do init0-tweaks %d\n", -ret);
1007                 goto bye;
1008         }
1009
1010         /*
1011          * And finally tell the firmware to initialize itself using the
1012          * parameters from the Configuration File.
1013          */
1014         ret = t4_fw_initialize(adapter, adapter->mbox);
1015         if (ret < 0) {
1016                 dev_warn(adapter, "Initializing Firmware failed, error %d\n",
1017                          -ret);
1018                 goto bye;
1019         }
1020
1021         /*
1022          * Return successfully and note that we're operating with parameters
1023          * not supplied by the driver, rather than from hard-wired
1024          * initialization constants buried in the driver.
1025          */
1026         dev_info(adapter,
1027                  "Successfully configured using Firmware Configuration File \"%s\", version %#x, computed checksum %#x\n",
1028                  config_name, finiver, cfcsum);
1029
1030         return 0;
1031
1032         /*
1033          * Something bad happened.  Return the error ...  (If the "error"
1034          * is that there's no Configuration File on the adapter we don't
1035          * want to issue a warning since this is fairly common.)
1036          */
1037 bye:
1038         if (config_issued && ret != -ENOENT)
1039                 dev_warn(adapter, "\"%s\" configuration file error %d\n",
1040                          config_name, -ret);
1041
1042         dev_debug(adapter, "%s: returning ret = %d ..\n", __func__, ret);
1043         return ret;
1044 }
1045
1046 static int adap_init0(struct adapter *adap)
1047 {
1048         struct fw_caps_config_cmd caps_cmd;
1049         int ret = 0;
1050         u32 v, port_vec;
1051         enum dev_state state;
1052         u32 params[7], val[7];
1053         int reset = 1;
1054         int mbox = adap->mbox;
1055
1056         /*
1057          * Contact FW, advertising Master capability.
1058          */
1059         ret = t4_fw_hello(adap, adap->mbox, adap->mbox, MASTER_MAY, &state);
1060         if (ret < 0) {
1061                 dev_err(adap, "%s: could not connect to FW, error %d\n",
1062                         __func__, -ret);
1063                 goto bye;
1064         }
1065
1066         CXGBE_DEBUG_MBOX(adap, "%s: adap->mbox = %d; ret = %d\n", __func__,
1067                          adap->mbox, ret);
1068
1069         if (ret == mbox)
1070                 adap->flags |= MASTER_PF;
1071
1072         if (state == DEV_STATE_INIT) {
1073                 /*
1074                  * Force halt and reset FW because a previous instance may have
1075                  * exited abnormally without properly shutting down
1076                  */
1077                 ret = t4_fw_halt(adap, adap->mbox, reset);
1078                 if (ret < 0) {
1079                         dev_err(adap, "Failed to halt. Exit.\n");
1080                         goto bye;
1081                 }
1082
1083                 ret = t4_fw_restart(adap, adap->mbox, reset);
1084                 if (ret < 0) {
1085                         dev_err(adap, "Failed to restart. Exit.\n");
1086                         goto bye;
1087                 }
1088                 state = (enum dev_state)((unsigned)state & ~DEV_STATE_INIT);
1089         }
1090
1091         t4_get_version_info(adap);
1092
1093         ret = t4_get_core_clock(adap, &adap->params.vpd);
1094         if (ret < 0) {
1095                 dev_err(adap, "%s: could not get core clock, error %d\n",
1096                         __func__, -ret);
1097                 goto bye;
1098         }
1099
1100         /*
1101          * If the firmware is initialized already (and we're not forcing a
1102          * master initialization), note that we're living with existing
1103          * adapter parameters.  Otherwise, it's time to try initializing the
1104          * adapter ...
1105          */
1106         if (state == DEV_STATE_INIT) {
1107                 dev_info(adap, "Coming up as %s: Adapter already initialized\n",
1108                          adap->flags & MASTER_PF ? "MASTER" : "SLAVE");
1109         } else {
1110                 dev_info(adap, "Coming up as MASTER: Initializing adapter\n");
1111
1112                 ret = adap_init0_config(adap, reset);
1113                 if (ret == -ENOENT) {
1114                         dev_err(adap,
1115                                 "No Configuration File present on adapter. Using hard-wired configuration parameters.\n");
1116                         goto bye;
1117                 }
1118         }
1119         if (ret < 0) {
1120                 dev_err(adap, "could not initialize adapter, error %d\n", -ret);
1121                 goto bye;
1122         }
1123
1124         /* Now that we've successfully configured and initialized the adapter
1125          * (or found it already initialized), we can ask the Firmware what
1126          * resources it has provisioned for us.
1127          */
1128         ret = t4_get_pfres(adap);
1129         if (ret) {
1130                 dev_err(adap->pdev_dev,
1131                         "Unable to retrieve resource provisioning info\n");
1132                 goto bye;
1133         }
1134
1135         /* Find out what ports are available to us. */
1136         v = V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_DEV) |
1137             V_FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_DEV_PORTVEC);
1138         ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 1, &v, &port_vec);
1139         if (ret < 0) {
1140                 dev_err(adap, "%s: failure in t4_query_params; error = %d\n",
1141                         __func__, ret);
1142                 goto bye;
1143         }
1144
1145         adap->params.nports = hweight32(port_vec);
1146         adap->params.portvec = port_vec;
1147
1148         dev_debug(adap, "%s: adap->params.nports = %u\n", __func__,
1149                   adap->params.nports);
1150
1151         /*
1152          * Give the SGE code a chance to pull in anything that it needs ...
1153          * Note that this must be called after we retrieve our VPD parameters
1154          * in order to know how to convert core ticks to seconds, etc.
1155          */
1156         ret = t4_sge_init(adap);
1157         if (ret < 0) {
1158                 dev_err(adap, "t4_sge_init failed with error %d\n",
1159                         -ret);
1160                 goto bye;
1161         }
1162
1163         /*
1164          * Grab some of our basic fundamental operating parameters.
1165          */
1166         params[0] = CXGBE_FW_PARAM_PFVF(L2T_START);
1167         params[1] = CXGBE_FW_PARAM_PFVF(L2T_END);
1168         params[2] = CXGBE_FW_PARAM_PFVF(FILTER_START);
1169         params[3] = CXGBE_FW_PARAM_PFVF(FILTER_END);
1170         ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 4, params, val);
1171         if (ret < 0)
1172                 goto bye;
1173         adap->l2t_start = val[0];
1174         adap->l2t_end = val[1];
1175         adap->tids.ftid_base = val[2];
1176         adap->tids.nftids = val[3] - val[2] + 1;
1177
1178         params[0] = CXGBE_FW_PARAM_PFVF(CLIP_START);
1179         params[1] = CXGBE_FW_PARAM_PFVF(CLIP_END);
1180         ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 2, params, val);
1181         if (ret < 0)
1182                 goto bye;
1183         adap->clipt_start = val[0];
1184         adap->clipt_end = val[1];
1185
1186         /*
1187          * Get device capabilities so we can determine what resources we need
1188          * to manage.
1189          */
1190         memset(&caps_cmd, 0, sizeof(caps_cmd));
1191         caps_cmd.op_to_write = htonl(V_FW_CMD_OP(FW_CAPS_CONFIG_CMD) |
1192                                      F_FW_CMD_REQUEST | F_FW_CMD_READ);
1193         caps_cmd.cfvalid_to_len16 = htonl(FW_LEN16(caps_cmd));
1194         ret = t4_wr_mbox(adap, adap->mbox, &caps_cmd, sizeof(caps_cmd),
1195                          &caps_cmd);
1196         if (ret < 0)
1197                 goto bye;
1198
1199         if ((caps_cmd.niccaps & cpu_to_be16(FW_CAPS_CONFIG_NIC_HASHFILTER)) &&
1200             is_t6(adap->params.chip)) {
1201                 if (cxgbe_init_hash_filter(adap) < 0)
1202                         goto bye;
1203         }
1204
1205         /* See if FW supports FW_FILTER2 work request */
1206         if (is_t4(adap->params.chip)) {
1207                 adap->params.filter2_wr_support = 0;
1208         } else {
1209                 params[0] = CXGBE_FW_PARAM_DEV(FILTER2_WR);
1210                 ret = t4_query_params(adap, adap->mbox, adap->pf, 0,
1211                                       1, params, val);
1212                 adap->params.filter2_wr_support = (ret == 0 && val[0] != 0);
1213         }
1214
1215         /* query tid-related parameters */
1216         params[0] = CXGBE_FW_PARAM_DEV(NTID);
1217         ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 1,
1218                               params, val);
1219         if (ret < 0)
1220                 goto bye;
1221         adap->tids.ntids = val[0];
1222         adap->tids.natids = min(adap->tids.ntids / 2, MAX_ATIDS);
1223
1224         /* If we're running on newer firmware, let it know that we're
1225          * prepared to deal with encapsulated CPL messages.  Older
1226          * firmware won't understand this and we'll just get
1227          * unencapsulated messages ...
1228          */
1229         params[0] = CXGBE_FW_PARAM_PFVF(CPLFW4MSG_ENCAP);
1230         val[0] = 1;
1231         (void)t4_set_params(adap, adap->mbox, adap->pf, 0, 1, params, val);
1232
1233         /*
1234          * Find out whether we're allowed to use the T5+ ULPTX MEMWRITE DSGL
1235          * capability.  Earlier versions of the firmware didn't have the
1236          * ULPTX_MEMWRITE_DSGL so we'll interpret a query failure as no
1237          * permission to use ULPTX MEMWRITE DSGL.
1238          */
1239         if (is_t4(adap->params.chip)) {
1240                 adap->params.ulptx_memwrite_dsgl = false;
1241         } else {
1242                 params[0] = CXGBE_FW_PARAM_DEV(ULPTX_MEMWRITE_DSGL);
1243                 ret = t4_query_params(adap, adap->mbox, adap->pf, 0,
1244                                       1, params, val);
1245                 adap->params.ulptx_memwrite_dsgl = (ret == 0 && val[0] != 0);
1246         }
1247
1248         /* Query for max number of packets that can be coalesced for Tx */
1249         params[0] = CXGBE_FW_PARAM_PFVF(MAX_PKTS_PER_ETH_TX_PKTS_WR);
1250         ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 1, params, val);
1251         if (!ret && val[0] > 0)
1252                 adap->params.max_tx_coalesce_num = val[0];
1253         else
1254                 adap->params.max_tx_coalesce_num = ETH_COALESCE_PKT_NUM;
1255
1256         /*
1257          * The MTU/MSS Table is initialized by now, so load their values.  If
1258          * we're initializing the adapter, then we'll make any modifications
1259          * we want to the MTU/MSS Table and also initialize the congestion
1260          * parameters.
1261          */
1262         t4_read_mtu_tbl(adap, adap->params.mtus, NULL);
1263         if (state != DEV_STATE_INIT) {
1264                 int i;
1265
1266                 /*
1267                  * The default MTU Table contains values 1492 and 1500.
1268                  * However, for TCP, it's better to have two values which are
1269                  * a multiple of 8 +/- 4 bytes apart near this popular MTU.
1270                  * This allows us to have a TCP Data Payload which is a
1271                  * multiple of 8 regardless of what combination of TCP Options
1272                  * are in use (always a multiple of 4 bytes) which is
1273                  * important for performance reasons.  For instance, if no
1274                  * options are in use, then we have a 20-byte IP header and a
1275                  * 20-byte TCP header.  In this case, a 1500-byte MSS would
1276                  * result in a TCP Data Payload of 1500 - 40 == 1460 bytes
1277                  * which is not a multiple of 8.  So using an MSS of 1488 in
1278                  * this case results in a TCP Data Payload of 1448 bytes which
1279                  * is a multiple of 8.  On the other hand, if 12-byte TCP Time
1280                  * Stamps have been negotiated, then an MTU of 1500 bytes
1281                  * results in a TCP Data Payload of 1448 bytes which, as
1282                  * above, is a multiple of 8 bytes ...
1283                  */
1284                 for (i = 0; i < NMTUS; i++)
1285                         if (adap->params.mtus[i] == 1492) {
1286                                 adap->params.mtus[i] = 1488;
1287                                 break;
1288                         }
1289
1290                 t4_load_mtus(adap, adap->params.mtus, adap->params.a_wnd,
1291                              adap->params.b_wnd);
1292         }
1293         t4_init_sge_params(adap);
1294         t4_init_tp_params(adap);
1295         configure_pcie_ext_tag(adap);
1296         configure_vlan_types(adap);
1297         cxgbe_configure_max_ethqsets(adap);
1298
1299         adap->params.drv_memwin = MEMWIN_NIC;
1300         adap->flags |= FW_OK;
1301         dev_debug(adap, "%s: returning zero..\n", __func__);
1302         return 0;
1303
1304         /*
1305          * Something bad happened.  If a command timed out or failed with EIO
1306          * FW does not operate within its spec or something catastrophic
1307          * happened to HW/FW, stop issuing commands.
1308          */
1309 bye:
1310         if (ret != -ETIMEDOUT && ret != -EIO)
1311                 t4_fw_bye(adap, adap->mbox);
1312         return ret;
1313 }
1314
1315 /**
1316  * t4_os_portmod_changed - handle port module changes
1317  * @adap: the adapter associated with the module change
1318  * @port_id: the port index whose module status has changed
1319  *
1320  * This is the OS-dependent handler for port module changes.  It is
1321  * invoked when a port module is removed or inserted for any OS-specific
1322  * processing.
1323  */
1324 void t4_os_portmod_changed(const struct adapter *adap, int port_id)
1325 {
1326         static const char * const mod_str[] = {
1327                 NULL, "LR", "SR", "ER", "passive DA", "active DA", "LRM"
1328         };
1329
1330         const struct port_info *pi = adap2pinfo(adap, port_id);
1331
1332         if (pi->mod_type == FW_PORT_MOD_TYPE_NONE)
1333                 dev_info(adap, "Port%d: port module unplugged\n", pi->port_id);
1334         else if (pi->mod_type < ARRAY_SIZE(mod_str))
1335                 dev_info(adap, "Port%d: %s port module inserted\n", pi->port_id,
1336                          mod_str[pi->mod_type]);
1337         else if (pi->mod_type == FW_PORT_MOD_TYPE_NOTSUPPORTED)
1338                 dev_info(adap, "Port%d: unsupported port module inserted\n",
1339                          pi->port_id);
1340         else if (pi->mod_type == FW_PORT_MOD_TYPE_UNKNOWN)
1341                 dev_info(adap, "Port%d: unknown port module inserted\n",
1342                          pi->port_id);
1343         else if (pi->mod_type == FW_PORT_MOD_TYPE_ERROR)
1344                 dev_info(adap, "Port%d: transceiver module error\n",
1345                          pi->port_id);
1346         else
1347                 dev_info(adap, "Port%d: unknown module type %d inserted\n",
1348                          pi->port_id, pi->mod_type);
1349 }
1350
1351 bool cxgbe_force_linkup(struct adapter *adap)
1352 {
1353         if (is_pf4(adap))
1354                 return false;   /* force_linkup not required for pf driver */
1355
1356         return adap->devargs.force_link_up;
1357 }
1358
1359 /**
1360  * link_start - enable a port
1361  * @dev: the port to enable
1362  *
1363  * Performs the MAC and PHY actions needed to enable a port.
1364  */
1365 int cxgbe_link_start(struct port_info *pi)
1366 {
1367         struct adapter *adapter = pi->adapter;
1368         u64 conf_offloads;
1369         unsigned int mtu;
1370         int ret;
1371
1372         mtu = pi->eth_dev->data->dev_conf.rxmode.max_rx_pkt_len -
1373               (RTE_ETHER_HDR_LEN + RTE_ETHER_CRC_LEN);
1374
1375         conf_offloads = pi->eth_dev->data->dev_conf.rxmode.offloads;
1376
1377         /*
1378          * We do not set address filters and promiscuity here, the stack does
1379          * that step explicitly.
1380          */
1381         ret = t4_set_rxmode(adapter, adapter->mbox, pi->viid, mtu, -1, -1, -1,
1382                             !!(conf_offloads & DEV_RX_OFFLOAD_VLAN_STRIP),
1383                             true);
1384         if (ret == 0) {
1385                 ret = cxgbe_mpstcam_modify(pi, (int)pi->xact_addr_filt,
1386                                 (u8 *)&pi->eth_dev->data->mac_addrs[0]);
1387                 if (ret >= 0) {
1388                         pi->xact_addr_filt = ret;
1389                         ret = 0;
1390                 }
1391         }
1392         if (ret == 0 && is_pf4(adapter))
1393                 ret = t4_link_l1cfg(adapter, adapter->mbox, pi->tx_chan,
1394                                     &pi->link_cfg);
1395         if (ret == 0) {
1396                 /*
1397                  * Enabling a Virtual Interface can result in an interrupt
1398                  * during the processing of the VI Enable command and, in some
1399                  * paths, result in an attempt to issue another command in the
1400                  * interrupt context.  Thus, we disable interrupts during the
1401                  * course of the VI Enable command ...
1402                  */
1403                 ret = t4_enable_vi_params(adapter, adapter->mbox, pi->viid,
1404                                           true, true, false);
1405         }
1406
1407         if (ret == 0 && cxgbe_force_linkup(adapter))
1408                 pi->eth_dev->data->dev_link.link_status = ETH_LINK_UP;
1409         return ret;
1410 }
1411
1412 /**
1413  * cxgbe_write_rss_conf - flash the RSS configuration for a given port
1414  * @pi: the port
1415  * @rss_hf: Hash configuration to apply
1416  */
1417 int cxgbe_write_rss_conf(const struct port_info *pi, uint64_t rss_hf)
1418 {
1419         struct adapter *adapter = pi->adapter;
1420         const struct sge_eth_rxq *rxq;
1421         u64 flags = 0;
1422         u16 rss;
1423         int err;
1424
1425         /*  Should never be called before setting up sge eth rx queues */
1426         if (!(adapter->flags & FULL_INIT_DONE)) {
1427                 dev_err(adap, "%s No RXQs available on port %d\n",
1428                         __func__, pi->port_id);
1429                 return -EINVAL;
1430         }
1431
1432         /* Don't allow unsupported hash functions */
1433         if (rss_hf & ~CXGBE_RSS_HF_ALL)
1434                 return -EINVAL;
1435
1436         if (rss_hf & CXGBE_RSS_HF_IPV4_MASK)
1437                 flags |= F_FW_RSS_VI_CONFIG_CMD_IP4TWOTUPEN;
1438
1439         if (rss_hf & ETH_RSS_NONFRAG_IPV4_TCP)
1440                 flags |= F_FW_RSS_VI_CONFIG_CMD_IP4FOURTUPEN;
1441
1442         if (rss_hf & ETH_RSS_NONFRAG_IPV4_UDP)
1443                 flags |= F_FW_RSS_VI_CONFIG_CMD_IP4FOURTUPEN |
1444                          F_FW_RSS_VI_CONFIG_CMD_UDPEN;
1445
1446         if (rss_hf & CXGBE_RSS_HF_IPV6_MASK)
1447                 flags |= F_FW_RSS_VI_CONFIG_CMD_IP6TWOTUPEN;
1448
1449         if (rss_hf & CXGBE_RSS_HF_TCP_IPV6_MASK)
1450                 flags |= F_FW_RSS_VI_CONFIG_CMD_IP6TWOTUPEN |
1451                          F_FW_RSS_VI_CONFIG_CMD_IP6FOURTUPEN;
1452
1453         if (rss_hf & CXGBE_RSS_HF_UDP_IPV6_MASK)
1454                 flags |= F_FW_RSS_VI_CONFIG_CMD_IP6TWOTUPEN |
1455                          F_FW_RSS_VI_CONFIG_CMD_IP6FOURTUPEN |
1456                          F_FW_RSS_VI_CONFIG_CMD_UDPEN;
1457
1458         rxq = &adapter->sge.ethrxq[pi->first_qset];
1459         rss = rxq[0].rspq.abs_id;
1460
1461         /* If Tunnel All Lookup isn't specified in the global RSS
1462          * Configuration, then we need to specify a default Ingress
1463          * Queue for any ingress packets which aren't hashed.  We'll
1464          * use our first ingress queue ...
1465          */
1466         err = t4_config_vi_rss(adapter, adapter->mbox, pi->viid,
1467                                flags, rss);
1468         return err;
1469 }
1470
1471 /**
1472  * cxgbe_write_rss - write the RSS table for a given port
1473  * @pi: the port
1474  * @queues: array of queue indices for RSS
1475  *
1476  * Sets up the portion of the HW RSS table for the port's VI to distribute
1477  * packets to the Rx queues in @queues.
1478  */
1479 int cxgbe_write_rss(const struct port_info *pi, const u16 *queues)
1480 {
1481         u16 *rss;
1482         int i, err;
1483         struct adapter *adapter = pi->adapter;
1484         const struct sge_eth_rxq *rxq;
1485
1486         /*  Should never be called before setting up sge eth rx queues */
1487         BUG_ON(!(adapter->flags & FULL_INIT_DONE));
1488
1489         rxq = &adapter->sge.ethrxq[pi->first_qset];
1490         rss = rte_zmalloc(NULL, pi->rss_size * sizeof(u16), 0);
1491         if (!rss)
1492                 return -ENOMEM;
1493
1494         /* map the queue indices to queue ids */
1495         for (i = 0; i < pi->rss_size; i++, queues++)
1496                 rss[i] = rxq[*queues].rspq.abs_id;
1497
1498         err = t4_config_rss_range(adapter, adapter->pf, pi->viid, 0,
1499                                   pi->rss_size, rss, pi->rss_size);
1500         rte_free(rss);
1501         return err;
1502 }
1503
1504 /**
1505  * setup_rss - configure RSS
1506  * @adapter: the adapter
1507  *
1508  * Sets up RSS to distribute packets to multiple receive queues.  We
1509  * configure the RSS CPU lookup table to distribute to the number of HW
1510  * receive queues, and the response queue lookup table to narrow that
1511  * down to the response queues actually configured for each port.
1512  * We always configure the RSS mapping for all ports since the mapping
1513  * table has plenty of entries.
1514  */
1515 int cxgbe_setup_rss(struct port_info *pi)
1516 {
1517         int j, err;
1518         struct adapter *adapter = pi->adapter;
1519
1520         dev_debug(adapter, "%s:  pi->rss_size = %u; pi->n_rx_qsets = %u\n",
1521                   __func__, pi->rss_size, pi->n_rx_qsets);
1522
1523         if (!(pi->flags & PORT_RSS_DONE)) {
1524                 if (adapter->flags & FULL_INIT_DONE) {
1525                         /* Fill default values with equal distribution */
1526                         for (j = 0; j < pi->rss_size; j++)
1527                                 pi->rss[j] = j % pi->n_rx_qsets;
1528
1529                         err = cxgbe_write_rss(pi, pi->rss);
1530                         if (err)
1531                                 return err;
1532
1533                         err = cxgbe_write_rss_conf(pi, pi->rss_hf);
1534                         if (err)
1535                                 return err;
1536                         pi->flags |= PORT_RSS_DONE;
1537                 }
1538         }
1539         return 0;
1540 }
1541
1542 /*
1543  * Enable NAPI scheduling and interrupt generation for all Rx queues.
1544  */
1545 static void enable_rx(struct adapter *adap, struct sge_rspq *q)
1546 {
1547         /* 0-increment GTS to start the timer and enable interrupts */
1548         t4_write_reg(adap, is_pf4(adap) ? MYPF_REG(A_SGE_PF_GTS) :
1549                                           T4VF_SGE_BASE_ADDR + A_SGE_VF_GTS,
1550                      V_SEINTARM(q->intr_params) |
1551                      V_INGRESSQID(q->cntxt_id));
1552 }
1553
1554 void cxgbe_enable_rx_queues(struct port_info *pi)
1555 {
1556         struct adapter *adap = pi->adapter;
1557         struct sge *s = &adap->sge;
1558         unsigned int i;
1559
1560         for (i = 0; i < pi->n_rx_qsets; i++)
1561                 enable_rx(adap, &s->ethrxq[pi->first_qset + i].rspq);
1562 }
1563
1564 /**
1565  * fw_caps_to_speed_caps - translate Firmware Port Caps to Speed Caps.
1566  * @port_type: Firmware Port Type
1567  * @fw_caps: Firmware Port Capabilities
1568  * @speed_caps: Device Info Speed Capabilities
1569  *
1570  * Translate a Firmware Port Capabilities specification to Device Info
1571  * Speed Capabilities.
1572  */
1573 static void fw_caps_to_speed_caps(enum fw_port_type port_type,
1574                                   unsigned int fw_caps,
1575                                   u32 *speed_caps)
1576 {
1577 #define SET_SPEED(__speed_name) \
1578         do { \
1579                 *speed_caps |= ETH_LINK_ ## __speed_name; \
1580         } while (0)
1581
1582 #define FW_CAPS_TO_SPEED(__fw_name) \
1583         do { \
1584                 if (fw_caps & FW_PORT_CAP32_ ## __fw_name) \
1585                         SET_SPEED(__fw_name); \
1586         } while (0)
1587
1588         switch (port_type) {
1589         case FW_PORT_TYPE_BT_SGMII:
1590         case FW_PORT_TYPE_BT_XFI:
1591         case FW_PORT_TYPE_BT_XAUI:
1592                 FW_CAPS_TO_SPEED(SPEED_100M);
1593                 FW_CAPS_TO_SPEED(SPEED_1G);
1594                 FW_CAPS_TO_SPEED(SPEED_10G);
1595                 break;
1596
1597         case FW_PORT_TYPE_KX4:
1598         case FW_PORT_TYPE_KX:
1599         case FW_PORT_TYPE_FIBER_XFI:
1600         case FW_PORT_TYPE_FIBER_XAUI:
1601         case FW_PORT_TYPE_SFP:
1602         case FW_PORT_TYPE_QSFP_10G:
1603         case FW_PORT_TYPE_QSA:
1604                 FW_CAPS_TO_SPEED(SPEED_1G);
1605                 FW_CAPS_TO_SPEED(SPEED_10G);
1606                 break;
1607
1608         case FW_PORT_TYPE_KR:
1609                 SET_SPEED(SPEED_10G);
1610                 break;
1611
1612         case FW_PORT_TYPE_BP_AP:
1613         case FW_PORT_TYPE_BP4_AP:
1614                 SET_SPEED(SPEED_1G);
1615                 SET_SPEED(SPEED_10G);
1616                 break;
1617
1618         case FW_PORT_TYPE_BP40_BA:
1619         case FW_PORT_TYPE_QSFP:
1620                 SET_SPEED(SPEED_40G);
1621                 break;
1622
1623         case FW_PORT_TYPE_CR_QSFP:
1624         case FW_PORT_TYPE_SFP28:
1625         case FW_PORT_TYPE_KR_SFP28:
1626                 FW_CAPS_TO_SPEED(SPEED_1G);
1627                 FW_CAPS_TO_SPEED(SPEED_10G);
1628                 FW_CAPS_TO_SPEED(SPEED_25G);
1629                 break;
1630
1631         case FW_PORT_TYPE_CR2_QSFP:
1632                 SET_SPEED(SPEED_50G);
1633                 break;
1634
1635         case FW_PORT_TYPE_KR4_100G:
1636         case FW_PORT_TYPE_CR4_QSFP:
1637                 FW_CAPS_TO_SPEED(SPEED_25G);
1638                 FW_CAPS_TO_SPEED(SPEED_40G);
1639                 FW_CAPS_TO_SPEED(SPEED_50G);
1640                 FW_CAPS_TO_SPEED(SPEED_100G);
1641                 break;
1642
1643         default:
1644                 break;
1645         }
1646
1647 #undef FW_CAPS_TO_SPEED
1648 #undef SET_SPEED
1649 }
1650
1651 /**
1652  * cxgbe_get_speed_caps - Fetch supported speed capabilities
1653  * @pi: Underlying port's info
1654  * @speed_caps: Device Info speed capabilities
1655  *
1656  * Fetch supported speed capabilities of the underlying port.
1657  */
1658 void cxgbe_get_speed_caps(struct port_info *pi, u32 *speed_caps)
1659 {
1660         *speed_caps = 0;
1661
1662         fw_caps_to_speed_caps(pi->port_type, pi->link_cfg.pcaps,
1663                               speed_caps);
1664
1665         if (!(pi->link_cfg.pcaps & FW_PORT_CAP32_ANEG))
1666                 *speed_caps |= ETH_LINK_SPEED_FIXED;
1667 }
1668
1669 /**
1670  * cxgbe_set_link_status - Set device link up or down.
1671  * @pi: Underlying port's info
1672  * @status: 0 - down, 1 - up
1673  *
1674  * Set the device link up or down.
1675  */
1676 int cxgbe_set_link_status(struct port_info *pi, bool status)
1677 {
1678         struct adapter *adapter = pi->adapter;
1679         int err = 0;
1680
1681         err = t4_enable_vi(adapter, adapter->mbox, pi->viid, status, status);
1682         if (err) {
1683                 dev_err(adapter, "%s: disable_vi failed: %d\n", __func__, err);
1684                 return err;
1685         }
1686
1687         if (!status)
1688                 t4_reset_link_config(adapter, pi->pidx);
1689
1690         return 0;
1691 }
1692
1693 /**
1694  * cxgb_up - enable the adapter
1695  * @adap: adapter being enabled
1696  *
1697  * Called when the first port is enabled, this function performs the
1698  * actions necessary to make an adapter operational, such as completing
1699  * the initialization of HW modules, and enabling interrupts.
1700  */
1701 int cxgbe_up(struct adapter *adap)
1702 {
1703         enable_rx(adap, &adap->sge.fw_evtq);
1704         t4_sge_tx_monitor_start(adap);
1705         if (is_pf4(adap))
1706                 t4_intr_enable(adap);
1707         adap->flags |= FULL_INIT_DONE;
1708
1709         /* TODO: deadman watchdog ?? */
1710         return 0;
1711 }
1712
1713 /*
1714  * Close the port
1715  */
1716 int cxgbe_down(struct port_info *pi)
1717 {
1718         return cxgbe_set_link_status(pi, false);
1719 }
1720
1721 /*
1722  * Release resources when all the ports have been stopped.
1723  */
1724 void cxgbe_close(struct adapter *adapter)
1725 {
1726         struct port_info *pi;
1727         int i;
1728
1729         if (adapter->flags & FULL_INIT_DONE) {
1730                 tid_free(&adapter->tids);
1731                 t4_cleanup_mpstcam(adapter);
1732                 t4_cleanup_clip_tbl(adapter);
1733                 t4_cleanup_l2t(adapter);
1734                 if (is_pf4(adapter))
1735                         t4_intr_disable(adapter);
1736                 t4_sge_tx_monitor_stop(adapter);
1737                 t4_free_sge_resources(adapter);
1738                 for_each_port(adapter, i) {
1739                         pi = adap2pinfo(adapter, i);
1740                         if (pi->viid != 0)
1741                                 t4_free_vi(adapter, adapter->mbox,
1742                                            adapter->pf, 0, pi->viid);
1743                         rte_eth_dev_release_port(pi->eth_dev);
1744                 }
1745                 adapter->flags &= ~FULL_INIT_DONE;
1746         }
1747
1748         if (is_pf4(adapter) && (adapter->flags & FW_OK))
1749                 t4_fw_bye(adapter, adapter->mbox);
1750 }
1751
1752 int cxgbe_probe(struct adapter *adapter)
1753 {
1754         struct port_info *pi;
1755         int chip;
1756         int func, i;
1757         int err = 0;
1758         u32 whoami;
1759
1760         whoami = t4_read_reg(adapter, A_PL_WHOAMI);
1761         chip = t4_get_chip_type(adapter,
1762                         CHELSIO_PCI_ID_VER(adapter->pdev->id.device_id));
1763         if (chip < 0)
1764                 return chip;
1765
1766         func = CHELSIO_CHIP_VERSION(chip) <= CHELSIO_T5 ?
1767                G_SOURCEPF(whoami) : G_T6_SOURCEPF(whoami);
1768
1769         adapter->mbox = func;
1770         adapter->pf = func;
1771
1772         t4_os_lock_init(&adapter->mbox_lock);
1773         TAILQ_INIT(&adapter->mbox_list);
1774         t4_os_lock_init(&adapter->win0_lock);
1775
1776         err = t4_prep_adapter(adapter);
1777         if (err)
1778                 return err;
1779
1780         setup_memwin(adapter);
1781         err = adap_init0(adapter);
1782         if (err) {
1783                 dev_err(adapter, "%s: Adapter initialization failed, error %d\n",
1784                         __func__, err);
1785                 goto out_free;
1786         }
1787
1788         if (!is_t4(adapter->params.chip)) {
1789                 /*
1790                  * The userspace doorbell BAR is split evenly into doorbell
1791                  * regions, each associated with an egress queue.  If this
1792                  * per-queue region is large enough (at least UDBS_SEG_SIZE)
1793                  * then it can be used to submit a tx work request with an
1794                  * implied doorbell.  Enable write combining on the BAR if
1795                  * there is room for such work requests.
1796                  */
1797                 int s_qpp, qpp, num_seg;
1798
1799                 s_qpp = (S_QUEUESPERPAGEPF0 +
1800                         (S_QUEUESPERPAGEPF1 - S_QUEUESPERPAGEPF0) *
1801                         adapter->pf);
1802                 qpp = 1 << ((t4_read_reg(adapter,
1803                                 A_SGE_EGRESS_QUEUES_PER_PAGE_PF) >> s_qpp)
1804                                 & M_QUEUESPERPAGEPF0);
1805                 num_seg = CXGBE_PAGE_SIZE / UDBS_SEG_SIZE;
1806                 if (qpp > num_seg)
1807                         dev_warn(adapter, "Incorrect SGE EGRESS QUEUES_PER_PAGE configuration, continuing in debug mode\n");
1808
1809                 adapter->bar2 = (void *)adapter->pdev->mem_resource[2].addr;
1810                 if (!adapter->bar2) {
1811                         dev_err(adapter, "cannot map device bar2 region\n");
1812                         err = -ENOMEM;
1813                         goto out_free;
1814                 }
1815                 t4_write_reg(adapter, A_SGE_STAT_CFG, V_STATSOURCE_T5(7) |
1816                              V_STATMODE(0));
1817         }
1818
1819         for_each_port(adapter, i) {
1820                 const unsigned int numa_node = rte_socket_id();
1821                 char name[RTE_ETH_NAME_MAX_LEN];
1822                 struct rte_eth_dev *eth_dev;
1823
1824                 snprintf(name, sizeof(name), "%s_%d",
1825                          adapter->pdev->device.name, i);
1826
1827                 if (i == 0) {
1828                         /* First port is already allocated by DPDK */
1829                         eth_dev = adapter->eth_dev;
1830                         goto allocate_mac;
1831                 }
1832
1833                 /*
1834                  * now do all data allocation - for eth_dev structure,
1835                  * and internal (private) data for the remaining ports
1836                  */
1837
1838                 /* reserve an ethdev entry */
1839                 eth_dev = rte_eth_dev_allocate(name);
1840                 if (!eth_dev)
1841                         goto out_free;
1842
1843                 eth_dev->data->dev_private =
1844                         rte_zmalloc_socket(name, sizeof(struct port_info),
1845                                            RTE_CACHE_LINE_SIZE, numa_node);
1846                 if (!eth_dev->data->dev_private)
1847                         goto out_free;
1848
1849 allocate_mac:
1850                 pi = eth_dev->data->dev_private;
1851                 adapter->port[i] = pi;
1852                 pi->eth_dev = eth_dev;
1853                 pi->adapter = adapter;
1854                 pi->xact_addr_filt = -1;
1855                 pi->port_id = i;
1856                 pi->pidx = i;
1857
1858                 pi->eth_dev->device = &adapter->pdev->device;
1859                 pi->eth_dev->dev_ops = adapter->eth_dev->dev_ops;
1860                 pi->eth_dev->tx_pkt_burst = adapter->eth_dev->tx_pkt_burst;
1861                 pi->eth_dev->rx_pkt_burst = adapter->eth_dev->rx_pkt_burst;
1862
1863                 rte_eth_copy_pci_info(pi->eth_dev, adapter->pdev);
1864
1865                 pi->eth_dev->data->mac_addrs = rte_zmalloc(name,
1866                                                         RTE_ETHER_ADDR_LEN, 0);
1867                 if (!pi->eth_dev->data->mac_addrs) {
1868                         dev_err(adapter, "%s: Mem allocation failed for storing mac addr, aborting\n",
1869                                 __func__);
1870                         err = -1;
1871                         goto out_free;
1872                 }
1873
1874                 if (i > 0) {
1875                         /* First port will be notified by upper layer */
1876                         rte_eth_dev_probing_finish(eth_dev);
1877                 }
1878         }
1879
1880         if (adapter->flags & FW_OK) {
1881                 err = t4_port_init(adapter, adapter->mbox, adapter->pf, 0);
1882                 if (err) {
1883                         dev_err(adapter, "%s: t4_port_init failed with err %d\n",
1884                                 __func__, err);
1885                         goto out_free;
1886                 }
1887         }
1888
1889         cxgbe_cfg_queues(adapter->eth_dev);
1890
1891         cxgbe_print_adapter_info(adapter);
1892         cxgbe_print_port_info(adapter);
1893
1894         adapter->clipt = t4_init_clip_tbl(adapter->clipt_start,
1895                                           adapter->clipt_end);
1896         if (!adapter->clipt) {
1897                 /* We tolerate a lack of clip_table, giving up some
1898                  * functionality
1899                  */
1900                 dev_warn(adapter, "could not allocate CLIP. Continuing\n");
1901         }
1902
1903         adapter->l2t = t4_init_l2t(adapter->l2t_start, adapter->l2t_end);
1904         if (!adapter->l2t) {
1905                 /* We tolerate a lack of L2T, giving up some functionality */
1906                 dev_warn(adapter, "could not allocate L2T. Continuing\n");
1907         }
1908
1909         if (tid_init(&adapter->tids) < 0) {
1910                 /* Disable filtering support */
1911                 dev_warn(adapter, "could not allocate TID table, "
1912                          "filter support disabled. Continuing\n");
1913         }
1914
1915         adapter->mpstcam = t4_init_mpstcam(adapter);
1916         if (!adapter->mpstcam)
1917                 dev_warn(adapter, "could not allocate mps tcam table."
1918                          " Continuing\n");
1919
1920         if (is_hashfilter(adapter)) {
1921                 if (t4_read_reg(adapter, A_LE_DB_CONFIG) & F_HASHEN) {
1922                         u32 hash_base, hash_reg;
1923
1924                         hash_reg = A_LE_DB_TID_HASHBASE;
1925                         hash_base = t4_read_reg(adapter, hash_reg);
1926                         adapter->tids.hash_base = hash_base / 4;
1927                 }
1928         } else {
1929                 /* Disable hash filtering support */
1930                 dev_warn(adapter,
1931                          "Maskless filter support disabled. Continuing\n");
1932         }
1933
1934         err = cxgbe_init_rss(adapter);
1935         if (err)
1936                 goto out_free;
1937
1938         return 0;
1939
1940 out_free:
1941         for_each_port(adapter, i) {
1942                 pi = adap2pinfo(adapter, i);
1943                 if (pi->viid != 0)
1944                         t4_free_vi(adapter, adapter->mbox, adapter->pf,
1945                                    0, pi->viid);
1946                 rte_eth_dev_release_port(pi->eth_dev);
1947         }
1948
1949         if (adapter->flags & FW_OK)
1950                 t4_fw_bye(adapter, adapter->mbox);
1951         return -err;
1952 }