1 /* QLogic qed NIC Driver
2 * Copyright (c) 2015 QLogic Corporation
4 * This software is available under the terms of the GNU General Public License
5 * (GPL) Version 2, available from the file COPYING in the main directory of
9 #include <linux/types.h>
10 #include <asm/byteorder.h>
12 #include <linux/delay.h>
13 #include <linux/dma-mapping.h>
14 #include <linux/errno.h>
15 #include <linux/kernel.h>
16 #include <linux/mutex.h>
17 #include <linux/pci.h>
18 #include <linux/slab.h>
19 #include <linux/string.h>
20 #include <linux/vmalloc.h>
21 #include <linux/etherdevice.h>
22 #include <linux/qed/qed_chain.h>
23 #include <linux/qed/qed_if.h>
27 #include "qed_dev_api.h"
30 #include "qed_init_ops.h"
33 #include "qed_reg_addr.h"
35 #include "qed_sriov.h"
38 static DEFINE_SPINLOCK(qm_lock);
40 /* API common to all protocols */
42 BAR_ID_0, /* used for GRC */
43 BAR_ID_1 /* Used for doorbells */
46 static u32 qed_hw_bar_size(struct qed_hwfn *p_hwfn, enum BAR_ID bar_id)
48 u32 bar_reg = (bar_id == BAR_ID_0 ?
49 PGLUE_B_REG_PF_BAR0_SIZE : PGLUE_B_REG_PF_BAR1_SIZE);
52 if (IS_VF(p_hwfn->cdev))
55 val = qed_rd(p_hwfn, p_hwfn->p_main_ptt, bar_reg);
57 return 1 << (val + 15);
59 /* Old MFW initialized above registered only conditionally */
60 if (p_hwfn->cdev->num_hwfns > 1) {
62 "BAR size not configured. Assuming BAR size of 256kB for GRC and 512kB for DB\n");
63 return BAR_ID_0 ? 256 * 1024 : 512 * 1024;
66 "BAR size not configured. Assuming BAR size of 512kB for GRC and 512kB for DB\n");
71 void qed_init_dp(struct qed_dev *cdev, u32 dp_module, u8 dp_level)
75 cdev->dp_level = dp_level;
76 cdev->dp_module = dp_module;
77 for (i = 0; i < MAX_HWFNS_PER_DEVICE; i++) {
78 struct qed_hwfn *p_hwfn = &cdev->hwfns[i];
80 p_hwfn->dp_level = dp_level;
81 p_hwfn->dp_module = dp_module;
85 void qed_init_struct(struct qed_dev *cdev)
89 for (i = 0; i < MAX_HWFNS_PER_DEVICE; i++) {
90 struct qed_hwfn *p_hwfn = &cdev->hwfns[i];
94 p_hwfn->b_active = false;
96 mutex_init(&p_hwfn->dmae_info.mutex);
99 /* hwfn 0 is always active */
100 cdev->hwfns[0].b_active = true;
102 /* set the default cache alignment to 128 */
103 cdev->cache_shift = 7;
106 static void qed_qm_info_free(struct qed_hwfn *p_hwfn)
108 struct qed_qm_info *qm_info = &p_hwfn->qm_info;
110 kfree(qm_info->qm_pq_params);
111 qm_info->qm_pq_params = NULL;
112 kfree(qm_info->qm_vport_params);
113 qm_info->qm_vport_params = NULL;
114 kfree(qm_info->qm_port_params);
115 qm_info->qm_port_params = NULL;
116 kfree(qm_info->wfq_data);
117 qm_info->wfq_data = NULL;
120 void qed_resc_free(struct qed_dev *cdev)
127 kfree(cdev->fw_data);
128 cdev->fw_data = NULL;
130 kfree(cdev->reset_stats);
132 for_each_hwfn(cdev, i) {
133 struct qed_hwfn *p_hwfn = &cdev->hwfns[i];
135 kfree(p_hwfn->p_tx_cids);
136 p_hwfn->p_tx_cids = NULL;
137 kfree(p_hwfn->p_rx_cids);
138 p_hwfn->p_rx_cids = NULL;
141 for_each_hwfn(cdev, i) {
142 struct qed_hwfn *p_hwfn = &cdev->hwfns[i];
144 qed_cxt_mngr_free(p_hwfn);
145 qed_qm_info_free(p_hwfn);
146 qed_spq_free(p_hwfn);
147 qed_eq_free(p_hwfn, p_hwfn->p_eq);
148 qed_consq_free(p_hwfn, p_hwfn->p_consq);
149 qed_int_free(p_hwfn);
150 qed_iov_free(p_hwfn);
151 qed_dmae_info_free(p_hwfn);
152 qed_dcbx_info_free(p_hwfn, p_hwfn->p_dcbx_info);
156 static int qed_init_qm_info(struct qed_hwfn *p_hwfn, bool b_sleepable)
158 u8 num_vports, vf_offset = 0, i, vport_id, num_ports, curr_queue = 0;
159 struct qed_qm_info *qm_info = &p_hwfn->qm_info;
160 struct init_qm_port_params *p_qm_port;
161 bool init_rdma_offload_pq = false;
162 bool init_pure_ack_pq = false;
163 bool init_ooo_pq = false;
164 u16 num_pqs, multi_cos_tcs = 1;
165 u8 pf_wfq = qm_info->pf_wfq;
166 u32 pf_rl = qm_info->pf_rl;
170 #ifdef CONFIG_QED_SRIOV
171 if (p_hwfn->cdev->p_iov_info)
172 num_vfs = p_hwfn->cdev->p_iov_info->total_vfs;
174 memset(qm_info, 0, sizeof(*qm_info));
176 num_pqs = multi_cos_tcs + num_vfs + 1; /* The '1' is for pure-LB */
177 num_vports = (u8)RESC_NUM(p_hwfn, QED_VPORT);
179 if (p_hwfn->hw_info.personality == QED_PCI_ETH_ROCE) {
180 num_pqs++; /* for RoCE queue */
181 init_rdma_offload_pq = true;
182 /* we subtract num_vfs because each require a rate limiter,
183 * and one default rate limiter
185 if (p_hwfn->pf_params.rdma_pf_params.enable_dcqcn)
186 num_pf_rls = RESC_NUM(p_hwfn, QED_RL) - num_vfs - 1;
188 num_pqs += num_pf_rls;
189 qm_info->num_pf_rls = (u8) num_pf_rls;
192 if (p_hwfn->hw_info.personality == QED_PCI_ISCSI) {
193 num_pqs += 2; /* for iSCSI pure-ACK / OOO queue */
194 init_pure_ack_pq = true;
198 /* Sanity checking that setup requires legal number of resources */
199 if (num_pqs > RESC_NUM(p_hwfn, QED_PQ)) {
201 "Need too many Physical queues - 0x%04x when only %04x are available\n",
202 num_pqs, RESC_NUM(p_hwfn, QED_PQ));
206 /* PQs will be arranged as follows: First per-TC PQ then pure-LB quete.
208 qm_info->qm_pq_params = kcalloc(num_pqs,
209 sizeof(struct init_qm_pq_params),
210 b_sleepable ? GFP_KERNEL : GFP_ATOMIC);
211 if (!qm_info->qm_pq_params)
214 qm_info->qm_vport_params = kcalloc(num_vports,
215 sizeof(struct init_qm_vport_params),
216 b_sleepable ? GFP_KERNEL
218 if (!qm_info->qm_vport_params)
221 qm_info->qm_port_params = kcalloc(MAX_NUM_PORTS,
222 sizeof(struct init_qm_port_params),
223 b_sleepable ? GFP_KERNEL
225 if (!qm_info->qm_port_params)
228 qm_info->wfq_data = kcalloc(num_vports, sizeof(struct qed_wfq_data),
229 b_sleepable ? GFP_KERNEL : GFP_ATOMIC);
230 if (!qm_info->wfq_data)
233 vport_id = (u8)RESC_START(p_hwfn, QED_VPORT);
235 /* First init rate limited queues */
236 for (curr_queue = 0; curr_queue < num_pf_rls; curr_queue++) {
237 qm_info->qm_pq_params[curr_queue].vport_id = vport_id++;
238 qm_info->qm_pq_params[curr_queue].tc_id =
239 p_hwfn->hw_info.non_offload_tc;
240 qm_info->qm_pq_params[curr_queue].wrr_group = 1;
241 qm_info->qm_pq_params[curr_queue].rl_valid = 1;
244 /* First init per-TC PQs */
245 for (i = 0; i < multi_cos_tcs; i++) {
246 struct init_qm_pq_params *params =
247 &qm_info->qm_pq_params[curr_queue++];
249 if (p_hwfn->hw_info.personality == QED_PCI_ETH_ROCE ||
250 p_hwfn->hw_info.personality == QED_PCI_ETH) {
251 params->vport_id = vport_id;
252 params->tc_id = p_hwfn->hw_info.non_offload_tc;
253 params->wrr_group = 1;
255 params->vport_id = vport_id;
256 params->tc_id = p_hwfn->hw_info.offload_tc;
257 params->wrr_group = 1;
261 /* Then init pure-LB PQ */
262 qm_info->pure_lb_pq = curr_queue;
263 qm_info->qm_pq_params[curr_queue].vport_id =
264 (u8) RESC_START(p_hwfn, QED_VPORT);
265 qm_info->qm_pq_params[curr_queue].tc_id = PURE_LB_TC;
266 qm_info->qm_pq_params[curr_queue].wrr_group = 1;
269 qm_info->offload_pq = 0;
270 if (init_rdma_offload_pq) {
271 qm_info->offload_pq = curr_queue;
272 qm_info->qm_pq_params[curr_queue].vport_id = vport_id;
273 qm_info->qm_pq_params[curr_queue].tc_id =
274 p_hwfn->hw_info.offload_tc;
275 qm_info->qm_pq_params[curr_queue].wrr_group = 1;
279 if (init_pure_ack_pq) {
280 qm_info->pure_ack_pq = curr_queue;
281 qm_info->qm_pq_params[curr_queue].vport_id = vport_id;
282 qm_info->qm_pq_params[curr_queue].tc_id =
283 p_hwfn->hw_info.offload_tc;
284 qm_info->qm_pq_params[curr_queue].wrr_group = 1;
289 qm_info->ooo_pq = curr_queue;
290 qm_info->qm_pq_params[curr_queue].vport_id = vport_id;
291 qm_info->qm_pq_params[curr_queue].tc_id = DCBX_ISCSI_OOO_TC;
292 qm_info->qm_pq_params[curr_queue].wrr_group = 1;
296 /* Then init per-VF PQs */
297 vf_offset = curr_queue;
298 for (i = 0; i < num_vfs; i++) {
299 /* First vport is used by the PF */
300 qm_info->qm_pq_params[curr_queue].vport_id = vport_id + i + 1;
301 qm_info->qm_pq_params[curr_queue].tc_id =
302 p_hwfn->hw_info.non_offload_tc;
303 qm_info->qm_pq_params[curr_queue].wrr_group = 1;
304 qm_info->qm_pq_params[curr_queue].rl_valid = 1;
308 qm_info->vf_queues_offset = vf_offset;
309 qm_info->num_pqs = num_pqs;
310 qm_info->num_vports = num_vports;
312 /* Initialize qm port parameters */
313 num_ports = p_hwfn->cdev->num_ports_in_engines;
314 for (i = 0; i < num_ports; i++) {
315 p_qm_port = &qm_info->qm_port_params[i];
316 p_qm_port->active = 1;
318 p_qm_port->active_phys_tcs = 0x7;
320 p_qm_port->active_phys_tcs = 0x9f;
321 p_qm_port->num_pbf_cmd_lines = PBF_MAX_CMD_LINES / num_ports;
322 p_qm_port->num_btb_blocks = BTB_MAX_BLOCKS / num_ports;
325 qm_info->max_phys_tcs_per_port = NUM_OF_PHYS_TCS;
327 qm_info->start_pq = (u16)RESC_START(p_hwfn, QED_PQ);
329 qm_info->num_vf_pqs = num_vfs;
330 qm_info->start_vport = (u8) RESC_START(p_hwfn, QED_VPORT);
332 for (i = 0; i < qm_info->num_vports; i++)
333 qm_info->qm_vport_params[i].vport_wfq = 1;
335 qm_info->vport_rl_en = 1;
336 qm_info->vport_wfq_en = 1;
337 qm_info->pf_rl = pf_rl;
338 qm_info->pf_wfq = pf_wfq;
343 DP_NOTICE(p_hwfn, "Failed to allocate memory for QM params\n");
344 qed_qm_info_free(p_hwfn);
348 /* This function reconfigures the QM pf on the fly.
349 * For this purpose we:
350 * 1. reconfigure the QM database
351 * 2. set new values to runtime arrat
352 * 3. send an sdm_qm_cmd through the rbc interface to stop the QM
353 * 4. activate init tool in QM_PF stage
354 * 5. send an sdm_qm_cmd through rbc interface to release the QM
356 int qed_qm_reconf(struct qed_hwfn *p_hwfn, struct qed_ptt *p_ptt)
358 struct qed_qm_info *qm_info = &p_hwfn->qm_info;
362 /* qm_info is allocated in qed_init_qm_info() which is already called
363 * from qed_resc_alloc() or previous call of qed_qm_reconf().
364 * The allocated size may change each init, so we free it before next
367 qed_qm_info_free(p_hwfn);
369 /* initialize qed's qm data structure */
370 rc = qed_init_qm_info(p_hwfn, false);
374 /* stop PF's qm queues */
375 spin_lock_bh(&qm_lock);
376 b_rc = qed_send_qm_stop_cmd(p_hwfn, p_ptt, false, true,
377 qm_info->start_pq, qm_info->num_pqs);
378 spin_unlock_bh(&qm_lock);
382 /* clear the QM_PF runtime phase leftovers from previous init */
383 qed_init_clear_rt_data(p_hwfn);
385 /* prepare QM portion of runtime array */
386 qed_qm_init_pf(p_hwfn);
388 /* activate init tool on runtime array */
389 rc = qed_init_run(p_hwfn, p_ptt, PHASE_QM_PF, p_hwfn->rel_pf_id,
390 p_hwfn->hw_info.hw_mode);
394 /* start PF's qm queues */
395 spin_lock_bh(&qm_lock);
396 b_rc = qed_send_qm_stop_cmd(p_hwfn, p_ptt, true, true,
397 qm_info->start_pq, qm_info->num_pqs);
398 spin_unlock_bh(&qm_lock);
405 int qed_resc_alloc(struct qed_dev *cdev)
407 struct qed_consq *p_consq;
414 cdev->fw_data = kzalloc(sizeof(*cdev->fw_data), GFP_KERNEL);
418 /* Allocate Memory for the Queue->CID mapping */
419 for_each_hwfn(cdev, i) {
420 struct qed_hwfn *p_hwfn = &cdev->hwfns[i];
421 int tx_size = sizeof(struct qed_hw_cid_data) *
422 RESC_NUM(p_hwfn, QED_L2_QUEUE);
423 int rx_size = sizeof(struct qed_hw_cid_data) *
424 RESC_NUM(p_hwfn, QED_L2_QUEUE);
426 p_hwfn->p_tx_cids = kzalloc(tx_size, GFP_KERNEL);
427 if (!p_hwfn->p_tx_cids) {
429 "Failed to allocate memory for Tx Cids\n");
433 p_hwfn->p_rx_cids = kzalloc(rx_size, GFP_KERNEL);
434 if (!p_hwfn->p_rx_cids) {
436 "Failed to allocate memory for Rx Cids\n");
441 for_each_hwfn(cdev, i) {
442 struct qed_hwfn *p_hwfn = &cdev->hwfns[i];
443 u32 n_eqes, num_cons;
445 /* First allocate the context manager structure */
446 rc = qed_cxt_mngr_alloc(p_hwfn);
450 /* Set the HW cid/tid numbers (in the contest manager)
451 * Must be done prior to any further computations.
453 rc = qed_cxt_set_pf_params(p_hwfn);
457 /* Prepare and process QM requirements */
458 rc = qed_init_qm_info(p_hwfn, true);
462 /* Compute the ILT client partition */
463 rc = qed_cxt_cfg_ilt_compute(p_hwfn);
467 /* CID map / ILT shadow table / T2
468 * The talbes sizes are determined by the computations above
470 rc = qed_cxt_tables_alloc(p_hwfn);
474 /* SPQ, must follow ILT because initializes SPQ context */
475 rc = qed_spq_alloc(p_hwfn);
479 /* SP status block allocation */
480 p_hwfn->p_dpc_ptt = qed_get_reserved_ptt(p_hwfn,
483 rc = qed_int_alloc(p_hwfn, p_hwfn->p_main_ptt);
487 rc = qed_iov_alloc(p_hwfn);
492 n_eqes = qed_chain_get_capacity(&p_hwfn->p_spq->chain);
493 if (p_hwfn->hw_info.personality == QED_PCI_ETH_ROCE) {
494 num_cons = qed_cxt_get_proto_cid_count(p_hwfn,
497 n_eqes += num_cons + 2 * MAX_NUM_VFS_BB;
498 } else if (p_hwfn->hw_info.personality == QED_PCI_ISCSI) {
500 qed_cxt_get_proto_cid_count(p_hwfn,
501 PROTOCOLID_ISCSI, 0);
502 n_eqes += 2 * num_cons;
505 if (n_eqes > 0xFFFF) {
507 "Cannot allocate 0x%x EQ elements. The maximum of a u16 chain is 0x%x\n",
513 p_eq = qed_eq_alloc(p_hwfn, (u16) n_eqes);
518 p_consq = qed_consq_alloc(p_hwfn);
521 p_hwfn->p_consq = p_consq;
523 /* DMA info initialization */
524 rc = qed_dmae_info_alloc(p_hwfn);
527 "Failed to allocate memory for dmae_info structure\n");
531 /* DCBX initialization */
532 rc = qed_dcbx_info_alloc(p_hwfn);
535 "Failed to allocate memory for dcbx structure\n");
540 cdev->reset_stats = kzalloc(sizeof(*cdev->reset_stats), GFP_KERNEL);
541 if (!cdev->reset_stats) {
542 DP_NOTICE(cdev, "Failed to allocate reset statistics\n");
555 void qed_resc_setup(struct qed_dev *cdev)
562 for_each_hwfn(cdev, i) {
563 struct qed_hwfn *p_hwfn = &cdev->hwfns[i];
565 qed_cxt_mngr_setup(p_hwfn);
566 qed_spq_setup(p_hwfn);
567 qed_eq_setup(p_hwfn, p_hwfn->p_eq);
568 qed_consq_setup(p_hwfn, p_hwfn->p_consq);
570 /* Read shadow of current MFW mailbox */
571 qed_mcp_read_mb(p_hwfn, p_hwfn->p_main_ptt);
572 memcpy(p_hwfn->mcp_info->mfw_mb_shadow,
573 p_hwfn->mcp_info->mfw_mb_cur,
574 p_hwfn->mcp_info->mfw_mb_length);
576 qed_int_setup(p_hwfn, p_hwfn->p_main_ptt);
578 qed_iov_setup(p_hwfn, p_hwfn->p_main_ptt);
582 #define FINAL_CLEANUP_POLL_CNT (100)
583 #define FINAL_CLEANUP_POLL_TIME (10)
584 int qed_final_cleanup(struct qed_hwfn *p_hwfn,
585 struct qed_ptt *p_ptt, u16 id, bool is_vf)
587 u32 command = 0, addr, count = FINAL_CLEANUP_POLL_CNT;
590 addr = GTT_BAR0_MAP_REG_USDM_RAM +
591 USTORM_FLR_FINAL_ACK_OFFSET(p_hwfn->rel_pf_id);
596 command |= X_FINAL_CLEANUP_AGG_INT <<
597 SDM_AGG_INT_COMP_PARAMS_AGG_INT_INDEX_SHIFT;
598 command |= 1 << SDM_AGG_INT_COMP_PARAMS_AGG_VECTOR_ENABLE_SHIFT;
599 command |= id << SDM_AGG_INT_COMP_PARAMS_AGG_VECTOR_BIT_SHIFT;
600 command |= SDM_COMP_TYPE_AGG_INT << SDM_OP_GEN_COMP_TYPE_SHIFT;
602 /* Make sure notification is not set before initiating final cleanup */
603 if (REG_RD(p_hwfn, addr)) {
605 "Unexpected; Found final cleanup notification before initiating final cleanup\n");
606 REG_WR(p_hwfn, addr, 0);
609 DP_VERBOSE(p_hwfn, QED_MSG_IOV,
610 "Sending final cleanup for PFVF[%d] [Command %08x\n]",
613 qed_wr(p_hwfn, p_ptt, XSDM_REG_OPERATION_GEN, command);
615 /* Poll until completion */
616 while (!REG_RD(p_hwfn, addr) && count--)
617 msleep(FINAL_CLEANUP_POLL_TIME);
619 if (REG_RD(p_hwfn, addr))
623 "Failed to receive FW final cleanup notification\n");
625 /* Cleanup afterwards */
626 REG_WR(p_hwfn, addr, 0);
631 static void qed_calc_hw_mode(struct qed_hwfn *p_hwfn)
635 hw_mode = (1 << MODE_BB_B0);
637 switch (p_hwfn->cdev->num_ports_in_engines) {
639 hw_mode |= 1 << MODE_PORTS_PER_ENG_1;
642 hw_mode |= 1 << MODE_PORTS_PER_ENG_2;
645 hw_mode |= 1 << MODE_PORTS_PER_ENG_4;
648 DP_NOTICE(p_hwfn, "num_ports_in_engine = %d not supported\n",
649 p_hwfn->cdev->num_ports_in_engines);
653 switch (p_hwfn->cdev->mf_mode) {
656 hw_mode |= 1 << MODE_MF_SI;
659 hw_mode |= 1 << MODE_MF_SD;
662 DP_NOTICE(p_hwfn, "Unsupported MF mode, init as DEFAULT\n");
663 hw_mode |= 1 << MODE_MF_SI;
666 hw_mode |= 1 << MODE_ASIC;
668 if (p_hwfn->cdev->num_hwfns > 1)
669 hw_mode |= 1 << MODE_100G;
671 p_hwfn->hw_info.hw_mode = hw_mode;
673 DP_VERBOSE(p_hwfn, (NETIF_MSG_PROBE | NETIF_MSG_IFUP),
674 "Configuring function for hw_mode: 0x%08x\n",
675 p_hwfn->hw_info.hw_mode);
678 /* Init run time data for all PFs on an engine. */
679 static void qed_init_cau_rt_data(struct qed_dev *cdev)
681 u32 offset = CAU_REG_SB_VAR_MEMORY_RT_OFFSET;
684 for_each_hwfn(cdev, i) {
685 struct qed_hwfn *p_hwfn = &cdev->hwfns[i];
686 struct qed_igu_info *p_igu_info;
687 struct qed_igu_block *p_block;
688 struct cau_sb_entry sb_entry;
690 p_igu_info = p_hwfn->hw_info.p_igu_info;
692 for (sb_id = 0; sb_id < QED_MAPPING_MEMORY_SIZE(cdev);
694 p_block = &p_igu_info->igu_map.igu_blocks[sb_id];
698 qed_init_cau_sb_entry(p_hwfn, &sb_entry,
699 p_block->function_id, 0, 0);
700 STORE_RT_REG_AGG(p_hwfn, offset + sb_id * 2, sb_entry);
705 static int qed_hw_init_common(struct qed_hwfn *p_hwfn,
706 struct qed_ptt *p_ptt, int hw_mode)
708 struct qed_qm_info *qm_info = &p_hwfn->qm_info;
709 struct qed_qm_common_rt_init_params params;
710 struct qed_dev *cdev = p_hwfn->cdev;
716 qed_init_cau_rt_data(cdev);
718 /* Program GTT windows */
719 qed_gtt_init(p_hwfn);
721 if (p_hwfn->mcp_info) {
722 if (p_hwfn->mcp_info->func_info.bandwidth_max)
723 qm_info->pf_rl_en = 1;
724 if (p_hwfn->mcp_info->func_info.bandwidth_min)
725 qm_info->pf_wfq_en = 1;
728 memset(¶ms, 0, sizeof(params));
729 params.max_ports_per_engine = p_hwfn->cdev->num_ports_in_engines;
730 params.max_phys_tcs_per_port = qm_info->max_phys_tcs_per_port;
731 params.pf_rl_en = qm_info->pf_rl_en;
732 params.pf_wfq_en = qm_info->pf_wfq_en;
733 params.vport_rl_en = qm_info->vport_rl_en;
734 params.vport_wfq_en = qm_info->vport_wfq_en;
735 params.port_params = qm_info->qm_port_params;
737 qed_qm_common_rt_init(p_hwfn, ¶ms);
739 qed_cxt_hw_init_common(p_hwfn);
741 /* Close gate from NIG to BRB/Storm; By default they are open, but
742 * we close them to prevent NIG from passing data to reset blocks.
743 * Should have been done in the ENGINE phase, but init-tool lacks
744 * proper port-pretend capabilities.
746 qed_wr(p_hwfn, p_ptt, NIG_REG_RX_BRB_OUT_EN, 0);
747 qed_wr(p_hwfn, p_ptt, NIG_REG_STORM_OUT_EN, 0);
748 qed_port_pretend(p_hwfn, p_ptt, p_hwfn->port_id ^ 1);
749 qed_wr(p_hwfn, p_ptt, NIG_REG_RX_BRB_OUT_EN, 0);
750 qed_wr(p_hwfn, p_ptt, NIG_REG_STORM_OUT_EN, 0);
751 qed_port_unpretend(p_hwfn, p_ptt);
753 rc = qed_init_run(p_hwfn, p_ptt, PHASE_ENGINE, ANY_PHASE_ID, hw_mode);
757 qed_wr(p_hwfn, p_ptt, PSWRQ2_REG_L2P_VALIDATE_VFID, 0);
758 qed_wr(p_hwfn, p_ptt, PGLUE_B_REG_USE_CLIENTID_IN_TAG, 1);
760 if (QED_IS_BB(p_hwfn->cdev)) {
761 num_pfs = NUM_OF_ENG_PFS(p_hwfn->cdev);
762 for (pf_id = 0; pf_id < num_pfs; pf_id++) {
763 qed_fid_pretend(p_hwfn, p_ptt, pf_id);
764 qed_wr(p_hwfn, p_ptt, PRS_REG_SEARCH_ROCE, 0x0);
765 qed_wr(p_hwfn, p_ptt, PRS_REG_SEARCH_TCP, 0x0);
767 /* pretend to original PF */
768 qed_fid_pretend(p_hwfn, p_ptt, p_hwfn->rel_pf_id);
771 for (vf_id = 0; vf_id < MAX_NUM_VFS_BB; vf_id++) {
772 concrete_fid = qed_vfid_to_concrete(p_hwfn, vf_id);
773 qed_fid_pretend(p_hwfn, p_ptt, (u16) concrete_fid);
774 qed_wr(p_hwfn, p_ptt, CCFC_REG_STRONG_ENABLE_VF, 0x1);
776 /* pretend to original PF */
777 qed_fid_pretend(p_hwfn, p_ptt, p_hwfn->rel_pf_id);
782 static int qed_hw_init_port(struct qed_hwfn *p_hwfn,
783 struct qed_ptt *p_ptt, int hw_mode)
787 rc = qed_init_run(p_hwfn, p_ptt, PHASE_PORT, p_hwfn->port_id, hw_mode);
791 if (hw_mode & (1 << MODE_MF_SI)) {
794 if (!qed_hw_init_first_eth(p_hwfn, p_ptt, &pf_id)) {
795 DP_VERBOSE(p_hwfn, NETIF_MSG_IFUP,
796 "PF[%08x] is first eth on engine\n", pf_id);
798 /* We should have configured BIT for ppfid, i.e., the
799 * relative function number in the port. But there's a
800 * bug in LLH in BB where the ppfid is actually engine
801 * based, so we need to take this into account.
803 qed_wr(p_hwfn, p_ptt,
804 NIG_REG_LLH_TAGMAC_DEF_PF_VECTOR, 1 << pf_id);
807 /* Take the protocol-based hit vector if there is a hit,
808 * otherwise take the other vector.
810 qed_wr(p_hwfn, p_ptt, NIG_REG_LLH_CLS_TYPE_DUALMODE, 0x2);
815 static int qed_hw_init_pf(struct qed_hwfn *p_hwfn,
816 struct qed_ptt *p_ptt,
817 struct qed_tunn_start_params *p_tunn,
820 enum qed_int_mode int_mode,
821 bool allow_npar_tx_switch)
823 u8 rel_pf_id = p_hwfn->rel_pf_id;
826 if (p_hwfn->mcp_info) {
827 struct qed_mcp_function_info *p_info;
829 p_info = &p_hwfn->mcp_info->func_info;
830 if (p_info->bandwidth_min)
831 p_hwfn->qm_info.pf_wfq = p_info->bandwidth_min;
833 /* Update rate limit once we'll actually have a link */
834 p_hwfn->qm_info.pf_rl = 100000;
837 qed_cxt_hw_init_pf(p_hwfn);
839 qed_int_igu_init_rt(p_hwfn);
841 /* Set VLAN in NIG if needed */
842 if (hw_mode & BIT(MODE_MF_SD)) {
843 DP_VERBOSE(p_hwfn, NETIF_MSG_HW, "Configuring LLH_FUNC_TAG\n");
844 STORE_RT_REG(p_hwfn, NIG_REG_LLH_FUNC_TAG_EN_RT_OFFSET, 1);
845 STORE_RT_REG(p_hwfn, NIG_REG_LLH_FUNC_TAG_VALUE_RT_OFFSET,
846 p_hwfn->hw_info.ovlan);
849 /* Enable classification by MAC if needed */
850 if (hw_mode & BIT(MODE_MF_SI)) {
851 DP_VERBOSE(p_hwfn, NETIF_MSG_HW,
852 "Configuring TAGMAC_CLS_TYPE\n");
854 NIG_REG_LLH_FUNC_TAGMAC_CLS_TYPE_RT_OFFSET, 1);
857 /* Protocl Configuration */
858 STORE_RT_REG(p_hwfn, PRS_REG_SEARCH_TCP_RT_OFFSET,
859 (p_hwfn->hw_info.personality == QED_PCI_ISCSI) ? 1 : 0);
860 STORE_RT_REG(p_hwfn, PRS_REG_SEARCH_FCOE_RT_OFFSET, 0);
861 STORE_RT_REG(p_hwfn, PRS_REG_SEARCH_ROCE_RT_OFFSET, 0);
863 /* Cleanup chip from previous driver if such remains exist */
864 rc = qed_final_cleanup(p_hwfn, p_ptt, rel_pf_id, false);
868 /* PF Init sequence */
869 rc = qed_init_run(p_hwfn, p_ptt, PHASE_PF, rel_pf_id, hw_mode);
873 /* QM_PF Init sequence (may be invoked separately e.g. for DCB) */
874 rc = qed_init_run(p_hwfn, p_ptt, PHASE_QM_PF, rel_pf_id, hw_mode);
878 /* Pure runtime initializations - directly to the HW */
879 qed_int_igu_init_pure_rt(p_hwfn, p_ptt, true, true);
881 if (hw_mode & (1 << MODE_MF_SI)) {
885 if (!qed_hw_init_first_eth(p_hwfn, p_ptt, &pf_id)) {
886 if (p_hwfn->rel_pf_id == pf_id) {
887 DP_VERBOSE(p_hwfn, NETIF_MSG_IFUP,
888 "PF[%d] is first ETH on engine\n",
892 qed_wr(p_hwfn, p_ptt, PRS_REG_MSG_INFO, val);
897 /* enable interrupts */
898 qed_int_igu_enable(p_hwfn, p_ptt, int_mode);
900 /* send function start command */
901 rc = qed_sp_pf_start(p_hwfn, p_tunn, p_hwfn->cdev->mf_mode,
902 allow_npar_tx_switch);
904 DP_NOTICE(p_hwfn, "Function start ramrod failed\n");
909 static int qed_change_pci_hwfn(struct qed_hwfn *p_hwfn,
910 struct qed_ptt *p_ptt,
913 u32 delay_idx = 0, val, set_val = enable ? 1 : 0;
915 /* Change PF in PXP */
916 qed_wr(p_hwfn, p_ptt,
917 PGLUE_B_REG_INTERNAL_PFID_ENABLE_MASTER, set_val);
919 /* wait until value is set - try for 1 second every 50us */
920 for (delay_idx = 0; delay_idx < 20000; delay_idx++) {
921 val = qed_rd(p_hwfn, p_ptt,
922 PGLUE_B_REG_INTERNAL_PFID_ENABLE_MASTER);
926 usleep_range(50, 60);
929 if (val != set_val) {
931 "PFID_ENABLE_MASTER wasn't changed after a second\n");
938 static void qed_reset_mb_shadow(struct qed_hwfn *p_hwfn,
939 struct qed_ptt *p_main_ptt)
941 /* Read shadow of current MFW mailbox */
942 qed_mcp_read_mb(p_hwfn, p_main_ptt);
943 memcpy(p_hwfn->mcp_info->mfw_mb_shadow,
944 p_hwfn->mcp_info->mfw_mb_cur, p_hwfn->mcp_info->mfw_mb_length);
947 int qed_hw_init(struct qed_dev *cdev,
948 struct qed_tunn_start_params *p_tunn,
950 enum qed_int_mode int_mode,
951 bool allow_npar_tx_switch,
952 const u8 *bin_fw_data)
954 u32 load_code, param;
957 if ((int_mode == QED_INT_MODE_MSI) && (cdev->num_hwfns > 1)) {
958 DP_NOTICE(cdev, "MSI mode is not supported for CMT devices\n");
963 rc = qed_init_fw_data(cdev, bin_fw_data);
968 for_each_hwfn(cdev, i) {
969 struct qed_hwfn *p_hwfn = &cdev->hwfns[i];
972 p_hwfn->b_int_enabled = 1;
976 /* Enable DMAE in PXP */
977 rc = qed_change_pci_hwfn(p_hwfn, p_hwfn->p_main_ptt, true);
979 qed_calc_hw_mode(p_hwfn);
981 rc = qed_mcp_load_req(p_hwfn, p_hwfn->p_main_ptt, &load_code);
983 DP_NOTICE(p_hwfn, "Failed sending LOAD_REQ command\n");
987 qed_reset_mb_shadow(p_hwfn, p_hwfn->p_main_ptt);
989 DP_VERBOSE(p_hwfn, QED_MSG_SP,
990 "Load request was sent. Resp:0x%x, Load code: 0x%x\n",
993 p_hwfn->first_on_engine = (load_code ==
994 FW_MSG_CODE_DRV_LOAD_ENGINE);
997 case FW_MSG_CODE_DRV_LOAD_ENGINE:
998 rc = qed_hw_init_common(p_hwfn, p_hwfn->p_main_ptt,
999 p_hwfn->hw_info.hw_mode);
1003 case FW_MSG_CODE_DRV_LOAD_PORT:
1004 rc = qed_hw_init_port(p_hwfn, p_hwfn->p_main_ptt,
1005 p_hwfn->hw_info.hw_mode);
1010 case FW_MSG_CODE_DRV_LOAD_FUNCTION:
1011 rc = qed_hw_init_pf(p_hwfn, p_hwfn->p_main_ptt,
1012 p_tunn, p_hwfn->hw_info.hw_mode,
1013 b_hw_start, int_mode,
1014 allow_npar_tx_switch);
1023 "init phase failed for loadcode 0x%x (rc %d)\n",
1026 /* ACK mfw regardless of success or failure of initialization */
1027 mfw_rc = qed_mcp_cmd(p_hwfn, p_hwfn->p_main_ptt,
1028 DRV_MSG_CODE_LOAD_DONE,
1029 0, &load_code, ¶m);
1033 DP_NOTICE(p_hwfn, "Failed sending LOAD_DONE command\n");
1037 /* send DCBX attention request command */
1040 "sending phony dcbx set command to trigger DCBx attention handling\n");
1041 mfw_rc = qed_mcp_cmd(p_hwfn, p_hwfn->p_main_ptt,
1042 DRV_MSG_CODE_SET_DCBX,
1043 1 << DRV_MB_PARAM_DCBX_NOTIFY_SHIFT,
1044 &load_code, ¶m);
1047 "Failed to send DCBX attention request\n");
1051 p_hwfn->hw_init_done = true;
1057 #define QED_HW_STOP_RETRY_LIMIT (10)
1058 static void qed_hw_timers_stop(struct qed_dev *cdev,
1059 struct qed_hwfn *p_hwfn, struct qed_ptt *p_ptt)
1064 qed_wr(p_hwfn, p_ptt, TM_REG_PF_ENABLE_CONN, 0x0);
1065 qed_wr(p_hwfn, p_ptt, TM_REG_PF_ENABLE_TASK, 0x0);
1067 for (i = 0; i < QED_HW_STOP_RETRY_LIMIT; i++) {
1068 if ((!qed_rd(p_hwfn, p_ptt,
1069 TM_REG_PF_SCAN_ACTIVE_CONN)) &&
1070 (!qed_rd(p_hwfn, p_ptt, TM_REG_PF_SCAN_ACTIVE_TASK)))
1073 /* Dependent on number of connection/tasks, possibly
1074 * 1ms sleep is required between polls
1076 usleep_range(1000, 2000);
1079 if (i < QED_HW_STOP_RETRY_LIMIT)
1083 "Timers linear scans are not over [Connection %02x Tasks %02x]\n",
1084 (u8)qed_rd(p_hwfn, p_ptt, TM_REG_PF_SCAN_ACTIVE_CONN),
1085 (u8)qed_rd(p_hwfn, p_ptt, TM_REG_PF_SCAN_ACTIVE_TASK));
1088 void qed_hw_timers_stop_all(struct qed_dev *cdev)
1092 for_each_hwfn(cdev, j) {
1093 struct qed_hwfn *p_hwfn = &cdev->hwfns[j];
1094 struct qed_ptt *p_ptt = p_hwfn->p_main_ptt;
1096 qed_hw_timers_stop(cdev, p_hwfn, p_ptt);
1100 int qed_hw_stop(struct qed_dev *cdev)
1105 for_each_hwfn(cdev, j) {
1106 struct qed_hwfn *p_hwfn = &cdev->hwfns[j];
1107 struct qed_ptt *p_ptt = p_hwfn->p_main_ptt;
1109 DP_VERBOSE(p_hwfn, NETIF_MSG_IFDOWN, "Stopping hw/fw\n");
1112 qed_vf_pf_int_cleanup(p_hwfn);
1116 /* mark the hw as uninitialized... */
1117 p_hwfn->hw_init_done = false;
1119 rc = qed_sp_pf_stop(p_hwfn);
1122 "Failed to close PF against FW. Continue to stop HW to prevent illegal host access by the device\n");
1124 qed_wr(p_hwfn, p_ptt,
1125 NIG_REG_RX_LLH_BRB_GATE_DNTFWD_PERPF, 0x1);
1127 qed_wr(p_hwfn, p_ptt, PRS_REG_SEARCH_TCP, 0x0);
1128 qed_wr(p_hwfn, p_ptt, PRS_REG_SEARCH_UDP, 0x0);
1129 qed_wr(p_hwfn, p_ptt, PRS_REG_SEARCH_FCOE, 0x0);
1130 qed_wr(p_hwfn, p_ptt, PRS_REG_SEARCH_ROCE, 0x0);
1131 qed_wr(p_hwfn, p_ptt, PRS_REG_SEARCH_OPENFLOW, 0x0);
1133 qed_hw_timers_stop(cdev, p_hwfn, p_ptt);
1135 /* Disable Attention Generation */
1136 qed_int_igu_disable_int(p_hwfn, p_ptt);
1138 qed_wr(p_hwfn, p_ptt, IGU_REG_LEADING_EDGE_LATCH, 0);
1139 qed_wr(p_hwfn, p_ptt, IGU_REG_TRAILING_EDGE_LATCH, 0);
1141 qed_int_igu_init_pure_rt(p_hwfn, p_ptt, false, true);
1143 /* Need to wait 1ms to guarantee SBs are cleared */
1144 usleep_range(1000, 2000);
1148 /* Disable DMAE in PXP - in CMT, this should only be done for
1149 * first hw-function, and only after all transactions have
1150 * stopped for all active hw-functions.
1152 t_rc = qed_change_pci_hwfn(&cdev->hwfns[0],
1153 cdev->hwfns[0].p_main_ptt, false);
1161 void qed_hw_stop_fastpath(struct qed_dev *cdev)
1165 for_each_hwfn(cdev, j) {
1166 struct qed_hwfn *p_hwfn = &cdev->hwfns[j];
1167 struct qed_ptt *p_ptt = p_hwfn->p_main_ptt;
1170 qed_vf_pf_int_cleanup(p_hwfn);
1175 NETIF_MSG_IFDOWN, "Shutting down the fastpath\n");
1177 qed_wr(p_hwfn, p_ptt,
1178 NIG_REG_RX_LLH_BRB_GATE_DNTFWD_PERPF, 0x1);
1180 qed_wr(p_hwfn, p_ptt, PRS_REG_SEARCH_TCP, 0x0);
1181 qed_wr(p_hwfn, p_ptt, PRS_REG_SEARCH_UDP, 0x0);
1182 qed_wr(p_hwfn, p_ptt, PRS_REG_SEARCH_FCOE, 0x0);
1183 qed_wr(p_hwfn, p_ptt, PRS_REG_SEARCH_ROCE, 0x0);
1184 qed_wr(p_hwfn, p_ptt, PRS_REG_SEARCH_OPENFLOW, 0x0);
1186 qed_int_igu_init_pure_rt(p_hwfn, p_ptt, false, false);
1188 /* Need to wait 1ms to guarantee SBs are cleared */
1189 usleep_range(1000, 2000);
1193 void qed_hw_start_fastpath(struct qed_hwfn *p_hwfn)
1195 if (IS_VF(p_hwfn->cdev))
1198 /* Re-open incoming traffic */
1199 qed_wr(p_hwfn, p_hwfn->p_main_ptt,
1200 NIG_REG_RX_LLH_BRB_GATE_DNTFWD_PERPF, 0x0);
1203 static int qed_reg_assert(struct qed_hwfn *p_hwfn,
1204 struct qed_ptt *p_ptt, u32 reg, bool expected)
1206 u32 assert_val = qed_rd(p_hwfn, p_ptt, reg);
1208 if (assert_val != expected) {
1209 DP_NOTICE(p_hwfn, "Value at address 0x%08x != 0x%08x\n",
1217 int qed_hw_reset(struct qed_dev *cdev)
1220 u32 unload_resp, unload_param;
1223 for_each_hwfn(cdev, i) {
1224 struct qed_hwfn *p_hwfn = &cdev->hwfns[i];
1227 rc = qed_vf_pf_reset(p_hwfn);
1233 DP_VERBOSE(p_hwfn, NETIF_MSG_IFDOWN, "Resetting hw/fw\n");
1235 /* Check for incorrect states */
1236 qed_reg_assert(p_hwfn, p_hwfn->p_main_ptt,
1237 QM_REG_USG_CNT_PF_TX, 0);
1238 qed_reg_assert(p_hwfn, p_hwfn->p_main_ptt,
1239 QM_REG_USG_CNT_PF_OTHER, 0);
1241 /* Disable PF in HW blocks */
1242 qed_wr(p_hwfn, p_hwfn->p_main_ptt, DORQ_REG_PF_DB_ENABLE, 0);
1243 qed_wr(p_hwfn, p_hwfn->p_main_ptt, QM_REG_PF_EN, 0);
1244 qed_wr(p_hwfn, p_hwfn->p_main_ptt,
1245 TCFC_REG_STRONG_ENABLE_PF, 0);
1246 qed_wr(p_hwfn, p_hwfn->p_main_ptt,
1247 CCFC_REG_STRONG_ENABLE_PF, 0);
1249 /* Send unload command to MCP */
1250 rc = qed_mcp_cmd(p_hwfn, p_hwfn->p_main_ptt,
1251 DRV_MSG_CODE_UNLOAD_REQ,
1252 DRV_MB_PARAM_UNLOAD_WOL_MCP,
1253 &unload_resp, &unload_param);
1255 DP_NOTICE(p_hwfn, "qed_hw_reset: UNLOAD_REQ failed\n");
1256 unload_resp = FW_MSG_CODE_DRV_UNLOAD_ENGINE;
1259 rc = qed_mcp_cmd(p_hwfn, p_hwfn->p_main_ptt,
1260 DRV_MSG_CODE_UNLOAD_DONE,
1261 0, &unload_resp, &unload_param);
1263 DP_NOTICE(p_hwfn, "qed_hw_reset: UNLOAD_DONE failed\n");
1271 /* Free hwfn memory and resources acquired in hw_hwfn_prepare */
1272 static void qed_hw_hwfn_free(struct qed_hwfn *p_hwfn)
1274 qed_ptt_pool_free(p_hwfn);
1275 kfree(p_hwfn->hw_info.p_igu_info);
1278 /* Setup bar access */
1279 static void qed_hw_hwfn_prepare(struct qed_hwfn *p_hwfn)
1281 /* clear indirect access */
1282 qed_wr(p_hwfn, p_hwfn->p_main_ptt, PGLUE_B_REG_PGL_ADDR_88_F0, 0);
1283 qed_wr(p_hwfn, p_hwfn->p_main_ptt, PGLUE_B_REG_PGL_ADDR_8C_F0, 0);
1284 qed_wr(p_hwfn, p_hwfn->p_main_ptt, PGLUE_B_REG_PGL_ADDR_90_F0, 0);
1285 qed_wr(p_hwfn, p_hwfn->p_main_ptt, PGLUE_B_REG_PGL_ADDR_94_F0, 0);
1287 /* Clean Previous errors if such exist */
1288 qed_wr(p_hwfn, p_hwfn->p_main_ptt,
1289 PGLUE_B_REG_WAS_ERROR_PF_31_0_CLR, 1 << p_hwfn->abs_pf_id);
1291 /* enable internal target-read */
1292 qed_wr(p_hwfn, p_hwfn->p_main_ptt,
1293 PGLUE_B_REG_INTERNAL_PFID_ENABLE_TARGET_READ, 1);
1296 static void get_function_id(struct qed_hwfn *p_hwfn)
1299 p_hwfn->hw_info.opaque_fid = (u16) REG_RD(p_hwfn,
1300 PXP_PF_ME_OPAQUE_ADDR);
1302 p_hwfn->hw_info.concrete_fid = REG_RD(p_hwfn, PXP_PF_ME_CONCRETE_ADDR);
1304 p_hwfn->abs_pf_id = (p_hwfn->hw_info.concrete_fid >> 16) & 0xf;
1305 p_hwfn->rel_pf_id = GET_FIELD(p_hwfn->hw_info.concrete_fid,
1306 PXP_CONCRETE_FID_PFID);
1307 p_hwfn->port_id = GET_FIELD(p_hwfn->hw_info.concrete_fid,
1308 PXP_CONCRETE_FID_PORT);
1310 DP_VERBOSE(p_hwfn, NETIF_MSG_PROBE,
1311 "Read ME register: Concrete 0x%08x Opaque 0x%04x\n",
1312 p_hwfn->hw_info.concrete_fid, p_hwfn->hw_info.opaque_fid);
1315 static void qed_hw_set_feat(struct qed_hwfn *p_hwfn)
1317 u32 *feat_num = p_hwfn->hw_info.feat_num;
1318 int num_features = 1;
1320 feat_num[QED_PF_L2_QUE] = min_t(u32, RESC_NUM(p_hwfn, QED_SB) /
1322 RESC_NUM(p_hwfn, QED_L2_QUEUE));
1323 DP_VERBOSE(p_hwfn, NETIF_MSG_PROBE,
1324 "#PF_L2_QUEUES=%d #SBS=%d num_features=%d\n",
1325 feat_num[QED_PF_L2_QUE], RESC_NUM(p_hwfn, QED_SB),
1329 static int qed_hw_get_resc(struct qed_hwfn *p_hwfn)
1331 u8 enabled_func_idx = p_hwfn->enabled_func_idx;
1332 u32 *resc_start = p_hwfn->hw_info.resc_start;
1333 u8 num_funcs = p_hwfn->num_funcs_on_engine;
1334 u32 *resc_num = p_hwfn->hw_info.resc_num;
1335 struct qed_sb_cnt_info sb_cnt_info;
1336 int i, max_vf_vlan_filters;
1338 memset(&sb_cnt_info, 0, sizeof(sb_cnt_info));
1340 #ifdef CONFIG_QED_SRIOV
1341 max_vf_vlan_filters = QED_ETH_MAX_VF_NUM_VLAN_FILTERS;
1343 max_vf_vlan_filters = 0;
1346 qed_int_get_num_sbs(p_hwfn, &sb_cnt_info);
1348 resc_num[QED_SB] = min_t(u32,
1349 (MAX_SB_PER_PATH_BB / num_funcs),
1350 sb_cnt_info.sb_cnt);
1351 resc_num[QED_L2_QUEUE] = MAX_NUM_L2_QUEUES_BB / num_funcs;
1352 resc_num[QED_VPORT] = MAX_NUM_VPORTS_BB / num_funcs;
1353 resc_num[QED_RSS_ENG] = ETH_RSS_ENGINE_NUM_BB / num_funcs;
1354 resc_num[QED_PQ] = MAX_QM_TX_QUEUES_BB / num_funcs;
1355 resc_num[QED_RL] = min_t(u32, 64, resc_num[QED_VPORT]);
1356 resc_num[QED_MAC] = ETH_NUM_MAC_FILTERS / num_funcs;
1357 resc_num[QED_VLAN] = (ETH_NUM_VLAN_FILTERS - 1 /*For vlan0*/) /
1359 resc_num[QED_ILT] = PXP_NUM_ILT_RECORDS_BB / num_funcs;
1361 for (i = 0; i < QED_MAX_RESC; i++)
1362 resc_start[i] = resc_num[i] * enabled_func_idx;
1364 /* Sanity for ILT */
1365 if (RESC_END(p_hwfn, QED_ILT) > PXP_NUM_ILT_RECORDS_BB) {
1366 DP_NOTICE(p_hwfn, "Can't assign ILT pages [%08x,...,%08x]\n",
1367 RESC_START(p_hwfn, QED_ILT),
1368 RESC_END(p_hwfn, QED_ILT) - 1);
1372 qed_hw_set_feat(p_hwfn);
1374 DP_VERBOSE(p_hwfn, NETIF_MSG_PROBE,
1375 "The numbers for each resource are:\n"
1376 "SB = %d start = %d\n"
1377 "L2_QUEUE = %d start = %d\n"
1378 "VPORT = %d start = %d\n"
1379 "PQ = %d start = %d\n"
1380 "RL = %d start = %d\n"
1381 "MAC = %d start = %d\n"
1382 "VLAN = %d start = %d\n"
1383 "ILT = %d start = %d\n",
1384 p_hwfn->hw_info.resc_num[QED_SB],
1385 p_hwfn->hw_info.resc_start[QED_SB],
1386 p_hwfn->hw_info.resc_num[QED_L2_QUEUE],
1387 p_hwfn->hw_info.resc_start[QED_L2_QUEUE],
1388 p_hwfn->hw_info.resc_num[QED_VPORT],
1389 p_hwfn->hw_info.resc_start[QED_VPORT],
1390 p_hwfn->hw_info.resc_num[QED_PQ],
1391 p_hwfn->hw_info.resc_start[QED_PQ],
1392 p_hwfn->hw_info.resc_num[QED_RL],
1393 p_hwfn->hw_info.resc_start[QED_RL],
1394 p_hwfn->hw_info.resc_num[QED_MAC],
1395 p_hwfn->hw_info.resc_start[QED_MAC],
1396 p_hwfn->hw_info.resc_num[QED_VLAN],
1397 p_hwfn->hw_info.resc_start[QED_VLAN],
1398 p_hwfn->hw_info.resc_num[QED_ILT],
1399 p_hwfn->hw_info.resc_start[QED_ILT]);
1404 static int qed_hw_get_nvm_info(struct qed_hwfn *p_hwfn, struct qed_ptt *p_ptt)
1406 u32 nvm_cfg1_offset, mf_mode, addr, generic_cont0, core_cfg;
1407 u32 port_cfg_addr, link_temp, nvm_cfg_addr, device_capabilities;
1408 struct qed_mcp_link_params *link;
1410 /* Read global nvm_cfg address */
1411 nvm_cfg_addr = qed_rd(p_hwfn, p_ptt, MISC_REG_GEN_PURP_CR0);
1413 /* Verify MCP has initialized it */
1414 if (!nvm_cfg_addr) {
1415 DP_NOTICE(p_hwfn, "Shared memory not initialized\n");
1419 /* Read nvm_cfg1 (Notice this is just offset, and not offsize (TBD) */
1420 nvm_cfg1_offset = qed_rd(p_hwfn, p_ptt, nvm_cfg_addr + 4);
1422 addr = MCP_REG_SCRATCH + nvm_cfg1_offset +
1423 offsetof(struct nvm_cfg1, glob) +
1424 offsetof(struct nvm_cfg1_glob, core_cfg);
1426 core_cfg = qed_rd(p_hwfn, p_ptt, addr);
1428 switch ((core_cfg & NVM_CFG1_GLOB_NETWORK_PORT_MODE_MASK) >>
1429 NVM_CFG1_GLOB_NETWORK_PORT_MODE_OFFSET) {
1430 case NVM_CFG1_GLOB_NETWORK_PORT_MODE_BB_2X40G:
1431 p_hwfn->hw_info.port_mode = QED_PORT_MODE_DE_2X40G;
1433 case NVM_CFG1_GLOB_NETWORK_PORT_MODE_2X50G:
1434 p_hwfn->hw_info.port_mode = QED_PORT_MODE_DE_2X50G;
1436 case NVM_CFG1_GLOB_NETWORK_PORT_MODE_BB_1X100G:
1437 p_hwfn->hw_info.port_mode = QED_PORT_MODE_DE_1X100G;
1439 case NVM_CFG1_GLOB_NETWORK_PORT_MODE_4X10G_F:
1440 p_hwfn->hw_info.port_mode = QED_PORT_MODE_DE_4X10G_F;
1442 case NVM_CFG1_GLOB_NETWORK_PORT_MODE_BB_4X10G_E:
1443 p_hwfn->hw_info.port_mode = QED_PORT_MODE_DE_4X10G_E;
1445 case NVM_CFG1_GLOB_NETWORK_PORT_MODE_BB_4X20G:
1446 p_hwfn->hw_info.port_mode = QED_PORT_MODE_DE_4X20G;
1448 case NVM_CFG1_GLOB_NETWORK_PORT_MODE_1X40G:
1449 p_hwfn->hw_info.port_mode = QED_PORT_MODE_DE_1X40G;
1451 case NVM_CFG1_GLOB_NETWORK_PORT_MODE_2X25G:
1452 p_hwfn->hw_info.port_mode = QED_PORT_MODE_DE_2X25G;
1454 case NVM_CFG1_GLOB_NETWORK_PORT_MODE_1X25G:
1455 p_hwfn->hw_info.port_mode = QED_PORT_MODE_DE_1X25G;
1458 DP_NOTICE(p_hwfn, "Unknown port mode in 0x%08x\n", core_cfg);
1462 /* Read default link configuration */
1463 link = &p_hwfn->mcp_info->link_input;
1464 port_cfg_addr = MCP_REG_SCRATCH + nvm_cfg1_offset +
1465 offsetof(struct nvm_cfg1, port[MFW_PORT(p_hwfn)]);
1466 link_temp = qed_rd(p_hwfn, p_ptt,
1468 offsetof(struct nvm_cfg1_port, speed_cap_mask));
1469 link_temp &= NVM_CFG1_PORT_DRV_SPEED_CAPABILITY_MASK_MASK;
1470 link->speed.advertised_speeds = link_temp;
1472 link_temp = link->speed.advertised_speeds;
1473 p_hwfn->mcp_info->link_capabilities.speed_capabilities = link_temp;
1475 link_temp = qed_rd(p_hwfn, p_ptt,
1477 offsetof(struct nvm_cfg1_port, link_settings));
1478 switch ((link_temp & NVM_CFG1_PORT_DRV_LINK_SPEED_MASK) >>
1479 NVM_CFG1_PORT_DRV_LINK_SPEED_OFFSET) {
1480 case NVM_CFG1_PORT_DRV_LINK_SPEED_AUTONEG:
1481 link->speed.autoneg = true;
1483 case NVM_CFG1_PORT_DRV_LINK_SPEED_1G:
1484 link->speed.forced_speed = 1000;
1486 case NVM_CFG1_PORT_DRV_LINK_SPEED_10G:
1487 link->speed.forced_speed = 10000;
1489 case NVM_CFG1_PORT_DRV_LINK_SPEED_25G:
1490 link->speed.forced_speed = 25000;
1492 case NVM_CFG1_PORT_DRV_LINK_SPEED_40G:
1493 link->speed.forced_speed = 40000;
1495 case NVM_CFG1_PORT_DRV_LINK_SPEED_50G:
1496 link->speed.forced_speed = 50000;
1498 case NVM_CFG1_PORT_DRV_LINK_SPEED_BB_100G:
1499 link->speed.forced_speed = 100000;
1502 DP_NOTICE(p_hwfn, "Unknown Speed in 0x%08x\n", link_temp);
1505 link_temp &= NVM_CFG1_PORT_DRV_FLOW_CONTROL_MASK;
1506 link_temp >>= NVM_CFG1_PORT_DRV_FLOW_CONTROL_OFFSET;
1507 link->pause.autoneg = !!(link_temp &
1508 NVM_CFG1_PORT_DRV_FLOW_CONTROL_AUTONEG);
1509 link->pause.forced_rx = !!(link_temp &
1510 NVM_CFG1_PORT_DRV_FLOW_CONTROL_RX);
1511 link->pause.forced_tx = !!(link_temp &
1512 NVM_CFG1_PORT_DRV_FLOW_CONTROL_TX);
1513 link->loopback_mode = 0;
1515 DP_VERBOSE(p_hwfn, NETIF_MSG_LINK,
1516 "Read default link: Speed 0x%08x, Adv. Speed 0x%08x, AN: 0x%02x, PAUSE AN: 0x%02x\n",
1517 link->speed.forced_speed, link->speed.advertised_speeds,
1518 link->speed.autoneg, link->pause.autoneg);
1520 /* Read Multi-function information from shmem */
1521 addr = MCP_REG_SCRATCH + nvm_cfg1_offset +
1522 offsetof(struct nvm_cfg1, glob) +
1523 offsetof(struct nvm_cfg1_glob, generic_cont0);
1525 generic_cont0 = qed_rd(p_hwfn, p_ptt, addr);
1527 mf_mode = (generic_cont0 & NVM_CFG1_GLOB_MF_MODE_MASK) >>
1528 NVM_CFG1_GLOB_MF_MODE_OFFSET;
1531 case NVM_CFG1_GLOB_MF_MODE_MF_ALLOWED:
1532 p_hwfn->cdev->mf_mode = QED_MF_OVLAN;
1534 case NVM_CFG1_GLOB_MF_MODE_NPAR1_0:
1535 p_hwfn->cdev->mf_mode = QED_MF_NPAR;
1537 case NVM_CFG1_GLOB_MF_MODE_DEFAULT:
1538 p_hwfn->cdev->mf_mode = QED_MF_DEFAULT;
1541 DP_INFO(p_hwfn, "Multi function mode is %08x\n",
1542 p_hwfn->cdev->mf_mode);
1544 /* Read Multi-function information from shmem */
1545 addr = MCP_REG_SCRATCH + nvm_cfg1_offset +
1546 offsetof(struct nvm_cfg1, glob) +
1547 offsetof(struct nvm_cfg1_glob, device_capabilities);
1549 device_capabilities = qed_rd(p_hwfn, p_ptt, addr);
1550 if (device_capabilities & NVM_CFG1_GLOB_DEVICE_CAPABILITIES_ETHERNET)
1551 __set_bit(QED_DEV_CAP_ETH,
1552 &p_hwfn->hw_info.device_capabilities);
1553 if (device_capabilities & NVM_CFG1_GLOB_DEVICE_CAPABILITIES_ISCSI)
1554 __set_bit(QED_DEV_CAP_ISCSI,
1555 &p_hwfn->hw_info.device_capabilities);
1556 if (device_capabilities & NVM_CFG1_GLOB_DEVICE_CAPABILITIES_ROCE)
1557 __set_bit(QED_DEV_CAP_ROCE,
1558 &p_hwfn->hw_info.device_capabilities);
1560 return qed_mcp_fill_shmem_func_info(p_hwfn, p_ptt);
1563 static void qed_get_num_funcs(struct qed_hwfn *p_hwfn, struct qed_ptt *p_ptt)
1565 u8 num_funcs, enabled_func_idx = p_hwfn->rel_pf_id;
1566 u32 reg_function_hide, tmp, eng_mask, low_pfs_mask;
1568 num_funcs = MAX_NUM_PFS_BB;
1570 /* Bit 0 of MISCS_REG_FUNCTION_HIDE indicates whether the bypass values
1571 * in the other bits are selected.
1572 * Bits 1-15 are for functions 1-15, respectively, and their value is
1573 * '0' only for enabled functions (function 0 always exists and
1575 * In case of CMT, only the "even" functions are enabled, and thus the
1576 * number of functions for both hwfns is learnt from the same bits.
1578 reg_function_hide = qed_rd(p_hwfn, p_ptt, MISCS_REG_FUNCTION_HIDE);
1580 if (reg_function_hide & 0x1) {
1581 if (QED_PATH_ID(p_hwfn) && p_hwfn->cdev->num_hwfns == 1) {
1589 /* Get the number of the enabled functions on the engine */
1590 tmp = (reg_function_hide ^ 0xffffffff) & eng_mask;
1597 /* Get the PF index within the enabled functions */
1598 low_pfs_mask = (0x1 << p_hwfn->abs_pf_id) - 1;
1599 tmp = reg_function_hide & eng_mask & low_pfs_mask;
1607 p_hwfn->num_funcs_on_engine = num_funcs;
1608 p_hwfn->enabled_func_idx = enabled_func_idx;
1612 "PF [rel_id %d, abs_id %d] occupies index %d within the %d enabled functions on the engine\n",
1615 p_hwfn->enabled_func_idx, p_hwfn->num_funcs_on_engine);
1619 qed_get_hw_info(struct qed_hwfn *p_hwfn,
1620 struct qed_ptt *p_ptt,
1621 enum qed_pci_personality personality)
1626 /* Since all information is common, only first hwfns should do this */
1627 if (IS_LEAD_HWFN(p_hwfn)) {
1628 rc = qed_iov_hw_info(p_hwfn);
1633 /* Read the port mode */
1634 port_mode = qed_rd(p_hwfn, p_ptt,
1635 CNIG_REG_NW_PORT_MODE_BB_B0);
1637 if (port_mode < 3) {
1638 p_hwfn->cdev->num_ports_in_engines = 1;
1639 } else if (port_mode <= 5) {
1640 p_hwfn->cdev->num_ports_in_engines = 2;
1642 DP_NOTICE(p_hwfn, "PORT MODE: %d not supported\n",
1643 p_hwfn->cdev->num_ports_in_engines);
1645 /* Default num_ports_in_engines to something */
1646 p_hwfn->cdev->num_ports_in_engines = 1;
1649 qed_hw_get_nvm_info(p_hwfn, p_ptt);
1651 rc = qed_int_igu_read_cam(p_hwfn, p_ptt);
1655 if (qed_mcp_is_init(p_hwfn))
1656 ether_addr_copy(p_hwfn->hw_info.hw_mac_addr,
1657 p_hwfn->mcp_info->func_info.mac);
1659 eth_random_addr(p_hwfn->hw_info.hw_mac_addr);
1661 if (qed_mcp_is_init(p_hwfn)) {
1662 if (p_hwfn->mcp_info->func_info.ovlan != QED_MCP_VLAN_UNSET)
1663 p_hwfn->hw_info.ovlan =
1664 p_hwfn->mcp_info->func_info.ovlan;
1666 qed_mcp_cmd_port_init(p_hwfn, p_ptt);
1669 if (qed_mcp_is_init(p_hwfn)) {
1670 enum qed_pci_personality protocol;
1672 protocol = p_hwfn->mcp_info->func_info.protocol;
1673 p_hwfn->hw_info.personality = protocol;
1676 qed_get_num_funcs(p_hwfn, p_ptt);
1678 return qed_hw_get_resc(p_hwfn);
1681 static int qed_get_dev_info(struct qed_dev *cdev)
1683 struct qed_hwfn *p_hwfn = QED_LEADING_HWFN(cdev);
1686 /* Read Vendor Id / Device Id */
1687 pci_read_config_word(cdev->pdev, PCI_VENDOR_ID, &cdev->vendor_id);
1688 pci_read_config_word(cdev->pdev, PCI_DEVICE_ID, &cdev->device_id);
1690 cdev->chip_num = (u16)qed_rd(p_hwfn, p_hwfn->p_main_ptt,
1691 MISCS_REG_CHIP_NUM);
1692 cdev->chip_rev = (u16)qed_rd(p_hwfn, p_hwfn->p_main_ptt,
1693 MISCS_REG_CHIP_REV);
1694 MASK_FIELD(CHIP_REV, cdev->chip_rev);
1696 cdev->type = QED_DEV_TYPE_BB;
1697 /* Learn number of HW-functions */
1698 tmp = qed_rd(p_hwfn, p_hwfn->p_main_ptt,
1699 MISCS_REG_CMT_ENABLED_FOR_PAIR);
1701 if (tmp & (1 << p_hwfn->rel_pf_id)) {
1702 DP_NOTICE(cdev->hwfns, "device in CMT mode\n");
1703 cdev->num_hwfns = 2;
1705 cdev->num_hwfns = 1;
1708 cdev->chip_bond_id = qed_rd(p_hwfn, p_hwfn->p_main_ptt,
1709 MISCS_REG_CHIP_TEST_REG) >> 4;
1710 MASK_FIELD(CHIP_BOND_ID, cdev->chip_bond_id);
1711 cdev->chip_metal = (u16)qed_rd(p_hwfn, p_hwfn->p_main_ptt,
1712 MISCS_REG_CHIP_METAL);
1713 MASK_FIELD(CHIP_METAL, cdev->chip_metal);
1715 DP_INFO(cdev->hwfns,
1716 "Chip details - Num: %04x Rev: %04x Bond id: %04x Metal: %04x\n",
1717 cdev->chip_num, cdev->chip_rev,
1718 cdev->chip_bond_id, cdev->chip_metal);
1720 if (QED_IS_BB(cdev) && CHIP_REV_IS_A0(cdev)) {
1721 DP_NOTICE(cdev->hwfns,
1722 "The chip type/rev (BB A0) is not supported!\n");
1729 static int qed_hw_prepare_single(struct qed_hwfn *p_hwfn,
1730 void __iomem *p_regview,
1731 void __iomem *p_doorbells,
1732 enum qed_pci_personality personality)
1736 /* Split PCI bars evenly between hwfns */
1737 p_hwfn->regview = p_regview;
1738 p_hwfn->doorbells = p_doorbells;
1740 if (IS_VF(p_hwfn->cdev))
1741 return qed_vf_hw_prepare(p_hwfn);
1743 /* Validate that chip access is feasible */
1744 if (REG_RD(p_hwfn, PXP_PF_ME_OPAQUE_ADDR) == 0xffffffff) {
1746 "Reading the ME register returns all Fs; Preventing further chip access\n");
1750 get_function_id(p_hwfn);
1752 /* Allocate PTT pool */
1753 rc = qed_ptt_pool_alloc(p_hwfn);
1755 DP_NOTICE(p_hwfn, "Failed to prepare hwfn's hw\n");
1759 /* Allocate the main PTT */
1760 p_hwfn->p_main_ptt = qed_get_reserved_ptt(p_hwfn, RESERVED_PTT_MAIN);
1762 /* First hwfn learns basic information, e.g., number of hwfns */
1763 if (!p_hwfn->my_id) {
1764 rc = qed_get_dev_info(p_hwfn->cdev);
1769 qed_hw_hwfn_prepare(p_hwfn);
1771 /* Initialize MCP structure */
1772 rc = qed_mcp_cmd_init(p_hwfn, p_hwfn->p_main_ptt);
1774 DP_NOTICE(p_hwfn, "Failed initializing mcp command\n");
1778 /* Read the device configuration information from the HW and SHMEM */
1779 rc = qed_get_hw_info(p_hwfn, p_hwfn->p_main_ptt, personality);
1781 DP_NOTICE(p_hwfn, "Failed to get HW information\n");
1785 /* Allocate the init RT array and initialize the init-ops engine */
1786 rc = qed_init_alloc(p_hwfn);
1788 DP_NOTICE(p_hwfn, "Failed to allocate the init array\n");
1794 if (IS_LEAD_HWFN(p_hwfn))
1795 qed_iov_free_hw_info(p_hwfn->cdev);
1796 qed_mcp_free(p_hwfn);
1798 qed_hw_hwfn_free(p_hwfn);
1803 int qed_hw_prepare(struct qed_dev *cdev,
1806 struct qed_hwfn *p_hwfn = QED_LEADING_HWFN(cdev);
1809 /* Store the precompiled init data ptrs */
1811 qed_init_iro_array(cdev);
1813 /* Initialize the first hwfn - will learn number of hwfns */
1814 rc = qed_hw_prepare_single(p_hwfn,
1816 cdev->doorbells, personality);
1820 personality = p_hwfn->hw_info.personality;
1822 /* Initialize the rest of the hwfns */
1823 if (cdev->num_hwfns > 1) {
1824 void __iomem *p_regview, *p_doorbell;
1827 /* adjust bar offset for second engine */
1828 addr = cdev->regview + qed_hw_bar_size(p_hwfn, BAR_ID_0) / 2;
1831 /* adjust doorbell bar offset for second engine */
1832 addr = cdev->doorbells + qed_hw_bar_size(p_hwfn, BAR_ID_1) / 2;
1835 /* prepare second hw function */
1836 rc = qed_hw_prepare_single(&cdev->hwfns[1], p_regview,
1837 p_doorbell, personality);
1839 /* in case of error, need to free the previously
1840 * initiliazed hwfn 0.
1844 qed_init_free(p_hwfn);
1845 qed_mcp_free(p_hwfn);
1846 qed_hw_hwfn_free(p_hwfn);
1854 void qed_hw_remove(struct qed_dev *cdev)
1858 for_each_hwfn(cdev, i) {
1859 struct qed_hwfn *p_hwfn = &cdev->hwfns[i];
1862 qed_vf_pf_release(p_hwfn);
1866 qed_init_free(p_hwfn);
1867 qed_hw_hwfn_free(p_hwfn);
1868 qed_mcp_free(p_hwfn);
1871 qed_iov_free_hw_info(cdev);
1874 static void qed_chain_free_next_ptr(struct qed_dev *cdev,
1875 struct qed_chain *p_chain)
1877 void *p_virt = p_chain->p_virt_addr, *p_virt_next = NULL;
1878 dma_addr_t p_phys = p_chain->p_phys_addr, p_phys_next = 0;
1879 struct qed_chain_next *p_next;
1885 size = p_chain->elem_size * p_chain->usable_per_page;
1887 for (i = 0; i < p_chain->page_cnt; i++) {
1891 p_next = (struct qed_chain_next *)((u8 *)p_virt + size);
1892 p_virt_next = p_next->next_virt;
1893 p_phys_next = HILO_DMA_REGPAIR(p_next->next_phys);
1895 dma_free_coherent(&cdev->pdev->dev,
1896 QED_CHAIN_PAGE_SIZE, p_virt, p_phys);
1898 p_virt = p_virt_next;
1899 p_phys = p_phys_next;
1903 static void qed_chain_free_single(struct qed_dev *cdev,
1904 struct qed_chain *p_chain)
1906 if (!p_chain->p_virt_addr)
1909 dma_free_coherent(&cdev->pdev->dev,
1910 QED_CHAIN_PAGE_SIZE,
1911 p_chain->p_virt_addr, p_chain->p_phys_addr);
1914 static void qed_chain_free_pbl(struct qed_dev *cdev, struct qed_chain *p_chain)
1916 void **pp_virt_addr_tbl = p_chain->pbl.pp_virt_addr_tbl;
1917 u32 page_cnt = p_chain->page_cnt, i, pbl_size;
1918 u8 *p_pbl_virt = p_chain->pbl.p_virt_table;
1920 if (!pp_virt_addr_tbl)
1923 if (!p_chain->pbl.p_virt_table)
1926 for (i = 0; i < page_cnt; i++) {
1927 if (!pp_virt_addr_tbl[i])
1930 dma_free_coherent(&cdev->pdev->dev,
1931 QED_CHAIN_PAGE_SIZE,
1932 pp_virt_addr_tbl[i],
1933 *(dma_addr_t *)p_pbl_virt);
1935 p_pbl_virt += QED_CHAIN_PBL_ENTRY_SIZE;
1938 pbl_size = page_cnt * QED_CHAIN_PBL_ENTRY_SIZE;
1939 dma_free_coherent(&cdev->pdev->dev,
1941 p_chain->pbl.p_virt_table, p_chain->pbl.p_phys_table);
1943 vfree(p_chain->pbl.pp_virt_addr_tbl);
1946 void qed_chain_free(struct qed_dev *cdev, struct qed_chain *p_chain)
1948 switch (p_chain->mode) {
1949 case QED_CHAIN_MODE_NEXT_PTR:
1950 qed_chain_free_next_ptr(cdev, p_chain);
1952 case QED_CHAIN_MODE_SINGLE:
1953 qed_chain_free_single(cdev, p_chain);
1955 case QED_CHAIN_MODE_PBL:
1956 qed_chain_free_pbl(cdev, p_chain);
1962 qed_chain_alloc_sanity_check(struct qed_dev *cdev,
1963 enum qed_chain_cnt_type cnt_type,
1964 size_t elem_size, u32 page_cnt)
1966 u64 chain_size = ELEMS_PER_PAGE(elem_size) * page_cnt;
1968 /* The actual chain size can be larger than the maximal possible value
1969 * after rounding up the requested elements number to pages, and after
1970 * taking into acount the unusuable elements (next-ptr elements).
1971 * The size of a "u16" chain can be (U16_MAX + 1) since the chain
1972 * size/capacity fields are of a u32 type.
1974 if ((cnt_type == QED_CHAIN_CNT_TYPE_U16 &&
1975 chain_size > 0x10000) ||
1976 (cnt_type == QED_CHAIN_CNT_TYPE_U32 &&
1977 chain_size > 0x100000000ULL)) {
1979 "The actual chain size (0x%llx) is larger than the maximal possible value\n",
1988 qed_chain_alloc_next_ptr(struct qed_dev *cdev, struct qed_chain *p_chain)
1990 void *p_virt = NULL, *p_virt_prev = NULL;
1991 dma_addr_t p_phys = 0;
1994 for (i = 0; i < p_chain->page_cnt; i++) {
1995 p_virt = dma_alloc_coherent(&cdev->pdev->dev,
1996 QED_CHAIN_PAGE_SIZE,
1997 &p_phys, GFP_KERNEL);
1999 DP_NOTICE(cdev, "Failed to allocate chain memory\n");
2004 qed_chain_init_mem(p_chain, p_virt, p_phys);
2005 qed_chain_reset(p_chain);
2007 qed_chain_init_next_ptr_elem(p_chain, p_virt_prev,
2011 p_virt_prev = p_virt;
2013 /* Last page's next element should point to the beginning of the
2016 qed_chain_init_next_ptr_elem(p_chain, p_virt_prev,
2017 p_chain->p_virt_addr,
2018 p_chain->p_phys_addr);
2024 qed_chain_alloc_single(struct qed_dev *cdev, struct qed_chain *p_chain)
2026 dma_addr_t p_phys = 0;
2027 void *p_virt = NULL;
2029 p_virt = dma_alloc_coherent(&cdev->pdev->dev,
2030 QED_CHAIN_PAGE_SIZE, &p_phys, GFP_KERNEL);
2032 DP_NOTICE(cdev, "Failed to allocate chain memory\n");
2036 qed_chain_init_mem(p_chain, p_virt, p_phys);
2037 qed_chain_reset(p_chain);
2042 static int qed_chain_alloc_pbl(struct qed_dev *cdev, struct qed_chain *p_chain)
2044 u32 page_cnt = p_chain->page_cnt, size, i;
2045 dma_addr_t p_phys = 0, p_pbl_phys = 0;
2046 void **pp_virt_addr_tbl = NULL;
2047 u8 *p_pbl_virt = NULL;
2048 void *p_virt = NULL;
2050 size = page_cnt * sizeof(*pp_virt_addr_tbl);
2051 pp_virt_addr_tbl = vmalloc(size);
2052 if (!pp_virt_addr_tbl) {
2054 "Failed to allocate memory for the chain virtual addresses table\n");
2057 memset(pp_virt_addr_tbl, 0, size);
2059 /* The allocation of the PBL table is done with its full size, since it
2060 * is expected to be successive.
2061 * qed_chain_init_pbl_mem() is called even in a case of an allocation
2062 * failure, since pp_virt_addr_tbl was previously allocated, and it
2063 * should be saved to allow its freeing during the error flow.
2065 size = page_cnt * QED_CHAIN_PBL_ENTRY_SIZE;
2066 p_pbl_virt = dma_alloc_coherent(&cdev->pdev->dev,
2067 size, &p_pbl_phys, GFP_KERNEL);
2068 qed_chain_init_pbl_mem(p_chain, p_pbl_virt, p_pbl_phys,
2071 DP_NOTICE(cdev, "Failed to allocate chain pbl memory\n");
2075 for (i = 0; i < page_cnt; i++) {
2076 p_virt = dma_alloc_coherent(&cdev->pdev->dev,
2077 QED_CHAIN_PAGE_SIZE,
2078 &p_phys, GFP_KERNEL);
2080 DP_NOTICE(cdev, "Failed to allocate chain memory\n");
2085 qed_chain_init_mem(p_chain, p_virt, p_phys);
2086 qed_chain_reset(p_chain);
2089 /* Fill the PBL table with the physical address of the page */
2090 *(dma_addr_t *)p_pbl_virt = p_phys;
2091 /* Keep the virtual address of the page */
2092 p_chain->pbl.pp_virt_addr_tbl[i] = p_virt;
2094 p_pbl_virt += QED_CHAIN_PBL_ENTRY_SIZE;
2100 int qed_chain_alloc(struct qed_dev *cdev,
2101 enum qed_chain_use_mode intended_use,
2102 enum qed_chain_mode mode,
2103 enum qed_chain_cnt_type cnt_type,
2104 u32 num_elems, size_t elem_size, struct qed_chain *p_chain)
2109 if (mode == QED_CHAIN_MODE_SINGLE)
2112 page_cnt = QED_CHAIN_PAGE_CNT(num_elems, elem_size, mode);
2114 rc = qed_chain_alloc_sanity_check(cdev, cnt_type, elem_size, page_cnt);
2117 "Cannot allocate a chain with the given arguments:\n"
2118 "[use_mode %d, mode %d, cnt_type %d, num_elems %d, elem_size %zu]\n",
2119 intended_use, mode, cnt_type, num_elems, elem_size);
2123 qed_chain_init_params(p_chain, page_cnt, (u8) elem_size, intended_use,
2127 case QED_CHAIN_MODE_NEXT_PTR:
2128 rc = qed_chain_alloc_next_ptr(cdev, p_chain);
2130 case QED_CHAIN_MODE_SINGLE:
2131 rc = qed_chain_alloc_single(cdev, p_chain);
2133 case QED_CHAIN_MODE_PBL:
2134 rc = qed_chain_alloc_pbl(cdev, p_chain);
2143 qed_chain_free(cdev, p_chain);
2147 int qed_fw_l2_queue(struct qed_hwfn *p_hwfn, u16 src_id, u16 *dst_id)
2149 if (src_id >= RESC_NUM(p_hwfn, QED_L2_QUEUE)) {
2152 min = (u16) RESC_START(p_hwfn, QED_L2_QUEUE);
2153 max = min + RESC_NUM(p_hwfn, QED_L2_QUEUE);
2155 "l2_queue id [%d] is not valid, available indices [%d - %d]\n",
2161 *dst_id = RESC_START(p_hwfn, QED_L2_QUEUE) + src_id;
2166 int qed_fw_vport(struct qed_hwfn *p_hwfn, u8 src_id, u8 *dst_id)
2168 if (src_id >= RESC_NUM(p_hwfn, QED_VPORT)) {
2171 min = (u8)RESC_START(p_hwfn, QED_VPORT);
2172 max = min + RESC_NUM(p_hwfn, QED_VPORT);
2174 "vport id [%d] is not valid, available indices [%d - %d]\n",
2180 *dst_id = RESC_START(p_hwfn, QED_VPORT) + src_id;
2185 int qed_fw_rss_eng(struct qed_hwfn *p_hwfn, u8 src_id, u8 *dst_id)
2187 if (src_id >= RESC_NUM(p_hwfn, QED_RSS_ENG)) {
2190 min = (u8)RESC_START(p_hwfn, QED_RSS_ENG);
2191 max = min + RESC_NUM(p_hwfn, QED_RSS_ENG);
2193 "rss_eng id [%d] is not valid, available indices [%d - %d]\n",
2199 *dst_id = RESC_START(p_hwfn, QED_RSS_ENG) + src_id;
2204 static int qed_set_coalesce(struct qed_hwfn *p_hwfn, struct qed_ptt *p_ptt,
2205 u32 hw_addr, void *p_eth_qzone,
2206 size_t eth_qzone_size, u8 timeset)
2208 struct coalescing_timeset *p_coal_timeset;
2210 if (p_hwfn->cdev->int_coalescing_mode != QED_COAL_MODE_ENABLE) {
2211 DP_NOTICE(p_hwfn, "Coalescing configuration not enabled\n");
2215 p_coal_timeset = p_eth_qzone;
2216 memset(p_coal_timeset, 0, eth_qzone_size);
2217 SET_FIELD(p_coal_timeset->value, COALESCING_TIMESET_TIMESET, timeset);
2218 SET_FIELD(p_coal_timeset->value, COALESCING_TIMESET_VALID, 1);
2219 qed_memcpy_to(p_hwfn, p_ptt, hw_addr, p_eth_qzone, eth_qzone_size);
2224 int qed_set_rxq_coalesce(struct qed_hwfn *p_hwfn, struct qed_ptt *p_ptt,
2225 u16 coalesce, u8 qid, u16 sb_id)
2227 struct ustorm_eth_queue_zone eth_qzone;
2228 u8 timeset, timer_res;
2233 /* Coalesce = (timeset << timer-resolution), timeset is 7bit wide */
2234 if (coalesce <= 0x7F) {
2236 } else if (coalesce <= 0xFF) {
2238 } else if (coalesce <= 0x1FF) {
2241 DP_ERR(p_hwfn, "Invalid coalesce value - %d\n", coalesce);
2244 timeset = (u8)(coalesce >> timer_res);
2246 rc = qed_fw_l2_queue(p_hwfn, (u16)qid, &fw_qid);
2250 rc = qed_int_set_timer_res(p_hwfn, p_ptt, timer_res, sb_id, false);
2254 address = BAR0_MAP_REG_USDM_RAM + USTORM_ETH_QUEUE_ZONE_OFFSET(fw_qid);
2256 rc = qed_set_coalesce(p_hwfn, p_ptt, address, ð_qzone,
2257 sizeof(struct ustorm_eth_queue_zone), timeset);
2261 p_hwfn->cdev->rx_coalesce_usecs = coalesce;
2266 int qed_set_txq_coalesce(struct qed_hwfn *p_hwfn, struct qed_ptt *p_ptt,
2267 u16 coalesce, u8 qid, u16 sb_id)
2269 struct xstorm_eth_queue_zone eth_qzone;
2270 u8 timeset, timer_res;
2275 /* Coalesce = (timeset << timer-resolution), timeset is 7bit wide */
2276 if (coalesce <= 0x7F) {
2278 } else if (coalesce <= 0xFF) {
2280 } else if (coalesce <= 0x1FF) {
2283 DP_ERR(p_hwfn, "Invalid coalesce value - %d\n", coalesce);
2286 timeset = (u8)(coalesce >> timer_res);
2288 rc = qed_fw_l2_queue(p_hwfn, (u16)qid, &fw_qid);
2292 rc = qed_int_set_timer_res(p_hwfn, p_ptt, timer_res, sb_id, true);
2296 address = BAR0_MAP_REG_XSDM_RAM + XSTORM_ETH_QUEUE_ZONE_OFFSET(fw_qid);
2298 rc = qed_set_coalesce(p_hwfn, p_ptt, address, ð_qzone,
2299 sizeof(struct xstorm_eth_queue_zone), timeset);
2303 p_hwfn->cdev->tx_coalesce_usecs = coalesce;
2308 /* Calculate final WFQ values for all vports and configure them.
2309 * After this configuration each vport will have
2310 * approx min rate = min_pf_rate * (vport_wfq / QED_WFQ_UNIT)
2312 static void qed_configure_wfq_for_all_vports(struct qed_hwfn *p_hwfn,
2313 struct qed_ptt *p_ptt,
2316 struct init_qm_vport_params *vport_params;
2319 vport_params = p_hwfn->qm_info.qm_vport_params;
2321 for (i = 0; i < p_hwfn->qm_info.num_vports; i++) {
2322 u32 wfq_speed = p_hwfn->qm_info.wfq_data[i].min_speed;
2324 vport_params[i].vport_wfq = (wfq_speed * QED_WFQ_UNIT) /
2326 qed_init_vport_wfq(p_hwfn, p_ptt,
2327 vport_params[i].first_tx_pq_id,
2328 vport_params[i].vport_wfq);
2332 static void qed_init_wfq_default_param(struct qed_hwfn *p_hwfn,
2338 for (i = 0; i < p_hwfn->qm_info.num_vports; i++)
2339 p_hwfn->qm_info.qm_vport_params[i].vport_wfq = 1;
2342 static void qed_disable_wfq_for_all_vports(struct qed_hwfn *p_hwfn,
2343 struct qed_ptt *p_ptt,
2346 struct init_qm_vport_params *vport_params;
2349 vport_params = p_hwfn->qm_info.qm_vport_params;
2351 for (i = 0; i < p_hwfn->qm_info.num_vports; i++) {
2352 qed_init_wfq_default_param(p_hwfn, min_pf_rate);
2353 qed_init_vport_wfq(p_hwfn, p_ptt,
2354 vport_params[i].first_tx_pq_id,
2355 vport_params[i].vport_wfq);
2359 /* This function performs several validations for WFQ
2360 * configuration and required min rate for a given vport
2361 * 1. req_rate must be greater than one percent of min_pf_rate.
2362 * 2. req_rate should not cause other vports [not configured for WFQ explicitly]
2363 * rates to get less than one percent of min_pf_rate.
2364 * 3. total_req_min_rate [all vports min rate sum] shouldn't exceed min_pf_rate.
2366 static int qed_init_wfq_param(struct qed_hwfn *p_hwfn,
2367 u16 vport_id, u32 req_rate, u32 min_pf_rate)
2369 u32 total_req_min_rate = 0, total_left_rate = 0, left_rate_per_vp = 0;
2370 int non_requested_count = 0, req_count = 0, i, num_vports;
2372 num_vports = p_hwfn->qm_info.num_vports;
2374 /* Accounting for the vports which are configured for WFQ explicitly */
2375 for (i = 0; i < num_vports; i++) {
2378 if ((i != vport_id) &&
2379 p_hwfn->qm_info.wfq_data[i].configured) {
2381 tmp_speed = p_hwfn->qm_info.wfq_data[i].min_speed;
2382 total_req_min_rate += tmp_speed;
2386 /* Include current vport data as well */
2388 total_req_min_rate += req_rate;
2389 non_requested_count = num_vports - req_count;
2391 if (req_rate < min_pf_rate / QED_WFQ_UNIT) {
2392 DP_VERBOSE(p_hwfn, NETIF_MSG_LINK,
2393 "Vport [%d] - Requested rate[%d Mbps] is less than one percent of configured PF min rate[%d Mbps]\n",
2394 vport_id, req_rate, min_pf_rate);
2398 if (num_vports > QED_WFQ_UNIT) {
2399 DP_VERBOSE(p_hwfn, NETIF_MSG_LINK,
2400 "Number of vports is greater than %d\n",
2405 if (total_req_min_rate > min_pf_rate) {
2406 DP_VERBOSE(p_hwfn, NETIF_MSG_LINK,
2407 "Total requested min rate for all vports[%d Mbps] is greater than configured PF min rate[%d Mbps]\n",
2408 total_req_min_rate, min_pf_rate);
2412 total_left_rate = min_pf_rate - total_req_min_rate;
2414 left_rate_per_vp = total_left_rate / non_requested_count;
2415 if (left_rate_per_vp < min_pf_rate / QED_WFQ_UNIT) {
2416 DP_VERBOSE(p_hwfn, NETIF_MSG_LINK,
2417 "Non WFQ configured vports rate [%d Mbps] is less than one percent of configured PF min rate[%d Mbps]\n",
2418 left_rate_per_vp, min_pf_rate);
2422 p_hwfn->qm_info.wfq_data[vport_id].min_speed = req_rate;
2423 p_hwfn->qm_info.wfq_data[vport_id].configured = true;
2425 for (i = 0; i < num_vports; i++) {
2426 if (p_hwfn->qm_info.wfq_data[i].configured)
2429 p_hwfn->qm_info.wfq_data[i].min_speed = left_rate_per_vp;
2435 static int __qed_configure_vport_wfq(struct qed_hwfn *p_hwfn,
2436 struct qed_ptt *p_ptt, u16 vp_id, u32 rate)
2438 struct qed_mcp_link_state *p_link;
2441 p_link = &p_hwfn->cdev->hwfns[0].mcp_info->link_output;
2443 if (!p_link->min_pf_rate) {
2444 p_hwfn->qm_info.wfq_data[vp_id].min_speed = rate;
2445 p_hwfn->qm_info.wfq_data[vp_id].configured = true;
2449 rc = qed_init_wfq_param(p_hwfn, vp_id, rate, p_link->min_pf_rate);
2452 qed_configure_wfq_for_all_vports(p_hwfn, p_ptt,
2453 p_link->min_pf_rate);
2456 "Validation failed while configuring min rate\n");
2461 static int __qed_configure_vp_wfq_on_link_change(struct qed_hwfn *p_hwfn,
2462 struct qed_ptt *p_ptt,
2465 bool use_wfq = false;
2469 /* Validate all pre configured vports for wfq */
2470 for (i = 0; i < p_hwfn->qm_info.num_vports; i++) {
2473 if (!p_hwfn->qm_info.wfq_data[i].configured)
2476 rate = p_hwfn->qm_info.wfq_data[i].min_speed;
2479 rc = qed_init_wfq_param(p_hwfn, i, rate, min_pf_rate);
2482 "WFQ validation failed while configuring min rate\n");
2488 qed_configure_wfq_for_all_vports(p_hwfn, p_ptt, min_pf_rate);
2490 qed_disable_wfq_for_all_vports(p_hwfn, p_ptt, min_pf_rate);
2495 /* Main API for qed clients to configure vport min rate.
2496 * vp_id - vport id in PF Range[0 - (total_num_vports_per_pf - 1)]
2497 * rate - Speed in Mbps needs to be assigned to a given vport.
2499 int qed_configure_vport_wfq(struct qed_dev *cdev, u16 vp_id, u32 rate)
2501 int i, rc = -EINVAL;
2503 /* Currently not supported; Might change in future */
2504 if (cdev->num_hwfns > 1) {
2506 "WFQ configuration is not supported for this device\n");
2510 for_each_hwfn(cdev, i) {
2511 struct qed_hwfn *p_hwfn = &cdev->hwfns[i];
2512 struct qed_ptt *p_ptt;
2514 p_ptt = qed_ptt_acquire(p_hwfn);
2518 rc = __qed_configure_vport_wfq(p_hwfn, p_ptt, vp_id, rate);
2521 qed_ptt_release(p_hwfn, p_ptt);
2525 qed_ptt_release(p_hwfn, p_ptt);
2531 /* API to configure WFQ from mcp link change */
2532 void qed_configure_vp_wfq_on_link_change(struct qed_dev *cdev, u32 min_pf_rate)
2536 if (cdev->num_hwfns > 1) {
2539 "WFQ configuration is not supported for this device\n");
2543 for_each_hwfn(cdev, i) {
2544 struct qed_hwfn *p_hwfn = &cdev->hwfns[i];
2546 __qed_configure_vp_wfq_on_link_change(p_hwfn,
2552 int __qed_configure_pf_max_bandwidth(struct qed_hwfn *p_hwfn,
2553 struct qed_ptt *p_ptt,
2554 struct qed_mcp_link_state *p_link,
2559 p_hwfn->mcp_info->func_info.bandwidth_max = max_bw;
2561 if (!p_link->line_speed && (max_bw != 100))
2564 p_link->speed = (p_link->line_speed * max_bw) / 100;
2565 p_hwfn->qm_info.pf_rl = p_link->speed;
2567 /* Since the limiter also affects Tx-switched traffic, we don't want it
2568 * to limit such traffic in case there's no actual limit.
2569 * In that case, set limit to imaginary high boundary.
2572 p_hwfn->qm_info.pf_rl = 100000;
2574 rc = qed_init_pf_rl(p_hwfn, p_ptt, p_hwfn->rel_pf_id,
2575 p_hwfn->qm_info.pf_rl);
2577 DP_VERBOSE(p_hwfn, NETIF_MSG_LINK,
2578 "Configured MAX bandwidth to be %08x Mb/sec\n",
2584 /* Main API to configure PF max bandwidth where bw range is [1 - 100] */
2585 int qed_configure_pf_max_bandwidth(struct qed_dev *cdev, u8 max_bw)
2587 int i, rc = -EINVAL;
2589 if (max_bw < 1 || max_bw > 100) {
2590 DP_NOTICE(cdev, "PF max bw valid range is [1-100]\n");
2594 for_each_hwfn(cdev, i) {
2595 struct qed_hwfn *p_hwfn = &cdev->hwfns[i];
2596 struct qed_hwfn *p_lead = QED_LEADING_HWFN(cdev);
2597 struct qed_mcp_link_state *p_link;
2598 struct qed_ptt *p_ptt;
2600 p_link = &p_lead->mcp_info->link_output;
2602 p_ptt = qed_ptt_acquire(p_hwfn);
2606 rc = __qed_configure_pf_max_bandwidth(p_hwfn, p_ptt,
2609 qed_ptt_release(p_hwfn, p_ptt);
2618 int __qed_configure_pf_min_bandwidth(struct qed_hwfn *p_hwfn,
2619 struct qed_ptt *p_ptt,
2620 struct qed_mcp_link_state *p_link,
2625 p_hwfn->mcp_info->func_info.bandwidth_min = min_bw;
2626 p_hwfn->qm_info.pf_wfq = min_bw;
2628 if (!p_link->line_speed)
2631 p_link->min_pf_rate = (p_link->line_speed * min_bw) / 100;
2633 rc = qed_init_pf_wfq(p_hwfn, p_ptt, p_hwfn->rel_pf_id, min_bw);
2635 DP_VERBOSE(p_hwfn, NETIF_MSG_LINK,
2636 "Configured MIN bandwidth to be %d Mb/sec\n",
2637 p_link->min_pf_rate);
2642 /* Main API to configure PF min bandwidth where bw range is [1-100] */
2643 int qed_configure_pf_min_bandwidth(struct qed_dev *cdev, u8 min_bw)
2645 int i, rc = -EINVAL;
2647 if (min_bw < 1 || min_bw > 100) {
2648 DP_NOTICE(cdev, "PF min bw valid range is [1-100]\n");
2652 for_each_hwfn(cdev, i) {
2653 struct qed_hwfn *p_hwfn = &cdev->hwfns[i];
2654 struct qed_hwfn *p_lead = QED_LEADING_HWFN(cdev);
2655 struct qed_mcp_link_state *p_link;
2656 struct qed_ptt *p_ptt;
2658 p_link = &p_lead->mcp_info->link_output;
2660 p_ptt = qed_ptt_acquire(p_hwfn);
2664 rc = __qed_configure_pf_min_bandwidth(p_hwfn, p_ptt,
2667 qed_ptt_release(p_hwfn, p_ptt);
2671 if (p_link->min_pf_rate) {
2672 u32 min_rate = p_link->min_pf_rate;
2674 rc = __qed_configure_vp_wfq_on_link_change(p_hwfn,
2679 qed_ptt_release(p_hwfn, p_ptt);
2685 void qed_clean_wfq_db(struct qed_hwfn *p_hwfn, struct qed_ptt *p_ptt)
2687 struct qed_mcp_link_state *p_link;
2689 p_link = &p_hwfn->mcp_info->link_output;
2691 if (p_link->min_pf_rate)
2692 qed_disable_wfq_for_all_vports(p_hwfn, p_ptt,
2693 p_link->min_pf_rate);
2695 memset(p_hwfn->qm_info.wfq_data, 0,
2696 sizeof(*p_hwfn->qm_info.wfq_data) * p_hwfn->qm_info.num_vports);