1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 2017 - 2019 Pensando Systems, Inc */
4 #include <linux/kernel.h>
5 #include <linux/types.h>
6 #include <linux/errno.h>
8 #include <linux/slab.h>
9 #include <linux/etherdevice.h>
11 #include "ionic_dev.h"
12 #include "ionic_lif.h"
14 static void ionic_watchdog_cb(struct timer_list *t)
16 struct ionic *ionic = from_timer(ionic, t, watchdog_timer);
19 mod_timer(&ionic->watchdog_timer,
20 round_jiffies(jiffies + ionic->watchdog_period));
25 hb = ionic_heartbeat_check(ionic);
28 ionic_link_status_check_request(ionic->lif, false);
31 void ionic_init_devinfo(struct ionic *ionic)
33 struct ionic_dev *idev = &ionic->idev;
35 idev->dev_info.asic_type = ioread8(&idev->dev_info_regs->asic_type);
36 idev->dev_info.asic_rev = ioread8(&idev->dev_info_regs->asic_rev);
38 memcpy_fromio(idev->dev_info.fw_version,
39 idev->dev_info_regs->fw_version,
40 IONIC_DEVINFO_FWVERS_BUFLEN);
42 memcpy_fromio(idev->dev_info.serial_num,
43 idev->dev_info_regs->serial_num,
44 IONIC_DEVINFO_SERIAL_BUFLEN);
46 idev->dev_info.fw_version[IONIC_DEVINFO_FWVERS_BUFLEN] = 0;
47 idev->dev_info.serial_num[IONIC_DEVINFO_SERIAL_BUFLEN] = 0;
49 dev_dbg(ionic->dev, "fw_version %s\n", idev->dev_info.fw_version);
52 int ionic_dev_setup(struct ionic *ionic)
54 struct ionic_dev_bar *bar = ionic->bars;
55 unsigned int num_bars = ionic->num_bars;
56 struct ionic_dev *idev = &ionic->idev;
57 struct device *dev = ionic->dev;
60 /* BAR0: dev_cmd and interrupts */
62 dev_err(dev, "No bars found, aborting\n");
66 if (bar->len < IONIC_BAR0_SIZE) {
67 dev_err(dev, "Resource bar size %lu too small, aborting\n",
72 idev->dev_info_regs = bar->vaddr + IONIC_BAR0_DEV_INFO_REGS_OFFSET;
73 idev->dev_cmd_regs = bar->vaddr + IONIC_BAR0_DEV_CMD_REGS_OFFSET;
74 idev->intr_status = bar->vaddr + IONIC_BAR0_INTR_STATUS_OFFSET;
75 idev->intr_ctrl = bar->vaddr + IONIC_BAR0_INTR_CTRL_OFFSET;
77 sig = ioread32(&idev->dev_info_regs->signature);
78 if (sig != IONIC_DEV_INFO_SIGNATURE) {
79 dev_err(dev, "Incompatible firmware signature %x", sig);
83 ionic_init_devinfo(ionic);
88 dev_err(dev, "Doorbell bar missing, aborting\n");
92 idev->last_fw_status = 0xff;
93 timer_setup(&ionic->watchdog_timer, ionic_watchdog_cb, 0);
94 ionic->watchdog_period = IONIC_WATCHDOG_SECS * HZ;
95 mod_timer(&ionic->watchdog_timer,
96 round_jiffies(jiffies + ionic->watchdog_period));
98 idev->db_pages = bar->vaddr;
99 idev->phy_db_pages = bar->bus_addr;
104 /* Devcmd Interface */
105 int ionic_heartbeat_check(struct ionic *ionic)
107 struct ionic_dev *idev = &ionic->idev;
108 unsigned long hb_time;
112 /* wait a little more than one second before testing again */
114 if (time_before(hb_time, (idev->last_hb_time + ionic->watchdog_period)))
117 /* firmware is useful only if the running bit is set and
118 * fw_status != 0xff (bad PCI read)
120 fw_status = ioread8(&idev->dev_info_regs->fw_status);
121 if (fw_status != 0xff)
122 fw_status &= IONIC_FW_STS_F_RUNNING; /* use only the run bit */
124 /* is this a transition? */
125 if (fw_status != idev->last_fw_status &&
126 idev->last_fw_status != 0xff) {
127 struct ionic_lif *lif = ionic->lif;
128 bool trigger = false;
130 if (!fw_status || fw_status == 0xff) {
131 dev_info(ionic->dev, "FW stopped %u\n", fw_status);
132 if (lif && !test_bit(IONIC_LIF_F_FW_RESET, lif->state))
135 dev_info(ionic->dev, "FW running %u\n", fw_status);
136 if (lif && test_bit(IONIC_LIF_F_FW_RESET, lif->state))
141 struct ionic_deferred_work *work;
143 work = kzalloc(sizeof(*work), GFP_ATOMIC);
145 dev_err(ionic->dev, "%s OOM\n", __func__);
147 work->type = IONIC_DW_TYPE_LIF_RESET;
148 if (fw_status & IONIC_FW_STS_F_RUNNING &&
151 ionic_lif_deferred_enqueue(&lif->deferred, work);
155 idev->last_fw_status = fw_status;
157 if (!fw_status || fw_status == 0xff)
160 /* early FW has no heartbeat, else FW will return non-zero */
161 hb = ioread32(&idev->dev_info_regs->fw_heartbeat);
165 /* are we stalled? */
166 if (hb == idev->last_hb) {
167 /* only complain once for each stall seen */
168 if (idev->last_hb_time != 1) {
169 dev_info(ionic->dev, "FW heartbeat stalled at %d\n",
171 idev->last_hb_time = 1;
177 if (idev->last_hb_time == 1)
178 dev_info(ionic->dev, "FW heartbeat restored at %d\n", hb);
181 idev->last_hb_time = hb_time;
186 u8 ionic_dev_cmd_status(struct ionic_dev *idev)
188 return ioread8(&idev->dev_cmd_regs->comp.comp.status);
191 bool ionic_dev_cmd_done(struct ionic_dev *idev)
193 return ioread32(&idev->dev_cmd_regs->done) & IONIC_DEV_CMD_DONE;
196 void ionic_dev_cmd_comp(struct ionic_dev *idev, union ionic_dev_cmd_comp *comp)
198 memcpy_fromio(comp, &idev->dev_cmd_regs->comp, sizeof(*comp));
201 void ionic_dev_cmd_go(struct ionic_dev *idev, union ionic_dev_cmd *cmd)
203 memcpy_toio(&idev->dev_cmd_regs->cmd, cmd, sizeof(*cmd));
204 iowrite32(0, &idev->dev_cmd_regs->done);
205 iowrite32(1, &idev->dev_cmd_regs->doorbell);
208 /* Device commands */
209 void ionic_dev_cmd_identify(struct ionic_dev *idev, u8 ver)
211 union ionic_dev_cmd cmd = {
212 .identify.opcode = IONIC_CMD_IDENTIFY,
216 ionic_dev_cmd_go(idev, &cmd);
219 void ionic_dev_cmd_init(struct ionic_dev *idev)
221 union ionic_dev_cmd cmd = {
222 .init.opcode = IONIC_CMD_INIT,
226 ionic_dev_cmd_go(idev, &cmd);
229 void ionic_dev_cmd_reset(struct ionic_dev *idev)
231 union ionic_dev_cmd cmd = {
232 .reset.opcode = IONIC_CMD_RESET,
235 ionic_dev_cmd_go(idev, &cmd);
239 void ionic_dev_cmd_port_identify(struct ionic_dev *idev)
241 union ionic_dev_cmd cmd = {
242 .port_init.opcode = IONIC_CMD_PORT_IDENTIFY,
243 .port_init.index = 0,
246 ionic_dev_cmd_go(idev, &cmd);
249 void ionic_dev_cmd_port_init(struct ionic_dev *idev)
251 union ionic_dev_cmd cmd = {
252 .port_init.opcode = IONIC_CMD_PORT_INIT,
253 .port_init.index = 0,
254 .port_init.info_pa = cpu_to_le64(idev->port_info_pa),
257 ionic_dev_cmd_go(idev, &cmd);
260 void ionic_dev_cmd_port_reset(struct ionic_dev *idev)
262 union ionic_dev_cmd cmd = {
263 .port_reset.opcode = IONIC_CMD_PORT_RESET,
264 .port_reset.index = 0,
267 ionic_dev_cmd_go(idev, &cmd);
270 void ionic_dev_cmd_port_state(struct ionic_dev *idev, u8 state)
272 union ionic_dev_cmd cmd = {
273 .port_setattr.opcode = IONIC_CMD_PORT_SETATTR,
274 .port_setattr.index = 0,
275 .port_setattr.attr = IONIC_PORT_ATTR_STATE,
276 .port_setattr.state = state,
279 ionic_dev_cmd_go(idev, &cmd);
282 void ionic_dev_cmd_port_speed(struct ionic_dev *idev, u32 speed)
284 union ionic_dev_cmd cmd = {
285 .port_setattr.opcode = IONIC_CMD_PORT_SETATTR,
286 .port_setattr.index = 0,
287 .port_setattr.attr = IONIC_PORT_ATTR_SPEED,
288 .port_setattr.speed = cpu_to_le32(speed),
291 ionic_dev_cmd_go(idev, &cmd);
294 void ionic_dev_cmd_port_autoneg(struct ionic_dev *idev, u8 an_enable)
296 union ionic_dev_cmd cmd = {
297 .port_setattr.opcode = IONIC_CMD_PORT_SETATTR,
298 .port_setattr.index = 0,
299 .port_setattr.attr = IONIC_PORT_ATTR_AUTONEG,
300 .port_setattr.an_enable = an_enable,
303 ionic_dev_cmd_go(idev, &cmd);
306 void ionic_dev_cmd_port_fec(struct ionic_dev *idev, u8 fec_type)
308 union ionic_dev_cmd cmd = {
309 .port_setattr.opcode = IONIC_CMD_PORT_SETATTR,
310 .port_setattr.index = 0,
311 .port_setattr.attr = IONIC_PORT_ATTR_FEC,
312 .port_setattr.fec_type = fec_type,
315 ionic_dev_cmd_go(idev, &cmd);
318 void ionic_dev_cmd_port_pause(struct ionic_dev *idev, u8 pause_type)
320 union ionic_dev_cmd cmd = {
321 .port_setattr.opcode = IONIC_CMD_PORT_SETATTR,
322 .port_setattr.index = 0,
323 .port_setattr.attr = IONIC_PORT_ATTR_PAUSE,
324 .port_setattr.pause_type = pause_type,
327 ionic_dev_cmd_go(idev, &cmd);
331 int ionic_set_vf_config(struct ionic *ionic, int vf, u8 attr, u8 *data)
333 union ionic_dev_cmd cmd = {
334 .vf_setattr.opcode = IONIC_CMD_VF_SETATTR,
335 .vf_setattr.attr = attr,
336 .vf_setattr.vf_index = cpu_to_le16(vf),
341 case IONIC_VF_ATTR_SPOOFCHK:
342 cmd.vf_setattr.spoofchk = *data;
343 dev_dbg(ionic->dev, "%s: vf %d spoof %d\n",
344 __func__, vf, *data);
346 case IONIC_VF_ATTR_TRUST:
347 cmd.vf_setattr.trust = *data;
348 dev_dbg(ionic->dev, "%s: vf %d trust %d\n",
349 __func__, vf, *data);
351 case IONIC_VF_ATTR_LINKSTATE:
352 cmd.vf_setattr.linkstate = *data;
353 dev_dbg(ionic->dev, "%s: vf %d linkstate %d\n",
354 __func__, vf, *data);
356 case IONIC_VF_ATTR_MAC:
357 ether_addr_copy(cmd.vf_setattr.macaddr, data);
358 dev_dbg(ionic->dev, "%s: vf %d macaddr %pM\n",
361 case IONIC_VF_ATTR_VLAN:
362 cmd.vf_setattr.vlanid = cpu_to_le16(*(u16 *)data);
363 dev_dbg(ionic->dev, "%s: vf %d vlan %d\n",
364 __func__, vf, *(u16 *)data);
366 case IONIC_VF_ATTR_RATE:
367 cmd.vf_setattr.maxrate = cpu_to_le32(*(u32 *)data);
368 dev_dbg(ionic->dev, "%s: vf %d maxrate %d\n",
369 __func__, vf, *(u32 *)data);
371 case IONIC_VF_ATTR_STATSADDR:
372 cmd.vf_setattr.stats_pa = cpu_to_le64(*(u64 *)data);
373 dev_dbg(ionic->dev, "%s: vf %d stats_pa 0x%08llx\n",
374 __func__, vf, *(u64 *)data);
380 mutex_lock(&ionic->dev_cmd_lock);
381 ionic_dev_cmd_go(&ionic->idev, &cmd);
382 err = ionic_dev_cmd_wait(ionic, DEVCMD_TIMEOUT);
383 mutex_unlock(&ionic->dev_cmd_lock);
389 void ionic_dev_cmd_queue_identify(struct ionic_dev *idev,
390 u16 lif_type, u8 qtype, u8 qver)
392 union ionic_dev_cmd cmd = {
393 .q_identify.opcode = IONIC_CMD_Q_IDENTIFY,
394 .q_identify.lif_type = cpu_to_le16(lif_type),
395 .q_identify.type = qtype,
396 .q_identify.ver = qver,
399 ionic_dev_cmd_go(idev, &cmd);
402 void ionic_dev_cmd_lif_identify(struct ionic_dev *idev, u8 type, u8 ver)
404 union ionic_dev_cmd cmd = {
405 .lif_identify.opcode = IONIC_CMD_LIF_IDENTIFY,
406 .lif_identify.type = type,
407 .lif_identify.ver = ver,
410 ionic_dev_cmd_go(idev, &cmd);
413 void ionic_dev_cmd_lif_init(struct ionic_dev *idev, u16 lif_index,
416 union ionic_dev_cmd cmd = {
417 .lif_init.opcode = IONIC_CMD_LIF_INIT,
418 .lif_init.index = cpu_to_le16(lif_index),
419 .lif_init.info_pa = cpu_to_le64(info_pa),
422 ionic_dev_cmd_go(idev, &cmd);
425 void ionic_dev_cmd_lif_reset(struct ionic_dev *idev, u16 lif_index)
427 union ionic_dev_cmd cmd = {
428 .lif_init.opcode = IONIC_CMD_LIF_RESET,
429 .lif_init.index = cpu_to_le16(lif_index),
432 ionic_dev_cmd_go(idev, &cmd);
435 void ionic_dev_cmd_adminq_init(struct ionic_dev *idev, struct ionic_qcq *qcq,
436 u16 lif_index, u16 intr_index)
438 struct ionic_queue *q = &qcq->q;
439 struct ionic_cq *cq = &qcq->cq;
441 union ionic_dev_cmd cmd = {
442 .q_init.opcode = IONIC_CMD_Q_INIT,
443 .q_init.lif_index = cpu_to_le16(lif_index),
444 .q_init.type = q->type,
445 .q_init.ver = qcq->q.lif->qtype_info[q->type].version,
446 .q_init.index = cpu_to_le32(q->index),
447 .q_init.flags = cpu_to_le16(IONIC_QINIT_F_IRQ |
449 .q_init.pid = cpu_to_le16(q->pid),
450 .q_init.intr_index = cpu_to_le16(intr_index),
451 .q_init.ring_size = ilog2(q->num_descs),
452 .q_init.ring_base = cpu_to_le64(q->base_pa),
453 .q_init.cq_ring_base = cpu_to_le64(cq->base_pa),
456 ionic_dev_cmd_go(idev, &cmd);
459 int ionic_db_page_num(struct ionic_lif *lif, int pid)
461 return (lif->hw_index * lif->dbid_count) + pid;
464 int ionic_cq_init(struct ionic_lif *lif, struct ionic_cq *cq,
465 struct ionic_intr_info *intr,
466 unsigned int num_descs, size_t desc_size)
468 unsigned int ring_size;
470 if (desc_size == 0 || !is_power_of_2(num_descs))
473 ring_size = ilog2(num_descs);
474 if (ring_size < 2 || ring_size > 16)
478 cq->bound_intr = intr;
479 cq->num_descs = num_descs;
480 cq->desc_size = desc_size;
487 void ionic_cq_map(struct ionic_cq *cq, void *base, dma_addr_t base_pa)
489 struct ionic_cq_info *cur;
493 cq->base_pa = base_pa;
495 for (i = 0, cur = cq->info; i < cq->num_descs; i++, cur++)
496 cur->cq_desc = base + (i * cq->desc_size);
499 void ionic_cq_bind(struct ionic_cq *cq, struct ionic_queue *q)
504 unsigned int ionic_cq_service(struct ionic_cq *cq, unsigned int work_to_do,
505 ionic_cq_cb cb, ionic_cq_done_cb done_cb,
508 struct ionic_cq_info *cq_info;
509 unsigned int work_done = 0;
514 cq_info = &cq->info[cq->tail_idx];
515 while (cb(cq, cq_info)) {
516 if (cq->tail_idx == cq->num_descs - 1)
517 cq->done_color = !cq->done_color;
518 cq->tail_idx = (cq->tail_idx + 1) & (cq->num_descs - 1);
519 cq_info = &cq->info[cq->tail_idx];
520 DEBUG_STATS_CQE_CNT(cq);
522 if (++work_done >= work_to_do)
526 if (work_done && done_cb)
532 int ionic_q_init(struct ionic_lif *lif, struct ionic_dev *idev,
533 struct ionic_queue *q, unsigned int index, const char *name,
534 unsigned int num_descs, size_t desc_size,
535 size_t sg_desc_size, unsigned int pid)
537 unsigned int ring_size;
539 if (desc_size == 0 || !is_power_of_2(num_descs))
542 ring_size = ilog2(num_descs);
543 if (ring_size < 2 || ring_size > 16)
549 q->num_descs = num_descs;
550 q->desc_size = desc_size;
551 q->sg_desc_size = sg_desc_size;
556 snprintf(q->name, sizeof(q->name), "L%d-%s%u", lif->index, name, index);
561 void ionic_q_map(struct ionic_queue *q, void *base, dma_addr_t base_pa)
563 struct ionic_desc_info *cur;
567 q->base_pa = base_pa;
569 for (i = 0, cur = q->info; i < q->num_descs; i++, cur++)
570 cur->desc = base + (i * q->desc_size);
573 void ionic_q_sg_map(struct ionic_queue *q, void *base, dma_addr_t base_pa)
575 struct ionic_desc_info *cur;
579 q->sg_base_pa = base_pa;
581 for (i = 0, cur = q->info; i < q->num_descs; i++, cur++)
582 cur->sg_desc = base + (i * q->sg_desc_size);
585 void ionic_q_post(struct ionic_queue *q, bool ring_doorbell, ionic_desc_cb cb,
588 struct device *dev = q->lif->ionic->dev;
589 struct ionic_desc_info *desc_info;
590 struct ionic_lif *lif = q->lif;
592 desc_info = &q->info[q->head_idx];
594 desc_info->cb_arg = cb_arg;
596 q->head_idx = (q->head_idx + 1) & (q->num_descs - 1);
598 dev_dbg(dev, "lif=%d qname=%s qid=%d qtype=%d p_index=%d ringdb=%d\n",
599 q->lif->index, q->name, q->hw_type, q->hw_index,
600 q->head_idx, ring_doorbell);
603 ionic_dbell_ring(lif->kern_dbpage, q->hw_type,
604 q->dbval | q->head_idx);
607 static bool ionic_q_is_posted(struct ionic_queue *q, unsigned int pos)
609 unsigned int mask, tail, head;
611 mask = q->num_descs - 1;
615 return ((pos - tail) & mask) < ((head - tail) & mask);
618 void ionic_q_service(struct ionic_queue *q, struct ionic_cq_info *cq_info,
619 unsigned int stop_index)
621 struct ionic_desc_info *desc_info;
626 /* check for empty queue */
627 if (q->tail_idx == q->head_idx)
630 /* stop index must be for a descriptor that is not yet completed */
631 if (unlikely(!ionic_q_is_posted(q, stop_index)))
632 dev_err(q->lif->ionic->dev,
633 "ionic stop is not posted %s stop %u tail %u head %u\n",
634 q->name, stop_index, q->tail_idx, q->head_idx);
637 desc_info = &q->info[q->tail_idx];
639 q->tail_idx = (q->tail_idx + 1) & (q->num_descs - 1);
642 cb_arg = desc_info->cb_arg;
644 desc_info->cb = NULL;
645 desc_info->cb_arg = NULL;
648 cb(q, desc_info, cq_info, cb_arg);
649 } while (index != stop_index);