1 /* SPDX-License-Identifier: (BSD-3-Clause OR GPL-2.0)
2 * Copyright(c) 2018-2019 Pensando Systems, Inc. All rights reserved.
7 #include <rte_malloc.h>
10 #include "ionic_lif.h"
14 ionic_dev_setup(struct ionic_adapter *adapter)
16 struct ionic_dev_bar *bar = adapter->bars;
17 unsigned int num_bars = adapter->num_bars;
18 struct ionic_dev *idev = &adapter->idev;
23 /* BAR0: dev_cmd and interrupts */
25 IONIC_PRINT(ERR, "No bars found, aborting");
29 if (bar->len < IONIC_BAR0_SIZE) {
31 "Resource bar size %lu too small, aborting",
36 bar0_base = bar->vaddr;
37 idev->dev_info = (union ionic_dev_info_regs *)
38 &bar0_base[IONIC_BAR0_DEV_INFO_REGS_OFFSET];
39 idev->dev_cmd = (union ionic_dev_cmd_regs *)
40 &bar0_base[IONIC_BAR0_DEV_CMD_REGS_OFFSET];
41 idev->intr_status = (struct ionic_intr_status *)
42 &bar0_base[IONIC_BAR0_INTR_STATUS_OFFSET];
43 idev->intr_ctrl = (struct ionic_intr *)
44 &bar0_base[IONIC_BAR0_INTR_CTRL_OFFSET];
46 sig = ioread32(&idev->dev_info->signature);
47 if (sig != IONIC_DEV_INFO_SIGNATURE) {
48 IONIC_PRINT(ERR, "Incompatible firmware signature %" PRIx32 "",
53 for (i = 0; i < IONIC_DEVINFO_FWVERS_BUFLEN; i++)
54 adapter->fw_version[i] =
55 ioread8(&idev->dev_info->fw_version[i]);
56 adapter->fw_version[IONIC_DEVINFO_FWVERS_BUFLEN - 1] = '\0';
58 IONIC_PRINT(DEBUG, "Firmware version: %s", adapter->fw_version);
63 IONIC_PRINT(ERR, "Doorbell bar missing, aborting");
67 idev->db_pages = bar->vaddr;
68 idev->phy_db_pages = bar->bus_addr;
73 /* Devcmd Interface */
76 ionic_dev_cmd_status(struct ionic_dev *idev)
78 return ioread8(&idev->dev_cmd->comp.comp.status);
82 ionic_dev_cmd_done(struct ionic_dev *idev)
84 return ioread32(&idev->dev_cmd->done) & IONIC_DEV_CMD_DONE;
88 ionic_dev_cmd_comp(struct ionic_dev *idev, void *mem)
90 union ionic_dev_cmd_comp *comp = mem;
92 uint32_t comp_size = sizeof(comp->words) /
93 sizeof(comp->words[0]);
95 for (i = 0; i < comp_size; i++)
96 comp->words[i] = ioread32(&idev->dev_cmd->comp.words[i]);
100 ionic_dev_cmd_go(struct ionic_dev *idev, union ionic_dev_cmd *cmd)
103 uint32_t cmd_size = sizeof(cmd->words) /
104 sizeof(cmd->words[0]);
106 for (i = 0; i < cmd_size; i++)
107 iowrite32(cmd->words[i], &idev->dev_cmd->cmd.words[i]);
109 iowrite32(0, &idev->dev_cmd->done);
110 iowrite32(1, &idev->dev_cmd->doorbell);
113 /* Device commands */
116 ionic_dev_cmd_identify(struct ionic_dev *idev, uint8_t ver)
118 union ionic_dev_cmd cmd = {
119 .identify.opcode = IONIC_CMD_IDENTIFY,
123 ionic_dev_cmd_go(idev, &cmd);
127 ionic_dev_cmd_init(struct ionic_dev *idev)
129 union ionic_dev_cmd cmd = {
130 .init.opcode = IONIC_CMD_INIT,
134 ionic_dev_cmd_go(idev, &cmd);
138 ionic_dev_cmd_reset(struct ionic_dev *idev)
140 union ionic_dev_cmd cmd = {
141 .reset.opcode = IONIC_CMD_RESET,
144 ionic_dev_cmd_go(idev, &cmd);
150 ionic_dev_cmd_port_identify(struct ionic_dev *idev)
152 union ionic_dev_cmd cmd = {
153 .port_init.opcode = IONIC_CMD_PORT_IDENTIFY,
154 .port_init.index = 0,
157 ionic_dev_cmd_go(idev, &cmd);
161 ionic_dev_cmd_port_init(struct ionic_dev *idev)
163 union ionic_dev_cmd cmd = {
164 .port_init.opcode = IONIC_CMD_PORT_INIT,
165 .port_init.index = 0,
166 .port_init.info_pa = idev->port_info_pa,
169 ionic_dev_cmd_go(idev, &cmd);
173 ionic_dev_cmd_port_reset(struct ionic_dev *idev)
175 union ionic_dev_cmd cmd = {
176 .port_reset.opcode = IONIC_CMD_PORT_RESET,
177 .port_reset.index = 0,
180 ionic_dev_cmd_go(idev, &cmd);
184 ionic_dev_cmd_port_state(struct ionic_dev *idev, uint8_t state)
186 union ionic_dev_cmd cmd = {
187 .port_setattr.opcode = IONIC_CMD_PORT_SETATTR,
188 .port_setattr.index = 0,
189 .port_setattr.attr = IONIC_PORT_ATTR_STATE,
190 .port_setattr.state = state,
193 ionic_dev_cmd_go(idev, &cmd);
197 ionic_dev_cmd_port_speed(struct ionic_dev *idev, uint32_t speed)
199 union ionic_dev_cmd cmd = {
200 .port_setattr.opcode = IONIC_CMD_PORT_SETATTR,
201 .port_setattr.index = 0,
202 .port_setattr.attr = IONIC_PORT_ATTR_SPEED,
203 .port_setattr.speed = speed,
206 ionic_dev_cmd_go(idev, &cmd);
210 ionic_dev_cmd_port_mtu(struct ionic_dev *idev, uint32_t mtu)
212 union ionic_dev_cmd cmd = {
213 .port_setattr.opcode = IONIC_CMD_PORT_SETATTR,
214 .port_setattr.index = 0,
215 .port_setattr.attr = IONIC_PORT_ATTR_MTU,
216 .port_setattr.mtu = mtu,
219 ionic_dev_cmd_go(idev, &cmd);
223 ionic_dev_cmd_port_autoneg(struct ionic_dev *idev, uint8_t an_enable)
225 union ionic_dev_cmd cmd = {
226 .port_setattr.opcode = IONIC_CMD_PORT_SETATTR,
227 .port_setattr.index = 0,
228 .port_setattr.attr = IONIC_PORT_ATTR_AUTONEG,
229 .port_setattr.an_enable = an_enable,
232 ionic_dev_cmd_go(idev, &cmd);
236 ionic_dev_cmd_port_fec(struct ionic_dev *idev, uint8_t fec_type)
238 union ionic_dev_cmd cmd = {
239 .port_setattr.opcode = IONIC_CMD_PORT_SETATTR,
240 .port_setattr.index = 0,
241 .port_setattr.attr = IONIC_PORT_ATTR_FEC,
242 .port_setattr.fec_type = fec_type,
245 ionic_dev_cmd_go(idev, &cmd);
249 ionic_dev_cmd_port_pause(struct ionic_dev *idev, uint8_t pause_type)
251 union ionic_dev_cmd cmd = {
252 .port_setattr.opcode = IONIC_CMD_PORT_SETATTR,
253 .port_setattr.index = 0,
254 .port_setattr.attr = IONIC_PORT_ATTR_PAUSE,
255 .port_setattr.pause_type = pause_type,
258 ionic_dev_cmd_go(idev, &cmd);
262 ionic_dev_cmd_port_loopback(struct ionic_dev *idev, uint8_t loopback_mode)
264 union ionic_dev_cmd cmd = {
265 .port_setattr.opcode = IONIC_CMD_PORT_SETATTR,
266 .port_setattr.index = 0,
267 .port_setattr.attr = IONIC_PORT_ATTR_LOOPBACK,
268 .port_setattr.loopback_mode = loopback_mode,
271 ionic_dev_cmd_go(idev, &cmd);
277 ionic_dev_cmd_lif_identify(struct ionic_dev *idev, uint8_t type, uint8_t ver)
279 union ionic_dev_cmd cmd = {
280 .lif_identify.opcode = IONIC_CMD_LIF_IDENTIFY,
281 .lif_identify.type = type,
282 .lif_identify.ver = ver,
285 ionic_dev_cmd_go(idev, &cmd);
289 ionic_dev_cmd_lif_init(struct ionic_dev *idev, uint16_t lif_index,
292 union ionic_dev_cmd cmd = {
293 .lif_init.opcode = IONIC_CMD_LIF_INIT,
294 .lif_init.index = lif_index,
295 .lif_init.info_pa = info_pa,
298 ionic_dev_cmd_go(idev, &cmd);
302 ionic_dev_cmd_lif_reset(struct ionic_dev *idev, uint16_t lif_index)
304 union ionic_dev_cmd cmd = {
305 .lif_init.opcode = IONIC_CMD_LIF_RESET,
306 .lif_init.index = lif_index,
309 ionic_dev_cmd_go(idev, &cmd);
312 struct ionic_doorbell *
313 ionic_db_map(struct ionic_lif *lif, struct ionic_queue *q)
315 return lif->kern_dbpage + q->hw_type;
319 ionic_db_page_num(struct ionic_lif *lif, int pid)
321 return (lif->index * 0) + pid;
325 ionic_intr_init(struct ionic_dev *idev, struct ionic_intr_info *intr,
328 ionic_intr_clean(idev->intr_ctrl, index);
333 ionic_dev_cmd_adminq_init(struct ionic_dev *idev,
334 struct ionic_qcq *qcq,
335 uint16_t lif_index, uint16_t intr_index)
337 struct ionic_queue *q = &qcq->q;
338 struct ionic_cq *cq = &qcq->cq;
340 union ionic_dev_cmd cmd = {
341 .q_init.opcode = IONIC_CMD_Q_INIT,
342 .q_init.lif_index = lif_index,
343 .q_init.type = q->type,
344 .q_init.index = q->index,
345 .q_init.flags = IONIC_QINIT_F_ENA,
346 .q_init.pid = q->pid,
347 .q_init.intr_index = intr_index,
348 .q_init.ring_size = rte_log2_u32(q->num_descs),
349 .q_init.ring_base = q->base_pa,
350 .q_init.cq_ring_base = cq->base_pa,
353 ionic_dev_cmd_go(idev, &cmd);
357 ionic_cq_init(struct ionic_lif *lif, struct ionic_cq *cq,
358 struct ionic_intr_info *intr,
359 uint32_t num_descs, size_t desc_size)
361 if (desc_size == 0) {
362 IONIC_PRINT(ERR, "Descriptor size is %zu", desc_size);
366 if (!rte_is_power_of_2(num_descs) ||
367 num_descs < IONIC_MIN_RING_DESC ||
368 num_descs > IONIC_MAX_RING_DESC) {
369 IONIC_PRINT(ERR, "%u descriptors (min: %u max: %u)",
370 num_descs, IONIC_MIN_RING_DESC, IONIC_MAX_RING_DESC);
375 cq->bound_intr = intr;
376 cq->num_descs = num_descs;
377 cq->desc_size = desc_size;
385 ionic_cq_map(struct ionic_cq *cq, void *base, rte_iova_t base_pa)
388 cq->base_pa = base_pa;
392 ionic_cq_bind(struct ionic_cq *cq, struct ionic_queue *q)
399 ionic_cq_service(struct ionic_cq *cq, uint32_t work_to_do,
400 ionic_cq_cb cb, void *cb_arg)
402 uint32_t work_done = 0;
407 while (cb(cq, cq->tail_idx, cb_arg)) {
408 cq->tail_idx = (cq->tail_idx + 1) & (cq->num_descs - 1);
409 if (cq->tail_idx == 0)
410 cq->done_color = !cq->done_color;
412 if (++work_done == work_to_do)
420 ionic_q_init(struct ionic_lif *lif, struct ionic_dev *idev,
421 struct ionic_queue *q, uint32_t index, uint32_t num_descs,
422 size_t desc_size, size_t sg_desc_size, uint32_t pid)
426 if (desc_size == 0 || !rte_is_power_of_2(num_descs))
429 ring_size = rte_log2_u32(num_descs);
431 if (ring_size < 2 || ring_size > 16)
437 q->num_descs = num_descs;
438 q->desc_size = desc_size;
439 q->sg_desc_size = sg_desc_size;
448 ionic_q_map(struct ionic_queue *q, void *base, rte_iova_t base_pa)
451 q->base_pa = base_pa;
455 ionic_q_sg_map(struct ionic_queue *q, void *base, rte_iova_t base_pa)
458 q->sg_base_pa = base_pa;
462 ionic_q_flush(struct ionic_queue *q)
464 writeq(IONIC_DBELL_QID(q->hw_index) | q->head_idx, q->db);
468 ionic_q_post(struct ionic_queue *q, bool ring_doorbell, desc_cb cb,
471 struct ionic_desc_info *head = &q->info[q->head_idx];
474 head->cb_arg = cb_arg;
476 q->head_idx = (q->head_idx + 1) & (q->num_descs - 1);
483 ionic_q_space_avail(struct ionic_queue *q)
485 uint32_t avail = q->tail_idx;
487 if (q->head_idx >= avail)
488 avail += q->num_descs - q->head_idx - 1;
490 avail -= q->head_idx + 1;
496 ionic_q_has_space(struct ionic_queue *q, uint32_t want)
498 return ionic_q_space_avail(q) >= want;
502 ionic_q_service(struct ionic_queue *q, uint32_t cq_desc_index,
503 uint32_t stop_index, void *service_cb_arg)
505 struct ionic_desc_info *desc_info;
506 uint32_t curr_q_tail_idx;
509 desc_info = &q->info[q->tail_idx];
512 desc_info->cb(q, q->tail_idx, cq_desc_index,
513 desc_info->cb_arg, service_cb_arg);
515 desc_info->cb = NULL;
516 desc_info->cb_arg = NULL;
518 curr_q_tail_idx = q->tail_idx;
519 q->tail_idx = (q->tail_idx + 1) & (q->num_descs - 1);
521 } while (curr_q_tail_idx != stop_index);
525 ionic_adminq_cb(struct ionic_queue *q,
526 uint32_t q_desc_index, uint32_t cq_desc_index,
527 void *cb_arg, void *service_cb_arg __rte_unused)
529 struct ionic_admin_ctx *ctx = cb_arg;
530 struct ionic_admin_comp *cq_desc_base = q->bound_cq->base;
531 struct ionic_admin_comp *cq_desc = &cq_desc_base[cq_desc_index];
533 if (unlikely(cq_desc->comp_index != q_desc_index)) {
534 IONIC_WARN_ON(cq_desc->comp_index != q_desc_index);
538 memcpy(&ctx->comp, cq_desc, sizeof(*cq_desc));
540 ctx->pending_work = false; /* done */
543 /** ionic_adminq_post - Post an admin command.
544 * @lif: Handle to lif.
545 * @cmd_ctx: Api admin command context.
547 * Post the command to an admin queue in the ethernet driver. If this command
548 * succeeds, then the command has been posted, but that does not indicate a
549 * completion. If this command returns success, then the completion callback
550 * will eventually be called.
552 * Return: zero or negative error status.
555 ionic_adminq_post(struct ionic_lif *lif, struct ionic_admin_ctx *ctx)
557 struct ionic_queue *adminq = &lif->adminqcq->q;
558 struct ionic_admin_cmd *q_desc_base = adminq->base;
559 struct ionic_admin_cmd *q_desc;
562 rte_spinlock_lock(&lif->adminq_lock);
564 if (!ionic_q_has_space(adminq, 1)) {
569 q_desc = &q_desc_base[adminq->head_idx];
571 memcpy(q_desc, &ctx->cmd, sizeof(ctx->cmd));
573 ionic_q_post(adminq, true, ionic_adminq_cb, ctx);
576 rte_spinlock_unlock(&lif->adminq_lock);