1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2018 Chelsio Communications.
6 #include <rte_ethdev_driver.h>
13 * t4vf_wait_dev_ready - wait till to reads of registers work
15 * Wait for the device to become ready (signified by our "who am I" register
16 * returning a value other than all 1's). Return an error if it doesn't
19 static int t4vf_wait_dev_ready(struct adapter *adapter)
21 const u32 whoami = T4VF_PL_BASE_ADDR + A_PL_VF_WHOAMI;
22 const u32 notready1 = 0xffffffff;
23 const u32 notready2 = 0xeeeeeeee;
26 val = t4_read_reg(adapter, whoami);
27 if (val != notready1 && val != notready2)
31 val = t4_read_reg(adapter, whoami);
32 if (val != notready1 && val != notready2)
35 dev_err(adapter, "Device didn't become ready for access, whoami = %#x\n",
41 * Get the reply to a mailbox command and store it in @rpl in big-endian order.
43 static void get_mbox_rpl(struct adapter *adap, __be64 *rpl, int nflit,
46 for ( ; nflit; nflit--, mbox_addr += 8)
47 *rpl++ = htobe64(t4_read_reg64(adap, mbox_addr));
51 * t4vf_wr_mbox_core - send a command to FW through the mailbox
52 * @adapter: the adapter
53 * @cmd: the command to write
54 * @size: command length in bytes
55 * @rpl: where to optionally store the reply
56 * @sleep_ok: if true we may sleep while awaiting command completion
58 * Sends the given command to FW through the mailbox and waits for the
59 * FW to execute the command. If @rpl is not %NULL it is used to store
60 * the FW's reply to the command. The command and its optional reply
61 * are of the same length. FW can take up to 500 ms to respond.
62 * @sleep_ok determines whether we may sleep while awaiting the response.
63 * If sleeping is allowed we use progressive backoff otherwise we spin.
65 * The return value is 0 on success or a negative errno on failure. A
66 * failure can happen either because we are not able to execute the
67 * command or FW executes it but signals an error. In the latter case
68 * the return value is the error code indicated by FW (negated).
70 int t4vf_wr_mbox_core(struct adapter *adapter,
71 const void __attribute__((__may_alias__)) *cmd,
72 int size, void *rpl, bool sleep_ok)
75 * We delay in small increments at first in an effort to maintain
76 * responsiveness for simple, fast executing commands but then back
77 * off to larger delays to a maximum retry delay.
79 static const int delay[] = {
80 1, 1, 3, 5, 10, 10, 20, 50, 100
84 u32 mbox_ctl = T4VF_CIM_BASE_ADDR + A_CIM_VF_EXT_MAILBOX_CTRL;
85 __be64 cmd_rpl[MBOX_LEN / 8];
86 struct mbox_entry entry;
87 unsigned int delay_idx;
93 /* In T6, mailbox size is changed to 128 bytes to avoid
94 * invalidating the entire prefetch buffer.
96 if (CHELSIO_CHIP_VERSION(adapter->params.chip) <= CHELSIO_T5)
97 mbox_data = T4VF_MBDATA_BASE_ADDR;
99 mbox_data = T6VF_MBDATA_BASE_ADDR;
102 * Commands must be multiples of 16 bytes in length and may not be
103 * larger than the size of the Mailbox Data register array.
105 if ((size % 16) != 0 ||
106 size > NUM_CIM_VF_MAILBOX_DATA_INSTANCES * 4)
110 * Queue ourselves onto the mailbox access list. When our entry is at
111 * the front of the list, we have rights to access the mailbox. So we
112 * wait [for a while] till we're at the front [or bail out with an
115 t4_os_atomic_add_tail(&entry, &adapter->mbox_list, &adapter->mbox_lock);
120 for (i = 0; ; i += ms) {
122 * If we've waited too long, return a busy indication. This
123 * really ought to be based on our initial position in the
124 * mailbox access list but this is a start. We very rarely
125 * contend on access to the mailbox ...
127 if (i > (2 * FW_CMD_MAX_TIMEOUT)) {
128 t4_os_atomic_list_del(&entry, &adapter->mbox_list,
129 &adapter->mbox_lock);
135 * If we're at the head, break out and start the mailbox
138 if (t4_os_list_first_entry(&adapter->mbox_list) == &entry)
142 * Delay for a bit before checking again ...
145 ms = delay[delay_idx]; /* last element may repeat */
146 if (delay_idx < ARRAY_SIZE(delay) - 1)
155 * Loop trying to get ownership of the mailbox. Return an error
156 * if we can't gain ownership.
158 v = G_MBOWNER(t4_read_reg(adapter, mbox_ctl));
159 for (i = 0; v == X_MBOWNER_NONE && i < 3; i++)
160 v = G_MBOWNER(t4_read_reg(adapter, mbox_ctl));
162 if (v != X_MBOWNER_PL) {
163 t4_os_atomic_list_del(&entry, &adapter->mbox_list,
164 &adapter->mbox_lock);
165 ret = (v == X_MBOWNER_FW) ? -EBUSY : -ETIMEDOUT;
170 * Write the command array into the Mailbox Data register array and
171 * transfer ownership of the mailbox to the firmware.
173 for (i = 0, p = cmd; i < size; i += 8)
174 t4_write_reg64(adapter, mbox_data + i, be64_to_cpu(*p++));
176 t4_read_reg(adapter, mbox_data); /* flush write */
177 t4_write_reg(adapter, mbox_ctl,
178 F_MBMSGVALID | V_MBOWNER(X_MBOWNER_FW));
179 t4_read_reg(adapter, mbox_ctl); /* flush write */
184 * Spin waiting for firmware to acknowledge processing our command.
186 for (i = 0; i < FW_CMD_MAX_TIMEOUT; i++) {
188 ms = delay[delay_idx]; /* last element may repeat */
189 if (delay_idx < ARRAY_SIZE(delay) - 1)
197 * If we're the owner, see if this is the reply we wanted.
199 v = t4_read_reg(adapter, mbox_ctl);
200 if (G_MBOWNER(v) == X_MBOWNER_PL) {
202 * If the Message Valid bit isn't on, revoke ownership
203 * of the mailbox and continue waiting for our reply.
205 if ((v & F_MBMSGVALID) == 0) {
206 t4_write_reg(adapter, mbox_ctl,
207 V_MBOWNER(X_MBOWNER_NONE));
212 * We now have our reply. Extract the command return
213 * value, copy the reply back to our caller's buffer
214 * (if specified) and revoke ownership of the mailbox.
215 * We return the (negated) firmware command return
216 * code (this depends on FW_SUCCESS == 0). (Again we
217 * avoid clogging the log with FW_VI_STATS_CMD
222 * Retrieve the command reply and release the mailbox.
224 get_mbox_rpl(adapter, cmd_rpl, size / 8, mbox_data);
225 t4_write_reg(adapter, mbox_ctl,
226 V_MBOWNER(X_MBOWNER_NONE));
227 t4_os_atomic_list_del(&entry, &adapter->mbox_list,
228 &adapter->mbox_lock);
230 /* return value in high-order host-endian word */
231 v = be64_to_cpu(cmd_rpl[0]);
234 /* request bit in high-order BE word */
235 WARN_ON((be32_to_cpu(*(const u32 *)cmd)
236 & F_FW_CMD_REQUEST) == 0);
237 memcpy(rpl, cmd_rpl, size);
239 return -((int)G_FW_CMD_RETVAL(v));
244 * We timed out. Return the error ...
246 dev_err(adapter, "command %#x timed out\n",
248 dev_err(adapter, " Control = %#x\n", t4_read_reg(adapter, mbox_ctl));
249 t4_os_atomic_list_del(&entry, &adapter->mbox_list, &adapter->mbox_lock);
255 * t4vf_fw_reset - issue a reset to FW
256 * @adapter: the adapter
258 * Issues a reset command to FW. For a Physical Function this would
259 * result in the Firmware resetting all of its state. For a Virtual
260 * Function this just resets the state associated with the VF.
262 int t4vf_fw_reset(struct adapter *adapter)
264 struct fw_reset_cmd cmd;
266 memset(&cmd, 0, sizeof(cmd));
267 cmd.op_to_write = cpu_to_be32(V_FW_CMD_OP(FW_RESET_CMD) |
269 cmd.retval_len16 = cpu_to_be32(V_FW_CMD_LEN16(FW_LEN16(cmd)));
270 return t4vf_wr_mbox(adapter, &cmd, sizeof(cmd), NULL);
274 * t4vf_prep_adapter - prepare SW and HW for operation
275 * @adapter: the adapter
277 * Initialize adapter SW state for the various HW modules, set initial
278 * values for some adapter tunables, take PHYs out of reset, and
279 * initialize the MDIO interface.
281 int t4vf_prep_adapter(struct adapter *adapter)
286 ret = t4vf_wait_dev_ready(adapter);
291 * Default port and clock for debugging in case we can't reach
294 adapter->params.nports = 1;
295 adapter->params.vfres.pmask = 1;
296 adapter->params.vpd.cclk = 50000;
298 pl_vf_rev = G_REV(t4_read_reg(adapter, A_PL_VF_REV));
299 adapter->params.pci.device_id = adapter->pdev->id.device_id;
300 adapter->params.pci.vendor_id = adapter->pdev->id.vendor_id;
303 * WE DON'T NEED adapter->params.chip CODE ONCE PL_REV CONTAINS
304 * ADAPTER (VERSION << 4 | REVISION)
306 ver = CHELSIO_PCI_ID_VER(adapter->params.pci.device_id);
307 adapter->params.chip = 0;
310 adapter->params.chip |= CHELSIO_CHIP_CODE(CHELSIO_T5,
312 adapter->params.arch.sge_fl_db = F_DBPRIO | F_DBTYPE;
313 adapter->params.arch.mps_tcam_size =
314 NUM_MPS_T5_CLS_SRAM_L_INSTANCES;
317 adapter->params.chip |= CHELSIO_CHIP_CODE(CHELSIO_T6,
319 adapter->params.arch.sge_fl_db = 0;
320 adapter->params.arch.mps_tcam_size =
321 NUM_MPS_T5_CLS_SRAM_L_INSTANCES;
324 dev_err(adapter, "%s: Device %d is not supported\n",
325 __func__, adapter->params.pci.device_id);
332 * t4vf_query_params - query FW or device parameters
333 * @adapter: the adapter
334 * @nparams: the number of parameters
335 * @params: the parameter names
336 * @vals: the parameter values
338 * Reads the values of firmware or device parameters. Up to 7 parameters
339 * can be queried at once.
341 int t4vf_query_params(struct adapter *adapter, unsigned int nparams,
342 const u32 *params, u32 *vals)
344 struct fw_params_cmd cmd, rpl;
345 struct fw_params_param *p;
353 memset(&cmd, 0, sizeof(cmd));
354 cmd.op_to_vfn = cpu_to_be32(V_FW_CMD_OP(FW_PARAMS_CMD) |
357 len16 = DIV_ROUND_UP(offsetof(struct fw_params_cmd,
358 param[nparams]), 16);
359 cmd.retval_len16 = cpu_to_be32(V_FW_CMD_LEN16(len16));
360 for (i = 0, p = &cmd.param[0]; i < nparams; i++, p++)
361 p->mnem = cpu_to_be32(*params++);
362 ret = t4vf_wr_mbox(adapter, &cmd, sizeof(cmd), &rpl);
364 for (i = 0, p = &rpl.param[0]; i < nparams; i++, p++)
365 *vals++ = be32_to_cpu(p->val);
370 * t4vf_get_vpd_params - retrieve device VPD paremeters
371 * @adapter: the adapter
373 * Retrives various device Vital Product Data parameters. The parameters
374 * are stored in @adapter->params.vpd.
376 int t4vf_get_vpd_params(struct adapter *adapter)
378 struct vpd_params *vpd_params = &adapter->params.vpd;
379 u32 params[7], vals[7];
382 params[0] = (V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_DEV) |
383 V_FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_DEV_CCLK));
384 v = t4vf_query_params(adapter, 1, params, vals);
387 vpd_params->cclk = vals[0];
388 dev_debug(adapter, "%s: vpd_params->cclk = %u\n",
389 __func__, vpd_params->cclk);
394 * t4vf_get_dev_params - retrieve device paremeters
395 * @adapter: the adapter
397 * Retrives fw and tp version.
399 int t4vf_get_dev_params(struct adapter *adapter)
401 u32 params[7], vals[7];
404 params[0] = (V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_DEV) |
405 V_FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_DEV_FWREV));
406 params[1] = (V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_DEV) |
407 V_FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_DEV_TPREV));
408 v = t4vf_query_params(adapter, 2, params, vals);
411 adapter->params.fw_vers = vals[0];
412 adapter->params.tp_vers = vals[1];
414 dev_info(adapter, "Firmware version: %u.%u.%u.%u\n",
415 G_FW_HDR_FW_VER_MAJOR(adapter->params.fw_vers),
416 G_FW_HDR_FW_VER_MINOR(adapter->params.fw_vers),
417 G_FW_HDR_FW_VER_MICRO(adapter->params.fw_vers),
418 G_FW_HDR_FW_VER_BUILD(adapter->params.fw_vers));
420 dev_info(adapter, "TP Microcode version: %u.%u.%u.%u\n",
421 G_FW_HDR_FW_VER_MAJOR(adapter->params.tp_vers),
422 G_FW_HDR_FW_VER_MINOR(adapter->params.tp_vers),
423 G_FW_HDR_FW_VER_MICRO(adapter->params.tp_vers),
424 G_FW_HDR_FW_VER_BUILD(adapter->params.tp_vers));
429 * t4vf_set_params - sets FW or device parameters
430 * @adapter: the adapter
431 * @nparams: the number of parameters
432 * @params: the parameter names
433 * @vals: the parameter values
435 * Sets the values of firmware or device parameters. Up to 7 parameters
436 * can be specified at once.
438 int t4vf_set_params(struct adapter *adapter, unsigned int nparams,
439 const u32 *params, const u32 *vals)
441 struct fw_params_param *p;
442 struct fw_params_cmd cmd;
449 memset(&cmd, 0, sizeof(cmd));
450 cmd.op_to_vfn = cpu_to_be32(V_FW_CMD_OP(FW_PARAMS_CMD) |
453 len16 = DIV_ROUND_UP(offsetof(struct fw_params_cmd,
454 param[nparams]), 16);
455 cmd.retval_len16 = cpu_to_be32(V_FW_CMD_LEN16(len16));
456 for (i = 0, p = &cmd.param[0]; i < nparams; i++, p++) {
457 p->mnem = cpu_to_be32(*params++);
458 p->val = cpu_to_be32(*vals++);
460 return t4vf_wr_mbox(adapter, &cmd, sizeof(cmd), NULL);
464 * t4vf_fl_pkt_align - return the fl packet alignment
465 * @adapter: the adapter
467 * T4 has a single field to specify the packing and padding boundary.
468 * T5 onwards has separate fields for this and hence the alignment for
469 * next packet offset is maximum of these two.
471 int t4vf_fl_pkt_align(struct adapter *adapter, u32 sge_control,
474 unsigned int ingpadboundary, ingpackboundary, fl_align, ingpad_shift;
476 /* T4 uses a single control field to specify both the PCIe Padding and
477 * Packing Boundary. T5 introduced the ability to specify these
478 * separately. The actual Ingress Packet Data alignment boundary
479 * within Packed Buffer Mode is the maximum of these two
482 if (CHELSIO_CHIP_VERSION(adapter->params.chip) <= CHELSIO_T5)
483 ingpad_shift = X_INGPADBOUNDARY_SHIFT;
485 ingpad_shift = X_T6_INGPADBOUNDARY_SHIFT;
487 ingpadboundary = 1 << (G_INGPADBOUNDARY(sge_control) + ingpad_shift);
489 fl_align = ingpadboundary;
490 if (!is_t4(adapter->params.chip)) {
491 ingpackboundary = G_INGPACKBOUNDARY(sge_control2);
492 if (ingpackboundary == X_INGPACKBOUNDARY_16B)
493 ingpackboundary = 16;
495 ingpackboundary = 1 << (ingpackboundary +
496 X_INGPACKBOUNDARY_SHIFT);
498 fl_align = max(ingpadboundary, ingpackboundary);
503 unsigned int t4vf_get_pf_from_vf(struct adapter *adapter)
507 whoami = t4_read_reg(adapter, T4VF_PL_BASE_ADDR + A_PL_VF_WHOAMI);
508 return (CHELSIO_CHIP_VERSION(adapter->params.chip) <= CHELSIO_T5 ?
509 G_SOURCEPF(whoami) : G_T6_SOURCEPF(whoami));
513 * t4vf_get_vfres - retrieve VF resource limits
514 * @adapter: the adapter
516 * Retrieves configured resource limits and capabilities for a virtual
517 * function. The results are stored in @adapter->vfres.
519 int t4vf_get_vfres(struct adapter *adapter)
521 struct vf_resources *vfres = &adapter->params.vfres;
522 struct fw_pfvf_cmd cmd, rpl;
527 * Execute PFVF Read command to get VF resource limits; bail out early
528 * with error on command failure.
530 memset(&cmd, 0, sizeof(cmd));
531 cmd.op_to_vfn = cpu_to_be32(V_FW_CMD_OP(FW_PFVF_CMD) |
534 cmd.retval_len16 = cpu_to_be32(FW_LEN16(cmd));
535 v = t4vf_wr_mbox(adapter, &cmd, sizeof(cmd), &rpl);
540 * Extract VF resource limits and return success.
542 word = be32_to_cpu(rpl.niqflint_niq);
543 vfres->niqflint = G_FW_PFVF_CMD_NIQFLINT(word);
544 vfres->niq = G_FW_PFVF_CMD_NIQ(word);
546 word = be32_to_cpu(rpl.type_to_neq);
547 vfres->neq = G_FW_PFVF_CMD_NEQ(word);
548 vfres->pmask = G_FW_PFVF_CMD_PMASK(word);
550 word = be32_to_cpu(rpl.tc_to_nexactf);
551 vfres->tc = G_FW_PFVF_CMD_TC(word);
552 vfres->nvi = G_FW_PFVF_CMD_NVI(word);
553 vfres->nexactf = G_FW_PFVF_CMD_NEXACTF(word);
555 word = be32_to_cpu(rpl.r_caps_to_nethctrl);
556 vfres->r_caps = G_FW_PFVF_CMD_R_CAPS(word);
557 vfres->wx_caps = G_FW_PFVF_CMD_WX_CAPS(word);
558 vfres->nethctrl = G_FW_PFVF_CMD_NETHCTRL(word);
562 static int t4vf_alloc_vi(struct adapter *adapter, int port_id)
564 struct fw_vi_cmd cmd, rpl;
568 * Execute a VI command to allocate Virtual Interface and return its
571 memset(&cmd, 0, sizeof(cmd));
572 cmd.op_to_vfn = cpu_to_be32(V_FW_CMD_OP(FW_VI_CMD) |
576 cmd.alloc_to_len16 = cpu_to_be32(FW_LEN16(cmd) |
578 cmd.portid_pkd = V_FW_VI_CMD_PORTID(port_id);
579 v = t4vf_wr_mbox(adapter, &cmd, sizeof(cmd), &rpl);
582 return G_FW_VI_CMD_VIID(be16_to_cpu(rpl.type_to_viid));
585 int t4vf_port_init(struct adapter *adapter)
587 unsigned int fw_caps = adapter->params.fw_caps_support;
588 struct fw_port_cmd port_cmd, port_rpl;
589 struct fw_vi_cmd vi_cmd, vi_rpl;
590 fw_port_cap32_t pcaps, acaps;
591 enum fw_port_type port_type;
595 for_each_port(adapter, i) {
596 struct port_info *p = adap2pinfo(adapter, i);
599 * If we haven't yet determined if we're talking to Firmware
600 * which knows the new 32-bit Port Caps, it's time to find
601 * out now. This will also tell new Firmware to send us Port
602 * Status Updates using the new 32-bit Port Capabilities
603 * version of the Port Information message.
605 if (fw_caps == FW_CAPS_UNKNOWN) {
608 param = (V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_PFVF) |
610 (FW_PARAMS_PARAM_PFVF_PORT_CAPS32));
612 ret = t4vf_set_params(adapter, 1, ¶m, &val);
613 fw_caps = (ret == 0 ? FW_CAPS32 : FW_CAPS16);
614 adapter->params.fw_caps_support = fw_caps;
617 ret = t4vf_alloc_vi(adapter, p->port_id);
619 dev_err(&pdev->dev, "cannot allocate VI for port %d:"
620 " err=%d\n", p->port_id, ret);
626 * Execute a VI Read command to get our Virtual Interface
627 * information like MAC address, etc.
629 memset(&vi_cmd, 0, sizeof(vi_cmd));
630 vi_cmd.op_to_vfn = cpu_to_be32(V_FW_CMD_OP(FW_VI_CMD) |
633 vi_cmd.alloc_to_len16 = cpu_to_be32(FW_LEN16(vi_cmd));
634 vi_cmd.type_to_viid = cpu_to_be16(V_FW_VI_CMD_VIID(p->viid));
635 ret = t4vf_wr_mbox(adapter, &vi_cmd, sizeof(vi_cmd), &vi_rpl);
636 if (ret != FW_SUCCESS)
639 p->rss_size = G_FW_VI_CMD_RSSSIZE
640 (be16_to_cpu(vi_rpl.norss_rsssize));
641 t4_os_set_hw_addr(adapter, i, vi_rpl.mac);
644 * If we don't have read access to our port information, we're
645 * done now. Else, execute a PORT Read command to get it ...
647 if (!(adapter->params.vfres.r_caps & FW_CMD_CAP_PORT))
650 memset(&port_cmd, 0, sizeof(port_cmd));
651 port_cmd.op_to_portid = cpu_to_be32
652 (V_FW_CMD_OP(FW_PORT_CMD) | F_FW_CMD_REQUEST |
654 V_FW_PORT_CMD_PORTID(p->port_id));
655 port_cmd.action_to_len16 = cpu_to_be32
656 (V_FW_PORT_CMD_ACTION(fw_caps == FW_CAPS16 ?
657 FW_PORT_ACTION_GET_PORT_INFO :
658 FW_PORT_ACTION_GET_PORT_INFO32) |
660 ret = t4vf_wr_mbox(adapter, &port_cmd, sizeof(port_cmd),
662 if (ret != FW_SUCCESS)
666 * Extract the various fields from the Port Information message.
668 if (fw_caps == FW_CAPS16) {
669 u32 lstatus = be32_to_cpu
670 (port_rpl.u.info.lstatus_to_modtype);
672 port_type = G_FW_PORT_CMD_PTYPE(lstatus);
673 mdio_addr = ((lstatus & F_FW_PORT_CMD_MDIOCAP) ?
674 (int)G_FW_PORT_CMD_MDIOADDR(lstatus) :
676 pcaps = fwcaps16_to_caps32
677 (be16_to_cpu(port_rpl.u.info.pcap));
678 acaps = fwcaps16_to_caps32
679 (be16_to_cpu(port_rpl.u.info.acap));
681 u32 lstatus32 = be32_to_cpu
682 (port_rpl.u.info32.lstatus32_to_cbllen32);
684 port_type = G_FW_PORT_CMD_PORTTYPE32(lstatus32);
685 mdio_addr = ((lstatus32 & F_FW_PORT_CMD_MDIOCAP32) ?
686 (int)G_FW_PORT_CMD_MDIOADDR32(lstatus32) :
688 pcaps = be32_to_cpu(port_rpl.u.info32.pcaps32);
689 acaps = be32_to_cpu(port_rpl.u.info32.acaps32);
692 p->port_type = port_type;
693 p->mdio_addr = mdio_addr;
694 p->mod_type = FW_PORT_MOD_TYPE_NA;
695 init_link_config(&p->link_cfg, pcaps, acaps);