xref: /linux/drivers/net/ethernet/pensando/ionic/ionic_dev.c (revision a1ff5a7d78a036d6c2178ee5acd6ba4946243800)
1fbfb8031SShannon Nelson // SPDX-License-Identifier: GPL-2.0
2fbfb8031SShannon Nelson /* Copyright(c) 2017 - 2019 Pensando Systems, Inc */
3fbfb8031SShannon Nelson 
4fbfb8031SShannon Nelson #include <linux/kernel.h>
5fbfb8031SShannon Nelson #include <linux/types.h>
6fbfb8031SShannon Nelson #include <linux/errno.h>
7fbfb8031SShannon Nelson #include <linux/io.h>
8fbfb8031SShannon Nelson #include <linux/slab.h>
9fbfb8031SShannon Nelson #include <linux/etherdevice.h>
10fbfb8031SShannon Nelson #include "ionic.h"
11fbfb8031SShannon Nelson #include "ionic_dev.h"
126461b446SShannon Nelson #include "ionic_lif.h"
13fbfb8031SShannon Nelson 
ionic_watchdog_cb(struct timer_list * t)14089406bcSShannon Nelson static void ionic_watchdog_cb(struct timer_list *t)
15089406bcSShannon Nelson {
16089406bcSShannon Nelson 	struct ionic *ionic = from_timer(ionic, t, watchdog_timer);
178c775344SShannon Nelson 	struct ionic_lif *lif = ionic->lif;
18969f8439SShannon Nelson 	struct ionic_deferred_work *work;
19987c0871SShannon Nelson 	int hb;
20089406bcSShannon Nelson 
21089406bcSShannon Nelson 	mod_timer(&ionic->watchdog_timer,
22089406bcSShannon Nelson 		  round_jiffies(jiffies + ionic->watchdog_period));
23089406bcSShannon Nelson 
248c775344SShannon Nelson 	if (!lif)
250816e0c8SShannon Nelson 		return;
260816e0c8SShannon Nelson 
27987c0871SShannon Nelson 	hb = ionic_heartbeat_check(ionic);
28b2b9a8d7SShannon Nelson 	dev_dbg(ionic->dev, "%s: hb %d running %d UP %d\n",
29b2b9a8d7SShannon Nelson 		__func__, hb, netif_running(lif->netdev),
30b2b9a8d7SShannon Nelson 		test_bit(IONIC_LIF_F_UP, lif->state));
31987c0871SShannon Nelson 
328c775344SShannon Nelson 	if (hb >= 0 &&
338c775344SShannon Nelson 	    !test_bit(IONIC_LIF_F_FW_RESET, lif->state))
348c775344SShannon Nelson 		ionic_link_status_check_request(lif, CAN_NOT_SLEEP);
35969f8439SShannon Nelson 
36abd75d14SShannon Nelson 	if (test_bit(IONIC_LIF_F_FILTER_SYNC_NEEDED, lif->state) &&
37abd75d14SShannon Nelson 	    !test_bit(IONIC_LIF_F_FW_RESET, lif->state)) {
38969f8439SShannon Nelson 		work = kzalloc(sizeof(*work), GFP_ATOMIC);
39969f8439SShannon Nelson 		if (!work) {
40969f8439SShannon Nelson 			netdev_err(lif->netdev, "rxmode change dropped\n");
41969f8439SShannon Nelson 			return;
42969f8439SShannon Nelson 		}
43969f8439SShannon Nelson 
44969f8439SShannon Nelson 		work->type = IONIC_DW_TYPE_RX_MODE;
45969f8439SShannon Nelson 		netdev_dbg(lif->netdev, "deferred: rx_mode\n");
469e25450dSShannon Nelson 		ionic_lif_deferred_enqueue(lif, work);
47969f8439SShannon Nelson 	}
48089406bcSShannon Nelson }
49089406bcSShannon Nelson 
ionic_napi_schedule_do_softirq(struct napi_struct * napi)504ded136cSShannon Nelson static void ionic_napi_schedule_do_softirq(struct napi_struct *napi)
514ded136cSShannon Nelson {
524ded136cSShannon Nelson 	local_bh_disable();
534ded136cSShannon Nelson 	napi_schedule(napi);
544ded136cSShannon Nelson 	local_bh_enable();
554ded136cSShannon Nelson }
564ded136cSShannon Nelson 
ionic_doorbell_napi_work(struct work_struct * work)57d7f9bc68SShannon Nelson void ionic_doorbell_napi_work(struct work_struct *work)
58d7f9bc68SShannon Nelson {
59d7f9bc68SShannon Nelson 	struct ionic_qcq *qcq = container_of(work, struct ionic_qcq,
60d7f9bc68SShannon Nelson 					     doorbell_napi_work);
6155a3982eSShannon Nelson 	unsigned long now, then, dif;
6255a3982eSShannon Nelson 
6355a3982eSShannon Nelson 	now = READ_ONCE(jiffies);
6455a3982eSShannon Nelson 	then = qcq->q.dbell_jiffies;
6555a3982eSShannon Nelson 	dif = now - then;
6655a3982eSShannon Nelson 
6755a3982eSShannon Nelson 	if (dif > qcq->q.dbell_deadline)
68d7f9bc68SShannon Nelson 		ionic_napi_schedule_do_softirq(&qcq->napi);
69d7f9bc68SShannon Nelson }
70d7f9bc68SShannon Nelson 
ionic_get_preferred_cpu(struct ionic * ionic,struct ionic_intr_info * intr)714ded136cSShannon Nelson static int ionic_get_preferred_cpu(struct ionic *ionic,
724ded136cSShannon Nelson 				   struct ionic_intr_info *intr)
734ded136cSShannon Nelson {
744ded136cSShannon Nelson 	int cpu;
754ded136cSShannon Nelson 
764ded136cSShannon Nelson 	cpu = cpumask_first_and(*intr->affinity_mask, cpu_online_mask);
774ded136cSShannon Nelson 	if (cpu >= nr_cpu_ids)
784ded136cSShannon Nelson 		cpu = cpumask_local_spread(0, dev_to_node(ionic->dev));
794ded136cSShannon Nelson 
804ded136cSShannon Nelson 	return cpu;
814ded136cSShannon Nelson }
824ded136cSShannon Nelson 
ionic_queue_dbell_napi_work(struct ionic * ionic,struct ionic_qcq * qcq)83d7f9bc68SShannon Nelson static void ionic_queue_dbell_napi_work(struct ionic *ionic,
84d7f9bc68SShannon Nelson 					struct ionic_qcq *qcq)
85d7f9bc68SShannon Nelson {
86d7f9bc68SShannon Nelson 	int cpu;
87d7f9bc68SShannon Nelson 
88d7f9bc68SShannon Nelson 	if (!(qcq->flags & IONIC_QCQ_F_INTR))
89d7f9bc68SShannon Nelson 		return;
90d7f9bc68SShannon Nelson 
91d7f9bc68SShannon Nelson 	cpu = ionic_get_preferred_cpu(ionic, &qcq->intr);
92d7f9bc68SShannon Nelson 	queue_work_on(cpu, ionic->wq, &qcq->doorbell_napi_work);
93d7f9bc68SShannon Nelson }
94d7f9bc68SShannon Nelson 
ionic_doorbell_check_dwork(struct work_struct * work)954ded136cSShannon Nelson static void ionic_doorbell_check_dwork(struct work_struct *work)
964ded136cSShannon Nelson {
974ded136cSShannon Nelson 	struct ionic *ionic = container_of(work, struct ionic,
984ded136cSShannon Nelson 					   doorbell_check_dwork.work);
994ded136cSShannon Nelson 	struct ionic_lif *lif = ionic->lif;
1004ded136cSShannon Nelson 
1014ded136cSShannon Nelson 	mutex_lock(&lif->queue_lock);
1024ded136cSShannon Nelson 
1034ded136cSShannon Nelson 	if (test_bit(IONIC_LIF_F_FW_STOPPING, lif->state) ||
1044ded136cSShannon Nelson 	    test_bit(IONIC_LIF_F_FW_RESET, lif->state)) {
1054ded136cSShannon Nelson 		mutex_unlock(&lif->queue_lock);
1064ded136cSShannon Nelson 		return;
1074ded136cSShannon Nelson 	}
1084ded136cSShannon Nelson 
1094ded136cSShannon Nelson 	ionic_napi_schedule_do_softirq(&lif->adminqcq->napi);
1104ded136cSShannon Nelson 
1114ded136cSShannon Nelson 	if (test_bit(IONIC_LIF_F_UP, lif->state)) {
1124ded136cSShannon Nelson 		int i;
1134ded136cSShannon Nelson 
1144ded136cSShannon Nelson 		for (i = 0; i < lif->nxqs; i++) {
115d7f9bc68SShannon Nelson 			ionic_queue_dbell_napi_work(ionic, lif->txqcqs[i]);
116d7f9bc68SShannon Nelson 			ionic_queue_dbell_napi_work(ionic, lif->rxqcqs[i]);
1174ded136cSShannon Nelson 		}
1184ded136cSShannon Nelson 
1194ded136cSShannon Nelson 		if (lif->hwstamp_txq &&
1204ded136cSShannon Nelson 		    lif->hwstamp_txq->flags & IONIC_QCQ_F_INTR)
1214ded136cSShannon Nelson 			ionic_napi_schedule_do_softirq(&lif->hwstamp_txq->napi);
1224ded136cSShannon Nelson 		if (lif->hwstamp_rxq &&
1234ded136cSShannon Nelson 		    lif->hwstamp_rxq->flags & IONIC_QCQ_F_INTR)
1244ded136cSShannon Nelson 			ionic_napi_schedule_do_softirq(&lif->hwstamp_rxq->napi);
1254ded136cSShannon Nelson 	}
1264ded136cSShannon Nelson 	mutex_unlock(&lif->queue_lock);
1274ded136cSShannon Nelson 
1284ded136cSShannon Nelson 	ionic_queue_doorbell_check(ionic, IONIC_NAPI_DEADLINE);
1294ded136cSShannon Nelson }
1304ded136cSShannon Nelson 
ionic_doorbell_wa(struct ionic * ionic)131*da0262c2SBrett Creeley bool ionic_doorbell_wa(struct ionic *ionic)
132*da0262c2SBrett Creeley {
133*da0262c2SBrett Creeley 	u8 asic_type = ionic->idev.dev_info.asic_type;
134*da0262c2SBrett Creeley 
135*da0262c2SBrett Creeley 	return !asic_type || asic_type == IONIC_ASIC_TYPE_ELBA;
136*da0262c2SBrett Creeley }
137*da0262c2SBrett Creeley 
ionic_watchdog_init(struct ionic * ionic)1389e25450dSShannon Nelson static int ionic_watchdog_init(struct ionic *ionic)
139e6958cefSShannon Nelson {
140e6958cefSShannon Nelson 	struct ionic_dev *idev = &ionic->idev;
141e6958cefSShannon Nelson 
142e6958cefSShannon Nelson 	timer_setup(&ionic->watchdog_timer, ionic_watchdog_cb, 0);
143e6958cefSShannon Nelson 	ionic->watchdog_period = IONIC_WATCHDOG_SECS * HZ;
144e6958cefSShannon Nelson 
145e6958cefSShannon Nelson 	/* set times to ensure the first check will proceed */
146e6958cefSShannon Nelson 	atomic_long_set(&idev->last_check_time, jiffies - 2 * HZ);
147e6958cefSShannon Nelson 	idev->last_hb_time = jiffies - 2 * ionic->watchdog_period;
148e6958cefSShannon Nelson 	/* init as ready, so no transition if the first check succeeds */
149e6958cefSShannon Nelson 	idev->last_fw_hb = 0;
150e6958cefSShannon Nelson 	idev->fw_hb_ready = true;
151e6958cefSShannon Nelson 	idev->fw_status_ready = true;
152e6958cefSShannon Nelson 	idev->fw_generation = IONIC_FW_STS_F_GENERATION &
153e6958cefSShannon Nelson 			      ioread8(&idev->dev_info_regs->fw_status);
1549e25450dSShannon Nelson 
1559e25450dSShannon Nelson 	ionic->wq = alloc_workqueue("%s-wq", WQ_UNBOUND, 0,
1569e25450dSShannon Nelson 				    dev_name(ionic->dev));
1579e25450dSShannon Nelson 	if (!ionic->wq) {
1589e25450dSShannon Nelson 		dev_err(ionic->dev, "alloc_workqueue failed");
1599e25450dSShannon Nelson 		return -ENOMEM;
1609e25450dSShannon Nelson 	}
161*da0262c2SBrett Creeley 
162*da0262c2SBrett Creeley 	if (ionic_doorbell_wa(ionic))
1634ded136cSShannon Nelson 		INIT_DELAYED_WORK(&ionic->doorbell_check_dwork,
1644ded136cSShannon Nelson 				  ionic_doorbell_check_dwork);
1659e25450dSShannon Nelson 
1669e25450dSShannon Nelson 	return 0;
167e6958cefSShannon Nelson }
168e6958cefSShannon Nelson 
ionic_queue_doorbell_check(struct ionic * ionic,int delay)1694ded136cSShannon Nelson void ionic_queue_doorbell_check(struct ionic *ionic, int delay)
1704ded136cSShannon Nelson {
1714ded136cSShannon Nelson 	int cpu;
1724ded136cSShannon Nelson 
173*da0262c2SBrett Creeley 	if (!ionic->lif->doorbell_wa)
174*da0262c2SBrett Creeley 		return;
175*da0262c2SBrett Creeley 
1764ded136cSShannon Nelson 	cpu = ionic_get_preferred_cpu(ionic, &ionic->lif->adminqcq->intr);
1774ded136cSShannon Nelson 	queue_delayed_work_on(cpu, ionic->wq, &ionic->doorbell_check_dwork,
1784ded136cSShannon Nelson 			      delay);
1794ded136cSShannon Nelson }
1804ded136cSShannon Nelson 
ionic_init_devinfo(struct ionic * ionic)181fbfb8031SShannon Nelson void ionic_init_devinfo(struct ionic *ionic)
182fbfb8031SShannon Nelson {
183fbfb8031SShannon Nelson 	struct ionic_dev *idev = &ionic->idev;
184fbfb8031SShannon Nelson 
185fbfb8031SShannon Nelson 	idev->dev_info.asic_type = ioread8(&idev->dev_info_regs->asic_type);
186fbfb8031SShannon Nelson 	idev->dev_info.asic_rev = ioread8(&idev->dev_info_regs->asic_rev);
187fbfb8031SShannon Nelson 
188fbfb8031SShannon Nelson 	memcpy_fromio(idev->dev_info.fw_version,
189fbfb8031SShannon Nelson 		      idev->dev_info_regs->fw_version,
190fbfb8031SShannon Nelson 		      IONIC_DEVINFO_FWVERS_BUFLEN);
191fbfb8031SShannon Nelson 
192fbfb8031SShannon Nelson 	memcpy_fromio(idev->dev_info.serial_num,
193fbfb8031SShannon Nelson 		      idev->dev_info_regs->serial_num,
194fbfb8031SShannon Nelson 		      IONIC_DEVINFO_SERIAL_BUFLEN);
195fbfb8031SShannon Nelson 
196fbfb8031SShannon Nelson 	idev->dev_info.fw_version[IONIC_DEVINFO_FWVERS_BUFLEN] = 0;
197fbfb8031SShannon Nelson 	idev->dev_info.serial_num[IONIC_DEVINFO_SERIAL_BUFLEN] = 0;
198fbfb8031SShannon Nelson 
199fbfb8031SShannon Nelson 	dev_dbg(ionic->dev, "fw_version %s\n", idev->dev_info.fw_version);
200fbfb8031SShannon Nelson }
201fbfb8031SShannon Nelson 
ionic_dev_setup(struct ionic * ionic)202fbfb8031SShannon Nelson int ionic_dev_setup(struct ionic *ionic)
203fbfb8031SShannon Nelson {
204fbfb8031SShannon Nelson 	struct ionic_dev_bar *bar = ionic->bars;
205fbfb8031SShannon Nelson 	unsigned int num_bars = ionic->num_bars;
206fbfb8031SShannon Nelson 	struct ionic_dev *idev = &ionic->idev;
207fbfb8031SShannon Nelson 	struct device *dev = ionic->dev;
20840bc471dSShannon Nelson 	int size;
209fbfb8031SShannon Nelson 	u32 sig;
2109e25450dSShannon Nelson 	int err;
211fbfb8031SShannon Nelson 
212fbfb8031SShannon Nelson 	/* BAR0: dev_cmd and interrupts */
213fbfb8031SShannon Nelson 	if (num_bars < 1) {
214fbfb8031SShannon Nelson 		dev_err(dev, "No bars found, aborting\n");
215fbfb8031SShannon Nelson 		return -EFAULT;
216fbfb8031SShannon Nelson 	}
217fbfb8031SShannon Nelson 
218fbfb8031SShannon Nelson 	if (bar->len < IONIC_BAR0_SIZE) {
219fbfb8031SShannon Nelson 		dev_err(dev, "Resource bar size %lu too small, aborting\n",
220fbfb8031SShannon Nelson 			bar->len);
221fbfb8031SShannon Nelson 		return -EFAULT;
222fbfb8031SShannon Nelson 	}
223fbfb8031SShannon Nelson 
224fbfb8031SShannon Nelson 	idev->dev_info_regs = bar->vaddr + IONIC_BAR0_DEV_INFO_REGS_OFFSET;
225fbfb8031SShannon Nelson 	idev->dev_cmd_regs = bar->vaddr + IONIC_BAR0_DEV_CMD_REGS_OFFSET;
226fbfb8031SShannon Nelson 	idev->intr_status = bar->vaddr + IONIC_BAR0_INTR_STATUS_OFFSET;
227fbfb8031SShannon Nelson 	idev->intr_ctrl = bar->vaddr + IONIC_BAR0_INTR_CTRL_OFFSET;
228fbfb8031SShannon Nelson 
22961db421dSShannon Nelson 	idev->hwstamp_regs = &idev->dev_info_regs->hwstamp;
23061db421dSShannon Nelson 
231fbfb8031SShannon Nelson 	sig = ioread32(&idev->dev_info_regs->signature);
232fbfb8031SShannon Nelson 	if (sig != IONIC_DEV_INFO_SIGNATURE) {
233fbfb8031SShannon Nelson 		dev_err(dev, "Incompatible firmware signature %x", sig);
234fbfb8031SShannon Nelson 		return -EFAULT;
235fbfb8031SShannon Nelson 	}
236fbfb8031SShannon Nelson 
237fbfb8031SShannon Nelson 	ionic_init_devinfo(ionic);
238fbfb8031SShannon Nelson 
239fbfb8031SShannon Nelson 	/* BAR1: doorbells */
240fbfb8031SShannon Nelson 	bar++;
241fbfb8031SShannon Nelson 	if (num_bars < 2) {
242fbfb8031SShannon Nelson 		dev_err(dev, "Doorbell bar missing, aborting\n");
243fbfb8031SShannon Nelson 		return -EFAULT;
244fbfb8031SShannon Nelson 	}
245fbfb8031SShannon Nelson 
2469e25450dSShannon Nelson 	err = ionic_watchdog_init(ionic);
2479e25450dSShannon Nelson 	if (err)
2489e25450dSShannon Nelson 		return err;
249b2b9a8d7SShannon Nelson 
250fbfb8031SShannon Nelson 	idev->db_pages = bar->vaddr;
251fbfb8031SShannon Nelson 	idev->phy_db_pages = bar->bus_addr;
252fbfb8031SShannon Nelson 
25340bc471dSShannon Nelson 	/* BAR2: optional controller memory mapping */
25440bc471dSShannon Nelson 	bar++;
25540bc471dSShannon Nelson 	mutex_init(&idev->cmb_inuse_lock);
25640bc471dSShannon Nelson 	if (num_bars < 3 || !ionic->bars[IONIC_PCI_BAR_CMB].len) {
25740bc471dSShannon Nelson 		idev->cmb_inuse = NULL;
258fbfb8031SShannon Nelson 		return 0;
259fbfb8031SShannon Nelson 	}
260fbfb8031SShannon Nelson 
26140bc471dSShannon Nelson 	idev->phy_cmb_pages = bar->bus_addr;
26240bc471dSShannon Nelson 	idev->cmb_npages = bar->len / PAGE_SIZE;
26340bc471dSShannon Nelson 	size = BITS_TO_LONGS(idev->cmb_npages) * sizeof(long);
26440bc471dSShannon Nelson 	idev->cmb_inuse = kzalloc(size, GFP_KERNEL);
26540bc471dSShannon Nelson 	if (!idev->cmb_inuse)
26640bc471dSShannon Nelson 		dev_warn(dev, "No memory for CMB, disabling\n");
26740bc471dSShannon Nelson 
26840bc471dSShannon Nelson 	return 0;
26940bc471dSShannon Nelson }
27040bc471dSShannon Nelson 
ionic_dev_teardown(struct ionic * ionic)27140bc471dSShannon Nelson void ionic_dev_teardown(struct ionic *ionic)
27240bc471dSShannon Nelson {
27340bc471dSShannon Nelson 	struct ionic_dev *idev = &ionic->idev;
27440bc471dSShannon Nelson 
27540bc471dSShannon Nelson 	kfree(idev->cmb_inuse);
27640bc471dSShannon Nelson 	idev->cmb_inuse = NULL;
27740bc471dSShannon Nelson 	idev->phy_cmb_pages = 0;
27840bc471dSShannon Nelson 	idev->cmb_npages = 0;
27940bc471dSShannon Nelson 
2809e25450dSShannon Nelson 	destroy_workqueue(ionic->wq);
28140bc471dSShannon Nelson 	mutex_destroy(&idev->cmb_inuse_lock);
28240bc471dSShannon Nelson }
28340bc471dSShannon Nelson 
284fbfb8031SShannon Nelson /* Devcmd Interface */
__ionic_is_fw_running(struct ionic_dev * idev,u8 * status_ptr)285219e1832SShannon Nelson static bool __ionic_is_fw_running(struct ionic_dev *idev, u8 *status_ptr)
286b8fd0271SBrett Creeley {
287219e1832SShannon Nelson 	u8 fw_status;
288219e1832SShannon Nelson 
289219e1832SShannon Nelson 	if (!idev->dev_info_regs) {
290219e1832SShannon Nelson 		if (status_ptr)
291219e1832SShannon Nelson 			*status_ptr = 0xff;
292219e1832SShannon Nelson 		return false;
293219e1832SShannon Nelson 	}
294219e1832SShannon Nelson 
295219e1832SShannon Nelson 	fw_status = ioread8(&idev->dev_info_regs->fw_status);
296219e1832SShannon Nelson 	if (status_ptr)
297219e1832SShannon Nelson 		*status_ptr = fw_status;
298b8fd0271SBrett Creeley 
299b8fd0271SBrett Creeley 	/* firmware is useful only if the running bit is set and
300b8fd0271SBrett Creeley 	 * fw_status != 0xff (bad PCI read)
301b8fd0271SBrett Creeley 	 */
302b8fd0271SBrett Creeley 	return (fw_status != 0xff) && (fw_status & IONIC_FW_STS_F_RUNNING);
303b8fd0271SBrett Creeley }
304b8fd0271SBrett Creeley 
ionic_is_fw_running(struct ionic_dev * idev)305219e1832SShannon Nelson bool ionic_is_fw_running(struct ionic_dev *idev)
306219e1832SShannon Nelson {
307219e1832SShannon Nelson 	return __ionic_is_fw_running(idev, NULL);
308219e1832SShannon Nelson }
309219e1832SShannon Nelson 
ionic_heartbeat_check(struct ionic * ionic)31097ca4865SShannon Nelson int ionic_heartbeat_check(struct ionic *ionic)
31197ca4865SShannon Nelson {
312b2b9a8d7SShannon Nelson 	unsigned long check_time, last_check_time;
313abd75d14SShannon Nelson 	struct ionic_dev *idev = &ionic->idev;
314abd75d14SShannon Nelson 	struct ionic_lif *lif = ionic->lif;
315d2662072SShannon Nelson 	bool fw_status_ready = true;
316d2662072SShannon Nelson 	bool fw_hb_ready;
317d2662072SShannon Nelson 	u8 fw_generation;
31868b759a7SShannon Nelson 	u8 fw_status;
319b2b9a8d7SShannon Nelson 	u32 fw_hb;
32097ca4865SShannon Nelson 
321b2b9a8d7SShannon Nelson 	/* wait a least one second before testing again */
322b2b9a8d7SShannon Nelson 	check_time = jiffies;
323b2b9a8d7SShannon Nelson 	last_check_time = atomic_long_read(&idev->last_check_time);
324b2b9a8d7SShannon Nelson do_check_time:
325b2b9a8d7SShannon Nelson 	if (time_before(check_time, last_check_time + HZ))
32697ca4865SShannon Nelson 		return 0;
327b2b9a8d7SShannon Nelson 	if (!atomic_long_try_cmpxchg_relaxed(&idev->last_check_time,
328b2b9a8d7SShannon Nelson 					     &last_check_time, check_time)) {
329b2b9a8d7SShannon Nelson 		/* if called concurrently, only the first should proceed. */
330b2b9a8d7SShannon Nelson 		dev_dbg(ionic->dev, "%s: do_check_time again\n", __func__);
331b2b9a8d7SShannon Nelson 		goto do_check_time;
332b2b9a8d7SShannon Nelson 	}
33397ca4865SShannon Nelson 
334b8fd0271SBrett Creeley 	/* If fw_status is not ready don't bother with the generation */
335219e1832SShannon Nelson 	if (!__ionic_is_fw_running(idev, &fw_status)) {
336d2662072SShannon Nelson 		fw_status_ready = false;
337d2662072SShannon Nelson 	} else {
338d2662072SShannon Nelson 		fw_generation = fw_status & IONIC_FW_STS_F_GENERATION;
339d2662072SShannon Nelson 		if (idev->fw_generation != fw_generation) {
340d2662072SShannon Nelson 			dev_info(ionic->dev, "FW generation 0x%02x -> 0x%02x\n",
341d2662072SShannon Nelson 				 idev->fw_generation, fw_generation);
342d2662072SShannon Nelson 
343d2662072SShannon Nelson 			idev->fw_generation = fw_generation;
344d2662072SShannon Nelson 
345d2662072SShannon Nelson 			/* If the generation changed, the fw status is not
346d2662072SShannon Nelson 			 * ready so we need to trigger a fw-down cycle.  After
347d2662072SShannon Nelson 			 * the down, the next watchdog will see the fw is up
348d2662072SShannon Nelson 			 * and the generation value stable, so will trigger
349d2662072SShannon Nelson 			 * the fw-up activity.
350abd75d14SShannon Nelson 			 *
351abd75d14SShannon Nelson 			 * If we had already moved to FW_RESET from a RESET event,
352abd75d14SShannon Nelson 			 * it is possible that we never saw the fw_status go to 0,
353abd75d14SShannon Nelson 			 * so we fake the current idev->fw_status_ready here to
354abd75d14SShannon Nelson 			 * force the transition and get FW up again.
355d2662072SShannon Nelson 			 */
356abd75d14SShannon Nelson 			if (test_bit(IONIC_LIF_F_FW_RESET, lif->state))
357abd75d14SShannon Nelson 				idev->fw_status_ready = false;	/* go to running */
358abd75d14SShannon Nelson 			else
359abd75d14SShannon Nelson 				fw_status_ready = false;	/* go to down */
360d2662072SShannon Nelson 		}
361d2662072SShannon Nelson 	}
362c672412fSShannon Nelson 
363688a5efeSShannon Nelson 	dev_dbg(ionic->dev, "fw_status 0x%02x ready %d idev->ready %d last_hb 0x%x state 0x%02lx\n",
364688a5efeSShannon Nelson 		fw_status, fw_status_ready, idev->fw_status_ready,
365688a5efeSShannon Nelson 		idev->last_fw_hb, lif->state[0]);
366688a5efeSShannon Nelson 
367c672412fSShannon Nelson 	/* is this a transition? */
368688a5efeSShannon Nelson 	if (fw_status_ready != idev->fw_status_ready &&
369688a5efeSShannon Nelson 	    !test_bit(IONIC_LIF_F_FW_STOPPING, lif->state)) {
370c672412fSShannon Nelson 		bool trigger = false;
371c672412fSShannon Nelson 
37212b1b997SShannon Nelson 		idev->fw_status_ready = fw_status_ready;
37312b1b997SShannon Nelson 
374688a5efeSShannon Nelson 		if (!fw_status_ready &&
375398d1e37SShannon Nelson 		    !test_bit(IONIC_LIF_F_FW_RESET, lif->state) &&
376398d1e37SShannon Nelson 		    !test_and_set_bit(IONIC_LIF_F_FW_STOPPING, lif->state)) {
377398d1e37SShannon Nelson 			dev_info(ionic->dev, "FW stopped 0x%02x\n", fw_status);
378c672412fSShannon Nelson 			trigger = true;
379398d1e37SShannon Nelson 
380688a5efeSShannon Nelson 		} else if (fw_status_ready &&
381688a5efeSShannon Nelson 			   test_bit(IONIC_LIF_F_FW_RESET, lif->state)) {
382398d1e37SShannon Nelson 			dev_info(ionic->dev, "FW running 0x%02x\n", fw_status);
383c672412fSShannon Nelson 			trigger = true;
384c672412fSShannon Nelson 		}
385c672412fSShannon Nelson 
386c672412fSShannon Nelson 		if (trigger) {
387c672412fSShannon Nelson 			struct ionic_deferred_work *work;
388c672412fSShannon Nelson 
389c672412fSShannon Nelson 			work = kzalloc(sizeof(*work), GFP_ATOMIC);
390b2b9a8d7SShannon Nelson 			if (work) {
391c672412fSShannon Nelson 				work->type = IONIC_DW_TYPE_LIF_RESET;
392b2b9a8d7SShannon Nelson 				work->fw_status = fw_status_ready;
3939e25450dSShannon Nelson 				ionic_lif_deferred_enqueue(lif, work);
394c672412fSShannon Nelson 			}
395c672412fSShannon Nelson 		}
396c672412fSShannon Nelson 	}
397c672412fSShannon Nelson 
398398d1e37SShannon Nelson 	if (!idev->fw_status_ready)
39997ca4865SShannon Nelson 		return -ENXIO;
40097ca4865SShannon Nelson 
401ec8ee714SShannon Nelson 	/* Because of some variability in the actual FW heartbeat, we
402ec8ee714SShannon Nelson 	 * wait longer than the DEVCMD_TIMEOUT before checking again.
403ec8ee714SShannon Nelson 	 */
404b2b9a8d7SShannon Nelson 	last_check_time = idev->last_hb_time;
405ec8ee714SShannon Nelson 	if (time_before(check_time, last_check_time + DEVCMD_TIMEOUT * 2 * HZ))
40697ca4865SShannon Nelson 		return 0;
40797ca4865SShannon Nelson 
408b2b9a8d7SShannon Nelson 	fw_hb = ioread32(&idev->dev_info_regs->fw_heartbeat);
409b2b9a8d7SShannon Nelson 	fw_hb_ready = fw_hb != idev->last_fw_hb;
410b2b9a8d7SShannon Nelson 
411b2b9a8d7SShannon Nelson 	/* early FW version had no heartbeat, so fake it */
412b2b9a8d7SShannon Nelson 	if (!fw_hb_ready && !fw_hb)
413b2b9a8d7SShannon Nelson 		fw_hb_ready = true;
414b2b9a8d7SShannon Nelson 
415b2b9a8d7SShannon Nelson 	dev_dbg(ionic->dev, "%s: fw_hb %u last_fw_hb %u ready %u\n",
416b2b9a8d7SShannon Nelson 		__func__, fw_hb, idev->last_fw_hb, fw_hb_ready);
417b2b9a8d7SShannon Nelson 
418b2b9a8d7SShannon Nelson 	idev->last_fw_hb = fw_hb;
419b2b9a8d7SShannon Nelson 
420b2b9a8d7SShannon Nelson 	/* log a transition */
421b2b9a8d7SShannon Nelson 	if (fw_hb_ready != idev->fw_hb_ready) {
422b2b9a8d7SShannon Nelson 		idev->fw_hb_ready = fw_hb_ready;
423b2b9a8d7SShannon Nelson 		if (!fw_hb_ready)
424b2b9a8d7SShannon Nelson 			dev_info(ionic->dev, "FW heartbeat stalled at %d\n", fw_hb);
425b2b9a8d7SShannon Nelson 		else
426b2b9a8d7SShannon Nelson 			dev_info(ionic->dev, "FW heartbeat restored at %d\n", fw_hb);
42797ca4865SShannon Nelson 	}
42897ca4865SShannon Nelson 
429b2b9a8d7SShannon Nelson 	if (!fw_hb_ready)
43097ca4865SShannon Nelson 		return -ENXIO;
43197ca4865SShannon Nelson 
432b2b9a8d7SShannon Nelson 	idev->last_hb_time = check_time;
43397ca4865SShannon Nelson 
43497ca4865SShannon Nelson 	return 0;
43597ca4865SShannon Nelson }
43697ca4865SShannon Nelson 
ionic_dev_cmd_status(struct ionic_dev * idev)437fbfb8031SShannon Nelson u8 ionic_dev_cmd_status(struct ionic_dev *idev)
438fbfb8031SShannon Nelson {
4397662fad3SShannon Nelson 	if (!idev->dev_cmd_regs)
4407662fad3SShannon Nelson 		return (u8)PCI_ERROR_RESPONSE;
441fbfb8031SShannon Nelson 	return ioread8(&idev->dev_cmd_regs->comp.comp.status);
442fbfb8031SShannon Nelson }
443fbfb8031SShannon Nelson 
ionic_dev_cmd_done(struct ionic_dev * idev)444fbfb8031SShannon Nelson bool ionic_dev_cmd_done(struct ionic_dev *idev)
445fbfb8031SShannon Nelson {
4467662fad3SShannon Nelson 	if (!idev->dev_cmd_regs)
4477662fad3SShannon Nelson 		return false;
448fbfb8031SShannon Nelson 	return ioread32(&idev->dev_cmd_regs->done) & IONIC_DEV_CMD_DONE;
449fbfb8031SShannon Nelson }
450fbfb8031SShannon Nelson 
ionic_dev_cmd_comp(struct ionic_dev * idev,union ionic_dev_cmd_comp * comp)451fbfb8031SShannon Nelson void ionic_dev_cmd_comp(struct ionic_dev *idev, union ionic_dev_cmd_comp *comp)
452fbfb8031SShannon Nelson {
4537662fad3SShannon Nelson 	if (!idev->dev_cmd_regs)
4547662fad3SShannon Nelson 		return;
455fbfb8031SShannon Nelson 	memcpy_fromio(comp, &idev->dev_cmd_regs->comp, sizeof(*comp));
456fbfb8031SShannon Nelson }
457fbfb8031SShannon Nelson 
ionic_dev_cmd_go(struct ionic_dev * idev,union ionic_dev_cmd * cmd)458fbfb8031SShannon Nelson void ionic_dev_cmd_go(struct ionic_dev *idev, union ionic_dev_cmd *cmd)
459fbfb8031SShannon Nelson {
46024f11024SShannon Nelson 	idev->opcode = cmd->cmd.opcode;
4617662fad3SShannon Nelson 
4627662fad3SShannon Nelson 	if (!idev->dev_cmd_regs)
4637662fad3SShannon Nelson 		return;
4647662fad3SShannon Nelson 
465fbfb8031SShannon Nelson 	memcpy_toio(&idev->dev_cmd_regs->cmd, cmd, sizeof(*cmd));
466fbfb8031SShannon Nelson 	iowrite32(0, &idev->dev_cmd_regs->done);
467fbfb8031SShannon Nelson 	iowrite32(1, &idev->dev_cmd_regs->doorbell);
468fbfb8031SShannon Nelson }
469fbfb8031SShannon Nelson 
470fbfb8031SShannon Nelson /* Device commands */
ionic_dev_cmd_identify(struct ionic_dev * idev,u8 ver)471fbfb8031SShannon Nelson void ionic_dev_cmd_identify(struct ionic_dev *idev, u8 ver)
472fbfb8031SShannon Nelson {
473fbfb8031SShannon Nelson 	union ionic_dev_cmd cmd = {
474fbfb8031SShannon Nelson 		.identify.opcode = IONIC_CMD_IDENTIFY,
475fbfb8031SShannon Nelson 		.identify.ver = ver,
476fbfb8031SShannon Nelson 	};
477fbfb8031SShannon Nelson 
478fbfb8031SShannon Nelson 	ionic_dev_cmd_go(idev, &cmd);
479fbfb8031SShannon Nelson }
480fbfb8031SShannon Nelson 
ionic_dev_cmd_init(struct ionic_dev * idev)481fbfb8031SShannon Nelson void ionic_dev_cmd_init(struct ionic_dev *idev)
482fbfb8031SShannon Nelson {
483fbfb8031SShannon Nelson 	union ionic_dev_cmd cmd = {
484fbfb8031SShannon Nelson 		.init.opcode = IONIC_CMD_INIT,
485fbfb8031SShannon Nelson 		.init.type = 0,
486fbfb8031SShannon Nelson 	};
487fbfb8031SShannon Nelson 
488fbfb8031SShannon Nelson 	ionic_dev_cmd_go(idev, &cmd);
489fbfb8031SShannon Nelson }
490fbfb8031SShannon Nelson 
ionic_dev_cmd_reset(struct ionic_dev * idev)491fbfb8031SShannon Nelson void ionic_dev_cmd_reset(struct ionic_dev *idev)
492fbfb8031SShannon Nelson {
493fbfb8031SShannon Nelson 	union ionic_dev_cmd cmd = {
494fbfb8031SShannon Nelson 		.reset.opcode = IONIC_CMD_RESET,
495fbfb8031SShannon Nelson 	};
496fbfb8031SShannon Nelson 
497fbfb8031SShannon Nelson 	ionic_dev_cmd_go(idev, &cmd);
498fbfb8031SShannon Nelson }
49904436595SShannon Nelson 
50004436595SShannon Nelson /* Port commands */
ionic_dev_cmd_port_identify(struct ionic_dev * idev)50104436595SShannon Nelson void ionic_dev_cmd_port_identify(struct ionic_dev *idev)
50204436595SShannon Nelson {
50304436595SShannon Nelson 	union ionic_dev_cmd cmd = {
50404436595SShannon Nelson 		.port_init.opcode = IONIC_CMD_PORT_IDENTIFY,
50504436595SShannon Nelson 		.port_init.index = 0,
50604436595SShannon Nelson 	};
50704436595SShannon Nelson 
50804436595SShannon Nelson 	ionic_dev_cmd_go(idev, &cmd);
50904436595SShannon Nelson }
51004436595SShannon Nelson 
ionic_dev_cmd_port_init(struct ionic_dev * idev)51104436595SShannon Nelson void ionic_dev_cmd_port_init(struct ionic_dev *idev)
51204436595SShannon Nelson {
51304436595SShannon Nelson 	union ionic_dev_cmd cmd = {
51404436595SShannon Nelson 		.port_init.opcode = IONIC_CMD_PORT_INIT,
51504436595SShannon Nelson 		.port_init.index = 0,
51604436595SShannon Nelson 		.port_init.info_pa = cpu_to_le64(idev->port_info_pa),
51704436595SShannon Nelson 	};
51804436595SShannon Nelson 
51904436595SShannon Nelson 	ionic_dev_cmd_go(idev, &cmd);
52004436595SShannon Nelson }
52104436595SShannon Nelson 
ionic_dev_cmd_port_reset(struct ionic_dev * idev)52204436595SShannon Nelson void ionic_dev_cmd_port_reset(struct ionic_dev *idev)
52304436595SShannon Nelson {
52404436595SShannon Nelson 	union ionic_dev_cmd cmd = {
52504436595SShannon Nelson 		.port_reset.opcode = IONIC_CMD_PORT_RESET,
52604436595SShannon Nelson 		.port_reset.index = 0,
52704436595SShannon Nelson 	};
52804436595SShannon Nelson 
52904436595SShannon Nelson 	ionic_dev_cmd_go(idev, &cmd);
53004436595SShannon Nelson }
53104436595SShannon Nelson 
ionic_dev_cmd_port_state(struct ionic_dev * idev,u8 state)53204436595SShannon Nelson void ionic_dev_cmd_port_state(struct ionic_dev *idev, u8 state)
53304436595SShannon Nelson {
53404436595SShannon Nelson 	union ionic_dev_cmd cmd = {
53504436595SShannon Nelson 		.port_setattr.opcode = IONIC_CMD_PORT_SETATTR,
53604436595SShannon Nelson 		.port_setattr.index = 0,
53704436595SShannon Nelson 		.port_setattr.attr = IONIC_PORT_ATTR_STATE,
53804436595SShannon Nelson 		.port_setattr.state = state,
53904436595SShannon Nelson 	};
54004436595SShannon Nelson 
54104436595SShannon Nelson 	ionic_dev_cmd_go(idev, &cmd);
54204436595SShannon Nelson }
54304436595SShannon Nelson 
ionic_dev_cmd_port_speed(struct ionic_dev * idev,u32 speed)54404436595SShannon Nelson void ionic_dev_cmd_port_speed(struct ionic_dev *idev, u32 speed)
54504436595SShannon Nelson {
54604436595SShannon Nelson 	union ionic_dev_cmd cmd = {
54704436595SShannon Nelson 		.port_setattr.opcode = IONIC_CMD_PORT_SETATTR,
54804436595SShannon Nelson 		.port_setattr.index = 0,
54904436595SShannon Nelson 		.port_setattr.attr = IONIC_PORT_ATTR_SPEED,
55004436595SShannon Nelson 		.port_setattr.speed = cpu_to_le32(speed),
55104436595SShannon Nelson 	};
55204436595SShannon Nelson 
55304436595SShannon Nelson 	ionic_dev_cmd_go(idev, &cmd);
55404436595SShannon Nelson }
55504436595SShannon Nelson 
ionic_dev_cmd_port_autoneg(struct ionic_dev * idev,u8 an_enable)55604436595SShannon Nelson void ionic_dev_cmd_port_autoneg(struct ionic_dev *idev, u8 an_enable)
55704436595SShannon Nelson {
55804436595SShannon Nelson 	union ionic_dev_cmd cmd = {
55904436595SShannon Nelson 		.port_setattr.opcode = IONIC_CMD_PORT_SETATTR,
56004436595SShannon Nelson 		.port_setattr.index = 0,
56104436595SShannon Nelson 		.port_setattr.attr = IONIC_PORT_ATTR_AUTONEG,
56204436595SShannon Nelson 		.port_setattr.an_enable = an_enable,
56304436595SShannon Nelson 	};
56404436595SShannon Nelson 
56504436595SShannon Nelson 	ionic_dev_cmd_go(idev, &cmd);
56604436595SShannon Nelson }
56704436595SShannon Nelson 
ionic_dev_cmd_port_fec(struct ionic_dev * idev,u8 fec_type)56804436595SShannon Nelson void ionic_dev_cmd_port_fec(struct ionic_dev *idev, u8 fec_type)
56904436595SShannon Nelson {
57004436595SShannon Nelson 	union ionic_dev_cmd cmd = {
57104436595SShannon Nelson 		.port_setattr.opcode = IONIC_CMD_PORT_SETATTR,
57204436595SShannon Nelson 		.port_setattr.index = 0,
57304436595SShannon Nelson 		.port_setattr.attr = IONIC_PORT_ATTR_FEC,
57404436595SShannon Nelson 		.port_setattr.fec_type = fec_type,
57504436595SShannon Nelson 	};
57604436595SShannon Nelson 
57704436595SShannon Nelson 	ionic_dev_cmd_go(idev, &cmd);
57804436595SShannon Nelson }
57904436595SShannon Nelson 
ionic_dev_cmd_port_pause(struct ionic_dev * idev,u8 pause_type)58004436595SShannon Nelson void ionic_dev_cmd_port_pause(struct ionic_dev *idev, u8 pause_type)
58104436595SShannon Nelson {
58204436595SShannon Nelson 	union ionic_dev_cmd cmd = {
58304436595SShannon Nelson 		.port_setattr.opcode = IONIC_CMD_PORT_SETATTR,
58404436595SShannon Nelson 		.port_setattr.index = 0,
58504436595SShannon Nelson 		.port_setattr.attr = IONIC_PORT_ATTR_PAUSE,
58604436595SShannon Nelson 		.port_setattr.pause_type = pause_type,
58704436595SShannon Nelson 	};
58804436595SShannon Nelson 
58904436595SShannon Nelson 	ionic_dev_cmd_go(idev, &cmd);
59004436595SShannon Nelson }
5911a58e196SShannon Nelson 
592fbb39807SShannon Nelson /* VF commands */
ionic_set_vf_config(struct ionic * ionic,int vf,struct ionic_vf_setattr_cmd * vfc)59336197d82SShannon Nelson int ionic_set_vf_config(struct ionic *ionic, int vf,
59436197d82SShannon Nelson 			struct ionic_vf_setattr_cmd *vfc)
595fbb39807SShannon Nelson {
596fbb39807SShannon Nelson 	union ionic_dev_cmd cmd = {
597fbb39807SShannon Nelson 		.vf_setattr.opcode = IONIC_CMD_VF_SETATTR,
59836197d82SShannon Nelson 		.vf_setattr.attr = vfc->attr,
599d701ec32SShannon Nelson 		.vf_setattr.vf_index = cpu_to_le16(vf),
600fbb39807SShannon Nelson 	};
601fbb39807SShannon Nelson 	int err;
602fbb39807SShannon Nelson 
60336197d82SShannon Nelson 	memcpy(cmd.vf_setattr.pad, vfc->pad, sizeof(vfc->pad));
604fbb39807SShannon Nelson 
605fbb39807SShannon Nelson 	mutex_lock(&ionic->dev_cmd_lock);
606fbb39807SShannon Nelson 	ionic_dev_cmd_go(&ionic->idev, &cmd);
607fbb39807SShannon Nelson 	err = ionic_dev_cmd_wait(ionic, DEVCMD_TIMEOUT);
608fbb39807SShannon Nelson 	mutex_unlock(&ionic->dev_cmd_lock);
609fbb39807SShannon Nelson 
610fbb39807SShannon Nelson 	return err;
611fbb39807SShannon Nelson }
612fbb39807SShannon Nelson 
ionic_vf_start(struct ionic * ionic)613f43a96d9SShannon Nelson void ionic_vf_start(struct ionic *ionic)
614f43a96d9SShannon Nelson {
615f43a96d9SShannon Nelson 	union ionic_dev_cmd cmd = {
616f43a96d9SShannon Nelson 		.vf_ctrl.opcode = IONIC_CMD_VF_CTRL,
617f43a96d9SShannon Nelson 		.vf_ctrl.ctrl_opcode = IONIC_VF_CTRL_START_ALL,
618f43a96d9SShannon Nelson 	};
619f43a96d9SShannon Nelson 
620f43a96d9SShannon Nelson 	if (!(ionic->ident.dev.capabilities & cpu_to_le64(IONIC_DEV_CAP_VF_CTRL)))
621f43a96d9SShannon Nelson 		return;
622f43a96d9SShannon Nelson 
623f43a96d9SShannon Nelson 	ionic_dev_cmd_go(&ionic->idev, &cmd);
624f43a96d9SShannon Nelson 	ionic_dev_cmd_wait(ionic, DEVCMD_TIMEOUT);
625f43a96d9SShannon Nelson }
626f43a96d9SShannon Nelson 
6271a58e196SShannon Nelson /* LIF commands */
ionic_dev_cmd_queue_identify(struct ionic_dev * idev,u16 lif_type,u8 qtype,u8 qver)6285b3f3f2aSShannon Nelson void ionic_dev_cmd_queue_identify(struct ionic_dev *idev,
6295b3f3f2aSShannon Nelson 				  u16 lif_type, u8 qtype, u8 qver)
6305b3f3f2aSShannon Nelson {
6315b3f3f2aSShannon Nelson 	union ionic_dev_cmd cmd = {
6325b3f3f2aSShannon Nelson 		.q_identify.opcode = IONIC_CMD_Q_IDENTIFY,
633d701ec32SShannon Nelson 		.q_identify.lif_type = cpu_to_le16(lif_type),
6345b3f3f2aSShannon Nelson 		.q_identify.type = qtype,
6355b3f3f2aSShannon Nelson 		.q_identify.ver = qver,
6365b3f3f2aSShannon Nelson 	};
6375b3f3f2aSShannon Nelson 
6385b3f3f2aSShannon Nelson 	ionic_dev_cmd_go(idev, &cmd);
6395b3f3f2aSShannon Nelson }
6405b3f3f2aSShannon Nelson 
ionic_dev_cmd_lif_identify(struct ionic_dev * idev,u8 type,u8 ver)6411a58e196SShannon Nelson void ionic_dev_cmd_lif_identify(struct ionic_dev *idev, u8 type, u8 ver)
6421a58e196SShannon Nelson {
6431a58e196SShannon Nelson 	union ionic_dev_cmd cmd = {
6441a58e196SShannon Nelson 		.lif_identify.opcode = IONIC_CMD_LIF_IDENTIFY,
6451a58e196SShannon Nelson 		.lif_identify.type = type,
6461a58e196SShannon Nelson 		.lif_identify.ver = ver,
6471a58e196SShannon Nelson 	};
6481a58e196SShannon Nelson 
6491a58e196SShannon Nelson 	ionic_dev_cmd_go(idev, &cmd);
6501a58e196SShannon Nelson }
6511a58e196SShannon Nelson 
ionic_dev_cmd_lif_init(struct ionic_dev * idev,u16 lif_index,dma_addr_t info_pa)6521a58e196SShannon Nelson void ionic_dev_cmd_lif_init(struct ionic_dev *idev, u16 lif_index,
6531a58e196SShannon Nelson 			    dma_addr_t info_pa)
6541a58e196SShannon Nelson {
6551a58e196SShannon Nelson 	union ionic_dev_cmd cmd = {
6561a58e196SShannon Nelson 		.lif_init.opcode = IONIC_CMD_LIF_INIT,
6571a58e196SShannon Nelson 		.lif_init.index = cpu_to_le16(lif_index),
6581a58e196SShannon Nelson 		.lif_init.info_pa = cpu_to_le64(info_pa),
6591a58e196SShannon Nelson 	};
6601a58e196SShannon Nelson 
6611a58e196SShannon Nelson 	ionic_dev_cmd_go(idev, &cmd);
6621a58e196SShannon Nelson }
6631a58e196SShannon Nelson 
ionic_dev_cmd_lif_reset(struct ionic_dev * idev,u16 lif_index)6641a58e196SShannon Nelson void ionic_dev_cmd_lif_reset(struct ionic_dev *idev, u16 lif_index)
6651a58e196SShannon Nelson {
6661a58e196SShannon Nelson 	union ionic_dev_cmd cmd = {
6671a58e196SShannon Nelson 		.lif_init.opcode = IONIC_CMD_LIF_RESET,
6681a58e196SShannon Nelson 		.lif_init.index = cpu_to_le16(lif_index),
6691a58e196SShannon Nelson 	};
6701a58e196SShannon Nelson 
6711a58e196SShannon Nelson 	ionic_dev_cmd_go(idev, &cmd);
6721a58e196SShannon Nelson }
6736461b446SShannon Nelson 
ionic_dev_cmd_adminq_init(struct ionic_dev * idev,struct ionic_qcq * qcq,u16 lif_index,u16 intr_index)6741d062b7bSShannon Nelson void ionic_dev_cmd_adminq_init(struct ionic_dev *idev, struct ionic_qcq *qcq,
6751d062b7bSShannon Nelson 			       u16 lif_index, u16 intr_index)
6761d062b7bSShannon Nelson {
6771d062b7bSShannon Nelson 	struct ionic_queue *q = &qcq->q;
6781d062b7bSShannon Nelson 	struct ionic_cq *cq = &qcq->cq;
6791d062b7bSShannon Nelson 
6801d062b7bSShannon Nelson 	union ionic_dev_cmd cmd = {
6811d062b7bSShannon Nelson 		.q_init.opcode = IONIC_CMD_Q_INIT,
6821d062b7bSShannon Nelson 		.q_init.lif_index = cpu_to_le16(lif_index),
6831d062b7bSShannon Nelson 		.q_init.type = q->type,
6845b3f3f2aSShannon Nelson 		.q_init.ver = qcq->q.lif->qtype_info[q->type].version,
6851d062b7bSShannon Nelson 		.q_init.index = cpu_to_le32(q->index),
6861d062b7bSShannon Nelson 		.q_init.flags = cpu_to_le16(IONIC_QINIT_F_IRQ |
6871d062b7bSShannon Nelson 					    IONIC_QINIT_F_ENA),
6881d062b7bSShannon Nelson 		.q_init.pid = cpu_to_le16(q->pid),
6891d062b7bSShannon Nelson 		.q_init.intr_index = cpu_to_le16(intr_index),
6901d062b7bSShannon Nelson 		.q_init.ring_size = ilog2(q->num_descs),
6911d062b7bSShannon Nelson 		.q_init.ring_base = cpu_to_le64(q->base_pa),
6921d062b7bSShannon Nelson 		.q_init.cq_ring_base = cpu_to_le64(cq->base_pa),
6931d062b7bSShannon Nelson 	};
6941d062b7bSShannon Nelson 
6951d062b7bSShannon Nelson 	ionic_dev_cmd_go(idev, &cmd);
6961d062b7bSShannon Nelson }
6971d062b7bSShannon Nelson 
ionic_db_page_num(struct ionic_lif * lif,int pid)6986461b446SShannon Nelson int ionic_db_page_num(struct ionic_lif *lif, int pid)
6996461b446SShannon Nelson {
7006461b446SShannon Nelson 	return (lif->hw_index * lif->dbid_count) + pid;
7016461b446SShannon Nelson }
7021d062b7bSShannon Nelson 
ionic_get_cmb(struct ionic_lif * lif,u32 * pgid,phys_addr_t * pgaddr,int order)70340bc471dSShannon Nelson int ionic_get_cmb(struct ionic_lif *lif, u32 *pgid, phys_addr_t *pgaddr, int order)
70440bc471dSShannon Nelson {
70540bc471dSShannon Nelson 	struct ionic_dev *idev = &lif->ionic->idev;
70640bc471dSShannon Nelson 	int ret;
70740bc471dSShannon Nelson 
70840bc471dSShannon Nelson 	mutex_lock(&idev->cmb_inuse_lock);
70940bc471dSShannon Nelson 	ret = bitmap_find_free_region(idev->cmb_inuse, idev->cmb_npages, order);
71040bc471dSShannon Nelson 	mutex_unlock(&idev->cmb_inuse_lock);
71140bc471dSShannon Nelson 
71240bc471dSShannon Nelson 	if (ret < 0)
71340bc471dSShannon Nelson 		return ret;
71440bc471dSShannon Nelson 
71540bc471dSShannon Nelson 	*pgid = ret;
71640bc471dSShannon Nelson 	*pgaddr = idev->phy_cmb_pages + ret * PAGE_SIZE;
71740bc471dSShannon Nelson 
71840bc471dSShannon Nelson 	return 0;
71940bc471dSShannon Nelson }
72040bc471dSShannon Nelson 
ionic_put_cmb(struct ionic_lif * lif,u32 pgid,int order)72140bc471dSShannon Nelson void ionic_put_cmb(struct ionic_lif *lif, u32 pgid, int order)
72240bc471dSShannon Nelson {
72340bc471dSShannon Nelson 	struct ionic_dev *idev = &lif->ionic->idev;
72440bc471dSShannon Nelson 
72540bc471dSShannon Nelson 	mutex_lock(&idev->cmb_inuse_lock);
72640bc471dSShannon Nelson 	bitmap_release_region(idev->cmb_inuse, pgid, order);
72740bc471dSShannon Nelson 	mutex_unlock(&idev->cmb_inuse_lock);
72840bc471dSShannon Nelson }
72940bc471dSShannon Nelson 
ionic_cq_init(struct ionic_lif * lif,struct ionic_cq * cq,struct ionic_intr_info * intr,unsigned int num_descs,size_t desc_size)7301d062b7bSShannon Nelson int ionic_cq_init(struct ionic_lif *lif, struct ionic_cq *cq,
7311d062b7bSShannon Nelson 		  struct ionic_intr_info *intr,
7321d062b7bSShannon Nelson 		  unsigned int num_descs, size_t desc_size)
7331d062b7bSShannon Nelson {
7341d062b7bSShannon Nelson 	unsigned int ring_size;
7351d062b7bSShannon Nelson 
7361d062b7bSShannon Nelson 	if (desc_size == 0 || !is_power_of_2(num_descs))
7371d062b7bSShannon Nelson 		return -EINVAL;
7381d062b7bSShannon Nelson 
7391d062b7bSShannon Nelson 	ring_size = ilog2(num_descs);
7401d062b7bSShannon Nelson 	if (ring_size < 2 || ring_size > 16)
7411d062b7bSShannon Nelson 		return -EINVAL;
7421d062b7bSShannon Nelson 
7431d062b7bSShannon Nelson 	cq->lif = lif;
7441d062b7bSShannon Nelson 	cq->bound_intr = intr;
7451d062b7bSShannon Nelson 	cq->num_descs = num_descs;
7461d062b7bSShannon Nelson 	cq->desc_size = desc_size;
747f1d2e894SShannon Nelson 	cq->tail_idx = 0;
7481d062b7bSShannon Nelson 	cq->done_color = 1;
74901658924SShannon Nelson 	cq->idev = &lif->ionic->idev;
7501d062b7bSShannon Nelson 
7511d062b7bSShannon Nelson 	return 0;
7521d062b7bSShannon Nelson }
7531d062b7bSShannon Nelson 
ionic_cq_service(struct ionic_cq * cq,unsigned int work_to_do,ionic_cq_cb cb,ionic_cq_done_cb done_cb,void * done_arg)7541d062b7bSShannon Nelson unsigned int ionic_cq_service(struct ionic_cq *cq, unsigned int work_to_do,
7551d062b7bSShannon Nelson 			      ionic_cq_cb cb, ionic_cq_done_cb done_cb,
7561d062b7bSShannon Nelson 			      void *done_arg)
7571d062b7bSShannon Nelson {
7581d062b7bSShannon Nelson 	unsigned int work_done = 0;
7591d062b7bSShannon Nelson 
7601d062b7bSShannon Nelson 	if (work_to_do == 0)
7611d062b7bSShannon Nelson 		return 0;
7621d062b7bSShannon Nelson 
76365e548f6SShannon Nelson 	while (cb(cq)) {
764f1d2e894SShannon Nelson 		if (cq->tail_idx == cq->num_descs - 1)
7651d062b7bSShannon Nelson 			cq->done_color = !cq->done_color;
76665e548f6SShannon Nelson 
767f1d2e894SShannon Nelson 		cq->tail_idx = (cq->tail_idx + 1) & (cq->num_descs - 1);
7681d062b7bSShannon Nelson 
7691d062b7bSShannon Nelson 		if (++work_done >= work_to_do)
7701d062b7bSShannon Nelson 			break;
7711d062b7bSShannon Nelson 	}
7721d062b7bSShannon Nelson 
7731d062b7bSShannon Nelson 	if (work_done && done_cb)
7741d062b7bSShannon Nelson 		done_cb(done_arg);
7751d062b7bSShannon Nelson 
7761d062b7bSShannon Nelson 	return work_done;
7771d062b7bSShannon Nelson }
7781d062b7bSShannon Nelson 
ionic_q_init(struct ionic_lif * lif,struct ionic_dev * idev,struct ionic_queue * q,unsigned int index,const char * name,unsigned int num_descs,size_t desc_size,size_t sg_desc_size,unsigned int pid)7791d062b7bSShannon Nelson int ionic_q_init(struct ionic_lif *lif, struct ionic_dev *idev,
7801d062b7bSShannon Nelson 		 struct ionic_queue *q, unsigned int index, const char *name,
7811d062b7bSShannon Nelson 		 unsigned int num_descs, size_t desc_size,
7821d062b7bSShannon Nelson 		 size_t sg_desc_size, unsigned int pid)
7831d062b7bSShannon Nelson {
7841d062b7bSShannon Nelson 	unsigned int ring_size;
7851d062b7bSShannon Nelson 
7861d062b7bSShannon Nelson 	if (desc_size == 0 || !is_power_of_2(num_descs))
7871d062b7bSShannon Nelson 		return -EINVAL;
7881d062b7bSShannon Nelson 
7891d062b7bSShannon Nelson 	ring_size = ilog2(num_descs);
7901d062b7bSShannon Nelson 	if (ring_size < 2 || ring_size > 16)
7911d062b7bSShannon Nelson 		return -EINVAL;
7921d062b7bSShannon Nelson 
7931d062b7bSShannon Nelson 	q->lif = lif;
7941d062b7bSShannon Nelson 	q->index = index;
7951d062b7bSShannon Nelson 	q->num_descs = num_descs;
7961d062b7bSShannon Nelson 	q->desc_size = desc_size;
7971d062b7bSShannon Nelson 	q->sg_desc_size = sg_desc_size;
798f1d2e894SShannon Nelson 	q->tail_idx = 0;
799f1d2e894SShannon Nelson 	q->head_idx = 0;
8001d062b7bSShannon Nelson 	q->pid = pid;
8011d062b7bSShannon Nelson 
8021d062b7bSShannon Nelson 	snprintf(q->name, sizeof(q->name), "L%d-%s%u", lif->index, name, index);
8031d062b7bSShannon Nelson 
8041d062b7bSShannon Nelson 	return 0;
8051d062b7bSShannon Nelson }
8061d062b7bSShannon Nelson 
ionic_q_post(struct ionic_queue * q,bool ring_doorbell)8074dcd4575SShannon Nelson void ionic_q_post(struct ionic_queue *q, bool ring_doorbell)
8081d062b7bSShannon Nelson {
8091d062b7bSShannon Nelson 	struct ionic_lif *lif = q->lif;
810f37bc346SShannon Nelson 	struct device *dev = q->dev;
8111d062b7bSShannon Nelson 
812f1d2e894SShannon Nelson 	q->head_idx = (q->head_idx + 1) & (q->num_descs - 1);
8131d062b7bSShannon Nelson 
8141d062b7bSShannon Nelson 	dev_dbg(dev, "lif=%d qname=%s qid=%d qtype=%d p_index=%d ringdb=%d\n",
8151d062b7bSShannon Nelson 		q->lif->index, q->name, q->hw_type, q->hw_index,
816f1d2e894SShannon Nelson 		q->head_idx, ring_doorbell);
8171d062b7bSShannon Nelson 
818b69585bfSAllen Hubbe 	if (ring_doorbell) {
8191d062b7bSShannon Nelson 		ionic_dbell_ring(lif->kern_dbpage, q->hw_type,
820f1d2e894SShannon Nelson 				 q->dbval | q->head_idx);
821b69585bfSAllen Hubbe 
822b69585bfSAllen Hubbe 		q->dbell_jiffies = jiffies;
823b69585bfSAllen Hubbe 	}
8241d062b7bSShannon Nelson }
8251d062b7bSShannon Nelson 
ionic_q_is_posted(struct ionic_queue * q,unsigned int pos)826ae24a8f8SShannon Nelson bool ionic_q_is_posted(struct ionic_queue *q, unsigned int pos)
8271d062b7bSShannon Nelson {
8281d062b7bSShannon Nelson 	unsigned int mask, tail, head;
8291d062b7bSShannon Nelson 
8301d062b7bSShannon Nelson 	mask = q->num_descs - 1;
831f1d2e894SShannon Nelson 	tail = q->tail_idx;
832f1d2e894SShannon Nelson 	head = q->head_idx;
8331d062b7bSShannon Nelson 
8341d062b7bSShannon Nelson 	return ((pos - tail) & mask) < ((head - tail) & mask);
8351d062b7bSShannon Nelson }
836