caif_hsi.c 35.8 KB
Newer Older
1 2 3 4 5 6 7 8
/*
 * Copyright (C) ST-Ericsson AB 2010
 * Contact: Sjur Brendeland / sjur.brandeland@stericsson.com
 * Author:  Daniel Martensson / daniel.martensson@stericsson.com
 *	    Dmitry.Tarnyagin  / dmitry.tarnyagin@stericsson.com
 * License terms: GNU General Public License (GPL) version 2.
 */

9 10
#define pr_fmt(fmt) KBUILD_MODNAME fmt

11 12 13 14 15 16 17 18 19 20 21 22
#include <linux/init.h>
#include <linux/module.h>
#include <linux/device.h>
#include <linux/platform_device.h>
#include <linux/netdevice.h>
#include <linux/string.h>
#include <linux/list.h>
#include <linux/interrupt.h>
#include <linux/delay.h>
#include <linux/sched.h>
#include <linux/if_arp.h>
#include <linux/timer.h>
Sjur Brændeland's avatar
Sjur Brændeland committed
23
#include <net/rtnetlink.h>
24
#include <linux/pkt_sched.h>
25 26 27 28 29 30 31 32 33 34 35
#include <net/caif/caif_layer.h>
#include <net/caif/caif_hsi.h>

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Daniel Martensson<daniel.martensson@stericsson.com>");
MODULE_DESCRIPTION("CAIF HSI driver");

/* Returns the number of padding bytes for alignment. */
#define PAD_POW2(x, pow) ((((x)&((pow)-1)) == 0) ? 0 :\
				(((pow)-((x)&((pow)-1)))))

36 37 38 39
static int inactivity_timeout = 1000;
module_param(inactivity_timeout, int, S_IRUGO | S_IWUSR);
MODULE_PARM_DESC(inactivity_timeout, "Inactivity timeout on HSI, ms.");

40 41 42 43
static int aggregation_timeout = 1;
module_param(aggregation_timeout, int, S_IRUGO | S_IWUSR);
MODULE_PARM_DESC(aggregation_timeout, "Aggregation timeout on HSI, ms.");

44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
/*
 * HSI padding options.
 * Warning: must be a base of 2 (& operation used) and can not be zero !
 */
static int hsi_head_align = 4;
module_param(hsi_head_align, int, S_IRUGO);
MODULE_PARM_DESC(hsi_head_align, "HSI head alignment.");

static int hsi_tail_align = 4;
module_param(hsi_tail_align, int, S_IRUGO);
MODULE_PARM_DESC(hsi_tail_align, "HSI tail alignment.");

/*
 * HSI link layer flowcontrol thresholds.
 * Warning: A high threshold value migth increase throughput but it will at
 * the same time prevent channel prioritization and increase the risk of
 * flooding the modem. The high threshold should be above the low.
 */
static int hsi_high_threshold = 100;
module_param(hsi_high_threshold, int, S_IRUGO);
MODULE_PARM_DESC(hsi_high_threshold, "HSI high threshold (FLOW OFF).");

static int hsi_low_threshold = 50;
module_param(hsi_low_threshold, int, S_IRUGO);
MODULE_PARM_DESC(hsi_low_threshold, "HSI high threshold (FLOW ON).");

#define ON 1
#define OFF 0

/*
 * Threshold values for the HSI packet queue. Flowcontrol will be asserted
 * when the number of packets exceeds HIGH_WATER_MARK. It will not be
 * de-asserted before the number of packets drops below LOW_WATER_MARK.
 */
#define LOW_WATER_MARK   hsi_low_threshold
#define HIGH_WATER_MARK  hsi_high_threshold

static LIST_HEAD(cfhsi_list);

static void cfhsi_inactivity_tout(unsigned long arg)
{
	struct cfhsi *cfhsi = (struct cfhsi *)arg;

87
	netdev_dbg(cfhsi->ndev, "%s.\n",
88 89 90 91 92 93 94
		__func__);

	/* Schedule power down work queue. */
	if (!test_bit(CFHSI_SHUTDOWN, &cfhsi->bits))
		queue_work(cfhsi->wq, &cfhsi->wake_down_work);
}

95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
static void cfhsi_update_aggregation_stats(struct cfhsi *cfhsi,
					   const struct sk_buff *skb,
					   int direction)
{
	struct caif_payload_info *info;
	int hpad, tpad, len;

	info = (struct caif_payload_info *)&skb->cb;
	hpad = 1 + PAD_POW2((info->hdr_len + 1), hsi_head_align);
	tpad = PAD_POW2((skb->len + hpad), hsi_tail_align);
	len = skb->len + hpad + tpad;

	if (direction > 0)
		cfhsi->aggregation_len += len;
	else if (direction < 0)
		cfhsi->aggregation_len -= len;
}

static bool cfhsi_can_send_aggregate(struct cfhsi *cfhsi)
{
	int i;

117
	if (cfhsi->aggregation_timeout == 0)
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
		return true;

	for (i = 0; i < CFHSI_PRIO_BEBK; ++i) {
		if (cfhsi->qhead[i].qlen)
			return true;
	}

	/* TODO: Use aggregation_len instead */
	if (cfhsi->qhead[CFHSI_PRIO_BEBK].qlen >= CFHSI_MAX_PKTS)
		return true;

	return false;
}

static struct sk_buff *cfhsi_dequeue(struct cfhsi *cfhsi)
{
	struct sk_buff *skb;
	int i;

	for (i = 0; i < CFHSI_PRIO_LAST; ++i) {
		skb = skb_dequeue(&cfhsi->qhead[i]);
		if (skb)
			break;
	}

	return skb;
}

static int cfhsi_tx_queue_len(struct cfhsi *cfhsi)
{
	int i, len = 0;
	for (i = 0; i < CFHSI_PRIO_LAST; ++i)
		len += skb_queue_len(&cfhsi->qhead[i]);
	return len;
}

154 155 156 157 158 159
static void cfhsi_abort_tx(struct cfhsi *cfhsi)
{
	struct sk_buff *skb;

	for (;;) {
		spin_lock_bh(&cfhsi->lock);
160
		skb = cfhsi_dequeue(cfhsi);
161 162 163 164 165
		if (!skb)
			break;

		cfhsi->ndev->stats.tx_errors++;
		cfhsi->ndev->stats.tx_dropped++;
166
		cfhsi_update_aggregation_stats(cfhsi, skb, -1);
167 168 169 170 171
		spin_unlock_bh(&cfhsi->lock);
		kfree_skb(skb);
	}
	cfhsi->tx_state = CFHSI_TX_STATE_IDLE;
	if (!test_bit(CFHSI_SHUTDOWN, &cfhsi->bits))
172
		mod_timer(&cfhsi->inactivity_timer,
173
			jiffies + cfhsi->inactivity_timeout);
174 175 176 177 178 179 180 181 182
	spin_unlock_bh(&cfhsi->lock);
}

static int cfhsi_flush_fifo(struct cfhsi *cfhsi)
{
	char buffer[32]; /* Any reasonable value */
	size_t fifo_occupancy;
	int ret;

183
	netdev_dbg(cfhsi->ndev, "%s.\n",
184 185 186 187 188 189
		__func__);

	do {
		ret = cfhsi->dev->cfhsi_fifo_occupancy(cfhsi->dev,
				&fifo_occupancy);
		if (ret) {
190
			netdev_warn(cfhsi->ndev,
191 192 193 194 195 196 197 198 199 200 201 202 203
				"%s: can't get FIFO occupancy: %d.\n",
				__func__, ret);
			break;
		} else if (!fifo_occupancy)
			/* No more data, exitting normally */
			break;

		fifo_occupancy = min(sizeof(buffer), fifo_occupancy);
		set_bit(CFHSI_FLUSH_FIFO, &cfhsi->bits);
		ret = cfhsi->dev->cfhsi_rx(buffer, fifo_occupancy,
				cfhsi->dev);
		if (ret) {
			clear_bit(CFHSI_FLUSH_FIFO, &cfhsi->bits);
204
			netdev_warn(cfhsi->ndev,
205 206 207 208 209 210
				"%s: can't read data: %d.\n",
				__func__, ret);
			break;
		}

		ret = 5 * HZ;
211
		ret = wait_event_interruptible_timeout(cfhsi->flush_fifo_wait,
212 213 214
			 !test_bit(CFHSI_FLUSH_FIFO, &cfhsi->bits), ret);

		if (ret < 0) {
215
			netdev_warn(cfhsi->ndev,
216 217 218 219 220
				"%s: can't wait for flush complete: %d.\n",
				__func__, ret);
			break;
		} else if (!ret) {
			ret = -ETIMEDOUT;
221
			netdev_warn(cfhsi->ndev,
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
				"%s: timeout waiting for flush complete.\n",
				__func__);
			break;
		}
	} while (1);

	return ret;
}

static int cfhsi_tx_frm(struct cfhsi_desc *desc, struct cfhsi *cfhsi)
{
	int nfrms = 0;
	int pld_len = 0;
	struct sk_buff *skb;
	u8 *pfrm = desc->emb_frm + CFHSI_MAX_EMB_FRM_SZ;

238
	skb = cfhsi_dequeue(cfhsi);
239 240 241
	if (!skb)
		return 0;

242 243 244
	/* Clear offset. */
	desc->offset = 0;

245 246 247
	/* Check if we can embed a CAIF frame. */
	if (skb->len < CFHSI_MAX_EMB_FRM_SZ) {
		struct caif_payload_info *info;
248 249
		int hpad;
		int tpad;
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264

		/* Calculate needed head alignment and tail alignment. */
		info = (struct caif_payload_info *)&skb->cb;

		hpad = 1 + PAD_POW2((info->hdr_len + 1), hsi_head_align);
		tpad = PAD_POW2((skb->len + hpad), hsi_tail_align);

		/* Check if frame still fits with added alignment. */
		if ((skb->len + hpad + tpad) <= CFHSI_MAX_EMB_FRM_SZ) {
			u8 *pemb = desc->emb_frm;
			desc->offset = CFHSI_DESC_SHORT_SZ;
			*pemb = (u8)(hpad - 1);
			pemb += hpad;

			/* Update network statistics. */
265
			spin_lock_bh(&cfhsi->lock);
266 267
			cfhsi->ndev->stats.tx_packets++;
			cfhsi->ndev->stats.tx_bytes += skb->len;
268 269
			cfhsi_update_aggregation_stats(cfhsi, skb, -1);
			spin_unlock_bh(&cfhsi->lock);
270 271 272

			/* Copy in embedded CAIF frame. */
			skb_copy_bits(skb, 0, pemb, skb->len);
273 274

			/* Consume the SKB */
275 276 277
			consume_skb(skb);
			skb = NULL;
		}
278
	}
279 280 281 282 283

	/* Create payload CAIF frames. */
	pfrm = desc->emb_frm + CFHSI_MAX_EMB_FRM_SZ;
	while (nfrms < CFHSI_MAX_PKTS) {
		struct caif_payload_info *info;
284 285
		int hpad;
		int tpad;
286 287

		if (!skb)
288
			skb = cfhsi_dequeue(cfhsi);
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306

		if (!skb)
			break;

		/* Calculate needed head alignment and tail alignment. */
		info = (struct caif_payload_info *)&skb->cb;

		hpad = 1 + PAD_POW2((info->hdr_len + 1), hsi_head_align);
		tpad = PAD_POW2((skb->len + hpad), hsi_tail_align);

		/* Fill in CAIF frame length in descriptor. */
		desc->cffrm_len[nfrms] = hpad + skb->len + tpad;

		/* Fill head padding information. */
		*pfrm = (u8)(hpad - 1);
		pfrm += hpad;

		/* Update network statistics. */
307
		spin_lock_bh(&cfhsi->lock);
308 309
		cfhsi->ndev->stats.tx_packets++;
		cfhsi->ndev->stats.tx_bytes += skb->len;
310 311
		cfhsi_update_aggregation_stats(cfhsi, skb, -1);
		spin_unlock_bh(&cfhsi->lock);
312 313 314 315 316 317 318 319 320

		/* Copy in CAIF frame. */
		skb_copy_bits(skb, 0, pfrm, skb->len);

		/* Update payload length. */
		pld_len += desc->cffrm_len[nfrms];

		/* Update frame pointer. */
		pfrm += skb->len + tpad;
321 322

		/* Consume the SKB */
323 324 325 326 327 328 329 330 331 332 333 334 335 336
		consume_skb(skb);
		skb = NULL;

		/* Update number of frames. */
		nfrms++;
	}

	/* Unused length fields should be zero-filled (according to SPEC). */
	while (nfrms < CFHSI_MAX_PKTS) {
		desc->cffrm_len[nfrms] = 0x0000;
		nfrms++;
	}

	/* Check if we can piggy-back another descriptor. */
337
	if (cfhsi_can_send_aggregate(cfhsi))
338 339 340 341 342 343 344
		desc->header |= CFHSI_PIGGY_DESC;
	else
		desc->header &= ~CFHSI_PIGGY_DESC;

	return CFHSI_DESC_SZ + pld_len;
}

345
static void cfhsi_start_tx(struct cfhsi *cfhsi)
346
{
347 348
	struct cfhsi_desc *desc = (struct cfhsi_desc *)cfhsi->tx_buf;
	int len, res;
349

350
	netdev_dbg(cfhsi->ndev, "%s.\n", __func__);
351 352 353 354 355 356

	if (test_bit(CFHSI_SHUTDOWN, &cfhsi->bits))
		return;

	do {
		/* Create HSI frame. */
357 358 359 360
		len = cfhsi_tx_frm(desc, cfhsi);
		if (!len) {
			spin_lock_bh(&cfhsi->lock);
			if (unlikely(cfhsi_tx_queue_len(cfhsi))) {
361
				spin_unlock_bh(&cfhsi->lock);
362 363
				res = -EAGAIN;
				continue;
364
			}
365 366 367 368 369 370 371
			cfhsi->tx_state = CFHSI_TX_STATE_IDLE;
			/* Start inactivity timer. */
			mod_timer(&cfhsi->inactivity_timer,
				jiffies + cfhsi->inactivity_timeout);
			spin_unlock_bh(&cfhsi->lock);
			break;
		}
372 373 374

		/* Set up new transfer. */
		res = cfhsi->dev->cfhsi_tx(cfhsi->tx_buf, len, cfhsi->dev);
375
		if (WARN_ON(res < 0))
376
			netdev_err(cfhsi->ndev, "%s: TX error %d.\n",
377 378
				__func__, res);
	} while (res < 0);
379 380 381 382
}

static void cfhsi_tx_done(struct cfhsi *cfhsi)
{
383
	netdev_dbg(cfhsi->ndev, "%s.\n", __func__);
384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408

	if (test_bit(CFHSI_SHUTDOWN, &cfhsi->bits))
		return;

	/*
	 * Send flow on if flow off has been previously signalled
	 * and number of packets is below low water mark.
	 */
	spin_lock_bh(&cfhsi->lock);
	if (cfhsi->flow_off_sent &&
			cfhsi_tx_queue_len(cfhsi) <= cfhsi->q_low_mark &&
			cfhsi->cfdev.flowctrl) {

		cfhsi->flow_off_sent = 0;
		cfhsi->cfdev.flowctrl(cfhsi->ndev, ON);
	}

	if (cfhsi_can_send_aggregate(cfhsi)) {
		spin_unlock_bh(&cfhsi->lock);
		cfhsi_start_tx(cfhsi);
	} else {
		mod_timer(&cfhsi->aggregation_timer,
			jiffies + cfhsi->aggregation_timeout);
		spin_unlock_bh(&cfhsi->lock);
	}
409 410

	return;
411 412 413 414 415 416 417
}

static void cfhsi_tx_done_cb(struct cfhsi_drv *drv)
{
	struct cfhsi *cfhsi;

	cfhsi = container_of(drv, struct cfhsi, drv);
418
	netdev_dbg(cfhsi->ndev, "%s.\n",
419 420 421 422
		__func__);

	if (test_bit(CFHSI_SHUTDOWN, &cfhsi->bits))
		return;
423
	cfhsi_tx_done(cfhsi);
424 425
}

426
static int cfhsi_rx_desc(struct cfhsi_desc *desc, struct cfhsi *cfhsi)
427 428 429 430 431 432 433 434
{
	int xfer_sz = 0;
	int nfrms = 0;
	u16 *plen = NULL;
	u8 *pfrm = NULL;

	if ((desc->header & ~CFHSI_PIGGY_DESC) ||
			(desc->offset > CFHSI_MAX_EMB_FRM_SZ)) {
435
		netdev_err(cfhsi->ndev, "%s: Invalid descriptor.\n",
436
			__func__);
437
		return -EPROTO;
438 439 440 441 442 443
	}

	/* Check for embedded CAIF frame. */
	if (desc->offset) {
		struct sk_buff *skb;
		u8 *dst = NULL;
444
		int len = 0;
445 446 447 448 449 450 451 452 453 454
		pfrm = ((u8 *)desc) + desc->offset;

		/* Remove offset padding. */
		pfrm += *pfrm + 1;

		/* Read length of CAIF frame (little endian). */
		len = *pfrm;
		len |= ((*(pfrm+1)) << 8) & 0xFF00;
		len += 2;	/* Add FCS fields. */

455 456
		/* Sanity check length of CAIF frame. */
		if (unlikely(len > CFHSI_MAX_CAIF_FRAME_SZ)) {
457
			netdev_err(cfhsi->ndev, "%s: Invalid length.\n",
458 459 460
				__func__);
			return -EPROTO;
		}
461 462

		/* Allocate SKB (OK even in IRQ context). */
463 464
		skb = alloc_skb(len + 1, GFP_ATOMIC);
		if (!skb) {
465
			netdev_err(cfhsi->ndev, "%s: Out of memory !\n",
466 467
				__func__);
			return -ENOMEM;
468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504
		}
		caif_assert(skb != NULL);

		dst = skb_put(skb, len);
		memcpy(dst, pfrm, len);

		skb->protocol = htons(ETH_P_CAIF);
		skb_reset_mac_header(skb);
		skb->dev = cfhsi->ndev;

		/*
		 * We are called from a arch specific platform device.
		 * Unfortunately we don't know what context we're
		 * running in.
		 */
		if (in_interrupt())
			netif_rx(skb);
		else
			netif_rx_ni(skb);

		/* Update network statistics. */
		cfhsi->ndev->stats.rx_packets++;
		cfhsi->ndev->stats.rx_bytes += len;
	}

	/* Calculate transfer length. */
	plen = desc->cffrm_len;
	while (nfrms < CFHSI_MAX_PKTS && *plen) {
		xfer_sz += *plen;
		plen++;
		nfrms++;
	}

	/* Check for piggy-backed descriptor. */
	if (desc->header & CFHSI_PIGGY_DESC)
		xfer_sz += CFHSI_DESC_SZ;

505
	if ((xfer_sz % 4) || (xfer_sz > (CFHSI_BUF_SZ_RX - CFHSI_DESC_SZ))) {
506
		netdev_err(cfhsi->ndev,
507 508
				"%s: Invalid payload len: %d, ignored.\n",
			__func__, xfer_sz);
509
		return -EPROTO;
510 511 512 513
	}
	return xfer_sz;
}

514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542
static int cfhsi_rx_desc_len(struct cfhsi_desc *desc)
{
	int xfer_sz = 0;
	int nfrms = 0;
	u16 *plen;

	if ((desc->header & ~CFHSI_PIGGY_DESC) ||
			(desc->offset > CFHSI_MAX_EMB_FRM_SZ)) {

		pr_err("Invalid descriptor. %x %x\n", desc->header,
				desc->offset);
		return -EPROTO;
	}

	/* Calculate transfer length. */
	plen = desc->cffrm_len;
	while (nfrms < CFHSI_MAX_PKTS && *plen) {
		xfer_sz += *plen;
		plen++;
		nfrms++;
	}

	if (xfer_sz % 4) {
		pr_err("Invalid payload len: %d, ignored.\n", xfer_sz);
		return -EPROTO;
	}
	return xfer_sz;
}

543
static int cfhsi_rx_pld(struct cfhsi_desc *desc, struct cfhsi *cfhsi)
544 545 546 547 548 549 550 551 552
{
	int rx_sz = 0;
	int nfrms = 0;
	u16 *plen = NULL;
	u8 *pfrm = NULL;

	/* Sanity check header and offset. */
	if (WARN_ON((desc->header & ~CFHSI_PIGGY_DESC) ||
			(desc->offset > CFHSI_MAX_EMB_FRM_SZ))) {
553
		netdev_err(cfhsi->ndev, "%s: Invalid descriptor.\n",
554
			__func__);
555
		return -EPROTO;
556 557 558 559 560
	}

	/* Set frame pointer to start of payload. */
	pfrm = desc->emb_frm + CFHSI_MAX_EMB_FRM_SZ;
	plen = desc->cffrm_len;
561 562 563 564 565 566 567 568 569 570

	/* Skip already processed frames. */
	while (nfrms < cfhsi->rx_state.nfrms) {
		pfrm += *plen;
		rx_sz += *plen;
		plen++;
		nfrms++;
	}

	/* Parse payload. */
571 572 573 574
	while (nfrms < CFHSI_MAX_PKTS && *plen) {
		struct sk_buff *skb;
		u8 *dst = NULL;
		u8 *pcffrm = NULL;
575
		int len;
576 577 578 579 580 581 582 583 584

		/* CAIF frame starts after head padding. */
		pcffrm = pfrm + *pfrm + 1;

		/* Read length of CAIF frame (little endian). */
		len = *pcffrm;
		len |= ((*(pcffrm + 1)) << 8) & 0xFF00;
		len += 2;	/* Add FCS fields. */

585 586
		/* Sanity check length of CAIF frames. */
		if (unlikely(len > CFHSI_MAX_CAIF_FRAME_SZ)) {
587
			netdev_err(cfhsi->ndev, "%s: Invalid length.\n",
588 589 590 591
				__func__);
			return -EPROTO;
		}

592
		/* Allocate SKB (OK even in IRQ context). */
593 594
		skb = alloc_skb(len + 1, GFP_ATOMIC);
		if (!skb) {
595
			netdev_err(cfhsi->ndev, "%s: Out of memory !\n",
596 597 598
				__func__);
			cfhsi->rx_state.nfrms = nfrms;
			return -ENOMEM;
599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630
		}
		caif_assert(skb != NULL);

		dst = skb_put(skb, len);
		memcpy(dst, pcffrm, len);

		skb->protocol = htons(ETH_P_CAIF);
		skb_reset_mac_header(skb);
		skb->dev = cfhsi->ndev;

		/*
		 * We're called from a platform device,
		 * and don't know the context we're running in.
		 */
		if (in_interrupt())
			netif_rx(skb);
		else
			netif_rx_ni(skb);

		/* Update network statistics. */
		cfhsi->ndev->stats.rx_packets++;
		cfhsi->ndev->stats.rx_bytes += len;

		pfrm += *plen;
		rx_sz += *plen;
		plen++;
		nfrms++;
	}

	return rx_sz;
}

631
static void cfhsi_rx_done(struct cfhsi *cfhsi)
632 633
{
	int res;
634
	int desc_pld_len = 0, rx_len, rx_state;
635
	struct cfhsi_desc *desc = NULL;
636 637
	u8 *rx_ptr, *rx_buf;
	struct cfhsi_desc *piggy_desc = NULL;
638 639 640

	desc = (struct cfhsi_desc *)cfhsi->rx_buf;

641
	netdev_dbg(cfhsi->ndev, "%s\n", __func__);
642 643 644 645 646

	if (test_bit(CFHSI_SHUTDOWN, &cfhsi->bits))
		return;

	/* Update inactivity timer if pending. */
647
	spin_lock_bh(&cfhsi->lock);
648
	mod_timer_pending(&cfhsi->inactivity_timer,
649
			jiffies + cfhsi->inactivity_timeout);
650
	spin_unlock_bh(&cfhsi->lock);
651

652
	if (cfhsi->rx_state.state == CFHSI_RX_STATE_DESC) {
653 654 655
		desc_pld_len = cfhsi_rx_desc_len(desc);

		if (desc_pld_len < 0)
656
			goto out_of_sync;
657 658 659 660 661 662 663

		rx_buf = cfhsi->rx_buf;
		rx_len = desc_pld_len;
		if (desc_pld_len > 0 && (desc->header & CFHSI_PIGGY_DESC))
			rx_len += CFHSI_DESC_SZ;
		if (desc_pld_len == 0)
			rx_buf = cfhsi->rx_flip_buf;
664
	} else {
665
		rx_buf = cfhsi->rx_flip_buf;
666

667 668 669
		rx_len = CFHSI_DESC_SZ;
		if (cfhsi->rx_state.pld_len > 0 &&
				(desc->header & CFHSI_PIGGY_DESC)) {
670 671 672

			piggy_desc = (struct cfhsi_desc *)
				(desc->emb_frm + CFHSI_MAX_EMB_FRM_SZ +
673 674
						cfhsi->rx_state.pld_len);

675
			cfhsi->rx_state.piggy_desc = true;
676

677 678 679 680 681
			/* Extract payload len from piggy-backed descriptor. */
			desc_pld_len = cfhsi_rx_desc_len(piggy_desc);
			if (desc_pld_len < 0)
				goto out_of_sync;

682
			if (desc_pld_len > 0) {
683
				rx_len = desc_pld_len;
684 685 686
				if (piggy_desc->header & CFHSI_PIGGY_DESC)
					rx_len += CFHSI_DESC_SZ;
			}
687 688 689 690 691

			/*
			 * Copy needed information from the piggy-backed
			 * descriptor to the descriptor in the start.
			 */
692
			memcpy(rx_buf, (u8 *)piggy_desc,
693
					CFHSI_DESC_SHORT_SZ);
694 695
			/* Mark no embedded frame here */
			piggy_desc->offset = 0;
696
		}
697 698
	}

699
	if (desc_pld_len) {
700 701
		rx_state = CFHSI_RX_STATE_PAYLOAD;
		rx_ptr = rx_buf + CFHSI_DESC_SZ;
702
	} else {
703 704 705
		rx_state = CFHSI_RX_STATE_DESC;
		rx_ptr = rx_buf;
		rx_len = CFHSI_DESC_SZ;
706 707
	}

708
	/* Initiate next read */
709 710
	if (test_bit(CFHSI_AWAKE, &cfhsi->bits)) {
		/* Set up new transfer. */
711
		netdev_dbg(cfhsi->ndev, "%s: Start RX.\n",
712 713 714
				__func__);

		res = cfhsi->dev->cfhsi_rx(rx_ptr, rx_len,
715 716
				cfhsi->dev);
		if (WARN_ON(res < 0)) {
717
			netdev_err(cfhsi->ndev, "%s: RX error %d.\n",
718 719 720 721 722
				__func__, res);
			cfhsi->ndev->stats.rx_errors++;
			cfhsi->ndev->stats.rx_dropped++;
		}
	}
723

724 725 726 727 728 729 730 731 732 733 734 735 736
	if (cfhsi->rx_state.state == CFHSI_RX_STATE_DESC) {
		/* Extract payload from descriptor */
		if (cfhsi_rx_desc(desc, cfhsi) < 0)
			goto out_of_sync;
	} else {
		/* Extract payload */
		if (cfhsi_rx_pld(desc, cfhsi) < 0)
			goto out_of_sync;
		if (piggy_desc) {
			/* Extract any payload in piggyback descriptor. */
			if (cfhsi_rx_desc(piggy_desc, cfhsi) < 0)
				goto out_of_sync;
		}
737
	}
738 739 740 741 742 743 744 745 746 747 748

	/* Update state info */
	memset(&cfhsi->rx_state, 0, sizeof(cfhsi->rx_state));
	cfhsi->rx_state.state = rx_state;
	cfhsi->rx_ptr = rx_ptr;
	cfhsi->rx_len = rx_len;
	cfhsi->rx_state.pld_len = desc_pld_len;
	cfhsi->rx_state.piggy_desc = desc->header & CFHSI_PIGGY_DESC;

	if (rx_buf != cfhsi->rx_buf)
		swap(cfhsi->rx_buf, cfhsi->rx_flip_buf);
749 750 751
	return;

out_of_sync:
752
	netdev_err(cfhsi->ndev, "%s: Out of sync.\n", __func__);
753 754 755
	print_hex_dump_bytes("--> ", DUMP_PREFIX_NONE,
			cfhsi->rx_buf, CFHSI_DESC_SZ);
	schedule_work(&cfhsi->out_of_sync_work);
756 757 758 759 760 761
}

static void cfhsi_rx_slowpath(unsigned long arg)
{
	struct cfhsi *cfhsi = (struct cfhsi *)arg;

762
	netdev_dbg(cfhsi->ndev, "%s.\n",
763 764 765
		__func__);

	cfhsi_rx_done(cfhsi);
766 767 768 769 770 771 772
}

static void cfhsi_rx_done_cb(struct cfhsi_drv *drv)
{
	struct cfhsi *cfhsi;

	cfhsi = container_of(drv, struct cfhsi, drv);
773
	netdev_dbg(cfhsi->ndev, "%s.\n",
774 775 776 777 778 779 780 781
		__func__);

	if (test_bit(CFHSI_SHUTDOWN, &cfhsi->bits))
		return;

	if (test_and_clear_bit(CFHSI_FLUSH_FIFO, &cfhsi->bits))
		wake_up_interruptible(&cfhsi->flush_fifo_wait);
	else
782
		cfhsi_rx_done(cfhsi);
783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800
}

static void cfhsi_wake_up(struct work_struct *work)
{
	struct cfhsi *cfhsi = NULL;
	int res;
	int len;
	long ret;

	cfhsi = container_of(work, struct cfhsi, wake_up_work);

	if (test_bit(CFHSI_SHUTDOWN, &cfhsi->bits))
		return;

	if (unlikely(test_bit(CFHSI_AWAKE, &cfhsi->bits))) {
		/* It happenes when wakeup is requested by
		 * both ends at the same time. */
		clear_bit(CFHSI_WAKE_UP, &cfhsi->bits);
801
		clear_bit(CFHSI_WAKE_UP_ACK, &cfhsi->bits);
802 803 804 805 806 807
		return;
	}

	/* Activate wake line. */
	cfhsi->dev->cfhsi_wake_up(cfhsi->dev);

808
	netdev_dbg(cfhsi->ndev, "%s: Start waiting.\n",
809 810 811
		__func__);

	/* Wait for acknowledge. */
812 813 814
	ret = CFHSI_WAKE_TOUT;
	ret = wait_event_interruptible_timeout(cfhsi->wake_up_wait,
					test_and_clear_bit(CFHSI_WAKE_UP_ACK,
815 816 817
							&cfhsi->bits), ret);
	if (unlikely(ret < 0)) {
		/* Interrupted by signal. */
818
		netdev_err(cfhsi->ndev, "%s: Signalled: %ld.\n",
819
			__func__, ret);
820

821 822 823 824
		clear_bit(CFHSI_WAKE_UP, &cfhsi->bits);
		cfhsi->dev->cfhsi_wake_down(cfhsi->dev);
		return;
	} else if (!ret) {
825 826 827
		bool ca_wake = false;
		size_t fifo_occupancy = 0;

828
		/* Wakeup timeout */
829
		netdev_dbg(cfhsi->ndev, "%s: Timeout.\n",
830
			__func__);
831 832 833 834 835

		/* Check FIFO to check if modem has sent something. */
		WARN_ON(cfhsi->dev->cfhsi_fifo_occupancy(cfhsi->dev,
					&fifo_occupancy));

836
		netdev_dbg(cfhsi->ndev, "%s: Bytes in FIFO: %u.\n",
837 838 839 840 841 842 843
				__func__, (unsigned) fifo_occupancy);

		/* Check if we misssed the interrupt. */
		WARN_ON(cfhsi->dev->cfhsi_get_peer_wake(cfhsi->dev,
							&ca_wake));

		if (ca_wake) {
844
			netdev_err(cfhsi->ndev, "%s: CA Wake missed !.\n",
845 846 847 848 849 850 851 852 853
				__func__);

			/* Clear the CFHSI_WAKE_UP_ACK bit to prevent race. */
			clear_bit(CFHSI_WAKE_UP_ACK, &cfhsi->bits);

			/* Continue execution. */
			goto wake_ack;
		}

854 855 856 857
		clear_bit(CFHSI_WAKE_UP, &cfhsi->bits);
		cfhsi->dev->cfhsi_wake_down(cfhsi->dev);
		return;
	}
858
wake_ack:
859
	netdev_dbg(cfhsi->ndev, "%s: Woken.\n",
860 861 862 863 864 865 866
		__func__);

	/* Clear power up bit. */
	set_bit(CFHSI_AWAKE, &cfhsi->bits);
	clear_bit(CFHSI_WAKE_UP, &cfhsi->bits);

	/* Resume read operation. */
867
	netdev_dbg(cfhsi->ndev, "%s: Start RX.\n", __func__);
868 869 870
	res = cfhsi->dev->cfhsi_rx(cfhsi->rx_ptr, cfhsi->rx_len, cfhsi->dev);

	if (WARN_ON(res < 0))
871
		netdev_err(cfhsi->ndev, "%s: RX err %d.\n", __func__, res);
872 873 874 875 876 877

	/* Clear power up acknowledment. */
	clear_bit(CFHSI_WAKE_UP_ACK, &cfhsi->bits);

	spin_lock_bh(&cfhsi->lock);

878 879
	/* Resume transmit if queues are not empty. */
	if (!cfhsi_tx_queue_len(cfhsi)) {
880
		netdev_dbg(cfhsi->ndev, "%s: Peer wake, start timer.\n",
881 882
			__func__);
		/* Start inactivity timer. */
883
		mod_timer(&cfhsi->inactivity_timer,
884
				jiffies + cfhsi->inactivity_timeout);
885 886 887 888
		spin_unlock_bh(&cfhsi->lock);
		return;
	}

889
	netdev_dbg(cfhsi->ndev, "%s: Host wake.\n",
890 891 892 893 894 895 896 897 898 899 900
		__func__);

	spin_unlock_bh(&cfhsi->lock);

	/* Create HSI frame. */
	len = cfhsi_tx_frm((struct cfhsi_desc *)cfhsi->tx_buf, cfhsi);

	if (likely(len > 0)) {
		/* Set up new transfer. */
		res = cfhsi->dev->cfhsi_tx(cfhsi->tx_buf, len, cfhsi->dev);
		if (WARN_ON(res < 0)) {
901
			netdev_err(cfhsi->ndev, "%s: TX error %d.\n",
902 903 904 905
				__func__, res);
			cfhsi_abort_tx(cfhsi);
		}
	} else {
906
		netdev_err(cfhsi->ndev,
907 908 909 910 911 912 913 914 915
				"%s: Failed to create HSI frame: %d.\n",
				__func__, len);
	}
}

static void cfhsi_wake_down(struct work_struct *work)
{
	long ret;
	struct cfhsi *cfhsi = NULL;
916 917
	size_t fifo_occupancy = 0;
	int retry = CFHSI_WAKE_TOUT;
918 919

	cfhsi = container_of(work, struct cfhsi, wake_down_work);
920
	netdev_dbg(cfhsi->ndev, "%s.\n", __func__);
921 922 923 924 925 926 927 928

	if (test_bit(CFHSI_SHUTDOWN, &cfhsi->bits))
		return;

	/* Deactivate wake line. */
	cfhsi->dev->cfhsi_wake_down(cfhsi->dev);

	/* Wait for acknowledge. */
929
	ret = CFHSI_WAKE_TOUT;
930
	ret = wait_event_interruptible_timeout(cfhsi->wake_down_wait,
931 932
					test_and_clear_bit(CFHSI_WAKE_DOWN_ACK,
							&cfhsi->bits), ret);
933 934
	if (ret < 0) {
		/* Interrupted by signal. */
935
		netdev_err(cfhsi->ndev, "%s: Signalled: %ld.\n",
936 937 938
			__func__, ret);
		return;
	} else if (!ret) {
939 940
		bool ca_wake = true;

941
		/* Timeout */
942
		netdev_err(cfhsi->ndev, "%s: Timeout.\n", __func__);
943 944 945 946 947

		/* Check if we misssed the interrupt. */
		WARN_ON(cfhsi->dev->cfhsi_get_peer_wake(cfhsi->dev,
							&ca_wake));
		if (!ca_wake)
948
			netdev_err(cfhsi->ndev, "%s: CA Wake missed !.\n",
949
				__func__);
950 951
	}

952 953 954 955 956 957 958 959 960 961 962 963 964 965
	/* Check FIFO occupancy. */
	while (retry) {
		WARN_ON(cfhsi->dev->cfhsi_fifo_occupancy(cfhsi->dev,
							&fifo_occupancy));

		if (!fifo_occupancy)
			break;

		set_current_state(TASK_INTERRUPTIBLE);
		schedule_timeout(1);
		retry--;
	}

	if (!retry)
966
		netdev_err(cfhsi->ndev, "%s: FIFO Timeout.\n", __func__);
967 968

	/* Clear AWAKE condition. */
969 970
	clear_bit(CFHSI_AWAKE, &cfhsi->bits);

971 972
	/* Cancel pending RX requests. */
	cfhsi->dev->cfhsi_rx_cancel(cfhsi->dev);
973 974 975

}

976 977 978 979 980 981 982 983 984 985 986
static void cfhsi_out_of_sync(struct work_struct *work)
{
	struct cfhsi *cfhsi = NULL;

	cfhsi = container_of(work, struct cfhsi, out_of_sync_work);

	rtnl_lock();
	dev_close(cfhsi->ndev);
	rtnl_unlock();
}

987 988 989 990 991
static void cfhsi_wake_up_cb(struct cfhsi_drv *drv)
{
	struct cfhsi *cfhsi = NULL;

	cfhsi = container_of(drv, struct cfhsi, drv);
992
	netdev_dbg(cfhsi->ndev, "%s.\n",
993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010
		__func__);

	set_bit(CFHSI_WAKE_UP_ACK, &cfhsi->bits);
	wake_up_interruptible(&cfhsi->wake_up_wait);

	if (test_bit(CFHSI_SHUTDOWN, &cfhsi->bits))
		return;

	/* Schedule wake up work queue if the peer initiates. */
	if (!test_and_set_bit(CFHSI_WAKE_UP, &cfhsi->bits))
		queue_work(cfhsi->wq, &cfhsi->wake_up_work);
}

static void cfhsi_wake_down_cb(struct cfhsi_drv *drv)
{
	struct cfhsi *cfhsi = NULL;

	cfhsi = container_of(drv, struct cfhsi, drv);
1011
	netdev_dbg(cfhsi->ndev, "%s.\n",
1012 1013 1014 1015 1016 1017 1018
		__func__);

	/* Initiating low power is only permitted by the host (us). */
	set_bit(CFHSI_WAKE_DOWN_ACK, &cfhsi->bits);
	wake_up_interruptible(&cfhsi->wake_down_wait);
}

1019 1020 1021 1022
static void cfhsi_aggregation_tout(unsigned long arg)
{
	struct cfhsi *cfhsi = (struct cfhsi *)arg;

1023
	netdev_dbg(cfhsi->ndev, "%s.\n",
1024 1025 1026 1027 1028
		__func__);

	cfhsi_start_tx(cfhsi);
}

1029 1030 1031 1032 1033
static int cfhsi_xmit(struct sk_buff *skb, struct net_device *dev)
{
	struct cfhsi *cfhsi = NULL;
	int start_xfer = 0;
	int timer_active;
1034
	int prio;
1035 1036 1037 1038 1039 1040

	if (!dev)
		return -EINVAL;

	cfhsi = netdev_priv(dev);

1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058
	switch (skb->priority) {
	case TC_PRIO_BESTEFFORT:
	case TC_PRIO_FILLER:
	case TC_PRIO_BULK:
		prio = CFHSI_PRIO_BEBK;
		break;
	case TC_PRIO_INTERACTIVE_BULK:
		prio = CFHSI_PRIO_VI;
		break;
	case TC_PRIO_INTERACTIVE:
		prio = CFHSI_PRIO_VO;
		break;
	case TC_PRIO_CONTROL:
	default:
		prio = CFHSI_PRIO_CTL;
		break;
	}

1059 1060
	spin_lock_bh(&cfhsi->lock);

1061 1062 1063 1064 1065
	/* Update aggregation statistics  */
	cfhsi_update_aggregation_stats(cfhsi, skb, 1);

	/* Queue the SKB */
	skb_queue_tail(&cfhsi->qhead[prio], skb);
1066 1067 1068 1069 1070 1071 1072 1073 1074 1075

	/* Sanity check; xmit should not be called after unregister_netdev */
	if (WARN_ON(test_bit(CFHSI_SHUTDOWN, &cfhsi->bits))) {
		spin_unlock_bh(&cfhsi->lock);
		cfhsi_abort_tx(cfhsi);
		return -EINVAL;
	}

	/* Send flow off if number of packets is above high water mark. */
	if (!cfhsi->flow_off_sent &&
1076
		cfhsi_tx_queue_len(cfhsi) > cfhsi->q_high_mark &&
1077 1078 1079 1080 1081 1082 1083 1084 1085 1086
		cfhsi->cfdev.flowctrl) {
		cfhsi->flow_off_sent = 1;
		cfhsi->cfdev.flowctrl(cfhsi->ndev, OFF);
	}

	if (cfhsi->tx_state == CFHSI_TX_STATE_IDLE) {
		cfhsi->tx_state = CFHSI_TX_STATE_XFER;
		start_xfer = 1;
	}

1087
	if (!start_xfer) {
1088 1089 1090 1091
		/* Send aggregate if it is possible */
		bool aggregate_ready =
			cfhsi_can_send_aggregate(cfhsi) &&
			del_timer(&cfhsi->aggregation_timer) > 0;
1092
		spin_unlock_bh(&cfhsi->lock);
1093 1094
		if (aggregate_ready)
			cfhsi_start_tx(cfhsi);
1095
		return 0;
1096
	}
1097 1098

	/* Delete inactivity timer if started. */
1099
	timer_active = del_timer_sync(&cfhsi->inactivity_timer);
1100

1101 1102
	spin_unlock_bh(&cfhsi->lock);

1103 1104 1105 1106 1107 1108 1109
	if (timer_active) {
		struct cfhsi_desc *desc = (struct cfhsi_desc *)cfhsi->tx_buf;
		int len;
		int res;

		/* Create HSI frame. */
		len = cfhsi_tx_frm(desc, cfhsi);
1110
		WARN_ON(!len);
1111 1112 1113 1114

		/* Set up new transfer. */
		res = cfhsi->dev->cfhsi_tx(cfhsi->tx_buf, len, cfhsi->dev);
		if (WARN_ON(res < 0)) {
1115
			netdev_err(cfhsi->ndev, "%s: TX error %d.\n",
1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127
				__func__, res);
			cfhsi_abort_tx(cfhsi);
		}
	} else {
		/* Schedule wake up work queue if the we initiate. */
		if (!test_and_set_bit(CFHSI_WAKE_UP, &cfhsi->bits))
			queue_work(cfhsi->wq, &cfhsi->wake_up_work);
	}

	return 0;
}

1128
static const struct net_device_ops cfhsi_ops;
1129 1130 1131

static void cfhsi_setup(struct net_device *dev)
{
1132
	int i;
1133 1134 1135 1136 1137
	struct cfhsi *cfhsi = netdev_priv(dev);
	dev->features = 0;
	dev->netdev_ops = &cfhsi_ops;
	dev->type = ARPHRD_CAIF;
	dev->flags = IFF_POINTOPOINT | IFF_NOARP;
1138
	dev->mtu = CFHSI_MAX_CAIF_FRAME_SZ;
1139 1140
	dev->tx_queue_len = 0;
	dev->destructor = free_netdev;
1141 1142
	for (i = 0; i < CFHSI_PRIO_LAST; ++i)
		skb_queue_head_init(&cfhsi->qhead[i]);
1143 1144 1145 1146 1147 1148 1149
	cfhsi->cfdev.link_select = CAIF_LINK_HIGH_BANDW;
	cfhsi->cfdev.use_frag = false;
	cfhsi->cfdev.use_stx = false;
	cfhsi->cfdev.use_fcs = false;
	cfhsi->ndev = dev;
}

1150 1151 1152 1153 1154 1155 1156
static int cfhsi_open(struct net_device *ndev)
{
	struct cfhsi *cfhsi = netdev_priv(ndev);
	int res;

	clear_bit(CFHSI_SHUTDOWN, &cfhsi->bits);

1157 1158
	/* Initialize state vaiables. */
	cfhsi->tx_state = CFHSI_TX_STATE_IDLE;
1159
	cfhsi->rx_state.state = CFHSI_RX_STATE_DESC;
1160 1161 1162 1163 1164