blob: 82c4d6ca2d3f919b93ca1e2bbe967074467b25c2 [file] [log] [blame]
Dmitry.Tarnyagin40d69042011-06-01 03:29:18 +00001/*
2 * Copyright (C) ST-Ericsson AB 2010
3 * Contact: Sjur Brendeland / sjur.brandeland@stericsson.com
4 * Author: Daniel Martensson / daniel.martensson@stericsson.com
5 * Dmitry.Tarnyagin / dmitry.tarnyagin@stericsson.com
6 * License terms: GNU General Public License (GPL) version 2.
7 */
8
Dmitry.Tarnyagin40d69042011-06-01 03:29:18 +00009#include <linux/init.h>
10#include <linux/module.h>
11#include <linux/device.h>
12#include <linux/platform_device.h>
13#include <linux/netdevice.h>
14#include <linux/string.h>
15#include <linux/list.h>
16#include <linux/interrupt.h>
17#include <linux/delay.h>
18#include <linux/sched.h>
19#include <linux/if_arp.h>
20#include <linux/timer.h>
21#include <net/caif/caif_layer.h>
22#include <net/caif/caif_hsi.h>
23
24MODULE_LICENSE("GPL");
25MODULE_AUTHOR("Daniel Martensson<daniel.martensson@stericsson.com>");
26MODULE_DESCRIPTION("CAIF HSI driver");
27
28/* Returns the number of padding bytes for alignment. */
29#define PAD_POW2(x, pow) ((((x)&((pow)-1)) == 0) ? 0 :\
30 (((pow)-((x)&((pow)-1)))))
31
32/*
33 * HSI padding options.
34 * Warning: must be a base of 2 (& operation used) and can not be zero !
35 */
36static int hsi_head_align = 4;
37module_param(hsi_head_align, int, S_IRUGO);
38MODULE_PARM_DESC(hsi_head_align, "HSI head alignment.");
39
40static int hsi_tail_align = 4;
41module_param(hsi_tail_align, int, S_IRUGO);
42MODULE_PARM_DESC(hsi_tail_align, "HSI tail alignment.");
43
44/*
45 * HSI link layer flowcontrol thresholds.
46 * Warning: A high threshold value migth increase throughput but it will at
47 * the same time prevent channel prioritization and increase the risk of
48 * flooding the modem. The high threshold should be above the low.
49 */
50static int hsi_high_threshold = 100;
51module_param(hsi_high_threshold, int, S_IRUGO);
52MODULE_PARM_DESC(hsi_high_threshold, "HSI high threshold (FLOW OFF).");
53
54static int hsi_low_threshold = 50;
55module_param(hsi_low_threshold, int, S_IRUGO);
56MODULE_PARM_DESC(hsi_low_threshold, "HSI high threshold (FLOW ON).");
57
58#define ON 1
59#define OFF 0
60
61/*
62 * Threshold values for the HSI packet queue. Flowcontrol will be asserted
63 * when the number of packets exceeds HIGH_WATER_MARK. It will not be
64 * de-asserted before the number of packets drops below LOW_WATER_MARK.
65 */
66#define LOW_WATER_MARK hsi_low_threshold
67#define HIGH_WATER_MARK hsi_high_threshold
68
69static LIST_HEAD(cfhsi_list);
70static spinlock_t cfhsi_list_lock;
71
72static void cfhsi_inactivity_tout(unsigned long arg)
73{
74 struct cfhsi *cfhsi = (struct cfhsi *)arg;
75
76 dev_dbg(&cfhsi->ndev->dev, "%s.\n",
77 __func__);
78
79 /* Schedule power down work queue. */
80 if (!test_bit(CFHSI_SHUTDOWN, &cfhsi->bits))
81 queue_work(cfhsi->wq, &cfhsi->wake_down_work);
82}
83
84static void cfhsi_abort_tx(struct cfhsi *cfhsi)
85{
86 struct sk_buff *skb;
87
88 for (;;) {
89 spin_lock_bh(&cfhsi->lock);
90 skb = skb_dequeue(&cfhsi->qhead);
91 if (!skb)
92 break;
93
94 cfhsi->ndev->stats.tx_errors++;
95 cfhsi->ndev->stats.tx_dropped++;
96 spin_unlock_bh(&cfhsi->lock);
97 kfree_skb(skb);
98 }
99 cfhsi->tx_state = CFHSI_TX_STATE_IDLE;
100 if (!test_bit(CFHSI_SHUTDOWN, &cfhsi->bits))
101 mod_timer(&cfhsi->timer, jiffies + CFHSI_INACTIVITY_TOUT);
102 spin_unlock_bh(&cfhsi->lock);
103}
104
105static int cfhsi_flush_fifo(struct cfhsi *cfhsi)
106{
107 char buffer[32]; /* Any reasonable value */
108 size_t fifo_occupancy;
109 int ret;
110
111 dev_dbg(&cfhsi->ndev->dev, "%s.\n",
112 __func__);
113
114
115 ret = cfhsi->dev->cfhsi_wake_up(cfhsi->dev);
116 if (ret) {
117 dev_warn(&cfhsi->ndev->dev,
118 "%s: can't wake up HSI interface: %d.\n",
119 __func__, ret);
120 return ret;
121 }
122
123 do {
124 ret = cfhsi->dev->cfhsi_fifo_occupancy(cfhsi->dev,
125 &fifo_occupancy);
126 if (ret) {
127 dev_warn(&cfhsi->ndev->dev,
128 "%s: can't get FIFO occupancy: %d.\n",
129 __func__, ret);
130 break;
131 } else if (!fifo_occupancy)
132 /* No more data, exitting normally */
133 break;
134
135 fifo_occupancy = min(sizeof(buffer), fifo_occupancy);
136 set_bit(CFHSI_FLUSH_FIFO, &cfhsi->bits);
137 ret = cfhsi->dev->cfhsi_rx(buffer, fifo_occupancy,
138 cfhsi->dev);
139 if (ret) {
140 clear_bit(CFHSI_FLUSH_FIFO, &cfhsi->bits);
141 dev_warn(&cfhsi->ndev->dev,
142 "%s: can't read data: %d.\n",
143 __func__, ret);
144 break;
145 }
146
147 ret = 5 * HZ;
148 wait_event_interruptible_timeout(cfhsi->flush_fifo_wait,
149 !test_bit(CFHSI_FLUSH_FIFO, &cfhsi->bits), ret);
150
151 if (ret < 0) {
152 dev_warn(&cfhsi->ndev->dev,
153 "%s: can't wait for flush complete: %d.\n",
154 __func__, ret);
155 break;
156 } else if (!ret) {
157 ret = -ETIMEDOUT;
158 dev_warn(&cfhsi->ndev->dev,
159 "%s: timeout waiting for flush complete.\n",
160 __func__);
161 break;
162 }
163 } while (1);
164
165 cfhsi->dev->cfhsi_wake_down(cfhsi->dev);
166
167 return ret;
168}
169
170static int cfhsi_tx_frm(struct cfhsi_desc *desc, struct cfhsi *cfhsi)
171{
172 int nfrms = 0;
173 int pld_len = 0;
174 struct sk_buff *skb;
175 u8 *pfrm = desc->emb_frm + CFHSI_MAX_EMB_FRM_SZ;
176
177 skb = skb_dequeue(&cfhsi->qhead);
178 if (!skb)
179 return 0;
180
sjur.brandeland@stericsson.com94230fe2011-10-13 11:29:22 +0000181 /* Clear offset. */
182 desc->offset = 0;
183
Dmitry.Tarnyagin40d69042011-06-01 03:29:18 +0000184 /* Check if we can embed a CAIF frame. */
185 if (skb->len < CFHSI_MAX_EMB_FRM_SZ) {
186 struct caif_payload_info *info;
187 int hpad = 0;
188 int tpad = 0;
189
190 /* Calculate needed head alignment and tail alignment. */
191 info = (struct caif_payload_info *)&skb->cb;
192
193 hpad = 1 + PAD_POW2((info->hdr_len + 1), hsi_head_align);
194 tpad = PAD_POW2((skb->len + hpad), hsi_tail_align);
195
196 /* Check if frame still fits with added alignment. */
197 if ((skb->len + hpad + tpad) <= CFHSI_MAX_EMB_FRM_SZ) {
198 u8 *pemb = desc->emb_frm;
199 desc->offset = CFHSI_DESC_SHORT_SZ;
200 *pemb = (u8)(hpad - 1);
201 pemb += hpad;
202
203 /* Update network statistics. */
204 cfhsi->ndev->stats.tx_packets++;
205 cfhsi->ndev->stats.tx_bytes += skb->len;
206
207 /* Copy in embedded CAIF frame. */
208 skb_copy_bits(skb, 0, pemb, skb->len);
209 consume_skb(skb);
210 skb = NULL;
211 }
sjur.brandeland@stericsson.com94230fe2011-10-13 11:29:22 +0000212 }
Dmitry.Tarnyagin40d69042011-06-01 03:29:18 +0000213
214 /* Create payload CAIF frames. */
215 pfrm = desc->emb_frm + CFHSI_MAX_EMB_FRM_SZ;
216 while (nfrms < CFHSI_MAX_PKTS) {
217 struct caif_payload_info *info;
218 int hpad = 0;
219 int tpad = 0;
220
221 if (!skb)
222 skb = skb_dequeue(&cfhsi->qhead);
223
224 if (!skb)
225 break;
226
227 /* Calculate needed head alignment and tail alignment. */
228 info = (struct caif_payload_info *)&skb->cb;
229
230 hpad = 1 + PAD_POW2((info->hdr_len + 1), hsi_head_align);
231 tpad = PAD_POW2((skb->len + hpad), hsi_tail_align);
232
233 /* Fill in CAIF frame length in descriptor. */
234 desc->cffrm_len[nfrms] = hpad + skb->len + tpad;
235
236 /* Fill head padding information. */
237 *pfrm = (u8)(hpad - 1);
238 pfrm += hpad;
239
240 /* Update network statistics. */
241 cfhsi->ndev->stats.tx_packets++;
242 cfhsi->ndev->stats.tx_bytes += skb->len;
243
244 /* Copy in CAIF frame. */
245 skb_copy_bits(skb, 0, pfrm, skb->len);
246
247 /* Update payload length. */
248 pld_len += desc->cffrm_len[nfrms];
249
250 /* Update frame pointer. */
251 pfrm += skb->len + tpad;
252 consume_skb(skb);
253 skb = NULL;
254
255 /* Update number of frames. */
256 nfrms++;
257 }
258
259 /* Unused length fields should be zero-filled (according to SPEC). */
260 while (nfrms < CFHSI_MAX_PKTS) {
261 desc->cffrm_len[nfrms] = 0x0000;
262 nfrms++;
263 }
264
265 /* Check if we can piggy-back another descriptor. */
266 skb = skb_peek(&cfhsi->qhead);
267 if (skb)
268 desc->header |= CFHSI_PIGGY_DESC;
269 else
270 desc->header &= ~CFHSI_PIGGY_DESC;
271
272 return CFHSI_DESC_SZ + pld_len;
273}
274
275static void cfhsi_tx_done_work(struct work_struct *work)
276{
277 struct cfhsi *cfhsi = NULL;
278 struct cfhsi_desc *desc = NULL;
279 int len = 0;
280 int res;
281
282 cfhsi = container_of(work, struct cfhsi, tx_done_work);
283 dev_dbg(&cfhsi->ndev->dev, "%s.\n",
284 __func__);
285
286 if (test_bit(CFHSI_SHUTDOWN, &cfhsi->bits))
287 return;
288
289 desc = (struct cfhsi_desc *)cfhsi->tx_buf;
290
291 do {
292 /*
293 * Send flow on if flow off has been previously signalled
294 * and number of packets is below low water mark.
295 */
296 spin_lock_bh(&cfhsi->lock);
297 if (cfhsi->flow_off_sent &&
298 cfhsi->qhead.qlen <= cfhsi->q_low_mark &&
299 cfhsi->cfdev.flowctrl) {
300
301 cfhsi->flow_off_sent = 0;
302 cfhsi->cfdev.flowctrl(cfhsi->ndev, ON);
303 }
304 spin_unlock_bh(&cfhsi->lock);
305
306 /* Create HSI frame. */
Dmitry Tarnyaginfe47f122011-10-13 11:29:23 +0000307 do {
308 len = cfhsi_tx_frm(desc, cfhsi);
309 if (!len) {
310 spin_lock_bh(&cfhsi->lock);
311 if (unlikely(skb_peek(&cfhsi->qhead))) {
312 spin_unlock_bh(&cfhsi->lock);
313 continue;
314 }
315 cfhsi->tx_state = CFHSI_TX_STATE_IDLE;
316 /* Start inactivity timer. */
317 mod_timer(&cfhsi->timer,
Dmitry.Tarnyagin40d69042011-06-01 03:29:18 +0000318 jiffies + CFHSI_INACTIVITY_TOUT);
Dmitry Tarnyaginfe47f122011-10-13 11:29:23 +0000319 spin_unlock_bh(&cfhsi->lock);
320 goto done;
321 }
322 } while (!len);
Dmitry.Tarnyagin40d69042011-06-01 03:29:18 +0000323
324 /* Set up new transfer. */
325 res = cfhsi->dev->cfhsi_tx(cfhsi->tx_buf, len, cfhsi->dev);
326 if (WARN_ON(res < 0)) {
327 dev_err(&cfhsi->ndev->dev, "%s: TX error %d.\n",
328 __func__, res);
329 }
330 } while (res < 0);
Dmitry Tarnyaginfe47f122011-10-13 11:29:23 +0000331
332done:
333 return;
Dmitry.Tarnyagin40d69042011-06-01 03:29:18 +0000334}
335
336static void cfhsi_tx_done_cb(struct cfhsi_drv *drv)
337{
338 struct cfhsi *cfhsi;
339
340 cfhsi = container_of(drv, struct cfhsi, drv);
341 dev_dbg(&cfhsi->ndev->dev, "%s.\n",
342 __func__);
343
344 if (test_bit(CFHSI_SHUTDOWN, &cfhsi->bits))
345 return;
346
347 queue_work(cfhsi->wq, &cfhsi->tx_done_work);
348}
349
350static int cfhsi_rx_desc(struct cfhsi_desc *desc, struct cfhsi *cfhsi)
351{
352 int xfer_sz = 0;
353 int nfrms = 0;
354 u16 *plen = NULL;
355 u8 *pfrm = NULL;
356
357 if ((desc->header & ~CFHSI_PIGGY_DESC) ||
358 (desc->offset > CFHSI_MAX_EMB_FRM_SZ)) {
359 dev_err(&cfhsi->ndev->dev, "%s: Invalid descriptor.\n",
360 __func__);
361 return 0;
362 }
363
364 /* Check for embedded CAIF frame. */
365 if (desc->offset) {
366 struct sk_buff *skb;
367 u8 *dst = NULL;
368 int len = 0, retries = 0;
369 pfrm = ((u8 *)desc) + desc->offset;
370
371 /* Remove offset padding. */
372 pfrm += *pfrm + 1;
373
374 /* Read length of CAIF frame (little endian). */
375 len = *pfrm;
376 len |= ((*(pfrm+1)) << 8) & 0xFF00;
377 len += 2; /* Add FCS fields. */
378
379
380 /* Allocate SKB (OK even in IRQ context). */
381 skb = alloc_skb(len + 1, GFP_KERNEL);
382 while (!skb) {
383 retries++;
384 schedule_timeout(1);
385 skb = alloc_skb(len + 1, GFP_KERNEL);
386 if (skb) {
387 printk(KERN_WARNING "%s: slept for %u "
388 "before getting memory\n",
389 __func__, retries);
390 break;
391 }
392 if (retries > HZ) {
393 printk(KERN_ERR "%s: slept for 1HZ and "
394 "did not get memory\n",
395 __func__);
396 cfhsi->ndev->stats.rx_dropped++;
397 goto drop_frame;
398 }
399 }
400 caif_assert(skb != NULL);
401
402 dst = skb_put(skb, len);
403 memcpy(dst, pfrm, len);
404
405 skb->protocol = htons(ETH_P_CAIF);
406 skb_reset_mac_header(skb);
407 skb->dev = cfhsi->ndev;
408
409 /*
410 * We are called from a arch specific platform device.
411 * Unfortunately we don't know what context we're
412 * running in.
413 */
414 if (in_interrupt())
415 netif_rx(skb);
416 else
417 netif_rx_ni(skb);
418
419 /* Update network statistics. */
420 cfhsi->ndev->stats.rx_packets++;
421 cfhsi->ndev->stats.rx_bytes += len;
422 }
423
424drop_frame:
425 /* Calculate transfer length. */
426 plen = desc->cffrm_len;
427 while (nfrms < CFHSI_MAX_PKTS && *plen) {
428 xfer_sz += *plen;
429 plen++;
430 nfrms++;
431 }
432
433 /* Check for piggy-backed descriptor. */
434 if (desc->header & CFHSI_PIGGY_DESC)
435 xfer_sz += CFHSI_DESC_SZ;
436
437 if (xfer_sz % 4) {
438 dev_err(&cfhsi->ndev->dev,
439 "%s: Invalid payload len: %d, ignored.\n",
440 __func__, xfer_sz);
441 xfer_sz = 0;
442 }
443
444 return xfer_sz;
445}
446
447static int cfhsi_rx_pld(struct cfhsi_desc *desc, struct cfhsi *cfhsi)
448{
449 int rx_sz = 0;
450 int nfrms = 0;
451 u16 *plen = NULL;
452 u8 *pfrm = NULL;
453
454 /* Sanity check header and offset. */
455 if (WARN_ON((desc->header & ~CFHSI_PIGGY_DESC) ||
456 (desc->offset > CFHSI_MAX_EMB_FRM_SZ))) {
457 dev_err(&cfhsi->ndev->dev, "%s: Invalid descriptor.\n",
458 __func__);
459 return -EINVAL;
460 }
461
462 /* Set frame pointer to start of payload. */
463 pfrm = desc->emb_frm + CFHSI_MAX_EMB_FRM_SZ;
464 plen = desc->cffrm_len;
465 while (nfrms < CFHSI_MAX_PKTS && *plen) {
466 struct sk_buff *skb;
467 u8 *dst = NULL;
468 u8 *pcffrm = NULL;
469 int len = 0, retries = 0;
470
471 if (WARN_ON(desc->cffrm_len[nfrms] > CFHSI_MAX_PAYLOAD_SZ)) {
472 dev_err(&cfhsi->ndev->dev, "%s: Invalid payload.\n",
473 __func__);
474 return -EINVAL;
475 }
476
477 /* CAIF frame starts after head padding. */
478 pcffrm = pfrm + *pfrm + 1;
479
480 /* Read length of CAIF frame (little endian). */
481 len = *pcffrm;
482 len |= ((*(pcffrm + 1)) << 8) & 0xFF00;
483 len += 2; /* Add FCS fields. */
484
485 /* Allocate SKB (OK even in IRQ context). */
486 skb = alloc_skb(len + 1, GFP_KERNEL);
487 while (!skb) {
488 retries++;
489 schedule_timeout(1);
490 skb = alloc_skb(len + 1, GFP_KERNEL);
491 if (skb) {
492 printk(KERN_WARNING "%s: slept for %u "
493 "before getting memory\n",
494 __func__, retries);
495 break;
496 }
497 if (retries > HZ) {
498 printk(KERN_ERR "%s: slept for 1HZ "
499 "and did not get memory\n",
500 __func__);
501 cfhsi->ndev->stats.rx_dropped++;
502 goto drop_frame;
503 }
504 }
505 caif_assert(skb != NULL);
506
507 dst = skb_put(skb, len);
508 memcpy(dst, pcffrm, len);
509
510 skb->protocol = htons(ETH_P_CAIF);
511 skb_reset_mac_header(skb);
512 skb->dev = cfhsi->ndev;
513
514 /*
515 * We're called from a platform device,
516 * and don't know the context we're running in.
517 */
518 if (in_interrupt())
519 netif_rx(skb);
520 else
521 netif_rx_ni(skb);
522
523 /* Update network statistics. */
524 cfhsi->ndev->stats.rx_packets++;
525 cfhsi->ndev->stats.rx_bytes += len;
526
527drop_frame:
528 pfrm += *plen;
529 rx_sz += *plen;
530 plen++;
531 nfrms++;
532 }
533
534 return rx_sz;
535}
536
537static void cfhsi_rx_done_work(struct work_struct *work)
538{
539 int res;
540 int desc_pld_len = 0;
541 struct cfhsi *cfhsi = NULL;
542 struct cfhsi_desc *desc = NULL;
543
544 cfhsi = container_of(work, struct cfhsi, rx_done_work);
545 desc = (struct cfhsi_desc *)cfhsi->rx_buf;
546
547 dev_dbg(&cfhsi->ndev->dev, "%s: Kick timer if pending.\n",
548 __func__);
549
550 if (test_bit(CFHSI_SHUTDOWN, &cfhsi->bits))
551 return;
552
553 /* Update inactivity timer if pending. */
Dmitry Tarnyagin73033c92011-10-13 11:29:24 +0000554 spin_lock_bh(&cfhsi->lock);
Dmitry.Tarnyagin40d69042011-06-01 03:29:18 +0000555 mod_timer_pending(&cfhsi->timer, jiffies + CFHSI_INACTIVITY_TOUT);
Dmitry Tarnyagin73033c92011-10-13 11:29:24 +0000556 spin_unlock_bh(&cfhsi->lock);
Dmitry.Tarnyagin40d69042011-06-01 03:29:18 +0000557
558 if (cfhsi->rx_state == CFHSI_RX_STATE_DESC) {
559 desc_pld_len = cfhsi_rx_desc(desc, cfhsi);
560 } else {
561 int pld_len;
562
563 pld_len = cfhsi_rx_pld(desc, cfhsi);
564
565 if ((pld_len > 0) && (desc->header & CFHSI_PIGGY_DESC)) {
566 struct cfhsi_desc *piggy_desc;
567 piggy_desc = (struct cfhsi_desc *)
568 (desc->emb_frm + CFHSI_MAX_EMB_FRM_SZ +
569 pld_len);
570
571 /* Extract piggy-backed descriptor. */
572 desc_pld_len = cfhsi_rx_desc(piggy_desc, cfhsi);
573
574 /*
575 * Copy needed information from the piggy-backed
576 * descriptor to the descriptor in the start.
577 */
578 memcpy((u8 *)desc, (u8 *)piggy_desc,
579 CFHSI_DESC_SHORT_SZ);
580 }
581 }
582
583 if (desc_pld_len) {
584 cfhsi->rx_state = CFHSI_RX_STATE_PAYLOAD;
585 cfhsi->rx_ptr = cfhsi->rx_buf + CFHSI_DESC_SZ;
586 cfhsi->rx_len = desc_pld_len;
587 } else {
588 cfhsi->rx_state = CFHSI_RX_STATE_DESC;
589 cfhsi->rx_ptr = cfhsi->rx_buf;
590 cfhsi->rx_len = CFHSI_DESC_SZ;
591 }
592 clear_bit(CFHSI_PENDING_RX, &cfhsi->bits);
593
594 if (test_bit(CFHSI_AWAKE, &cfhsi->bits)) {
595 /* Set up new transfer. */
596 dev_dbg(&cfhsi->ndev->dev, "%s: Start RX.\n",
597 __func__);
598 res = cfhsi->dev->cfhsi_rx(cfhsi->rx_ptr, cfhsi->rx_len,
599 cfhsi->dev);
600 if (WARN_ON(res < 0)) {
601 dev_err(&cfhsi->ndev->dev, "%s: RX error %d.\n",
602 __func__, res);
603 cfhsi->ndev->stats.rx_errors++;
604 cfhsi->ndev->stats.rx_dropped++;
605 }
606 }
607}
608
609static void cfhsi_rx_done_cb(struct cfhsi_drv *drv)
610{
611 struct cfhsi *cfhsi;
612
613 cfhsi = container_of(drv, struct cfhsi, drv);
614 dev_dbg(&cfhsi->ndev->dev, "%s.\n",
615 __func__);
616
617 if (test_bit(CFHSI_SHUTDOWN, &cfhsi->bits))
618 return;
619
620 set_bit(CFHSI_PENDING_RX, &cfhsi->bits);
621
622 if (test_and_clear_bit(CFHSI_FLUSH_FIFO, &cfhsi->bits))
623 wake_up_interruptible(&cfhsi->flush_fifo_wait);
624 else
625 queue_work(cfhsi->wq, &cfhsi->rx_done_work);
626}
627
628static void cfhsi_wake_up(struct work_struct *work)
629{
630 struct cfhsi *cfhsi = NULL;
631 int res;
632 int len;
633 long ret;
634
635 cfhsi = container_of(work, struct cfhsi, wake_up_work);
636
637 if (test_bit(CFHSI_SHUTDOWN, &cfhsi->bits))
638 return;
639
640 if (unlikely(test_bit(CFHSI_AWAKE, &cfhsi->bits))) {
641 /* It happenes when wakeup is requested by
642 * both ends at the same time. */
643 clear_bit(CFHSI_WAKE_UP, &cfhsi->bits);
644 return;
645 }
646
647 /* Activate wake line. */
648 cfhsi->dev->cfhsi_wake_up(cfhsi->dev);
649
650 dev_dbg(&cfhsi->ndev->dev, "%s: Start waiting.\n",
651 __func__);
652
653 /* Wait for acknowledge. */
654 ret = CFHSI_WAKEUP_TOUT;
655 wait_event_interruptible_timeout(cfhsi->wake_up_wait,
656 test_bit(CFHSI_WAKE_UP_ACK,
657 &cfhsi->bits), ret);
658 if (unlikely(ret < 0)) {
659 /* Interrupted by signal. */
660 dev_info(&cfhsi->ndev->dev, "%s: Signalled: %ld.\n",
661 __func__, ret);
662 clear_bit(CFHSI_WAKE_UP, &cfhsi->bits);
663 cfhsi->dev->cfhsi_wake_down(cfhsi->dev);
664 return;
665 } else if (!ret) {
666 /* Wakeup timeout */
667 dev_err(&cfhsi->ndev->dev, "%s: Timeout.\n",
668 __func__);
669 clear_bit(CFHSI_WAKE_UP, &cfhsi->bits);
670 cfhsi->dev->cfhsi_wake_down(cfhsi->dev);
671 return;
672 }
673 dev_dbg(&cfhsi->ndev->dev, "%s: Woken.\n",
674 __func__);
675
676 /* Clear power up bit. */
677 set_bit(CFHSI_AWAKE, &cfhsi->bits);
678 clear_bit(CFHSI_WAKE_UP, &cfhsi->bits);
679
680 /* Resume read operation. */
681 if (!test_bit(CFHSI_PENDING_RX, &cfhsi->bits)) {
682 dev_dbg(&cfhsi->ndev->dev, "%s: Start RX.\n",
683 __func__);
684 res = cfhsi->dev->cfhsi_rx(cfhsi->rx_ptr,
685 cfhsi->rx_len, cfhsi->dev);
686 if (WARN_ON(res < 0)) {
687 dev_err(&cfhsi->ndev->dev, "%s: RX error %d.\n",
688 __func__, res);
689 }
690 }
691
692 /* Clear power up acknowledment. */
693 clear_bit(CFHSI_WAKE_UP_ACK, &cfhsi->bits);
694
695 spin_lock_bh(&cfhsi->lock);
696
697 /* Resume transmit if queue is not empty. */
698 if (!skb_peek(&cfhsi->qhead)) {
699 dev_dbg(&cfhsi->ndev->dev, "%s: Peer wake, start timer.\n",
700 __func__);
701 /* Start inactivity timer. */
702 mod_timer(&cfhsi->timer,
703 jiffies + CFHSI_INACTIVITY_TOUT);
704 spin_unlock_bh(&cfhsi->lock);
705 return;
706 }
707
708 dev_dbg(&cfhsi->ndev->dev, "%s: Host wake.\n",
709 __func__);
710
711 spin_unlock_bh(&cfhsi->lock);
712
713 /* Create HSI frame. */
714 len = cfhsi_tx_frm((struct cfhsi_desc *)cfhsi->tx_buf, cfhsi);
715
716 if (likely(len > 0)) {
717 /* Set up new transfer. */
718 res = cfhsi->dev->cfhsi_tx(cfhsi->tx_buf, len, cfhsi->dev);
719 if (WARN_ON(res < 0)) {
720 dev_err(&cfhsi->ndev->dev, "%s: TX error %d.\n",
721 __func__, res);
722 cfhsi_abort_tx(cfhsi);
723 }
724 } else {
725 dev_err(&cfhsi->ndev->dev,
726 "%s: Failed to create HSI frame: %d.\n",
727 __func__, len);
728 }
729
730}
731
732static void cfhsi_wake_down(struct work_struct *work)
733{
734 long ret;
735 struct cfhsi *cfhsi = NULL;
736 size_t fifo_occupancy;
737
738 cfhsi = container_of(work, struct cfhsi, wake_down_work);
739 dev_dbg(&cfhsi->ndev->dev, "%s.\n",
740 __func__);
741
742 if (test_bit(CFHSI_SHUTDOWN, &cfhsi->bits))
743 return;
744
745 /* Check if there is something in FIFO. */
746 if (WARN_ON(cfhsi->dev->cfhsi_fifo_occupancy(cfhsi->dev,
747 &fifo_occupancy)))
748 fifo_occupancy = 0;
749
750 if (fifo_occupancy) {
751 dev_dbg(&cfhsi->ndev->dev,
752 "%s: %u words in RX FIFO, restart timer.\n",
753 __func__, (unsigned) fifo_occupancy);
754 spin_lock_bh(&cfhsi->lock);
755 mod_timer(&cfhsi->timer,
756 jiffies + CFHSI_INACTIVITY_TOUT);
757 spin_unlock_bh(&cfhsi->lock);
758 return;
759 }
760
761 /* Cancel pending RX requests */
762 cfhsi->dev->cfhsi_rx_cancel(cfhsi->dev);
763
764 /* Deactivate wake line. */
765 cfhsi->dev->cfhsi_wake_down(cfhsi->dev);
766
767 /* Wait for acknowledge. */
768 ret = CFHSI_WAKEUP_TOUT;
769 ret = wait_event_interruptible_timeout(cfhsi->wake_down_wait,
770 test_bit(CFHSI_WAKE_DOWN_ACK,
771 &cfhsi->bits),
772 ret);
773 if (ret < 0) {
774 /* Interrupted by signal. */
775 dev_info(&cfhsi->ndev->dev, "%s: Signalled: %ld.\n",
776 __func__, ret);
777 return;
778 } else if (!ret) {
779 /* Timeout */
780 dev_err(&cfhsi->ndev->dev, "%s: Timeout.\n",
781 __func__);
782 }
783
784 /* Clear power down acknowledment. */
785 clear_bit(CFHSI_WAKE_DOWN_ACK, &cfhsi->bits);
786 clear_bit(CFHSI_AWAKE, &cfhsi->bits);
787
788 /* Check if there is something in FIFO. */
789 if (WARN_ON(cfhsi->dev->cfhsi_fifo_occupancy(cfhsi->dev,
790 &fifo_occupancy)))
791 fifo_occupancy = 0;
792
793 if (fifo_occupancy) {
794 dev_dbg(&cfhsi->ndev->dev,
795 "%s: %u words in RX FIFO, wakeup forced.\n",
796 __func__, (unsigned) fifo_occupancy);
797 if (!test_and_set_bit(CFHSI_WAKE_UP, &cfhsi->bits))
798 queue_work(cfhsi->wq, &cfhsi->wake_up_work);
799 } else
800 dev_dbg(&cfhsi->ndev->dev, "%s: Done.\n",
801 __func__);
802}
803
804static void cfhsi_wake_up_cb(struct cfhsi_drv *drv)
805{
806 struct cfhsi *cfhsi = NULL;
807
808 cfhsi = container_of(drv, struct cfhsi, drv);
809 dev_dbg(&cfhsi->ndev->dev, "%s.\n",
810 __func__);
811
812 set_bit(CFHSI_WAKE_UP_ACK, &cfhsi->bits);
813 wake_up_interruptible(&cfhsi->wake_up_wait);
814
815 if (test_bit(CFHSI_SHUTDOWN, &cfhsi->bits))
816 return;
817
818 /* Schedule wake up work queue if the peer initiates. */
819 if (!test_and_set_bit(CFHSI_WAKE_UP, &cfhsi->bits))
820 queue_work(cfhsi->wq, &cfhsi->wake_up_work);
821}
822
823static void cfhsi_wake_down_cb(struct cfhsi_drv *drv)
824{
825 struct cfhsi *cfhsi = NULL;
826
827 cfhsi = container_of(drv, struct cfhsi, drv);
828 dev_dbg(&cfhsi->ndev->dev, "%s.\n",
829 __func__);
830
831 /* Initiating low power is only permitted by the host (us). */
832 set_bit(CFHSI_WAKE_DOWN_ACK, &cfhsi->bits);
833 wake_up_interruptible(&cfhsi->wake_down_wait);
834}
835
836static int cfhsi_xmit(struct sk_buff *skb, struct net_device *dev)
837{
838 struct cfhsi *cfhsi = NULL;
839 int start_xfer = 0;
840 int timer_active;
841
842 if (!dev)
843 return -EINVAL;
844
845 cfhsi = netdev_priv(dev);
846
847 spin_lock_bh(&cfhsi->lock);
848
849 skb_queue_tail(&cfhsi->qhead, skb);
850
851 /* Sanity check; xmit should not be called after unregister_netdev */
852 if (WARN_ON(test_bit(CFHSI_SHUTDOWN, &cfhsi->bits))) {
853 spin_unlock_bh(&cfhsi->lock);
854 cfhsi_abort_tx(cfhsi);
855 return -EINVAL;
856 }
857
858 /* Send flow off if number of packets is above high water mark. */
859 if (!cfhsi->flow_off_sent &&
860 cfhsi->qhead.qlen > cfhsi->q_high_mark &&
861 cfhsi->cfdev.flowctrl) {
862 cfhsi->flow_off_sent = 1;
863 cfhsi->cfdev.flowctrl(cfhsi->ndev, OFF);
864 }
865
866 if (cfhsi->tx_state == CFHSI_TX_STATE_IDLE) {
867 cfhsi->tx_state = CFHSI_TX_STATE_XFER;
868 start_xfer = 1;
869 }
870
Dmitry Tarnyagin73033c92011-10-13 11:29:24 +0000871 if (!start_xfer) {
872 spin_unlock_bh(&cfhsi->lock);
Dmitry.Tarnyagin40d69042011-06-01 03:29:18 +0000873 return 0;
Dmitry Tarnyagin73033c92011-10-13 11:29:24 +0000874 }
Dmitry.Tarnyagin40d69042011-06-01 03:29:18 +0000875
876 /* Delete inactivity timer if started. */
877#ifdef CONFIG_SMP
878 timer_active = del_timer_sync(&cfhsi->timer);
879#else
880 timer_active = del_timer(&cfhsi->timer);
881#endif /* CONFIG_SMP */
882
Dmitry Tarnyagin73033c92011-10-13 11:29:24 +0000883 spin_unlock_bh(&cfhsi->lock);
884
Dmitry.Tarnyagin40d69042011-06-01 03:29:18 +0000885 if (timer_active) {
886 struct cfhsi_desc *desc = (struct cfhsi_desc *)cfhsi->tx_buf;
887 int len;
888 int res;
889
890 /* Create HSI frame. */
891 len = cfhsi_tx_frm(desc, cfhsi);
892 BUG_ON(!len);
893
894 /* Set up new transfer. */
895 res = cfhsi->dev->cfhsi_tx(cfhsi->tx_buf, len, cfhsi->dev);
896 if (WARN_ON(res < 0)) {
897 dev_err(&cfhsi->ndev->dev, "%s: TX error %d.\n",
898 __func__, res);
899 cfhsi_abort_tx(cfhsi);
900 }
901 } else {
902 /* Schedule wake up work queue if the we initiate. */
903 if (!test_and_set_bit(CFHSI_WAKE_UP, &cfhsi->bits))
904 queue_work(cfhsi->wq, &cfhsi->wake_up_work);
905 }
906
907 return 0;
908}
909
910static int cfhsi_open(struct net_device *dev)
911{
912 netif_wake_queue(dev);
913
914 return 0;
915}
916
917static int cfhsi_close(struct net_device *dev)
918{
919 netif_stop_queue(dev);
920
921 return 0;
922}
923
924static const struct net_device_ops cfhsi_ops = {
925 .ndo_open = cfhsi_open,
926 .ndo_stop = cfhsi_close,
927 .ndo_start_xmit = cfhsi_xmit
928};
929
930static void cfhsi_setup(struct net_device *dev)
931{
932 struct cfhsi *cfhsi = netdev_priv(dev);
933 dev->features = 0;
934 dev->netdev_ops = &cfhsi_ops;
935 dev->type = ARPHRD_CAIF;
936 dev->flags = IFF_POINTOPOINT | IFF_NOARP;
937 dev->mtu = CFHSI_MAX_PAYLOAD_SZ;
938 dev->tx_queue_len = 0;
939 dev->destructor = free_netdev;
940 skb_queue_head_init(&cfhsi->qhead);
941 cfhsi->cfdev.link_select = CAIF_LINK_HIGH_BANDW;
942 cfhsi->cfdev.use_frag = false;
943 cfhsi->cfdev.use_stx = false;
944 cfhsi->cfdev.use_fcs = false;
945 cfhsi->ndev = dev;
946}
947
948int cfhsi_probe(struct platform_device *pdev)
949{
950 struct cfhsi *cfhsi = NULL;
951 struct net_device *ndev;
952 struct cfhsi_dev *dev;
953 int res;
954
955 ndev = alloc_netdev(sizeof(struct cfhsi), "cfhsi%d", cfhsi_setup);
Joe Perches7ac2ed02011-08-25 13:22:24 +0000956 if (!ndev)
Dmitry.Tarnyagin40d69042011-06-01 03:29:18 +0000957 return -ENODEV;
Dmitry.Tarnyagin40d69042011-06-01 03:29:18 +0000958
959 cfhsi = netdev_priv(ndev);
960 cfhsi->ndev = ndev;
961 cfhsi->pdev = pdev;
962
963 /* Initialize state vaiables. */
964 cfhsi->tx_state = CFHSI_TX_STATE_IDLE;
965 cfhsi->rx_state = CFHSI_RX_STATE_DESC;
966
967 /* Set flow info */
968 cfhsi->flow_off_sent = 0;
969 cfhsi->q_low_mark = LOW_WATER_MARK;
970 cfhsi->q_high_mark = HIGH_WATER_MARK;
971
972 /* Assign the HSI device. */
973 dev = (struct cfhsi_dev *)pdev->dev.platform_data;
974 cfhsi->dev = dev;
975
976 /* Assign the driver to this HSI device. */
977 dev->drv = &cfhsi->drv;
978
979 /*
980 * Allocate a TX buffer with the size of a HSI packet descriptors
981 * and the necessary room for CAIF payload frames.
982 */
983 cfhsi->tx_buf = kzalloc(CFHSI_BUF_SZ_TX, GFP_KERNEL);
984 if (!cfhsi->tx_buf) {
Dmitry.Tarnyagin40d69042011-06-01 03:29:18 +0000985 res = -ENODEV;
986 goto err_alloc_tx;
987 }
988
989 /*
990 * Allocate a RX buffer with the size of two HSI packet descriptors and
991 * the necessary room for CAIF payload frames.
992 */
993 cfhsi->rx_buf = kzalloc(CFHSI_BUF_SZ_RX, GFP_KERNEL);
994 if (!cfhsi->rx_buf) {
Dmitry.Tarnyagin40d69042011-06-01 03:29:18 +0000995 res = -ENODEV;
996 goto err_alloc_rx;
997 }
998
Joe Perches864834f2011-06-29 05:52:03 -0700999 /* Initialize receive variables. */
Dmitry.Tarnyagin40d69042011-06-01 03:29:18 +00001000 cfhsi->rx_ptr = cfhsi->rx_buf;
1001 cfhsi->rx_len = CFHSI_DESC_SZ;
1002
1003 /* Initialize spin locks. */
1004 spin_lock_init(&cfhsi->lock);
1005
1006 /* Set up the driver. */
1007 cfhsi->drv.tx_done_cb = cfhsi_tx_done_cb;
1008 cfhsi->drv.rx_done_cb = cfhsi_rx_done_cb;
sjur.brandeland@stericsson.com94230fe2011-10-13 11:29:22 +00001009 cfhsi->drv.wake_up_cb = cfhsi_wake_up_cb;
1010 cfhsi->drv.wake_down_cb = cfhsi_wake_down_cb;
Dmitry.Tarnyagin40d69042011-06-01 03:29:18 +00001011
1012 /* Initialize the work queues. */
1013 INIT_WORK(&cfhsi->wake_up_work, cfhsi_wake_up);
1014 INIT_WORK(&cfhsi->wake_down_work, cfhsi_wake_down);
1015 INIT_WORK(&cfhsi->rx_done_work, cfhsi_rx_done_work);
1016 INIT_WORK(&cfhsi->tx_done_work, cfhsi_tx_done_work);
1017
1018 /* Clear all bit fields. */
1019 clear_bit(CFHSI_WAKE_UP_ACK, &cfhsi->bits);
1020 clear_bit(CFHSI_WAKE_DOWN_ACK, &cfhsi->bits);
1021 clear_bit(CFHSI_WAKE_UP, &cfhsi->bits);
1022 clear_bit(CFHSI_AWAKE, &cfhsi->bits);
1023 clear_bit(CFHSI_PENDING_RX, &cfhsi->bits);
1024
1025 /* Create work thread. */
1026 cfhsi->wq = create_singlethread_workqueue(pdev->name);
1027 if (!cfhsi->wq) {
1028 dev_err(&ndev->dev, "%s: Failed to create work queue.\n",
1029 __func__);
1030 res = -ENODEV;
1031 goto err_create_wq;
1032 }
1033
1034 /* Initialize wait queues. */
1035 init_waitqueue_head(&cfhsi->wake_up_wait);
1036 init_waitqueue_head(&cfhsi->wake_down_wait);
1037 init_waitqueue_head(&cfhsi->flush_fifo_wait);
1038
1039 /* Setup the inactivity timer. */
1040 init_timer(&cfhsi->timer);
1041 cfhsi->timer.data = (unsigned long)cfhsi;
1042 cfhsi->timer.function = cfhsi_inactivity_tout;
1043
1044 /* Add CAIF HSI device to list. */
1045 spin_lock(&cfhsi_list_lock);
1046 list_add_tail(&cfhsi->list, &cfhsi_list);
1047 spin_unlock(&cfhsi_list_lock);
1048
1049 /* Activate HSI interface. */
1050 res = cfhsi->dev->cfhsi_up(cfhsi->dev);
1051 if (res) {
1052 dev_err(&cfhsi->ndev->dev,
1053 "%s: can't activate HSI interface: %d.\n",
1054 __func__, res);
1055 goto err_activate;
1056 }
1057
1058 /* Flush FIFO */
1059 res = cfhsi_flush_fifo(cfhsi);
1060 if (res) {
1061 dev_err(&ndev->dev, "%s: Can't flush FIFO: %d.\n",
1062 __func__, res);
1063 goto err_net_reg;
1064 }
1065
Dmitry.Tarnyagin40d69042011-06-01 03:29:18 +00001066 /* Register network device. */
1067 res = register_netdev(ndev);
1068 if (res) {
1069 dev_err(&ndev->dev, "%s: Registration error: %d.\n",
1070 __func__, res);
1071 goto err_net_reg;
1072 }
1073
1074 netif_stop_queue(ndev);
1075
1076 return res;
1077
1078 err_net_reg:
1079 cfhsi->dev->cfhsi_down(cfhsi->dev);
1080 err_activate:
1081 destroy_workqueue(cfhsi->wq);
1082 err_create_wq:
1083 kfree(cfhsi->rx_buf);
1084 err_alloc_rx:
1085 kfree(cfhsi->tx_buf);
1086 err_alloc_tx:
1087 free_netdev(ndev);
1088
1089 return res;
1090}
1091
1092static void cfhsi_shutdown(struct cfhsi *cfhsi, bool remove_platform_dev)
1093{
1094 u8 *tx_buf, *rx_buf;
1095
1096 /* Stop TXing */
1097 netif_tx_stop_all_queues(cfhsi->ndev);
1098
1099 /* going to shutdown driver */
1100 set_bit(CFHSI_SHUTDOWN, &cfhsi->bits);
1101
1102 if (remove_platform_dev) {
1103 /* Flush workqueue */
1104 flush_workqueue(cfhsi->wq);
1105
1106 /* Notify device. */
1107 platform_device_unregister(cfhsi->pdev);
1108 }
1109
1110 /* Flush workqueue */
1111 flush_workqueue(cfhsi->wq);
1112
1113 /* Delete timer if pending */
1114#ifdef CONFIG_SMP
1115 del_timer_sync(&cfhsi->timer);
1116#else
1117 del_timer(&cfhsi->timer);
1118#endif /* CONFIG_SMP */
1119
1120 /* Cancel pending RX request (if any) */
1121 cfhsi->dev->cfhsi_rx_cancel(cfhsi->dev);
1122
1123 /* Flush again and destroy workqueue */
1124 destroy_workqueue(cfhsi->wq);
1125
1126 /* Store bufferes: will be freed later. */
1127 tx_buf = cfhsi->tx_buf;
1128 rx_buf = cfhsi->rx_buf;
1129
1130 /* Flush transmit queues. */
1131 cfhsi_abort_tx(cfhsi);
1132
1133 /* Deactivate interface */
1134 cfhsi->dev->cfhsi_down(cfhsi->dev);
1135
1136 /* Finally unregister the network device. */
1137 unregister_netdev(cfhsi->ndev);
1138
1139 /* Free buffers. */
1140 kfree(tx_buf);
1141 kfree(rx_buf);
1142}
1143
1144int cfhsi_remove(struct platform_device *pdev)
1145{
1146 struct list_head *list_node;
1147 struct list_head *n;
1148 struct cfhsi *cfhsi = NULL;
1149 struct cfhsi_dev *dev;
1150
1151 dev = (struct cfhsi_dev *)pdev->dev.platform_data;
1152 spin_lock(&cfhsi_list_lock);
1153 list_for_each_safe(list_node, n, &cfhsi_list) {
1154 cfhsi = list_entry(list_node, struct cfhsi, list);
1155 /* Find the corresponding device. */
1156 if (cfhsi->dev == dev) {
1157 /* Remove from list. */
1158 list_del(list_node);
1159 spin_unlock(&cfhsi_list_lock);
1160
1161 /* Shutdown driver. */
1162 cfhsi_shutdown(cfhsi, false);
1163
1164 return 0;
1165 }
1166 }
1167 spin_unlock(&cfhsi_list_lock);
1168 return -ENODEV;
1169}
1170
1171struct platform_driver cfhsi_plat_drv = {
1172 .probe = cfhsi_probe,
1173 .remove = cfhsi_remove,
1174 .driver = {
1175 .name = "cfhsi",
1176 .owner = THIS_MODULE,
1177 },
1178};
1179
1180static void __exit cfhsi_exit_module(void)
1181{
1182 struct list_head *list_node;
1183 struct list_head *n;
1184 struct cfhsi *cfhsi = NULL;
1185
1186 spin_lock(&cfhsi_list_lock);
1187 list_for_each_safe(list_node, n, &cfhsi_list) {
1188 cfhsi = list_entry(list_node, struct cfhsi, list);
1189
1190 /* Remove from list. */
1191 list_del(list_node);
1192 spin_unlock(&cfhsi_list_lock);
1193
1194 /* Shutdown driver. */
1195 cfhsi_shutdown(cfhsi, true);
1196
1197 spin_lock(&cfhsi_list_lock);
1198 }
1199 spin_unlock(&cfhsi_list_lock);
1200
1201 /* Unregister platform driver. */
1202 platform_driver_unregister(&cfhsi_plat_drv);
1203}
1204
1205static int __init cfhsi_init_module(void)
1206{
1207 int result;
1208
1209 /* Initialize spin lock. */
1210 spin_lock_init(&cfhsi_list_lock);
1211
1212 /* Register platform driver. */
1213 result = platform_driver_register(&cfhsi_plat_drv);
1214 if (result) {
1215 printk(KERN_ERR "Could not register platform HSI driver: %d.\n",
1216 result);
1217 goto err_dev_register;
1218 }
1219
1220 return result;
1221
1222 err_dev_register:
1223 return result;
1224}
1225
1226module_init(cfhsi_init_module);
1227module_exit(cfhsi_exit_module);