ionic_lif.c 85.5 KB
Newer Older
S
Shannon Nelson 已提交
1 2 3
// SPDX-License-Identifier: GPL-2.0
/* Copyright(c) 2017 - 2019 Pensando Systems, Inc */

4
#include <linux/ethtool.h>
5 6
#include <linux/printk.h>
#include <linux/dynamic_debug.h>
S
Shannon Nelson 已提交
7 8
#include <linux/netdevice.h>
#include <linux/etherdevice.h>
9
#include <linux/if_vlan.h>
10
#include <linux/rtnetlink.h>
S
Shannon Nelson 已提交
11 12 13 14 15 16 17
#include <linux/interrupt.h>
#include <linux/pci.h>
#include <linux/cpumask.h>

#include "ionic.h"
#include "ionic_bus.h"
#include "ionic_lif.h"
S
Shannon Nelson 已提交
18
#include "ionic_txrx.h"
19
#include "ionic_ethtool.h"
S
Shannon Nelson 已提交
20 21
#include "ionic_debugfs.h"

22 23 24 25 26 27 28 29 30 31
/* queuetype support level */
static const u8 ionic_qtype_versions[IONIC_QTYPE_MAX] = {
	[IONIC_QTYPE_ADMINQ]  = 0,   /* 0 = Base version with CQ support */
	[IONIC_QTYPE_NOTIFYQ] = 0,   /* 0 = Base version */
	[IONIC_QTYPE_RXQ]     = 0,   /* 0 = Base version with CQ+SG support */
	[IONIC_QTYPE_TXQ]     = 1,   /* 0 = Base version with CQ+SG support
				      * 1 =   ... with Tx SG version 1
				      */
};

32 33 34
static void ionic_lif_rx_mode(struct ionic_lif *lif, unsigned int rx_mode);
static int ionic_lif_addr_add(struct ionic_lif *lif, const u8 *addr);
static int ionic_lif_addr_del(struct ionic_lif *lif, const u8 *addr);
35
static void ionic_link_status_check(struct ionic_lif *lif);
S
Shannon Nelson 已提交
36 37 38
static void ionic_lif_handle_fw_down(struct ionic_lif *lif);
static void ionic_lif_handle_fw_up(struct ionic_lif *lif);
static void ionic_lif_set_netdev_info(struct ionic_lif *lif);
39

40 41
static void ionic_txrx_deinit(struct ionic_lif *lif);
static int ionic_txrx_init(struct ionic_lif *lif);
42 43
static int ionic_start_queues(struct ionic_lif *lif);
static void ionic_stop_queues(struct ionic_lif *lif);
44
static void ionic_lif_queue_identify(struct ionic_lif *lif);
45

46 47 48 49 50 51 52 53 54 55 56 57 58 59
static void ionic_dim_work(struct work_struct *work)
{
	struct dim *dim = container_of(work, struct dim, work);
	struct dim_cq_moder cur_moder;
	struct ionic_qcq *qcq;
	u32 new_coal;

	cur_moder = net_dim_get_rx_moderation(dim->mode, dim->profile_ix);
	qcq = container_of(dim, struct ionic_qcq, dim);
	new_coal = ionic_coal_usec_to_hw(qcq->q.lif->ionic, cur_moder.usec);
	qcq->intr.dim_coal_hw = new_coal ? new_coal : 1;
	dim->state = DIM_START_MEASURE;
}

60 61 62 63 64 65
static void ionic_lif_deferred_work(struct work_struct *work)
{
	struct ionic_lif *lif = container_of(work, struct ionic_lif, deferred.work);
	struct ionic_deferred *def = &lif->deferred;
	struct ionic_deferred_work *w = NULL;

S
Shannon Nelson 已提交
66 67 68 69 70 71 72 73 74 75 76
	do {
		spin_lock_bh(&def->lock);
		if (!list_empty(&def->list)) {
			w = list_first_entry(&def->list,
					     struct ionic_deferred_work, list);
			list_del(&w->list);
		}
		spin_unlock_bh(&def->lock);

		if (!w)
			break;
77 78 79 80 81 82 83 84 85 86 87

		switch (w->type) {
		case IONIC_DW_TYPE_RX_MODE:
			ionic_lif_rx_mode(lif, w->rx_mode);
			break;
		case IONIC_DW_TYPE_RX_ADDR_ADD:
			ionic_lif_addr_add(lif, w->addr);
			break;
		case IONIC_DW_TYPE_RX_ADDR_DEL:
			ionic_lif_addr_del(lif, w->addr);
			break;
88 89 90
		case IONIC_DW_TYPE_LINK_STATUS:
			ionic_link_status_check(lif);
			break;
S
Shannon Nelson 已提交
91 92 93 94 95 96
		case IONIC_DW_TYPE_LIF_RESET:
			if (w->fw_status)
				ionic_lif_handle_fw_up(lif);
			else
				ionic_lif_handle_fw_down(lif);
			break;
97 98 99 100
		default:
			break;
		}
		kfree(w);
S
Shannon Nelson 已提交
101 102
		w = NULL;
	} while (true);
103 104
}

S
Shannon Nelson 已提交
105 106
void ionic_lif_deferred_enqueue(struct ionic_deferred *def,
				struct ionic_deferred_work *work)
107 108 109 110 111 112 113
{
	spin_lock_bh(&def->lock);
	list_add_tail(&work->list, &def->list);
	spin_unlock_bh(&def->lock);
	schedule_work(&def->work);
}

114 115 116 117 118 119
static void ionic_link_status_check(struct ionic_lif *lif)
{
	struct net_device *netdev = lif->netdev;
	u16 link_status;
	bool link_up;

120
	if (!test_bit(IONIC_LIF_F_LINK_CHECK_REQUESTED, lif->state))
121 122
		return;

123 124 125 126 127 128
	/* Don't put carrier back up if we're in a broken state */
	if (test_bit(IONIC_LIF_F_BROKEN, lif->state)) {
		clear_bit(IONIC_LIF_F_LINK_CHECK_REQUESTED, lif->state);
		return;
	}

129 130 131 132
	link_status = le16_to_cpu(lif->info->status.link_status);
	link_up = link_status == IONIC_PORT_OPER_STATUS_UP;

	if (link_up) {
133 134
		int err = 0;

S
Shannon Nelson 已提交
135
		if (netdev->flags & IFF_UP && netif_running(netdev)) {
136
			mutex_lock(&lif->queue_lock);
137 138 139 140 141 142 143
			err = ionic_start_queues(lif);
			if (err) {
				netdev_err(lif->netdev,
					   "Failed to start queues: %d\n", err);
				set_bit(IONIC_LIF_F_BROKEN, lif->state);
				netif_carrier_off(lif->netdev);
			}
144 145 146
			mutex_unlock(&lif->queue_lock);
		}

147
		if (!err && !netif_carrier_ok(netdev)) {
148 149
			ionic_port_identify(lif->ionic);
			netdev_info(netdev, "Link up - %d Gbps\n",
S
Shannon Nelson 已提交
150
				    le32_to_cpu(lif->info->status.link_speed) / 1000);
S
Shannon Nelson 已提交
151 152
			netif_carrier_on(netdev);
		}
153
	} else {
154 155 156 157
		if (netif_carrier_ok(netdev)) {
			netdev_info(netdev, "Link down\n");
			netif_carrier_off(netdev);
		}
158

S
Shannon Nelson 已提交
159
		if (netdev->flags & IFF_UP && netif_running(netdev)) {
160
			mutex_lock(&lif->queue_lock);
161
			ionic_stop_queues(lif);
162 163
			mutex_unlock(&lif->queue_lock);
		}
164 165
	}

S
Shannon Nelson 已提交
166
	clear_bit(IONIC_LIF_F_LINK_CHECK_REQUESTED, lif->state);
167 168
}

169
void ionic_link_status_check_request(struct ionic_lif *lif, bool can_sleep)
170 171 172 173
{
	struct ionic_deferred_work *work;

	/* we only need one request outstanding at a time */
S
Shannon Nelson 已提交
174
	if (test_and_set_bit(IONIC_LIF_F_LINK_CHECK_REQUESTED, lif->state))
175 176
		return;

177
	if (!can_sleep) {
178
		work = kzalloc(sizeof(*work), GFP_ATOMIC);
179 180
		if (!work) {
			clear_bit(IONIC_LIF_F_LINK_CHECK_REQUESTED, lif->state);
181
			return;
182
		}
183 184 185 186 187 188 189 190

		work->type = IONIC_DW_TYPE_LINK_STATUS;
		ionic_lif_deferred_enqueue(&lif->deferred, work);
	} else {
		ionic_link_status_check(lif);
	}
}

S
Shannon Nelson 已提交
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
static irqreturn_t ionic_isr(int irq, void *data)
{
	struct napi_struct *napi = data;

	napi_schedule_irqoff(napi);

	return IRQ_HANDLED;
}

static int ionic_request_irq(struct ionic_lif *lif, struct ionic_qcq *qcq)
{
	struct ionic_intr_info *intr = &qcq->intr;
	struct device *dev = lif->ionic->dev;
	struct ionic_queue *q = &qcq->q;
	const char *name;

	if (lif->registered)
		name = lif->netdev->name;
	else
		name = dev_name(dev);

	snprintf(intr->name, sizeof(intr->name),
		 "%s-%s-%s", IONIC_DRV_NAME, name, q->name);

	return devm_request_irq(dev, intr->vector, ionic_isr,
				0, intr->name, &qcq->napi);
}

static int ionic_intr_alloc(struct ionic_lif *lif, struct ionic_intr_info *intr)
{
	struct ionic *ionic = lif->ionic;
	int index;

	index = find_first_zero_bit(ionic->intrs, ionic->nintrs);
	if (index == ionic->nintrs) {
		netdev_warn(lif->netdev, "%s: no intr, index=%d nintrs=%d\n",
			    __func__, index, ionic->nintrs);
		return -ENOSPC;
	}

	set_bit(index, ionic->intrs);
	ionic_intr_init(&ionic->idev, intr, index);

	return 0;
}

237
static void ionic_intr_free(struct ionic *ionic, int index)
S
Shannon Nelson 已提交
238
{
S
Shannon Nelson 已提交
239
	if (index != IONIC_INTR_INDEX_NOT_ASSIGNED && index < ionic->nintrs)
240
		clear_bit(index, ionic->intrs);
S
Shannon Nelson 已提交
241 242
}

S
Shannon Nelson 已提交
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
static int ionic_qcq_enable(struct ionic_qcq *qcq)
{
	struct ionic_queue *q = &qcq->q;
	struct ionic_lif *lif = q->lif;
	struct ionic_dev *idev;
	struct device *dev;

	struct ionic_admin_ctx ctx = {
		.work = COMPLETION_INITIALIZER_ONSTACK(ctx.work),
		.cmd.q_control = {
			.opcode = IONIC_CMD_Q_CONTROL,
			.lif_index = cpu_to_le16(lif->index),
			.type = q->type,
			.index = cpu_to_le32(q->index),
			.oper = IONIC_Q_ENABLE,
		},
	};

	idev = &lif->ionic->idev;
	dev = lif->ionic->dev;

	dev_dbg(dev, "q_enable.index %d q_enable.qtype %d\n",
		ctx.cmd.q_control.index, ctx.cmd.q_control.type);

	if (qcq->flags & IONIC_QCQ_F_INTR) {
		irq_set_affinity_hint(qcq->intr.vector,
				      &qcq->intr.affinity_mask);
		napi_enable(&qcq->napi);
		ionic_intr_clean(idev->intr_ctrl, qcq->intr.index);
		ionic_intr_mask(idev->intr_ctrl, qcq->intr.index,
				IONIC_INTR_MASK_CLEAR);
	}

	return ionic_adminq_post_wait(lif, &ctx);
}

279
static int ionic_qcq_disable(struct ionic_qcq *qcq, bool send_to_hw)
S
Shannon Nelson 已提交
280
{
281 282
	struct ionic_queue *q;
	struct ionic_lif *lif;
283
	int err = 0;
S
Shannon Nelson 已提交
284 285 286 287 288 289 290 291 292

	struct ionic_admin_ctx ctx = {
		.work = COMPLETION_INITIALIZER_ONSTACK(ctx.work),
		.cmd.q_control = {
			.opcode = IONIC_CMD_Q_CONTROL,
			.oper = IONIC_Q_DISABLE,
		},
	};

293 294
	if (!qcq)
		return -ENXIO;
S
Shannon Nelson 已提交
295

296 297
	q = &qcq->q;
	lif = q->lif;
S
Shannon Nelson 已提交
298 299

	if (qcq->flags & IONIC_QCQ_F_INTR) {
300 301
		struct ionic_dev *idev = &lif->ionic->idev;

302
		cancel_work_sync(&qcq->dim.work);
S
Shannon Nelson 已提交
303 304 305 306 307 308 309
		ionic_intr_mask(idev->intr_ctrl, qcq->intr.index,
				IONIC_INTR_MASK_SET);
		synchronize_irq(qcq->intr.vector);
		irq_set_affinity_hint(qcq->intr.vector, NULL);
		napi_disable(&qcq->napi);
	}

310 311 312 313 314 315
	if (send_to_hw) {
		ctx.cmd.q_control.lif_index = cpu_to_le16(lif->index);
		ctx.cmd.q_control.type = q->type;
		ctx.cmd.q_control.index = cpu_to_le32(q->index);
		dev_dbg(lif->ionic->dev, "q_disable.index %d q_disable.qtype %d\n",
			ctx.cmd.q_control.index, ctx.cmd.q_control.type);
316

317 318 319 320
		err = ionic_adminq_post_wait(lif, &ctx);
	}

	return err;
S
Shannon Nelson 已提交
321 322
}

S
Shannon Nelson 已提交
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
static void ionic_lif_qcq_deinit(struct ionic_lif *lif, struct ionic_qcq *qcq)
{
	struct ionic_dev *idev = &lif->ionic->idev;

	if (!qcq)
		return;

	if (!(qcq->flags & IONIC_QCQ_F_INITED))
		return;

	if (qcq->flags & IONIC_QCQ_F_INTR) {
		ionic_intr_mask(idev->intr_ctrl, qcq->intr.index,
				IONIC_INTR_MASK_SET);
		netif_napi_del(&qcq->napi);
	}

	qcq->flags &= ~IONIC_QCQ_F_INITED;
}

342 343 344 345 346 347 348 349 350 351 352 353
static void ionic_qcq_intr_free(struct ionic_lif *lif, struct ionic_qcq *qcq)
{
	if (!(qcq->flags & IONIC_QCQ_F_INTR) || qcq->intr.vector == 0)
		return;

	irq_set_affinity_hint(qcq->intr.vector, NULL);
	devm_free_irq(lif->ionic->dev, qcq->intr.vector, &qcq->napi);
	qcq->intr.vector = 0;
	ionic_intr_free(lif->ionic, qcq->intr.index);
	qcq->intr.index = IONIC_INTR_INDEX_NOT_ASSIGNED;
}

S
Shannon Nelson 已提交
354 355 356 357 358 359 360
static void ionic_qcq_free(struct ionic_lif *lif, struct ionic_qcq *qcq)
{
	struct device *dev = lif->ionic->dev;

	if (!qcq)
		return;

361 362
	ionic_debugfs_del_qcq(qcq);

363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379
	if (qcq->q_base) {
		dma_free_coherent(dev, qcq->q_size, qcq->q_base, qcq->q_base_pa);
		qcq->q_base = NULL;
		qcq->q_base_pa = 0;
	}

	if (qcq->cq_base) {
		dma_free_coherent(dev, qcq->cq_size, qcq->cq_base, qcq->cq_base_pa);
		qcq->cq_base = NULL;
		qcq->cq_base_pa = 0;
	}

	if (qcq->sg_base) {
		dma_free_coherent(dev, qcq->sg_size, qcq->sg_base, qcq->sg_base_pa);
		qcq->sg_base = NULL;
		qcq->sg_base_pa = 0;
	}
S
Shannon Nelson 已提交
380

381
	ionic_qcq_intr_free(lif, qcq);
S
Shannon Nelson 已提交
382

383 384 385 386 387 388 389 390
	if (qcq->cq.info) {
		devm_kfree(dev, qcq->cq.info);
		qcq->cq.info = NULL;
	}
	if (qcq->q.info) {
		devm_kfree(dev, qcq->q.info);
		qcq->q.info = NULL;
	}
S
Shannon Nelson 已提交
391 392 393 394
}

static void ionic_qcqs_free(struct ionic_lif *lif)
{
S
Shannon Nelson 已提交
395
	struct device *dev = lif->ionic->dev;
396 397
	struct ionic_qcq *adminqcq;
	unsigned long irqflags;
S
Shannon Nelson 已提交
398

S
Shannon Nelson 已提交
399 400
	if (lif->notifyqcq) {
		ionic_qcq_free(lif, lif->notifyqcq);
401
		devm_kfree(dev, lif->notifyqcq);
S
Shannon Nelson 已提交
402 403 404
		lif->notifyqcq = NULL;
	}

S
Shannon Nelson 已提交
405
	if (lif->adminqcq) {
406 407
		spin_lock_irqsave(&lif->adminq_lock, irqflags);
		adminqcq = READ_ONCE(lif->adminqcq);
S
Shannon Nelson 已提交
408
		lif->adminqcq = NULL;
409 410 411 412 413
		spin_unlock_irqrestore(&lif->adminq_lock, irqflags);
		if (adminqcq) {
			ionic_qcq_free(lif, adminqcq);
			devm_kfree(dev, adminqcq);
		}
S
Shannon Nelson 已提交
414
	}
S
Shannon Nelson 已提交
415

416
	if (lif->rxqcqs) {
417 418
		devm_kfree(dev, lif->rxqstats);
		lif->rxqstats = NULL;
419 420 421
		devm_kfree(dev, lif->rxqcqs);
		lif->rxqcqs = NULL;
	}
S
Shannon Nelson 已提交
422

423
	if (lif->txqcqs) {
424 425
		devm_kfree(dev, lif->txqstats);
		lif->txqstats = NULL;
426 427 428
		devm_kfree(dev, lif->txqcqs);
		lif->txqcqs = NULL;
	}
S
Shannon Nelson 已提交
429 430
}

S
Shannon Nelson 已提交
431 432 433 434
static void ionic_link_qcq_interrupts(struct ionic_qcq *src_qcq,
				      struct ionic_qcq *n_qcq)
{
	if (WARN_ON(n_qcq->flags & IONIC_QCQ_F_INTR)) {
435
		ionic_intr_free(n_qcq->cq.lif->ionic, n_qcq->intr.index);
S
Shannon Nelson 已提交
436 437 438 439 440 441 442
		n_qcq->flags &= ~IONIC_QCQ_F_INTR;
	}

	n_qcq->intr.vector = src_qcq->intr.vector;
	n_qcq->intr.index = src_qcq->intr.index;
}

443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489
static int ionic_alloc_qcq_interrupt(struct ionic_lif *lif, struct ionic_qcq *qcq)
{
	int err;

	if (!(qcq->flags & IONIC_QCQ_F_INTR)) {
		qcq->intr.index = IONIC_INTR_INDEX_NOT_ASSIGNED;
		return 0;
	}

	err = ionic_intr_alloc(lif, &qcq->intr);
	if (err) {
		netdev_warn(lif->netdev, "no intr for %s: %d\n",
			    qcq->q.name, err);
		goto err_out;
	}

	err = ionic_bus_get_irq(lif->ionic, qcq->intr.index);
	if (err < 0) {
		netdev_warn(lif->netdev, "no vector for %s: %d\n",
			    qcq->q.name, err);
		goto err_out_free_intr;
	}
	qcq->intr.vector = err;
	ionic_intr_mask_assert(lif->ionic->idev.intr_ctrl, qcq->intr.index,
			       IONIC_INTR_MASK_SET);

	err = ionic_request_irq(lif, qcq);
	if (err) {
		netdev_warn(lif->netdev, "irq request failed %d\n", err);
		goto err_out_free_intr;
	}

	/* try to get the irq on the local numa node first */
	qcq->intr.cpu = cpumask_local_spread(qcq->intr.index,
					     dev_to_node(lif->ionic->dev));
	if (qcq->intr.cpu != -1)
		cpumask_set_cpu(qcq->intr.cpu, &qcq->intr.affinity_mask);

	netdev_dbg(lif->netdev, "%s: Interrupt index %d\n", qcq->q.name, qcq->intr.index);
	return 0;

err_out_free_intr:
	ionic_intr_free(lif->ionic, qcq->intr.index);
err_out:
	return err;
}

S
Shannon Nelson 已提交
490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515
static int ionic_qcq_alloc(struct ionic_lif *lif, unsigned int type,
			   unsigned int index,
			   const char *name, unsigned int flags,
			   unsigned int num_descs, unsigned int desc_size,
			   unsigned int cq_desc_size,
			   unsigned int sg_desc_size,
			   unsigned int pid, struct ionic_qcq **qcq)
{
	struct ionic_dev *idev = &lif->ionic->idev;
	struct device *dev = lif->ionic->dev;
	void *q_base, *cq_base, *sg_base;
	dma_addr_t cq_base_pa = 0;
	dma_addr_t sg_base_pa = 0;
	dma_addr_t q_base_pa = 0;
	struct ionic_qcq *new;
	int err;

	*qcq = NULL;

	new = devm_kzalloc(dev, sizeof(*new), GFP_KERNEL);
	if (!new) {
		netdev_err(lif->netdev, "Cannot allocate queue structure\n");
		err = -ENOMEM;
		goto err_out;
	}

516
	new->q.dev = dev;
S
Shannon Nelson 已提交
517 518
	new->flags = flags;

519
	new->q.info = devm_kcalloc(dev, num_descs, sizeof(*new->q.info),
S
Shannon Nelson 已提交
520 521 522 523
				   GFP_KERNEL);
	if (!new->q.info) {
		netdev_err(lif->netdev, "Cannot allocate queue info\n");
		err = -ENOMEM;
524
		goto err_out_free_qcq;
S
Shannon Nelson 已提交
525 526 527
	}

	new->q.type = type;
528
	new->q.max_sg_elems = lif->qtype_info[type].max_sg_elems;
S
Shannon Nelson 已提交
529 530 531 532 533

	err = ionic_q_init(lif, idev, &new->q, index, name, num_descs,
			   desc_size, sg_desc_size, pid);
	if (err) {
		netdev_err(lif->netdev, "Cannot initialize queue\n");
534
		goto err_out_free_q_info;
S
Shannon Nelson 已提交
535 536
	}

537 538 539
	err = ionic_alloc_qcq_interrupt(lif, new);
	if (err)
		goto err_out;
S
Shannon Nelson 已提交
540

541
	new->cq.info = devm_kcalloc(dev, num_descs, sizeof(*new->cq.info),
S
Shannon Nelson 已提交
542 543 544 545
				    GFP_KERNEL);
	if (!new->cq.info) {
		netdev_err(lif->netdev, "Cannot allocate completion queue info\n");
		err = -ENOMEM;
546
		goto err_out_free_irq;
S
Shannon Nelson 已提交
547 548 549 550 551
	}

	err = ionic_cq_init(lif, &new->cq, &new->intr, num_descs, cq_desc_size);
	if (err) {
		netdev_err(lif->netdev, "Cannot initialize completion queue\n");
552
		goto err_out_free_cq_info;
S
Shannon Nelson 已提交
553 554
	}

555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602
	if (flags & IONIC_QCQ_F_NOTIFYQ) {
		int q_size, cq_size;

		/* q & cq need to be contiguous in case of notifyq */
		q_size = ALIGN(num_descs * desc_size, PAGE_SIZE);
		cq_size = ALIGN(num_descs * cq_desc_size, PAGE_SIZE);

		new->q_size = PAGE_SIZE + q_size + cq_size;
		new->q_base = dma_alloc_coherent(dev, new->q_size,
						 &new->q_base_pa, GFP_KERNEL);
		if (!new->q_base) {
			netdev_err(lif->netdev, "Cannot allocate qcq DMA memory\n");
			err = -ENOMEM;
			goto err_out_free_cq_info;
		}
		q_base = PTR_ALIGN(new->q_base, PAGE_SIZE);
		q_base_pa = ALIGN(new->q_base_pa, PAGE_SIZE);
		ionic_q_map(&new->q, q_base, q_base_pa);

		cq_base = PTR_ALIGN(q_base + q_size, PAGE_SIZE);
		cq_base_pa = ALIGN(new->q_base_pa + q_size, PAGE_SIZE);
		ionic_cq_map(&new->cq, cq_base, cq_base_pa);
		ionic_cq_bind(&new->cq, &new->q);
	} else {
		new->q_size = PAGE_SIZE + (num_descs * desc_size);
		new->q_base = dma_alloc_coherent(dev, new->q_size, &new->q_base_pa,
						 GFP_KERNEL);
		if (!new->q_base) {
			netdev_err(lif->netdev, "Cannot allocate queue DMA memory\n");
			err = -ENOMEM;
			goto err_out_free_cq_info;
		}
		q_base = PTR_ALIGN(new->q_base, PAGE_SIZE);
		q_base_pa = ALIGN(new->q_base_pa, PAGE_SIZE);
		ionic_q_map(&new->q, q_base, q_base_pa);

		new->cq_size = PAGE_SIZE + (num_descs * cq_desc_size);
		new->cq_base = dma_alloc_coherent(dev, new->cq_size, &new->cq_base_pa,
						  GFP_KERNEL);
		if (!new->cq_base) {
			netdev_err(lif->netdev, "Cannot allocate cq DMA memory\n");
			err = -ENOMEM;
			goto err_out_free_q;
		}
		cq_base = PTR_ALIGN(new->cq_base, PAGE_SIZE);
		cq_base_pa = ALIGN(new->cq_base_pa, PAGE_SIZE);
		ionic_cq_map(&new->cq, cq_base, cq_base_pa);
		ionic_cq_bind(&new->cq, &new->q);
603
	}
S
Shannon Nelson 已提交
604 605

	if (flags & IONIC_QCQ_F_SG) {
606 607 608 609 610 611 612 613 614 615
		new->sg_size = PAGE_SIZE + (num_descs * sg_desc_size);
		new->sg_base = dma_alloc_coherent(dev, new->sg_size, &new->sg_base_pa,
						  GFP_KERNEL);
		if (!new->sg_base) {
			netdev_err(lif->netdev, "Cannot allocate sg DMA memory\n");
			err = -ENOMEM;
			goto err_out_free_cq;
		}
		sg_base = PTR_ALIGN(new->sg_base, PAGE_SIZE);
		sg_base_pa = ALIGN(new->sg_base_pa, PAGE_SIZE);
S
Shannon Nelson 已提交
616 617 618
		ionic_q_sg_map(&new->q, sg_base, sg_base_pa);
	}

619 620 621
	INIT_WORK(&new->dim.work, ionic_dim_work);
	new->dim.mode = DIM_CQ_PERIOD_MODE_START_FROM_EQE;

S
Shannon Nelson 已提交
622 623 624 625
	*qcq = new;

	return 0;

626 627 628 629 630 631
err_out_free_cq:
	dma_free_coherent(dev, new->cq_size, new->cq_base, new->cq_base_pa);
err_out_free_q:
	dma_free_coherent(dev, new->q_size, new->q_base, new->q_base_pa);
err_out_free_cq_info:
	devm_kfree(dev, new->cq.info);
632
err_out_free_irq:
633
	if (flags & IONIC_QCQ_F_INTR) {
634
		devm_free_irq(dev, new->intr.vector, &new->napi);
635
		ionic_intr_free(lif->ionic, new->intr.index);
636
	}
637 638 639 640
err_out_free_q_info:
	devm_kfree(dev, new->q.info);
err_out_free_qcq:
	devm_kfree(dev, new);
S
Shannon Nelson 已提交
641 642 643 644 645 646 647
err_out:
	dev_err(dev, "qcq alloc of %s%d failed %d\n", name, index, err);
	return err;
}

static int ionic_qcqs_alloc(struct ionic_lif *lif)
{
S
Shannon Nelson 已提交
648
	struct device *dev = lif->ionic->dev;
S
Shannon Nelson 已提交
649 650 651 652 653 654 655 656 657 658 659
	unsigned int flags;
	int err;

	flags = IONIC_QCQ_F_INTR;
	err = ionic_qcq_alloc(lif, IONIC_QTYPE_ADMINQ, 0, "admin", flags,
			      IONIC_ADMINQ_LENGTH,
			      sizeof(struct ionic_admin_cmd),
			      sizeof(struct ionic_admin_comp),
			      0, lif->kern_pid, &lif->adminqcq);
	if (err)
		return err;
660
	ionic_debugfs_add_qcq(lif, lif->adminqcq);
S
Shannon Nelson 已提交
661

S
Shannon Nelson 已提交
662 663 664 665 666 667 668 669
	if (lif->ionic->nnqs_per_lif) {
		flags = IONIC_QCQ_F_NOTIFYQ;
		err = ionic_qcq_alloc(lif, IONIC_QTYPE_NOTIFYQ, 0, "notifyq",
				      flags, IONIC_NOTIFYQ_LENGTH,
				      sizeof(struct ionic_notifyq_cmd),
				      sizeof(union ionic_notifyq_comp),
				      0, lif->kern_pid, &lif->notifyqcq);
		if (err)
670
			goto err_out;
671
		ionic_debugfs_add_qcq(lif, lif->notifyqcq);
S
Shannon Nelson 已提交
672 673 674 675 676

		/* Let the notifyq ride on the adminq interrupt */
		ionic_link_qcq_interrupts(lif->adminqcq, lif->notifyqcq);
	}

S
Shannon Nelson 已提交
677
	err = -ENOMEM;
678
	lif->txqcqs = devm_kcalloc(dev, lif->ionic->ntxqs_per_lif,
679
				   sizeof(struct ionic_qcq *), GFP_KERNEL);
S
Shannon Nelson 已提交
680
	if (!lif->txqcqs)
681
		goto err_out;
682
	lif->rxqcqs = devm_kcalloc(dev, lif->ionic->nrxqs_per_lif,
683
				   sizeof(struct ionic_qcq *), GFP_KERNEL);
S
Shannon Nelson 已提交
684
	if (!lif->rxqcqs)
685
		goto err_out;
S
Shannon Nelson 已提交
686

687 688 689 690 691 692 693 694
	lif->txqstats = devm_kcalloc(dev, lif->ionic->ntxqs_per_lif,
				     sizeof(struct ionic_tx_stats), GFP_KERNEL);
	if (!lif->txqstats)
		goto err_out;
	lif->rxqstats = devm_kcalloc(dev, lif->ionic->nrxqs_per_lif,
				     sizeof(struct ionic_rx_stats), GFP_KERNEL);
	if (!lif->rxqstats)
		goto err_out;
S
Shannon Nelson 已提交
695

696
	return 0;
S
Shannon Nelson 已提交
697

698 699
err_out:
	ionic_qcqs_free(lif);
S
Shannon Nelson 已提交
700 701 702
	return err;
}

703 704 705 706 707 708 709 710 711 712 713
static void ionic_qcq_sanitize(struct ionic_qcq *qcq)
{
	qcq->q.tail_idx = 0;
	qcq->q.head_idx = 0;
	qcq->cq.tail_idx = 0;
	qcq->cq.done_color = 1;
	memset(qcq->q_base, 0, qcq->q_size);
	memset(qcq->cq_base, 0, qcq->cq_size);
	memset(qcq->sg_base, 0, qcq->sg_size);
}

S
Shannon Nelson 已提交
714 715 716 717 718 719 720 721 722 723 724
static int ionic_lif_txq_init(struct ionic_lif *lif, struct ionic_qcq *qcq)
{
	struct device *dev = lif->ionic->dev;
	struct ionic_queue *q = &qcq->q;
	struct ionic_cq *cq = &qcq->cq;
	struct ionic_admin_ctx ctx = {
		.work = COMPLETION_INITIALIZER_ONSTACK(ctx.work),
		.cmd.q_init = {
			.opcode = IONIC_CMD_Q_INIT,
			.lif_index = cpu_to_le16(lif->index),
			.type = q->type,
725
			.ver = lif->qtype_info[q->type].version,
S
Shannon Nelson 已提交
726 727 728 729 730 731 732 733 734 735
			.index = cpu_to_le32(q->index),
			.flags = cpu_to_le16(IONIC_QINIT_F_IRQ |
					     IONIC_QINIT_F_SG),
			.pid = cpu_to_le16(q->pid),
			.ring_size = ilog2(q->num_descs),
			.ring_base = cpu_to_le64(q->base_pa),
			.cq_ring_base = cpu_to_le64(cq->base_pa),
			.sg_ring_base = cpu_to_le64(q->sg_base_pa),
		},
	};
736
	unsigned int intr_index;
S
Shannon Nelson 已提交
737 738
	int err;

739 740
	intr_index = qcq->intr.index;

741 742
	ctx.cmd.q_init.intr_index = cpu_to_le16(intr_index);

S
Shannon Nelson 已提交
743 744 745 746
	dev_dbg(dev, "txq_init.pid %d\n", ctx.cmd.q_init.pid);
	dev_dbg(dev, "txq_init.index %d\n", ctx.cmd.q_init.index);
	dev_dbg(dev, "txq_init.ring_base 0x%llx\n", ctx.cmd.q_init.ring_base);
	dev_dbg(dev, "txq_init.ring_size %d\n", ctx.cmd.q_init.ring_size);
747 748
	dev_dbg(dev, "txq_init.flags 0x%x\n", ctx.cmd.q_init.flags);
	dev_dbg(dev, "txq_init.ver %d\n", ctx.cmd.q_init.ver);
749
	dev_dbg(dev, "txq_init.intr_index %d\n", ctx.cmd.q_init.intr_index);
S
Shannon Nelson 已提交
750

751
	ionic_qcq_sanitize(qcq);
752

S
Shannon Nelson 已提交
753 754 755 756 757 758 759 760 761 762 763
	err = ionic_adminq_post_wait(lif, &ctx);
	if (err)
		return err;

	q->hw_type = ctx.comp.q_init.hw_type;
	q->hw_index = le32_to_cpu(ctx.comp.q_init.hw_index);
	q->dbval = IONIC_DBELL_QID(q->hw_index);

	dev_dbg(dev, "txq->hw_type %d\n", q->hw_type);
	dev_dbg(dev, "txq->hw_index %d\n", q->hw_index);

764 765 766 767
	if (test_bit(IONIC_LIF_F_SPLIT_INTR, lif->state))
		netif_napi_add(lif->netdev, &qcq->napi, ionic_tx_napi,
			       NAPI_POLL_WEIGHT);

S
Shannon Nelson 已提交
768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783
	qcq->flags |= IONIC_QCQ_F_INITED;

	return 0;
}

static int ionic_lif_rxq_init(struct ionic_lif *lif, struct ionic_qcq *qcq)
{
	struct device *dev = lif->ionic->dev;
	struct ionic_queue *q = &qcq->q;
	struct ionic_cq *cq = &qcq->cq;
	struct ionic_admin_ctx ctx = {
		.work = COMPLETION_INITIALIZER_ONSTACK(ctx.work),
		.cmd.q_init = {
			.opcode = IONIC_CMD_Q_INIT,
			.lif_index = cpu_to_le16(lif->index),
			.type = q->type,
784
			.ver = lif->qtype_info[q->type].version,
S
Shannon Nelson 已提交
785
			.index = cpu_to_le32(q->index),
786 787
			.flags = cpu_to_le16(IONIC_QINIT_F_IRQ |
					     IONIC_QINIT_F_SG),
S
Shannon Nelson 已提交
788 789 790 791 792
			.intr_index = cpu_to_le16(cq->bound_intr->index),
			.pid = cpu_to_le16(q->pid),
			.ring_size = ilog2(q->num_descs),
			.ring_base = cpu_to_le64(q->base_pa),
			.cq_ring_base = cpu_to_le64(cq->base_pa),
793
			.sg_ring_base = cpu_to_le64(q->sg_base_pa),
S
Shannon Nelson 已提交
794 795 796 797 798 799 800 801
		},
	};
	int err;

	dev_dbg(dev, "rxq_init.pid %d\n", ctx.cmd.q_init.pid);
	dev_dbg(dev, "rxq_init.index %d\n", ctx.cmd.q_init.index);
	dev_dbg(dev, "rxq_init.ring_base 0x%llx\n", ctx.cmd.q_init.ring_base);
	dev_dbg(dev, "rxq_init.ring_size %d\n", ctx.cmd.q_init.ring_size);
802 803
	dev_dbg(dev, "rxq_init.flags 0x%x\n", ctx.cmd.q_init.flags);
	dev_dbg(dev, "rxq_init.ver %d\n", ctx.cmd.q_init.ver);
804
	dev_dbg(dev, "rxq_init.intr_index %d\n", ctx.cmd.q_init.intr_index);
S
Shannon Nelson 已提交
805

806
	ionic_qcq_sanitize(qcq);
807

S
Shannon Nelson 已提交
808 809 810 811 812 813 814 815 816 817 818
	err = ionic_adminq_post_wait(lif, &ctx);
	if (err)
		return err;

	q->hw_type = ctx.comp.q_init.hw_type;
	q->hw_index = le32_to_cpu(ctx.comp.q_init.hw_index);
	q->dbval = IONIC_DBELL_QID(q->hw_index);

	dev_dbg(dev, "rxq->hw_type %d\n", q->hw_type);
	dev_dbg(dev, "rxq->hw_index %d\n", q->hw_index);

819 820 821 822 823 824
	if (test_bit(IONIC_LIF_F_SPLIT_INTR, lif->state))
		netif_napi_add(lif->netdev, &qcq->napi, ionic_rx_napi,
			       NAPI_POLL_WEIGHT);
	else
		netif_napi_add(lif->netdev, &qcq->napi, ionic_txrx_napi,
			       NAPI_POLL_WEIGHT);
S
Shannon Nelson 已提交
825 826 827 828 829 830

	qcq->flags |= IONIC_QCQ_F_INITED;

	return 0;
}

S
Shannon Nelson 已提交
831 832 833 834
static bool ionic_notifyq_service(struct ionic_cq *cq,
				  struct ionic_cq_info *cq_info)
{
	union ionic_notifyq_comp *comp = cq_info->cq_desc;
S
Shannon Nelson 已提交
835
	struct ionic_deferred_work *work;
S
Shannon Nelson 已提交
836 837 838 839 840 841 842 843 844 845 846
	struct net_device *netdev;
	struct ionic_queue *q;
	struct ionic_lif *lif;
	u64 eid;

	q = cq->bound_q;
	lif = q->info[0].cb_arg;
	netdev = lif->netdev;
	eid = le64_to_cpu(comp->event.eid);

	/* Have we run out of new completions to process? */
847
	if ((s64)(eid - lif->last_eid) <= 0)
S
Shannon Nelson 已提交
848 849 850 851 852 853 854 855 856 857
		return false;

	lif->last_eid = eid;

	dev_dbg(lif->ionic->dev, "notifyq event:\n");
	dynamic_hex_dump("event ", DUMP_PREFIX_OFFSET, 16, 1,
			 comp, sizeof(*comp), true);

	switch (le16_to_cpu(comp->event.ecode)) {
	case IONIC_EVENT_LINK_CHANGE:
S
Shannon Nelson 已提交
858
		ionic_link_status_check_request(lif, CAN_NOT_SLEEP);
S
Shannon Nelson 已提交
859 860
		break;
	case IONIC_EVENT_RESET:
S
Shannon Nelson 已提交
861 862
		work = kzalloc(sizeof(*work), GFP_ATOMIC);
		if (!work) {
863
			netdev_err(lif->netdev, "Reset event dropped\n");
S
Shannon Nelson 已提交
864 865 866 867
		} else {
			work->type = IONIC_DW_TYPE_LIF_RESET;
			ionic_lif_deferred_enqueue(&lif->deferred, work);
		}
S
Shannon Nelson 已提交
868 869
		break;
	default:
870
		netdev_warn(netdev, "Notifyq event ecode=%d eid=%lld\n",
S
Shannon Nelson 已提交
871 872 873 874 875 876 877
			    comp->event.ecode, eid);
		break;
	}

	return true;
}

S
Shannon Nelson 已提交
878 879 880 881 882 883 884 885 886 887 888 889 890 891 892
static bool ionic_adminq_service(struct ionic_cq *cq,
				 struct ionic_cq_info *cq_info)
{
	struct ionic_admin_comp *comp = cq_info->cq_desc;

	if (!color_match(comp->color, cq->done_color))
		return false;

	ionic_q_service(cq->bound_q, cq_info, le16_to_cpu(comp->comp_index));

	return true;
}

static int ionic_adminq_napi(struct napi_struct *napi, int budget)
{
893
	struct ionic_intr_info *intr = napi_to_cq(napi)->bound_intr;
S
Shannon Nelson 已提交
894
	struct ionic_lif *lif = napi_to_cq(napi)->lif;
895
	struct ionic_dev *idev = &lif->ionic->idev;
896
	unsigned long irqflags;
897
	unsigned int flags = 0;
S
Shannon Nelson 已提交
898 899
	int n_work = 0;
	int a_work = 0;
900 901 902 903 904
	int work_done;

	if (lif->notifyqcq && lif->notifyqcq->flags & IONIC_QCQ_F_INITED)
		n_work = ionic_cq_service(&lif->notifyqcq->cq, budget,
					  ionic_notifyq_service, NULL, NULL);
S
Shannon Nelson 已提交
905

906
	spin_lock_irqsave(&lif->adminq_lock, irqflags);
907 908 909
	if (lif->adminqcq && lif->adminqcq->flags & IONIC_QCQ_F_INITED)
		a_work = ionic_cq_service(&lif->adminqcq->cq, budget,
					  ionic_adminq_service, NULL, NULL);
910
	spin_unlock_irqrestore(&lif->adminq_lock, irqflags);
911 912 913 914

	work_done = max(n_work, a_work);
	if (work_done < budget && napi_complete_done(napi, work_done)) {
		flags |= IONIC_INTR_CRED_UNMASK;
S
Shannon Nelson 已提交
915
		intr->rearm_count++;
916
	}
S
Shannon Nelson 已提交
917

918 919 920 921 922 923 924 925
	if (work_done || flags) {
		flags |= IONIC_INTR_CRED_RESET_COALESCE;
		ionic_intr_credits(idev->intr_ctrl,
				   intr->index,
				   n_work + a_work, flags);
	}

	return work_done;
S
Shannon Nelson 已提交
926 927
}

S
Shannon Nelson 已提交
928 929
void ionic_get_stats64(struct net_device *netdev,
		       struct rtnl_link_stats64 *ns)
930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980
{
	struct ionic_lif *lif = netdev_priv(netdev);
	struct ionic_lif_stats *ls;

	memset(ns, 0, sizeof(*ns));
	ls = &lif->info->stats;

	ns->rx_packets = le64_to_cpu(ls->rx_ucast_packets) +
			 le64_to_cpu(ls->rx_mcast_packets) +
			 le64_to_cpu(ls->rx_bcast_packets);

	ns->tx_packets = le64_to_cpu(ls->tx_ucast_packets) +
			 le64_to_cpu(ls->tx_mcast_packets) +
			 le64_to_cpu(ls->tx_bcast_packets);

	ns->rx_bytes = le64_to_cpu(ls->rx_ucast_bytes) +
		       le64_to_cpu(ls->rx_mcast_bytes) +
		       le64_to_cpu(ls->rx_bcast_bytes);

	ns->tx_bytes = le64_to_cpu(ls->tx_ucast_bytes) +
		       le64_to_cpu(ls->tx_mcast_bytes) +
		       le64_to_cpu(ls->tx_bcast_bytes);

	ns->rx_dropped = le64_to_cpu(ls->rx_ucast_drop_packets) +
			 le64_to_cpu(ls->rx_mcast_drop_packets) +
			 le64_to_cpu(ls->rx_bcast_drop_packets);

	ns->tx_dropped = le64_to_cpu(ls->tx_ucast_drop_packets) +
			 le64_to_cpu(ls->tx_mcast_drop_packets) +
			 le64_to_cpu(ls->tx_bcast_drop_packets);

	ns->multicast = le64_to_cpu(ls->rx_mcast_packets);

	ns->rx_over_errors = le64_to_cpu(ls->rx_queue_empty);

	ns->rx_missed_errors = le64_to_cpu(ls->rx_dma_error) +
			       le64_to_cpu(ls->rx_queue_disabled) +
			       le64_to_cpu(ls->rx_desc_fetch_error) +
			       le64_to_cpu(ls->rx_desc_data_error);

	ns->tx_aborted_errors = le64_to_cpu(ls->tx_dma_error) +
				le64_to_cpu(ls->tx_queue_disabled) +
				le64_to_cpu(ls->tx_desc_fetch_error) +
				le64_to_cpu(ls->tx_desc_data_error);

	ns->rx_errors = ns->rx_over_errors +
			ns->rx_missed_errors;

	ns->tx_errors = ns->tx_aborted_errors;
}

981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000
static int ionic_lif_addr_add(struct ionic_lif *lif, const u8 *addr)
{
	struct ionic_admin_ctx ctx = {
		.work = COMPLETION_INITIALIZER_ONSTACK(ctx.work),
		.cmd.rx_filter_add = {
			.opcode = IONIC_CMD_RX_FILTER_ADD,
			.lif_index = cpu_to_le16(lif->index),
			.match = cpu_to_le16(IONIC_RX_FILTER_MATCH_MAC),
		},
	};
	struct ionic_rx_filter *f;
	int err;

	/* don't bother if we already have it */
	spin_lock_bh(&lif->rx_filters.lock);
	f = ionic_rx_filter_by_addr(lif, addr);
	spin_unlock_bh(&lif->rx_filters.lock);
	if (f)
		return 0;

1001
	netdev_dbg(lif->netdev, "rx_filter add ADDR %pM\n", addr);
1002 1003 1004

	memcpy(ctx.cmd.rx_filter_add.mac.addr, addr, ETH_ALEN);
	err = ionic_adminq_post_wait(lif, &ctx);
1005
	if (err && err != -EEXIST)
1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029
		return err;

	return ionic_rx_filter_save(lif, 0, IONIC_RXQ_INDEX_ANY, 0, &ctx);
}

static int ionic_lif_addr_del(struct ionic_lif *lif, const u8 *addr)
{
	struct ionic_admin_ctx ctx = {
		.work = COMPLETION_INITIALIZER_ONSTACK(ctx.work),
		.cmd.rx_filter_del = {
			.opcode = IONIC_CMD_RX_FILTER_DEL,
			.lif_index = cpu_to_le16(lif->index),
		},
	};
	struct ionic_rx_filter *f;
	int err;

	spin_lock_bh(&lif->rx_filters.lock);
	f = ionic_rx_filter_by_addr(lif, addr);
	if (!f) {
		spin_unlock_bh(&lif->rx_filters.lock);
		return -ENOENT;
	}

1030 1031 1032
	netdev_dbg(lif->netdev, "rx_filter del ADDR %pM (id %d)\n",
		   addr, f->filter_id);

1033 1034 1035 1036 1037
	ctx.cmd.rx_filter_del.filter_id = cpu_to_le32(f->filter_id);
	ionic_rx_filter_free(lif, f);
	spin_unlock_bh(&lif->rx_filters.lock);

	err = ionic_adminq_post_wait(lif, &ctx);
1038
	if (err && err != -EEXIST)
1039 1040 1041 1042 1043
		return err;

	return 0;
}

1044 1045
static int ionic_lif_addr(struct ionic_lif *lif, const u8 *addr, bool add,
			  bool can_sleep)
1046 1047 1048 1049 1050 1051 1052 1053 1054 1055
{
	struct ionic_deferred_work *work;
	unsigned int nmfilters;
	unsigned int nufilters;

	if (add) {
		/* Do we have space for this filter?  We test the counters
		 * here before checking the need for deferral so that we
		 * can return an overflow error to the stack.
		 */
1056 1057
		nmfilters = le32_to_cpu(lif->identity->eth.max_mcast_filters);
		nufilters = le32_to_cpu(lif->identity->eth.max_ucast_filters);
1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072

		if ((is_multicast_ether_addr(addr) && lif->nmcast < nmfilters))
			lif->nmcast++;
		else if (!is_multicast_ether_addr(addr) &&
			 lif->nucast < nufilters)
			lif->nucast++;
		else
			return -ENOSPC;
	} else {
		if (is_multicast_ether_addr(addr) && lif->nmcast)
			lif->nmcast--;
		else if (!is_multicast_ether_addr(addr) && lif->nucast)
			lif->nucast--;
	}

1073
	if (!can_sleep) {
1074
		work = kzalloc(sizeof(*work), GFP_ATOMIC);
1075
		if (!work)
1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096
			return -ENOMEM;
		work->type = add ? IONIC_DW_TYPE_RX_ADDR_ADD :
				   IONIC_DW_TYPE_RX_ADDR_DEL;
		memcpy(work->addr, addr, ETH_ALEN);
		netdev_dbg(lif->netdev, "deferred: rx_filter %s %pM\n",
			   add ? "add" : "del", addr);
		ionic_lif_deferred_enqueue(&lif->deferred, work);
	} else {
		netdev_dbg(lif->netdev, "rx_filter %s %pM\n",
			   add ? "add" : "del", addr);
		if (add)
			return ionic_lif_addr_add(lif, addr);
		else
			return ionic_lif_addr_del(lif, addr);
	}

	return 0;
}

static int ionic_addr_add(struct net_device *netdev, const u8 *addr)
{
S
Shannon Nelson 已提交
1097
	return ionic_lif_addr(netdev_priv(netdev), addr, ADD_ADDR, CAN_SLEEP);
1098 1099 1100 1101
}

static int ionic_ndo_addr_add(struct net_device *netdev, const u8 *addr)
{
S
Shannon Nelson 已提交
1102
	return ionic_lif_addr(netdev_priv(netdev), addr, ADD_ADDR, CAN_NOT_SLEEP);
1103 1104 1105 1106
}

static int ionic_addr_del(struct net_device *netdev, const u8 *addr)
{
S
Shannon Nelson 已提交
1107
	return ionic_lif_addr(netdev_priv(netdev), addr, DEL_ADDR, CAN_SLEEP);
1108 1109 1110 1111
}

static int ionic_ndo_addr_del(struct net_device *netdev, const u8 *addr)
{
S
Shannon Nelson 已提交
1112
	return ionic_lif_addr(netdev_priv(netdev), addr, DEL_ADDR, CAN_NOT_SLEEP);
1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129
}

static void ionic_lif_rx_mode(struct ionic_lif *lif, unsigned int rx_mode)
{
	struct ionic_admin_ctx ctx = {
		.work = COMPLETION_INITIALIZER_ONSTACK(ctx.work),
		.cmd.rx_mode_set = {
			.opcode = IONIC_CMD_RX_MODE_SET,
			.lif_index = cpu_to_le16(lif->index),
			.rx_mode = cpu_to_le16(rx_mode),
		},
	};
	char buf[128];
	int err;
	int i;
#define REMAIN(__x) (sizeof(buf) - (__x))

1130 1131
	i = scnprintf(buf, sizeof(buf), "rx_mode 0x%04x -> 0x%04x:",
		      lif->rx_mode, rx_mode);
1132
	if (rx_mode & IONIC_RX_MODE_F_UNICAST)
1133
		i += scnprintf(&buf[i], REMAIN(i), " RX_MODE_F_UNICAST");
1134
	if (rx_mode & IONIC_RX_MODE_F_MULTICAST)
1135
		i += scnprintf(&buf[i], REMAIN(i), " RX_MODE_F_MULTICAST");
1136
	if (rx_mode & IONIC_RX_MODE_F_BROADCAST)
1137
		i += scnprintf(&buf[i], REMAIN(i), " RX_MODE_F_BROADCAST");
1138
	if (rx_mode & IONIC_RX_MODE_F_PROMISC)
1139
		i += scnprintf(&buf[i], REMAIN(i), " RX_MODE_F_PROMISC");
1140
	if (rx_mode & IONIC_RX_MODE_F_ALLMULTI)
1141
		i += scnprintf(&buf[i], REMAIN(i), " RX_MODE_F_ALLMULTI");
1142 1143 1144 1145 1146 1147 1148 1149 1150 1151
	netdev_dbg(lif->netdev, "lif%d %s\n", lif->index, buf);

	err = ionic_adminq_post_wait(lif, &ctx);
	if (err)
		netdev_warn(lif->netdev, "set rx_mode 0x%04x failed: %d\n",
			    rx_mode, err);
	else
		lif->rx_mode = rx_mode;
}

1152
static void ionic_set_rx_mode(struct net_device *netdev, bool can_sleep)
1153 1154
{
	struct ionic_lif *lif = netdev_priv(netdev);
1155
	struct ionic_deferred_work *work;
1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171
	unsigned int nfilters;
	unsigned int rx_mode;

	rx_mode = IONIC_RX_MODE_F_UNICAST;
	rx_mode |= (netdev->flags & IFF_MULTICAST) ? IONIC_RX_MODE_F_MULTICAST : 0;
	rx_mode |= (netdev->flags & IFF_BROADCAST) ? IONIC_RX_MODE_F_BROADCAST : 0;
	rx_mode |= (netdev->flags & IFF_PROMISC) ? IONIC_RX_MODE_F_PROMISC : 0;
	rx_mode |= (netdev->flags & IFF_ALLMULTI) ? IONIC_RX_MODE_F_ALLMULTI : 0;

	/* sync unicast addresses
	 * next check to see if we're in an overflow state
	 *    if so, we track that we overflowed and enable NIC PROMISC
	 *    else if the overflow is set and not needed
	 *       we remove our overflow flag and check the netdev flags
	 *       to see if we can disable NIC PROMISC
	 */
1172
	if (can_sleep)
1173
		__dev_uc_sync(netdev, ionic_addr_add, ionic_addr_del);
1174 1175
	else
		__dev_uc_sync(netdev, ionic_ndo_addr_add, ionic_ndo_addr_del);
1176
	nfilters = le32_to_cpu(lif->identity->eth.max_ucast_filters);
1177 1178 1179 1180 1181 1182 1183 1184 1185 1186
	if (netdev_uc_count(netdev) + 1 > nfilters) {
		rx_mode |= IONIC_RX_MODE_F_PROMISC;
		lif->uc_overflow = true;
	} else if (lif->uc_overflow) {
		lif->uc_overflow = false;
		if (!(netdev->flags & IFF_PROMISC))
			rx_mode &= ~IONIC_RX_MODE_F_PROMISC;
	}

	/* same for multicast */
1187
	if (can_sleep)
1188
		__dev_mc_sync(netdev, ionic_addr_add, ionic_addr_del);
1189 1190
	else
		__dev_mc_sync(netdev, ionic_ndo_addr_add, ionic_ndo_addr_del);
1191
	nfilters = le32_to_cpu(lif->identity->eth.max_mcast_filters);
1192 1193 1194 1195 1196 1197 1198 1199 1200
	if (netdev_mc_count(netdev) > nfilters) {
		rx_mode |= IONIC_RX_MODE_F_ALLMULTI;
		lif->mc_overflow = true;
	} else if (lif->mc_overflow) {
		lif->mc_overflow = false;
		if (!(netdev->flags & IFF_ALLMULTI))
			rx_mode &= ~IONIC_RX_MODE_F_ALLMULTI;
	}

1201
	if (lif->rx_mode != rx_mode) {
1202
		if (!can_sleep) {
1203 1204
			work = kzalloc(sizeof(*work), GFP_ATOMIC);
			if (!work) {
1205
				netdev_err(lif->netdev, "rxmode change dropped\n");
1206 1207 1208 1209 1210 1211 1212 1213 1214 1215
				return;
			}
			work->type = IONIC_DW_TYPE_RX_MODE;
			work->rx_mode = rx_mode;
			netdev_dbg(lif->netdev, "deferred: rx_mode\n");
			ionic_lif_deferred_enqueue(&lif->deferred, work);
		} else {
			ionic_lif_rx_mode(lif, rx_mode);
		}
	}
1216 1217 1218 1219
}

static void ionic_ndo_set_rx_mode(struct net_device *netdev)
{
S
Shannon Nelson 已提交
1220
	ionic_set_rx_mode(netdev, CAN_NOT_SLEEP);
1221 1222
}

1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277
static __le64 ionic_netdev_features_to_nic(netdev_features_t features)
{
	u64 wanted = 0;

	if (features & NETIF_F_HW_VLAN_CTAG_TX)
		wanted |= IONIC_ETH_HW_VLAN_TX_TAG;
	if (features & NETIF_F_HW_VLAN_CTAG_RX)
		wanted |= IONIC_ETH_HW_VLAN_RX_STRIP;
	if (features & NETIF_F_HW_VLAN_CTAG_FILTER)
		wanted |= IONIC_ETH_HW_VLAN_RX_FILTER;
	if (features & NETIF_F_RXHASH)
		wanted |= IONIC_ETH_HW_RX_HASH;
	if (features & NETIF_F_RXCSUM)
		wanted |= IONIC_ETH_HW_RX_CSUM;
	if (features & NETIF_F_SG)
		wanted |= IONIC_ETH_HW_TX_SG;
	if (features & NETIF_F_HW_CSUM)
		wanted |= IONIC_ETH_HW_TX_CSUM;
	if (features & NETIF_F_TSO)
		wanted |= IONIC_ETH_HW_TSO;
	if (features & NETIF_F_TSO6)
		wanted |= IONIC_ETH_HW_TSO_IPV6;
	if (features & NETIF_F_TSO_ECN)
		wanted |= IONIC_ETH_HW_TSO_ECN;
	if (features & NETIF_F_GSO_GRE)
		wanted |= IONIC_ETH_HW_TSO_GRE;
	if (features & NETIF_F_GSO_GRE_CSUM)
		wanted |= IONIC_ETH_HW_TSO_GRE_CSUM;
	if (features & NETIF_F_GSO_IPXIP4)
		wanted |= IONIC_ETH_HW_TSO_IPXIP4;
	if (features & NETIF_F_GSO_IPXIP6)
		wanted |= IONIC_ETH_HW_TSO_IPXIP6;
	if (features & NETIF_F_GSO_UDP_TUNNEL)
		wanted |= IONIC_ETH_HW_TSO_UDP;
	if (features & NETIF_F_GSO_UDP_TUNNEL_CSUM)
		wanted |= IONIC_ETH_HW_TSO_UDP_CSUM;

	return cpu_to_le64(wanted);
}

static int ionic_set_nic_features(struct ionic_lif *lif,
				  netdev_features_t features)
{
	struct device *dev = lif->ionic->dev;
	struct ionic_admin_ctx ctx = {
		.work = COMPLETION_INITIALIZER_ONSTACK(ctx.work),
		.cmd.lif_setattr = {
			.opcode = IONIC_CMD_LIF_SETATTR,
			.index = cpu_to_le16(lif->index),
			.attr = IONIC_LIF_ATTR_FEATURES,
		},
	};
	u64 vlan_flags = IONIC_ETH_HW_VLAN_TX_TAG |
			 IONIC_ETH_HW_VLAN_RX_STRIP |
			 IONIC_ETH_HW_VLAN_RX_FILTER;
1278
	u64 old_hw_features;
1279 1280 1281 1282 1283 1284 1285
	int err;

	ctx.cmd.lif_setattr.features = ionic_netdev_features_to_nic(features);
	err = ionic_adminq_post_wait(lif, &ctx);
	if (err)
		return err;

1286
	old_hw_features = lif->hw_features;
1287 1288 1289
	lif->hw_features = le64_to_cpu(ctx.cmd.lif_setattr.features &
				       ctx.comp.lif_setattr.features);

1290 1291 1292
	if ((old_hw_features ^ lif->hw_features) & IONIC_ETH_HW_RX_HASH)
		ionic_lif_rss_config(lif, lif->rss_types, NULL, NULL);

1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393
	if ((vlan_flags & features) &&
	    !(vlan_flags & le64_to_cpu(ctx.comp.lif_setattr.features)))
		dev_info_once(lif->ionic->dev, "NIC is not supporting vlan offload, likely in SmartNIC mode\n");

	if (lif->hw_features & IONIC_ETH_HW_VLAN_TX_TAG)
		dev_dbg(dev, "feature ETH_HW_VLAN_TX_TAG\n");
	if (lif->hw_features & IONIC_ETH_HW_VLAN_RX_STRIP)
		dev_dbg(dev, "feature ETH_HW_VLAN_RX_STRIP\n");
	if (lif->hw_features & IONIC_ETH_HW_VLAN_RX_FILTER)
		dev_dbg(dev, "feature ETH_HW_VLAN_RX_FILTER\n");
	if (lif->hw_features & IONIC_ETH_HW_RX_HASH)
		dev_dbg(dev, "feature ETH_HW_RX_HASH\n");
	if (lif->hw_features & IONIC_ETH_HW_TX_SG)
		dev_dbg(dev, "feature ETH_HW_TX_SG\n");
	if (lif->hw_features & IONIC_ETH_HW_TX_CSUM)
		dev_dbg(dev, "feature ETH_HW_TX_CSUM\n");
	if (lif->hw_features & IONIC_ETH_HW_RX_CSUM)
		dev_dbg(dev, "feature ETH_HW_RX_CSUM\n");
	if (lif->hw_features & IONIC_ETH_HW_TSO)
		dev_dbg(dev, "feature ETH_HW_TSO\n");
	if (lif->hw_features & IONIC_ETH_HW_TSO_IPV6)
		dev_dbg(dev, "feature ETH_HW_TSO_IPV6\n");
	if (lif->hw_features & IONIC_ETH_HW_TSO_ECN)
		dev_dbg(dev, "feature ETH_HW_TSO_ECN\n");
	if (lif->hw_features & IONIC_ETH_HW_TSO_GRE)
		dev_dbg(dev, "feature ETH_HW_TSO_GRE\n");
	if (lif->hw_features & IONIC_ETH_HW_TSO_GRE_CSUM)
		dev_dbg(dev, "feature ETH_HW_TSO_GRE_CSUM\n");
	if (lif->hw_features & IONIC_ETH_HW_TSO_IPXIP4)
		dev_dbg(dev, "feature ETH_HW_TSO_IPXIP4\n");
	if (lif->hw_features & IONIC_ETH_HW_TSO_IPXIP6)
		dev_dbg(dev, "feature ETH_HW_TSO_IPXIP6\n");
	if (lif->hw_features & IONIC_ETH_HW_TSO_UDP)
		dev_dbg(dev, "feature ETH_HW_TSO_UDP\n");
	if (lif->hw_features & IONIC_ETH_HW_TSO_UDP_CSUM)
		dev_dbg(dev, "feature ETH_HW_TSO_UDP_CSUM\n");

	return 0;
}

static int ionic_init_nic_features(struct ionic_lif *lif)
{
	struct net_device *netdev = lif->netdev;
	netdev_features_t features;
	int err;

	/* set up what we expect to support by default */
	features = NETIF_F_HW_VLAN_CTAG_TX |
		   NETIF_F_HW_VLAN_CTAG_RX |
		   NETIF_F_HW_VLAN_CTAG_FILTER |
		   NETIF_F_RXHASH |
		   NETIF_F_SG |
		   NETIF_F_HW_CSUM |
		   NETIF_F_RXCSUM |
		   NETIF_F_TSO |
		   NETIF_F_TSO6 |
		   NETIF_F_TSO_ECN;

	err = ionic_set_nic_features(lif, features);
	if (err)
		return err;

	/* tell the netdev what we actually can support */
	netdev->features |= NETIF_F_HIGHDMA;

	if (lif->hw_features & IONIC_ETH_HW_VLAN_TX_TAG)
		netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_TX;
	if (lif->hw_features & IONIC_ETH_HW_VLAN_RX_STRIP)
		netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_RX;
	if (lif->hw_features & IONIC_ETH_HW_VLAN_RX_FILTER)
		netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_FILTER;
	if (lif->hw_features & IONIC_ETH_HW_RX_HASH)
		netdev->hw_features |= NETIF_F_RXHASH;
	if (lif->hw_features & IONIC_ETH_HW_TX_SG)
		netdev->hw_features |= NETIF_F_SG;

	if (lif->hw_features & IONIC_ETH_HW_TX_CSUM)
		netdev->hw_enc_features |= NETIF_F_HW_CSUM;
	if (lif->hw_features & IONIC_ETH_HW_RX_CSUM)
		netdev->hw_enc_features |= NETIF_F_RXCSUM;
	if (lif->hw_features & IONIC_ETH_HW_TSO)
		netdev->hw_enc_features |= NETIF_F_TSO;
	if (lif->hw_features & IONIC_ETH_HW_TSO_IPV6)
		netdev->hw_enc_features |= NETIF_F_TSO6;
	if (lif->hw_features & IONIC_ETH_HW_TSO_ECN)
		netdev->hw_enc_features |= NETIF_F_TSO_ECN;
	if (lif->hw_features & IONIC_ETH_HW_TSO_GRE)
		netdev->hw_enc_features |= NETIF_F_GSO_GRE;
	if (lif->hw_features & IONIC_ETH_HW_TSO_GRE_CSUM)
		netdev->hw_enc_features |= NETIF_F_GSO_GRE_CSUM;
	if (lif->hw_features & IONIC_ETH_HW_TSO_IPXIP4)
		netdev->hw_enc_features |= NETIF_F_GSO_IPXIP4;
	if (lif->hw_features & IONIC_ETH_HW_TSO_IPXIP6)
		netdev->hw_enc_features |= NETIF_F_GSO_IPXIP6;
	if (lif->hw_features & IONIC_ETH_HW_TSO_UDP)
		netdev->hw_enc_features |= NETIF_F_GSO_UDP_TUNNEL;
	if (lif->hw_features & IONIC_ETH_HW_TSO_UDP_CSUM)
		netdev->hw_enc_features |= NETIF_F_GSO_UDP_TUNNEL_CSUM;

	netdev->hw_features |= netdev->hw_enc_features;
	netdev->features |= netdev->hw_features;
1394
	netdev->vlan_features |= netdev->features & ~NETIF_F_VLAN_FEATURES;
1395

S
Shannon Nelson 已提交
1396 1397
	netdev->priv_flags |= IFF_UNICAST_FLT |
			      IFF_LIVE_ADDR_CHANGE;
1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417

	return 0;
}

static int ionic_set_features(struct net_device *netdev,
			      netdev_features_t features)
{
	struct ionic_lif *lif = netdev_priv(netdev);
	int err;

	netdev_dbg(netdev, "%s: lif->features=0x%08llx new_features=0x%08llx\n",
		   __func__, (u64)lif->netdev->features, (u64)features);

	err = ionic_set_nic_features(lif, features);

	return err;
}

static int ionic_set_mac_address(struct net_device *netdev, void *sa)
{
1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439
	struct sockaddr *addr = sa;
	u8 *mac;
	int err;

	mac = (u8 *)addr->sa_data;
	if (ether_addr_equal(netdev->dev_addr, mac))
		return 0;

	err = eth_prepare_mac_addr_change(netdev, addr);
	if (err)
		return err;

	if (!is_zero_ether_addr(netdev->dev_addr)) {
		netdev_info(netdev, "deleting mac addr %pM\n",
			    netdev->dev_addr);
		ionic_addr_del(netdev, netdev->dev_addr);
	}

	eth_commit_mac_addr_change(netdev, addr);
	netdev_info(netdev, "updating mac addr %pM\n", mac);

	return ionic_addr_add(netdev, mac);
1440 1441
}

1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464
static void ionic_stop_queues_reconfig(struct ionic_lif *lif)
{
	/* Stop and clean the queues before reconfiguration */
	mutex_lock(&lif->queue_lock);
	netif_device_detach(lif->netdev);
	ionic_stop_queues(lif);
	ionic_txrx_deinit(lif);
}

static int ionic_start_queues_reconfig(struct ionic_lif *lif)
{
	int err;

	/* Re-init the queues after reconfiguration */

	/* The only way txrx_init can fail here is if communication
	 * with FW is suddenly broken.  There's not much we can do
	 * at this point - error messages have already been printed,
	 * so we can continue on and the user can eventually do a
	 * DOWN and UP to try to reset and clear the issue.
	 */
	err = ionic_txrx_init(lif);
	mutex_unlock(&lif->queue_lock);
S
Shannon Nelson 已提交
1465
	ionic_link_status_check_request(lif, CAN_SLEEP);
1466 1467 1468 1469 1470
	netif_device_attach(lif->netdev);

	return err;
}

1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488
static int ionic_change_mtu(struct net_device *netdev, int new_mtu)
{
	struct ionic_lif *lif = netdev_priv(netdev);
	struct ionic_admin_ctx ctx = {
		.work = COMPLETION_INITIALIZER_ONSTACK(ctx.work),
		.cmd.lif_setattr = {
			.opcode = IONIC_CMD_LIF_SETATTR,
			.index = cpu_to_le16(lif->index),
			.attr = IONIC_LIF_ATTR_MTU,
			.mtu = cpu_to_le32(new_mtu),
		},
	};
	int err;

	err = ionic_adminq_post_wait(lif, &ctx);
	if (err)
		return err;

1489
	/* if we're not running, nothing more to do */
1490 1491
	if (!netif_running(netdev)) {
		netdev->mtu = new_mtu;
1492
		return 0;
1493
	}
1494

1495
	ionic_stop_queues_reconfig(lif);
1496
	netdev->mtu = new_mtu;
1497
	return ionic_start_queues_reconfig(lif);
1498 1499
}

1500 1501 1502 1503
static void ionic_tx_timeout_work(struct work_struct *ws)
{
	struct ionic_lif *lif = container_of(ws, struct ionic_lif, tx_timeout_work);

1504 1505
	if (test_bit(IONIC_LIF_F_FW_RESET, lif->state))
		return;
1506

1507 1508 1509 1510 1511 1512 1513 1514
	/* if we were stopped before this scheduled job was launched,
	 * don't bother the queues as they are already stopped.
	 */
	if (!netif_running(lif->netdev))
		return;

	ionic_stop_queues_reconfig(lif);
	ionic_start_queues_reconfig(lif);
1515 1516
}

1517
static void ionic_tx_timeout(struct net_device *netdev, unsigned int txqueue)
1518
{
1519 1520
	struct ionic_lif *lif = netdev_priv(netdev);

1521
	netdev_info(lif->netdev, "Tx Timeout triggered - txq %d\n", txqueue);
1522
	schedule_work(&lif->tx_timeout_work);
1523 1524 1525 1526 1527
}

static int ionic_vlan_rx_add_vid(struct net_device *netdev, __be16 proto,
				 u16 vid)
{
1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539
	struct ionic_lif *lif = netdev_priv(netdev);
	struct ionic_admin_ctx ctx = {
		.work = COMPLETION_INITIALIZER_ONSTACK(ctx.work),
		.cmd.rx_filter_add = {
			.opcode = IONIC_CMD_RX_FILTER_ADD,
			.lif_index = cpu_to_le16(lif->index),
			.match = cpu_to_le16(IONIC_RX_FILTER_MATCH_VLAN),
			.vlan.vlan = cpu_to_le16(vid),
		},
	};
	int err;

1540
	netdev_dbg(netdev, "rx_filter add VLAN %d\n", vid);
1541 1542 1543 1544 1545
	err = ionic_adminq_post_wait(lif, &ctx);
	if (err)
		return err;

	return ionic_rx_filter_save(lif, 0, IONIC_RXQ_INDEX_ANY, 0, &ctx);
1546 1547 1548 1549 1550
}

static int ionic_vlan_rx_kill_vid(struct net_device *netdev, __be16 proto,
				  u16 vid)
{
1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568
	struct ionic_lif *lif = netdev_priv(netdev);
	struct ionic_admin_ctx ctx = {
		.work = COMPLETION_INITIALIZER_ONSTACK(ctx.work),
		.cmd.rx_filter_del = {
			.opcode = IONIC_CMD_RX_FILTER_DEL,
			.lif_index = cpu_to_le16(lif->index),
		},
	};
	struct ionic_rx_filter *f;

	spin_lock_bh(&lif->rx_filters.lock);

	f = ionic_rx_filter_by_vlan(lif, vid);
	if (!f) {
		spin_unlock_bh(&lif->rx_filters.lock);
		return -ENOENT;
	}

1569 1570
	netdev_dbg(netdev, "rx_filter del VLAN %d (id %d)\n",
		   vid, f->filter_id);
1571 1572 1573 1574 1575 1576

	ctx.cmd.rx_filter_del.filter_id = cpu_to_le32(f->filter_id);
	ionic_rx_filter_free(lif, f);
	spin_unlock_bh(&lif->rx_filters.lock);

	return ionic_adminq_post_wait(lif, &ctx);
1577 1578
}

S
Shannon Nelson 已提交
1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591
int ionic_lif_rss_config(struct ionic_lif *lif, const u16 types,
			 const u8 *key, const u32 *indir)
{
	struct ionic_admin_ctx ctx = {
		.work = COMPLETION_INITIALIZER_ONSTACK(ctx.work),
		.cmd.lif_setattr = {
			.opcode = IONIC_CMD_LIF_SETATTR,
			.attr = IONIC_LIF_ATTR_RSS,
			.rss.addr = cpu_to_le64(lif->rss_ind_tbl_pa),
		},
	};
	unsigned int i, tbl_sz;

1592 1593 1594 1595
	if (lif->hw_features & IONIC_ETH_HW_RX_HASH) {
		lif->rss_types = types;
		ctx.cmd.lif_setattr.rss.types = cpu_to_le16(types);
	}
S
Shannon Nelson 已提交
1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628

	if (key)
		memcpy(lif->rss_hash_key, key, IONIC_RSS_HASH_KEY_SIZE);

	if (indir) {
		tbl_sz = le16_to_cpu(lif->ionic->ident.lif.eth.rss_ind_tbl_sz);
		for (i = 0; i < tbl_sz; i++)
			lif->rss_ind_tbl[i] = indir[i];
	}

	memcpy(ctx.cmd.lif_setattr.rss.key, lif->rss_hash_key,
	       IONIC_RSS_HASH_KEY_SIZE);

	return ionic_adminq_post_wait(lif, &ctx);
}

static int ionic_lif_rss_init(struct ionic_lif *lif)
{
	unsigned int tbl_sz;
	unsigned int i;

	lif->rss_types = IONIC_RSS_TYPE_IPV4     |
			 IONIC_RSS_TYPE_IPV4_TCP |
			 IONIC_RSS_TYPE_IPV4_UDP |
			 IONIC_RSS_TYPE_IPV6     |
			 IONIC_RSS_TYPE_IPV6_TCP |
			 IONIC_RSS_TYPE_IPV6_UDP;

	/* Fill indirection table with 'default' values */
	tbl_sz = le16_to_cpu(lif->ionic->ident.lif.eth.rss_ind_tbl_sz);
	for (i = 0; i < tbl_sz; i++)
		lif->rss_ind_tbl[i] = ethtool_rxfh_indir_default(i, lif->nxqs);

1629
	return ionic_lif_rss_config(lif, lif->rss_types, NULL, NULL);
S
Shannon Nelson 已提交
1630 1631
}

1632
static void ionic_lif_rss_deinit(struct ionic_lif *lif)
S
Shannon Nelson 已提交
1633
{
1634 1635 1636 1637 1638 1639 1640
	int tbl_sz;

	tbl_sz = le16_to_cpu(lif->ionic->ident.lif.eth.rss_ind_tbl_sz);
	memset(lif->rss_ind_tbl, 0, tbl_sz);
	memset(lif->rss_hash_key, 0, IONIC_RSS_HASH_KEY_SIZE);

	ionic_lif_rss_config(lif, 0x0, NULL, NULL);
S
Shannon Nelson 已提交
1641 1642
}

S
Shannon Nelson 已提交
1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660
static void ionic_lif_quiesce(struct ionic_lif *lif)
{
	struct ionic_admin_ctx ctx = {
		.work = COMPLETION_INITIALIZER_ONSTACK(ctx.work),
		.cmd.lif_setattr = {
			.opcode = IONIC_CMD_LIF_SETATTR,
			.index = cpu_to_le16(lif->index),
			.attr = IONIC_LIF_ATTR_STATE,
			.state = IONIC_LIF_QUIESCE,
		},
	};
	int err;

	err = ionic_adminq_post_wait(lif, &ctx);
	if (err)
		netdev_err(lif->netdev, "lif quiesce failed %d\n", err);
}

S
Shannon Nelson 已提交
1661 1662 1663
static void ionic_txrx_disable(struct ionic_lif *lif)
{
	unsigned int i;
1664
	int err = 0;
S
Shannon Nelson 已提交
1665

1666
	if (lif->txqcqs) {
1667 1668
		for (i = 0; i < lif->nxqs; i++)
			err = ionic_qcq_disable(lif->txqcqs[i], (err != -ETIMEDOUT));
1669 1670 1671
	}

	if (lif->rxqcqs) {
1672 1673
		for (i = 0; i < lif->nxqs; i++)
			err = ionic_qcq_disable(lif->rxqcqs[i], (err != -ETIMEDOUT));
S
Shannon Nelson 已提交
1674
	}
S
Shannon Nelson 已提交
1675 1676

	ionic_lif_quiesce(lif);
S
Shannon Nelson 已提交
1677 1678 1679 1680 1681 1682
}

static void ionic_txrx_deinit(struct ionic_lif *lif)
{
	unsigned int i;

1683
	if (lif->txqcqs) {
1684
		for (i = 0; i < lif->nxqs && lif->txqcqs[i]; i++) {
1685 1686 1687
			ionic_lif_qcq_deinit(lif, lif->txqcqs[i]);
			ionic_tx_flush(&lif->txqcqs[i]->cq);
			ionic_tx_empty(&lif->txqcqs[i]->q);
1688 1689
		}
	}
S
Shannon Nelson 已提交
1690

1691
	if (lif->rxqcqs) {
1692
		for (i = 0; i < lif->nxqs && lif->rxqcqs[i]; i++) {
1693 1694
			ionic_lif_qcq_deinit(lif, lif->rxqcqs[i]);
			ionic_rx_empty(&lif->rxqcqs[i]->q);
1695
		}
S
Shannon Nelson 已提交
1696
	}
1697
	lif->rx_mode = 0;
S
Shannon Nelson 已提交
1698 1699 1700 1701 1702 1703
}

static void ionic_txrx_free(struct ionic_lif *lif)
{
	unsigned int i;

1704
	if (lif->txqcqs) {
1705
		for (i = 0; i < lif->ionic->ntxqs_per_lif && lif->txqcqs[i]; i++) {
1706
			ionic_qcq_free(lif, lif->txqcqs[i]);
1707
			devm_kfree(lif->ionic->dev, lif->txqcqs[i]);
1708
			lif->txqcqs[i] = NULL;
1709 1710
		}
	}
S
Shannon Nelson 已提交
1711

1712
	if (lif->rxqcqs) {
1713
		for (i = 0; i < lif->ionic->nrxqs_per_lif && lif->rxqcqs[i]; i++) {
1714
			ionic_qcq_free(lif, lif->rxqcqs[i]);
1715
			devm_kfree(lif->ionic->dev, lif->rxqcqs[i]);
1716
			lif->rxqcqs[i] = NULL;
1717
		}
S
Shannon Nelson 已提交
1718 1719 1720 1721 1722
	}
}

static int ionic_txrx_alloc(struct ionic_lif *lif)
{
1723
	unsigned int sg_desc_sz;
S
Shannon Nelson 已提交
1724 1725 1726 1727
	unsigned int flags;
	unsigned int i;
	int err = 0;

1728 1729 1730 1731 1732 1733 1734
	if (lif->qtype_info[IONIC_QTYPE_TXQ].version >= 1 &&
	    lif->qtype_info[IONIC_QTYPE_TXQ].sg_desc_sz ==
					  sizeof(struct ionic_txq_sg_desc_v1))
		sg_desc_sz = sizeof(struct ionic_txq_sg_desc_v1);
	else
		sg_desc_sz = sizeof(struct ionic_txq_sg_desc);

S
Shannon Nelson 已提交
1735
	flags = IONIC_QCQ_F_TX_STATS | IONIC_QCQ_F_SG;
1736 1737
	if (test_bit(IONIC_LIF_F_SPLIT_INTR, lif->state))
		flags |= IONIC_QCQ_F_INTR;
S
Shannon Nelson 已提交
1738 1739 1740 1741 1742
	for (i = 0; i < lif->nxqs; i++) {
		err = ionic_qcq_alloc(lif, IONIC_QTYPE_TXQ, i, "tx", flags,
				      lif->ntxq_descs,
				      sizeof(struct ionic_txq_desc),
				      sizeof(struct ionic_txq_comp),
1743
				      sg_desc_sz,
1744
				      lif->kern_pid, &lif->txqcqs[i]);
S
Shannon Nelson 已提交
1745 1746 1747
		if (err)
			goto err_out;

1748
		if (flags & IONIC_QCQ_F_INTR) {
1749
			ionic_intr_coal_init(lif->ionic->idev.intr_ctrl,
1750
					     lif->txqcqs[i]->intr.index,
1751
					     lif->tx_coalesce_hw);
1752 1753 1754
			if (test_bit(IONIC_LIF_F_TX_DIM_INTR, lif->state))
				lif->txqcqs[i]->intr.dim_coal_hw = lif->tx_coalesce_hw;
		}
1755

1756
		ionic_debugfs_add_qcq(lif, lif->txqcqs[i]);
S
Shannon Nelson 已提交
1757 1758
	}

1759
	flags = IONIC_QCQ_F_RX_STATS | IONIC_QCQ_F_SG | IONIC_QCQ_F_INTR;
S
Shannon Nelson 已提交
1760 1761 1762 1763 1764
	for (i = 0; i < lif->nxqs; i++) {
		err = ionic_qcq_alloc(lif, IONIC_QTYPE_RXQ, i, "rx", flags,
				      lif->nrxq_descs,
				      sizeof(struct ionic_rxq_desc),
				      sizeof(struct ionic_rxq_comp),
1765
				      sizeof(struct ionic_rxq_sg_desc),
1766
				      lif->kern_pid, &lif->rxqcqs[i]);
S
Shannon Nelson 已提交
1767 1768 1769
		if (err)
			goto err_out;

1770
		ionic_intr_coal_init(lif->ionic->idev.intr_ctrl,
1771
				     lif->rxqcqs[i]->intr.index,
1772
				     lif->rx_coalesce_hw);
1773 1774
		if (test_bit(IONIC_LIF_F_RX_DIM_INTR, lif->state))
			lif->rxqcqs[i]->intr.dim_coal_hw = lif->rx_coalesce_hw;
1775 1776

		if (!test_bit(IONIC_LIF_F_SPLIT_INTR, lif->state))
1777 1778
			ionic_link_qcq_interrupts(lif->rxqcqs[i],
						  lif->txqcqs[i]);
1779

1780
		ionic_debugfs_add_qcq(lif, lif->rxqcqs[i]);
S
Shannon Nelson 已提交
1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796
	}

	return 0;

err_out:
	ionic_txrx_free(lif);

	return err;
}

static int ionic_txrx_init(struct ionic_lif *lif)
{
	unsigned int i;
	int err;

	for (i = 0; i < lif->nxqs; i++) {
1797
		err = ionic_lif_txq_init(lif, lif->txqcqs[i]);
S
Shannon Nelson 已提交
1798 1799 1800
		if (err)
			goto err_out;

1801
		err = ionic_lif_rxq_init(lif, lif->rxqcqs[i]);
S
Shannon Nelson 已提交
1802
		if (err) {
1803
			ionic_lif_qcq_deinit(lif, lif->txqcqs[i]);
S
Shannon Nelson 已提交
1804 1805 1806 1807
			goto err_out;
		}
	}

S
Shannon Nelson 已提交
1808 1809 1810
	if (lif->netdev->features & NETIF_F_RXHASH)
		ionic_lif_rss_init(lif);

S
Shannon Nelson 已提交
1811
	ionic_set_rx_mode(lif->netdev, CAN_SLEEP);
S
Shannon Nelson 已提交
1812 1813 1814 1815 1816

	return 0;

err_out:
	while (i--) {
1817 1818
		ionic_lif_qcq_deinit(lif, lif->txqcqs[i]);
		ionic_lif_qcq_deinit(lif, lif->rxqcqs[i]);
S
Shannon Nelson 已提交
1819 1820 1821 1822 1823 1824 1825
	}

	return err;
}

static int ionic_txrx_enable(struct ionic_lif *lif)
{
1826
	int derr = 0;
S
Shannon Nelson 已提交
1827 1828 1829
	int i, err;

	for (i = 0; i < lif->nxqs; i++) {
1830 1831 1832 1833 1834 1835
		if (!(lif->rxqcqs[i] && lif->txqcqs[i])) {
			dev_err(lif->ionic->dev, "%s: bad qcq %d\n", __func__, i);
			err = -ENXIO;
			goto err_out;
		}

1836 1837
		ionic_rx_fill(&lif->rxqcqs[i]->q);
		err = ionic_qcq_enable(lif->rxqcqs[i]);
S
Shannon Nelson 已提交
1838 1839 1840
		if (err)
			goto err_out;

1841
		err = ionic_qcq_enable(lif->txqcqs[i]);
S
Shannon Nelson 已提交
1842
		if (err) {
1843
			derr = ionic_qcq_disable(lif->rxqcqs[i], (err != -ETIMEDOUT));
S
Shannon Nelson 已提交
1844 1845 1846 1847 1848 1849 1850 1851
			goto err_out;
		}
	}

	return 0;

err_out:
	while (i--) {
1852 1853
		derr = ionic_qcq_disable(lif->txqcqs[i], (derr != -ETIMEDOUT));
		derr = ionic_qcq_disable(lif->rxqcqs[i], (derr != -ETIMEDOUT));
S
Shannon Nelson 已提交
1854 1855 1856 1857 1858
	}

	return err;
}

1859 1860 1861 1862
static int ionic_start_queues(struct ionic_lif *lif)
{
	int err;

1863 1864 1865
	if (test_bit(IONIC_LIF_F_BROKEN, lif->state))
		return -EIO;

1866 1867 1868
	if (test_bit(IONIC_LIF_F_FW_RESET, lif->state))
		return -EBUSY;

1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881
	if (test_and_set_bit(IONIC_LIF_F_UP, lif->state))
		return 0;

	err = ionic_txrx_enable(lif);
	if (err) {
		clear_bit(IONIC_LIF_F_UP, lif->state);
		return err;
	}
	netif_tx_wake_all_queues(lif->netdev);

	return 0;
}

1882
static int ionic_open(struct net_device *netdev)
1883 1884
{
	struct ionic_lif *lif = netdev_priv(netdev);
S
Shannon Nelson 已提交
1885
	int err;
1886

1887 1888 1889 1890
	/* If recovering from a broken state, clear the bit and we'll try again */
	if (test_and_clear_bit(IONIC_LIF_F_BROKEN, lif->state))
		netdev_info(netdev, "clearing broken state\n");

S
Shannon Nelson 已提交
1891 1892 1893 1894 1895 1896
	err = ionic_txrx_alloc(lif);
	if (err)
		return err;

	err = ionic_txrx_init(lif);
	if (err)
S
Shannon Nelson 已提交
1897
		goto err_txrx_free;
1898

1899 1900 1901 1902 1903 1904 1905 1906
	err = netif_set_real_num_tx_queues(netdev, lif->nxqs);
	if (err)
		goto err_txrx_deinit;

	err = netif_set_real_num_rx_queues(netdev, lif->nxqs);
	if (err)
		goto err_txrx_deinit;

1907 1908 1909 1910 1911 1912
	/* don't start the queues until we have link */
	if (netif_carrier_ok(netdev)) {
		err = ionic_start_queues(lif);
		if (err)
			goto err_txrx_deinit;
	}
1913

1914
	return 0;
S
Shannon Nelson 已提交
1915 1916 1917

err_txrx_deinit:
	ionic_txrx_deinit(lif);
S
Shannon Nelson 已提交
1918
err_txrx_free:
S
Shannon Nelson 已提交
1919 1920
	ionic_txrx_free(lif);
	return err;
1921 1922
}

1923
static void ionic_stop_queues(struct ionic_lif *lif)
1924
{
1925 1926
	if (!test_and_clear_bit(IONIC_LIF_F_UP, lif->state))
		return;
1927

1928
	netif_tx_disable(lif->netdev);
1929
	ionic_txrx_disable(lif);
1930
}
1931

1932
static int ionic_stop(struct net_device *netdev)
1933 1934
{
	struct ionic_lif *lif = netdev_priv(netdev);
S
Shannon Nelson 已提交
1935

1936
	if (test_bit(IONIC_LIF_F_FW_RESET, lif->state))
S
Shannon Nelson 已提交
1937 1938
		return 0;

1939
	ionic_stop_queues(lif);
S
Shannon Nelson 已提交
1940 1941
	ionic_txrx_deinit(lif);
	ionic_txrx_free(lif);
1942

1943
	return 0;
1944 1945
}

1946 1947 1948 1949 1950 1951 1952
static int ionic_get_vf_config(struct net_device *netdev,
			       int vf, struct ifla_vf_info *ivf)
{
	struct ionic_lif *lif = netdev_priv(netdev);
	struct ionic *ionic = lif->ionic;
	int ret = 0;

1953 1954 1955
	if (!netif_device_present(netdev))
		return -EBUSY;

1956 1957 1958 1959 1960 1961
	down_read(&ionic->vf_op_lock);

	if (vf >= pci_num_vf(ionic->pdev) || !ionic->vfs) {
		ret = -EINVAL;
	} else {
		ivf->vf           = vf;
S
Shannon Nelson 已提交
1962
		ivf->vlan         = le16_to_cpu(ionic->vfs[vf].vlanid);
1963 1964 1965
		ivf->qos	  = 0;
		ivf->spoofchk     = ionic->vfs[vf].spoofchk;
		ivf->linkstate    = ionic->vfs[vf].linkstate;
S
Shannon Nelson 已提交
1966
		ivf->max_tx_rate  = le32_to_cpu(ionic->vfs[vf].maxrate);
1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982
		ivf->trusted      = ionic->vfs[vf].trusted;
		ether_addr_copy(ivf->mac, ionic->vfs[vf].macaddr);
	}

	up_read(&ionic->vf_op_lock);
	return ret;
}

static int ionic_get_vf_stats(struct net_device *netdev, int vf,
			      struct ifla_vf_stats *vf_stats)
{
	struct ionic_lif *lif = netdev_priv(netdev);
	struct ionic *ionic = lif->ionic;
	struct ionic_lif_stats *vs;
	int ret = 0;

1983 1984 1985
	if (!netif_device_present(netdev))
		return -EBUSY;

1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020
	down_read(&ionic->vf_op_lock);

	if (vf >= pci_num_vf(ionic->pdev) || !ionic->vfs) {
		ret = -EINVAL;
	} else {
		memset(vf_stats, 0, sizeof(*vf_stats));
		vs = &ionic->vfs[vf].stats;

		vf_stats->rx_packets = le64_to_cpu(vs->rx_ucast_packets);
		vf_stats->tx_packets = le64_to_cpu(vs->tx_ucast_packets);
		vf_stats->rx_bytes   = le64_to_cpu(vs->rx_ucast_bytes);
		vf_stats->tx_bytes   = le64_to_cpu(vs->tx_ucast_bytes);
		vf_stats->broadcast  = le64_to_cpu(vs->rx_bcast_packets);
		vf_stats->multicast  = le64_to_cpu(vs->rx_mcast_packets);
		vf_stats->rx_dropped = le64_to_cpu(vs->rx_ucast_drop_packets) +
				       le64_to_cpu(vs->rx_mcast_drop_packets) +
				       le64_to_cpu(vs->rx_bcast_drop_packets);
		vf_stats->tx_dropped = le64_to_cpu(vs->tx_ucast_drop_packets) +
				       le64_to_cpu(vs->tx_mcast_drop_packets) +
				       le64_to_cpu(vs->tx_bcast_drop_packets);
	}

	up_read(&ionic->vf_op_lock);
	return ret;
}

static int ionic_set_vf_mac(struct net_device *netdev, int vf, u8 *mac)
{
	struct ionic_lif *lif = netdev_priv(netdev);
	struct ionic *ionic = lif->ionic;
	int ret;

	if (!(is_zero_ether_addr(mac) || is_valid_ether_addr(mac)))
		return -EINVAL;

2021 2022 2023
	if (!netif_device_present(netdev))
		return -EBUSY;

S
Shannon Nelson 已提交
2024
	down_write(&ionic->vf_op_lock);
2025 2026 2027 2028 2029 2030 2031 2032 2033

	if (vf >= pci_num_vf(ionic->pdev) || !ionic->vfs) {
		ret = -EINVAL;
	} else {
		ret = ionic_set_vf_config(ionic, vf, IONIC_VF_ATTR_MAC, mac);
		if (!ret)
			ether_addr_copy(ionic->vfs[vf].macaddr, mac);
	}

S
Shannon Nelson 已提交
2034
	up_write(&ionic->vf_op_lock);
2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054
	return ret;
}

static int ionic_set_vf_vlan(struct net_device *netdev, int vf, u16 vlan,
			     u8 qos, __be16 proto)
{
	struct ionic_lif *lif = netdev_priv(netdev);
	struct ionic *ionic = lif->ionic;
	int ret;

	/* until someday when we support qos */
	if (qos)
		return -EINVAL;

	if (vlan > 4095)
		return -EINVAL;

	if (proto != htons(ETH_P_8021Q))
		return -EPROTONOSUPPORT;

2055 2056 2057
	if (!netif_device_present(netdev))
		return -EBUSY;

S
Shannon Nelson 已提交
2058
	down_write(&ionic->vf_op_lock);
2059 2060 2061 2062 2063 2064 2065

	if (vf >= pci_num_vf(ionic->pdev) || !ionic->vfs) {
		ret = -EINVAL;
	} else {
		ret = ionic_set_vf_config(ionic, vf,
					  IONIC_VF_ATTR_VLAN, (u8 *)&vlan);
		if (!ret)
S
Shannon Nelson 已提交
2066
			ionic->vfs[vf].vlanid = cpu_to_le16(vlan);
2067 2068
	}

S
Shannon Nelson 已提交
2069
	up_write(&ionic->vf_op_lock);
2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083
	return ret;
}

static int ionic_set_vf_rate(struct net_device *netdev, int vf,
			     int tx_min, int tx_max)
{
	struct ionic_lif *lif = netdev_priv(netdev);
	struct ionic *ionic = lif->ionic;
	int ret;

	/* setting the min just seems silly */
	if (tx_min)
		return -EINVAL;

2084 2085 2086
	if (!netif_device_present(netdev))
		return -EBUSY;

2087 2088 2089 2090 2091 2092 2093 2094
	down_write(&ionic->vf_op_lock);

	if (vf >= pci_num_vf(ionic->pdev) || !ionic->vfs) {
		ret = -EINVAL;
	} else {
		ret = ionic_set_vf_config(ionic, vf,
					  IONIC_VF_ATTR_RATE, (u8 *)&tx_max);
		if (!ret)
S
Shannon Nelson 已提交
2095
			lif->ionic->vfs[vf].maxrate = cpu_to_le32(tx_max);
2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108
	}

	up_write(&ionic->vf_op_lock);
	return ret;
}

static int ionic_set_vf_spoofchk(struct net_device *netdev, int vf, bool set)
{
	struct ionic_lif *lif = netdev_priv(netdev);
	struct ionic *ionic = lif->ionic;
	u8 data = set;  /* convert to u8 for config */
	int ret;

2109 2110 2111
	if (!netif_device_present(netdev))
		return -EBUSY;

2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133
	down_write(&ionic->vf_op_lock);

	if (vf >= pci_num_vf(ionic->pdev) || !ionic->vfs) {
		ret = -EINVAL;
	} else {
		ret = ionic_set_vf_config(ionic, vf,
					  IONIC_VF_ATTR_SPOOFCHK, &data);
		if (!ret)
			ionic->vfs[vf].spoofchk = data;
	}

	up_write(&ionic->vf_op_lock);
	return ret;
}

static int ionic_set_vf_trust(struct net_device *netdev, int vf, bool set)
{
	struct ionic_lif *lif = netdev_priv(netdev);
	struct ionic *ionic = lif->ionic;
	u8 data = set;  /* convert to u8 for config */
	int ret;

2134 2135 2136
	if (!netif_device_present(netdev))
		return -EBUSY;

2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172
	down_write(&ionic->vf_op_lock);

	if (vf >= pci_num_vf(ionic->pdev) || !ionic->vfs) {
		ret = -EINVAL;
	} else {
		ret = ionic_set_vf_config(ionic, vf,
					  IONIC_VF_ATTR_TRUST, &data);
		if (!ret)
			ionic->vfs[vf].trusted = data;
	}

	up_write(&ionic->vf_op_lock);
	return ret;
}

static int ionic_set_vf_link_state(struct net_device *netdev, int vf, int set)
{
	struct ionic_lif *lif = netdev_priv(netdev);
	struct ionic *ionic = lif->ionic;
	u8 data;
	int ret;

	switch (set) {
	case IFLA_VF_LINK_STATE_ENABLE:
		data = IONIC_VF_LINK_STATUS_UP;
		break;
	case IFLA_VF_LINK_STATE_DISABLE:
		data = IONIC_VF_LINK_STATUS_DOWN;
		break;
	case IFLA_VF_LINK_STATE_AUTO:
		data = IONIC_VF_LINK_STATUS_AUTO;
		break;
	default:
		return -EINVAL;
	}

2173 2174 2175
	if (!netif_device_present(netdev))
		return -EBUSY;

2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190
	down_write(&ionic->vf_op_lock);

	if (vf >= pci_num_vf(ionic->pdev) || !ionic->vfs) {
		ret = -EINVAL;
	} else {
		ret = ionic_set_vf_config(ionic, vf,
					  IONIC_VF_ATTR_LINKSTATE, &data);
		if (!ret)
			ionic->vfs[vf].linkstate = set;
	}

	up_write(&ionic->vf_op_lock);
	return ret;
}

2191 2192 2193
static const struct net_device_ops ionic_netdev_ops = {
	.ndo_open               = ionic_open,
	.ndo_stop               = ionic_stop,
S
Shannon Nelson 已提交
2194
	.ndo_start_xmit		= ionic_start_xmit,
2195
	.ndo_get_stats64	= ionic_get_stats64,
2196
	.ndo_set_rx_mode	= ionic_ndo_set_rx_mode,
2197 2198 2199 2200 2201 2202 2203
	.ndo_set_features	= ionic_set_features,
	.ndo_set_mac_address	= ionic_set_mac_address,
	.ndo_validate_addr	= eth_validate_addr,
	.ndo_tx_timeout         = ionic_tx_timeout,
	.ndo_change_mtu         = ionic_change_mtu,
	.ndo_vlan_rx_add_vid    = ionic_vlan_rx_add_vid,
	.ndo_vlan_rx_kill_vid   = ionic_vlan_rx_kill_vid,
2204 2205 2206 2207 2208 2209 2210 2211
	.ndo_set_vf_vlan	= ionic_set_vf_vlan,
	.ndo_set_vf_trust	= ionic_set_vf_trust,
	.ndo_set_vf_mac		= ionic_set_vf_mac,
	.ndo_set_vf_rate	= ionic_set_vf_rate,
	.ndo_set_vf_spoofchk	= ionic_set_vf_spoofchk,
	.ndo_get_vf_config	= ionic_get_vf_config,
	.ndo_set_vf_link_state	= ionic_set_vf_link_state,
	.ndo_get_vf_stats       = ionic_get_vf_stats,
2212 2213
};

2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237
static void ionic_swap_queues(struct ionic_qcq *a, struct ionic_qcq *b)
{
	/* only swapping the queues, not the napi, flags, or other stuff */
	swap(a->q.num_descs,  b->q.num_descs);
	swap(a->q.base,       b->q.base);
	swap(a->q.base_pa,    b->q.base_pa);
	swap(a->q.info,       b->q.info);
	swap(a->q_base,       b->q_base);
	swap(a->q_base_pa,    b->q_base_pa);
	swap(a->q_size,       b->q_size);

	swap(a->q.sg_base,    b->q.sg_base);
	swap(a->q.sg_base_pa, b->q.sg_base_pa);
	swap(a->sg_base,      b->sg_base);
	swap(a->sg_base_pa,   b->sg_base_pa);
	swap(a->sg_size,      b->sg_size);

	swap(a->cq.num_descs, b->cq.num_descs);
	swap(a->cq.base,      b->cq.base);
	swap(a->cq.base_pa,   b->cq.base_pa);
	swap(a->cq.info,      b->cq.info);
	swap(a->cq_base,      b->cq_base);
	swap(a->cq_base_pa,   b->cq_base_pa);
	swap(a->cq_size,      b->cq_size);
2238 2239 2240

	ionic_debugfs_del_qcq(a);
	ionic_debugfs_add_qcq(a->q.lif, a);
2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253
}

int ionic_reconfigure_queues(struct ionic_lif *lif,
			     struct ionic_queue_params *qparam)
{
	struct ionic_qcq **tx_qcqs = NULL;
	struct ionic_qcq **rx_qcqs = NULL;
	unsigned int sg_desc_sz;
	unsigned int flags;
	int err = -ENOMEM;
	unsigned int i;

	/* allocate temporary qcq arrays to hold new queue structs */
2254 2255
	if (qparam->nxqs != lif->nxqs || qparam->ntxq_descs != lif->ntxq_descs) {
		tx_qcqs = devm_kcalloc(lif->ionic->dev, lif->ionic->ntxqs_per_lif,
2256 2257 2258 2259
				       sizeof(struct ionic_qcq *), GFP_KERNEL);
		if (!tx_qcqs)
			goto err_out;
	}
2260 2261
	if (qparam->nxqs != lif->nxqs || qparam->nrxq_descs != lif->nrxq_descs) {
		rx_qcqs = devm_kcalloc(lif->ionic->dev, lif->ionic->nrxqs_per_lif,
2262 2263 2264 2265 2266
				       sizeof(struct ionic_qcq *), GFP_KERNEL);
		if (!rx_qcqs)
			goto err_out;
	}

2267 2268 2269
	/* allocate new desc_info and rings, but leave the interrupt setup
	 * until later so as to not mess with the still-running queues
	 */
2270 2271 2272 2273 2274 2275 2276 2277
	if (lif->qtype_info[IONIC_QTYPE_TXQ].version >= 1 &&
	    lif->qtype_info[IONIC_QTYPE_TXQ].sg_desc_sz ==
					  sizeof(struct ionic_txq_sg_desc_v1))
		sg_desc_sz = sizeof(struct ionic_txq_sg_desc_v1);
	else
		sg_desc_sz = sizeof(struct ionic_txq_sg_desc);

	if (tx_qcqs) {
2278
		for (i = 0; i < qparam->nxqs; i++) {
2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291
			flags = lif->txqcqs[i]->flags & ~IONIC_QCQ_F_INTR;
			err = ionic_qcq_alloc(lif, IONIC_QTYPE_TXQ, i, "tx", flags,
					      qparam->ntxq_descs,
					      sizeof(struct ionic_txq_desc),
					      sizeof(struct ionic_txq_comp),
					      sg_desc_sz,
					      lif->kern_pid, &tx_qcqs[i]);
			if (err)
				goto err_out;
		}
	}

	if (rx_qcqs) {
2292
		for (i = 0; i < qparam->nxqs; i++) {
2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307
			flags = lif->rxqcqs[i]->flags & ~IONIC_QCQ_F_INTR;
			err = ionic_qcq_alloc(lif, IONIC_QTYPE_RXQ, i, "rx", flags,
					      qparam->nrxq_descs,
					      sizeof(struct ionic_rxq_desc),
					      sizeof(struct ionic_rxq_comp),
					      sizeof(struct ionic_rxq_sg_desc),
					      lif->kern_pid, &rx_qcqs[i]);
			if (err)
				goto err_out;
		}
	}

	/* stop and clean the queues */
	ionic_stop_queues_reconfig(lif);

2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318
	if (qparam->nxqs != lif->nxqs) {
		err = netif_set_real_num_tx_queues(lif->netdev, qparam->nxqs);
		if (err)
			goto err_out_reinit_unlock;
		err = netif_set_real_num_rx_queues(lif->netdev, qparam->nxqs);
		if (err) {
			netif_set_real_num_tx_queues(lif->netdev, lif->nxqs);
			goto err_out_reinit_unlock;
		}
	}

2319 2320 2321
	/* swap new desc_info and rings, keeping existing interrupt config */
	if (tx_qcqs) {
		lif->ntxq_descs = qparam->ntxq_descs;
2322
		for (i = 0; i < qparam->nxqs; i++)
2323 2324 2325 2326 2327
			ionic_swap_queues(lif->txqcqs[i], tx_qcqs[i]);
	}

	if (rx_qcqs) {
		lif->nrxq_descs = qparam->nrxq_descs;
2328
		for (i = 0; i < qparam->nxqs; i++)
2329 2330 2331
			ionic_swap_queues(lif->rxqcqs[i], rx_qcqs[i]);
	}

2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362
	/* if we need to change the interrupt layout, this is the time */
	if (qparam->intr_split != test_bit(IONIC_LIF_F_SPLIT_INTR, lif->state) ||
	    qparam->nxqs != lif->nxqs) {
		if (qparam->intr_split) {
			set_bit(IONIC_LIF_F_SPLIT_INTR, lif->state);
		} else {
			clear_bit(IONIC_LIF_F_SPLIT_INTR, lif->state);
			lif->tx_coalesce_usecs = lif->rx_coalesce_usecs;
			lif->tx_coalesce_hw = lif->rx_coalesce_hw;
		}

		/* clear existing interrupt assignments */
		for (i = 0; i < lif->ionic->ntxqs_per_lif; i++) {
			ionic_qcq_intr_free(lif, lif->txqcqs[i]);
			ionic_qcq_intr_free(lif, lif->rxqcqs[i]);
		}

		/* re-assign the interrupts */
		for (i = 0; i < qparam->nxqs; i++) {
			lif->rxqcqs[i]->flags |= IONIC_QCQ_F_INTR;
			err = ionic_alloc_qcq_interrupt(lif, lif->rxqcqs[i]);
			ionic_intr_coal_init(lif->ionic->idev.intr_ctrl,
					     lif->rxqcqs[i]->intr.index,
					     lif->rx_coalesce_hw);

			if (qparam->intr_split) {
				lif->txqcqs[i]->flags |= IONIC_QCQ_F_INTR;
				err = ionic_alloc_qcq_interrupt(lif, lif->txqcqs[i]);
				ionic_intr_coal_init(lif->ionic->idev.intr_ctrl,
						     lif->txqcqs[i]->intr.index,
						     lif->tx_coalesce_hw);
2363 2364
				if (test_bit(IONIC_LIF_F_TX_DIM_INTR, lif->state))
					lif->txqcqs[i]->intr.dim_coal_hw = lif->tx_coalesce_hw;
2365 2366 2367 2368 2369 2370 2371
			} else {
				lif->txqcqs[i]->flags &= ~IONIC_QCQ_F_INTR;
				ionic_link_qcq_interrupts(lif->rxqcqs[i], lif->txqcqs[i]);
			}
		}
	}

2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386
	/* now we can rework the debugfs mappings */
	if (tx_qcqs) {
		for (i = 0; i < qparam->nxqs; i++) {
			ionic_debugfs_del_qcq(lif->txqcqs[i]);
			ionic_debugfs_add_qcq(lif, lif->txqcqs[i]);
		}
	}

	if (rx_qcqs) {
		for (i = 0; i < qparam->nxqs; i++) {
			ionic_debugfs_del_qcq(lif->rxqcqs[i]);
			ionic_debugfs_add_qcq(lif, lif->rxqcqs[i]);
		}
	}

2387 2388 2389
	swap(lif->nxqs, qparam->nxqs);

err_out_reinit_unlock:
S
Shannon Nelson 已提交
2390
	/* re-init the queues, but don't lose an error code */
2391 2392 2393 2394
	if (err)
		ionic_start_queues_reconfig(lif);
	else
		err = ionic_start_queues_reconfig(lif);
2395 2396 2397

err_out:
	/* free old allocs without cleaning intr */
2398
	for (i = 0; i < qparam->nxqs; i++) {
2399 2400 2401
		if (tx_qcqs && tx_qcqs[i]) {
			tx_qcqs[i]->flags &= ~IONIC_QCQ_F_INTR;
			ionic_qcq_free(lif, tx_qcqs[i]);
2402
			devm_kfree(lif->ionic->dev, tx_qcqs[i]);
2403 2404 2405 2406 2407
			tx_qcqs[i] = NULL;
		}
		if (rx_qcqs && rx_qcqs[i]) {
			rx_qcqs[i]->flags &= ~IONIC_QCQ_F_INTR;
			ionic_qcq_free(lif, rx_qcqs[i]);
2408
			devm_kfree(lif->ionic->dev, rx_qcqs[i]);
2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422
			rx_qcqs[i] = NULL;
		}
	}

	/* free q array */
	if (rx_qcqs) {
		devm_kfree(lif->ionic->dev, rx_qcqs);
		rx_qcqs = NULL;
	}
	if (tx_qcqs) {
		devm_kfree(lif->ionic->dev, tx_qcqs);
		tx_qcqs = NULL;
	}

2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433
	/* clean the unused dma and info allocations when new set is smaller
	 * than the full array, but leave the qcq shells in place
	 */
	for (i = lif->nxqs; i < lif->ionic->ntxqs_per_lif; i++) {
		lif->txqcqs[i]->flags &= ~IONIC_QCQ_F_INTR;
		ionic_qcq_free(lif, lif->txqcqs[i]);

		lif->rxqcqs[i]->flags &= ~IONIC_QCQ_F_INTR;
		ionic_qcq_free(lif, lif->rxqcqs[i]);
	}

2434 2435 2436
	return err;
}

S
Shannon Nelson 已提交
2437
int ionic_lif_alloc(struct ionic *ionic)
S
Shannon Nelson 已提交
2438 2439
{
	struct device *dev = ionic->dev;
2440
	union ionic_lif_identity *lid;
S
Shannon Nelson 已提交
2441 2442
	struct net_device *netdev;
	struct ionic_lif *lif;
S
Shannon Nelson 已提交
2443
	int tbl_sz;
S
Shannon Nelson 已提交
2444 2445
	int err;

2446 2447
	lid = kzalloc(sizeof(*lid), GFP_KERNEL);
	if (!lid)
S
Shannon Nelson 已提交
2448
		return -ENOMEM;
2449

S
Shannon Nelson 已提交
2450 2451 2452 2453
	netdev = alloc_etherdev_mqs(sizeof(*lif),
				    ionic->ntxqs_per_lif, ionic->ntxqs_per_lif);
	if (!netdev) {
		dev_err(dev, "Cannot allocate netdev, aborting\n");
2454 2455
		err = -ENOMEM;
		goto err_out_free_lid;
S
Shannon Nelson 已提交
2456 2457 2458 2459 2460 2461
	}

	SET_NETDEV_DEV(netdev, dev);

	lif = netdev_priv(netdev);
	lif->netdev = netdev;
S
Shannon Nelson 已提交
2462
	ionic->lif = lif;
2463
	netdev->netdev_ops = &ionic_netdev_ops;
2464
	ionic_ethtool_set_ops(netdev);
2465 2466

	netdev->watchdog_timeo = 2 * HZ;
2467 2468
	netif_carrier_off(netdev);

2469 2470
	lif->identity = lid;
	lif->lif_type = IONIC_LIF_TYPE_CLASSIC;
2471 2472 2473 2474 2475 2476
	err = ionic_lif_identify(ionic, lif->lif_type, lif->identity);
	if (err) {
		dev_err(ionic->dev, "Cannot identify type %d: %d\n",
			lif->lif_type, err);
		goto err_out_free_netdev;
	}
2477 2478
	lif->netdev->min_mtu = max_t(unsigned int, ETH_MIN_MTU,
				     le32_to_cpu(lif->identity->eth.min_frame_size));
2479 2480
	lif->netdev->max_mtu =
		le32_to_cpu(lif->identity->eth.max_frame_size) - ETH_HLEN - VLAN_HLEN;
S
Shannon Nelson 已提交
2481 2482 2483 2484 2485

	lif->neqs = ionic->neqs_per_lif;
	lif->nxqs = ionic->ntxqs_per_lif;

	lif->ionic = ionic;
S
Shannon Nelson 已提交
2486
	lif->index = 0;
S
Shannon Nelson 已提交
2487 2488
	lif->ntxq_descs = IONIC_DEF_TXRX_DESC;
	lif->nrxq_descs = IONIC_DEF_TXRX_DESC;
S
Shannon Nelson 已提交
2489

2490
	/* Convert the default coalesce value to actual hw resolution */
2491
	lif->rx_coalesce_usecs = IONIC_ITR_COAL_USEC_DEFAULT;
2492
	lif->rx_coalesce_hw = ionic_coal_usec_to_hw(lif->ionic,
2493
						    lif->rx_coalesce_usecs);
2494 2495
	lif->tx_coalesce_usecs = lif->rx_coalesce_usecs;
	lif->tx_coalesce_hw = lif->rx_coalesce_hw;
2496 2497
	set_bit(IONIC_LIF_F_RX_DIM_INTR, lif->state);
	set_bit(IONIC_LIF_F_TX_DIM_INTR, lif->state);
2498

S
Shannon Nelson 已提交
2499
	snprintf(lif->name, sizeof(lif->name), "lif%u", lif->index);
S
Shannon Nelson 已提交
2500

S
Shannon Nelson 已提交
2501 2502
	spin_lock_init(&lif->adminq_lock);

2503 2504 2505 2506
	spin_lock_init(&lif->deferred.lock);
	INIT_LIST_HEAD(&lif->deferred.list);
	INIT_WORK(&lif->deferred.work, ionic_lif_deferred_work);

S
Shannon Nelson 已提交
2507 2508 2509 2510 2511 2512 2513 2514 2515 2516
	/* allocate lif info */
	lif->info_sz = ALIGN(sizeof(*lif->info), PAGE_SIZE);
	lif->info = dma_alloc_coherent(dev, lif->info_sz,
				       &lif->info_pa, GFP_KERNEL);
	if (!lif->info) {
		dev_err(dev, "Failed to allocate lif info, aborting\n");
		err = -ENOMEM;
		goto err_out_free_netdev;
	}

2517 2518
	ionic_debugfs_add_lif(lif);

S
Shannon Nelson 已提交
2519 2520
	/* allocate control queues and txrx queue arrays */
	ionic_lif_queue_identify(lif);
S
Shannon Nelson 已提交
2521 2522 2523 2524
	err = ionic_qcqs_alloc(lif);
	if (err)
		goto err_out_free_lif_info;

S
Shannon Nelson 已提交
2525 2526 2527 2528 2529 2530 2531 2532
	/* allocate rss indirection table */
	tbl_sz = le16_to_cpu(lif->ionic->ident.lif.eth.rss_ind_tbl_sz);
	lif->rss_ind_tbl_sz = sizeof(*lif->rss_ind_tbl) * tbl_sz;
	lif->rss_ind_tbl = dma_alloc_coherent(dev, lif->rss_ind_tbl_sz,
					      &lif->rss_ind_tbl_pa,
					      GFP_KERNEL);

	if (!lif->rss_ind_tbl) {
2533
		err = -ENOMEM;
S
Shannon Nelson 已提交
2534 2535 2536
		dev_err(dev, "Failed to allocate rss indirection table, aborting\n");
		goto err_out_free_qcqs;
	}
2537
	netdev_rss_key_fill(lif->rss_hash_key, IONIC_RSS_HASH_KEY_SIZE);
S
Shannon Nelson 已提交
2538

S
Shannon Nelson 已提交
2539
	return 0;
S
Shannon Nelson 已提交
2540

S
Shannon Nelson 已提交
2541 2542
err_out_free_qcqs:
	ionic_qcqs_free(lif);
S
Shannon Nelson 已提交
2543 2544 2545 2546
err_out_free_lif_info:
	dma_free_coherent(dev, lif->info_sz, lif->info, lif->info_pa);
	lif->info = NULL;
	lif->info_pa = 0;
S
Shannon Nelson 已提交
2547 2548 2549
err_out_free_netdev:
	free_netdev(lif->netdev);
	lif = NULL;
2550
err_out_free_lid:
2551
	kfree(lid);
S
Shannon Nelson 已提交
2552

S
Shannon Nelson 已提交
2553
	return err;
S
Shannon Nelson 已提交
2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565
}

static void ionic_lif_reset(struct ionic_lif *lif)
{
	struct ionic_dev *idev = &lif->ionic->idev;

	mutex_lock(&lif->ionic->dev_cmd_lock);
	ionic_dev_cmd_lif_reset(idev, lif->index);
	ionic_dev_cmd_wait(lif->ionic, DEVCMD_TIMEOUT);
	mutex_unlock(&lif->ionic->dev_cmd_lock);
}

S
Shannon Nelson 已提交
2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578
static void ionic_lif_handle_fw_down(struct ionic_lif *lif)
{
	struct ionic *ionic = lif->ionic;

	if (test_and_set_bit(IONIC_LIF_F_FW_RESET, lif->state))
		return;

	dev_info(ionic->dev, "FW Down: Stopping LIFs\n");

	netif_device_detach(lif->netdev);

	if (test_bit(IONIC_LIF_F_UP, lif->state)) {
		dev_info(ionic->dev, "Surprise FW stop, stopping queues\n");
2579
		mutex_lock(&lif->queue_lock);
S
Shannon Nelson 已提交
2580
		ionic_stop_queues(lif);
2581
		mutex_unlock(&lif->queue_lock);
S
Shannon Nelson 已提交
2582 2583 2584 2585 2586 2587
	}

	if (netif_running(lif->netdev)) {
		ionic_txrx_deinit(lif);
		ionic_txrx_free(lif);
	}
S
Shannon Nelson 已提交
2588
	ionic_lif_deinit(lif);
2589
	ionic_reset(ionic);
S
Shannon Nelson 已提交
2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604
	ionic_qcqs_free(lif);

	dev_info(ionic->dev, "FW Down: LIFs stopped\n");
}

static void ionic_lif_handle_fw_up(struct ionic_lif *lif)
{
	struct ionic *ionic = lif->ionic;
	int err;

	if (!test_bit(IONIC_LIF_F_FW_RESET, lif->state))
		return;

	dev_info(ionic->dev, "FW Up: restarting LIFs\n");

2605
	ionic_init_devinfo(ionic);
2606 2607 2608 2609 2610 2611 2612 2613 2614
	err = ionic_identify(ionic);
	if (err)
		goto err_out;
	err = ionic_port_identify(ionic);
	if (err)
		goto err_out;
	err = ionic_port_init(ionic);
	if (err)
		goto err_out;
S
Shannon Nelson 已提交
2615 2616 2617 2618
	err = ionic_qcqs_alloc(lif);
	if (err)
		goto err_out;

S
Shannon Nelson 已提交
2619
	err = ionic_lif_init(lif);
S
Shannon Nelson 已提交
2620 2621 2622 2623 2624 2625
	if (err)
		goto err_qcqs_free;

	if (lif->registered)
		ionic_lif_set_netdev_info(lif);

2626 2627
	ionic_rx_filter_replay(lif);

S
Shannon Nelson 已提交
2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638
	if (netif_running(lif->netdev)) {
		err = ionic_txrx_alloc(lif);
		if (err)
			goto err_lifs_deinit;

		err = ionic_txrx_init(lif);
		if (err)
			goto err_txrx_free;
	}

	clear_bit(IONIC_LIF_F_FW_RESET, lif->state);
S
Shannon Nelson 已提交
2639
	ionic_link_status_check_request(lif, CAN_SLEEP);
S
Shannon Nelson 已提交
2640 2641 2642 2643 2644 2645 2646 2647
	netif_device_attach(lif->netdev);
	dev_info(ionic->dev, "FW Up: LIFs restarted\n");

	return;

err_txrx_free:
	ionic_txrx_free(lif);
err_lifs_deinit:
S
Shannon Nelson 已提交
2648
	ionic_lif_deinit(lif);
S
Shannon Nelson 已提交
2649 2650 2651 2652 2653 2654
err_qcqs_free:
	ionic_qcqs_free(lif);
err_out:
	dev_err(ionic->dev, "FW Up: LIFs restart failed - err %d\n", err);
}

S
Shannon Nelson 已提交
2655
void ionic_lif_free(struct ionic_lif *lif)
S
Shannon Nelson 已提交
2656 2657 2658
{
	struct device *dev = lif->ionic->dev;

S
Shannon Nelson 已提交
2659 2660 2661 2662 2663 2664
	/* free rss indirection table */
	dma_free_coherent(dev, lif->rss_ind_tbl_sz, lif->rss_ind_tbl,
			  lif->rss_ind_tbl_pa);
	lif->rss_ind_tbl = NULL;
	lif->rss_ind_tbl_pa = 0;

S
Shannon Nelson 已提交
2665 2666
	/* free queues */
	ionic_qcqs_free(lif);
S
Shannon Nelson 已提交
2667 2668
	if (!test_bit(IONIC_LIF_F_FW_RESET, lif->state))
		ionic_lif_reset(lif);
S
Shannon Nelson 已提交
2669 2670

	/* free lif info */
2671
	kfree(lif->identity);
S
Shannon Nelson 已提交
2672 2673 2674 2675
	dma_free_coherent(dev, lif->info_sz, lif->info, lif->info_pa);
	lif->info = NULL;
	lif->info_pa = 0;

2676 2677 2678 2679 2680 2681
	/* unmap doorbell page */
	ionic_bus_unmap_dbpage(lif->ionic, lif->kern_dbpage);
	lif->kern_dbpage = NULL;
	kfree(lif->dbid_inuse);
	lif->dbid_inuse = NULL;

S
Shannon Nelson 已提交
2682 2683 2684 2685 2686
	/* free netdev & lif */
	ionic_debugfs_del_lif(lif);
	free_netdev(lif->netdev);
}

S
Shannon Nelson 已提交
2687
void ionic_lif_deinit(struct ionic_lif *lif)
S
Shannon Nelson 已提交
2688
{
S
Shannon Nelson 已提交
2689
	if (!test_and_clear_bit(IONIC_LIF_F_INITED, lif->state))
S
Shannon Nelson 已提交
2690 2691
		return;

S
Shannon Nelson 已提交
2692 2693 2694
	if (!test_bit(IONIC_LIF_F_FW_RESET, lif->state)) {
		cancel_work_sync(&lif->deferred.work);
		cancel_work_sync(&lif->tx_timeout_work);
2695
		ionic_rx_filters_deinit(lif);
2696 2697
		if (lif->netdev->features & NETIF_F_RXHASH)
			ionic_lif_rss_deinit(lif);
S
Shannon Nelson 已提交
2698
	}
S
Shannon Nelson 已提交
2699

S
Shannon Nelson 已提交
2700
	napi_disable(&lif->adminqcq->napi);
S
Shannon Nelson 已提交
2701
	ionic_lif_qcq_deinit(lif, lif->notifyqcq);
S
Shannon Nelson 已提交
2702 2703
	ionic_lif_qcq_deinit(lif, lif->adminqcq);

2704
	mutex_destroy(&lif->queue_lock);
S
Shannon Nelson 已提交
2705 2706 2707
	ionic_lif_reset(lif);
}

S
Shannon Nelson 已提交
2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751
static int ionic_lif_adminq_init(struct ionic_lif *lif)
{
	struct device *dev = lif->ionic->dev;
	struct ionic_q_init_comp comp;
	struct ionic_dev *idev;
	struct ionic_qcq *qcq;
	struct ionic_queue *q;
	int err;

	idev = &lif->ionic->idev;
	qcq = lif->adminqcq;
	q = &qcq->q;

	mutex_lock(&lif->ionic->dev_cmd_lock);
	ionic_dev_cmd_adminq_init(idev, qcq, lif->index, qcq->intr.index);
	err = ionic_dev_cmd_wait(lif->ionic, DEVCMD_TIMEOUT);
	ionic_dev_cmd_comp(idev, (union ionic_dev_cmd_comp *)&comp);
	mutex_unlock(&lif->ionic->dev_cmd_lock);
	if (err) {
		netdev_err(lif->netdev, "adminq init failed %d\n", err);
		return err;
	}

	q->hw_type = comp.hw_type;
	q->hw_index = le32_to_cpu(comp.hw_index);
	q->dbval = IONIC_DBELL_QID(q->hw_index);

	dev_dbg(dev, "adminq->hw_type %d\n", q->hw_type);
	dev_dbg(dev, "adminq->hw_index %d\n", q->hw_index);

	netif_napi_add(lif->netdev, &qcq->napi, ionic_adminq_napi,
		       NAPI_POLL_WEIGHT);

	napi_enable(&qcq->napi);

	if (qcq->flags & IONIC_QCQ_F_INTR)
		ionic_intr_mask(idev->intr_ctrl, qcq->intr.index,
				IONIC_INTR_MASK_CLEAR);

	qcq->flags |= IONIC_QCQ_F_INITED;

	return 0;
}

S
Shannon Nelson 已提交
2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764
static int ionic_lif_notifyq_init(struct ionic_lif *lif)
{
	struct ionic_qcq *qcq = lif->notifyqcq;
	struct device *dev = lif->ionic->dev;
	struct ionic_queue *q = &qcq->q;
	int err;

	struct ionic_admin_ctx ctx = {
		.work = COMPLETION_INITIALIZER_ONSTACK(ctx.work),
		.cmd.q_init = {
			.opcode = IONIC_CMD_Q_INIT,
			.lif_index = cpu_to_le16(lif->index),
			.type = q->type,
2765
			.ver = lif->qtype_info[q->type].version,
S
Shannon Nelson 已提交
2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784
			.index = cpu_to_le32(q->index),
			.flags = cpu_to_le16(IONIC_QINIT_F_IRQ |
					     IONIC_QINIT_F_ENA),
			.intr_index = cpu_to_le16(lif->adminqcq->intr.index),
			.pid = cpu_to_le16(q->pid),
			.ring_size = ilog2(q->num_descs),
			.ring_base = cpu_to_le64(q->base_pa),
		}
	};

	dev_dbg(dev, "notifyq_init.pid %d\n", ctx.cmd.q_init.pid);
	dev_dbg(dev, "notifyq_init.index %d\n", ctx.cmd.q_init.index);
	dev_dbg(dev, "notifyq_init.ring_base 0x%llx\n", ctx.cmd.q_init.ring_base);
	dev_dbg(dev, "notifyq_init.ring_size %d\n", ctx.cmd.q_init.ring_size);

	err = ionic_adminq_post_wait(lif, &ctx);
	if (err)
		return err;

S
Shannon Nelson 已提交
2785
	lif->last_eid = 0;
S
Shannon Nelson 已提交
2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800
	q->hw_type = ctx.comp.q_init.hw_type;
	q->hw_index = le32_to_cpu(ctx.comp.q_init.hw_index);
	q->dbval = IONIC_DBELL_QID(q->hw_index);

	dev_dbg(dev, "notifyq->hw_type %d\n", q->hw_type);
	dev_dbg(dev, "notifyq->hw_index %d\n", q->hw_index);

	/* preset the callback info */
	q->info[0].cb_arg = lif;

	qcq->flags |= IONIC_QCQ_F_INITED;

	return 0;
}

2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817
static int ionic_station_set(struct ionic_lif *lif)
{
	struct net_device *netdev = lif->netdev;
	struct ionic_admin_ctx ctx = {
		.work = COMPLETION_INITIALIZER_ONSTACK(ctx.work),
		.cmd.lif_getattr = {
			.opcode = IONIC_CMD_LIF_GETATTR,
			.index = cpu_to_le16(lif->index),
			.attr = IONIC_LIF_ATTR_MAC,
		},
	};
	struct sockaddr addr;
	int err;

	err = ionic_adminq_post_wait(lif, &ctx);
	if (err)
		return err;
2818 2819
	netdev_dbg(lif->netdev, "found initial MAC addr %pM\n",
		   ctx.comp.lif_getattr.mac);
2820 2821 2822
	if (is_zero_ether_addr(ctx.comp.lif_getattr.mac))
		return 0;

2823 2824 2825 2826 2827 2828 2829 2830
	if (!is_zero_ether_addr(netdev->dev_addr)) {
		/* If the netdev mac is non-zero and doesn't match the default
		 * device address, it was set by something earlier and we're
		 * likely here again after a fw-upgrade reset.  We need to be
		 * sure the netdev mac is in our filter list.
		 */
		if (!ether_addr_equal(ctx.comp.lif_getattr.mac,
				      netdev->dev_addr))
S
Shannon Nelson 已提交
2831
			ionic_lif_addr(lif, netdev->dev_addr, ADD_ADDR, CAN_SLEEP);
2832 2833
	} else {
		/* Update the netdev mac with the device's mac */
2834 2835 2836 2837 2838 2839 2840 2841
		memcpy(addr.sa_data, ctx.comp.lif_getattr.mac, netdev->addr_len);
		addr.sa_family = AF_INET;
		err = eth_prepare_mac_addr_change(netdev, &addr);
		if (err) {
			netdev_warn(lif->netdev, "ignoring bad MAC addr from NIC %pM - err %d\n",
				    addr.sa_data, err);
			return 0;
		}
2842

2843 2844
		eth_commit_mac_addr_change(netdev, &addr);
	}
2845

2846 2847
	netdev_dbg(lif->netdev, "adding station MAC addr %pM\n",
		   netdev->dev_addr);
S
Shannon Nelson 已提交
2848
	ionic_lif_addr(lif, netdev->dev_addr, ADD_ADDR, CAN_SLEEP);
2849 2850 2851 2852

	return 0;
}

S
Shannon Nelson 已提交
2853
int ionic_lif_init(struct ionic_lif *lif)
S
Shannon Nelson 已提交
2854 2855
{
	struct ionic_dev *idev = &lif->ionic->idev;
2856
	struct device *dev = lif->ionic->dev;
S
Shannon Nelson 已提交
2857
	struct ionic_lif_init_comp comp;
2858
	int dbpage_num;
S
Shannon Nelson 已提交
2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869
	int err;

	mutex_lock(&lif->ionic->dev_cmd_lock);
	ionic_dev_cmd_lif_init(idev, lif->index, lif->info_pa);
	err = ionic_dev_cmd_wait(lif->ionic, DEVCMD_TIMEOUT);
	ionic_dev_cmd_comp(idev, (union ionic_dev_cmd_comp *)&comp);
	mutex_unlock(&lif->ionic->dev_cmd_lock);
	if (err)
		return err;

	lif->hw_index = le16_to_cpu(comp.hw_index);
2870
	mutex_init(&lif->queue_lock);
S
Shannon Nelson 已提交
2871

2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896
	/* now that we have the hw_index we can figure out our doorbell page */
	lif->dbid_count = le32_to_cpu(lif->ionic->ident.dev.ndbpgs_per_lif);
	if (!lif->dbid_count) {
		dev_err(dev, "No doorbell pages, aborting\n");
		return -EINVAL;
	}

	lif->dbid_inuse = bitmap_alloc(lif->dbid_count, GFP_KERNEL);
	if (!lif->dbid_inuse) {
		dev_err(dev, "Failed alloc doorbell id bitmap, aborting\n");
		return -ENOMEM;
	}

	/* first doorbell id reserved for kernel (dbid aka pid == zero) */
	set_bit(0, lif->dbid_inuse);
	lif->kern_pid = 0;

	dbpage_num = ionic_db_page_num(lif, lif->kern_pid);
	lif->kern_dbpage = ionic_bus_map_dbpage(lif->ionic, dbpage_num);
	if (!lif->kern_dbpage) {
		dev_err(dev, "Cannot map dbpage, aborting\n");
		err = -ENOMEM;
		goto err_out_free_dbid;
	}

S
Shannon Nelson 已提交
2897 2898 2899 2900
	err = ionic_lif_adminq_init(lif);
	if (err)
		goto err_out_adminq_deinit;

S
Shannon Nelson 已提交
2901 2902 2903 2904 2905 2906
	if (lif->ionic->nnqs_per_lif) {
		err = ionic_lif_notifyq_init(lif);
		if (err)
			goto err_out_notifyq_deinit;
	}

2907 2908 2909 2910
	err = ionic_init_nic_features(lif);
	if (err)
		goto err_out_notifyq_deinit;

2911 2912 2913 2914 2915
	if (!test_bit(IONIC_LIF_F_FW_RESET, lif->state)) {
		err = ionic_rx_filters_init(lif);
		if (err)
			goto err_out_notifyq_deinit;
	}
2916

2917 2918 2919 2920
	err = ionic_station_set(lif);
	if (err)
		goto err_out_notifyq_deinit;

S
Shannon Nelson 已提交
2921 2922
	lif->rx_copybreak = IONIC_RX_COPYBREAK_DEFAULT;

S
Shannon Nelson 已提交
2923
	set_bit(IONIC_LIF_F_INITED, lif->state);
S
Shannon Nelson 已提交
2924

2925 2926
	INIT_WORK(&lif->tx_timeout_work, ionic_tx_timeout_work);

S
Shannon Nelson 已提交
2927
	return 0;
2928

S
Shannon Nelson 已提交
2929 2930
err_out_notifyq_deinit:
	ionic_lif_qcq_deinit(lif, lif->notifyqcq);
S
Shannon Nelson 已提交
2931 2932 2933 2934 2935
err_out_adminq_deinit:
	ionic_lif_qcq_deinit(lif, lif->adminqcq);
	ionic_lif_reset(lif);
	ionic_bus_unmap_dbpage(lif->ionic, lif->kern_dbpage);
	lif->kern_dbpage = NULL;
2936 2937 2938 2939 2940
err_out_free_dbid:
	kfree(lif->dbid_inuse);
	lif->dbid_inuse = NULL;

	return err;
S
Shannon Nelson 已提交
2941 2942
}

2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990
static void ionic_lif_notify_work(struct work_struct *ws)
{
}

static void ionic_lif_set_netdev_info(struct ionic_lif *lif)
{
	struct ionic_admin_ctx ctx = {
		.work = COMPLETION_INITIALIZER_ONSTACK(ctx.work),
		.cmd.lif_setattr = {
			.opcode = IONIC_CMD_LIF_SETATTR,
			.index = cpu_to_le16(lif->index),
			.attr = IONIC_LIF_ATTR_NAME,
		},
	};

	strlcpy(ctx.cmd.lif_setattr.name, lif->netdev->name,
		sizeof(ctx.cmd.lif_setattr.name));

	ionic_adminq_post_wait(lif, &ctx);
}

static struct ionic_lif *ionic_netdev_lif(struct net_device *netdev)
{
	if (!netdev || netdev->netdev_ops->ndo_start_xmit != ionic_start_xmit)
		return NULL;

	return netdev_priv(netdev);
}

static int ionic_lif_notify(struct notifier_block *nb,
			    unsigned long event, void *info)
{
	struct net_device *ndev = netdev_notifier_info_to_dev(info);
	struct ionic *ionic = container_of(nb, struct ionic, nb);
	struct ionic_lif *lif = ionic_netdev_lif(ndev);

	if (!lif || lif->ionic != ionic)
		return NOTIFY_DONE;

	switch (event) {
	case NETDEV_CHANGENAME:
		ionic_lif_set_netdev_info(lif);
		break;
	}

	return NOTIFY_DONE;
}

S
Shannon Nelson 已提交
2991
int ionic_lif_register(struct ionic_lif *lif)
2992 2993 2994
{
	int err;

S
Shannon Nelson 已提交
2995
	INIT_WORK(&lif->ionic->nb_work, ionic_lif_notify_work);
2996

S
Shannon Nelson 已提交
2997
	lif->ionic->nb.notifier_call = ionic_lif_notify;
2998

S
Shannon Nelson 已提交
2999
	err = register_netdevice_notifier(&lif->ionic->nb);
3000
	if (err)
S
Shannon Nelson 已提交
3001
		lif->ionic->nb.notifier_call = NULL;
3002

3003
	/* only register LIF0 for now */
S
Shannon Nelson 已提交
3004
	err = register_netdev(lif->netdev);
3005
	if (err) {
S
Shannon Nelson 已提交
3006
		dev_err(lif->ionic->dev, "Cannot register net device, aborting\n");
3007 3008
		return err;
	}
3009

S
Shannon Nelson 已提交
3010
	ionic_link_status_check_request(lif, CAN_SLEEP);
S
Shannon Nelson 已提交
3011 3012
	lif->registered = true;
	ionic_lif_set_netdev_info(lif);
3013 3014 3015 3016

	return 0;
}

S
Shannon Nelson 已提交
3017
void ionic_lif_unregister(struct ionic_lif *lif)
3018
{
S
Shannon Nelson 已提交
3019 3020 3021 3022
	if (lif->ionic->nb.notifier_call) {
		unregister_netdevice_notifier(&lif->ionic->nb);
		cancel_work_sync(&lif->ionic->nb_work);
		lif->ionic->nb.notifier_call = NULL;
3023 3024
	}

S
Shannon Nelson 已提交
3025 3026 3027
	if (lif->netdev->reg_state == NETREG_REGISTERED)
		unregister_netdev(lif->netdev);
	lif->registered = false;
3028 3029
}

3030 3031
static void ionic_lif_queue_identify(struct ionic_lif *lif)
{
S
Shannon Nelson 已提交
3032
	union ionic_q_identity __iomem *q_ident;
3033 3034 3035 3036 3037 3038
	struct ionic *ionic = lif->ionic;
	struct ionic_dev *idev;
	int qtype;
	int err;

	idev = &lif->ionic->idev;
S
Shannon Nelson 已提交
3039
	q_ident = (union ionic_q_identity __iomem *)&idev->dev_cmd_regs->data;
3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061

	for (qtype = 0; qtype < ARRAY_SIZE(ionic_qtype_versions); qtype++) {
		struct ionic_qtype_info *qti = &lif->qtype_info[qtype];

		/* filter out the ones we know about */
		switch (qtype) {
		case IONIC_QTYPE_ADMINQ:
		case IONIC_QTYPE_NOTIFYQ:
		case IONIC_QTYPE_RXQ:
		case IONIC_QTYPE_TXQ:
			break;
		default:
			continue;
		}

		memset(qti, 0, sizeof(*qti));

		mutex_lock(&ionic->dev_cmd_lock);
		ionic_dev_cmd_queue_identify(idev, lif->lif_type, qtype,
					     ionic_qtype_versions[qtype]);
		err = ionic_dev_cmd_wait(ionic, DEVCMD_TIMEOUT);
		if (!err) {
S
Shannon Nelson 已提交
3062 3063 3064 3065 3066 3067 3068 3069
			qti->version   = readb(&q_ident->version);
			qti->supported = readb(&q_ident->supported);
			qti->features  = readq(&q_ident->features);
			qti->desc_sz   = readw(&q_ident->desc_sz);
			qti->comp_sz   = readw(&q_ident->comp_sz);
			qti->sg_desc_sz   = readw(&q_ident->sg_desc_sz);
			qti->max_sg_elems = readw(&q_ident->max_sg_elems);
			qti->sg_desc_stride = readw(&q_ident->sg_desc_stride);
3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103
		}
		mutex_unlock(&ionic->dev_cmd_lock);

		if (err == -EINVAL) {
			dev_err(ionic->dev, "qtype %d not supported\n", qtype);
			continue;
		} else if (err == -EIO) {
			dev_err(ionic->dev, "q_ident failed, not supported on older FW\n");
			return;
		} else if (err) {
			dev_err(ionic->dev, "q_ident failed, qtype %d: %d\n",
				qtype, err);
			return;
		}

		dev_dbg(ionic->dev, " qtype[%d].version = %d\n",
			qtype, qti->version);
		dev_dbg(ionic->dev, " qtype[%d].supported = 0x%02x\n",
			qtype, qti->supported);
		dev_dbg(ionic->dev, " qtype[%d].features = 0x%04llx\n",
			qtype, qti->features);
		dev_dbg(ionic->dev, " qtype[%d].desc_sz = %d\n",
			qtype, qti->desc_sz);
		dev_dbg(ionic->dev, " qtype[%d].comp_sz = %d\n",
			qtype, qti->comp_sz);
		dev_dbg(ionic->dev, " qtype[%d].sg_desc_sz = %d\n",
			qtype, qti->sg_desc_sz);
		dev_dbg(ionic->dev, " qtype[%d].max_sg_elems = %d\n",
			qtype, qti->max_sg_elems);
		dev_dbg(ionic->dev, " qtype[%d].sg_desc_stride = %d\n",
			qtype, qti->sg_desc_stride);
	}
}

S
Shannon Nelson 已提交
3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145
int ionic_lif_identify(struct ionic *ionic, u8 lif_type,
		       union ionic_lif_identity *lid)
{
	struct ionic_dev *idev = &ionic->idev;
	size_t sz;
	int err;

	sz = min(sizeof(*lid), sizeof(idev->dev_cmd_regs->data));

	mutex_lock(&ionic->dev_cmd_lock);
	ionic_dev_cmd_lif_identify(idev, lif_type, IONIC_IDENTITY_VERSION_1);
	err = ionic_dev_cmd_wait(ionic, DEVCMD_TIMEOUT);
	memcpy_fromio(lid, &idev->dev_cmd_regs->data, sz);
	mutex_unlock(&ionic->dev_cmd_lock);
	if (err)
		return (err);

	dev_dbg(ionic->dev, "capabilities 0x%llx\n",
		le64_to_cpu(lid->capabilities));

	dev_dbg(ionic->dev, "eth.max_ucast_filters %d\n",
		le32_to_cpu(lid->eth.max_ucast_filters));
	dev_dbg(ionic->dev, "eth.max_mcast_filters %d\n",
		le32_to_cpu(lid->eth.max_mcast_filters));
	dev_dbg(ionic->dev, "eth.features 0x%llx\n",
		le64_to_cpu(lid->eth.config.features));
	dev_dbg(ionic->dev, "eth.queue_count[IONIC_QTYPE_ADMINQ] %d\n",
		le32_to_cpu(lid->eth.config.queue_count[IONIC_QTYPE_ADMINQ]));
	dev_dbg(ionic->dev, "eth.queue_count[IONIC_QTYPE_NOTIFYQ] %d\n",
		le32_to_cpu(lid->eth.config.queue_count[IONIC_QTYPE_NOTIFYQ]));
	dev_dbg(ionic->dev, "eth.queue_count[IONIC_QTYPE_RXQ] %d\n",
		le32_to_cpu(lid->eth.config.queue_count[IONIC_QTYPE_RXQ]));
	dev_dbg(ionic->dev, "eth.queue_count[IONIC_QTYPE_TXQ] %d\n",
		le32_to_cpu(lid->eth.config.queue_count[IONIC_QTYPE_TXQ]));
	dev_dbg(ionic->dev, "eth.config.name %s\n", lid->eth.config.name);
	dev_dbg(ionic->dev, "eth.config.mac %pM\n", lid->eth.config.mac);
	dev_dbg(ionic->dev, "eth.config.mtu %d\n",
		le32_to_cpu(lid->eth.config.mtu));

	return 0;
}

S
Shannon Nelson 已提交
3146
int ionic_lif_size(struct ionic *ionic)
S
Shannon Nelson 已提交
3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220
{
	struct ionic_identity *ident = &ionic->ident;
	unsigned int nintrs, dev_nintrs;
	union ionic_lif_config *lc;
	unsigned int ntxqs_per_lif;
	unsigned int nrxqs_per_lif;
	unsigned int neqs_per_lif;
	unsigned int nnqs_per_lif;
	unsigned int nxqs, neqs;
	unsigned int min_intrs;
	int err;

	lc = &ident->lif.eth.config;
	dev_nintrs = le32_to_cpu(ident->dev.nintrs);
	neqs_per_lif = le32_to_cpu(ident->lif.rdma.eq_qtype.qid_count);
	nnqs_per_lif = le32_to_cpu(lc->queue_count[IONIC_QTYPE_NOTIFYQ]);
	ntxqs_per_lif = le32_to_cpu(lc->queue_count[IONIC_QTYPE_TXQ]);
	nrxqs_per_lif = le32_to_cpu(lc->queue_count[IONIC_QTYPE_RXQ]);

	nxqs = min(ntxqs_per_lif, nrxqs_per_lif);
	nxqs = min(nxqs, num_online_cpus());
	neqs = min(neqs_per_lif, num_online_cpus());

try_again:
	/* interrupt usage:
	 *    1 for master lif adminq/notifyq
	 *    1 for each CPU for master lif TxRx queue pairs
	 *    whatever's left is for RDMA queues
	 */
	nintrs = 1 + nxqs + neqs;
	min_intrs = 2;  /* adminq + 1 TxRx queue pair */

	if (nintrs > dev_nintrs)
		goto try_fewer;

	err = ionic_bus_alloc_irq_vectors(ionic, nintrs);
	if (err < 0 && err != -ENOSPC) {
		dev_err(ionic->dev, "Can't get intrs from OS: %d\n", err);
		return err;
	}
	if (err == -ENOSPC)
		goto try_fewer;

	if (err != nintrs) {
		ionic_bus_free_irq_vectors(ionic);
		goto try_fewer;
	}

	ionic->nnqs_per_lif = nnqs_per_lif;
	ionic->neqs_per_lif = neqs;
	ionic->ntxqs_per_lif = nxqs;
	ionic->nrxqs_per_lif = nxqs;
	ionic->nintrs = nintrs;

	ionic_debugfs_add_sizes(ionic);

	return 0;

try_fewer:
	if (nnqs_per_lif > 1) {
		nnqs_per_lif >>= 1;
		goto try_again;
	}
	if (neqs > 1) {
		neqs >>= 1;
		goto try_again;
	}
	if (nxqs > 1) {
		nxqs >>= 1;
		goto try_again;
	}
	dev_err(ionic->dev, "Can't get minimum %d intrs from OS\n", min_intrs);
	return -ENOSPC;
}