caif-hsi: Fix for wakeup condition problem
[linux-2.6/btrfs-unstable.git] / drivers / net / caif / caif_hsi.c
blob82c4d6ca2d3f919b93ca1e2bbe967074467b25c2
1 /*
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 */
9 #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>
24 MODULE_LICENSE("GPL");
25 MODULE_AUTHOR("Daniel Martensson<daniel.martensson@stericsson.com>");
26 MODULE_DESCRIPTION("CAIF HSI driver");
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)))))
33 * HSI padding options.
34 * Warning: must be a base of 2 (& operation used) and can not be zero !
36 static int hsi_head_align = 4;
37 module_param(hsi_head_align, int, S_IRUGO);
38 MODULE_PARM_DESC(hsi_head_align, "HSI head alignment.");
40 static int hsi_tail_align = 4;
41 module_param(hsi_tail_align, int, S_IRUGO);
42 MODULE_PARM_DESC(hsi_tail_align, "HSI tail alignment.");
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.
50 static int hsi_high_threshold = 100;
51 module_param(hsi_high_threshold, int, S_IRUGO);
52 MODULE_PARM_DESC(hsi_high_threshold, "HSI high threshold (FLOW OFF).");
54 static int hsi_low_threshold = 50;
55 module_param(hsi_low_threshold, int, S_IRUGO);
56 MODULE_PARM_DESC(hsi_low_threshold, "HSI high threshold (FLOW ON).");
58 #define ON 1
59 #define OFF 0
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.
66 #define LOW_WATER_MARK hsi_low_threshold
67 #define HIGH_WATER_MARK hsi_high_threshold
69 static LIST_HEAD(cfhsi_list);
70 static spinlock_t cfhsi_list_lock;
72 static void cfhsi_inactivity_tout(unsigned long arg)
74 struct cfhsi *cfhsi = (struct cfhsi *)arg;
76 dev_dbg(&cfhsi->ndev->dev, "%s.\n",
77 __func__);
79 /* Schedule power down work queue. */
80 if (!test_bit(CFHSI_SHUTDOWN, &cfhsi->bits))
81 queue_work(cfhsi->wq, &cfhsi->wake_down_work);
84 static void cfhsi_abort_tx(struct cfhsi *cfhsi)
86 struct sk_buff *skb;
88 for (;;) {
89 spin_lock_bh(&cfhsi->lock);
90 skb = skb_dequeue(&cfhsi->qhead);
91 if (!skb)
92 break;
94 cfhsi->ndev->stats.tx_errors++;
95 cfhsi->ndev->stats.tx_dropped++;
96 spin_unlock_bh(&cfhsi->lock);
97 kfree_skb(skb);
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);
105 static int cfhsi_flush_fifo(struct cfhsi *cfhsi)
107 char buffer[32]; /* Any reasonable value */
108 size_t fifo_occupancy;
109 int ret;
111 dev_dbg(&cfhsi->ndev->dev, "%s.\n",
112 __func__);
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;
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;
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;
147 ret = 5 * HZ;
148 wait_event_interruptible_timeout(cfhsi->flush_fifo_wait,
149 !test_bit(CFHSI_FLUSH_FIFO, &cfhsi->bits), ret);
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;
163 } while (1);
165 cfhsi->dev->cfhsi_wake_down(cfhsi->dev);
167 return ret;
170 static int cfhsi_tx_frm(struct cfhsi_desc *desc, struct cfhsi *cfhsi)
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;
177 skb = skb_dequeue(&cfhsi->qhead);
178 if (!skb)
179 return 0;
181 /* Clear offset. */
182 desc->offset = 0;
184 /* 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;
190 /* Calculate needed head alignment and tail alignment. */
191 info = (struct caif_payload_info *)&skb->cb;
193 hpad = 1 + PAD_POW2((info->hdr_len + 1), hsi_head_align);
194 tpad = PAD_POW2((skb->len + hpad), hsi_tail_align);
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;
203 /* Update network statistics. */
204 cfhsi->ndev->stats.tx_packets++;
205 cfhsi->ndev->stats.tx_bytes += skb->len;
207 /* Copy in embedded CAIF frame. */
208 skb_copy_bits(skb, 0, pemb, skb->len);
209 consume_skb(skb);
210 skb = NULL;
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;
221 if (!skb)
222 skb = skb_dequeue(&cfhsi->qhead);
224 if (!skb)
225 break;
227 /* Calculate needed head alignment and tail alignment. */
228 info = (struct caif_payload_info *)&skb->cb;
230 hpad = 1 + PAD_POW2((info->hdr_len + 1), hsi_head_align);
231 tpad = PAD_POW2((skb->len + hpad), hsi_tail_align);
233 /* Fill in CAIF frame length in descriptor. */
234 desc->cffrm_len[nfrms] = hpad + skb->len + tpad;
236 /* Fill head padding information. */
237 *pfrm = (u8)(hpad - 1);
238 pfrm += hpad;
240 /* Update network statistics. */
241 cfhsi->ndev->stats.tx_packets++;
242 cfhsi->ndev->stats.tx_bytes += skb->len;
244 /* Copy in CAIF frame. */
245 skb_copy_bits(skb, 0, pfrm, skb->len);
247 /* Update payload length. */
248 pld_len += desc->cffrm_len[nfrms];
250 /* Update frame pointer. */
251 pfrm += skb->len + tpad;
252 consume_skb(skb);
253 skb = NULL;
255 /* Update number of frames. */
256 nfrms++;
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++;
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;
272 return CFHSI_DESC_SZ + pld_len;
275 static void cfhsi_tx_done_work(struct work_struct *work)
277 struct cfhsi *cfhsi = NULL;
278 struct cfhsi_desc *desc = NULL;
279 int len = 0;
280 int res;
282 cfhsi = container_of(work, struct cfhsi, tx_done_work);
283 dev_dbg(&cfhsi->ndev->dev, "%s.\n",
284 __func__);
286 if (test_bit(CFHSI_SHUTDOWN, &cfhsi->bits))
287 return;
289 desc = (struct cfhsi_desc *)cfhsi->tx_buf;
291 do {
293 * Send flow on if flow off has been previously signalled
294 * and number of packets is below low water mark.
296 spin_lock_bh(&cfhsi->lock);
297 if (cfhsi->flow_off_sent &&
298 cfhsi->qhead.qlen <= cfhsi->q_low_mark &&
299 cfhsi->cfdev.flowctrl) {
301 cfhsi->flow_off_sent = 0;
302 cfhsi->cfdev.flowctrl(cfhsi->ndev, ON);
304 spin_unlock_bh(&cfhsi->lock);
306 /* Create HSI frame. */
307 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;
315 cfhsi->tx_state = CFHSI_TX_STATE_IDLE;
316 /* Start inactivity timer. */
317 mod_timer(&cfhsi->timer,
318 jiffies + CFHSI_INACTIVITY_TOUT);
319 spin_unlock_bh(&cfhsi->lock);
320 goto done;
322 } while (!len);
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);
330 } while (res < 0);
332 done:
333 return;
336 static void cfhsi_tx_done_cb(struct cfhsi_drv *drv)
338 struct cfhsi *cfhsi;
340 cfhsi = container_of(drv, struct cfhsi, drv);
341 dev_dbg(&cfhsi->ndev->dev, "%s.\n",
342 __func__);
344 if (test_bit(CFHSI_SHUTDOWN, &cfhsi->bits))
345 return;
347 queue_work(cfhsi->wq, &cfhsi->tx_done_work);
350 static int cfhsi_rx_desc(struct cfhsi_desc *desc, struct cfhsi *cfhsi)
352 int xfer_sz = 0;
353 int nfrms = 0;
354 u16 *plen = NULL;
355 u8 *pfrm = NULL;
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;
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;
371 /* Remove offset padding. */
372 pfrm += *pfrm + 1;
374 /* Read length of CAIF frame (little endian). */
375 len = *pfrm;
376 len |= ((*(pfrm+1)) << 8) & 0xFF00;
377 len += 2; /* Add FCS fields. */
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;
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;
400 caif_assert(skb != NULL);
402 dst = skb_put(skb, len);
403 memcpy(dst, pfrm, len);
405 skb->protocol = htons(ETH_P_CAIF);
406 skb_reset_mac_header(skb);
407 skb->dev = cfhsi->ndev;
410 * We are called from a arch specific platform device.
411 * Unfortunately we don't know what context we're
412 * running in.
414 if (in_interrupt())
415 netif_rx(skb);
416 else
417 netif_rx_ni(skb);
419 /* Update network statistics. */
420 cfhsi->ndev->stats.rx_packets++;
421 cfhsi->ndev->stats.rx_bytes += len;
424 drop_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++;
433 /* Check for piggy-backed descriptor. */
434 if (desc->header & CFHSI_PIGGY_DESC)
435 xfer_sz += CFHSI_DESC_SZ;
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;
444 return xfer_sz;
447 static int cfhsi_rx_pld(struct cfhsi_desc *desc, struct cfhsi *cfhsi)
449 int rx_sz = 0;
450 int nfrms = 0;
451 u16 *plen = NULL;
452 u8 *pfrm = NULL;
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;
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;
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;
477 /* CAIF frame starts after head padding. */
478 pcffrm = pfrm + *pfrm + 1;
480 /* Read length of CAIF frame (little endian). */
481 len = *pcffrm;
482 len |= ((*(pcffrm + 1)) << 8) & 0xFF00;
483 len += 2; /* Add FCS fields. */
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;
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;
505 caif_assert(skb != NULL);
507 dst = skb_put(skb, len);
508 memcpy(dst, pcffrm, len);
510 skb->protocol = htons(ETH_P_CAIF);
511 skb_reset_mac_header(skb);
512 skb->dev = cfhsi->ndev;
515 * We're called from a platform device,
516 * and don't know the context we're running in.
518 if (in_interrupt())
519 netif_rx(skb);
520 else
521 netif_rx_ni(skb);
523 /* Update network statistics. */
524 cfhsi->ndev->stats.rx_packets++;
525 cfhsi->ndev->stats.rx_bytes += len;
527 drop_frame:
528 pfrm += *plen;
529 rx_sz += *plen;
530 plen++;
531 nfrms++;
534 return rx_sz;
537 static void cfhsi_rx_done_work(struct work_struct *work)
539 int res;
540 int desc_pld_len = 0;
541 struct cfhsi *cfhsi = NULL;
542 struct cfhsi_desc *desc = NULL;
544 cfhsi = container_of(work, struct cfhsi, rx_done_work);
545 desc = (struct cfhsi_desc *)cfhsi->rx_buf;
547 dev_dbg(&cfhsi->ndev->dev, "%s: Kick timer if pending.\n",
548 __func__);
550 if (test_bit(CFHSI_SHUTDOWN, &cfhsi->bits))
551 return;
553 /* Update inactivity timer if pending. */
554 spin_lock_bh(&cfhsi->lock);
555 mod_timer_pending(&cfhsi->timer, jiffies + CFHSI_INACTIVITY_TOUT);
556 spin_unlock_bh(&cfhsi->lock);
558 if (cfhsi->rx_state == CFHSI_RX_STATE_DESC) {
559 desc_pld_len = cfhsi_rx_desc(desc, cfhsi);
560 } else {
561 int pld_len;
563 pld_len = cfhsi_rx_pld(desc, cfhsi);
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);
571 /* Extract piggy-backed descriptor. */
572 desc_pld_len = cfhsi_rx_desc(piggy_desc, cfhsi);
575 * Copy needed information from the piggy-backed
576 * descriptor to the descriptor in the start.
578 memcpy((u8 *)desc, (u8 *)piggy_desc,
579 CFHSI_DESC_SHORT_SZ);
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;
592 clear_bit(CFHSI_PENDING_RX, &cfhsi->bits);
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++;
609 static void cfhsi_rx_done_cb(struct cfhsi_drv *drv)
611 struct cfhsi *cfhsi;
613 cfhsi = container_of(drv, struct cfhsi, drv);
614 dev_dbg(&cfhsi->ndev->dev, "%s.\n",
615 __func__);
617 if (test_bit(CFHSI_SHUTDOWN, &cfhsi->bits))
618 return;
620 set_bit(CFHSI_PENDING_RX, &cfhsi->bits);
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);
628 static void cfhsi_wake_up(struct work_struct *work)
630 struct cfhsi *cfhsi = NULL;
631 int res;
632 int len;
633 long ret;
635 cfhsi = container_of(work, struct cfhsi, wake_up_work);
637 if (test_bit(CFHSI_SHUTDOWN, &cfhsi->bits))
638 return;
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;
647 /* Activate wake line. */
648 cfhsi->dev->cfhsi_wake_up(cfhsi->dev);
650 dev_dbg(&cfhsi->ndev->dev, "%s: Start waiting.\n",
651 __func__);
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;
673 dev_dbg(&cfhsi->ndev->dev, "%s: Woken.\n",
674 __func__);
676 /* Clear power up bit. */
677 set_bit(CFHSI_AWAKE, &cfhsi->bits);
678 clear_bit(CFHSI_WAKE_UP, &cfhsi->bits);
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);
692 /* Clear power up acknowledment. */
693 clear_bit(CFHSI_WAKE_UP_ACK, &cfhsi->bits);
695 spin_lock_bh(&cfhsi->lock);
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;
708 dev_dbg(&cfhsi->ndev->dev, "%s: Host wake.\n",
709 __func__);
711 spin_unlock_bh(&cfhsi->lock);
713 /* Create HSI frame. */
714 len = cfhsi_tx_frm((struct cfhsi_desc *)cfhsi->tx_buf, cfhsi);
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);
724 } else {
725 dev_err(&cfhsi->ndev->dev,
726 "%s: Failed to create HSI frame: %d.\n",
727 __func__, len);
732 static void cfhsi_wake_down(struct work_struct *work)
734 long ret;
735 struct cfhsi *cfhsi = NULL;
736 size_t fifo_occupancy;
738 cfhsi = container_of(work, struct cfhsi, wake_down_work);
739 dev_dbg(&cfhsi->ndev->dev, "%s.\n",
740 __func__);
742 if (test_bit(CFHSI_SHUTDOWN, &cfhsi->bits))
743 return;
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;
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;
761 /* Cancel pending RX requests */
762 cfhsi->dev->cfhsi_rx_cancel(cfhsi->dev);
764 /* Deactivate wake line. */
765 cfhsi->dev->cfhsi_wake_down(cfhsi->dev);
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__);
784 /* Clear power down acknowledment. */
785 clear_bit(CFHSI_WAKE_DOWN_ACK, &cfhsi->bits);
786 clear_bit(CFHSI_AWAKE, &cfhsi->bits);
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;
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__);
804 static void cfhsi_wake_up_cb(struct cfhsi_drv *drv)
806 struct cfhsi *cfhsi = NULL;
808 cfhsi = container_of(drv, struct cfhsi, drv);
809 dev_dbg(&cfhsi->ndev->dev, "%s.\n",
810 __func__);
812 set_bit(CFHSI_WAKE_UP_ACK, &cfhsi->bits);
813 wake_up_interruptible(&cfhsi->wake_up_wait);
815 if (test_bit(CFHSI_SHUTDOWN, &cfhsi->bits))
816 return;
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);
823 static void cfhsi_wake_down_cb(struct cfhsi_drv *drv)
825 struct cfhsi *cfhsi = NULL;
827 cfhsi = container_of(drv, struct cfhsi, drv);
828 dev_dbg(&cfhsi->ndev->dev, "%s.\n",
829 __func__);
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);
836 static int cfhsi_xmit(struct sk_buff *skb, struct net_device *dev)
838 struct cfhsi *cfhsi = NULL;
839 int start_xfer = 0;
840 int timer_active;
842 if (!dev)
843 return -EINVAL;
845 cfhsi = netdev_priv(dev);
847 spin_lock_bh(&cfhsi->lock);
849 skb_queue_tail(&cfhsi->qhead, skb);
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;
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);
866 if (cfhsi->tx_state == CFHSI_TX_STATE_IDLE) {
867 cfhsi->tx_state = CFHSI_TX_STATE_XFER;
868 start_xfer = 1;
871 if (!start_xfer) {
872 spin_unlock_bh(&cfhsi->lock);
873 return 0;
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 */
883 spin_unlock_bh(&cfhsi->lock);
885 if (timer_active) {
886 struct cfhsi_desc *desc = (struct cfhsi_desc *)cfhsi->tx_buf;
887 int len;
888 int res;
890 /* Create HSI frame. */
891 len = cfhsi_tx_frm(desc, cfhsi);
892 BUG_ON(!len);
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);
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);
907 return 0;
910 static int cfhsi_open(struct net_device *dev)
912 netif_wake_queue(dev);
914 return 0;
917 static int cfhsi_close(struct net_device *dev)
919 netif_stop_queue(dev);
921 return 0;
924 static const struct net_device_ops cfhsi_ops = {
925 .ndo_open = cfhsi_open,
926 .ndo_stop = cfhsi_close,
927 .ndo_start_xmit = cfhsi_xmit
930 static void cfhsi_setup(struct net_device *dev)
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;
948 int cfhsi_probe(struct platform_device *pdev)
950 struct cfhsi *cfhsi = NULL;
951 struct net_device *ndev;
952 struct cfhsi_dev *dev;
953 int res;
955 ndev = alloc_netdev(sizeof(struct cfhsi), "cfhsi%d", cfhsi_setup);
956 if (!ndev)
957 return -ENODEV;
959 cfhsi = netdev_priv(ndev);
960 cfhsi->ndev = ndev;
961 cfhsi->pdev = pdev;
963 /* Initialize state vaiables. */
964 cfhsi->tx_state = CFHSI_TX_STATE_IDLE;
965 cfhsi->rx_state = CFHSI_RX_STATE_DESC;
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;
972 /* Assign the HSI device. */
973 dev = (struct cfhsi_dev *)pdev->dev.platform_data;
974 cfhsi->dev = dev;
976 /* Assign the driver to this HSI device. */
977 dev->drv = &cfhsi->drv;
980 * Allocate a TX buffer with the size of a HSI packet descriptors
981 * and the necessary room for CAIF payload frames.
983 cfhsi->tx_buf = kzalloc(CFHSI_BUF_SZ_TX, GFP_KERNEL);
984 if (!cfhsi->tx_buf) {
985 res = -ENODEV;
986 goto err_alloc_tx;
990 * Allocate a RX buffer with the size of two HSI packet descriptors and
991 * the necessary room for CAIF payload frames.
993 cfhsi->rx_buf = kzalloc(CFHSI_BUF_SZ_RX, GFP_KERNEL);
994 if (!cfhsi->rx_buf) {
995 res = -ENODEV;
996 goto err_alloc_rx;
999 /* Initialize receive variables. */
1000 cfhsi->rx_ptr = cfhsi->rx_buf;
1001 cfhsi->rx_len = CFHSI_DESC_SZ;
1003 /* Initialize spin locks. */
1004 spin_lock_init(&cfhsi->lock);
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;
1009 cfhsi->drv.wake_up_cb = cfhsi_wake_up_cb;
1010 cfhsi->drv.wake_down_cb = cfhsi_wake_down_cb;
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);
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);
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;
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);
1039 /* Setup the inactivity timer. */
1040 init_timer(&cfhsi->timer);
1041 cfhsi->timer.data = (unsigned long)cfhsi;
1042 cfhsi->timer.function = cfhsi_inactivity_tout;
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);
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;
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;
1066 /* 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;
1074 netif_stop_queue(ndev);
1076 return res;
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);
1089 return res;
1092 static void cfhsi_shutdown(struct cfhsi *cfhsi, bool remove_platform_dev)
1094 u8 *tx_buf, *rx_buf;
1096 /* Stop TXing */
1097 netif_tx_stop_all_queues(cfhsi->ndev);
1099 /* going to shutdown driver */
1100 set_bit(CFHSI_SHUTDOWN, &cfhsi->bits);
1102 if (remove_platform_dev) {
1103 /* Flush workqueue */
1104 flush_workqueue(cfhsi->wq);
1106 /* Notify device. */
1107 platform_device_unregister(cfhsi->pdev);
1110 /* Flush workqueue */
1111 flush_workqueue(cfhsi->wq);
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 */
1120 /* Cancel pending RX request (if any) */
1121 cfhsi->dev->cfhsi_rx_cancel(cfhsi->dev);
1123 /* Flush again and destroy workqueue */
1124 destroy_workqueue(cfhsi->wq);
1126 /* Store bufferes: will be freed later. */
1127 tx_buf = cfhsi->tx_buf;
1128 rx_buf = cfhsi->rx_buf;
1130 /* Flush transmit queues. */
1131 cfhsi_abort_tx(cfhsi);
1133 /* Deactivate interface */
1134 cfhsi->dev->cfhsi_down(cfhsi->dev);
1136 /* Finally unregister the network device. */
1137 unregister_netdev(cfhsi->ndev);
1139 /* Free buffers. */
1140 kfree(tx_buf);
1141 kfree(rx_buf);
1144 int cfhsi_remove(struct platform_device *pdev)
1146 struct list_head *list_node;
1147 struct list_head *n;
1148 struct cfhsi *cfhsi = NULL;
1149 struct cfhsi_dev *dev;
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);
1161 /* Shutdown driver. */
1162 cfhsi_shutdown(cfhsi, false);
1164 return 0;
1167 spin_unlock(&cfhsi_list_lock);
1168 return -ENODEV;
1171 struct platform_driver cfhsi_plat_drv = {
1172 .probe = cfhsi_probe,
1173 .remove = cfhsi_remove,
1174 .driver = {
1175 .name = "cfhsi",
1176 .owner = THIS_MODULE,
1180 static void __exit cfhsi_exit_module(void)
1182 struct list_head *list_node;
1183 struct list_head *n;
1184 struct cfhsi *cfhsi = NULL;
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);
1190 /* Remove from list. */
1191 list_del(list_node);
1192 spin_unlock(&cfhsi_list_lock);
1194 /* Shutdown driver. */
1195 cfhsi_shutdown(cfhsi, true);
1197 spin_lock(&cfhsi_list_lock);
1199 spin_unlock(&cfhsi_list_lock);
1201 /* Unregister platform driver. */
1202 platform_driver_unregister(&cfhsi_plat_drv);
1205 static int __init cfhsi_init_module(void)
1207 int result;
1209 /* Initialize spin lock. */
1210 spin_lock_init(&cfhsi_list_lock);
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;
1220 return result;
1222 err_dev_register:
1223 return result;
1226 module_init(cfhsi_init_module);
1227 module_exit(cfhsi_exit_module);