caif_hsi.c 30.2 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/*
 * 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.
 */

#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>
21
#include <linux/rtnetlink.h>
22
23
24
25
26
27
28
29
30
31
32
#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)))))

33
34
35
36
static int inactivity_timeout = 1000;
module_param(inactivity_timeout, int, S_IRUGO | S_IWUSR);
MODULE_PARM_DESC(inactivity_timeout, "Inactivity timeout on HSI, ms.");

37
38
39
40
41
42
43
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/*
 * 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 spinlock_t cfhsi_list_lock;

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

	dev_dbg(&cfhsi->ndev->dev, "%s.\n",
		__func__);

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

static void cfhsi_abort_tx(struct cfhsi *cfhsi)
{
	struct sk_buff *skb;

	for (;;) {
		spin_lock_bh(&cfhsi->lock);
		skb = skb_dequeue(&cfhsi->qhead);
		if (!skb)
			break;

		cfhsi->ndev->stats.tx_errors++;
		cfhsi->ndev->stats.tx_dropped++;
		spin_unlock_bh(&cfhsi->lock);
		kfree_skb(skb);
	}
	cfhsi->tx_state = CFHSI_TX_STATE_IDLE;
	if (!test_bit(CFHSI_SHUTDOWN, &cfhsi->bits))
106
107
		mod_timer(&cfhsi->timer,
			jiffies + cfhsi->inactivity_timeout);
108
109
110
111
112
113
114
115
116
117
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
	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;

	dev_dbg(&cfhsi->ndev->dev, "%s.\n",
		__func__);


	ret = cfhsi->dev->cfhsi_wake_up(cfhsi->dev);
	if (ret) {
		dev_warn(&cfhsi->ndev->dev,
			"%s: can't wake up HSI interface: %d.\n",
			__func__, ret);
		return ret;
	}

	do {
		ret = cfhsi->dev->cfhsi_fifo_occupancy(cfhsi->dev,
				&fifo_occupancy);
		if (ret) {
			dev_warn(&cfhsi->ndev->dev,
				"%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);
			dev_warn(&cfhsi->ndev->dev,
				"%s: can't read data: %d.\n",
				__func__, ret);
			break;
		}

		ret = 5 * HZ;
154
		ret = wait_event_interruptible_timeout(cfhsi->flush_fifo_wait,
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
			 !test_bit(CFHSI_FLUSH_FIFO, &cfhsi->bits), ret);

		if (ret < 0) {
			dev_warn(&cfhsi->ndev->dev,
				"%s: can't wait for flush complete: %d.\n",
				__func__, ret);
			break;
		} else if (!ret) {
			ret = -ETIMEDOUT;
			dev_warn(&cfhsi->ndev->dev,
				"%s: timeout waiting for flush complete.\n",
				__func__);
			break;
		}
	} while (1);

	cfhsi->dev->cfhsi_wake_down(cfhsi->dev);

	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;

	skb = skb_dequeue(&cfhsi->qhead);
	if (!skb)
		return 0;

187
188
189
	/* Clear offset. */
	desc->offset = 0;

190
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
	/* Check if we can embed a CAIF frame. */
	if (skb->len < CFHSI_MAX_EMB_FRM_SZ) {
		struct caif_payload_info *info;
		int hpad = 0;
		int tpad = 0;

		/* 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. */
			cfhsi->ndev->stats.tx_packets++;
			cfhsi->ndev->stats.tx_bytes += skb->len;

			/* Copy in embedded CAIF frame. */
			skb_copy_bits(skb, 0, pemb, skb->len);
			consume_skb(skb);
			skb = NULL;
		}
218
	}
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
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
279
280

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

		if (!skb)
			skb = skb_dequeue(&cfhsi->qhead);

		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. */
		cfhsi->ndev->stats.tx_packets++;
		cfhsi->ndev->stats.tx_bytes += skb->len;

		/* 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;
		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. */
	skb = skb_peek(&cfhsi->qhead);
	if (skb)
		desc->header |= CFHSI_PIGGY_DESC;
	else
		desc->header &= ~CFHSI_PIGGY_DESC;

	return CFHSI_DESC_SZ + pld_len;
}

281
static void cfhsi_tx_done(struct cfhsi *cfhsi)
282
283
284
285
286
{
	struct cfhsi_desc *desc = NULL;
	int len = 0;
	int res;

287
	dev_dbg(&cfhsi->ndev->dev, "%s.\n", __func__);
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309

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

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

	do {
		/*
		 * 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->qhead.qlen <= cfhsi->q_low_mark &&
				cfhsi->cfdev.flowctrl) {

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

		/* Create HSI frame. */
310
311
312
313
314
315
316
317
318
319
320
		do {
			len = cfhsi_tx_frm(desc, cfhsi);
			if (!len) {
				spin_lock_bh(&cfhsi->lock);
				if (unlikely(skb_peek(&cfhsi->qhead))) {
					spin_unlock_bh(&cfhsi->lock);
					continue;
				}
				cfhsi->tx_state = CFHSI_TX_STATE_IDLE;
				/* Start inactivity timer. */
				mod_timer(&cfhsi->timer,
321
					jiffies + cfhsi->inactivity_timeout);
322
323
324
325
				spin_unlock_bh(&cfhsi->lock);
				goto done;
			}
		} while (!len);
326
327
328
329
330
331
332
333

		/* Set up new transfer. */
		res = cfhsi->dev->cfhsi_tx(cfhsi->tx_buf, len, cfhsi->dev);
		if (WARN_ON(res < 0)) {
			dev_err(&cfhsi->ndev->dev, "%s: TX error %d.\n",
				__func__, res);
		}
	} while (res < 0);
334
335
336

done:
	return;
337
338
339
340
341
342
343
344
345
346
347
348
}

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

	cfhsi = container_of(drv, struct cfhsi, drv);
	dev_dbg(&cfhsi->ndev->dev, "%s.\n",
		__func__);

	if (test_bit(CFHSI_SHUTDOWN, &cfhsi->bits))
		return;
349
	cfhsi_tx_done(cfhsi);
350
351
}

352
static int cfhsi_rx_desc(struct cfhsi_desc *desc, struct cfhsi *cfhsi)
353
354
355
356
357
358
359
360
361
362
{
	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)) {
		dev_err(&cfhsi->ndev->dev, "%s: Invalid descriptor.\n",
			__func__);
363
		return -EPROTO;
364
365
366
367
368
369
	}

	/* Check for embedded CAIF frame. */
	if (desc->offset) {
		struct sk_buff *skb;
		u8 *dst = NULL;
370
		int len = 0;
371
372
373
374
375
376
377
378
379
380
		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. */

381
382
383
384
385
386
		/* Sanity check length of CAIF frame. */
		if (unlikely(len > CFHSI_MAX_CAIF_FRAME_SZ)) {
			dev_err(&cfhsi->ndev->dev, "%s: Invalid length.\n",
				__func__);
			return -EPROTO;
		}
387
388

		/* Allocate SKB (OK even in IRQ context). */
389
390
391
392
393
		skb = alloc_skb(len + 1, GFP_ATOMIC);
		if (!skb) {
			dev_err(&cfhsi->ndev->dev, "%s: Out of memory !\n",
				__func__);
			return -ENOMEM;
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
		}
		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;

431
	if ((xfer_sz % 4) || (xfer_sz > (CFHSI_BUF_SZ_RX - CFHSI_DESC_SZ))) {
432
433
434
		dev_err(&cfhsi->ndev->dev,
				"%s: Invalid payload len: %d, ignored.\n",
			__func__, xfer_sz);
435
		return -EPROTO;
436
437
438
439
	}
	return xfer_sz;
}

440
static int cfhsi_rx_pld(struct cfhsi_desc *desc, struct cfhsi *cfhsi)
441
442
443
444
445
446
447
448
449
450
451
{
	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))) {
		dev_err(&cfhsi->ndev->dev, "%s: Invalid descriptor.\n",
			__func__);
452
		return -EPROTO;
453
454
455
456
457
	}

	/* Set frame pointer to start of payload. */
	pfrm = desc->emb_frm + CFHSI_MAX_EMB_FRM_SZ;
	plen = desc->cffrm_len;
458
459
460
461
462
463
464
465
466
467

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

	/* Parse payload. */
468
469
470
471
	while (nfrms < CFHSI_MAX_PKTS && *plen) {
		struct sk_buff *skb;
		u8 *dst = NULL;
		u8 *pcffrm = NULL;
472
		int len = 0;
473
474
475
476
477
478
479
480
481

		/* 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. */

482
483
484
485
486
487
488
		/* Sanity check length of CAIF frames. */
		if (unlikely(len > CFHSI_MAX_CAIF_FRAME_SZ)) {
			dev_err(&cfhsi->ndev->dev, "%s: Invalid length.\n",
				__func__);
			return -EPROTO;
		}

489
		/* Allocate SKB (OK even in IRQ context). */
490
491
492
493
494
495
		skb = alloc_skb(len + 1, GFP_ATOMIC);
		if (!skb) {
			dev_err(&cfhsi->ndev->dev, "%s: Out of memory !\n",
				__func__);
			cfhsi->rx_state.nfrms = nfrms;
			return -ENOMEM;
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
		}
		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;
}

528
static void cfhsi_rx_done(struct cfhsi *cfhsi)
529
530
531
532
533
534
535
{
	int res;
	int desc_pld_len = 0;
	struct cfhsi_desc *desc = NULL;

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

536
	dev_dbg(&cfhsi->ndev->dev, "%s\n", __func__);
537
538
539
540
541

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

	/* Update inactivity timer if pending. */
542
	spin_lock_bh(&cfhsi->lock);
543
544
	mod_timer_pending(&cfhsi->timer,
			jiffies + cfhsi->inactivity_timeout);
545
	spin_unlock_bh(&cfhsi->lock);
546

547
	if (cfhsi->rx_state.state == CFHSI_RX_STATE_DESC) {
548
		desc_pld_len = cfhsi_rx_desc(desc, cfhsi);
549
550
		if (desc_pld_len == -ENOMEM)
			goto restart;
551
552
		if (desc_pld_len == -EPROTO)
			goto out_of_sync;
553
554
555
	} else {
		int pld_len;

556
		if (!cfhsi->rx_state.piggy_desc) {
557
			pld_len = cfhsi_rx_pld(desc, cfhsi);
558
559
			if (pld_len == -ENOMEM)
				goto restart;
560
561
			if (pld_len == -EPROTO)
				goto out_of_sync;
562
563
564
565
			cfhsi->rx_state.pld_len = pld_len;
		} else {
			pld_len = cfhsi->rx_state.pld_len;
		}
566
567
568
569
570
571

		if ((pld_len > 0) && (desc->header & CFHSI_PIGGY_DESC)) {
			struct cfhsi_desc *piggy_desc;
			piggy_desc = (struct cfhsi_desc *)
				(desc->emb_frm + CFHSI_MAX_EMB_FRM_SZ +
						pld_len);
572
			cfhsi->rx_state.piggy_desc = true;
573
574

			/* Extract piggy-backed descriptor. */
575
			desc_pld_len = cfhsi_rx_desc(piggy_desc, cfhsi);
576
577
			if (desc_pld_len == -ENOMEM)
				goto restart;
578
579
580
581
582
583
584
585

			/*
			 * Copy needed information from the piggy-backed
			 * descriptor to the descriptor in the start.
			 */
			memcpy((u8 *)desc, (u8 *)piggy_desc,
					CFHSI_DESC_SHORT_SZ);

586
587
588
			if (desc_pld_len == -EPROTO)
				goto out_of_sync;
		}
589
590
591
	}

	memset(&cfhsi->rx_state, 0, sizeof(cfhsi->rx_state));
592
	if (desc_pld_len) {
593
		cfhsi->rx_state.state = CFHSI_RX_STATE_PAYLOAD;
594
595
596
		cfhsi->rx_ptr = cfhsi->rx_buf + CFHSI_DESC_SZ;
		cfhsi->rx_len = desc_pld_len;
	} else {
597
		cfhsi->rx_state.state = CFHSI_RX_STATE_DESC;
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
		cfhsi->rx_ptr = cfhsi->rx_buf;
		cfhsi->rx_len = CFHSI_DESC_SZ;
	}

	if (test_bit(CFHSI_AWAKE, &cfhsi->bits)) {
		/* Set up new transfer. */
		dev_dbg(&cfhsi->ndev->dev, "%s: Start RX.\n",
			__func__);
		res = cfhsi->dev->cfhsi_rx(cfhsi->rx_ptr, cfhsi->rx_len,
				cfhsi->dev);
		if (WARN_ON(res < 0)) {
			dev_err(&cfhsi->ndev->dev, "%s: RX error %d.\n",
				__func__, res);
			cfhsi->ndev->stats.rx_errors++;
			cfhsi->ndev->stats.rx_dropped++;
		}
	}
615
616
617
618
619
620
621
622
623
624
	return;

restart:
	if (++cfhsi->rx_state.retries > CFHSI_MAX_RX_RETRIES) {
		dev_err(&cfhsi->ndev->dev, "%s: No memory available "
			"in %d iterations.\n",
			__func__, CFHSI_MAX_RX_RETRIES);
		BUG();
	}
	mod_timer(&cfhsi->rx_slowpath_timer, jiffies + 1);
625
626
627
628
629
630
631
	return;

out_of_sync:
	dev_err(&cfhsi->ndev->dev, "%s: Out of sync.\n", __func__);
	print_hex_dump_bytes("--> ", DUMP_PREFIX_NONE,
			cfhsi->rx_buf, CFHSI_DESC_SZ);
	schedule_work(&cfhsi->out_of_sync_work);
632
633
634
635
636
637
638
639
640
641
}

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

	dev_dbg(&cfhsi->ndev->dev, "%s.\n",
		__func__);

	cfhsi_rx_done(cfhsi);
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
}

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

	cfhsi = container_of(drv, struct cfhsi, drv);
	dev_dbg(&cfhsi->ndev->dev, "%s.\n",
		__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
658
		cfhsi_rx_done(cfhsi);
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
}

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);
677
		clear_bit(CFHSI_WAKE_UP_ACK, &cfhsi->bits);
678
679
680
681
682
683
684
685
686
687
		return;
	}

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

	dev_dbg(&cfhsi->ndev->dev, "%s: Start waiting.\n",
		__func__);

	/* Wait for acknowledge. */
688
689
690
	ret = CFHSI_WAKE_TOUT;
	ret = wait_event_interruptible_timeout(cfhsi->wake_up_wait,
					test_and_clear_bit(CFHSI_WAKE_UP_ACK,
691
692
693
							&cfhsi->bits), ret);
	if (unlikely(ret < 0)) {
		/* Interrupted by signal. */
694
		dev_err(&cfhsi->ndev->dev, "%s: Signalled: %ld.\n",
695
			__func__, ret);
696

697
698
699
700
		clear_bit(CFHSI_WAKE_UP, &cfhsi->bits);
		cfhsi->dev->cfhsi_wake_down(cfhsi->dev);
		return;
	} else if (!ret) {
701
702
703
		bool ca_wake = false;
		size_t fifo_occupancy = 0;

704
705
706
		/* Wakeup timeout */
		dev_err(&cfhsi->ndev->dev, "%s: Timeout.\n",
			__func__);
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729

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

		dev_err(&cfhsi->ndev->dev, "%s: Bytes in FIFO: %u.\n",
				__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) {
			dev_err(&cfhsi->ndev->dev, "%s: CA Wake missed !.\n",
				__func__);

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

			/* Continue execution. */
			goto wake_ack;
		}

730
731
732
733
		clear_bit(CFHSI_WAKE_UP, &cfhsi->bits);
		cfhsi->dev->cfhsi_wake_down(cfhsi->dev);
		return;
	}
734
wake_ack:
735
736
737
738
739
740
741
742
	dev_dbg(&cfhsi->ndev->dev, "%s: Woken.\n",
		__func__);

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

	/* Resume read operation. */
743
744
745
746
747
	dev_dbg(&cfhsi->ndev->dev, "%s: Start RX.\n", __func__);
	res = cfhsi->dev->cfhsi_rx(cfhsi->rx_ptr, cfhsi->rx_len, cfhsi->dev);

	if (WARN_ON(res < 0))
		dev_err(&cfhsi->ndev->dev, "%s: RX err %d.\n", __func__, res);
748
749
750
751
752
753
754
755
756
757
758
759

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

	spin_lock_bh(&cfhsi->lock);

	/* Resume transmit if queue is not empty. */
	if (!skb_peek(&cfhsi->qhead)) {
		dev_dbg(&cfhsi->ndev->dev, "%s: Peer wake, start timer.\n",
			__func__);
		/* Start inactivity timer. */
		mod_timer(&cfhsi->timer,
760
				jiffies + cfhsi->inactivity_timeout);
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
		spin_unlock_bh(&cfhsi->lock);
		return;
	}

	dev_dbg(&cfhsi->ndev->dev, "%s: Host wake.\n",
		__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)) {
			dev_err(&cfhsi->ndev->dev, "%s: TX error %d.\n",
				__func__, res);
			cfhsi_abort_tx(cfhsi);
		}
	} else {
		dev_err(&cfhsi->ndev->dev,
				"%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;
792
793
	size_t fifo_occupancy = 0;
	int retry = CFHSI_WAKE_TOUT;
794
795

	cfhsi = container_of(work, struct cfhsi, wake_down_work);
796
	dev_dbg(&cfhsi->ndev->dev, "%s.\n", __func__);
797
798
799
800
801
802
803
804

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

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

	/* Wait for acknowledge. */
805
	ret = CFHSI_WAKE_TOUT;
806
	ret = wait_event_interruptible_timeout(cfhsi->wake_down_wait,
807
808
					test_and_clear_bit(CFHSI_WAKE_DOWN_ACK,
							&cfhsi->bits), ret);
809
810
	if (ret < 0) {
		/* Interrupted by signal. */
811
		dev_err(&cfhsi->ndev->dev, "%s: Signalled: %ld.\n",
812
813
814
			__func__, ret);
		return;
	} else if (!ret) {
815
816
		bool ca_wake = true;

817
		/* Timeout */
818
		dev_err(&cfhsi->ndev->dev, "%s: Timeout.\n", __func__);
819
820
821
822
823
824
825

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

828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
	/* 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)
		dev_err(&cfhsi->ndev->dev, "%s: FIFO Timeout.\n", __func__);

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

847
848
	/* Cancel pending RX requests. */
	cfhsi->dev->cfhsi_rx_cancel(cfhsi->dev);
849
850
851

}

852
853
854
855
856
857
858
859
860
861
862
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();
}

863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
static void cfhsi_wake_up_cb(struct cfhsi_drv *drv)
{
	struct cfhsi *cfhsi = NULL;

	cfhsi = container_of(drv, struct cfhsi, drv);
	dev_dbg(&cfhsi->ndev->dev, "%s.\n",
		__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);
	dev_dbg(&cfhsi->ndev->dev, "%s.\n",
		__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);
}

static int cfhsi_xmit(struct sk_buff *skb, struct net_device *dev)
{
	struct cfhsi *cfhsi = NULL;
	int start_xfer = 0;
	int timer_active;

	if (!dev)
		return -EINVAL;

	cfhsi = netdev_priv(dev);

	spin_lock_bh(&cfhsi->lock);

	skb_queue_tail(&cfhsi->qhead, skb);

	/* 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 &&
		cfhsi->qhead.qlen > cfhsi->q_high_mark &&
		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;
	}

930
931
	if (!start_xfer) {
		spin_unlock_bh(&cfhsi->lock);
932
		return 0;
933
	}
934
935
936
937

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

938
939
	spin_unlock_bh(&cfhsi->lock);

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
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
	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);
		BUG_ON(!len);

		/* Set up new transfer. */
		res = cfhsi->dev->cfhsi_tx(cfhsi->tx_buf, len, cfhsi->dev);
		if (WARN_ON(res < 0)) {
			dev_err(&cfhsi->ndev->dev, "%s: TX error %d.\n",
				__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;
}

static int cfhsi_open(struct net_device *dev)
{
	netif_wake_queue(dev);

	return 0;
}

static int cfhsi_close(struct net_device *dev)
{
	netif_stop_queue(dev);

	return 0;
}

static const struct net_device_ops cfhsi_ops = {
	.ndo_open = cfhsi_open,
	.ndo_stop = cfhsi_close,
	.ndo_start_xmit = cfhsi_xmit
};

static void cfhsi_setup(struct net_device *dev)
{
	struct cfhsi *cfhsi = netdev_priv(dev);
	dev->features = 0;
	dev->netdev_ops = &cfhsi_ops;
	dev->type = ARPHRD_CAIF;
	dev->flags = IFF_POINTOPOINT | IFF_NOARP;
	dev->mtu = CFHSI_MAX_PAYLOAD_SZ;
	dev->tx_queue_len = 0;
	dev->destructor = free_netdev;
	skb_queue_head_init(&cfhsi->qhead);
	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;